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