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

Dependencies:   mbed-http

Committer:
ocomeni
Date:
Fri Mar 15 23:25:30 2019 +0000
Revision:
77:0b505d1e15f4
Parent:
76:6afda865fbf8
Child:
78:07bb86e3ce14
BLE & ATCmdManager working concurrently

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ocomeni 73:6f5021cbe752 1 #define MBED_CONF_MBED_TRACE_ENABLE 1
ocomeni 73:6f5021cbe752 2
ocomeni 73:6f5021cbe752 3 #include "select-demo.h"
ocomeni 73:6f5021cbe752 4
ocomeni 73:6f5021cbe752 5 #if DEMO == DEMO_HTTPS
ocomeni 73:6f5021cbe752 6
ocomeni 73:6f5021cbe752 7 //#include "mbed.h"
ocomeni 73:6f5021cbe752 8 #include <events/mbed_events.h>
ocomeni 73:6f5021cbe752 9 #include <mbed.h>
ocomeni 73:6f5021cbe752 10 #include "ble/BLE.h"
ocomeni 73:6f5021cbe752 11 //#include "BLE.h"
ocomeni 73:6f5021cbe752 12 #include "ATCmdParser.h"
ocomeni 73:6f5021cbe752 13 //#include "BLEDevice.h"
ocomeni 73:6f5021cbe752 14
ocomeni 73:6f5021cbe752 15 #include "LEDService.h"
ocomeni 73:6f5021cbe752 16 #include "ble/services/UARTService.h"
ocomeni 74:f26e846adfe9 17 #include "common_config.h"
ocomeni 74:f26e846adfe9 18 #include "ATCmdManager.h"
ocomeni 75:08eff6258e1b 19 #include "BleManager.h"
ocomeni 73:6f5021cbe752 20 UARTService *uart;
ocomeni 73:6f5021cbe752 21
ocomeni 73:6f5021cbe752 22 DigitalOut alivenessLED(LED1, 0);
ocomeni 73:6f5021cbe752 23 DigitalOut actuatedLED(LED2, 0);
ocomeni 73:6f5021cbe752 24
ocomeni 73:6f5021cbe752 25
ocomeni 74:f26e846adfe9 26 static RawSerial *device; // tx, rx
ocomeni 74:f26e846adfe9 27 static UARTSerial *_serial; // tx, rx
ocomeni 74:f26e846adfe9 28 static ATCmdParser *_parser;
ocomeni 77:0b505d1e15f4 29 static SMDevicePeripheral *peripheral;
ocomeni 75:08eff6258e1b 30 const static char DEVICE_NAME_MAIN[] = "BLE-UART";
ocomeni 73:6f5021cbe752 31 static const uint16_t uuid16_list[] = {LEDService::LED_SERVICE_UUID};
ocomeni 73:6f5021cbe752 32 char buffer[BUFFER_LEN];
ocomeni 73:6f5021cbe752 33 uint8_t TxBuffer[TX_BUFFER_LEN];
ocomeni 73:6f5021cbe752 34 uint8_t RxBuffer[RX_BUFFER_LEN];
ocomeni 73:6f5021cbe752 35 static EventQueue eventQueue(/* event count */ 20 * EVENTS_EVENT_SIZE);
ocomeni 73:6f5021cbe752 36 //static EventQueue eventQueue2(/* event count */ 10 * EVENTS_EVENT_SIZE);
ocomeni 73:6f5021cbe752 37
ocomeni 73:6f5021cbe752 38 LEDService *ledServicePtr;
ocomeni 73:6f5021cbe752 39
ocomeni 74:f26e846adfe9 40
ocomeni 74:f26e846adfe9 41 /* allocate statically stacks for the three threads */
ocomeni 74:f26e846adfe9 42 //unsigned char rt_stk[1024];
ocomeni 74:f26e846adfe9 43 //unsigned char hp_stk[1024];
ocomeni 74:f26e846adfe9 44 //unsigned char lp_stk[1024];
ocomeni 77:0b505d1e15f4 45 unsigned char btle_stk[8*1024];
ocomeni 74:f26e846adfe9 46 unsigned char wifi_stk[1024];
ocomeni 77:0b505d1e15f4 47 unsigned char atcmd_stk[8*1024];
ocomeni 74:f26e846adfe9 48 static bool bleInitializationCompleted = false;
ocomeni 74:f26e846adfe9 49
ocomeni 74:f26e846adfe9 50 /* creates three tread objects with different priorities */
ocomeni 74:f26e846adfe9 51 //Thread real_time_thread(osPriorityRealtime, 1024, &rt_stk[0]);
ocomeni 74:f26e846adfe9 52 //Thread high_prio_thread(osPriorityHigh, 1024, &hp_stk[0]);
ocomeni 74:f26e846adfe9 53 //Thread low_prio_thread(osPriorityNormal, 1024, &lp_stk[0]);
ocomeni 74:f26e846adfe9 54 Thread btle_thread(BTLE_THREAD_PRIORITY, 1024, &btle_stk[0]);
ocomeni 74:f26e846adfe9 55 Thread wifi_thread(WIFI_THREAD_PRIORITY, 1024, &wifi_stk[0]);
ocomeni 74:f26e846adfe9 56 Thread atcmd_thread(ATCMD_THREAD_PRIORITY, 1024, &atcmd_stk[0]);
ocomeni 74:f26e846adfe9 57
ocomeni 74:f26e846adfe9 58
ocomeni 74:f26e846adfe9 59 /* create a semaphore to synchronize the threads */
ocomeni 74:f26e846adfe9 60 Semaphore sync_sema;
ocomeni 74:f26e846adfe9 61
ocomeni 73:6f5021cbe752 62 Thread t;
ocomeni 74:f26e846adfe9 63 #include "network-helper.h"
ocomeni 73:6f5021cbe752 64
ocomeni 73:6f5021cbe752 65 /* List of trusted root CA certificates
ocomeni 73:6f5021cbe752 66 * currently two: GlobalSign, the CA for os.mbed.com and Let's Encrypt, the CA for httpbin.org
ocomeni 73:6f5021cbe752 67 *
ocomeni 73:6f5021cbe752 68 * To add more root certificates, just concatenate them.
ocomeni 73:6f5021cbe752 69 */
ocomeni 73:6f5021cbe752 70 #include "https_certificates.h"
ocomeni 73:6f5021cbe752 71
ocomeni 74:f26e846adfe9 72 // wifi demo
ocomeni 74:f26e846adfe9 73 #include "wifi_demo.h"
ocomeni 73:6f5021cbe752 74
ocomeni 77:0b505d1e15f4 75 Mutex _smutex; // Protect memory access
ocomeni 74:f26e846adfe9 76 // check free memory
ocomeni 74:f26e846adfe9 77
ocomeni 74:f26e846adfe9 78 void performFreeMemoryCheck()
ocomeni 74:f26e846adfe9 79 {
ocomeni 77:0b505d1e15f4 80 _smutex.lock();
ocomeni 74:f26e846adfe9 81 // perform free memory check
ocomeni 74:f26e846adfe9 82 int blockSize = 16;
ocomeni 74:f26e846adfe9 83 int i = 1;
ocomeni 74:f26e846adfe9 84 printf("Checking memory with blocksize %d char ...\n", blockSize);
ocomeni 74:f26e846adfe9 85 while (true) {
ocomeni 74:f26e846adfe9 86 char *p = (char *) malloc(i * blockSize);
ocomeni 74:f26e846adfe9 87 if (p == NULL)
ocomeni 74:f26e846adfe9 88 break;
ocomeni 74:f26e846adfe9 89 free(p);
ocomeni 74:f26e846adfe9 90 ++i;
ocomeni 74:f26e846adfe9 91 }
ocomeni 74:f26e846adfe9 92 printf("Ok for %d char\n", (i - 1) * blockSize);
ocomeni 77:0b505d1e15f4 93 _smutex.unlock();
ocomeni 74:f26e846adfe9 94
ocomeni 74:f26e846adfe9 95 }
ocomeni 74:f26e846adfe9 96
ocomeni 73:6f5021cbe752 97 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
ocomeni 73:6f5021cbe752 98 {
ocomeni 73:6f5021cbe752 99 (void) params;
ocomeni 73:6f5021cbe752 100 BLE::Instance().gap().startAdvertising();
ocomeni 73:6f5021cbe752 101 }
ocomeni 73:6f5021cbe752 102
ocomeni 73:6f5021cbe752 103 void blinkCallback(void)
ocomeni 73:6f5021cbe752 104 {
ocomeni 73:6f5021cbe752 105 alivenessLED = !alivenessLED; /* Do blinky on LED1 to indicate system aliveness. */
ocomeni 73:6f5021cbe752 106 }
ocomeni 73:6f5021cbe752 107
ocomeni 73:6f5021cbe752 108 void EchoBleUartReceived()
ocomeni 73:6f5021cbe752 109 {
ocomeni 73:6f5021cbe752 110 uart->writeString(buffer);
ocomeni 73:6f5021cbe752 111 uart->writeString("\n"); //flushes uart output buffer and sends data
ocomeni 73:6f5021cbe752 112 }
ocomeni 73:6f5021cbe752 113
ocomeni 73:6f5021cbe752 114 /**
ocomeni 73:6f5021cbe752 115 * This callback allows the LEDService to receive updates to the ledState Characteristic.
ocomeni 73:6f5021cbe752 116 *
ocomeni 73:6f5021cbe752 117 * @param[in] params
ocomeni 73:6f5021cbe752 118 * Information about the characterisitc being updated.
ocomeni 73:6f5021cbe752 119 */
ocomeni 73:6f5021cbe752 120 void onDataWrittenCallback(const GattWriteCallbackParams *params) {
ocomeni 73:6f5021cbe752 121 if ((params->handle == ledServicePtr->getValueHandle()) && (params->len == 1)) {
ocomeni 73:6f5021cbe752 122 actuatedLED = *(params->data);
ocomeni 73:6f5021cbe752 123 }
ocomeni 73:6f5021cbe752 124 else if ((uart != NULL) && (params->handle == uart->getTXCharacteristicHandle())) {
ocomeni 73:6f5021cbe752 125 uint16_t bytesRead = params->len;
ocomeni 73:6f5021cbe752 126
ocomeni 73:6f5021cbe752 127 printf("received %u bytes\n\r ", bytesRead);
ocomeni 73:6f5021cbe752 128
ocomeni 73:6f5021cbe752 129 if(bytesRead >= 255){
ocomeni 73:6f5021cbe752 130 printf("Overflow command %u n\r ", bytesRead);
ocomeni 73:6f5021cbe752 131 bytesRead = 255;
ocomeni 73:6f5021cbe752 132 }
ocomeni 73:6f5021cbe752 133
ocomeni 73:6f5021cbe752 134 unsigned index = 0;
ocomeni 73:6f5021cbe752 135 for (; index < bytesRead; index++) {
ocomeni 73:6f5021cbe752 136 buffer[index] = params->data[index];
ocomeni 73:6f5021cbe752 137 }
ocomeni 73:6f5021cbe752 138
ocomeni 73:6f5021cbe752 139 buffer[index++] = 0;
ocomeni 73:6f5021cbe752 140
ocomeni 73:6f5021cbe752 141 printf("Data : %s ",buffer);
ocomeni 73:6f5021cbe752 142 printf("\r\n");
ocomeni 73:6f5021cbe752 143 eventQueue.call(EchoBleUartReceived);
ocomeni 73:6f5021cbe752 144
ocomeni 73:6f5021cbe752 145 }
ocomeni 73:6f5021cbe752 146 }
ocomeni 73:6f5021cbe752 147
ocomeni 73:6f5021cbe752 148
ocomeni 73:6f5021cbe752 149 /**
ocomeni 73:6f5021cbe752 150 * This function is called when the ble initialization process has failled
ocomeni 73:6f5021cbe752 151 */
ocomeni 73:6f5021cbe752 152 void onBleInitError(BLE &ble, ble_error_t error)
ocomeni 73:6f5021cbe752 153 {
ocomeni 73:6f5021cbe752 154 printf("\n BLE Initialization failed!! \n");
ocomeni 73:6f5021cbe752 155
ocomeni 73:6f5021cbe752 156 /* Initialization error handling should go here */
ocomeni 73:6f5021cbe752 157 }
ocomeni 73:6f5021cbe752 158
ocomeni 74:f26e846adfe9 159
ocomeni 74:f26e846adfe9 160 /* handle BLE timouts */
ocomeni 74:f26e846adfe9 161 static int bleTimoutCount = 0;
ocomeni 74:f26e846adfe9 162
ocomeni 74:f26e846adfe9 163 void printBleTimeoutMsg()
ocomeni 74:f26e846adfe9 164 {
ocomeni 74:f26e846adfe9 165 bleTimoutCount++;
ocomeni 74:f26e846adfe9 166 device->printf("\n --- BLE Times out!! bleTimoutCount = %d --- \n", bleTimoutCount);
ocomeni 74:f26e846adfe9 167 }
ocomeni 74:f26e846adfe9 168
ocomeni 74:f26e846adfe9 169 //void timeoutCallback(Gap::TimeoutEventCallback_t* context) {
ocomeni 74:f26e846adfe9 170 void timeoutCallback(Gap::TimeoutSource_t timeoutSource) {
ocomeni 74:f26e846adfe9 171 BLE &ble = BLE::Instance();
ocomeni 74:f26e846adfe9 172 eventQueue.call(printBleTimeoutMsg);
ocomeni 74:f26e846adfe9 173 }
ocomeni 74:f26e846adfe9 174
ocomeni 74:f26e846adfe9 175
ocomeni 74:f26e846adfe9 176
ocomeni 73:6f5021cbe752 177 void printMacAddress()
ocomeni 73:6f5021cbe752 178 {
ocomeni 73:6f5021cbe752 179 /* Print out device MAC address to the console*/
ocomeni 73:6f5021cbe752 180 Gap::AddressType_t addr_type;
ocomeni 73:6f5021cbe752 181 Gap::Address_t address;
ocomeni 73:6f5021cbe752 182 BLE::Instance().gap().getAddress(&addr_type, address);
ocomeni 74:f26e846adfe9 183 printf("\nDEVICE MAC ADDRESS: ");
ocomeni 73:6f5021cbe752 184 for (int i = 5; i >= 1; i--){
ocomeni 73:6f5021cbe752 185 printf("%02x:", address[i]);
ocomeni 73:6f5021cbe752 186 }
ocomeni 73:6f5021cbe752 187 printf("%02x\r\n", address[0]);
ocomeni 73:6f5021cbe752 188 }
ocomeni 73:6f5021cbe752 189
ocomeni 73:6f5021cbe752 190 /**
ocomeni 73:6f5021cbe752 191 * Callback triggered when the ble initialization process has finished
ocomeni 73:6f5021cbe752 192 */
ocomeni 73:6f5021cbe752 193 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
ocomeni 73:6f5021cbe752 194 {
ocomeni 73:6f5021cbe752 195 BLE& ble = params->ble;
ocomeni 73:6f5021cbe752 196 ble_error_t error = params->error;
ocomeni 73:6f5021cbe752 197
ocomeni 73:6f5021cbe752 198 if (error != BLE_ERROR_NONE) {
ocomeni 73:6f5021cbe752 199 /* In case of error, forward the error handling to onBleInitError */
ocomeni 73:6f5021cbe752 200 onBleInitError(ble, error);
ocomeni 73:6f5021cbe752 201 return;
ocomeni 73:6f5021cbe752 202 }
ocomeni 73:6f5021cbe752 203
ocomeni 73:6f5021cbe752 204 /* Ensure that it is the default instance of BLE */
ocomeni 73:6f5021cbe752 205 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
ocomeni 73:6f5021cbe752 206 return;
ocomeni 73:6f5021cbe752 207 }
ocomeni 73:6f5021cbe752 208
ocomeni 73:6f5021cbe752 209 ble.gap().onDisconnection(disconnectionCallback);
ocomeni 73:6f5021cbe752 210 ble.gattServer().onDataWritten(onDataWrittenCallback);
ocomeni 74:f26e846adfe9 211 ble.gap().onTimeout(timeoutCallback);
ocomeni 73:6f5021cbe752 212
ocomeni 73:6f5021cbe752 213 bool initialValueForLEDCharacteristic = false;
ocomeni 73:6f5021cbe752 214 ledServicePtr = new LEDService(ble, initialValueForLEDCharacteristic);
ocomeni 73:6f5021cbe752 215 /* Setup primary service */
ocomeni 73:6f5021cbe752 216 uart = new UARTService(ble);
ocomeni 73:6f5021cbe752 217
ocomeni 73:6f5021cbe752 218 /* setup advertising */
ocomeni 73:6f5021cbe752 219 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
ocomeni 73:6f5021cbe752 220 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
ocomeni 75:08eff6258e1b 221 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME_MAIN, sizeof(DEVICE_NAME_MAIN));
ocomeni 73:6f5021cbe752 222 /* set up the services that can be discovered */
ocomeni 73:6f5021cbe752 223 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,(const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
ocomeni 73:6f5021cbe752 224 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
ocomeni 73:6f5021cbe752 225 ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
ocomeni 74:f26e846adfe9 226 ble.gap().setAdvertisingTimeout(300); /* 16 * 1000ms. */
ocomeni 73:6f5021cbe752 227 ble.gap().startAdvertising();
ocomeni 73:6f5021cbe752 228
ocomeni 73:6f5021cbe752 229 printMacAddress();
ocomeni 74:f26e846adfe9 230 bleInitializationCompleted = true;
ocomeni 73:6f5021cbe752 231 }
ocomeni 73:6f5021cbe752 232
ocomeni 73:6f5021cbe752 233 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
ocomeni 73:6f5021cbe752 234 BLE &ble = BLE::Instance();
ocomeni 73:6f5021cbe752 235 eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
ocomeni 73:6f5021cbe752 236 }
ocomeni 73:6f5021cbe752 237
ocomeni 73:6f5021cbe752 238
ocomeni 73:6f5021cbe752 239 void bleInitialization()
ocomeni 73:6f5021cbe752 240 {
ocomeni 73:6f5021cbe752 241 BLE &ble = BLE::Instance();
ocomeni 73:6f5021cbe752 242 device->printf("\n --- BLE Instance Instantiated --- \n");
ocomeni 73:6f5021cbe752 243 ble.onEventsToProcess(scheduleBleEventsProcessing);
ocomeni 73:6f5021cbe752 244 device->printf("\n --- BLE scheduleBleEventsProcessing setup --- \n");
ocomeni 73:6f5021cbe752 245 ble.init(bleInitComplete);
ocomeni 73:6f5021cbe752 246 }
ocomeni 73:6f5021cbe752 247
ocomeni 73:6f5021cbe752 248
ocomeni 74:f26e846adfe9 249 void restartBleAdvertising()
ocomeni 74:f26e846adfe9 250 {
ocomeni 74:f26e846adfe9 251 BLE &ble = BLE::Instance();
ocomeni 74:f26e846adfe9 252
ocomeni 74:f26e846adfe9 253 //ble.init(bleInitComplete);
ocomeni 74:f26e846adfe9 254 // clear advertising payload
ocomeni 74:f26e846adfe9 255 ble.gap().clearAdvertisingPayload();
ocomeni 74:f26e846adfe9 256 /* setup advertising */
ocomeni 74:f26e846adfe9 257 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
ocomeni 74:f26e846adfe9 258 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
ocomeni 75:08eff6258e1b 259 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME_MAIN, sizeof(DEVICE_NAME_MAIN));
ocomeni 74:f26e846adfe9 260 /* set up the services that can be discovered */
ocomeni 74:f26e846adfe9 261 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,(const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
ocomeni 74:f26e846adfe9 262 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
ocomeni 74:f26e846adfe9 263 ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
ocomeni 74:f26e846adfe9 264 ble.gap().startAdvertising();
ocomeni 74:f26e846adfe9 265 eventQueue.dispatch(1000); // Dispatch time - 1000msec
ocomeni 74:f26e846adfe9 266 }
ocomeni 74:f26e846adfe9 267
ocomeni 73:6f5021cbe752 268 static int uartExpectedRcvCount = 0;
ocomeni 73:6f5021cbe752 269 static int uartCharRcvCount = 0;
ocomeni 73:6f5021cbe752 270 static bool UartBusy = false;
ocomeni 73:6f5021cbe752 271 int WriteUartBytes(const uint8_t * txBuffer, size_t bufSize, int txLen)
ocomeni 73:6f5021cbe752 272 {
ocomeni 73:6f5021cbe752 273 if(txLen > bufSize)
ocomeni 73:6f5021cbe752 274 {
ocomeni 73:6f5021cbe752 275 txLen = bufSize;
ocomeni 73:6f5021cbe752 276 }
ocomeni 73:6f5021cbe752 277 //int goodTxLen;
ocomeni 73:6f5021cbe752 278 //goodTxLen = _parser.write((const char *) txBuffer, txLen);
ocomeni 73:6f5021cbe752 279 for(int i=0;i<txLen;i++)
ocomeni 73:6f5021cbe752 280 {
ocomeni 73:6f5021cbe752 281 device->putc(txBuffer[i]);
ocomeni 73:6f5021cbe752 282 }
ocomeni 73:6f5021cbe752 283 // return number of bytes written to UART
ocomeni 73:6f5021cbe752 284 return (int) txLen;
ocomeni 73:6f5021cbe752 285 }
ocomeni 73:6f5021cbe752 286
ocomeni 75:08eff6258e1b 287 void printUartRxResult()
ocomeni 75:08eff6258e1b 288 {
ocomeni 75:08eff6258e1b 289
ocomeni 75:08eff6258e1b 290 if(uartCharRcvCount == 0)
ocomeni 75:08eff6258e1b 291 {
ocomeni 75:08eff6258e1b 292 device->printf("\nFirst Call to UART attach callback!!\n");
ocomeni 75:08eff6258e1b 293 }
ocomeni 75:08eff6258e1b 294 else if(uartCharRcvCount >= uartExpectedRcvCount)
ocomeni 75:08eff6258e1b 295 {
ocomeni 75:08eff6258e1b 296 device->printf("\nNumber of Received Bytes = %d\n\n", uartCharRcvCount);
ocomeni 75:08eff6258e1b 297 device->printf("--- Writing back received bytes --- \n");
ocomeni 75:08eff6258e1b 298 int n;
ocomeni 75:08eff6258e1b 299 n = WriteUartBytes(RxBuffer, TX_BUFFER_LEN, uartCharRcvCount);
ocomeni 75:08eff6258e1b 300 UartBusy = false;
ocomeni 75:08eff6258e1b 301 }
ocomeni 75:08eff6258e1b 302 }
ocomeni 75:08eff6258e1b 303
ocomeni 75:08eff6258e1b 304
ocomeni 73:6f5021cbe752 305 void UartRxcallback_ex() {
ocomeni 73:6f5021cbe752 306 if(uartCharRcvCount >= uartExpectedRcvCount)
ocomeni 73:6f5021cbe752 307 {
ocomeni 73:6f5021cbe752 308 int x = device->getc();
ocomeni 73:6f5021cbe752 309 return;
ocomeni 73:6f5021cbe752 310 }
ocomeni 73:6f5021cbe752 311 if(uartCharRcvCount == 0)
ocomeni 73:6f5021cbe752 312 {
ocomeni 75:08eff6258e1b 313 eventQueue.call(printUartRxResult);
ocomeni 73:6f5021cbe752 314 }
ocomeni 73:6f5021cbe752 315 // Note: you need to actually read from the serial to clear the RX interrupt
ocomeni 73:6f5021cbe752 316 RxBuffer[uartCharRcvCount] = (uint8_t) device->getc();
ocomeni 73:6f5021cbe752 317 uartCharRcvCount++;
ocomeni 73:6f5021cbe752 318 if(uartCharRcvCount >= uartExpectedRcvCount)
ocomeni 73:6f5021cbe752 319 {
ocomeni 73:6f5021cbe752 320 alivenessLED = !alivenessLED; /* Do blinky on LED1 to indicate system aliveness. */
ocomeni 75:08eff6258e1b 321 eventQueue.call(printUartRxResult);
ocomeni 73:6f5021cbe752 322 }
ocomeni 73:6f5021cbe752 323 }
ocomeni 73:6f5021cbe752 324
ocomeni 73:6f5021cbe752 325 void BackGndUartRead(uint8_t * rxBuffer, size_t bufSize, int rxLen)
ocomeni 73:6f5021cbe752 326 {
ocomeni 73:6f5021cbe752 327 UartBusy = true;
ocomeni 73:6f5021cbe752 328 device->printf("Setting up background UART read - rxLen = %d\n", rxLen);
ocomeni 73:6f5021cbe752 329 uartCharRcvCount = 0;
ocomeni 73:6f5021cbe752 330 if(rxLen > bufSize)
ocomeni 73:6f5021cbe752 331 {
ocomeni 73:6f5021cbe752 332 rxLen = bufSize;
ocomeni 73:6f5021cbe752 333 }
ocomeni 73:6f5021cbe752 334 uartExpectedRcvCount = rxLen;
ocomeni 75:08eff6258e1b 335 device->printf("\nattaching to device UART\n\n");
ocomeni 73:6f5021cbe752 336 device->attach(&UartRxcallback_ex);
ocomeni 73:6f5021cbe752 337 device->printf("\nBackground UART read setup completed\n\n");
ocomeni 73:6f5021cbe752 338 }
ocomeni 73:6f5021cbe752 339
ocomeni 73:6f5021cbe752 340 int ReadUartBytes(uint8_t * rxBuffer, size_t bufSize, int rxLen, bool echo)
ocomeni 73:6f5021cbe752 341 {
ocomeni 73:6f5021cbe752 342 UartBusy = true;
ocomeni 73:6f5021cbe752 343 if(rxLen > bufSize)
ocomeni 73:6f5021cbe752 344 {
ocomeni 73:6f5021cbe752 345 rxLen = bufSize;
ocomeni 73:6f5021cbe752 346 }
ocomeni 73:6f5021cbe752 347 for(int i=0;i<rxLen;i++)
ocomeni 73:6f5021cbe752 348 {
ocomeni 73:6f5021cbe752 349 rxBuffer[i] = (uint8_t) device->getc();
ocomeni 73:6f5021cbe752 350 if(echo)device->putc(rxBuffer[i]);
ocomeni 73:6f5021cbe752 351 }
ocomeni 73:6f5021cbe752 352 UartBusy = false;
ocomeni 73:6f5021cbe752 353 //return number of bytes written to UART
ocomeni 73:6f5021cbe752 354 return rxLen;
ocomeni 73:6f5021cbe752 355 }
ocomeni 73:6f5021cbe752 356
ocomeni 73:6f5021cbe752 357
ocomeni 73:6f5021cbe752 358 void checkUartReceive()
ocomeni 73:6f5021cbe752 359 {
ocomeni 73:6f5021cbe752 360 //device->printf("Hello World!\n\r");
ocomeni 73:6f5021cbe752 361 char cbuf[100];
ocomeni 73:6f5021cbe752 362 int rxCnt=0;
ocomeni 73:6f5021cbe752 363 while(device->readable()) {
ocomeni 73:6f5021cbe752 364 //device->printf("uartCharRcvCount = %d\n\r", uartCharRcvCount++);
ocomeni 73:6f5021cbe752 365 cbuf[rxCnt++] = device->getc();
ocomeni 73:6f5021cbe752 366 //putc(getc() + 1); // echo input back to terminal
ocomeni 73:6f5021cbe752 367 }
ocomeni 73:6f5021cbe752 368 cbuf[rxCnt] = NULL;
ocomeni 73:6f5021cbe752 369 if(rxCnt > 0)
ocomeni 73:6f5021cbe752 370 {
ocomeni 73:6f5021cbe752 371 device->printf("received %d chars\n", rxCnt);
ocomeni 73:6f5021cbe752 372 device->printf("%s\n", cbuf);
ocomeni 73:6f5021cbe752 373 }
ocomeni 73:6f5021cbe752 374
ocomeni 73:6f5021cbe752 375 }
ocomeni 73:6f5021cbe752 376 uint64_t lastTime = 0;
ocomeni 73:6f5021cbe752 377 uint64_t now = 0;
ocomeni 73:6f5021cbe752 378 uint32_t callCount = 0;
ocomeni 73:6f5021cbe752 379 void HelloUart()
ocomeni 73:6f5021cbe752 380 {
ocomeni 73:6f5021cbe752 381 //if(UartBusy)return;
ocomeni 73:6f5021cbe752 382 // 64-bit time doesn't wrap for half a billion years, at least
ocomeni 73:6f5021cbe752 383 lastTime = now;
ocomeni 73:6f5021cbe752 384 now = Kernel::get_ms_count();
ocomeni 73:6f5021cbe752 385 callCount++;
ocomeni 73:6f5021cbe752 386 device->printf("\nHello : %d secs elapsed : CallCount = %d \n", uint32_t(now - lastTime), callCount);
ocomeni 73:6f5021cbe752 387 }
ocomeni 73:6f5021cbe752 388
ocomeni 73:6f5021cbe752 389
ocomeni 73:6f5021cbe752 390
ocomeni 73:6f5021cbe752 391
ocomeni 73:6f5021cbe752 392 //Serial device(USBTX, USBRX); // tx, rx
ocomeni 73:6f5021cbe752 393 //RawSerial device(MBED_CONF_APP_UART1_TX, MBED_CONF_APP_UART1_RX); // tx, rx
ocomeni 73:6f5021cbe752 394
ocomeni 73:6f5021cbe752 395
ocomeni 73:6f5021cbe752 396
ocomeni 73:6f5021cbe752 397
ocomeni 73:6f5021cbe752 398 // Wifi-demo
ocomeni 73:6f5021cbe752 399 void wifi_demo(NetworkInterface* network){
ocomeni 74:f26e846adfe9 400 int n = wifi_demo_func(network);
ocomeni 74:f26e846adfe9 401 if(n > 0)// error
ocomeni 73:6f5021cbe752 402 {
ocomeni 74:f26e846adfe9 403 device->printf("\n --- Error running wifi demo --- \n");
ocomeni 73:6f5021cbe752 404 }
ocomeni 74:f26e846adfe9 405 }
ocomeni 73:6f5021cbe752 406
ocomeni 74:f26e846adfe9 407 // Wifi-demo2
ocomeni 74:f26e846adfe9 408 void wifi_demo2(){
ocomeni 74:f26e846adfe9 409 //int n = wifi_demo_func(network);
ocomeni 74:f26e846adfe9 410 int n =5;
ocomeni 74:f26e846adfe9 411 if(n > 0)// error
ocomeni 73:6f5021cbe752 412 {
ocomeni 74:f26e846adfe9 413 device->printf("\n --- Error running wifi demo --- \n");
ocomeni 73:6f5021cbe752 414 }
ocomeni 74:f26e846adfe9 415 }
ocomeni 73:6f5021cbe752 416
ocomeni 74:f26e846adfe9 417 void reportGapState()
ocomeni 74:f26e846adfe9 418 {
ocomeni 74:f26e846adfe9 419 BLE &ble = BLE::Instance();
ocomeni 74:f26e846adfe9 420 Gap::GapState_t gapState = ble.gap().getState();
ocomeni 74:f26e846adfe9 421 char connStr[20] = " Not Connected ";
ocomeni 74:f26e846adfe9 422 char advStr[20] = " Not Advertising ";
ocomeni 74:f26e846adfe9 423 char devName[20] = "";
ocomeni 74:f26e846adfe9 424 if(gapState.advertising){
ocomeni 74:f26e846adfe9 425 strncpy(advStr, " Advertising ", 20);
ocomeni 74:f26e846adfe9 426 }
ocomeni 74:f26e846adfe9 427 if(gapState.connected){
ocomeni 74:f26e846adfe9 428 strncpy(connStr, " Connected ", 20);
ocomeni 74:f26e846adfe9 429 }
ocomeni 74:f26e846adfe9 430 device->printf("\n Advertising Status = %s\n Connection Status = %s\n", advStr, connStr);
ocomeni 74:f26e846adfe9 431 unsigned nLen;
ocomeni 74:f26e846adfe9 432 ble_error_t error;
ocomeni 74:f26e846adfe9 433 error = ble.gap().getDeviceName((uint8_t *) devName, &nLen);
ocomeni 74:f26e846adfe9 434 if(error != BLE_ERROR_NONE)
ocomeni 74:f26e846adfe9 435 {
ocomeni 74:f26e846adfe9 436 device->printf("\n Error Reading BLE device Name \n");
ocomeni 74:f26e846adfe9 437 return;
ocomeni 74:f26e846adfe9 438 }
ocomeni 74:f26e846adfe9 439 devName[nLen] = NULL;
ocomeni 74:f26e846adfe9 440 device->printf("\n BLE Device name = %s : Name Len %d\n", devName, nLen);
ocomeni 74:f26e846adfe9 441 for(int i=0;i<8;i++)
ocomeni 74:f26e846adfe9 442 device->putc(devName[i]);
ocomeni 74:f26e846adfe9 443
ocomeni 74:f26e846adfe9 444 }
ocomeni 73:6f5021cbe752 445
ocomeni 74:f26e846adfe9 446 void printWait(int numSecs)
ocomeni 74:f26e846adfe9 447 {
ocomeni 74:f26e846adfe9 448 printf("Waiting for %d seconds...\n", numSecs);
ocomeni 74:f26e846adfe9 449 for(int i=0;i<numSecs;i++){
ocomeni 74:f26e846adfe9 450 printf("%d", i);
ocomeni 75:08eff6258e1b 451 printf("\n");
ocomeni 74:f26e846adfe9 452 wait(0.5);
ocomeni 74:f26e846adfe9 453 eventQueue.dispatch(500); // Dispatch time - 500msec
ocomeni 73:6f5021cbe752 454 }
ocomeni 74:f26e846adfe9 455 }
ocomeni 73:6f5021cbe752 456
ocomeni 74:f26e846adfe9 457 static int reset_counter = 0;
ocomeni 73:6f5021cbe752 458
ocomeni 75:08eff6258e1b 459
ocomeni 75:08eff6258e1b 460
ocomeni 75:08eff6258e1b 461
ocomeni 76:6afda865fbf8 462 #define MAX_LOOP_COUNT 3
ocomeni 75:08eff6258e1b 463 int ble_security_main()
ocomeni 75:08eff6258e1b 464 {
ocomeni 75:08eff6258e1b 465 BLE& ble = BLE::Instance();
ocomeni 75:08eff6258e1b 466 events::EventQueue queue;
ocomeni 75:08eff6258e1b 467
ocomeni 75:08eff6258e1b 468 #if MBED_CONF_APP_FILESYSTEM_SUPPORT
ocomeni 75:08eff6258e1b 469 /* if filesystem creation fails or there is no filesystem the security manager
ocomeni 75:08eff6258e1b 470 * will fallback to storing the security database in memory */
ocomeni 75:08eff6258e1b 471 if (!create_filesystem()) {
ocomeni 75:08eff6258e1b 472 printf("Filesystem creation failed, will use memory storage\r\n");
ocomeni 75:08eff6258e1b 473 }
ocomeni 75:08eff6258e1b 474 #endif
ocomeni 76:6afda865fbf8 475 int loopCount = 0;
ocomeni 75:08eff6258e1b 476 while(1) {
ocomeni 75:08eff6258e1b 477 {
ocomeni 75:08eff6258e1b 478 printf("\r\n PERIPHERAL \r\n\r\n");
ocomeni 75:08eff6258e1b 479 SMDevicePeripheral peripheral(ble, queue, peer_address);
ocomeni 75:08eff6258e1b 480 peripheral.run();
ocomeni 77:0b505d1e15f4 481 return 0;
ocomeni 75:08eff6258e1b 482 }
ocomeni 76:6afda865fbf8 483 if(loopCount >= MAX_LOOP_COUNT)
ocomeni 76:6afda865fbf8 484 {
ocomeni 76:6afda865fbf8 485 return 0;
ocomeni 76:6afda865fbf8 486 }
ocomeni 75:08eff6258e1b 487
ocomeni 75:08eff6258e1b 488 {
ocomeni 75:08eff6258e1b 489 printf("\r\n CENTRAL \r\n\r\n");
ocomeni 75:08eff6258e1b 490 SMDeviceCentral central(ble, queue, peer_address);
ocomeni 75:08eff6258e1b 491 central.run();
ocomeni 75:08eff6258e1b 492 }
ocomeni 76:6afda865fbf8 493 loopCount++;
ocomeni 76:6afda865fbf8 494 printf("loop Cycle #%d\r\n", loopCount);
ocomeni 75:08eff6258e1b 495 }
ocomeni 75:08eff6258e1b 496
ocomeni 75:08eff6258e1b 497 return 0;
ocomeni 75:08eff6258e1b 498 }
ocomeni 75:08eff6258e1b 499
ocomeni 77:0b505d1e15f4 500 void print_memory_info() {
ocomeni 77:0b505d1e15f4 501 // allocate enough room for every thread's stack statistics
ocomeni 77:0b505d1e15f4 502 int cnt = osThreadGetCount();
ocomeni 77:0b505d1e15f4 503 mbed_stats_stack_t *stats = (mbed_stats_stack_t*) malloc(cnt * sizeof(mbed_stats_stack_t));
ocomeni 77:0b505d1e15f4 504
ocomeni 77:0b505d1e15f4 505 cnt = mbed_stats_stack_get_each(stats, cnt);
ocomeni 77:0b505d1e15f4 506 for (int i = 0; i < cnt; i++) {
ocomeni 77:0b505d1e15f4 507 printf("Thread: 0x%lX, Stack size: %lu / %lu\r\n", stats[i].thread_id, stats[i].max_size, stats[i].reserved_size);
ocomeni 77:0b505d1e15f4 508 }
ocomeni 77:0b505d1e15f4 509 free(stats);
ocomeni 77:0b505d1e15f4 510
ocomeni 77:0b505d1e15f4 511 // Grab the heap statistics
ocomeni 77:0b505d1e15f4 512 mbed_stats_heap_t heap_stats;
ocomeni 77:0b505d1e15f4 513 mbed_stats_heap_get(&heap_stats);
ocomeni 77:0b505d1e15f4 514 printf("Heap size: %lu / %lu bytes\r\n", heap_stats.current_size, heap_stats.reserved_size);
ocomeni 77:0b505d1e15f4 515 }
ocomeni 77:0b505d1e15f4 516
ocomeni 77:0b505d1e15f4 517 #define DISABLE_WIFI_DEMO
ocomeni 73:6f5021cbe752 518 int main() {
ocomeni 74:f26e846adfe9 519 reset_counter++;
ocomeni 77:0b505d1e15f4 520 //performFreeMemoryCheck();
ocomeni 77:0b505d1e15f4 521 print_memory_info();
ocomeni 77:0b505d1e15f4 522 printf("\r\n ++++++ PROGRAM STARTING -- reset count = %d ++++++ \r\n", reset_counter);
ocomeni 73:6f5021cbe752 523 device = new RawSerial(USBTX, USBRX, DEFAULT_BAUD_RATE);
ocomeni 77:0b505d1e15f4 524 //ble_security_main();
ocomeni 75:08eff6258e1b 525
ocomeni 77:0b505d1e15f4 526 BLE& ble = BLE::Instance();
ocomeni 77:0b505d1e15f4 527 events::EventQueue queue(/* event count */ 10 * EVENTS_EVENT_SIZE);
ocomeni 77:0b505d1e15f4 528 #if MBED_CONF_APP_FILESYSTEM_SUPPORT
ocomeni 77:0b505d1e15f4 529 /* if filesystem creation fails or there is no filesystem the security manager
ocomeni 77:0b505d1e15f4 530 * will fallback to storing the security database in memory */
ocomeni 77:0b505d1e15f4 531 if (!create_filesystem()) {
ocomeni 77:0b505d1e15f4 532 printf("Filesystem creation failed, will use memory storage\r\n");
ocomeni 75:08eff6258e1b 533 }
ocomeni 76:6afda865fbf8 534 #endif
ocomeni 77:0b505d1e15f4 535 print_memory_info();
ocomeni 77:0b505d1e15f4 536 printf("\r\n PERIPHERAL \r\n\r\n");
ocomeni 77:0b505d1e15f4 537 //SMDevicePeripheral peripheral(ble, queue, peer_address);
ocomeni 77:0b505d1e15f4 538 peripheral = new SMDevicePeripheral(ble, queue, peer_address);
ocomeni 77:0b505d1e15f4 539 print_memory_info();
ocomeni 77:0b505d1e15f4 540
ocomeni 77:0b505d1e15f4 541 peripheral->run();
ocomeni 77:0b505d1e15f4 542 btle_thread.start(callback(&queue, &EventQueue::dispatch_forever));
ocomeni 77:0b505d1e15f4 543 //btle_thread.start(callback(peripheral, &SMDevicePeripheral::run));
ocomeni 77:0b505d1e15f4 544 //performFreeMemoryCheck();
ocomeni 77:0b505d1e15f4 545 //reportGapState();
ocomeni 77:0b505d1e15f4 546 print_memory_info();
ocomeni 77:0b505d1e15f4 547 printWait(60); // lets give time to see BLE advertising...
ocomeni 77:0b505d1e15f4 548 //reportGapState();
ocomeni 77:0b505d1e15f4 549 peripheral->stopAdvertising();
ocomeni 77:0b505d1e15f4 550 //queue.break_dispatch();
ocomeni 77:0b505d1e15f4 551 //reportGapState();
ocomeni 77:0b505d1e15f4 552 //btle_thread.join();
ocomeni 77:0b505d1e15f4 553 //performFreeMemoryCheck();
ocomeni 77:0b505d1e15f4 554 printf("\r\n BTLE THREAD HAS RETURNED \r\n\r\n");
ocomeni 77:0b505d1e15f4 555
ocomeni 76:6afda865fbf8 556 #ifndef DISABLE_WIFI_DEMO // comment out wifi part
ocomeni 77:0b505d1e15f4 557 printMacAddress();
ocomeni 77:0b505d1e15f4 558 //reportGapState();
ocomeni 77:0b505d1e15f4 559 int start = Kernel::get_ms_count();
ocomeni 77:0b505d1e15f4 560 NetworkInterface* network = connect_to_default_network_interface();
ocomeni 77:0b505d1e15f4 561 int stop = Kernel::get_ms_count();
ocomeni 77:0b505d1e15f4 562 device->printf("\n The Wifi Network scan took %d ms or %4.1f seconds\n", (stop - start), (float)((stop - start)/1000.0));
ocomeni 77:0b505d1e15f4 563 // run on separate thread;
ocomeni 77:0b505d1e15f4 564 t.start(callback(wifi_demo, network));
ocomeni 77:0b505d1e15f4 565 t.join();
ocomeni 77:0b505d1e15f4 566 network->disconnect();
ocomeni 77:0b505d1e15f4 567 delete network;
ocomeni 77:0b505d1e15f4 568 printMacAddress();
ocomeni 77:0b505d1e15f4 569 //reportGapState();
ocomeni 77:0b505d1e15f4 570 device->printf("\n Wifi-Demo completed - restarting BLE \n\n");
ocomeni 77:0b505d1e15f4 571 #else
ocomeni 77:0b505d1e15f4 572 device->printf("\n Wifi Demo disabled so just waiting it out... \n\n");
ocomeni 77:0b505d1e15f4 573 printWait(60); // lets wait for a minute before turning BLE back on
ocomeni 77:0b505d1e15f4 574 device->printf("\n ++++++ restarting BLE ++++++ \n\n");
ocomeni 75:08eff6258e1b 575 #endif
ocomeni 77:0b505d1e15f4 576 peripheral->startAdvertising();
ocomeni 77:0b505d1e15f4 577 //peripheral->run();
ocomeni 77:0b505d1e15f4 578 //btle_thread.start(callback(&queue, &EventQueue::dispatch_forever));
ocomeni 77:0b505d1e15f4 579 //peripheral.run();
ocomeni 77:0b505d1e15f4 580 //btle_thread.start(callback(peripheral, &SMDevicePeripheral::run));
ocomeni 77:0b505d1e15f4 581 //t.start(callback(&queue, &EventQueue::dispatch_forever));
ocomeni 77:0b505d1e15f4 582 printWait(60);
ocomeni 77:0b505d1e15f4 583 //reportGapState();
ocomeni 77:0b505d1e15f4 584 #ifdef ENABLE_UART_BACKGRND_DEMO
ocomeni 73:6f5021cbe752 585 for(int i=0;i<255;i++)
ocomeni 73:6f5021cbe752 586 {
ocomeni 73:6f5021cbe752 587 device->putc(i);
ocomeni 73:6f5021cbe752 588 }
ocomeni 77:0b505d1e15f4 589 //int n;
ocomeni 73:6f5021cbe752 590 //ReadUartBytes(RxBuffer, RX_BUFFER_LEN, 4);
ocomeni 74:f26e846adfe9 591 reportGapState();
ocomeni 73:6f5021cbe752 592 device->printf("\n\n\nEnter # of expected bytes: ");
ocomeni 77:0b505d1e15f4 593 ReadUartBytes(RxBuffer, RX_BUFFER_LEN, 4, true);
ocomeni 74:f26e846adfe9 594 uint8_t rxLen = (uint8_t) (100*(RxBuffer[0]-'0') + 10*(RxBuffer[1]-'0') + (RxBuffer[2]-'0')) %256;
ocomeni 73:6f5021cbe752 595 device->printf("\n\nExpected # of Received Bytes = %d\n", rxLen);
ocomeni 74:f26e846adfe9 596 BackGndUartRead(RxBuffer, RX_BUFFER_LEN, (int) rxLen);
ocomeni 73:6f5021cbe752 597
ocomeni 75:08eff6258e1b 598 device->printf("\n Waiting for 5 seconds ");
ocomeni 75:08eff6258e1b 599 printWait(5);
ocomeni 77:0b505d1e15f4 600 device->printf("\n Waiting finished!!!\n Now waiting for expected bytes to be received \n\n");
ocomeni 75:08eff6258e1b 601 while(UartBusy){
ocomeni 75:08eff6258e1b 602 wait(0.1);
ocomeni 75:08eff6258e1b 603 }
ocomeni 77:0b505d1e15f4 604 #endif
ocomeni 77:0b505d1e15f4 605 device->printf("\r\n++++++ Starting ATCmdmanager ++++++ \r\n");
ocomeni 74:f26e846adfe9 606 ATCmdManager *aTCmdManager = new ATCmdManager(USBTX, USBRX, true);
ocomeni 75:08eff6258e1b 607 aTCmdManager->runMain();
ocomeni 77:0b505d1e15f4 608 atcmd_thread.start(callback(aTCmdManager, &ATCmdManager::runMain));
ocomeni 75:08eff6258e1b 609 //performFreeMemoryCheck();
ocomeni 73:6f5021cbe752 610
ocomeni 73:6f5021cbe752 611 //eventQueue.dispatch_forever();
ocomeni 73:6f5021cbe752 612 //t.start(callback(&eventQueue, &EventQueue::dispatch_forever));
ocomeni 73:6f5021cbe752 613 //eventQueue2.dispatch_forever();
ocomeni 73:6f5021cbe752 614
ocomeni 73:6f5021cbe752 615 return 0;
ocomeni 73:6f5021cbe752 616 }
ocomeni 73:6f5021cbe752 617
ocomeni 73:6f5021cbe752 618 #endif