Skip to content

Commit b76b3f7

Browse files
authored
Merge pull request #5472 from pan-/doc_battery_service
BLE: Update BatteryService documentation.
2 parents bc92e40 + 220652f commit b76b3f7

File tree

1 file changed

+82
-27
lines changed

1 file changed

+82
-27
lines changed

features/FEATURE_BLE/ble/services/BatteryService.h

Lines changed: 82 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,64 +14,119 @@
1414
* limitations under the License.
1515
*/
1616

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__
1919

20+
#include "platform/mbed_assert.h"
2021
#include "ble/BLE.h"
2122

2223
/**
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+
*/
2854
class BatteryService {
2955
public:
3056
/**
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.
3567
*/
3668
BatteryService(BLE &_ble, uint8_t level = 100) :
3769
ble(_ble),
3870
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+
);
4384

4485
ble.addService(batteryService);
4586
}
4687

4788
/**
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.
5090
*
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.
53100
*/
54-
void updateBatteryLevel(uint8_t newLevel) {
101+
void updateBatteryLevel(uint8_t newLevel)
102+
{
103+
MBED_ASSERT(newLevel <= 100);
55104
batteryLevel = newLevel;
56-
ble.gattServer().write(batteryLevelCharacteristic.getValueHandle(), &batteryLevel, 1);
105+
ble.gattServer().write(
106+
batteryLevelCharacteristic.getValueHandle(),
107+
&batteryLevel,
108+
1
109+
);
57110
}
58111

59112
protected:
60113
/**
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.
63118
*/
64119
BLE &ble;
65120

66121
/**
67122
* The current battery level represented as an integer from 0% to 100%.
68123
*/
69-
uint8_t batteryLevel;
124+
uint8_t batteryLevel;
125+
70126
/**
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.
73128
*/
74129
ReadOnlyGattCharacteristic<uint8_t> batteryLevelCharacteristic;
75130
};
76131

77-
#endif /* #ifndef __BLE_BATTERY_SERVICE_H__*/
132+
#endif /* #ifndef MBED_BLE_BATTERY_SERVICE_H__*/

0 commit comments

Comments
 (0)