The eddystone config service allows you to configure the eddystone frame data over BLE for a set period of time and then starts an eddystone beacon. This example defaults to 30 seconds of config time.

Dependencies:   BLE_API mbed nRF51822 X_NUCLEO_IDB0XA1

Fork of BLE_EddystoneBeaconConfigServiceRelease by Austin Blackstone

This is the eddystone config service. This code starts up and for a user configured time period (default 30 seconds) will advertise the configuration service.

The configuration service allows for modifying various frames of the eddystone specification.

For more details on the Configuration Service please see : https://github.com/google/eddystone/blob/master/eddystone-url/docs/config-service-spec.md

Committer:
mbedAustin
Date:
Thu Oct 01 16:21:03 2015 +0000
Revision:
1:5b266d95731f
Parent:
0:f528fb53a9ef
Child:
2:378323d1b4d9
Updated onDisconnection callback for latest BLE API change;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbedAustin 0:f528fb53a9ef 1 /* mbed Microcontroller Library
mbedAustin 0:f528fb53a9ef 2 * Copyright (c) 2006-2013 ARM Limited
mbedAustin 0:f528fb53a9ef 3 *
mbedAustin 0:f528fb53a9ef 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbedAustin 0:f528fb53a9ef 5 * you may not use this file except in compliance with the License.
mbedAustin 0:f528fb53a9ef 6 * You may obtain a copy of the License at
mbedAustin 0:f528fb53a9ef 7 *
mbedAustin 0:f528fb53a9ef 8 * http://www.apache.org/licenses/LICENSE-2.0
mbedAustin 0:f528fb53a9ef 9 *
mbedAustin 0:f528fb53a9ef 10 * Unless required by applicable law or agreed to in writing, software
mbedAustin 0:f528fb53a9ef 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbedAustin 0:f528fb53a9ef 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbedAustin 0:f528fb53a9ef 13 * See the License for the specific language governing permissions and
mbedAustin 0:f528fb53a9ef 14 * limitations under the License.
mbedAustin 0:f528fb53a9ef 15 */
mbedAustin 0:f528fb53a9ef 16
mbedAustin 0:f528fb53a9ef 17 #include "mbed.h"
mbedAustin 0:f528fb53a9ef 18 #include "ble/BLE.h"
mbedAustin 0:f528fb53a9ef 19 #include "ble/services/EddystoneConfigService.h"
mbedAustin 0:f528fb53a9ef 20 #include "ConfigParamsPersistence.h"
mbedAustin 0:f528fb53a9ef 21
mbedAustin 0:f528fb53a9ef 22 BLE ble;
mbedAustin 0:f528fb53a9ef 23 EddystoneConfigService *EddystoneBeaconConfig;
mbedAustin 0:f528fb53a9ef 24 EddystoneConfigService::Params_t params;
mbedAustin 0:f528fb53a9ef 25
mbedAustin 0:f528fb53a9ef 26 /**
mbedAustin 0:f528fb53a9ef 27 * URIBeaconConfig service can operate in two modes: a configuration mode which
mbedAustin 0:f528fb53a9ef 28 * allows a user to update settings over a connection; and normal URIBeacon mode
mbedAustin 0:f528fb53a9ef 29 * which involves advertising a URI. Constructing an object from URIBeaconConfig
mbedAustin 0:f528fb53a9ef 30 * service sets up advertisements for the configuration mode. It is then up to
mbedAustin 0:f528fb53a9ef 31 * the application to switch to URIBeacon mode based on some timeout.
mbedAustin 0:f528fb53a9ef 32 *
mbedAustin 0:f528fb53a9ef 33 * The following help with this switch.
mbedAustin 0:f528fb53a9ef 34 */
mbedAustin 0:f528fb53a9ef 35 static const int CONFIG_ADVERTISEMENT_TIMEOUT_SECONDS = 30; // Duration after power-on that config service is available.
mbedAustin 0:f528fb53a9ef 36
mbedAustin 0:f528fb53a9ef 37 Ticker configAdvertisementTimeout;
mbedAustin 0:f528fb53a9ef 38
mbedAustin 0:f528fb53a9ef 39 /**
mbedAustin 0:f528fb53a9ef 40 * Stop advertising the Config Service after a delay; and switch to a non-connectable advertising mode only beacon.
mbedAustin 0:f528fb53a9ef 41 */
mbedAustin 0:f528fb53a9ef 42 void timeout(void)
mbedAustin 0:f528fb53a9ef 43 {
mbedAustin 0:f528fb53a9ef 44 Gap::GapState_t state;
mbedAustin 0:f528fb53a9ef 45 state = ble.getGapState();
mbedAustin 0:f528fb53a9ef 46 if (!state.connected) { /* don't switch if we're in a connected state. */
mbedAustin 0:f528fb53a9ef 47 configAdvertisementTimeout.detach(); /* disable the callback from the timeout Ticker. */
mbedAustin 0:f528fb53a9ef 48 EddystoneBeaconConfig->setupEddystoneAdvertisements();
mbedAustin 0:f528fb53a9ef 49 }
mbedAustin 0:f528fb53a9ef 50 }
mbedAustin 0:f528fb53a9ef 51
mbedAustin 0:f528fb53a9ef 52 /**
mbedAustin 0:f528fb53a9ef 53 * Callback triggered upon a disconnection event.
mbedAustin 0:f528fb53a9ef 54 */
mbedAustin 1:5b266d95731f 55 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *cbParams)
mbedAustin 0:f528fb53a9ef 56 {
mbedAustin 0:f528fb53a9ef 57 if (true == params.isConfigured){
mbedAustin 0:f528fb53a9ef 58 // end advertising, the beacon is configured
mbedAustin 0:f528fb53a9ef 59 timeout();
mbedAustin 0:f528fb53a9ef 60 }
mbedAustin 0:f528fb53a9ef 61 else{
mbedAustin 0:f528fb53a9ef 62 // eddystone is not configured, continue advertising
mbedAustin 0:f528fb53a9ef 63 ble.gap().startAdvertising();
mbedAustin 0:f528fb53a9ef 64 }
mbedAustin 0:f528fb53a9ef 65 }
mbedAustin 0:f528fb53a9ef 66
mbedAustin 0:f528fb53a9ef 67 int main(void)
mbedAustin 0:f528fb53a9ef 68 {
mbedAustin 0:f528fb53a9ef 69 ble.init();
mbedAustin 0:f528fb53a9ef 70 ble.gap().onDisconnection(disconnectionCallback);
mbedAustin 0:f528fb53a9ef 71
mbedAustin 0:f528fb53a9ef 72 /*
mbedAustin 0:f528fb53a9ef 73 * Load parameters from (platform specific) persistent storage. Parameters
mbedAustin 0:f528fb53a9ef 74 * can be set to non-default values while the URIBeacon is in configuration
mbedAustin 0:f528fb53a9ef 75 * mode (within the first 60 seconds of power-up). Thereafter, parameters
mbedAustin 0:f528fb53a9ef 76 * get copied out to persistent storage before switching to normal URIBeacon
mbedAustin 0:f528fb53a9ef 77 * operation.
mbedAustin 0:f528fb53a9ef 78 */
mbedAustin 0:f528fb53a9ef 79 bool fetchedFromPersistentStorage = loadURIBeaconConfigParams(&params);
mbedAustin 0:f528fb53a9ef 80
mbedAustin 0:f528fb53a9ef 81 // Set UID and TLM frame data
mbedAustin 0:f528fb53a9ef 82 EddystoneConfigService::UIDNamespaceID_t uidNamespaceID = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}; // 10Byte Namespace UUID
mbedAustin 0:f528fb53a9ef 83 EddystoneConfigService::UIDInstanceID_t uidInstanceID = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}; // 6Byte Instance ID
mbedAustin 0:f528fb53a9ef 84 uint8_t tlmVersion = 0x00;
mbedAustin 0:f528fb53a9ef 85
mbedAustin 0:f528fb53a9ef 86 /* Initialize a EddystoneBeaconConfig service providing config params, default URI, and power levels. */
mbedAustin 0:f528fb53a9ef 87 static EddystoneConfigService::PowerLevels_t defaultAdvPowerLevels = {-47, -33, -21, -13}; // Values for ADV packets related to firmware levels, calibrated based on measured values at 1m
mbedAustin 0:f528fb53a9ef 88 static EddystoneConfigService::PowerLevels_t radioPowerLevels = {-30, -16, -4, 4}; // Values for radio power levels, provided by manufacturer.
mbedAustin 0:f528fb53a9ef 89
mbedAustin 0:f528fb53a9ef 90 // Create Eddystone Config Service object
mbedAustin 0:f528fb53a9ef 91 EddystoneBeaconConfig = new EddystoneConfigService(ble, params, defaultAdvPowerLevels, radioPowerLevels);
mbedAustin 0:f528fb53a9ef 92
mbedAustin 0:f528fb53a9ef 93 // Set default URI, UID and TLM frame data if not initialized through the config service
mbedAustin 0:f528fb53a9ef 94 EddystoneBeaconConfig->setDefaultURIFrameData("http://mbed.org",2);
mbedAustin 0:f528fb53a9ef 95 EddystoneBeaconConfig->setDefaultUIDFrameData(&uidNamespaceID, &uidInstanceID,5);
mbedAustin 0:f528fb53a9ef 96 EddystoneBeaconConfig->setDefaultTLMFrameData(tlmVersion,10);
mbedAustin 0:f528fb53a9ef 97
mbedAustin 0:f528fb53a9ef 98 // start the config service
mbedAustin 0:f528fb53a9ef 99 EddystoneBeaconConfig->start(!fetchedFromPersistentStorage);
mbedAustin 0:f528fb53a9ef 100
mbedAustin 0:f528fb53a9ef 101 if (!EddystoneBeaconConfig->initSuccessfully()) {
mbedAustin 0:f528fb53a9ef 102 error("failed to accommodate URI");
mbedAustin 0:f528fb53a9ef 103 }
mbedAustin 0:f528fb53a9ef 104 configAdvertisementTimeout.attach(timeout, CONFIG_ADVERTISEMENT_TIMEOUT_SECONDS);
mbedAustin 0:f528fb53a9ef 105
mbedAustin 0:f528fb53a9ef 106 ble.gap().startAdvertising(); /* Set the whole thing in motion. After this call a GAP central can scan the EddystoneBeaconConfig
mbedAustin 0:f528fb53a9ef 107 * service. This can then be switched to the normal URIBeacon functionality after a timeout. */
mbedAustin 0:f528fb53a9ef 108 while (true) {
mbedAustin 0:f528fb53a9ef 109 ble.waitForEvent();
mbedAustin 0:f528fb53a9ef 110 }
mbedAustin 0:f528fb53a9ef 111 }