mqtt client release v1.1.0 ...

This commit is contained in:
jiejietop
2020-06-18 20:29:15 +08:00
parent b203904468
commit d58bd1e84d
5 changed files with 79 additions and 83 deletions

View File

@@ -10,7 +10,7 @@
int platform_net_socket_connect(const char *host, const char *port, int proto) int platform_net_socket_connect(const char *host, const char *port, int proto)
{ {
int fd, ret = MQTT_SOCKET_UNKNOWN_HOST_ERROR; int fd, ret = MQTT_SOCKET_UNKNOWN_HOST_ERROR;
#ifdef MQTT_NETSOCKET_USE_AT #ifdef MQTT_NETSOCKET_USING_AT
fd = tos_sal_module_connect(host, port, TOS_SAL_PROTO_TCP); fd = tos_sal_module_connect(host, port, TOS_SAL_PROTO_TCP);
@@ -57,7 +57,7 @@ int platform_net_socket_connect(const char *host, const char *port, int proto)
int platform_net_socket_recv(int fd, void *buf, size_t len, int flags) int platform_net_socket_recv(int fd, void *buf, size_t len, int flags)
{ {
#ifdef MQTT_NETSOCKET_USE_AT #ifdef MQTT_NETSOCKET_USING_AT
return tos_sal_module_recv(fd, buf, len); return tos_sal_module_recv(fd, buf, len);
#else #else
return recv(fd, buf, len, flags); return recv(fd, buf, len, flags);
@@ -66,7 +66,7 @@ int platform_net_socket_recv(int fd, void *buf, size_t len, int flags)
int platform_net_socket_recv_timeout(int fd, unsigned char *buf, int len, int timeout) int platform_net_socket_recv_timeout(int fd, unsigned char *buf, int len, int timeout)
{ {
#ifdef MQTT_NETSOCKET_USE_AT #ifdef MQTT_NETSOCKET_USING_AT
return tos_sal_module_recv_timeout(fd, buf, len, timeout); return tos_sal_module_recv_timeout(fd, buf, len, timeout);
#else #else
int nread; int nread;
@@ -103,7 +103,7 @@ int platform_net_socket_recv_timeout(int fd, unsigned char *buf, int len, int ti
int platform_net_socket_write(int fd, void *buf, size_t len) int platform_net_socket_write(int fd, void *buf, size_t len)
{ {
#ifdef MQTT_NETSOCKET_USE_AT #ifdef MQTT_NETSOCKET_USING_AT
return tos_sal_module_send(fd, buf, len); return tos_sal_module_send(fd, buf, len);
#else #else
return write(fd, buf, len); return write(fd, buf, len);
@@ -112,7 +112,7 @@ 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) int platform_net_socket_write_timeout(int fd, unsigned char *buf, int len, int timeout)
{ {
#ifdef MQTT_NETSOCKET_USE_AT #ifdef MQTT_NETSOCKET_USING_AT
return tos_sal_module_send(fd, buf, len); return tos_sal_module_send(fd, buf, len);
#else #else
struct timeval tv = { struct timeval tv = {
@@ -133,14 +133,14 @@ int platform_net_socket_write_timeout(int fd, unsigned char *buf, int len, int t
int platform_net_socket_close(int fd) int platform_net_socket_close(int fd)
{ {
#ifdef MQTT_NETSOCKET_USE_AT #ifdef MQTT_NETSOCKET_USING_AT
return tos_sal_module_close(fd); return tos_sal_module_close(fd);
#else #else
return close(fd); return close(fd);
#endif #endif
} }
#ifndef MQTT_NETSOCKET_USE_AT #ifndef MQTT_NETSOCKET_USING_AT
int platform_net_socket_set_block(int fd) int platform_net_socket_set_block(int fd)
{ {

View File

@@ -11,7 +11,7 @@
#include "network.h" #include "network.h"
#include "mqtt_error.h" #include "mqtt_error.h"
#ifdef MQTT_NETSOCKET_USE_AT #ifdef MQTT_NETSOCKET_USING_AT
#include "sal_module_wrapper.h" #include "sal_module_wrapper.h"
@@ -35,7 +35,7 @@ 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); int platform_net_socket_write_timeout(int fd, unsigned char *buf, int len, int timeout);
int platform_net_socket_close(int fd); int platform_net_socket_close(int fd);
#ifndef MQTT_NETSOCKET_USE_AT #ifndef MQTT_NETSOCKET_USING_AT
int platform_net_socket_set_block(int fd); int platform_net_socket_set_block(int fd);
int platform_net_socket_set_nonblock(int fd); int platform_net_socket_set_nonblock(int fd);
int platform_net_socket_setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen); int platform_net_socket_setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen);

View File

@@ -7,16 +7,13 @@
k_task_t task; k_task_t task;
k_stack_t task_stack[2048]; k_stack_t task_stack[2048];
mqtt_client_t client;
client_init_params_t init_params;
static void tos_topic_handler(void* client, message_data_t* msg) static void tos_topic_handler(void* client, message_data_t* msg)
{ {
(void) client; (void) client;
LOG_I("-----------------------------------------------------------------------------------"); MQTT_LOG_I("-----------------------------------------------------------------------------------");
LOG_I("%s:%d %s()...\ntopic: %s, qos: %d. \nmessage:\n\t%s\n", __FILE__, __LINE__, __FUNCTION__, MQTT_LOG_I("%s:%d %s()...\ntopic: %s, qos: %d. \nmessage:\n\t%s\n", __FILE__, __LINE__, __FUNCTION__,
msg->topic_name, msg->message->qos, (char*)msg->message->payload); msg->topic_name, msg->message->qos, (char*)msg->message->payload);
LOG_I("-----------------------------------------------------------------------------------\n"); MQTT_LOG_I("-----------------------------------------------------------------------------------\n");
} }
@@ -25,41 +22,41 @@ void mqttclient_task(void *Parameter)
int error; int error;
char buf[100] = { 0 }; char buf[100] = { 0 };
mqtt_message_t msg; mqtt_message_t msg;
mqtt_client_t *client = NULL;
memset(&msg, 0, sizeof(msg)); memset(&msg, 0, sizeof(msg));
printf("\nwelcome to mqttclient test...\n");
esp8266_sal_init(HAL_UART_PORT_2); esp8266_sal_init(HAL_UART_PORT_2);
esp8266_join_ap("wifii", "woshijiejie"); esp8266_join_ap("wifii", "woshijiejie");
init_params.read_buf_size = 256; mqtt_log_init();
init_params.write_buf_size = 256;
#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"; client = mqtt_lease();
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(); mqtt_set_port(client, "1883");
mqtt_set_host(client, "www.jiejie01.top");
mqtt_set_client_id(client, random_string(10));
mqtt_set_user_name(client, random_string(10));
mqtt_set_password(client, random_string(10));
mqtt_set_clean_session(client, 1);
mqtt_set_read_buf_size(client, 256);
mqtt_set_write_buf_size(client, 256);
mqtt_init(&client, &init_params); error = mqtt_connect(client);
MQTT_LOG_D("mqtt connect error is %#x", error);
error = mqtt_connect(&client); error = mqtt_connect(client);
LOG_D("mqtt connect error is %#x", error); MQTT_LOG_D("mqtt connect error is %#x", error);
mqtt_subscribe(&client, "tos-topic", QOS0, tos_topic_handler); mqtt_subscribe(client, "tos-topic", QOS0, tos_topic_handler);
LOG_D("mqtt subscribe error is %#x", error); MQTT_LOG_D("mqtt subscribe error is %#x", error);
memset(&msg, 0, sizeof(msg)); memset(&msg, 0, sizeof(msg));
@@ -70,7 +67,7 @@ void mqttclient_task(void *Parameter)
msg.qos = QOS0; msg.qos = QOS0;
msg.payload = (void *) buf; msg.payload = (void *) buf;
error = mqtt_publish(&client, "tos-topic", &msg); error = mqtt_publish(client, "tos-topic", &msg);
tos_task_delay(4000); tos_task_delay(4000);
} }

View File

@@ -16,27 +16,28 @@
#include "mqttclient.h" #include "mqttclient.h"
#define TEST_USEING_TLS
extern const char *test_ca_get(void); extern const char *test_ca_get(void);
static void tos_topic_handler(void* client, message_data_t* msg) static void tos_topic_handler(void* client, message_data_t* msg)
{ {
(void) client; (void) client;
LOG_I("-----------------------------------------------------------------------------------"); MQTT_LOG_I("-----------------------------------------------------------------------------------");
LOG_I("%s:%d %s()...\ntopic: %s, qos: %d. \nmessage:\n\t%s\n", __FILE__, __LINE__, __FUNCTION__, MQTT_LOG_I("%s:%d %s()...\ntopic: %s, qos: %d. \nmessage:\n\t%s\n", __FILE__, __LINE__, __FUNCTION__,
msg->topic_name, msg->message->qos, (char*)msg->message->payload); msg->topic_name, msg->message->qos, (char*)msg->message->payload);
LOG_I("-----------------------------------------------------------------------------------\n"); MQTT_LOG_I("-----------------------------------------------------------------------------------\n");
} }
mqtt_client_t client;
client_init_params_t init_params;
extern void TCPIP_Init(void); extern void TCPIP_Init(void);
void application_entry(void *arg) void application_entry(void *arg)
{ {
int error; int error;
char buf[80] = { 0 }; char buf[80] = { 0 };
mqtt_client_t *client = NULL;
mqtt_message_t msg; mqtt_message_t msg;
memset(&msg, 0, sizeof(msg)); memset(&msg, 0, sizeof(msg));
@@ -44,33 +45,30 @@ void application_entry(void *arg)
TCPIP_Init(); TCPIP_Init();
init_params.read_buf_size = 1024; mqtt_log_init();
init_params.write_buf_size = 1024;
client = mqtt_lease();
#ifdef MQTT_NETWORK_TYPE_TLS
init_params.connect_params.network_params.network_ssl_params.ca_crt = test_ca_get(); #ifdef TEST_USEING_TLS
init_params.connect_params.network_params.port = "8883"; mqtt_set_port(client, "8883");
mqtt_set_ca(client, (char*)test_ca_get());
#else #else
init_params.connect_params.network_params.port = "1883"; mqtt_set_port(client, "1883");
#endif #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); mqtt_set_host(client, "www.jiejie01.top");
init_params.connect_params.password = random_string(10); mqtt_set_client_id(client, random_string(10));
init_params.connect_params.client_id = random_string(10); mqtt_set_user_name(client, random_string(10));
init_params.connect_params.clean_session = 1; mqtt_set_password(client, random_string(10));
mqtt_set_clean_session(client, 1);
log_init(); error = mqtt_connect(client);
mqtt_init(&client, &init_params);
error = mqtt_connect(&client);
LOG_D("mqtt connect error is %#x", error); MQTT_LOG_D("mqtt connect error is %#x", error);
mqtt_subscribe(&client, "tos-topic", QOS0, tos_topic_handler); mqtt_subscribe(client, "tos-topic", QOS0, tos_topic_handler);
LOG_D("mqtt subscribe error is %#x", error); MQTT_LOG_D("mqtt subscribe error is %#x", error);
memset(&msg, 0, sizeof(msg)); memset(&msg, 0, sizeof(msg));
@@ -81,7 +79,7 @@ void application_entry(void *arg)
msg.qos = QOS0; msg.qos = QOS0;
msg.payload = (void *) buf; msg.payload = (void *) buf;
mqtt_publish(&client, "tos-topic", &msg); mqtt_publish(client, "tos-topic", &msg);
tos_task_delay(4000); tos_task_delay(4000);
} }

View File

@@ -19,15 +19,14 @@
static void tos_topic_handler(void* client, message_data_t* msg) static void tos_topic_handler(void* client, message_data_t* msg)
{ {
(void) client; (void) client;
LOG_I("-----------------------------------------------------------------------------------"); MQTT_LOG_I("-----------------------------------------------------------------------------------");
LOG_I("%s:%d %s()...\ntopic: %s, qos: %d. \nmessage:\n\t%s\n", __FILE__, __LINE__, __FUNCTION__, MQTT_LOG_I("%s:%d %s()...\ntopic: %s, qos: %d. \nmessage:\n\t%s\n", __FILE__, __LINE__, __FUNCTION__,
msg->topic_name, msg->message->qos, (char*)msg->message->payload); msg->topic_name, msg->message->qos, (char*)msg->message->payload);
LOG_I("-----------------------------------------------------------------------------------\n"); MQTT_LOG_I("-----------------------------------------------------------------------------------\n");
} }
mqtt_client_t client; mqtt_client_t client;
client_init_params_t init_params;
extern void TCPIP_Init(void); extern void TCPIP_Init(void);
@@ -36,39 +35,41 @@ void application_entry(void *arg)
int error; int error;
char buf[80] = { 0 }; char buf[80] = { 0 };
mqtt_message_t msg; mqtt_message_t msg;
mqtt_client_t *client = NULL;
memset(&msg, 0, sizeof(msg)); memset(&msg, 0, sizeof(msg));
sprintf(buf, "welcome to mqttclient, this is a publish test..."); printf("\nwelcome to mqttclient test...\n");
TCPIP_Init(); TCPIP_Init();
init_params.read_buf_size = 1024; mqtt_log_init();
init_params.write_buf_size = 1024;
init_params.connect_params.network_params.addr = "iotcloud-mqtt.gz.tencentdevices.com";
init_params.connect_params.network_params.port = "1883";
init_params.connect_params.user_name = "7ABAKP6KNCtest1;12010126;KGRDH;1618120908";
init_params.connect_params.password = "35c065988c4beeb32d8a2f1f283a7a1535e79295f4b6368f2ec61ee7b1b8c271;hmacsha256";
init_params.connect_params.client_id = "7ABAKP6KNCtest1";
init_params.connect_params.clean_session = 1;
log_init(); client = mqtt_lease();
mqtt_init(&client, &init_params); mqtt_set_port(client, "1883");
error = mqtt_connect(&client); mqtt_set_host(client, "iotcloud-mqtt.gz.tencentdevices.com");
mqtt_set_client_id(client, "7ABAKP6KNCtest1");
mqtt_set_user_name(client, "7ABAKP6KNCtest1;12010126;KGRDH;1618120908");
mqtt_set_password(client, "35c065988c4beeb32d8a2f1f283a7a1535e79295f4b6368f2ec61ee7b1b8c271;hmacsha256");
mqtt_set_clean_session(client, 1);
LOG_D("mqtt connect error is %#x", error); error = mqtt_connect(client);
mqtt_subscribe(&client, "7ABAKP6KNC/test1/data", QOS0, tos_topic_handler); MQTT_LOG_D("mqtt connect error is %#x", error);
mqtt_subscribe(client, "7ABAKP6KNC/test1/data", QOS0, tos_topic_handler);
memset(&msg, 0, sizeof(msg)); memset(&msg, 0, sizeof(msg));
sprintf(buf, "welcome to mqttclient, this is a publish test...");
for (;;) { for (;;) {
msg.qos = QOS0; msg.qos = QOS0;
msg.payload = (void *) buf; msg.payload = (void *) buf;
mqtt_publish(&client, "7ABAKP6KNC/test1/data", &msg); mqtt_publish(client, "7ABAKP6KNC/test1/data", &msg);
tos_task_delay(4000); tos_task_delay(4000);
} }