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.

MyTransportNRF5_ESB.cpp 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. * Copyright (C) 2017 Frank Holtz
  11. * Full contributor list:
  12. * https://github.com/mysensors/MySensors/graphs/contributors
  13. *
  14. * Documentation: http://www.mysensors.org
  15. * Support Forum: http://forum.mysensors.org
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License
  19. * version 2 as published by the Free Software Foundation.
  20. */
  21. #include "hal/transport/NRF5_ESB/driver/Radio.h"
  22. #include "hal/transport/NRF5_ESB/driver/Radio_ESB.h"
  23. #include "drivers/CircularBuffer/CircularBuffer.h"
  24. #if defined(MY_NRF5_ESB_ENABLE_ENCRYPTION)
  25. #include "drivers/AES/AES.cpp"
  26. AES NRF5_ESB_aes;
  27. uint8_t NRF5_ESB_dataenc[32] = {0};
  28. #endif
  29. bool transportInit(void)
  30. {
  31. #if defined(MY_NRF5_ESB_ENABLE_ENCRYPTION)
  32. uint8_t NRF5_ESB_psk[16];
  33. #ifdef MY_ENCRYPTION_SIMPLE_PASSWD
  34. (void)memset(NRF5_ESB_psk, 0, 16);
  35. (void)memcpy(NRF5_ESB_psk, MY_ENCRYPTION_SIMPLE_PASSWD, strnlen(MY_ENCRYPTION_SIMPLE_PASSWD, 16));
  36. #else
  37. hwReadConfigBlock((void*)NRF5_ESB_psk, (void*)EEPROM_RF_ENCRYPTION_KEY_ADDRESS,
  38. 16);
  39. #endif
  40. // set up AES-key
  41. NRF5_ESB_aes.set_key(NRF5_ESB_psk, 16);
  42. // Make sure it is purged from memory when set
  43. (void)memset(NRF5_ESB_psk, 0, 16);
  44. #endif
  45. return NRF5_ESB_initialize();
  46. }
  47. void transportSetAddress(const uint8_t address)
  48. {
  49. NRF5_ESB_setNodeAddress(address);
  50. NRF5_ESB_startListening();
  51. }
  52. uint8_t transportGetAddress(void)
  53. {
  54. return NRF5_ESB_getNodeID();
  55. }
  56. bool transportSend(const uint8_t to, const void *data, const uint8_t len, const bool noACK)
  57. {
  58. #if defined(MY_NRF5_ESB_ENABLE_ENCRYPTION)
  59. // copy input data because it is read-only
  60. (void)memcpy(NRF5_ESB_dataenc, data, len);
  61. // has to be adjusted, WIP!
  62. NRF5_ESB_aes.set_IV(0);
  63. const uint8_t finalLength = len > 16 ? 32 : 16;
  64. // encrypt data
  65. NRF5_ESB_aes.cbc_encrypt(NRF5_ESB_dataenc, NRF5_ESB_dataenc, finalLength / 16);
  66. return NRF5_ESB_sendMessage(to, NRF5_ESB_dataenc, finalLength, noACK);
  67. #else
  68. return NRF5_ESB_sendMessage(to, data, len, noACK);
  69. #endif
  70. }
  71. bool transportAvailable(void)
  72. {
  73. return NRF5_ESB_isDataAvailable();
  74. }
  75. bool transportSanityCheck(void)
  76. {
  77. return NRF5_ESB_sanityCheck();
  78. }
  79. uint8_t transportReceive(void *data)
  80. {
  81. uint8_t len = 0;
  82. len = NRF5_ESB_readMessage(data);
  83. #if defined(MY_NRF5_ESB_ENABLE_ENCRYPTION)
  84. // has to be adjusted, WIP!
  85. NRF5_ESB_aes.set_IV(0);
  86. // decrypt data
  87. if (NRF5_ESB_aes.cbc_decrypt((uint8_t *)(data), (uint8_t *)(data),
  88. len > 16 ? 2 : 1) != AES_SUCCESS) {
  89. len = 0;
  90. }
  91. #endif
  92. return len;
  93. }
  94. void transportPowerDown(void)
  95. {
  96. NRF5_ESB_powerDown();
  97. }
  98. void transportPowerUp(void)
  99. {
  100. NRF5_ESB_powerUp();
  101. }
  102. void transportSleep(void)
  103. {
  104. NRF5_ESB_sleep();
  105. }
  106. void transportStandBy(void)
  107. {
  108. NRF5_ESB_standBy();
  109. }
  110. int16_t transportGetSendingRSSI(void)
  111. {
  112. return NRF5_ESB_getSendingRSSI();
  113. }
  114. int16_t transportGetReceivingRSSI(void)
  115. {
  116. return NRF5_ESB_getReceivingRSSI();
  117. }
  118. int16_t transportGetSendingSNR(void)
  119. {
  120. return INVALID_SNR;
  121. }
  122. int16_t transportGetReceivingSNR(void)
  123. {
  124. return INVALID_SNR;
  125. }
  126. int16_t transportGetTxPowerPercent(void)
  127. {
  128. return NRF5_getTxPowerPercent();
  129. }
  130. int16_t transportGetTxPowerLevel(void)
  131. {
  132. return static_cast<int16_t>(NRF5_getTxPowerLevel());
  133. }
  134. bool transportSetTxPowerPercent(const uint8_t powerPercent)
  135. {
  136. return NRF5_setTxPowerPercent(powerPercent);
  137. }