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.

MyTransportRFM95.cpp 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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/RFM95/driver/RFM95.h"
  20. #if defined(MY_RFM95_ENABLE_ENCRYPTION)
  21. #include "drivers/AES/AES.h"
  22. #endif
  23. #if defined(MY_RFM95_ENABLE_ENCRYPTION)
  24. AES RFM95_aes;
  25. uint8_t RFM95_dataenc[32] = {0};
  26. #endif
  27. #if defined(MY_RFM95_ENABLE_ENCRYPTION)
  28. #include "drivers/AES/AES.cpp"
  29. #endif
  30. bool transportInit(void)
  31. {
  32. #if defined(MY_RFM95_ENABLE_ENCRYPTION)
  33. uint8_t RFM95_psk[16];
  34. #ifdef MY_SIGNING_SIMPLE_PASSWD
  35. (void)memset((void *)RFM95_psk, 0, 16);
  36. (void)memcpy((void *)RFM95_psk, MY_SIGNING_SIMPLE_PASSWD, strnlen(MY_SIGNING_SIMPLE_PASSWD, 16));
  37. #else
  38. hwReadConfigBlock((void *)RFM95_psk, (void *)EEPROM_RF_ENCRYPTION_AES_KEY_ADDRESS, 16);
  39. #endif
  40. //set up AES-key
  41. RFM95_aes.set_key(RFM95_psk, 16);
  42. // Make sure it is purged from memory when set
  43. (void)memset((void *)RFM95_psk, 0, 16);
  44. #endif
  45. const bool result = RFM95_initialise(MY_RFM95_FREQUENCY);
  46. #if defined(MY_RFM95_TCXO)
  47. RFM95_enableTCXO();
  48. #endif
  49. #if !defined(MY_GATEWAY_FEATURE) && !defined(MY_RFM95_ATC_MODE_DISABLED)
  50. // only enable ATC mode in nodes
  51. RFM95_ATCmode(true, MY_RFM95_ATC_TARGET_RSSI);
  52. #endif
  53. return result;
  54. }
  55. void transportSetAddress(const uint8_t address)
  56. {
  57. RFM95_setAddress(address);
  58. }
  59. uint8_t transportGetAddress(void)
  60. {
  61. return RFM95_getAddress();
  62. }
  63. bool transportSend(const uint8_t to, const void *data, const uint8_t len, const bool noACK)
  64. {
  65. #if defined(MY_RFM95_ENABLE_ENCRYPTION)
  66. // copy input data because it is read-only
  67. (void)memcpy((void *)RFM95_dataenc, (const void *)data, len);
  68. // has to be adjusted, WIP!
  69. RFM95_aes.set_IV(0);
  70. const uint8_t finalLength = len > 16 ? 32 : 16;
  71. //encrypt data
  72. RFM95_aes.cbc_encrypt(RFM95_dataenc, RFM95_dataenc, finalLength / 16);
  73. if (noACK) {
  74. (void)RFM95_sendWithRetry(to, RFM95_dataenc, finalLength, 0, 0);
  75. return true;
  76. }
  77. return RFM95_sendWithRetry(to, RFM95_dataenc, finalLength);
  78. #else
  79. if (noACK) {
  80. (void)RFM95_sendWithRetry(to, data, len, 0, 0);
  81. return true;
  82. }
  83. return RFM95_sendWithRetry(to, data, len);
  84. #endif
  85. }
  86. bool transportAvailable(void)
  87. {
  88. RFM95_handler();
  89. return RFM95_available();
  90. }
  91. bool transportSanityCheck(void)
  92. {
  93. return RFM95_sanityCheck();
  94. }
  95. uint8_t transportReceive(void *data)
  96. {
  97. uint8_t len = RFM95_receive((uint8_t *)data, MAX_MESSAGE_LENGTH);
  98. #if defined(MY_RFM95_ENABLE_ENCRYPTION)
  99. // has to be adjusted, WIP!
  100. RFM95_aes.set_IV(0);
  101. // decrypt data
  102. if (RFM95_aes.cbc_decrypt((uint8_t *)data, (uint8_t *)data, len > 16 ? 2 : 1) != AES_SUCCESS) {
  103. len = 0;
  104. }
  105. #endif
  106. return len;
  107. }
  108. void transportSleep(void)
  109. {
  110. (void)RFM95_sleep();
  111. }
  112. void transportStandBy(void)
  113. {
  114. (void)RFM95_standBy();
  115. }
  116. void transportPowerDown(void)
  117. {
  118. RFM95_powerDown();
  119. }
  120. void transportPowerUp(void)
  121. {
  122. RFM95_powerUp();
  123. }
  124. void transportToggleATCmode(const bool OnOff, const int16_t targetRSSI)
  125. {
  126. RFM95_ATCmode(OnOff, targetRSSI);
  127. }
  128. int16_t transportGetSendingRSSI(void)
  129. {
  130. return RFM95_getSendingRSSI();
  131. }
  132. int16_t transportGetReceivingRSSI(void)
  133. {
  134. return RFM95_getReceivingRSSI();
  135. }
  136. int16_t transportGetSendingSNR(void)
  137. {
  138. return RFM95_getSendingSNR();
  139. }
  140. int16_t transportGetReceivingSNR(void)
  141. {
  142. return RFM95_getReceivingSNR();
  143. }
  144. int16_t transportGetTxPowerPercent(void)
  145. {
  146. return RFM95_getTxPowerPercent();
  147. }
  148. int16_t transportGetTxPowerLevel(void)
  149. {
  150. return RFM95_getTxPowerLevel();
  151. }
  152. bool transportSetTxPowerPercent(const uint8_t powerPercent)
  153. {
  154. return RFM95_setTxPowerPercent(powerPercent);
  155. }