Master Implementation of WANOT

Dependencies:   SX1276Lib mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MasterSetUp.cpp Source File

MasterSetUp.cpp

00001 
00002 #include "WANOT.h"
00003 
00004 /*
00005  *  Global variables declarations
00006  */
00007  
00008 extern SuperSlotStates SuperSlotState;
00009 
00010 volatile MasterSetUpStates MasterSetUpState = sendBeacon;
00011 
00012 volatile msgType messageType = msgRTS;
00013 
00014 extern RadioEvents_t RadioEvents;
00015 extern SX1276MB1xAS Radio;
00016 
00017 extern uint16_t BufferSize;
00018 extern uint8_t Buffer[];
00019 
00020 extern uint32_t LORA_Channels[NUMBER_OF_CHANNELS];
00021 
00022 extern int16_t RssiValue;
00023 extern int8_t SnrValue;
00024 
00025 extern uint8_t TDMAChannel;
00026 
00027 static Timer timer;
00028 static int begin, end;
00029 
00030 void SetUp_OnTxDone(void)
00031 {
00032     if(MasterSetUpState != Wait_for_JoinReq)
00033         Radio.Rx(RX_TIMEOUT_VALUE);
00034     else
00035         Radio.Rx(1.5 * RX_TIMEOUT_VALUE);
00036     debug("OnTxDone!!\n\r");
00037 
00038 }
00039 
00040 void SetUp_OnRxDone(uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr)
00041 {
00042     Radio.Sleep();
00043     BufferSize = size;
00044     memcpy(Buffer, payload, BufferSize);
00045     RssiValue = rssi;
00046     SnrValue = snr;
00047 
00048     debug("OnRxDone!!\n\r");
00049 
00050     switch (MasterSetUpState) {
00051         case Wait_for_RTS:
00052             messageType = msgRTS;
00053             if (messageType == Buffer[0]) {
00054                 MasterSetUpState = Send_CTS;
00055                 debug("RTS Packet\n\r");
00056             } else {
00057                 MasterSetUpState = sendBeacon;
00058                 debug("RTS Packet Mismatch\n\r");
00059             }
00060             break;
00061 
00062         case Wait_for_JoinReq:
00063             messageType = msgJoinReq;
00064             if (messageType == Buffer[0]) {
00065                 MasterSetUpState = Send_JoinAccept;
00066                 debug("JoinReq Packet\n\r");
00067             } else {
00068                 MasterSetUpState = sendBeacon;
00069                 debug("JoinReq Packet Mismatch\n\r");
00070             }
00071             break;
00072 
00073         case Wait_for_Ack:
00074             messageType = msgAck;
00075             if (messageType == Buffer[0]) {
00076                 MasterSetUpState = AckDone;
00077                 debug("Ack Packet\n\r");
00078             } else {
00079                 MasterSetUpState = sendBeacon;
00080                 debug("Ack Packet Mistmatch\n\r");
00081 
00082             }
00083             break;
00084 
00085         default:
00086             break;
00087     }
00088 
00089 
00090 }
00091 
00092 
00093 
00094 void SetUp_OnTxTimeout(void)
00095 {
00096 
00097 }
00098 
00099 void SetUp_OnRxTimeout(void)
00100 {
00101     MasterSetUpState = sendBeacon;
00102     debug("OnRxTimeout!!\n\r");
00103 
00104 }
00105 
00106 void SetUp_OnRxError(void)
00107 {
00108     MasterSetUpState = sendBeacon;
00109 }
00110 
00111 void MasterSetUp()
00112 {
00113     debug("Master Set Up Phase Started...\n\r");
00114     
00115     timer.start();
00116     begin = timer.read_us();
00117     
00118     // Initialize Radio driver
00119     RadioEvents.TxDone = SetUp_OnTxDone;
00120     RadioEvents.RxDone = SetUp_OnRxDone;
00121     RadioEvents.RxError = SetUp_OnRxError;
00122     RadioEvents.TxTimeout = SetUp_OnTxTimeout;
00123     RadioEvents.RxTimeout = SetUp_OnRxTimeout;
00124     Radio.Init(&RadioEvents);
00125     
00126     uint8_t Beacon_Counter = 0;
00127 
00128     while (SuperSlotState == SetUp_Phase) {
00129         switch (MasterSetUpState) {
00130             case sendBeacon:
00131                 if(Beacon_Counter <=4) {
00132                     Beacon_Counter++;
00133                     Buffer[0] = SETUP_BEACON_SYNCWORD;
00134                     Radio.Send(Buffer, BUFFER_SIZE_RTS);
00135                     debug("Sending Beacon\n\r");
00136                     MasterSetUpState = Wait_for_RTS;
00137                 } else {
00138                     debug("Beacon Timed Out without SLaves\n\r");
00139                     MasterSetUpState = Slave_Not_Found;
00140                     Beacon_Counter = 0;
00141                 }
00142                 break;
00143 
00144             case Send_CTS:
00145                 messageType = msgCTS;
00146                 Buffer[0] = messageType;
00147                 Buffer[1] = SnrValue;
00148                 Radio.Send(Buffer, BUFFER_SIZE_CTS);
00149                 debug("Sending CTS\n\r");
00150                 MasterSetUpState = Wait_for_JoinReq;
00151                 break;
00152 
00153             case Send_JoinAccept:
00154                 messageType = msgJoinAccept;
00155                 Buffer[0] = messageType;
00156                 Buffer[11] = 5;
00157                 Radio.Send(Buffer, BUFFER_SIZE_JoinAccept);
00158                 debug("Sending JoinAccept\n\r");
00159                 MasterSetUpState = Wait_for_Ack;
00160                 break;
00161 
00162             case AckDone:
00163                 debug("Ack done: +1 \n\r");
00164                 MasterSetUpState = sendBeacon;
00165                 end = timer.read_us();
00166                 debug("Registeration took : %f S\n\r", (end-begin)/1000000.0);
00167                 break;
00168 
00169             case Slave_Not_Found:
00170                 debug("Slave Not Found .. Sleep");
00171                 Radio.Sleep();
00172                 MasterSetUpState = sendBeacon;
00173                 break;
00174 
00175             case Wait_for_Ack:
00176             case Wait_for_RTS:
00177             case Wait_for_JoinReq:
00178                 break;
00179             default:
00180                 break;
00181 
00182         }
00183 
00184     }
00185     debug("Master Set Up Phase Finished...\n\r");
00186 }