Igor Levkov / Mbed 2 deprecated BLE_PowerBank_HeyFaradey

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_SecureHeartRate 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 "BLE.h"
00019 #include "ble/services/BatteryService.h"
00020 #include "DeviceInformationService.h"
00021 #include <string> 
00022 
00023 BLE        ble;
00024 AnalogIn ain(p2); 
00025 DigitalOut led1(LED1);
00026 
00027 const static char     DEVICE_NAME[]        = "Battery";
00028 static const uint16_t uuid16_list[]        = {GattService::UUID_BATTERY_SERVICE};
00029 uint8_t pass[6] = {'1', '1', '1', '1', '1', '1'};
00030 uint8_t  currentBattery  = 40;
00031 static volatile bool  triggerSensorPolling = false;
00032 
00033 static BatteryService *batteryServicePtr;
00034 
00035 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00036 {
00037     printf("Disconnected!\r\n");
00038     ble.startAdvertising(); // restart advertising
00039 }
00040 
00041 void periodicCallback(void)
00042 {
00043     led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
00044 
00045     /* Note that the periodicCallback() executes in interrupt context, so it is safer to do
00046      * heavy-weight sensor polling from the main thread. */
00047     triggerSensorPolling = true;
00048 }
00049 
00050 void connectionCallback(const Gap::ConnectionCallbackParams_t *params)
00051 {
00052     printf("Connected!\r\n");
00053 }
00054 
00055 void passkeyDisplayCallback(Gap::Handle_t handle, const SecurityManager::Passkey_t passkey)
00056 {
00057     printf("Input passKey: ");
00058     for (unsigned i = 0; i < Gap::ADDR_LEN; i++) {
00059         printf("%c ", passkey[i]);
00060     }
00061     printf("\r\n");
00062 }
00063 
00064 void securitySetupCompletedCallback(Gap::Handle_t handle, SecurityManager::SecurityCompletionStatus_t status)
00065 {
00066     if (status == SecurityManager::SEC_STATUS_SUCCESS) {
00067         printf("Security success\r\n");
00068     } else {
00069         printf("Security failed\r\n");
00070     }
00071 }
00072 
00073 int main(void)
00074 {
00075     unsigned long dev1 = (unsigned long)NRF_FICR->DEVICEID[0];
00076     printf("DeviceID_1: ");
00077     printf("%lu", dev1);
00078     unsigned long dev2 = (unsigned long)NRF_FICR->DEVICEID[1];
00079     printf(" DeviceID_2: ");
00080     printf("%lu\n", dev2);
00081     unsigned long dev3 = (unsigned long)NRF_FICR->DEVICEADDR[0];
00082     printf("DeviceAddress_1: ");
00083     printf("%lu", dev3);
00084     unsigned long dev4 = (unsigned long)NRF_FICR->DEVICEADDR[1];
00085     printf(" DeviceAddress_2: ");
00086     printf("%lu\n", dev4);
00087     
00088     unsigned long devaddr = dev1 + dev2 + dev3 + dev4;
00089     printf("DeviceID: ");
00090     printf("%lu\n", devaddr);
00091        
00092     for (int i = 0; i < 6; i++){
00093         int k = devaddr % 10;
00094         devaddr = devaddr / 10;
00095         pass[5-i] = k + '0';
00096     }
00097     
00098     printf("passKey: ");
00099     for (unsigned i = 0; i < Gap::ADDR_LEN; i++) {
00100         printf("%c ", pass[i]);
00101     }
00102     printf("\r\n");
00103 
00104     
00105     led1 = 1;
00106     Ticker ticker;
00107     ticker.attach(periodicCallback, 1); // blink LED every second
00108     
00109     /* Initialize BLE module */
00110     ble.init();
00111     
00112     /* Initialize BLE security */
00113     bool enableBonding = true;
00114     bool requireMITM   = true;
00115     ble.securityManager().init(enableBonding, requireMITM, SecurityManager::IO_CAPS_DISPLAY_ONLY, pass);
00116     
00117     /* Set callback functions */
00118     ble.gap().onConnection(connectionCallback);
00119     ble.gap().onDisconnection(disconnectionCallback);
00120     ble.securityManager().onPasskeyDisplay(passkeyDisplayCallback);
00121     ble.securityManager().onSecuritySetupCompleted(securitySetupCompletedCallback);
00122     
00123     batteryServicePtr = new BatteryService(ble, currentBattery);
00124 
00125 
00126     /* Setup auxiliary service. */
00127     DeviceInformationService deviceInfo(ble, "ARM", "Model1", "SN1", "hw-rev1", "fw-rev1", "soft-rev1");
00128 
00129     /* Setup advertising. */
00130     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
00131     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
00132     //ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR);
00133     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
00134     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00135     ble.gap().setAdvertisingInterval(1000);
00136     
00137     ble.gap().startAdvertising();
00138 
00139     // infinite loop
00140     while (1) {
00141         // check for trigger from periodicCallback()
00142         if (triggerSensorPolling && ble.getGapState().connected) {
00143             triggerSensorPolling = false;
00144 
00145            currentBattery = ain.read()*100;
00146             batteryServicePtr->updateBatteryLevel(currentBattery);
00147             
00148         } else {
00149             ble.waitForEvent(); // low power wait for event
00150         }
00151     }
00152 }