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