1 /*
2  * SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef _TIME_SCENE_SERVER_H_
8 #define _TIME_SCENE_SERVER_H_
9 
10 #include "server_common.h"
11 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 
16 /**
17  * 1. Mesh defines times based on International Atomic Time (TAI). The base
18  *    representation of times is the number of seconds after 00:00:00 TAI
19  *    on 2000-01-01 (that is, 1999-12-31 T23:59:28 UTC).
20  * 2. UTC: Coordinated Universal Time. For more information, please refer
21  *    to https://time.is/zh/UTC
22  * 3. For the algorithm used for the transfer between TAI and UTC, please
23  *    refer to Mesh Model Spec Section 5.1.1
24  */
25 
26 #define UNKNOWN_TAI_SECONDS         0x0000000000
27 #define UNKNOWN_TAI_ZONE_CHANGE     0x0000000000
28 #define UNKNOWN_TAI_DELTA_CHANGE    0x0000000000
29 #define TAI_UTC_DELTA_MAX_VALUE     0x7FFF
30 #define TAI_SECONDS_LEN             0x05
31 #define TAI_OF_ZONE_CHANGE_LEN      0x05
32 #define TAI_OF_DELTA_CHANGE_LEN     0x05
33 
34 #define INVALID_SCENE_NUMBER        0x0000
35 #define SCENE_NUMBER_LEN            0x02
36 
37 #define SCHEDULE_YEAR_ANY_YEAR      0x64
38 
39 #define SCHEDULE_DAY_ANY_DAY        0x00
40 
41 #define SCHEDULE_HOUR_ANY_HOUR      0x18
42 #define SCHEDULE_HOUR_ONCE_A_DAY    0x19
43 
44 #define SCHEDULE_SEC_ANY_OF_HOUR    0x3C
45 #define SCHEDULE_SEC_EVERY_15_MIN   0x3D
46 #define SCHEDULE_SEC_EVERY_20_MIN   0x3E
47 #define SCHEDULE_SEC_ONCE_AN_HOUR   0x3F
48 
49 #define SCHEDULE_SEC_ANY_OF_MIN     0x3C
50 #define SCHEDULE_SEC_EVERY_15_SEC   0x3D
51 #define SCHEDULE_SEC_EVERY_20_SEC   0x3E
52 #define SCHEDULE_SEC_ONCE_AN_MIN    0x3F
53 
54 #define SCHEDULE_ACT_TURN_OFF       0x00
55 #define SCHEDULE_ACT_TURN_ON        0x01
56 #define SCHEDULE_ACT_SCENE_RECALL   0x02
57 #define SCHEDULE_ACT_NO_ACTION      0x0F
58 
59 #define SCHEDULE_SCENE_NO_SCENE     0x0000
60 
61 #define SCHEDULE_ENTRY_MAX_INDEX    0x0F
62 
63 #define TIME_NONE                   0x00
64 #define TIME_AUTHORITY              0x01
65 #define TIME_RELAY                  0x02
66 #define TIME_CLINET                 0x03
67 
68 #define SCENE_SUCCESS               0x00
69 #define SCENE_REG_FULL              0x01
70 #define SCENE_NOT_FOUND             0x02
71 
72 /**
73  * The Time state represents the present TAI time, the current TAI-UTC Delta
74  * and local time zone offset, and the next change to each of the latter
75  * (e.g., because of a switch from winter to summer time or an announced leap
76  * second). It consists of 10 fields with a total size of 183 bits.
77  */
78 struct bt_mesh_time_state {
79     struct {
80         uint8_t  tai_seconds[5];
81         uint8_t  subsecond;
82         uint8_t  uncertainty;
83         uint8_t  time_zone_offset_curr;
84         uint8_t  time_zone_offset_new;
85         uint8_t  tai_zone_change[5];
86         uint16_t time_authority : 1,
87                  tai_utc_delta_curr : 15;
88         uint16_t tai_utc_delta_new : 15;
89         uint8_t  tai_delta_change[5];
90     } time;
91     uint8_t time_role;
92 };
93 
94 struct bt_mesh_time_srv {
95     struct bt_mesh_model *model;
96     struct bt_mesh_server_rsp_ctrl rsp_ctrl;
97     struct bt_mesh_time_state *state;
98 };
99 
100 struct bt_mesh_time_setup_srv {
101     struct bt_mesh_model *model;
102     struct bt_mesh_server_rsp_ctrl rsp_ctrl;
103     struct bt_mesh_time_state *state;
104 };
105 
106 struct scene_register {
107     uint16_t scene_number;
108     uint8_t  scene_type;   /* Indicate the type of scene value */
109     /**
110      * Scene value may use a union to represent later, the union contains
111      * structures of all the model states which can be stored in a scene.
112      */
113     struct net_buf_simple *scene_value;
114 };
115 
116 /**
117  * Scenes serve as memory banks for storage of states (e.g., a power level
118  * or a light level/color). Values of states of an element can be stored
119  * as a scene and can be recalled later from the scene memory.
120  *
121  * A scene is represented by a Scene Number, which is a 16-bit non-zero,
122  * mesh-wide value. (There can be a maximum of 65535 scenes in a mesh
123  * network.) The meaning of a scene, as well as the state storage container
124  * associated with it, are determined by a model.
125  *
126  * The Scenes state change may start numerous parallel model transitions.
127  * In that case, each individual model handles the transition internally.
128  *
129  * The scene transition is defined as a group of individual model transitions
130  * started by a Scene Recall operation. The scene transition is in progress
131  * when at least one transition from the group of individual model transitions
132  * is in progress.
133  */
134 struct bt_mesh_scenes_state {
135     const uint16_t scene_count;
136     struct scene_register *scenes;
137 
138     /**
139      * The Current Scene state is a 16-bit value that contains either the Scene
140      * Number of the currently active scene or a value of 0x0000 when no scene
141      * is active.
142      *
143      * When a Scene Store operation or a Scene Recall operation completes with
144      * success, the Current Scene state value shall be to the Scene Number used
145      * during that operation.
146      *
147      * When the Current Scene Number is deleted from a Scene Register state as a
148      * result of Scene Delete operation, the Current Scene state shall be set to
149      * 0x0000.
150      *
151      * When any of the element's state that is marked as “Stored with Scene” has
152      * changed not as a result of a Scene Recall operation, the value of the
153      * Current Scene state shall be set to 0x0000.
154      *
155      * When a scene transition is in progress, the value of the Current Scene
156      * state shall be set to 0x0000.
157      */
158     uint16_t current_scene;
159 
160     /**
161      * The Target Scene state is a 16-bit value that contains the target Scene
162      * Number when a scene transition is in progress.
163      *
164      * When the scene transition is in progress and the target Scene Number is
165      * deleted from a Scene Register state as a result of Scene Delete operation,
166      * the Target Scene state shall be set to 0x0000.
167      *
168      * When the scene transition is in progress and a new Scene Number is stored
169      * in the Scene Register as a result of Scene Store operation, the Target
170      * Scene state shall be set to the new Scene Number.
171      *
172      * When the scene transition is not in progress, the value of the Target Scene
173      * state shall be set to 0x0000.
174      */
175     uint16_t target_scene;
176 
177     /* Indicate the status code for the last operation */
178     uint8_t status_code;
179 
180     /* Indicate if scene transition is in progress */
181     bool in_progress;
182 };
183 
184 struct bt_mesh_scene_srv {
185     struct bt_mesh_model *model;
186     struct bt_mesh_server_rsp_ctrl rsp_ctrl;
187     struct bt_mesh_scenes_state *state;
188     struct bt_mesh_last_msg_info last;
189     struct bt_mesh_state_transition transition;
190 };
191 
192 struct bt_mesh_scene_setup_srv {
193     struct bt_mesh_model *model;
194     struct bt_mesh_server_rsp_ctrl rsp_ctrl;
195     struct bt_mesh_scenes_state *state;
196 };
197 
198 struct schedule_register {
199     bool     in_use;
200     uint64_t year : 7,
201              month : 12,
202              day : 5,
203              hour : 5,
204              minute : 6,
205              second : 6,
206              day_of_week : 7,
207              action : 4,
208              trans_time : 8;
209     uint16_t scene_number;
210 };
211 
212 struct bt_mesh_scheduler_state {
213     const uint8_t schedule_count;
214     struct schedule_register *schedules; /* Up to 16 scheduled entries */
215 
216     /**
217      * A recommended implementation of the Scheduler should calculate the
218      * value of the TAI Seconds of the next scheduled event and put it in
219      * a queue of scheduled events sorted by time.
220      *
221      * Every second, the first event in the queue is compared with the value
222      * of the Time state. The first event is executed if it is less than or
223      * equal to the Time state and then removed from the queue. After
224      * execution, the Repeat Flag shall be checked, and the next occurrence
225      * of the scheduled event is calculated and put in the queue.
226      *
227      * One second timeout value, and compare the first event in queue with the
228      * Time state. If it is satisfied, then execute the first event. Also the
229      * Repeat Flag need to be checked, if it is set then the event needs to
230      * be put into the end of queue.
231      *
232      * sys_slist_t event_queue;
233      *
234      * For each event_queue item, it can use the following struct:
235      * struct schedule_event {
236      *     sys_snode_t node;
237      *     uint8_t event_index;
238      * };
239      *
240      * Also we need a "struct k_delayed_work track_timer" which can be used to
241      * track the schedule timer and handle proper scheduled events.
242      */
243 };
244 
245 struct bt_mesh_scheduler_srv {
246     struct bt_mesh_model *model;
247     struct bt_mesh_server_rsp_ctrl rsp_ctrl;
248     struct bt_mesh_scheduler_state *state;
249 };
250 
251 struct bt_mesh_scheduler_setup_srv {
252     struct bt_mesh_model *model;
253     struct bt_mesh_server_rsp_ctrl rsp_ctrl;
254     struct bt_mesh_scheduler_state *state;
255 };
256 
257 typedef union {
258     struct {
259         uint8_t  tai_seconds[5];
260         uint8_t  subsecond;
261         uint8_t  uncertainty;
262         uint16_t time_authority : 1;
263         uint16_t tai_utc_delta_curr : 15;
264         uint8_t  time_zone_offset_curr;
265     } time_set;
266     struct {
267         uint8_t  tai_seconds[5];
268         uint8_t  subsecond;
269         uint8_t  uncertainty;
270         uint16_t time_authority : 1;
271         uint16_t tai_utc_delta_curr : 15;
272         uint8_t  time_zone_offset_curr;
273     } time_status;
274     struct {
275         uint8_t time_zone_offset_new;
276         uint8_t tai_zone_change[5];
277     } time_zone_set;
278     struct {
279         uint16_t tai_utc_delta_new : 15;
280         uint8_t  tai_delta_change[5];
281     } tai_utc_delta_set;
282     struct {
283         uint8_t role;
284     } time_role_set;
285     struct {
286         uint16_t scene_number;
287     } scene_store;
288     struct {
289         uint16_t scene_number;
290     } scene_recall;
291     struct {
292         uint16_t scene_number;
293     } scene_delete;
294     struct {
295         uint64_t index : 4,
296                  year : 7,
297                  month : 12,
298                  day : 5,
299                  hour : 5,
300                  minute : 6,
301                  second : 6,
302                  day_of_week : 7,
303                  action : 4,
304                  trans_time : 8;
305         uint16_t scene_number;
306     } scheduler_act_set;
307 } bt_mesh_time_scene_server_state_change_t;
308 
309 typedef union {
310     struct {
311         uint8_t index;
312     } scheduler_act_get;
313 } bt_mesh_time_scene_server_recv_get_msg_t;
314 
315 typedef union {
316     struct {
317         uint8_t  tai_seconds[5];
318         uint8_t  subsecond;
319         uint8_t  uncertainty;
320         uint16_t time_authority : 1;
321         uint16_t tai_utc_delta : 15;
322         uint8_t  time_zone_offset;
323     } time_set;
324     struct {
325         uint8_t time_zone_offset_new;
326         uint8_t tai_zone_change[5];
327     } time_zone_set;
328     struct {
329         uint16_t tai_utc_delta_new : 15;
330         uint16_t padding : 1;
331         uint8_t  tai_delta_change[5];
332     } tai_utc_delta_set;
333     struct {
334         uint8_t time_role;
335     } time_role_set;
336     struct {
337         uint16_t scene_number;
338     } scene_store;
339     struct {
340         bool     op_en;
341         uint16_t scene_number;
342         uint8_t  tid;
343         uint8_t  trans_time;
344         uint8_t  delay;
345     } scene_recall;
346     struct {
347         uint16_t scene_number;
348     } scene_delete;
349     struct {
350         uint64_t index : 4,
351                  year : 7,
352                  month : 12,
353                  day : 5,
354                  hour : 5,
355                  minute : 6,
356                  second : 6,
357                  day_of_week : 7,
358                  action : 4,
359                  trans_time : 8;
360         uint16_t scene_number;
361     } scheduler_act_set;
362 } bt_mesh_time_scene_server_recv_set_msg_t;
363 
364 typedef union {
365     struct {
366         uint8_t  tai_seconds[5];
367         uint8_t  subsecond;
368         uint8_t  uncertainty;
369         uint16_t time_authority : 1;
370         uint16_t tai_utc_delta : 15;
371         uint8_t  time_zone_offset;
372     } time_status;
373 } bt_mesh_time_scene_server_recv_status_msg_t;
374 
375 void bt_mesh_time_scene_server_lock(void);
376 void bt_mesh_time_scene_server_unlock(void);
377 
378 void scene_publish(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, uint16_t opcode);
379 
380 #ifdef __cplusplus
381 }
382 #endif
383 
384 #endif /* _TIME_SCENE_SERVER_H_ */
385