Sample program

Dependencies:   BLE_API mbed nRF51822

Fork of mbed_EddystoneURL_Beacon_ssci by Mako SHIMURA

Committer:
asagin
Date:
Tue Apr 26 06:44:55 2016 +0000
Revision:
6:30b60c0cd516
Parent:
5:92bda961cc04
changed URL

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 2:420532c6dd54 36 static const int CONFIG_ADVERTISEMENT_TIMEOUT_SECONDS = 30; // 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 2:420532c6dd54 47 configAdvertisementTimeoutTicker.detach(); /* disable the callback from the timeout Ticker. */
roywant 1:0ad7573b5918 48 eddystoneUrlConfig->setupEddystoneURLAdvertisements();
roywant 0:094734b4e8bd 49 ble.startAdvertising();
roywant 0:094734b4e8bd 50 }
roywant 0:094734b4e8bd 51 }
roywant 0:094734b4e8bd 52
roywant 0:094734b4e8bd 53 /**
roywant 0:094734b4e8bd 54 * Callback triggered upon a disconnection event. Needs to re-enable advertisements.
roywant 0:094734b4e8bd 55 */
roywant 0:094734b4e8bd 56 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
roywant 0:094734b4e8bd 57 {
roywant 2:420532c6dd54 58 configAdvertisementTimeoutTicker.detach(); /* disable the callback from the timeout Ticker. */
roywant 2:420532c6dd54 59 eddystoneUrlConfig->setupEddystoneURLAdvertisements();
roywant 0:094734b4e8bd 60 ble.startAdvertising();
roywant 0:094734b4e8bd 61 }
roywant 0:094734b4e8bd 62
roywant 0:094734b4e8bd 63 int main(void)
roywant 0:094734b4e8bd 64 {
roywant 0:094734b4e8bd 65 ble.init();
roywant 0:094734b4e8bd 66 ble.onDisconnection(disconnectionCallback);
roywant 0:094734b4e8bd 67
roywant 0:094734b4e8bd 68 /*
roywant 0:094734b4e8bd 69 * Load parameters from (platform specific) persistent storage. Parameters
roywant 1:0ad7573b5918 70 * can be set to non-default values while the Eddystone-URL beacon is in configuration
roywant 0:094734b4e8bd 71 * mode (within the first 60 seconds of power-up). Thereafter, parameters
roywant 1:0ad7573b5918 72 * get copied out to persistent storage before switching to normal EddystoneURL
roywant 0:094734b4e8bd 73 * operation.
roywant 0:094734b4e8bd 74 */
roywant 1:0ad7573b5918 75 EddystoneURLConfigService::Params_t params;
roywant 1:0ad7573b5918 76 bool fetchedFromPersistentStorage = loadEddystoneURLConfigParams(&params);
roywant 0:094734b4e8bd 77
roywant 1:0ad7573b5918 78 /* Initialize a Eddystone URL Config service providing config params, default URI, and power levels. */
roywant 1:0ad7573b5918 79 static EddystoneURLConfigService::PowerLevels_t defaultAdvPowerLevels = {-20, -4, 0, 10}; // Values for ADV packets related to firmware levels
asagin 6:30b60c0cd516 80 eddystoneUrlConfig= new EddystoneURLConfigService(ble, params, !fetchedFromPersistentStorage, "http://ssci.to/i/", defaultAdvPowerLevels);
roywant 1:0ad7573b5918 81 if (!eddystoneUrlConfig->configuredSuccessfully()) {
roywant 0:094734b4e8bd 82 error("failed to accommodate URI");
roywant 0:094734b4e8bd 83 }
roywant 0:094734b4e8bd 84 configAdvertisementTimeoutTicker.attach(timeout, CONFIG_ADVERTISEMENT_TIMEOUT_SECONDS);
roywant 0:094734b4e8bd 85
roywant 0:094734b4e8bd 86 // Setup auxiliary services to allow over-the-air firmware updates, etc
roywant 0:094734b4e8bd 87 DFUService dfu(ble);
roywant 1:0ad7573b5918 88 DeviceInformationService deviceInfo(ble, "ARM", "Eddystone-URL", "SN1", "hw-rev1", "fw-rev1", "soft-rev1");
roywant 0:094734b4e8bd 89
roywant 1:0ad7573b5918 90 ble.startAdvertising(); /* Set the whole thing in motion. After this call a GAP central can scan the EddystoneURLConfig
roywant 1:0ad7573b5918 91 * service. This can then be switched to the normal Eddystone-URL beacon functionality after a timeout. */
roywant 0:094734b4e8bd 92
roywant 0:094734b4e8bd 93 while (true) {
roywant 0:094734b4e8bd 94 ble.waitForEvent();
roywant 0:094734b4e8bd 95 }
asagin 5:92bda961cc04 96 }