Add loading HAL module before runnig API
[platform/hal/api/device.git] / src / led.c
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <hal/hal-common.h>
18
19 #include "common.h"
20 #include "hal-led-interface.h"
21
22 static hal_backend_led_funcs *hal_led_funcs = NULL;
23 /*
24 -1 : failed to initialize
25 0  : not initialized
26 1  : succeeded to initialize
27 */
28 static int hal_initialized = 0;
29
30 int hal_device_led_get_backend(void)
31 {
32         int ret;
33
34         if (hal_led_funcs)
35                 return 0;
36
37         ret = hal_common_get_backend(HAL_MODULE_DEVICE_LED, (void **)&hal_led_funcs);
38         if (ret < 0) {
39                  _E("Failed to get led backend");
40                 hal_initialized = -1;
41                 return -EINVAL;
42         }
43
44         hal_initialized = 1;
45         return 0;
46 }
47
48 int hal_device_led_put_backend(void)
49 {
50         if (!hal_led_funcs)
51                 return 0;
52
53         hal_common_put_backend(HAL_MODULE_DEVICE_LED, (void *)hal_led_funcs);
54         hal_led_funcs = NULL;
55         hal_initialized = 0;
56
57         return 0;
58 }
59
60 int hal_device_led_set_state(enum led_device_type type, struct led_state *state)
61 {
62         int ret;
63
64         if (!hal_led_funcs && !hal_initialized) {
65                 if ((ret = hal_device_led_get_backend()) < 0)
66                         return ret;
67         }
68
69         switch (type) {
70         case CAMERA_FRONT:
71                 if (!hal_led_funcs ||
72                         !(hal_led_funcs->camera_front) ||
73                         !(hal_led_funcs->camera_front->set_state))
74                         return -ENODEV;
75                 return hal_led_funcs->camera_front->set_state(type, state);
76
77         case CAMERA_BACK:
78                 if (!hal_led_funcs ||
79                         !(hal_led_funcs->camera_back) ||
80                         !(hal_led_funcs->camera_back->set_state))
81                         return -ENODEV;
82                 return hal_led_funcs->camera_back->set_state(type, state);
83
84         case NOTIFICATION:
85                 if (!hal_led_funcs ||
86                         !(hal_led_funcs->notification) ||
87                         !(hal_led_funcs->notification->set_state))
88                         return -ENODEV;
89                 return hal_led_funcs->notification->set_state(type, state);
90
91         case TOUCH_KEY:
92                 if (!hal_led_funcs ||
93                         !(hal_led_funcs->touch_key) ||
94                         !(hal_led_funcs->touch_key->set_state))
95                         return -ENODEV;
96                 return hal_led_funcs->touch_key->set_state(type, state);
97
98         default:
99                 _E("Invalid led type: %d", type);
100                 return -ENODEV;
101         }
102 }
103
104 int hal_device_led_get_state(enum led_device_type type, struct led_state **state)
105 {
106         int ret;
107
108         if (!hal_led_funcs && !hal_initialized) {
109                 if ((ret = hal_device_led_get_backend()) < 0)
110                         return ret;
111         }
112
113         switch (type) {
114         case CAMERA_FRONT:
115                 if (!hal_led_funcs ||
116                         !(hal_led_funcs->camera_front) ||
117                         !(hal_led_funcs->camera_front->get_state))
118                         return -ENODEV;
119                 return hal_led_funcs->camera_front->get_state(type, state);
120
121         case CAMERA_BACK:
122                 if (!hal_led_funcs ||
123                         !(hal_led_funcs->camera_back) ||
124                         !(hal_led_funcs->camera_back->get_state))
125                         return -ENODEV;
126                 return hal_led_funcs->camera_back->get_state(type, state);
127
128         case NOTIFICATION:
129                 if (!hal_led_funcs ||
130                         !(hal_led_funcs->notification) ||
131                         !(hal_led_funcs->notification->get_state))
132                         return -ENODEV;
133                 return hal_led_funcs->notification->get_state(type, state);
134
135         case TOUCH_KEY:
136                 if (!hal_led_funcs ||
137                         !(hal_led_funcs->touch_key) ||
138                         !(hal_led_funcs->touch_key->get_state))
139                         return -ENODEV;
140                 return hal_led_funcs->touch_key->get_state(type, state);
141
142         default:
143                 _E("Invalid led type: %d", type);
144                 return -ENODEV;
145         }
146 }
147
148 int hal_device_led_get_number(void)
149 {
150         int ret;
151
152         if (!hal_led_funcs && !hal_initialized) {
153                 if ((ret = hal_device_led_get_backend()) < 0)
154                         return ret;
155         }
156
157         if (!hal_led_funcs ||
158                 !(hal_led_funcs->notification) ||
159                 !(hal_led_funcs->notification->get_number))
160                 return -ENODEV;
161
162         return hal_led_funcs->notification->get_number();
163 }
164
165 int hal_device_led_set_number(int number)
166 {
167         int ret;
168
169         if (!hal_led_funcs && !hal_initialized) {
170                 if ((ret = hal_device_led_get_backend()) < 0)
171                         return ret;
172         }
173
174         if (!hal_led_funcs ||
175                 !(hal_led_funcs->notification) ||
176                 !(hal_led_funcs->notification->set_num))
177                 return -ENODEV;
178
179         hal_led_funcs->notification->set_num(number);
180
181         return 0;
182 }
183
184 int hal_device_led_get_max_num(void)
185 {
186         int ret;
187
188         if (!hal_led_funcs && !hal_initialized) {
189                 if ((ret = hal_device_led_get_backend()) < 0)
190                         return ret;
191         }
192
193         if (!hal_led_funcs ||
194                 !(hal_led_funcs->notification) ||
195                 !(hal_led_funcs->notification->get_max_num))
196                 return -ENODEV;
197
198         return hal_led_funcs->notification->get_max_num();
199 }
200
201 int hal_device_keyled_set_state(struct keyled_state *state)
202 {
203         int ret;
204
205         if (!hal_led_funcs && !hal_initialized) {
206                 if ((ret = hal_device_led_get_backend()) < 0)
207                         return ret;
208         }
209
210         if (!hal_led_funcs ||
211                 !(hal_led_funcs->touch_key) ||
212                 !(hal_led_funcs->touch_key->keyled_set_state))
213                 return -ENODEV;
214
215         return hal_led_funcs->touch_key->keyled_set_state(state);
216 }
217
218 int hal_device_keyled_get_state(int *keycode, int *brightness)
219 {
220         int ret;
221
222         if (!hal_led_funcs && !hal_initialized) {
223                 if ((ret = hal_device_led_get_backend()) < 0)
224                         return ret;
225         }
226
227         if (!hal_led_funcs ||
228                 !(hal_led_funcs->touch_key) ||
229                 !(hal_led_funcs->touch_key->keyled_get_state))
230                 return -ENODEV;
231
232         return hal_led_funcs->touch_key->keyled_get_state(keycode, brightness);
233 }
234