1 /*
2  * Copyright (C) 2018-2020 Alibaba Group Holding Limited
3  */
4 
5 #ifndef __GENIE_TIME_H__
6 #define __GENIE_TIME_H__
7 
8 #include <hal/hal.h>
9 
10 #pragma pack(1)
11 typedef struct
12 {
13     uint16_t type;
14     uint8_t para;
15 } vendor_attr_data_t;
16 #pragma pack()
17 
18 #define GENIE_TIME_HW_TEIMER_ID (0)
19 #define GENIE_TIME_HW_TEIMER_PERIOD (1000 * 1000)
20 
21 #define DEF_SYNC_PERIOD 180
22 #define DEF_SYNC_DELAY 10
23 #define DEF_SYNC_DELAY_RETRY 10
24 
25 //no update when init,because Genie will push UTC time when device bootup
26 //#define GENIE_TIME_UPDATE_WHEN_BOOTUP (1)
27 
28 //#define GT_STORE
29 
30 #define MINU 60
31 #define HOUR (60 * MINU)
32 #define DAY (24 * HOUR)
33 
34 #define GENIE_TIME_MAX (40)
35 
36 typedef int (*genie_time_event_func_t)(uint8_t event, uint8_t index, vendor_attr_data_t *data);
37 
38 typedef enum
39 {
40     TIMER_OFF = 0,
41     TIMER_ON = 1,
42     TIMER_INVAILD = 0xf,
43 } vt_state;
44 
45 typedef struct _genie_time_s
46 {
47     sys_snode_t next;
48     uint8_t index;
49     uint8_t state : 4;
50     uint8_t periodic : 1;
51     uint32_t periodic_time;
52     uint8_t schedule;
53     uint32_t unixtime_match;
54     vendor_attr_data_t attr_data;
55 } genie_time_t;
56 
57 typedef struct _unixtime_sync_s
58 {
59     uint16_t period_time;
60     uint8_t retry_delay;
61     uint8_t retry_times;
62 } unixtime_sync_t;
63 
64 typedef struct _genie_time_data_s
65 {
66     uint16_t magic;
67     int8_t timezone;
68     unixtime_sync_t timing_sync_config;
69     genie_time_t timer_data[GENIE_TIME_MAX];
70 } genie_time_data_t;
71 
72 typedef struct _genie_time_timer_s
73 {
74     uint8_t init : 1;
75     uint8_t update : 1;
76     timer_dev_t timer;
77     sys_slist_t timer_list_active;
78     sys_slist_t timer_list_idle;
79     uint32_t unix_time;
80     uint32_t unix_time_sync_match;
81     uint8_t unix_time_sync_retry_times;
82     genie_time_event_func_t genie_time_event_cb;
83     struct k_work work;
84     struct k_sem lock;
85 } genie_time_timer_t;
86 
87 typedef struct _utc_time_s
88 {
89     uint16_t year;   // 2019+
90     uint8_t month;   // 0-11
91     uint8_t day;     // 1-31
92     uint8_t seconds; // 0-59
93     uint8_t minutes; // 0-59
94     uint8_t hour;    // 0-23
95     uint8_t weekday; // 0 means sunday
96 } utc_time_t;
97 
98 typedef enum
99 {
100     GT_OK = 0,
101     GT_E_INIT,
102     GT_E_LOCALTIME_NOTSET,
103     GT_E_INDEX,
104     GT_E_PARAM,
105     GT_E_NORESOURCE,
106     GT_E_OTHER,
107 } genie_time_errno;
108 
109 enum
110 {
111     GT_TIMEOUT = 0,
112     GT_TIMING_SYNC = 1,
113 };
114 
115 int genie_time_init(void);
116 int genie_time_finalize(void);
117 int genie_time_handle_model_mesg(genie_transport_model_param_t *p_msg);
118 
119 #endif
120