Skip to content

Commit e723940

Browse files
authored
Merge pull request #97 from ajaybhargav/master
Multiple fixes and improvements in mqtt client source
2 parents 956e0c8 + 1d831f5 commit e723940

23 files changed

+289
-270
lines changed

mqttclient/mqttclient.c

Lines changed: 180 additions & 157 deletions
Large diffs are not rendered by default.

mqttclient/mqttclient.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,12 @@ typedef struct message_data {
6565
typedef void (*interceptor_handler_t)(void* client, message_data_t* msg);
6666
typedef void (*message_handler_t)(void* client, message_data_t* msg);
6767
typedef void (*reconnect_handler_t)(void* client, void* reconnect_date);
68+
typedef void (*topiclist_hanndler_t)(const char *topicname, mqtt_qos_t qos);
6869

6970
typedef struct message_handlers {
7071
mqtt_list_t list;
7172
mqtt_qos_t qos;
72-
const char* topic_filter;
73+
char *topic_filter;
7374
message_handler_t handler;
7475
} message_handlers_t;
7576

@@ -111,6 +112,7 @@ typedef struct mqtt_client {
111112
uint32_t mqtt_read_buf_size;
112113
uint32_t mqtt_write_buf_size;
113114
uint32_t mqtt_reconnect_try_duration;
115+
uint32_t mqtt_thread_stack_size;
114116
size_t mqtt_client_id_len;
115117
size_t mqtt_user_name_len;
116118
size_t mqtt_password_len;
@@ -158,6 +160,7 @@ MQTT_CLIENT_SET_STATEMENT(cmd_timeout, uint32_t)
158160
MQTT_CLIENT_SET_STATEMENT(read_buf_size, uint32_t)
159161
MQTT_CLIENT_SET_STATEMENT(write_buf_size, uint32_t)
160162
MQTT_CLIENT_SET_STATEMENT(reconnect_try_duration, uint32_t)
163+
MQTT_CLIENT_SET_STATEMENT(thread_stack_size, uint32_t)
161164
MQTT_CLIENT_SET_STATEMENT(reconnect_handler, reconnect_handler_t)
162165
MQTT_CLIENT_SET_STATEMENT(interceptor_handler, interceptor_handler_t)
163166

@@ -170,7 +173,7 @@ int mqtt_keep_alive(mqtt_client_t* c);
170173
int mqtt_subscribe(mqtt_client_t* c, const char* topic_filter, mqtt_qos_t qos, message_handler_t msg_handler);
171174
int mqtt_unsubscribe(mqtt_client_t* c, const char* topic_filter);
172175
int mqtt_publish(mqtt_client_t* c, const char* topic_filter, mqtt_message_t* msg);
173-
int mqtt_list_subscribe_topic(mqtt_client_t* c);
176+
int mqtt_list_subscribe_topic(mqtt_client_t* c, topiclist_hanndler_t handler);
174177
int mqtt_set_will_options(mqtt_client_t* c, char *topic, mqtt_qos_t qos, uint8_t retained, char *message);
175178

176179
#ifdef __cplusplus

network/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
string(REGEX REPLACE ".*/(.*)" "\\1" CURRENT_LIB_NAME ${CMAKE_CURRENT_SOURCE_DIR})
1+
string(REGEX REPLACE ".*/(.*)" "\\1" CURRENT_LIB_NAME ${CMAKE_CURRENT_SOURCE_DIR})
22

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

@@ -16,7 +16,7 @@ set(${CURRENT_LIB_NAME}_INC_DIRS
1616
)
1717

1818
# 源文件目录
19-
set(${CURRENT_LIB_NAME}_SRC_DIRS
19+
set(${CURRENT_LIB_NAME}_SRC_DIRS
2020
${CMAKE_CURRENT_SOURCE_DIR}
2121
)
2222

network/nettype_tcp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ int nettype_tcp_connect(network_t* n)
2424
n->socket = platform_net_socket_connect(n->host, n->port, PLATFORM_NET_PROTO_TCP);
2525
if (n->socket < 0)
2626
RETURN_ERROR(n->socket);
27-
27+
2828
RETURN_ERROR(MQTT_SUCCESS_ERROR);
2929
}
3030

network/nettype_tls.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ static int nettype_tls_entropy_source(void *data, uint8_t *output, size_t len, s
5151
static int nettype_tls_init(network_t* n, nettype_tls_params_t* nettype_tls_params)
5252
{
5353
int rc = MQTT_SUCCESS_ERROR;
54-
54+
5555
mbedtls_platform_set_calloc_free(platform_memory_calloc, platform_memory_free);
56-
56+
5757
mbedtls_net_init(&(nettype_tls_params->socket_fd));
5858
mbedtls_ssl_init(&(nettype_tls_params->ssl));
5959
mbedtls_ssl_config_init(&(nettype_tls_params->ssl_conf));
@@ -99,7 +99,7 @@ static int nettype_tls_init(network_t* n, nettype_tls_params_t* nettype_tls_para
9999
MQTT_LOG_E("%s:%d %s()... mbedtls_ssl_conf_own_cert failed returned 0x%04x", __FILE__, __LINE__, __FUNCTION__, (rc < 0 )? -rc : rc);
100100
RETURN_ERROR(rc);
101101
}
102-
102+
103103
mbedtls_ssl_conf_verify(&(nettype_tls_params->ssl_conf), server_certificate_verify, (void *)n->host);
104104

105105
mbedtls_ssl_conf_authmode(&(nettype_tls_params->ssl_conf), MBEDTLS_SSL_VERIFY_REQUIRED);
@@ -130,7 +130,7 @@ int nettype_tls_connect(network_t* n)
130130
int rc;
131131
if (NULL == n)
132132
RETURN_ERROR(MQTT_NULL_VALUE_ERROR);
133-
133+
134134
nettype_tls_params_t *nettype_tls_params = (nettype_tls_params_t *) platform_memory_alloc(sizeof(nettype_tls_params_t));
135135

136136
if (NULL == nettype_tls_params)
@@ -170,12 +170,12 @@ int nettype_tls_connect(network_t* n)
170170
}
171171

172172

173-
void nettype_tls_disconnect(network_t* n)
173+
void nettype_tls_disconnect(network_t* n)
174174
{
175175
int rc = 0;
176176
if (NULL == n)
177177
return;
178-
178+
179179
nettype_tls_params_t *nettype_tls_params = (nettype_tls_params_t *) n->nettype_tls_params;
180180

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

205205
if (NULL == n)
206206
RETURN_ERROR(MQTT_NULL_VALUE_ERROR);
207-
207+
208208
nettype_tls_params_t *nettype_tls_params = (nettype_tls_params_t *) n->nettype_tls_params;
209209

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

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

232232
if (NULL == n)
233233
RETURN_ERROR(MQTT_NULL_VALUE_ERROR);
234-
234+
235235
nettype_tls_params_t *nettype_tls_params = (nettype_tls_params_t *) n->nettype_tls_params;
236236

237237
platform_timer_cutdown(&timer, timeout);
238-
238+
239239
do {
240240
rc = mbedtls_ssl_read(&(nettype_tls_params->ssl), (unsigned char *)(buf + read_len), len - read_len);
241241

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

250250
return read_len;

network/nettype_tls.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,4 @@ void nettype_tls_disconnect(network_t* n);
6262
#endif
6363

6464
#endif
65+
s

network/network.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ int network_set_ca(network_t *n, const char *ca)
9191
#ifndef MQTT_NETWORK_TYPE_NO_TLS
9292
if ((NULL == n) || (NULL == ca))
9393
RETURN_ERROR(MQTT_NULL_VALUE_ERROR);
94-
94+
9595
n->ca_crt = ca;
9696
n->ca_crt_len = strlen(ca);
9797
n->channel = NETWORK_CHANNEL_TLS;
@@ -110,4 +110,3 @@ int network_set_host_port(network_t* n, char *host, char *port)
110110

111111
RETURN_ERROR(MQTT_SUCCESS_ERROR);
112112
}
113-

platform/CMakeLists.txt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
string(REGEX REPLACE ".*/(.*)" "\\1" CURRENT_LIB_NAME ${CMAKE_CURRENT_SOURCE_DIR})
1+
string(REGEX REPLACE ".*/(.*)" "\\1" CURRENT_LIB_NAME ${CMAKE_CURRENT_SOURCE_DIR})
22

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

@@ -10,7 +10,7 @@ set(${CURRENT_LIB_NAME}_INC_DIRS
1010
)
1111

1212
# 源文件目录
13-
set(${CURRENT_LIB_NAME}_SRC_DIRS
13+
set(${CURRENT_LIB_NAME}_SRC_DIRS
1414
${CMAKE_CURRENT_SOURCE_DIR}/linux
1515
)
1616

@@ -59,5 +59,3 @@ install(DIRECTORY ${${CURRENT_LIB_NAME}_INC_DIRS}
5959
PATTERN "*.h"
6060
PATTERN "*.hpp"
6161
PATTERN "CMakeLists.txt" EXCLUDE)
62-
63-

platform/FreeRTOS/platform_memory.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,3 @@ void platform_memory_free(void *ptr)
2727
{
2828
vPortFree(ptr);
2929
}
30-
31-
32-

platform/FreeRTOS/platform_net_socket.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ int platform_net_socket_connect(const char *host, const char *port, int proto)
1111
{
1212
int fd, ret = MQTT_SOCKET_UNKNOWN_HOST_ERROR;
1313
struct addrinfo hints, *addr_list, *cur;
14-
14+
1515
/* Do name resolution with both IPv6 and IPv4 */
1616
memset(&hints, 0, sizeof(hints));
1717
hints.ai_family = AF_UNSPEC;
1818
hints.ai_socktype = (proto == PLATFORM_NET_PROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
1919
hints.ai_protocol = (proto == PLATFORM_NET_PROTO_UDP) ? IPPROTO_UDP : IPPROTO_TCP;
20-
20+
2121
if (getaddrinfo(host, port, &hints, &addr_list) != 0) {
2222
return ret;
2323
}
24-
24+
2525
for (cur = addr_list; cur != NULL; cur = cur->ai_next) {
2626
fd = socket(cur->ai_family, cur->ai_socktype, cur->ai_protocol);
2727
if (fd < 0) {
@@ -51,14 +51,14 @@ int platform_net_socket_recv_timeout(int fd, unsigned char *buf, int len, int ti
5151
{
5252
int nread;
5353
int nleft = len;
54-
unsigned char *ptr;
54+
unsigned char *ptr;
5555
ptr = buf;
5656

5757
struct timeval tv = {
58-
timeout / 1000,
58+
timeout / 1000,
5959
(timeout % 1000) * 1000
6060
};
61-
61+
6262
if (tv.tv_sec < 0 || (tv.tv_sec == 0 && tv.tv_usec <= 0)) {
6363
tv.tv_sec = 0;
6464
tv.tv_usec = 100;
@@ -88,17 +88,17 @@ int platform_net_socket_write(int fd, void *buf, size_t len)
8888
int platform_net_socket_write_timeout(int fd, unsigned char *buf, int len, int timeout)
8989
{
9090
struct timeval tv = {
91-
timeout / 1000,
91+
timeout / 1000,
9292
(timeout % 1000) * 1000
9393
};
94-
94+
9595
if (tv.tv_sec < 0 || (tv.tv_sec == 0 && tv.tv_usec <= 0)) {
9696
tv.tv_sec = 0;
9797
tv.tv_usec = 100;
9898
}
9999

100100
setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv,sizeof(struct timeval));
101-
101+
102102
return write(fd, buf, len);
103103
}
104104

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

0 commit comments

Comments
 (0)