mqtt_msg.c 13 KB

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