blob: 3b78429473456589537abfd4f8bca2b3e5a2e7af [file] [log] [blame]
liubin281ac462023-07-19 14:22:54 +08001/*************************************************************************
2 > File Name: main.c
3 > Author: LFJ
b.liubcf86c92024-08-19 19:48:28 +08004 > Mail:
liubin281ac462023-07-19 14:22:54 +08005 > Created Time: 2018年09月05日 星期三 13时48分17秒
6 ************************************************************************/
7
8#include <stdio.h>
9
10#include "MQTTClient.h"
11#include "mbtk_mqtt.h"
12#include "pthread.h"
13#include "string.h"
14#include "unistd.h"
15#include "sys/stat.h"
16#include "sys/types.h"
17#include "sys/socket.h"
18#include "netinet/in.h"
19#include "arpa/inet.h"
20#include "fcntl.h"
21#include "mbtk_sock2.h"
22
23#include "mbtk_type.h"
24#include "mbtk_http.h"
b.liubcf86c92024-08-19 19:48:28 +080025#include "mbtk_log.h"
liubin281ac462023-07-19 14:22:54 +080026
27/*
28static Cloud_MQTT_t *iot_mqtt;
29
30void set_mqtt_t(Cloud_MQTT_t *piot_mqtt)
31{
32 iot_mqtt = piot_mqtt;
33}
34Cloud_MQTT_t * get_mqtt_t()
35{
36 return iot_mqtt;
37}
38*/
39
40extern regnwl_info_t regnwl_info;
41
42
43#define CORE_AUTH_SDK_VERSION "sdk-c-4.1.0"
44#define CORE_AUTH_TIMESTAMP "2524608000000"
45#define MBTK_MQTT_HTTP_AUTH_PATH "/auth/register/device"
46#define MBTK_MQTT_HTTP_SIGN_METHOD_HMACSHA256 "hmacsha256"
47
48
49const char * mqtt_url[7] = {
50 "iot-as-mqtt.cn-shanghai.aliyuncs.com", /* Shanghai */
51 "iot-as-mqtt.ap-southeast-1.aliyuncs.com", /* Singapore */
52 "iot-as-mqtt.ap-northeast-1.aliyuncs.com", /* Japan */
53 "iot-as-mqtt.us-west-1.aliyuncs.com", /* America */
54 "iot-as-mqtt.eu-central-1.aliyuncs.com", /* Germany */
55 "118.114.239.159", /* me */
56 NULL, /* Custom */
57 };
58
59
60const char *http_host[6] = {
61 "iot-auth.cn-shanghai.aliyuncs.com", /* Shanghai */
62 "iot-auth.ap-southeast-1.aliyuncs.com", /* Singapore */
63 "iot-auth.ap-northeast-1.aliyuncs.com", /* Japan */
64 "iot-auth.us-west-1.aliyuncs.com", /* America */
65 "iot-auth.eu-central-1.aliyuncs.com", /* Germany */
66 NULL, /* Custom */
67 };
68
69mbtk_mqtt_device_session_t mbtk_mqtt_device ={0};
70
71
72char mbtk_mqtt_http_url[255] ={0};
73char mbtk_mqtt_http_content[1024] ={0};
74
75char Device_Secret[255] = {0};
76
77
78int mbtk_imqtt_http_dynreg_sign(char *product_key,char* product_secret,char*device_name,char* random,char* sign)
79{
80 int sign_source_len = 0;
81 uint8_t signnum[32];
82 uint8_t *sign_source = NULL;
83 const char *dynamic_register_sign_fmt = "deviceName%sproductKey%srandom%s";
84
85 /* Start Dynamic Register */
86 /* Calculate SHA256 Value */
87 sign_source_len = strlen(dynamic_register_sign_fmt) + strlen(device_name) + strlen(product_key) + strlen(random) + 1;
88 sign_source = malloc(sign_source_len);
89 if (sign_source == NULL)
90 {
91 return -1;
92 }
93 memset(sign_source, 0, sign_source_len);
94 snprintf((char *)sign_source, sign_source_len, dynamic_register_sign_fmt, device_name, product_key, random);
95
96 core_hmac_sha256(sign_source, strlen((const char *)sign_source), (uint8_t *)product_secret, strlen(product_secret),
97 signnum);
98 core_hex2str(signnum, 32, sign,0);
99 free(sign_source);
100 sign_source = NULL;
101
102 return 0;
103
104}
105
106
107int mbtk_imqtt_send_post_request(char *product_key, char *product_secret, char *device_name, int host)
108{
109
110 printf("product_key: %s\n", product_key);
111 printf("product_secret: %s\n", product_secret);
112 printf("device_name: %s\n", device_name);
113
114 int32_t res = 0, content_len = 0;
115 char content[255] = {0};
116 char random[15+1]={0};;
117 char sign[65] = {0};
118 int dynamic_register_request_len = 0;
119// char *content_fmt = "productKey=%s&deviceName=%s&random=%s&sign=%s&signMethod=%s";
120 char *signMethod = MBTK_MQTT_HTTP_SIGN_METHOD_HMACSHA256;
121
122
123 memcpy(random, "8Ygb7ULYh53B6OA", strlen("8Ygb7ULYh53B6OA"));
124 char *content_src[] = { product_key, device_name, random,sign,signMethod};
125 mbtk_imqtt_http_dynreg_sign(product_key,product_secret,device_name,random,sign);
126
127 sprintf(content, "productKey=%s&deviceName=%s&random=%s&sign=%s&signMethod=%s",
128 product_key, device_name, random, sign, MBTK_MQTT_HTTP_SIGN_METHOD_HMACSHA256);
129
130 memset(mbtk_mqtt_http_url, 0, sizeof(mbtk_mqtt_http_url));
131 sprintf(mbtk_mqtt_http_url, "https://%s:443/auth/register/device", http_host[host]);
132
133 memset(mbtk_mqtt_http_content, 0, sizeof(mbtk_mqtt_http_content));
134 memcpy(mbtk_mqtt_http_content, content, strlen(content));
135
136 printf("mbtk_mqtt_http_url:%s\n", mbtk_mqtt_http_url);
137 printf("mbtk_mqtt_http_content:%s\n", mbtk_mqtt_http_content);
138
139 return 0;
140}
141
142void mbtk_mqt_http_device_secret_response(void *data,int data_len)
143{
144 if(data != NULL && data_len != 0)
145 {
146 char* ptr_start = NULL;
147 char* ptr_end = NULL;
148 char fac[64] = {'\0'};
149 ptr_start = strstr(data, "deviceSecret");
150 if(ptr_start != NULL)
151 {
152 ptr_end = strstr(ptr_start, "\",");
153 if(ptr_end != NULL)
154 {
155 strncpy(Device_Secret, ptr_start + 15, ptr_end - ptr_start - 15);
156 printf("device_secret:%s\n", Device_Secret);
157 }
158 printf("ptr_start:%s,\n ptr_end:%s\n", ptr_start, ptr_end);
159 }
160
161 }
162
163 return;
164}
165
166
167static void mbtk_mqtt_http_data_cb_func(
168 int session_id, mbtk_http_data_type_enum type,
169 void *data,int data_len)
170{
171 if(type == MBTK_HTTP_DATA_HEADER) {
172 printf("Header(%d):%s\n",data_len,(char*)data);
173 mbtk_mqt_http_device_secret_response((char*)data, data_len);
174 } else if(type == MBTK_HTTP_DATA_CONTENT) {
175 printf("Data(%d):%s\n",data_len,(char*)data);
176 mbtk_mqt_http_device_secret_response((char*)data, data_len);
177 } else {
178 printf(">>>>>Complete<<<<<\n");
179 }
180}
181
182int mbtk_aliyun_mqtt_one_type_one_secret_regint_get_info(mbtk_mqtt_device_session_t *mbtk_mqtt_device_t)
183{
184 printf("mbtk_mqtt_http_request 1111111");
185 char product_key[255] = {0};
186 char product_secret[255] = {0};
187 char device_name[255] = {0};
188
189 int http_handle = mbtk_http_handle_get(TRUE, mbtk_mqtt_http_data_cb_func);
190 if(http_handle < 0)
191 {
192 printf("mbtk_http_handle_get() fail.");
193 return -1;
194 }
195
196 int http_session = mbtk_http_session_create(http_handle,HTTP_OPTION_POST,HTTP_VERSION_1_1);
197 if(http_handle < 0)
198 {
199 printf("mbtk_http_session_create() fail.");
200 return -1;
201 }
202
203// mbtk_http_session_option_reset(http_handle, http_session , HTTP_OPTION_HEAD_ONLY);
204// mbtk_http_session_option_reset(http_handle, http_session , HTTP_OPTION_POST);
205
206 memcpy(product_key, mbtk_mqtt_device_t->product_key, strlen(mbtk_mqtt_device_t->product_key));
207 memcpy(product_secret, mbtk_mqtt_device_t->product_secret, strlen(mbtk_mqtt_device_t->product_secret));
208 memcpy(device_name, mbtk_mqtt_device_t->device_name, strlen(mbtk_mqtt_device_t->device_name));
209
210 mbtk_imqtt_send_post_request(product_key, product_secret,device_name, 0);
211
212 if(mbtk_http_session_url_set(http_handle, http_session, mbtk_mqtt_http_url)) {
213 printf("mbtk_http_session_url_set() fail.\n");
214 return -1;
215 }
216
217 const mbtk_http_session_t* session = mbtk_http_session_get(http_handle, http_session);
218 printf("HTTP:%d,%s,%d,%s\n",session->option,session->host,session->port,session->uri);
219
220 char *header = "Accept: text/xml,text/javascript,text/html,application/json\r\n" \
221 "Content-Type: application/x-www-form-urlencoded\r\n";
222
223
224 mbtk_http_session_head_add(http_handle, http_session, "Accept", "text/xml,text/javascript,text/html,application/json");
225
226 mbtk_http_session_head_add(http_handle, http_session, "Content-Type", "application/x-www-form-urlencoded");
227
228 mbtk_http_session_content_set(http_handle, http_session, mbtk_mqtt_http_content, strlen(mbtk_mqtt_http_content) );
229
230// mbtk_http_session_option_reset(http_handle, http_session , HTTP_OPTION_HEAD_ONLY);
231// mbtk_http_session_option_reset(http_handle, http_session , HTTP_OPTION_POST);
232
233 memset(Device_Secret, 0, sizeof(Device_Secret));
234 if(mbtk_http_session_start(http_handle, http_session)) {
235 printf("mbtk_http_session_start() fail.\n");
236 return -1;
237 }
238
239 if(mbtk_http_handle_free(http_handle))
240 {
241 printf("mbtk_http_handle_free() fail.");
242 return -1;
243 }
244
245 if(strlen(Device_Secret ) > 0)
246 {
247 printf("\nstrlen(Device_Secret :%d\n )", strlen(Device_Secret ));
248 memcpy(mbtk_mqtt_device_t->device_secret,Device_Secret, strlen(Device_Secret) );
249 }else{
250 printf("get Device_Secret fail\n");
251 return -1;
252 }
253
254 printf("MBTK_HTTP exit.");
255 return 0;
256
257}
258
259
260void mbtk_aliyun_mqtt_get_connect_para(char *password,char *cliendid,char *username, mbtk_mqtt_device_session_t *mbtk_mqtt_device_t)
261{
262 /*********password.mqttClientId.user_name *************/
263 char content[MBTK_IMQTT_PASSWORD_LEN] = {0};
264
265 printf("mbtk_mqtt_device_t->device_name: %s\n", mbtk_mqtt_device_t->device_name);
266 printf("mbtk_mqtt_device_t->product_key: %s\n", mbtk_mqtt_device_t->product_key);
267 printf("mbtk_mqtt_device_t->product_secret: %s\n", mbtk_mqtt_device_t->product_secret);
268
269 sprintf(content,"deviceName%sproductKey%srandom123",mbtk_mqtt_device_t->device_name,mbtk_mqtt_device_t->product_key);
270 core_hmac_sha1((const char*)content,strlen((const char*)content),(char *)password,mbtk_mqtt_device_t->product_secret,strlen(mbtk_mqtt_device_t->product_secret));
271
272
273// snprintf((char *)cliendid,MBTK_IMQTT_CLIENT_ID_LEN,"%s|securemode=-2,authType=regnwl,random=123,signmethod=hmacsha1,instanceId=%s|",mbtk_mqtt_device_t->device_name,"iot-06z00ag5qidat54");
274
275 snprintf((char *)cliendid,MBTK_IMQTT_CLIENT_ID_LEN,"%s|securemode=-2,authType=regnwl,random=123,signmethod=hmacsha1|",mbtk_mqtt_device_t->device_name);
276 snprintf((char *)username,MBTK_IMQTT_USER_NAME_LEN,"%s&%s",mbtk_mqtt_device_t->device_name,mbtk_mqtt_device_t->product_key);
277}
278
279
280void mbtk_imqtt_auth_hostname(char *dest, char *product_key, int host)
281{
282// char *host = "&product_key.iot-as-mqtt.cn-shanghai.aliyuncs.com";
283 int8_t public_instance = 1;
284
285 snprintf(dest, 100, "%s.%s", product_key, mqtt_url[host]);
286}
287
288
289void mbtk_imqtt_auth_clientid(char *dest,char *product_key, char *device_name)
290{
291// char *clientid = "gyj01ZAd7HF.MATT1|securemode=3,signmethod=hmacsha256,timestamp=1647830635443|";
292
293 sprintf(dest, "%s.%s|securemode=3,signmethod=hmacsha256,timestamp=%s|", product_key,device_name,CORE_AUTH_TIMESTAMP);
294}
295
296void mbtk_imqtt_auth_clientid_yixinyimi_unregin(char *dest,char *client_id)
297{
298// char *clientid = "client_id|securemode=-2,authType=connwl|";
299
300 sprintf(dest, "%s|securemode=-2,authType=connwl|", client_id);
301
302}
303
304
305void mbtk_aliyun_imqtt_auth_clientid(char *dest, char *product_key, char *device_name)
306{
307
308 sprintf(dest, "%s.%s|timestamp=%s,_ss=1,_v=%s,securemode=2,signmethod=hmacsha256,ext=3|", product_key,device_name,CORE_AUTH_TIMESTAMP,CORE_AUTH_SDK_VERSION);
309
310 //instanceId 实例id
311// sprintf(dest, "%s.%s|timestamp=%s,_ss=1,_v=%s,securemode=2,signmethod=hmacsha256,ext=3,instanceId=%s|", product_key,device_name,CORE_AUTH_TIMESTAMP,CORE_AUTH_SDK_VERSION,"iot-06z00ag5qidat54");
312}
313
314
315void mbtk_imqtt_auth_username(char *dest,char *device_name, char *product_key)
316{
317 //data.username.cstring="$deviceName&$productKey";
318
319 snprintf(dest,128, "%s&%s", device_name, product_key );
320}
321
322void mbtk_imqtt_auth_password(char *dest, char *device_name, char *product_key, char *device_secret)
323{
324// > data.password.cstring=hmacsha1($deviceSecret,$content);
325// $content为productKey,deviceName,timestamp,clientId按照手母顺序排序,然后将参数值依次拼接例如
326
327
328 char content[300] = {0};
329 uint8_t source_temp[65]={0};
330 sprintf(content,"clientId%s.%sdeviceName%sproductKey%stimestamp%s",product_key, device_name, device_name, product_key,CORE_AUTH_TIMESTAMP);
b.liubcf86c92024-08-19 19:48:28 +0800331
liubin281ac462023-07-19 14:22:54 +0800332// sprintf(content,"clientId%sdeviceName%sproductKey%stimestamp%s",client_id, device_name, product_key,CORE_AUTH_TIMESTAMP);
333 core_hmac_sha256((uint8_t *)content, strlen((const char *)content), (uint8_t *)device_secret, strlen(device_secret),
334 source_temp);
b.liubcf86c92024-08-19 19:48:28 +0800335
liubin281ac462023-07-19 14:22:54 +0800336 core_hex2str(source_temp, 32, dest,0);
337}
338
b.liubcf86c92024-08-19 19:48:28 +0800339void iot_aliyun_mqtt_init(Cloud_MQTT_t *piot_mqtt,int host,int port ,char *device_name,char *product_key,
340 char * DeviceSecret,int keepAliveInterval,int version,char *sub_topic,char *pub_topic,pMessageArrived_Fun mqtt_data_rx_cb)
liubin281ac462023-07-19 14:22:54 +0800341{
342 mbtk_imqtt_auth_hostname(piot_mqtt->mqtt_host,product_key,host);
343 mbtk_imqtt_auth_clientid(piot_mqtt->mqtt_client_id,product_key,device_name);
344 mbtk_imqtt_auth_username(piot_mqtt->mqtt_user,device_name,product_key);
345 mbtk_imqtt_auth_password(piot_mqtt->mqtt_pass,device_name,product_key,DeviceSecret);
346 piot_mqtt->mqtt_port = port;
347 piot_mqtt->keepAliveInterval = keepAliveInterval;
348 // piot_mqtt->mqtt_version = version;
349 piot_mqtt->mqtt_version = 3;
350
351 printf("mqtt_host is %s\nmqtt_port is %d\nmqtt_client_id is %s\nmqtt_user is %s\nmqtt_pass is %s\n",piot_mqtt->mqtt_host,piot_mqtt->mqtt_port,piot_mqtt->mqtt_client_id,piot_mqtt->mqtt_user,piot_mqtt->mqtt_pass);
352 memset(piot_mqtt->sub_topic, '\0', MQTT_TOPIC_SIZE);
353 memset(piot_mqtt->pub_topic, '\0', MQTT_TOPIC_SIZE);
b.liubcf86c92024-08-19 19:48:28 +0800354
liubin281ac462023-07-19 14:22:54 +0800355 sprintf(piot_mqtt->sub_topic, "%s", sub_topic); //将初始化好的订阅主题填到数组中
356 printf("subscribe:%s\n", piot_mqtt->sub_topic);
357
358 sprintf(piot_mqtt->pub_topic, "%s", pub_topic); //将初始化好的发布主题填到数组中
359 printf("pub:%s\n", piot_mqtt->pub_topic);
b.liubcf86c92024-08-19 19:48:28 +0800360
liubin281ac462023-07-19 14:22:54 +0800361 piot_mqtt->DataArrived_Cb = mqtt_data_rx_cb; //设置接收到数据回调函数
362 printf("iot_mqtt_init end\n");
363}
364
b.liubcf86c92024-08-19 19:48:28 +0800365void mbtk_aliyun_mqtt_one_type_one_secret_unregin_set_info_init(Cloud_MQTT_t *piot_mqtt,mbtk_mqtt_device_session_t *device,int keepAliveInterval,int version,pMessageArrived_Fun mqtt_data_rx_cb)
liubin281ac462023-07-19 14:22:54 +0800366{
367
368 mbtk_imqtt_auth_hostname(piot_mqtt->mqtt_host,device->product_key,device->host);
369 mbtk_aliyun_mqtt_get_connect_para(piot_mqtt->mqtt_pass, piot_mqtt->mqtt_client_id, piot_mqtt->mqtt_user, device);
370
371 piot_mqtt->mqtt_port = device->port;
372 piot_mqtt->keepAliveInterval = keepAliveInterval;
373 piot_mqtt->mqtt_version = version;
374
375 printf("mqtt_host is %s\nmqtt_port is %d\nmqtt_client_id is %s\nmqtt_user is %s\nmqtt_pass is %s\n",piot_mqtt->mqtt_host,piot_mqtt->mqtt_port,piot_mqtt->mqtt_client_id,piot_mqtt->mqtt_user,piot_mqtt->mqtt_pass);
376 printf("piot_mqtt->mqtt_version:%d\n", piot_mqtt->mqtt_version);
377 memset(piot_mqtt->sub_topic, '\0', MQTT_TOPIC_SIZE);
378 memset(piot_mqtt->pub_topic, '\0', MQTT_TOPIC_SIZE);
b.liubcf86c92024-08-19 19:48:28 +0800379
liubin281ac462023-07-19 14:22:54 +0800380 piot_mqtt->DataArrived_Cb = mqtt_data_rx_cb; //设置接收到数据回调函数
381 printf("iot_mqtt_init end\n");
382}
383
384int mbtk_aliyun_mqtt_one_type_one_secret_unregin_get_regin_info(char *clientId, char *deviceToken)
385{
386 int ret = 0;
387 if(strlen(regnwl_info.deviceToken) > 5)
388 {
389 memcpy(clientId, regnwl_info.clientId, strlen(regnwl_info.clientId));
390 memcpy(deviceToken, regnwl_info.deviceToken , strlen(regnwl_info.deviceToken));
391 }else{
392 ret= -1;
393 }
394
395 printf("regnwl_info.clientId:%s\n", regnwl_info.clientId);
396 printf("regn->deviceToken:%s\n", regnwl_info.deviceToken);
397 return ret;
398}
399
400
401void iot_aliyun_mqtt_one_type_one_secret_unregin_connect_init(Cloud_MQTT_t *piot_mqtt,mbtk_mqtt_device_session_t *device, char *clientId, char *deviceToken,
b.liubcf86c92024-08-19 19:48:28 +0800402 int keepAliveInterval,int version,char *sub_topic,char *pub_topic,pMessageArrived_Fun mqtt_data_rx_cb)
liubin281ac462023-07-19 14:22:54 +0800403{
404 mbtk_imqtt_auth_hostname(piot_mqtt->mqtt_host,device->product_key,device->host);
405 mbtk_imqtt_auth_clientid_yixinyimi_unregin(piot_mqtt->mqtt_client_id, clientId);
406 mbtk_imqtt_auth_username(piot_mqtt->mqtt_user,device->device_name,device->product_key);
407 sprintf(piot_mqtt->mqtt_pass, "%s", deviceToken);
408 piot_mqtt->mqtt_port = device->port;
409 piot_mqtt->keepAliveInterval = keepAliveInterval;
410 // piot_mqtt->mqtt_version = version;
411 piot_mqtt->mqtt_version = 3;
412
413 printf("mqtt_host is %s\nmqtt_port is %d\nmqtt_client_id is %s\nmqtt_user is %s\nmqtt_pass is %s\n",piot_mqtt->mqtt_host,piot_mqtt->mqtt_port,piot_mqtt->mqtt_client_id,piot_mqtt->mqtt_user,piot_mqtt->mqtt_pass);
414 memset(piot_mqtt->sub_topic, '\0', MQTT_TOPIC_SIZE);
415 memset(piot_mqtt->pub_topic, '\0', MQTT_TOPIC_SIZE);
b.liubcf86c92024-08-19 19:48:28 +0800416
liubin281ac462023-07-19 14:22:54 +0800417 sprintf(piot_mqtt->sub_topic, "%s", sub_topic); //将初始化好的订阅主题填到数组中
418 printf("subscribe:%s\n", piot_mqtt->sub_topic);
419
420 sprintf(piot_mqtt->pub_topic, "%s", pub_topic); //将初始化好的发布主题填到数组中
421 printf("pub:%s\n", piot_mqtt->pub_topic);
b.liubcf86c92024-08-19 19:48:28 +0800422
liubin281ac462023-07-19 14:22:54 +0800423 piot_mqtt->DataArrived_Cb = mqtt_data_rx_cb; //设置接收到数据回调函数
424 printf("iot_mqtt_init end\n");
425}
426
427
b.liubcf86c92024-08-19 19:48:28 +0800428void mbtk_aliyun_mqtt_one_type_one_secret_regint_connect_init(Cloud_MQTT_t *piot_mqtt,int host,int port ,char *device_name,char *product_key,
429 char * DeviceSecret,int keepAliveInterval,int version,char *sub_topic,char *pub_topic,pMessageArrived_Fun mqtt_data_rx_cb)
liubin281ac462023-07-19 14:22:54 +0800430{
431 mbtk_imqtt_auth_hostname(piot_mqtt->mqtt_host,product_key,host);
432 mbtk_aliyun_imqtt_auth_clientid(piot_mqtt->mqtt_client_id,product_key,device_name);
433 mbtk_imqtt_auth_username(piot_mqtt->mqtt_user,device_name,product_key);
434 mbtk_imqtt_auth_password(piot_mqtt->mqtt_pass,device_name,product_key,DeviceSecret);
435 piot_mqtt->mqtt_port = port;
436 piot_mqtt->keepAliveInterval = keepAliveInterval;
437 // piot_mqtt->mqtt_version = version;
438 piot_mqtt->mqtt_version = 3;
439
440 printf("mqtt_host is %s\nmqtt_port is %d\nmqtt_client_id is %s\nmqtt_user is %s\nmqtt_pass is %s\n",piot_mqtt->mqtt_host,piot_mqtt->mqtt_port,piot_mqtt->mqtt_client_id,piot_mqtt->mqtt_user,piot_mqtt->mqtt_pass);
441 memset(piot_mqtt->sub_topic, '\0', MQTT_TOPIC_SIZE);
442 memset(piot_mqtt->pub_topic, '\0', MQTT_TOPIC_SIZE);
b.liubcf86c92024-08-19 19:48:28 +0800443
liubin281ac462023-07-19 14:22:54 +0800444 sprintf(piot_mqtt->sub_topic, "%s", sub_topic); //将初始化好的订阅主题填到数组中
445 printf("subscribe:%s\n", piot_mqtt->sub_topic);
446
447 sprintf(piot_mqtt->pub_topic, "%s", pub_topic); //将初始化好的发布主题填到数组中
448 printf("pub:%s\n", piot_mqtt->pub_topic);
b.liubcf86c92024-08-19 19:48:28 +0800449
liubin281ac462023-07-19 14:22:54 +0800450 piot_mqtt->DataArrived_Cb = mqtt_data_rx_cb; //设置接收到数据回调函数
451 printf("iot_mqtt_init end\n");
452}
453
454
b.liubcf86c92024-08-19 19:48:28 +0800455void iot_mqtt_init(Cloud_MQTT_t *piot_mqtt,char *host,int port ,char *clientid,char *user,char *pass,int keepAliveInterval,int version,char *sub_topic,char *pub_topic,pMessageArrived_Fun mqtt_data_rx_cb)
liubin281ac462023-07-19 14:22:54 +0800456{
457
458 memcpy(piot_mqtt->mqtt_host,host,strlen(host));
459 memcpy(piot_mqtt->mqtt_client_id,clientid,strlen(clientid));
460 memcpy(piot_mqtt->mqtt_user,user,strlen(user));
461 memcpy(piot_mqtt->mqtt_pass,pass,strlen(pass));
462 piot_mqtt->mqtt_port = port;
463 piot_mqtt->keepAliveInterval = keepAliveInterval;
464 piot_mqtt->mqtt_version = version;
465
b.liubcf86c92024-08-19 19:48:28 +0800466
liubin281ac462023-07-19 14:22:54 +0800467 printf("mqtt_host is %s\nmqtt_port is %d\nmqtt_client_id is %s\nmqtt_user is %s\nmqtt_pass is %s\n",piot_mqtt->mqtt_host,piot_mqtt->mqtt_port,piot_mqtt->mqtt_client_id,piot_mqtt->mqtt_user,piot_mqtt->mqtt_pass);
468 memset(piot_mqtt->sub_topic, '\0', MQTT_TOPIC_SIZE);
469 memset(piot_mqtt->pub_topic, '\0', MQTT_TOPIC_SIZE);
b.liubcf86c92024-08-19 19:48:28 +0800470
liubin281ac462023-07-19 14:22:54 +0800471 sprintf(piot_mqtt->sub_topic, "%s", sub_topic); //将初始化好的订阅主题填到数组中
472 printf("subscribe:%s\n", piot_mqtt->sub_topic);
473
474 sprintf(piot_mqtt->pub_topic, "%s", pub_topic); //将初始化好的发布主题填到数组中
475 printf("pub:%s\n", piot_mqtt->pub_topic);
b.liubcf86c92024-08-19 19:48:28 +0800476
liubin281ac462023-07-19 14:22:54 +0800477 piot_mqtt->DataArrived_Cb = mqtt_data_rx_cb; //设置接收到数据回调函数
478 printf("iot_mqtt_init end\n");
479}
480/*
481void MQTTMessageArrived_Cb(MessageData* md)
482{
b.liubcf86c92024-08-19 19:48:28 +0800483 MQTTMessage *message = md->message;
liubin281ac462023-07-19 14:22:54 +0800484
485 Cloud_MQTT_t *piot_mqtt = get_mqtt_t();
486
487 if (NULL != piot_mqtt->DataArrived_Cb) {
488 piot_mqtt->DataArrived_Cb((void *)message->payload, message->payloadlen);//异步消息体
489 }
490}
491*/
492
493int mbtk_aliyun_mqtt_one_type_one_secret_unregin_device_connect(Cloud_MQTT_t *piot_mqtt)
494{
495 int rc = 0, ret = 0;
496 Network* network = (Network*)malloc(sizeof(Network));
497 memset(network ,0x0, sizeof(Network));
498 NewNetwork(network);
499 piot_mqtt->network = network;
500
501 printf("topic = %s\n", piot_mqtt->sub_topic);
502// set_mqtt_t(piot_mqtt);
503 piot_mqtt->network->is_support_ssl = 1;
504 printf("23131mqtt_host is %s\nmqtt_port is %d\nmqtt_client_id is %s\nmqtt_user is %s\nmqtt_pass is %s\n",piot_mqtt->mqtt_host,piot_mqtt->mqtt_port,piot_mqtt->mqtt_client_id,piot_mqtt->mqtt_user,piot_mqtt->mqtt_pass);
505 rc = ConnectNetwork(piot_mqtt->network, piot_mqtt->mqtt_host, piot_mqtt->mqtt_port,piot_mqtt->network->is_support_ssl,piot_mqtt->network->ingnore_cert);
506 if (rc != 0) {
507 ret = -101;
508 goto __END;
509 }
510 MQTTClient(&piot_mqtt->Client, piot_mqtt->network, 1000*30, piot_mqtt->mqtt_buffer, MQTT_BUF_SIZE, piot_mqtt->mqtt_read_buffer, MQTT_BUF_SIZE);
511 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
512
513 data.MQTTVersion = piot_mqtt->mqtt_version;
514 data.clientID.cstring = piot_mqtt->mqtt_client_id;
515 data.username.cstring = piot_mqtt->mqtt_user;
516 data.password.cstring = piot_mqtt->mqtt_pass;
517 data.keepAliveInterval = piot_mqtt->keepAliveInterval;
518 data.willFlag = 0;
519 data.will.qos = 0;
520 data.will.retained = 0;
521 data.will.topicName.cstring = NULL;
522 data.will.message.cstring = NULL;
523
524 data.cleansession = 1;
525 rc = MQTTConnect(&piot_mqtt->Client, &data);
526 if (rc) {
527 printf("mqtt connect broker fail \n");
528 printf("rc = %d\n", rc);
529 ret = -102;
530 goto __END;
531 }
532__END:
533 return ret;
534}
535
536int mqtt_device_connect(Cloud_MQTT_t *piot_mqtt)
537{
538 int rc = 0, ret = 0;
539 Network* network = (Network*)malloc(sizeof(Network));
540 memset(network ,0x0, sizeof(Network));
541 NewNetwork(network);
542 piot_mqtt->network = network;
543
544 printf("topic = %s\n", piot_mqtt->sub_topic);
545// set_mqtt_t(piot_mqtt);
546 printf("23131mqtt_host is %s\nmqtt_port is %d\nmqtt_client_id is %s\nmqtt_user is %s\nmqtt_pass is %s\n",piot_mqtt->mqtt_host,piot_mqtt->mqtt_port,piot_mqtt->mqtt_client_id,piot_mqtt->mqtt_user,piot_mqtt->mqtt_pass);
547 rc = ConnectNetwork(piot_mqtt->network, piot_mqtt->mqtt_host, piot_mqtt->mqtt_port,piot_mqtt->network->is_support_ssl,piot_mqtt->network->ingnore_cert);
548 if (rc != 0) {
549 ret = -101;
550 goto __END;
551 }
552 MQTTClient(&piot_mqtt->Client, piot_mqtt->network, 1000, piot_mqtt->mqtt_buffer, MQTT_BUF_SIZE, piot_mqtt->mqtt_read_buffer, MQTT_BUF_SIZE);
553 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
554
555 if (piot_mqtt->willFlag) {
556 data.willFlag = 1;
557 memcpy(&data.will, &piot_mqtt->will, sizeof(MQTTPacket_willOptions));
558 } else {
559 data.willFlag = 0;
560 }
561 data.MQTTVersion = piot_mqtt->mqtt_version;
562 data.clientID.cstring = piot_mqtt->mqtt_client_id;
563 data.username.cstring = piot_mqtt->mqtt_user;
564 data.password.cstring = piot_mqtt->mqtt_pass;
565 data.keepAliveInterval = piot_mqtt->keepAliveInterval;
566 data.cleansession = 1;
567 rc = MQTTConnect(&piot_mqtt->Client, &data);
568 if (rc) {
569 printf("mqtt connect broker fail \n");
570 printf("rc = %d\n", rc);
571 ret = -102;
572 goto __END;
573 }
574__END:
575 return ret;
576}
577
578int mqtt_device_disconnect(Cloud_MQTT_t *piot_mqtt)//断开mqtt连接
579{
580 int ret = 0;
581
582 ret = MQTTDisconnect(&piot_mqtt->Client,piot_mqtt->network);
583 printf("disconnectNetwork ret = %d\n", ret);
584
585 return ret;
586}
587
588void iot_yield(Cloud_MQTT_t *piot_mqtt,iot_device_info_t *gateway)
589{
590 int ret;
591 switch (gateway->iotstatus) {
592 case IOT_STATUS_LOGIN:
593 ret = mqtt_device_connect(piot_mqtt);
594 if (ret < 0) {
595 printf("iot connect error code %d\n", ret);
596 sleep(1);
597 }
598 break;
599 case IOT_STATUS_CONNECT:
600 ret = MQTTYield(&piot_mqtt->Client, 200);
601 //printf("iot_yield ret %d\n",ret);
602 if (ret != SUCCESS) {
603 printf("why???\n");
604 gateway->iotstatus = IOT_STATUS_DROP;
605 }
606 break;
607 case IOT_STATUS_DROP:
608 mqtt_device_disconnect(piot_mqtt);
609 //gateway.iotstatus = IOT_STATUS_LOGIN;
610 usleep(1000);
611 break;
612 default:
613 break;
614 }
615}
616
617int mqtt_will_msg_set(Cloud_MQTT_t *piot_mqtt, char *pbuf, int len)//设置遗嘱函数
618{
619 memset(piot_mqtt->will_topic, '\0', MQTT_TOPIC_SIZE);
620 MQTTPacket_willOptions mqtt_will = MQTTPacket_willOptions_initializer;
621
622 strcpy(piot_mqtt->will_topic, piot_mqtt->pub_topic);
623 memcpy(&piot_mqtt->will, &mqtt_will, sizeof(MQTTPacket_willOptions));
624
625 piot_mqtt->willFlag = 1;
626 piot_mqtt->will.retained = 0;
627 piot_mqtt->will.topicName.cstring = (char *)piot_mqtt->will_topic;
628 piot_mqtt->will.message.cstring = (char *)pbuf;
629 piot_mqtt->will.qos = QOS2;
630 return 0;
631
632}
633
634int mbtk_MQTTSubscribe(Client* c, const char* topicFilter, enum QoS qos, messageHandler messageHandler)
635{
636 return MQTTSubscribe(c, topicFilter, qos, messageHandler);
637}
638int mbtk_MQTTUnsubscribe(Client* c, const char* topicFilter)
639{
640 return MQTTUnsubscribe(c,topicFilter);
641}
642/*
643int mbtk_MQTTPublish(Client* c, const char* topicName, MQTTMessage* message)
644{
645 return MQTTPublish(c,topicName,message);
646}
647*/
648int mbtk_MQTTPublish(char *pbuf, int len, char retain,Client* c,const char* pub_topic,enum QoS qos,char dup)
649{
650 int ret = 0;
651 MQTTMessage message;
652 char my_topic[128] = {0};
653
654 strcpy(my_topic, pub_topic);
655
656 message.payload = (void *)pbuf;
657 message.payloadlen = len;
658 message.dup = dup;
659 message.qos = qos;
660 if (retain) {
661 message.retained = 1;
662 } else {
663 message.retained = 0;
664 }
665
666 ret = MQTTPublish(c, my_topic, &message); //发布一个主题
667
668 return ret;
669}
670
b.liubcf86c92024-08-19 19:48:28 +0800671void mbtk_mqtt_lib_info_print()
672{
673 MBTK_SOURCE_INFO_PRINT("mbtk_mqtt_lib");
674}
675
676