liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame^] | 1 | #include <stdio.h> |
| 2 | #include "mbtk_mqtt.h" |
| 3 | #include "pthread.h" |
| 4 | #include "string.h" |
| 5 | #include "unistd.h" |
| 6 | #include "sys/stat.h" |
| 7 | #include "sys/types.h" |
| 8 | #include "sys/socket.h" |
| 9 | #include "netinet/in.h" |
| 10 | #include "arpa/inet.h" |
| 11 | #include "fcntl.h" |
| 12 | #include "mbtk_sock2.h" |
| 13 | /* |
| 14 | typedef struct { |
| 15 | char clientId[255]; |
| 16 | char deviceToken[255]; |
| 17 | } regnwl_info_t; |
| 18 | */ |
| 19 | |
| 20 | int aliyun_xiyinyimi_connect(mbtk_mqtt_device_session_t *device, regnwl_info_t *regn); |
| 21 | |
| 22 | struct opts_struct opts = { |
| 23 | (char *)"iot-dev", 0, (char *)"\n", QOS0, "admin", "password", (char *)"localhost", 1883, 0 |
| 24 | };//初始化结构体 |
| 25 | |
| 26 | iot_device_info_t gateway = { |
| 27 | .iotstatus = IOT_STATUS_LOGIN, |
| 28 | .model = {"2022"}, |
| 29 | .company = {"/my"} |
| 30 | };//初始化主题 |
| 31 | |
| 32 | static Cloud_MQTT_t *miot_mqtt; |
| 33 | |
| 34 | void set_mqtt_t(Cloud_MQTT_t *piot_mqtt) |
| 35 | { |
| 36 | miot_mqtt = piot_mqtt; |
| 37 | } |
| 38 | Cloud_MQTT_t * get_mqtt_t() |
| 39 | { |
| 40 | return miot_mqtt; |
| 41 | } |
| 42 | |
| 43 | void mqtt_data_rx_cb(void *pbuf, int len) |
| 44 | { |
| 45 | |
| 46 | printf("data = %s\n", (unsigned char *)pbuf); //打印接收到的数据 |
| 47 | } |
| 48 | |
| 49 | |
| 50 | void MQTTMessageArrived_Cb(MessageData* md) |
| 51 | { |
| 52 | MQTTMessage *message = md->message; |
| 53 | |
| 54 | Cloud_MQTT_t *piot_mqtt = get_mqtt_t(); |
| 55 | |
| 56 | if (NULL != piot_mqtt->DataArrived_Cb) { |
| 57 | piot_mqtt->DataArrived_Cb((void *)message->payload, message->payloadlen);//异步消息体 |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | |
| 62 | void *mqtt_thread(void *arg) |
| 63 | { |
| 64 | while (gateway.iotstatus != IOT_STATUS_DROP){ |
| 65 | printf("gateway.iotstatus is %d\n",gateway.iotstatus); |
| 66 | iot_yield(arg,&gateway); //维持服务器稳定,断开重连 |
| 67 | } |
| 68 | printf("gateway.iotstatus is IOT_STATUS_DROP"); |
| 69 | // mqtt_device_disconnect(arg); |
| 70 | pthread_exit(NULL); |
| 71 | return NULL; |
| 72 | } |
| 73 | |
| 74 | |
| 75 | |
| 76 | //int mbtk_aliyun_mqtt_register_init(mbtk_mqtt_device_session_t *mbtk_mqtt_device ) |
| 77 | int main(int argc, char *argv[]) |
| 78 | { |
| 79 | |
| 80 | printf("aliyun mqtt start2\n"); |
| 81 | int ret, len; |
| 82 | char will_msg[256] = {"hello world"}; //初始化遗嘱数据 |
| 83 | static int i = 0; |
| 84 | |
| 85 | mbtk_mqtt_device_session_t mbtk_mqtt_device = {0}; |
| 86 | memcpy(mbtk_mqtt_device.device_name, argv[1] , strlen(argv[1])); |
| 87 | memcpy(mbtk_mqtt_device.product_key, "a14Vv4QUPeP" , strlen("a14Vv4QUPeP")); |
| 88 | memcpy(mbtk_mqtt_device.product_secret, "GiBigxEaluyAPnD4" , strlen( "GiBigxEaluyAPnD4")); |
| 89 | mbtk_mqtt_device.host = 0; |
| 90 | mbtk_mqtt_device.port = 1883; |
| 91 | |
| 92 | |
| 93 | Cloud_MQTT_t *Iot_mqtt; |
| 94 | Iot_mqtt = (Cloud_MQTT_t *)malloc(sizeof(Cloud_MQTT_t)); |
| 95 | memset(Iot_mqtt,0x0,sizeof(Cloud_MQTT_t)); |
| 96 | |
| 97 | //一型一密免预注册mqtt |
| 98 | mbtk_aliyun_mqtt_one_type_one_secret_unregin_set_info_init(Iot_mqtt, &mbtk_mqtt_device,60*1000,4,mqtt_data_rx_cb); |
| 99 | |
| 100 | mqtt_will_msg_set(Iot_mqtt, will_msg, strlen(will_msg)); //设置遗嘱 |
| 101 | set_mqtt_t(Iot_mqtt); |
| 102 | ret = mbtk_aliyun_mqtt_one_type_one_secret_unregin_device_connect(Iot_mqtt); //初始化并连接mqtt服务器 |
| 103 | |
| 104 | printf("connect success\n"); |
| 105 | int rc = mbtk_MQTTSubscribe(&Iot_mqtt->Client, Iot_mqtt->sub_topic, opts.qos, MQTTMessageArrived_Cb); |
| 106 | if (rc) { |
| 107 | printf("mqtt subscribe fail \n"); |
| 108 | char clientId[255] = {0}; |
| 109 | char deviceToken[255] = {0}; |
| 110 | if(! mbtk_aliyun_mqtt_one_type_one_secret_unregin_get_regin_info(clientId, deviceToken) ) |
| 111 | { |
| 112 | regnwl_info_t regn_info = {0}; |
| 113 | memcpy(regn_info.clientId,clientId , strlen(clientId)); |
| 114 | memcpy(regn_info.deviceToken, deviceToken , strlen(deviceToken)); |
| 115 | printf("example\n"); |
| 116 | printf("%s\n, %s\n", clientId, deviceToken); |
| 117 | aliyun_xiyinyimi_connect(&mbtk_mqtt_device, ®n_info); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | int aliyun_xiyinyimi_connect(mbtk_mqtt_device_session_t *device, regnwl_info_t *regn) |
| 125 | { |
| 126 | |
| 127 | int ret, len; |
| 128 | char will_msg[256] = {"hello world"}; //初始化遗嘱数据 |
| 129 | static int retry_count = 0; |
| 130 | pthread_t thread_ID; //定义线程id |
| 131 | static int i = 0; |
| 132 | |
| 133 | Cloud_MQTT_t *Iot_mqtt; |
| 134 | Iot_mqtt = (Cloud_MQTT_t *)malloc(sizeof(Cloud_MQTT_t)); |
| 135 | memset(Iot_mqtt,0x0,sizeof(Cloud_MQTT_t)); |
| 136 | |
| 137 | |
| 138 | iot_aliyun_mqtt_one_type_one_secret_unregin_connect_init(Iot_mqtt,device, regn->clientId, regn->deviceToken, 30,3,"aaa","aaa",mqtt_data_rx_cb); |
| 139 | |
| 140 | // iot_mqtt_init(Iot_mqtt,host,port, client, user, pass,30,3,"aaa","aaa",mqtt_data_rx_cb); |
| 141 | |
| 142 | mqtt_will_msg_set(Iot_mqtt, will_msg, strlen(will_msg)); //设置遗嘱 |
| 143 | |
| 144 | set_mqtt_t(Iot_mqtt); |
| 145 | ret = mqtt_device_connect(Iot_mqtt); //初始化并连接mqtt服务器 |
| 146 | |
| 147 | |
| 148 | while (ret < 0 && retry_count <= 5) { |
| 149 | printf("ret = %d\r\n", ret); |
| 150 | retry_count ++ ; |
| 151 | printf("retry_count = %d\n",retry_count); |
| 152 | sleep(3); |
| 153 | ret = mqtt_device_connect(Iot_mqtt); |
| 154 | } |
| 155 | if(ret < 0 && retry_count > 5) |
| 156 | { |
| 157 | printf("reconnect times more than 5 exit\n"); |
| 158 | return NULL; |
| 159 | } |
| 160 | retry_count = 0; |
| 161 | |
| 162 | printf("connect success\n"); |
| 163 | int rc = mbtk_MQTTSubscribe(&Iot_mqtt->Client, Iot_mqtt->sub_topic, opts.qos, MQTTMessageArrived_Cb); |
| 164 | if (rc) { |
| 165 | printf("mqtt subscribe fail \n"); |
| 166 | ret = -105; |
| 167 | return 0; |
| 168 | } |
| 169 | gateway.iotstatus = IOT_STATUS_CONNECT; |
| 170 | |
| 171 | pthread_create(&thread_ID, NULL, &mqtt_thread, (void *)Iot_mqtt); //创建一个线程执行mqtt客户端 |
| 172 | pthread_detach(thread_ID); //设置线程结束收尸 |
| 173 | |
| 174 | while (1) |
| 175 | { |
| 176 | mbtk_MQTTPublish("my yes", 6, 0,&Iot_mqtt->Client,Iot_mqtt->pub_topic,2,0);//循环发布"my yes" |
| 177 | sleep(3); //睡眠3s |
| 178 | i++; |
| 179 | if (i > 5) |
| 180 | { |
| 181 | mbtk_MQTTUnsubscribe(&Iot_mqtt->Client,Iot_mqtt->sub_topic); |
| 182 | break; |
| 183 | } |
| 184 | } |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | |
| 189 | |
| 190 | |