MQTTDeserializePublish.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #define min(a, b) ((a < b) ? 1 : 0)
  20. /**
  21. * Deserializes the supplied (wire) buffer into publish data
  22. * @param dup returned integer - the MQTT dup flag
  23. * @param qos returned integer - the MQTT QoS value
  24. * @param retained returned integer - the MQTT retained flag
  25. * @param packetid returned integer - the MQTT packet identifier
  26. * @param topicName returned MQTTString - the MQTT topic in the publish
  27. * @param payload returned byte buffer - the MQTT publish payload
  28. * @param payloadlen returned integer - the length of the MQTT payload
  29. * @param buf the raw buffer data, of the correct length determined by the remaining length field
  30. * @param buflen the length in bytes of the data in the supplied buffer
  31. * @return error code. 1 is success
  32. */
  33. int MQTTDeserialize_publish(unsigned char* dup, int* qos, unsigned char* retained, unsigned short* packetid, MQTTString* topicName,
  34. unsigned char** payload, int* payloadlen, unsigned char* buf, int buflen)
  35. {
  36. MQTTHeader header = {0};
  37. unsigned char* curdata = buf;
  38. unsigned char* enddata = NULL;
  39. int rc = 0;
  40. int mylen = 0;
  41. FUNC_ENTRY;
  42. header.byte = readChar(&curdata);
  43. if (header.bits.type != PUBLISH)
  44. goto exit;
  45. *dup = header.bits.dup;
  46. *qos = header.bits.qos;
  47. *retained = header.bits.retain;
  48. curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
  49. enddata = curdata + mylen;
  50. if (!readMQTTLenString(topicName, &curdata, enddata) ||
  51. enddata - curdata < 0) /* do we have enough data to read the protocol version byte? */
  52. goto exit;
  53. if (*qos > 0)
  54. *packetid = readInt(&curdata);
  55. *payloadlen = enddata - curdata;
  56. *payload = curdata;
  57. rc = 1;
  58. exit:
  59. FUNC_EXIT_RC(rc);
  60. return rc;
  61. }
  62. /**
  63. * Deserializes the supplied (wire) buffer into an ack
  64. * @param packettype returned integer - the MQTT packet type
  65. * @param dup returned integer - the MQTT dup flag
  66. * @param packetid returned integer - the MQTT packet identifier
  67. * @param buf the raw buffer data, of the correct length determined by the remaining length field
  68. * @param buflen the length in bytes of the data in the supplied buffer
  69. * @return error code. 1 is success, 0 is failure
  70. */
  71. int MQTTDeserialize_ack(unsigned char* packettype, unsigned char* dup, unsigned short* packetid, unsigned char* buf, int buflen)
  72. {
  73. MQTTHeader header = {0};
  74. unsigned char* curdata = buf;
  75. unsigned char* enddata = NULL;
  76. int rc = 0;
  77. int mylen;
  78. FUNC_ENTRY;
  79. header.byte = readChar(&curdata);
  80. *dup = header.bits.dup;
  81. *packettype = header.bits.type;
  82. curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
  83. enddata = curdata + mylen;
  84. if (enddata - curdata < 2)
  85. goto exit;
  86. *packetid = readInt(&curdata);
  87. rc = 1;
  88. exit:
  89. FUNC_EXIT_RC(rc);
  90. return rc;
  91. }