BraceletUS / Mbed 2 deprecated S05APP3

Dependencies:   ConfigFile EthernetInterface mbed-rtos mbed

Fork of S05APP3 by App S5

Revision:
15:4c748df01e5b
Parent:
14:8b59a90725bc
Child:
16:cede55e5b075
--- a/main.cpp	Mon Feb 13 18:21:15 2017 +0000
+++ b/main.cpp	Mon Feb 13 20:14:12 2017 +0000
@@ -5,7 +5,9 @@
 #include "mbed.h"
 #include "rtos.h"
 
+AnalogOut errorLed(p18);
 DigitalOut reset(p8);
+DigitalOut led1(LED1);
 EthernetInterface eth;
 Serial xbee(p13, p14);   // tx, rx
 Serial pc(USBTX, USBRX); // tx, rx
@@ -30,8 +32,7 @@
 char serverAddress[32];
 char panIdChar[5];
 char portNbr[5];
-bool boolLED = false; 
-bool isRouterAddressSet = false;
+bool led = false; 
 
 void printBuffer(uint8_t bufferSize, uint8_t* buffer) {
     for(uint8_t k = 0; k <= bufferSize; k++) {
@@ -40,6 +41,12 @@
     printf("\n");
 }
 
+void toggleErrorLed() {
+    errorLed.write(3.3);
+    wait(1);
+    errorLed.write(0);
+}
+
 void readConfigFile() {
     LocalFileSystem local("local");
     ConfigFile cfg;
@@ -63,9 +70,8 @@
     }
 }
 
