this is using the mbed os version 5-13-1

Dependencies:   mbed-http

Files at this revision

API Documentation at this revision

Comitter:
ocomeni
Date:
Tue Mar 26 23:33:51 2019 +0000
Parent:
82:10072c1794d3
Child:
84:7c7add00f4bf
Commit message:
implemented parser for header

Changed in this revision

source/ATCmdManager.cpp Show annotated file Show diff for this revision Revisions of this file
source/ATCmdManager.h Show annotated file Show diff for this revision Revisions of this file
source/common_config.h Show annotated file Show diff for this revision Revisions of this file
source/common_types.h Show annotated file Show diff for this revision Revisions of this file
source/main-https.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/source/ATCmdManager.cpp	Sun Mar 24 17:32:06 2019 +0000
+++ b/source/ATCmdManager.cpp	Tue Mar 26 23:33:51 2019 +0000
@@ -58,6 +58,7 @@
     _parser.oob("AT+UWSC=0,2", callback(this, &ATCmdManager::_oob_setWiFiSSID));
     _parser.oob("AT+UWSC=0,8", callback(this, &ATCmdManager::_oob_setWiFiPWD));
     _parser.oob("AT+UWSC=0,5", callback(this, &ATCmdManager::_oob_setWiFiSecurity));
+    _parser.oob("AT+UWSC=0,5", callback(this, &ATCmdManager::_oob_sendHttpMessage));
  
 //AT+UWSC=0,2,<SSID>
 //AT+UWSC=0,8,<PWD>
@@ -146,14 +147,126 @@
 }
 
 
