Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple fixes and improvements in mqtt client source #97

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
323 changes: 169 additions & 154 deletions mqttclient/mqttclient.c

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion mqttclient/mqttclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ typedef void (*reconnect_handler_t)(void* client, void* reconnect_date);
typedef struct message_handlers {
mqtt_list_t list;
mqtt_qos_t qos;
const char* topic_filter;
char *topic_filter;
message_handler_t handler;
} message_handlers_t;

Expand Down
4 changes: 2 additions & 2 deletions network/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
string(REGEX REPLACE ".*/(.*)" "\\1" CURRENT_LIB_NAME ${CMAKE_CURRENT_SOURCE_DIR})
string(REGEX REPLACE ".*/(.*)" "\\1" CURRENT_LIB_NAME ${CMAKE_CURRENT_SOURCE_DIR})

set(CURRENT_LIB_NAME ${LIBRARY_PREFIX}-${CURRENT_LIB_NAME})

Expand All @@ -16,7 +16,7 @@ set(${CURRENT_LIB_NAME}_INC_DIRS
)

# 源文件目录
set(${CURRENT_LIB_NAME}_SRC_DIRS
set(${CURRENT_LIB_NAME}_SRC_DIRS
${CMAKE_CURRENT_SOURCE_DIR}
)

Expand Down
2 changes: 1 addition & 1 deletion network/nettype_tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ int nettype_tcp_connect(network_t* n)
n->socket = platform_net_socket_connect(n->host, n->port, PLATFORM_NET_PROTO_TCP);
if (n->socket < 0)
RETURN_ERROR(n->socket);

RETURN_ERROR(MQTT_SUCCESS_ERROR);
}

Expand Down
22 changes: 11 additions & 11 deletions network/nettype_tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ static int nettype_tls_entropy_source(void *data, uint8_t *output, size_t len, s
static int nettype_tls_init(network_t* n, nettype_tls_params_t* nettype_tls_params)
{
int rc = MQTT_SUCCESS_ERROR;

mbedtls_platform_set_calloc_free(platform_memory_calloc, platform_memory_free);

mbedtls_net_init(&(nettype_tls_params->socket_fd));
mbedtls_ssl_init(&(nettype_tls_params->ssl));
mbedtls_ssl_config_init(&(nettype_tls_params->ssl_conf));
Expand Down Expand Up @@ -99,7 +99,7 @@ static int nettype_tls_init(network_t* n, nettype_tls_params_t* nettype_tls_para
MQTT_LOG_E("%s:%d %s()... mbedtls_ssl_conf_own_cert failed returned 0x%04x", __FILE__, __LINE__, __FUNCTION__, (rc < 0 )? -rc : rc);
RETURN_ERROR(rc);
}

mbedtls_ssl_conf_verify(&(nettype_tls_params->ssl_conf), server_certificate_verify, (void *)n->host);

mbedtls_ssl_conf_authmode(&(nettype_tls_params->ssl_conf), MBEDTLS_SSL_VERIFY_REQUIRED);
Expand Down Expand Up @@ -130,7 +130,7 @@ int nettype_tls_connect(network_t* n)
int rc;
if (NULL == n)
RETURN_ERROR(MQTT_NULL_VALUE_ERROR);

nettype_tls_params_t *nettype_tls_params = (nettype_tls_params_t *) platform_memory_alloc(sizeof(nettype_tls_params_t));

if (NULL == nettype_tls_params)
Expand Down Expand Up @@ -170,12 +170,12 @@ int nettype_tls_connect(network_t* n)
}


void nettype_tls_disconnect(network_t* n)
void nettype_tls_disconnect(network_t* n)
{
int rc = 0;
if (NULL == n)
return;

nettype_tls_params_t *nettype_tls_params = (nettype_tls_params_t *) n->nettype_tls_params;

do {
Expand Down Expand Up @@ -204,7 +204,7 @@ int nettype_tls_write(network_t *n, unsigned char *buf, int len, int timeout)

if (NULL == n)
RETURN_ERROR(MQTT_NULL_VALUE_ERROR);

nettype_tls_params_t *nettype_tls_params = (nettype_tls_params_t *) n->nettype_tls_params;

platform_timer_cutdown(&timer, timeout);
Expand All @@ -217,7 +217,7 @@ int nettype_tls_write(network_t *n, unsigned char *buf, int len, int timeout)
} else if ((rc == 0) || ((rc != MBEDTLS_ERR_SSL_WANT_WRITE) && (rc != MBEDTLS_ERR_SSL_WANT_READ) && (rc != MBEDTLS_ERR_SSL_TIMEOUT))) {
MQTT_LOG_E("%s:%d %s()... mbedtls_ssl_write failed: 0x%04x", __FILE__, __LINE__, __FUNCTION__, (rc < 0 )? -rc : rc);
break;
}
}
} while((!platform_timer_is_expired(&timer)) && (write_len < len));

return write_len;
Expand All @@ -231,11 +231,11 @@ int nettype_tls_read(network_t *n, unsigned char *buf, int len, int timeout)

