|
19 | 19 |
|
20 | 20 | #include "MyConfig.h"
|
21 | 21 | #include "MyTransport.h"
|
22 |
| -#include <stdint.h> |
23 | 22 | #include "drivers/RF24/RF24.h"
|
24 |
| -#include "drivers/RF24/RF24_config.h" |
| 23 | + |
25 | 24 | #if defined(MY_RF24_ENABLE_ENCRYPTION)
|
26 |
| -#include "drivers/AES/AES.h" |
| 25 | + #include "drivers/AES/AES.h" |
27 | 26 | #endif
|
28 | 27 |
|
29 |
| -#define TO_ADDR(x) (MY_RF24_BASE_RADIO_ID + x) |
30 |
| - |
31 |
| -#define WRITE_PIPE ((uint8_t)0) |
32 |
| -#define CURRENT_NODE_PIPE ((uint8_t)1) |
33 |
| -#define BROADCAST_PIPE ((uint8_t)2) |
34 |
| - |
35 |
| - |
36 |
| -RF24 _rf24(MY_RF24_CE_PIN, MY_RF24_CS_PIN); |
37 |
| -uint8_t _address; |
38 | 28 | #if defined(MY_RF24_ENABLE_ENCRYPTION)
|
39 | 29 | AES _aes;
|
40 | 30 | uint8_t _dataenc[32] = {0};
|
41 | 31 | uint8_t _psk[16];
|
42 | 32 | #endif
|
43 | 33 |
|
44 | 34 | bool transportInit() {
|
45 |
| - // Start up the radio library |
46 |
| - _rf24.begin(); |
47 |
| - |
48 |
| - if (!_rf24.isPVariant()) { |
49 |
| - return false; |
50 |
| - } |
51 |
| - _rf24.setAutoAck(1); |
52 |
| - _rf24.setAutoAck(BROADCAST_PIPE,false); // Turn off auto ack for broadcast |
53 |
| - _rf24.enableAckPayload(); |
54 |
| - _rf24.setChannel(MY_RF24_CHANNEL); |
55 |
| - _rf24.setPALevel(MY_RF24_PA_LEVEL); |
56 |
| - if (!_rf24.setDataRate(MY_RF24_DATARATE)) { |
57 |
| - return false; |
58 |
| - } |
59 |
| - _rf24.setRetries(5,15); |
60 |
| - _rf24.setCRCLength(RF24_CRC_16); |
61 |
| - _rf24.enableDynamicPayloads(); |
62 |
| - |
63 |
| - // All nodes listen to broadcast pipe (for FIND_PARENT_RESPONSE messages) |
64 |
| - _rf24.openReadingPipe(BROADCAST_PIPE, TO_ADDR(BROADCAST_ADDRESS)); |
65 |
| - |
66 |
| - |
| 35 | + |
67 | 36 | #if defined(MY_RF24_ENABLE_ENCRYPTION)
|
68 | 37 | hwReadConfigBlock((void*)_psk, (void*)EEPROM_RF_ENCRYPTION_AES_KEY_ADDRESS, 16);
|
69 |
| - _aes.set_key(_psk, 16); //set up AES-key |
70 |
| - memset(_psk, 0, 16); // Make sure it is purged from memory when set |
| 38 | + //set up AES-key |
| 39 | + _aes.set_key(_psk, 16); |
| 40 | + // Make sure it is purged from memory when set |
| 41 | + memset(_psk, 0, 16); |
71 | 42 | #endif
|
72 |
| - |
73 |
| - //_rf24.printDetails(); |
74 |
| - |
75 |
| - return true; |
| 43 | + |
| 44 | + return initializeRF24(); |
76 | 45 | }
|
77 | 46 |
|
78 | 47 | void transportSetAddress(uint8_t address) {
|
79 |
| - _address = address; |
80 |
| - _rf24.openReadingPipe(WRITE_PIPE, TO_ADDR(address)); |
81 |
| - _rf24.openReadingPipe(CURRENT_NODE_PIPE, TO_ADDR(address)); |
82 |
| - _rf24.startListening(); |
| 48 | + setNodeAddress(address); |
| 49 | + startListening(); |
83 | 50 | }
|
84 | 51 |
|
85 | 52 | uint8_t transportGetAddress() {
|
86 |
| - return _address; |
| 53 | + return getNodeID(); |
87 | 54 | }
|
88 | 55 |
|
89 |
| -bool transportSend(uint8_t to, const void* data, uint8_t len) { |
| 56 | +bool transportSend(uint8_t recipient, const void* data, uint8_t len) { |
90 | 57 | #if defined(MY_RF24_ENABLE_ENCRYPTION)
|
91 |
| - memcpy(_dataenc,data,len); // copy input data because it is read-only |
92 |
| - |
93 |
| - _aes.set_IV(0);//not sure if necessary |
| 58 | + // copy input data because it is read-only |
| 59 | + memcpy(_dataenc,data,len); |
| 60 | + // has to be adjusted, WIP! |
| 61 | + _aes.set_IV(0); |
94 | 62 | len = len > 16 ? 32 : 16;
|
95 |
| - _aes.cbc_encrypt(_dataenc, _dataenc, len/16); //encrypt |
96 |
| - #endif |
97 |
| - |
98 |
| - // Make sure radio has powered up |
99 |
| - |
100 |
| - _rf24.powerUp(); |
101 |
| - _rf24.stopListening(); |
102 |
| - _rf24.openWritingPipe(TO_ADDR(to)); |
103 |
| - #if defined(MY_RF24_ENABLE_ENCRYPTION) |
104 |
| - bool ok = _rf24.write(_dataenc, len, to == BROADCAST_ADDRESS); |
| 63 | + //encrypt data |
| 64 | + _aes.cbc_encrypt(_dataenc, _dataenc, len/16); |
| 65 | + bool status = sendMessage( recipient, _dataenc, len ); |
105 | 66 | #else
|
106 |
| - bool ok = _rf24.write(data, len, to == BROADCAST_ADDRESS); |
| 67 | + bool status = sendMessage( recipient, data, len ); |
107 | 68 | #endif
|
108 |
| - _rf24.startListening(); |
109 |
| - return ok; |
| 69 | + |
| 70 | + return status; |
110 | 71 | }
|
111 | 72 |
|
112 | 73 | bool transportAvailable(uint8_t *to) {
|
113 |
| - uint8_t pipe = 255; |
114 |
| - boolean avail = _rf24.available(&pipe); |
115 |
| - |
116 |
| - (void)avail; //until somebody makes use of 'avail' |
117 |
| - if (pipe == CURRENT_NODE_PIPE) |
118 |
| - *to = _address; |
119 |
| - else if (pipe == BROADCAST_PIPE) |
120 |
| - *to = BROADCAST_ADDRESS; |
121 |
| - return (_rf24.available() && pipe < 6); |
| 74 | + bool avail = IsDataAvailable(to); |
| 75 | + return avail; |
122 | 76 | }
|
123 | 77 |
|
124 | 78 | uint8_t transportReceive(void* data) {
|
125 |
| - uint8_t len = _rf24.getDynamicPayloadSize(); |
126 |
| - _rf24.read(data, len); |
| 79 | + uint8_t len = readMessage(data); |
127 | 80 | #if defined(MY_RF24_ENABLE_ENCRYPTION)
|
128 |
| - _aes.set_IV(0);//not sure if necessary |
129 |
| - _aes.cbc_decrypt((byte*)(data), (byte*)(data), len>16?2:1); // decrypt |
| 81 | + // has to be adjusted, WIP! |
| 82 | + _aes.set_IV(0); |
| 83 | + // decrypt data |
| 84 | + _aes.cbc_decrypt((byte*)(data), (byte*)(data), len>16?2:1); |
130 | 85 | #endif
|
131 | 86 | return len;
|
132 | 87 | }
|
133 | 88 |
|
134 | 89 | void transportPowerDown() {
|
135 |
| - _rf24.powerDown(); |
| 90 | + powerDown(); |
136 | 91 | }
|
0 commit comments