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:
Sun May 19 13:09:27 2019 +0000
Branch:
PassingRegression
Parent:
115:8054dbadfaa0
Child:
117:8fd05113efc1
Commit message:
refactoring main, atcmd, BLE and WiFi. added data structures into main and replaced values with references in main. tested and works.

Changed in this revision

source/BleManager.cpp Show annotated file Show diff for this revision Revisions of this file
source/BleManager.h Show annotated file Show diff for this revision Revisions of this file
source/WiFiManager.cpp Show annotated file Show diff for this revision Revisions of this file
source/WiFiManager.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/BleManager.cpp	Sun May 19 11:25:28 2019 +0000
+++ b/source/BleManager.cpp	Sun May 19 13:09:27 2019 +0000
@@ -61,7 +61,7 @@
  *  your application is interested in.
  */
 SMDevice::SMDevice(BLE &ble, events::EventQueue &event_queue, 
-                   BLEProtocol::AddressBytes_t &peer_address, ble_config_t ble_config) :
+                   BLEProtocol::AddressBytes_t &peer_address, ble_config_t *ble_config) :
         _ble(ble),
         _event_queue(event_queue),
         _peer_address(peer_address),
@@ -202,7 +202,7 @@
         true,
         false,
         SecurityManager::IO_CAPS_DISPLAY_ONLY, // SecurityManager::IO_CAPS_NONE
-        ble_config.pairingKey,
+        ble_config->pairingKey,
         false,
         db_path
     );
@@ -384,7 +384,7 @@
 
 /** A peripheral device will advertise, accept the connection and request
  * a change in link security. */
-SMDevicePeripheral::SMDevicePeripheral(BLE &ble, events::EventQueue &event_queue, BLEProtocol::AddressBytes_t &peer_address, ble_config_t ble_config)
+SMDevicePeripheral::SMDevicePeripheral(BLE &ble, events::EventQueue &event_queue, BLEProtocol::AddressBytes_t &peer_address, ble_config_t *ble_config)
         : SMDevice(ble, event_queue, peer_address, ble_config) { }
 
 void SMDevicePeripheral::start()
@@ -401,8 +401,8 @@
     /* add device name */
     advertising_data.addData(
         GapAdvertisingData::COMPLETE_LOCAL_NAME,
-        (const uint8_t *)ble_config.deviceName,
-        strlen(ble_config.deviceName)
+        (const uint8_t *)ble_config->deviceName,
+        strlen(ble_config->deviceName)
         );
     /* Setup primary service */
     uart = new UARTService(_ble);
@@ -435,8 +435,8 @@
      * increases the chances of being seen at the cost of more power */
     //_ble.gap().setAdvertisingInterval(20);
     //_ble.gap().setAdvertisingTimeout(0);
-    _ble.gap().setAdvertisingInterval(ble_config.advInterval); /* setting in ble_config */
-    _ble.gap().setAdvertisingTimeout(ble_config.advTimeout);   /* setting in ble_config */
+    _ble.gap().setAdvertisingInterval(ble_config->advInterval); /* setting in ble_config */
+    _ble.gap().setAdvertisingTimeout(ble_config->advTimeout);   /* setting in ble_config */
 
     error = _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE);
 
@@ -520,7 +520,7 @@
 
 /** A central device will scan, connect to a peer and request pairing. */
 
-SMDeviceCentral::SMDeviceCentral(BLE &ble, events::EventQueue &event_queue, BLEProtocol::AddressBytes_t &peer_address, ble_config_t ble_config)
+SMDeviceCentral::SMDeviceCentral(BLE &ble, events::EventQueue &event_queue, BLEProtocol::AddressBytes_t &peer_address, ble_config_t *ble_config)
     : SMDevice(ble, event_queue, peer_address, ble_config) { };
 
 void SMDeviceCentral::start()
