![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Wireless interface using LoRa technology
Dependencies: AlohaTransceiver RingBuffer SX1276Lib SerialInterfaceProtocol mbed L3PDU
main.cpp@15:182b95c25cf2, 2016-08-11 (annotated)
- Committer:
- rba90
- Date:
- Thu Aug 11 03:43:51 2016 +0000
- Revision:
- 15:182b95c25cf2
- Parent:
- 14:25e836a5a7bf
- Child:
- 16:ab2d9341997a
integration test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rba90 | 0:1e42d79b42e1 | 1 | #include "mbed.h" |
rba90 | 0:1e42d79b42e1 | 2 | #include "AlohaTransceiver.h" |
rba90 | 0:1e42d79b42e1 | 3 | #include "buffer.h" |
rba90 | 0:1e42d79b42e1 | 4 | #include "SerialInterfaceProtocol.h" |
rba90 | 0:1e42d79b42e1 | 5 | #include "AlohaFrame.h" |
rba90 | 14:25e836a5a7bf | 6 | #include "ControlPacket.h" |
rba90 | 14:25e836a5a7bf | 7 | #include "DataBlockPacket.h" |
rba90 | 0:1e42d79b42e1 | 8 | |
rba90 | 0:1e42d79b42e1 | 9 | Serial pc(USBTX, USBRX); |
rba90 | 0:1e42d79b42e1 | 10 | |
rba90 | 0:1e42d79b42e1 | 11 | // sip uses two buffer queues |
rba90 | 0:1e42d79b42e1 | 12 | CircularBuffer<uint8_t> SerialInputBuffer; |
rba90 | 0:1e42d79b42e1 | 13 | CircularBuffer<uint8_t> SerialOutputBuffer; |
rba90 | 0:1e42d79b42e1 | 14 | SerialInterfaceProtocol SIP(&SerialInputBuffer, &SerialOutputBuffer); |
rba90 | 0:1e42d79b42e1 | 15 | |
rba90 | 0:1e42d79b42e1 | 16 | // aloha transceiver |
rba90 | 8:332099ece6e9 | 17 | AlohaTransceiver aloha(DEVICE_ID); |
rba90 | 0:1e42d79b42e1 | 18 | AlohaFrame txFrame; |
rba90 | 0:1e42d79b42e1 | 19 | |
rba90 | 0:1e42d79b42e1 | 20 | void serialInterruptHandler() { |
rba90 | 0:1e42d79b42e1 | 21 | // Note: you need to actually read from the serial to clear the RX interrupt |
rba90 | 0:1e42d79b42e1 | 22 | int c = pc.getc(); |
rba90 | 0:1e42d79b42e1 | 23 | |
rba90 | 0:1e42d79b42e1 | 24 | // add to buffer |
rba90 | 0:1e42d79b42e1 | 25 | if (SerialInputBuffer.isLocked()) |
rba90 | 0:1e42d79b42e1 | 26 | { |
rba90 | 0:1e42d79b42e1 | 27 | printf("Mutex Locked\r\n"); |
rba90 | 0:1e42d79b42e1 | 28 | } |
rba90 | 0:1e42d79b42e1 | 29 | else |
rba90 | 0:1e42d79b42e1 | 30 | { |
rba90 | 0:1e42d79b42e1 | 31 | SerialInputBuffer.enqueue((uint8_t) c); |
rba90 | 0:1e42d79b42e1 | 32 | } |
rba90 | 0:1e42d79b42e1 | 33 | } |
rba90 | 0:1e42d79b42e1 | 34 | |
rba90 | 0:1e42d79b42e1 | 35 | int toggleChecksum(uint8_t *payload, uint8_t payload_length, uint8_t *response, uint8_t *response_length) |
rba90 | 0:1e42d79b42e1 | 36 | { |
rba90 | 0:1e42d79b42e1 | 37 | // one payload |
rba90 | 0:1e42d79b42e1 | 38 | if (payload_length != 1) |
rba90 | 0:1e42d79b42e1 | 39 | { |
rba90 | 0:1e42d79b42e1 | 40 | sprintf((char *) response, "Wrong Payload Length\r\n"); |
rba90 | 0:1e42d79b42e1 | 41 | *response_length = 22; |
rba90 | 0:1e42d79b42e1 | 42 | return 1; |
rba90 | 0:1e42d79b42e1 | 43 | } |
rba90 | 0:1e42d79b42e1 | 44 | |
rba90 | 0:1e42d79b42e1 | 45 | if ((bool) payload[0]) |
rba90 | 0:1e42d79b42e1 | 46 | { |
rba90 | 0:1e42d79b42e1 | 47 | SIP.enableChecksum(); |
rba90 | 0:1e42d79b42e1 | 48 | } |
rba90 | 0:1e42d79b42e1 | 49 | else |
rba90 | 0:1e42d79b42e1 | 50 | { |
rba90 | 0:1e42d79b42e1 | 51 | SIP.disableChecksum(); |
rba90 | 0:1e42d79b42e1 | 52 | } |
rba90 | 0:1e42d79b42e1 | 53 | |
rba90 | 0:1e42d79b42e1 | 54 | return 0; |
rba90 | 0:1e42d79b42e1 | 55 | } |
rba90 | 0:1e42d79b42e1 | 56 | |
rba90 | 9:e13e02aa4a2f | 57 | /* |
rba90 | 9:e13e02aa4a2f | 58 | * Format: |
rba90 | 14:25e836a5a7bf | 59 | * < : start flag |
rba90 | 14:25e836a5a7bf | 60 | * 02 : command |
rba90 | 14:25e836a5a7bf | 61 | * xx : length |
rba90 | 14:25e836a5a7bf | 62 | * xx: : 00: get, 01: set |
rba90 | 14:25e836a5a7bf | 63 | * xx : index for parameters |
rba90 | 9:e13e02aa4a2f | 64 | * ... |
rba90 | 14:25e836a5a7bf | 65 | * ff :checksum (not currently in used) |
rba90 | 9:e13e02aa4a2f | 66 | * > :end flag |
rba90 | 9:e13e02aa4a2f | 67 | */ |
rba90 | 9:e13e02aa4a2f | 68 | int configureRadio(uint8_t *payload, uint8_t payload_length, uint8_t *response, uint8_t *response_length) |
rba90 | 9:e13e02aa4a2f | 69 | { |
rba90 | 9:e13e02aa4a2f | 70 | // read settings from radio |
rba90 | 9:e13e02aa4a2f | 71 | #if USE_MODEM_LORA == 1 |
rba90 | 9:e13e02aa4a2f | 72 | AlohaTransceiver::LoRaSettings_t *settings = aloha.getSettings(); |
rba90 | 9:e13e02aa4a2f | 73 | #elif USE_MODEM_FSK == 1 |
rba90 | 9:e13e02aa4a2f | 74 | AlohaTransceiver::FskSettings_t *settings = aloha.getSettings(); |
rba90 | 9:e13e02aa4a2f | 75 | #else |
rba90 | 9:e13e02aa4a2f | 76 | #error "Please define a modem in the compiler options." |
rba90 | 9:e13e02aa4a2f | 77 | #endif |
rba90 | 9:e13e02aa4a2f | 78 | |
rba90 | 9:e13e02aa4a2f | 79 | if (payload_length < 2) |
rba90 | 9:e13e02aa4a2f | 80 | { |
rba90 | 9:e13e02aa4a2f | 81 | sprintf((char *) response, "Wrong Payload Length\r\n"); |
rba90 | 9:e13e02aa4a2f | 82 | *response_length = 22; |
rba90 | 9:e13e02aa4a2f | 83 | return 1; |
rba90 | 9:e13e02aa4a2f | 84 | } |
rba90 | 9:e13e02aa4a2f | 85 | |
rba90 | 9:e13e02aa4a2f | 86 | // true is set, false is get |
rba90 | 9:e13e02aa4a2f | 87 | bool isSet = (bool) payload[0]; |
rba90 | 9:e13e02aa4a2f | 88 | uint8_t idx = payload[1]; |
rba90 | 9:e13e02aa4a2f | 89 | |
rba90 | 9:e13e02aa4a2f | 90 | switch(idx) |
rba90 | 9:e13e02aa4a2f | 91 | { |
rba90 | 9:e13e02aa4a2f | 92 | case 0x00: // Power |
rba90 | 9:e13e02aa4a2f | 93 | { |
rba90 | 9:e13e02aa4a2f | 94 | if (isSet) |
rba90 | 9:e13e02aa4a2f | 95 | { |
rba90 | 9:e13e02aa4a2f | 96 | int8_t Power = (int8_t) payload[2]; |
rba90 | 9:e13e02aa4a2f | 97 | settings->Power = Power; |
rba90 | 9:e13e02aa4a2f | 98 | |
rba90 | 9:e13e02aa4a2f | 99 | return 0; |
rba90 | 9:e13e02aa4a2f | 100 | } |
rba90 | 9:e13e02aa4a2f | 101 | else |
rba90 | 9:e13e02aa4a2f | 102 | { |
rba90 | 9:e13e02aa4a2f | 103 | response[0] = (uint8_t) settings->Power; |
rba90 | 9:e13e02aa4a2f | 104 | *response_length = 1; |
rba90 | 9:e13e02aa4a2f | 105 | |
rba90 | 9:e13e02aa4a2f | 106 | return 0; |
rba90 | 9:e13e02aa4a2f | 107 | } |
rba90 | 9:e13e02aa4a2f | 108 | } |
rba90 | 9:e13e02aa4a2f | 109 | |
rba90 | 9:e13e02aa4a2f | 110 | case 0x01: // Bandwidth |
rba90 | 9:e13e02aa4a2f | 111 | { |
rba90 | 9:e13e02aa4a2f | 112 | if (isSet) |
rba90 | 9:e13e02aa4a2f | 113 | { |
rba90 | 9:e13e02aa4a2f | 114 | uint32_t Bandwidth = (payload[5]) | |
rba90 | 9:e13e02aa4a2f | 115 | (payload[4] << 8) | |
rba90 | 9:e13e02aa4a2f | 116 | (payload[3] << 16) | |
rba90 | 9:e13e02aa4a2f | 117 | (payload[2] << 24); |
rba90 | 9:e13e02aa4a2f | 118 | settings->Bandwidth = Bandwidth; |
rba90 | 9:e13e02aa4a2f | 119 | |
rba90 | 9:e13e02aa4a2f | 120 | return 0; |
rba90 | 9:e13e02aa4a2f | 121 | } |
rba90 | 9:e13e02aa4a2f | 122 | else |
rba90 | 9:e13e02aa4a2f | 123 | { |
rba90 | 9:e13e02aa4a2f | 124 | response[3] = (uint8_t) (settings->Bandwidth); |
rba90 | 9:e13e02aa4a2f | 125 | response[2] = (uint8_t) (settings->Bandwidth >> 8); |
rba90 | 9:e13e02aa4a2f | 126 | response[1] = (uint8_t) (settings->Bandwidth >> 16); |
rba90 | 9:e13e02aa4a2f | 127 | response[0] = (uint8_t) (settings->Bandwidth >> 24); |
rba90 | 9:e13e02aa4a2f | 128 | *response_length = 4; |
rba90 | 9:e13e02aa4a2f | 129 | |
rba90 | 9:e13e02aa4a2f | 130 | return 0; |
rba90 | 9:e13e02aa4a2f | 131 | } |
rba90 | 9:e13e02aa4a2f | 132 | |
rba90 | 9:e13e02aa4a2f | 133 | } |
rba90 | 9:e13e02aa4a2f | 134 | |
rba90 | 9:e13e02aa4a2f | 135 | case 0x02: // Datarate, AKA Spreading Factor |
rba90 | 9:e13e02aa4a2f | 136 | { |
rba90 | 9:e13e02aa4a2f | 137 | if (isSet) |
rba90 | 9:e13e02aa4a2f | 138 | { |
rba90 | 9:e13e02aa4a2f | 139 | uint32_t Datarate = (payload[5]) | |
rba90 | 9:e13e02aa4a2f | 140 | (payload[4] << 8) | |
rba90 | 9:e13e02aa4a2f | 141 | (payload[3] << 16) | |
rba90 | 9:e13e02aa4a2f | 142 | (payload[2] << 24); |
rba90 | 9:e13e02aa4a2f | 143 | settings->Datarate = Datarate; |
rba90 | 9:e13e02aa4a2f | 144 | |
rba90 | 9:e13e02aa4a2f | 145 | return 0; |
rba90 | 9:e13e02aa4a2f | 146 | } |
rba90 | 9:e13e02aa4a2f | 147 | else |
rba90 | 9:e13e02aa4a2f | 148 | { |
rba90 | 9:e13e02aa4a2f | 149 | response[3] = (uint8_t) (settings->Datarate); |
rba90 | 9:e13e02aa4a2f | 150 | response[2] = (uint8_t) (settings->Datarate >> 8); |
rba90 | 9:e13e02aa4a2f | 151 | response[1] = (uint8_t) (settings->Datarate >> 16); |
rba90 | 9:e13e02aa4a2f | 152 | response[0] = (uint8_t) (settings->Datarate >> 24); |
rba90 | 9:e13e02aa4a2f | 153 | *response_length = 4; |
rba90 | 9:e13e02aa4a2f | 154 | |
rba90 | 9:e13e02aa4a2f | 155 | return 0; |
rba90 | 9:e13e02aa4a2f | 156 | } |
rba90 | 9:e13e02aa4a2f | 157 | } |
rba90 | 9:e13e02aa4a2f | 158 | |
rba90 | 9:e13e02aa4a2f | 159 | case 0x03: // Coderate |
rba90 | 9:e13e02aa4a2f | 160 | { |
rba90 | 9:e13e02aa4a2f | 161 | if (isSet) |
rba90 | 9:e13e02aa4a2f | 162 | { |
rba90 | 9:e13e02aa4a2f | 163 | uint8_t Coderate = payload[2]; |
rba90 | 9:e13e02aa4a2f | 164 | settings->Coderate = Coderate; |
rba90 | 9:e13e02aa4a2f | 165 | |
rba90 | 9:e13e02aa4a2f | 166 | return 0; |
rba90 | 9:e13e02aa4a2f | 167 | } |
rba90 | 9:e13e02aa4a2f | 168 | else |
rba90 | 9:e13e02aa4a2f | 169 | { |
rba90 | 9:e13e02aa4a2f | 170 | response[0] = (uint8_t) settings->Coderate; |
rba90 | 9:e13e02aa4a2f | 171 | *response_length = 1; |
rba90 | 9:e13e02aa4a2f | 172 | |
rba90 | 9:e13e02aa4a2f | 173 | return 0; |
rba90 | 9:e13e02aa4a2f | 174 | } |
rba90 | 9:e13e02aa4a2f | 175 | } |
rba90 | 9:e13e02aa4a2f | 176 | |
rba90 | 9:e13e02aa4a2f | 177 | case 0x04: //Preamble Length |
rba90 | 9:e13e02aa4a2f | 178 | { |
rba90 | 9:e13e02aa4a2f | 179 | if (isSet) |
rba90 | 9:e13e02aa4a2f | 180 | { |
rba90 | 9:e13e02aa4a2f | 181 | uint16_t PreambleLen = payload[3] | (payload[2] << 8); |
rba90 | 9:e13e02aa4a2f | 182 | settings->PreambleLen = PreambleLen; |
rba90 | 9:e13e02aa4a2f | 183 | return 0; |
rba90 | 9:e13e02aa4a2f | 184 | } |
rba90 | 9:e13e02aa4a2f | 185 | else |
rba90 | 9:e13e02aa4a2f | 186 | { |
rba90 | 9:e13e02aa4a2f | 187 | response[1] = (uint8_t) (settings->PreambleLen); |
rba90 | 9:e13e02aa4a2f | 188 | response[0] = (uint8_t)(settings->PreambleLen >> 8); |
rba90 | 9:e13e02aa4a2f | 189 | *response_length = 2; |
rba90 | 9:e13e02aa4a2f | 190 | |
rba90 | 9:e13e02aa4a2f | 191 | return 0; |
rba90 | 9:e13e02aa4a2f | 192 | } |
rba90 | 9:e13e02aa4a2f | 193 | } |
rba90 | 9:e13e02aa4a2f | 194 | |
rba90 | 9:e13e02aa4a2f | 195 | case 0x05: //Symbol Timeout |
rba90 | 9:e13e02aa4a2f | 196 | { |
rba90 | 9:e13e02aa4a2f | 197 | if (isSet) |
rba90 | 9:e13e02aa4a2f | 198 | { |
rba90 | 9:e13e02aa4a2f | 199 | uint16_t SymbolTimeout = payload[3] | (payload[2] << 8); |
rba90 | 9:e13e02aa4a2f | 200 | settings->SymbolTimeout = SymbolTimeout; |
rba90 | 9:e13e02aa4a2f | 201 | return 0; |
rba90 | 9:e13e02aa4a2f | 202 | } |
rba90 | 9:e13e02aa4a2f | 203 | else |
rba90 | 9:e13e02aa4a2f | 204 | { |
rba90 | 9:e13e02aa4a2f | 205 | response[1] = (uint8_t) (settings->SymbolTimeout); |
rba90 | 9:e13e02aa4a2f | 206 | response[0] = (uint8_t) (settings->SymbolTimeout >> 8); |
rba90 | 9:e13e02aa4a2f | 207 | *response_length = 2; |
rba90 | 9:e13e02aa4a2f | 208 | |
rba90 | 9:e13e02aa4a2f | 209 | return 0; |
rba90 | 9:e13e02aa4a2f | 210 | } |
rba90 | 9:e13e02aa4a2f | 211 | } |
rba90 | 9:e13e02aa4a2f | 212 | |
rba90 | 9:e13e02aa4a2f | 213 | case 0x06: //FixLen |
rba90 | 9:e13e02aa4a2f | 214 | { |
rba90 | 9:e13e02aa4a2f | 215 | if (isSet) |
rba90 | 9:e13e02aa4a2f | 216 | { |
rba90 | 9:e13e02aa4a2f | 217 | bool FixLen = payload[2]; |
rba90 | 9:e13e02aa4a2f | 218 | settings->FixLen = FixLen; |
rba90 | 9:e13e02aa4a2f | 219 | return 0; |
rba90 | 9:e13e02aa4a2f | 220 | } |
rba90 | 9:e13e02aa4a2f | 221 | else |
rba90 | 9:e13e02aa4a2f | 222 | { |
rba90 | 9:e13e02aa4a2f | 223 | response[0] = (bool) (settings->SymbolTimeout); |
rba90 | 9:e13e02aa4a2f | 224 | *response_length = 1; |
rba90 | 9:e13e02aa4a2f | 225 | |
rba90 | 9:e13e02aa4a2f | 226 | return 0; |
rba90 | 9:e13e02aa4a2f | 227 | } |
rba90 | 9:e13e02aa4a2f | 228 | } |
rba90 | 9:e13e02aa4a2f | 229 | |
rba90 | 9:e13e02aa4a2f | 230 | case 0x07: //PayloadLen |
rba90 | 9:e13e02aa4a2f | 231 | { |
rba90 | 9:e13e02aa4a2f | 232 | if (isSet) |
rba90 | 9:e13e02aa4a2f | 233 | { |
rba90 | 9:e13e02aa4a2f | 234 | uint8_t PayloadLen = payload[2]; |
rba90 | 9:e13e02aa4a2f | 235 | settings->PayloadLen = PayloadLen; |
rba90 | 9:e13e02aa4a2f | 236 | return 0; |
rba90 | 9:e13e02aa4a2f | 237 | } |
rba90 | 9:e13e02aa4a2f | 238 | else |
rba90 | 9:e13e02aa4a2f | 239 | { |
rba90 | 9:e13e02aa4a2f | 240 | response[0] = (uint8_t) (settings->PayloadLen); |
rba90 | 9:e13e02aa4a2f | 241 | |
rba90 | 9:e13e02aa4a2f | 242 | return 0; |
rba90 | 9:e13e02aa4a2f | 243 | } |
rba90 | 9:e13e02aa4a2f | 244 | } |
rba90 | 9:e13e02aa4a2f | 245 | |
rba90 | 9:e13e02aa4a2f | 246 | case 0x08: //CrcOn |
rba90 | 9:e13e02aa4a2f | 247 | { |
rba90 | 9:e13e02aa4a2f | 248 | if (isSet) { |
rba90 | 9:e13e02aa4a2f | 249 | bool CrcOn = payload[2]; |
rba90 | 9:e13e02aa4a2f | 250 | settings->CrcOn = CrcOn; |
rba90 | 9:e13e02aa4a2f | 251 | return 0; |
rba90 | 9:e13e02aa4a2f | 252 | } |
rba90 | 9:e13e02aa4a2f | 253 | else |
rba90 | 9:e13e02aa4a2f | 254 | { |
rba90 | 9:e13e02aa4a2f | 255 | response[0] = (bool) (settings->CrcOn); |
rba90 | 9:e13e02aa4a2f | 256 | |
rba90 | 9:e13e02aa4a2f | 257 | return 0; |
rba90 | 9:e13e02aa4a2f | 258 | } |
rba90 | 9:e13e02aa4a2f | 259 | } |
rba90 | 9:e13e02aa4a2f | 260 | |
rba90 | 9:e13e02aa4a2f | 261 | case 0x09: //FreqHopOn |
rba90 | 9:e13e02aa4a2f | 262 | { |
rba90 | 9:e13e02aa4a2f | 263 | if (isSet) { |
rba90 | 9:e13e02aa4a2f | 264 | bool FreqHopOn = payload[2]; |
rba90 | 9:e13e02aa4a2f | 265 | settings->FreqHopOn = FreqHopOn; |
rba90 | 9:e13e02aa4a2f | 266 | return 0; |
rba90 | 9:e13e02aa4a2f | 267 | } |
rba90 | 9:e13e02aa4a2f | 268 | else |
rba90 | 9:e13e02aa4a2f | 269 | { |
rba90 | 9:e13e02aa4a2f | 270 | response[0] = (bool) (settings->FreqHopOn); |
rba90 | 9:e13e02aa4a2f | 271 | |
rba90 | 9:e13e02aa4a2f | 272 | return 0; |
rba90 | 9:e13e02aa4a2f | 273 | } |
rba90 | 9:e13e02aa4a2f | 274 | } |
rba90 | 9:e13e02aa4a2f | 275 | |
rba90 | 9:e13e02aa4a2f | 276 | case 0x0A: //HopPeriod |
rba90 | 9:e13e02aa4a2f | 277 | { |
rba90 | 9:e13e02aa4a2f | 278 | if (isSet) { |
rba90 | 9:e13e02aa4a2f | 279 | uint8_t HopPeriod = payload[2]; |
rba90 | 9:e13e02aa4a2f | 280 | settings->HopPeriod = HopPeriod; |
rba90 | 9:e13e02aa4a2f | 281 | return 0; |
rba90 | 9:e13e02aa4a2f | 282 | } |
rba90 | 9:e13e02aa4a2f | 283 | else |
rba90 | 9:e13e02aa4a2f | 284 | { |
rba90 | 9:e13e02aa4a2f | 285 | response[0] = (uint8_t) (settings->HopPeriod); |
rba90 | 9:e13e02aa4a2f | 286 | return 0; |
rba90 | 9:e13e02aa4a2f | 287 | } |
rba90 | 9:e13e02aa4a2f | 288 | } |
rba90 | 9:e13e02aa4a2f | 289 | |
rba90 | 9:e13e02aa4a2f | 290 | case 0x0B: //IqInverted |
rba90 | 9:e13e02aa4a2f | 291 | { |
rba90 | 9:e13e02aa4a2f | 292 | if (isSet) { |
rba90 | 9:e13e02aa4a2f | 293 | bool IqInverted = payload[2]; |
rba90 | 9:e13e02aa4a2f | 294 | settings->IqInverted = IqInverted; |
rba90 | 9:e13e02aa4a2f | 295 | return 0; |
rba90 | 9:e13e02aa4a2f | 296 | } |
rba90 | 9:e13e02aa4a2f | 297 | else |
rba90 | 9:e13e02aa4a2f | 298 | { |
rba90 | 9:e13e02aa4a2f | 299 | response[0] = (bool) (settings->IqInverted); |
rba90 | 9:e13e02aa4a2f | 300 | return 0; |
rba90 | 9:e13e02aa4a2f | 301 | } |
rba90 | 9:e13e02aa4a2f | 302 | } |
rba90 | 9:e13e02aa4a2f | 303 | |
rba90 | 9:e13e02aa4a2f | 304 | case 0x0C: //RxContinuous |
rba90 | 9:e13e02aa4a2f | 305 | { |
rba90 | 9:e13e02aa4a2f | 306 | if(isSet) |
rba90 | 9:e13e02aa4a2f | 307 | { |
rba90 | 9:e13e02aa4a2f | 308 | bool RxContinuous = payload[2]; |
rba90 | 9:e13e02aa4a2f | 309 | settings->RxContinuous = RxContinuous; |
rba90 | 9:e13e02aa4a2f | 310 | return 0; |
rba90 | 9:e13e02aa4a2f | 311 | } |
rba90 | 9:e13e02aa4a2f | 312 | else |
rba90 | 9:e13e02aa4a2f | 313 | { |
rba90 | 9:e13e02aa4a2f | 314 | response[0] = (bool) (settings->RxContinuous); |
rba90 | 9:e13e02aa4a2f | 315 | return 0; |
rba90 | 9:e13e02aa4a2f | 316 | } |
rba90 | 9:e13e02aa4a2f | 317 | } |
rba90 | 9:e13e02aa4a2f | 318 | |
rba90 | 9:e13e02aa4a2f | 319 | case 0x0D: //TxTimeout |
rba90 | 9:e13e02aa4a2f | 320 | { |
rba90 | 9:e13e02aa4a2f | 321 | if (isSet) |
rba90 | 9:e13e02aa4a2f | 322 | { |
rba90 | 9:e13e02aa4a2f | 323 | uint32_t TxTimeout = (payload[5]) | |
rba90 | 9:e13e02aa4a2f | 324 | (payload[4] << 8) | |
rba90 | 9:e13e02aa4a2f | 325 | (payload[3] << 16) | |
rba90 | 9:e13e02aa4a2f | 326 | (payload[2] << 24); |
rba90 | 9:e13e02aa4a2f | 327 | settings->TxTimeout = TxTimeout; |
rba90 | 9:e13e02aa4a2f | 328 | return 0; |
rba90 | 9:e13e02aa4a2f | 329 | } |
rba90 | 9:e13e02aa4a2f | 330 | else |
rba90 | 9:e13e02aa4a2f | 331 | { |
rba90 | 9:e13e02aa4a2f | 332 | response[3] = (uint8_t) (settings->TxTimeout); |
rba90 | 9:e13e02aa4a2f | 333 | response[2] = (uint8_t) (settings->TxTimeout >> 8); |
rba90 | 9:e13e02aa4a2f | 334 | response[1] = (uint8_t) (settings->TxTimeout >> 16); |
rba90 | 9:e13e02aa4a2f | 335 | response[0] = (uint8_t) (settings->TxTimeout >> 24); |
rba90 | 9:e13e02aa4a2f | 336 | *response_length = 4; |
rba90 | 9:e13e02aa4a2f | 337 | |
rba90 | 9:e13e02aa4a2f | 338 | return 0; |
rba90 | 9:e13e02aa4a2f | 339 | } |
rba90 | 9:e13e02aa4a2f | 340 | } |
rba90 | 9:e13e02aa4a2f | 341 | |
rba90 | 9:e13e02aa4a2f | 342 | |
rba90 | 9:e13e02aa4a2f | 343 | default: |
rba90 | 9:e13e02aa4a2f | 344 | { |
rba90 | 9:e13e02aa4a2f | 345 | break; |
rba90 | 9:e13e02aa4a2f | 346 | } |
rba90 | 9:e13e02aa4a2f | 347 | |
rba90 | 9:e13e02aa4a2f | 348 | //case |
rba90 | 9:e13e02aa4a2f | 349 | } |
rba90 | 9:e13e02aa4a2f | 350 | |
rba90 | 9:e13e02aa4a2f | 351 | |
rba90 | 9:e13e02aa4a2f | 352 | return 0; |
rba90 | 9:e13e02aa4a2f | 353 | } |
rba90 | 9:e13e02aa4a2f | 354 | |
rba90 | 9:e13e02aa4a2f | 355 | int radioUpdateSettings(uint8_t *payload, uint8_t payload_length, uint8_t *response, uint8_t *response_length) |
rba90 | 9:e13e02aa4a2f | 356 | { |
rba90 | 9:e13e02aa4a2f | 357 | aloha.updateSettings(); |
rba90 | 9:e13e02aa4a2f | 358 | |
rba90 | 9:e13e02aa4a2f | 359 | return 0; |
rba90 | 9:e13e02aa4a2f | 360 | } |
rba90 | 9:e13e02aa4a2f | 361 | |
rba90 | 14:25e836a5a7bf | 362 | /* |
rba90 | 14:25e836a5a7bf | 363 | * Format: |
rba90 | 14:25e836a5a7bf | 364 | * < : start flag |
rba90 | 14:25e836a5a7bf | 365 | * 04 : command |
rba90 | 14:25e836a5a7bf | 366 | * 02 : length (2 bytes in this case) |
rba90 | 14:25e836a5a7bf | 367 | * 00/01 : query for temperature or door sensor |
rba90 | 14:25e836a5a7bf | 368 | * xx : node to query |
rba90 | 14:25e836a5a7bf | 369 | * ff : checksum (not currently in used) |
rba90 | 14:25e836a5a7bf | 370 | * > : end flag |
rba90 | 14:25e836a5a7bf | 371 | */ |
rba90 | 14:25e836a5a7bf | 372 | int querySensors(uint8_t *payload, uint8_t payload_length, uint8_t *response, uint8_t *response_length) |
rba90 | 6:19457108c899 | 373 | { |
rba90 | 14:25e836a5a7bf | 374 | // check with payloads |
rba90 | 14:25e836a5a7bf | 375 | if (payload_length != 2) |
rba90 | 14:25e836a5a7bf | 376 | { |
rba90 | 14:25e836a5a7bf | 377 | sprintf((char *) response, "Wrong Payload Length"); |
rba90 | 14:25e836a5a7bf | 378 | *response_length = 20; |
rba90 | 14:25e836a5a7bf | 379 | return 1; |
rba90 | 14:25e836a5a7bf | 380 | } |
rba90 | 14:25e836a5a7bf | 381 | |
rba90 | 14:25e836a5a7bf | 382 | // decode the user message |
rba90 | 14:25e836a5a7bf | 383 | uint8_t sensor_type = payload[0]; |
rba90 | 14:25e836a5a7bf | 384 | uint8_t node_id = payload[1]; |
rba90 | 6:19457108c899 | 385 | |
rba90 | 14:25e836a5a7bf | 386 | // create a command packet |
rba90 | 14:25e836a5a7bf | 387 | ControlPacket packet; |
rba90 | 14:25e836a5a7bf | 388 | |
rba90 | 14:25e836a5a7bf | 389 | // sequence id is not in used in this case |
rba90 | 14:25e836a5a7bf | 390 | packet.setSequenceID(0x0); |
rba90 | 14:25e836a5a7bf | 391 | |
rba90 | 14:25e836a5a7bf | 392 | // set source id as current device id |
rba90 | 14:25e836a5a7bf | 393 | packet.setSourceID(aloha.getDeviceID()); |
rba90 | 14:25e836a5a7bf | 394 | |
rba90 | 14:25e836a5a7bf | 395 | // set destination id as node id |
rba90 | 14:25e836a5a7bf | 396 | packet.setDestinationID(node_id); |
rba90 | 6:19457108c899 | 397 | |
rba90 | 14:25e836a5a7bf | 398 | // set command as query (00 in this case) |
rba90 | 14:25e836a5a7bf | 399 | packet.setCommand(0x00); |
rba90 | 6:19457108c899 | 400 | |
rba90 | 14:25e836a5a7bf | 401 | // store sensor type in data block 0 |
rba90 | 14:25e836a5a7bf | 402 | packet.setData(0, sensor_type); |
rba90 | 14:25e836a5a7bf | 403 | |
rba90 | 14:25e836a5a7bf | 404 | // generate crc |
rba90 | 14:25e836a5a7bf | 405 | packet.generateCrc(); |
rba90 | 14:25e836a5a7bf | 406 | |
rba90 | 14:25e836a5a7bf | 407 | // create buffer for transmission |
rba90 | 14:25e836a5a7bf | 408 | uint8_t buffer[8]; |
rba90 | 6:19457108c899 | 409 | memset(buffer, 0x0, sizeof(buffer)); |
rba90 | 6:19457108c899 | 410 | |
rba90 | 14:25e836a5a7bf | 411 | // copy bytes into buffer |
rba90 | 14:25e836a5a7bf | 412 | packet.serialize(buffer); |
rba90 | 14:25e836a5a7bf | 413 | |
rba90 | 14:25e836a5a7bf | 414 | // send to aloha transceiver |
rba90 | 14:25e836a5a7bf | 415 | aloha.send(buffer, 8, node_id); |
rba90 | 11:9590f3c9d915 | 416 | |
rba90 | 11:9590f3c9d915 | 417 | return 0; |
rba90 | 11:9590f3c9d915 | 418 | } |
rba90 | 11:9590f3c9d915 | 419 | |
rba90 | 14:25e836a5a7bf | 420 | void AlohaDataPacketHandler(uint8_t *payload, uint8_t payload_length, uint8_t src_addr) |
rba90 | 14:25e836a5a7bf | 421 | { |
rba90 | 14:25e836a5a7bf | 422 | // try to decode packet |
rba90 | 14:25e836a5a7bf | 423 | BasicPacket packet(payload); |
rba90 | 2:ae85337d4ca7 | 424 | |
rba90 | 14:25e836a5a7bf | 425 | // verify crc |
rba90 | 15:182b95c25cf2 | 426 | // skip this for now |
rba90 | 14:25e836a5a7bf | 427 | |
rba90 | 14:25e836a5a7bf | 428 | // process the packet based on different feature id |
rba90 | 14:25e836a5a7bf | 429 | BasicPacket::L3Fid_t fid = (BasicPacket::L3Fid_t) packet.getFid(); |
rba90 | 2:ae85337d4ca7 | 430 | |
rba90 | 14:25e836a5a7bf | 431 | // we don't care about the type conversion. just create a new one. |
rba90 | 14:25e836a5a7bf | 432 | switch (fid) |
rba90 | 3:a4f6abad9378 | 433 | { |
rba90 | 14:25e836a5a7bf | 434 | case BasicPacket::L3ControlPacket: |
rba90 | 14:25e836a5a7bf | 435 | { |
rba90 | 14:25e836a5a7bf | 436 | ControlPacket controlPacket(payload); |
rba90 | 14:25e836a5a7bf | 437 | |
rba90 | 14:25e836a5a7bf | 438 | // currently the base station do not respond to any control packet |
rba90 | 14:25e836a5a7bf | 439 | |
rba90 | 14:25e836a5a7bf | 440 | break; |
rba90 | 14:25e836a5a7bf | 441 | } |
rba90 | 14:25e836a5a7bf | 442 | case BasicPacket::L3DataBlockPacket: |
rba90 | 14:25e836a5a7bf | 443 | { |
rba90 | 14:25e836a5a7bf | 444 | DataBlockPacket dataBlockPacket(payload); |
rba90 | 14:25e836a5a7bf | 445 | |
rba90 | 15:182b95c25cf2 | 446 | uint8_t sensor_type = dataBlockPacket.getSourceType(); |
rba90 | 15:182b95c25cf2 | 447 | switch(sensor_type) |
rba90 | 15:182b95c25cf2 | 448 | { |
rba90 | 15:182b95c25cf2 | 449 | case 0x0: // temperature sensor |
rba90 | 15:182b95c25cf2 | 450 | { |
rba90 | 15:182b95c25cf2 | 451 | uint8_t sensor_data = dataBlockPacket.getData(0); |
rba90 | 15:182b95c25cf2 | 452 | |
rba90 | 15:182b95c25cf2 | 453 | char serial_resp[32]; |
rba90 | 15:182b95c25cf2 | 454 | memset(serial_resp, 0x0, sizeof(serial_resp)); |
rba90 | 15:182b95c25cf2 | 455 | sprintf(serial_resp, "<f00300%02x%02xff>", src_addr, sensor_data); |
rba90 | 15:182b95c25cf2 | 456 | printf("expected: %s\r\n", serial_resp); |
rba90 | 15:182b95c25cf2 | 457 | break; |
rba90 | 15:182b95c25cf2 | 458 | } |
rba90 | 15:182b95c25cf2 | 459 | case 0x1: // temperature sensor |
rba90 | 15:182b95c25cf2 | 460 | { |
rba90 | 15:182b95c25cf2 | 461 | break; |
rba90 | 15:182b95c25cf2 | 462 | } |
rba90 | 15:182b95c25cf2 | 463 | default: |
rba90 | 15:182b95c25cf2 | 464 | break; |
rba90 | 15:182b95c25cf2 | 465 | } |
rba90 | 15:182b95c25cf2 | 466 | |
rba90 | 14:25e836a5a7bf | 467 | // do something |
rba90 | 14:25e836a5a7bf | 468 | break; |
rba90 | 14:25e836a5a7bf | 469 | } |
rba90 | 14:25e836a5a7bf | 470 | default: |
rba90 | 14:25e836a5a7bf | 471 | break; |
rba90 | 3:a4f6abad9378 | 472 | } |
rba90 | 0:1e42d79b42e1 | 473 | } |
rba90 | 0:1e42d79b42e1 | 474 | |
rba90 | 14:25e836a5a7bf | 475 | |
rba90 | 0:1e42d79b42e1 | 476 | int main() { |
rba90 | 0:1e42d79b42e1 | 477 | // initialize radio module |
rba90 | 5:445f35444a6a | 478 | aloha.boardInit(); |
rba90 | 6:19457108c899 | 479 | aloha.updateSettings(); |
rba90 | 6:19457108c899 | 480 | aloha.enable(); |
rba90 | 0:1e42d79b42e1 | 481 | |
rba90 | 0:1e42d79b42e1 | 482 | // attach serial interrupt handler |
rba90 | 0:1e42d79b42e1 | 483 | pc.attach(&serialInterruptHandler); |
rba90 | 0:1e42d79b42e1 | 484 | |
rba90 | 0:1e42d79b42e1 | 485 | // register callback functions for SIP |
rba90 | 0:1e42d79b42e1 | 486 | SIP.registerCommand(0x00, toggleChecksum); |
rba90 | 15:182b95c25cf2 | 487 | SIP.registerCommand(0x02, NULL); // send message is not available for the base station |
rba90 | 9:e13e02aa4a2f | 488 | SIP.registerCommand(0x02, configureRadio); |
rba90 | 9:e13e02aa4a2f | 489 | SIP.registerCommand(0x03, radioUpdateSettings); |
rba90 | 14:25e836a5a7bf | 490 | SIP.registerCommand(0x04, querySensors); |
rba90 | 0:1e42d79b42e1 | 491 | |
rba90 | 0:1e42d79b42e1 | 492 | // register callback functions for aloha transceiver |
rba90 | 14:25e836a5a7bf | 493 | aloha.registerType(AlohaFrame::Aloha_Data, AlohaDataPacketHandler); |
rba90 | 0:1e42d79b42e1 | 494 | |
rba90 | 0:1e42d79b42e1 | 495 | while(1) { |
rba90 | 0:1e42d79b42e1 | 496 | SIP.poll(); |
rba90 | 0:1e42d79b42e1 | 497 | aloha.poll(); |
rba90 | 0:1e42d79b42e1 | 498 | |
rba90 | 0:1e42d79b42e1 | 499 | while (SerialOutputBuffer.getCounter() > 0) |
rba90 | 0:1e42d79b42e1 | 500 | { |
rba90 | 0:1e42d79b42e1 | 501 | uint8_t ch; |
rba90 | 0:1e42d79b42e1 | 502 | ch = SerialOutputBuffer.dequeue(); |
rba90 | 0:1e42d79b42e1 | 503 | pc.putc(ch); |
rba90 | 0:1e42d79b42e1 | 504 | } |
rba90 | 0:1e42d79b42e1 | 505 | } |
rba90 | 0:1e42d79b42e1 | 506 | } |