928f0ce239f5319d34443787fe72f61f83e1c6bb
[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
18 #include <stdbool.h>
19 #include <hal/hal-common.h>
20
21 #include "common.h"
22 #include "hal-led-interface.h"
23
24 static hal_backend_led_funcs *g_led_funcs = NULL;
25
26 int hal_device_led_get_backend(void)
27 {
28         int ret;
29
30         if (g_led_funcs)
31                 return 0;
32
33         ret = hal_common_get_backend(HAL_MODULE_DEVICE_LED, (void **)&g_led_funcs);
34         if (ret < 0) {
35                  _E("Failed to get led backend");
36                 return -EINVAL;
37         }
38
39         return 0;
40 }
41
42 int hal_device_led_put_backend(void)
43 {
44         if (!g_led_funcs)
45                 return 0;
46
47         hal_common_put_backend(HAL_MODULE_DEVICE_LED, (void *)g_led_funcs);
48         g_led_funcs = NULL;
49
50         return 0;
51 }
52
53 int hal_device_led_set_state(struct led_state *state)
54 {
55         if (!g_led_funcs ||
56             !g_led_funcs->set_state)
57                 return -ENODEV;
58
59         return g_led_funcs->set_state(state);
60 }
61
62 int hal_device_led_get_state(struct led_state **state)
63 {
64         if (!g_led_funcs ||
65             !g_led_funcs->get_state)
66                 return -ENODEV;
67
68         return g_led_funcs->get_state(state);
69 }
70
71 int hal_device_led_get_number(void)
72 {
73         if (!g_led_funcs ||
74             !g_led_funcs->get_number)
75                 return -ENODEV;
76
77         return g_led_funcs->get_number();
78 }
79
80 int hal_device_led_set_number(int number)
81 {
82         if (!g_led_funcs ||
83             !g_led_funcs->set_num)
84                 return -ENODEV;
85
86         g_led_funcs->set_num(number);
87
88         return 0;
89 }
90
91 int hal_device_led_get_max_num(void)
92 {
93         if (!g_led_funcs ||
94             !g_led_funcs->get_max_num)
95                 return -ENODEV;
96
97         return g_led_funcs->get_max_num();
98 }
99
100 int hal_device_keyled_set_state(struct keyled_state *state)
101 {
102         if (!g_led_funcs ||
103             !g_led_funcs->keyled_set_state)
104                 return -ENODEV;
105
106         return g_led_funcs->keyled_set_state(state);
107 }
108
109 int hal_device_keyled_get_state(int *keycode, int *brightness)
110 {
111         if (!g_led_funcs ||
112             !g_led_funcs->keyled_get_state)
113                 return -ENODEV;
114
115         return g_led_funcs->keyled_get_state(keycode, brightness);
116 }
117