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:
Thu Jul 28 23:14:36 2016 +0100
Revision:
1:9db4d46bb63f
Parent:
0:4c8f8bf32a99
Child:
2:9ee673e0b86a
Merge branch 'master' of https://github.com/ARMmbed/mbed-os-example-ble


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

Who changed what in which revision?

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