types.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /////////////////////////////////////////////////////////////////////////////
  2. /// @file types.h
  3. /// Basic types and type conversions for the Paho MQTT C++ library.
  4. /// @date May 17, 2015 @author Frank Pagliughi
  5. /////////////////////////////////////////////////////////////////////////////
  6. /*******************************************************************************
  7. * Copyright (c) 2015-2017 Frank Pagliughi <fpagliughi@mindspring.com>
  8. *
  9. * All rights reserved. This program and the accompanying materials
  10. * are made available under the terms of the Eclipse Public License v2.0
  11. * and Eclipse Distribution License v1.0 which accompany this distribution.
  12. *
  13. * The Eclipse Public License is available at
  14. * http://www.eclipse.org/legal/epl-v20.html
  15. * and the Eclipse Distribution License is available at
  16. * http://www.eclipse.org/org/documents/edl-v10.php.
  17. *
  18. * Contributors:
  19. * Frank Pagliughi - initial implementation and documentation
  20. *******************************************************************************/
  21. #ifndef __mqtt_types_h
  22. #define __mqtt_types_h
  23. #include <string>
  24. #include <vector>
  25. #include <memory>
  26. #include <chrono>
  27. namespace mqtt {
  28. /////////////////////////////////////////////////////////////////////////////
  29. // Basic data types
  30. /** A 'byte' is an 8-bit, unsigned int */
  31. using byte = uint8_t;
  32. /** An mqtt string is just a std::string */
  33. using string = std::string;
  34. /** A binary blob of data is, umm, just a string too! */
  35. using binary = std::string;
  36. /** Smart/shared pointer to a const string */
  37. using string_ptr = std::shared_ptr<const string>;
  38. /** Smart/shared pointer to a const binary blob */
  39. using binary_ptr = std::shared_ptr<const binary>;
  40. /////////////////////////////////////////////////////////////////////////////
  41. // General protocol enumerations
  42. /**
  43. * The MQTT v5 Reason Codes.
  44. *
  45. * These map to the Paho C MQTTReasonCodes
  46. */
  47. enum ReasonCode {
  48. SUCCESS = 0,
  49. NORMAL_DISCONNECTION = 0,
  50. GRANTED_QOS_0 = 0,
  51. GRANTED_QOS_1 = 1,
  52. GRANTED_QOS_2 = 2,
  53. DISCONNECT_WITH_WILL_MESSAGE = 4,
  54. NO_MATCHING_SUBSCRIBERS = 16,
  55. NO_SUBSCRIPTION_FOUND = 17,
  56. CONTINUE_AUTHENTICATION = 24,
  57. RE_AUTHENTICATE = 25,
  58. UNSPECIFIED_ERROR = 128,
  59. MALFORMED_PACKET = 129,
  60. PROTOCOL_ERROR = 130,
  61. IMPLEMENTATION_SPECIFIC_ERROR = 131,
  62. UNSUPPORTED_PROTOCOL_VERSION = 132,
  63. CLIENT_IDENTIFIER_NOT_VALID = 133,
  64. BAD_USER_NAME_OR_PASSWORD = 134,
  65. NOT_AUTHORIZED = 135,
  66. SERVER_UNAVAILABLE = 136,
  67. SERVER_BUSY = 137,
  68. BANNED = 138,
  69. SERVER_SHUTTING_DOWN = 139,
  70. BAD_AUTHENTICATION_METHOD = 140,
  71. KEEP_ALIVE_TIMEOUT = 141,
  72. SESSION_TAKEN_OVER = 142,
  73. TOPIC_FILTER_INVALID = 143,
  74. TOPIC_NAME_INVALID = 144,
  75. PACKET_IDENTIFIER_IN_USE = 145,
  76. PACKET_IDENTIFIER_NOT_FOUND = 146,
  77. RECEIVE_MAXIMUM_EXCEEDED = 147,
  78. TOPIC_ALIAS_INVALID = 148,
  79. PACKET_TOO_LARGE = 149,
  80. MESSAGE_RATE_TOO_HIGH = 150,
  81. QUOTA_EXCEEDED = 151,
  82. ADMINISTRATIVE_ACTION = 152,
  83. PAYLOAD_FORMAT_INVALID = 153,
  84. RETAIN_NOT_SUPPORTED = 154,
  85. QOS_NOT_SUPPORTED = 155,
  86. USE_ANOTHER_SERVER = 156,
  87. SERVER_MOVED = 157,
  88. SHARED_SUBSCRIPTIONS_NOT_SUPPORTED = 158,
  89. CONNECTION_RATE_EXCEEDED = 159,
  90. MAXIMUM_CONNECT_TIME = 160,
  91. SUBSCRIPTION_IDENTIFIERS_NOT_SUPPORTED = 161,
  92. WILDCARD_SUBSCRIPTIONS_NOT_SUPPORTED = 162,
  93. MQTTPP_V3_CODE = 255 // This is not a protocol code; used internally by the library
  94. };
  95. /////////////////////////////////////////////////////////////////////////////
  96. // Time functions
  97. /**
  98. * Convert a chrono duration to seconds.
  99. * This casts away precision to get integer seconds.
  100. * @param dur A chrono duration type
  101. * @return The duration as a chrono seconds value
  102. */
  103. template <class Rep, class Period>
  104. std::chrono::seconds to_seconds(const std::chrono::duration<Rep, Period>& dur) {
  105. return std::chrono::duration_cast<std::chrono::seconds>(dur);
  106. }
  107. /**
  108. * Convert a chrono duration to a number of seconds.
  109. * This casts away precision to get integer seconds.
  110. * @param dur A chrono duration type
  111. * @return The duration as a number of seconds
  112. */
  113. template <class Rep, class Period>
  114. long to_seconds_count(const std::chrono::duration<Rep, Period>& dur) {
  115. return (long) to_seconds(dur).count();
  116. }
  117. /**
  118. * Convert a chrono duration to milliseconds.
  119. * This casts away precision to get integer milliseconds.
  120. * @param dur A chrono duration type
  121. * @return The duration as a chrono milliseconds value
  122. */
  123. template <class Rep, class Period>
  124. std::chrono::milliseconds to_milliseconds(const std::chrono::duration<Rep, Period>& dur) {
  125. return std::chrono::duration_cast<std::chrono::milliseconds>(dur);
  126. }
  127. /**
  128. * Convert a chrono duration to a number of milliseconds.
  129. * This casts away precision to get integer milliseconds.
  130. * @param dur A chrono duration type
  131. * @return The duration as a number of milliseconds
  132. */
  133. template <class Rep, class Period>
  134. long to_milliseconds_count(const std::chrono::duration<Rep, Period>& dur) {
  135. return (long) to_milliseconds(dur).count();
  136. }
  137. /////////////////////////////////////////////////////////////////////////////
  138. // Misc
  139. /**
  140. * Converts an into to a bool.
  141. * @param n An integer.
  142. * @return @em true if n not equal to zero, @em false otherwise
  143. */
  144. inline bool to_bool(int n) { return n != 0; }
  145. /**
  146. * Converts the boolean into a C integer true/false value.
  147. * @param b A boolean
  148. * @return Zero if b is false, non-zero if b is true.
  149. */
  150. inline int to_int(bool b) { return b ? (!0) : 0; }
  151. /**
  152. * Gets a valid string for the char pointer, returning an empty string if
  153. * the pointer is NULL.
  154. * @param cstr A C-string pointer
  155. * @return A string copy of the C array. If `cstr` is NULL, this returns an
  156. * empty string.
  157. */
  158. inline string to_string(const char* cstr) {
  159. return cstr ? string(cstr) : string();
  160. }
  161. /////////////////////////////////////////////////////////////////////////////
  162. // end namespace mqtt
  163. }
  164. #endif // __mqtt_types_h