if (NULL == n)
RETURN_ERROR(MQTT_NULL_VALUE_ERROR);

nettype_tls_params_t *nettype_tls_params = (nettype_tls_params_t *) n->nettype_tls_params;

platform_timer_cutdown(&timer, timeout);

do {
rc = mbedtls_ssl_read(&(nettype_tls_params->ssl), (unsigned char *)(buf + read_len), len - read_len);

Expand All @@ -244,7 +244,7 @@ int nettype_tls_read(network_t *n, unsigned char *buf, int len, int timeout)
} else if ((rc == 0) || ((rc != MBEDTLS_ERR_SSL_WANT_WRITE) && (rc != MBEDTLS_ERR_SSL_WANT_READ) && (rc != MBEDTLS_ERR_SSL_TIMEOUT))) {
// MQTT_LOG_E("%s:%d %s()... mbedtls_ssl_read failed: 0x%04x", __FILE__, __LINE__, __FUNCTION__, (rc < 0 )? -rc : rc);
break;
}
}
} while((!platform_timer_is_expired(&timer)) && (read_len < len));

return read_len;
Expand Down
1 change: 1 addition & 0 deletions network/nettype_tls.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,4 @@ void nettype_tls_disconnect(network_t* n);
#endif

#endif
s
3 changes: 1 addition & 2 deletions network/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ int network_set_ca(network_t *n, const char *ca)
#ifndef MQTT_NETWORK_TYPE_NO_TLS
if ((NULL == n) || (NULL == ca))
RETURN_ERROR(MQTT_NULL_VALUE_ERROR);

n->ca_crt = ca;
n->ca_crt_len = strlen(ca);
n->channel = NETWORK_CHANNEL_TLS;
Expand All @@ -110,4 +110,3 @@ int network_set_host_port(network_t* n, char *host, char *port)

RETURN_ERROR(MQTT_SUCCESS_ERROR);
}

6 changes: 2 additions & 4 deletions platform/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
string(REGEX REPLACE ".*/(.*)" "\\1" CURRENT_LIB_NAME ${CMAKE_CURRENT_SOURCE_DIR})
string(REGEX REPLACE ".*/(.*)" "\\1" CURRENT_LIB_NAME ${CMAKE_CURRENT_SOURCE_DIR})

set(CURRENT_LIB_NAME ${LIBRARY_PREFIX}-${CURRENT_LIB_NAME})

Expand All @@ -10,7 +10,7 @@ set(${CURRENT_LIB_NAME}_INC_DIRS
)

