1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- /*
- * @Author:
- * @Github:
- * @Date:
- * @LastEditTime :
- * @Description:
- */
- #include <stdio.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include "mqtt_config.h"
- #include "mqtt_log.h"
- #include "mqttclient.h"
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- int msgQueueID;
- static void topic1_handler(void* client, message_data_t* msg);
- void *mqtt_publish_thread(void *arg)
- {
- mqtt_client_t *client = (mqtt_client_t *)arg;
- char buf[100] = { 0 };
- mqtt_message_t msg;
- memset(&msg, 0, sizeof(msg));
- sprintf(buf, "welcome to mqttclient, this is a publish test...");
- sleep(2);
- mqtt_list_subscribe_topic(client);
- msg.payload = (void *) buf;
- msg.qos = 0;
- while(1) {
- sprintf(buf, "welcome to mqttclient, this is a publish test, a rand number: %d ...", random_number());
- mqtt_publish(client, "topic1", &msg);
- sleep(4);
- }
- }
- /*
- * mqtt初始化IP:36.134.213.14 1883
- * */
- int mqtt_init_connect(mqtt_client_t *client,char *client_id,char *user_name,char *password,char *ip,char *port)
- {
- mqtt_set_port(client, port);
- mqtt_set_host(client, ip);
- mqtt_set_client_id(client, client_id);
- mqtt_set_user_name(client, user_name);
- mqtt_set_password(client, password);
- mqtt_set_clean_session(client, 1);
- return mqtt_connect(client);
- }
- //创建mqtt任务
- void create_mqtt_task()
- {
- mqtt_client_t *client = NULL;
- pthread_t thread1;
- client = mqtt_lease();
- mqtt_log_init();
- if(NULL==client)
- {
- MQTT_LOG_E("client malloc memory error");
- exit(1);
- }
- int rc=mqtt_init_connect(client,"hisi_test",NULL,NULL,"36.134.23.11","1883");
- if(rc!=0)
- {
- MQTT_LOG_E("mqtt connect error please check config");
- exit(rc);
- }
- mqtt_subscribe(client, "rec_mqtt", QOS0, topic1_handler);
- rc = pthread_create(&thread1, NULL, mqtt_publish_thread, client);
- if(rc != 0) {
- MQTT_LOG_E("create mqtt publish thread fail");
- exit(rc);
- }
- }
- /*
- * 所订阅消息的处理函数
- */
- static void topic1_handler(void* client, message_data_t* msg)
- {
- (void) client;
- MQTT_LOG_I("-----------------------------------------------------------------------------------");
- MQTT_LOG_I("%s:%d %s()...\ntopic: %s\nmessage:%s", __FILE__, __LINE__, __FUNCTION__, msg->topic_name, (char*)msg->message->payload);
- MQTT_LOG_I("-----------------------------------------------------------------------------------");
- }
|