1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  *
4  * HDF is dual licensed: you can use it either under the terms of
5  * the GPL, or the BSD license, at your option.
6  * See the LICENSE file in the root of this repository for complete details.
7  */
8 
9 #ifndef LIGHT_DRIVER_H
10 #define LIGHT_DRIVER_H
11 
12 #include "device_resource_if.h"
13 #include "gpio_if.h"
14 #include "hdf_base.h"
15 #include "hdf_device_desc.h"
16 #include "hdf_log.h"
17 #include "hdf_workqueue.h"
18 #include "osal_mutex.h"
19 #include "osal_timer.h"
20 
21 #define LIGHT_MAKE_R_BIT    0X00FF0000
22 #define LIGHT_MAKE_G_BIT    0X0000FF00
23 #define LIGHT_MAKE_B_BIT    0X000000FF
24 #define LIGHT_WAIT_TIME     50
25 
26 #define LIGHT_INVALID_GPIO    (-1)
27 
28 #define LIGHT_ID_NUM    4
29 
30 #define NAME_MAX_LEN    16
31 
32 #define CHECK_LIGHT_NULL_PTR_RETURN_VALUE(ptr, ret) do { \
33     if ((ptr) == NULL) { \
34         HDF_LOGE("%s:line %d pointer is null and return ret", __func__, __LINE__); \
35         return (ret); \
36     } \
37 } while (0)
38 
39 #define CHECK_LIGHT_NULL_PTR_RETURN(ptr) do { \
40     if ((ptr) == NULL) { \
41         HDF_LOGE("%s:line %d pointer is null and return", __func__, __LINE__); \
42         return; \
43     } \
44 } while (0)
45 
46 #define CHECK_LIGHT_PARSER_RESULT_RETURN_VALUE(ret, str) do { \
47     if ((ret) != HDF_SUCCESS) { \
48         HDF_LOGE("%s:line %d %s fail, ret = %d!", __func__, __LINE__, str, ret); \
49         return HDF_FAILURE; \
50     } \
51 } while (0)
52 
53 #define CHECK_LIGHT_PARSER_NUM_RETURN_VALUE(ret, num) do { \
54     if ((ret) != HDF_SUCCESS) { \
55         (num) = LIGHT_INVALID_GPIO; \
56     } \
57 } while (0)
58 
59 enum LightIoCmd {
60     LIGHT_IO_CMD_GET_INFO_LIST     = 0,
61     LIGHT_IO_CMD_OPS               = 1,
62     LIGHT_IO_CMD_END,
63 };
64 
65 enum LightOpsCmd {
66     LIGHT_OPS_IO_CMD_ENABLE              = 0,
67     LIGHT_OPS_IO_CMD_DISABLE             = 1,
68     LIGHT_OPS_IO_CMD_ENABLE_MULTI_LIGHTS = 2,
69     LIGHT_OPS_IO_CMD_END,
70 };
71 
72 enum LightState {
73     LIGHT_STATE_START   = 0,
74     LIGHT_STATE_STOP    = 1,
75     LIGHT_STATE_BUTT,
76 };
77 
78 enum LightId {
79     LIGHT_ID_NONE                = 0,
80     LIGHT_ID_BATTERY             = 1,
81     LIGHT_ID_NOTIFICATIONS       = 2,
82     LIGHT_ID_ATTENTION           = 3,
83     LIGHT_ID_BUTT,
84 };
85 
86 enum HdfLightType {
87     HDF_LIGHT_TYPE_SINGLE_COLOR = 1,
88     HDF_LIGHT_TYPE_RGB_COLOR = 2,
89     HDF_LIGHT_TYPE_WRGB_COLOR = 3,
90 };
91 
92 enum LightFlashMode {
93     LIGHT_FLASH_NONE = 0,
94     LIGHT_FLASH_BLINK = 1,
95     LIGHT_FLASH_GRADIENT = 2,
96     LIGHT_FLASH_BUTT = 3,
97 };
98 
99 struct LightFlashEffect {
100     int32_t flashMode;
101     uint32_t onTime;
102     uint32_t offTime;
103 };
104 
105 struct WRGBColor {
106     uint8_t w;
107     uint8_t r;
108     uint8_t g;
109     uint8_t b;
110 };
111 
112 struct RGBColor {
113     uint8_t r;
114     uint8_t g;
115     uint8_t b;
116     uint8_t reserved;
117 };
118 
119 union ColorValue {
120     int32_t singleColor;
121     struct RGBColor rgbColor;
122     struct WRGBColor wrgbColor;
123 };
124 
125 struct LightColor {
126     union ColorValue colorValue;
127 };
128 
129 struct LightEffect {
130     struct LightColor lightColor;
131     struct LightFlashEffect flashEffect;
132 };
133 
134 struct LightInfo {
135     char lightName[NAME_MAX_LEN];
136     uint32_t lightId;
137     uint32_t lightNumber;
138     int32_t lightType;
139 };
140 
141 struct LightDeviceInfo {
142     enum LightState lightState;
143     int32_t busRNum;
144     int32_t busGNum;
145     int32_t busBNum;
146     uint32_t lightBrightness;
147     uint32_t defaultBrightness;
148     uint32_t onTime;
149     uint32_t offTime;
150     struct LightInfo lightInfo;
151 };
152 
153 struct LightDriverData {
154     struct IDeviceIoService ioService;
155     struct HdfDeviceObject *device;
156     HdfWorkQueue workQueue;
157     HdfWork work;
158     OsalTimer timer;
159     struct OsalMutex mutex;
160     uint32_t lightId;
161     uint32_t lightNum;
162     struct LightDeviceInfo *info[LIGHT_ID_BUTT];
163 };
164 
165 typedef int32_t (*LightCmdHandle)(uint32_t lightId, struct HdfSBuf *data, struct HdfSBuf *reply);
166 
167 struct LightCmdHandleList {
168     enum LightOpsCmd cmd;
169     LightCmdHandle func;
170 };
171 
172 #endif /* LIGHT_DRIVER_H */
173