MQTTProperties.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*******************************************************************************
  2. * Copyright (c) 2017, 2019 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. #if !defined(MQTTPROPERTIES_H)
  17. #define MQTTPROPERTIES_H
  18. #define MQTT_INVALID_PROPERTY_ID -2
  19. /** The one byte MQTT V5 property indicator */
  20. enum MQTTPropertyCodes {
  21. MQTTPROPERTY_CODE_PAYLOAD_FORMAT_INDICATOR = 1, /**< The value is 1 */
  22. MQTTPROPERTY_CODE_MESSAGE_EXPIRY_INTERVAL = 2, /**< The value is 2 */
  23. MQTTPROPERTY_CODE_CONTENT_TYPE = 3, /**< The value is 3 */
  24. MQTTPROPERTY_CODE_RESPONSE_TOPIC = 8, /**< The value is 8 */
  25. MQTTPROPERTY_CODE_CORRELATION_DATA = 9, /**< The value is 9 */
  26. MQTTPROPERTY_CODE_SUBSCRIPTION_IDENTIFIER = 11, /**< The value is 11 */
  27. MQTTPROPERTY_CODE_SESSION_EXPIRY_INTERVAL = 17, /**< The value is 17 */
  28. MQTTPROPERTY_CODE_ASSIGNED_CLIENT_IDENTIFER = 18,/**< The value is 18 */
  29. MQTTPROPERTY_CODE_SERVER_KEEP_ALIVE = 19, /**< The value is 19 */
  30. MQTTPROPERTY_CODE_AUTHENTICATION_METHOD = 21, /**< The value is 21 */
  31. MQTTPROPERTY_CODE_AUTHENTICATION_DATA = 22, /**< The value is 22 */
  32. MQTTPROPERTY_CODE_REQUEST_PROBLEM_INFORMATION = 23,/**< The value is 23 */
  33. MQTTPROPERTY_CODE_WILL_DELAY_INTERVAL = 24, /**< The value is 24 */
  34. MQTTPROPERTY_CODE_REQUEST_RESPONSE_INFORMATION = 25,/**< The value is 25 */
  35. MQTTPROPERTY_CODE_RESPONSE_INFORMATION = 26, /**< The value is 26 */
  36. MQTTPROPERTY_CODE_SERVER_REFERENCE = 28, /**< The value is 28 */
  37. MQTTPROPERTY_CODE_REASON_STRING = 31, /**< The value is 31 */
  38. MQTTPROPERTY_CODE_RECEIVE_MAXIMUM = 33, /**< The value is 33*/
  39. MQTTPROPERTY_CODE_TOPIC_ALIAS_MAXIMUM = 34, /**< The value is 34 */
  40. MQTTPROPERTY_CODE_TOPIC_ALIAS = 35, /**< The value is 35 */
  41. MQTTPROPERTY_CODE_MAXIMUM_QOS = 36, /**< The value is 36 */
  42. MQTTPROPERTY_CODE_RETAIN_AVAILABLE = 37, /**< The value is 37 */
  43. MQTTPROPERTY_CODE_USER_PROPERTY = 38, /**< The value is 38 */
  44. MQTTPROPERTY_CODE_MAXIMUM_PACKET_SIZE = 39, /**< The value is 39 */
  45. MQTTPROPERTY_CODE_WILDCARD_SUBSCRIPTION_AVAILABLE = 40,/**< The value is 40 */
  46. MQTTPROPERTY_CODE_SUBSCRIPTION_IDENTIFIERS_AVAILABLE = 41,/**< The value is 41 */
  47. MQTTPROPERTY_CODE_SHARED_SUBSCRIPTION_AVAILABLE = 42/**< The value is 241 */
  48. };
  49. #if defined(WIN32) || defined(WIN64)
  50. #define DLLImport __declspec(dllimport)
  51. #define DLLExport __declspec(dllexport)
  52. #else
  53. #define DLLImport extern
  54. #define DLLExport __attribute__ ((visibility ("default")))
  55. #endif
  56. /**
  57. * Returns a printable string description of an MQTT V5 property code.
  58. * @param value an MQTT V5 property code.
  59. * @return the printable string description of the input property code.
  60. * NULL if the code was not found.
  61. */
  62. DLLExport const char* MQTTPropertyName(enum MQTTPropertyCodes value);
  63. /** The one byte MQTT V5 property type */
  64. enum MQTTPropertyTypes {
  65. MQTTPROPERTY_TYPE_BYTE,
  66. MQTTPROPERTY_TYPE_TWO_BYTE_INTEGER,
  67. MQTTPROPERTY_TYPE_FOUR_BYTE_INTEGER,
  68. MQTTPROPERTY_TYPE_VARIABLE_BYTE_INTEGER,
  69. MQTTPROPERTY_TYPE_BINARY_DATA,
  70. MQTTPROPERTY_TYPE_UTF_8_ENCODED_STRING,
  71. MQTTPROPERTY_TYPE_UTF_8_STRING_PAIR
  72. };
  73. /**
  74. * Returns the MQTT V5 type code of an MQTT V5 property.
  75. * @param value an MQTT V5 property code.
  76. * @return the MQTT V5 type code of the input property. -1 if the code was not found.
  77. */
  78. DLLExport int MQTTProperty_getType(enum MQTTPropertyCodes value);
  79. /**
  80. * The data for a length delimited string
  81. */
  82. typedef struct
  83. {
  84. int len; /**< the length of the string */
  85. char* data; /**< pointer to the string data */
  86. } MQTTLenString;
  87. /**
  88. * Structure to hold an MQTT version 5 property of any type
  89. */
  90. typedef struct
  91. {
  92. enum MQTTPropertyCodes identifier; /**< The MQTT V5 property id. A multi-byte integer. */
  93. /** The value of the property, as a union of the different possible types. */
  94. union {
  95. unsigned char byte; /**< holds the value of a byte property type */
  96. unsigned short integer2; /**< holds the value of a 2 byte integer property type */
  97. unsigned int integer4; /**< holds the value of a 4 byte integer property type */
  98. struct {
  99. MQTTLenString data; /**< The value of a string property, or the name of a user property. */
  100. MQTTLenString value; /**< The value of a user property. */
  101. };
  102. } value;
  103. } MQTTProperty;
  104. /**
  105. * MQTT version 5 property list
  106. */
  107. typedef struct MQTTProperties
  108. {
  109. int count; /**< number of property entries in the array */
  110. int max_count; /**< max number of properties that the currently allocated array can store */
  111. int length; /**< mbi: byte length of all properties */
  112. MQTTProperty *array; /**< array of properties */
  113. } MQTTProperties;
  114. #define MQTTProperties_initializer {0, 0, 0, NULL}
  115. /**
  116. * Returns the length of the properties structure when serialized ready for network transmission.
  117. * @param props an MQTT V5 property structure.
  118. * @return the length in bytes of the properties when serialized.
  119. */
  120. int MQTTProperties_len(MQTTProperties* props);
  121. /**
  122. * Add a property pointer to the property array. There is no memory allocation.
  123. * @param props The property list to add the property to.
  124. * @param prop The property to add to the list.
  125. * @return 0 on success, -1 on failure.
  126. */
  127. DLLExport int MQTTProperties_add(MQTTProperties* props, const MQTTProperty* prop);
  128. /**
  129. * Serialize the given property list to a character buffer, e.g. for writing to the network.
  130. * @param pptr pointer to the buffer - move the pointer as we add data
  131. * @param properties pointer to the property list, can be NULL
  132. * @return whether the write succeeded or not: number of bytes written, or < 0 on failure.
  133. */
  134. int MQTTProperties_write(char** pptr, const MQTTProperties* properties);
  135. /**
  136. * Reads a property list from a character buffer into an array.
  137. * @param properties pointer to the property list to be filled. Should be initalized but empty.
  138. * @param pptr pointer to the character buffer.
  139. * @param enddata pointer to the end of the character buffer so we don't read beyond.
  140. * @return 1 if the properties were read successfully.
  141. */
  142. int MQTTProperties_read(MQTTProperties* properties, char** pptr, char* enddata);
  143. /**
  144. * Free all memory allocated to the property list, including any to individual properties.
  145. * @param properties pointer to the property list.
  146. */
  147. DLLExport void MQTTProperties_free(MQTTProperties* properties);
  148. /**
  149. * Copy the contents of a property list, allocating additional memory if needed.
  150. * @param props pointer to the property list.
  151. * @return the duplicated property list.
  152. */
  153. DLLExport MQTTProperties MQTTProperties_copy(const MQTTProperties* props);
  154. /**
  155. * Checks if property list contains a specific property.
  156. * @param props pointer to the property list.
  157. * @param propid the property id to check for.
  158. * @return 1 if found, 0 if not.
  159. */
  160. DLLExport int MQTTProperties_hasProperty(MQTTProperties *props, enum MQTTPropertyCodes propid);
  161. /**
  162. * Returns the number of instances of a property id. Most properties can exist only once.
  163. * User properties and subscription ids can exist more than once.
  164. * @param props pointer to the property list.
  165. * @param propid the property id to check for.
  166. * @return the number of times found. Can be 0.
  167. */
  168. DLLExport int MQTTProperties_propertyCount(MQTTProperties *props, enum MQTTPropertyCodes propid);
  169. /**
  170. * Returns the integer value of a specific property. The property given must be a numeric type.
  171. * @param props pointer to the property list.
  172. * @param propid the property id to check for.
  173. * @return the integer value of the property. -9999999 on failure.
  174. */
  175. DLLExport int MQTTProperties_getNumericValue(MQTTProperties *props, enum MQTTPropertyCodes propid);
  176. /**
  177. * Returns the integer value of a specific property when it's not the only instance.
  178. * The property given must be a numeric type.
  179. * @param props pointer to the property list.
  180. * @param propid the property id to check for.
  181. * @param index the instance number, starting at 0.
  182. * @return the integer value of the property. -9999999 on failure.
  183. */
  184. DLLExport int MQTTProperties_getNumericValueAt(MQTTProperties *props, enum MQTTPropertyCodes propid, int index);
  185. /**
  186. * Returns a pointer to the property structure for a specific property.
  187. * @param props pointer to the property list.
  188. * @param propid the property id to check for.
  189. * @return the pointer to the property structure if found. NULL if not found.
  190. */
  191. DLLExport MQTTProperty* MQTTProperties_getProperty(MQTTProperties *props, enum MQTTPropertyCodes propid);
  192. /**
  193. * Returns a pointer to the property structure for a specific property when it's not the only instance.
  194. * @param props pointer to the property list.
  195. * @param propid the property id to check for.
  196. * @param index the instance number, starting at 0.
  197. * @return the pointer to the property structure if found. NULL if not found.
  198. */
  199. DLLExport MQTTProperty* MQTTProperties_getPropertyAt(MQTTProperties *props, enum MQTTPropertyCodes propid, int index);
  200. #endif /* MQTTPROPERTIES_H */