BTLE demo for MAXWSNENV.
Dependencies: BLE_API BMP180 Si7020 mbed MaximBLE
EnvironmentalService.h@2:6f76d6160601, 2016-08-18 (annotated)
- Committer:
- enginerd
- Date:
- Thu Aug 18 21:09:13 2016 +0000
- Revision:
- 2:6f76d6160601
- Parent:
- 0:f71931ae3db1
Updated library versions.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kgills | 0:f71931ae3db1 | 1 | /******************************************************************************* |
kgills | 0:f71931ae3db1 | 2 | * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved. |
kgills | 0:f71931ae3db1 | 3 | * |
kgills | 0:f71931ae3db1 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
kgills | 0:f71931ae3db1 | 5 | * copy of this software and associated documentation files (the "Software"), |
kgills | 0:f71931ae3db1 | 6 | * to deal in the Software without restriction, including without limitation |
kgills | 0:f71931ae3db1 | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
kgills | 0:f71931ae3db1 | 8 | * and/or sell copies of the Software, and to permit persons to whom the |
kgills | 0:f71931ae3db1 | 9 | * Software is furnished to do so, subject to the following conditions: |
kgills | 0:f71931ae3db1 | 10 | * |
kgills | 0:f71931ae3db1 | 11 | * The above copyright notice and this permission notice shall be included |
kgills | 0:f71931ae3db1 | 12 | * in all copies or substantial portions of the Software. |
kgills | 0:f71931ae3db1 | 13 | * |
kgills | 0:f71931ae3db1 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
kgills | 0:f71931ae3db1 | 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
kgills | 0:f71931ae3db1 | 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
kgills | 0:f71931ae3db1 | 17 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
kgills | 0:f71931ae3db1 | 18 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
kgills | 0:f71931ae3db1 | 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
kgills | 0:f71931ae3db1 | 20 | * OTHER DEALINGS IN THE SOFTWARE. |
kgills | 0:f71931ae3db1 | 21 | * |
kgills | 0:f71931ae3db1 | 22 | * Except as contained in this notice, the name of Maxim Integrated |
kgills | 0:f71931ae3db1 | 23 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
kgills | 0:f71931ae3db1 | 24 | * Products, Inc. Branding Policy. |
kgills | 0:f71931ae3db1 | 25 | * |
kgills | 0:f71931ae3db1 | 26 | * The mere transfer of this software does not imply any licenses |
kgills | 0:f71931ae3db1 | 27 | * of trade secrets, proprietary technology, copyrights, patents, |
kgills | 0:f71931ae3db1 | 28 | * trademarks, maskwork rights, or any other form of intellectual |
kgills | 0:f71931ae3db1 | 29 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
kgills | 0:f71931ae3db1 | 30 | * ownership rights. |
kgills | 0:f71931ae3db1 | 31 | ******************************************************************************* |
kgills | 0:f71931ae3db1 | 32 | */ |
kgills | 0:f71931ae3db1 | 33 | |
kgills | 0:f71931ae3db1 | 34 | #ifndef __BLE_ENVIRONMENTAL_SERVICE_H__ |
kgills | 0:f71931ae3db1 | 35 | #define __BLE_ENVIRONMENTAL_SERVICE_H__ |
kgills | 0:f71931ae3db1 | 36 | |
kgills | 0:f71931ae3db1 | 37 | #include <time.h> |
kgills | 0:f71931ae3db1 | 38 | #include "BLE.h" |
kgills | 0:f71931ae3db1 | 39 | #include "Characteristic.h" |
kgills | 0:f71931ae3db1 | 40 | #include "HumidityChar.h" |
kgills | 0:f71931ae3db1 | 41 | #include "PressureChar.h" |
kgills | 0:f71931ae3db1 | 42 | #include "TemperatureChar.h" |
kgills | 0:f71931ae3db1 | 43 | #include "LogCharacteristic.h" |
kgills | 0:f71931ae3db1 | 44 | |
kgills | 0:f71931ae3db1 | 45 | // db8d3576-de54-4b92-8754-8a99e7a18d6a |
kgills | 0:f71931ae3db1 | 46 | static const uint8_t envServiceUUID[] = {0x6a,0x8d,0xa1,0xe7,0x99,0x8a,0x54, |
kgills | 0:f71931ae3db1 | 47 | 0x87,0x92,0x4b,0x54,0xde,0x76,0x35,0x8d,0xdb}; |
kgills | 0:f71931ae3db1 | 48 | |
kgills | 0:f71931ae3db1 | 49 | /** |
kgills | 0:f71931ae3db1 | 50 | * @class EnvironmentalService |
kgills | 0:f71931ae3db1 | 51 | * @brief BLE Environmental sensor data service. |
kgills | 0:f71931ae3db1 | 52 | * @details Custom service for communicating environmental sensor data. |
kgills | 0:f71931ae3db1 | 53 | */ |
kgills | 0:f71931ae3db1 | 54 | class EnvironmentalService |
kgills | 0:f71931ae3db1 | 55 | { |
kgills | 0:f71931ae3db1 | 56 | public: |
kgills | 0:f71931ae3db1 | 57 | |
kgills | 0:f71931ae3db1 | 58 | /** |
kgills | 0:f71931ae3db1 | 59 | * @brief EnvironmentalService constructor. |
kgills | 0:f71931ae3db1 | 60 | * @param ble Reference to BLE device. |
kgills | 0:f71931ae3db1 | 61 | * @param pres_unit Unit for the pressure measurements. |
kgills | 0:f71931ae3db1 | 62 | * @param temp_unit Unit for the temperature measurements. |
kgills | 0:f71931ae3db1 | 63 | */ |
kgills | 0:f71931ae3db1 | 64 | EnvironmentalService(BLEDevice &_ble, PressureChar::pres_unit_t pres_unit, |
kgills | 0:f71931ae3db1 | 65 | TemperatureChar::temp_unit_t temp_unit) : |
kgills | 0:f71931ae3db1 | 66 | ble(_ble), |
kgills | 0:f71931ae3db1 | 67 | humidity(), |
kgills | 0:f71931ae3db1 | 68 | logHumidity(), |
kgills | 0:f71931ae3db1 | 69 | pressure(pres_unit), |
kgills | 0:f71931ae3db1 | 70 | logPressure(), |
kgills | 0:f71931ae3db1 | 71 | temperature(temp_unit), |
kgills | 0:f71931ae3db1 | 72 | logTemperature() |
kgills | 0:f71931ae3db1 | 73 | { |
kgills | 0:f71931ae3db1 | 74 | GattCharacteristic *envChars[] = { humidity.getChar(), |
kgills | 0:f71931ae3db1 | 75 | logHumidity.getChar(), |
kgills | 0:f71931ae3db1 | 76 | pressure.getChar(), |
kgills | 0:f71931ae3db1 | 77 | logPressure.getChar(), |
kgills | 0:f71931ae3db1 | 78 | temperature.getChar(), |
kgills | 0:f71931ae3db1 | 79 | logTemperature.getChar() }; |
kgills | 0:f71931ae3db1 | 80 | |
kgills | 0:f71931ae3db1 | 81 | GattService envService(envServiceUUID, envChars, |
kgills | 0:f71931ae3db1 | 82 | sizeof(envChars) / sizeof(GattCharacteristic *)); |
kgills | 0:f71931ae3db1 | 83 | |
kgills | 0:f71931ae3db1 | 84 | ble.gattServer().addService(envService); |
kgills | 0:f71931ae3db1 | 85 | ble.gattServer().onDataRead(this, &EnvironmentalService::dataReadCallback); |
kgills | 0:f71931ae3db1 | 86 | } |
kgills | 0:f71931ae3db1 | 87 | |
kgills | 0:f71931ae3db1 | 88 | /** |
kgills | 0:f71931ae3db1 | 89 | * @brief Update humidity characteristic. |
kgills | 0:f71931ae3db1 | 90 | * @param value New humidity measurement. |
kgills | 0:f71931ae3db1 | 91 | * @param time Timestamp to send with temperature measurement. |
kgills | 0:f71931ae3db1 | 92 | */ |
kgills | 0:f71931ae3db1 | 93 | void updateHumidity(float value, time_t time = 0) |
kgills | 0:f71931ae3db1 | 94 | { |
kgills | 0:f71931ae3db1 | 95 | bool enabledP; |
kgills | 0:f71931ae3db1 | 96 | |
kgills | 0:f71931ae3db1 | 97 | if (ble.gattServer().areUpdatesEnabled(*humidity.getChar(), &enabledP) == BLE_ERROR_NONE) { |
kgills | 0:f71931ae3db1 | 98 | if (enabledP) { |
kgills | 0:f71931ae3db1 | 99 | // Notifications are enabled. Write the value to the attribute and send the notification |
kgills | 0:f71931ae3db1 | 100 | humidity.update(value, time); |
kgills | 0:f71931ae3db1 | 101 | ble.gattServer().write(humidity.getChar()->getValueHandle(), |
kgills | 0:f71931ae3db1 | 102 | humidity.getBytes(), humidity.getNumBytes()); |
kgills | 0:f71931ae3db1 | 103 | |
kgills | 0:f71931ae3db1 | 104 | /* A log is not kept while notifications are enabled. However, the latest value will |
kgills | 0:f71931ae3db1 | 105 | * be kept in case notifications are disabled before the value was sent. |
kgills | 0:f71931ae3db1 | 106 | */ |
kgills | 0:f71931ae3db1 | 107 | logHumidity.clear(); |
kgills | 0:f71931ae3db1 | 108 | } |
kgills | 0:f71931ae3db1 | 109 | } |
kgills | 0:f71931ae3db1 | 110 | |
kgills | 0:f71931ae3db1 | 111 | // Write the value to the log |
kgills | 0:f71931ae3db1 | 112 | LogCharacteristic::qElement_t element = {value, time}; |
kgills | 0:f71931ae3db1 | 113 | logHumidity.put(element); |
kgills | 0:f71931ae3db1 | 114 | } |
kgills | 0:f71931ae3db1 | 115 | |
kgills | 0:f71931ae3db1 | 116 | /** |
kgills | 0:f71931ae3db1 | 117 | * @brief Update pressure characteristic. |
kgills | 0:f71931ae3db1 | 118 | * @param value New pressure measurement. |
kgills | 0:f71931ae3db1 | 119 | * @param time Timestamp to send with temperature measurement. |
kgills | 0:f71931ae3db1 | 120 | */ |
kgills | 0:f71931ae3db1 | 121 | void updatePressure(float value, time_t time = 0) |
kgills | 0:f71931ae3db1 | 122 | { |
kgills | 0:f71931ae3db1 | 123 | bool enabledP; |
kgills | 0:f71931ae3db1 | 124 | |
kgills | 0:f71931ae3db1 | 125 | if (ble.gattServer().areUpdatesEnabled(*pressure.getChar(), &enabledP) == BLE_ERROR_NONE) { |
kgills | 0:f71931ae3db1 | 126 | if (enabledP) { |
kgills | 0:f71931ae3db1 | 127 | // Notifications are enabled. Write the value to the attribute and send the notification |
kgills | 0:f71931ae3db1 | 128 | pressure.update(value, time); |
kgills | 0:f71931ae3db1 | 129 | ble.gattServer().write(pressure.getChar()->getValueHandle(), |
kgills | 0:f71931ae3db1 | 130 | pressure.getBytes(), pressure.getNumBytes()); |
kgills | 0:f71931ae3db1 | 131 | |
kgills | 0:f71931ae3db1 | 132 | /* A log is not kept while notifications are enabled. However, the latest value will |
kgills | 0:f71931ae3db1 | 133 | * be kept in case notifications are disabled before the value was sent. |
kgills | 0:f71931ae3db1 | 134 | */ |
kgills | 0:f71931ae3db1 | 135 | logPressure.clear(); |
kgills | 0:f71931ae3db1 | 136 | } |
kgills | 0:f71931ae3db1 | 137 | } |
kgills | 0:f71931ae3db1 | 138 | |
kgills | 0:f71931ae3db1 | 139 | // Write the value to the log |
kgills | 0:f71931ae3db1 | 140 | LogCharacteristic::qElement_t element = {value, time}; |
kgills | 0:f71931ae3db1 | 141 | logPressure.put(element); |
kgills | 0:f71931ae3db1 | 142 | } |
kgills | 0:f71931ae3db1 | 143 | |
kgills | 0:f71931ae3db1 | 144 | /** |
kgills | 0:f71931ae3db1 | 145 | * @brief Update temperature characteristic. |
kgills | 0:f71931ae3db1 | 146 | * @param temperature New temperature measurement. |
kgills | 0:f71931ae3db1 | 147 | * @param time Timestamp to send with temperature measurement. |
kgills | 0:f71931ae3db1 | 148 | */ |
kgills | 0:f71931ae3db1 | 149 | void updateTemperature(float value, time_t time = 0) |
kgills | 0:f71931ae3db1 | 150 | { |
kgills | 0:f71931ae3db1 | 151 | bool enabledP; |
kgills | 0:f71931ae3db1 | 152 | |
kgills | 0:f71931ae3db1 | 153 | if (ble.gattServer().areUpdatesEnabled(*temperature.getChar(), &enabledP) == BLE_ERROR_NONE) { |
kgills | 0:f71931ae3db1 | 154 | if (enabledP) { |
kgills | 0:f71931ae3db1 | 155 | // Notifications are enabled. Write the value to the attribute and send the notification |
kgills | 0:f71931ae3db1 | 156 | temperature.update(value, time); |
kgills | 0:f71931ae3db1 | 157 | ble.gattServer().write(temperature.getChar()->getValueHandle(), |
kgills | 0:f71931ae3db1 | 158 | temperature.getBytes(), temperature.getNumBytes()); |
kgills | 0:f71931ae3db1 | 159 | |
kgills | 0:f71931ae3db1 | 160 | /* A log is not kept while notifications are enabled. However, the latest value will |
kgills | 0:f71931ae3db1 | 161 | * be kept in case notifications are disabled before the value was sent. |
kgills | 0:f71931ae3db1 | 162 | */ |
kgills | 0:f71931ae3db1 | 163 | logTemperature.clear(); |
kgills | 0:f71931ae3db1 | 164 | } |
kgills | 0:f71931ae3db1 | 165 | } |
kgills | 0:f71931ae3db1 | 166 | |
kgills | 0:f71931ae3db1 | 167 | // Write the value to the log |
kgills | 0:f71931ae3db1 | 168 | LogCharacteristic::qElement_t element = {value, time}; |
kgills | 0:f71931ae3db1 | 169 | logTemperature.put(element); |
kgills | 0:f71931ae3db1 | 170 | } |
kgills | 0:f71931ae3db1 | 171 | |
kgills | 0:f71931ae3db1 | 172 | private: |
kgills | 0:f71931ae3db1 | 173 | |
kgills | 0:f71931ae3db1 | 174 | /** |
kgills | 0:f71931ae3db1 | 175 | * @brief Called when a characteristics value is being read |
kgills | 0:f71931ae3db1 | 176 | */ |
kgills | 0:f71931ae3db1 | 177 | void dataReadCallback(const GattReadCallbackParams *eventDataP) |
kgills | 0:f71931ae3db1 | 178 | { |
kgills | 0:f71931ae3db1 | 179 | if (eventDataP->handle == humidity.getChar()->getValueHandle()) { |
kgills | 0:f71931ae3db1 | 180 | if (logHumidity.len() > 0) { |
kgills | 0:f71931ae3db1 | 181 | // Get the next value from the log and update the characteristic value |
kgills | 0:f71931ae3db1 | 182 | LogCharacteristic::qElement_t element; |
kgills | 0:f71931ae3db1 | 183 | element = logHumidity.get(); |
kgills | 0:f71931ae3db1 | 184 | humidity.update(element.value, element.time); |
kgills | 0:f71931ae3db1 | 185 | } |
kgills | 0:f71931ae3db1 | 186 | // The log is empty, leave the last characteristic value in place |
kgills | 0:f71931ae3db1 | 187 | } else if (eventDataP->handle == pressure.getChar()->getValueHandle()) { |
kgills | 0:f71931ae3db1 | 188 | if (logPressure.len() > 0) { |
kgills | 0:f71931ae3db1 | 189 | // Get the next value from the log and update the characteristic value |
kgills | 0:f71931ae3db1 | 190 | LogCharacteristic::qElement_t element; |
kgills | 0:f71931ae3db1 | 191 | element = logPressure.get(); |
kgills | 0:f71931ae3db1 | 192 | pressure.update(element.value, element.time); |
kgills | 0:f71931ae3db1 | 193 | } |
kgills | 0:f71931ae3db1 | 194 | // The log is empty, leave the last characteristic value in place |
kgills | 0:f71931ae3db1 | 195 | } else if (eventDataP->handle == temperature.getChar()->getValueHandle()) { |
kgills | 0:f71931ae3db1 | 196 | if (logTemperature.len() > 0) { |
kgills | 0:f71931ae3db1 | 197 | // Get the next value from the log and update the characteristic value |
kgills | 0:f71931ae3db1 | 198 | LogCharacteristic::qElement_t element; |
kgills | 0:f71931ae3db1 | 199 | element = logTemperature.get(); |
kgills | 0:f71931ae3db1 | 200 | temperature.update(element.value, element.time); |
kgills | 0:f71931ae3db1 | 201 | } |
kgills | 0:f71931ae3db1 | 202 | // The log is empty, leave the last characteristic value in place |
kgills | 0:f71931ae3db1 | 203 | } |
kgills | 0:f71931ae3db1 | 204 | } |
kgills | 0:f71931ae3db1 | 205 | |
kgills | 0:f71931ae3db1 | 206 | private: |
kgills | 0:f71931ae3db1 | 207 | |
kgills | 0:f71931ae3db1 | 208 | BLEDevice &ble; |
kgills | 0:f71931ae3db1 | 209 | HumidityChar humidity; |
kgills | 0:f71931ae3db1 | 210 | LogCharacteristic logHumidity; |
kgills | 0:f71931ae3db1 | 211 | PressureChar pressure; |
kgills | 0:f71931ae3db1 | 212 | LogCharacteristic logPressure; |
kgills | 0:f71931ae3db1 | 213 | TemperatureChar temperature; |
kgills | 0:f71931ae3db1 | 214 | LogCharacteristic logTemperature; |
kgills | 0:f71931ae3db1 | 215 | }; |
kgills | 0:f71931ae3db1 | 216 | |
kgills | 0:f71931ae3db1 | 217 | #endif /* #ifndef __BLE_ENVIRONMENTAL_SERVICE_H__*/ |