1 /* Bluetooth: Mesh Generic OnOff, Generic Level, Lighting & Vendor Models
2  *
3  * SPDX-FileCopyrightText: 2018 Vikrant More
4  * SPDX-FileContributor: 2018-2021 Espressif Systems (Shanghai) CO LTD
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #ifndef _GENERIC_SERVER_H_
10 #define _GENERIC_SERVER_H_
11 
12 #include "server_common.h"
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 struct bt_mesh_gen_onoff_state {
19     uint8_t onoff;
20     uint8_t target_onoff;
21 };
22 
23 struct bt_mesh_gen_onoff_srv {
24     struct bt_mesh_model *model;
25     struct bt_mesh_server_rsp_ctrl rsp_ctrl;
26     struct bt_mesh_gen_onoff_state state;
27     struct bt_mesh_last_msg_info last;
28     struct bt_mesh_state_transition transition;
29 };
30 
31 struct bt_mesh_gen_level_state {
32     int16_t level;
33     int16_t target_level;
34 
35     int16_t last_level;
36     int32_t last_delta;
37 
38     bool move_start;
39     bool positive;
40 };
41 
42 struct bt_mesh_gen_level_srv {
43     struct bt_mesh_model *model;
44     struct bt_mesh_server_rsp_ctrl rsp_ctrl;
45     struct bt_mesh_gen_level_state state;
46     struct bt_mesh_last_msg_info last;
47     struct bt_mesh_state_transition transition;
48     int32_t tt_delta_level;
49 };
50 
51 struct bt_mesh_gen_def_trans_time_state {
52     uint8_t trans_time;
53 };
54 
55 struct bt_mesh_gen_def_trans_time_srv {
56     struct bt_mesh_model *model;
57     struct bt_mesh_server_rsp_ctrl rsp_ctrl;
58     struct bt_mesh_gen_def_trans_time_state state;
59 };
60 
61 struct bt_mesh_gen_onpowerup_state {
62     uint8_t onpowerup;
63 };
64 
65 struct bt_mesh_gen_power_onoff_srv {
66     struct bt_mesh_model *model;
67     struct bt_mesh_server_rsp_ctrl rsp_ctrl;
68     struct bt_mesh_gen_onpowerup_state *state;
69 };
70 
71 struct bt_mesh_gen_power_onoff_setup_srv {
72     struct bt_mesh_model *model;
73     struct bt_mesh_server_rsp_ctrl rsp_ctrl;
74     struct bt_mesh_gen_onpowerup_state *state;
75 };
76 
77 struct bt_mesh_gen_power_level_state {
78     uint16_t power_actual;
79     uint16_t target_power_actual;
80 
81     uint16_t power_last;
82     uint16_t power_default;
83 
84     uint8_t  status_code;
85     uint16_t power_range_min;
86     uint16_t power_range_max;
87 };
88 
89 struct bt_mesh_gen_power_level_srv {
90     struct bt_mesh_model *model;
91     struct bt_mesh_server_rsp_ctrl rsp_ctrl;
92     struct bt_mesh_gen_power_level_state *state;
93     struct bt_mesh_last_msg_info last;
94     struct bt_mesh_state_transition transition;
95     int32_t tt_delta_level;
96 };
97 
98 struct bt_mesh_gen_power_level_setup_srv {
99     struct bt_mesh_model *model;
100     struct bt_mesh_server_rsp_ctrl rsp_ctrl;
101     struct bt_mesh_gen_power_level_state *state;
102 };
103 
104 struct bt_mesh_gen_battery_state {
105     uint32_t battery_level : 8,
106              time_to_discharge : 24;
107     uint32_t time_to_charge : 24,
108              battery_flags : 8;
109 };
110 
111 struct bt_mesh_gen_battery_srv {
112     struct bt_mesh_model *model;
113     struct bt_mesh_server_rsp_ctrl rsp_ctrl;
114     struct bt_mesh_gen_battery_state state;
115 };
116 
117 struct bt_mesh_gen_location_state {
118     int32_t  global_latitude;
119     int32_t  global_longitude;
120     int16_t  global_altitude;
121     int16_t  local_north;
122     int16_t  local_east;
123     int16_t  local_altitude;
124     uint8_t  floor_number;
125     uint16_t uncertainty;
126 };
127 
128 struct bt_mesh_gen_location_srv {
129     struct bt_mesh_model *model;
130     struct bt_mesh_server_rsp_ctrl rsp_ctrl;
131     struct bt_mesh_gen_location_state *state;
132 };
133 
134 struct bt_mesh_gen_location_setup_srv {
135     struct bt_mesh_model *model;
136     struct bt_mesh_server_rsp_ctrl rsp_ctrl;
137     struct bt_mesh_gen_location_state *state;
138 };
139 
140 /**
141  * According to the hierarchy of Generic Property states (Model Spec section 3.1.8),
142  * the Manufacturer Properties and Admin Properties may contain multiple Property
143  * states. User Properties just a collection of which can be accessed.
144  *
145  * property_count: Number of the properties contained in the table
146  * properties:     Table of the properties
147  *
148  * These variables need to be initialized in the application layer, the precise
149  * number of the properties should be set and memories used to store the property
150  * values should be allocated.
151  */
152 
153 enum bt_mesh_gen_user_prop_access {
154     USER_ACCESS_PROHIBIT,
155     USER_ACCESS_READ,
156     USER_ACCESS_WRITE,
157     USER_ACCESS_READ_WRITE,
158 };
159 
160 enum bt_mesh_gen_admin_prop_access {
161     ADMIN_NOT_USER_PROP,
162     ADMIN_ACCESS_READ,
163     ADMIN_ACCESS_WRITE,
164     ADMIN_ACCESS_READ_WRITE,
165 };
166 
167 enum bt_mesh_gen_manu_prop_access {
168     MANU_NOT_USER_PROP,
169     MANU_ACCESS_READ,
170 };
171 
172 struct bt_mesh_generic_property {
173     uint16_t id;
174     uint8_t  user_access;
175     uint8_t  admin_access;
176     uint8_t  manu_access;
177     struct net_buf_simple *val;
178 };
179 
180 struct bt_mesh_gen_user_prop_srv {
181     struct bt_mesh_model *model;
182     struct bt_mesh_server_rsp_ctrl rsp_ctrl;
183     uint8_t property_count;
184     struct bt_mesh_generic_property *properties;
185 };
186 
187 struct bt_mesh_gen_admin_prop_srv {
188     struct bt_mesh_model *model;
189     struct bt_mesh_server_rsp_ctrl rsp_ctrl;
190     uint8_t property_count;
191     struct bt_mesh_generic_property *properties;
192 };
193 
194 struct bt_mesh_gen_manu_prop_srv {
195     struct bt_mesh_model *model;
196     struct bt_mesh_server_rsp_ctrl rsp_ctrl;
197     uint8_t property_count;
198     struct bt_mesh_generic_property *properties;
199 };
200 
201 struct bt_mesh_gen_client_prop_srv {
202     struct bt_mesh_model *model;
203     struct bt_mesh_server_rsp_ctrl rsp_ctrl;
204     uint8_t id_count;
205     uint16_t *property_ids;
206 };
207 
208 typedef union {
209     struct {
210         uint8_t onoff;
211     } gen_onoff_set;
212     struct {
213         int16_t level;
214     } gen_level_set;
215     struct {
216         int16_t level;
217     } gen_delta_set;
218     struct {
219         int16_t level;
220     } gen_move_set;
221     struct {
222         uint8_t trans_time;
223     } gen_def_trans_time_set;
224     struct {
225         uint8_t onpowerup;
226     } gen_onpowerup_set;
227     struct {
228         uint16_t power;
229     } gen_power_level_set;
230     struct {
231         uint16_t power;
232     } gen_power_default_set;
233     struct {
234         uint16_t range_min;
235         uint16_t range_max;
236     } gen_power_range_set;
237     struct {
238         int32_t latitude;
239         int32_t longitude;
240         int16_t altitude;
241     } gen_loc_global_set;
242     struct {
243         int16_t  north;
244         int16_t  east;
245         int16_t  altitude;
246         uint8_t  floor_number;
247         uint16_t uncertainty;
248     } gen_loc_local_set;
249     struct {
250         uint16_t id;
251         struct net_buf_simple *value;
252     } gen_user_prop_set;
253     struct {
254         uint16_t id;
255         uint8_t  access;
256         struct net_buf_simple *value;
257     } gen_admin_prop_set;
258     struct {
259         uint16_t id;
260         uint8_t  access;
261     } gen_manu_prop_set;
262 } bt_mesh_gen_server_state_change_t;
263 
264 typedef union {
265     struct {
266         uint16_t id;
267     } user_property_get;
268     struct {
269         uint16_t id;
270     } admin_property_get;
271     struct {
272         uint16_t id;
273     } manu_property_get;
274     struct {
275         uint16_t id;
276     } client_properties_get;
277 } bt_mesh_gen_server_recv_get_msg_t;
278 
279 typedef union {
280     struct {
281         bool    op_en;
282         uint8_t onoff;
283         uint8_t tid;
284         uint8_t trans_time;
285         uint8_t delay;
286     } onoff_set;
287     struct {
288         bool    op_en;
289         int16_t level;
290         uint8_t tid;
291         uint8_t trans_time;
292         uint8_t delay;
293     } level_set;
294     struct {
295         bool    op_en;
296         int32_t delta_level;
297         uint8_t tid;
298         uint8_t trans_time;
299         uint8_t delay;
300     } delta_set;
301     struct {
302         bool    op_en;
303         int16_t delta_level;
304         uint8_t tid;
305         uint8_t trans_time;
306         uint8_t delay;
307     } move_set;
308     struct {
309         uint8_t trans_time;
310     } def_trans_time_set;
311     struct {
312         uint8_t onpowerup;
313     } onpowerup_set;
314     struct {
315         bool     op_en;
316         uint16_t power;
317         uint8_t  tid;
318         uint8_t  trans_time;
319         uint8_t  delay;
320     } power_level_set;
321     struct {
322         uint16_t power;
323     } power_default_set;
324     struct {
325         uint16_t range_min;
326         uint16_t range_max;
327     } power_range_set;
328     struct {
329         int32_t latitude;
330         int32_t longitude;
331         int16_t altitude;
332     } loc_global_set;
333     struct {
334         int16_t  north;
335         int16_t  east;
336         int16_t  altitude;
337         uint8_t  floor_number;
338         uint16_t uncertainty;
339     } loc_local_set;
340     struct {
341         uint16_t id;
342         struct net_buf_simple *value;
343     } user_property_set;
344     struct {
345         uint16_t id;
346         uint8_t  access;
347         struct net_buf_simple *value;
348     } admin_property_set;
349     struct {
350         uint16_t id;
351         uint8_t  access;
352     } manu_property_set;
353 } bt_mesh_gen_server_recv_set_msg_t;
354 
355 void bt_mesh_generic_server_lock(void);
356 void bt_mesh_generic_server_unlock(void);
357 
358 void gen_onoff_publish(struct bt_mesh_model *model);
359 void gen_level_publish(struct bt_mesh_model *model);
360 void gen_onpowerup_publish(struct bt_mesh_model *model);
361 void gen_power_level_publish(struct bt_mesh_model *model, uint16_t opcode);
362 
363 #ifdef __cplusplus
364 }
365 #endif
366 
367 #endif /* _GENERIC_SERVER_H_ */
368