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:
mbed_official
Date:
Fri Jul 29 22:45:40 2016 +0100
Revision:
3:5120491ba317
Parent:
2:9ee673e0b86a
Child:
12:b31d7c0f906c
Merge branch 'master' of https://github.com/ARMmbed/mbed-os-example-ble


Commit copied from https://github.com/ARMmbed/mbed-os-example-ble

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 3:5120491ba317 17 #include <mbed-events/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 3:5120491ba317 42 static EventQueue eventQueue(
mbed_official 3:5120491ba317 43 /* event count */ 16 * /* event size */ 32
mbed_official 3:5120491ba317 44 );
mbed_official 3:5120491ba317 45
mbed_official 3:5120491ba317 46 DigitalOut led(LED1, 1);
mbed_official 3:5120491ba317 47
mbed_official 3:5120491ba317 48 /**
mbed_official 3:5120491ba317 49 * Callback triggered upon a disconnection event.
mbed_official 3:5120491ba317 50 */
mbed_official 3:5120491ba317 51 static void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *cbParams)
mbed_official 3:5120491ba317 52 {
mbed_official 3:5120491ba317 53 (void) cbParams;
mbed_official 3:5120491ba317 54 BLE::Instance().gap().startAdvertising();
mbed_official 3:5120491ba317 55 }
mbed_official 3:5120491ba317 56
mbed_official 3:5120491ba317 57 /**
mbed_official 3:5120491ba317 58 * Callback triggered some time after application started to switch to beacon mode.
mbed_official 3:5120491ba317 59 */
mbed_official 3:5120491ba317 60 static void timeout(void)
mbed_official 3:5120491ba317 61 {
mbed_official 3:5120491ba317 62 Gap::GapState_t state;
mbed_official 3:5120491ba317 63 state = BLE::Instance().gap().getState();
mbed_official 3:5120491ba317 64 if (!state.connected) { /* don't switch if we're in a connected state. */
mbed_official 3:5120491ba317 65 eddyServicePtr->startBeaconService();
mbed_official 3:5120491ba317 66 EddystoneService::EddystoneParams_t params;
mbed_official 3:5120491ba317 67 eddyServicePtr->getEddystoneParams(params);
mbed_official 3:5120491ba317 68 saveEddystoneServiceConfigParams(&params);
mbed_official 3:5120491ba317 69 } else {
mbed_official 3:5120491ba317 70 eventQueue.post_in(CONFIG_ADVERTISEMENT_TIMEOUT_SECONDS * 1000, timeout);
mbed_official 3:5120491ba317 71 }
mbed_official 3:5120491ba317 72 }
mbed_official 3:5120491ba317 73
mbed_official 3:5120491ba317 74 static void blinky(void)
mbed_official 3:5120491ba317 75 {
mbed_official 3:5120491ba317 76 led = !led;
mbed_official 3:5120491ba317 77 }
mbed_official 3:5120491ba317 78
mbed_official 3:5120491ba317 79 static void onBleInitError(BLE::InitializationCompleteCallbackContext* initContext)
mbed_official 3:5120491ba317 80 {
mbed_official 3:5120491ba317 81 /* Initialization error handling goes here... */
mbed_official 3:5120491ba317 82 (void) initContext;
mbed_official 3:5120491ba317 83 }
mbed_official 3:5120491ba317 84
mbed_official 3:5120491ba317 85 static void initializeEddystoneToDefaults(BLE &ble)
mbed_official 3:5120491ba317 86 {
mbed_official 3:5120491ba317 87 /* Set everything to defaults */
mbed_official 3:5120491ba317 88 eddyServicePtr = new EddystoneService(ble, defaultAdvPowerLevels, radioPowerLevels, eventQueue);
mbed_official 3:5120491ba317 89
mbed_official 3:5120491ba317 90 /* Set default URL, UID and TLM frame data if not initialized through the config service */
mbed_official 3:5120491ba317 91 const char* url = YOTTA_CFG_EDDYSTONE_DEFAULT_URL;
mbed_official 3:5120491ba317 92 eddyServicePtr->setURLData(url);
mbed_official 3:5120491ba317 93 eddyServicePtr->setUIDData(uidNamespaceID, uidInstanceID);
mbed_official 3:5120491ba317 94 eddyServicePtr->setTLMData(tlmVersion);
mbed_official 3:5120491ba317 95 }
mbed_official 3:5120491ba317 96
mbed_official 3:5120491ba317 97 static void bleInitComplete(BLE::InitializationCompleteCallbackContext* initContext)
mbed_official 3:5120491ba317 98 {
mbed_official 3:5120491ba317 99 BLE &ble = initContext->ble;
mbed_official 3:5120491ba317 100 ble_error_t error = initContext->error;
mbed_official 3:5120491ba317 101
mbed_official 3:5120491ba317 102 if (error != BLE_ERROR_NONE) {
mbed_official 3:5120491ba317 103 onBleInitError(initContext);
mbed_official 3:5120491ba317 104 return;
mbed_official 3:5120491ba317 105 }
mbed_official 3:5120491ba317 106
mbed_official 3:5120491ba317 107 ble.gap().onDisconnection(disconnectionCallback);
mbed_official 3:5120491ba317 108
mbed_official 3:5120491ba317 109 EddystoneService::EddystoneParams_t params;
mbed_official 3:5120491ba317 110 if (loadEddystoneServiceConfigParams(&params)) {
mbed_official 3:5120491ba317 111 eddyServicePtr = new EddystoneService(ble, params, radioPowerLevels, eventQueue);
mbed_official 3:5120491ba317 112 } else {
mbed_official 3:5120491ba317 113 initializeEddystoneToDefaults(ble);
mbed_official 3:5120491ba317 114 }
mbed_official 3:5120491ba317 115
mbed_official 3:5120491ba317 116 /* Start Eddystone in config mode */
mbed_official 3:5120491ba317 117 eddyServicePtr->startConfigService();
mbed_official 3:5120491ba317 118
mbed_official 3:5120491ba317 119 eventQueue.post_in(CONFIG_ADVERTISEMENT_TIMEOUT_SECONDS * 1000, timeout);
mbed_official 3:5120491ba317 120 }
mbed_official 3:5120491ba317 121
mbed_official 3:5120491ba317 122 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
mbed_official 3:5120491ba317 123 BLE &ble = BLE::Instance();
mbed_official 3:5120491ba317 124 eventQueue.post(Callback<void()>(&ble, &BLE::processEvents));
mbed_official 3:5120491ba317 125 }
mbed_official 3:5120491ba317 126
mbed_official 3:5120491ba317 127
mbed_official 3:5120491ba317 128 int main()
mbed_official 3:5120491ba317 129 {
mbed_official 3:5120491ba317 130 /* Tell standard C library to not allocate large buffers for these streams */
mbed_official 3:5120491ba317 131 setbuf(stdout, NULL);
mbed_official 3:5120491ba317 132 setbuf(stderr, NULL);
mbed_official 3:5120491ba317 133 setbuf(stdin, NULL);
mbed_official 3:5120491ba317 134
mbed_official 3:5120491ba317 135 eventQueue.post_every(500, blinky);
mbed_official 3:5120491ba317 136
mbed_official 3:5120491ba317 137 BLE &ble = BLE::Instance();
mbed_official 3:5120491ba317 138 ble.onEventsToProcess(scheduleBleEventsProcessing);
mbed_official 3:5120491ba317 139 ble.init(bleInitComplete);
mbed_official 3:5120491ba317 140
mbed_official 3:5120491ba317 141 while (true) {
mbed_official 3:5120491ba317 142 eventQueue.dispatch();
mbed_official 3:5120491ba317 143 }
mbed_official 3:5120491ba317 144 return 0;
mbed_official 3:5120491ba317 145 }