For switch science magazine

Dependencies:   BLE_API mbed nRF51822

Fork of mbed_EddystoneURL_Beacon by Roy Want

Committer:
roywant
Date:
Tue Aug 18 00:45:39 2015 +0000
Revision:
1:0ad7573b5918
Parent:
0:094734b4e8bd
Child:
2:420532c6dd54
Converted BLE_URIBeacon to a Eddystone-URL by changing the Service Data UUID and Frame byte. All other edits are changing class, variable names and comments to Eddystone.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
roywant 0:094734b4e8bd 1 /* mbed Microcontroller Library
roywant 0:094734b4e8bd 2 * Copyright (c) 2006-2013 ARM Limited
roywant 0:094734b4e8bd 3 *
roywant 0:094734b4e8bd 4 * Licensed under the Apache License, Version 2.0 (the "License");
roywant 0:094734b4e8bd 5 * you may not use this file except in compliance with the License.
roywant 0:094734b4e8bd 6 * You may obtain a copy of the License at
roywant 0:094734b4e8bd 7 *
roywant 0:094734b4e8bd 8 * http://www.apache.org/licenses/LICENSE-2.0
roywant 0:094734b4e8bd 9 *
roywant 0:094734b4e8bd 10 * Unless required by applicable law or agreed to in writing, software
roywant 0:094734b4e8bd 11 * distributed under the License is distributed on an "AS IS" BASIS,
roywant 0:094734b4e8bd 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
roywant 0:094734b4e8bd 13 * See the License for the specific language governing permissions and
roywant 0:094734b4e8bd 14 * limitations under the License.
roywant 0:094734b4e8bd 15 */
roywant 0:094734b4e8bd 16
roywant 0:094734b4e8bd 17 #include "mbed.h"
roywant 0:094734b4e8bd 18 #include "ble/BLE.h"
roywant 1:0ad7573b5918 19 #include "ble/services/EddystoneURLConfigService.h"
roywant 0:094734b4e8bd 20 #include "ble/services/DFUService.h"
roywant 0:094734b4e8bd 21 #include "ble/services/DeviceInformationService.h"
roywant 0:094734b4e8bd 22 #include "ConfigParamsPersistence.h"
roywant 0:094734b4e8bd 23
roywant 0:094734b4e8bd 24 BLE ble;
roywant 1:0ad7573b5918 25 EddystoneURLConfigService *eddystoneUrlConfig;
roywant 0:094734b4e8bd 26
roywant 0:094734b4e8bd 27 /**
roywant 1:0ad7573b5918 28 * Eddystone-URL beacons can operate in two modes: a configuration mode which
roywant 1:0ad7573b5918 29 * allows a user to update settings over a connection; and normal Eddystone URL Beacon mode
roywant 1:0ad7573b5918 30 * which involves advertising a URI. Constructing an object from the EddystoneURLConfigService
roywant 1:0ad7573b5918 31 * sets up advertisements for the configuration mode. It is then up to
roywant 1:0ad7573b5918 32 * the application to switch to the Eddystone-URL beacon mode based on some timeout.
roywant 0:094734b4e8bd 33 *
roywant 0:094734b4e8bd 34 * The following help with this switch.
roywant 0:094734b4e8bd 35 */
roywant 1:0ad7573b5918 36 static const int CONFIG_ADVERTISEMENT_TIMEOUT_SECONDS = 15; // Duration after power-on that config service is available.
roywant 0:094734b4e8bd 37 Ticker configAdvertisementTimeoutTicker;
roywant 0:094734b4e8bd 38
roywant 0:094734b4e8bd 39 /**
roywant 1:0ad7573b5918 40 * Stop advertising the Eddystone URL Config Service after a delay; and switch to normal Eddystone-URL advertisements.
roywant 0:094734b4e8bd 41 */
roywant 0:094734b4e8bd 42 void timeout(void)
roywant 0:094734b4e8bd 43 {
roywant 0:094734b4e8bd 44 Gap::GapState_t state;
roywant 0:094734b4e8bd 45 state = ble.getGapState();
roywant 0:094734b4e8bd 46 if (!state.connected) { /* don't switch if we're in a connected state. */
roywant 1:0ad7573b5918 47 eddystoneUrlConfig->setupEddystoneURLAdvertisements();
roywant 0:094734b4e8bd 48 ble.startAdvertising();
roywant 0:094734b4e8bd 49
roywant 0:094734b4e8bd 50 configAdvertisementTimeoutTicker.detach(); /* disable the callback from the timeout Ticker. */
roywant 0:094734b4e8bd 51 }
roywant 0:094734b4e8bd 52 }
roywant 0:094734b4e8bd 53
roywant 0:094734b4e8bd 54 /**
roywant 0:094734b4e8bd 55 * Callback triggered upon a disconnection event. Needs to re-enable advertisements.
roywant 0:094734b4e8bd 56 */
roywant 0:094734b4e8bd 57 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
roywant 0:094734b4e8bd 58 {
roywant 0:094734b4e8bd 59 ble.startAdvertising();
roywant 0:094734b4e8bd 60 }
roywant 0:094734b4e8bd 61
roywant 0:094734b4e8bd 62 int main(void)
roywant 0:094734b4e8bd 63 {
roywant 0:094734b4e8bd 64 ble.init();
roywant 0:094734b4e8bd 65 ble.onDisconnection(disconnectionCallback);
roywant 0:094734b4e8bd 66
roywant 0:094734b4e8bd 67 /*
roywant 0:094734b4e8bd 68 * Load parameters from (platform specific) persistent storage. Parameters
roywant 1:0ad7573b5918 69 * can be set to non-default values while the Eddystone-URL beacon is in configuration
roywant 0:094734b4e8bd 70 * mode (within the first 60 seconds of power-up). Thereafter, parameters
roywant 1:0ad7573b5918 71 * get copied out to persistent storage before switching to normal EddystoneURL
roywant 0:094734b4e8bd 72 * operation.
roywant 0:094734b4e8bd 73 */
roywant 1:0ad7573b5918 74 EddystoneURLConfigService::Params_t params;
roywant 1:0ad7573b5918 75 bool fetchedFromPersistentStorage = loadEddystoneURLConfigParams(&params);
roywant 0:094734b4e8bd 76
roywant 1:0ad7573b5918 77 /* Initialize a Eddystone URL Config service providing config params, default URI, and power levels. */
roywant 1:0ad7573b5918 78 static EddystoneURLConfigService::PowerLevels_t defaultAdvPowerLevels = {-20, -4, 0, 10}; // Values for ADV packets related to firmware levels
roywant 1:0ad7573b5918 79 eddystoneUrlConfig= new EddystoneURLConfigService(ble, params, !fetchedFromPersistentStorage, "http://physical-web.org", defaultAdvPowerLevels);
roywant 1:0ad7573b5918 80 if (!eddystoneUrlConfig->configuredSuccessfully()) {
roywant 0:094734b4e8bd 81 error("failed to accommodate URI");
roywant 0:094734b4e8bd 82 }
roywant 0:094734b4e8bd 83 configAdvertisementTimeoutTicker.attach(timeout, CONFIG_ADVERTISEMENT_TIMEOUT_SECONDS);
roywant 0:094734b4e8bd 84
roywant 0:094734b4e8bd 85 // Setup auxiliary services to allow over-the-air firmware updates, etc
roywant 0:094734b4e8bd 86 DFUService dfu(ble);
roywant 1:0ad7573b5918 87 DeviceInformationService deviceInfo(ble, "ARM", "Eddystone-URL", "SN1", "hw-rev1", "fw-rev1", "soft-rev1");
roywant 0:094734b4e8bd 88
roywant 1:0ad7573b5918 89 ble.startAdvertising(); /* Set the whole thing in motion. After this call a GAP central can scan the EddystoneURLConfig
roywant 1:0ad7573b5918 90 * service. This can then be switched to the normal Eddystone-URL beacon functionality after a timeout. */
roywant 0:094734b4e8bd 91
roywant 0:094734b4e8bd 92 while (true) {
roywant 0:094734b4e8bd 93 ble.waitForEvent();
roywant 0:094734b4e8bd 94 }
roywant 0:094734b4e8bd 95 }