--- a/source/BleManager.h	Sun May 19 11:25:28 2019 +0000
+++ b/source/BleManager.h	Sun May 19 13:09:27 2019 +0000
@@ -55,7 +55,7 @@
 {
 public:
     SMDevice(BLE &ble, events::EventQueue &event_queue, 
-             BLEProtocol::AddressBytes_t &peer_address, ble_config_t ble_config);
+             BLEProtocol::AddressBytes_t &peer_address, ble_config_t *ble_config);
 
     virtual ~SMDevice();
 
@@ -134,7 +134,7 @@
 
 protected:
     BLE &_ble;
-    ble_config_t ble_config;
+    ble_config_t *ble_config;
     events::EventQueue &_event_queue;
     BLEProtocol::AddressBytes_t &_peer_address;
     ble::connection_handle_t _handle;
@@ -146,7 +146,7 @@
 class SMDevicePeripheral : public SMDevice {
 public:
     SMDevicePeripheral(BLE &ble, events::EventQueue &event_queue, 
-                       BLEProtocol::AddressBytes_t &peer_address, ble_config_t ble_config);
+                       BLEProtocol::AddressBytes_t &peer_address, ble_config_t *ble_config);
 
     virtual void start();
 
@@ -162,7 +162,7 @@
 class SMDeviceCentral : public SMDevice {
 public:
     SMDeviceCentral(BLE &ble, events::EventQueue &event_queue, 
-                    BLEProtocol::AddressBytes_t &peer_address, ble_config_t ble_config);
+                    BLEProtocol::AddressBytes_t &peer_address, ble_config_t *ble_config);
 
     virtual void start();
 
--- a/source/WiFiManager.cpp	Sun May 19 11:25:28 2019 +0000
+++ b/source/WiFiManager.cpp	Sun May 19 13:09:27 2019 +0000
@@ -4,7 +4,8 @@
 #define FILE_CODE       "wifi"
 #define USE_EVENTS_FOR_HTTPS_REQUESTS
 