-uint8_t calculateChecksum(uint8_t frameSize, uint8_t* buffer) {
+uint8_t calculateChecksum(uint8_t frameTypeIndex, uint8_t frameSize, uint8_t* buffer) {
     char checksum = 0;
-    uint8_t frameTypeIndex = 3;
     
     for (int i = frameTypeIndex; i < frameSize - 1; i++) {
         checksum += buffer[i];
@@ -77,20 +83,18 @@
 void toggleLed() {
     // uint8_t high[20] = {0x7E, 0x00, 0x10, 0x17, 0x01, 0x00, 0x13, 0xA2, 0x00, 0x40, 0x8B, 0x41, 0x6E, 0xFF, 0xFE, 0x02, 0x44, 0x34, 0x05, 0x3C};
     // uint8_t low[20]  = {0x7E, 0x00, 0x10, 0x17, 0x02, 0x00, 0x13, 0xA2, 0x00, 0x40, 0x8B, 0x41, 0x6E, 0xFF, 0xFE, 0x02, 0x44, 0x34, 0x04, 0x3C};
+    Thread::signal_wait(0x2);
+    
     while(1) {
         Thread::signal_wait(0x1);
         
-        if (!isRouterAddressSet) { // The router address is unknown
-            return;
-        }
-        
         uint8_t command[20] = {0};
         
         command[0] = 0x7E;
         command[1] = 0x00;
         command[2] = 0x10;
         command[3] = 0x17;
-        if (boolLED) { 
+        if (led) { 
             command[4] = 0x01;
         }
         else {
@@ -109,15 +113,13 @@
         command[15] = 0x02;
         command[16] = 0x44;
         command[17] = 0x34;
-        if (boolLED) {
+        if (led) {
             command[18] = 0x05;
         }
         else {
             command[18] = 0x04;
         }
-        
-        command[19] = calculateChecksum(20, command);
-        printBuffer(19, command);
+        command[19] = calculateChecksum(3, 20, command);
         
         for (uint8_t i = 0; i < 20;) {
             if (xbee.writeable()) {
@@ -125,8 +127,8 @@
                 i++; 
             }   
         }
-        
-        boolLED = !boolLED;
+        led = !led;
+        led1 = !led1;
     }
 }
 
@@ -161,14 +163,110 @@
     }
 }
 
-void checkForError(uint8_t* buffer) {
-    if (buffer[0] == 0x97) {
-        if (buffer[1] == 0x02) {
-            pc.printf("Erreur dans la remote AT command pour mettre la DEL a l'etat bas.\n");
-        }
-        else if (buffer[1] == 0x01) {
-            pc.printf("Erreur dans la remote AT command pour mettre la DEL a l'etat haut\n");
-        }
+void readATCommandResponse(uint8_t* buffer) {
+    if (!buffer[4] == 0x00)    toggleErrorLed();
+    
+    if (buffer[4] == 0x01)       pc.printf("AT Command Response: Error.\n");
+    else if (buffer[4] == 0x02)  pc.printf("AT Command Response: Invalid Command.\n");
+    else if (buffer[4] == 0x03)  pc.printf("AT Command Response: Invalid Parameter.\n");
+    else if (buffer[4] == 0x04)  pc.printf("AT Command Response: Tx Failure.\n"); 
+}
+
+void readRemoteATCommandResponse(uint8_t* buffer) {
+    if (!buffer[14] == 0x00)    toggleErrorLed();
+
+    if (buffer[14] == 0x01)      pc.printf("Remote AT Command Response: Error.\n");
+    else if (buffer[14] == 0x02) pc.printf("Remote AT Command Response: Invalid Command.\n");
+    else if (buffer[14] == 0x03) pc.printf("Remote AT Command Response: Invalid Parameter.\n");
+    else if (buffer[14] == 0x04) pc.printf("Remote AT Command Response: Tx Failure.\n");
+}
+
+void readTransmitStatus(uint8_t* buffer) {
+    if (buffer[5] == 0x01)      pc.printf("Transmit Status: An expedted MAC acknowledgement never occured. \n");
+    else if (buffer[5] == 0x02) pc.printf("Transmit Status: CCA failure. \n");
+    else if (buffer[5] == 0x03) pc.printf("Transmit Status: Packet was purgedwithoutbeing transmitted. \n");
+    else if (buffer[5] == 0x04) pc.printf("Transmit Status: Physical error on the interface with the WiFi transceiver. \n");
+    else if (buffer[5] == 0x18) pc.printf("Transmit Status: No buffers. \n");
+    else if (buffer[5] == 0x21) pc.printf("Transmit Status: Expected networkacknowledgement never occured. \n");
+    else if (buffer[5] == 0x22) pc.printf("Transmit Status: Not joined to network. \n");
+    else if (buffer[5] == 0x23) pc.printf("Transmit Status: Self-addressed. \n");
+    else if (buffer[5] == 0x24) pc.printf("Transmit Status: Address not found. \n");
+    else if (buffer[5] == 0x25) pc.printf("Transmit Status: Route not found. \n");
+    else if (buffer[5] == 0x26) pc.printf("Transmit Status: Broadcast relay was not heard. \n");
+    else if (buffer[5] == 0x2B) pc.printf("Transmit Status: Invalid binding table index. \n");
+    else if (buffer[5] == 0x2C) pc.printf("Transmit Status: Invalid Endpoint. \n");
+    else if (buffer[5] == 0x31) pc.printf("Transmit Status: A software error occurred. \n");
+    else if (buffer[5] == 0x32) pc.printf("Transmit Status: Resource Error. \n");
+    else if (buffer[5] == 0x74) pc.printf("Transmit Status: Data payload too large. \n");
+    else if (buffer[5] == 0x76) pc.printf("Transmit Status: Client socket creationat attempt failed. \n");
+}
+
+void readModemStatus(uint8_t* buffer) {
+    if (buffer[1] == 0x00) {
+        pc.printf("Modem status: Hardware reset.\n");
+    }
+    else if (buffer[1] == 0x01) {
+        pc.printf("Modem status: Watchdog timer reset.\n");
+        toggleErrorLed();
+    }
+    else if (buffer[1] == 0x02) {
+        pc.printf("Modem status: Joined network.\n");
+        toggleErrorLed();
+    }
+    else if (buffer[1] == 0x03) {
+        pc.printf("Modem status: Disassociated.\n");
+        toggleErrorLed();
+    }
+    else if (buffer[1] == 0x04) {
+        pc.printf("Modem status: Configuration error/synchronization.\n");
+        toggleErrorLed();
+    }
+    else if (buffer[1] == 0x05) {
+        pc.printf("Modem status: Coordinator realignment.\n");
+        toggleErrorLed();
+    }
+    else if (buffer[1] == 0x06) {
+        pc.printf("Modem status: Coordinator started.\n");
+    }
+    else if (buffer[1] == 0x07) {
+        pc.printf("Modem status: Network security key updated.\n");
+        toggleErrorLed();
+    }
+    else if (buffer[1] == 0x08) {
+        pc.printf("Modem status: Network woke up.\n");
+        toggleErrorLed();
+    }
+    else if (buffer[1] == 0x0C) {
+        pc.printf("Modem status: Network went to sleep.\n");
+        toggleErrorLed();
+    }
+    else if (buffer[1] == 0x0E) {
+        pc.printf("Modem status: Device cloud connected.\n");
+        toggleErrorLed();
+    }
+    else if (buffer[1] == 0x0F) {
+        pc.printf("Modem status: Device cloud disconnected.\n");
+        toggleErrorLed();
+    }
+    else if (buffer[1] == 0x11) {
+        pc.printf("Modem status: Modem configurationchanged while join in progress.\n");
+        toggleErrorLed();
+    }
+    else if (buffer[1] == 0x80) {
+        pc.printf("Modem status: Stack error (80+).\n");
+        toggleErrorLed();
+    }
+    else if (buffer[1] == 0x82) {
+        pc.printf("Modem status: Send/join command issuedwithout connecting from AP.\n");
+        toggleErrorLed();
+    }
+    else if (buffer[1] == 0x83) {
+        pc.printf("Modem status: Access point not found.\n");
+        toggleErrorLed();
+    }
+    else if (buffer[1] == 0x84) {
+        pc.printf("Modem status: PSK not configured.\n");
+        toggleErrorLed();
     }
 }
 
@@ -185,7 +283,6 @@
     return true;
 }
 
-// We are storing the 64 bits address of the router.
 void readRouterAddress(uint8_t* buffer) {    
     routerAddress[0] = buffer[1];
     routerAddress[1] = buffer[2];
@@ -195,7 +292,7 @@
     routerAddress[5] = buffer[6];
     routerAddress[6] = buffer[7];
     routerAddress[7] = buffer[8];
-    isRouterAddressSet = true;
+    t1->signal_set(0x02);
 }
 
 void readRouterData(uint8_t bufferSize, uint8_t* buffer) {
@@ -244,14 +341,59 @@
                 if (buffer[0] == 0x90) {
                     readRouterAddress(buffer);
                     readRouterData(bufferSize, buffer);
-                } else {
                     
-                }
+                } else if (buffer[0] == 0x8A) { // Modem Status
+                    readModemStatus(buffer);
+                }  else if (buffer[0] == 0x8B) { // Transmit Status
+                    // Should never occur...
+                }  else if (buffer[0] == 0x97) { // Remote AT Command Response
+                    readRemoteATCommandResponse(buffer);
+                }  else if (buffer[0] == 0x09) { // AT Command Response
+                    printf("AT COMMAND RESPONSE");
+                    
+                }      
+            } else {
+            
+                // HERE: Try to resend the frame...
             }
         }
     }
 }
 
+void setCoordinatorPanId(uint16_t panId) {
+    char _8bitsPanId[2];
+    _8bitsPanId[0] = (panId >> 8) & 0xFF;
+    _8bitsPanId[1] = panId & 0xFF;
+    uint8_t setPanIdBuffer[10] = {0x07, 0x00, 0x06, 0x09, 0x01, 0x49, 0x44, _8bitsPanId[0], _8bitsPanId[1], 0x00};
+    setPanIdBuffer[10] = calculateChecksum(0, 10, setPanIdBuffer);
+    
+    for (uint8_t i = 0; i < 10;) {
+        if (xbee.writeable()) {
+            xbee.putc(setPanIdBuffer[i]);
+            i++; 
+        }   
+    }
+    
+    uint8_t saveChangesBuffer[8] = {0x7E, 0x00, 0x04, 0x09, 0x02, 0x57, 0x52, 0x4B};
+    
+    for (uint8_t i = 0; i < 8;) {
+        if (xbee.writeable()) {
+            xbee.putc(saveChangesBuffer[i]);
+            i++; 
+        }   
+    }
+    
+    
+    uint8_t applyChangesBuffer[8] = {0x7E, 0x00, 0x04, 0x09, 0x03, 0x41, 0x43, 0x6F};
+    
+    for (uint8_t i = 0; i < 8;) {
+        if (xbee.writeable()) {
+            xbee.putc(saveChangesBuffer[i]);
+            i++; 
+        }   
+    }
+}
+
 void isr() {
     t1->signal_set(0x1);
 }
@@ -267,10 +409,11 @@
     readConfigFile();
     
     xbee.baud(9600);                    // Set baud rate
-    xbee.printf("ATID %i\r",panId);     // Set the 64-bit PAN ID
-    xbee.printf("ATWR \r");             // Save the changes  
-    xbee.printf("ATCN \r");             // Apply changes by sending the CN command
-    
+    //xbee.printf("ATID %i\r",panId);     // Set the 64-bit PAN ID
+    //xbee.printf("ATWR \r");             // Save the changes  
+    //xbee.printf("ATCN \r");             // Apply changes by sending the CN command
+    setCoordinatorPanId(panId);
+
     if (eth.init() != 0) { // Use DHCP
         pc.printf("Erreur d'initialisation du RJ45.\n");
     }
@@ -279,17 +422,17 @@
     }
     pc.printf("IP Address is %s\n", eth.getIPAddress());
     
-    int numberOfRetries = 0;
+    /*int numberOfRetries = 0;
     while (sock.connect(serverAddress, portNumber) != 0) {
         if (numberOfRetries == 5) {
             error("Erreur dans la connection au socket.\n");
         }
         numberOfRetries++;
-    } 
+    }*/ 
     
     // Démarrage des tâches:
     Thread _readDataFromRouter(readDataFromRouter);
-    Thread _sendDataToServer(sendDataToServer);
+    //Thread _sendDataToServer(sendDataToServer);
     Thread _toggleRouterLed(toggleLed);
     t1 = &_toggleRouterLed;