Okundu Omeni
/
wifi-https-ble-sm-uart-atcmd-5-13-1
this is using the mbed os version 5-13-1
Diff: source/main-https.cpp
- Revision:
- 103:7b566b522427
- Parent:
- 99:05398b3184f8
- Child:
- 105:e5ce023eee93
diff -r 9748f290a1a5 -r 7b566b522427 source/main-https.cpp --- a/source/main-https.cpp Wed Apr 24 17:34:17 2019 +0000 +++ b/source/main-https.cpp Thu May 02 21:50:17 2019 +0000 @@ -1,6 +1,7 @@ #define MBED_CONF_MBED_TRACE_ENABLE 1 #include "select-demo.h" +#include "debug.h" #if DEMO == DEMO_HTTPS @@ -11,6 +12,7 @@ #include "fault_handlers.h" //#include "BLE.h" #include "ATCmdParser.h" + //#include "BLEDevice.h" #include "LEDService.h" @@ -26,6 +28,7 @@ DigitalOut alivenessLED(LED1, 0); DigitalOut actuatedLED(LED2, 0); +#define FILE_CODE "main" static RawSerial *device; // tx, rx @@ -126,7 +129,7 @@ // perform free memory check int blockSize = 16; int i = 1; - printf("Checking memory with blocksize %d char ...\n", blockSize); + dbg_printf(LOG, "Checking memory with blocksize %d char ...\n", blockSize); while (true) { char *p = (char *) malloc(i * blockSize); if (p == NULL) @@ -134,7 +137,7 @@ free(p); ++i; } - printf("Ok for %d char\n", (i - 1) * blockSize); + dbg_printf(LOG, "Ok for %d char\n", (i - 1) * blockSize); _smutex.unlock(); } @@ -163,12 +166,12 @@ if(uartCharRcvCount == 0) { - device->printf("\nFirst Call to UART attach callback!!\n"); + dbg_printf(LOG, "\nFirst Call to UART attach callback!!\n"); } else if(uartCharRcvCount >= uartExpectedRcvCount) { - device->printf("\nNumber of Received Bytes = %d\n\n", uartCharRcvCount); - device->printf("--- Writing back received bytes --- \n"); + dbg_printf(LOG, "\nNumber of Received Bytes = %d\n\n", uartCharRcvCount); + dbg_printf(LOG, "--- Writing back received bytes --- \n"); int n; n = WriteUartBytes(RxBuffer, TX_BUFFER_LEN, uartCharRcvCount); UartBusy = false; @@ -198,16 +201,16 @@ void BackGndUartRead(uint8_t * rxBuffer, size_t bufSize, int rxLen) { UartBusy = true; - device->printf("Setting up background UART read - rxLen = %d\n", rxLen); + dbg_printf(LOG, "Setting up background UART read - rxLen = %d\n", rxLen); uartCharRcvCount = 0; if(rxLen > bufSize) { rxLen = bufSize; } uartExpectedRcvCount = rxLen; - device->printf("\nattaching to device UART\n\n"); + dbg_printf(LOG, "\nattaching to device UART\n\n"); device->attach(&UartRxcallback_ex); - device->printf("\nBackground UART read setup completed\n\n"); + dbg_printf(LOG, "\nBackground UART read setup completed\n\n"); } int ReadUartBytes(uint8_t * rxBuffer, size_t bufSize, int rxLen, bool echo) @@ -230,19 +233,19 @@ void checkUartReceive() { - //device->printf("Hello World!\n\r"); + //dbg_printf(LOG, "Hello World!\n\r"); char cbuf[100]; int rxCnt=0; while(device->readable()) { - //device->printf("uartCharRcvCount = %d\n\r", uartCharRcvCount++); + //dbg_printf(LOG, "uartCharRcvCount = %d\n\r", uartCharRcvCount++); cbuf[rxCnt++] = device->getc(); //putc(getc() + 1); // echo input back to terminal } cbuf[rxCnt] = NULL; if(rxCnt > 0) { - device->printf("received %d chars\n", rxCnt); - device->printf("%s\n", cbuf); + dbg_printf(LOG, "received %d chars\n", rxCnt); + dbg_printf(LOG, "%s\n", cbuf); } } @@ -258,7 +261,7 @@ lastTime = now; now = Kernel::get_ms_count(); callCount++; - device->printf("\nHello : %d secs elapsed : CallCount = %d \n", uint32_t(now - lastTime), callCount); + dbg_printf(LOG, "\nHello : %d secs elapsed : CallCount = %d \n", uint32_t(now - lastTime), callCount); } @@ -275,7 +278,7 @@ int n = wifi_demo_func(network); if(n > 0)// error { - device->printf("\n --- Error running wifi demo --- \n"); + dbg_printf(LOG, "\n --- Error running wifi demo --- \n"); } } @@ -285,16 +288,16 @@ int n =5; if(n > 0)// error { - device->printf("\n --- Error running wifi demo --- \n"); + dbg_printf(LOG, "\n --- Error running wifi demo --- \n"); } } void printWait(int numSecs) { - printf("Waiting for %d seconds...\n", numSecs); + dbg_printf(LOG, "Waiting for %d seconds...\n", numSecs); for(int i=0;i<numSecs;i++){ - printf("%d", i); - printf("\n"); + dbg_printf(LOG, "%d", i); + dbg_printf(LOG, "\n"); wait(0.5); eventQueue.dispatch(500); // Dispatch time - 500msec } @@ -302,17 +305,17 @@ void printWaitAbortKeyPress(int numSecs) { - printf("Waiting for %d seconds... [press key to abort]\n", numSecs); + dbg_printf(LOG, "Waiting for %d seconds... [press key to abort]\n", numSecs); char fmtstr[20]; for(int i=0;i<numSecs;i++){ - printf("%d", i); - printf("\n"); + dbg_printf(LOG, "%d", i); + dbg_printf(LOG, "\n"); sprintf(fmtstr, "BLE: loop # %d\n", i); peripheral->sendBLEUartData(fmtstr); wait(0.5); eventQueue.dispatch(500); // Dispatch time - 500msec if(device->readable()){ - printf("keypress detected aborting....\n"); + dbg_printf(LOG, "keypress detected aborting....\n"); device->getc(); break; } @@ -344,59 +347,24 @@ -#define MAX_LOOP_COUNT 3 -int ble_security_main() -{ - BLE& _ble = BLE::Instance(); - events::EventQueue queue; - -#if MBED_CONF_APP_FILESYSTEM_SUPPORT - /* if filesystem creation fails or there is no filesystem the security manager - * will fallback to storing the security database in memory */ - if (!create_filesystem()) { - printf("Filesystem creation failed, will use memory storage\r\n"); - } -#endif - int loopCount = 0; - while(1) { - { - printf("\r\n PERIPHERAL \r\n\r\n"); - SMDevicePeripheral peripheral(_ble, queue, peer_address, ble_config); - peripheral.run(); - return 0; - } - if(loopCount >= MAX_LOOP_COUNT) - { - return 0; - } - - { - printf("\r\n CENTRAL \r\n\r\n"); - SMDeviceCentral central(_ble, queue, peer_address, ble_config); - central.run(); - } - loopCount++; - printf("loop Cycle #%d\r\n", loopCount); - } - - return 0; -} void print_memory_info() { +#ifdef ENABLE_MEMORY_CHECKS // allocate enough room for every thread's stack statistics int cnt = osThreadGetCount(); mbed_stats_stack_t *stats = (mbed_stats_stack_t*) malloc(cnt * sizeof(mbed_stats_stack_t)); cnt = mbed_stats_stack_get_each(stats, cnt); for (int i = 0; i < cnt; i++) { - printf("Thread: 0x%lX, Stack size: %lu / %lu\r\n", stats[i].thread_id, stats[i].max_size, stats[i].reserved_size); + dbg_printf(LOG, "Thread: 0x%lX, Stack size: %lu / %lu\r\n", stats[i].thread_id, stats[i].max_size, stats[i].reserved_size); } free(stats); // Grab the heap statistics mbed_stats_heap_t heap_stats; mbed_stats_heap_get(&heap_stats); - printf("Heap size: %lu / %lu bytes\r\n", heap_stats.current_size, heap_stats.reserved_size); + dbg_printf(LOG, "Heap size: %lu / %lu bytes\r\n", heap_stats.current_size, heap_stats.reserved_size); +#endif } //#define DISABLE_WIFI @@ -405,21 +373,20 @@ #define BLE_STOP_START_ADV_SCAN_DEMO #define SKIP_WIFI_CONNECT_DEMO #define PAUSE_SECONDS 0 -#define PAUSE_SECONDS_BLE 2 +#define PAUSE_SECONDS_BLE 0 int main() { - //print_all_thread_info(); - //print_heap_and_isr_stack_info(); + initialise_debug(); #ifdef MBED_MAJOR_VERSION - printf("Mbed OS version %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION); + dbg_printf(LOG, "Mbed OS version %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION); #endif reset_counter++; print_memory_info(); - printf("\r\n ++++++ PROGRAM STARTING -- reset count = %d ++++++ \r\n", reset_counter); - device = new RawSerial(USBTX, USBRX, DEFAULT_BAUD_RATE); + dbg_printf(LOG, "\r\n ++++++ PROGRAM STARTING -- reset count = %d ++++++ \r\n", reset_counter); + device = new RawSerial(USBTX, USBRX, 2*DEFAULT_BAUD_RATE); //device = new RawSerial(PA_9, PA_10, DEFAULT_BAUD_RATE); - printf("*** HELLO MESSAGE *** \n %s", (char *)&hello_msg[6]); + dbg_printf(LOG, "*** HELLO MESSAGE *** \n %s", (char *)&hello_msg[6]); setupDefaultWiFiConfig(); setupDefaultBleConfig(); BLE& _ble = BLE::Instance(); @@ -428,11 +395,11 @@ /* if filesystem creation fails or there is no filesystem the security manager * will fallback to storing the security database in memory */ if (!create_filesystem()) { - printf("Filesystem creation failed, will use memory storage\r\n"); + dbg_printf(LOG, "Filesystem creation failed, will use memory storage\r\n"); } #endif print_memory_info(); - printf("\r\n PERIPHERAL \r\n\r\n"); + dbg_printf(LOG, "\r\n PERIPHERAL \r\n\r\n"); peripheral = new SMDevicePeripheral(_ble, queue, peer_address, ble_config); print_memory_info(); @@ -447,7 +414,7 @@ #ifdef DISABLE_WIFI_DEMO network = WiFiInterface::get_default_instance(); if (!network) { - printf("ERROR: No WiFiInterface found.\n"); + dbg_printf(LOG, "ERROR: No WiFiInterface found.\n"); } wiFiManager = new WiFiManager(wifi_config, network, eventQueue, @@ -459,19 +426,19 @@ #else NetworkInterface* network = connect_to_default_network_interface(); int stop = Kernel::get_ms_count(); - device->printf("\n The Wifi Network scan took %d ms or %4.1f seconds\n", (stop - start), (float)((stop - start)/1000.0)); + dbg_printf(LOG, "\n The Wifi Network scan took %d ms or %4.1f seconds\n", (stop - start), (float)((stop - start)/1000.0)); // run on separate thread; evt_thread.start(callback(wifi_demo, network)); evt_thread.join(); network->disconnect(); delete network; - device->printf("\n Wifi-Demo completed - restarting BLE \n\n"); + dbg_printf(LOG, "\n Wifi-Demo completed - restarting BLE \n\n"); #endif /// endif DISABLE_WIFI_DEMO #else - device->printf("\n Wifi Demo disabled so just waiting it out... \n\n"); + dbg_printf(LOG, "\n Wifi Demo disabled so just waiting it out... \n\n"); printWait(2); // lets wait for a minute before turning BLE back on - device->printf("\n ++++++ restarting BLE ++++++ \n\n"); + dbg_printf(LOG, "\n ++++++ restarting BLE ++++++ \n\n"); #endif /// endif DISABLE_WIFI printWaitAbortKeyPress(PAUSE_SECONDS); //peripheral->startAdvertising(); @@ -481,72 +448,72 @@ device->putc(i); } reportGapState(); - device->printf("\n\n\nEnter # of expected bytes: "); + dbg_printf(LOG, "\n\n\nEnter # of expected bytes: "); ReadUartBytes(RxBuffer, RX_BUFFER_LEN, 4, true); uint8_t rxLen = (uint8_t) (100*(RxBuffer[0]-'0') + 10*(RxBuffer[1]-'0') + (RxBuffer[2]-'0')) %256; - device->printf("\n\nExpected # of Received Bytes = %d\n", rxLen); + dbg_printf(LOG, "\n\nExpected # of Received Bytes = %d\n", rxLen); BackGndUartRead(RxBuffer, RX_BUFFER_LEN, (int) rxLen); - device->printf("\n Waiting for 5 seconds "); + dbg_printf(LOG, "\n Waiting for 5 seconds "); printWait(5); - device->printf("\n Waiting finished!!!\n Now waiting for expected bytes to be received \n\n"); + dbg_printf(LOG, "\n Waiting finished!!!\n Now waiting for expected bytes to be received \n\n"); while(UartBusy){ wait(0.1); } #endif - device->printf("\r\n++++++ Press key for Wifi demo test ++++++ \r\n"); + dbg_printf(LOG, "\r\n++++++ Press key for Wifi demo test ++++++ \r\n"); printWaitAbortKeyPress(PAUSE_SECONDS); #ifndef SKIP_WIFI_SCAN_DEMO - device->printf("\r\n++++++ Test WiFi Manager Network Scan ++++++ \r\n"); + dbg_printf(LOG, "\r\n++++++ Test WiFi Manager Network Scan ++++++ \r\n"); int count; count = wiFiManager->scanNetworks(); if (count <= 0) { - device->printf("scan() failed with return value: %d\n", count); + dbg_printf(LOG, "scan() failed with return value: %d\n", count); } else { - device->printf("\r\n++++++ Test WiFi Scan found %d networks ++++++ \r\n ++++ SUCCESS ++++\r\n", count); + dbg_printf(LOG, "\r\n++++++ Test WiFi Scan found %d networks ++++++ \r\n ++++ SUCCESS ++++\r\n", count); } #endif #ifndef SKIP_WIFI_CONNECT_DEMO - device->printf("\r\n++++++ Test WiFi Manager Network connect ++++++ \r\n"); + dbg_printf(LOG, "\r\n++++++ Test WiFi Manager Network connect ++++++ \r\n"); nsapi_error_t werror; werror = wiFiManager->connect(); if (werror < 0) { - device->printf("connect() failed with return value: %d\n", werror); + dbg_printf(LOG, "connect() failed with return value: %d\n", werror); } else { - device->printf("\r\n++++++ Test WiFi connect SUCCESSFUL ++++++ \r\n"); + dbg_printf(LOG, "\r\n++++++ Test WiFi connect SUCCESSFUL ++++++ \r\n"); } if(!werror) // connect successful - test dicsonnection { - device->printf("\r\n++++++ Test WiFi Manager Network disconnect ++++++ \r\n"); + dbg_printf(LOG, "\r\n++++++ Test WiFi Manager Network disconnect ++++++ \r\n"); werror = wiFiManager->disconnect(); if (werror) { - device->printf("disconnect() failed with return value: %d\n", werror); + dbg_printf(LOG, "disconnect() failed with return value: %d\n", werror); } else { - device->printf("\r\n++++++ Test WiFi disconnect SUCCESSFUL ++++++ \r\n"); + dbg_printf(LOG, "\r\n++++++ Test WiFi disconnect SUCCESSFUL ++++++ \r\n"); } } #endif //wiFiManager->runMain(); - device->printf("\r\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++ \r\n"); - device->printf("\r\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++ \r\n"); - device->printf("\r\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++ \r\n"); - device->printf("\r\n++++++ Test WiFi Manager Network scan from thread ++++++ \r\n"); - device->printf("\r\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++ \r\n"); - device->printf("\r\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++ \r\n"); - device->printf("\r\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++ \r\n"); + dbg_printf(LOG, "\r\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++ \r\n"); + dbg_printf(LOG, "\r\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++ \r\n"); + dbg_printf(LOG, "\r\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++ \r\n"); + dbg_printf(LOG, "\r\n++++++ Test WiFi Manager Network scan from thread ++++++ \r\n"); + dbg_printf(LOG, "\r\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++ \r\n"); + dbg_printf(LOG, "\r\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++ \r\n"); + dbg_printf(LOG, "\r\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++ \r\n"); wifi_thread.start(callback(wiFiManager, &WiFiManager::runMain)); - printf("\r\n after starting wifi thread \r\n"); + dbg_printf(LOG, "\r\n after starting wifi thread \r\n"); print_memory_info(); // dispatch event queue on event thread - printf("\r\n [MAIN THREAD] Thread Id = %X\r\n", (uint32_t)ThisThread::get_id()); - printf("\r\n [EVENT THREAD] Thread Id = %X\r\n", (uint32_t)evt_thread.get_id()); + dbg_printf(LOG, "\r\n [MAIN THREAD] Thread Id = %X\r\n", (uint32_t)ThisThread::get_id()); + dbg_printf(LOG, "\r\n [EVENT THREAD] Thread Id = %X\r\n", (uint32_t)evt_thread.get_id()); evt_thread.start(callback(&eventQueue, &EventQueue::dispatch_forever)); - device->printf("\r\n++++++ Starting ATCmdmanager ++++++ \r\n"); + dbg_printf(LOG, "\r\n++++++ Starting ATCmdmanager ++++++ \r\n"); ATCmdManager *aTCmdManager = new ATCmdManager(USBTX, USBRX, peripheral, eventQueue, wiFiManager, &aT2WiFimPool, &aT2WiFiCmdQueue, @@ -556,7 +523,7 @@ false); //aTCmdManager->runMain(); atcmd_thread.start(callback(aTCmdManager, &ATCmdManager::runMain)); - printf("\r\n after starting atcmd thread \r\n"); + dbg_printf(LOG, "\r\n after starting atcmd thread \r\n"); print_memory_info(); //SCB->SHCSR |= 0x00070000;//Enable fault handler. //wiFiManager->runMain();