mirror of https://github.com/ARMmbed/mbed-os.git
remove obsolete services and add information about the services repo
parent
c73413893f
commit
c6d2ca17d5
|
@ -1,141 +0,0 @@
|
|||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2020 ARM Limited
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef MBED_BLE_BATTERY_SERVICE_H__
|
||||
#define MBED_BLE_BATTERY_SERVICE_H__
|
||||
|
||||
#if BLE_FEATURE_GATT_SERVER
|
||||
|
||||
#include "platform/mbed_assert.h"
|
||||
|
||||
#include "ble/BLE.h"
|
||||
#include "ble/Gap.h"
|
||||
#include "ble/GattServer.h"
|
||||
|
||||
/**
|
||||
* BLE Battery service.
|
||||
*
|
||||
* @par purpose
|
||||
*
|
||||
* The battery service exposes the charge level of the battery of the device.
|
||||
* This information is exposed as a percentage from 0% to 100%; a value of 0%
|
||||
* represents a fully discharged battery, and a value of 100% represents a
|
||||
* fully charged battery.
|
||||
*
|
||||
* Clients can read the current charge level and subscribe to server initiated
|
||||
* updates of the charge level. The server delivers these updates to the subscribed
|
||||
* client in a notification packet.
|
||||
*
|
||||
* The subscription mechanism is useful to save power; it avoids unecessary data
|
||||
* traffic between the client and the server, which may be induced by polling the
|
||||
* battery level characteristic value.
|
||||
*
|
||||
* @par usage
|
||||
*
|
||||
* When this class is instantiated, it adds a battery service in the GattServer.
|
||||
*
|
||||
* The application code can use the function updateBatteryLevel() to update the
|
||||
* charge level that the service exposes and to notify the subscribed client that the
|
||||
* value changed.
|
||||
*
|
||||
* @note You can find specification of the battery service here:
|
||||
* https://www.bluetooth.com/specifications/gatt
|
||||
*
|
||||
* @attention Multiple instances of this battery service are not supported.
|
||||
*/
|
||||
class BatteryService {
|
||||
public:
|
||||
/**
|
||||
* Instantiate a battery service.
|
||||
*
|
||||
* The construction of a BatteryService adds a GATT battery service in @p
|
||||
* _ble GattServer and sets the initial charge level of the battery to @p
|
||||
* level.
|
||||
*
|
||||
* @param[in] _ble BLE device which will host the battery service.
|
||||
* @param[in] level Initial charge level of the battery. It is a percentage
|
||||
* where 0% means that the battery is fully discharged and 100% means that
|
||||
* the battery is fully charged.
|
||||
*/
|
||||
BatteryService(BLE &_ble, uint8_t level = 100) :
|
||||
ble(_ble),
|
||||
batteryLevel(level),
|
||||
batteryLevelCharacteristic(
|
||||
GattCharacteristic::UUID_BATTERY_LEVEL_CHAR,
|
||||
&batteryLevel,
|
||||
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY
|
||||
)
|
||||
{
|
||||
MBED_ASSERT(level <= 100);
|
||||
GattCharacteristic *charTable[] = { &batteryLevelCharacteristic };
|
||||
GattService batteryService(
|
||||
GattService::UUID_BATTERY_SERVICE,
|
||||
charTable,
|
||||
sizeof(charTable) / sizeof(charTable[0])
|
||||
);
|
||||
|
||||
ble.gattServer().addService(batteryService);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the battery charge level that the service exposes.
|
||||
*
|
||||
* The server sends a notification of the new value to clients that have
|
||||
* subscribed to the battery level characteristic updates, and clients
|
||||
* reading the charge level after the update obtain the updated value.
|
||||
*
|
||||
* @param newLevel Charge level of the battery. It is a percentage of the
|
||||
* remaining charge between 0% and 100%.
|
||||
*
|
||||
* @attention This function must be called in the execution context of the
|
||||
* BLE stack.
|
||||
*/
|
||||
void updateBatteryLevel(uint8_t newLevel)
|
||||
{
|
||||
MBED_ASSERT(newLevel <= 100);
|
||||
batteryLevel = newLevel;
|
||||
ble.gattServer().write(
|
||||
batteryLevelCharacteristic.getValueHandle(),
|
||||
&batteryLevel,
|
||||
1
|
||||
);
|
||||
}
|
||||
|
||||
protected:
|
||||
/**
|
||||
* Reference to the underlying BLE instance that this object is attached to.
|
||||
*
|
||||
* The services and characteristics are registered in the GattServer of
|
||||
* this BLE instance.
|
||||
*/
|
||||
BLE &ble;
|
||||
|
||||
/**
|
||||
* The current battery level represented as an integer from 0% to 100%.
|
||||
*/
|
||||
uint8_t batteryLevel;
|
||||
|
||||
/**
|
||||
* The GATT characteristic, which exposes the charge level.
|
||||
*/
|
||||
ReadOnlyGattCharacteristic<uint8_t> batteryLevelCharacteristic;
|
||||
};
|
||||
|
||||
#endif // BLE_FEATURE_GATT_SERVER
|
||||
|
||||
#endif /* #ifndef MBED_BLE_BATTERY_SERVICE_H__*/
|
|
@ -1,146 +0,0 @@
|
|||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2020 ARM Limited
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef __BLE_DEVICE_INFORMATION_SERVICE_H__
|
||||
#define __BLE_DEVICE_INFORMATION_SERVICE_H__
|
||||
|
||||
#include "ble/BLE.h"
|
||||
#include "ble/Gap.h"
|
||||
#include "ble/GattServer.h"
|
||||
|
||||
#if BLE_FEATURE_GATT_SERVER
|
||||
|
||||
/**
|
||||
* @class DeviceInformationService
|
||||
* @brief BLE Device Information Service
|
||||
* Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.device_information.xml
|
||||
* Manufacturer Name String Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.manufacturer_name_string.xml
|
||||
*/
|
||||
class DeviceInformationService {
|
||||
public:
|
||||
/**
|
||||
* @brief Device Information Service Constructor: copies device-specific information
|
||||
* into the BLE stack.
|
||||
*
|
||||
* @param[in] _ble
|
||||
* A reference to a BLE object for the underlying controller.
|
||||
* @param[in] manufacturersName
|
||||
* The name of the manufacturer of the device.
|
||||
* @param[in] modelNumber
|
||||
* The model number that is assigned by the device vendor.
|
||||
* @param[in] serialNumber
|
||||
* The serial number for a particular instance of the device.
|
||||
* @param[in] hardwareRevision
|
||||
* The hardware revision for the hardware within the device.
|
||||
* @param[in] firmwareRevision
|
||||
* The device's firmware version.
|
||||
* @param[in] softwareRevision
|
||||
* The device's software version.
|
||||
*/
|
||||
DeviceInformationService(BLE &_ble,
|
||||
const char *manufacturersName = nullptr,
|
||||
const char *modelNumber = nullptr,
|
||||
const char *serialNumber = nullptr,
|
||||
const char *hardwareRevision = nullptr,
|
||||
const char *firmwareRevision = nullptr,
|
||||
const char *softwareRevision = nullptr) :
|
||||
ble(_ble),
|
||||
manufacturersNameStringCharacteristic(GattCharacteristic::UUID_MANUFACTURER_NAME_STRING_CHAR,
|
||||
(uint8_t *)manufacturersName,
|
||||
(manufacturersName != nullptr) ? strlen(manufacturersName) : 0, /* Min length */
|
||||
(manufacturersName != nullptr) ? strlen(manufacturersName) : 0, /* Max length */
|
||||
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ),
|
||||
modelNumberStringCharacteristic(GattCharacteristic::UUID_MODEL_NUMBER_STRING_CHAR,
|
||||
(uint8_t *)modelNumber,
|
||||
(modelNumber != nullptr) ? strlen(modelNumber) : 0, /* Min length */
|
||||
(modelNumber != nullptr) ? strlen(modelNumber) : 0, /* Max length */
|
||||
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ),
|
||||
serialNumberStringCharacteristic(GattCharacteristic::UUID_SERIAL_NUMBER_STRING_CHAR,
|
||||
(uint8_t *)serialNumber,
|
||||
(serialNumber != nullptr) ? strlen(serialNumber) : 0, /* Min length */
|
||||
(serialNumber != nullptr) ? strlen(serialNumber) : 0, /* Max length */
|
||||
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ),
|
||||
hardwareRevisionStringCharacteristic(GattCharacteristic::UUID_HARDWARE_REVISION_STRING_CHAR,
|
||||
(uint8_t *)hardwareRevision,
|
||||
(hardwareRevision != nullptr) ? strlen(hardwareRevision) : 0, /* Min length */
|
||||
(hardwareRevision != nullptr) ? strlen(hardwareRevision) : 0, /* Max length */
|
||||
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ),
|
||||
firmwareRevisionStringCharacteristic(GattCharacteristic::UUID_FIRMWARE_REVISION_STRING_CHAR,
|
||||
(uint8_t *)firmwareRevision,
|
||||
(firmwareRevision != nullptr) ? strlen(firmwareRevision) : 0, /* Min length */
|
||||
(firmwareRevision != nullptr) ? strlen(firmwareRevision) : 0, /* Max length */
|
||||
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ),
|
||||
softwareRevisionStringCharacteristic(GattCharacteristic::UUID_SOFTWARE_REVISION_STRING_CHAR,
|
||||
(uint8_t *)softwareRevision,
|
||||
(softwareRevision != nullptr) ? strlen(softwareRevision) : 0, /* Min length */
|
||||
(softwareRevision != nullptr) ? strlen(softwareRevision) : 0, /* Max length */
|
||||
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ)
|
||||
{
|
||||
static bool serviceAdded = false; /* We only add the information service once. */
|
||||
if (serviceAdded) {
|
||||
return;
|
||||
}
|
||||
|
||||
GattCharacteristic *charTable[] = {&manufacturersNameStringCharacteristic,
|
||||
&modelNumberStringCharacteristic,
|
||||
&serialNumberStringCharacteristic,
|
||||
&hardwareRevisionStringCharacteristic,
|
||||
&firmwareRevisionStringCharacteristic,
|
||||
&softwareRevisionStringCharacteristic};
|
||||
GattService deviceInformationService(GattService::UUID_DEVICE_INFORMATION_SERVICE, charTable,
|
||||
sizeof(charTable) / sizeof(charTable[0]));
|
||||
|
||||
ble.gattServer().addService(deviceInformationService);
|
||||
serviceAdded = true;
|
||||
}
|
||||
|
||||
protected:
|
||||
/**
|
||||
* A reference to the BLE instance object to which the services and
|
||||
* characteristics will be added.
|
||||
*/
|
||||
BLE &ble;
|
||||
/**
|
||||
* BLE characterising to allow BLE peers access to the manufacturer's name.
|
||||
*/
|
||||
GattCharacteristic manufacturersNameStringCharacteristic;
|
||||
/**
|
||||
* BLE characterising to allow BLE peers access to the model number.
|
||||
*/
|
||||
GattCharacteristic modelNumberStringCharacteristic;
|
||||
/**
|
||||
* BLE characterising to allow BLE peers access to the serial number.
|
||||
*/
|
||||
GattCharacteristic serialNumberStringCharacteristic;
|
||||
/**
|
||||
* BLE characterising to allow BLE peers access to the hardware revision string.
|
||||
*/
|
||||
GattCharacteristic hardwareRevisionStringCharacteristic;
|
||||
/**
|
||||
* BLE characterising to allow BLE peers access to the firmware revision string.
|
||||
*/
|
||||
GattCharacteristic firmwareRevisionStringCharacteristic;
|
||||
/**
|
||||
* BLE characterising to allow BLE peers access to the software revision string.
|
||||
*/
|
||||
GattCharacteristic softwareRevisionStringCharacteristic;
|
||||
};
|
||||
|
||||
#endif // BLE_FEATURE_GATT_SERVER
|
||||
|
||||
#endif /* #ifndef __BLE_DEVICE_INFORMATION_SERVICE_H__*/
|
|
@ -1,111 +0,0 @@
|
|||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2020 ARM Limited
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef __BLE_ENVIRONMENTAL_SERVICE_H__
|
||||
#define __BLE_ENVIRONMENTAL_SERVICE_H__
|
||||
|
||||
#include "ble/BLE.h"
|
||||
#include "ble/Gap.h"
|
||||
#include "ble/GattServer.h"
|
||||
|
||||
#if BLE_FEATURE_GATT_SERVER
|
||||
|
||||
/**
|
||||
* @class EnvironmentalService
|
||||
* @brief BLE Environmental Service. This service provides temperature, humidity and pressure measurement.
|
||||
* Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.environmental_sensing.xml
|
||||
* Temperature: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature.xml
|
||||
* Humidity: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.humidity.xml
|
||||
* Pressure: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.pressure.xml
|
||||
*/
|
||||
class EnvironmentalService {
|
||||
public:
|
||||
typedef int16_t TemperatureType_t;
|
||||
typedef uint16_t HumidityType_t;
|
||||
typedef uint32_t PressureType_t;
|
||||
|
||||
/**
|
||||
* @brief EnvironmentalService constructor.
|
||||
* @param _ble Reference to BLE device.
|
||||
*/
|
||||
EnvironmentalService(BLE& _ble) :
|
||||
ble(_ble),
|
||||
temperatureCharacteristic(GattCharacteristic::UUID_TEMPERATURE_CHAR, &temperature),
|
||||
humidityCharacteristic(GattCharacteristic::UUID_HUMIDITY_CHAR, &humidity),
|
||||
pressureCharacteristic(GattCharacteristic::UUID_PRESSURE_CHAR, &pressure)
|
||||
{
|
||||
static bool serviceAdded = false; /* We should only ever need to add the information service once. */
|
||||
if (serviceAdded) {
|
||||
return;
|
||||
}
|
||||
|
||||
GattCharacteristic *charTable[] = { &humidityCharacteristic,
|
||||
&pressureCharacteristic,
|
||||
&temperatureCharacteristic };
|
||||
|
||||
GattService environmentalService(GattService::UUID_ENVIRONMENTAL_SERVICE, charTable, sizeof(charTable) / sizeof(charTable[0]));
|
||||
|
||||
ble.gattServer().addService(environmentalService);
|
||||
serviceAdded = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Update humidity characteristic.
|
||||
* @param newHumidityVal New humidity measurement.
|
||||
*/
|
||||
void updateHumidity(HumidityType_t newHumidityVal)
|
||||
{
|
||||
humidity = (HumidityType_t) (newHumidityVal * 100);
|
||||
ble.gattServer().write(humidityCharacteristic.getValueHandle(), (uint8_t *) &humidity, sizeof(HumidityType_t));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Update pressure characteristic.
|
||||
* @param newPressureVal New pressure measurement.
|
||||
*/
|
||||
void updatePressure(PressureType_t newPressureVal)
|
||||
{
|
||||
pressure = (PressureType_t) (newPressureVal * 10);
|
||||
ble.gattServer().write(pressureCharacteristic.getValueHandle(), (uint8_t *) &pressure, sizeof(PressureType_t));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Update temperature characteristic.
|
||||
* @param newTemperatureVal New temperature measurement.
|
||||
*/
|
||||
void updateTemperature(float newTemperatureVal)
|
||||
{
|
||||
temperature = (TemperatureType_t) (newTemperatureVal * 100);
|
||||
ble.gattServer().write(temperatureCharacteristic.getValueHandle(), (uint8_t *) &temperature, sizeof(TemperatureType_t));
|
||||
}
|
||||
|
||||
private:
|
||||
BLE& ble;
|
||||
|
||||
TemperatureType_t temperature{};
|
||||
HumidityType_t humidity{};
|
||||
PressureType_t pressure{};
|
||||
|
||||
ReadOnlyGattCharacteristic<TemperatureType_t> temperatureCharacteristic;
|
||||
ReadOnlyGattCharacteristic<HumidityType_t> humidityCharacteristic;
|
||||
ReadOnlyGattCharacteristic<PressureType_t> pressureCharacteristic;
|
||||
};
|
||||
|
||||
#endif // BLE_FEATURE_GATT_SERVER
|
||||
|
||||
#endif /* #ifndef __BLE_ENVIRONMENTAL_SERVICE_H__*/
|
|
@ -1,155 +0,0 @@
|
|||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2020 ARM Limited
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef __BLE_HEALTH_THERMOMETER_SERVICE_H__
|
||||
#define __BLE_HEALTH_THERMOMETER_SERVICE_H__
|
||||
|
||||
#include "ble/BLE.h"
|
||||
#include "ble/Gap.h"
|
||||
#include "ble/GattServer.h"
|
||||
|
||||
#if BLE_FEATURE_GATT_SERVER
|
||||
|
||||
/**
|
||||
* @class HealthThermometerService
|
||||
* @brief BLE Health Thermometer Service. This service provides the location of the thermometer and the temperature.
|
||||
* Service: https://developer.bluetooth.org/gatt/profiles/Pages/ProfileViewer.aspx?u=org.bluetooth.profile.health_thermometer.xml
|
||||
* Temperature Measurement: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature_measurement.xml
|
||||
* Temperature Type: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature_type.xml
|
||||
*/
|
||||
class HealthThermometerService {
|
||||
public:
|
||||
/**
|
||||
* @brief Location of sensor on the body.
|
||||
*/
|
||||
enum SensorLocation_t {
|
||||
LOCATION_ARMPIT = 1, /*!< Armpit. */
|
||||
LOCATION_BODY, /*!< Body. */
|
||||
LOCATION_EAR, /*!< Ear. */
|
||||
LOCATION_FINGER, /*!< Finger. */
|
||||
LOCATION_GI_TRACT, /*!< GI tract */
|
||||
LOCATION_MOUTH, /*!< Mouth. */
|
||||
LOCATION_RECTUM, /*!< Rectum. */
|
||||
LOCATION_TOE, /*!< Toe. */
|
||||
LOCATION_EAR_DRUM, /*!< Eardrum. */
|
||||
};
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief Add the Health Thermometer Service to an existing BLE object, initialize with temperature and location.
|
||||
* @param[in] _ble Reference to the BLE device.
|
||||
* @param[in] initialTemp Initial value in celsius.
|
||||
* @param[in] _location
|
||||
*/
|
||||
HealthThermometerService(BLE &_ble, float initialTemp, uint8_t _location) :
|
||||
ble(_ble),
|
||||
valueBytes(initialTemp),
|
||||
tempMeasurement(GattCharacteristic::UUID_TEMPERATURE_MEASUREMENT_CHAR, (TemperatureValueBytes *)valueBytes.getPointer(), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE),
|
||||
tempLocation(GattCharacteristic::UUID_TEMPERATURE_TYPE_CHAR, &_location) {
|
||||
|
||||
GattCharacteristic *hrmChars[] = {&tempMeasurement, &tempLocation, };
|
||||
GattService hrmService(GattService::UUID_HEALTH_THERMOMETER_SERVICE, hrmChars, sizeof(hrmChars) / sizeof(hrmChars[0]));
|
||||
|
||||
ble.gattServer().addService(hrmService);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Update the temperature being broadcast.
|
||||
*
|
||||
* @param[in] temperature
|
||||
* Floating point value of the temperature.
|
||||
*
|
||||
*/
|
||||
void updateTemperature(float temperature) {
|
||||
valueBytes.updateTemperature(temperature);
|
||||
ble.gattServer().write(tempMeasurement.getValueHandle(), valueBytes.getPointer(), sizeof(TemperatureValueBytes));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Update the location.
|
||||
* @param loc
|
||||
* New location value.
|
||||
*/
|
||||
void updateLocation(SensorLocation_t loc) {
|
||||
ble.gattServer().write(tempLocation.getValueHandle(), reinterpret_cast<uint8_t *>(&loc), sizeof(uint8_t));
|
||||
}
|
||||
|
||||
private:
|
||||
/* Private internal representation for the bytes used to work with the vaulue of the temperature characteristic. */
|
||||
struct TemperatureValueBytes {
|
||||
static const unsigned OFFSET_OF_FLAGS = 0;
|
||||
static const unsigned OFFSET_OF_VALUE = OFFSET_OF_FLAGS + sizeof(uint8_t);
|
||||
static const unsigned SIZEOF_VALUE_BYTES = sizeof(uint8_t) + sizeof(float);
|
||||
|
||||
static const unsigned TEMPERATURE_UNITS_FLAG_POS = 0;
|
||||
static const unsigned TIMESTAMP_FLAG_POS = 1;
|
||||
static const unsigned TEMPERATURE_TYPE_FLAG_POS = 2;
|
||||
|
||||
static const uint8_t TEMPERATURE_UNITS_CELSIUS = 0;
|
||||
static const uint8_t TEMPERATURE_UNITS_FAHRENHEIT = 1;
|
||||
|
||||
TemperatureValueBytes(float initialTemperature) : bytes() {
|
||||
/* Assumption: temperature values are expressed in celsius */
|
||||
bytes[OFFSET_OF_FLAGS] = (TEMPERATURE_UNITS_CELSIUS << TEMPERATURE_UNITS_FLAG_POS) |
|
||||
(false << TIMESTAMP_FLAG_POS) |
|
||||
(false << TEMPERATURE_TYPE_FLAG_POS);
|
||||
updateTemperature(initialTemperature);
|
||||
}
|
||||
|
||||
void updateTemperature(float temp) {
|
||||
uint32_t temp_ieee11073 = quick_ieee11073_from_float(temp);
|
||||
memcpy(&bytes[OFFSET_OF_VALUE], &temp_ieee11073, sizeof(float));
|
||||
}
|
||||
|
||||
uint8_t *getPointer() {
|
||||
return bytes;
|
||||
}
|
||||
|
||||
const uint8_t *getPointer() const {
|
||||
return bytes;
|
||||
}
|
||||
|
||||
private:
|
||||
/**
|
||||
* @brief A very quick conversion between a float temperature and 11073-20601 FLOAT-Type.
|
||||
* @param temperature The temperature as a float.
|
||||
* @return The temperature in 11073-20601 FLOAT-Type format.
|
||||
*/
|
||||
static uint32_t quick_ieee11073_from_float(float temperature) {
|
||||
uint8_t exponent = 0xFE; //Exponent is -2
|
||||
uint32_t mantissa = (uint32_t)(temperature * 100);
|
||||
|
||||
return (((uint32_t)exponent) << 24) | mantissa;
|
||||
}
|
||||
|
||||
private:
|
||||
/* First byte: 8-bit flags. Second field is a float holding the temperature value. */
|
||||
/* See https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature_measurement.xml */
|
||||
uint8_t bytes[SIZEOF_VALUE_BYTES];
|
||||
};
|
||||
|
||||
protected:
|
||||
BLE &ble;
|
||||
TemperatureValueBytes valueBytes;
|
||||
ReadOnlyGattCharacteristic<TemperatureValueBytes> tempMeasurement;
|
||||
ReadOnlyGattCharacteristic<uint8_t> tempLocation;
|
||||
};
|
||||
|
||||
#endif // BLE_FEATURE_GATT_SERVER
|
||||
|
||||
#endif /* #ifndef __BLE_HEALTH_THERMOMETER_SERVICE_H__*/
|
|
@ -1,239 +0,0 @@
|
|||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2020 ARM Limited
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef MBED_BLE_HEART_RATE_SERVICE_H__
|
||||
#define MBED_BLE_HEART_RATE_SERVICE_H__
|
||||
|
||||
#include "ble/BLE.h"
|
||||
#include "ble/Gap.h"
|
||||
#include "ble/GattServer.h"
|
||||
|
||||
#if BLE_FEATURE_GATT_SERVER
|
||||
|
||||
/**
|
||||
* BLE Heart Rate Service.
|
||||
*
|
||||
* @par purpose
|
||||
*
|
||||
* Fitness applications use the heart rate service to expose the heart
|
||||
* beat per minute measured by a heart rate sensor.
|
||||
*
|
||||
* Clients can read the intended location of the sensor and the last heart rate
|
||||
* value measured. Additionally, clients can subscribe to server initiated
|
||||
* updates of the heart rate value measured by the sensor. The service delivers
|
||||
* these updates to the subscribed client in a notification packet.
|
||||
*
|
||||
* The subscription mechanism is useful to save power; it avoids unecessary data
|
||||
* traffic between the client and the server, which may be induced by polling the
|
||||
* value of the heart rate measurement characteristic.
|
||||
*
|
||||
* @par usage
|
||||
*
|
||||
* When this class is instantiated, it adds a heart rate service in the GattServer.
|
||||
* The service contains the location of the sensor and the initial value measured
|
||||
* by the sensor.
|
||||
*
|
||||
* Application code can invoke updateHeartRate() when a new heart rate measurement
|
||||
* is acquired; this function updates the value of the heart rate measurement
|
||||
* characteristic and notifies the new value to subscribed clients.
|
||||
*
|
||||
* @note You can find specification of the heart rate service here:
|
||||
* https://www.bluetooth.com/specifications/gatt
|
||||
*
|
||||
* @attention The service does not expose information related to the sensor
|
||||
* contact, the accumulated energy expanded or the interbeat intervals.
|
||||
*
|
||||
* @attention The heart rate profile limits the number of instantiations of the
|
||||
* heart rate services to one.
|
||||
*/
|
||||
class HeartRateService {
|
||||
public:
|
||||
/**
|
||||
* Intended location of the heart rate sensor.
|
||||
*/
|
||||
enum BodySensorLocation {
|
||||
/**
|
||||
* Other location.
|
||||
*/
|
||||
LOCATION_OTHER = 0,
|
||||
|
||||
/**
|
||||
* Chest.
|
||||
*/
|
||||
LOCATION_CHEST = 1,
|
||||
|
||||
/**
|
||||
* Wrist.
|
||||
*/
|
||||
LOCATION_WRIST = 2,
|
||||
|
||||
/**
|
||||
* Finger.
|
||||
*/
|
||||
LOCATION_FINGER,
|
||||
|
||||
/**
|
||||
* Hand.
|
||||
*/
|
||||
LOCATION_HAND,
|
||||
|
||||
/**
|
||||
* Earlobe.
|
||||
*/
|
||||
LOCATION_EAR_LOBE,
|
||||
|
||||
/**
|
||||
* Foot.
|
||||
*/
|
||||
LOCATION_FOOT,
|
||||
};
|
||||
|
||||
public:
|
||||
/**
|
||||
* Construct and initialize a heart rate service.
|
||||
*
|
||||
* The construction process adds a GATT heart rate service in @p _ble
|
||||
* GattServer, sets the value of the heart rate measurement characteristic
|
||||
* to @p hrmCounter and the value of the body sensor location characteristic
|
||||
* to @p location.
|
||||
*
|
||||
* @param[in] _ble BLE device that hosts the heart rate service.
|
||||
* @param[in] hrmCounter Heart beats per minute measured by the heart rate
|
||||
* sensor.
|
||||
* @param[in] location Intended location of the heart rate sensor.
|
||||
*/
|
||||
HeartRateService(BLE &_ble, uint16_t hrmCounter, BodySensorLocation location) :
|
||||
ble(_ble),
|
||||
valueBytes(hrmCounter),
|
||||
hrmRate(
|
||||
GattCharacteristic::UUID_HEART_RATE_MEASUREMENT_CHAR,
|
||||
valueBytes.getPointer(),
|
||||
valueBytes.getNumValueBytes(),
|
||||
HeartRateValueBytes::MAX_VALUE_BYTES,
|
||||
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY
|
||||
),
|
||||
hrmLocation(
|
||||
GattCharacteristic::UUID_BODY_SENSOR_LOCATION_CHAR,
|
||||
reinterpret_cast<uint8_t*>(&location)
|
||||
)
|
||||
{
|
||||
setupService();
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the heart rate that the service exposes.
|
||||
*
|
||||
* The server sends a notification of the new value to clients that have
|
||||
* subscribed to updates of the heart rate measurement characteristic; clients
|
||||
* reading the heart rate measurement characteristic after the update obtain
|
||||
* the updated value.
|
||||
*
|
||||
* @param[in] hrmCounter Heart rate measured in BPM.
|
||||
*
|
||||
* @attention This function must be called in the execution context of the
|
||||
* BLE stack.
|
||||
*/
|
||||
void updateHeartRate(uint16_t hrmCounter) {
|
||||
valueBytes.updateHeartRate(hrmCounter);
|
||||
ble.gattServer().write(
|
||||
hrmRate.getValueHandle(),
|
||||
valueBytes.getPointer(),
|
||||
valueBytes.getNumValueBytes()
|
||||
);
|
||||
}
|
||||
|
||||
protected:
|
||||
/**
|
||||
* Construct and add to the GattServer the heart rate service.
|
||||
*/
|
||||
void setupService() {
|
||||
GattCharacteristic *charTable[] = {
|
||||
&hrmRate,
|
||||
&hrmLocation
|
||||
};
|
||||
GattService hrmService(
|
||||
GattService::UUID_HEART_RATE_SERVICE,
|
||||
charTable,
|
||||
sizeof(charTable) / sizeof(charTable[0])
|
||||
);
|
||||
|
||||
ble.gattServer().addService(hrmService);
|
||||
}
|
||||
|
||||
protected:
|
||||
/*
|
||||
* Heart rate measurement value.
|
||||
*/
|
||||
struct HeartRateValueBytes {
|
||||
/* 1 byte for the Flags, and up to two bytes for heart rate value. */
|
||||
static const unsigned MAX_VALUE_BYTES = 3;
|
||||
static const unsigned FLAGS_BYTE_INDEX = 0;
|
||||
|
||||
static const unsigned VALUE_FORMAT_BITNUM = 0;
|
||||
static const uint8_t VALUE_FORMAT_FLAG = (1 << VALUE_FORMAT_BITNUM);
|
||||
|
||||
HeartRateValueBytes(uint16_t hrmCounter) : valueBytes()
|
||||
{
|
||||
updateHeartRate(hrmCounter);
|
||||
}
|
||||
|
||||
void updateHeartRate(uint16_t hrmCounter)
|
||||
{
|
||||
if (hrmCounter <= 255) {
|
||||
valueBytes[FLAGS_BYTE_INDEX] &= ~VALUE_FORMAT_FLAG;
|
||||
valueBytes[FLAGS_BYTE_INDEX + 1] = hrmCounter;
|
||||
} else {
|
||||
valueBytes[FLAGS_BYTE_INDEX] |= VALUE_FORMAT_FLAG;
|
||||
valueBytes[FLAGS_BYTE_INDEX + 1] = (uint8_t)(hrmCounter & 0xFF);
|
||||
valueBytes[FLAGS_BYTE_INDEX + 2] = (uint8_t)(hrmCounter >> 8);
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t *getPointer()
|
||||
{
|
||||
return valueBytes;
|
||||
}
|
||||
|
||||
const uint8_t *getPointer() const
|
||||
{
|
||||
return valueBytes;
|
||||
}
|
||||
|
||||
unsigned getNumValueBytes() const
|
||||
{
|
||||
if (valueBytes[FLAGS_BYTE_INDEX] & VALUE_FORMAT_FLAG) {
|
||||
return 1 + sizeof(uint16_t);
|
||||
} else {
|
||||
return 1 + sizeof(uint8_t);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
uint8_t valueBytes[MAX_VALUE_BYTES];
|
||||
};
|
||||
|
||||
protected:
|
||||
BLE &ble;
|
||||
HeartRateValueBytes valueBytes;
|
||||
GattCharacteristic hrmRate;
|
||||
ReadOnlyGattCharacteristic<uint8_t> hrmLocation;
|
||||
};
|
||||
|
||||
#endif // BLE_FEATURE_GATT_SERVER
|
||||
|
||||
#endif /* #ifndef MBED_BLE_HEART_RATE_SERVICE_H__*/
|
|
@ -1,114 +0,0 @@
|
|||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2020 ARM Limited
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef __BLE_LINK_LOSS_SERVICE_H__
|
||||
#define __BLE_LINK_LOSS_SERVICE_H__
|
||||
|
||||
#include "ble/BLE.h"
|
||||
#include "ble/Gap.h"
|
||||
#include "ble/GattServer.h"
|
||||
|
||||
#if BLE_FEATURE_GATT_SERVER
|
||||
|
||||
/**
|
||||
* @class LinkLossService
|
||||
* @brief This service defines behavior when a link is lost between two devices.
|
||||
* Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.link_loss.xml
|
||||
* Alertness Level Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.alert_level.xml
|
||||
*/
|
||||
class LinkLossService : public ble::Gap::EventHandler
|
||||
{
|
||||
public:
|
||||
enum AlertLevel_t {
|
||||
NO_ALERT = 0,
|
||||
MILD_ALERT = 1,
|
||||
HIGH_ALERT = 2
|
||||
};
|
||||
|
||||
typedef void (* callback_t)(AlertLevel_t level);
|
||||
|
||||
/**
|
||||
* @param bleIn
|
||||
* BLE object for the underlying controller.
|
||||
* @param callbackIn Callback invoked upon disconnection.
|
||||
* @param levelIn Alert level.
|
||||
*/
|
||||
LinkLossService(BLE &bleIn, callback_t callbackIn, AlertLevel_t levelIn = NO_ALERT) :
|
||||
ble(bleIn),
|
||||
alertLevel(levelIn),
|
||||
callback(callbackIn),
|
||||
alertLevelChar(GattCharacteristic::UUID_ALERT_LEVEL_CHAR, reinterpret_cast<uint8_t *>(&alertLevel)) {
|
||||
static bool serviceAdded = false; /* We should only ever add one LinkLoss service. */
|
||||
if (serviceAdded) {
|
||||
return;
|
||||
}
|
||||
|
||||
GattCharacteristic *charTable[] = {&alertLevelChar};
|
||||
GattService linkLossService(GattService::UUID_LINK_LOSS_SERVICE, charTable, sizeof(charTable) / sizeof(charTable[0]));
|
||||
|
||||
ble.gattServer().addService(linkLossService);
|
||||
serviceAdded = true;
|
||||
|
||||
ble.gap().setEventHandler(this);
|
||||
ble.gattServer().onDataWritten(this, &LinkLossService::onDataWritten);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the callback.
|
||||
*/
|
||||
void setCallback(callback_t newCallback) {
|
||||
callback = newCallback;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update alertness level.
|
||||
*/
|
||||
void setAlertLevel(AlertLevel_t newLevel) {
|
||||
alertLevel = newLevel;
|
||||
}
|
||||
|
||||
protected:
|
||||
/**
|
||||
* This callback allows receiving updates to the AlertLevel characteristic.
|
||||
*
|
||||
* @param[in] params
|
||||
* Information about the characteristic being updated.
|
||||
*/
|
||||
virtual void onDataWritten(const GattWriteCallbackParams *params) {
|
||||
if (params->handle == alertLevelChar.getValueHandle()) {
|
||||
alertLevel = *reinterpret_cast<const AlertLevel_t *>(params->data);
|
||||
}
|
||||
}
|
||||
|
||||
void onDisconnectionComplete(const ble::DisconnectionCompleteEvent &) override {
|
||||
if (alertLevel != NO_ALERT) {
|
||||
callback(alertLevel);
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
BLE &ble;
|
||||
AlertLevel_t alertLevel;
|
||||
callback_t callback;
|
||||
|
||||
ReadWriteGattCharacteristic<uint8_t> alertLevelChar;
|
||||
};
|
||||
|
||||
#endif // BLE_FEATURE_GATT_SERVER
|
||||
|
||||
#endif /* __BLE_LINK_LOSS_SERVICE_H__ */
|
|
@ -0,0 +1,13 @@
|
|||
# Mbed OS BLE services
|
||||
|
||||
BLE services are available in the https://github.com/ARMmbed/mbed-os-experimental-ble-services repository. It's a
|
||||
community effort and contains services created by mbed-os team, our partners and mbed-os users.
|
||||
|
||||
To use the services there add the repository to your project by adding a 'mbed-os-experimental-ble-services.lib' file
|
||||
containing the link to the repo like so:
|
||||
```
|
||||
https://github.com/ARMmbed/mbed-os-experimental-ble-services
|
||||
```
|
||||
|
||||
Each BLE service is an Mbed OS library. To use it with new cmake based mbed-tools you will need to add the library
|
||||
to your link libraries for your application.
|
Loading…
Reference in New Issue