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:
Vincent Coubard
Date:
Thu Jul 28 23:27:37 2016 +0100
Revision:
2:9ee673e0b86a
Parent:
1:9db4d46bb63f
Child:
3:5120491ba317
Sync with mbed-os-5.1.0-rc3

Who changed what in which revision?

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