https://github.com/google/physical-web

Dependencies:   BLE_API mbed nRF51822

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 "BLEDevice.h"
00019 #include "URIBeaconConfigService.h"
00020 #include "DFUService.h"
00021 #include "DeviceInformationService.h"
00022 
00023 BLEDevice ble;
00024 URIBeaconConfigService *uriBeaconConfig;
00025 
00026 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
00027 {
00028     ble.startAdvertising();
00029 }
00030 
00031 void switchCallback(void)
00032 {
00033     static bool switched = false;
00034     if (!switched) {
00035         printf("executing switch\r\n");
00036         delete uriBeaconConfig;
00037         uriBeaconConfig = NULL;
00038 
00039         static const uint8_t BEACON_UUID[] = {0xD8, 0xFE};
00040         static const uint8_t urldata[] = {
00041             BEACON_UUID[0],
00042             BEACON_UUID[1],
00043             0x00, // flags
00044             0x20, // power
00045             0x00, // http://www.
00046             'm',
00047             'b',
00048             'e',
00049             'd',
00050             0x08, // .".org"
00051         };
00052 
00053         ble.shutdown();
00054         ble.init();
00055 
00056         ble.clearAdvertisingPayload();
00057         ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, BEACON_UUID, sizeof(BEACON_UUID));
00058         ble.accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, urldata, sizeof(urldata));
00059 
00060         ble.setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED);
00061         ble.setAdvertisingInterval(1600); /* 1s; in multiples of 0.625ms. */
00062         ble.startAdvertising();
00063 
00064         switched = true;
00065     }
00066 }
00067 
00068 int main(void)
00069 {
00070     Ticker ticker;
00071     ticker.attach(switchCallback, 30);
00072 
00073     ble.init();
00074     ble.onDisconnection(disconnectionCallback);
00075 
00076     uriBeaconConfig = new URIBeaconConfigService(ble, "http://www.mbed.org");
00077     if (!uriBeaconConfig->configuredSuccessfully()) {
00078         error("failed to accommodate URI");
00079     }
00080     /* optional use of the API offered by URIBeaconConfigService */
00081     const int8_t powerLevels[] = {-20, -4, 0, 10};
00082     uriBeaconConfig->setTxPowerLevels(powerLevels);
00083     uriBeaconConfig->setTxPowerMode(URIBeaconConfigService::TX_POWER_MODE_LOW);
00084 
00085     /* Setup auxiliary services. */
00086     DFUService dfu(ble); /* To allow over-the-air firmware udpates. optional. */
00087     DeviceInformationService deviceInfo(ble, "ARM", "URIBeacon2", "SN1", "hw-rev1", "fw-rev1", "soft-rev1"); /* optional */
00088 
00089     ble.startAdvertising();
00090 
00091     while (true) {
00092         ble.waitForEvent();
00093     }
00094 }