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