Test for central Device A iBeacon

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 "iBeaconService.h"
00019 
00020 #define BLE_CHECK(X)  (X == BLE_ERROR_NONE) ? (printf("{{success}}\r\n")) : printf("{{failure}} %s at line %u ERROR CODE: %u\r\n", #X, __LINE__, (X));
00021 #define BLE_EQUAL(X,Y) ((X)==(Y)) ? (printf("{{sucess}}\n")) : printf("{{failure}}\n");
00022     
00023 /**
00024  * For this demo application, populate the beacon advertisement payload
00025  * with 2 AD structures: FLAG and MSD (manufacturer specific data).
00026  *
00027  * Reference:
00028  *  Bluetooth Core Specification 4.0 (Vol. 3), Part C, Section 11, 18
00029  */
00030 
00031 BLE ble;
00032 Ticker tick;
00033 DigitalOut myled(LED1);
00034 /**
00035  * The Beacon payload has the following composition:
00036  * 128-Bit / 16byte UUID = E2 0A 39 F4 73 F5 4B C4 A1 2F 17 D1 AD 07 A9 61
00037  * Major/Minor  = 0x1122 / 0x3344
00038  * Tx Power     = 0xC8 = 200, 2's compliment is 256-200 = (-56dB)
00039  *
00040  * Note: please remember to calibrate your beacons
00041  * TX Power for more accurate results.
00042  */
00043 const uint8_t uuid[] = {0xE2, 0x0A, 0x39, 0xF4, 0x73, 0xF5, 0x4B, 0xC4,
00044                         0xA1, 0x2F, 0x17, 0xD1, 0xAD, 0x07, 0xA9, 0x61
00045                        };
00046 uint16_t majorNumber = 1122;
00047 uint16_t minorNumber = 3344;
00048 uint16_t txPower = 0xC8;
00049 
00050 Gap::AddressType_t addressType;
00051 Gap::Address_t address;
00052 Gap::Address_t addrTest = {110,100,100,100,100,100};
00053 const static char     DEVICE_NAME[]        = "IBEACONTEST";
00054 Gap::GapState_t state;
00055 
00056 void changeAdvPay(void);
00057 void changeScanRes(void);
00058 /**
00059 * Test to change advertisement interval
00060 */
00061 void changeAdvTime(void){
00062     tick.detach();
00063     BLE_CHECK(ble.gap().stopAdvertising());
00064     ble.gap().setAdvertisingTimeout(0);
00065     ble.gap().setAdvertisingInterval(500);
00066     BLE_CHECK(ble.gap().startAdvertising());
00067 }
00068 /**
00069 * Test to change advertisement payload
00070 */
00071 void changeAdvPay(void){
00072     tick.detach();
00073     BLE_CHECK(ble.gap().stopAdvertising());
00074     ble.gap().clearAdvertisingPayload();
00075     ble.gap().setAdvertisingTimeout(0);
00076     BLE_CHECK(ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::LE_GENERAL_DISCOVERABLE | GapAdvertisingData::BREDR_NOT_SUPPORTED));
00077     BLE_CHECK(ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::OUTDOOR_GENERIC));
00078     BLE_CHECK(ble.gap().accumulateAdvertisingPayloadTxPower(10));
00079     uint8_t data[5] = {123,123,123,123,123};
00080     BLE_CHECK(ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, data, 5));
00081     //BLE_CHECK(ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)));
00082     ble.gap().setAdvertisingInterval(500);
00083     BLE_CHECK(ble.gap().startAdvertising());
00084 }
00085 /**
00086 * Test to change add a scan response
00087 */
00088 void changeScanRes(void){
00089     tick.detach();
00090     BLE_CHECK(ble.gap().stopAdvertising());
00091     ble.gap().clearAdvertisingPayload();
00092     ble.gap().clearScanResponse();
00093     ble.gap().setAdvertisingTimeout(0);
00094     ble.setAdvertisingType(GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED);
00095     uint8_t data2[5] = {50,50,50,50,50};
00096     BLE_CHECK(ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, data2, sizeof(data2)));
00097     uint8_t data[5] = {50,50,50,50,50};
00098     BLE_CHECK(ble.gap().accumulateScanResponse(GapAdvertisingData::SERVICE_DATA, data, sizeof(data))); 
00099     ble.gap().setAdvertisingInterval(500);
00100     BLE_CHECK(ble.gap().startAdvertising());
00101 }
00102 /**
00103 * Test to change advertisement timeout
00104 */
00105 void timeoutTest(void){
00106     BLE_CHECK(ble.gap().stopAdvertising());
00107     ble.gap().clearAdvertisingPayload();
00108     ble.gap().clearScanResponse();
00109     ble.gap().setAdvertisingTimeout(5);
00110     BLE_CHECK(ble.gap().startAdvertising());
00111 }
00112 /**
00113 * Reset function run after every test
00114 */
00115 void reset(void){
00116     tick.detach();
00117     ble.gap().stopAdvertising();
00118     ble.gap().clearAdvertisingPayload();
00119     ble.gap().clearScanResponse();
00120     ble.gap().setAdvertisingTimeout(0);
00121     ble.gap().setAdvertisingInterval(1000);
00122     uint8_t data2[5] = {0,0,0,0,0};
00123     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, data2, sizeof(data2));
00124     BLE_CHECK(ble.gap().startAdvertising());
00125 }
00126 /**
00127 * Controls which tests are run from input from PC
00128 */
00129 void testCentre(void){
00130     int x;
00131     scanf("%d" , &x); 
00132     printf("%d:%d:%d:%d:%d:%d\n", address[0], address[1], address[2], address[3], address[4], address[5]);
00133     GapAdvertisingData testData = ble.gap().getAdvertisingPayload();
00134     state = ble.gap().getState();
00135     !(testData.getPayload()) ? printf("{{failure}} ble.gap().getAdvertisingPayload() at line %u\r\n", __LINE__) : printf("{{success}}\r\n");
00136     (state.connected == 1 || state.advertising == 0) ? printf("{{failure}} ble.gap().getState() at line %u\r\n", __LINE__) : printf("{{success}}\r\n");
00137     while(true){
00138         scanf("%d", &x);
00139         switch (x){
00140             case 0:
00141                 changeAdvTime();
00142                 break;
00143             case 1:
00144                 changeAdvPay();
00145                 break;
00146             case 2:
00147                 timeoutTest();
00148                 break;
00149             case 3:
00150                 changeScanRes();
00151                 break;
00152             default:
00153                 return;
00154         }
00155         scanf("%d", &x);
00156         reset();
00157     }
00158 }
00159 
00160 int main(void)
00161 {
00162     myled = 0;
00163     
00164     BLE_CHECK(ble.init());
00165 
00166     iBeaconService ibeacon(ble, uuid, majorNumber, minorNumber, txPower);
00167 
00168     ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
00169     ble.gap().setAdvertisingTimeout(0);
00170     
00171     BLE_CHECK(ble.gap().setAddress(Gap::ADDR_TYPE_PUBLIC, addrTest));
00172     BLE_CHECK(ble.gap().getAddress(&addressType, address));
00173     BLE_CHECK(ble.gap().startAdvertising());
00174     
00175     GapAdvertisingData testData = ble.gap().getAdvertisingPayload();
00176     
00177     printf("{{success}}" "\n" "{{end}}" "\n"); //tells mbedhtrun to finish
00178     testCentre();
00179 }