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

Dependencies:   mbed-http

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main-https.cpp Source File

main-https.cpp

00001 #define MBED_CONF_MBED_TRACE_ENABLE 1
00002 
00003 #include "select-demo.h"
00004 #include "debug.h"
00005 
00006 #if DEMO == DEMO_HTTPS
00007 
00008 #include <events/mbed_events.h>
00009 #include <mbed.h>
00010 #include "nvstore.h"
00011 #include "ble/BLE.h"
00012 #include "fault_handlers.h"
00013 #include "ATCmdParser.h"
00014 
00015 
00016 #include "LEDService.h"
00017 #include "ble/services/UARTService.h"
00018 #include "common_config.h"
00019 #include "common_types.h"
00020 #include "ATCmdManager.h"
00021 #include "BleManager.h"
00022 #include "WiFiManager.h"
00023 #include "mbed_memory_status.h"
00024 UARTService *uart;
00025 
00026 DigitalOut led1(LED1);
00027 DigitalOut led2(LED2);
00028 DigitalOut led3(LED3);
00029 
00030 #define FILE_CODE       "main"
00031 
00032 // NVStore is a sigleton, get its instance
00033 NVStore &nvstore = NVStore::get_instance();
00034 
00035 main_states_t mainLoop;
00036 static RawSerial *device; // tx, rx
00037 
00038 static app_config_t app_config;
00039 // wifi configuration
00040 //static wifi_config_t wifi_config;
00041 // wifi interface pointer
00042 static WiFiInterface *network;
00043 // wifi manager pointer
00044 static WiFiManager *wiFiManager;
00045 
00046 // BLE instance
00047 //BLE& _ble;
00048 BLE& _ble = BLE::Instance();
00049 
00050 // BLE configuration
00051 //static ble_config_t ble_config;
00052 
00053 // internet/cloud configuration
00054 //internet_config_t internet_config;
00055 
00056 const uint8_t pairingPassword[6] = "1101";
00057 // BLE peripheral pointer
00058 static SMDevicePeripheral *peripheral;
00059 
00060 const static char     DEVICE_NAME_MAIN[] = "UBLOX-BLE";
00061 //char buffer[BUFFER_LEN];
00062 static EventQueue eventQueue_atcmd(/* event count */ 32 * EVENTS_EVENT_SIZE);
00063 static EventQueue eventQueue_wifi(/* event count */ 64 * EVENTS_EVENT_SIZE);
00064 static EventQueue eventQueue_ble(/* event count */ 32 * EVENTS_EVENT_SIZE);
00065 
00066 /*  Queue and memory pool for AT to Wifi commands */
00067 static MemoryPool<wifi_cmd_message_t, 16> aT2WiFimPool;
00068 static Queue<wifi_cmd_message_t, 16> aT2WiFiCmdQueue;
00069 
00070 /*  Queue and memory pool for WiFi to AT commands */
00071 static MemoryPool<at_resp_message_t, 16> wiFi2ATmPool;
00072 static Queue<at_resp_message_t, 16> wiFi2ATCmdQueue;
00073 
00074 /*  Queue and memory pool for AT to WiFi data */
00075 static MemoryPool<wifi_data_msg_t, PQDSZ> aT2WiFiDatamPool;
00076 static Queue<wifi_data_msg_t, PQDSZ> aT2WiFiDataQueue;
00077 
00078 
00079 /*  Queue and memory pool for WiFi to AT data */
00080 static MemoryPool<at_data_msg_t, PQDSZ> wiFi2ATDatamPool;
00081 static Queue<at_data_msg_t, PQDSZ> wiFi2ATDataQueue;
00082 
00083 
00084 /*  Queue and memory pool for AT to BLE data */
00085 static MemoryPool<at_ble_msg_t, PQDSZ_BLE> aT2BleDatamPool;
00086 static Queue<at_ble_msg_t, PQDSZ_BLE> aT2BleDataQueue;
00087 
00088 
00089 /*  Queue and memory pool for BLE to AT data */
00090 static MemoryPool<ble_at_msg_t, PQDSZ_BLE> ble2ATDatamPool;
00091 static Queue<ble_at_msg_t, PQDSZ_BLE> ble2ATDataQueue;
00092 
00093 
00094 
00095 
00096 /* creates three tread objects with different priorities */
00097 //Thread real_time_thread(osPriorityRealtime, 1024, &rt_stk[0]);
00098 //Thread high_prio_thread(osPriorityHigh, 1024, &hp_stk[0]);
00099 //Thread low_prio_thread(osPriorityNormal, 1024, &lp_stk[0]);
00100 
00101 #ifdef USE_MAIN_THREAD_STACK
00102 // using main thread stack
00103 unsigned char btle_stk[1024];
00104 unsigned char wifi_stk[8*1024];
00105 unsigned char atcmd_stk[4*1024];
00106 Thread btle_thread(BTLE_THREAD_PRIORITY, 1024, &btle_stk[0]);
00107 Thread wifi_thread(WIFI_THREAD_PRIORITY, 8*1024, &wifi_stk[0]);
00108 Thread atcmd_thread(ATCMD_THREAD_PRIORITY, 4*1024, &atcmd_stk[0]);
00109 #else
00110 // using global heap
00111 Thread btle_thread(BTLE_THREAD_PRIORITY, 4*1024);
00112 Thread wifi_thread(WIFI_THREAD_PRIORITY, 4*1024);
00113 Thread atcmd_thread(ATCMD_THREAD_PRIORITY, 4*1024);
00114 #endif
00115 
00116 /* create a semaphore to synchronize the threads */
00117 Semaphore sync_sema;
00118 
00119 Thread atcmd_evt_thread(osPriorityNormal, 1024);
00120 Thread wifi_evt_thread;
00121 #include "network-helper.h"
00122 
00123 /* List of trusted root CA certificates
00124  * currently two: GlobalSign, the CA for os.mbed.com and Let's Encrypt, the CA for httpbin.org
00125  *
00126  * To add more root certificates, just concatenate them.
00127  */
00128 #include "https_certificates.h"
00129 
00130 // wifi demo
00131 #include "wifi_demo.h"
00132 
00133 Mutex _smutex; // Protect memory access
00134 
00135 bool ble_mgr_started;
00136 bool wifi_mgr_started;
00137 
00138 uint64_t lastTime = 0;
00139 uint64_t now = 0;
00140 uint32_t callCount = 0;
00141 
00142 
00143 // Wifi-demo
00144 void wifi_demo(NetworkInterface* network){
00145     int n = wifi_demo_func(network);
00146     if(n > 0)// error
00147     {
00148         dbg_printf(LOG, "\n --- Error running wifi demo --- \n");
00149     }
00150 }
00151 
00152 // Wifi-demo2
00153 void wifi_demo2(){
00154     //int n = wifi_demo_func(network);
00155     int n =5;
00156     if(n > 0)// error
00157     {
00158         dbg_printf(LOG, "\n --- Error running wifi demo --- \n");
00159     }
00160 }
00161 
00162 
00163 void printWaitAbortKeyPress(int numSecs, const char * printStr=NULL)
00164 {
00165 #ifndef DEBUG_ENABLED
00166     return;
00167 #endif
00168     dbg_printf(LOG, "%s", printStr);
00169     dbg_printf(LOG, "Waiting for %d seconds... [press key to abort]\n", numSecs);
00170     char fmtstr[20];
00171     int len;
00172     for(int i=0;i<numSecs;i++){
00173         dbg_printf(LOG, "%d", i);
00174         dbg_printf(LOG, "\n");
00175         sprintf(fmtstr, "BLE: loop # %d\n", i);
00176         len = strlen(fmtstr)+1;
00177         peripheral->sendBLEUartData((const uint8_t *)fmtstr, len);
00178         wait(0.5);
00179         //eventQueue_atcmd.dispatch(500);        // Dispatch time - 500msec
00180         if(device->readable()){
00181             dbg_printf(LOG, "keypress detected aborting....\n");
00182             device->getc();
00183             break;
00184         }
00185     }
00186 }
00187 
00188 
00189 
00190 void setupDefaultBleConfig()
00191 {
00192     strcpy(app_config.ble_config.deviceName, DEVICE_NAME_MAIN);// set BLE device name
00193     app_config.ble_config.advInterval = 1000;             // set advertising interval to 1 second default
00194     app_config.ble_config.advTimeout = 0;                 // set advertising timeout to disabled by default
00195     // This works in C and C++
00196     memcpy(app_config.ble_config.pairingKey, pairingPassword, 6); // 
00197 
00198     //ble_config.pairingKey = pairingPassword;
00199 }
00200 
00201 void setupDefaultWiFiConfig()
00202 {
00203     strcpy(app_config.wifi_config.ssid, MBED_CONF_APP_WIFI_SSID);
00204     strcpy(app_config.wifi_config.pass, MBED_CONF_APP_WIFI_PASSWORD);
00205     app_config.wifi_config.security = NSAPI_SECURITY_WPA_WPA2;
00206 }
00207 
00208 // Entry point for the example
00209 void print_app_config()
00210 {
00211     //dbg_printf(LOG, "Return code is %d ", rc);
00212 }
00213 
00214 // Entry point for the example
00215 void print_return_code(int rc, int expected_rc)
00216 {
00217     dbg_printf(LOG, "Return code is %d ", rc);
00218     if (rc == expected_rc)
00219         dbg_printf(LOG, "(as expected).\n");
00220     else
00221         dbg_printf(LOG, "(expected %d!).\n", expected_rc);
00222 }
00223 void setupDefaultStartupConfig()
00224 {
00225     app_config.startup_config.ble_enable = true;
00226 }
00227 
00228 void setupDefaultCloudConfig()
00229 {
00230     strcpy(app_config.internet_config.url, "tcp://https://dev2.dnanudge.io");
00231     app_config.internet_config.peer_id = 0;
00232     app_config.internet_config.remote_port = 443; // default HTTPS port
00233     app_config.internet_config.connectionScheme = ALWAYS_CONNECTED;
00234 }
00235 
00236 void setupDefaultUartConfig()
00237 {
00238     app_config.uart_config.baudrate             = 2*DEFAULT_BAUD_RATE;
00239     app_config.uart_config.flow_ctrl            = 2;
00240     app_config.uart_config.data_bits            = 8;
00241     app_config.uart_config.stop_bits            = 1;
00242     app_config.uart_config.parity               = 1;
00243     app_config.uart_config.change_after_confirm = 1;
00244 }
00245 
00246 void resetConfiguration()
00247 {
00248     int rc;
00249     // Clear NVStore data. Should only be done once at factory configuration
00250     rc = nvstore.reset();
00251     dbg_printf(LOG, "Reset NVStore. ");
00252     print_return_code(rc, NVSTORE_SUCCESS);
00253 }
00254 
00255 bool deleteConfiguration(nvstore_key_t configKey)
00256 {
00257     int rc;
00258     // Clear NVStore data. Should only be done once at factory configuration
00259     rc = nvstore.remove(configKey);
00260     dbg_printf(LOG, "Deleted config key %d from NVStore. ", (int)configKey);
00261     print_return_code(rc, NVSTORE_SUCCESS);
00262     return (rc == NVSTORE_SUCCESS);
00263 }
00264 
00265 void saveConfiguration(nvstore_key_t configKey)
00266 {
00267     int rc;
00268     rc = nvstore.set((uint16_t)configKey, sizeof(app_config), &app_config);
00269     print_return_code(rc, NVSTORE_SUCCESS);
00270 }
00271 
00272 bool loadConfiguration(nvstore_key_t configKey)
00273 {
00274     int rc;
00275     // Get the value of this key (should be 3000)
00276     uint16_t actual_len_bytes;
00277     rc = nvstore.get((uint16_t)configKey, sizeof(app_config), &app_config, actual_len_bytes);
00278     print_app_config();
00279     print_return_code(rc, NVSTORE_SUCCESS);
00280     return (rc == NVSTORE_SUCCESS && (sizeof(app_config) == actual_len_bytes));
00281 }
00282 
00283 static int reset_counter = 0;
00284 
00285 
00286 
00287 //#define ENABLE_MEMORY_CHECKS
00288 
00289 void print_memory_info() {
00290 #ifdef ENABLE_MEMORY_CHECKS
00291     // allocate enough room for every thread's stack statistics
00292     int cnt = osThreadGetCount();
00293     mbed_stats_stack_t *stats = (mbed_stats_stack_t*) malloc(cnt * sizeof(mbed_stats_stack_t));
00294  
00295     cnt = mbed_stats_stack_get_each(stats, cnt);
00296     for (int i = 0; i < cnt; i++) {
00297         dbg_printf(LOG, "Thread: 0x%lX, Stack size: %lu / %lu\r\n", stats[i].thread_id, stats[i].max_size, stats[i].reserved_size);
00298     }
00299     free(stats);
00300  
00301     // Grab the heap statistics
00302     mbed_stats_heap_t heap_stats;
00303     mbed_stats_heap_get(&heap_stats);
00304     dbg_printf(LOG, "Heap size: %lu / %lu bytes\r\n", heap_stats.current_size, heap_stats.reserved_size);
00305 #endif
00306 }
00307 
00308 void blinkLEDs()
00309 {
00310     static int cnt =0;
00311     cnt++;
00312     if(cnt == 3){
00313         cnt = 0;
00314     }
00315     if(cnt==0)
00316         led1 = !led1;
00317     //wait(1.0);
00318     if(cnt==1)
00319         led2 = !led2;
00320     //wait(1.0);
00321     if(cnt==2)
00322         led3 = !led3;
00323 }
00324 
00325 void start_BLE()
00326 {
00327     //_ble = BLE::Instance();
00328 #if MBED_CONF_APP_FILESYSTEM_SUPPORT
00329     /* if filesystem creation fails or there is no filesystem the security manager
00330      * will fallback to storing the security database in memory */
00331     if (!create_filesystem()) {
00332         dbg_printf(LOG, "Filesystem creation failed, will use memory storage\r\n");
00333     }
00334 #endif
00335     dbg_printf(LOG, "\r\n PERIPHERAL \r\n\r\n");
00336     peripheral = new SMDevicePeripheral(_ble, eventQueue_ble, peer_address, 
00337                                         &aT2BleDatamPool, &aT2BleDataQueue,
00338                                         &ble2ATDatamPool, &ble2ATDataQueue,
00339                                         &app_config.ble_config);
00340 
00341     peripheral->run();
00342     btle_thread.start(callback(&eventQueue_ble, &EventQueue::dispatch_forever));
00343     ble_mgr_started = true;
00344     wait_ms(10);
00345 }
00346 
00347 void stop_BLE()
00348 {
00349     btle_thread.terminate();
00350     delete peripheral;
00351     ble_mgr_started = false;
00352     wait_ms(10);
00353 }
00354 
00355 void start_WiFi()
00356 {
00357     wiFiManager = new WiFiManager(&app_config.wifi_config, network, 
00358                                   &app_config.internet_config,
00359                                   eventQueue_wifi,
00360                                   &aT2WiFimPool, &aT2WiFiCmdQueue,
00361                                   &wiFi2ATmPool, &wiFi2ATCmdQueue,
00362                                   &aT2WiFiDatamPool, &aT2WiFiDataQueue,
00363                                   &wiFi2ATDatamPool, &wiFi2ATDataQueue
00364                                   );
00365     dbg_printf(LOG, "\r\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++ \r\n");
00366     dbg_printf(LOG, "\r\n++++++ Test WiFi Manager Network scan from thread ++++++ \r\n");
00367     dbg_printf(LOG, "\r\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++ \r\n");
00368     wifi_thread.start(callback(wiFiManager, &WiFiManager::runMain));
00369     dbg_printf(LOG, "\r\n after starting wifi thread \r\n");
00370     printWaitAbortKeyPress(120, "Wait after WiFi Manager dispatch\r\n");
00371     // dispatch wifi event queue on event thread
00372     wifi_evt_thread.start(callback(&eventQueue_wifi, &EventQueue::dispatch_forever));
00373     wifi_mgr_started = true;
00374 }
00375 
00376 void stop_WiFi()
00377 {
00378     wifi_evt_thread.terminate();
00379     wifi_thread.terminate();
00380     delete wiFiManager;
00381     delete network;
00382     wifi_mgr_started = false;
00383 }
00384 
00385 
00386 void trigger_start_BLE()
00387 {
00388     mainLoop = START_BLE;
00389 }
00390 
00391 
00392 void trigger_stop_BLE()
00393 {
00394     mainLoop = STOP_BLE;
00395 }
00396 
00397 
00398 void trigger_start_WiFi()
00399 {
00400     mainLoop = START_WIFI;
00401 }
00402 
00403 
00404 void trigger_stop_WiFi()
00405 {
00406     mainLoop = STOP_WIFI;
00407 }
00408 
00409 //#define USE_DEFAULT_CONFIGURATION
00410 //#define TEST_NVSTORE
00411 //#define STARTUP_DEBUG_ENABLE
00412 //#define AUTO_START_BLE_MANAGER
00413 //#define AUTO_START_WIFI_MANAGER
00414 #define PAUSE_SECONDS   0
00415 #define PAUSE_SECONDS_BLE 0
00416 int main() {
00417 #ifndef USE_DEFAULT_CONFIGURATION
00418     if(loadConfiguration(APP_CONFIG_0) == false)
00419     {
00420         setupDefaultStartupConfig();
00421         setupDefaultUartConfig();
00422         setupDefaultWiFiConfig();
00423         setupDefaultBleConfig();
00424         setupDefaultCloudConfig();
00425     }
00426     if(app_config.startup_config.ble_enable)
00427     {
00428         trigger_start_BLE();
00429     }
00430 #else
00431     setupDefaultUartConfig();
00432     setupDefaultWiFiConfig();
00433     setupDefaultBleConfig();
00434     setupDefaultCloudConfig();
00435 #endif
00436     device = new RawSerial(USBTX, USBRX, app_config.uart_config.baudrate);
00437 #if defined (STARTUP_DEBUG_ENABLE) || defined (DEBUG_ENABLED)
00438     uint8_t debug_level = (LOG | ERR | TXT | DBG);
00439     initialise_debug(debug_level);
00440 #else
00441     initialise_debug(NONE);
00442 #endif
00443 #ifdef MBED_MAJOR_VERSION
00444     dbg_printf(LOG, "Mbed OS version %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
00445 #endif
00446 #ifdef TEST_NVSTORE
00447     app_config_t *current_config = new app_config_t;
00448     memcpy(current_config, &app_config, sizeof(app_config_t));
00449     saveConfiguration(APP_CONFIG_0);
00450     memset(&app_config, 0, sizeof(app_config_t));
00451     loadConfiguration(APP_CONFIG_0);
00452     int cmp;
00453     cmp = memcmp(current_config, &app_config, sizeof(app_config_t));
00454     if(cmp == 0)
00455     {
00456         dbg_printf(LOG, " NVSTRORE TEST Passed!!\r\n");
00457     }
00458     else
00459     {
00460         dbg_printf(LOG, " NVSTRORE TEST Failed!!\r\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
00461     }
00462     delete current_config;
00463 #endif
00464     
00465     reset_counter++;
00466     dbg_printf(LOG, "\r\n ++++++ PROGRAM STARTING -- reset count = %d ++++++ \r\n", reset_counter);
00467 #ifdef AUTO_START_BLE_MANAGER
00468     //btle_thread.start(callback(peripheral, &SMDevicePeripheral::run));
00469     start_BLE();
00470     printWaitAbortKeyPress(120, "Wait after BLE dispatch\r\n");
00471 #endif
00472     //int start = Kernel::get_ms_count();
00473 #ifdef AUTO_START_WIFI_MANAGER
00474     start_WiFi();
00475     printWaitAbortKeyPress(120, "Wait after WiFi instantiation\r\n");
00476 #endif
00477     // dispatch atcmd event queue on event thread
00478     atcmd_evt_thread.start(callback(&eventQueue_atcmd, &EventQueue::dispatch_forever));
00479     dbg_printf(LOG, "\r\n++++++ Starting ATCmdmanager ++++++ \r\n");
00480     ATCmdManager *aTCmdManager = new ATCmdManager(USBTX, USBRX, &app_config.uart_config, 
00481                                                 eventQueue_atcmd, wiFiManager, 
00482                                                 &aT2WiFimPool, &aT2WiFiCmdQueue,
00483                                                 &wiFi2ATmPool, &wiFi2ATCmdQueue,
00484                                                 &aT2WiFiDatamPool, &aT2WiFiDataQueue,
00485                                                 &wiFi2ATDatamPool, &wiFi2ATDataQueue,
00486                                                 &aT2BleDatamPool, &aT2BleDataQueue,
00487                                                 &ble2ATDatamPool, &ble2ATDataQueue,
00488                                                 &app_config.startup_config, false);
00489     atcmd_thread.start(callback(aTCmdManager, &ATCmdManager::runMain));
00490     dbg_printf(LOG, "\r\n after starting atcmd thread \r\n");
00491     print_memory_info();
00492 #ifdef STARTUP_DEBUG_ENABLE
00493     initialise_debug(NONE);
00494 #endif
00495     while(1)
00496     {
00497         switch(mainLoop)
00498         {
00499             case MAIN_IDLE:
00500                 break;
00501             case START_BLE:
00502                 start_BLE();
00503                 mainLoop = MAIN_IDLE;
00504                 break;
00505             case START_WIFI:
00506                 if(!ble_mgr_started)
00507                 {
00508                     start_BLE();
00509                 }
00510                 start_WiFi();
00511                 mainLoop = MAIN_IDLE;
00512                 break;
00513             case STOP_BLE:
00514                 stop_BLE();
00515                 mainLoop = MAIN_IDLE;
00516                 break;
00517             case STOP_WIFI:
00518                 stop_WiFi();
00519                 mainLoop = MAIN_IDLE;
00520                 break;
00521             default:
00522                 mainLoop = MAIN_IDLE;
00523                 break;
00524         }
00525         wait(1);
00526     }
00527 }
00528 
00529 #endif