Mistake on this page?
Report an issue in GitHub or email us
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 #ifndef __BLE_ENVIRONMENTAL_SERVICE_H__
20 #define __BLE_ENVIRONMENTAL_SERVICE_H__
21 
22 #include "ble/BLE.h"
23 #include "ble/Gap.h"
24 #include "ble/GattServer.h"
25 
26 #if BLE_FEATURE_GATT_SERVER
27 
28 /**
29 * @class EnvironmentalService
30 * @brief BLE Environmental Service. This service provides temperature, humidity and pressure measurement.
31 * Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.environmental_sensing.xml
32 * Temperature: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature.xml
33 * Humidity: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.humidity.xml
34 * Pressure: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.pressure.xml
35 */
37 public:
38  typedef int16_t TemperatureType_t;
39  typedef uint16_t HumidityType_t;
40  typedef uint32_t PressureType_t;
41 
42  /**
43  * @brief EnvironmentalService constructor.
44  * @param _ble Reference to BLE device.
45  */
47  ble(_ble),
48  temperatureCharacteristic(GattCharacteristic::UUID_TEMPERATURE_CHAR, &temperature),
49  humidityCharacteristic(GattCharacteristic::UUID_HUMIDITY_CHAR, &humidity),
50  pressureCharacteristic(GattCharacteristic::UUID_PRESSURE_CHAR, &pressure)
51  {
52  static bool serviceAdded = false; /* We should only ever need to add the information service once. */
53  if (serviceAdded) {
54  return;
55  }
56 
57  GattCharacteristic *charTable[] = { &humidityCharacteristic,
58  &pressureCharacteristic,
59  &temperatureCharacteristic };
60 
61  GattService environmentalService(GattService::UUID_ENVIRONMENTAL_SERVICE, charTable, sizeof(charTable) / sizeof(charTable[0]));
62 
63  ble.gattServer().addService(environmentalService);
64  serviceAdded = true;
65  }
66 
67  /**
68  * @brief Update humidity characteristic.
69  * @param newHumidityVal New humidity measurement.
70  */
71  void updateHumidity(HumidityType_t newHumidityVal)
72  {
73  humidity = (HumidityType_t) (newHumidityVal * 100);
74  ble.gattServer().write(humidityCharacteristic.getValueHandle(), (uint8_t *) &humidity, sizeof(HumidityType_t));
75  }
76 
77  /**
78  * @brief Update pressure characteristic.
79  * @param newPressureVal New pressure measurement.
80  */
81  void updatePressure(PressureType_t newPressureVal)
82  {
83  pressure = (PressureType_t) (newPressureVal * 10);
84  ble.gattServer().write(pressureCharacteristic.getValueHandle(), (uint8_t *) &pressure, sizeof(PressureType_t));
85  }
86 
87  /**
88  * @brief Update temperature characteristic.
89  * @param newTemperatureVal New temperature measurement.
90  */
91  void updateTemperature(float newTemperatureVal)
92  {
93  temperature = (TemperatureType_t) (newTemperatureVal * 100);
94  ble.gattServer().write(temperatureCharacteristic.getValueHandle(), (uint8_t *) &temperature, sizeof(TemperatureType_t));
95  }
96 
97 private:
98  BLE& ble;
99 
100  TemperatureType_t temperature{};
101  HumidityType_t humidity{};
102  PressureType_t pressure{};
103 
104  ReadOnlyGattCharacteristic<TemperatureType_t> temperatureCharacteristic;
105  ReadOnlyGattCharacteristic<HumidityType_t> humidityCharacteristic;
106  ReadOnlyGattCharacteristic<PressureType_t> pressureCharacteristic;
107 };
108 
109 #endif // BLE_FEATURE_GATT_SERVER
110 
111 #endif /* #ifndef __BLE_ENVIRONMENTAL_SERVICE_H__*/
void updateHumidity(HumidityType_t newHumidityVal)
Update humidity characteristic.
UUID of the environmental service.
GattAttribute::Handle_t getValueHandle() const
Get the characteristic&#39;s value attribute handle in the ATT table.
BLE Environmental Service.
void updatePressure(PressureType_t newPressureVal)
Update pressure characteristic.
EnvironmentalService(BLE &_ble)
EnvironmentalService constructor.
Representation of a GattServer characteristic.
Representation of a GattServer service.
void updateTemperature(float newTemperatureVal)
Update temperature characteristic.
Entry namespace for all BLE API definitions.
Abstract away BLE-capable radio transceivers or SOCs.
Definition: BLE.h:137
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.