| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- #include <stdlib.h>
- #include <string.h>
- #include <stdint.h>
- #include <rtthread.h>
- #define DBG_ENABLE
- #define DBG_SECTION_NAME "mqtt.sample"
- #define DBG_LEVEL DBG_LOG
- #define DBG_COLOR
- #include <rtdbg.h>
- #include "mqtt_client.h"
- /**
- * MQTT URI farmat:
- * domain mode
- * tcp://iot.eclipse.org:1883
- *
- * ipv4 mode
- * tcp://192.168.10.1:1883
- * ssl://192.168.10.1:1884
- *
- * ipv6 mode
- * tcp://[fe80::20c:29ff:fe9a:a07e]:1883
- * ssl://[fe80::20c:29ff:fe9a:a07e]:1884
- */
- #define MQTT_URI "tcp://test.mosquitto.org:1883" //"tcp://mq.tongxinmao.com:18831"
- #define MQTT_SUBTOPIC "/mqtt/test"
- #define MQTT_PUBTOPIC "/mqtt/test"
- #define MQTT_WILLMSG "Goodbye!"
- /* define MQTT client context */
- static mqtt_client client;
- static int is_started = 0;
- static void mqtt_sub_callback(mqtt_client *c, message_data *msg_data)
- {
- *((char *)msg_data->message->payload + msg_data->message->payloadlen) = '\0';
- LOG_D("mqtt sub callback: %.*s %.*s",
- msg_data->topic_name->lenstring.len,
- msg_data->topic_name->lenstring.data,
- msg_data->message->payloadlen,
- (char *)msg_data->message->payload);
- }
- static void mqtt_sub_default_callback(mqtt_client *c, message_data *msg_data)
- {
- *((char *)msg_data->message->payload + msg_data->message->payloadlen) = '\0';
- LOG_D("mqtt sub default callback: %.*s %.*s",
- msg_data->topic_name->lenstring.len,
- msg_data->topic_name->lenstring.data,
- msg_data->message->payloadlen,
- (char *)msg_data->message->payload);
- }
- static void mqtt_connect_callback(mqtt_client *c)
- {
- LOG_D("inter mqtt_connect_callback!");
- }
- static void mqtt_online_callback(mqtt_client *c)
- {
- LOG_D("inter mqtt_online_callback!");
- }
- static void mqtt_offline_callback(mqtt_client *c)
- {
- LOG_D("inter mqtt_offline_callback!");
- }
- static int mqtt_start(int argc, char **argv)
- {
- /* init condata param by using MQTTPacket_connectData_initializer */
- MQTTPacket_connectData condata = MQTTPacket_connectData_initializer;
- static char cid[20] = { 0 };
-
- if (argc != 1)
- {
- rt_kprintf("mqtt_start --start a mqtt worker thread.\n");
- return -1;
- }
- if (is_started)
- {
- LOG_E("mqtt client is already connected.");
- return -1;
- }
- /* config MQTT context param */
- {
- client.isconnected = 0;
- client.uri = MQTT_URI;
- /* generate the random client ID */
- rt_snprintf(cid, sizeof(cid), "rtthread%d", rt_tick_get());
- /* config connect param */
- memcpy(&client.condata, &condata, sizeof(condata));
- client.condata.clientID.cstring = cid;
- // client.condata.username.cstring = "huaning";
- // client.condata.password.cstring = "sjhn20140916";
- client.condata.keepAliveInterval = 30;
- client.condata.cleansession = 1;
- /* config MQTT will param. */
- client.condata.willFlag = 1;
- client.condata.will.qos = 1;
- client.condata.will.retained = 0;
- client.condata.will.topicName.cstring = MQTT_PUBTOPIC;
- client.condata.will.message.cstring = MQTT_WILLMSG;
- /* malloc buffer. */
- client.buf_size = client.readbuf_size = 1024;
- client.buf = rt_calloc(1, client.buf_size);
- client.readbuf = rt_calloc(1, client.readbuf_size);
- if (!(client.buf && client.readbuf))
- {
- LOG_E("no memory for MQTT client buffer!");
- return -1;
- }
- /* set event callback function */
- client.connect_callback = mqtt_connect_callback;
- client.online_callback = mqtt_online_callback;
- client.offline_callback = mqtt_offline_callback;
- /* set subscribe table and event callback */
- client.message_handlers[0].topicFilter = rt_strdup(MQTT_SUBTOPIC);
- client.message_handlers[0].callback = mqtt_sub_callback;
- client.message_handlers[0].qos = QOS1;
- /* set default subscribe event callback */
- client.default_message_handlers = mqtt_sub_default_callback;
- }
-
- {
- int value;
- uint16_t u16Value;
- value = 5;
- paho_mqtt_control(&client, MQTT_CTRL_SET_CONN_TIMEO, &value);
- value = 5;
- paho_mqtt_control(&client, MQTT_CTRL_SET_MSG_TIMEO, &value);
- value = 5;
- paho_mqtt_control(&client, MQTT_CTRL_SET_RECONN_INTERVAL, &value);
- value = 30;
- paho_mqtt_control(&client, MQTT_CTRL_SET_KEEPALIVE_INTERVAL, &value);
- u16Value = 3;
- paho_mqtt_control(&client, MQTT_CTRL_SET_KEEPALIVE_COUNT, &u16Value);
- }
- /* run mqtt client */
- paho_mqtt_start(&client, 8196, 20);
- is_started = 1;
- return 0;
- }
- static int mqtt_stop(int argc, char **argv)
- {
- if (argc != 1)
- {
- rt_kprintf("mqtt_stop --stop mqtt worker thread and free mqtt client object.\n");
- }
- is_started = 0;
- return paho_mqtt_stop(&client);
- }
- static int mqtt_publish(int argc, char **argv)
- {
- if (is_started == 0)
- {
- LOG_E("mqtt client is not connected.");
- return -1;
- }
- if (argc == 2)
- {
- paho_mqtt_publish(&client, QOS1, MQTT_PUBTOPIC, argv[1], strlen(argv[1]));
- }
- else if (argc == 3)
- {
- paho_mqtt_publish(&client, QOS1, argv[1], argv[2],strlen(argv[2]));
- }
- else
- {
- rt_kprintf("mqtt_publish <topic> [message] --mqtt publish message to specified topic.\n");
- return -1;
- }
- return 0;
- }
- static void mqtt_new_sub_callback(mqtt_client *client, message_data *msg_data)
- {
- *((char *)msg_data->message->payload + msg_data->message->payloadlen) = '\0';
- LOG_D("mqtt new subscribe callback: %.*s %.*s",
- msg_data->topic_name->lenstring.len,
- msg_data->topic_name->lenstring.data,
- msg_data->message->payloadlen,
- (char *)msg_data->message->payload);
- }
- static int mqtt_subscribe(int argc, char **argv)
- {
- if (argc != 2)
- {
- rt_kprintf("mqtt_subscribe [topic] --send an mqtt subscribe packet and wait for suback before returning.\n");
- return -1;
- }
-
- if (is_started == 0)
- {
- LOG_E("mqtt client is not connected.");
- return -1;
- }
- return paho_mqtt_subscribe(&client, QOS1, argv[1], mqtt_new_sub_callback);
- }
- static int mqtt_unsubscribe(int argc, char **argv)
- {
- if (argc != 2)
- {
- rt_kprintf("mqtt_unsubscribe [topic] --send an mqtt unsubscribe packet and wait for suback before returning.\n");
- return -1;
- }
-
- if (is_started == 0)
- {
- LOG_E("mqtt client is not connected.");
- return -1;
- }
- return paho_mqtt_unsubscribe(&client, argv[1]);
- }
- #ifdef FINSH_USING_MSH
- MSH_CMD_EXPORT(mqtt_start, startup mqtt client);
- MSH_CMD_EXPORT(mqtt_stop, stop mqtt client);
- MSH_CMD_EXPORT(mqtt_publish, mqtt publish message to specified topic);
- MSH_CMD_EXPORT(mqtt_subscribe, mqtt subscribe topic);
- MSH_CMD_EXPORT(mqtt_unsubscribe, mqtt unsubscribe topic);
- #endif /* FINSH_USING_MSH */
|