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.

MyProtocolMySensors.cpp 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 "MyConfig.h"
  20. #include "MyTransport.h"
  21. #include "MyProtocol.h"
  22. #include <string.h>
  23. uint8_t protocolH2i(char c);
  24. char _fmtBuffer[MY_GATEWAY_MAX_SEND_LENGTH];
  25. char _convBuffer[MAX_PAYLOAD*2+1];
  26. bool protocolParse(MyMessage &message, char *inputString)
  27. {
  28. char *str, *p, *value=NULL;
  29. uint8_t bvalue[MAX_PAYLOAD];
  30. uint8_t blen = 0;
  31. int i = 0;
  32. uint8_t command = 0;
  33. // Extract command data coming on serial line
  34. for (str = strtok_r(inputString, ";", &p); // split using semicolon
  35. str && i < 6; // loop while str is not null an max 5 times
  36. str = strtok_r(NULL, ";", &p) // get subsequent tokens
  37. ) {
  38. switch (i) {
  39. case 0: // Radio id (destination)
  40. message.destination = atoi(str);
  41. break;
  42. case 1: // Child id
  43. message.sensor = atoi(str);
  44. break;
  45. case 2: // Message type
  46. command = atoi(str);
  47. mSetCommand(message, command);
  48. break;
  49. case 3: // Should we request ack from destination?
  50. mSetRequestAck(message, atoi(str)?1:0);
  51. break;
  52. case 4: // Data type
  53. message.type = atoi(str);
  54. break;
  55. case 5: // Variable value
  56. if (command == C_STREAM) {
  57. while (*str) {
  58. uint8_t val;
  59. val = protocolH2i(*str++) << 4;
  60. val += protocolH2i(*str++);
  61. bvalue[blen] = val;
  62. blen++;
  63. }
  64. } else {
  65. value = str;
  66. // Remove trailing carriage return and newline character (if it exists)
  67. uint8_t lastCharacter = strlen(value)-1;
  68. if (value[lastCharacter] == '\r') {
  69. value[lastCharacter] = 0;
  70. }
  71. if (value[lastCharacter] == '\n') {
  72. value[lastCharacter] = 0;
  73. }
  74. }
  75. break;
  76. }
  77. i++;
  78. }
  79. // Check for invalid input
  80. if (i < 5) {
  81. return false;
  82. }
  83. message.sender = GATEWAY_ADDRESS;
  84. message.last = GATEWAY_ADDRESS;
  85. mSetAck(message, false);
  86. if (command == C_STREAM) {
  87. message.set(bvalue, blen);
  88. } else {
  89. message.set(value);
  90. }
  91. return true;
  92. }
  93. char * protocolFormat(MyMessage &message)
  94. {
  95. snprintf_P(_fmtBuffer, MY_GATEWAY_MAX_SEND_LENGTH,
  96. PSTR("%" PRIu8 ";%" PRIu8 ";%" PRIu8 ";%" PRIu8 ";%" PRIu8 ";%s\n"), message.sender,
  97. message.sensor, (uint8_t)mGetCommand(message), (uint8_t)mGetAck(message), message.type,
  98. message.getString(_convBuffer));
  99. return _fmtBuffer;
  100. }
  101. char * protocolFormatMQTTTopic(const char* prefix, MyMessage &message)
  102. {
  103. snprintf_P(_fmtBuffer, MY_GATEWAY_MAX_SEND_LENGTH,
  104. PSTR("%s/%" PRIu8 "/%" PRIu8 "/%" PRIu8 "/%" PRIu8 "/%" PRIu8 ""), prefix,
  105. message.sender, message.sensor, mGetCommand(message), mGetAck(message), message.type);
  106. return _fmtBuffer;
  107. }
  108. char * protocolFormatMQTTSubscribe(const char* prefix)
  109. {
  110. snprintf_P(_fmtBuffer, MY_GATEWAY_MAX_SEND_LENGTH, PSTR("%s/+/+/+/+/+"), prefix);
  111. return _fmtBuffer;
  112. }
  113. #ifdef MY_GATEWAY_MQTT_CLIENT
  114. bool protocolMQTTParse(MyMessage &message, char* topic, uint8_t* payload, unsigned int length)
  115. {
  116. char *str, *p;
  117. uint8_t i = 0;
  118. uint8_t command = 0;
  119. if (topic != strstr(topic, MY_MQTT_SUBSCRIBE_TOPIC_PREFIX)) {
  120. // Prefix doesn't match incoming topic
  121. return false;
  122. }
  123. for (str = strtok_r(topic + strlen(MY_MQTT_SUBSCRIBE_TOPIC_PREFIX) + 1, "/", &p); str && i <= 5;
  124. str = strtok_r(NULL, "/", &p)) {
  125. switch (i) {
  126. case 0: {
  127. // Node id
  128. message.destination = atoi(str);
  129. break;
  130. }
  131. case 1: {
  132. // Sensor id
  133. message.sensor = atoi(str);
  134. break;
  135. }
  136. case 2: {
  137. // Command type
  138. command = atoi(str);
  139. mSetCommand(message, command);
  140. break;
  141. }
  142. case 3: {
  143. // Ack flag
  144. mSetRequestAck(message, atoi(str)?1:0);
  145. break;
  146. }
  147. case 4: {
  148. // Sub type
  149. message.type = atoi(str);
  150. break;
  151. }
  152. }
  153. i++;
  154. }
  155. if (i != 5) {
  156. return false;
  157. }
  158. message.sender = GATEWAY_ADDRESS;
  159. message.last = GATEWAY_ADDRESS;
  160. mSetAck(message, false);
  161. // Add payload
  162. if (command == C_STREAM) {
  163. uint8_t bvalue[MAX_PAYLOAD];
  164. uint8_t blen = 0;
  165. while (*payload) {
  166. uint8_t val;
  167. val = protocolH2i(*payload++) << 4;
  168. val += protocolH2i(*payload++);
  169. bvalue[blen] = val;
  170. blen++;
  171. }
  172. message.set(bvalue, blen);
  173. } else {
  174. char* ca;
  175. ca = (char *) payload;
  176. ca += length;
  177. *ca = '\0';
  178. message.set((const char*) payload);
  179. }
  180. return true;
  181. }
  182. #endif
  183. uint8_t protocolH2i(char c)
  184. {
  185. uint8_t i = 0;
  186. if (c <= '9') {
  187. i += c - '0';
  188. } else if (c >= 'a') {
  189. i += c - 'a' + 10;
  190. } else {
  191. i += c - 'A' + 10;
  192. }
  193. return i;
  194. }