BLE EddystoneService example
This example is a fork of the following mbed-os example:
https://developer.mbed.org/teams/mbed-os-examples/code/mbed-os-example-ble-EddystoneService/
Please read the documentation in this page.
source/main.cpp@0:4c8f8bf32a99, 2016-07-26 (annotated)
- Committer:
- Vincent Coubard
- Date:
- Tue Jul 26 14:40:25 2016 +0100
- Revision:
- 0:4c8f8bf32a99
- Child:
- 1:9db4d46bb63f
Update example at tag mbed-os-5.0.1-rc1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Vincent Coubard |
0:4c8f8bf32a99 | 1 | /* mbed Microcontroller Library |
Vincent Coubard |
0:4c8f8bf32a99 | 2 | * Copyright (c) 2006-2013 ARM Limited |
Vincent Coubard |
0:4c8f8bf32a99 | 3 | * |
Vincent Coubard |
0:4c8f8bf32a99 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Vincent Coubard |
0:4c8f8bf32a99 | 5 | * you may not use this file except in compliance with the License. |
Vincent Coubard |
0:4c8f8bf32a99 | 6 | * You may obtain a copy of the License at |
Vincent Coubard |
0:4c8f8bf32a99 | 7 | * |
Vincent Coubard |
0:4c8f8bf32a99 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Vincent Coubard |
0:4c8f8bf32a99 | 9 | * |
Vincent Coubard |
0:4c8f8bf32a99 | 10 | * Unless required by applicable law or agreed to in writing, software |
Vincent Coubard |
0:4c8f8bf32a99 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Vincent Coubard |
0:4c8f8bf32a99 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Vincent Coubard |
0:4c8f8bf32a99 | 13 | * See the License for the specific language governing permissions and |
Vincent Coubard |
0:4c8f8bf32a99 | 14 | * limitations under the License. |
Vincent Coubard |
0:4c8f8bf32a99 | 15 | */ |
Vincent Coubard |
0:4c8f8bf32a99 | 16 | |
Vincent Coubard |
0:4c8f8bf32a99 | 17 | #include <mbed-events/events.h> |
Vincent Coubard |
0:4c8f8bf32a99 | 18 | |
Vincent Coubard |
0:4c8f8bf32a99 | 19 | #include <mbed.h> |
Vincent Coubard |
0:4c8f8bf32a99 | 20 | #include "ble/BLE.h" |
Vincent Coubard |
0:4c8f8bf32a99 | 21 | #include "EddystoneService.h" |
Vincent Coubard |
0:4c8f8bf32a99 | 22 | |
Vincent Coubard |
0:4c8f8bf32a99 | 23 | #include "PersistentStorageHelper/ConfigParamsPersistence.h" |
Vincent Coubard |
0:4c8f8bf32a99 | 24 | |
Vincent Coubard |
0:4c8f8bf32a99 | 25 | EddystoneService *eddyServicePtr; |
Vincent Coubard |
0:4c8f8bf32a99 | 26 | |
Vincent Coubard |
0:4c8f8bf32a99 | 27 | /* Duration after power-on that config service is available. */ |
Vincent Coubard |
0:4c8f8bf32a99 | 28 | static const int CONFIG_ADVERTISEMENT_TIMEOUT_SECONDS = 30; |
Vincent Coubard |
0:4c8f8bf32a99 | 29 | |
Vincent Coubard |
0:4c8f8bf32a99 | 30 | /* Default UID frame data */ |
Vincent Coubard |
0:4c8f8bf32a99 | 31 | static const UIDNamespaceID_t uidNamespaceID = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}; |
Vincent Coubard |
0:4c8f8bf32a99 | 32 | static const UIDInstanceID_t uidInstanceID = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}; |
Vincent Coubard |
0:4c8f8bf32a99 | 33 | |
Vincent Coubard |
0:4c8f8bf32a99 | 34 | /* Default version in TLM frame */ |
Vincent Coubard |
0:4c8f8bf32a99 | 35 | static const uint8_t tlmVersion = 0x00; |
Vincent Coubard |
0:4c8f8bf32a99 | 36 | |
Vincent Coubard |
0:4c8f8bf32a99 | 37 | /* Values for ADV packets related to firmware levels, calibrated based on measured values at 1m */ |
Vincent Coubard |
0:4c8f8bf32a99 | 38 | static const PowerLevels_t defaultAdvPowerLevels = {-47, -33, -21, -13}; |
Vincent Coubard |
0:4c8f8bf32a99 | 39 | /* Values for radio power levels, provided by manufacturer. */ |
Vincent Coubard |
0:4c8f8bf32a99 | 40 | static const PowerLevels_t radioPowerLevels = {-30, -16, -4, 4}; |
Vincent Coubard |
0:4c8f8bf32a99 | 41 | |
Vincent Coubard |
0:4c8f8bf32a99 | 42 | static EventQueue eventQueue( |
Vincent Coubard |
0:4c8f8bf32a99 | 43 | /* event count */ 16 * /* event size */ 32 |
Vincent Coubard |
0:4c8f8bf32a99 | 44 | ); |
Vincent Coubard |
0:4c8f8bf32a99 | 45 | |
Vincent Coubard |
0:4c8f8bf32a99 | 46 | DigitalOut led(LED1, 1); |
Vincent Coubard |
0:4c8f8bf32a99 | 47 | |
Vincent Coubard |
0:4c8f8bf32a99 | 48 | /** |
Vincent Coubard |
0:4c8f8bf32a99 | 49 | * Callback triggered upon a disconnection event. |
Vincent Coubard |
0:4c8f8bf32a99 | 50 | */ |
Vincent Coubard |
0:4c8f8bf32a99 | 51 | static void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *cbParams) |
Vincent Coubard |
0:4c8f8bf32a99 | 52 | { |
Vincent Coubard |
0:4c8f8bf32a99 | 53 | (void) cbParams; |
Vincent Coubard |
0:4c8f8bf32a99 | 54 | BLE::Instance().gap().startAdvertising(); |
Vincent Coubard |
0:4c8f8bf32a99 | 55 | } |
Vincent Coubard |
0:4c8f8bf32a99 | 56 | |
Vincent Coubard |
0:4c8f8bf32a99 | 57 | /** |
Vincent Coubard |
0:4c8f8bf32a99 | 58 | * Callback triggered some time after application started to switch to beacon mode. |
Vincent Coubard |
0:4c8f8bf32a99 | 59 | */ |
Vincent Coubard |
0:4c8f8bf32a99 | 60 | static void timeout(void) |
Vincent Coubard |
0:4c8f8bf32a99 | 61 | { |
Vincent Coubard |
0:4c8f8bf32a99 | 62 | Gap::GapState_t state; |
Vincent Coubard |
0:4c8f8bf32a99 | 63 | state = BLE::Instance().gap().getState(); |
Vincent Coubard |
0:4c8f8bf32a99 | 64 | if (!state.connected) { /* don't switch if we're in a connected state. */ |
Vincent Coubard |
0:4c8f8bf32a99 | 65 | eddyServicePtr->startBeaconService(); |
Vincent Coubard |
0:4c8f8bf32a99 | 66 | EddystoneService::EddystoneParams_t params; |
Vincent Coubard |
0:4c8f8bf32a99 | 67 | eddyServicePtr->getEddystoneParams(params); |
Vincent Coubard |
0:4c8f8bf32a99 | 68 | saveEddystoneServiceConfigParams(¶ms); |
Vincent Coubard |
0:4c8f8bf32a99 | 69 | } else { |
Vincent Coubard |
0:4c8f8bf32a99 | 70 | eventQueue.post_in(timeout, CONFIG_ADVERTISEMENT_TIMEOUT_SECONDS * 1000); |
Vincent Coubard |
0:4c8f8bf32a99 | 71 | } |
Vincent Coubard |
0:4c8f8bf32a99 | 72 | } |
Vincent Coubard |
0:4c8f8bf32a99 | 73 | |
Vincent Coubard |
0:4c8f8bf32a99 | 74 | static void blinky(void) |
Vincent Coubard |
0:4c8f8bf32a99 | 75 | { |
Vincent Coubard |
0:4c8f8bf32a99 | 76 | led = !led; |
Vincent Coubard |
0:4c8f8bf32a99 | 77 | } |
Vincent Coubard |
0:4c8f8bf32a99 | 78 | |
Vincent Coubard |
0:4c8f8bf32a99 | 79 | static void onBleInitError(BLE::InitializationCompleteCallbackContext* initContext) |
Vincent Coubard |
0:4c8f8bf32a99 | 80 | { |
Vincent Coubard |
0:4c8f8bf32a99 | 81 | /* Initialization error handling goes here... */ |
Vincent Coubard |
0:4c8f8bf32a99 | 82 | (void) initContext; |
Vincent Coubard |
0:4c8f8bf32a99 | 83 | } |
Vincent Coubard |
0:4c8f8bf32a99 | 84 | |
Vincent Coubard |
0:4c8f8bf32a99 | 85 | static void initializeEddystoneToDefaults(BLE &ble) |
Vincent Coubard |
0:4c8f8bf32a99 | 86 | { |
Vincent Coubard |
0:4c8f8bf32a99 | 87 | /* Set everything to defaults */ |
Vincent Coubard |
0:4c8f8bf32a99 | 88 | eddyServicePtr = new EddystoneService(ble, defaultAdvPowerLevels, radioPowerLevels, eventQueue); |
Vincent Coubard |
0:4c8f8bf32a99 | 89 | |
Vincent Coubard |
0:4c8f8bf32a99 | 90 | /* Set default URL, UID and TLM frame data if not initialized through the config service */ |
Vincent Coubard |
0:4c8f8bf32a99 | 91 | const char* url = YOTTA_CFG_EDDYSTONE_DEFAULT_URL; |
Vincent Coubard |
0:4c8f8bf32a99 | 92 | eddyServicePtr->setURLData(url); |
Vincent Coubard |
0:4c8f8bf32a99 | 93 | eddyServicePtr->setUIDData(uidNamespaceID, uidInstanceID); |
Vincent Coubard |
0:4c8f8bf32a99 | 94 | eddyServicePtr->setTLMData(tlmVersion); |
Vincent Coubard |
0:4c8f8bf32a99 | 95 | } |
Vincent Coubard |
0:4c8f8bf32a99 | 96 | |
Vincent Coubard |
0:4c8f8bf32a99 | 97 | static void bleInitComplete(BLE::InitializationCompleteCallbackContext* initContext) |
Vincent Coubard |
0:4c8f8bf32a99 | 98 | { |
Vincent Coubard |
0:4c8f8bf32a99 | 99 | BLE &ble = initContext->ble; |
Vincent Coubard |
0:4c8f8bf32a99 | 100 | ble_error_t error = initContext->error; |
Vincent Coubard |
0:4c8f8bf32a99 | 101 | |
Vincent Coubard |
0:4c8f8bf32a99 | 102 | if (error != BLE_ERROR_NONE) { |
Vincent Coubard |
0:4c8f8bf32a99 | 103 | onBleInitError(initContext); |
Vincent Coubard |
0:4c8f8bf32a99 | 104 | return; |
Vincent Coubard |
0:4c8f8bf32a99 | 105 | } |
Vincent Coubard |
0:4c8f8bf32a99 | 106 | |
Vincent Coubard |
0:4c8f8bf32a99 | 107 | ble.gap().onDisconnection(disconnectionCallback); |
Vincent Coubard |
0:4c8f8bf32a99 | 108 | |
Vincent Coubard |
0:4c8f8bf32a99 | 109 | EddystoneService::EddystoneParams_t params; |
Vincent Coubard |
0:4c8f8bf32a99 | 110 | if (loadEddystoneServiceConfigParams(¶ms)) { |
Vincent Coubard |
0:4c8f8bf32a99 | 111 | eddyServicePtr = new EddystoneService(ble, params, radioPowerLevels, eventQueue); |
Vincent Coubard |
0:4c8f8bf32a99 | 112 | } else { |
Vincent Coubard |
0:4c8f8bf32a99 | 113 | initializeEddystoneToDefaults(ble); |
Vincent Coubard |
0:4c8f8bf32a99 | 114 | } |
Vincent Coubard |
0:4c8f8bf32a99 | 115 | |
Vincent Coubard |
0:4c8f8bf32a99 | 116 | /* Start Eddystone in config mode */ |
Vincent Coubard |
0:4c8f8bf32a99 | 117 | eddyServicePtr->startConfigService(); |
Vincent Coubard |
0:4c8f8bf32a99 | 118 | |
Vincent Coubard |
0:4c8f8bf32a99 | 119 | eventQueue.post_in(timeout, CONFIG_ADVERTISEMENT_TIMEOUT_SECONDS * 1000); |
Vincent Coubard |
0:4c8f8bf32a99 | 120 | } |
Vincent Coubard |
0:4c8f8bf32a99 | 121 | |
Vincent Coubard |
0:4c8f8bf32a99 | 122 | void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) { |
Vincent Coubard |
0:4c8f8bf32a99 | 123 | BLE &ble = BLE::Instance(); |
Vincent Coubard |
0:4c8f8bf32a99 | 124 | eventQueue.post(Callback<void()>(&ble, &BLE::processEvents)); |
Vincent Coubard |
0:4c8f8bf32a99 | 125 | } |
Vincent Coubard |
0:4c8f8bf32a99 | 126 | |
Vincent Coubard |
0:4c8f8bf32a99 | 127 | |
Vincent Coubard |
0:4c8f8bf32a99 | 128 | int main() |
Vincent Coubard |
0:4c8f8bf32a99 | 129 | { |
Vincent Coubard |
0:4c8f8bf32a99 | 130 | /* Tell standard C library to not allocate large buffers for these streams */ |
Vincent Coubard |
0:4c8f8bf32a99 | 131 | setbuf(stdout, NULL); |
Vincent Coubard |
0:4c8f8bf32a99 | 132 | setbuf(stderr, NULL); |
Vincent Coubard |
0:4c8f8bf32a99 | 133 | setbuf(stdin, NULL); |
Vincent Coubard |
0:4c8f8bf32a99 | 134 | |
Vincent Coubard |
0:4c8f8bf32a99 | 135 | eventQueue.post_every(blinky, 500); |
Vincent Coubard |
0:4c8f8bf32a99 | 136 | |
Vincent Coubard |
0:4c8f8bf32a99 | 137 | BLE &ble = BLE::Instance(); |
Vincent Coubard |
0:4c8f8bf32a99 | 138 | ble.onEventsToProcess(scheduleBleEventsProcessing); |
Vincent Coubard |
0:4c8f8bf32a99 | 139 | ble.init(bleInitComplete); |
Vincent Coubard |
0:4c8f8bf32a99 | 140 | |
Vincent Coubard |
0:4c8f8bf32a99 | 141 | while (true) { |
Vincent Coubard |
0:4c8f8bf32a99 | 142 | eventQueue.dispatch(); |
Vincent Coubard |
0:4c8f8bf32a99 | 143 | } |
Vincent Coubard |
0:4c8f8bf32a99 | 144 | return 0; |
Vincent Coubard |
0:4c8f8bf32a99 | 145 | } |