Merge branch 'feature/mqtt_component' into 'master'
added tuan's mqtt library into idf See merge request idf/esp-idf!2851
This commit is contained in:
@@ -1,14 +1,11 @@
|
||||
set(COMPONENT_SRCS "esp_http_client.c"
|
||||
"lib/http_auth.c"
|
||||
"lib/http_header.c"
|
||||
"lib/http_utils.c"
|
||||
"lib/transport.c"
|
||||
"lib/transport_ssl.c"
|
||||
"lib/transport_tcp.c")
|
||||
"lib/http_utils.c")
|
||||
set(COMPONENT_ADD_INCLUDEDIRS "include")
|
||||
set(COMPONENT_PRIV_INCLUDEDIRS "lib/include")
|
||||
|
||||
set(COMPONENT_REQUIRES "nghttp")
|
||||
set(COMPONENT_PRIV_REQUIRES "mbedtls" "lwip" "esp-tls")
|
||||
set(COMPONENT_PRIV_REQUIRES "mbedtls" "lwip" "esp-tls" "tcp_transport")
|
||||
|
||||
register_component()
|
||||
|
||||
@@ -123,9 +123,3 @@ int http_utils_str_starts_with(const char *str, const char *start)
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void http_utils_ms_to_timeval(int timeout_ms, struct timeval *tv)
|
||||
{
|
||||
tv->tv_sec = timeout_ms / 1000;
|
||||
tv->tv_usec = (timeout_ms - (tv->tv_sec * 1000)) * 1000;
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#ifndef _HTTP_UTILS_H_
|
||||
#define _HTTP_UTILS_H_
|
||||
#include <sys/time.h>
|
||||
#include "transport_utils.h"
|
||||
/**
|
||||
* @brief Assign new_str to *str pointer, and realloc *str if it not NULL
|
||||
*
|
||||
@@ -78,18 +79,8 @@ char *http_utils_join_string(const char *first_str, int len_first, const char *s
|
||||
*/
|
||||
int http_utils_str_starts_with(const char *str, const char *start);
|
||||
|
||||
/**
|
||||
* @brief Convert milliseconds to timeval struct
|
||||
*
|
||||
* @param[in] timeout_ms The timeout milliseconds
|
||||
* @param[out] tv Timeval struct
|
||||
*/
|
||||
void http_utils_ms_to_timeval(int timeout_ms, struct timeval *tv);
|
||||
|
||||
#define HTTP_MEM_CHECK(TAG, a, action) if (!(a)) { \
|
||||
ESP_LOGE(TAG,"%s:%d (%s): %s", __FILE__, __LINE__, __FUNCTION__, "Memory exhausted"); \
|
||||
action; \
|
||||
}
|
||||
#define HTTP_MEM_CHECK(TAG, a, action) TRANSPORT_MEM_CHECK(TAG, a, action)
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
7
components/mqtt/CMakeLists.txt
Normal file
7
components/mqtt/CMakeLists.txt
Normal file
@@ -0,0 +1,7 @@
|
||||
set(COMPONENT_ADD_INCLUDEDIRS esp-mqtt/include)
|
||||
set(COMPONENT_PRIV_INCLUDEDIRS "esp-mqtt/lib/include")
|
||||
set(COMPONENT_SRCDIRS esp-mqtt esp-mqtt/lib)
|
||||
|
||||
set(COMPONENT_REQUIRES lwip nghttp mbedtls tcp_transport)
|
||||
|
||||
register_component()
|
||||
1
components/mqtt/Kconfig
Normal file
1
components/mqtt/Kconfig
Normal file
@@ -0,0 +1 @@
|
||||
source "$IDF_PATH/components/mqtt/esp-mqtt/Kconfig.included"
|
||||
4
components/mqtt/component.mk
Normal file
4
components/mqtt/component.mk
Normal file
@@ -0,0 +1,4 @@
|
||||
COMPONENT_SUBMODULES += esp-mqtt
|
||||
COMPONENT_ADD_INCLUDEDIRS := esp-mqtt/include
|
||||
COMPONENT_SRCDIRS := esp-mqtt esp-mqtt/lib
|
||||
COMPONENT_PRIV_INCLUDEDIRS := esp-mqtt/lib/include
|
||||
1
components/mqtt/esp-mqtt
Submodule
1
components/mqtt/esp-mqtt
Submodule
Submodule components/mqtt/esp-mqtt added at abaab2abcc
6
components/tcp_transport/CMakeLists.txt
Normal file
6
components/tcp_transport/CMakeLists.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
set(COMPONENT_SRCDIRS ".")
|
||||
set(COMPONENT_ADD_INCLUDEDIRS "include")
|
||||
|
||||
set(COMPONENT_REQUIRES lwip esp-tls)
|
||||
|
||||
register_component()
|
||||
4
components/tcp_transport/component.mk
Normal file
4
components/tcp_transport/component.mk
Normal file
@@ -0,0 +1,4 @@
|
||||
#
|
||||
# Component Makefile
|
||||
#
|
||||
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)
|
||||
@@ -30,6 +30,7 @@ typedef int (*io_func)(transport_handle_t t, const char *buffer, int len, int ti
|
||||
typedef int (*io_read_func)(transport_handle_t t, char *buffer, int len, int timeout_ms);
|
||||
typedef int (*trans_func)(transport_handle_t t);
|
||||
typedef int (*poll_func)(transport_handle_t t, int timeout_ms);
|
||||
typedef transport_handle_t (*payload_transfer_func)(transport_handle_t);
|
||||
|
||||
/**
|
||||
* @brief Create transport list
|
||||
@@ -211,6 +212,17 @@ int transport_close(transport_handle_t t);
|
||||
*/
|
||||
void *transport_get_context_data(transport_handle_t t);
|
||||
|
||||
/**
|
||||
* @brief Get transport handle of underlying protocol
|
||||
* which can access this protocol payload directly
|
||||
* (used for receiving longer msg multiple times)
|
||||
*
|
||||
* @param[in] t The transport handle
|
||||
*
|
||||
* @return Payload transport handle
|
||||
*/
|
||||
transport_handle_t transport_get_payload_transport_handle(transport_handle_t t);
|
||||
|
||||
/**
|
||||
* @brief Set the user context data for this transport
|
||||
*
|
||||
@@ -233,6 +245,7 @@ esp_err_t transport_set_context_data(transport_handle_t t, void *data);
|
||||
* @param[in] _poll_read The poll read function pointer
|
||||
* @param[in] _poll_write The poll write function pointer
|
||||
* @param[in] _destroy The destroy function pointer
|
||||
* @param[in] _parrent_transport The parrent transfer getter pointer
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK
|
||||
@@ -244,7 +257,8 @@ esp_err_t transport_set_func(transport_handle_t t,
|
||||
trans_func _close,
|
||||
poll_func _poll_read,
|
||||
poll_func _poll_write,
|
||||
trans_func _destroy);
|
||||
trans_func _destroy,
|
||||
payload_transfer_func _parrent_transport);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
40
components/tcp_transport/include/transport_utils.h
Normal file
40
components/tcp_transport/include/transport_utils.h
Normal file
@@ -0,0 +1,40 @@
|
||||
// Copyright 2015-2018 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef _TRANSPORT_UTILS_H_
|
||||
#define _TRANSPORT_UTILS_H_
|
||||
#include <sys/time.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Convert milliseconds to timeval struct
|
||||
*
|
||||
* @param[in] timeout_ms The timeout milliseconds
|
||||
* @param[out] tv Timeval struct
|
||||
*/
|
||||
void transport_utils_ms_to_timeval(int timeout_ms, struct timeval *tv);
|
||||
|
||||
|
||||
#define TRANSPORT_MEM_CHECK(TAG, a, action) if (!(a)) { \
|
||||
ESP_LOGE(TAG,"%s:%d (%s): %s", __FILE__, __LINE__, __FUNCTION__, "Memory exhausted"); \
|
||||
action; \
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif /* _TRANSPORT_UTILS_H_ */
|
||||
@@ -20,7 +20,7 @@
|
||||
#include "esp_log.h"
|
||||
|
||||
#include "transport.h"
|
||||
#include "http_utils.h"
|
||||
#include "transport_utils.h"
|
||||
|
||||
static const char *TAG = "TRANSPORT";
|
||||
|
||||
@@ -40,6 +40,8 @@ struct transport_item_t {
|
||||
poll_func _poll_read; /*!< Poll and read */
|
||||
poll_func _poll_write; /*!< Poll and write */
|
||||
trans_func _destroy; /*!< Destroy and free transport */
|
||||
payload_transfer_func _parrent_transfer; /*!< Function returning underlying transport layer */
|
||||
|
||||
STAILQ_ENTRY(transport_item_t) next;
|
||||
};
|
||||
|
||||
@@ -53,7 +55,7 @@ STAILQ_HEAD(transport_list_t, transport_item_t);
|
||||
transport_list_handle_t transport_list_init()
|
||||
{
|
||||
transport_list_handle_t list = calloc(1, sizeof(struct transport_list_t));
|
||||
HTTP_MEM_CHECK(TAG, list, return NULL);
|
||||
TRANSPORT_MEM_CHECK(TAG, list, return NULL);
|
||||
STAILQ_INIT(list);
|
||||
return list;
|
||||
}
|
||||
@@ -64,7 +66,7 @@ esp_err_t transport_list_add(transport_list_handle_t list, transport_handle_t t,
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
t->scheme = calloc(1, strlen(scheme) + 1);
|
||||
HTTP_MEM_CHECK(TAG, t->scheme, return ESP_ERR_NO_MEM);
|
||||
TRANSPORT_MEM_CHECK(TAG, t->scheme, return ESP_ERR_NO_MEM);
|
||||
strcpy(t->scheme, scheme);
|
||||
STAILQ_INSERT_TAIL(list, t, next);
|
||||
return ESP_OK;
|
||||
@@ -113,10 +115,18 @@ esp_err_t transport_list_clean(transport_list_handle_t list)
|
||||
transport_handle_t transport_init()
|
||||
{
|
||||
transport_handle_t t = calloc(1, sizeof(struct transport_item_t));
|
||||
HTTP_MEM_CHECK(TAG, t, return NULL);
|
||||
TRANSPORT_MEM_CHECK(TAG, t, return NULL);
|
||||
return t;
|
||||
}
|
||||
|
||||
transport_handle_t transport_get_payload_transport_handle(transport_handle_t t)
|
||||
{
|
||||
if (t && t->_read) {
|
||||
return t->_parrent_transfer(t);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
esp_err_t transport_destroy(transport_handle_t t)
|
||||
{
|
||||
if (t->scheme) {
|
||||
@@ -199,7 +209,8 @@ esp_err_t transport_set_func(transport_handle_t t,
|
||||
trans_func _close,
|
||||
poll_func _poll_read,
|
||||
poll_func _poll_write,
|
||||
trans_func _destroy)
|
||||
trans_func _destroy,
|
||||
payload_transfer_func _parrent_transport)
|
||||
{
|
||||
if (t == NULL) {
|
||||
return ESP_FAIL;
|
||||
@@ -211,6 +222,7 @@ esp_err_t transport_set_func(transport_handle_t t,
|
||||
t->_poll_read = _poll_read;
|
||||
t->_poll_write = _poll_write;
|
||||
t->_destroy = _destroy;
|
||||
t->_parrent_transfer = _parrent_transport;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
@@ -230,3 +242,8 @@ esp_err_t transport_set_default_port(transport_handle_t t, int port)
|
||||
t->port = port;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
transport_handle_t transport_get_handle(transport_handle_t t)
|
||||
{
|
||||
return t;
|
||||
}
|
||||
@@ -23,7 +23,7 @@
|
||||
|
||||
#include "transport.h"
|
||||
#include "transport_ssl.h"
|
||||
#include "http_utils.h"
|
||||
#include "transport_utils.h"
|
||||
|
||||
static const char *TAG = "TRANS_SSL";
|
||||
/**
|
||||
@@ -37,6 +37,8 @@ typedef struct {
|
||||
bool verify_server;
|
||||
} transport_ssl_t;
|
||||
|
||||
transport_handle_t transport_get_handle(transport_handle_t t);
|
||||
|
||||
static int ssl_close(transport_handle_t t);
|
||||
|
||||
static int ssl_connect(transport_handle_t t, const char *host, int port, int timeout_ms)
|
||||
@@ -65,7 +67,7 @@ static int ssl_poll_read(transport_handle_t t, int timeout_ms)
|
||||
FD_ZERO(&readset);
|
||||
FD_SET(ssl->tls->sockfd, &readset);
|
||||
struct timeval timeout;
|
||||
http_utils_ms_to_timeval(timeout_ms, &timeout);
|
||||
transport_utils_ms_to_timeval(timeout_ms, &timeout);
|
||||
|
||||
return select(ssl->tls->sockfd + 1, &readset, NULL, NULL, &timeout);
|
||||
}
|
||||
@@ -77,7 +79,7 @@ static int ssl_poll_write(transport_handle_t t, int timeout_ms)
|
||||
FD_ZERO(&writeset);
|
||||
FD_SET(ssl->tls->sockfd, &writeset);
|
||||
struct timeval timeout;
|
||||
http_utils_ms_to_timeval(timeout_ms, &timeout);
|
||||
transport_utils_ms_to_timeval(timeout_ms, &timeout);
|
||||
return select(ssl->tls->sockfd + 1, NULL, &writeset, NULL, &timeout);
|
||||
}
|
||||
|
||||
@@ -147,9 +149,9 @@ transport_handle_t transport_ssl_init()
|
||||
{
|
||||
transport_handle_t t = transport_init();
|
||||
transport_ssl_t *ssl = calloc(1, sizeof(transport_ssl_t));
|
||||
HTTP_MEM_CHECK(TAG, ssl, return NULL);
|
||||
TRANSPORT_MEM_CHECK(TAG, ssl, return NULL);
|
||||
transport_set_context_data(t, ssl);
|
||||
transport_set_func(t, ssl_connect, ssl_read, ssl_write, ssl_close, ssl_poll_read, ssl_poll_write, ssl_destroy);
|
||||
transport_set_func(t, ssl_connect, ssl_read, ssl_write, ssl_close, ssl_poll_read, ssl_poll_write, ssl_destroy, transport_get_handle);
|
||||
return t;
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
#include "esp_system.h"
|
||||
#include "esp_err.h"
|
||||
|
||||
#include "http_utils.h"
|
||||
#include "transport_utils.h"
|
||||
#include "transport.h"
|
||||
|
||||
static const char *TAG = "TRANS_TCP";
|
||||
@@ -32,6 +32,8 @@ typedef struct {
|
||||
int sock;
|
||||
} transport_tcp_t;
|
||||
|
||||
transport_handle_t transport_get_handle(transport_handle_t t);
|
||||
|
||||
static int resolve_dns(const char *host, struct sockaddr_in *ip) {
|
||||
|
||||
struct hostent *he;
|
||||
@@ -74,7 +76,7 @@ static int tcp_connect(transport_handle_t t, const char *host, int port, int tim
|
||||
remote_ip.sin_family = AF_INET;
|
||||
remote_ip.sin_port = htons(port);
|
||||
|
||||
http_utils_ms_to_timeval(timeout_ms, &tv);
|
||||
transport_utils_ms_to_timeval(timeout_ms, &tv);
|
||||
|
||||
setsockopt(tcp->sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
|
||||
|
||||
@@ -119,7 +121,7 @@ static int tcp_poll_read(transport_handle_t t, int timeout_ms)
|
||||
FD_ZERO(&readset);
|
||||
FD_SET(tcp->sock, &readset);
|
||||
struct timeval timeout;
|
||||
http_utils_ms_to_timeval(timeout_ms, &timeout);
|
||||
transport_utils_ms_to_timeval(timeout_ms, &timeout);
|
||||
return select(tcp->sock + 1, &readset, NULL, NULL, &timeout);
|
||||
}
|
||||
|
||||
@@ -130,7 +132,7 @@ static int tcp_poll_write(transport_handle_t t, int timeout_ms)
|
||||
FD_ZERO(&writeset);
|
||||
FD_SET(tcp->sock, &writeset);
|
||||
struct timeval timeout;
|
||||
http_utils_ms_to_timeval(timeout_ms, &timeout);
|
||||
transport_utils_ms_to_timeval(timeout_ms, &timeout);
|
||||
return select(tcp->sock + 1, NULL, &writeset, NULL, &timeout);
|
||||
}
|
||||
|
||||
@@ -157,9 +159,9 @@ transport_handle_t transport_tcp_init()
|
||||
{
|
||||
transport_handle_t t = transport_init();
|
||||
transport_tcp_t *tcp = calloc(1, sizeof(transport_tcp_t));
|
||||
HTTP_MEM_CHECK(TAG, tcp, return NULL);
|
||||
TRANSPORT_MEM_CHECK(TAG, tcp, return NULL);
|
||||
tcp->sock = -1;
|
||||
transport_set_func(t, tcp_connect, tcp_read, tcp_write, tcp_close, tcp_poll_read, tcp_poll_write, tcp_destroy);
|
||||
transport_set_func(t, tcp_connect, tcp_read, tcp_write, tcp_close, tcp_poll_read, tcp_poll_write, tcp_destroy, transport_get_handle);
|
||||
transport_set_context_data(t, tcp);
|
||||
|
||||
return t;
|
||||
13
components/tcp_transport/transport_utils.c
Normal file
13
components/tcp_transport/transport_utils.c
Normal file
@@ -0,0 +1,13 @@
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "transport_utils.h"
|
||||
|
||||
void transport_utils_ms_to_timeval(int timeout_ms, struct timeval *tv)
|
||||
{
|
||||
tv->tv_sec = timeout_ms / 1000;
|
||||
tv->tv_usec = (timeout_ms - (tv->tv_sec * 1000)) * 1000;
|
||||
}
|
||||
Reference in New Issue
Block a user