Eddystone test using modified DAL

Dependencies:   BLE_API mbed-dev-bin nRF51822

Dependents:   microbit-eddystone

Fork of microbit-dal by Lancaster University

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MicroBitTemperatureService.cpp Source File

MicroBitTemperatureService.cpp

00001 /*
00002 The MIT License (MIT)
00003 
00004 Copyright (c) 2016 British Broadcasting Corporation.
00005 This software is provided by Lancaster University by arrangement with the BBC.
00006 
00007 Permission is hereby granted, free of charge, to any person obtaining a
00008 copy of this software and associated documentation files (the "Software"),
00009 to deal in the Software without restriction, including without limitation
00010 the rights to use, copy, modify, merge, publish, distribute, sublicense,
00011 and/or sell copies of the Software, and to permit persons to whom the
00012 Software is furnished to do so, subject to the following conditions:
00013 
00014 The above copyright notice and this permission notice shall be included in
00015 all copies or substantial portions of the Software.
00016 
00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00018 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00020 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00021 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00022 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00023 DEALINGS IN THE SOFTWARE.
00024 */
00025 
00026 /**
00027   * Class definition for the custom MicroBit Temperature Service.
00028   * Provides a BLE service to remotely read the silicon temperature of the nRF51822.
00029   */
00030 #include "MicroBitConfig.h"
00031 #include "ble/UUID.h"
00032 
00033 #include "MicroBitTemperatureService.h"
00034 
00035 /**
00036   * Constructor.
00037   * Create a representation of the TemperatureService
00038   * @param _ble The instance of a BLE device that we're running on.
00039   * @param _thermometer An instance of MicroBitThermometer to use as our temperature source.
00040   */
00041 MicroBitTemperatureService::MicroBitTemperatureService(BLEDevice &_ble, MicroBitThermometer &_thermometer) :
00042         ble(_ble), thermometer(_thermometer)
00043 {
00044     // Create the data structures that represent each of our characteristics in Soft Device.
00045     GattCharacteristic  temperatureDataCharacteristic(MicroBitTemperatureServiceDataUUID, (uint8_t *)&temperatureDataCharacteristicBuffer, 0,
00046     sizeof(temperatureDataCharacteristicBuffer), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
00047 
00048     GattCharacteristic  temperaturePeriodCharacteristic(MicroBitTemperatureServicePeriodUUID, (uint8_t *)&temperaturePeriodCharacteristicBuffer, 0,
00049     sizeof(temperaturePeriodCharacteristicBuffer), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE);
00050 
00051     // Initialise our characteristic values.
00052     temperatureDataCharacteristicBuffer = 0;
00053     temperaturePeriodCharacteristicBuffer = thermometer.getPeriod();
00054 
00055     // Set default security requirements
00056     temperatureDataCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL);
00057     temperaturePeriodCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL);
00058 
00059     GattCharacteristic *characteristics[] = {&temperatureDataCharacteristic, &temperaturePeriodCharacteristic};
00060     GattService         service(MicroBitTemperatureServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
00061 
00062     ble.addService(service);
00063 
00064     temperatureDataCharacteristicHandle = temperatureDataCharacteristic.getValueHandle();
00065     temperaturePeriodCharacteristicHandle = temperaturePeriodCharacteristic.getValueHandle();
00066 
00067     ble.gattServer().write(temperatureDataCharacteristicHandle,(uint8_t *)&temperatureDataCharacteristicBuffer, sizeof(temperatureDataCharacteristicBuffer));
00068     ble.gattServer().write(temperaturePeriodCharacteristicHandle,(uint8_t *)&temperaturePeriodCharacteristicBuffer, sizeof(temperaturePeriodCharacteristicBuffer));
00069 
00070     ble.onDataWritten(this, &MicroBitTemperatureService::onDataWritten);
00071     if (EventModel::defaultEventBus)
00072         EventModel::defaultEventBus->listen(MICROBIT_ID_THERMOMETER, MICROBIT_THERMOMETER_EVT_UPDATE, this, &MicroBitTemperatureService::temperatureUpdate, MESSAGE_BUS_LISTENER_IMMEDIATE);
00073 }
00074 
00075 /**
00076   * Temperature update callback
00077   */
00078 void MicroBitTemperatureService::temperatureUpdate(MicroBitEvent)
00079 {
00080     if (ble.getGapState().connected)
00081     {
00082         temperatureDataCharacteristicBuffer = thermometer.getTemperature();
00083         ble.gattServer().notify(temperatureDataCharacteristicHandle,(uint8_t *)&temperatureDataCharacteristicBuffer, sizeof(temperatureDataCharacteristicBuffer));
00084     }
00085 }
00086 
00087 /**
00088   * Callback. Invoked when any of our attributes are written via BLE.
00089   */
00090 void MicroBitTemperatureService::onDataWritten(const GattWriteCallbackParams *params)
00091 {
00092     if (params->handle == temperaturePeriodCharacteristicHandle && params->len >= sizeof(temperaturePeriodCharacteristicBuffer))
00093     {
00094         temperaturePeriodCharacteristicBuffer = *((uint16_t *)params->data);
00095         thermometer.setPeriod(temperaturePeriodCharacteristicBuffer);
00096 
00097         // The accelerometer will choose the nearest period to that requested that it can support
00098         // Read back the ACTUAL period it is using, and report this back.
00099         temperaturePeriodCharacteristicBuffer = thermometer.getPeriod();
00100         ble.gattServer().write(temperaturePeriodCharacteristicHandle, (const uint8_t *)&temperaturePeriodCharacteristicBuffer, sizeof(temperaturePeriodCharacteristicBuffer));
00101     }
00102 }
00103 
00104 
00105 const uint8_t  MicroBitTemperatureServiceUUID[] = {
00106     0xe9,0x5d,0x61,0x00,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8
00107 };
00108 
00109 const uint8_t  MicroBitTemperatureServiceDataUUID[] = {
00110     0xe9,0x5d,0x92,0x50,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8
00111 };
00112 
00113 const uint8_t  MicroBitTemperatureServicePeriodUUID[] = {
00114     0xe9,0x5d,0x1b,0x25,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8
00115 };