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.

MyTransportRF24.cpp 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 "hal/transport/RF24/driver/RF24.h"
  20. #if defined(MY_RF24_ENABLE_ENCRYPTION)
  21. #include "drivers/AES/AES.cpp"
  22. AES RF24_aes;
  23. uint8_t RF24_dataenc[32] = {0};
  24. #endif
  25. #if defined(MY_RX_MESSAGE_BUFFER_FEATURE)
  26. #include "drivers/CircularBuffer/CircularBuffer.h"
  27. typedef struct _transportQueuedMessage {
  28. uint8_t m_len; // Length of the data
  29. uint8_t m_data[MAX_MESSAGE_LENGTH]; // The raw data
  30. } transportQueuedMessage;
  31. /** Buffer to store queued messages in. */
  32. static transportQueuedMessage transportRxQueueStorage[MY_RX_MESSAGE_BUFFER_SIZE];
  33. /** Circular buffer, which uses the transportRxQueueStorage and administers stored messages. */
  34. static CircularBuffer<transportQueuedMessage> transportRxQueue(transportRxQueueStorage,
  35. MY_RX_MESSAGE_BUFFER_SIZE);
  36. static volatile uint8_t transportLostMessageCount = 0;
  37. static void transportRxCallback(void)
  38. {
  39. // Called for each message received by radio, from interrupt context.
  40. // This function _must_ call RF24_readMessage() to de-assert interrupt line!
  41. if (!transportRxQueue.full()) {
  42. transportQueuedMessage* msg = transportRxQueue.getFront();
  43. msg->m_len = RF24_readMessage(msg->m_data); // Read payload & clear RX_DR
  44. (void)transportRxQueue.pushFront(msg);
  45. } else {
  46. // Queue is full. Discard message.
  47. (void)RF24_readMessage(NULL); // Read payload & clear RX_DR
  48. // Keep track of messages lost. Max 255, prevent wrapping.
  49. if (transportLostMessageCount < 255) {
  50. ++transportLostMessageCount;
  51. }
  52. }
  53. }
  54. #endif
  55. bool transportInit(void)
  56. {
  57. #if defined(MY_RF24_ENABLE_ENCRYPTION)
  58. uint8_t RF24_psk[16];
  59. #ifdef MY_ENCRYPTION_SIMPLE_PASSWD
  60. (void)memset(RF24_psk, 0, 16);
  61. (void)memcpy(RF24_psk, MY_ENCRYPTION_SIMPLE_PASSWD, strnlen(MY_ENCRYPTION_SIMPLE_PASSWD, 16));
  62. #else
  63. hwReadConfigBlock((void *)RF24_psk, (void *)EEPROM_RF_ENCRYPTION_AES_KEY_ADDRESS, 16);
  64. #endif
  65. //set up AES-key
  66. RF24_aes.set_key(RF24_psk, 16);
  67. // Make sure it is purged from memory when set
  68. (void)memset(RF24_psk, 0, 16);
  69. #endif
  70. #if defined(MY_RX_MESSAGE_BUFFER_FEATURE)
  71. RF24_registerReceiveCallback( transportRxCallback );
  72. #endif
  73. return RF24_initialize();
  74. }
  75. void transportSetAddress(const uint8_t address)
  76. {
  77. RF24_setNodeAddress(address);
  78. RF24_startListening();
  79. }
  80. uint8_t transportGetAddress(void)
  81. {
  82. return RF24_getNodeID();
  83. }
  84. bool transportSend(const uint8_t to, const void *data, const uint8_t len, const bool noACK)
  85. {
  86. #if defined(MY_RF24_ENABLE_ENCRYPTION)
  87. // copy input data because it is read-only
  88. (void)memcpy(RF24_dataenc,data,len);
  89. // has to be adjusted, WIP!
  90. RF24_aes.set_IV(0);
  91. const uint8_t finalLength = len > 16 ? 32 : 16;
  92. //encrypt data
  93. RF24_aes.cbc_encrypt(RF24_dataenc, RF24_dataenc, finalLength / 16);
  94. return RF24_sendMessage(to, RF24_dataenc, finalLength, noACK);
  95. #else
  96. return RF24_sendMessage(to, data, len, noACK);
  97. #endif
  98. }
  99. bool transportAvailable(void)
  100. {
  101. #if defined(MY_RX_MESSAGE_BUFFER_FEATURE)
  102. (void)RF24_isDataAvailable; // Prevent 'defined but not used' warning
  103. return !transportRxQueue.empty();
  104. #else
  105. return RF24_isDataAvailable();
  106. #endif
  107. }
  108. bool transportSanityCheck(void)
  109. {
  110. return RF24_sanityCheck();
  111. }
  112. uint8_t transportReceive(void *data)
  113. {
  114. uint8_t len = 0;
  115. #if defined(MY_RX_MESSAGE_BUFFER_FEATURE)
  116. transportQueuedMessage* msg = transportRxQueue.getBack();
  117. if (msg) {
  118. len = msg->m_len;
  119. (void)memcpy(data, msg->m_data, len);
  120. (void)transportRxQueue.popBack();
  121. }
  122. #else
  123. len = RF24_readMessage(data);
  124. #endif
  125. #if defined(MY_RF24_ENABLE_ENCRYPTION)
  126. // has to be adjusted, WIP!
  127. RF24_aes.set_IV(0);
  128. // decrypt data
  129. if (RF24_aes.cbc_decrypt((uint8_t *)data, (uint8_t *)data, len > 16 ? 2 : 1) != AES_SUCCESS) {
  130. len = 0;
  131. }
  132. #endif
  133. return len;
  134. }
  135. void transportSleep(void)
  136. {
  137. RF24_sleep();
  138. }
  139. void transportStandBy(void)
  140. {
  141. RF24_standBy();
  142. }
  143. void transportPowerDown(void)
  144. {
  145. RF24_powerDown();
  146. }
  147. void transportPowerUp(void)
  148. {
  149. RF24_powerUp();
  150. }
  151. int16_t transportGetSendingRSSI(void)
  152. {
  153. return RF24_getSendingRSSI();
  154. }
  155. int16_t transportGetReceivingRSSI(void)
  156. {
  157. // not available, only bool RPD
  158. return INVALID_RSSI;
  159. }
  160. int16_t transportGetSendingSNR(void)
  161. {
  162. return INVALID_SNR;
  163. }
  164. int16_t transportGetReceivingSNR(void)
  165. {
  166. return INVALID_SNR;
  167. }
  168. int16_t transportGetTxPowerPercent(void)
  169. {
  170. return static_cast<int16_t>(RF24_getTxPowerPercent());
  171. }
  172. int16_t transportGetTxPowerLevel(void)
  173. {
  174. return static_cast<int16_t>(RF24_getTxPowerLevel());
  175. }
  176. bool transportSetTxPowerPercent(const uint8_t powerPercent)
  177. {
  178. return RF24_setTxPowerPercent(powerPercent);
  179. }