
this is using the mbed os version 5-13-1
Diff: source/main-https.cpp
- Revision:
- 78:07bb86e3ce14
- Parent:
- 77:0b505d1e15f4
- Child:
- 79:a2187bbfa407
--- a/source/main-https.cpp Fri Mar 15 23:25:30 2019 +0000 +++ b/source/main-https.cpp Sat Mar 16 13:05:52 2019 +0000 @@ -17,6 +17,7 @@ #include "common_config.h" #include "ATCmdManager.h" #include "BleManager.h" +#include "WiFiManager.h" UARTService *uart; DigitalOut alivenessLED(LED1, 0); @@ -24,10 +25,22 @@ static RawSerial *device; // tx, rx -static UARTSerial *_serial; // tx, rx -static ATCmdParser *_parser; + +// wifi configuration +static wifi_config_t wifi_config; +// wifi interface pointer +static WiFiInterface *network; +// wifi manager pointer +static WiFiManager *wiFiManager; + +// BLE configuration +static ble_config_t ble_config; +// BLE interface pointer +//BLE &_ble; +// BLE peripheral pointer static SMDevicePeripheral *peripheral; -const static char DEVICE_NAME_MAIN[] = "BLE-UART"; + +const static char DEVICE_NAME_MAIN[] = "UBLOX-BLE"; static const uint16_t uuid16_list[] = {LEDService::LED_SERVICE_UUID}; char buffer[BUFFER_LEN]; uint8_t TxBuffer[TX_BUFFER_LEN]; @@ -42,10 +55,9 @@ //unsigned char rt_stk[1024]; //unsigned char hp_stk[1024]; //unsigned char lp_stk[1024]; -unsigned char btle_stk[8*1024]; +unsigned char btle_stk[1024]; unsigned char wifi_stk[1024]; -unsigned char atcmd_stk[8*1024]; -static bool bleInitializationCompleted = false; +unsigned char atcmd_stk[1024]; /* creates three tread objects with different priorities */ //Thread real_time_thread(osPriorityRealtime, 1024, &rt_stk[0]); @@ -94,177 +106,6 @@ } -void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) -{ - (void) params; - BLE::Instance().gap().startAdvertising(); -} - -void blinkCallback(void) -{ - alivenessLED = !alivenessLED; /* Do blinky on LED1 to indicate system aliveness. */ -} - -void EchoBleUartReceived() -{ - uart->writeString(buffer); - uart->writeString("\n"); //flushes uart output buffer and sends data -} - -/** - * This callback allows the LEDService to receive updates to the ledState Characteristic. - * - * @param[in] params - * Information about the characterisitc being updated. - */ -void onDataWrittenCallback(const GattWriteCallbackParams *params) { - if ((params->handle == ledServicePtr->getValueHandle()) && (params->len == 1)) { - actuatedLED = *(params->data); - } - else if ((uart != NULL) && (params->handle == uart->getTXCharacteristicHandle())) { - uint16_t bytesRead = params->len; - - printf("received %u bytes\n\r ", bytesRead); - - if(bytesRead >= 255){ - printf("Overflow command %u n\r ", bytesRead); - bytesRead = 255; - } - - unsigned index = 0; - for (; index < bytesRead; index++) { - buffer[index] = params->data[index]; - } - - buffer[index++] = 0; - - printf("Data : %s ",buffer); - printf("\r\n"); - eventQueue.call(EchoBleUartReceived); - - } -} - - -/** - * This function is called when the ble initialization process has failled - */ -void onBleInitError(BLE &ble, ble_error_t error) -{ - printf("\n BLE Initialization failed!! \n"); - - /* Initialization error handling should go here */ -} - - -/* handle BLE timouts */ -static int bleTimoutCount = 0; - -void printBleTimeoutMsg() -{ - bleTimoutCount++; - device->printf("\n --- BLE Times out!! bleTimoutCount = %d --- \n", bleTimoutCount); -} - -//void timeoutCallback(Gap::TimeoutEventCallback_t* context) { -void timeoutCallback(Gap::TimeoutSource_t timeoutSource) { - BLE &ble = BLE::Instance(); - eventQueue.call(printBleTimeoutMsg); -} - - - -void printMacAddress() -{ - /* Print out device MAC address to the console*/ - Gap::AddressType_t addr_type; - Gap::Address_t address; - BLE::Instance().gap().getAddress(&addr_type, address); - printf("\nDEVICE MAC ADDRESS: "); - for (int i = 5; i >= 1; i--){ - printf("%02x:", address[i]); - } - printf("%02x\r\n", address[0]); -} - -/** - * Callback triggered when the ble initialization process has finished - */ -void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) -{ - BLE& ble = params->ble; - ble_error_t error = params->error; - - if (error != BLE_ERROR_NONE) { - /* In case of error, forward the error handling to onBleInitError */ - onBleInitError(ble, error); - return; - } - - /* Ensure that it is the default instance of BLE */ - if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { - return; - } - - ble.gap().onDisconnection(disconnectionCallback); - ble.gattServer().onDataWritten(onDataWrittenCallback); - ble.gap().onTimeout(timeoutCallback); - - bool initialValueForLEDCharacteristic = false; - ledServicePtr = new LEDService(ble, initialValueForLEDCharacteristic); - /* Setup primary service */ - uart = new UARTService(ble); - - /* setup advertising */ - ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); - ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); - ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME_MAIN, sizeof(DEVICE_NAME_MAIN)); - /* set up the services that can be discovered */ - ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,(const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); - ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); - ble.gap().setAdvertisingInterval(1000); /* 1000ms. */ - ble.gap().setAdvertisingTimeout(300); /* 16 * 1000ms. */ - ble.gap().startAdvertising(); - - printMacAddress(); - bleInitializationCompleted = true; -} - -void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) { - BLE &ble = BLE::Instance(); - eventQueue.call(Callback<void()>(&ble, &BLE::processEvents)); -} - - -void bleInitialization() -{ - BLE &ble = BLE::Instance(); - device->printf("\n --- BLE Instance Instantiated --- \n"); - ble.onEventsToProcess(scheduleBleEventsProcessing); - device->printf("\n --- BLE scheduleBleEventsProcessing setup --- \n"); - ble.init(bleInitComplete); -} - - -void restartBleAdvertising() -{ - BLE &ble = BLE::Instance(); - - //ble.init(bleInitComplete); - // clear advertising payload - ble.gap().clearAdvertisingPayload(); - /* setup advertising */ - ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); - ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); - ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME_MAIN, sizeof(DEVICE_NAME_MAIN)); - /* set up the services that can be discovered */ - ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,(const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); - ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); - ble.gap().setAdvertisingInterval(1000); /* 1000ms. */ - ble.gap().startAdvertising(); - eventQueue.dispatch(1000); // Dispatch time - 1000msec -} - static int uartExpectedRcvCount = 0; static int uartCharRcvCount = 0; static bool UartBusy = false; @@ -414,35 +255,6 @@ } } -void reportGapState() -{ - BLE &ble = BLE::Instance(); - Gap::GapState_t gapState = ble.gap().getState(); - char connStr[20] = " Not Connected "; - char advStr[20] = " Not Advertising "; - char devName[20] = ""; - if(gapState.advertising){ - strncpy(advStr, " Advertising ", 20); - } - if(gapState.connected){ - strncpy(connStr, " Connected ", 20); - } - device->printf("\n Advertising Status = %s\n Connection Status = %s\n", advStr, connStr); - unsigned nLen; - ble_error_t error; - error = ble.gap().getDeviceName((uint8_t *) devName, &nLen); - if(error != BLE_ERROR_NONE) - { - device->printf("\n Error Reading BLE device Name \n"); - return; - } - devName[nLen] = NULL; - device->printf("\n BLE Device name = %s : Name Len %d\n", devName, nLen); - for(int i=0;i<8;i++) - device->putc(devName[i]); - -} - void printWait(int numSecs) { printf("Waiting for %d seconds...\n", numSecs); @@ -454,6 +266,21 @@ } } + +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 +} + +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; +} + static int reset_counter = 0; @@ -462,7 +289,7 @@ #define MAX_LOOP_COUNT 3 int ble_security_main() { - BLE& ble = BLE::Instance(); + BLE& _ble = BLE::Instance(); events::EventQueue queue; #if MBED_CONF_APP_FILESYSTEM_SUPPORT @@ -476,7 +303,7 @@ while(1) { { printf("\r\n PERIPHERAL \r\n\r\n"); - SMDevicePeripheral peripheral(ble, queue, peer_address); + SMDevicePeripheral peripheral(_ble, queue, peer_address, ble_config); peripheral.run(); return 0; } @@ -487,7 +314,7 @@ { printf("\r\n CENTRAL \r\n\r\n"); - SMDeviceCentral central(ble, queue, peer_address); + SMDeviceCentral central(_ble, queue, peer_address, ble_config); central.run(); } loopCount++; @@ -514,7 +341,7 @@ printf("Heap size: %lu / %lu bytes\r\n", heap_stats.current_size, heap_stats.reserved_size); } -#define DISABLE_WIFI_DEMO +//#define DISABLE_WIFI int main() { reset_counter++; //performFreeMemoryCheck(); @@ -522,8 +349,9 @@ printf("\r\n ++++++ PROGRAM STARTING -- reset count = %d ++++++ \r\n", reset_counter); device = new RawSerial(USBTX, USBRX, DEFAULT_BAUD_RATE); //ble_security_main(); - - BLE& ble = BLE::Instance(); + setupDefaultBleConfig(); + setupDefaultWiFiConfig(); + BLE& _ble = BLE::Instance(); events::EventQueue queue(/* event count */ 10 * EVENTS_EVENT_SIZE); #if MBED_CONF_APP_FILESYSTEM_SUPPORT /* if filesystem creation fails or there is no filesystem the security manager @@ -535,7 +363,7 @@ print_memory_info(); printf("\r\n PERIPHERAL \r\n\r\n"); //SMDevicePeripheral peripheral(ble, queue, peer_address); - peripheral = new SMDevicePeripheral(ble, queue, peer_address); + peripheral = new SMDevicePeripheral(_ble, queue, peer_address, ble_config); print_memory_info(); peripheral->run(); @@ -553,10 +381,11 @@ //performFreeMemoryCheck(); printf("\r\n BTLE THREAD HAS RETURNED \r\n\r\n"); -#ifndef DISABLE_WIFI_DEMO // comment out wifi part - printMacAddress(); - //reportGapState(); +#ifndef DISABLE_WIFI // comment out wifi part int start = Kernel::get_ms_count(); +#ifdef DISABLE_WIFI_DEMO + wiFiManager = new WiFiManager(wifi_config, wifi); +#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)); @@ -565,22 +394,15 @@ t.join(); network->disconnect(); delete network; - printMacAddress(); - //reportGapState(); device->printf("\n Wifi-Demo completed - restarting BLE \n\n"); +#endif #else device->printf("\n Wifi Demo disabled so just waiting it out... \n\n"); printWait(60); // lets wait for a minute before turning BLE back on device->printf("\n ++++++ restarting BLE ++++++ \n\n"); #endif peripheral->startAdvertising(); - //peripheral->run(); - //btle_thread.start(callback(&queue, &EventQueue::dispatch_forever)); - //peripheral.run(); - //btle_thread.start(callback(peripheral, &SMDevicePeripheral::run)); - //t.start(callback(&queue, &EventQueue::dispatch_forever)); printWait(60); - //reportGapState(); #ifdef ENABLE_UART_BACKGRND_DEMO for(int i=0;i<255;i++) { @@ -603,7 +425,7 @@ } #endif device->printf("\r\n++++++ Starting ATCmdmanager ++++++ \r\n"); - ATCmdManager *aTCmdManager = new ATCmdManager(USBTX, USBRX, true); + ATCmdManager *aTCmdManager = new ATCmdManager(USBTX, USBRX, peripheral, wiFiManager, true); aTCmdManager->runMain(); atcmd_thread.start(callback(aTCmdManager, &ATCmdManager::runMain)); //performFreeMemoryCheck();