diff --git a/components/connectivity/mqttclient/common/error.h b/components/connectivity/mqttclient/common/error.h index 96f072ee..1e166f0d 100644 --- a/components/connectivity/mqttclient/common/error.h +++ b/components/connectivity/mqttclient/common/error.h @@ -2,7 +2,7 @@ * @Author: jiejie * @Github: https://github.com/jiejieTop * @Date: 2019-12-15 00:42:16 - * @LastEditTime: 2020-03-21 16:49:07 + * @LastEditTime: 2020-04-25 18:59:08 * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. */ #ifndef _ERROR_H_ @@ -10,12 +10,12 @@ typedef enum mqtt_error { MQTT_SSL_CERT_ERROR = -0x001C, /* cetr parse failed */ - MQTT_SOCKET_FAILED = -0x001B, /* socket fd failed */ - MQTT_SOCKET_UNKNOWN_HOST = -0x001A, /* socket unknown host ip or domain */ - MQTT_SET_PUBLISH_DUP_FAILED = -0x0019, /* mqtt publish packet set udp bit failed */ + MQTT_SOCKET_FAILED_ERROR = -0x001B, /* socket fd failed */ + MQTT_SOCKET_UNKNOWN_HOST_ERROR = -0x001A, /* socket unknown host ip or domain */ + MQTT_SET_PUBLISH_DUP_FAILED_ERROR = -0x0019, /* mqtt publish packet set udp bit failed */ MQTT_CLEAN_SESSION_ERROR = -0x0018, /* mqtt clean session error */ - MQTT_ACK_NODE_IS_EXIST = -0x0017, /* mqtt ack list is exist ack node */ - MQTT_ACK_HANDLER_NUM_TOO_MUCH = -0x0016, /* mqtt ack handler number is too much */ + MQTT_ACK_NODE_IS_EXIST_ERROR = -0x0017, /* mqtt ack list is exist ack node */ + MQTT_ACK_HANDLER_NUM_TOO_MUCH_ERROR = -0x0016, /* mqtt ack handler number is too much */ MQTT_RESUBSCRIBE_ERROR = -0x0015, /* mqtt resubscribe error */ MQTT_SUBSCRIBE_ERROR = -0x0014, /* mqtt subscribe error */ MQTT_SEND_PACKET_ERROR = -0x0013, /* mqtt send a packet */ diff --git a/components/connectivity/mqttclient/mqttclient/mqttclient.c b/components/connectivity/mqttclient/mqttclient/mqttclient.c index 57ea6e99..1da99b7c 100644 --- a/components/connectivity/mqttclient/mqttclient/mqttclient.c +++ b/components/connectivity/mqttclient/mqttclient/mqttclient.c @@ -2,7 +2,7 @@ * @Author: jiejie * @Github: https://github.com/jiejieTop * @Date: 2019-12-09 21:31:25 - * @LastEditTime: 2020-03-15 01:33:01 + * @LastEditTime: 2020-04-25 18:58:14 * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. */ #include "mqttclient.h" @@ -48,12 +48,12 @@ static int mqtt_set_publish_dup(mqtt_client_t* c, unsigned char dup) MQTTHeader header = {0}; if (NULL == c->write_buf) - RETURN_ERROR(MQTT_SET_PUBLISH_DUP_FAILED); + RETURN_ERROR(MQTT_SET_PUBLISH_DUP_FAILED_ERROR); header.byte = readChar(&read_data); /* read header */ if (header.bits.type != PUBLISH) - RETURN_ERROR(MQTT_SET_PUBLISH_DUP_FAILED); + RETURN_ERROR(MQTT_SET_PUBLISH_DUP_FAILED_ERROR); header.bits.dup = dup; writeChar(&write_data, header.byte); /* write header */ @@ -297,12 +297,15 @@ static int mqtt_deliver_message(mqtt_client_t* c, MQTTString* topic_name, mqtt_m mqtt_new_message_data(&md, topic_name, message); /* make a message data */ msg_handler->handler(c, &md); /* deliver the message */ rc = MQTT_SUCCESS_ERROR; - } else - goto exit; + } else if (NULL != c->interceptor_handler) { + message_data_t md; + mqtt_new_message_data(&md, topic_name, message); /* make a message data */ + c->interceptor_handler(c, &md); + rc = MQTT_SUCCESS_ERROR; + } memset(message->payload, 0, strlen(message->payload)); memset(topic_name->lenstring.data, 0, topic_name->lenstring.len); -exit: RETURN_ERROR(rc); } @@ -379,7 +382,7 @@ static int mqtt_ack_list_record(mqtt_client_t* c, int type, unsigned short packe /* Determine if the node already exists */ if (mqtt_ack_list_node_is_exist(c, type, packet_id)) - RETURN_ERROR(MQTT_ACK_NODE_IS_EXIST); + RETURN_ERROR(MQTT_ACK_NODE_IS_EXIST_ERROR); /* create a ack handler node */ ack_handler = mqtt_ack_handler_create(c, type, packet_id, payload_len, handler); @@ -448,9 +451,6 @@ static int mqtt_msg_handler_is_exist(mqtt_client_t* c, message_handlers_t *handl list_t *curr, *next; message_handlers_t *msg_handler; - if ((NULL == c) || (NULL == handler)) - return 0; - if (list_is_empty(&c->msg_handler_list)) return 0; @@ -459,7 +459,8 @@ static int mqtt_msg_handler_is_exist(mqtt_client_t* c, message_handlers_t *handl /* determine whether a node already exists by mqtt topic, but wildcards are not supported */ if ((NULL != msg_handler->topic_filter) && (mqtt_is_topic_equals(msg_handler->topic_filter, handler->topic_filter))) { - LOG_W("%s:%d %s()...msg_handler->topic_filter: %s, handler->topic_filter: %s", __FILE__, __LINE__, __FUNCTION__, msg_handler->topic_filter, handler->topic_filter); + LOG_W("%s:%d %s()...msg_handler->topic_filter: %s, handler->topic_filter: %s", + __FILE__, __LINE__, __FUNCTION__, msg_handler->topic_filter, handler->topic_filter); return 1; } } @@ -469,6 +470,9 @@ static int mqtt_msg_handler_is_exist(mqtt_client_t* c, message_handlers_t *handl static int mqtt_msg_handlers_install(mqtt_client_t* c, message_handlers_t *handler) { + if ((NULL == c) || (NULL == handler)) + RETURN_ERROR(MQTT_NULL_VALUE_ERROR); + if (mqtt_msg_handler_is_exist(c, handler)) { mqtt_msg_handler_destory(handler); RETURN_ERROR(MQTT_SUCCESS_ERROR); @@ -548,7 +552,7 @@ static int mqtt_try_resubscribe(mqtt_client_t* c) msg_handler = LIST_ENTRY(curr, message_handlers_t, list); /* resubscribe topic */ - if ((rc = mqtt_subscribe(c, msg_handler->topic_filter, msg_handler->qos, msg_handler->handler)) == MQTT_ACK_HANDLER_NUM_TOO_MUCH) + if ((rc = mqtt_subscribe(c, msg_handler->topic_filter, msg_handler->qos, msg_handler->handler)) == MQTT_ACK_HANDLER_NUM_TOO_MUCH_ERROR) LOG_W("%s:%d %s()... mqtt ack handler num too much ...", __FILE__, __LINE__, __FUNCTION__); } @@ -567,7 +571,7 @@ static int mqtt_try_do_reconnect(mqtt_client_t* c) rc = mqtt_try_resubscribe(c); /* resubscribe */ } - LOG_I("%s:%d %s()... mqtt try connect result is %#x", __FILE__, __LINE__, __FUNCTION__, rc); + LOG_I("%s:%d %s()... mqtt try connect result is -0x%04x", __FILE__, __LINE__, __FUNCTION__, -rc); RETURN_ERROR(rc); } @@ -578,7 +582,7 @@ static int mqtt_try_reconnect(mqtt_client_t* c) rc = mqtt_try_do_reconnect(c); - if (platform_timer_is_expired(&c->reconnect_timer)) { + if ((MQTT_SUCCESS_ERROR != rc) && (platform_timer_is_expired(&c->reconnect_timer))) { platform_timer_cutdown(&c->reconnect_timer, c->reconnect_try_duration); if (NULL != c->reconnect_handler) c->reconnect_handler(c, c->reconnect_date); @@ -673,6 +677,7 @@ static int mqtt_suback_packet_handle(mqtt_client_t *c, platform_timer_t *timer) if (is_nack) { mqtt_msg_handler_destory(msg_handler); /* subscribe topic failed, destory message handler */ + LOG_D("subscribe topic failed..."); RETURN_ERROR(MQTT_SUBSCRIBE_NOT_ACK_ERROR); } @@ -746,7 +751,7 @@ static int mqtt_publish_packet_handle(mqtt_client_t *c, platform_timer_t *timer) mqtt_deliver_message(c, &topic_name, &msg); else { /* record the received of a qos2 message and only processes it when the qos2 message is received for the first time */ - if ((rc = mqtt_ack_list_record(c, PUBREL, msg.id, len, NULL)) != MQTT_ACK_NODE_IS_EXIST) + if ((rc = mqtt_ack_list_record(c, PUBREL, msg.id, len, NULL)) != MQTT_ACK_NODE_IS_EXIST_ERROR) mqtt_deliver_message(c, &topic_name, &msg); } @@ -840,6 +845,54 @@ static int mqtt_wait_packet(mqtt_client_t* c, int packet_type, platform_timer_t* RETURN_ERROR(rc); } +static int mqtt_yield(mqtt_client_t* c, int timeout_ms) +{ + int rc = MQTT_SUCCESS_ERROR; + client_state_t state; + platform_timer_t timer; + + if (NULL == c) + RETURN_ERROR(MQTT_FAILED_ERROR); + + if (0 == timeout_ms) + timeout_ms = c->cmd_timeout; + + platform_timer_init(&timer); + platform_timer_cutdown(&timer, timeout_ms); + + while (!platform_timer_is_expired(&timer)) { + state = mqtt_get_client_state(c); + if (CLIENT_STATE_CLEAN_SESSION == state) { + RETURN_ERROR(MQTT_CLEAN_SESSION_ERROR); + } else if (CLIENT_STATE_CONNECTED != state) { + /* mqtt not connect, need reconnect */ + rc = mqtt_try_reconnect(c); + + if (MQTT_RECONNECT_TIMEOUT_ERROR == rc) + RETURN_ERROR(rc); + continue; + } + + /* mqtt connected, handle mqtt packet */ + rc = mqtt_packet_handle(c, &timer); + + if (rc >= 0) { + /* scan ack list, destroy ack handler that have timed out or resend them */ + mqtt_ack_list_scan(c); + + } else if (MQTT_NOT_CONNECT_ERROR == rc) { + LOG_E("%s:%d %s()... mqtt not connect", __FILE__, __LINE__, __FUNCTION__); + + /* reconnect timer cutdown */ + platform_timer_cutdown(&c->reconnect_timer, c->reconnect_try_duration); + } else { + break; + } + } + + RETURN_ERROR(rc); +} + static void mqtt_yield_thread(void *arg) { int rc; @@ -882,9 +935,6 @@ static int mqtt_connect_with_results(mqtt_client_t* c) if (CLIENT_STATE_CONNECTED == mqtt_get_client_state(c)) RETURN_ERROR(MQTT_SUCCESS_ERROR); - platform_timer_init(&connect_timer); - platform_timer_cutdown(&connect_timer, c->cmd_timeout); - rc = c->network->connect(c->network); if (MQTT_SUCCESS_ERROR != rc) RETURN_ERROR(rc); @@ -905,6 +955,9 @@ static int mqtt_connect_with_results(mqtt_client_t* c) /* serialize connect packet */ if ((len = MQTTSerialize_connect(c->write_buf, c->write_buf_size, &connect_data)) <= 0) goto exit; + + platform_timer_init(&connect_timer); + platform_timer_cutdown(&connect_timer, c->cmd_timeout); /* send connect packet */ if ((rc = mqtt_send_packet(c, len, &connect_timer)) != MQTT_SUCCESS_ERROR) @@ -1032,6 +1085,7 @@ int mqtt_init(mqtt_client_t* c, client_init_params_t* init) c->reconnect_date = init->reconnect_date; c->reconnect_handler = init->reconnect_handler; + c->interceptor_handler = NULL; // c->network->network_params = &init->connect_params.network_params; if ((rc = network_init(c->network, &init->connect_params.network_params)) < 0) @@ -1052,11 +1106,23 @@ int mqtt_init(mqtt_client_t* c, client_init_params_t* init) int mqtt_release(mqtt_client_t* c) { + platform_timer_t timer; + if (NULL == c) RETURN_ERROR(MQTT_NULL_VALUE_ERROR); - - while (CLIENT_STATE_INVALID != mqtt_get_client_state(c)); /* wait for the clean session to complete */ + platform_timer_init(&timer); + platform_timer_cutdown(&timer, c->cmd_timeout); + + /* wait for the clean session to complete */ + while ((CLIENT_STATE_INVALID != mqtt_get_client_state(c))) { + // platform_timer_usleep(1000); // 1ms avoid compiler optimization. + if (platform_timer_is_expired(&timer)) { + LOG_E("%s:%d %s()... mqtt release failed...", __FILE__, __LINE__, __FUNCTION__); + RETURN_ERROR(MQTT_FAILED_ERROR) + } + } + if (NULL != c->network) { platform_memory_free(c->network); c->network = NULL; @@ -1067,9 +1133,9 @@ int mqtt_release(mqtt_client_t* c) c->read_buf = NULL; } - if (NULL != c->read_buf) { - platform_memory_free(c->read_buf); - c->read_buf = NULL; + if (NULL != c->write_buf) { + platform_memory_free(c->write_buf); + c->write_buf = NULL; } memset(c, 0, sizeof(mqtt_client_t)); @@ -1203,9 +1269,9 @@ int mqtt_publish(mqtt_client_t* c, const char* topic_filter, mqtt_message_t* msg platform_mutex_lock(&c->write_lock); - if (msg->qos == QOS1 || msg->qos == QOS2) { + if (QOS0 != msg->qos) { if (mqtt_ack_handler_is_maximum(c)) { - rc = MQTT_ACK_HANDLER_NUM_TOO_MUCH; /* the recorded ack handler has reached the maximum */ + rc = MQTT_ACK_HANDLER_NUM_TOO_MUCH_ERROR; /* the recorded ack handler has reached the maximum */ goto exit; } msg->id = mqtt_get_next_packet_id(c); @@ -1236,54 +1302,46 @@ int mqtt_publish(mqtt_client_t* c, const char* topic_filter, mqtt_message_t* msg exit: platform_mutex_unlock(&c->write_lock); + if ((MQTT_ACK_HANDLER_NUM_TOO_MUCH_ERROR == rc) || (MQTT_MEM_NOT_ENOUGH_ERROR == rc)) { + LOG_W("%s:%d %s()... there is not enough memory space to record...", __FILE__, __LINE__, __FUNCTION__); + + /* record too much retransmitted data, may be disconnected, need to reconnect */ + mqtt_set_client_state(c, CLIENT_STATE_DISCONNECTED); + } + RETURN_ERROR(rc); } -int mqtt_yield(mqtt_client_t* c, int timeout_ms) +int mqtt_list_subscribe_topic(mqtt_client_t* c) { - int rc = MQTT_SUCCESS_ERROR; - client_state_t state; - platform_timer_t timer; - - if (NULL == c) - RETURN_ERROR(MQTT_FAILED_ERROR); - - if (0 == timeout_ms) - timeout_ms = c->cmd_timeout; - - platform_timer_init(&timer); - platform_timer_cutdown(&timer, timeout_ms); + int i = 0; + list_t *curr, *next; + message_handlers_t *msg_handler; - while (!platform_timer_is_expired(&timer)) { - state = mqtt_get_client_state(c); - if (CLIENT_STATE_CLEAN_SESSION == state) { - RETURN_ERROR(MQTT_CLEAN_SESSION_ERROR); - } else if (CLIENT_STATE_CONNECTED != state) { - /* mqtt not connect, need reconnect */ - rc = mqtt_try_reconnect(c); + if (NULL == c) + RETURN_ERROR(MQTT_NULL_VALUE_ERROR); - if (MQTT_RECONNECT_TIMEOUT_ERROR == rc) - RETURN_ERROR(rc); - continue; - } - - /* mqtt connected, handle mqtt packet */ - rc = mqtt_packet_handle(c, &timer); + if (list_is_empty(&c->msg_handler_list)) + LOG_I("%s:%d %s()... there are no subscribed topics...", __FILE__, __LINE__, __FUNCTION__); - if (rc >= 0) { - /* scan ack list, destroy ack handler that have timed out or resend them */ - mqtt_ack_list_scan(c); - - } else if (MQTT_NOT_CONNECT_ERROR == rc) { - LOG_E("%s:%d %s()... mqtt not connect", __FILE__, __LINE__, __FUNCTION__); - - /* reconnect timer cutdown */ - platform_timer_cutdown(&c->reconnect_timer, c->reconnect_try_duration); - } else { - break; + LIST_FOR_EACH_SAFE(curr, next, &c->msg_handler_list) { + msg_handler = LIST_ENTRY(curr, message_handlers_t, list); + /* determine whether a node already exists by mqtt topic, but wildcards are not supported */ + if (NULL != msg_handler->topic_filter) { + LOG_I("%s:%d %s()...[%d] subscribe topic: %s", __FILE__, __LINE__, __FUNCTION__, ++i ,msg_handler->topic_filter); } } - - RETURN_ERROR(rc); + + RETURN_ERROR(MQTT_SUCCESS_ERROR); +} + +int mqtt_set_interceptor_handler(mqtt_client_t* c, interceptor_handler_t handler) +{ + if (NULL == handler) + RETURN_ERROR(MQTT_NULL_VALUE_ERROR); + + c->interceptor_handler = handler; + + RETURN_ERROR(MQTT_SUCCESS_ERROR); } diff --git a/components/connectivity/mqttclient/mqttclient/mqttclient.h b/components/connectivity/mqttclient/mqttclient/mqttclient.h index 5b466ae5..f7c444f5 100644 --- a/components/connectivity/mqttclient/mqttclient/mqttclient.h +++ b/components/connectivity/mqttclient/mqttclient/mqttclient.h @@ -2,7 +2,7 @@ * @Author: jiejie * @Github: https://github.com/jiejieTop * @Date: 2019-12-09 21:31:25 - * @LastEditTime: 2020-03-15 01:12:28 + * @LastEditTime: 2020-04-18 12:29:23 * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. */ #ifndef _MQTTCLIENT_H_ @@ -57,6 +57,7 @@ typedef struct message_data { mqtt_message_t *message; } message_data_t; +typedef void (*interceptor_handler_t)(void* client, message_data_t* msg); typedef void (*message_handler_t)(void* client, message_data_t* msg); typedef void (*reconnect_handler_t)(void* client, void* reconnect_date); @@ -115,6 +116,7 @@ typedef struct mqtt_client { platform_timer_t last_sent; platform_timer_t last_received; connect_params_t *connect_params; + interceptor_handler_t interceptor_handler; } mqtt_client_t; typedef struct client_init_params{ @@ -135,8 +137,8 @@ int mqtt_disconnect(mqtt_client_t* c); int mqtt_subscribe(mqtt_client_t* c, const char* topic_filter, mqtt_qos_t qos, message_handler_t msg_handler); int mqtt_unsubscribe(mqtt_client_t* c, const char* topic_filter); int mqtt_publish(mqtt_client_t* c, const char* topic_filter, mqtt_message_t* msg); -int mqtt_yield(mqtt_client_t* c, int timeout_ms); - +int mqtt_list_subscribe_topic(mqtt_client_t* c); +int mqtt_set_interceptor_handler(mqtt_client_t* c, interceptor_handler_t handler); #endif /* _MQTTCLIENT_H_ */ diff --git a/components/connectivity/mqttclient/platform/TencentOS-tiny/platform_net_socket.c b/components/connectivity/mqttclient/platform/TencentOS-tiny/platform_net_socket.c index 9c9e1971..4582dcd8 100644 --- a/components/connectivity/mqttclient/platform/TencentOS-tiny/platform_net_socket.c +++ b/components/connectivity/mqttclient/platform/TencentOS-tiny/platform_net_socket.c @@ -2,14 +2,14 @@ * @Author: jiejie * @Github: https://github.com/jiejieTop * @Date: 2020-01-10 23:45:59 - * @LastEditTime : 2020-01-13 02:48:53 + * @LastEditTime: 2020-04-25 18:59:28 * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. */ #include "platform_net_socket.h" int platform_net_socket_connect(const char *host, const char *port, int proto) { - int fd, ret = MQTT_SOCKET_UNKNOWN_HOST; + int fd, ret = MQTT_SOCKET_UNKNOWN_HOST_ERROR; #ifdef MQTT_NETSOCKET_USE_AT fd = tos_sal_module_connect(host, port, TOS_SAL_PROTO_TCP); @@ -36,7 +36,7 @@ int platform_net_socket_connect(const char *host, const char *port, int proto) for (cur = addr_list; cur != NULL; cur = cur->ai_next) { fd = socket(cur->ai_family, cur->ai_socktype, cur->ai_protocol); if (fd < 0) { - ret = MQTT_SOCKET_FAILED; + ret = MQTT_SOCKET_FAILED_ERROR; continue; } diff --git a/components/connectivity/mqttclient/test/test.c b/components/connectivity/mqttclient/test/test.c index 6df18d1f..887d1117 100644 --- a/components/connectivity/mqttclient/test/test.c +++ b/components/connectivity/mqttclient/test/test.c @@ -2,7 +2,7 @@ * @Author: jiejie * @Github: https://github.com/jiejieTop * @Date: 2019-12-11 21:53:07 - * @LastEditTime : 2020-01-18 13:54:38 + * @LastEditTime: 2020-04-23 15:03:32 * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. */ #include @@ -17,12 +17,7 @@ extern const char *test_ca_get(); mqtt_client_t client; client_init_params_t init_params; -// static void reconnect_handler(void* client, void* reconnect_date) -// { -// LOG_E("%s:%d %s()...mqtt is reconnecting, reconnect_date is %s", __FILE__, __LINE__, __FUNCTION__, (char*)reconnect_date); -// } - -static void topic_test1_handler(void* client, message_data_t* msg) +static void topic1_handler(void* client, message_data_t* msg) { (void) client; LOG_I("-----------------------------------------------------------------------------------"); @@ -30,38 +25,32 @@ static void topic_test1_handler(void* client, message_data_t* msg) LOG_I("-----------------------------------------------------------------------------------"); } -// void *mqtt_unsubscribe_thread(void *arg) -// { -// sleep(2); -// mqtt_unsubscribe(&client, "test"); - -// // sleep(10); -// mqtt_disconnect(&client); - -// sleep(2); - -// mqtt_connect(&client); - -// pthread_exit(NULL); -// } - void *mqtt_publish_thread(void *arg) { - char buf[80] = { 0 }; + char buf[100] = { 0 }; mqtt_message_t msg; memset(&msg, 0, sizeof(msg)); sprintf(buf, "welcome to mqttclient, this is a publish test..."); - - msg.qos = 2; + + sleep(2); + + mqtt_list_subscribe_topic(&client); + msg.payload = (void *) buf; - // msg.payloadlen = strlen(buf); + while(1) { - mqtt_publish(&client, "testtopic1-acer3", &msg); - mqtt_publish(&client, "testtopic2-acer3", &msg); - mqtt_publish(&client, "testtopic3-acer3", &msg); - mqtt_publish(&client, "testtopic4-acer3", &msg); - // LOG_I("random_number is %d",random_number()); - sleep(2); + sprintf(buf, "welcome to mqttclient, this is a publish test, a rand number: %d ...", random_number()); + + msg.qos = 0; + mqtt_publish(&client, "topic1", &msg); + + msg.qos = 1; + mqtt_publish(&client, "topic2", &msg); + + msg.qos = 2; + mqtt_publish(&client, "topic3", &msg); + + sleep(4); } } @@ -70,41 +59,34 @@ int main(void) int res; // pthread_t thread1; pthread_t thread2; + + printf("\nwelcome to mqttclient test...\n"); + + log_init(); init_params.read_buf_size = 1024; init_params.write_buf_size = 1024; - // init_params.reconnect_date = "this is a test"; - // init_params.reconnect_handler = reconnect_handler; - init_params.connect_params.network_params.network_ssl_params.ca_crt = test_ca_get(); - init_params.connect_params.network_params.addr = "www.jiejie01.top"; //"47.95.164.112";//"jiejie01.top"; //"129.204.201.235"; //"192.168.1.101"; - init_params.connect_params.network_params.port = "8883"; - init_params.connect_params.user_name = random_string(10); // random_string(10); //"jiejietop-acer1"; - init_params.connect_params.password = random_string(10);; //random_string(10); // "123456"; - init_params.connect_params.client_id = random_string(10);; //random_string(10); // "clientid-acer1"; - init_params.connect_params.clean_session = 1; - log_init(); +#ifdef MQTT_NETWORK_TYPE_TLS + init_params.connect_params.network_params.network_ssl_params.ca_crt = test_ca_get(); + init_params.connect_params.network_params.port = "8883"; +#else + init_params.connect_params.network_params.port = "1883"; +#endif + init_params.connect_params.network_params.addr = "www.jiejie01.top"; //"47.95.164.112";//"jiejie01.top"; //"129.204.201.235"; //"192.168.1.101"; + + init_params.connect_params.user_name = random_string(10); // random_string(10); //"jiejietop-acer1"; + init_params.connect_params.password = random_string(10); //random_string(10); // "123456"; + init_params.connect_params.client_id = random_string(10); //random_string(10); // "clientid-acer1"; + init_params.connect_params.clean_session = 1; mqtt_init(&client, &init_params); mqtt_connect(&client); - - LOG_D("mqtt connect success..."); - mqtt_subscribe(&client, "testtopic1-acer3", QOS2, topic_test1_handler); - mqtt_subscribe(&client, "testtopic2-acer3", QOS2, NULL); - mqtt_subscribe(&client, "testtopic3-acer3", QOS2, NULL); - mqtt_subscribe(&client, "testtopic4-acer3", QOS2, NULL); - mqtt_subscribe(&client, "testtopic5-acer3", QOS1, NULL); - mqtt_subscribe(&client, "testtopic6-acer3", QOS2, NULL); - mqtt_subscribe(&client, "testtopic7-acer3", QOS0, NULL); - - // LOG_E("create mqtt publish thread fail"); - // res = pthread_create(&thread1, NULL, mqtt_unsubscribe_thread, NULL); - // if(res != 0) { - // LOG_I("create thread2 fail"); - // exit(res); - // } + mqtt_subscribe(&client, "topic1", QOS0, topic1_handler); + mqtt_subscribe(&client, "topic2", QOS1, NULL); + mqtt_subscribe(&client, "topic3", QOS2, NULL); res = pthread_create(&thread2, NULL, mqtt_publish_thread, NULL); if(res != 0) { @@ -115,4 +97,4 @@ int main(void) while (1) { sleep(100); } -} \ No newline at end of file +} diff --git a/components/connectivity/mqttclient/test/test_ca.c b/components/connectivity/mqttclient/test/test_ca.c index 2c61f7fe..d102d75b 100644 --- a/components/connectivity/mqttclient/test/test_ca.c +++ b/components/connectivity/mqttclient/test/test_ca.c @@ -2,41 +2,42 @@ * @Author: jiejie * @Github: https://github.com/jiejieTop * @Date: 2020-01-12 10:51:11 - * @LastEditTime : 2020-01-16 00:18:45 + * @LastEditTime: 2020-03-21 21:07:24 * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. */ static const char *test_ca_crt = { "-----BEGIN CERTIFICATE-----\r\n" - "MIIFlzCCBH+gAwIBAgIQBf0c/dicZUkWCJnzWyxOuDANBgkqhkiG9w0BAQsFADBy\r\n" + "MIIFrTCCBJWgAwIBAgIQDApSGhCHMtFicAaXgcO0vjANBgkqhkiG9w0BAQsFADBy\r\n" "MQswCQYDVQQGEwJDTjElMCMGA1UEChMcVHJ1c3RBc2lhIFRlY2hub2xvZ2llcywg\r\n" "SW5jLjEdMBsGA1UECxMURG9tYWluIFZhbGlkYXRlZCBTU0wxHTAbBgNVBAMTFFRy\r\n" - "dXN0QXNpYSBUTFMgUlNBIENBMB4XDTE5MDMyOTAwMDAwMFoXDTIwMDMyODEyMDAw\r\n" + "dXN0QXNpYSBUTFMgUlNBIENBMB4XDTIwMDEyOTAwMDAwMFoXDTIxMDQyODEyMDAw\r\n" "MFowGzEZMBcGA1UEAxMQd3d3LmppZWppZTAxLnRvcDCCASIwDQYJKoZIhvcNAQEB\r\n" - "BQADggEPADCCAQoCggEBAKV5ot0wC7L2sa2ABzLDaYYmxbX61FpHYscY8R1R8nUQ\r\n" - "pRfbj8eCNcg6DZf85OX/sSmtqRnk2Z+Nu9ML5KO5sSvVCr/eL0bW8jqlQ39FuyoR\r\n" - "vsLkUP8iXIkHyEulPagYK8yFNCf0eR0c/SQO0U+UI4JrVOtwFHV/y8IbZ9pEboBH\r\n" - "2II2OYC8azavFoUQXVn+niQiYJb9KGN6Jz4mCgVeGXfoKjW5qtQuCtj0r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