Wireless interface using LoRa technology

Dependencies:   AlohaTransceiver RingBuffer SX1276Lib_inAir SerialInterfaceProtocol mbed L3PDU

Committer:
rba90
Date:
Wed Aug 24 04:09:42 2016 +0000
Revision:
17:7093a533b4ae
Parent:
16:dcc5f9075595
Child:
18:ce8dc94163d8
fix a bug where wrong packet was generated

Who changed what in which revision?

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