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.

MyTransportRFM69.cpp 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. #if defined(MY_RFM69_NEW_DRIVER)
  20. #include "hal/transport/RFM69/driver/new/RFM69_new.h"
  21. bool transportInit(void)
  22. {
  23. const bool result = RFM69_initialise(MY_RFM69_FREQUENCY);
  24. #if defined(MY_GATEWAY_FEATURE) || defined(MY_RFM69_ATC_MODE_DISABLED)
  25. // ATC mode function not used
  26. (void)RFM69_ATCmode;
  27. #else
  28. RFM69_ATCmode(true, MY_RFM69_ATC_TARGET_RSSI_DBM);
  29. #endif
  30. #ifdef MY_RFM69_ENABLE_ENCRYPTION
  31. uint8_t RFM69_psk[16];
  32. #ifdef MY_ENCRYPTION_SIMPLE_PASSWD
  33. (void)memset(RFM69_psk, 0, 16);
  34. (void)memcpy(RFM69_psk, MY_ENCRYPTION_SIMPLE_PASSWD, strnlen(MY_ENCRYPTION_SIMPLE_PASSWD, 16));
  35. #else
  36. hwReadConfigBlock((void *)RFM69_psk, (void*)EEPROM_RF_ENCRYPTION_AES_KEY_ADDRESS, 16);
  37. #endif
  38. RFM69_encrypt((const char *)RFM69_psk);
  39. (void)memset(RFM69_psk, 0, 16); // Make sure it is purged from memory when set
  40. #else
  41. (void)RFM69_encrypt;
  42. #endif
  43. return result;
  44. }
  45. void transportSetAddress(const uint8_t address)
  46. {
  47. RFM69_setAddress(address);
  48. }
  49. uint8_t transportGetAddress(void)
  50. {
  51. return RFM69_getAddress();
  52. }
  53. bool transportSend(const uint8_t to, const void *data, uint8_t len, const bool noACK)
  54. {
  55. if (noACK) {
  56. (void)RFM69_sendWithRetry(to, data, len, 0, 0);
  57. return true;
  58. }
  59. return RFM69_sendWithRetry(to, data, len);
  60. }
  61. bool transportAvailable(void)
  62. {
  63. RFM69_handler();
  64. return RFM69_available();
  65. }
  66. bool transportSanityCheck(void)
  67. {
  68. return RFM69_sanityCheck();
  69. }
  70. uint8_t transportReceive(void *data)
  71. {
  72. return RFM69_receive((uint8_t *)data, MAX_MESSAGE_LENGTH);
  73. }
  74. void transportSleep(void)
  75. {
  76. (void)RFM69_sleep();
  77. }
  78. void transportStandBy(void)
  79. {
  80. (void)RFM69_standBy();
  81. }
  82. void transportPowerDown(void)
  83. {
  84. (void)RFM69_powerDown();
  85. }
  86. void transportPowerUp(void)
  87. {
  88. (void)RFM69_powerUp();
  89. }
  90. bool transportSetTxPowerLevel(const uint8_t powerLevel)
  91. {
  92. // range 0..23
  93. return RFM69_setTxPowerLevel(powerLevel);
  94. }
  95. void transportSetTargetRSSI(const int16_t targetSignalStrength)
  96. {
  97. #if !defined(MY_GATEWAY_FEATURE) && !defined(MY_RFM69_ATC_MODE_DISABLED)
  98. RFM69_ATCmode(true, targetSignalStrength);
  99. #else
  100. (void)targetSignalStrength;
  101. #endif
  102. }
  103. int16_t transportGetSendingRSSI(void)
  104. {
  105. return RFM69_getSendingRSSI();
  106. }
  107. int16_t transportGetReceivingRSSI(void)
  108. {
  109. return RFM69_getReceivingRSSI();
  110. }
  111. int16_t transportGetSendingSNR(void)
  112. {
  113. return INVALID_SNR;
  114. }
  115. int16_t transportGetReceivingSNR(void)
  116. {
  117. return INVALID_SNR;
  118. }
  119. int16_t transportGetTxPowerPercent(void)
  120. {
  121. return RFM69_getTxPowerPercent();
  122. }
  123. int16_t transportGetTxPowerLevel(void)
  124. {
  125. return RFM69_getTxPowerLevel();
  126. }
  127. bool transportSetTxPowerPercent(const uint8_t powerPercent)
  128. {
  129. return RFM69_setTxPowerPercent(powerPercent);
  130. }
  131. #else
  132. #include "hal/transport/RFM69/driver/old/RFM69_old.h"
  133. RFM69 _radio(MY_RFM69_CS_PIN, MY_RFM69_IRQ_PIN, MY_RFM69HW, MY_RFM69_IRQ_NUM);
  134. uint8_t _address;
  135. bool transportInit(void)
  136. {
  137. #if defined(MY_RFM69_POWER_PIN)
  138. //hwPinMode(MY_RFM69_POWER_PIN, OUTPUT);
  139. #endif
  140. #ifdef MY_RF69_DIO5
  141. //hwPinMode(MY_RF69_DIO5, INPUT);
  142. #endif
  143. // Start up the radio library (_address will be set later by the MySensors library)
  144. if (_radio.initialize(MY_RFM69_FREQUENCY, _address, MY_RFM69_NETWORKID)) {
  145. #ifdef MY_RFM69_ENABLE_ENCRYPTION
  146. uint8_t RFM69_psk[16];
  147. #ifdef MY_ENCRYPTION_SIMPLE_PASSWD
  148. (void)memset(RFM69_psk, 0, 16);
  149. (void)memcpy(RFM69_psk, MY_ENCRYPTION_SIMPLE_PASSWD, strnlen(MY_ENCRYPTION_SIMPLE_PASSWD, 16));
  150. #else
  151. hwReadConfigBlock((void *)RFM69_psk, (void *)EEPROM_RF_ENCRYPTION_AES_KEY_ADDRESS, 16);
  152. #endif
  153. _radio.encrypt((const char *)RFM69_psk);
  154. (void)memset(RFM69_psk, 0, 16); // Make sure it is purged from memory when set
  155. #endif
  156. return true;
  157. }
  158. return false;
  159. }
  160. void transportSetAddress(const uint8_t address)
  161. {
  162. _address = address;
  163. _radio.setAddress(address);
  164. }
  165. uint8_t transportGetAddress(void)
  166. {
  167. return _address;
  168. }
  169. bool transportSend(const uint8_t to, const void *data, const uint8_t len, const bool noACK)
  170. {
  171. if (noACK) {
  172. (void)_radio.sendWithRetry(to, data, len, 0, 0);
  173. return true;
  174. }
  175. return _radio.sendWithRetry(to, data, len);
  176. }
  177. bool transportAvailable(void)
  178. {
  179. return _radio.receiveDone();
  180. }
  181. bool transportSanityCheck(void)
  182. {
  183. return _radio.sanityCheck();
  184. }
  185. uint8_t transportReceive(void *data)
  186. {
  187. // save payload length
  188. const uint8_t dataLen = _radio.DATALEN < MAX_MESSAGE_LENGTH? _radio.DATALEN : MAX_MESSAGE_LENGTH;
  189. (void)memcpy((void *)data, (void *)_radio.DATA, dataLen);
  190. // Send ack back if this message wasn't a broadcast
  191. if (_radio.ACKRequested()) {
  192. _radio.sendACK();
  193. }
  194. return dataLen;
  195. }
  196. void transportSleep(void)
  197. {
  198. _radio.sleep();
  199. }
  200. void transportStandBy(void)
  201. {
  202. _radio.standBy();
  203. }
  204. void transportPowerDown(void)
  205. {
  206. _radio.powerDown();
  207. }
  208. void transportPowerUp(void)
  209. {
  210. _radio.powerUp();
  211. }
  212. int16_t transportGetSendingRSSI(void)
  213. {
  214. return INVALID_RSSI;
  215. }
  216. int16_t transportGetReceivingRSSI(void)
  217. {
  218. return _radio.RSSI;
  219. }
  220. int16_t transportGetSendingSNR(void)
  221. {
  222. return INVALID_SNR;
  223. }
  224. int16_t transportGetReceivingSNR(void)
  225. {
  226. return INVALID_SNR;
  227. }
  228. int16_t transportGetTxPowerPercent(void)
  229. {
  230. return INVALID_PERCENT;
  231. }
  232. int16_t transportGetTxPowerLevel(void)
  233. {
  234. return INVALID_LEVEL;
  235. }
  236. bool transportSetTxPowerLevel(const uint8_t powerLevel)
  237. {
  238. // not implemented
  239. (void)powerLevel;
  240. return false;
  241. }
  242. #endif