Austin Blackstone / Mbed 2 deprecated BLE_URIBeacon_ScavengerHunt

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_URIBeacon by Bluetooth Low Energy

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 <string>
00019 #include "BLEDevice.h"
00020 #include "URIBeaconConfigService.h"
00021 #include "DFUService.h"
00022 #include "DeviceInformationService.h"
00023 #include "ConfigParamsPersistence.h"
00024 
00025 BLEDevice ble;
00026 URIBeaconConfigService *uriBeaconConfig;
00027 
00028 /**
00029  * URIBeaconConfig service can operate in two modes: a configuration mode which
00030  * allows a user to update settings over a connection; and normal URIBeacon mode
00031  * which involves advertising a URI. Constructing an object from URIBeaconConfig
00032  * service sets up advertisements for the configuration mode. It is then up to
00033  * the application to switch to URIBeacon mode based on some timeout.
00034  *
00035  * The following help with this switch.
00036  */
00037 static const int CONFIG_ADVERTISEMENT_TIMEOUT_SECONDS = 5;  // Duration after power-on that config service is available.
00038 Ticker configAdvertisementTimeoutTicker;
00039 
00040 /**
00041  * Stop advertising the UriBeaconConfig Service after a delay; and switch to normal URIBeacon.
00042  */
00043 void timeout(void)
00044 {
00045     Gap::GapState_t state;
00046     state = ble.getGapState();
00047     if (!state.connected) { /* don't switch if we're in a connected state. */
00048         uriBeaconConfig->setupURIBeaconAdvertisements();
00049         ble.startAdvertising();
00050 
00051         configAdvertisementTimeoutTicker.detach(); /* disable the callback from the timeout Ticker. */
00052     }
00053 }
00054 
00055 /**
00056  * Callback triggered upon a disconnection event. Needs to re-enable advertisements.
00057  */
00058 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
00059 {
00060     ble.startAdvertising();
00061 }
00062 
00063 int main(void)
00064 {
00065     ble.init();
00066     ble.onDisconnection(disconnectionCallback);
00067 
00068     uint8_t mac[6] = {};
00069     ble.getAddress(0,mac);
00070 
00071     printf("MAC Address = ");
00072     for(int x =0; x<7; x++) {
00073         printf("%x",mac[x]);
00074     }
00075     printf("\n\r");
00076 
00077 //    string url = "http://snurl.com/ub?q=" ;
00078     string url = "http://snurl.com/u6?q=" ;
00079     url += ((mac[0] & 0xF0) >> 4) + 'A';
00080     url += ((mac[0] & 0x0F) >> 0) + 'A';
00081     url += ((mac[1] & 0xF0) >> 4) + 'A';
00082     url += ((mac[1] & 0x0F) >> 0) + 'A';
00083     url += ((mac[2] & 0xF0) >> 4) + 'A';
00084     url += ((mac[2] & 0x0F) >> 0) + 'A';
00085     url += ((mac[3] & 0xF0) >> 4) + 'A';
00086     url += ((mac[3] & 0x0F) >> 0) + 'A';
00087     url += ((mac[4] & 0xF0) >> 4) + 'A';
00088     url += ((mac[4] & 0x0F) >> 0) + 'A';
00089     
00090     for(int y=0; y<6; y++) {
00091         printf("\n\rmac[%d]=0x%x, d%d, c%c,modH=%c, modL=%c",y,mac[y],mac[y],mac[y],((mac[y] & 0xF0) >> 4) + 'A',((mac[y] & 0x0F) >> 0) + 'A');
00092     }
00093 //    printf("\n\r0x%x->%c, 0x%x->%c\n\r%s\n\r",mac[4],(mac[4]%(122-32))+32,mac[5],(mac[5]%(122-32))+32,url);
00094     printf("\n\rurl = %s\n\r",url);
00095 
00096     /*
00097      * Load parameters from (platform specific) persistent storage. Parameters
00098      * can be set to non-default values while the URIBeacon is in configuration
00099      * mode (within the first 60 seconds of power-up). Thereafter, parameters
00100      * get copied out to persistent storage before switching to normal URIBeacon
00101      * operation.
00102      */
00103     URIBeaconConfigService::Params_t params;
00104     bool fetchedFromPersistentStorage = loadURIBeaconConfigParams(&params);
00105 
00106     /* Initialize a URIBeaconConfig service providing config params, default URI, and power levels. */
00107     static URIBeaconConfigService::PowerLevels_t defaultAdvPowerLevels = {-20, -4, 0, 10}; // Values for ADV packets related to firmware levels
00108     uriBeaconConfig = new URIBeaconConfigService(ble, params, !fetchedFromPersistentStorage, (const char*)&url[0], defaultAdvPowerLevels);
00109     if (!uriBeaconConfig->configuredSuccessfully()) {
00110         error("failed to accommodate URI");
00111     }
00112     configAdvertisementTimeoutTicker.attach(timeout, CONFIG_ADVERTISEMENT_TIMEOUT_SECONDS);
00113 
00114     // Setup auxiliary services to allow over-the-air firmware updates, etc
00115     DFUService dfu(ble);
00116     DeviceInformationService deviceInfo(ble, "ARM", "UriBeacon", "SN1", "hw-rev1", "fw-rev1", "soft-rev1");
00117 
00118     ble.startAdvertising(); /* Set the whole thing in motion. After this call a GAP central can scan the URIBeaconConfig
00119                              * service. This can then be switched to the normal URIBeacon functionality after a timeout. */
00120 
00121 
00122     while (true) {
00123         ble.waitForEvent();
00124     }
00125 }