Alexandre Lemay / Mbed 2 deprecated APP3_Coordonator

Dependencies:   mbed

Revision:
2:ddaf240af296
Parent:
1:fe18dd0ba172
Child:
3:eda76f539da3
--- a/main.cpp	Mon Oct 02 21:58:44 2017 +0000
+++ b/main.cpp	Tue Oct 03 04:48:02 2017 +0000
@@ -14,29 +14,73 @@
 const uint8_t ZERO = 0x00;
 const uint8_t APPLY_CHANGES = 0x02;
 const uint8_t FF_CONST = 0xFF;
-const char OPEN_LED[2] = {'D','1'};
+const char OPEN_LED[2] = {'D','5'};
 
 
 bool atCommand = false;
 
 const int TRANSMIT_REQUEST_DATA = 12;
-uint8_t addresse_64_bit[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
+uint8_t addresse_64_bit[8] = {0xcd, 0x2b, 0x0, 0x13, 0xa2, 0x0, 0x40, 0x33};
 uint8_t addresse_16_bit[2] = {0xFF,0xFE};
 
 uint8_t pan_id[14]={0x7E, 0x00, 0x0A, 0x08, 0x01, 0x49, 0x44, 0x46, 0x46, 0x39, 0x39, 0x31, 0x31, 0x09};
 
-void sendAtCommand(){
-       pc.printf("trure");
-       atCommand = true;
+uint8_t nd_Command[8] =  { 0x7E, 0x00, 0x04, 0x08, 0x01, 0x4E, 0x44, 0x64 };
+
+void afficherErreurLed()
+{
+    
+    uint8_t d5_command[9] = { 0x7E, 0x00, 0x05, 0x08, 0x01, 'D', '5', 5, 0};
+
+    static bool nextState = true;
+    
+    //check sum
+    
+    uint8_t checkSum = 0;
+    for(int i = 3; i<9;i++)
+    {
+        checkSum += d5_command[i];
+    }
+    
+    if(nextState)
+    {
+        d5_command[7] = 5;
+    }
+    else
+    {
+        d5_command[7] = 4;
+    }
+    
+    checkSum = FF_CONST - checkSum;
+    d5_command[8] = checkSum;
+    for(int i = 0 ; i < 9; i++)
+    {
+        xBee.putc(d5_command[i]);
+    }
+    
 }
 
-void sendAtCommandPaquet(const char* type){
+void sendNDCommand()
+{
+    for ( int i = 0 ; i < 8; i++ ) 
+    {
+        xBee.putc(nd_Command[i]);
+    }
+}
+
+void sendRemoteAtCommand(){
+    atCommand = true;
+}
+
+void sendRemoteLedCommandPaquet(){
+    static bool nextState = true;
     uint8_t length = 0xF;
     uint8_t paquet[25];
     uint8_t checkSum= 0x0;
     uint8_t LSBLength;
     uint8_t MSBLength;
-       pc.printf("in");
+    
+    char type[2] = { 'D','5'};
 
     LSBLength = length&0xff;
     MSBLength = (length &0xff00) >> 8;
@@ -59,8 +103,15 @@
     paquet[15] = APPLY_CHANGES; //BroadCast Radius
     paquet[16] = type[0];
     paquet[17] = type[1];
-    paquet[18] = 0x05;
-       pc.printf("in2");
+    
+    if(nextState)
+    {
+        paquet[18] = 0x05;
+    }
+    else
+    {
+        paquet[18] = 0x04;
+    }
     
     //check sum
     
@@ -81,8 +132,65 @@
 
 }
 
+bool checkForError(int length,uint8_t type, uint8_t* data)
+{
+    data[0] = type;
+    uint8_t origCheckSum;
+    uint8_t checkSum = type;
+    for (int i =1; i < length; i++)
+    {
+        data[i] = xBee.getc();
+        checkSum += data[i];
+    }
+    checkSum = 0xFF - checkSum;
+    origCheckSum = xBee.getc();
+    
+    return checkSum != origCheckSum;
+}
+
+void receiveAtCommandResponse(int length, uint8_t* data)
+{
+    // 1 Command id
+    
+    uint8_t id = data[1];
+    
+    // La command est identifie par les deux prochains bits
+    uint16_t command = data[2] << 8;
+    command += data[3];
+    
+    pc.printf("\n Command %x\n",command);
+    
+    uint8_t status = data[4];
+    
+    // Data == 5-length
+    if(status != 0)
+    {
+        pc.printf("erreur");
+        afficherErreurLed();
+        return;
+    }
+    switch(command)
+    {
+        
+    case 0x4e44: // Node Discovery AT Command
+    {
+        uint64_t serialNumber = 0;
+        for(int i = 5; i < 13; i++)
+        {
+            serialNumber += data[i] << (8 * (13-i -1) );
+            addresse_64_bit[i-5] = data[i];
+            pc.printf("\nSerial number %i %x\n",i,data[i]);
+        }
+        
+        return;
+    }
+    break;
+    }
+     
+}
+
 void receiveTransmitRequest(int length,uint8_t type){
-    uint8_t data[100];
+    uint8_t data[512];
     data[0] = type;
     uint8_t origCheckSum;
     uint8_t checkSum = type;
@@ -98,8 +206,9 @@
     if(origCheckSum == checkSum){
         for(int i = TRANSMIT_REQUEST_DATA; i <length;i++)
         {
-            pc.printf("%x", data[i]);
+            pc.printf("%c", data[i]);
         }
+        pc.printf("\n");
     }
     else{
         
@@ -114,15 +223,32 @@
     
     if(xBee.getc() == START_DELEMITER)
     {
+        // MSB
         length = (((xBee.getc()) << 8));
+        // LSB
         length +=(xBee.getc());
         uint8_t frame_type = xBee.getc();
         
+        uint8_t data[512];
+        data[0] = frame_type;
+        
         switch (frame_type){
-         case 0x90: 
+        case 0x90: 
                receiveTransmitRequest(length, frame_type);
-            
-            
+        // AT command Response frame
+        break;
+        case 0x88:
+            pc.printf("\nat response : length %i, frame time %i\n",length,frame_type);
+            bool error = checkForError(length, frame_type,data);
+            if(true)
+            {
+                receiveAtCommandResponse(length, data);
+            }
+            else
+            {
+                pc.printf("Transmit or checksum error");
+            }
+            break;
         }
         
     
@@ -134,30 +260,27 @@
 
 
 int main() {
-    flipper.attach (&sendAtCommand,1);
+    flipper.attach (&sendRemoteAtCommand,1);
     reset =0;
     wait_ms(500);
     reset=1;
     wait(1);
     xBee.printf("+++");
     wait(1);
-    
+    sendNDCommand();
     //
 //    for(int i =0;i <14;i++){
 //        xBee.putc(pan_id[i]);   
 //    }
     
     while(1) {
-
         if(xBee.readable()){
                 receiveNewPaquet();
         }
         if (atCommand){
             atCommand = false;
-            sendAtCommandPaquet(OPEN_LED);
-        }    
-            
-            
+            sendRemoteLedCommandPaquet();
+        }            
     }
 }