-
+bool ATCmdManager::validate(edm_header_t edm_header)
+{
+    if(edm_header.startByte != EDM_START_BYTE)return false; // unexpected start byte found!
+    if(edm_header.payloadID != CONNECT_EVENT &&
+       edm_header.payloadID != DISCONNECT_EVENT &&
+       edm_header.payloadID != DATA_EVENT &&
+       edm_header.payloadID != DATA_COMMAND &&
+       edm_header.payloadID != AT_REQUEST &&
+       edm_header.payloadID != AT_CONFIRMATION &&
+       edm_header.payloadID != AT_EVENT 
+       )return false; // unexpected payload ID found!
+    if(edm_header.payloadLen > MAX_EDM_PAYLOAD_LEN ||
+       edm_header.payloadLen < MIN_EDM_PAYLOAD_LEN 
+       ) return false; // unexpected length received!
+    return true;
+}
 
 // OOB processing
 void ATCmdManager::_process_oob(uint32_t timeout, bool all){
     set_timeout(timeout);
-    // Poll for inbound packets
-    while (_parser.process_oob() && all) {
+    static int cnt = 0;
+    if(dataMode == AT_EXT_DATA_MODE)
+    {
+        int n;
+        if(cnt++ % 10 == 0)printf("In EDM mode\n");
+        // Poll for edm packets
+        do{
+            n = _parser.read((char *)&edm_hdr, EDM_HDR_LEN);
+            if(n == -1) break; // break if it times out
+            printf("%d bytes read!\n", n);
+            if(n==5)
+            printf("Start = %d, payloadID = %d len = %d\n", edm_hdr.startByte, 
+                                                            edm_hdr.payloadID, 
+                                                            edm_hdr.payloadLen);
+            if(n == EDM_HDR_LEN && validate(edm_hdr)) // if AT command use process oob to decode
+            {
+                if(edm_hdr.payloadID == AT_REQUEST)
+                {
+                    _parser.process_oob();
+                    break;
+                }
+                else
+                {
+                    int pLen = edm_hdr.payloadLen-1;
+                    rx_buf_ptr = (uint8_t *) malloc (pLen); // we already read 2 bytes from payload but expect 1 stop byte
+                    rx_buf_ptr[pLen-1] = 0x00; // clear last byte so the readback value is as expected
+                    n = _parser.read((char *)rx_buf_ptr, pLen);
+                    if(n == -1) break; // timeout!
+                    printf("%d bytes read - expected %d!\n", n, pLen);
+                    for(int i =0;i<pLen;i++){
+                        if(i%8==0) printf("\n[%3d]",i/8);
+                        printf("%2x ", rx_buf_ptr[i]);
+                    }
+                    printf("\n");
+                    printf("rx_buf_ptr[pLen-1] = %0x\n",rx_buf_ptr[pLen-1]);
+                    if(rx_buf_ptr[pLen-1] != EDM_STOP_BYTE) {
+                        break; // exit if stop byte now found - possible data corruption!
+                    }
+                    switch(edm_hdr.payloadID)
+                    {
+                        case CONNECT_EVENT:
+                            printf("Connection Event received!\n");
+                            break;
+                        case DISCONNECT_EVENT:
+                            printf("DISCONNECT_EVENT received!\n");
+                            break;
+                        case DATA_EVENT:
+                            printf("DATA_EVENT received!\n");
+                            break;
+                        case DATA_COMMAND:
+                            char s1[100];
+                            printf("DATA_COMMAND received!\n");
+                            int n = 0;
+                            int cnt = 0;
+                            int len = 0;
+                            char * strPtr = (char *)&rx_buf_ptr[1];
+                            char * p2 = strstr(strPtr, "\r\n\r\n");
+                            char * nxtPtr = strPtr;
+                            printf("\nstrPtr address= %x",strPtr);
+                            printf("\np2 address= %x", p2);
+                            while(1){
+                                n = sscanf(nxtPtr,"%s[^:]", s1);
+                                len = strlen(s1);
+                                if(n<=0) break;
+                                printf("\ns[%d] = %s len = %d, n = %d",cnt++,s1, len, n );
+                                if(cnt==20) break;
+                                nxtPtr = strstr(nxtPtr, s1)+len;
+                                printf("\nxtPtr address= %x p2 addr = %x",nxtPtr, p2);
+                                if(nxtPtr > p2) break;
+                            }
+                            
+                            break;
+                        case AT_REQUEST:
+                            printf("AT_REQUEST received!\n");
+                            break;
+                        case AT_CONFIRMATION:
+                            printf("AT_CONFIRMATION received!\n");
+                            break;
+                        case AT_EVENT:
+                            printf("AT_EVENT received!\n");
+                            break;
+                        default:
+                            printf("UNKNOWN MESSAGE received!\n");
+                            break;
+                    }
+                }
+                //_parser.process_oob();
+            }
+            else // incorrect # of bytes received abort!!
+            {
+                break;
+            }
+        }while (all);  // continue to process until timeout
     }
+    else
+    {
+        // Poll for inbound packets
+        while (_parser.process_oob() && all) {
+        }
+    }        
     set_timeout();
 }
 
@@ -234,7 +347,6 @@
 
 
 void ATCmdManager::_oob_data_mode(){
-    int dataMode;
     _smutex.lock();
     printf("\n Received EDM mode command!!\n");
     if(_parser.scanf("%d", &dataMode) >0) {
@@ -242,15 +354,15 @@
         switch(dataMode)
         {
             case 0:
-                printf("\nATCmdParser: Command Mode request received");
+                printf("\nATCmdParser: Command Mode request received\n");
                 dataMode = AT_CMD_DATA_MODE;
                 break;
             case 1:
-                printf("\nATCmdParser: Data Mode request received");
+                printf("\nATCmdParser: Data Mode request received\n");
                 dataMode = AT_STD_DATA_MODE;
                 break;
             case 2:
-                printf("\nATCmdParser: Extended data Mode request received");
+                printf("\nATCmdParser: Extended data Mode request received\n");
                 dataMode = AT_EXT_DATA_MODE;
                 break;
             default:
@@ -258,7 +370,7 @@
                 break;
         }
     } else {
-        printf("\nATCmdParser: Retrieving Uart Options failed");
+        printf("\nATCmdParser: Retrieving Uart Options failed\n");
     }
     _parser.send("OK\n");
     _smutex.unlock();
@@ -405,21 +517,36 @@
     wifi_cfg.security = NSAPI_SECURITY_UNKNOWN;
     return wifi_cfg;    
 }
-
-int ATCmdManager::readBytes(uint8_t *buf, int maxBytes)
+/*  read ASCII characters into buffer and null terminate */
+int ATCmdManager::readStringBytes(uint8_t *buf, int maxBytes)
 { 
     int c;
     int sptr = 0;
     for(int i=0;i<maxBytes;i++){
         c = _parser.getc();
         if(c==-1){
-            buf[sptr] = '\0'; // null terminate if string
+            buf[sptr] = NULL; // null terminate if string
             return i;
         }
         if(c != ',' && c!= '"'){
             buf[sptr++] = (uint8_t) c;
         }
     }
+    return maxBytes;
+}
+
+int ATCmdManager::ReadBytes(uint8_t *buf, int maxBytes)
+{ 
+    int c;
+    int sptr = 0;
+    for(int i=0;i<maxBytes;i++){
+        c = _parser.getc();
+        if(c==-1){
+            return i;
+        }
+        buf[sptr++] = (uint8_t) c;
+    }
+    return maxBytes;
 }
 
 void ATCmdManager::_oob_setWiFiSSID()
@@ -427,7 +554,7 @@
     int n;
     wifi_config_t wifi_cfg = init_wifi_config();
     _smutex.lock();
-    n = readBytes((uint8_t *)wifi_cfg.ssid, 32);
+    n = readStringBytes((uint8_t *)wifi_cfg.ssid, 32);
     printf("[ATCMD MAN]: number of bytes read = %d\n", n);
     if(n>0) 
     {
@@ -452,7 +579,7 @@
     wifi_config_t wifi_cfg = init_wifi_config();
     _smutex.lock();
     //n = _parser.scanf("%31[^\r\n]", wifi_cfg.pass);
-    n = readBytes((uint8_t *)wifi_cfg.pass, 32);
+    n = readStringBytes((uint8_t *)wifi_cfg.pass, 32);
     if(n>0) 
     {
         printf("ATCMD MAN]: wifi_cfg.pass = %s\n", wifi_cfg.pass);
@@ -493,6 +620,33 @@
     _smutex.unlock();
 }
 
+
+void ATCmdManager::_oob_sendHttpMessage()
+{
+    int n;
+    http_post_request_t http_post_request;
+    _smutex.lock();
+    /*
+    n = _parser.scanf(",%d", &wifi_cfg.security);
+    if(n>0) 
+    {
+        printf("ATCMD MAN]: wifi_cfg.security = %s\n", sec2str(wifi_cfg.security));
+        // package and send on wifi data queue
+        wifi_data_msg_t data_msg;
+        data_msg.wifi_cmd = WIFI_CMD_CONFIG;
+        data_msg.dataLen = sizeof(wifi_config_t);
+        memcpy(data_msg.buffer,&wifi_cfg, data_msg.dataLen);
+        queueWiFiDataRequest(data_msg);
+        _parser.send("OK\n");
+    } else {
+        printf("\n[ATCMD MAN]: wifi configuration failed  \n");
+        _parser.send("NAK\n");
+    }
+    */
+    _smutex.unlock();
+
+}
+
 bool  ATCmdManager::queueWiFiCommand(wifi_cmd_t cmd){
     wifi_cmd_message_t *wifiCmd = _aT2WiFimPool->alloc();
     wifiCmd->wifi_cmd            = cmd;
--- a/source/ATCmdManager.h	Sun Mar 24 17:32:06 2019 +0000
+++ b/source/ATCmdManager.h	Tue Mar 26 23:33:51 2019 +0000
@@ -2,6 +2,7 @@
 #define __ATCMD_MANAGER_H__
 #include <events/mbed_events.h>
 #include <mbed.h>
+#include <vector>
 #include "ATCmdParser.h"
 #include "BleManager.h"
 #include "WiFiManager.h"
@@ -63,7 +64,8 @@
     
     //pointer to response data - should be deleted after processing
     at_data_msg_t *resp_data;
-
+    edm_header_t   edm_hdr;
+    uint8_t        *rx_buf_ptr;
     
     // OOB processing
     void _process_oob(uint32_t timeout, bool all);
@@ -90,6 +92,7 @@
     void _oob_setWiFiSSID();
     void _oob_setWiFiPWD();
     void _oob_setWiFiSecurity();
+    void _oob_sendHttpMessage();
     wifi_config_t init_wifi_config();
     const char * sec2str(nsapi_security_t sec);
     bool  queueWiFiCommand(wifi_cmd_t cmd);
@@ -98,7 +101,9 @@
     bool  dequeueATdataResponse();
     void  processResponses();
     bool  setNextResponse(at_cmd_resp_t resp);    
-    int   readBytes(uint8_t *buf, int maxBytes);
+    int   ReadBytes(uint8_t *buf, int maxBytes);
+    int   readStringBytes(uint8_t *buf, int maxBytes);
+    bool  validate(edm_header_t edm_header);
 
     
     /**
--- a/source/common_config.h	Sun Mar 24 17:32:06 2019 +0000
+++ b/source/common_config.h	Tue Mar 26 23:33:51 2019 +0000
@@ -12,4 +12,24 @@
 #define UBLOX_ODIN_W2_RECV_TIMEOUT    2000
 #endif
 
+const uint8_t hello_msg[] = {0xaa,0x00,0x96,0x00,0x36,0x00,0x50,0x4f
+                            ,0x53,0x54,0x20,0x2f,0x6e,0x75,0x64,0x67
+                            ,0x65,0x62,0x6f,0x78,0x2f,0x76,0x31,0x20
+                            ,0x48,0x54,0x54,0x50,0x2f,0x31,0x2e,0x31
+                            ,0x0d,0x0a,0x48,0x6f,0x73,0x74,0x3a,0x20
+                            ,0x33,0x35,0x2e,0x31,0x37,0x36,0x2e,0x31
+                            ,0x39,0x32,0x2e,0x33,0x33,0x3a,0x38,0x30
+                            ,0x0d,0x0a,0x41,0x63,0x63,0x65,0x70,0x74
+                            ,0x3a,0x20,0x2a,0x2f,0x2a,0x0d,0x0a,0x43
+                            ,0x6f,0x6e,0x74,0x65,0x6e,0x74,0x2d,0x54
+                            ,0x79,0x70,0x65,0x3a,0x20,0x61,0x70,0x70
+                            ,0x6c,0x69,0x63,0x61,0x74,0x69,0x6f,0x6e
+                            ,0x2f,0x6f,0x63,0x74,0x65,0x74,0x2d,0x73
+                            ,0x74,0x72,0x65,0x61,0x6d,0x0d,0x0a,0x43
+                            ,0x6f,0x6e,0x74,0x65,0x6e,0x74,0x2d,0x4c
+                            ,0x65,0x6e,0x67,0x74,0x68,0x3a,0x20,0x32
+                            ,0x30,0x0d,0x0a,0x0d,0x0a,0x00,0x08,0xd4
+                            ,0xca,0x6e,0x79,0x05,0x4e,0x01,0x68,0x65
+                            ,0x6c,0x6c,0x6f,0x00,0x00,0x91,0xb5,0xa4
+                            ,0x10,0x55};
 #endif // __COMMON_CONFIG_H__
\ No newline at end of file
--- a/source/common_types.h	Sun Mar 24 17:32:06 2019 +0000
+++ b/source/common_types.h	Tue Mar 26 23:33:51 2019 +0000
@@ -71,6 +71,17 @@
   AT_EXT_DATA_MODE
 }at_data_mode_t;
 
+typedef enum edm_payload_id
+{
+  CONNECT_EVENT    = 0x0011,
+  DISCONNECT_EVENT = 0x0021,
+  DATA_EVENT       = 0x0031,
+  DATA_COMMAND     = 0x0036,
+  AT_REQUEST       = 0x0044,
+  AT_CONFIRMATION  = 0x0045,
+  AT_EVENT         = 0x0041
+}edm_payload_id_t;
+
 typedef enum conn_scheme
 {
   INVALID_SCHEME,
@@ -107,6 +118,12 @@
 } http_post_request_t;
 
 typedef struct {
+    uint8_t       startByte;   /* start Byte 0xAA */
+    uint16_t      payloadLen;  /* payload Length */
+    uint16_t      payloadID;   /* payload identifier     */
+} edm_header_t;
+
+typedef struct {
     string        contentType;  /* content type */
     int           bodyLen;      /* body length     */
     uint8_t       body[900];    /* body     */
@@ -118,5 +135,9 @@
     string        url;          /* body     */
 } internet_config_t;
 
-
+#define EDM_START_BYTE       0xAA
+#define EDM_STOP_BYTE        0x55
+#define EDM_HDR_LEN          5
+#define MAX_EDM_PAYLOAD_LEN  (4096-1)
+#define MIN_EDM_PAYLOAD_LEN (2)
 #endif  // __COMMON_TYPES_H__
\ No newline at end of file
--- a/source/main-https.cpp	Sun Mar 24 17:32:06 2019 +0000
+++ b/source/main-https.cpp	Tue Mar 26 23:33:51 2019 +0000
@@ -396,6 +396,8 @@
     print_memory_info();
     printf("\r\n ++++++ PROGRAM STARTING -- reset count = %d ++++++ \r\n", reset_counter);
     device = new RawSerial(USBTX, USBRX, DEFAULT_BAUD_RATE);
+    
+    printf("*** HELLO MESSAGE *** \n %s", (char *)&hello_msg[6]);
     setupDefaultWiFiConfig();
     setupDefaultBleConfig();
     BLE& _ble = BLE::Instance();