mqtt_msg.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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 <esp8266.h>
  32. #include "mqtt_msg.h"
  33. #define MQTT_MAX_FIXED_HEADER_SIZE 3
  34. enum mqtt_connect_flag {
  35. MQTT_CONNECT_FLAG_USERNAME = 1 << 7,
  36. MQTT_CONNECT_FLAG_PASSWORD = 1 << 6,
  37. MQTT_CONNECT_FLAG_WILL_RETAIN = 1 << 5,
  38. MQTT_CONNECT_FLAG_WILL = 1 << 2,
  39. MQTT_CONNECT_FLAG_CLEAN_SESSION = 1 << 1
  40. };
  41. struct
  42. __attribute__((__packed__)) mqtt_connect_variable_header {
  43. uint8_t lengthMsb;
  44. uint8_t lengthLsb;
  45. #if defined(PROTOCOL_NAMEv31)
  46. uint8_t magic[6];
  47. #elif defined(PROTOCOL_NAMEv311)
  48. uint8_t magic[4];
  49. #else
  50. #error "Please define protocol name"
  51. #endif
  52. uint8_t version;
  53. uint8_t flags;
  54. uint8_t keepaliveMsb;
  55. uint8_t keepaliveLsb;
  56. };
  57. static int ICACHE_FLASH_ATTR
  58. append_string(mqtt_connection_t* connection, const char* string, int len) {
  59. if (connection->message.length + len + 2 > connection->buffer_length)
  60. return -1;
  61. connection->buffer[connection->message.length++] = len >> 8;
  62. connection->buffer[connection->message.length++] = len & 0xff;
  63. memcpy(connection->buffer + connection->message.length, string, len);
  64. connection->message.length += len;
  65. return len + 2;
  66. }
  67. static uint16_t ICACHE_FLASH_ATTR
  68. append_message_id(mqtt_connection_t* connection, uint16_t message_id) {
  69. // If message_id is zero then we should assign one, otherwise
  70. // we'll use the one supplied by the caller
  71. while (message_id == 0)
  72. message_id = ++connection->message_id;
  73. if (connection->message.length + 2 > connection->buffer_length)
  74. return 0;
  75. connection->buffer[connection->message.length++] = message_id >> 8;
  76. connection->buffer[connection->message.length++] = message_id & 0xff;
  77. return message_id;
  78. }
  79. static int ICACHE_FLASH_ATTR
  80. init_message(mqtt_connection_t* connection) {
  81. connection->message.length = MQTT_MAX_FIXED_HEADER_SIZE;
  82. return MQTT_MAX_FIXED_HEADER_SIZE;
  83. }
  84. static mqtt_message_t* ICACHE_FLASH_ATTR
  85. fail_message(mqtt_connection_t* connection) {
  86. connection->message.data = connection->buffer;
  87. connection->message.length = 0;
  88. return &connection->message;
  89. }
  90. static mqtt_message_t* ICACHE_FLASH_ATTR
  91. fini_message(mqtt_connection_t* connection, int type, int dup, int qos, int retain) {
  92. int remaining_length = connection->message.length - MQTT_MAX_FIXED_HEADER_SIZE;
  93. if (remaining_length > 127) {
  94. connection->buffer[0] = ((type & 0x0f) << 4) | ((dup & 1) << 3) | ((qos & 3) << 1) | (retain & 1);
  95. connection->buffer[1] = 0x80 | (remaining_length % 128);
  96. connection->buffer[2] = remaining_length / 128;
  97. connection->message.length = remaining_length + 3;
  98. connection->message.data = connection->buffer;
  99. }
  100. else {
  101. connection->buffer[1] = ((type & 0x0f) << 4) | ((dup & 1) << 3) | ((qos & 3) << 1) | (retain & 1);
  102. connection->buffer[2] = remaining_length;
  103. connection->message.length = remaining_length + 2;
  104. connection->message.data = connection->buffer + 1;
  105. }
  106. return &connection->message;
  107. }
  108. void ICACHE_FLASH_ATTR
  109. mqtt_msg_init(mqtt_connection_t* connection, uint8_t* buffer, uint16_t buffer_length) {
  110. uint8_t len = sizeof(connection);
  111. memset(connection, '\0', len);
  112. connection->buffer = buffer;
  113. connection->buffer_length = buffer_length;
  114. }
  115. int ICACHE_FLASH_ATTR
  116. mqtt_get_total_length(const uint8_t* buffer, uint16_t length) {
  117. int i;
  118. int totlen = 0;
  119. for (i = 1; i < length; ++i) {
  120. totlen += (buffer[i] & 0x7f) << (7 * (i - 1));
  121. if ((buffer[i] & 0x80) == 0) {
  122. ++i;
  123. break;
  124. }
  125. }
  126. totlen += i;
  127. return totlen;
  128. }
  129. const char* ICACHE_FLASH_ATTR
  130. mqtt_get_publish_topic(const uint8_t* buffer, uint16_t* length) {
  131. int i;
  132. int totlen = 0;
  133. int topiclen;
  134. for (i = 1; i < *length; ++i) {
  135. totlen += (buffer[i] & 0x7f) << (7 * (i - 1));
  136. if ((buffer[i] & 0x80) == 0) {
  137. ++i;
  138. break;
  139. }
  140. }
  141. totlen += i;
  142. if (i + 2 >= *length)
  143. return NULL;
  144. topiclen = buffer[i++] << 8;
  145. topiclen |= buffer[i++];
  146. if (i + topiclen > *length)
  147. return NULL;
  148. *length = topiclen;
  149. return (const char*)(buffer + i);
  150. }
  151. const char* ICACHE_FLASH_ATTR
  152. mqtt_get_publish_data(const uint8_t* buffer, uint16_t* length) {
  153. int i;
  154. int totlen = 0;
  155. int topiclen;
  156. for (i = 1; i < *length; ++i) {
  157. totlen += (buffer[i] & 0x7f) << (7 * (i - 1));
  158. if ((buffer[i] & 0x80) == 0) {
  159. ++i;
  160. break;
  161. }
  162. }
  163. totlen += i;
  164. if (i + 2 >= *length)
  165. return NULL;
  166. topiclen = buffer[i++] << 8;
  167. topiclen |= buffer[i++];
  168. if (i + topiclen >= *length) {
  169. *length = 0;
  170. return NULL;
  171. }
  172. i += topiclen;
  173. if (mqtt_get_qos(buffer) > 0) {
  174. if (i + 2 >= *length)
  175. return NULL;
  176. i += 2;
  177. }
  178. if (totlen < i)
  179. return NULL;
  180. if (totlen <= *length)
  181. *length = totlen - i;
  182. else
  183. *length = *length - i;
  184. return (const char*)(buffer + i);
  185. }
  186. uint16_t ICACHE_FLASH_ATTR
  187. mqtt_get_id(const uint8_t* buffer, uint16_t length) {
  188. if (length < 1)
  189. return 0;
  190. switch (mqtt_get_type(buffer)) {
  191. case MQTT_MSG_TYPE_PUBLISH: {
  192. int i;
  193. int topiclen;
  194. for (i = 1; i < length; ++i) {
  195. if ((buffer[i] & 0x80) == 0) {
  196. ++i;
  197. break;
  198. }
  199. }
  200. if (i + 2 >= length)
  201. return 0;
  202. topiclen = buffer[i++] << 8;
  203. topiclen |= buffer[i++];
  204. if (i + topiclen >= length)
  205. return 0;
  206. i += topiclen;
  207. if (mqtt_get_qos(buffer) > 0) {
  208. if (i + 2 >= length)
  209. return 0;
  210. //i += 2;
  211. }
  212. else {
  213. return 0;
  214. }
  215. return (buffer[i] << 8) | buffer[i + 1];
  216. }
  217. case MQTT_MSG_TYPE_PUBACK:
  218. case MQTT_MSG_TYPE_PUBREC:
  219. case MQTT_MSG_TYPE_PUBREL:
  220. case MQTT_MSG_TYPE_PUBCOMP:
  221. case MQTT_MSG_TYPE_SUBACK:
  222. case MQTT_MSG_TYPE_UNSUBACK:
  223. case MQTT_MSG_TYPE_SUBSCRIBE: {
  224. // This requires the remaining length to be encoded in 1 byte,
  225. // which it should be.
  226. if (length >= 4 && (buffer[1] & 0x80) == 0)
  227. return (buffer[2] << 8) | buffer[3];
  228. else
  229. return 0;
  230. }
  231. default:
  232. return 0;
  233. }
  234. }
  235. mqtt_message_t* ICACHE_FLASH_ATTR
  236. mqtt_msg_connect(mqtt_connection_t* connection, mqtt_connect_info_t* info) {
  237. struct mqtt_connect_variable_header* variable_header;
  238. init_message(connection);
  239. if (connection->message.length + sizeof(*variable_header) > connection->buffer_length)
  240. return fail_message(connection);
  241. variable_header = (void*)(connection->buffer + connection->message.length);
  242. connection->message.length += sizeof(*variable_header);
  243. variable_header->lengthMsb = 0;
  244. #if defined(PROTOCOL_NAMEv31)
  245. variable_header->lengthLsb = 6;
  246. memcpy(variable_header->magic, "MQIsdp", 6);
  247. variable_header->version = 3;
  248. #elif defined(PROTOCOL_NAMEv311)
  249. variable_header->lengthLsb = 4;
  250. memcpy(variable_header->magic, "MQTT", 4);
  251. variable_header->version = 4;
  252. #else
  253. #error "Please define protocol name"
  254. #endif
  255. variable_header->flags = 0;
  256. variable_header->keepaliveMsb = info->keepalive >> 8;
  257. variable_header->keepaliveLsb = info->keepalive & 0xff;
  258. if (info->clean_session)
  259. variable_header->flags |= MQTT_CONNECT_FLAG_CLEAN_SESSION;
  260. if (info->client_id != NULL && info->client_id[0] != '\0') {
  261. if (append_string(connection, info->client_id, strlen(info->client_id)) < 0)
  262. return fail_message(connection);
  263. }
  264. else
  265. return fail_message(connection);
  266. if (info->will_topic != NULL && info->will_topic[0] != '\0') {
  267. if (append_string(connection, info->will_topic, strlen(info->will_topic)) < 0)
  268. return fail_message(connection);
  269. if (append_string(connection, info->will_message, strlen(info->will_message)) < 0)
  270. return fail_message(connection);
  271. variable_header->flags |= MQTT_CONNECT_FLAG_WILL;
  272. if (info->will_retain)
  273. variable_header->flags |= MQTT_CONNECT_FLAG_WILL_RETAIN;
  274. variable_header->flags |= (info->will_qos & 3) << 3;
  275. }
  276. if (info->username != NULL && info->username[0] != '\0') {
  277. if (append_string(connection, info->username, strlen(info->username)) < 0)
  278. return fail_message(connection);
  279. variable_header->flags |= MQTT_CONNECT_FLAG_USERNAME;
  280. }
  281. if (info->password != NULL && info->password[0] != '\0') {
  282. if (append_string(connection, info->password, strlen(info->password)) < 0)
  283. return fail_message(connection);
  284. variable_header->flags |= MQTT_CONNECT_FLAG_PASSWORD;
  285. }
  286. return fini_message(connection, MQTT_MSG_TYPE_CONNECT, 0, 0, 0);
  287. }
  288. mqtt_message_t* ICACHE_FLASH_ATTR
  289. mqtt_msg_publish(mqtt_connection_t* connection, const char* topic, const char* data, int data_length, int qos, int retain, uint16_t* message_id) {
  290. init_message(connection);
  291. if (topic == NULL || topic[0] == '\0')
  292. return fail_message(connection);
  293. if (append_string(connection, topic, strlen(topic)) < 0)
  294. return fail_message(connection);
  295. if (qos > 0) {
  296. if ((*message_id = append_message_id(connection, 0)) == 0)
  297. return fail_message(connection);
  298. }
  299. else
  300. *message_id = 0;
  301. if (connection->message.length + data_length > connection->buffer_length)
  302. return fail_message(connection);
  303. memcpy(connection->buffer + connection->message.length, data, data_length);
  304. connection->message.length += data_length;
  305. return fini_message(connection, MQTT_MSG_TYPE_PUBLISH, 0, qos, retain);
  306. }
  307. mqtt_message_t* ICACHE_FLASH_ATTR
  308. mqtt_msg_puback(mqtt_connection_t* connection, uint16_t message_id) {
  309. init_message(connection);
  310. if (append_message_id(connection, message_id) == 0)
  311. return fail_message(connection);
  312. return fini_message(connection, MQTT_MSG_TYPE_PUBACK, 0, 0, 0);
  313. }
  314. mqtt_message_t* ICACHE_FLASH_ATTR
  315. mqtt_msg_pubrec(mqtt_connection_t* connection, uint16_t message_id) {
  316. init_message(connection);
  317. if (append_message_id(connection, message_id) == 0)
  318. return fail_message(connection);
  319. return fini_message(connection, MQTT_MSG_TYPE_PUBREC, 0, 0, 0);
  320. }
  321. mqtt_message_t* ICACHE_FLASH_ATTR
  322. mqtt_msg_pubrel(mqtt_connection_t* connection, uint16_t message_id) {
  323. init_message(connection);
  324. if (append_message_id(connection, message_id) == 0)
  325. return fail_message(connection);
  326. return fini_message(connection, MQTT_MSG_TYPE_PUBREL, 0, 1, 0);
  327. }
  328. mqtt_message_t* ICACHE_FLASH_ATTR
  329. mqtt_msg_pubcomp(mqtt_connection_t* connection, uint16_t message_id) {
  330. init_message(connection);
  331. if (append_message_id(connection, message_id) == 0)
  332. return fail_message(connection);
  333. return fini_message(connection, MQTT_MSG_TYPE_PUBCOMP, 0, 0, 0);
  334. }
  335. mqtt_message_t* ICACHE_FLASH_ATTR
  336. mqtt_msg_subscribe(mqtt_connection_t* connection, const char* topic, int qos, uint16_t* message_id) {
  337. init_message(connection);
  338. if (topic == NULL || topic[0] == '\0')
  339. return fail_message(connection);
  340. if ((*message_id = append_message_id(connection, 0)) == 0)
  341. return fail_message(connection);
  342. if (append_string(connection, topic, strlen(topic)) < 0)
  343. return fail_message(connection);
  344. if (connection->message.length + 1 > connection->buffer_length)
  345. return fail_message(connection);
  346. connection->buffer[connection->message.length++] = qos;
  347. return fini_message(connection, MQTT_MSG_TYPE_SUBSCRIBE, 0, 1, 0);
  348. }
  349. mqtt_message_t* ICACHE_FLASH_ATTR
  350. mqtt_msg_unsubscribe(mqtt_connection_t* connection, const char* topic, uint16_t* message_id) {
  351. init_message(connection);
  352. if (topic == NULL || topic[0] == '\0')
  353. return fail_message(connection);
  354. if ((*message_id = append_message_id(connection, 0)) == 0)
  355. return fail_message(connection);
  356. if (append_string(connection, topic, strlen(topic)) < 0)
  357. return fail_message(connection);
  358. return fini_message(connection, MQTT_MSG_TYPE_UNSUBSCRIBE, 0, 1, 0);
  359. }
  360. mqtt_message_t* ICACHE_FLASH_ATTR
  361. mqtt_msg_pingreq(mqtt_connection_t* connection) {
  362. init_message(connection);
  363. return fini_message(connection, MQTT_MSG_TYPE_PINGREQ, 0, 0, 0);
  364. }
  365. mqtt_message_t* ICACHE_FLASH_ATTR
  366. mqtt_msg_pingresp(mqtt_connection_t* connection) {
  367. init_message(connection);
  368. return fini_message(connection, MQTT_MSG_TYPE_PINGRESP, 0, 0, 0);
  369. }
  370. mqtt_message_t* ICACHE_FLASH_ATTR
  371. mqtt_msg_disconnect(mqtt_connection_t* connection) {
  372. init_message(connection);
  373. return fini_message(connection, MQTT_MSG_TYPE_DISCONNECT, 0, 0, 0);
  374. }