Wireless interface using LoRa technology

Dependencies:   AlohaTransceiver RingBuffer SX1276Lib SerialInterfaceProtocol mbed L3PDU

Committer:
rba90
Date:
Wed Aug 31 03:38:21 2016 +0000
Revision:
23:378bd4d025d0
Parent:
21:0f6635739f66
Child:
24:2136cf94fedb
treat service quality query as normal query

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rba90 0:1e42d79b42e1 1 #include "mbed.h"
rba90 0:1e42d79b42e1 2 #include "AlohaTransceiver.h"
rba90 16:ab2d9341997a 3 #include "CommandPacket.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 19:6a59fb0ee921 20 // convert fp32 to 4 byte string
rba90 19:6a59fb0ee921 21 typedef union
rba90 19:6a59fb0ee921 22 {
rba90 19:6a59fb0ee921 23 float fp32;
rba90 19:6a59fb0ee921 24 uint8_t bytes[4];
rba90 19:6a59fb0ee921 25 } float_bytes_32;
rba90 19:6a59fb0ee921 26
rba90 19:6a59fb0ee921 27 typedef union
rba90 19:6a59fb0ee921 28 {
rba90 19:6a59fb0ee921 29 int16_t int16;
rba90 19:6a59fb0ee921 30 uint8_t bytes[2];
rba90 19:6a59fb0ee921 31 } int_string_16;
rba90 19:6a59fb0ee921 32
rba90 19:6a59fb0ee921 33
rba90 0:1e42d79b42e1 34 void serialInterruptHandler() {
rba90 0:1e42d79b42e1 35 // Note: you need to actually read from the serial to clear the RX interrupt
rba90 0:1e42d79b42e1 36 int c = pc.getc();
rba90 0:1e42d79b42e1 37
rba90 0:1e42d79b42e1 38 // add to buffer
rba90 0:1e42d79b42e1 39 if (SerialInputBuffer.isLocked())
rba90 0:1e42d79b42e1 40 {
rba90 0:1e42d79b42e1 41 printf("Mutex Locked\r\n");
rba90 0:1e42d79b42e1 42 }
rba90 0:1e42d79b42e1 43 else
rba90 0:1e42d79b42e1 44 {
rba90 0:1e42d79b42e1 45 SerialInputBuffer.enqueue((uint8_t) c);
rba90 0:1e42d79b42e1 46 }
rba90 0:1e42d79b42e1 47 }
rba90 0:1e42d79b42e1 48
rba90 0:1e42d79b42e1 49 int toggleChecksum(uint8_t *payload, uint8_t payload_length, uint8_t *response, uint8_t *response_length)
rba90 0:1e42d79b42e1 50 {
rba90 0:1e42d79b42e1 51 // one payload
rba90 0:1e42d79b42e1 52 if (payload_length != 1)
rba90 0:1e42d79b42e1 53 {
rba90 0:1e42d79b42e1 54 sprintf((char *) response, "Wrong Payload Length\r\n");
rba90 0:1e42d79b42e1 55 *response_length = 22;
rba90 0:1e42d79b42e1 56 return 1;
rba90 0:1e42d79b42e1 57 }
rba90 0:1e42d79b42e1 58
rba90 0:1e42d79b42e1 59 if ((bool) payload[0])
rba90 0:1e42d79b42e1 60 {
rba90 0:1e42d79b42e1 61 SIP.enableChecksum();
rba90 0:1e42d79b42e1 62 }
rba90 0:1e42d79b42e1 63 else
rba90 0:1e42d79b42e1 64 {
rba90 0:1e42d79b42e1 65 SIP.disableChecksum();
rba90 0:1e42d79b42e1 66 }
rba90 0:1e42d79b42e1 67
rba90 0:1e42d79b42e1 68 return 0;
rba90 0:1e42d79b42e1 69 }
rba90 0:1e42d79b42e1 70
rba90 9:e13e02aa4a2f 71 /*
rba90 9:e13e02aa4a2f 72 * Format:
rba90 14:25e836a5a7bf 73 * < : start flag
rba90 14:25e836a5a7bf 74 * 02 : command
rba90 14:25e836a5a7bf 75 * xx : length
rba90 14:25e836a5a7bf 76 * xx: : 00: get, 01: set
rba90 14:25e836a5a7bf 77 * xx : index for parameters
rba90 9:e13e02aa4a2f 78 * ...
rba90 14:25e836a5a7bf 79 * ff :checksum (not currently in used)
rba90 9:e13e02aa4a2f 80 * > :end flag
rba90 9:e13e02aa4a2f 81 */
rba90 9:e13e02aa4a2f 82 int configureRadio(uint8_t *payload, uint8_t payload_length, uint8_t *response, uint8_t *response_length)
rba90 9:e13e02aa4a2f 83 {
rba90 9:e13e02aa4a2f 84 // read settings from radio
rba90 9:e13e02aa4a2f 85 #if USE_MODEM_LORA == 1
rba90 9:e13e02aa4a2f 86 AlohaTransceiver::LoRaSettings_t *settings = aloha.getSettings();
rba90 9:e13e02aa4a2f 87 #elif USE_MODEM_FSK == 1
rba90 9:e13e02aa4a2f 88 AlohaTransceiver::FskSettings_t *settings = aloha.getSettings();
rba90 9:e13e02aa4a2f 89 #else
rba90 9:e13e02aa4a2f 90 #error "Please define a modem in the compiler options."
rba90 9:e13e02aa4a2f 91 #endif
rba90 9:e13e02aa4a2f 92
rba90 9:e13e02aa4a2f 93 if (payload_length < 2)
rba90 9:e13e02aa4a2f 94 {
rba90 9:e13e02aa4a2f 95 sprintf((char *) response, "Wrong Payload Length\r\n");
rba90 9:e13e02aa4a2f 96 *response_length = 22;
rba90 9:e13e02aa4a2f 97 return 1;
rba90 9:e13e02aa4a2f 98 }
rba90 9:e13e02aa4a2f 99
rba90 9:e13e02aa4a2f 100 // true is set, false is get
rba90 9:e13e02aa4a2f 101 bool isSet = (bool) payload[0];
rba90 9:e13e02aa4a2f 102 uint8_t idx = payload[1];
rba90 9:e13e02aa4a2f 103
rba90 9:e13e02aa4a2f 104 switch(idx)
rba90 9:e13e02aa4a2f 105 {
rba90 9:e13e02aa4a2f 106 case 0x00: // Power
rba90 9:e13e02aa4a2f 107 {
rba90 9:e13e02aa4a2f 108 if (isSet)
rba90 9:e13e02aa4a2f 109 {
rba90 9:e13e02aa4a2f 110 int8_t Power = (int8_t) payload[2];
rba90 9:e13e02aa4a2f 111 settings->Power = Power;
rba90 9:e13e02aa4a2f 112
rba90 9:e13e02aa4a2f 113 return 0;
rba90 9:e13e02aa4a2f 114 }
rba90 9:e13e02aa4a2f 115 else
rba90 9:e13e02aa4a2f 116 {
rba90 9:e13e02aa4a2f 117 response[0] = (uint8_t) settings->Power;
rba90 9:e13e02aa4a2f 118 *response_length = 1;
rba90 9:e13e02aa4a2f 119
rba90 9:e13e02aa4a2f 120 return 0;
rba90 9:e13e02aa4a2f 121 }
rba90 9:e13e02aa4a2f 122 }
rba90 9:e13e02aa4a2f 123
rba90 9:e13e02aa4a2f 124 case 0x01: // Bandwidth
rba90 9:e13e02aa4a2f 125 {
rba90 9:e13e02aa4a2f 126 if (isSet)
rba90 9:e13e02aa4a2f 127 {
rba90 9:e13e02aa4a2f 128 uint32_t Bandwidth = (payload[5]) |
rba90 9:e13e02aa4a2f 129 (payload[4] << 8) |
rba90 9:e13e02aa4a2f 130 (payload[3] << 16) |
rba90 9:e13e02aa4a2f 131 (payload[2] << 24);
rba90 9:e13e02aa4a2f 132 settings->Bandwidth = Bandwidth;
rba90 9:e13e02aa4a2f 133
rba90 9:e13e02aa4a2f 134 return 0;
rba90 9:e13e02aa4a2f 135 }
rba90 9:e13e02aa4a2f 136 else
rba90 9:e13e02aa4a2f 137 {
rba90 9:e13e02aa4a2f 138 response[3] = (uint8_t) (settings->Bandwidth);
rba90 9:e13e02aa4a2f 139 response[2] = (uint8_t) (settings->Bandwidth >> 8);
rba90 9:e13e02aa4a2f 140 response[1] = (uint8_t) (settings->Bandwidth >> 16);
rba90 9:e13e02aa4a2f 141 response[0] = (uint8_t) (settings->Bandwidth >> 24);
rba90 9:e13e02aa4a2f 142 *response_length = 4;
rba90 9:e13e02aa4a2f 143
rba90 9:e13e02aa4a2f 144 return 0;
rba90 9:e13e02aa4a2f 145 }
rba90 9:e13e02aa4a2f 146
rba90 9:e13e02aa4a2f 147 }
rba90 9:e13e02aa4a2f 148
rba90 9:e13e02aa4a2f 149 case 0x02: // Datarate, AKA Spreading Factor
rba90 9:e13e02aa4a2f 150 {
rba90 9:e13e02aa4a2f 151 if (isSet)
rba90 9:e13e02aa4a2f 152 {
rba90 9:e13e02aa4a2f 153 uint32_t Datarate = (payload[5]) |
rba90 9:e13e02aa4a2f 154 (payload[4] << 8) |
rba90 9:e13e02aa4a2f 155 (payload[3] << 16) |
rba90 9:e13e02aa4a2f 156 (payload[2] << 24);
rba90 9:e13e02aa4a2f 157 settings->Datarate = Datarate;
rba90 9:e13e02aa4a2f 158
rba90 9:e13e02aa4a2f 159 return 0;
rba90 9:e13e02aa4a2f 160 }
rba90 9:e13e02aa4a2f 161 else
rba90 9:e13e02aa4a2f 162 {
rba90 9:e13e02aa4a2f 163 response[3] = (uint8_t) (settings->Datarate);
rba90 9:e13e02aa4a2f 164 response[2] = (uint8_t) (settings->Datarate >> 8);
rba90 9:e13e02aa4a2f 165 response[1] = (uint8_t) (settings->Datarate >> 16);
rba90 9:e13e02aa4a2f 166 response[0] = (uint8_t) (settings->Datarate >> 24);
rba90 9:e13e02aa4a2f 167 *response_length = 4;
rba90 9:e13e02aa4a2f 168
rba90 9:e13e02aa4a2f 169 return 0;
rba90 9:e13e02aa4a2f 170 }
rba90 9:e13e02aa4a2f 171 }
rba90 9:e13e02aa4a2f 172
rba90 9:e13e02aa4a2f 173 case 0x03: // Coderate
rba90 9:e13e02aa4a2f 174 {
rba90 9:e13e02aa4a2f 175 if (isSet)
rba90 9:e13e02aa4a2f 176 {
rba90 9:e13e02aa4a2f 177 uint8_t Coderate = payload[2];
rba90 9:e13e02aa4a2f 178 settings->Coderate = Coderate;
rba90 9:e13e02aa4a2f 179
rba90 9:e13e02aa4a2f 180 return 0;
rba90 9:e13e02aa4a2f 181 }
rba90 9:e13e02aa4a2f 182 else
rba90 9:e13e02aa4a2f 183 {
rba90 9:e13e02aa4a2f 184 response[0] = (uint8_t) settings->Coderate;
rba90 9:e13e02aa4a2f 185 *response_length = 1;
rba90 9:e13e02aa4a2f 186
rba90 9:e13e02aa4a2f 187 return 0;
rba90 9:e13e02aa4a2f 188 }
rba90 9:e13e02aa4a2f 189 }
rba90 9:e13e02aa4a2f 190
rba90 9:e13e02aa4a2f 191 case 0x04: //Preamble Length
rba90 9:e13e02aa4a2f 192 {
rba90 9:e13e02aa4a2f 193 if (isSet)
rba90 9:e13e02aa4a2f 194 {
rba90 9:e13e02aa4a2f 195 uint16_t PreambleLen = payload[3] | (payload[2] << 8);
rba90 9:e13e02aa4a2f 196 settings->PreambleLen = PreambleLen;
rba90 9:e13e02aa4a2f 197 return 0;
rba90 9:e13e02aa4a2f 198 }
rba90 9:e13e02aa4a2f 199 else
rba90 9:e13e02aa4a2f 200 {
rba90 9:e13e02aa4a2f 201 response[1] = (uint8_t) (settings->PreambleLen);
rba90 9:e13e02aa4a2f 202 response[0] = (uint8_t)(settings->PreambleLen >> 8);
rba90 9:e13e02aa4a2f 203 *response_length = 2;
rba90 9:e13e02aa4a2f 204
rba90 9:e13e02aa4a2f 205 return 0;
rba90 9:e13e02aa4a2f 206 }
rba90 9:e13e02aa4a2f 207 }
rba90 9:e13e02aa4a2f 208
rba90 9:e13e02aa4a2f 209 case 0x05: //Symbol Timeout
rba90 9:e13e02aa4a2f 210 {
rba90 9:e13e02aa4a2f 211 if (isSet)
rba90 9:e13e02aa4a2f 212 {
rba90 9:e13e02aa4a2f 213 uint16_t SymbolTimeout = payload[3] | (payload[2] << 8);
rba90 9:e13e02aa4a2f 214 settings->SymbolTimeout = SymbolTimeout;
rba90 9:e13e02aa4a2f 215 return 0;
rba90 9:e13e02aa4a2f 216 }
rba90 9:e13e02aa4a2f 217 else
rba90 9:e13e02aa4a2f 218 {
rba90 9:e13e02aa4a2f 219 response[1] = (uint8_t) (settings->SymbolTimeout);
rba90 9:e13e02aa4a2f 220 response[0] = (uint8_t) (settings->SymbolTimeout >> 8);
rba90 9:e13e02aa4a2f 221 *response_length = 2;
rba90 9:e13e02aa4a2f 222
rba90 9:e13e02aa4a2f 223 return 0;
rba90 9:e13e02aa4a2f 224 }
rba90 9:e13e02aa4a2f 225 }
rba90 9:e13e02aa4a2f 226
rba90 9:e13e02aa4a2f 227 case 0x06: //FixLen
rba90 9:e13e02aa4a2f 228 {
rba90 9:e13e02aa4a2f 229 if (isSet)
rba90 9:e13e02aa4a2f 230 {
rba90 9:e13e02aa4a2f 231 bool FixLen = payload[2];
rba90 9:e13e02aa4a2f 232 settings->FixLen = FixLen;
rba90 9:e13e02aa4a2f 233 return 0;
rba90 9:e13e02aa4a2f 234 }
rba90 9:e13e02aa4a2f 235 else
rba90 9:e13e02aa4a2f 236 {
rba90 9:e13e02aa4a2f 237 response[0] = (bool) (settings->SymbolTimeout);
rba90 9:e13e02aa4a2f 238 *response_length = 1;
rba90 9:e13e02aa4a2f 239
rba90 9:e13e02aa4a2f 240 return 0;
rba90 9:e13e02aa4a2f 241 }
rba90 9:e13e02aa4a2f 242 }
rba90 9:e13e02aa4a2f 243
rba90 9:e13e02aa4a2f 244 case 0x07: //PayloadLen
rba90 9:e13e02aa4a2f 245 {
rba90 9:e13e02aa4a2f 246 if (isSet)
rba90 9:e13e02aa4a2f 247 {
rba90 9:e13e02aa4a2f 248 uint8_t PayloadLen = payload[2];
rba90 9:e13e02aa4a2f 249 settings->PayloadLen = PayloadLen;
rba90 9:e13e02aa4a2f 250 return 0;
rba90 9:e13e02aa4a2f 251 }
rba90 9:e13e02aa4a2f 252 else
rba90 9:e13e02aa4a2f 253 {
rba90 9:e13e02aa4a2f 254 response[0] = (uint8_t) (settings->PayloadLen);
rba90 9:e13e02aa4a2f 255
rba90 9:e13e02aa4a2f 256 return 0;
rba90 9:e13e02aa4a2f 257 }
rba90 9:e13e02aa4a2f 258 }
rba90 9:e13e02aa4a2f 259
rba90 9:e13e02aa4a2f 260 case 0x08: //CrcOn
rba90 9:e13e02aa4a2f 261 {
rba90 9:e13e02aa4a2f 262 if (isSet) {
rba90 9:e13e02aa4a2f 263 bool CrcOn = payload[2];
rba90 9:e13e02aa4a2f 264 settings->CrcOn = CrcOn;
rba90 9:e13e02aa4a2f 265 return 0;
rba90 9:e13e02aa4a2f 266 }
rba90 9:e13e02aa4a2f 267 else
rba90 9:e13e02aa4a2f 268 {
rba90 9:e13e02aa4a2f 269 response[0] = (bool) (settings->CrcOn);
rba90 9:e13e02aa4a2f 270
rba90 9:e13e02aa4a2f 271 return 0;
rba90 9:e13e02aa4a2f 272 }
rba90 9:e13e02aa4a2f 273 }
rba90 9:e13e02aa4a2f 274
rba90 9:e13e02aa4a2f 275 case 0x09: //FreqHopOn
rba90 9:e13e02aa4a2f 276 {
rba90 9:e13e02aa4a2f 277 if (isSet) {
rba90 9:e13e02aa4a2f 278 bool FreqHopOn = payload[2];
rba90 9:e13e02aa4a2f 279 settings->FreqHopOn = FreqHopOn;
rba90 9:e13e02aa4a2f 280 return 0;
rba90 9:e13e02aa4a2f 281 }
rba90 9:e13e02aa4a2f 282 else
rba90 9:e13e02aa4a2f 283 {
rba90 9:e13e02aa4a2f 284 response[0] = (bool) (settings->FreqHopOn);
rba90 9:e13e02aa4a2f 285
rba90 9:e13e02aa4a2f 286 return 0;
rba90 9:e13e02aa4a2f 287 }
rba90 9:e13e02aa4a2f 288 }
rba90 9:e13e02aa4a2f 289
rba90 9:e13e02aa4a2f 290 case 0x0A: //HopPeriod
rba90 9:e13e02aa4a2f 291 {
rba90 9:e13e02aa4a2f 292 if (isSet) {
rba90 9:e13e02aa4a2f 293 uint8_t HopPeriod = payload[2];
rba90 9:e13e02aa4a2f 294 settings->HopPeriod = HopPeriod;
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] = (uint8_t) (settings->HopPeriod);
rba90 9:e13e02aa4a2f 300 return 0;
rba90 9:e13e02aa4a2f 301 }
rba90 9:e13e02aa4a2f 302 }
rba90 9:e13e02aa4a2f 303
rba90 9:e13e02aa4a2f 304 case 0x0B: //IqInverted
rba90 9:e13e02aa4a2f 305 {
rba90 9:e13e02aa4a2f 306 if (isSet) {
rba90 9:e13e02aa4a2f 307 bool IqInverted = payload[2];
rba90 9:e13e02aa4a2f 308 settings->IqInverted = IqInverted;
rba90 9:e13e02aa4a2f 309 return 0;
rba90 9:e13e02aa4a2f 310 }
rba90 9:e13e02aa4a2f 311 else
rba90 9:e13e02aa4a2f 312 {
rba90 9:e13e02aa4a2f 313 response[0] = (bool) (settings->IqInverted);
rba90 9:e13e02aa4a2f 314 return 0;
rba90 9:e13e02aa4a2f 315 }
rba90 9:e13e02aa4a2f 316 }
rba90 9:e13e02aa4a2f 317
rba90 9:e13e02aa4a2f 318 case 0x0C: //RxContinuous
rba90 9:e13e02aa4a2f 319 {
rba90 9:e13e02aa4a2f 320 if(isSet)
rba90 9:e13e02aa4a2f 321 {
rba90 9:e13e02aa4a2f 322 bool RxContinuous = payload[2];
rba90 9:e13e02aa4a2f 323 settings->RxContinuous = RxContinuous;
rba90 9:e13e02aa4a2f 324 return 0;
rba90 9:e13e02aa4a2f 325 }
rba90 9:e13e02aa4a2f 326 else
rba90 9:e13e02aa4a2f 327 {
rba90 9:e13e02aa4a2f 328 response[0] = (bool) (settings->RxContinuous);
rba90 9:e13e02aa4a2f 329 return 0;
rba90 9:e13e02aa4a2f 330 }
rba90 9:e13e02aa4a2f 331 }
rba90 9:e13e02aa4a2f 332
rba90 9:e13e02aa4a2f 333 case 0x0D: //TxTimeout
rba90 9:e13e02aa4a2f 334 {
rba90 9:e13e02aa4a2f 335 if (isSet)
rba90 9:e13e02aa4a2f 336 {
rba90 9:e13e02aa4a2f 337 uint32_t TxTimeout = (payload[5]) |
rba90 9:e13e02aa4a2f 338 (payload[4] << 8) |
rba90 9:e13e02aa4a2f 339 (payload[3] << 16) |
rba90 9:e13e02aa4a2f 340 (payload[2] << 24);
rba90 9:e13e02aa4a2f 341 settings->TxTimeout = TxTimeout;
rba90 9:e13e02aa4a2f 342 return 0;
rba90 9:e13e02aa4a2f 343 }
rba90 9:e13e02aa4a2f 344 else
rba90 9:e13e02aa4a2f 345 {
rba90 9:e13e02aa4a2f 346 response[3] = (uint8_t) (settings->TxTimeout);
rba90 9:e13e02aa4a2f 347 response[2] = (uint8_t) (settings->TxTimeout >> 8);
rba90 9:e13e02aa4a2f 348 response[1] = (uint8_t) (settings->TxTimeout >> 16);
rba90 9:e13e02aa4a2f 349 response[0] = (uint8_t) (settings->TxTimeout >> 24);
rba90 9:e13e02aa4a2f 350 *response_length = 4;
rba90 9:e13e02aa4a2f 351
rba90 9:e13e02aa4a2f 352 return 0;
rba90 9:e13e02aa4a2f 353 }
rba90 9:e13e02aa4a2f 354 }
rba90 9:e13e02aa4a2f 355
rba90 9:e13e02aa4a2f 356
rba90 9:e13e02aa4a2f 357 default:
rba90 9:e13e02aa4a2f 358 {
rba90 9:e13e02aa4a2f 359 break;
rba90 9:e13e02aa4a2f 360 }
rba90 9:e13e02aa4a2f 361
rba90 9:e13e02aa4a2f 362 //case
rba90 9:e13e02aa4a2f 363 }
rba90 9:e13e02aa4a2f 364
rba90 9:e13e02aa4a2f 365
rba90 9:e13e02aa4a2f 366 return 0;
rba90 9:e13e02aa4a2f 367 }
rba90 9:e13e02aa4a2f 368
rba90 9:e13e02aa4a2f 369 int radioUpdateSettings(uint8_t *payload, uint8_t payload_length, uint8_t *response, uint8_t *response_length)
rba90 9:e13e02aa4a2f 370 {
rba90 9:e13e02aa4a2f 371 aloha.updateSettings();
rba90 9:e13e02aa4a2f 372
rba90 9:e13e02aa4a2f 373 return 0;
rba90 9:e13e02aa4a2f 374 }
rba90 9:e13e02aa4a2f 375
rba90 14:25e836a5a7bf 376 /*
rba90 14:25e836a5a7bf 377 * Format:
rba90 14:25e836a5a7bf 378 * < : start flag
rba90 14:25e836a5a7bf 379 * 04 : command
rba90 14:25e836a5a7bf 380 * 02 : length (2 bytes in this case)
rba90 14:25e836a5a7bf 381 * 00/01 : query for temperature or door sensor
rba90 14:25e836a5a7bf 382 * xx : node to query
rba90 14:25e836a5a7bf 383 * ff : checksum (not currently in used)
rba90 14:25e836a5a7bf 384 * > : end flag
rba90 14:25e836a5a7bf 385 */
rba90 14:25e836a5a7bf 386 int querySensors(uint8_t *payload, uint8_t payload_length, uint8_t *response, uint8_t *response_length)
rba90 6:19457108c899 387 {
rba90 14:25e836a5a7bf 388 // check with payloads
rba90 14:25e836a5a7bf 389 if (payload_length != 2)
rba90 14:25e836a5a7bf 390 {
rba90 14:25e836a5a7bf 391 sprintf((char *) response, "Wrong Payload Length");
rba90 14:25e836a5a7bf 392 *response_length = 20;
rba90 14:25e836a5a7bf 393 return 1;
rba90 14:25e836a5a7bf 394 }
rba90 14:25e836a5a7bf 395
rba90 14:25e836a5a7bf 396 // decode the user message
rba90 23:378bd4d025d0 397 uint8_t source_type = payload[0];
rba90 14:25e836a5a7bf 398 uint8_t node_id = payload[1];
rba90 6:19457108c899 399
rba90 14:25e836a5a7bf 400 // create a command packet
rba90 14:25e836a5a7bf 401 ControlPacket packet;
rba90 14:25e836a5a7bf 402
rba90 14:25e836a5a7bf 403 // sequence id is not in used in this case
rba90 14:25e836a5a7bf 404 packet.setSequenceID(0x0);
rba90 14:25e836a5a7bf 405
rba90 14:25e836a5a7bf 406 // set source id as current device id
rba90 14:25e836a5a7bf 407 packet.setSourceID(aloha.getDeviceID());
rba90 14:25e836a5a7bf 408
rba90 14:25e836a5a7bf 409 // set destination id as node id
rba90 14:25e836a5a7bf 410 packet.setDestinationID(node_id);
rba90 6:19457108c899 411
rba90 19:6a59fb0ee921 412 // set command as query (0 in this case)
rba90 19:6a59fb0ee921 413 packet.setCommand(0x0);
rba90 6:19457108c899 414
rba90 14:25e836a5a7bf 415 // store sensor type in data block 0
rba90 23:378bd4d025d0 416 packet.setData(0, source_type);
rba90 14:25e836a5a7bf 417
rba90 14:25e836a5a7bf 418 // generate crc
rba90 14:25e836a5a7bf 419 packet.generateCrc();
rba90 14:25e836a5a7bf 420
rba90 14:25e836a5a7bf 421 // create buffer for transmission
rba90 14:25e836a5a7bf 422 uint8_t buffer[8];
rba90 6:19457108c899 423 memset(buffer, 0x0, sizeof(buffer));
rba90 6:19457108c899 424
rba90 14:25e836a5a7bf 425 // copy bytes into buffer
rba90 14:25e836a5a7bf 426 packet.serialize(buffer);
rba90 14:25e836a5a7bf 427
rba90 14:25e836a5a7bf 428 // send to aloha transceiver
rba90 14:25e836a5a7bf 429 aloha.send(buffer, 8, node_id);
rba90 11:9590f3c9d915 430
rba90 11:9590f3c9d915 431 return 0;
rba90 11:9590f3c9d915 432 }
rba90 11:9590f3c9d915 433
rba90 19:6a59fb0ee921 434 int queryServiceQuality(uint8_t *payload, uint8_t payload_length, uint8_t *response, uint8_t *response_length)
rba90 19:6a59fb0ee921 435 {
rba90 19:6a59fb0ee921 436 // decode user message
rba90 19:6a59fb0ee921 437 uint8_t node_id = payload[0];
rba90 23:378bd4d025d0 438 uint8_t source_type = 0x2; // query for service quality
rba90 19:6a59fb0ee921 439
rba90 19:6a59fb0ee921 440 // create a command packet
rba90 19:6a59fb0ee921 441 ControlPacket packet;
rba90 19:6a59fb0ee921 442
rba90 19:6a59fb0ee921 443 // layer 3 sequence id is not used in this case
rba90 19:6a59fb0ee921 444 packet.setSequenceID(0x0);
rba90 19:6a59fb0ee921 445
rba90 19:6a59fb0ee921 446 // set source id as current device id
rba90 19:6a59fb0ee921 447 packet.setSourceID(aloha.getDeviceID());
rba90 19:6a59fb0ee921 448
rba90 19:6a59fb0ee921 449 // set destination id as node id
rba90 19:6a59fb0ee921 450 packet.setDestinationID(node_id);
rba90 19:6a59fb0ee921 451
rba90 23:378bd4d025d0 452 // set command as query (treat it similar to sensor)
rba90 23:378bd4d025d0 453 packet.setCommand(0x0);
rba90 23:378bd4d025d0 454
rba90 23:378bd4d025d0 455 // store sensor type in data block 0
rba90 23:378bd4d025d0 456 packet.setData(0, source_type);
rba90 19:6a59fb0ee921 457
rba90 19:6a59fb0ee921 458 // generate crc
rba90 19:6a59fb0ee921 459 packet.generateCrc();
rba90 19:6a59fb0ee921 460
rba90 19:6a59fb0ee921 461 // create buffer for transmission
rba90 19:6a59fb0ee921 462 uint8_t buffer[8];
rba90 19:6a59fb0ee921 463 memset(buffer, 0x0, sizeof(buffer));
rba90 19:6a59fb0ee921 464
rba90 19:6a59fb0ee921 465 // copy bytes into buffer
rba90 19:6a59fb0ee921 466 packet.serialize(buffer);
rba90 19:6a59fb0ee921 467
rba90 19:6a59fb0ee921 468 // send to aloha transceiver
rba90 19:6a59fb0ee921 469 aloha.send(buffer, 8, node_id);
rba90 19:6a59fb0ee921 470
rba90 19:6a59fb0ee921 471 return 0;
rba90 19:6a59fb0ee921 472 }
rba90 19:6a59fb0ee921 473
rba90 14:25e836a5a7bf 474 void AlohaDataPacketHandler(uint8_t *payload, uint8_t payload_length, uint8_t src_addr)
rba90 14:25e836a5a7bf 475 {
rba90 14:25e836a5a7bf 476 // try to decode packet
rba90 14:25e836a5a7bf 477 BasicPacket packet(payload);
rba90 2:ae85337d4ca7 478
rba90 14:25e836a5a7bf 479 // verify crc
rba90 15:182b95c25cf2 480 // skip this for now
rba90 14:25e836a5a7bf 481
rba90 14:25e836a5a7bf 482 // process the packet based on different feature id
rba90 14:25e836a5a7bf 483 BasicPacket::L3Fid_t fid = (BasicPacket::L3Fid_t) packet.getFid();
rba90 2:ae85337d4ca7 484
rba90 14:25e836a5a7bf 485 // we don't care about the type conversion. just create a new one.
rba90 14:25e836a5a7bf 486 switch (fid)
rba90 3:a4f6abad9378 487 {
rba90 14:25e836a5a7bf 488 case BasicPacket::L3ControlPacket:
rba90 14:25e836a5a7bf 489 {
rba90 14:25e836a5a7bf 490 ControlPacket controlPacket(payload);
rba90 14:25e836a5a7bf 491
rba90 19:6a59fb0ee921 492 // execute command
rba90 19:6a59fb0ee921 493 uint8_t command = controlPacket.getCommand();
rba90 19:6a59fb0ee921 494 switch(command)
rba90 19:6a59fb0ee921 495 {
rba90 23:378bd4d025d0 496 case 0x0: // query
rba90 19:6a59fb0ee921 497 {
rba90 23:378bd4d025d0 498 // execute query
rba90 23:378bd4d025d0 499 uint8_t sensor_type = controlPacket.getData(0);
rba90 23:378bd4d025d0 500 switch (sensor_type)
rba90 23:378bd4d025d0 501 {
rba90 23:378bd4d025d0 502 case 0x0: // temperature: NULL
rba90 23:378bd4d025d0 503 {
rba90 23:378bd4d025d0 504 break;
rba90 23:378bd4d025d0 505 }
rba90 23:378bd4d025d0 506 case 0x1: // door: NULL
rba90 23:378bd4d025d0 507 {
rba90 23:378bd4d025d0 508 break;
rba90 23:378bd4d025d0 509 }
rba90 23:378bd4d025d0 510 case 0x2: // service quality
rba90 23:378bd4d025d0 511 {
rba90 23:378bd4d025d0 512 int_string_16 rssi;
rba90 23:378bd4d025d0 513 uint8_t snr;
rba90 23:378bd4d025d0 514
rba90 23:378bd4d025d0 515 // get rssi and snr
rba90 23:378bd4d025d0 516 rssi.int16 = aloha.getRssi();
rba90 23:378bd4d025d0 517 snr = aloha.getSnr();
rba90 23:378bd4d025d0 518
rba90 23:378bd4d025d0 519 // create a response
rba90 23:378bd4d025d0 520 DataBlockPacket response;
rba90 23:378bd4d025d0 521
rba90 23:378bd4d025d0 522 // set sequence id (the sequence id is configured as incoming sequence id + 1)
rba90 23:378bd4d025d0 523 response.setSequenceID(controlPacket.getSequenceID() + 1);
rba90 23:378bd4d025d0 524
rba90 23:378bd4d025d0 525 // set source id (the source id is the current device ID)
rba90 23:378bd4d025d0 526 response.setSourceID(aloha.getDeviceID());
rba90 23:378bd4d025d0 527
rba90 23:378bd4d025d0 528 // set destination id (the destination ID is the original source address)
rba90 23:378bd4d025d0 529 response.setDestinationID(controlPacket.getSourceID());
rba90 23:378bd4d025d0 530
rba90 23:378bd4d025d0 531 // set source type (service quality)
rba90 23:378bd4d025d0 532 response.setSourceType(0x2);
rba90 23:378bd4d025d0 533
rba90 23:378bd4d025d0 534 // set payload type (byte)
rba90 23:378bd4d025d0 535 response.setPayloadType(0x0);
rba90 23:378bd4d025d0 536
rba90 23:378bd4d025d0 537 // set payload
rba90 23:378bd4d025d0 538 response.setData(0, (uint8_t) snr); // store SNR
rba90 23:378bd4d025d0 539 response.setData(1, (uint8_t) rssi.bytes[0]); // store higher bits of RSSI
rba90 23:378bd4d025d0 540 response.setData(2, (uint8_t) rssi.bytes[1]); // store lower bits of RSSI
rba90 23:378bd4d025d0 541
rba90 23:378bd4d025d0 542 // calculate crc
rba90 23:378bd4d025d0 543 response.generateCrc();
rba90 23:378bd4d025d0 544
rba90 23:378bd4d025d0 545 // serialize and send it
rba90 23:378bd4d025d0 546 uint8_t buffer[8];
rba90 23:378bd4d025d0 547 memset(buffer, 0x0, sizeof(buffer));
rba90 23:378bd4d025d0 548
rba90 23:378bd4d025d0 549 // copy bytes into buffer
rba90 23:378bd4d025d0 550 response.serialize(buffer);
rba90 23:378bd4d025d0 551
rba90 23:378bd4d025d0 552 // send to aloha transceiver
rba90 23:378bd4d025d0 553 aloha.send(buffer, 8, src_addr);
rba90 23:378bd4d025d0 554
rba90 23:378bd4d025d0 555 break;
rba90 23:378bd4d025d0 556 }
rba90 23:378bd4d025d0 557
rba90 23:378bd4d025d0 558 default:
rba90 23:378bd4d025d0 559 break;
rba90 23:378bd4d025d0 560 }
rba90 19:6a59fb0ee921 561
rba90 19:6a59fb0ee921 562 break;
rba90 19:6a59fb0ee921 563 }
rba90 23:378bd4d025d0 564
rba90 19:6a59fb0ee921 565 default:
rba90 19:6a59fb0ee921 566 break;
rba90 19:6a59fb0ee921 567 }
rba90 14:25e836a5a7bf 568
rba90 14:25e836a5a7bf 569 break;
rba90 14:25e836a5a7bf 570 }
rba90 19:6a59fb0ee921 571
rba90 14:25e836a5a7bf 572 case BasicPacket::L3DataBlockPacket:
rba90 14:25e836a5a7bf 573 {
rba90 14:25e836a5a7bf 574 DataBlockPacket dataBlockPacket(payload);
rba90 14:25e836a5a7bf 575
rba90 23:378bd4d025d0 576 uint8_t source_type = dataBlockPacket.getSourceType();
rba90 23:378bd4d025d0 577 switch(source_type)
rba90 15:182b95c25cf2 578 {
rba90 15:182b95c25cf2 579 case 0x0: // temperature sensor
rba90 15:182b95c25cf2 580 {
rba90 16:ab2d9341997a 581 uint8_t payload[3];
rba90 16:ab2d9341997a 582
rba90 16:ab2d9341997a 583 // copy sensor type
rba90 23:378bd4d025d0 584 payload[0] = 0x0; // assume 0x00 is temperature sensor
rba90 16:ab2d9341997a 585
rba90 16:ab2d9341997a 586 // copy sensor data
rba90 23:378bd4d025d0 587 payload[1] = dataBlockPacket.getSourceID(); // assume we only use one byte
rba90 16:ab2d9341997a 588
rba90 16:ab2d9341997a 589 // copy address of source node
rba90 16:ab2d9341997a 590 payload[2] = dataBlockPacket.getData(0);
rba90 16:ab2d9341997a 591
rba90 16:ab2d9341997a 592 // make response
rba90 16:ab2d9341997a 593 SIP.respond(0xf0, payload, 3);
rba90 16:ab2d9341997a 594
rba90 15:182b95c25cf2 595 break;
rba90 15:182b95c25cf2 596 }
rba90 16:ab2d9341997a 597 case 0x1: // door sensor
rba90 15:182b95c25cf2 598 {
rba90 16:ab2d9341997a 599 uint8_t payload[3];
rba90 16:ab2d9341997a 600
rba90 16:ab2d9341997a 601 // copy sensor type
rba90 23:378bd4d025d0 602 payload[0] = 0x1;
rba90 16:ab2d9341997a 603
rba90 16:ab2d9341997a 604 // copy sensor data
rba90 23:378bd4d025d0 605 payload[1] = dataBlockPacket.getSourceID();
rba90 16:ab2d9341997a 606
rba90 16:ab2d9341997a 607 // copy address of source node
rba90 16:ab2d9341997a 608 payload[2] = dataBlockPacket.getData(0);
rba90 16:ab2d9341997a 609
rba90 16:ab2d9341997a 610 // make response
rba90 16:ab2d9341997a 611 SIP.respond(0xf0, payload, 3);
rba90 16:ab2d9341997a 612
rba90 15:182b95c25cf2 613 break;
rba90 15:182b95c25cf2 614 }
rba90 23:378bd4d025d0 615 case 0x2: // service quality
rba90 23:378bd4d025d0 616 {
rba90 23:378bd4d025d0 617 uint8_t payload[8];
rba90 23:378bd4d025d0 618
rba90 23:378bd4d025d0 619 // copy source type
rba90 23:378bd4d025d0 620 payload[0] = 0x2;
rba90 23:378bd4d025d0 621
rba90 23:378bd4d025d0 622 // copy sensor data
rba90 23:378bd4d025d0 623 payload[1] = dataBlockPacket.getSourceID();
rba90 23:378bd4d025d0 624
rba90 23:378bd4d025d0 625 // first byte is the snr of foreign host
rba90 23:378bd4d025d0 626 payload[2] = dataBlockPacket.getData(0);
rba90 23:378bd4d025d0 627
rba90 23:378bd4d025d0 628 // second and third byte are the rssi of foreign host
rba90 23:378bd4d025d0 629 payload[3] = dataBlockPacket.getData(1);
rba90 23:378bd4d025d0 630 payload[4] = dataBlockPacket.getData(2);
rba90 23:378bd4d025d0 631
rba90 23:378bd4d025d0 632 // fourth byte is the snr of local host
rba90 23:378bd4d025d0 633 payload[5] = (uint8_t) aloha.getSnr();
rba90 23:378bd4d025d0 634
rba90 23:378bd4d025d0 635 // fifth and sixth byte are the rssi of local host
rba90 23:378bd4d025d0 636 int_string_16 rssi;
rba90 23:378bd4d025d0 637 rssi.int16 = aloha.getRssi();
rba90 23:378bd4d025d0 638
rba90 23:378bd4d025d0 639 payload[6] = rssi.bytes[0];
rba90 23:378bd4d025d0 640 payload[7] = rssi.bytes[1];
rba90 23:378bd4d025d0 641
rba90 23:378bd4d025d0 642 // make response
rba90 23:378bd4d025d0 643 SIP.respond(0xf0, payload, 8);
rba90 23:378bd4d025d0 644
rba90 23:378bd4d025d0 645 break;
rba90 23:378bd4d025d0 646 }
rba90 23:378bd4d025d0 647
rba90 15:182b95c25cf2 648 default:
rba90 15:182b95c25cf2 649 break;
rba90 15:182b95c25cf2 650 }
rba90 15:182b95c25cf2 651
rba90 14:25e836a5a7bf 652 break;
rba90 14:25e836a5a7bf 653 }
rba90 14:25e836a5a7bf 654 default:
rba90 14:25e836a5a7bf 655 break;
rba90 3:a4f6abad9378 656 }
rba90 0:1e42d79b42e1 657 }
rba90 0:1e42d79b42e1 658
rba90 14:25e836a5a7bf 659
rba90 0:1e42d79b42e1 660 int main() {
rba90 0:1e42d79b42e1 661 // initialize radio module
rba90 5:445f35444a6a 662 aloha.boardInit();
rba90 6:19457108c899 663 aloha.updateSettings();
rba90 6:19457108c899 664 aloha.enable();
rba90 0:1e42d79b42e1 665
rba90 0:1e42d79b42e1 666 // attach serial interrupt handler
rba90 0:1e42d79b42e1 667 pc.attach(&serialInterruptHandler);
rba90 0:1e42d79b42e1 668
rba90 0:1e42d79b42e1 669 // register callback functions for SIP
rba90 0:1e42d79b42e1 670 SIP.registerCommand(0x00, toggleChecksum);
rba90 19:6a59fb0ee921 671 SIP.registerCommand(0x01, queryServiceQuality);
rba90 9:e13e02aa4a2f 672 SIP.registerCommand(0x02, configureRadio);
rba90 9:e13e02aa4a2f 673 SIP.registerCommand(0x03, radioUpdateSettings);
rba90 14:25e836a5a7bf 674 SIP.registerCommand(0x04, querySensors);
rba90 0:1e42d79b42e1 675
rba90 0:1e42d79b42e1 676 // register callback functions for aloha transceiver
rba90 14:25e836a5a7bf 677 aloha.registerType(AlohaFrame::Aloha_Data, AlohaDataPacketHandler);
rba90 0:1e42d79b42e1 678
rba90 0:1e42d79b42e1 679 while(1) {
rba90 0:1e42d79b42e1 680 SIP.poll();
rba90 0:1e42d79b42e1 681 aloha.poll();
rba90 0:1e42d79b42e1 682
rba90 0:1e42d79b42e1 683 while (SerialOutputBuffer.getCounter() > 0)
rba90 0:1e42d79b42e1 684 {
rba90 0:1e42d79b42e1 685 uint8_t ch;
rba90 0:1e42d79b42e1 686 ch = SerialOutputBuffer.dequeue();
rba90 0:1e42d79b42e1 687 pc.putc(ch);
rba90 0:1e42d79b42e1 688 }
rba90 0:1e42d79b42e1 689 }
rba90 0:1e42d79b42e1 690 }