|
14 | 14 | * limitations under the License.
|
15 | 15 | */
|
16 | 16 |
|
17 |
| -#ifndef __BLE_BATTERY_SERVICE_H__ |
18 |
| -#define __BLE_BATTERY_SERVICE_H__ |
| 17 | +#ifndef MBED_BLE_BATTERY_SERVICE_H__ |
| 18 | +#define MBED_BLE_BATTERY_SERVICE_H__ |
19 | 19 |
|
| 20 | +#include "platform/mbed_assert.h" |
20 | 21 | #include "ble/BLE.h"
|
21 | 22 |
|
22 | 23 | /**
|
23 |
| -* @class BatteryService |
24 |
| -* @brief BLE Battery Service. This service displays the battery level from 0% to 100%, represented as an 8bit number. |
25 |
| -* Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.battery_service.xml |
26 |
| -* Battery Level Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.battery_level.xml |
27 |
| -*/ |
| 24 | + * BLE Battery service. |
| 25 | + * |
| 26 | + * @par purpose |
| 27 | + * |
| 28 | + * The battery service exposes the charge level of the battery of the device. |
| 29 | + * This information is exposed as a percentage from 0% to 100%; a value of 0% |
| 30 | + * represents a fully discharged battery, and a value of 100% represents a |
| 31 | + * fully charged battery. |
| 32 | + * |
| 33 | + * Clients can read the current charge level and subscribe to server initiated |
| 34 | + * updates of the charge level. The server delivers these updates to the subscribed |
| 35 | + * client in a notification packet. |
| 36 | + * |
| 37 | + * The subscription mechanism is useful to save power; it avoids unecessary data |
| 38 | + * traffic between the client and the server, which may be induced by polling the |
| 39 | + * battery level characteristic value. |
| 40 | + * |
| 41 | + * @par usage |
| 42 | + * |
| 43 | + * When this class is instantiated, it adds a battery service in the GattServer. |
| 44 | + * |
| 45 | + * The application code can use the function updateBatteryLevel() to update the |
| 46 | + * charge level that the service exposes and to notify the subscribed client that the |
| 47 | + * value changed. |
| 48 | + * |
| 49 | + * @note You can find specification of the battery service here: |
| 50 | + * https://www.bluetooth.com/specifications/gatt |
| 51 | + * |
| 52 | + * @important Multiple instances of this battery service are not supported. |
| 53 | + */ |
28 | 54 | class BatteryService {
|
29 | 55 | public:
|
30 | 56 | /**
|
31 |
| - * @param[in] _ble |
32 |
| - * BLE object for the underlying controller. |
33 |
| - * @param[in] level |
34 |
| - * 8bit batterly level. Usually used to represent percentage of batterly charge remaining. |
| 57 | + * Instantiate a battery service. |
| 58 | + * |
| 59 | + * The construction of a BatteryService adds a GATT battery service in @p |
| 60 | + * _ble GattServer and sets the initial charge level of the battery to @p |
| 61 | + * level. |
| 62 | + * |
| 63 | + * @param[in] _ble BLE device which will host the battery service. |
| 64 | + * @param[in] level Initial charge level of the battery. It is a percentage |
| 65 | + * where 0% means that the battery is fully discharged and 100% means that |
| 66 | + * the battery is fully charged. |
35 | 67 | */
|
36 | 68 | BatteryService(BLE &_ble, uint8_t level = 100) :
|
37 | 69 | ble(_ble),
|
38 | 70 | batteryLevel(level),
|
39 |
| - batteryLevelCharacteristic(GattCharacteristic::UUID_BATTERY_LEVEL_CHAR, &batteryLevel, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) { |
40 |
| - |
41 |
| - GattCharacteristic *charTable[] = {&batteryLevelCharacteristic}; |
42 |
| - GattService batteryService(GattService::UUID_BATTERY_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
| 71 | + batteryLevelCharacteristic( |
| 72 | + GattCharacteristic::UUID_BATTERY_LEVEL_CHAR, |
| 73 | + &batteryLevel, |
| 74 | + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY |
| 75 | + ) |
| 76 | + { |
| 77 | + MBED_ASSERT(level <= 100); |
| 78 | + GattCharacteristic *charTable[] = { &batteryLevelCharacteristic }; |
| 79 | + GattService batteryService( |
| 80 | + GattService::UUID_BATTERY_SERVICE, |
| 81 | + charTable, |
| 82 | + sizeof(charTable) / sizeof(GattCharacteristic *) |
| 83 | + ); |
43 | 84 |
|
44 | 85 | ble.addService(batteryService);
|
45 | 86 | }
|
46 | 87 |
|
47 | 88 | /**
|
48 |
| - * @brief Update the battery level with a new value. Valid values lie between 0 and 100, |
49 |
| - * anything outside this range will be ignored. |
| 89 | + * Update the battery charge level that the service exposes. |
50 | 90 | *
|
51 |
| - * @param newLevel |
52 |
| - * Update to battery level. |
| 91 | + * The server sends a notification of the new value to clients that have |
| 92 | + * subscribed to the battery level characteristic updates, and clients |
| 93 | + * reading the charge level after the update obtain the updated value. |
| 94 | + * |
| 95 | + * @param newLevel Charge level of the battery. It is a percentage of the |
| 96 | + * remaining charge between 0% and 100%. |
| 97 | + * |
| 98 | + * @important This function must be called in the execution context of the |
| 99 | + * BLE stack. |
53 | 100 | */
|
54 |
| - void updateBatteryLevel(uint8_t newLevel) { |
| 101 | + void updateBatteryLevel(uint8_t newLevel) |
| 102 | + { |
| 103 | + MBED_ASSERT(newLevel <= 100); |
55 | 104 | batteryLevel = newLevel;
|
56 |
| - ble.gattServer().write(batteryLevelCharacteristic.getValueHandle(), &batteryLevel, 1); |
| 105 | + ble.gattServer().write( |
| 106 | + batteryLevelCharacteristic.getValueHandle(), |
| 107 | + &batteryLevel, |
| 108 | + 1 |
| 109 | + ); |
57 | 110 | }
|
58 | 111 |
|
59 | 112 | protected:
|
60 | 113 | /**
|
61 |
| - * A reference to the underlying BLE instance that this object is attached to. |
62 |
| - * The services and characteristics will be registered in this BLE instance. |
| 114 | + * Reference to the underlying BLE instance that this object is attached to. |
| 115 | + * |
| 116 | + * The services and characteristics are registered in the GattServer of |
| 117 | + * this BLE instance. |
63 | 118 | */
|
64 | 119 | BLE &ble;
|
65 | 120 |
|
66 | 121 | /**
|
67 | 122 | * The current battery level represented as an integer from 0% to 100%.
|
68 | 123 | */
|
69 |
| - uint8_t batteryLevel; |
| 124 | + uint8_t batteryLevel; |
| 125 | + |
70 | 126 | /**
|
71 |
| - * A ReadOnlyGattCharacteristic that allows access to the peer device to the |
72 |
| - * batteryLevel value through BLE. |
| 127 | + * The GATT characteristic, which exposes the charge level. |
73 | 128 | */
|
74 | 129 | ReadOnlyGattCharacteristic<uint8_t> batteryLevelCharacteristic;
|
75 | 130 | };
|
76 | 131 |
|
77 |
| -#endif /* #ifndef __BLE_BATTERY_SERVICE_H__*/ |
| 132 | +#endif /* #ifndef MBED_BLE_BATTERY_SERVICE_H__*/ |
0 commit comments