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 _LIGHTING_SERVER_H_
10 #define _LIGHTING_SERVER_H_
11 
12 #include "server_common.h"
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 struct bt_mesh_light_lightness_state {
19     uint16_t lightness_linear;
20     uint16_t target_lightness_linear;
21 
22     uint16_t lightness_actual;
23     uint16_t target_lightness_actual;
24 
25     uint16_t lightness_last;
26     uint16_t lightness_default;
27 
28     uint8_t  status_code;
29     uint16_t lightness_range_min;
30     uint16_t lightness_range_max;
31 };
32 
33 struct bt_mesh_light_lightness_srv {
34     struct bt_mesh_model *model;
35     struct bt_mesh_server_rsp_ctrl rsp_ctrl;
36     struct bt_mesh_light_lightness_state *state;
37     struct bt_mesh_last_msg_info last;
38     struct bt_mesh_state_transition actual_transition;
39     struct bt_mesh_state_transition linear_transition;
40     int32_t tt_delta_lightness_actual;
41     int32_t tt_delta_lightness_linear;
42 };
43 
44 struct bt_mesh_light_lightness_setup_srv {
45     struct bt_mesh_model *model;
46     struct bt_mesh_server_rsp_ctrl rsp_ctrl;
47     struct bt_mesh_light_lightness_state *state;
48 };
49 
50 struct bt_mesh_light_ctl_state {
51     uint16_t lightness;
52     uint16_t target_lightness;
53 
54     uint16_t temperature;
55     uint16_t target_temperature;
56 
57     int16_t  delta_uv;
58     int16_t  target_delta_uv;
59 
60     uint8_t  status_code;
61     uint16_t temperature_range_min;
62     uint16_t temperature_range_max;
63 
64     uint16_t lightness_default;
65     uint16_t temperature_default;
66     int16_t  delta_uv_default;
67 };
68 
69 struct bt_mesh_light_ctl_srv {
70     struct bt_mesh_model *model;
71     struct bt_mesh_server_rsp_ctrl rsp_ctrl;
72     struct bt_mesh_light_ctl_state *state;
73     struct bt_mesh_last_msg_info last;
74     struct bt_mesh_state_transition transition;
75     int32_t tt_delta_lightness;
76     int32_t tt_delta_temperature;
77     int32_t tt_delta_delta_uv;
78 };
79 
80 struct bt_mesh_light_ctl_setup_srv {
81     struct bt_mesh_model *model;
82     struct bt_mesh_server_rsp_ctrl rsp_ctrl;
83     struct bt_mesh_light_ctl_state *state;
84 };
85 
86 struct bt_mesh_light_ctl_temp_srv {
87     struct bt_mesh_model *model;
88     struct bt_mesh_server_rsp_ctrl rsp_ctrl;
89     struct bt_mesh_light_ctl_state *state;
90     struct bt_mesh_last_msg_info last;
91     struct bt_mesh_state_transition transition;
92     int32_t tt_delta_temperature;
93     int32_t tt_delta_delta_uv;
94 };
95 
96 struct bt_mesh_light_hsl_state {
97     uint16_t lightness;
98     uint16_t target_lightness;
99 
100     uint16_t hue;
101     uint16_t target_hue;
102 
103     uint16_t saturation;
104     uint16_t target_saturation;
105 
106     uint16_t lightness_default;
107     uint16_t hue_default;
108     uint16_t saturation_default;
109 
110     uint8_t  status_code;
111     uint16_t hue_range_min;
112     uint16_t hue_range_max;
113     uint16_t saturation_range_min;
114     uint16_t saturation_range_max;
115 };
116 
117 struct bt_mesh_light_hsl_srv {
118     struct bt_mesh_model *model;
119     struct bt_mesh_server_rsp_ctrl rsp_ctrl;
120     struct bt_mesh_light_hsl_state *state;
121     struct bt_mesh_last_msg_info last;
122     struct bt_mesh_state_transition transition;
123     int32_t tt_delta_lightness;
124     int32_t tt_delta_hue;
125     int32_t tt_delta_saturation;
126 };
127 
128 struct bt_mesh_light_hsl_setup_srv {
129     struct bt_mesh_model *model;
130     struct bt_mesh_server_rsp_ctrl rsp_ctrl;
131     struct bt_mesh_light_hsl_state *state;
132 };
133 
134 struct bt_mesh_light_hsl_hue_srv {
135     struct bt_mesh_model *model;
136     struct bt_mesh_server_rsp_ctrl rsp_ctrl;
137     struct bt_mesh_light_hsl_state *state;
138     struct bt_mesh_last_msg_info last;
139     struct bt_mesh_state_transition transition;
140     int32_t tt_delta_hue;
141 };
142 
143 struct bt_mesh_light_hsl_sat_srv {
144     struct bt_mesh_model *model;
145     struct bt_mesh_server_rsp_ctrl rsp_ctrl;
146     struct bt_mesh_light_hsl_state *state;
147     struct bt_mesh_last_msg_info last;
148     struct bt_mesh_state_transition transition;
149     int32_t tt_delta_saturation;
150 };
151 
152 struct bt_mesh_light_xyl_state {
153     uint16_t lightness;
154     uint16_t target_lightness;
155 
156     uint16_t x;
157     uint16_t target_x;
158 
159     uint16_t y;
160     uint16_t target_y;
161 
162     uint16_t lightness_default;
163     uint16_t x_default;
164     uint16_t y_default;
165 
166     uint8_t  status_code;
167     uint16_t x_range_min;
168     uint16_t x_range_max;
169     uint16_t y_range_min;
170     uint16_t y_range_max;
171 };
172 
173 struct bt_mesh_light_xyl_srv {
174     struct bt_mesh_model *model;
175     struct bt_mesh_server_rsp_ctrl rsp_ctrl;
176     struct bt_mesh_light_xyl_state *state;
177     struct bt_mesh_last_msg_info last;
178     struct bt_mesh_state_transition transition;
179     int32_t tt_delta_lightness;
180     int32_t tt_delta_x;
181     int32_t tt_delta_y;
182 };
183 
184 struct bt_mesh_light_xyl_setup_srv {
185     struct bt_mesh_model *model;
186     struct bt_mesh_server_rsp_ctrl rsp_ctrl;
187     struct bt_mesh_light_xyl_state *state;
188 };
189 
190 struct bt_mesh_light_lc_state {
191     uint32_t mode : 1,              /* default 0 */
192              occupancy_mode : 1,    /* default 1 */
193              light_onoff : 1,
194              target_light_onoff : 1,
195              occupancy : 1,
196              ambient_luxlevel : 24; /* 0x000000 ~ 0xFFFFFF */
197 
198     uint16_t linear_output;         /* 0x0000 ~ 0xFFFF */
199 };
200 
201 struct bt_mesh_light_lc_property_state {
202     uint32_t time_occupancy_delay;         /* 0x003A */
203     uint32_t time_fade_on;                 /* 0x0037 */
204     uint32_t time_run_on;                  /* 0x003C */
205     uint32_t time_fade;                    /* 0x0036 */
206     uint32_t time_prolong;                 /* 0x003B */
207     uint32_t time_fade_standby_auto;       /* 0x0038 */
208     uint32_t time_fade_standby_manual;     /* 0x0039 */
209 
210     uint16_t lightness_on;                 /* 0x002E */
211     uint16_t lightness_prolong;            /* 0x002F */
212     uint16_t lightness_standby;            /* 0x0030 */
213 
214     uint16_t ambient_luxlevel_on;          /* 0x002B, 0x0000 ~ 0xFFFF */
215     uint16_t ambient_luxlevel_prolong;     /* 0x002C, 0x0000 ~ 0xFFFF */
216     uint16_t ambient_luxlevel_standby;     /* 0x002D, 0x0000 ~ 0xFFFF */
217 
218     float    regulator_kiu;                /* 0x0033, 0.0 ~ 1000.0, default 250.0 */
219     float    regulator_kid;                /* 0x0032, 0.0 ~ 1000.0, default 25.0 */
220     float    regulator_kpu;                /* 0x0035, 0.0 ~ 1000.0, default 80.0 */
221     float    regulator_kpd;                /* 0x0034, 0.0 ~ 1000.0, default 80.0 */
222     int8_t   regulator_accuracy;           /* 0x0031, 0.0 ~ 100.0, default 2.0 */
223 
224     uint32_t set_occupancy_to_1_delay;
225 };
226 
227 typedef enum {
228     LC_OFF,
229     LC_STANDBY,
230     LC_FADE_ON,
231     LC_RUN,
232     LC_FADE,
233     LC_PROLONG,
234     LC_FADE_STANDBY_AUTO,
235     LC_FADE_STANDBY_MANUAL,
236 } bt_mesh_lc_state;
237 
238 struct bt_mesh_light_lc_state_machine {
239     struct {
240         uint8_t fade_on;
241         uint8_t fade;
242         uint8_t fade_standby_auto;
243         uint8_t fade_standby_manual;
244     } trans_time;
245     bt_mesh_lc_state state;
246     struct k_delayed_work timer;
247 };
248 
249 struct bt_mesh_light_control {
250     struct bt_mesh_light_lc_state          state;
251     struct bt_mesh_light_lc_property_state prop_state;
252     struct bt_mesh_light_lc_state_machine  state_machine;
253 };
254 
255 struct bt_mesh_light_lc_srv {
256     struct bt_mesh_model *model;
257     struct bt_mesh_server_rsp_ctrl rsp_ctrl;
258     struct bt_mesh_light_control *lc;
259     struct bt_mesh_last_msg_info last;
260     struct bt_mesh_state_transition transition;
261 };
262 
263 struct bt_mesh_light_lc_setup_srv {
264     struct bt_mesh_model *model;
265     struct bt_mesh_server_rsp_ctrl rsp_ctrl;
266     struct bt_mesh_light_control *lc;
267 };
268 
269 typedef union {
270     struct {
271         uint16_t lightness;
272     } lightness_set;
273     struct {
274         uint16_t lightness;
275     } lightness_linear_set;
276     struct {
277         uint16_t lightness;
278     } lightness_default_set;
279     struct {
280         uint16_t range_min;
281         uint16_t range_max;
282     } lightness_range_set;
283     struct {
284         uint16_t lightness;
285         uint16_t temperature;
286         int16_t delta_uv;
287     } ctl_set;
288     struct {
289         uint16_t temperature;
290         int16_t delta_uv;
291     } ctl_temp_set;
292     struct {
293         uint16_t range_min;
294         uint16_t range_max;
295     } ctl_temp_range_set;
296     struct {
297         uint16_t lightness;
298         uint16_t temperature;
299         int16_t  delta_uv;
300     } ctl_default_set;
301     struct {
302         uint16_t lightness;
303         uint16_t hue;
304         uint16_t saturation;
305     } hsl_set;
306     struct {
307         uint16_t hue;
308     } hsl_hue_set;
309     struct {
310         uint16_t saturation;
311     } hsl_saturation_set;
312     struct {
313         uint16_t lightness;
314         uint16_t hue;
315         uint16_t saturation;
316     } hsl_default_set;
317     struct {
318         uint16_t hue_range_min;
319         uint16_t hue_range_max;
320         uint16_t sat_range_min;
321         uint16_t sat_range_max;
322     } hsl_range_set;
323     struct {
324         uint16_t lightness;
325         uint16_t x;
326         uint16_t y;
327     } xyl_set;
328     struct {
329         uint16_t lightness;
330         uint16_t x;
331         uint16_t y;
332     } xyl_default_set;
333     struct {
334         uint16_t x_range_min;
335         uint16_t x_range_max;
336         uint16_t y_range_min;
337         uint16_t y_range_max;
338     } xyl_range_set;
339     struct {
340         uint8_t mode;
341     } lc_mode_set;
342     struct {
343         uint8_t mode;
344     } lc_om_set;
345     struct {
346         uint8_t onoff;
347     } lc_light_onoff_set;
348     struct {
349         uint16_t id;
350         struct net_buf_simple *value;
351     } lc_property_set;
352     struct {
353         uint16_t property_id;
354         union {
355             uint8_t  occupancy;
356             uint32_t set_occupancy_to_1_delay;
357             uint32_t ambient_luxlevel;
358         } state;
359     } sensor_status;
360 } bt_mesh_light_server_state_change_t;
361 
362 typedef union {
363     struct {
364         uint16_t id;
365     } lc_property_get;
366 } bt_mesh_light_server_recv_get_msg_t;
367 
368 typedef union {
369     struct {
370         bool     op_en;
371         uint16_t lightness;
372         uint8_t  tid;
373         uint8_t  trans_time;
374         uint8_t  delay;
375     } lightness_set;
376     struct {
377         bool     op_en;
378         uint16_t lightness;
379         uint8_t  tid;
380         uint8_t  trans_time;
381         uint8_t  delay;
382     } lightness_linear_set;
383     struct {
384         uint16_t lightness;
385     } lightness_default_set;
386     struct {
387         uint16_t range_min;
388         uint16_t range_max;
389     } lightness_range_set;
390     struct {
391         bool     op_en;
392         uint16_t lightness;
393         uint16_t temperature;
394         int16_t  delta_uv;
395         uint8_t  tid;
396         uint8_t  trans_time;
397         uint8_t  delay;
398     } ctl_set;
399     struct {
400         bool     op_en;
401         uint16_t temperature;
402         int16_t  delta_uv;
403         uint8_t  tid;
404         uint8_t  trans_time;
405         uint8_t  delay;
406     } ctl_temp_set;
407     struct {
408         uint16_t range_min;
409         uint16_t range_max;
410     } ctl_temp_range_set;
411     struct {
412         uint16_t lightness;
413         uint16_t temperature;
414         int16_t  delta_uv;
415     } ctl_default_set;
416     struct {
417         bool     op_en;
418         uint16_t lightness;
419         uint16_t hue;
420         uint16_t saturation;
421         uint8_t  tid;
422         uint8_t  trans_time;
423         uint8_t  delay;
424     } hsl_set;
425     struct {
426         bool     op_en;
427         uint16_t hue;
428         uint8_t  tid;
429         uint8_t  trans_time;
430         uint8_t  delay;
431     } hsl_hue_set;
432     struct {
433         bool     op_en;
434         uint16_t saturation;
435         uint8_t  tid;
436         uint8_t  trans_time;
437         uint8_t  delay;
438     } hsl_saturation_set;
439     struct {
440         uint16_t lightness;
441         uint16_t hue;
442         uint16_t saturation;
443     } hsl_default_set;
444     struct {
445         uint16_t hue_range_min;
446         uint16_t hue_range_max;
447         uint16_t sat_range_min;
448         uint16_t sat_range_max;
449     } hsl_range_set;
450     struct {
451         bool     op_en;
452         uint16_t lightness;
453         uint16_t x;
454         uint16_t y;
455         uint8_t  tid;
456         uint8_t  trans_time;
457         uint8_t  delay;
458     } xyl_set;
459     struct {
460         uint16_t lightness;
461         uint16_t x;
462         uint16_t y;
463     } xyl_default_set;
464     struct {
465         uint16_t x_range_min;
466         uint16_t x_range_max;
467         uint16_t y_range_min;
468         uint16_t y_range_max;
469     } xyl_range_set;
470     struct {
471         uint8_t mode;
472     } lc_mode_set;
473     struct {
474         uint8_t mode;
475     } lc_om_set;
476     struct {
477         bool    op_en;
478         uint8_t light_onoff;
479         uint8_t tid;
480         uint8_t trans_time;
481         uint8_t delay;
482     } lc_light_onoff_set;
483     struct {
484         uint16_t id;
485         struct net_buf_simple *value;
486     } lc_property_set;
487 } bt_mesh_light_server_recv_set_msg_t;
488 
489 typedef union {
490     struct {
491         struct net_buf_simple *data;
492     } sensor_status;
493 } bt_mesh_light_server_recv_status_msg_t;
494 
495 void bt_mesh_light_server_lock(void);
496 void bt_mesh_light_server_unlock(void);
497 
498 uint8_t *bt_mesh_get_lc_prop_value(struct bt_mesh_model *model, uint16_t prop_id);
499 
500 void light_lightness_publish(struct bt_mesh_model *model, uint16_t opcode);
501 void light_ctl_publish(struct bt_mesh_model *model, uint16_t opcode);
502 void light_hsl_publish(struct bt_mesh_model *model, uint16_t opcode);
503 void light_xyl_publish(struct bt_mesh_model *model, uint16_t opcode);
504 void light_lc_publish(struct bt_mesh_model *model, uint16_t opcode);
505 
506 #ifdef __cplusplus
507 }
508 #endif
509 
510 #endif /* _LIGHTING_SERVER_H_ */
511