mqtt_demo.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /*================================================================
  2. Copyright (c) 2020 Quectel Wireless Solution, Co., Ltd. All Rights Reserved.
  3. Quectel Wireless Solution Proprietary and Confidential.
  4. =================================================================*/
  5. /*=================================================================
  6. EDIT HISTORY FOR MODULE
  7. This section contains comments describing changes made to the module.
  8. Notice that changes are listed in reverse chronological order.
  9. WHEN WHO WHAT, WHERE, WHY
  10. ------------ ------- -------------------------------------------------------------------------------
  11. =================================================================*/
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include "ql_api_osi.h"
  16. #include "ql_api_nw.h"
  17. #include "ql_log.h"
  18. #include "ql_api_datacall.h"
  19. #include "ql_mqttclient.h"
  20. #include "ql_ssl.h"
  21. #define QL_MQTT_LOG_LEVEL QL_LOG_LEVEL_INFO
  22. #define QL_MQTT_LOG(msg, ...) QL_LOG(QL_MQTT_LOG_LEVEL, "ql_MQTT", msg, ##__VA_ARGS__)
  23. #define QL_MQTT_LOG_PUSH(msg, ...) QL_LOG_PUSH("ql_MQTT", msg, ##__VA_ARGS__)
  24. static ql_task_t mqtt_task = NULL;
  25. #define MQTT_CLIENT_IDENTITY "quectel_01"
  26. #define MQTT_CLIENT_USER ""
  27. #define MQTT_CLIENT_PASS ""
  28. #define MQTT_CLIENT_ONENET_PRODUCTID "417661"
  29. #define MQTT_CLIENT_ONENET_ACCESSKEY "aggblpDBWoKg2CGdbxp/CVKt9xXWcV162hJ1+Bhzvw4="
  30. #define MQTT_CLIENT_ONENET_DEVICENAME "test_led"
  31. #define MQTT_CLIENT_ONENET_VERSION "2018-10-31"
  32. #define USE_CRT_BUFFER 0
  33. #define MQTT_CLIENT_QUECTEL_URL "mqtt://220.180.239.212:8306"
  34. #define MQTT_CLIENT_ONENET_URL "mqtt://mqtts.heclouds.com:1883" //onenet 的ip地址
  35. #if USE_CRT_BUFFER
  36. #define MQTT_CLIENT_QUECTEL_SSL_URL "mqtts://112.31.84.164:8308"
  37. #else
  38. #define MQTT_CLIENT_QUECTEL_SSL_URL "mqtts://220.180.239.212:8307"
  39. #endif
  40. #define MQTT_CLIENT_ONENET_SSL_URL "mqtts://mqttstls.heclouds.com:8883"//onenet 的ip地址
  41. // publist 的内容
  42. #define MQTT_PUB_MSG0 "{\"id\": 000000,\"dp\": {\"temperatrue\": [{\"v\": 0.001,}],\"power\": [{\"v\": 0.001,}]}}"
  43. #define MQTT_PUB_MSG1 "{\"id\": 111111,\"dp\": {\"temperatrue\": [{\"v\": 1.000,}],\"power\": [{\"v\": 1.001,}]}}"
  44. #define MQTT_PUB_MSG2 "{\"id\": 222222,\"dp\": {\"temperatrue\": [{\"v\": 2.000,}],\"power\": [{\"v\": 2.002,}]}}"
  45. static ql_sem_t mqtt_semp;
  46. static int mqtt_connected = 0;
  47. #if USE_CRT_BUFFER
  48. char *root_ca_crt_buffer= "-----BEGIN CERTIFICATE-----\r\n\
  49. MIIEhDCCAuwCCQDuE1BpeAeMwzANBgkqhkiG9w0BAQsFADCBgjELMAkGA1UEBhMC\r\n\
  50. Q04xCzAJBgNVBAgMA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VD6wIDAQABMA0GCSqGSIb3DQEB\r\n\
  65. CwUAA4IBgQAuNVwkBhd5nyWMmV/ESNxy59Sz+5FcesGclKjs4YocgcKbLD2bS+LN\r\n\
  66. lKk6zenES7Cq6+l3NMAxxh/QhgHUCThAfREzfPXbmiicrUfaudN4YFivpoFwKIAs\r\n\
  67. NczsL9S3FPbzAB4nLDATacc2BK0//aKMOU2t3KLNNomKbzlR+EW3wd0F1GoZ9SY6\r\n\
  68. sCQeLa8Wp1KarOmbvgoFL/DAiTSqjjsU/Lq24dOCCctmG+qXRZxQa4npHD4xJwQJ\r\n\
  69. qzA0JLu4n+DgoJftm1KpvB0wuzTn6M9+wnk5rv/fGc2t4Zra8B4prEReZZVfy65d\r\n\
  70. cb8pBdb20Yrmznj+6DR50X/o/8Qzoyj9XpxtjwF23ql0XPYCI7kB03Ms9euP0btc\r\n\
  71. HFacHapm0qBKx+vWy0V2Qf482OWSbewqaRbud44sErNoKqpqm02yN8PpsCywpFUj\r\n\
  72. UC5G5DzxzYspMzQv/yidti0scMSKFObseZmNGlRYymCWhXnxmoCFjLpw5RnJSB2+\r\n\
  73. cZ/1KFFHHZI=\r\n\
  74. -----END CERTIFICATE-----";
  75. char *client_crt_buffer= "-----BEGIN CERTIFICATE-----\r\n\
  76. MIIEhDCCAuwCCQDuE1BpeAeMwzANBgkqhkiG9w0BAQsFADCBgjELMAkGA1UEBhMC\r\n\
  77. Q04xCzAJBgNVBAgMAkFIMQswCQYDVQQHDAJIRjEQMA4GA1UECgwHUVVFQ1RFTDEL\r\n\
  78. MAkGA1UECwwCU1QxFjAUBgNVBAMMDTExMi4zMS44NC4xNjQxIjAgBgkqhkiG9w0B\r\n\
  79. CQEWE2VkZGllLnpoYW5nQHF1ZWN0ZWwwIBcNMjIwMTI1MDcyMzI3WhgPMjEyMjAx\r\n\
  80. MDEwNzIzMjdaMIGCMQswCQYDVQQGEwJDTjELMAkGA1UECAwCQUgxCzAJBgNVBAcM\r\n\
  81. AkhGMRAwDg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CCctmG+qXRZxQa4npHD4xJwQJ\r\n\
  96. qzA0JLu4n+DgoJftm1KpvB0wuzTn6M9+wnk5rv/fGc2t4Zra8B4prEReZZVfy65d\r\n\
  97. cb8pBdb20Yrmznj+6DR50X/o/8Qzoyj9XpxtjwF23ql0XPYCI7kB03Ms9euP0btc\r\n\
  98. HFacHapm0qBKx+vWy0V2Qf482OWSbewqaRbud44sErNoKqpqm02yN8PpsCywpFUj\r\n\
  99. UC5G5DzxzYspMzQv/yidti0scMSKFObseZmNGlRYymCWhXnxmoCFjLpw5RnJSB2+\r\n\
  100. cZ/1KFFHHZI=\r\n\
  101. -----END CERTIFICATE-----";
  102. char *client_key_buffer= "-----BEGIN RSA PRIVATE KEY-----\r\n\
  103. MIIG4wIBAAKCAYEAy3dyNjR4sQOq/xjsWiYD7s0bnO0BtHv4/9bGM+Wh2pENnkdA\r\n\
  104. 5GR8CvjiABlxawqijgMdMifCv0kFsGcRMEycvz7XeHU9GnkJ/BcoW+OmlZi8Fl95\r\n\
  105. PCFZBhtIwog2hL53VmXXWOD/5ltJ3sWLRtiryKkdu3fQf40A1Bk/VdB2wJ/n+S+f\r\n\
  106. 69eI/gSAlznolD0voAsgXZDoXcb89Npgi2Djrc2W/9L55GLVUwNKzxBz4NOVXCWB\r\n\
  107. lkl1KULxiOvmlWqjJebwQ0p9JOt2GsuIpJYlPP0lSvTgTtIn4KgECctg08dHbLQI\r\n\
  108. VL/XloJrUlsrbtkn/Ry8G/OlZ2RyCsO0ohzE1gz1AU+l6FvU3imCqLQ0+EKRz8Fq\r\n\
  109. x+ZdRpPAziCGGLi1BGm0ox1M3+R3IL5XlrwfKFs2/Dmp4mUskqmBn1fcdDO4qy0o\r\n\
  110. yTEs24MjMr1O87vMujCv7QF61R2WWr58ONdAzQz70/de/ZWYPAZGSZnQiNikOOb6\r\n\
  111. qoZMVUvPNXASpUPrAgMBAAECggGAG8evPF9lqyWJD1Nj0dsm5k/y2TYy6WWT1bqJ\r\n\
  112. TUSpG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wAGZ48J8imd7X7c9AJuCx3\r\n\
  127. B45t8G/D2tr9MHCrLMV827jCFcFNys6Xeg4PyOECgcEAye8x2ust87+4A2stDz55\r\n\
  128. HOFFc8vUE1d95/vy5jRmO0xOg7DxpCiou4ZI4mvKBkfwuidIYfGSKK4ZKs2660kK\r\n\
  129. ADan05eGAMThahVtsIhRJkDT8mLWCvuktd2Sj8MfqDwLuJuQLWQtdQdD9W1e0jdb\r\n\
  130. ObKSyCwYHSGsUz7QuXYrRpNgAqkCUom9IuHYD4SROd5ZPrZWnSu8VSQi4XeTol3a\r\n\
  131. lCrYfJtZjJE4xacZhXr29nGCMgAFXQAsM/640yxWtfbfAoHAXa8btS6u1nmgrlfc\r\n\
  132. jjQwrGt3dD9QkGL35iuuvzBy0eTmogECSE4VKkFLCCupU3EfZwa1jAkvNsEnxela\r\n\
  133. yJfM224yjW0pK8vkQ/5LXLIW/zCSqQAp2n5TugD3b0YPyIcssKIfGQZBucN8ou3L\r\n\
  134. uPwEjYMG8TQApdRTGpqmXdyrg4oyh/WDV33gzFj6CSNQLZO2hGfM8xq56HbFV0Fm\r\n\
  135. GoVNArEC6vjxrB+SALSFbDGgmBNeqqpFiYd6w2a0Q4toTz9hAoHAG0s/B5pI0kyp\r\n\
  136. voC/OkJrlhX5+WiIJ4jLseh+lqoLNjN4MzlVP6VhAgH5ATQOZiGxWwBmAglqJmiz\r\n\
  137. SpOWv0bG5117wox2I2GeQej9pduwqWCUvkvzXipVfbU75V+AcmpU96a7jKjE0Pw7\r\n\
  138. gQXUcB+TbvfHnjPOVLM0Y6SannlwTIGukOot4vgz2NLOl4PYtHZ9W8hjACS3aJ6O\r\n\
  139. NeSK2tDE/kM2APQa0qJg2yzJydY28f+45vPXScNcmfhlJ8wHd/aV\r\n\
  140. -----END RSA PRIVATE KEY-----";
  141. #endif
  142. static void mqtt_state_exception_cb(mqtt_client_t *client)
  143. {
  144. QL_MQTT_LOG("mqtt session abnormal disconnect");
  145. mqtt_connected = 0;
  146. }
  147. static void mqtt_connect_result_cb(mqtt_client_t *client, void *arg, mqtt_connection_status_e status)
  148. {
  149. QL_MQTT_LOG("status: %d", status);
  150. if(status == 0){
  151. mqtt_connected = 1;
  152. }
  153. ql_rtos_semaphore_release(mqtt_semp);
  154. }
  155. static void mqtt_requst_result_cb(mqtt_client_t *client, void *arg,int err)
  156. {
  157. QL_MQTT_LOG("err: %d", err);
  158. ql_rtos_semaphore_release(mqtt_semp);
  159. }
  160. static void mqtt_inpub_data_cb(mqtt_client_t *client, void *arg, int pkt_id, const char *topic, const unsigned char *payload, unsigned short payload_len)
  161. {
  162. QL_MQTT_LOG("topic: %s", topic);
  163. QL_MQTT_LOG("payload: %s", payload);
  164. }
  165. static void mqtt_disconnect_result_cb(mqtt_client_t *client, void *arg,int err){
  166. QL_MQTT_LOG("err: %d", err);
  167. ql_rtos_semaphore_release(mqtt_semp);
  168. }
  169. static void mqtt_app_thread(void * arg)
  170. {
  171. int ret = 0;
  172. int i = 0, run_num = 1;
  173. int profile_idx = 1;
  174. ql_data_call_info_s info;
  175. char ip4_addr_str[16] = {0};
  176. mqtt_client_t mqtt_cli;
  177. uint8_t nSim = 0;
  178. uint16_t sim_cid;
  179. struct mqtt_connect_client_info_t client_info = {0};
  180. char *token = NULL;
  181. int is_user_onenet = 1;
  182. ql_rtos_semaphore_create(&mqtt_semp, 0);
  183. ql_rtos_task_sleep_s(10);
  184. char* client_id = (char*)malloc(256);
  185. char* client_user = (char*)malloc(256);
  186. char* client_pass = (char*)malloc(256);
  187. QL_MQTT_LOG("========== mqtt demo start ==========");
  188. QL_MQTT_LOG("wait for network register done");
  189. while((ret = ql_network_register_wait(nSim, 120)) != 0 && i < 10){
  190. i++;
  191. ql_rtos_task_sleep_s(1);
  192. }
  193. if(ret == 0){
  194. i = 0;
  195. QL_MQTT_LOG("====network registered!!!!====");
  196. }else{
  197. QL_MQTT_LOG("====network register failure!!!!!====");
  198. goto exit;
  199. }
  200. ql_set_data_call_asyn_mode(nSim, profile_idx, 0);
  201. QL_MQTT_LOG("===start data call====");
  202. ret=ql_start_data_call(nSim, profile_idx, QL_PDP_TYPE_IP, "uninet", NULL, NULL, 0);
  203. QL_MQTT_LOG("===data call result:%d", ret);
  204. if(ret != 0){
  205. QL_MQTT_LOG("====data call failure!!!!=====");
  206. }
  207. memset(&info, 0x00, sizeof(ql_data_call_info_s));
  208. ret = ql_get_data_call_info(nSim, profile_idx, &info);
  209. if(ret != 0){
  210. QL_MQTT_LOG("ql_get_data_call_info ret: %d", ret);
  211. ql_stop_data_call(nSim, profile_idx);
  212. goto exit;
  213. }
  214. QL_MQTT_LOG("info->profile_idx: %d", info.profile_idx);
  215. QL_MQTT_LOG("info->ip_version: %d", info.ip_version);
  216. QL_MQTT_LOG("info->v4.state: %d", info.v4.state);
  217. inet_ntop(AF_INET, &info.v4.addr.ip, ip4_addr_str, sizeof(ip4_addr_str));
  218. QL_MQTT_LOG("info.v4.addr.ip: %s\r\n", ip4_addr_str);
  219. inet_ntop(AF_INET, &info.v4.addr.pri_dns, ip4_addr_str, sizeof(ip4_addr_str));
  220. QL_MQTT_LOG("info.v4.addr.pri_dns: %s\r\n", ip4_addr_str);
  221. inet_ntop(AF_INET, &info.v4.addr.sec_dns, ip4_addr_str, sizeof(ip4_addr_str));
  222. QL_MQTT_LOG("info.v4.addr.sec_dns: %s\r\n", ip4_addr_str);
  223. while(run_num <= 100)
  224. {
  225. int test_num = 0;
  226. int case_id = 0;
  227. if(is_user_onenet != 1)
  228. {
  229. case_id = run_num%2;
  230. }
  231. int ret = MQTTCLIENT_SUCCESS;
  232. QL_MQTT_LOG("==============mqtt_client_test[%d]================\n",run_num);
  233. if(QL_DATACALL_SUCCESS != ql_bind_sim_and_profile(nSim, profile_idx, &sim_cid))
  234. {
  235. QL_MQTT_LOG("nSim or profile_idx is invalid!!!!");
  236. break;
  237. }
  238. if(ql_mqtt_client_init(&mqtt_cli, sim_cid) != MQTTCLIENT_SUCCESS){
  239. QL_MQTT_LOG("mqtt client init failed!!!!");
  240. break;
  241. }
  242. QL_MQTT_LOG("mqtt_cli:%d", mqtt_cli);
  243. if(is_user_onenet == 1)
  244. {
  245. client_info.keep_alive = 60;
  246. client_info.pkt_timeout = 5;
  247. client_info.retry_times = 3;
  248. client_info.clean_session = 1;
  249. client_info.will_qos = 0;
  250. client_info.will_retain = 0;
  251. client_info.will_topic = NULL;
  252. client_info.will_msg = NULL;
  253. client_info.client_id = client_id;
  254. client_info.client_user = client_user;
  255. client_info.client_pass = client_pass;
  256. memset((void*)client_info.client_user,0,256);
  257. memset((void*)client_info.client_id,0,256);
  258. memset((void*)client_info.client_pass,0,256);
  259. /*token过期时间(单位为s),请按照实际具体需求计算token过期时间,本例中为当前时刻的一年后过期*/
  260. signed long long expire_time = 24 * 60 * 60 * 365;
  261. token = ql_mqtt_onenet_generate_auth_token(expire_time,MQTT_CLIENT_ONENET_PRODUCTID,MQTT_CLIENT_ONENET_DEVICENAME,MQTT_CLIENT_ONENET_VERSION,MQTT_CLIENT_ONENET_ACCESSKEY);
  262. if(NULL != token) {
  263. memcpy((void*)client_info.client_pass, token, strlen(token));
  264. memcpy((void*)client_info.client_user,MQTT_CLIENT_ONENET_PRODUCTID,strlen(MQTT_CLIENT_ONENET_PRODUCTID));
  265. memcpy((void*)client_info.client_id,MQTT_CLIENT_ONENET_DEVICENAME,strlen(MQTT_CLIENT_ONENET_DEVICENAME));
  266. free(token);
  267. token = NULL;
  268. }
  269. else
  270. {
  271. break;
  272. }
  273. QL_MQTT_LOG("clientid_str=%s", client_info.client_id);
  274. QL_MQTT_LOG("username_str=%s", client_info.client_user);
  275. QL_MQTT_LOG("password_str=%s", client_info.client_pass);
  276. }
  277. else
  278. {
  279. client_info.keep_alive = 60;
  280. client_info.pkt_timeout = 5;
  281. client_info.retry_times = 3;
  282. client_info.clean_session = 1;
  283. client_info.will_qos = 0;
  284. client_info.will_retain = 0;
  285. client_info.will_topic = NULL;
  286. client_info.will_msg = NULL;
  287. client_info.client_id = MQTT_CLIENT_IDENTITY;
  288. client_info.client_user = MQTT_CLIENT_USER;
  289. client_info.client_pass = MQTT_CLIENT_PASS;
  290. }
  291. QL_MQTT_LOG("connect ssl %d onenet mode %d",case_id,is_user_onenet);
  292. if(case_id == 0){
  293. client_info.ssl_cfg = NULL;
  294. if(is_user_onenet == 1)
  295. {
  296. ret = ql_mqtt_connect(&mqtt_cli, MQTT_CLIENT_ONENET_URL, mqtt_connect_result_cb, NULL, (const struct mqtt_connect_client_info_t *)&client_info, mqtt_state_exception_cb);
  297. }
  298. else
  299. {
  300. ret = ql_mqtt_connect(&mqtt_cli, MQTT_CLIENT_QUECTEL_URL , mqtt_connect_result_cb, NULL, (const struct mqtt_connect_client_info_t *)&client_info, mqtt_state_exception_cb);
  301. }
  302. }else{
  303. struct mqtt_ssl_config_t quectel_ssl_cfg = {
  304. .ssl_ctx_id = 1,
  305. #if USE_CRT_BUFFER
  306. .verify_level = MQTT_SSL_VERIFY_SERVER_CLIENT,
  307. .client_cert_type= QL_SSL_CLIENT_CERT_BUFFER,
  308. .cacert_buffer = root_ca_crt_buffer,
  309. .client_cert_path = client_crt_buffer,
  310. .client_key_path = client_key_buffer,
  311. #else
  312. .verify_level = MQTT_SSL_VERIFY_NONE,
  313. .cacert_path = NULL,
  314. .client_cert_path = NULL,
  315. .client_key_path = NULL,
  316. #endif
  317. .client_key_pwd = NULL,
  318. .ssl_version = QL_SSL_VERSION_ALL,
  319. .sni_enable = 0,
  320. .ssl_negotiate_timeout = QL_SSL_NEGOTIATE_TIME_DEF,
  321. .ignore_invalid_certsign = 0,
  322. .ignore_multi_certchain_verify = 0,
  323. .ignore_certitem = MBEDTLS_X509_BADCERT_NOT_TRUSTED|MBEDTLS_X509_BADCERT_EXPIRED|MBEDTLS_X509_BADCERT_FUTURE,
  324. };
  325. struct mqtt_ssl_config_t ontnet_ssl_cfg = {
  326. .ssl_ctx_id = 1,
  327. .verify_level = MQTT_SSL_VERIFY_NONE,
  328. .cacert_path = "UFS:MQTTS-certificate.pem",
  329. .client_cert_path = NULL,
  330. .client_key_path = NULL,
  331. .client_key_pwd = NULL,
  332. .ssl_version = QL_SSL_VERSION_ALL,
  333. .sni_enable = 0,
  334. .ssl_negotiate_timeout = QL_SSL_NEGOTIATE_TIME_DEF,
  335. .ignore_invalid_certsign = 0,
  336. .ignore_multi_certchain_verify = 0,
  337. .ignore_certitem = MBEDTLS_X509_BADCERT_EXPIRED|MBEDTLS_X509_BADCERT_FUTURE,
  338. };
  339. if(is_user_onenet == 1)
  340. {
  341. client_info.ssl_cfg = &ontnet_ssl_cfg;
  342. ret = ql_mqtt_connect(&mqtt_cli, MQTT_CLIENT_ONENET_SSL_URL, mqtt_connect_result_cb, NULL, (const struct mqtt_connect_client_info_t *)&client_info, mqtt_state_exception_cb);
  343. }
  344. else
  345. {
  346. client_info.ssl_cfg = &quectel_ssl_cfg;
  347. ret = ql_mqtt_connect(&mqtt_cli, MQTT_CLIENT_QUECTEL_SSL_URL, mqtt_connect_result_cb, NULL, (const struct mqtt_connect_client_info_t *)&client_info, mqtt_state_exception_cb);
  348. }
  349. }
  350. if(ret == MQTTCLIENT_WOUNDBLOCK){
  351. QL_MQTT_LOG("====wait connect result");
  352. ql_rtos_semaphore_wait(mqtt_semp, QL_WAIT_FOREVER);
  353. if(mqtt_connected == 0){
  354. ql_mqtt_client_deinit(&mqtt_cli);
  355. break;
  356. }
  357. }else{
  358. QL_MQTT_LOG("===mqtt connect failed ,ret = %d",ret);
  359. break;
  360. }
  361. ql_mqtt_set_inpub_callback(&mqtt_cli, mqtt_inpub_data_cb, NULL);
  362. if(is_user_onenet == 1)
  363. {
  364. if(is_user_onenet == 1)
  365. {
  366. if(ql_mqtt_sub_unsub(&mqtt_cli, "$sys/417661/test_led/dp/post/json/+", 1, mqtt_requst_result_cb,NULL, 1) == MQTTCLIENT_WOUNDBLOCK){
  367. QL_MQTT_LOG("======wait subscrible result");
  368. ql_rtos_semaphore_wait(mqtt_semp, QL_WAIT_FOREVER);
  369. }
  370. if(ql_mqtt_publish(&mqtt_cli, "$sys/417661/test_led/dp/post/json",MQTT_PUB_MSG0, strlen(MQTT_PUB_MSG0), 0, 0, mqtt_requst_result_cb,NULL) == MQTTCLIENT_WOUNDBLOCK){
  371. QL_MQTT_LOG("======wait publish result");
  372. ql_rtos_semaphore_wait(mqtt_semp, QL_WAIT_FOREVER);
  373. }
  374. if(ql_mqtt_publish(&mqtt_cli, "$sys/417661/test_led/dp/post/json", MQTT_PUB_MSG1, strlen(MQTT_PUB_MSG1), 1, 0, mqtt_requst_result_cb,NULL) == MQTTCLIENT_WOUNDBLOCK){
  375. QL_MQTT_LOG("======wait publish result");
  376. ql_rtos_semaphore_wait(mqtt_semp, QL_WAIT_FOREVER);
  377. }
  378. // onenet 平台不支持qos2
  379. if(ql_mqtt_sub_unsub(&mqtt_cli,"$sys/417661/test_led/dp/post/json/+", 1, mqtt_requst_result_cb,NULL, 0) == MQTTCLIENT_WOUNDBLOCK){
  380. QL_MQTT_LOG("=====wait unsubscrible result");
  381. ql_rtos_semaphore_wait(mqtt_semp, QL_WAIT_FOREVER);
  382. }
  383. }
  384. }
  385. else{
  386. while(test_num < 10 && mqtt_connected == 1){
  387. if(ql_mqtt_sub_unsub(&mqtt_cli, "test", 1, mqtt_requst_result_cb,NULL, 1) == MQTTCLIENT_WOUNDBLOCK){
  388. QL_MQTT_LOG("======wait subscrible result");
  389. ql_rtos_semaphore_wait(mqtt_semp, QL_WAIT_FOREVER);
  390. }
  391. if(ql_mqtt_publish(&mqtt_cli, "test", "hi, mqtt qos 0", strlen("hi, mqtt qos 0"), 0, 0, mqtt_requst_result_cb,NULL) == MQTTCLIENT_WOUNDBLOCK){
  392. QL_MQTT_LOG("======wait publish result");
  393. ql_rtos_semaphore_wait(mqtt_semp, QL_WAIT_FOREVER);
  394. }
  395. if(ql_mqtt_publish(&mqtt_cli, "test", "hi, mqtt qos 1", strlen("hi, mqtt qos 1"), 1, 0, mqtt_requst_result_cb,NULL) == MQTTCLIENT_WOUNDBLOCK){
  396. QL_MQTT_LOG("======wait publish result");
  397. ql_rtos_semaphore_wait(mqtt_semp, QL_WAIT_FOREVER);
  398. }
  399. if(ql_mqtt_publish(&mqtt_cli, "test", "hi, mqtt qos 2", strlen("hi, mqtt qos 2"), 2, 0, mqtt_requst_result_cb,NULL) == MQTTCLIENT_WOUNDBLOCK){
  400. QL_MQTT_LOG("======wait publish result");
  401. ql_rtos_semaphore_wait(mqtt_semp, QL_WAIT_FOREVER);
  402. }
  403. if(ql_mqtt_sub_unsub(&mqtt_cli, "test", 1, mqtt_requst_result_cb,NULL, 0) == MQTTCLIENT_WOUNDBLOCK){
  404. QL_MQTT_LOG("=====wait unsubscrible result");
  405. ql_rtos_semaphore_wait(mqtt_semp, QL_WAIT_FOREVER);
  406. }
  407. test_num++;
  408. ql_rtos_task_sleep_ms(500);
  409. }
  410. }
  411. if(mqtt_connected == 1 && ql_mqtt_disconnect(&mqtt_cli, mqtt_disconnect_result_cb, NULL) == MQTTCLIENT_WOUNDBLOCK){
  412. QL_MQTT_LOG("=====wait disconnect result");
  413. ql_rtos_semaphore_wait(mqtt_semp, QL_WAIT_FOREVER);
  414. }
  415. QL_MQTT_LOG("==============mqtt_client_test[%d] end=======%x=========\n",run_num,&mqtt_cli);
  416. ql_mqtt_client_deinit(&mqtt_cli);
  417. mqtt_connected = 0;
  418. run_num++;
  419. ql_rtos_task_sleep_s(1);
  420. if(is_user_onenet == 1)
  421. {
  422. break;
  423. }
  424. }
  425. exit:
  426. if(is_user_onenet == 1)
  427. {
  428. free((void*)client_id);
  429. free((void*)client_user);
  430. free((void*)client_pass);
  431. client_id = NULL;
  432. client_user = NULL;
  433. client_pass = NULL;
  434. }
  435. ql_rtos_semaphore_delete(mqtt_semp);
  436. ql_rtos_task_delete(mqtt_task);
  437. return;
  438. }
  439. int ql_mqtt_app_init(void)
  440. {
  441. QlOSStatus err = QL_OSI_SUCCESS;
  442. err = ql_rtos_task_create(&mqtt_task, 16*1024, APP_PRIORITY_ABOVE_NORMAL, "QmqttApp", mqtt_app_thread, NULL, 5);
  443. if(err != QL_OSI_SUCCESS)
  444. {
  445. QL_MQTT_LOG("mqtt_app init failed");
  446. }
  447. return err;
  448. }