mqtt_client.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. #include "mqtt_client.h"
  2. #include "hbase.h"
  3. #include "hlog.h"
  4. #include "herr.h"
  5. #include "hendian.h"
  6. static unsigned short mqtt_next_mid() {
  7. static unsigned short s_mid = 0;
  8. return ++s_mid;
  9. }
  10. static int mqtt_client_send(mqtt_client_t* cli, const void* buf, int len) {
  11. // thread-safe
  12. hmutex_lock(&cli->mutex_);
  13. int nwrite = hio_write(cli->io, buf, len);
  14. hmutex_unlock(&cli->mutex_);
  15. return nwrite;
  16. }
  17. static int mqtt_send_head(hio_t* io, int type, int length) {
  18. mqtt_client_t* cli = (mqtt_client_t*)hevent_userdata(io);
  19. mqtt_head_t head;
  20. memset(&head, 0, sizeof(head));
  21. head.type = type;
  22. head.length = length;
  23. unsigned char headbuf[8] = { 0 };
  24. int headlen = mqtt_head_pack(&head, headbuf);
  25. return mqtt_client_send(cli, headbuf, headlen);
  26. }
  27. static int mqtt_send_head_with_mid(hio_t* io, int type, unsigned short mid) {
  28. mqtt_client_t* cli = (mqtt_client_t*)hevent_userdata(io);
  29. mqtt_head_t head;
  30. memset(&head, 0, sizeof(head));
  31. head.type = type;
  32. if (head.type == MQTT_TYPE_PUBREL) {
  33. head.qos = 1;
  34. }
  35. head.length = 2;
  36. unsigned char headbuf[8] = { 0 };
  37. unsigned char* p = headbuf;
  38. int headlen = mqtt_head_pack(&head, p);
  39. p += headlen;
  40. PUSH16(p, mid);
  41. return mqtt_client_send(cli, headbuf, headlen + 2);
  42. }
  43. static void mqtt_send_ping(hio_t* io) {
  44. mqtt_client_t* cli = (mqtt_client_t*)hevent_userdata(io);
  45. if (cli->ping_cnt++ == 3) {
  46. hloge("mqtt no pong!");
  47. hio_close(io);
  48. return;
  49. }
  50. mqtt_send_head(io, MQTT_TYPE_PINGREQ, 0);
  51. }
  52. static void mqtt_send_pong(hio_t* io) {
  53. mqtt_send_head(io, MQTT_TYPE_PINGRESP, 0);
  54. }
  55. static void mqtt_send_disconnect(hio_t* io) {
  56. mqtt_send_head(io, MQTT_TYPE_DISCONNECT, 0);
  57. }
  58. /*
  59. * MQTT_TYPE_CONNECT
  60. * 2 + protocol_name + 1 protocol_version + 1 conn_flags + 2 keepalive + 2 + [client_id] +
  61. * [2 + will_topic + 2 + will_payload] +
  62. * [2 + username] + [2 + password]
  63. */
  64. static int mqtt_client_login(mqtt_client_t* cli) {
  65. int len = 2 + 1 + 1 + 2 + 2;
  66. unsigned short cid_len = 0,
  67. will_topic_len = 0,
  68. will_payload_len = 0,
  69. username_len = 0,
  70. password_len = 0;
  71. unsigned char conn_flags = 0;
  72. // protocol_name_len
  73. len += cli->protocol_version == MQTT_PROTOCOL_V31 ? 6 : 4;
  74. if (*cli->client_id) {
  75. cid_len = strlen(cli->client_id);
  76. } else {
  77. cid_len = 20;
  78. hv_random_string(cli->client_id, cid_len);
  79. hlogi("MQTT client_id: %.*s", (int)cid_len, cli->client_id);
  80. }
  81. len += cid_len;
  82. if (cid_len == 0) cli->clean_session = 1;
  83. if (cli->clean_session) {
  84. conn_flags |= MQTT_CONN_CLEAN_SESSION;
  85. }
  86. if (cli->will && cli->will->topic && cli->will->payload) {
  87. will_topic_len = cli->will->topic_len ? cli->will->topic_len : strlen(cli->will->topic);
  88. will_payload_len = cli->will->payload_len ? cli->will->payload_len : strlen(cli->will->payload);
  89. if (will_topic_len && will_payload_len) {
  90. conn_flags |= MQTT_CONN_HAS_WILL;
  91. conn_flags |= ((cli->will->qos & 3) << 3);
  92. if (cli->will->retain) {
  93. conn_flags |= MQTT_CONN_WILL_RETAIN;
  94. }
  95. len += 2 + will_topic_len;
  96. len += 2 + will_payload_len;
  97. }
  98. }
  99. if (*cli->username) {
  100. username_len = strlen(cli->username);
  101. if (username_len) {
  102. conn_flags |= MQTT_CONN_HAS_USERNAME;
  103. len += 2 + username_len;
  104. }
  105. }
  106. if (*cli->password) {
  107. password_len = strlen(cli->password);
  108. if (password_len) {
  109. conn_flags |= MQTT_CONN_HAS_PASSWORD;
  110. len += 2 + password_len;
  111. }
  112. }
  113. mqtt_head_t head;
  114. memset(&head, 0, sizeof(head));
  115. head.type = MQTT_TYPE_CONNECT;
  116. head.length = len;
  117. int buflen = mqtt_estimate_length(&head);
  118. unsigned char* buf = NULL;
  119. HV_STACK_ALLOC(buf, buflen);
  120. unsigned char* p = buf;
  121. int headlen = mqtt_head_pack(&head, p);
  122. p += headlen;
  123. // TODO: Not implement MQTT_PROTOCOL_V5
  124. if (cli->protocol_version == MQTT_PROTOCOL_V31) {
  125. PUSH16(p, 6);
  126. PUSH_N(p, MQTT_PROTOCOL_NAME_v31, 6);
  127. } else {
  128. PUSH16(p, 4);
  129. PUSH_N(p, MQTT_PROTOCOL_NAME, 4);
  130. }
  131. PUSH8(p, cli->protocol_version);
  132. PUSH8(p, conn_flags);
  133. PUSH16(p, cli->keepalive);
  134. PUSH16(p, cid_len);
  135. if (cid_len > 0) {
  136. PUSH_N(p, cli->client_id, cid_len);
  137. }
  138. if (conn_flags & MQTT_CONN_HAS_WILL) {
  139. PUSH16(p, will_topic_len);
  140. PUSH_N(p, cli->will->topic, will_topic_len);
  141. PUSH16(p, will_payload_len);
  142. PUSH_N(p, cli->will->payload, will_payload_len);
  143. }
  144. if (conn_flags & MQTT_CONN_HAS_USERNAME) {
  145. PUSH16(p, username_len);
  146. PUSH_N(p, cli->username, username_len);
  147. }
  148. if (conn_flags & MQTT_CONN_HAS_PASSWORD) {
  149. PUSH16(p, password_len);
  150. PUSH_N(p, cli->password, password_len);
  151. }
  152. int nwrite = mqtt_client_send(cli, buf, p - buf);
  153. HV_STACK_FREE(buf);
  154. return nwrite < 0 ? nwrite : 0;
  155. }
  156. static void reconnect_timer_cb(htimer_t* timer) {
  157. mqtt_client_t* cli = (mqtt_client_t*)hevent_userdata(timer);
  158. if (cli == NULL) return;
  159. cli->reconn_timer = NULL;
  160. mqtt_client_reconnect(cli);
  161. }
  162. static void on_close(hio_t* io) {
  163. mqtt_client_t* cli = (mqtt_client_t*)hevent_userdata(io);
  164. cli->connected = 0;
  165. if (cli->cb) {
  166. cli->head.type = MQTT_TYPE_DISCONNECT;
  167. cli->cb(cli, cli->head.type);
  168. }
  169. // reconnect
  170. if (cli->reconn_setting && reconn_setting_can_retry(cli->reconn_setting)) {
  171. uint32_t delay = reconn_setting_calc_delay(cli->reconn_setting);
  172. cli->reconn_timer = htimer_add(cli->loop, reconnect_timer_cb, delay, 1);
  173. hevent_set_userdata(cli->reconn_timer, cli);
  174. }
  175. }
  176. static void on_packet(hio_t* io, void* buf, int len) {
  177. mqtt_client_t* cli = (mqtt_client_t*)hevent_userdata(io);
  178. unsigned char* p = (unsigned char*)buf;
  179. unsigned char* end = p + len;
  180. memset(&cli->head, 0, sizeof(mqtt_head_t));
  181. int headlen = mqtt_head_unpack(&cli->head, p, len);
  182. if (headlen <= 0) return;
  183. p += headlen;
  184. switch (cli->head.type) {
  185. // case MQTT_TYPE_CONNECT:
  186. case MQTT_TYPE_CONNACK:
  187. {
  188. if (cli->head.length < 2) {
  189. hloge("MQTT CONNACK malformed!");
  190. hio_close(io);
  191. return;
  192. }
  193. unsigned char conn_flags = 0, rc = 0;
  194. POP8(p, conn_flags);
  195. POP8(p, rc);
  196. if (rc != MQTT_CONNACK_ACCEPTED) {
  197. cli->error = rc;
  198. hloge("MQTT CONNACK error=%d", cli->error);
  199. hio_close(io);
  200. return;
  201. }
  202. cli->connected = 1;
  203. if (cli->keepalive) {
  204. cli->ping_cnt = 0;
  205. hio_set_heartbeat(io, cli->keepalive * 1000, mqtt_send_ping);
  206. }
  207. }
  208. break;
  209. case MQTT_TYPE_PUBLISH:
  210. {
  211. if (cli->head.length < 2) {
  212. hloge("MQTT PUBLISH malformed!");
  213. hio_close(io);
  214. return;
  215. }
  216. memset(&cli->message, 0, sizeof(mqtt_message_t));
  217. POP16(p, cli->message.topic_len);
  218. if (end - p < cli->message.topic_len) {
  219. hloge("MQTT PUBLISH malformed!");
  220. hio_close(io);
  221. return;
  222. }
  223. // NOTE: Not deep copy
  224. cli->message.topic = (char*)p;
  225. p += cli->message.topic_len;
  226. if (cli->head.qos > 0) {
  227. if (end - p < 2) {
  228. hloge("MQTT PUBLISH malformed!");
  229. hio_close(io);
  230. return;
  231. }
  232. POP16(p, cli->mid);
  233. }
  234. cli->message.payload_len = end - p;
  235. if (cli->message.payload_len > 0) {
  236. // NOTE: Not deep copy
  237. cli->message.payload = (char*)p;
  238. }
  239. cli->message.qos = cli->head.qos;
  240. if (cli->message.qos == 0) {
  241. // Do nothing
  242. } else if (cli->message.qos == 1) {
  243. mqtt_send_head_with_mid(io, MQTT_TYPE_PUBACK, cli->mid);
  244. } else if (cli->message.qos == 2) {
  245. mqtt_send_head_with_mid(io, MQTT_TYPE_PUBREC, cli->mid);
  246. }
  247. }
  248. break;
  249. case MQTT_TYPE_PUBACK:
  250. case MQTT_TYPE_PUBREC:
  251. case MQTT_TYPE_PUBREL:
  252. case MQTT_TYPE_PUBCOMP:
  253. {
  254. if (cli->head.length < 2) {
  255. hloge("MQTT PUBACK malformed!");
  256. hio_close(io);
  257. return;
  258. }
  259. POP16(p, cli->mid);
  260. if (cli->head.type == MQTT_TYPE_PUBREC) {
  261. mqtt_send_head_with_mid(io, MQTT_TYPE_PUBREL, cli->mid);
  262. } else if (cli->head.type == MQTT_TYPE_PUBREL) {
  263. mqtt_send_head_with_mid(io, MQTT_TYPE_PUBCOMP, cli->mid);
  264. }
  265. }
  266. break;
  267. // case MQTT_TYPE_SUBSCRIBE:
  268. // break;
  269. case MQTT_TYPE_SUBACK:
  270. {
  271. if (cli->head.length < 2) {
  272. hloge("MQTT SUBACK malformed!");
  273. hio_close(io);
  274. return;
  275. }
  276. POP16(p, cli->mid);
  277. }
  278. break;
  279. // case MQTT_TYPE_UNSUBSCRIBE:
  280. // break;
  281. case MQTT_TYPE_UNSUBACK:
  282. {
  283. if (cli->head.length < 2) {
  284. hloge("MQTT UNSUBACK malformed!");
  285. hio_close(io);
  286. return;
  287. }
  288. POP16(p, cli->mid);
  289. }
  290. break;
  291. case MQTT_TYPE_PINGREQ:
  292. // printf("recv ping\n");
  293. // printf("send pong\n");
  294. mqtt_send_pong(io);
  295. return;
  296. case MQTT_TYPE_PINGRESP:
  297. // printf("recv pong\n");
  298. cli->ping_cnt = 0;
  299. return;
  300. case MQTT_TYPE_DISCONNECT:
  301. hio_close(io);
  302. return;
  303. default:
  304. hloge("MQTT client received wrong type=%d", (int)cli->head.type);
  305. hio_close(io);
  306. return;
  307. }
  308. if (cli->cb) {
  309. cli->cb(cli, cli->head.type);
  310. }
  311. }
  312. static void on_connect(hio_t* io) {
  313. mqtt_client_t* cli = (mqtt_client_t*)hevent_userdata(io);
  314. if (cli->cb) {
  315. cli->head.type = MQTT_TYPE_CONNECT;
  316. cli->cb(cli, cli->head.type);
  317. }
  318. if (cli->reconn_setting) {
  319. reconn_setting_reset(cli->reconn_setting);
  320. }
  321. static unpack_setting_t mqtt_unpack_setting;
  322. mqtt_unpack_setting.mode = UNPACK_BY_LENGTH_FIELD;
  323. mqtt_unpack_setting.package_max_length = DEFAULT_MQTT_PACKAGE_MAX_LENGTH;
  324. mqtt_unpack_setting.body_offset = 2;
  325. mqtt_unpack_setting.length_field_offset = 1;
  326. mqtt_unpack_setting.length_field_bytes = 1;
  327. mqtt_unpack_setting.length_field_coding = ENCODE_BY_VARINT;
  328. hio_set_unpack(io, &mqtt_unpack_setting);
  329. // start recv packet
  330. hio_setcb_read(io, on_packet);
  331. hio_read(io);
  332. mqtt_client_login(cli);
  333. }
  334. mqtt_client_t* mqtt_client_new(hloop_t* loop) {
  335. if (loop == NULL) {
  336. loop = hloop_new(HLOOP_FLAG_AUTO_FREE);
  337. if (loop == NULL) return NULL;
  338. }
  339. mqtt_client_t* cli = NULL;
  340. HV_ALLOC_SIZEOF(cli);
  341. if (cli == NULL) return NULL;
  342. cli->loop = loop;
  343. cli->protocol_version = MQTT_PROTOCOL_V311;
  344. cli->keepalive = DEFAULT_MQTT_KEEPALIVE;
  345. hmutex_init(&cli->mutex_);
  346. return cli;
  347. }
  348. void mqtt_client_free(mqtt_client_t* cli) {
  349. if (!cli) return;
  350. hmutex_destroy(&cli->mutex_);
  351. if (cli->ssl_ctx && cli->alloced_ssl_ctx) {
  352. hssl_ctx_free(cli->ssl_ctx);
  353. cli->ssl_ctx = NULL;
  354. }
  355. HV_FREE(cli->reconn_setting);
  356. HV_FREE(cli->will);
  357. HV_FREE(cli);
  358. }
  359. void mqtt_client_run (mqtt_client_t* cli) {
  360. if (!cli || !cli->loop) return;
  361. hloop_run(cli->loop);
  362. }
  363. void mqtt_client_stop(mqtt_client_t* cli) {
  364. if (!cli || !cli->loop) return;
  365. hloop_stop(cli->loop);
  366. }
  367. void mqtt_client_set_id(mqtt_client_t* cli, const char* id) {
  368. if (!cli || !id) return;
  369. hv_strncpy(cli->client_id, id, sizeof(cli->client_id));
  370. }
  371. void mqtt_client_set_will(mqtt_client_t* cli, mqtt_message_t* will) {
  372. if (!cli || !will) return;
  373. if (cli->will == NULL) {
  374. HV_ALLOC_SIZEOF(cli->will);
  375. }
  376. memcpy(cli->will, will, sizeof(mqtt_message_t));
  377. }
  378. void mqtt_client_set_auth(mqtt_client_t* cli, const char* username, const char* password) {
  379. if (!cli) return;
  380. if (username) {
  381. hv_strncpy(cli->username, username, sizeof(cli->username));
  382. }
  383. if (password) {
  384. hv_strncpy(cli->password, password, sizeof(cli->password));
  385. }
  386. }
  387. void mqtt_client_set_callback(mqtt_client_t* cli, mqtt_client_cb cb) {
  388. if (!cli) return;
  389. cli->cb = cb;
  390. }
  391. void mqtt_client_set_userdata(mqtt_client_t* cli, void* userdata) {
  392. if (!cli) return;
  393. cli->userdata = userdata;
  394. }
  395. void* mqtt_client_get_userdata(mqtt_client_t* cli) {
  396. if (!cli) return NULL;
  397. return cli->userdata;
  398. }
  399. int mqtt_client_get_last_error(mqtt_client_t* cli) {
  400. if (!cli) return -1;
  401. return cli->error;
  402. }
  403. int mqtt_client_set_ssl_ctx(mqtt_client_t* cli, hssl_ctx_t ssl_ctx) {
  404. cli->ssl_ctx = ssl_ctx;
  405. return 0;
  406. }
  407. int mqtt_client_new_ssl_ctx(mqtt_client_t* cli, hssl_ctx_opt_t* opt) {
  408. opt->endpoint = HSSL_CLIENT;
  409. hssl_ctx_t ssl_ctx = hssl_ctx_new(opt);
  410. if (ssl_ctx == NULL) return ERR_NEW_SSL_CTX;
  411. cli->alloced_ssl_ctx = true;
  412. return mqtt_client_set_ssl_ctx(cli, ssl_ctx);
  413. }
  414. int mqtt_client_set_reconnect(mqtt_client_t* cli, reconn_setting_t* reconn) {
  415. if (reconn == NULL) {
  416. HV_FREE(cli->reconn_setting);
  417. return 0;
  418. }
  419. if (cli->reconn_setting == NULL) {
  420. HV_ALLOC_SIZEOF(cli->reconn_setting);
  421. }
  422. *cli->reconn_setting = *reconn;
  423. return 0;
  424. }
  425. int mqtt_client_reconnect(mqtt_client_t* cli) {
  426. mqtt_client_connect(cli, cli->host, cli->port, cli->ssl);
  427. return 0;
  428. }
  429. void mqtt_client_set_connect_timeout(mqtt_client_t* cli, int ms) {
  430. cli->connect_timeout = ms;
  431. }
  432. int mqtt_client_connect(mqtt_client_t* cli, const char* host, int port, int ssl) {
  433. if (!cli) return -1;
  434. hv_strncpy(cli->host, host, sizeof(cli->host));
  435. cli->port = port;
  436. cli->ssl = ssl;
  437. hio_t* io = hio_create_socket(cli->loop, host, port, HIO_TYPE_TCP, HIO_CLIENT_SIDE);
  438. if (io == NULL) return -1;
  439. if (ssl) {
  440. if (cli->ssl_ctx) {
  441. hio_set_ssl_ctx(io, cli->ssl_ctx);
  442. }
  443. hio_enable_ssl(io);
  444. }
  445. if (cli->connect_timeout > 0) {
  446. hio_set_connect_timeout(io, cli->connect_timeout);
  447. }
  448. cli->io = io;
  449. hevent_set_userdata(io, cli);
  450. hio_setcb_connect(io, on_connect);
  451. hio_setcb_close(io, on_close);
  452. return hio_connect(io);
  453. }
  454. bool mqtt_client_is_connected(mqtt_client_t* cli) {
  455. return cli && cli->connected;
  456. }
  457. int mqtt_client_disconnect(mqtt_client_t* cli) {
  458. if (!cli || !cli->io) return -1;
  459. // cancel reconnect first
  460. mqtt_client_set_reconnect(cli, NULL);
  461. mqtt_send_disconnect(cli->io);
  462. return hio_close(cli->io);
  463. }
  464. int mqtt_client_publish(mqtt_client_t* cli, mqtt_message_t* msg) {
  465. if (!cli || !cli->io || !msg) return -1;
  466. if (!cli->connected) return -2;
  467. int topic_len = msg->topic_len ? msg->topic_len : strlen(msg->topic);
  468. int payload_len = msg->payload_len ? msg->payload_len : strlen(msg->payload);
  469. int len = 2 + topic_len + payload_len;
  470. if (msg->qos > 0) len += 2; // mid
  471. unsigned short mid = 0;
  472. mqtt_head_t head;
  473. memset(&head, 0, sizeof(head));
  474. head.type = MQTT_TYPE_PUBLISH;
  475. head.qos = msg->qos & 3;
  476. head.retain = msg->retain;
  477. head.length = len;
  478. int buflen = mqtt_estimate_length(&head);
  479. // NOTE: send payload alone
  480. buflen -= payload_len;
  481. unsigned char* buf = NULL;
  482. HV_STACK_ALLOC(buf, buflen);
  483. unsigned char* p = buf;
  484. int headlen = mqtt_head_pack(&head, p);
  485. p += headlen;
  486. PUSH16(p, topic_len);
  487. PUSH_N(p, msg->topic, topic_len);
  488. if (msg->qos) {
  489. mid = mqtt_next_mid();
  490. PUSH16(p, mid);
  491. }
  492. hmutex_lock(&cli->mutex_);
  493. // send head + topic + mid
  494. int nwrite = hio_write(cli->io, buf, p - buf);
  495. HV_STACK_FREE(buf);
  496. if (nwrite < 0) {
  497. goto unlock;
  498. }
  499. // send payload
  500. nwrite = hio_write(cli->io, msg->payload, payload_len);
  501. unlock:
  502. hmutex_unlock(&cli->mutex_);
  503. return nwrite < 0 ? nwrite : mid;
  504. }
  505. int mqtt_client_subscribe(mqtt_client_t* cli, const char* topic, int qos) {
  506. if (!cli || !cli->io || !topic) return -1;
  507. if (!cli->connected) return -2;
  508. int topic_len = strlen(topic);
  509. int len = 2 + 2 + topic_len + 1;
  510. mqtt_head_t head;
  511. memset(&head, 0, sizeof(head));
  512. head.type = MQTT_TYPE_SUBSCRIBE;
  513. head.qos = 1;
  514. head.length = len;
  515. int buflen = mqtt_estimate_length(&head);
  516. unsigned char* buf = NULL;
  517. HV_STACK_ALLOC(buf, buflen);
  518. unsigned char* p = buf;
  519. int headlen = mqtt_head_pack(&head, p);
  520. p += headlen;
  521. unsigned short mid = mqtt_next_mid();
  522. PUSH16(p, mid);
  523. PUSH16(p, topic_len);
  524. PUSH_N(p, topic, topic_len);
  525. PUSH8(p, qos & 3);
  526. // send head + mid + topic + qos
  527. int nwrite = mqtt_client_send(cli, buf, p - buf);
  528. HV_STACK_FREE(buf);
  529. return nwrite < 0 ? nwrite : mid;
  530. }
  531. int mqtt_client_unsubscribe(mqtt_client_t* cli, const char* topic) {
  532. if (!cli || !cli->io || !topic) return -1;
  533. if (!cli->connected) return -2;
  534. int topic_len = strlen(topic);
  535. int len = 2 + 2 + topic_len;
  536. mqtt_head_t head;
  537. memset(&head, 0, sizeof(head));
  538. head.type = MQTT_TYPE_UNSUBSCRIBE;
  539. head.qos = 1;
  540. head.length = len;
  541. int buflen = mqtt_estimate_length(&head);
  542. unsigned char* buf = NULL;
  543. HV_STACK_ALLOC(buf, buflen);
  544. unsigned char* p = buf;
  545. int headlen = mqtt_head_pack(&head, p);
  546. p += headlen;
  547. unsigned short mid = mqtt_next_mid();
  548. PUSH16(p, mid);
  549. PUSH16(p, topic_len);
  550. PUSH_N(p, topic, topic_len);
  551. // send head + mid + topic
  552. int nwrite = mqtt_client_send(cli, buf, p - buf);
  553. HV_STACK_FREE(buf);
  554. return nwrite < 0 ? nwrite : mid;
  555. }