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.cpp 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. #include "MyMessage.h"
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. MyMessage::MyMessage(void)
  24. {
  25. clear();
  26. }
  27. MyMessage::MyMessage(const uint8_t _sensor, const uint8_t _type)
  28. {
  29. clear();
  30. sensor = _sensor;
  31. type = _type;
  32. }
  33. void MyMessage::clear(void)
  34. {
  35. last = 0u;
  36. sender = 0u;
  37. destination = 0u; // Gateway is default destination
  38. version_length = 0u;
  39. command_ack_payload = 0u;
  40. type = 0u;
  41. sensor = 0u;
  42. (void)memset(data, 0u, sizeof(data));
  43. // set message protocol version
  44. miSetVersion(PROTOCOL_VERSION);
  45. }
  46. bool MyMessage::isAck(void) const
  47. {
  48. return miGetAck();
  49. }
  50. uint8_t MyMessage::getCommand(void) const
  51. {
  52. return miGetCommand();
  53. }
  54. /* Getters for payload converted to desired form */
  55. void* MyMessage::getCustom(void) const
  56. {
  57. return (void *)data;
  58. }
  59. const char* MyMessage::getString(void) const
  60. {
  61. uint8_t payloadType = miGetPayloadType();
  62. if (payloadType == P_STRING) {
  63. return data;
  64. } else {
  65. return NULL;
  66. }
  67. }
  68. char MyMessage::i2h(const uint8_t i) const
  69. {
  70. uint8_t k = i & 0x0F;
  71. if (k <= 9) {
  72. return '0' + k;
  73. } else {
  74. return 'A' + k - 10;
  75. }
  76. }
  77. char* MyMessage::getCustomString(char *buffer) const
  78. {
  79. for (uint8_t i = 0; i < miGetLength(); i++) {
  80. buffer[i * 2] = i2h(data[i] >> 4);
  81. buffer[(i * 2) + 1] = i2h(data[i]);
  82. }
  83. buffer[miGetLength() * 2] = '\0';
  84. return buffer;
  85. }
  86. char* MyMessage::getStream(char *buffer) const
  87. {
  88. uint8_t cmd = miGetCommand();
  89. if ((cmd == C_STREAM) && (buffer != NULL)) {
  90. return getCustomString(buffer);
  91. } else {
  92. return NULL;
  93. }
  94. }
  95. char* MyMessage::getString(char *buffer) const
  96. {
  97. uint8_t payloadType = miGetPayloadType();
  98. if (buffer != NULL) {
  99. if (payloadType == P_STRING) {
  100. (void)strncpy(buffer, data, miGetLength());
  101. buffer[miGetLength()] = 0;
  102. } else if (payloadType == P_BYTE) {
  103. (void)itoa(bValue, buffer, 10);
  104. } else if (payloadType == P_INT16) {
  105. (void)itoa(iValue, buffer, 10);
  106. } else if (payloadType == P_UINT16) {
  107. (void)utoa(uiValue, buffer, 10);
  108. } else if (payloadType == P_LONG32) {
  109. (void)ltoa(lValue, buffer, 10);
  110. } else if (payloadType == P_ULONG32) {
  111. (void)ultoa(ulValue, buffer, 10);
  112. } else if (payloadType == P_FLOAT32) {
  113. (void)dtostrf(fValue, 2, min(fPrecision, (uint8_t)8), buffer);
  114. } else if (payloadType == P_CUSTOM) {
  115. return getCustomString(buffer);
  116. }
  117. return buffer;
  118. } else {
  119. return NULL;
  120. }
  121. }
  122. bool MyMessage::getBool(void) const
  123. {
  124. return getByte();
  125. }
  126. uint8_t MyMessage::getByte(void) const
  127. {
  128. if (miGetPayloadType() == P_BYTE) {
  129. return data[0];
  130. } else if (miGetPayloadType() == P_STRING) {
  131. return atoi(data);
  132. } else {
  133. return 0;
  134. }
  135. }
  136. float MyMessage::getFloat(void) const
  137. {
  138. if (miGetPayloadType() == P_FLOAT32) {
  139. return fValue;
  140. } else if (miGetPayloadType() == P_STRING) {
  141. return atof(data);
  142. } else {
  143. return 0;
  144. }
  145. }
  146. int32_t MyMessage::getLong(void) const
  147. {
  148. if (miGetPayloadType() == P_LONG32) {
  149. return lValue;
  150. } else if (miGetPayloadType() == P_STRING) {
  151. return atol(data);
  152. } else {
  153. return 0;
  154. }
  155. }
  156. uint32_t MyMessage::getULong(void) const
  157. {
  158. if (miGetPayloadType() == P_ULONG32) {
  159. return ulValue;
  160. } else if (miGetPayloadType() == P_STRING) {
  161. return atol(data);
  162. } else {
  163. return 0;
  164. }
  165. }
  166. int16_t MyMessage::getInt(void) const
  167. {
  168. if (miGetPayloadType() == P_INT16) {
  169. return iValue;
  170. } else if (miGetPayloadType() == P_STRING) {
  171. return atoi(data);
  172. } else {
  173. return 0;
  174. }
  175. }
  176. uint16_t MyMessage::getUInt(void) const
  177. {
  178. if (miGetPayloadType() == P_UINT16) {
  179. return uiValue;
  180. } else if (miGetPayloadType() == P_STRING) {
  181. return atoi(data);
  182. } else {
  183. return 0;
  184. }
  185. }
  186. MyMessage& MyMessage::setType(const uint8_t _type)
  187. {
  188. type = _type;
  189. return *this;
  190. }
  191. MyMessage& MyMessage::setSensor(const uint8_t _sensor)
  192. {
  193. sensor = _sensor;
  194. return *this;
  195. }
  196. MyMessage& MyMessage::setDestination(const uint8_t _destination)
  197. {
  198. destination = _destination;
  199. return *this;
  200. }
  201. // Set payload
  202. MyMessage& MyMessage::set(const void* value, const uint8_t length)
  203. {
  204. uint8_t payloadLength = value == NULL ? 0 : min(length, (uint8_t)MAX_PAYLOAD);
  205. miSetLength(payloadLength);
  206. miSetPayloadType(P_CUSTOM);
  207. memcpy(data, value, payloadLength);
  208. return *this;
  209. }
  210. MyMessage& MyMessage::set(const char* value)
  211. {
  212. uint8_t length = value == NULL ? 0 : min(strlen(value), (size_t)MAX_PAYLOAD);
  213. miSetLength(length);
  214. miSetPayloadType(P_STRING);
  215. if (length) {
  216. strncpy(data, value, length);
  217. }
  218. // null terminate string
  219. data[length] = 0;
  220. return *this;
  221. }
  222. #if !defined(__linux__)
  223. MyMessage& MyMessage::set(const __FlashStringHelper* value)
  224. {
  225. uint8_t length = value == NULL ? 0
  226. : min(strlen_P(reinterpret_cast<const char *>(value)), (size_t)MAX_PAYLOAD);
  227. miSetLength(length);
  228. miSetPayloadType(P_STRING);
  229. if (length) {
  230. strncpy_P(data, reinterpret_cast<const char *>(value), length);
  231. }
  232. // null terminate string
  233. data[length] = 0;
  234. return *this;
  235. }
  236. #endif
  237. MyMessage& MyMessage::set(const bool value)
  238. {
  239. miSetLength(1);
  240. miSetPayloadType(P_BYTE);
  241. data[0] = value;
  242. return *this;
  243. }
  244. MyMessage& MyMessage::set(const uint8_t value)
  245. {
  246. miSetLength(1);
  247. miSetPayloadType(P_BYTE);
  248. data[0] = value;
  249. return *this;
  250. }
  251. MyMessage& MyMessage::set(const float value, const uint8_t decimals)
  252. {
  253. miSetLength(5); // 32 bit float + persi
  254. miSetPayloadType(P_FLOAT32);
  255. fValue=value;
  256. fPrecision = decimals;
  257. return *this;
  258. }
  259. MyMessage& MyMessage::set(const uint32_t value)
  260. {
  261. miSetPayloadType(P_ULONG32);
  262. miSetLength(4);
  263. ulValue = value;
  264. return *this;
  265. }
  266. MyMessage& MyMessage::set(const int32_t value)
  267. {
  268. miSetPayloadType(P_LONG32);
  269. miSetLength(4);
  270. lValue = value;
  271. return *this;
  272. }
  273. MyMessage& MyMessage::set(const uint16_t value)
  274. {
  275. miSetPayloadType(P_UINT16);
  276. miSetLength(2);
  277. uiValue = value;
  278. return *this;
  279. }
  280. MyMessage& MyMessage::set(const int16_t value)
  281. {
  282. miSetPayloadType(P_INT16);
  283. miSetLength(2);
  284. iValue = value;
  285. return *this;
  286. }