this is using the mbed os version 5-13-1
Diff: source/main-https.cpp
- Branch:
- PassingRegression
- Revision:
- 112:a0999ea4ece0
- Parent:
- 109:c274780ff609
- Child:
- 113:888e262ff0a9
--- a/source/main-https.cpp Fri May 10 13:45:50 2019 +0000
+++ b/source/main-https.cpp Sat May 11 11:55:29 2019 +0000
@@ -5,15 +5,12 @@
#if DEMO == DEMO_HTTPS
-//#include "mbed.h"
#include <events/mbed_events.h>
#include <mbed.h>
#include "ble/BLE.h"
#include "fault_handlers.h"
-//#include "BLE.h"
#include "ATCmdParser.h"
-//#include "BLEDevice.h"
#include "LEDService.h"
#include "ble/services/UARTService.h"
@@ -25,8 +22,6 @@
#include "mbed_memory_status.h"
UARTService *uart;
-//DigitalOut alivenessLED(LED1, 0);
-//DigitalOut actuatedLED(LED2, 0);
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
@@ -45,23 +40,14 @@
// BLE configuration
static ble_config_t ble_config;
const uint8_t pairingPassword[6] = "1101";
-// BLE interface pointer
-//BLE &_ble;
// BLE peripheral pointer
static SMDevicePeripheral *peripheral;
const static char DEVICE_NAME_MAIN[] = "UBLOX-BLE";
-static const uint16_t uuid16_list[] = {LEDService::LED_SERVICE_UUID};
char buffer[BUFFER_LEN];
-#ifdef ENABLE_UART_BACKGRND_DEMO
-uint8_t TxBuffer[TX_BUFFER_LEN];
-uint8_t RxBuffer[RX_BUFFER_LEN];
-#endif
static EventQueue eventQueue_atcmd(/* event count */ 32 * EVENTS_EVENT_SIZE);
static EventQueue eventQueue_wifi(/* event count */ 32 * EVENTS_EVENT_SIZE);
-LEDService *ledServicePtr;
-
/* Queue and memory pool for AT to Wifi commands */
static MemoryPool<wifi_cmd_message_t, 16> aT2WiFimPool;
static Queue<wifi_cmd_message_t, 16> aT2WiFiCmdQueue;
@@ -82,11 +68,6 @@
-/* allocate statically stacks for the three threads */
-//unsigned char rt_stk[1024];
-//unsigned char hp_stk[1024];
-//unsigned char lp_stk[1024];
-
/* creates three tread objects with different priorities */
//Thread real_time_thread(osPriorityRealtime, 1024, &rt_stk[0]);
//Thread high_prio_thread(osPriorityHigh, 1024, &hp_stk[0]);
@@ -125,156 +106,10 @@
#include "wifi_demo.h"
Mutex _smutex; // Protect memory access
-// check free memory
-
-void performFreeMemoryCheck()
-{
- _smutex.lock();
- // perform free memory check
- int blockSize = 16;
- int i = 1;
- dbg_printf(LOG, "Checking memory with blocksize %d char ...\n", blockSize);
- while (true) {
- char *p = (char *) malloc(i * blockSize);
- if (p == NULL)
- break;
- free(p);
- ++i;
- }
- dbg_printf(LOG, "Ok for %d char\n", (i - 1) * blockSize);
- _smutex.unlock();
-
-}
-#ifdef ENABLE_UART_BACKGRND_DEMO
-static int uartExpectedRcvCount = 0;
-static int uartCharRcvCount = 0;
-static bool UartBusy = false;
-int WriteUartBytes(const uint8_t * txBuffer, size_t bufSize, int txLen)
-{
- if(txLen > bufSize)
- {
- txLen = bufSize;
- }
- //int goodTxLen;
- //goodTxLen = _parser.write((const char *) txBuffer, txLen);
- for(int i=0;i<txLen;i++)
- {
- device->putc(txBuffer[i]);
- }
- // return number of bytes written to UART
- return (int) txLen;
-}
-
-void printUartRxResult()
-{
-
- if(uartCharRcvCount == 0)
- {
- dbg_printf(LOG, "\nFirst Call to UART attach callback!!\n");
- }
- else if(uartCharRcvCount >= uartExpectedRcvCount)
- {
- 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;
- }
-}
-
-
-void UartRxcallback_ex() {
- if(uartCharRcvCount >= uartExpectedRcvCount)
- {
- int x = device->getc();
- return;
- }
- if(uartCharRcvCount == 0)
- {
- eventQueue.call(printUartRxResult);
- }
- // Note: you need to actually read from the serial to clear the RX interrupt
- RxBuffer[uartCharRcvCount] = (uint8_t) device->getc();
- uartCharRcvCount++;
- if(uartCharRcvCount >= uartExpectedRcvCount)
- {
- //alivenessLED = !alivenessLED; /* Do blinky on LED1 to indicate system aliveness. */
- eventQueue.call(printUartRxResult);
- }
-}
-void BackGndUartRead(uint8_t * rxBuffer, size_t bufSize, int rxLen)
-{
- UartBusy = true;
- dbg_printf(LOG, "Setting up background UART read - rxLen = %d\n", rxLen);
- uartCharRcvCount = 0;
- if(rxLen > bufSize)
- {
- rxLen = bufSize;
- }
- uartExpectedRcvCount = rxLen;
- dbg_printf(LOG, "\nattaching to device UART\n\n");
- device->attach(&UartRxcallback_ex);
- dbg_printf(LOG, "\nBackground UART read setup completed\n\n");
-}
-
-int ReadUartBytes(uint8_t * rxBuffer, size_t bufSize, int rxLen, bool echo)
-{
- UartBusy = true;
- if(rxLen > bufSize)
- {
- rxLen = bufSize;
- }
- for(int i=0;i<rxLen;i++)
- {
- rxBuffer[i] = (uint8_t) device->getc();
- if(echo)device->putc(rxBuffer[i]);
- }
- UartBusy = false;
- //return number of bytes written to UART
- return rxLen;
-}
-
-
-void checkUartReceive()
-{
- //dbg_printf(LOG, "Hello World!\n\r");
- char cbuf[100];
- int rxCnt=0;
- while(device->readable()) {
- //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)
- {
- dbg_printf(LOG, "received %d chars\n", rxCnt);
- dbg_printf(LOG, "%s\n", cbuf);
- }
-
-}
-#endif
uint64_t lastTime = 0;
uint64_t now = 0;
uint32_t callCount = 0;
-void HelloUart()
-{
- //if(UartBusy)return;
- // 64-bit time doesn't wrap for half a billion years, at least
- lastTime = now;
- now = Kernel::get_ms_count();
- callCount++;
- dbg_printf(LOG, "\nHello : %d secs elapsed : CallCount = %d \n", uint32_t(now - lastTime), callCount);
-}
-
-
-
-
-//Serial device(USBTX, USBRX); // tx, rx
-//RawSerial device(MBED_CONF_APP_UART1_TX, MBED_CONF_APP_UART1_RX); // tx, rx
-
-
// Wifi-demo
@@ -296,16 +131,6 @@
}
}
-void printWait(int numSecs)
-{
- dbg_printf(LOG, "Waiting for %d seconds...\n", numSecs);
- for(int i=0;i<numSecs;i++){
- dbg_printf(LOG, "%d", i);
- dbg_printf(LOG, "\n");
- wait(0.5);
- eventQueue_atcmd.dispatch(500); // Dispatch time - 500msec
- }
-}
void printWaitAbortKeyPress(int numSecs)
{
@@ -388,11 +213,6 @@
led3 = !led3;
}
-//#define DISABLE_WIFI
-#define DISABLE_WIFI_DEMO
-#define SKIP_WIFI_SCAN_DEMO
-#define BLE_STOP_START_ADV_SCAN_DEMO
-#define SKIP_WIFI_CONNECT_DEMO
#define PAUSE_SECONDS 0
#define PAUSE_SECONDS_BLE 0
int main() {
@@ -403,11 +223,7 @@
#endif
reset_counter++;
- print_memory_info();
dbg_printf(LOG, "\r\n ++++++ PROGRAM STARTING -- reset count = %d ++++++ \r\n", reset_counter);
- //device = new RawSerial(PA_9, PA_10, DEFAULT_BAUD_RATE);
-
- dbg_printf(LOG, "*** HELLO MESSAGE *** \n %s", (char *)&hello_msg[6]);
setupDefaultWiFiConfig();
setupDefaultBleConfig();
BLE& _ble = BLE::Instance();
@@ -419,20 +235,13 @@
dbg_printf(LOG, "Filesystem creation failed, will use memory storage\r\n");
}
#endif
- print_memory_info();
dbg_printf(LOG, "\r\n PERIPHERAL \r\n\r\n");
peripheral = new SMDevicePeripheral(_ble, queue, peer_address, ble_config);
- print_memory_info();
peripheral->run();
btle_thread.start(callback(&queue, &EventQueue::dispatch_forever));
- print_memory_info();
- printWaitAbortKeyPress(PAUSE_SECONDS_BLE); // give BLE time to settle
- //peripheral->stopAdvertising();
-#ifndef DISABLE_WIFI // comment out wifi part
int start = Kernel::get_ms_count();
-#ifdef DISABLE_WIFI_DEMO
network = WiFiInterface::get_default_instance();
if (!network) {
dbg_printf(LOG, "ERROR: No WiFiInterface found.\n");
@@ -444,96 +253,14 @@
&aT2WiFiDatamPool, &aT2WiFiDataQueue,
&wiFi2ATDatamPool, &wiFi2ATDataQueue
);
-#else
- NetworkInterface* network = connect_to_default_network_interface();
- int stop = Kernel::get_ms_count();
- 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;
- wifi_evt_thread.start(callback(wifi_demo, network));
- wifi_evt_thread.join();
- network->disconnect();
- delete network;
- dbg_printf(LOG, "\n Wifi-Demo completed - restarting BLE \n\n");
-#endif /// endif DISABLE_WIFI_DEMO
-
-#else
- 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
- dbg_printf(LOG, "\n ++++++ restarting BLE ++++++ \n\n");
-#endif /// endif DISABLE_WIFI
- printWaitAbortKeyPress(PAUSE_SECONDS);
- //peripheral->startAdvertising();
-#ifdef ENABLE_UART_BACKGRND_DEMO
- for(int i=0;i<255;i++)
- {
- device->putc(i);
- }
- reportGapState();
- 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;
- dbg_printf(LOG, "\n\nExpected # of Received Bytes = %d\n", rxLen);
- BackGndUartRead(RxBuffer, RX_BUFFER_LEN, (int) rxLen);
-
- dbg_printf(LOG, "\n Waiting for 5 seconds ");
- printWait(5);
- dbg_printf(LOG, "\n Waiting finished!!!\n Now waiting for expected bytes to be received \n\n");
- while(UartBusy){
- wait(0.1);
- }
-#endif
-
- dbg_printf(LOG, "\r\n++++++ Press key for Wifi demo test ++++++ \r\n");
- printWaitAbortKeyPress(PAUSE_SECONDS);
-#ifndef SKIP_WIFI_SCAN_DEMO
- dbg_printf(LOG, "\r\n++++++ Test WiFi Manager Network Scan ++++++ \r\n");
- int count;
- count = wiFiManager->scanNetworks();
- if (count <= 0) {
- dbg_printf(LOG, "scan() failed with return value: %d\n", count);
- }
- else {
- dbg_printf(LOG, "\r\n++++++ Test WiFi Scan found %d networks ++++++ \r\n ++++ SUCCESS ++++\r\n", count);
- }
-#endif
-
-#ifndef SKIP_WIFI_CONNECT_DEMO
- dbg_printf(LOG, "\r\n++++++ Test WiFi Manager Network connect ++++++ \r\n");
- nsapi_error_t werror;
- werror = wiFiManager->connect();
- if (werror < 0) {
- dbg_printf(LOG, "connect() failed with return value: %d\n", werror);
- }
- else {
- dbg_printf(LOG, "\r\n++++++ Test WiFi connect SUCCESSFUL ++++++ \r\n");
- }
- if(!werror) // connect successful - test dicsonnection
- {
- dbg_printf(LOG, "\r\n++++++ Test WiFi Manager Network disconnect ++++++ \r\n");
- werror = wiFiManager->disconnect();
- if (werror) {
- dbg_printf(LOG, "disconnect() failed with return value: %d\n", werror);
- }
- else {
- dbg_printf(LOG, "\r\n++++++ Test WiFi disconnect SUCCESSFUL ++++++ \r\n");
- }
- }
-#endif
- //wiFiManager->runMain();
- 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));
dbg_printf(LOG, "\r\n after starting wifi thread \r\n");
- print_memory_info();
- // dispatch event queue on event thread
- 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)wifi_evt_thread.get_id());
+ // dispatch wifi event queue on event thread
wifi_evt_thread.start(callback(&eventQueue_wifi, &EventQueue::dispatch_forever));
+ // dispatch atcmd event queue on event thread
atcmd_evt_thread.start(callback(&eventQueue_atcmd, &EventQueue::dispatch_forever));
dbg_printf(LOG, "\r\n++++++ Starting ATCmdmanager ++++++ \r\n");
ATCmdManager *aTCmdManager = new ATCmdManager(USBTX, USBRX, peripheral,
@@ -543,23 +270,13 @@
&aT2WiFiDatamPool, &aT2WiFiDataQueue,
&wiFi2ATDatamPool, &wiFi2ATDataQueue,
false);
- //aTCmdManager->runMain();
atcmd_thread.start(callback(aTCmdManager, &ATCmdManager::runMain));
dbg_printf(LOG, "\r\n after starting atcmd thread \r\n");
print_memory_info();
- //SCB->SHCSR |= 0x00070000;//Enable fault handler.
- //wiFiManager->runMain();
while(1)
{
wait(0.1);
}
- //performFreeMemoryCheck();
-
- //eventQueue.dispatch_forever();
- //t.start(callback(&eventQueue, &EventQueue::dispatch_forever));
- //eventQueue2.dispatch_forever();
-
- return 0;
}
#endif