This example creates and updates a standard Battery Level service containing a single GATT characteristic with mbed OS 5.
main.cpp@0:d9a87d1192f6, 2017-06-02 (annotated)
- Committer:
- tsungta
- Date:
- Fri Jun 02 14:29:32 2017 +0000
- Revision:
- 0:d9a87d1192f6
First commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tsungta | 0:d9a87d1192f6 | 1 | /* mbed Microcontroller Library |
tsungta | 0:d9a87d1192f6 | 2 | * Copyright (c) 2006-2014 ARM Limited |
tsungta | 0:d9a87d1192f6 | 3 | * |
tsungta | 0:d9a87d1192f6 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
tsungta | 0:d9a87d1192f6 | 5 | * you may not use this file except in compliance with the License. |
tsungta | 0:d9a87d1192f6 | 6 | * You may obtain a copy of the License at |
tsungta | 0:d9a87d1192f6 | 7 | * |
tsungta | 0:d9a87d1192f6 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
tsungta | 0:d9a87d1192f6 | 9 | * |
tsungta | 0:d9a87d1192f6 | 10 | * Unless required by applicable law or agreed to in writing, software |
tsungta | 0:d9a87d1192f6 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
tsungta | 0:d9a87d1192f6 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
tsungta | 0:d9a87d1192f6 | 13 | * See the License for the specific language governing permissions and |
tsungta | 0:d9a87d1192f6 | 14 | * limitations under the License. |
tsungta | 0:d9a87d1192f6 | 15 | */ |
tsungta | 0:d9a87d1192f6 | 16 | |
tsungta | 0:d9a87d1192f6 | 17 | #include <events/mbed_events.h> |
tsungta | 0:d9a87d1192f6 | 18 | #include <mbed.h> |
tsungta | 0:d9a87d1192f6 | 19 | #include "ble/BLE.h" |
tsungta | 0:d9a87d1192f6 | 20 | #include "ble/Gap.h" |
tsungta | 0:d9a87d1192f6 | 21 | #include "ble/services/BatteryService.h" |
tsungta | 0:d9a87d1192f6 | 22 | |
tsungta | 0:d9a87d1192f6 | 23 | DigitalOut led1(LED1, 1); |
tsungta | 0:d9a87d1192f6 | 24 | |
tsungta | 0:d9a87d1192f6 | 25 | const static char DEVICE_NAME[] = "BATTERY"; |
tsungta | 0:d9a87d1192f6 | 26 | static const uint16_t uuid16_list[] = {GattService::UUID_BATTERY_SERVICE}; |
tsungta | 0:d9a87d1192f6 | 27 | |
tsungta | 0:d9a87d1192f6 | 28 | static uint8_t batteryLevel = 50; |
tsungta | 0:d9a87d1192f6 | 29 | static BatteryService* batteryServicePtr; |
tsungta | 0:d9a87d1192f6 | 30 | |
tsungta | 0:d9a87d1192f6 | 31 | static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE); |
tsungta | 0:d9a87d1192f6 | 32 | |
tsungta | 0:d9a87d1192f6 | 33 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
tsungta | 0:d9a87d1192f6 | 34 | { |
tsungta | 0:d9a87d1192f6 | 35 | BLE::Instance().gap().startAdvertising(); |
tsungta | 0:d9a87d1192f6 | 36 | } |
tsungta | 0:d9a87d1192f6 | 37 | |
tsungta | 0:d9a87d1192f6 | 38 | void updateSensorValue() { |
tsungta | 0:d9a87d1192f6 | 39 | batteryLevel++; |
tsungta | 0:d9a87d1192f6 | 40 | if (batteryLevel > 100) { |
tsungta | 0:d9a87d1192f6 | 41 | batteryLevel = 20; |
tsungta | 0:d9a87d1192f6 | 42 | } |
tsungta | 0:d9a87d1192f6 | 43 | |
tsungta | 0:d9a87d1192f6 | 44 | batteryServicePtr->updateBatteryLevel(batteryLevel); |
tsungta | 0:d9a87d1192f6 | 45 | } |
tsungta | 0:d9a87d1192f6 | 46 | |
tsungta | 0:d9a87d1192f6 | 47 | void blinkCallback(void) |
tsungta | 0:d9a87d1192f6 | 48 | { |
tsungta | 0:d9a87d1192f6 | 49 | led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ |
tsungta | 0:d9a87d1192f6 | 50 | |
tsungta | 0:d9a87d1192f6 | 51 | BLE &ble = BLE::Instance(); |
tsungta | 0:d9a87d1192f6 | 52 | if (ble.gap().getState().connected) { |
tsungta | 0:d9a87d1192f6 | 53 | eventQueue.call(updateSensorValue); |
tsungta | 0:d9a87d1192f6 | 54 | } |
tsungta | 0:d9a87d1192f6 | 55 | } |
tsungta | 0:d9a87d1192f6 | 56 | |
tsungta | 0:d9a87d1192f6 | 57 | /** |
tsungta | 0:d9a87d1192f6 | 58 | * This function is called when the ble initialization process has failled |
tsungta | 0:d9a87d1192f6 | 59 | */ |
tsungta | 0:d9a87d1192f6 | 60 | void onBleInitError(BLE &ble, ble_error_t error) |
tsungta | 0:d9a87d1192f6 | 61 | { |
tsungta | 0:d9a87d1192f6 | 62 | /* Initialization error handling should go here */ |
tsungta | 0:d9a87d1192f6 | 63 | } |
tsungta | 0:d9a87d1192f6 | 64 | |
tsungta | 0:d9a87d1192f6 | 65 | /** |
tsungta | 0:d9a87d1192f6 | 66 | * Callback triggered when the ble initialization process has finished |
tsungta | 0:d9a87d1192f6 | 67 | */ |
tsungta | 0:d9a87d1192f6 | 68 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
tsungta | 0:d9a87d1192f6 | 69 | { |
tsungta | 0:d9a87d1192f6 | 70 | BLE& ble = params->ble; |
tsungta | 0:d9a87d1192f6 | 71 | ble_error_t error = params->error; |
tsungta | 0:d9a87d1192f6 | 72 | |
tsungta | 0:d9a87d1192f6 | 73 | if (error != BLE_ERROR_NONE) { |
tsungta | 0:d9a87d1192f6 | 74 | /* In case of error, forward the error handling to onBleInitError */ |
tsungta | 0:d9a87d1192f6 | 75 | onBleInitError(ble, error); |
tsungta | 0:d9a87d1192f6 | 76 | return; |
tsungta | 0:d9a87d1192f6 | 77 | } |
tsungta | 0:d9a87d1192f6 | 78 | |
tsungta | 0:d9a87d1192f6 | 79 | /* Ensure that it is the default instance of BLE */ |
tsungta | 0:d9a87d1192f6 | 80 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
tsungta | 0:d9a87d1192f6 | 81 | return; |
tsungta | 0:d9a87d1192f6 | 82 | } |
tsungta | 0:d9a87d1192f6 | 83 | |
tsungta | 0:d9a87d1192f6 | 84 | ble.gap().onDisconnection(disconnectionCallback); |
tsungta | 0:d9a87d1192f6 | 85 | |
tsungta | 0:d9a87d1192f6 | 86 | /* Setup primary service */ |
tsungta | 0:d9a87d1192f6 | 87 | batteryServicePtr = new BatteryService(ble, batteryLevel); |
tsungta | 0:d9a87d1192f6 | 88 | |
tsungta | 0:d9a87d1192f6 | 89 | /* Setup advertising */ |
tsungta | 0:d9a87d1192f6 | 90 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
tsungta | 0:d9a87d1192f6 | 91 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *) uuid16_list, sizeof(uuid16_list)); |
tsungta | 0:d9a87d1192f6 | 92 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *) DEVICE_NAME, sizeof(DEVICE_NAME)); |
tsungta | 0:d9a87d1192f6 | 93 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
tsungta | 0:d9a87d1192f6 | 94 | ble.gap().setAdvertisingInterval(1000); /* 1000ms */ |
tsungta | 0:d9a87d1192f6 | 95 | ble.gap().startAdvertising(); |
tsungta | 0:d9a87d1192f6 | 96 | } |
tsungta | 0:d9a87d1192f6 | 97 | |
tsungta | 0:d9a87d1192f6 | 98 | void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) { |
tsungta | 0:d9a87d1192f6 | 99 | BLE &ble = BLE::Instance(); |
tsungta | 0:d9a87d1192f6 | 100 | eventQueue.call(Callback<void()>(&ble, &BLE::processEvents)); |
tsungta | 0:d9a87d1192f6 | 101 | } |
tsungta | 0:d9a87d1192f6 | 102 | |
tsungta | 0:d9a87d1192f6 | 103 | int main() |
tsungta | 0:d9a87d1192f6 | 104 | { |
tsungta | 0:d9a87d1192f6 | 105 | eventQueue.call_every(500, blinkCallback); |
tsungta | 0:d9a87d1192f6 | 106 | |
tsungta | 0:d9a87d1192f6 | 107 | BLE &ble = BLE::Instance(); |
tsungta | 0:d9a87d1192f6 | 108 | ble.onEventsToProcess(scheduleBleEventsProcessing); |
tsungta | 0:d9a87d1192f6 | 109 | ble.init(bleInitComplete); |
tsungta | 0:d9a87d1192f6 | 110 | |
tsungta | 0:d9a87d1192f6 | 111 | eventQueue.dispatch_forever(); |
tsungta | 0:d9a87d1192f6 | 112 | |
tsungta | 0:d9a87d1192f6 | 113 | return 0; |
tsungta | 0:d9a87d1192f6 | 114 | } |