Camilo Soto / Mbed 2 deprecated BLE_Basic

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_Basic by Mika Karaila

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  * Author: Mika Karaila, Metso Automation Inc.
00017  *
00018  * This Bluetooth LE example contains all basic device characteristics.
00019  * Measurement services are simulated for battery & temperature.
00020  *
00021  */
00022 
00023 #include "mbed.h"
00024 #include "BLEDevice.h"
00025 
00026 BLEDevice  ble;
00027 DigitalOut led1(LED1);
00028 DigitalOut led2(LED2);
00029 
00030 #define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console;
00031                                * it will have an impact on code-size and power consumption. */
00032 
00033 #if NEED_CONSOLE_OUTPUT
00034 Serial  pc(p12, p11);
00035 #define DEBUG(...) { pc.printf(__VA_ARGS__); }
00036 #else
00037 #define DEBUG(...) /* nothing */
00038 #endif /* #if NEED_CONSOLE_OUTPUT */
00039 
00040 // DEVICE_NAME_UUID = '2A00'; // Nordic fixed this to nRF5, can be written
00041 
00042 // Temperature 0-100 C, simulated
00043 static uint8_t      temperature = 32;
00044 GattCharacteristic  tempMeas(GattCharacteristic::UUID_TEMPERATURE_MEASUREMENT_CHAR, (uint8_t *)temperature, sizeof(temperature), sizeof(temperature),
00045                            GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
00046 GattCharacteristic *temperatureChars[] = {&tempMeas };
00047 // TODO add custom service instead of Heart rate
00048 GattService         temperatureService(GattService::UUID_HEART_RATE_SERVICE, temperatureChars, sizeof(temperatureChars) / sizeof(GattCharacteristic *));
00049 
00050 // SYSTEM
00051 static char         systemId = 'A';
00052 GattCharacteristic  systemID(GattCharacteristic::UUID_SYSTEM_ID_CHAR, (uint8_t *)systemId, sizeof(systemId), sizeof(systemId),
00053                            GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
00054 GattCharacteristic *systemChars[] = {&systemID };
00055 GattService         systemService(GattService::UUID_DEVICE_INFORMATION_SERVICE, systemChars, sizeof(systemChars) / sizeof(GattCharacteristic *));
00056 
00057 // MODEL
00058 static char         model[31] = "Test sensor";
00059 GattCharacteristic  modelID(GattCharacteristic::UUID_MODEL_NUMBER_STRING_CHAR, (uint8_t *)model, sizeof(model), sizeof(model),
00060                            GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
00061 GattCharacteristic *modelChars[] = {&modelID };
00062 GattService         modelService(GattService::UUID_DEVICE_INFORMATION_SERVICE, modelChars, sizeof(modelChars) / sizeof(GattCharacteristic *));
00063 
00064 // Firmware
00065 static char         fwversion[31] = "Firmware: 0216";
00066 GattCharacteristic  fwChars(GattCharacteristic::UUID_FIRMWARE_REVISION_STRING_CHAR, (uint8_t *)fwversion, sizeof(fwversion), sizeof(fwversion),
00067                            GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
00068 GattCharacteristic *firmwareChars[] = {&fwChars };
00069 GattService         firmwareService(GattService::UUID_DEVICE_INFORMATION_SERVICE, firmwareChars, sizeof(firmwareChars) / sizeof(GattCharacteristic *));
00070 
00071 // Software
00072 static char         swversion[31] = "Sensor build (0001)";
00073 GattCharacteristic  swChars(GattCharacteristic::UUID_SOFTWARE_REVISION_STRING_CHAR, (uint8_t *)swversion, sizeof(swversion), sizeof(swversion),
00074                            GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
00075 GattCharacteristic *softwareChars[] = {&swChars };
00076 GattService         softwareService(GattService::UUID_DEVICE_INFORMATION_SERVICE, softwareChars, sizeof(softwareChars) / sizeof(GattCharacteristic *));
00077 // Hardware
00078 static char         hwversion[31] = "Sensor hw proto 0";
00079 GattCharacteristic  hwChars(GattCharacteristic::UUID_HARDWARE_REVISION_STRING_CHAR, (uint8_t *)hwversion, sizeof(hwversion), sizeof(hwversion),
00080                            GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
00081 GattCharacteristic *hardwareChars[] = {&hwChars };
00082 GattService         hardwareService(GattService::UUID_DEVICE_INFORMATION_SERVICE, hardwareChars, sizeof(hardwareChars) / sizeof(GattCharacteristic *));
00083 // Manufacturer
00084 static char         vendor[31] = "Test Company Inc.";
00085 GattCharacteristic  vendorChars(GattCharacteristic::UUID_MANUFACTURER_NAME_STRING_CHAR, (uint8_t *)vendor, sizeof(vendor), sizeof(vendor),
00086                            GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
00087 GattCharacteristic *manufacturerChars[] = {&vendorChars };
00088 GattService         manufacturerService(GattService::UUID_DEVICE_INFORMATION_SERVICE, manufacturerChars, sizeof(manufacturerChars) / sizeof(GattCharacteristic *));
00089 // Serial number
00090 static char         serial[31] = "1234567890";
00091 GattCharacteristic  serialChars(GattCharacteristic::UUID_SERIAL_NUMBER_STRING_CHAR, (uint8_t *)serial, sizeof(serial), sizeof(serial),
00092                            GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
00093 GattCharacteristic *serialNumberChars[] = {&serialChars };
00094 GattService         serialNumberService(GattService::UUID_DEVICE_INFORMATION_SERVICE, serialNumberChars, sizeof(serialNumberChars) / sizeof(GattCharacteristic *));
00095 
00096 static uint8_t      batteryLevel = 100;
00097 GattCharacteristic  batteryPercentage(GattCharacteristic::UUID_BATTERY_LEVEL_CHAR, (uint8_t *)batteryLevel, sizeof(batteryLevel), sizeof(batteryLevel),
00098                            GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
00099 GattCharacteristic *batteryChars[] = {&batteryPercentage };
00100 GattService         batteryService(GattService::UUID_BATTERY_SERVICE, batteryChars, sizeof(batteryChars) / sizeof(GattCharacteristic *));
00101 
00102 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
00103 {
00104     printf("Disconnected handle %u!\n\r", handle);
00105     printf("Restarting the advertising process\n\r");
00106     ble.startAdvertising();
00107 }
00108 
00109 /**
00110  * Triggered periodically by the 'ticker' interrupt; updates hrmCounter.
00111  */
00112 void periodicCallback(void)
00113 {
00114     led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
00115     led2 = !led2;
00116     /*
00117     if (ble.getGapState().connected) {
00118         // Update the battery measurement 
00119         batteryLevel--;
00120         if (batteryLevel == 1) {
00121             batteryLevel = 100;
00122         }
00123         ble.updateCharacteristicValue(batteryPercentage.getValueAttribute().getHandle(), &batteryLevel, sizeof(batteryLevel));
00124         
00125          Update the temperature measurement 
00126         temperature++;
00127         if (temperature == 50) {
00128             temperature = 10;
00129         }
00130         ble.updateCharacteristicValue(tempMeas.getValueAttribute().getHandle(), &temperature, sizeof(temperature));
00131     }
00132     */
00133 }
00134 
00135 int main(void)
00136 {
00137     led1 = 1;
00138     Ticker ticker;
00139     ticker.attach(periodicCallback, 1);
00140 
00141     DEBUG("Initialising the nRF51822\n\r");
00142     ble.init();
00143     ble.onDisconnection(disconnectionCallback);
00144 
00145     /* setup advertising */
00146     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
00147     ble.accumulateAdvertisingPayload(GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
00148     ble.accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_THERMOMETER);
00149 
00150     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00151     ble.setAdvertisingInterval(160); /* 1000ms; in multiples of 0.625ms. */
00152     ble.startAdvertising();
00153 
00154     ble.addService(batteryService);
00155     ble.addService(firmwareService);
00156     ble.addService(softwareService);
00157     ble.addService(hardwareService);
00158     ble.addService(modelService);
00159     ble.addService(temperatureService);
00160 
00161     while (true) {
00162         ble.waitForEvent();
00163     }
00164 }