1
0

mqtt_client.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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. hio_set_heartbeat(io, cli->keepalive * 1000, mqtt_send_ping);
  205. }
  206. }
  207. break;
  208. case MQTT_TYPE_PUBLISH:
  209. {
  210. if (cli->head.length < 2) {
  211. hloge("MQTT PUBLISH malformed!");
  212. hio_close(io);
  213. return;
  214. }
  215. memset(&cli->message, 0, sizeof(mqtt_message_t));
  216. POP16(p, cli->message.topic_len);
  217. if (end - p < cli->message.topic_len) {
  218. hloge("MQTT PUBLISH malformed!");
  219. hio_close(io);
  220. return;
  221. }
  222. // NOTE: Not deep copy
  223. cli->message.topic = (char*)p;
  224. p += cli->message.topic_len;
  225. if (cli->head.qos > 0) {
  226. if (end - p < 2) {
  227. hloge("MQTT PUBLISH malformed!");
  228. hio_close(io);
  229. return;
  230. }
  231. POP16(p, cli->mid);
  232. }
  233. cli->message.payload_len = end - p;
  234. if (cli->message.payload_len > 0) {
  235. // NOTE: Not deep copy
  236. cli->message.payload = (char*)p;
  237. }
  238. cli->message.qos = cli->head.qos;
  239. if (cli->message.qos == 0) {
  240. // Do nothing
  241. } else if (cli->message.qos == 1) {
  242. mqtt_send_head_with_mid(io, MQTT_TYPE_PUBACK, cli->mid);
  243. } else if (cli->message.qos == 2) {
  244. mqtt_send_head_with_mid(io, MQTT_TYPE_PUBREC, cli->mid);
  245. }
  246. }
  247. break;
  248. case MQTT_TYPE_PUBACK:
  249. case MQTT_TYPE_PUBREC:
  250. case MQTT_TYPE_PUBREL:
  251. case MQTT_TYPE_PUBCOMP:
  252. {
  253. if (cli->head.length < 2) {
  254. hloge("MQTT PUBACK malformed!");
  255. hio_close(io);
  256. return;
  257. }
  258. POP16(p, cli->mid);
  259. if (cli->head.type == MQTT_TYPE_PUBREC) {
  260. mqtt_send_head_with_mid(io, MQTT_TYPE_PUBREL, cli->mid);
  261. } else if (cli->head.type == MQTT_TYPE_PUBREL) {
  262. mqtt_send_head_with_mid(io, MQTT_TYPE_PUBCOMP, cli->mid);
  263. }
  264. }
  265. break;
  266. // case MQTT_TYPE_SUBSCRIBE:
  267. // break;
  268. case MQTT_TYPE_SUBACK:
  269. {
  270. if (cli->head.length < 2) {
  271. hloge("MQTT SUBACK malformed!");
  272. hio_close(io);
  273. return;
  274. }
  275. POP16(p, cli->mid);
  276. }
  277. break;
  278. // case MQTT_TYPE_UNSUBSCRIBE:
  279. // break;
  280. case MQTT_TYPE_UNSUBACK:
  281. {
  282. if (cli->head.length < 2) {
  283. hloge("MQTT UNSUBACK malformed!");
  284. hio_close(io);
  285. return;
  286. }
  287. POP16(p, cli->mid);
  288. }
  289. break;
  290. case MQTT_TYPE_PINGREQ:
  291. // printf("recv ping\n");
  292. // printf("send pong\n");
  293. mqtt_send_pong(io);
  294. return;
  295. case MQTT_TYPE_PINGRESP:
  296. // printf("recv pong\n");
  297. cli->ping_cnt = 0;
  298. return;
  299. case MQTT_TYPE_DISCONNECT:
  300. hio_close(io);
  301. return;
  302. default:
  303. hloge("MQTT client received wrong type=%d", (int)cli->head.type);
  304. hio_close(io);
  305. return;
  306. }
  307. if (cli->cb) {
  308. cli->cb(cli, cli->head.type);
  309. }
  310. }
  311. static void on_connect(hio_t* io) {
  312. mqtt_client_t* cli = (mqtt_client_t*)hevent_userdata(io);
  313. if (cli->cb) {
  314. cli->head.type = MQTT_TYPE_CONNECT;
  315. cli->cb(cli, cli->head.type);
  316. }
  317. if (cli->reconn_setting) {
  318. reconn_setting_reset(cli->reconn_setting);
  319. }
  320. static unpack_setting_t mqtt_unpack_setting;
  321. mqtt_unpack_setting.mode = UNPACK_BY_LENGTH_FIELD;
  322. mqtt_unpack_setting.package_max_length = DEFAULT_MQTT_PACKAGE_MAX_LENGTH;
  323. mqtt_unpack_setting.body_offset = 2;
  324. mqtt_unpack_setting.length_field_offset = 1;
  325. mqtt_unpack_setting.length_field_bytes = 1;
  326. mqtt_unpack_setting.length_field_coding = ENCODE_BY_VARINT;
  327. hio_set_unpack(io, &mqtt_unpack_setting);
  328. // start recv packet
  329. hio_setcb_read(io, on_packet);
  330. hio_read(io);
  331. mqtt_client_login(cli);
  332. }
  333. mqtt_client_t* mqtt_client_new(hloop_t* loop) {
  334. if (loop == NULL) {
  335. loop = hloop_new(HLOOP_FLAG_AUTO_FREE);
  336. if (loop == NULL) return NULL;
  337. }
  338. mqtt_client_t* cli = NULL;
  339. HV_ALLOC_SIZEOF(cli);
  340. if (cli == NULL) return NULL;
  341. cli->loop = loop;
  342. cli->protocol_version = MQTT_PROTOCOL_V311;
  343. cli->keepalive = DEFAULT_MQTT_KEEPALIVE;
  344. hmutex_init(&cli->mutex_);
  345. return cli;
  346. }
  347. void mqtt_client_free(mqtt_client_t* cli) {
  348. if (!cli) return;
  349. hmutex_destroy(&cli->mutex_);
  350. if (cli->reconn_timer) {
  351. htimer_del(cli->reconn_timer);
  352. cli->reconn_timer = NULL;
  353. }
  354. if (cli->ssl_ctx && cli->alloced_ssl_ctx) {
  355. hssl_ctx_free(cli->ssl_ctx);
  356. cli->ssl_ctx = NULL;
  357. }
  358. HV_FREE(cli->reconn_setting);
  359. HV_FREE(cli->will);
  360. HV_FREE(cli);
  361. }
  362. void mqtt_client_run (mqtt_client_t* cli) {
  363. if (!cli || !cli->loop) return;
  364. hloop_run(cli->loop);
  365. }
  366. void mqtt_client_stop(mqtt_client_t* cli) {
  367. if (!cli || !cli->loop) return;
  368. hloop_stop(cli->loop);
  369. }
  370. void mqtt_client_set_id(mqtt_client_t* cli, const char* id) {
  371. if (!cli || !id) return;
  372. hv_strncpy(cli->client_id, id, sizeof(cli->client_id));
  373. }
  374. void mqtt_client_set_will(mqtt_client_t* cli, mqtt_message_t* will) {
  375. if (!cli || !will) return;
  376. if (cli->will == NULL) {
  377. HV_ALLOC_SIZEOF(cli->will);
  378. }
  379. memcpy(cli->will, will, sizeof(mqtt_message_t));
  380. }
  381. void mqtt_client_set_auth(mqtt_client_t* cli, const char* username, const char* password) {
  382. if (!cli) return;
  383. if (username) {
  384. hv_strncpy(cli->username, username, sizeof(cli->username));
  385. }
  386. if (password) {
  387. hv_strncpy(cli->password, password, sizeof(cli->password));
  388. }
  389. }
  390. void mqtt_client_set_callback(mqtt_client_t* cli, mqtt_client_cb cb) {
  391. if (!cli) return;
  392. cli->cb = cb;
  393. }
  394. void mqtt_client_set_userdata(mqtt_client_t* cli, void* userdata) {
  395. if (!cli) return;
  396. cli->userdata = userdata;
  397. }
  398. void* mqtt_client_get_userdata(mqtt_client_t* cli) {
  399. if (!cli) return NULL;
  400. return cli->userdata;
  401. }
  402. int mqtt_client_get_last_error(mqtt_client_t* cli) {
  403. if (!cli) return -1;
  404. return cli->error;
  405. }
  406. int mqtt_client_set_ssl_ctx(mqtt_client_t* cli, hssl_ctx_t ssl_ctx) {
  407. cli->ssl_ctx = ssl_ctx;
  408. return 0;
  409. }
  410. int mqtt_client_new_ssl_ctx(mqtt_client_t* cli, hssl_ctx_opt_t* opt) {
  411. opt->endpoint = HSSL_CLIENT;
  412. hssl_ctx_t ssl_ctx = hssl_ctx_new(opt);
  413. if (ssl_ctx == NULL) return ERR_NEW_SSL_CTX;
  414. cli->alloced_ssl_ctx = true;
  415. return mqtt_client_set_ssl_ctx(cli, ssl_ctx);
  416. }
  417. int mqtt_client_set_reconnect(mqtt_client_t* cli, reconn_setting_t* reconn) {
  418. if (reconn == NULL) {
  419. HV_FREE(cli->reconn_setting);
  420. return 0;
  421. }
  422. if (cli->reconn_setting == NULL) {
  423. HV_ALLOC_SIZEOF(cli->reconn_setting);
  424. }
  425. *cli->reconn_setting = *reconn;
  426. return 0;
  427. }
  428. int mqtt_client_reconnect(mqtt_client_t* cli) {
  429. mqtt_client_connect(cli, cli->host, cli->port, cli->ssl);
  430. return 0;
  431. }
  432. void mqtt_client_set_connect_timeout(mqtt_client_t* cli, int ms) {
  433. cli->connect_timeout = ms;
  434. }
  435. int mqtt_client_connect(mqtt_client_t* cli, const char* host, int port, int ssl) {
  436. if (!cli) return -1;
  437. hv_strncpy(cli->host, host, sizeof(cli->host));
  438. cli->port = port;
  439. cli->ssl = ssl;
  440. hio_t* io = hio_create_socket(cli->loop, host, port, HIO_TYPE_TCP, HIO_CLIENT_SIDE);
  441. if (io == NULL) return -1;
  442. if (ssl) {
  443. if (cli->ssl_ctx) {
  444. hio_set_ssl_ctx(io, cli->ssl_ctx);
  445. }
  446. hio_enable_ssl(io);
  447. }
  448. if (cli->connect_timeout > 0) {
  449. hio_set_connect_timeout(io, cli->connect_timeout);
  450. }
  451. cli->io = io;
  452. hevent_set_userdata(io, cli);
  453. hio_setcb_connect(io, on_connect);
  454. hio_setcb_close(io, on_close);
  455. return hio_connect(io);
  456. }
  457. bool mqtt_client_is_connected(mqtt_client_t* cli) {
  458. return cli && cli->connected;
  459. }
  460. int mqtt_client_disconnect(mqtt_client_t* cli) {
  461. if (!cli || !cli->io) return -1;
  462. // cancel reconnect first
  463. mqtt_client_set_reconnect(cli, NULL);
  464. mqtt_send_disconnect(cli->io);
  465. return hio_close(cli->io);
  466. }
  467. int mqtt_client_publish(mqtt_client_t* cli, mqtt_message_t* msg) {
  468. if (!cli || !cli->io || !msg) return -1;
  469. if (!cli->connected) return -2;
  470. int topic_len = msg->topic_len ? msg->topic_len : strlen(msg->topic);
  471. int payload_len = msg->payload_len ? msg->payload_len : strlen(msg->payload);
  472. int len = 2 + topic_len + payload_len;
  473. if (msg->qos > 0) len += 2; // mid
  474. unsigned short mid = 0;
  475. mqtt_head_t head;
  476. memset(&head, 0, sizeof(head));
  477. head.type = MQTT_TYPE_PUBLISH;
  478. head.qos = msg->qos & 3;
  479. head.retain = msg->retain;
  480. head.length = len;
  481. int buflen = mqtt_estimate_length(&head);
  482. // NOTE: send payload alone
  483. buflen -= payload_len;
  484. unsigned char* buf = NULL;
  485. HV_STACK_ALLOC(buf, buflen);
  486. unsigned char* p = buf;
  487. int headlen = mqtt_head_pack(&head, p);
  488. p += headlen;
  489. PUSH16(p, topic_len);
  490. PUSH_N(p, msg->topic, topic_len);
  491. if (msg->qos) {
  492. mid = mqtt_next_mid();
  493. PUSH16(p, mid);
  494. }
  495. hmutex_lock(&cli->mutex_);
  496. // send head + topic + mid
  497. int nwrite = hio_write(cli->io, buf, p - buf);
  498. HV_STACK_FREE(buf);
  499. if (nwrite < 0) {
  500. goto unlock;
  501. }
  502. // send payload
  503. nwrite = hio_write(cli->io, msg->payload, payload_len);
  504. unlock:
  505. hmutex_unlock(&cli->mutex_);
  506. return nwrite < 0 ? nwrite : mid;
  507. }
  508. int mqtt_client_subscribe(mqtt_client_t* cli, const char* topic, int qos) {
  509. if (!cli || !cli->io || !topic) return -1;
  510. if (!cli->connected) return -2;
  511. int topic_len = strlen(topic);
  512. int len = 2 + 2 + topic_len + 1;
  513. mqtt_head_t head;
  514. memset(&head, 0, sizeof(head));
  515. head.type = MQTT_TYPE_SUBSCRIBE;
  516. head.qos = 1;
  517. head.length = len;
  518. int buflen = mqtt_estimate_length(&head);
  519. unsigned char* buf = NULL;
  520. HV_STACK_ALLOC(buf, buflen);
  521. unsigned char* p = buf;
  522. int headlen = mqtt_head_pack(&head, p);
  523. p += headlen;
  524. unsigned short mid = mqtt_next_mid();
  525. PUSH16(p, mid);
  526. PUSH16(p, topic_len);
  527. PUSH_N(p, topic, topic_len);
  528. PUSH8(p, qos & 3);
  529. // send head + mid + topic + qos
  530. int nwrite = mqtt_client_send(cli, buf, p - buf);
  531. HV_STACK_FREE(buf);
  532. return nwrite < 0 ? nwrite : mid;
  533. }
  534. int mqtt_client_unsubscribe(mqtt_client_t* cli, const char* topic) {
  535. if (!cli || !cli->io || !topic) return -1;
  536. if (!cli->connected) return -2;
  537. int topic_len = strlen(topic);
  538. int len = 2 + 2 + topic_len;
  539. mqtt_head_t head;
  540. memset(&head, 0, sizeof(head));
  541. head.type = MQTT_TYPE_UNSUBSCRIBE;
  542. head.qos = 1;
  543. head.length = len;
  544. int buflen = mqtt_estimate_length(&head);
  545. unsigned char* buf = NULL;
  546. HV_STACK_ALLOC(buf, buflen);
  547. unsigned char* p = buf;
  548. int headlen = mqtt_head_pack(&head, p);
  549. p += headlen;
  550. unsigned short mid = mqtt_next_mid();
  551. PUSH16(p, mid);
  552. PUSH16(p, topic_len);
  553. PUSH_N(p, topic, topic_len);
  554. // send head + mid + topic
  555. int nwrite = mqtt_client_send(cli, buf, p - buf);
  556. HV_STACK_FREE(buf);
  557. return nwrite < 0 ? nwrite : mid;
  558. }