MQTTFormat.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*******************************************************************************
  2. * Copyright (c) 2014 IBM Corp.
  3. *
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v1.0
  6. * and Eclipse Distribution License v1.0 which accompany this distribution.
  7. *
  8. * The Eclipse Public License is available at
  9. * http://www.eclipse.org/legal/epl-v10.html
  10. * and the Eclipse Distribution License is available at
  11. * http://www.eclipse.org/org/documents/edl-v10.php.
  12. *
  13. * Contributors:
  14. * Ian Craggs - initial API and implementation and/or initial documentation
  15. *******************************************************************************/
  16. #include "StackTrace.h"
  17. #include "MQTTPacket.h"
  18. #include <string.h>
  19. const char* MQTTPacket_names[] =
  20. {
  21. "RESERVED", "CONNECT", "CONNACK", "PUBLISH", "PUBACK", "PUBREC", "PUBREL",
  22. "PUBCOMP", "SUBSCRIBE", "SUBACK", "UNSUBSCRIBE", "UNSUBACK",
  23. "PINGREQ", "PINGRESP", "DISCONNECT"
  24. };
  25. const char* MQTTPacket_getName(unsigned short packetid)
  26. {
  27. return MQTTPacket_names[packetid];
  28. }
  29. int MQTTStringFormat_connect(char* strbuf, int strbuflen, MQTTPacket_connectData* data)
  30. {
  31. int strindex = 0;
  32. strindex = snprintf(strbuf, strbuflen,
  33. "CONNECT MQTT version %d, client id %.*s, clean session %d, keep alive %d",
  34. (int)data->MQTTVersion, data->clientID.lenstring.len, data->clientID.lenstring.data,
  35. (int)data->cleansession, data->keepAliveInterval);
  36. if (data->willFlag)
  37. strindex += snprintf(&strbuf[strindex], strbuflen - strindex,
  38. ", will QoS %d, will retain %d, will topic %.*s, will message %.*s",
  39. data->will.qos, data->will.retained,
  40. data->will.topicName.lenstring.len, data->will.topicName.lenstring.data,
  41. data->will.message.lenstring.len, data->will.message.lenstring.data);
  42. if (data->username.lenstring.data && data->username.lenstring.len > 0)
  43. strindex += snprintf(&strbuf[strindex], strbuflen - strindex,
  44. ", user name %.*s", data->username.lenstring.len, data->username.lenstring.data);
  45. if (data->password.lenstring.data && data->password.lenstring.len > 0)
  46. strindex += snprintf(&strbuf[strindex], strbuflen - strindex,
  47. ", password %.*s", data->password.lenstring.len, data->password.lenstring.data);
  48. return strindex;
  49. }
  50. int MQTTStringFormat_connack(char* strbuf, int strbuflen, unsigned char connack_rc, unsigned char sessionPresent)
  51. {
  52. int strindex = snprintf(strbuf, strbuflen, "CONNACK session present %d, rc %d", sessionPresent, connack_rc);
  53. return strindex;
  54. }
  55. int MQTTStringFormat_publish(char* strbuf, int strbuflen, unsigned char dup, int qos, unsigned char retained,
  56. unsigned short packetid, MQTTString topicName, unsigned char* payload, int payloadlen)
  57. {
  58. int strindex = snprintf(strbuf, strbuflen,
  59. "PUBLISH dup %d, QoS %d, retained %d, packet id %d, topic %.*s, payload length %d, payload %.*s",
  60. dup, qos, retained, packetid,
  61. (topicName.lenstring.len < 20) ? topicName.lenstring.len : 20, topicName.lenstring.data,
  62. payloadlen, (payloadlen < 20) ? payloadlen : 20, payload);
  63. return strindex;
  64. }
  65. int MQTTStringFormat_ack(char* strbuf, int strbuflen, unsigned char packettype, unsigned char dup, unsigned short packetid)
  66. {
  67. int strindex = snprintf(strbuf, strbuflen, "%s, packet id %d", MQTTPacket_names[packettype], packetid);
  68. if (dup)
  69. strindex += snprintf(strbuf + strindex, strbuflen - strindex, ", dup %d", dup);
  70. return strindex;
  71. }
  72. int MQTTStringFormat_subscribe(char* strbuf, int strbuflen, unsigned char dup, unsigned short packetid, int count,
  73. MQTTString topicFilters[], int requestedQoSs[])
  74. {
  75. return snprintf(strbuf, strbuflen,
  76. "SUBSCRIBE dup %d, packet id %d count %d topic %.*s qos %d",
  77. dup, packetid, count,
  78. topicFilters[0].lenstring.len, topicFilters[0].lenstring.data,
  79. requestedQoSs[0]);
  80. }
  81. int MQTTStringFormat_suback(char* strbuf, int strbuflen, unsigned short packetid, int count, int* grantedQoSs)
  82. {
  83. return snprintf(strbuf, strbuflen,
  84. "SUBACK packet id %d count %d granted qos %d", packetid, count, grantedQoSs[0]);
  85. }
  86. int MQTTStringFormat_unsubscribe(char* strbuf, int strbuflen, unsigned char dup, unsigned short packetid,
  87. int count, MQTTString topicFilters[])
  88. {
  89. return snprintf(strbuf, strbuflen,
  90. "UNSUBSCRIBE dup %d, packet id %d count %d topic %.*s",
  91. dup, packetid, count,
  92. topicFilters[0].lenstring.len, topicFilters[0].lenstring.data);
  93. }
  94. #if defined(MQTT_CLIENT)
  95. char* MQTTFormat_toClientString(char* strbuf, int strbuflen, unsigned char* buf, int buflen)
  96. {
  97. int index = 0;
  98. int rem_length = 0;
  99. MQTTHeader header = {0};
  100. int strindex = 0;
  101. header.byte = buf[index++];
  102. index += MQTTPacket_decodeBuf(&buf[index], &rem_length);
  103. switch (header.bits.type)
  104. {
  105. case CONNACK:
  106. {
  107. unsigned char sessionPresent, connack_rc;
  108. if (MQTTDeserialize_connack(&sessionPresent, &connack_rc, buf, buflen) == 1)
  109. strindex = MQTTStringFormat_connack(strbuf, strbuflen, connack_rc, sessionPresent);
  110. }
  111. break;
  112. case PUBLISH:
  113. {
  114. unsigned char dup, retained, *payload;
  115. unsigned short packetid;
  116. int qos, payloadlen;
  117. MQTTString topicName = MQTTString_initializer;
  118. if (MQTTDeserialize_publish(&dup, &qos, &retained, &packetid, &topicName,
  119. &payload, &payloadlen, buf, buflen) == 1)
  120. strindex = MQTTStringFormat_publish(strbuf, strbuflen, dup, qos, retained, packetid,
  121. topicName, payload, payloadlen);
  122. }
  123. break;
  124. case PUBACK:
  125. case PUBREC:
  126. case PUBREL:
  127. case PUBCOMP:
  128. {
  129. unsigned char packettype, dup;
  130. unsigned short packetid;
  131. if (MQTTDeserialize_ack(&packettype, &dup, &packetid, buf, buflen) == 1)
  132. strindex = MQTTStringFormat_ack(strbuf, strbuflen, packettype, dup, packetid);
  133. }
  134. break;
  135. case SUBACK:
  136. {
  137. unsigned short packetid;
  138. int maxcount = 1, count = 0;
  139. int grantedQoSs[1];
  140. if (MQTTDeserialize_suback(&packetid, maxcount, &count, grantedQoSs, buf, buflen) == 1)
  141. strindex = MQTTStringFormat_suback(strbuf, strbuflen, packetid, count, grantedQoSs);
  142. }
  143. break;
  144. case UNSUBACK:
  145. {
  146. unsigned short packetid;
  147. if (MQTTDeserialize_unsuback(&packetid, buf, buflen) == 1)
  148. strindex = MQTTStringFormat_ack(strbuf, strbuflen, UNSUBACK, 0, packetid);
  149. }
  150. break;
  151. case PINGREQ:
  152. case PINGRESP:
  153. case DISCONNECT:
  154. strindex = snprintf(strbuf, strbuflen, "%s", MQTTPacket_names[header.bits.type]);
  155. break;
  156. }
  157. return strbuf;
  158. }
  159. #endif
  160. #if defined(MQTT_SERVER)
  161. char* MQTTFormat_toServerString(char* strbuf, int strbuflen, unsigned char* buf, int buflen)
  162. {
  163. int index = 0;
  164. int rem_length = 0;
  165. MQTTHeader header = {0};
  166. int strindex = 0;
  167. header.byte = buf[index++];
  168. index += MQTTPacket_decodeBuf(&buf[index], &rem_length);
  169. switch (header.bits.type)
  170. {
  171. case CONNECT:
  172. {
  173. MQTTPacket_connectData data;
  174. int rc;
  175. if ((rc = MQTTDeserialize_connect(&data, buf, buflen)) == 1)
  176. strindex = MQTTStringFormat_connect(strbuf, strbuflen, &data);
  177. }
  178. break;
  179. case PUBLISH:
  180. {
  181. unsigned char dup, retained, *payload;
  182. unsigned short packetid;
  183. int qos, payloadlen;
  184. MQTTString topicName = MQTTString_initializer;
  185. if (MQTTDeserialize_publish(&dup, &qos, &retained, &packetid, &topicName,
  186. &payload, &payloadlen, buf, buflen) == 1)
  187. strindex = MQTTStringFormat_publish(strbuf, strbuflen, dup, qos, retained, packetid,
  188. topicName, payload, payloadlen);
  189. }
  190. break;
  191. case PUBACK:
  192. case PUBREC:
  193. case PUBREL:
  194. case PUBCOMP:
  195. {
  196. unsigned char packettype, dup;
  197. unsigned short packetid;
  198. if (MQTTDeserialize_ack(&packettype, &dup, &packetid, buf, buflen) == 1)
  199. strindex = MQTTStringFormat_ack(strbuf, strbuflen, packettype, dup, packetid);
  200. }
  201. break;
  202. case SUBSCRIBE:
  203. {
  204. unsigned char dup;
  205. unsigned short packetid;
  206. int maxcount = 1, count = 0;
  207. MQTTString topicFilters[1];
  208. int requestedQoSs[1];
  209. if (MQTTDeserialize_subscribe(&dup, &packetid, maxcount, &count,
  210. topicFilters, requestedQoSs, buf, buflen) == 1)
  211. strindex = MQTTStringFormat_subscribe(strbuf, strbuflen, dup, packetid, count, topicFilters, requestedQoSs);;
  212. }
  213. break;
  214. case UNSUBSCRIBE:
  215. {
  216. unsigned char dup;
  217. unsigned short packetid;
  218. int maxcount = 1, count = 0;
  219. MQTTString topicFilters[1];
  220. if (MQTTDeserialize_unsubscribe(&dup, &packetid, maxcount, &count, topicFilters, buf, buflen) == 1)
  221. strindex = MQTTStringFormat_unsubscribe(strbuf, strbuflen, dup, packetid, count, topicFilters);
  222. }
  223. break;
  224. case PINGREQ:
  225. case PINGRESP:
  226. case DISCONNECT:
  227. strindex = snprintf(strbuf, strbuflen, "%s", MQTTPacket_names[header.bits.type]);
  228. break;
  229. }
  230. strbuf[strbuflen] = '\0';
  231. return strbuf;
  232. }
  233. #endif