-WiFiManager::WiFiManager(wifi_config_t wifi_config, WiFiInterface *wifi, 
+WiFiManager::WiFiManager(wifi_config_t *wifi_config, WiFiInterface *wifi, 
+                         internet_config_t *internet_config, 
                          events::EventQueue &event_queue, 
                          MemoryPool<wifi_cmd_message_t, 16> *aT2WiFimPool, 
                          Queue<wifi_cmd_message_t, 16> *aT2WiFiCmdQueue, 
@@ -17,6 +18,7 @@
 :
      wifi_config(wifi_config),
      network(wifi),
+     internet_config(internet_config),
     _event_queue(event_queue),
     _aT2WiFimPool(aT2WiFimPool),
     _aT2WiFiCmdQueue(aT2WiFiCmdQueue),
@@ -33,7 +35,7 @@
 {
     lastScanCount = 0;
     wifiCmd = WIFI_CMD_NONE;
-    internet_config.connectionScheme = ALWAYS_CONNECTED; // set default connection scheme
+    //internet_config.connectionScheme = ALWAYS_CONNECTED; // set default connection scheme
     is_connected = false;
     http_response = NULL;
     chunkNum = 0;
@@ -487,8 +489,8 @@
 
 void WiFiManager::set_WIFI_SSID(char * wifi_ssid)
 {
-    strcpy(wifi_config.ssid, wifi_ssid);
-    dbg_printf(LOG, "[WIFI-MAN]  wifi_ssid set to %s\n", wifi_config.ssid);
+    strcpy(wifi_config->ssid, wifi_ssid);
+    dbg_printf(LOG, "[WIFI-MAN]  wifi_ssid set to %s\n", wifi_config->ssid);
     https_connection_active = false; // reset whenever any of the security credentials change
     delete socket;
 }
@@ -496,7 +498,7 @@
 
 void WiFiManager::set_WIFI_PASSWORD(char * wifi_pass)
 {
-    strcpy(wifi_config.pass, wifi_pass);
+    strcpy(wifi_config->pass, wifi_pass);
     dbg_printf(LOG, "[WIFI-MAN]  wifi_pass set to %s\n", "****************");
     https_connection_active = false; // reset whenever any of the security credentials change
     delete socket;
@@ -505,8 +507,8 @@
 
 void WiFiManager::set_WIFI_SECURITY(nsapi_security_t wifi_security)
 {
-    wifi_config.security = wifi_security;
-    dbg_printf(LOG, "[WIFI-MAN]  wifi_security set to %s\n", sec2str(wifi_config.security));
+    wifi_config->security = wifi_security;
+    dbg_printf(LOG, "[WIFI-MAN]  wifi_security set to %s\n", sec2str(wifi_config->security));
     https_connection_active = false; // reset whenever any of the security credentials change
     delete socket;
 }
@@ -529,14 +531,14 @@
     network->add_dns_server(*addr);
 #endif
     char * serverAddress = new char[100];
-    char *p = strstr(internet_config.url, "//");
+    char *p = strstr(internet_config->url, "//");
     if(p != NULL && use_full_hostname == false)
     {
         strcpy(serverAddress,p+2);
     }
     else
     {
-        strcpy(serverAddress,internet_config.url);
+        strcpy(serverAddress,internet_config->url);
     }
     value_or_error = network->gethostbyname_async(serverAddress, 
                                          callback(this, &WiFiManager::gethostbyname_callback), 
@@ -562,14 +564,14 @@
 void WiFiManager::set_internet_config()
 {
     internet_config_t *internet_cfg  = (internet_config_t *) data_msg->buffer;
-    internet_config.peer_id          = internet_cfg->peer_id;
-    strncpy(internet_config.url,internet_cfg->url, strlen(internet_cfg->url)+1);
-    internet_config.connectionScheme = internet_cfg->connectionScheme;
+    internet_config->peer_id          = internet_cfg->peer_id;
+    strncpy(internet_config->url,internet_cfg->url, strlen(internet_cfg->url)+1);
+    internet_config->connectionScheme = internet_cfg->connectionScheme;
     free_DataMsg();
     dbg_printf(LOG, "[WIFI MAN] Internet configuration setup completed\n"); 
-    dbg_printf(LOG, "peer_id = %1d, url = %s, connScheme = %1d\n", internet_config.peer_id, 
-                                                      internet_config.url, 
-                                                      internet_config.connectionScheme);
+    dbg_printf(LOG, "peer_id = %1d, url = %s, connScheme = %1d\n", internet_config->peer_id, 
+                                                      internet_config->url, 
+                                                      internet_config->connectionScheme);
     if(https_connection_active)
     {
         https_connection_active = false; // reset whenever any of the security credentials change
@@ -665,7 +667,7 @@
             case WIFI_SSID:
                 sprintf(nextStrPtr, "\r\n%s%d,%s\r\n", WIFI_NETWORK_STATUS, 
                                                        status_id,
-                                                       wifi_config.ssid);
+                                                       wifi_config->ssid);
                 break;
             case WIFI_BSSID:
                 sprintf(nextStrPtr, "\r\n%s%d,%s\r\n", WIFI_NETWORK_STATUS, 
@@ -721,7 +723,7 @@
         case NSAPI_STATUS_GLOBAL_UP:
             dbg_printf(LOG, "Global IP address set!\r\n");
             dbg_printf(LOG, "[WIFI-MAN] IP address: %s\n", network->get_ip_address());
-            dbg_printf(LOG, "[WIFI-MAN] Connected to the network %s\n", wifi_config.ssid);
+            dbg_printf(LOG, "[WIFI-MAN] Connected to the network %s\n", wifi_config->ssid);
             responseString = (char *) malloc(MAX_RESPONSE_STRING_LEN); 
             sprintf(responseString, "\r\n%s%d,%s,%d\r\n", WIFI_LINK_ENABLED, 
                                                           WIFI_CHANNEL,
@@ -736,7 +738,7 @@
             dbg_printf(LOG, "\n [WIFI-MAN] No connection to network!\n");
             is_connected = false;
             // attempt reconnection if always connected scheme is set
-            if(internet_config.connectionScheme == ALWAYS_CONNECTED)
+            if(internet_config->connectionScheme == ALWAYS_CONNECTED)
             {
                 wifiBusy = 1;
                 nsapi_error_t error;
@@ -774,12 +776,12 @@
         return error;
     }
     dbg_printf(LOG, "[WIFI-MAN] Connecting to network ssid = %s passwd = %s  security = %s \r\n", 
-                                                    wifi_config.ssid, 
+                                                    wifi_config->ssid, 
                                                     "****************", 
-                                                    sec2str(wifi_config.security));
-    error = network->connect(wifi_config.ssid,
-                     wifi_config.pass,
-                     wifi_config.security);
+                                                    sec2str(wifi_config->security));
+    error = network->connect(wifi_config->ssid,
+                             wifi_config->pass,
+                             wifi_config->security);
     dbg_printf(LOG, "[WIFI-MAN] network->connect called. error = %d\r\n", error);
     return error;
 }
@@ -795,16 +797,16 @@
     if(is_connected && result>=0)
     {
         memcpy(&responseBytes[i], address->get_ip_bytes(), 4); // remote IPv4 address
-        strcpy(internet_config.remote_IPv4Address, address->get_ip_address());
+        strcpy(internet_config->remote_IPv4Address, address->get_ip_address());
         i +=4;
         uint16_t port = address->get_port();
-        internet_config.remote_port = port;
+        internet_config->remote_port = port;
         memcpy(&responseBytes[i], &port, 2); // remote IPv4 port #
         i +=2;
         // local IPv4 address
         int ipAddr[4];
-        strcpy(internet_config.local_IPv4Address, network->get_ip_address());
-        sscanf(internet_config.local_IPv4Address, "%d.%d.%d.%d", &ipAddr[0], &ipAddr[1], 
+        strcpy(internet_config->local_IPv4Address, network->get_ip_address());
+        sscanf(internet_config->local_IPv4Address, "%d.%d.%d.%d", &ipAddr[0], &ipAddr[1], 
                                                                  &ipAddr[2], &ipAddr[3]);
         responseBytes[i++] = (uint8_t) ipAddr[0];
         responseBytes[i++] = (uint8_t) ipAddr[1];
@@ -856,10 +858,10 @@
                                                               IP_PEER_HANDLE,
                                                               IPv4_CONNECTION,
                                                               TCP_PROTOCOL,
-                                                              internet_config.local_IPv4Address,
+                                                              internet_config->local_IPv4Address,
                                                               DEFAULT_LOCAL_PORT,
-                                                              internet_config.remote_IPv4Address,
-                                                              internet_config.remote_port);
+                                                              internet_config->remote_IPv4Address,
+                                                              internet_config->remote_port);
     sendATresponseString(AT_EVENT);
 }
 
@@ -1072,14 +1074,14 @@
     error = socket->getpeername(address);
     if(error>=0)   
     {
-        strcpy(internet_config.remote_IPv4Address, address->get_ip_address());
+        strcpy(internet_config->remote_IPv4Address, address->get_ip_address());
         uint16_t port = address->get_port();
-        internet_config.remote_port = port;
+        internet_config->remote_port = port;
     }
     else  
     {
-        strcpy(internet_config.remote_IPv4Address, "");
-        internet_config.remote_port = 0;
+        strcpy(internet_config->remote_IPv4Address, "");
+        internet_config->remote_port = 0;
     }
     delete address;
 }
@@ -1108,11 +1110,11 @@
     http_req_cfg = (http_request_t *) data_msg->buffer;
 #ifdef FULL_DEBUG_ENABLED
     dbg_printf(LOG, "\n[WIFI MAN] uri = %s\n", http_req_cfg->request_URI);
-    dbg_printf(LOG, "\n[WIFI MAN] internet cfg url = %s\n", internet_config.url);
+    dbg_printf(LOG, "\n[WIFI MAN] internet cfg url = %s\n", internet_config->url);
 #endif
     char full_url[100];
     char host[60] ;
-    strncpy(full_url,internet_config.url, strlen(internet_config.url)+1);
+    strncpy(full_url,internet_config->url, strlen(internet_config->url)+1);
     strncpy(host,http_req_cfg->hostName, strlen(http_req_cfg->hostName)+1);
     strncat(full_url, http_req_cfg->request_URI, strlen(http_req_cfg->request_URI)+1);
 #ifdef FULL_DEBUG_ENABLED
@@ -1132,7 +1134,7 @@
         printBufferInHex(http_req_cfg->body, bodyLen);
     }
 #endif    
-    if(strstr(internet_config.url, "http:")!=NULL) // http request
+    if(strstr(internet_config->url, "http:")!=NULL) // http request
     {
         http_request = new HttpRequest(network,  
                                        http_req_cfg->method, 
--- a/source/WiFiManager.h	Sun May 19 11:25:28 2019 +0000
+++ b/source/WiFiManager.h	Sun May 19 13:09:27 2019 +0000
@@ -19,7 +19,8 @@
 extern void print_memory_info();
 class WiFiManager {
 public:
-    WiFiManager(wifi_config_t wifi_config, WiFiInterface *wifi, 
+    WiFiManager(wifi_config_t *wifi_config, WiFiInterface *wifi, 
+                internet_config_t *internet_config, 
                 events::EventQueue &event_queue, 
                 MemoryPool<wifi_cmd_message_t, 16> *aT2WiFimPool, 
                 Queue<wifi_cmd_message_t, 16> *aT2WiFiCmdQueue, 
@@ -36,8 +37,8 @@
 
 private:
     Mutex _wmutex; // Protect wifi thread
-    wifi_config_t     wifi_config;
-    internet_config_t internet_config;
+    wifi_config_t     *wifi_config;
+    internet_config_t *internet_config;
     // define event queue
     events::EventQueue &_event_queue;
     WiFiInterface *network;
--- a/source/common_types.h	Sun May 19 11:25:28 2019 +0000
+++ b/source/common_types.h	Sun May 19 13:09:27 2019 +0000
@@ -17,6 +17,8 @@
 #define MAX_HTTP_HEADER_LINES
 #define MAX_URL_LEN          100
 #define MAX_IPv4_LEN         16
+#define MAX_CLOUD_USER_ID_LEN   32
+#define MAX_CLOUD_PASSWORD_LEN  32
 
 /** ble configuration structure
 */
@@ -28,6 +30,13 @@
     uint8_t   pairingKey[6];  /* pairing Key */
 } ble_config_t;
 
+/** login token configuration structure
+*/
+typedef struct  {
+    char             userid[MAX_CLOUD_USER_ID_LEN];  /* cloud login userid */
+    char             pass[MAX_CLOUD_PASSWORD_LEN]; /* cloud login password  */
+} login_config_t;
+
 /** wifi configuration structure
 */
 typedef struct  {
@@ -36,13 +45,6 @@
     nsapi_security_t security;  /* WiFi security */
 } wifi_config_t;
 
-/** application configuration structure
-*/
-typedef struct  {
-    wifi_config_t wifi_config; /* wifi configuration */
-    ble_config_t ble_config;   /* ble configuration */
-} app_config_t;
-
 
 typedef enum
 {
@@ -221,4 +223,15 @@
     uint16_t      remote_port;                        /* remote port     */
 } internet_config_t;
 
+
+/** application configuration structure
+*/
+typedef struct  {
+    wifi_config_t     wifi_config;      /* wifi configuration */
+    ble_config_t      ble_config;       /* ble configuration */
+    internet_config_t internet_config;  /* cloud configuration */
+    login_config_t    login_config;     /* cloud login credentials */
+} app_config_t;
+
+
 #endif  // __COMMON_TYPES_H__
\ No newline at end of file
--- a/source/main-https.cpp	Sun May 19 11:25:28 2019 +0000
+++ b/source/main-https.cpp	Sun May 19 13:09:27 2019 +0000
@@ -30,8 +30,9 @@
 main_states_t mainLoop;
 static RawSerial *device; // tx, rx
 
+static app_config_t app_config;
 // wifi configuration
-static wifi_config_t wifi_config;
+//static wifi_config_t wifi_config;
 // wifi interface pointer
 static WiFiInterface *network;
 // wifi manager pointer
@@ -42,7 +43,11 @@
 BLE& _ble = BLE::Instance();
 
 // BLE configuration
-static ble_config_t ble_config;
+//static ble_config_t ble_config;
+
+// internet/cloud configuration
+//internet_config_t internet_config;
+
 const uint8_t pairingPassword[6] = "1101";
 // BLE peripheral pointer
 static SMDevicePeripheral *peripheral;
@@ -164,20 +169,28 @@
 
 void setupDefaultBleConfig()
 {
-    strcpy(ble_config.deviceName, DEVICE_NAME_MAIN);// set BLE device name
-    ble_config.advInterval = 1000;             // set advertising interval to 1 second default
-    ble_config.advTimeout = 0;                 // set advertising timeout to disabled by default
+    strcpy(app_config.ble_config.deviceName, DEVICE_NAME_MAIN);// set BLE device name
+    app_config.ble_config.advInterval = 1000;             // set advertising interval to 1 second default
+    app_config.ble_config.advTimeout = 0;                 // set advertising timeout to disabled by default
     // This works in C and C++
-    memcpy(ble_config.pairingKey, pairingPassword, 6); // 
+    memcpy(app_config.ble_config.pairingKey, pairingPassword, 6); // 
 
     //ble_config.pairingKey = pairingPassword;
 }
 
 void setupDefaultWiFiConfig()
 {
-    strcpy(wifi_config.ssid, MBED_CONF_APP_WIFI_SSID);
-    strcpy(wifi_config.pass, MBED_CONF_APP_WIFI_PASSWORD);
-    wifi_config.security = NSAPI_SECURITY_WPA_WPA2;
+    strcpy(app_config.wifi_config.ssid, MBED_CONF_APP_WIFI_SSID);
+    strcpy(app_config.wifi_config.pass, MBED_CONF_APP_WIFI_PASSWORD);
+    app_config.wifi_config.security = NSAPI_SECURITY_WPA_WPA2;
+}
+
+void setupDefaultCloudConfig()
+{
+    strcpy(app_config.internet_config.url, "tcp://https://dev2.dnanudge.io");
+    app_config.internet_config.peer_id = 0;
+    app_config.internet_config.remote_port = 443; // default HTTPS port
+    app_config.internet_config.connectionScheme = ALWAYS_CONNECTED;
 }
 
 static int reset_counter = 0;
@@ -233,7 +246,8 @@
     }
 #endif
     dbg_printf(LOG, "\r\n PERIPHERAL \r\n\r\n");
-    peripheral = new SMDevicePeripheral(_ble, eventQueue_ble, peer_address, ble_config);
+    peripheral = new SMDevicePeripheral(_ble, eventQueue_ble, peer_address, 
+                                        &app_config.ble_config);
 
     peripheral->run();
     btle_thread.start(callback(&eventQueue_ble, &EventQueue::dispatch_forever));
@@ -246,7 +260,8 @@
 
 void start_WiFi()
 {
-    wiFiManager = new WiFiManager(wifi_config, network, 
+    wiFiManager = new WiFiManager(&app_config.wifi_config, network, 
+                                  &app_config.internet_config,
                                   eventQueue_wifi,
                                   &aT2WiFimPool, &aT2WiFiCmdQueue,
                                   &wiFi2ATmPool, &wiFi2ATCmdQueue,