For switch science magazine

Dependencies:   BLE_API mbed nRF51822

Fork of mbed_EddystoneURL_Beacon by Roy Want

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "mbed.h"
00018 #include "ble/BLE.h"
00019 #include "ble/services/EddystoneURLConfigService.h"
00020 #include "ble/services/DFUService.h"
00021 #include "ble/services/DeviceInformationService.h"
00022 #include "ConfigParamsPersistence.h"
00023 
00024 BLE ble;
00025 EddystoneURLConfigService *eddystoneUrlConfig;
00026 
00027 /**
00028  * Eddystone-URL beacons can operate in two modes: a configuration mode which
00029  * allows a user to update settings over a connection; and normal Eddystone URL Beacon mode
00030  * which involves advertising a URI. Constructing an object from the EddystoneURLConfigService
00031  * sets up advertisements for the configuration mode. It is then up to
00032  * the application to switch to the Eddystone-URL beacon mode based on some timeout.
00033  *
00034  * The following help with this switch.
00035  */
00036 static const int CONFIG_ADVERTISEMENT_TIMEOUT_SECONDS = 30;  // Duration after power-on that config service is available.
00037 Ticker configAdvertisementTimeoutTicker;
00038 
00039 /**
00040  * Stop advertising the Eddystone URL Config Service after a delay; and switch to normal Eddystone-URL advertisements.
00041  */
00042 void timeout(void)
00043 {
00044     Gap::GapState_t state;
00045     state = ble.getGapState();
00046     if (!state.connected) { /* don't switch if we're in a connected state. */
00047         configAdvertisementTimeoutTicker.detach(); /* disable the callback from the timeout Ticker. */
00048         eddystoneUrlConfig->setupEddystoneURLAdvertisements();
00049         ble.startAdvertising();
00050     }
00051 }
00052 
00053 /**
00054  * Callback triggered upon a disconnection event. Needs to re-enable advertisements.
00055  */
00056 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
00057 {
00058     configAdvertisementTimeoutTicker.detach(); /* disable the callback from the timeout Ticker. */
00059     eddystoneUrlConfig->setupEddystoneURLAdvertisements();
00060     ble.startAdvertising();
00061 }
00062 
00063 int main(void)
00064 {
00065     ble.init();
00066     ble.onDisconnection(disconnectionCallback);
00067 
00068     /*
00069      * Load parameters from (platform specific) persistent storage. Parameters
00070      * can be set to non-default values while the Eddystone-URL beacon is in configuration
00071      * mode (within the first 60 seconds of power-up). Thereafter, parameters
00072      * get copied out to persistent storage before switching to normal EddystoneURL
00073      * operation.
00074      */
00075     EddystoneURLConfigService::Params_t params;
00076     bool fetchedFromPersistentStorage = loadEddystoneURLConfigParams(&params);
00077 
00078     /* Initialize a Eddystone URL Config service providing config params, default URI, and power levels. */
00079     static EddystoneURLConfigService::PowerLevels_t defaultAdvPowerLevels = {-20, -4, 0, 10}; // Values for ADV packets related to firmware levels
00080     eddystoneUrlConfig= new EddystoneURLConfigService(ble, params, !fetchedFromPersistentStorage, "http://ssci.to", defaultAdvPowerLevels);
00081     if (!eddystoneUrlConfig->configuredSuccessfully()) {
00082         error("failed to accommodate URI");
00083     }
00084     configAdvertisementTimeoutTicker.attach(timeout, CONFIG_ADVERTISEMENT_TIMEOUT_SECONDS);
00085 
00086     // Setup auxiliary services to allow over-the-air firmware updates, etc
00087     DFUService dfu(ble);
00088     DeviceInformationService deviceInfo(ble, "ARM", "Eddystone-URL", "SN1", "hw-rev1", "fw-rev1", "soft-rev1");
00089 
00090     ble.startAdvertising(); /* Set the whole thing in motion. After this call a GAP central can scan the EddystoneURLConfig
00091                              * service. This can then be switched to the normal Eddystone-URL beacon functionality after a timeout. */
00092 
00093     while (true) {
00094         ble.waitForEvent();
00095     }
00096 }