# 源文件目录
set(${CURRENT_LIB_NAME}_SRC_DIRS
set(${CURRENT_LIB_NAME}_SRC_DIRS
${CMAKE_CURRENT_SOURCE_DIR}/linux
)

Expand Down Expand Up @@ -59,5 +59,3 @@ install(DIRECTORY ${${CURRENT_LIB_NAME}_INC_DIRS}
PATTERN "*.h"
PATTERN "*.hpp"
PATTERN "CMakeLists.txt" EXCLUDE)


3 changes: 0 additions & 3 deletions platform/FreeRTOS/platform_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,3 @@ void platform_memory_free(void *ptr)
{
vPortFree(ptr);
}



19 changes: 9 additions & 10 deletions platform/FreeRTOS/platform_net_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ int platform_net_socket_connect(const char *host, const char *port, int proto)
{
int fd, ret = MQTT_SOCKET_UNKNOWN_HOST_ERROR;
struct addrinfo hints, *addr_list, *cur;

/* Do name resolution with both IPv6 and IPv4 */
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = (proto == PLATFORM_NET_PROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
hints.ai_protocol = (proto == PLATFORM_NET_PROTO_UDP) ? IPPROTO_UDP : IPPROTO_TCP;

if (getaddrinfo(host, port, &hints, &addr_list) != 0) {
return ret;
}

for (cur = addr_list; cur != NULL; cur = cur->ai_next) {
fd = socket(cur->ai_family, cur->ai_socktype, cur->ai_protocol);
if (fd < 0) {
Expand Down Expand Up @@ -51,14 +51,14 @@ int platform_net_socket_recv_timeout(int fd, unsigned char *buf, int len, int ti
{
int nread;
int nleft = len;
unsigned char *ptr;
unsigned char *ptr;
ptr = buf;

struct timeval tv = {
timeout / 1000,
timeout / 1000,
(timeout % 1000) * 1000
};

if (tv.tv_sec < 0 || (tv.tv_sec == 0 && tv.tv_usec <= 0)) {
tv.tv_sec = 0;
tv.tv_usec = 100;
Expand Down Expand Up @@ -88,17 +88,17 @@ int platform_net_socket_write(int fd, void *buf, size_t len)
int platform_net_socket_write_timeout(int fd, unsigned char *buf, int len, int timeout)
{
struct timeval tv = {
timeout / 1000,
timeout / 1000,
(timeout % 1000) * 1000
};

if (tv.tv_sec < 0 || (tv.tv_sec == 0 && tv.tv_usec <= 0)) {
tv.tv_sec = 0;
tv.tv_usec = 100;
}

setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv,sizeof(struct timeval));

return write(fd, buf, len);
}

Expand All @@ -123,4 +123,3 @@ int platform_net_socket_setsockopt(int fd, int level, int optname, const void *o
{
return setsockopt(fd, level, optname, optval, optlen);
}

13 changes: 7 additions & 6 deletions platform/FreeRTOS/platform_thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ platform_thread_t *platform_thread_init( const char *name,
{
BaseType_t err;
platform_thread_t *thread;

thread = platform_memory_alloc(sizeof(platform_thread_t));

(void)tick;
Expand Down Expand Up @@ -50,10 +50,11 @@ void platform_thread_start(platform_thread_t* thread)

void platform_thread_destroy(platform_thread_t* thread)
{
if (NULL != thread)
vTaskDelete(thread->thread);

platform_memory_free(thread);
}
if (NULL != thread) {
TaskHandle_t thread_handle = thread->thread;

platform_memory_free(thread);

vTaskDelete(thread_handle);
}
}
3 changes: 1 addition & 2 deletions platform/FreeRTOS/platform_timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,10 @@ void platform_timer_usleep(unsigned long usec)

if(usec != 0) {
tick = usec / portTICK_PERIOD_MS;

if (tick == 0)
tick = 1;
}

vTaskDelay(tick);
}

3 changes: 0 additions & 3 deletions platform/RT-Thread/platform_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,3 @@ void platform_memory_free(void *ptr)
{
rt_free(ptr);
}



19 changes: 9 additions & 10 deletions platform/RT-Thread/platform_net_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ int platform_net_socket_connect(const char *host, const char *port, int proto)
{
int fd, ret = MQTT_SOCKET_UNKNOWN_HOST_ERROR;
struct addrinfo hints, *addr_list, *cur;

/* Do name resolution with both IPv6 and IPv4 */
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = (proto == PLATFORM_NET_PROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
hints.ai_protocol = (proto == PLATFORM_NET_PROTO_UDP) ? IPPROTO_UDP : IPPROTO_TCP;

if (getaddrinfo(host, port, &hints, &addr_list) != 0) {
return ret;
}

for (cur = addr_list; cur != NULL; cur = cur->ai_next) {
fd = socket(cur->ai_family, cur->ai_socktype, cur->ai_protocol);
if (fd < 0) {
Expand Down Expand Up @@ -51,14 +51,14 @@ int platform_net_socket_recv_timeout(int fd, unsigned char *buf, int len, int ti
{
int nread;
int nleft = len;
unsigned char *ptr;
unsigned char *ptr;
ptr = buf;

struct timeval tv = {
timeout / 1000,
timeout / 1000,
(timeout % 1000) * 1000
};

if (tv.tv_sec < 0 || (tv.tv_sec == 0 && tv.tv_usec <= 0)) {
tv.tv_sec = 0;
tv.tv_usec = 100;
Expand Down Expand Up @@ -88,17 +88,17 @@ int platform_net_socket_write(int fd, void *buf, size_t len)
int platform_net_socket_write_timeout(int fd, unsigned char *buf, int len, int timeout)
{
struct timeval tv = {
timeout / 1000,
timeout / 1000,
(timeout % 1000) * 1000
};

if (tv.tv_sec < 0 || (tv.tv_sec == 0 && tv.tv_usec <= 0)) {
tv.tv_sec = 0;
tv.tv_usec = 100;
}

setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv,sizeof(struct timeval));

return send(fd, buf, len, 0);
}

Expand All @@ -123,4 +123,3 @@ int platform_net_socket_setsockopt(int fd, int level, int optname, const void *o
{
return setsockopt(fd, level, optname, optval, optlen);
}

15 changes: 8 additions & 7 deletions platform/RT-Thread/platform_thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ platform_thread_t *platform_thread_init( const char *name,
thread->thread = rt_thread_create((const char *)name,
entry, param,
stack_size, priority, tick);

if (thread->thread == RT_NULL)
{
return RT_NULL;
return RT_NULL;
}
else
{
return thread;
return thread;
}

}
Expand All @@ -49,7 +49,6 @@ void platform_thread_startup(platform_thread_t* thread)
void platform_thread_stop(platform_thread_t* thread)
{
rt_thread_suspend(thread->thread);

}

void platform_thread_start(platform_thread_t* thread)
Expand All @@ -59,7 +58,9 @@ void platform_thread_start(platform_thread_t* thread)

void platform_thread_destroy(platform_thread_t* thread)
{
platform_memory_free(thread);
if (thread) {
rt_thread_t thread_handle = thread->thread;
platform_memory_free(thread);
rt_thread_delete(thread_handle);
}
}


3 changes: 0 additions & 3 deletions platform/TencentOS-tiny/platform_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,3 @@ void platform_memory_free(void *ptr)
{
tos_mmheap_free(ptr);
}



Loading