mqtt_msg.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /*
  2. * Copyright (c) 2014, Stephen Robinson
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the copyright holder nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  22. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #include <string.h>
  32. #include "mqtt/mqtt_msg.h"
  33. #include "user_config.h"
  34. #define MQTT_MAX_FIXED_HEADER_SIZE 3
  35. enum mqtt_connect_flag
  36. {
  37. MQTT_CONNECT_FLAG_USERNAME = 1 << 7,
  38. MQTT_CONNECT_FLAG_PASSWORD = 1 << 6,
  39. MQTT_CONNECT_FLAG_WILL_RETAIN = 1 << 5,
  40. MQTT_CONNECT_FLAG_WILL = 1 << 2,
  41. MQTT_CONNECT_FLAG_CLEAN_SESSION = 1 << 1
  42. };
  43. struct __attribute((__packed__)) mqtt_connect_variable_header
  44. {
  45. uint8_t lengthMsb;
  46. uint8_t lengthLsb;
  47. #if defined(PROTOCOL_NAMEv31)
  48. uint8_t magic[6];
  49. #elif defined(PROTOCOL_NAMEv311)
  50. uint8_t magic[4];
  51. #else
  52. #error "Please define protocol name"
  53. #endif
  54. uint8_t version;
  55. uint8_t flags;
  56. uint8_t keepaliveMsb;
  57. uint8_t keepaliveLsb;
  58. };
  59. static int ICACHE_FLASH_ATTR append_string(mqtt_connection_t* connection, const char* string, int len)
  60. {
  61. if (connection->message.length + len + 2 > connection->buffer_length)
  62. return -1;
  63. connection->buffer[connection->message.length++] = len >> 8;
  64. connection->buffer[connection->message.length++] = len & 0xff;
  65. memcpy(connection->buffer + connection->message.length, string, len);
  66. connection->message.length += len;
  67. return len + 2;
  68. }
  69. static uint16_t ICACHE_FLASH_ATTR append_message_id(mqtt_connection_t* connection, uint16_t message_id)
  70. {
  71. // If message_id is zero then we should assign one, otherwise
  72. // we'll use the one supplied by the caller
  73. while (message_id == 0)
  74. message_id = ++connection->message_id;
  75. if (connection->message.length + 2 > connection->buffer_length)
  76. return 0;
  77. connection->buffer[connection->message.length++] = message_id >> 8;
  78. connection->buffer[connection->message.length++] = message_id & 0xff;
  79. return message_id;
  80. }
  81. static int ICACHE_FLASH_ATTR init_message(mqtt_connection_t* connection)
  82. {
  83. connection->message.length = MQTT_MAX_FIXED_HEADER_SIZE;
  84. return MQTT_MAX_FIXED_HEADER_SIZE;
  85. }
  86. static mqtt_message_t* ICACHE_FLASH_ATTR fail_message(mqtt_connection_t* connection)
  87. {
  88. connection->message.data = connection->buffer;
  89. connection->message.length = 0;
  90. return &connection->message;
  91. }
  92. static mqtt_message_t* ICACHE_FLASH_ATTR fini_message(mqtt_connection_t* connection, int type, int dup, int qos, int retain)
  93. {
  94. int remaining_length = connection->message.length - MQTT_MAX_FIXED_HEADER_SIZE;
  95. if (remaining_length > 127)
  96. {
  97. connection->buffer[0] = ((type & 0x0f) << 4) | ((dup & 1) << 3) | ((qos & 3) << 1) | (retain & 1);
  98. connection->buffer[1] = 0x80 | (remaining_length % 128);
  99. connection->buffer[2] = remaining_length / 128;
  100. connection->message.length = remaining_length + 3;
  101. connection->message.data = connection->buffer;
  102. }
  103. else
  104. {
  105. connection->buffer[1] = ((type & 0x0f) << 4) | ((dup & 1) << 3) | ((qos & 3) << 1) | (retain & 1);
  106. connection->buffer[2] = remaining_length;
  107. connection->message.length = remaining_length + 2;
  108. connection->message.data = connection->buffer + 1;
  109. }
  110. return &connection->message;
  111. }
  112. void ICACHE_FLASH_ATTR mqtt_msg_init(mqtt_connection_t* connection, uint8_t* buffer, uint16_t buffer_length)
  113. {
  114. memset(connection, 0, sizeof(mqtt_connection_t));
  115. connection->buffer = buffer;
  116. connection->buffer_length = buffer_length;
  117. }
  118. int ICACHE_FLASH_ATTR mqtt_get_total_length(uint8_t* buffer, uint16_t length)
  119. {
  120. int i;
  121. int totlen = 0;
  122. for (i = 1; i < length; ++i)
  123. {
  124. totlen += (buffer[i] & 0x7f) << (7 * (i - 1));
  125. if ((buffer[i] & 0x80) == 0)
  126. {
  127. ++i;
  128. break;
  129. }
  130. }
  131. totlen += i;
  132. return totlen;
  133. }
  134. const char* ICACHE_FLASH_ATTR mqtt_get_publish_topic(uint8_t* buffer, uint16_t* length)
  135. {
  136. int i;
  137. int totlen = 0;
  138. int topiclen;
  139. for (i = 1; i < *length; ++i)
  140. {
  141. totlen += (buffer[i] & 0x7f) << (7 * (i - 1));
  142. if ((buffer[i] & 0x80) == 0)
  143. {
  144. ++i;
  145. break;
  146. }
  147. }
  148. totlen += i;
  149. if (i + 2 >= *length)
  150. return NULL;
  151. topiclen = buffer[i++] << 8;
  152. topiclen |= buffer[i++];
  153. if (i + topiclen > *length)
  154. return NULL;
  155. *length = topiclen;
  156. return (const char*)(buffer + i);
  157. }
  158. const char* ICACHE_FLASH_ATTR mqtt_get_publish_data(uint8_t* buffer, uint16_t* length)
  159. {
  160. int i;
  161. int totlen = 0;
  162. int topiclen;
  163. int blength = *length;
  164. *length = 0;
  165. for (i = 1; i < blength; ++i)
  166. {
  167. totlen += (buffer[i] & 0x7f) << (7 * (i - 1));
  168. if ((buffer[i] & 0x80) == 0)
  169. {
  170. ++i;
  171. break;
  172. }
  173. }
  174. totlen += i;
  175. if (i + 2 >= blength)
  176. return NULL;
  177. topiclen = buffer[i++] << 8;
  178. topiclen |= buffer[i++];
  179. if (i + topiclen >= blength)
  180. return NULL;
  181. i += topiclen;
  182. if (mqtt_get_qos(buffer) > 0)
  183. {
  184. if (i + 2 >= blength)
  185. return NULL;
  186. i += 2;
  187. }
  188. if (totlen < i)
  189. return NULL;
  190. if (totlen <= blength)
  191. *length = totlen - i;
  192. else
  193. *length = blength - i;
  194. return (const char*)(buffer + i);
  195. }
  196. uint16_t ICACHE_FLASH_ATTR mqtt_get_id(uint8_t* buffer, uint16_t length)
  197. {
  198. if (length < 1)
  199. return 0;
  200. switch (mqtt_get_type(buffer))
  201. {
  202. case MQTT_MSG_TYPE_PUBLISH:
  203. {
  204. int i;
  205. int topiclen;
  206. for (i = 1; i < length; ++i)
  207. {
  208. if ((buffer[i] & 0x80) == 0)
  209. {
  210. ++i;
  211. break;
  212. }
  213. }
  214. if (i + 2 >= length)
  215. return 0;
  216. topiclen = buffer[i++] << 8;
  217. topiclen |= buffer[i++];
  218. if (i + topiclen >= length)
  219. return 0;
  220. i += topiclen;
  221. if (mqtt_get_qos(buffer) > 0)
  222. {
  223. if (i + 2 >= length)
  224. return 0;
  225. //i += 2;
  226. } else {
  227. return 0;
  228. }
  229. return (buffer[i] << 8) | buffer[i + 1];
  230. }
  231. case MQTT_MSG_TYPE_PUBACK:
  232. case MQTT_MSG_TYPE_PUBREC:
  233. case MQTT_MSG_TYPE_PUBREL:
  234. case MQTT_MSG_TYPE_PUBCOMP:
  235. case MQTT_MSG_TYPE_SUBACK:
  236. case MQTT_MSG_TYPE_UNSUBACK:
  237. case MQTT_MSG_TYPE_SUBSCRIBE:
  238. {
  239. // This requires the remaining length to be encoded in 1 byte,
  240. // which it should be.
  241. if (length >= 4 && (buffer[1] & 0x80) == 0)
  242. return (buffer[2] << 8) | buffer[3];
  243. else
  244. return 0;
  245. }
  246. default:
  247. return 0;
  248. }
  249. }
  250. mqtt_message_t* ICACHE_FLASH_ATTR mqtt_msg_connect(mqtt_connection_t* connection, mqtt_connect_info_t* info)
  251. {
  252. struct mqtt_connect_variable_header* variable_header;
  253. init_message(connection);
  254. if (connection->message.length + sizeof(*variable_header) > connection->buffer_length)
  255. return fail_message(connection);
  256. variable_header = (void*)(connection->buffer + connection->message.length);
  257. connection->message.length += sizeof(*variable_header);
  258. variable_header->lengthMsb = 0;
  259. #if defined(PROTOCOL_NAMEv31)
  260. variable_header->lengthLsb = 6;
  261. memcpy(variable_header->magic, "MQIsdp", 6);
  262. variable_header->version = 3;
  263. #elif defined(PROTOCOL_NAMEv311)
  264. variable_header->lengthLsb = 4;
  265. memcpy(variable_header->magic, "MQTT", 4);
  266. variable_header->version = 4;
  267. #else
  268. #error "Please define protocol name"
  269. #endif
  270. variable_header->flags = 0;
  271. variable_header->keepaliveMsb = info->keepalive >> 8;
  272. variable_header->keepaliveLsb = info->keepalive & 0xff;
  273. if (info->clean_session)
  274. variable_header->flags |= MQTT_CONNECT_FLAG_CLEAN_SESSION;
  275. if (info->client_id == NULL)
  276. {
  277. /* Never allowed */
  278. return fail_message(connection);
  279. }
  280. else if (info->client_id[0] == '\0')
  281. {
  282. #ifdef PROTOCOL_NAMEv311
  283. /* Allowed. Format 0 Length ID */
  284. append_string(connection, info->client_id, 2) ;
  285. #else
  286. /* 0 Length not allowed */
  287. return fail_message(connection);
  288. #endif
  289. }
  290. else
  291. {
  292. /* No 0 data and at least 1 long. Good to go. */
  293. if(append_string(connection, info->client_id, strlen(info->client_id)) < 0)
  294. return fail_message(connection);
  295. }
  296. if (info->will_topic != NULL && info->will_topic[0] != '\0')
  297. {
  298. if (append_string(connection, info->will_topic, strlen(info->will_topic)) < 0)
  299. return fail_message(connection);
  300. if (append_string(connection, info->will_message, strlen(info->will_message)) < 0)
  301. return fail_message(connection);
  302. variable_header->flags |= MQTT_CONNECT_FLAG_WILL;
  303. if (info->will_retain)
  304. variable_header->flags |= MQTT_CONNECT_FLAG_WILL_RETAIN;
  305. variable_header->flags |= (info->will_qos & 3) << 3;
  306. }
  307. if (info->username != NULL && info->username[0] != '\0')
  308. {
  309. if (append_string(connection, info->username, strlen(info->username)) < 0)
  310. return fail_message(connection);
  311. variable_header->flags |= MQTT_CONNECT_FLAG_USERNAME;
  312. }
  313. if (info->password != NULL && info->password[0] != '\0')
  314. {
  315. if (append_string(connection, info->password, strlen(info->password)) < 0)
  316. return fail_message(connection);
  317. variable_header->flags |= MQTT_CONNECT_FLAG_PASSWORD;
  318. }
  319. return fini_message(connection, MQTT_MSG_TYPE_CONNECT, 0, 0, 0);
  320. }
  321. mqtt_message_t* ICACHE_FLASH_ATTR mqtt_msg_publish(mqtt_connection_t* connection, const char* topic, const char* data, int data_length, int qos, int retain, uint16_t* message_id)
  322. {
  323. init_message(connection);
  324. if (topic == NULL || topic[0] == '\0')
  325. return fail_message(connection);
  326. if (append_string(connection, topic, strlen(topic)) < 0)
  327. return fail_message(connection);
  328. if (qos > 0)
  329. {
  330. if ((*message_id = append_message_id(connection, 0)) == 0)
  331. return fail_message(connection);
  332. }
  333. else
  334. *message_id = 0;
  335. if (connection->message.length + data_length > connection->buffer_length)
  336. return fail_message(connection);
  337. memcpy(connection->buffer + connection->message.length, data, data_length);
  338. connection->message.length += data_length;
  339. return fini_message(connection, MQTT_MSG_TYPE_PUBLISH, 0, qos, retain);
  340. }
  341. mqtt_message_t* ICACHE_FLASH_ATTR mqtt_msg_puback(mqtt_connection_t* connection, uint16_t message_id)
  342. {
  343. init_message(connection);
  344. if (append_message_id(connection, message_id) == 0)
  345. return fail_message(connection);
  346. return fini_message(connection, MQTT_MSG_TYPE_PUBACK, 0, 0, 0);
  347. }
  348. mqtt_message_t* ICACHE_FLASH_ATTR mqtt_msg_pubrec(mqtt_connection_t* connection, uint16_t message_id)
  349. {
  350. init_message(connection);
  351. if (append_message_id(connection, message_id) == 0)
  352. return fail_message(connection);
  353. return fini_message(connection, MQTT_MSG_TYPE_PUBREC, 0, 0, 0);
  354. }
  355. mqtt_message_t* ICACHE_FLASH_ATTR mqtt_msg_pubrel(mqtt_connection_t* connection, uint16_t message_id)
  356. {
  357. init_message(connection);
  358. if (append_message_id(connection, message_id) == 0)
  359. return fail_message(connection);
  360. return fini_message(connection, MQTT_MSG_TYPE_PUBREL, 0, 1, 0);
  361. }
  362. mqtt_message_t* ICACHE_FLASH_ATTR mqtt_msg_pubcomp(mqtt_connection_t* connection, uint16_t message_id)
  363. {
  364. init_message(connection);
  365. if (append_message_id(connection, message_id) == 0)
  366. return fail_message(connection);
  367. return fini_message(connection, MQTT_MSG_TYPE_PUBCOMP, 0, 0, 0);
  368. }
  369. mqtt_message_t* ICACHE_FLASH_ATTR mqtt_msg_subscribe(mqtt_connection_t* connection, const char* topic, int qos, uint16_t* message_id)
  370. {
  371. init_message(connection);
  372. if (topic == NULL || topic[0] == '\0')
  373. return fail_message(connection);
  374. if ((*message_id = append_message_id(connection, 0)) == 0)
  375. return fail_message(connection);
  376. if (append_string(connection, topic, strlen(topic)) < 0)
  377. return fail_message(connection);
  378. if (connection->message.length + 1 > connection->buffer_length)
  379. return fail_message(connection);
  380. connection->buffer[connection->message.length++] = qos;
  381. return fini_message(connection, MQTT_MSG_TYPE_SUBSCRIBE, 0, 1, 0);
  382. }
  383. mqtt_message_t* ICACHE_FLASH_ATTR mqtt_msg_unsubscribe(mqtt_connection_t* connection, const char* topic, uint16_t* message_id)
  384. {
  385. init_message(connection);
  386. if (topic == NULL || topic[0] == '\0')
  387. return fail_message(connection);
  388. if ((*message_id = append_message_id(connection, 0)) == 0)
  389. return fail_message(connection);
  390. if (append_string(connection, topic, strlen(topic)) < 0)
  391. return fail_message(connection);
  392. return fini_message(connection, MQTT_MSG_TYPE_UNSUBSCRIBE, 0, 1, 0);
  393. }
  394. mqtt_message_t* ICACHE_FLASH_ATTR mqtt_msg_pingreq(mqtt_connection_t* connection)
  395. {
  396. init_message(connection);
  397. return fini_message(connection, MQTT_MSG_TYPE_PINGREQ, 0, 0, 0);
  398. }
  399. mqtt_message_t* ICACHE_FLASH_ATTR mqtt_msg_pingresp(mqtt_connection_t* connection)
  400. {
  401. init_message(connection);
  402. return fini_message(connection, MQTT_MSG_TYPE_PINGRESP, 0, 0, 0);
  403. }
  404. mqtt_message_t* ICACHE_FLASH_ATTR mqtt_msg_disconnect(mqtt_connection_t* connection)
  405. {
  406. init_message(connection);
  407. return fini_message(connection, MQTT_MSG_TYPE_DISCONNECT, 0, 0, 0);
  408. }