Smart-Home am Beispiel der Präsenzerkennung im Raum Projektarbeit Lennart Heimbs, Johannes Krug, Sebastian Dohle und Kevin Holzschuh bei Prof. Oliver Hofmann SS2019
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MyMessage.h 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. /*
  2. * The MySensors Arduino library handles the wireless radio link and protocol
  3. * between your home built sensors/actuators and HA controller of choice.
  4. * The sensors forms a self healing radio network with optional repeaters. Each
  5. * repeater and gateway builds a routing tables in EEPROM which keeps track of the
  6. * network topology allowing messages to be routed to nodes.
  7. *
  8. * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
  9. * Copyright (C) 2013-2018 Sensnology AB
  10. * Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors
  11. *
  12. * Documentation: http://www.mysensors.org
  13. * Support Forum: http://forum.mysensors.org
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * version 2 as published by the Free Software Foundation.
  18. */
  19. /**
  20. * @file MyMessage.h
  21. *
  22. * @brief API and type declarations for MySensors messages
  23. * @defgroup MyMessagegrp MyMessage
  24. * @ingroup publics
  25. * @{
  26. *
  27. * @brief Here you can find all message types used by the MySensors protocol as well as macros for
  28. * parsing and manipulating messages.
  29. */
  30. #ifndef MyMessage_h
  31. #define MyMessage_h
  32. #ifdef __cplusplus
  33. #include <Arduino.h>
  34. #include <stdint.h>
  35. #endif
  36. #define PROTOCOL_VERSION (2u) //!< The version of the protocol
  37. #define MAX_MESSAGE_LENGTH (32u) //!< The maximum size of a message (including header)
  38. #define HEADER_SIZE (7u) //!< The size of the header
  39. #define MAX_PAYLOAD (MAX_MESSAGE_LENGTH - HEADER_SIZE) //!< The maximum size of a payload depends on #MAX_MESSAGE_LENGTH and #HEADER_SIZE
  40. /// @brief The command field (message-type) defines the overall properties of a message
  41. typedef enum {
  42. C_PRESENTATION = 0, //!< Sent by a node when they present attached sensors. This is usually done in presentation() at startup.
  43. C_SET = 1, //!< This message is sent from or to a sensor when a sensor value should be updated.
  44. C_REQ = 2, //!< Requests a variable value (usually from an actuator destined for controller).
  45. C_INTERNAL = 3, //!< Internal MySensors messages (also include common messages provided/generated by the library).
  46. C_STREAM = 4 //!< For firmware and other larger chunks of data that need to be divided into pieces.
  47. } mysensors_command_t;
  48. #if !DOXYGEN // Hide until we migrate
  49. /// @brief Type of sensor (used when presenting sensors)
  50. typedef enum {
  51. S_DOOR = 0, //!< Door sensor, V_TRIPPED, V_ARMED
  52. S_MOTION = 1, //!< Motion sensor, V_TRIPPED, V_ARMED
  53. S_SMOKE = 2, //!< Smoke sensor, V_TRIPPED, V_ARMED
  54. S_BINARY = 3, //!< Binary light or relay, V_STATUS, V_WATT
  55. S_LIGHT = 3, //!< \deprecated Same as S_BINARY
  56. S_DIMMER = 4, //!< Dimmable light or fan device, V_STATUS (on/off), V_PERCENTAGE (dimmer level 0-100), V_WATT
  57. S_COVER = 5, //!< Blinds or window cover, V_UP, V_DOWN, V_STOP, V_PERCENTAGE (open/close to a percentage)
  58. S_TEMP = 6, //!< Temperature sensor, V_TEMP
  59. S_HUM = 7, //!< Humidity sensor, V_HUM
  60. S_BARO = 8, //!< Barometer sensor, V_PRESSURE, V_FORECAST
  61. S_WIND = 9, //!< Wind sensor, V_WIND, V_GUST
  62. S_RAIN = 10, //!< Rain sensor, V_RAIN, V_RAINRATE
  63. S_UV = 11, //!< Uv sensor, V_UV
  64. S_WEIGHT = 12, //!< Personal scale sensor, V_WEIGHT, V_IMPEDANCE
  65. S_POWER = 13, //!< Power meter, V_WATT, V_KWH, V_VAR, V_VA, V_POWER_FACTOR
  66. S_HEATER = 14, //!< Header device, V_HVAC_SETPOINT_HEAT, V_HVAC_FLOW_STATE, V_TEMP
  67. S_DISTANCE = 15, //!< Distance sensor, V_DISTANCE
  68. S_LIGHT_LEVEL = 16, //!< Light level sensor, V_LIGHT_LEVEL (uncalibrated in percentage), V_LEVEL (light level in lux)
  69. S_ARDUINO_NODE = 17, //!< Used (internally) for presenting a non-repeating Arduino node
  70. S_ARDUINO_REPEATER_NODE = 18, //!< Used (internally) for presenting a repeating Arduino node
  71. S_LOCK = 19, //!< Lock device, V_LOCK_STATUS
  72. S_IR = 20, //!< IR device, V_IR_SEND, V_IR_RECEIVE
  73. S_WATER = 21, //!< Water meter, V_FLOW, V_VOLUME
  74. S_AIR_QUALITY = 22, //!< Air quality sensor, V_LEVEL
  75. S_CUSTOM = 23, //!< Custom sensor
  76. S_DUST = 24, //!< Dust sensor, V_LEVEL
  77. S_SCENE_CONTROLLER = 25, //!< Scene controller device, V_SCENE_ON, V_SCENE_OFF.
  78. S_RGB_LIGHT = 26, //!< RGB light. Send color component data using V_RGB. Also supports V_WATT
  79. S_RGBW_LIGHT = 27, //!< RGB light with an additional White component. Send data using V_RGBW. Also supports V_WATT
  80. S_COLOR_SENSOR = 28, //!< Color sensor, send color information using V_RGB
  81. S_HVAC = 29, //!< Thermostat/HVAC device. V_HVAC_SETPOINT_HEAT, V_HVAC_SETPOINT_COLD, V_HVAC_FLOW_STATE, V_HVAC_FLOW_MODE, V_TEMP
  82. S_MULTIMETER = 30, //!< Multimeter device, V_VOLTAGE, V_CURRENT, V_IMPEDANCE
  83. S_SPRINKLER = 31, //!< Sprinkler, V_STATUS (turn on/off), V_TRIPPED (if fire detecting device)
  84. S_WATER_LEAK = 32, //!< Water leak sensor, V_TRIPPED, V_ARMED
  85. S_SOUND = 33, //!< Sound sensor, V_TRIPPED, V_ARMED, V_LEVEL (sound level in dB)
  86. S_VIBRATION = 34, //!< Vibration sensor, V_TRIPPED, V_ARMED, V_LEVEL (vibration in Hz)
  87. S_MOISTURE = 35, //!< Moisture sensor, V_TRIPPED, V_ARMED, V_LEVEL (water content or moisture in percentage?)
  88. S_INFO = 36, //!< LCD text device / Simple information device on controller, V_TEXT
  89. S_GAS = 37, //!< Gas meter, V_FLOW, V_VOLUME
  90. S_GPS = 38, //!< GPS Sensor, V_POSITION
  91. S_WATER_QUALITY = 39 //!< V_TEMP, V_PH, V_ORP, V_EC, V_STATUS
  92. } mysensors_sensor_t;
  93. /// @brief Type of sensor data (for set/req/ack messages)
  94. typedef enum {
  95. V_TEMP = 0, //!< S_TEMP. Temperature S_TEMP, S_HEATER, S_HVAC
  96. V_HUM = 1, //!< S_HUM. Humidity
  97. V_STATUS = 2, //!< S_BINARY, S_DIMMER, S_SPRINKLER, S_HVAC, S_HEATER. Used for setting/reporting binary (on/off) status. 1=on, 0=off
  98. V_LIGHT = 2, //!< \deprecated Same as V_STATUS
  99. V_PERCENTAGE = 3, //!< S_DIMMER. Used for sending a percentage value 0-100 (%).
  100. V_DIMMER = 3, //!< \deprecated Same as V_PERCENTAGE
  101. V_PRESSURE = 4, //!< S_BARO. Atmospheric Pressure
  102. V_FORECAST = 5, //!< S_BARO. Whether forecast. string of "stable", "sunny", "cloudy", "unstable", "thunderstorm" or "unknown"
  103. V_RAIN = 6, //!< S_RAIN. Amount of rain
  104. V_RAINRATE = 7, //!< S_RAIN. Rate of rain
  105. V_WIND = 8, //!< S_WIND. Wind speed
  106. V_GUST = 9, //!< S_WIND. Gust
  107. V_DIRECTION = 10, //!< S_WIND. Wind direction 0-360 (degrees)
  108. V_UV = 11, //!< S_UV. UV light level
  109. V_WEIGHT = 12, //!< S_WEIGHT. Weight(for scales etc)
  110. V_DISTANCE = 13, //!< S_DISTANCE. Distance
  111. V_IMPEDANCE = 14, //!< S_MULTIMETER, S_WEIGHT. Impedance value
  112. V_ARMED = 15, //!< S_DOOR, S_MOTION, S_SMOKE, S_SPRINKLER. Armed status of a security sensor. 1 = Armed, 0 = Bypassed
  113. V_TRIPPED = 16, //!< S_DOOR, S_MOTION, S_SMOKE, S_SPRINKLER, S_WATER_LEAK, S_SOUND, S_VIBRATION, S_MOISTURE. Tripped status of a security sensor. 1 = Tripped, 0
  114. V_WATT = 17, //!< S_POWER, S_BINARY, S_DIMMER, S_RGB_LIGHT, S_RGBW_LIGHT. Watt value for power meters
  115. V_KWH = 18, //!< S_POWER. Accumulated number of KWH for a power meter
  116. V_SCENE_ON = 19, //!< S_SCENE_CONTROLLER. Turn on a scene
  117. V_SCENE_OFF = 20, //!< S_SCENE_CONTROLLER. Turn of a scene
  118. V_HVAC_FLOW_STATE = 21, //!< S_HEATER, S_HVAC. HVAC flow state ("Off", "HeatOn", "CoolOn", or "AutoChangeOver")
  119. V_HEATER = 21, //!< \deprecated Same as V_HVAC_FLOW_STATE
  120. V_HVAC_SPEED = 22, //!< S_HVAC, S_HEATER. HVAC/Heater fan speed ("Min", "Normal", "Max", "Auto")
  121. V_LIGHT_LEVEL = 23, //!< S_LIGHT_LEVEL. Uncalibrated light level. 0-100%. Use V_LEVEL for light level in lux
  122. V_VAR1 = 24, //!< VAR1
  123. V_VAR2 = 25, //!< VAR2
  124. V_VAR3 = 26, //!< VAR3
  125. V_VAR4 = 27, //!< VAR4
  126. V_VAR5 = 28, //!< VAR5
  127. V_UP = 29, //!< S_COVER. Window covering. Up
  128. V_DOWN = 30, //!< S_COVER. Window covering. Down
  129. V_STOP = 31, //!< S_COVER. Window covering. Stop
  130. V_IR_SEND = 32, //!< S_IR. Send out an IR-command
  131. V_IR_RECEIVE = 33, //!< S_IR. This message contains a received IR-command
  132. V_FLOW = 34, //!< S_WATER. Flow of water (in meter)
  133. V_VOLUME = 35, //!< S_WATER. Water volume
  134. V_LOCK_STATUS = 36, //!< S_LOCK. Set or get lock status. 1=Locked, 0=Unlocked
  135. V_LEVEL = 37, //!< S_DUST, S_AIR_QUALITY, S_SOUND (dB), S_VIBRATION (hz), S_LIGHT_LEVEL (lux)
  136. V_VOLTAGE = 38, //!< S_MULTIMETER
  137. V_CURRENT = 39, //!< S_MULTIMETER
  138. V_RGB = 40, //!< S_RGB_LIGHT, S_COLOR_SENSOR. Sent as ASCII hex: RRGGBB (RR=red, GG=green, BB=blue component)
  139. V_RGBW = 41, //!< S_RGBW_LIGHT. Sent as ASCII hex: RRGGBBWW (WW=white component)
  140. V_ID = 42, //!< Used for sending in sensors hardware ids (i.e. OneWire DS1820b).
  141. V_UNIT_PREFIX = 43, //!< Allows sensors to send in a string representing the unit prefix to be displayed in GUI, not parsed by controller! E.g. cm, m, km, inch.
  142. V_HVAC_SETPOINT_COOL = 44, //!< S_HVAC. HVAC cool setpoint (Integer between 0-100)
  143. V_HVAC_SETPOINT_HEAT = 45, //!< S_HEATER, S_HVAC. HVAC/Heater setpoint (Integer between 0-100)
  144. V_HVAC_FLOW_MODE = 46, //!< S_HVAC. Flow mode for HVAC ("Auto", "ContinuousOn", "PeriodicOn")
  145. V_TEXT = 47, //!< S_INFO. Text message to display on LCD or controller device
  146. V_CUSTOM = 48, //!< Custom messages used for controller/inter node specific commands, preferably using S_CUSTOM device type.
  147. V_POSITION = 49, //!< GPS position and altitude. Payload: latitude;longitude;altitude(m). E.g. "55.722526;13.017972;18"
  148. V_IR_RECORD = 50, //!< Record IR codes S_IR for playback
  149. V_PH = 51, //!< S_WATER_QUALITY, water PH
  150. V_ORP = 52, //!< S_WATER_QUALITY, water ORP : redox potential in mV
  151. V_EC = 53, //!< S_WATER_QUALITY, water electric conductivity μS/cm (microSiemens/cm)
  152. V_VAR = 54, //!< S_POWER, Reactive power: volt-ampere reactive (var)
  153. V_VA = 55, //!< S_POWER, Apparent power: volt-ampere (VA)
  154. V_POWER_FACTOR = 56, //!< S_POWER, Ratio of real power to apparent power: floating point value in the range [-1,..,1]
  155. } mysensors_data_t;
  156. #endif
  157. /// @brief Type of internal messages (for internal messages)
  158. typedef enum {
  159. I_BATTERY_LEVEL = 0, //!< Battery level
  160. I_TIME = 1, //!< Time (request/response)
  161. I_VERSION = 2, //!< Version
  162. I_ID_REQUEST = 3, //!< ID request
  163. I_ID_RESPONSE = 4, //!< ID response
  164. I_INCLUSION_MODE = 5, //!< Inclusion mode
  165. I_CONFIG = 6, //!< Config (request/response)
  166. I_FIND_PARENT_REQUEST = 7, //!< Find parent
  167. I_FIND_PARENT_RESPONSE = 8, //!< Find parent response
  168. I_LOG_MESSAGE = 9, //!< Log message
  169. I_CHILDREN = 10, //!< Children
  170. I_SKETCH_NAME = 11, //!< Sketch name
  171. I_SKETCH_VERSION = 12, //!< Sketch version
  172. I_REBOOT = 13, //!< Reboot request
  173. I_GATEWAY_READY = 14, //!< Gateway ready
  174. I_SIGNING_PRESENTATION = 15, //!< Provides signing related preferences (first byte is preference version)
  175. I_NONCE_REQUEST = 16, //!< Request for a nonce
  176. I_NONCE_RESPONSE = 17, //!< Payload is nonce data
  177. I_HEARTBEAT_REQUEST = 18, //!< Heartbeat request
  178. I_PRESENTATION = 19, //!< Presentation message
  179. I_DISCOVER_REQUEST = 20, //!< Discover request
  180. I_DISCOVER_RESPONSE = 21, //!< Discover response
  181. I_HEARTBEAT_RESPONSE = 22, //!< Heartbeat response
  182. I_LOCKED = 23, //!< Node is locked (reason in string-payload)
  183. I_PING = 24, //!< Ping sent to node, payload incremental hop counter
  184. I_PONG = 25, //!< In return to ping, sent back to sender, payload incremental hop counter
  185. I_REGISTRATION_REQUEST = 26, //!< Register request to GW
  186. I_REGISTRATION_RESPONSE = 27, //!< Register response from GW
  187. I_DEBUG = 28, //!< Debug message
  188. I_SIGNAL_REPORT_REQUEST = 29, //!< Device signal strength request
  189. I_SIGNAL_REPORT_REVERSE = 30, //!< Internal
  190. I_SIGNAL_REPORT_RESPONSE = 31, //!< Device signal strength response (RSSI)
  191. I_PRE_SLEEP_NOTIFICATION = 32, //!< Message sent before node is going to sleep
  192. I_POST_SLEEP_NOTIFICATION = 33 //!< Message sent after node woke up (if enabled)
  193. } mysensors_internal_t;
  194. /// @brief Type of data stream (for streamed message)
  195. typedef enum {
  196. ST_FIRMWARE_CONFIG_REQUEST = 0, //!< Request new FW, payload contains current FW details
  197. ST_FIRMWARE_CONFIG_RESPONSE = 1, //!< New FW details to initiate OTA FW update
  198. ST_FIRMWARE_REQUEST = 2, //!< Request FW block
  199. ST_FIRMWARE_RESPONSE = 3, //!< Response FW block
  200. ST_SOUND = 4, //!< Sound
  201. ST_IMAGE = 5, //!< Image
  202. ST_FIRMWARE_CONFIRM = 6, //!< Mark running firmware as valid (MyOTAFirmwareUpdateNVM + mcuboot)
  203. ST_FIRMWARE_RESPONSE_RLE = 7, //!< Response FW block with run length encoded data
  204. } mysensors_stream_t;
  205. /// @brief Type of payload
  206. typedef enum {
  207. P_STRING = 0, //!< Payload type is string
  208. P_BYTE = 1, //!< Payload type is byte
  209. P_INT16 = 2, //!< Payload type is INT16
  210. P_UINT16 = 3, //!< Payload type is UINT16
  211. P_LONG32 = 4, //!< Payload type is INT32
  212. P_ULONG32 = 5, //!< Payload type is UINT32
  213. P_CUSTOM = 6, //!< Payload type is binary
  214. P_FLOAT32 = 7 //!< Payload type is float32
  215. } mysensors_payload_t;
  216. #ifndef BIT
  217. #define BIT(n) ( 1<<(n) ) //!< Bit indexing macro
  218. #endif
  219. #define BIT_MASK(len) ( BIT(len)-1 ) //!< Create a bitmask of length 'len'
  220. #define BF_MASK(start, len) ( BIT_MASK(len)<<(start) ) //!< Create a bitfield mask of length starting at bit 'start'
  221. #define BF_PREP(x, start, len) ( ((x)&BIT_MASK(len)) << (start) ) //!< Prepare a bitmask for insertion or combining
  222. #define BF_GET(y, start, len) ( ((y)>>(start)) & BIT_MASK(len) ) //!< Extract a bitfield of length 'len' starting at bit 'start' from 'y'
  223. #define BF_SET(y, x, start, len) ( y= ((y) &~ BF_MASK(start, len)) | BF_PREP(x, start, len) ) //!< Insert a new bitfield value 'x' into 'y'
  224. // Getters/setters for special bit fields in header
  225. #define mSetVersion(_message,_version) BF_SET(_message.version_length, _version, 0, 2) //!< Set version field
  226. #define mGetVersion(_message) ((uint8_t)BF_GET(_message.version_length, 0, 2)) //!< Get version field
  227. #define mSetSigned(_message,_signed) BF_SET(_message.version_length, _signed, 2, 1) //!< Set signed field
  228. #define mGetSigned(_message) ((bool)BF_GET(_message.version_length, 2, 1)) //!< Get signed field
  229. #define mSetLength(_message,_length) BF_SET(_message.version_length, _length, 3, 5) //!< Set length field
  230. #define mGetLength(_message) ((uint8_t)BF_GET(_message.version_length, 3, 5)) //!< Get length field
  231. #define mSetCommand(_message,_command) BF_SET(_message.command_ack_payload, _command, 0, 3) //!< Set command field
  232. #define mGetCommand(_message) ((uint8_t)BF_GET(_message.command_ack_payload, 0, 3)) //!< Get command field
  233. #define mSetRequestAck(_message,_rack) BF_SET(_message.command_ack_payload, _rack, 3, 1) //!< Set ack-request field
  234. #define mGetRequestAck(_message) ((bool)BF_GET(_message.command_ack_payload, 3, 1)) //!< Get ack-request field
  235. #define mSetAck(_message,_ackMsg) BF_SET(_message.command_ack_payload, _ackMsg, 4, 1) //!< Set ack field
  236. #define mGetAck(_message) ((bool)BF_GET(_message.command_ack_payload, 4, 1)) //!< Get ack field
  237. #define mSetPayloadType(_message, _pt) BF_SET(_message.command_ack_payload, _pt, 5, 3) //!< Set payload type field
  238. #define mGetPayloadType(_message) ((uint8_t)BF_GET(_message.command_ack_payload, 5, 3)) //!< Get payload type field
  239. // internal access for special fields
  240. #define miGetCommand() ((uint8_t)BF_GET(command_ack_payload, 0, 3)) //!< Internal getter for command field
  241. #define miSetLength(_length) BF_SET(version_length, _length, 3, 5) //!< Internal setter for length field
  242. #define miGetLength() ((uint8_t)BF_GET(version_length, 3, 5)) //!< Internal getter for length field
  243. #define miSetVersion(_version) BF_SET(version_length, _version, 0, 2) //!< Internal setter for version field
  244. #define miGetVersion() ((uint8_t)BF_GET(version_length, 0, 2)) //!< Internal getter for version field
  245. #define miSetRequestAck(_rack) BF_SET(command_ack_payload, _rack, 3, 1) //!< Internal setter for ack-request field
  246. #define miGetRequestAck() ((bool)BF_GET(command_ack_payload, 3, 1)) //!< Internal getter for ack-request field
  247. #define miSetAck(_ack) BF_SET(command_ack_payload, _ack, 4, 1) //!< Internal setter for ack field
  248. #define miGetAck() ((bool)BF_GET(command_ack_payload, 4, 1)) //!< Internal getter for ack field
  249. #define miSetPayloadType(_pt) BF_SET(command_ack_payload, _pt, 5, 3) //!< Internal setter for payload type field
  250. #define miGetPayloadType() (uint8_t)BF_GET(command_ack_payload, 5, 3) //!< Internal getter for payload type field
  251. #if defined(__cplusplus) || defined(DOXYGEN)
  252. /**
  253. * @brief MyMessage is used to create, manipulate, send and read MySensors messages
  254. */
  255. class MyMessage
  256. {
  257. private:
  258. char* getCustomString(char *buffer) const;
  259. public:
  260. /**
  261. * Default constructor
  262. */
  263. MyMessage(void);
  264. /**
  265. * Constructor
  266. * @param sensor id of the child sensor for this message
  267. * @param type see http://korturl.nu/stupidurl
  268. */
  269. MyMessage(const uint8_t sensor, const uint8_t type);
  270. /**
  271. * Single character hex (0 - 15) conversion
  272. * @param i byte (only lower 4 bits will be considered)
  273. * @return single char with the hex representation (0 to F) of the parameter
  274. */
  275. char i2h(const uint8_t i) const;
  276. /**
  277. * @brief Clear message contents.
  278. */
  279. void clear(void);
  280. /**
  281. * If payload is something else than P_STRING you can have the payload value converted
  282. * into string representation by supplying a buffer with the minimum size of
  283. * 2*MAX_PAYLOAD+1. This is to be able to fit hex-conversion of a full binary payload.
  284. * @param buffer pointer to a buffer that's at least 2*MAX_PAYLOAD+1 bytes large
  285. */
  286. char* getStream(char *buffer) const;
  287. /**
  288. * @brief Copy the payload into the supplied buffer
  289. */
  290. char* getString(char *buffer) const;
  291. /**
  292. * @brief Get payload as string
  293. * @return pointer to a char array storing the string
  294. */
  295. const char* getString(void) const;
  296. /**
  297. * @brief Get custom payload
  298. * @return pointer to the raw payload
  299. */
  300. void* getCustom(void) const;
  301. /**
  302. * @brief Get bool payload
  303. * @return a bool with the value of the payload (true/false)
  304. */
  305. bool getBool(void) const;
  306. /**
  307. * @brief Get unsigned 8-bit integer payload
  308. * @return the value of the payload, 0 to 255
  309. */
  310. uint8_t getByte(void) const;
  311. /**
  312. * @brief Get float payload
  313. * @return the floating-point value of the payload
  314. */
  315. float getFloat(void) const;
  316. /**
  317. * @brief Get signed 16-bit integer payload
  318. * @return the value of the payload, –32768 to 32767
  319. */
  320. int16_t getInt(void) const;
  321. /**
  322. * @brief Get unsigned 16-bit integer payload
  323. * @return the value of the payload, 0 to 65535
  324. */
  325. uint16_t getUInt(void) const;
  326. /**
  327. * @brief Get signed 32-bit integer payload
  328. * @return the value of the payload, –2147483648 to 2147483647
  329. */
  330. int32_t getLong(void) const;
  331. /**
  332. * @brief Get unsigned 32-bit integer payload
  333. * @return the value of the payload, 0 to 4294967295
  334. */
  335. uint32_t getULong(void) const;
  336. /**
  337. * @brief Getter for command type
  338. * @return #mysensors_command_t
  339. */
  340. uint8_t getCommand(void) const;
  341. /**
  342. * @brief Getter for ack-flag.
  343. * @return true if this is an ack message
  344. */
  345. bool isAck(void) const;
  346. /**
  347. * @brief Set message type
  348. * @param type see http://korturl.nu/stupidurl
  349. */
  350. MyMessage& setType(const uint8_t type);
  351. /**
  352. * @brief Set which child sensor this message belongs to
  353. * @param sensor
  354. */
  355. MyMessage& setSensor(const uint8_t sensor);
  356. /**
  357. * @brief Set final destination node id for this message
  358. * @param destination
  359. */
  360. MyMessage& setDestination(const uint8_t destination);
  361. /**
  362. * @brief Set entire payload
  363. * @param payload pointer to the buffer where the payload is stored
  364. * @param length of the payload
  365. */
  366. MyMessage& set(const void* payload, const uint8_t length);
  367. /**
  368. * @brief Set payload to character array
  369. * @param value pointer to the character array. The array must be null-terminated.
  370. */
  371. MyMessage& set(const char* value);
  372. #if !defined(__linux__)
  373. /**
  374. * @brief Set payload to character array from flash
  375. * @param value pointer to the character array. The array must be null-terminated.
  376. */
  377. MyMessage& set(const __FlashStringHelper* value);
  378. #endif
  379. /**
  380. * @brief Set payload to decimal number
  381. * @param value float
  382. * @param decimals number of decimals to include
  383. */
  384. MyMessage& set(const float value, const uint8_t decimals);
  385. /**
  386. * @brief Set payload to bool value
  387. * @param value true or false
  388. */
  389. MyMessage& set(const bool value);
  390. /**
  391. * @brief Set payload to unsigned 8-bit integer value
  392. * @param value (0 to 255)
  393. */
  394. MyMessage& set(const uint8_t value);
  395. /**
  396. * @brief Set payload to unsigned 32-bit integer value
  397. * @param value (0 to 4294967295)
  398. */
  399. MyMessage& set(const uint32_t value);
  400. /**
  401. * @brief Set payload to signed 32-bit integer value
  402. * @param value (–2147483648 to 2147483647)
  403. */
  404. MyMessage& set(const int32_t value);
  405. /**
  406. * @brief Set payload to unsigned 16-bit integer value
  407. * @param value (0 to 65535)
  408. */
  409. MyMessage& set(const uint16_t value);
  410. /**
  411. * @brief Set payload to signed 16-bit integer value
  412. * @param value (–32768 to 32767)
  413. */
  414. MyMessage& set(const int16_t value);
  415. #else
  416. typedef union {
  417. struct {
  418. #endif
  419. uint8_t last; //!< 8 bit - Id of last node this message passed
  420. uint8_t sender; //!< 8 bit - Id of sender node (origin)
  421. uint8_t destination; //!< 8 bit - Id of destination node
  422. /**
  423. * 2 bit - Protocol version<br>
  424. * 1 bit - Signed flag<br>
  425. * 5 bit - Length of payload
  426. */
  427. uint8_t version_length;
  428. /**
  429. * 3 bit - Command type<br>
  430. * 1 bit - Request an ack - Indicator that receiver should send an ack back<br>
  431. * 1 bit - Is ack message - Indicator that this is the actual ack message<br>
  432. * 3 bit - Payload data type
  433. */
  434. uint8_t command_ack_payload;
  435. uint8_t type; //!< 8 bit - Type varies depending on command
  436. uint8_t sensor; //!< 8 bit - Id of sensor that this message concerns.
  437. /*
  438. * Each message can transfer a payload. We add one extra byte for string
  439. * terminator \0 to be "printable" this is not transferred OTA
  440. * This union is used to simplify the construction of the binary data types transferred.
  441. */
  442. union {
  443. uint8_t bValue; //!< unsigned byte value (8-bit)
  444. uint16_t uiValue; //!< unsigned integer value (16-bit)
  445. int16_t iValue; //!< signed integer value (16-bit)
  446. uint32_t ulValue; //!< unsigned long value (32-bit)
  447. int32_t lValue; //!< signed long value (32-bit)
  448. struct { //!< Float messages
  449. float fValue;
  450. uint8_t fPrecision; //!< Number of decimals when serializing
  451. };
  452. struct { //!< Presentation messages
  453. uint8_t version; //!< Library version
  454. uint8_t sensorType; //!< Sensor type hint for controller, see table above
  455. };
  456. char data[MAX_PAYLOAD + 1]; //!< Buffer for raw payload data
  457. } __attribute__((packed)); //!< Doxygen will complain without this comment
  458. #if defined(__cplusplus) || defined(DOXYGEN)
  459. } __attribute__((packed));
  460. #else
  461. };
  462. uint8_t array[HEADER_SIZE + MAX_PAYLOAD + 1]; //!< buffer for entire message
  463. } __attribute__((packed)) MyMessage;
  464. #endif
  465. #endif
  466. /** @}*/