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