Mbed OS Reference
Loading...
Searching...
No Matches
EnvironmentalService.h
1/* mbed Microcontroller Library
2 * Copyright (c) 2006-2020 ARM Limited
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19/* MBED_DEPRECATED */
20#warning "These services are deprecated and will be removed. Please see services.md for details about replacement services."
21
22#ifndef __BLE_ENVIRONMENTAL_SERVICE_H__
23#define __BLE_ENVIRONMENTAL_SERVICE_H__
24
25#include "ble/BLE.h"
26#include "ble/Gap.h"
27#include "ble/GattServer.h"
28
29#if BLE_FEATURE_GATT_SERVER
30
31/**
32* @class EnvironmentalService
33* @brief BLE Environmental Service. This service provides temperature, humidity and pressure measurement.
34* Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.environmental_sensing.xml
35* Temperature: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature.xml
36* Humidity: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.humidity.xml
37* Pressure: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.pressure.xml
38*/
40public:
41 typedef int16_t TemperatureType_t;
42 typedef uint16_t HumidityType_t;
43 typedef uint32_t PressureType_t;
44
45 /**
46 * @brief EnvironmentalService constructor.
47 * @param _ble Reference to BLE device.
48 */
50 ble(_ble),
51 temperatureCharacteristic(GattCharacteristic::UUID_TEMPERATURE_CHAR, &temperature),
52 humidityCharacteristic(GattCharacteristic::UUID_HUMIDITY_CHAR, &humidity),
53 pressureCharacteristic(GattCharacteristic::UUID_PRESSURE_CHAR, &pressure)
54 {
55 static bool serviceAdded = false; /* We should only ever need to add the information service once. */
56 if (serviceAdded) {
57 return;
58 }
59
60 GattCharacteristic *charTable[] = { &humidityCharacteristic,
61 &pressureCharacteristic,
62 &temperatureCharacteristic };
63
64 GattService environmentalService(GattService::UUID_ENVIRONMENTAL_SERVICE, charTable, sizeof(charTable) / sizeof(charTable[0]));
65
66 ble.gattServer().addService(environmentalService);
67 serviceAdded = true;
68 }
69
70 /**
71 * @brief Update humidity characteristic.
72 * @param newHumidityVal New humidity measurement.
73 */
74 void updateHumidity(HumidityType_t newHumidityVal)
75 {
76 humidity = (HumidityType_t) (newHumidityVal * 100);
77 ble.gattServer().write(humidityCharacteristic.getValueHandle(), (uint8_t *) &humidity, sizeof(HumidityType_t));
78 }
79
80 /**
81 * @brief Update pressure characteristic.
82 * @param newPressureVal New pressure measurement.
83 */
84 void updatePressure(PressureType_t newPressureVal)
85 {
86 pressure = (PressureType_t) (newPressureVal * 10);
87 ble.gattServer().write(pressureCharacteristic.getValueHandle(), (uint8_t *) &pressure, sizeof(PressureType_t));
88 }
89
90 /**
91 * @brief Update temperature characteristic.
92 * @param newTemperatureVal New temperature measurement.
93 */
94 void updateTemperature(float newTemperatureVal)
95 {
96 temperature = (TemperatureType_t) (newTemperatureVal * 100);
97 ble.gattServer().write(temperatureCharacteristic.getValueHandle(), (uint8_t *) &temperature, sizeof(TemperatureType_t));
98 }
99
100private:
101 BLE& ble;
102
103 TemperatureType_t temperature{};
104 HumidityType_t humidity{};
105 PressureType_t pressure{};
106
107 ReadOnlyGattCharacteristic<TemperatureType_t> temperatureCharacteristic;
108 ReadOnlyGattCharacteristic<HumidityType_t> humidityCharacteristic;
109 ReadOnlyGattCharacteristic<PressureType_t> pressureCharacteristic;
110};
111
112#endif // BLE_FEATURE_GATT_SERVER
113
114#endif /* #ifndef __BLE_ENVIRONMENTAL_SERVICE_H__*/
BLE Environmental Service.
void updateTemperature(float newTemperatureVal)
Update temperature characteristic.
void updatePressure(PressureType_t newPressureVal)
Update pressure characteristic.
void updateHumidity(HumidityType_t newHumidityVal)
Update humidity characteristic.
EnvironmentalService(BLE &_ble)
EnvironmentalService constructor.
Representation of a GattServer characteristic.
GattAttribute::Handle_t getValueHandle() const
Get the characteristic's value attribute handle in the ATT table.
Representation of a GattServer service.
@ UUID_ENVIRONMENTAL_SERVICE
UUID of the environmental service.
Helper class that represents a read only GattCharacteristic.
Abstract away BLE-capable radio transceivers or SOCs.
Definition: BLE.h:137
Entry namespace for all BLE API definitions.