MQTTDeserializePublish.c 3.5 KB

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