03fe5a43cc2cd10c0e5c7d5aa1b2a2ad0315db91
[platform/hal/api/uwb.git] / src / hal-api-uwb.c
1 /*
2  * HAL (Hardware Abstract Layer) UWB API
3  *
4  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the License);
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #include <stdio.h>
20 #include <stdint.h>
21 #include <dlfcn.h>
22 #include <dlog.h>
23
24 #include <hal/hal-common.h>
25
26 #include "hal-uwb-interface.h"
27 #include "hal-uwb.h"
28 #include "common.h"
29
30 #ifndef EXPORT
31 #define EXPORT __attribute__ ((visibility("default")))
32 #endif
33
34 #define ARRAY_SIZE(name)        (sizeof(name)/sizeof(name[0]))
35
36 static hal_backend_uwb_funcs *g_uwb_funcs = NULL;
37
38 EXPORT
39 int hal_uwb_get_backend(void)
40 {
41         int ret;
42
43         if (g_uwb_funcs)
44                 return 0;
45
46         ret = hal_common_get_backend(HAL_MODULE_UWB, (void **)&g_uwb_funcs);
47         if (ret < 0) {
48                 _E("Failed to get backend\n");
49                 return -EINVAL;
50         }
51
52         return 0;
53 }
54
55 EXPORT
56 int hal_uwb_put_backend(void)
57 {
58         int ret;
59
60         if (!g_uwb_funcs)
61                 return -EINVAL;
62
63         ret = hal_common_put_backend(HAL_MODULE_UWB, (void *)g_uwb_funcs);
64         if (ret < 0) {
65                 _E("Failed to put backend\n");
66                 return -EINVAL;
67         }
68         g_uwb_funcs = NULL;
69
70         return 0;
71 }
72
73 EXPORT
74 int hal_uwb_start(uwb_hal_event_cbs_s *event_cbs)
75 {
76         if (!g_uwb_funcs)
77                 return -ENOTSUP;
78         return g_uwb_funcs->start(event_cbs);
79 }
80
81 EXPORT
82 int hal_uwb_stop(void)
83 {
84         if (!g_uwb_funcs)
85                 return -ENOTSUP;
86         return g_uwb_funcs->stop();
87 }
88
89 EXPORT
90 int hal_uwb_test(void)
91 {
92         if (!g_uwb_funcs)
93                 return -ENOTSUP;
94         return g_uwb_funcs->test();
95 }
96
97 EXPORT
98 int hal_uwb_reset(void)
99 {
100         if (!g_uwb_funcs)
101                 return -ENOTSUP;
102         return g_uwb_funcs->reset();
103 }
104
105 EXPORT
106 int hal_uwb_factory_reset(void)
107 {
108         if (!g_uwb_funcs)
109                 return -ENOTSUP;
110         return g_uwb_funcs->factory_reset();
111 }
112
113 EXPORT
114 int hal_uwb_enable_network(void)
115 {
116         if (!g_uwb_funcs)
117                 return -ENOTSUP;
118         return g_uwb_funcs->enable_network();
119 }
120
121 EXPORT
122 int hal_uwb_disable_network(void)
123 {
124         if (!g_uwb_funcs)
125                 return -ENOTSUP;
126         return g_uwb_funcs->disable_network();
127 }
128
129 EXPORT
130 int hal_uwb_get_network_info(uwb_hal_network_s **network_info)
131 {
132         if (!g_uwb_funcs)
133                 return -ENOTSUP;
134         return g_uwb_funcs->get_network_info(network_info);
135 }
136
137 EXPORT
138 int hal_uwb_set_configurations(uint16_t node_id, const GVariant *configurations)
139 {
140         if (!g_uwb_funcs)
141                 return -ENOTSUP;
142         return g_uwb_funcs->set_configurations(node_id, configurations);
143 }
144
145 EXPORT
146 int hal_uwb_get_configurations(uint16_t node_id, GVariant **configurations)
147 {
148         if (!g_uwb_funcs)
149                 return -ENOTSUP;
150         return g_uwb_funcs->get_configurations(node_id, configurations);
151 }
152
153 EXPORT
154 int hal_uwb_set_position(uint64_t node_id, int x, int y, int z)
155 {
156         if (!g_uwb_funcs)
157                 return -ENOTSUP;
158         return g_uwb_funcs->set_position(node_id, x, y, z);
159 }
160
161 EXPORT
162 int hal_uwb_get_own_node(uwb_hal_node_s **own_node)
163 {
164         if (!g_uwb_funcs)
165                 return -ENOTSUP;
166         return g_uwb_funcs->get_own_node(own_node);
167 }
168
169 EXPORT
170 int hal_uwb_send_message(const unsigned char *message, int message_length)
171 {
172         if (!g_uwb_funcs)
173                 return -ENOTSUP;
174         return g_uwb_funcs->send_message(message, message_length);
175 }
176
177 EXPORT
178 int hal_uwb_send_message_to(uint16_t node_id, const unsigned char *message, int message_length)
179 {
180         if (!g_uwb_funcs)
181                 return -ENOTSUP;
182         return g_uwb_funcs->send_message_to(node_id, message, message_length);
183 }