Example for SDT BLE-ibeacon

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* SDT-example-ble-ibeacon
00002  * 
00003  * Copyright (c) 2018 Sigma Delta Technologies Inc.
00004  * 
00005  * MIT License
00006  * 
00007  * Permission is hereby granted, free of charge, to any person
00008  * obtaining a copy of this software and associated documentation
00009  * files (the "Software"), to deal in the Software without
00010  * restriction, including without limitation the rights to use,
00011  * copy, modify, merge, publish, distribute, sublicense, and/or sell
00012  * copies of the Software, and to permit persons to whom the
00013  * Software is furnished to do so, subject to the following
00014  * conditions:
00015  * 
00016  * The above copyright notice and this permission notice shall be
00017  * included in all copies or substantial portions of the Software.
00018  * 
00019  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00020  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
00021  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00022  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
00023  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
00024  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00025  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00026  * OTHER DEALINGS IN THE SOFTWARE.
00027  */
00028 
00029 #include "mbed.h"
00030 #include "features/FEATURE_BLE/ble/BLE.h"
00031 #include "features/FEATURE_BLE/ble/services/iBeacon.h"
00032 #include "events/mbed_events.h"
00033 
00034 /* Serial */
00035 #define BAUDRATE 9600
00036 Serial g_Serial_pc(USBTX, USBRX, BAUDRATE);
00037 
00038 /* DigitalOut */
00039 #define LED_ON      0
00040 #define LED_OFF     1
00041 DigitalOut g_DO_LedRed(LED_RED, LED_OFF);
00042 DigitalOut g_DO_LedGreen(LED_GREEN, LED_OFF);
00043 DigitalOut g_DO_LedBlue(LED_BLUE, LED_OFF);
00044 
00045 /* Ticker */
00046 Ticker g_Ticker;
00047 
00048 /* EventQueue */
00049 EventQueue g_EventQueue(4 * EVENTS_EVENT_SIZE);
00050 
00051 /* BLE */
00052 #define BLE_DEVICE_NAME "SDT Device"
00053 BLE& g_pBle = BLE::Instance();  // Default ID of instance is 'DEFAULT_INSTANCE'
00054 
00055 /* iBeacon */
00056 iBeacon* g_pIbeacon;
00057 
00058 
00059 
00060 void callbackTicker(void) {
00061     g_Serial_pc.printf("LED Toggle\n");
00062     g_DO_LedBlue = !g_DO_LedBlue;
00063 }
00064 
00065 void callbackEventsToProcess(BLE::OnEventsToProcessCallbackContext* context) {
00066     g_EventQueue.call(Callback<void()>(&g_pBle, &BLE::processEvents));
00067 }
00068 
00069 void initIBeacon(BLE& ble) {
00070     /**
00071     * The Beacon payload has the following composition:
00072     * 128-Bit / 16byte UUID = E2 0A 39 F4 73 F5 4B C4 A1 2F 17 D1 AD 07 A9 61
00073     * Major/Minor  = 0x1122 / 0x3344
00074     * Tx Power     = 0xC8 = 200, 2's compliment is 256-200 = (-56dB)
00075     *
00076     * Note: please remember to calibrate your beacons TX Power for more accurate results.
00077     */
00078     const uint8_t uuid[] = {0xE2, 0x0A, 0x39, 0xF4, 0x73, 0xF5, 0x4B, 0xC4,
00079                             0xA1, 0x2F, 0x17, 0xD1, 0xAD, 0x07, 0xA9, 0x61};
00080     uint16_t majorNumber = 1122;
00081     uint16_t minorNumber = 3344;
00082     uint16_t txPower     = 0xC8;
00083     g_pIbeacon = new iBeacon(ble, uuid, majorNumber, minorNumber, txPower);
00084 }
00085 
00086 void printMacAddress(BLE& ble) {
00087     /* Print out device MAC address to the console */
00088     Gap::AddressType_t addr_type;
00089     Gap::Address_t address;
00090 
00091     ble.gap().getAddress(&addr_type, address);
00092     g_Serial_pc.printf("DEVICE MAC ADDRESS = ");
00093     for (int i = 5; i >= 1; i--){
00094         g_Serial_pc.printf("%02x:", address[i]);
00095     }
00096     g_Serial_pc.printf("%02x\n", address[0]);
00097 }
00098 
00099 /**
00100  * Callback triggered when the ble initialization process has finished
00101  */
00102 void callbackBleInitComplete(BLE::InitializationCompleteCallbackContext* params) {
00103     BLE& ble = params->ble;                     // 'ble' equals g_pBle declared in global
00104     ble_error_t error = params->error;          // 'error' has BLE_ERROR_NONE if the initialization procedure started successfully.
00105 
00106     if (error == BLE_ERROR_NONE) {
00107         g_Serial_pc.printf("Initialization completed successfully\n");
00108     }
00109     else {
00110         /* In case of error, forward the error handling to onBleInitError */
00111         g_Serial_pc.printf("Initialization failled\n");
00112         return;
00113     }
00114 
00115     /* Ensure that it is the default instance of BLE */
00116     if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
00117         g_Serial_pc.printf("ID of BLE instance is not DEFAULT_INSTANCE\n");
00118         return;
00119     }
00120 
00121     /* Setup iBeacon */
00122     initIBeacon(ble);
00123 
00124     /* Setup and start advertising */
00125     printMacAddress(ble);                       // Optional function
00126     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, (const uint8_t *)BLE_DEVICE_NAME, sizeof(BLE_DEVICE_NAME) - 1);
00127     ble.gap().setAdvertisingInterval(1000);     // Advertising interval in units of milliseconds
00128     ble.gap().startAdvertising();
00129     g_Serial_pc.printf("Start advertising\n");
00130 }
00131 
00132 int main(void) {
00133     g_Serial_pc.printf("< Sigma Delta Technologies Inc. >\n\r");
00134 
00135     /* Init BLE and iBeacon */
00136     g_pBle.onEventsToProcess(callbackEventsToProcess);
00137     g_pBle.init(callbackBleInitComplete);
00138 
00139     /* Check whether IC is running or not */
00140     g_Ticker.attach(callbackTicker, 1);
00141 
00142     g_EventQueue.dispatch_forever();
00143 
00144     return 0;
00145 }