Dawn Framework 1.0
Universal data acquisition framework for embedded systems
prph_ess.hxx
1// dawn/include/dawn/proto/nimble/prph_ess.hxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#pragma once
7
8#include <map>
9
10#include "dawn/porting/config.hxx"
11#include "dawn/proto/nimble/iprph.hxx"
12#include "host/ble_gatt.h"
13
14namespace dawn
15{
16// Forward reference
17
18class CProtoNimblePrph;
19
28{
29public:
30 constexpr static uint8_t ESS_CFG0_SIZE_SHIFT = 0;
31 constexpr static uint8_t ESS_CFG0_SIZE_MASK = 0b1111;
32 constexpr static uint8_t ESS_OBJCFG_TYPE_SHIFT = 0;
33 constexpr static uint8_t ESS_OBJCFG_TYPE_MASK = 0b1111;
34 constexpr static uint8_t ESS_EXTCFG_KIND_SHIFT = 0;
35 constexpr static uint8_t ESS_EXTCFG_KIND_MASK = 0xff;
36 constexpr static uint8_t ESS_EXTCFG_SIZE_SHIFT = 8;
37 constexpr static uint8_t ESS_EXTCFG_SIZE_MASK = 0xff;
38 constexpr static uint8_t ESS_DESC_USER_DESCRIPTION = 1 << 0;
39 constexpr static uint8_t ESS_DESC_VALID_RANGE = 1 << 1;
40 constexpr static uint8_t ESS_DESC_MEASUREMENT = 1 << 2;
41 constexpr static uint8_t ESS_DESC_CONFIGURATION = 1 << 3;
42 constexpr static uint8_t ESS_DESC_TRIGGER_SETTING = 1 << 4;
43 constexpr static uint8_t ESS_USER_DESCRIPTION_MAX = 16;
44 constexpr static uint8_t ESS_RAW_DESCRIPTOR_MAX = 16;
45
46 constexpr static uint16_t UUID16_CHR_USER_DESCRIPTION = 0x2901;
47 constexpr static uint16_t UUID16_VALID_RANGE = 0x2906;
48 constexpr static uint16_t UUID16_ES_CONFIGURATION = 0x290b;
49 constexpr static uint16_t UUID16_ES_MEASUREMENT = 0x290c;
50 constexpr static uint16_t UUID16_ES_TRIGGER_SETTING = 0x290d;
51
52 enum
53 {
59 };
60
61 enum
62 {
71 };
72
73 struct
74 {
75 uint32_t cfg;
77 uint32_t extCount;
78 } typedef SProtoNimblePrphIOBindEssObjid;
79
80 struct
81 {
82 uint32_t cfg0;
83 uint32_t cfg1;
84 uint32_t cfg2;
85
86 uint32_t data[];
87 } typedef SProtoNimblePrphIOBindEss;
88
89 CProtoNimblePrphEss(const SObjectCfg::SObjectCfgItem *item, IProtoNimblePrphCb *cb_);
90
91 ~CProtoNimblePrphEss() override;
92
93 int init() override;
94 int deinit() override;
95 int start() override;
96 int stop() override;
97
98 static uint32_t cfgIdIOBindEssCfgObj(uint8_t type)
99 {
100 return ((type & ESS_OBJCFG_TYPE_MASK) << ESS_OBJCFG_TYPE_SHIFT);
101 }
102
103 static uint8_t cfgIdIOBindEssCfgObjTypeGet(uint32_t val)
104 {
105 return (val >> ESS_OBJCFG_TYPE_SHIFT) & ESS_OBJCFG_TYPE_MASK;
106 }
107
108 static uint32_t cfgIdIOBindEssExt(uint8_t kind, uint8_t size)
109 {
110 return ((kind & ESS_EXTCFG_KIND_MASK) << ESS_EXTCFG_KIND_SHIFT) |
111 ((size & ESS_EXTCFG_SIZE_MASK) << ESS_EXTCFG_SIZE_SHIFT);
112 }
113
114 static uint8_t cfgIdIOBindEssExtKindGet(uint32_t val)
115 {
116 return (val >> ESS_EXTCFG_KIND_SHIFT) & ESS_EXTCFG_KIND_MASK;
117 }
118
119 static uint8_t cfgIdIOBindEssExtSizeGet(uint32_t val)
120 {
121 return (val >> ESS_EXTCFG_SIZE_SHIFT) & ESS_EXTCFG_SIZE_MASK;
122 }
123
124 static uint32_t cfgIdIOBindEssCfg0(uint8_t size)
125 {
126 return ((size & ESS_CFG0_SIZE_MASK) << ESS_CFG0_SIZE_SHIFT);
127 }
128
129 static uint8_t cfgIdIOBindEssCfg0SizeGet(uint32_t val)
130 {
131 return (val >> ESS_CFG0_SIZE_SHIFT) & ESS_CFG0_SIZE_MASK;
132 }
133
134 static uint32_t cfgIdIOBindEssCfg1(uint8_t res)
135 {
136 return 0;
137 }
138
139 static uint32_t cfgIdIOBindEssCfg2(uint8_t res)
140 {
141 return 0;
142 }
143
144private:
145 int id;
146 size_t noChar;
147 bool created;
148 struct ble_gatt_svc_def svc;
149 std::map<const SObjectId::ObjectId, uint8_t> ioTypeMap;
150
151 struct SEssMeta
152 {
153 uint32_t desc;
154 uint32_t validMin;
155 uint32_t validMax;
156 SObjectId::ObjectId measurementFlagsObjid;
157 SObjectId::ObjectId samplingFunctionObjid;
158 SObjectId::ObjectId measurementPeriodObjid;
159 SObjectId::ObjectId updateIntervalObjid;
160 SObjectId::ObjectId applicationObjid;
161 SObjectId::ObjectId uncertaintyObjid;
162 SObjectId::ObjectId configurationObjid;
163 SObjectId::ObjectId triggerSettingObjid;
164 char userDescription[ESS_USER_DESCRIPTION_MAX];
165 };
166
167 struct SEssDscField
168 {
169 CIOCommon *io;
170 io_ddata_t *data;
171 };
172
173 struct SEssDscCb
174 {
175 uint8_t kind;
176 uint8_t len;
177 uint8_t data[ESS_RAW_DESCRIPTOR_MAX];
178 SEssDscField field;
179 SEssDscField measurement[6];
180 };
181
182 std::map<const SObjectId::ObjectId, SEssMeta> ioMetaMap;
183
184#ifdef CONFIG_DAWN_IO_NOTIFY
185 static int notifierCb(void *priv, io_ddata_t *data);
186#endif
187
188 template<typename T, size_t WriteBytes = sizeof(T)>
189 static int callback(uint16_t conn_handle,
190 uint16_t attr_handle,
191 struct ble_gatt_access_ctxt *ctxt,
192 void *arg);
193
194 static int descriptorCb(uint16_t conn_handle,
195 uint16_t attr_handle,
196 struct ble_gatt_access_ctxt *ctxt,
197 void *arg);
198
199 int allocESS();
200 int createESS();
201 void deleteESS();
202 void configureDesc(const SObjectCfg::SObjectCfgItem *item);
203 void allocObject(const SProtoNimblePrphIOBindEssObjid &obj, const uint32_t *ext);
204 int configureDescriptors(struct ble_gatt_chr_def *chr, SObjectId::ObjectId objid, uint8_t type);
205};
206} // Namespace dawn
Environmental Sensing Service (ESS) for BLE Peripheral.
Definition prph_ess.hxx:28
int deinit()
Deinitialize service.
int stop()
Stop service.
@ ESS_EXT_TRIGGER_SETTING
Environmental Sensing Trigger Setting descriptor.
Definition prph_ess.hxx:58
@ ESS_EXT_USER_DESCRIPTION
Characteristic User Description descriptor.
Definition prph_ess.hxx:54
@ ESS_EXT_CONFIGURATION
Environmental Sensing Configuration descriptor.
Definition prph_ess.hxx:57
@ ESS_EXT_MEASUREMENT
Environmental Sensing Measurement descriptor.
Definition prph_ess.hxx:56
@ ESS_EXT_VALID_RANGE
Valid Range descriptor.
Definition prph_ess.hxx:55
int start()
Start service.
int init()
Initialize service.
@ PRPH_ESS_TYPE_UVIDX
UV index sensor.
Definition prph_ess.hxx:66
@ PRPH_ESS_TYPE_TEMP
Temperature sensor.
Definition prph_ess.hxx:63
@ PRPH_ESS_TYPE_PRESS
Pressure (barometric) sensor.
Definition prph_ess.hxx:65
@ PRPH_ESS_TYPE_TWINDSPEED
Wind speed sensor.
Definition prph_ess.hxx:67
@ PRPH_ESS_TYPE_HUM
Humidity sensor.
Definition prph_ess.hxx:64
@ PRPH_ESS_TYPE_GAS
Gas resistance sensor (non-standard UUID).
Definition prph_ess.hxx:69
@ PRPH_ESS_TYPE_TWINDDIR
Wind direction sensor.
Definition prph_ess.hxx:68
@ PRPH_ESS_TYPE_LIGHT
Illuminance / light sensor.
Definition prph_ess.hxx:70
Base interface for GATT services exposed by BLE peripheral.
Definition iprph.hxx:467
Out-of-tree user-extension hooks for Dawn.
Definition bindable.hxx:13
uint32_t ObjectId
ObjectID type - single 32-bit value.
Definition objectid.hxx:44
32-bit encoded object identifier (union with bit field).
Definition objectid.hxx:218