msn
Dependencies: BLE_API mbed nRF51822
Fork of BLE_LEDBlinker by
main.cpp@7:61e2224ec9a0, 2015-07-02 (annotated)
- Committer:
- rgrover1
- Date:
- Thu Jul 02 08:39:04 2015 +0000
- Revision:
- 7:61e2224ec9a0
- Parent:
- 6:1730f66fb14d
- Child:
- 8:b433a261072f
update to the latest of the underlying API
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rgrover1 | 0:415d7f24cb91 | 1 | /* mbed Microcontroller Library |
rgrover1 | 0:415d7f24cb91 | 2 | * Copyright (c) 2006-2015 ARM Limited |
rgrover1 | 0:415d7f24cb91 | 3 | * |
rgrover1 | 0:415d7f24cb91 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
rgrover1 | 0:415d7f24cb91 | 5 | * you may not use this file except in compliance with the License. |
rgrover1 | 0:415d7f24cb91 | 6 | * You may obtain a copy of the License at |
rgrover1 | 0:415d7f24cb91 | 7 | * |
rgrover1 | 0:415d7f24cb91 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
rgrover1 | 0:415d7f24cb91 | 9 | * |
rgrover1 | 0:415d7f24cb91 | 10 | * Unless required by applicable law or agreed to in writing, software |
rgrover1 | 0:415d7f24cb91 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
rgrover1 | 0:415d7f24cb91 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
rgrover1 | 0:415d7f24cb91 | 13 | * See the License for the specific language governing permissions and |
rgrover1 | 0:415d7f24cb91 | 14 | * limitations under the License. |
rgrover1 | 0:415d7f24cb91 | 15 | */ |
rgrover1 | 0:415d7f24cb91 | 16 | |
rgrover1 | 0:415d7f24cb91 | 17 | #include "mbed.h" |
rgrover1 | 6:1730f66fb14d | 18 | #include "ble/BLE.h" |
rgrover1 | 6:1730f66fb14d | 19 | #include "ble/DiscoveredCharacteristic.h" |
rgrover1 | 6:1730f66fb14d | 20 | #include "ble/DiscoveredService.h" |
rgrover1 | 7:61e2224ec9a0 | 21 | #include "ble/services/UARTService.h" |
rgrover1 | 0:415d7f24cb91 | 22 | |
rgrover1 | 6:1730f66fb14d | 23 | BLE ble; |
rgrover1 | 6:1730f66fb14d | 24 | |
rgrover1 | 3:146eba831693 | 25 | DigitalOut alivenessLED(LED1, 1); |
rgrover1 | 1:1db45b17552e | 26 | bool triggerLedCharacteristic = false; |
rgrover1 | 0:415d7f24cb91 | 27 | DiscoveredCharacteristic ledCharacteristic; |
rgrover1 | 0:415d7f24cb91 | 28 | |
rgrover1 | 0:415d7f24cb91 | 29 | void periodicCallback(void) { |
rgrover1 | 3:146eba831693 | 30 | alivenessLED = !alivenessLED; /* Do blinky on LED1 while we're waiting for BLE events */ |
rgrover1 | 0:415d7f24cb91 | 31 | } |
rgrover1 | 0:415d7f24cb91 | 32 | |
rgrover1 | 0:415d7f24cb91 | 33 | void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) { |
rgrover1 | 0:415d7f24cb91 | 34 | if (params->peerAddr[0] != 0x29) { /* !ALERT! Alter this filter to suit your device. */ |
rgrover1 | 0:415d7f24cb91 | 35 | return; |
rgrover1 | 0:415d7f24cb91 | 36 | } |
rgrover1 | 0:415d7f24cb91 | 37 | printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n", |
rgrover1 | 0:415d7f24cb91 | 38 | params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], |
rgrover1 | 0:415d7f24cb91 | 39 | params->rssi, params->isScanResponse, params->type); |
rgrover1 | 0:415d7f24cb91 | 40 | |
rgrover1 | 5:3bbad34d1a85 | 41 | ble.gap().connect(params->peerAddr, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL); |
rgrover1 | 0:415d7f24cb91 | 42 | } |
rgrover1 | 0:415d7f24cb91 | 43 | |
rgrover1 | 0:415d7f24cb91 | 44 | void serviceDiscoveryCallback(const DiscoveredService *service) { |
rgrover1 | 0:415d7f24cb91 | 45 | if (service->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) { |
rgrover1 | 0:415d7f24cb91 | 46 | printf("S UUID-%x attrs[%u %u]\r\n", service->getUUID().getShortUUID(), service->getStartHandle(), service->getEndHandle()); |
rgrover1 | 0:415d7f24cb91 | 47 | } else { |
rgrover1 | 0:415d7f24cb91 | 48 | printf("S UUID-"); |
rgrover1 | 0:415d7f24cb91 | 49 | const uint8_t *longUUIDBytes = service->getUUID().getBaseUUID(); |
rgrover1 | 0:415d7f24cb91 | 50 | for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) { |
rgrover1 | 0:415d7f24cb91 | 51 | printf("%02x", longUUIDBytes[i]); |
rgrover1 | 0:415d7f24cb91 | 52 | } |
rgrover1 | 0:415d7f24cb91 | 53 | printf(" attrs[%u %u]\r\n", service->getStartHandle(), service->getEndHandle()); |
rgrover1 | 0:415d7f24cb91 | 54 | } |
rgrover1 | 0:415d7f24cb91 | 55 | } |
rgrover1 | 0:415d7f24cb91 | 56 | |
rgrover1 | 0:415d7f24cb91 | 57 | void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) { |
rgrover1 | 0:415d7f24cb91 | 58 | printf(" C UUID-%x valueAttr[%u] props[%x]\r\n", characteristicP->getShortUUID(), characteristicP->getValueHandle(), (uint8_t)characteristicP->getProperties().broadcast()); |
rgrover1 | 1:1db45b17552e | 59 | if (characteristicP->getShortUUID() == 0xa001) { /* !ALERT! Alter this filter to suit your device. */ |
rgrover1 | 1:1db45b17552e | 60 | ledCharacteristic = *characteristicP; |
rgrover1 | 1:1db45b17552e | 61 | triggerLedCharacteristic = true; |
rgrover1 | 0:415d7f24cb91 | 62 | } |
rgrover1 | 0:415d7f24cb91 | 63 | } |
rgrover1 | 0:415d7f24cb91 | 64 | |
rgrover1 | 0:415d7f24cb91 | 65 | void discoveryTerminationCallback(Gap::Handle_t connectionHandle) { |
rgrover1 | 0:415d7f24cb91 | 66 | printf("terminated SD for handle %u\r\n", connectionHandle); |
rgrover1 | 0:415d7f24cb91 | 67 | } |
rgrover1 | 0:415d7f24cb91 | 68 | |
rgrover1 | 0:415d7f24cb91 | 69 | void connectionCallback(const Gap::ConnectionCallbackParams_t *params) { |
rgrover1 | 0:415d7f24cb91 | 70 | if (params->role == Gap::CENTRAL) { |
rgrover1 | 4:460ce53dc854 | 71 | ble.gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback); |
rgrover1 | 4:460ce53dc854 | 72 | ble.gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, 0xa000, 0xa001); |
rgrover1 | 0:415d7f24cb91 | 73 | } |
rgrover1 | 0:415d7f24cb91 | 74 | } |
rgrover1 | 0:415d7f24cb91 | 75 | |
rgrover1 | 0:415d7f24cb91 | 76 | void triggerToggledWrite(const GattReadCallbackParams *response) { |
rgrover1 | 0:415d7f24cb91 | 77 | if (response->handle == ledCharacteristic.getValueHandle()) { |
rgrover1 | 0:415d7f24cb91 | 78 | #if DUMP_READ_DATA |
rgrover1 | 0:415d7f24cb91 | 79 | printf("triggerToggledWrite: handle %u, offset %u, len %u\r\n", response->handle, response->offset, response->len); |
rgrover1 | 0:415d7f24cb91 | 80 | for (unsigned index = 0; index < response->len; index++) { |
rgrover1 | 0:415d7f24cb91 | 81 | printf("%c[%02x]", response->data[index], response->data[index]); |
rgrover1 | 0:415d7f24cb91 | 82 | } |
rgrover1 | 0:415d7f24cb91 | 83 | printf("\r\n"); |
rgrover1 | 0:415d7f24cb91 | 84 | #endif |
rgrover1 | 0:415d7f24cb91 | 85 | |
rgrover1 | 0:415d7f24cb91 | 86 | uint8_t toggledValue = response->data[0] ^ 0x1; |
rgrover1 | 0:415d7f24cb91 | 87 | ledCharacteristic.write(1, &toggledValue); |
rgrover1 | 0:415d7f24cb91 | 88 | } |
rgrover1 | 0:415d7f24cb91 | 89 | } |
rgrover1 | 0:415d7f24cb91 | 90 | |
rgrover1 | 0:415d7f24cb91 | 91 | void triggerRead(const GattWriteCallbackParams *response) { |
rgrover1 | 0:415d7f24cb91 | 92 | if (response->handle == ledCharacteristic.getValueHandle()) { |
rgrover1 | 0:415d7f24cb91 | 93 | ledCharacteristic.read(); |
rgrover1 | 0:415d7f24cb91 | 94 | } |
rgrover1 | 0:415d7f24cb91 | 95 | } |
rgrover1 | 0:415d7f24cb91 | 96 | |
rgrover1 | 0:415d7f24cb91 | 97 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) { |
rgrover1 | 0:415d7f24cb91 | 98 | printf("disconnected\r\n"); |
rgrover1 | 0:415d7f24cb91 | 99 | } |
rgrover1 | 0:415d7f24cb91 | 100 | |
rgrover1 | 0:415d7f24cb91 | 101 | int main(void) { |
rgrover1 | 0:415d7f24cb91 | 102 | Ticker ticker; |
rgrover1 | 0:415d7f24cb91 | 103 | ticker.attach(periodicCallback, 1); |
rgrover1 | 0:415d7f24cb91 | 104 | |
rgrover1 | 0:415d7f24cb91 | 105 | ble.init(); |
rgrover1 | 5:3bbad34d1a85 | 106 | ble.gap().onConnection(connectionCallback); |
rgrover1 | 5:3bbad34d1a85 | 107 | ble.gap().onDisconnection(disconnectionCallback); |
rgrover1 | 0:415d7f24cb91 | 108 | |
rgrover1 | 3:146eba831693 | 109 | /* Request for comments: should setupOnDataRead() be a static for DiscoveredCharacteristic? |
rgrover1 | 3:146eba831693 | 110 | * Or should it be passed in as a parameter to read()? */ |
rgrover1 | 7:61e2224ec9a0 | 111 | ble.gattClient().onDataRead(triggerToggledWrite); |
rgrover1 | 7:61e2224ec9a0 | 112 | ble.gattClient().onDataWrite(triggerRead); |
rgrover1 | 0:415d7f24cb91 | 113 | |
rgrover1 | 5:3bbad34d1a85 | 114 | ble.gap().setScanParams(500, 400); |
rgrover1 | 5:3bbad34d1a85 | 115 | ble.gap().startScan(advertisementCallback); |
rgrover1 | 0:415d7f24cb91 | 116 | |
rgrover1 | 0:415d7f24cb91 | 117 | while (true) { |
rgrover1 | 4:460ce53dc854 | 118 | if (triggerLedCharacteristic && !ble.gattClient().isServiceDiscoveryActive()) { |
rgrover1 | 1:1db45b17552e | 119 | triggerLedCharacteristic = false; |
rgrover1 | 0:415d7f24cb91 | 120 | ledCharacteristic.read(); /* We could have issued this read just as easily from |
rgrover1 | 0:415d7f24cb91 | 121 | * characteristicDiscoveryCallback(); but |
rgrover1 | 2:3e1e967035cb | 122 | * invoking it here demonstrates the use |
rgrover1 | 2:3e1e967035cb | 123 | * of isServiceDiscoveryActive() and also |
rgrover1 | 2:3e1e967035cb | 124 | * the fact that it is permitted to |
rgrover1 | 2:3e1e967035cb | 125 | * operate on application-local copies of |
rgrover1 | 2:3e1e967035cb | 126 | * DiscoveredCharacteristic. */ |
rgrover1 | 0:415d7f24cb91 | 127 | } |
rgrover1 | 0:415d7f24cb91 | 128 | ble.waitForEvent(); |
rgrover1 | 0:415d7f24cb91 | 129 | } |
rgrover1 | 0:415d7f24cb91 | 130 | } |