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.

Committer:
bcostm
Date:
Fri Jul 28 10:07:05 2017 +0200
Revision:
41:97bbb1eb43d7
Parent:
29:2dfc5cc8569b
Add DISCO_L475VG_IOT01A in mbed_app.json

Who changed what in which revision?

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