mqtt_client.c 17 KB

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