Initial release of hal-api-radio
[platform/hal/api/radio.git] / src / hal-api-radio.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdint.h>
4 #include <dlfcn.h>
5 #include <dlog.h>
6
7 #include <hal/hal-common.h>
8
9 #include "hal-radio.h"
10 #include "common.h"
11
12 #define HAL_RADIO_RETURN_IF_FAILED(arg, ret) \
13         do {\
14                 if (!(arg)) {\
15                         _E("[%s]failed, return[%s]", #arg, #ret);\
16                         return (ret);\
17                 }\
18         } while (0)
19
20 typedef struct _hal_radio_s {
21         void *backend;
22         hal_backend_radio_funcs *funcs;
23 } hal_radio_s;
24
25 hal_radio_error_t hal_radio_get_backend(void **hal_radio_handle)
26 {
27
28         int ret = 0;
29         hal_radio_s *new_radio_handle;
30
31         HAL_RADIO_RETURN_IF_FAILED(hal_radio_handle, HAL_RADIO_ERROR_INVALID_PARAMETER);
32
33         new_radio_handle = (hal_radio_s *)calloc(1, sizeof(hal_radio_s));
34         if (!new_radio_handle) {
35                 _E("Failed to malloc hal radio handle");
36                 return HAL_RADIO_ERROR_OUT_OF_MEMORY;
37         }
38
39         ret = hal_common_get_backend(HAL_MODULE_RADIO, (void **)&new_radio_handle->funcs);
40         if (ret != TIZEN_ERROR_NONE) {
41                 _E("Failed to get backend");
42                 free(new_radio_handle);
43                 return HAL_RADIO_ERROR_INTERNAL;
44         }
45
46         if (!new_radio_handle->funcs) {
47                 _E("Failed to load functions");
48                 free(new_radio_handle);
49                 return HAL_RADIO_ERROR_NOT_IMPLEMENTED;
50         }
51
52         *hal_radio_handle = new_radio_handle;
53
54         return HAL_RADIO_ERROR_NONE;
55 }
56
57 hal_radio_error_t hal_radio_put_backend(void *hal_radio_handle)
58 {
59         int ret = 0;
60         hal_radio_s *handle = (hal_radio_s *)hal_radio_handle;
61
62         HAL_RADIO_RETURN_IF_FAILED(handle, HAL_RADIO_ERROR_INVALID_PARAMETER);
63         HAL_RADIO_RETURN_IF_FAILED(handle->funcs, HAL_RADIO_ERROR_INVALID_PARAMETER);
64
65         ret = hal_common_put_backend(HAL_MODULE_RADIO, (void *)handle->funcs);
66
67         memset(handle, 0x0, sizeof(hal_radio_s));
68         free(handle);
69
70         return ret;
71 }
72
73 hal_radio_error_t hal_radio_init(void *hal_radio_handle)
74 {
75         int ret = HAL_RADIO_ERROR_NONE;
76         hal_radio_s *handle = (hal_radio_s *)hal_radio_handle;
77
78         HAL_RADIO_RETURN_IF_FAILED(handle, HAL_RADIO_ERROR_INVALID_PARAMETER);
79         HAL_RADIO_RETURN_IF_FAILED(handle->funcs, HAL_RADIO_ERROR_INVALID_PARAMETER);
80         HAL_RADIO_RETURN_IF_FAILED(handle->funcs->init, HAL_RADIO_ERROR_NOT_IMPLEMENTED);
81
82         ret = handle->funcs->init(&handle->backend);
83         if (ret != HAL_RADIO_ERROR_NONE)
84                 _E("backend init failed[0x%x]", ret);
85
86         return ret;
87 }
88
89 hal_radio_error_t hal_radio_deinit(void *hal_radio_handle)
90 {
91         int ret = HAL_RADIO_ERROR_NONE;
92         hal_radio_s *handle = (hal_radio_s *)hal_radio_handle;
93
94         HAL_RADIO_RETURN_IF_FAILED(handle, HAL_RADIO_ERROR_INVALID_PARAMETER);
95         HAL_RADIO_RETURN_IF_FAILED(handle->funcs, HAL_RADIO_ERROR_INVALID_PARAMETER);
96         HAL_RADIO_RETURN_IF_FAILED(handle->funcs->deinit, HAL_RADIO_ERROR_NOT_IMPLEMENTED);
97
98         ret = handle->funcs->deinit(handle->backend);
99         if (ret != HAL_RADIO_ERROR_NONE)
100                 _E("backend deinit failed[0x%x]", ret);
101
102         return ret;
103 }
104
105 hal_radio_error_t hal_radio_prepare(void *hal_radio_handle)
106 {
107         int ret = HAL_RADIO_ERROR_NONE;
108         hal_radio_s *handle = (hal_radio_s *)hal_radio_handle;
109
110         HAL_RADIO_RETURN_IF_FAILED(handle, HAL_RADIO_ERROR_INVALID_PARAMETER);
111         HAL_RADIO_RETURN_IF_FAILED(handle->funcs, HAL_RADIO_ERROR_INVALID_PARAMETER);
112         HAL_RADIO_RETURN_IF_FAILED(handle->funcs->prepare, HAL_RADIO_ERROR_NOT_IMPLEMENTED);
113
114         ret = handle->funcs->prepare(handle->backend);
115         if (ret != HAL_RADIO_ERROR_NONE)
116                 _E("backend prepare failed[0x%x]", ret);
117
118         return ret;
119 }
120
121 hal_radio_error_t hal_radio_unprepare(void *hal_radio_handle)
122 {
123         int ret = HAL_RADIO_ERROR_NONE;
124         hal_radio_s *handle = (hal_radio_s *)hal_radio_handle;
125
126         HAL_RADIO_RETURN_IF_FAILED(handle, HAL_RADIO_ERROR_INVALID_PARAMETER);
127         HAL_RADIO_RETURN_IF_FAILED(handle->funcs, HAL_RADIO_ERROR_INVALID_PARAMETER);
128         HAL_RADIO_RETURN_IF_FAILED(handle->funcs->unprepare, HAL_RADIO_ERROR_NOT_IMPLEMENTED);
129
130         ret = handle->funcs->unprepare(handle->backend);
131         if (ret != HAL_RADIO_ERROR_NONE)
132                 _E("backend unprepare failed[0x%x]", ret);
133
134         return ret;
135 }
136
137 hal_radio_error_t hal_radio_open(void *hal_radio_handle)
138 {
139         int ret = HAL_RADIO_ERROR_NONE;
140         hal_radio_s *handle = (hal_radio_s *)hal_radio_handle;
141
142         HAL_RADIO_RETURN_IF_FAILED(handle, HAL_RADIO_ERROR_INVALID_PARAMETER);
143         HAL_RADIO_RETURN_IF_FAILED(handle->funcs, HAL_RADIO_ERROR_INVALID_PARAMETER);
144         HAL_RADIO_RETURN_IF_FAILED(handle->funcs->open, HAL_RADIO_ERROR_NOT_IMPLEMENTED);
145
146         ret = handle->funcs->open(handle->backend);
147         if (ret != HAL_RADIO_ERROR_NONE)
148                 _E("backend open failed[0x%x]", ret);
149
150         return ret;
151 }
152
153 hal_radio_error_t hal_radio_close(void *hal_radio_handle)
154 {
155         int ret = HAL_RADIO_ERROR_NONE;
156         hal_radio_s *handle = (hal_radio_s *)hal_radio_handle;
157
158         HAL_RADIO_RETURN_IF_FAILED(handle, HAL_RADIO_ERROR_INVALID_PARAMETER);
159         HAL_RADIO_RETURN_IF_FAILED(handle->funcs, HAL_RADIO_ERROR_INVALID_PARAMETER);
160         HAL_RADIO_RETURN_IF_FAILED(handle->funcs->close, HAL_RADIO_ERROR_NOT_IMPLEMENTED);
161
162         ret = handle->funcs->close(handle->backend);
163         if (ret != HAL_RADIO_ERROR_NONE)
164                 _E("backend close failed[0x%x]", ret);
165
166         return ret;
167 }
168
169 hal_radio_error_t hal_radio_start(void *hal_radio_handle)
170 {
171         int ret = HAL_RADIO_ERROR_NONE;
172         hal_radio_s *handle = (hal_radio_s *)hal_radio_handle;
173
174         HAL_RADIO_RETURN_IF_FAILED(handle, HAL_RADIO_ERROR_INVALID_PARAMETER);
175         HAL_RADIO_RETURN_IF_FAILED(handle->funcs, HAL_RADIO_ERROR_INVALID_PARAMETER);
176         HAL_RADIO_RETURN_IF_FAILED(handle->funcs->start, HAL_RADIO_ERROR_NOT_IMPLEMENTED);
177
178         ret = handle->funcs->start(handle->backend);
179         if (ret != HAL_RADIO_ERROR_NONE)
180                 _E("backend start failed[0x%x]", ret);
181
182         return ret;
183 }
184
185 hal_radio_error_t hal_radio_stop(void *hal_radio_handle)
186 {
187         int ret = HAL_RADIO_ERROR_NONE;
188         hal_radio_s *handle = (hal_radio_s *)hal_radio_handle;
189
190         HAL_RADIO_RETURN_IF_FAILED(handle, HAL_RADIO_ERROR_INVALID_PARAMETER);
191         HAL_RADIO_RETURN_IF_FAILED(handle->funcs, HAL_RADIO_ERROR_INVALID_PARAMETER);
192         HAL_RADIO_RETURN_IF_FAILED(handle->funcs->stop, HAL_RADIO_ERROR_NOT_IMPLEMENTED);
193
194         ret = handle->funcs->stop(handle->backend);
195         if (ret != HAL_RADIO_ERROR_NONE)
196                 _E("backend stop failed[0x%x]", ret);
197
198         return ret;
199 }
200
201 hal_radio_error_t hal_radio_seek(void *hal_radio_handle, hal_radio_seek_direction_type_t direction)
202 {
203         int ret = HAL_RADIO_ERROR_NONE;
204         hal_radio_s *handle = (hal_radio_s *)hal_radio_handle;
205
206         HAL_RADIO_RETURN_IF_FAILED(handle, HAL_RADIO_ERROR_INVALID_PARAMETER);
207         HAL_RADIO_RETURN_IF_FAILED(handle->funcs, HAL_RADIO_ERROR_INVALID_PARAMETER);
208         HAL_RADIO_RETURN_IF_FAILED(handle->funcs->seek, HAL_RADIO_ERROR_NOT_IMPLEMENTED);
209
210         ret = handle->funcs->seek(handle->backend, direction);
211         if (ret != HAL_RADIO_ERROR_NONE)
212                 _E("backend seek [%s] failed[0x%x]", direction ? "down" : "up", ret);
213
214         return ret;
215 }
216
217 hal_radio_error_t hal_radio_get_frequency(void *hal_radio_handle, uint32_t *frequency)
218 {
219         int ret = HAL_RADIO_ERROR_NONE;
220         hal_radio_s *handle = (hal_radio_s *)hal_radio_handle;
221         uint32_t get_freq;
222
223         HAL_RADIO_RETURN_IF_FAILED(handle, HAL_RADIO_ERROR_INVALID_PARAMETER);
224         HAL_RADIO_RETURN_IF_FAILED(frequency, HAL_RADIO_ERROR_INVALID_PARAMETER);
225         HAL_RADIO_RETURN_IF_FAILED(handle->funcs, HAL_RADIO_ERROR_INVALID_PARAMETER);
226         HAL_RADIO_RETURN_IF_FAILED(handle->funcs->get_frequency, HAL_RADIO_ERROR_NOT_IMPLEMENTED);
227
228         ret = handle->funcs->get_frequency(handle->backend, &get_freq);
229         if (ret != HAL_RADIO_ERROR_NONE) {
230                 _E("backend get_frequency failed[0x%x]", ret);
231                 get_freq = 0;
232         }
233
234         *frequency = get_freq;
235
236         return ret;
237 }
238
239 hal_radio_error_t hal_radio_set_frequency(void *hal_radio_handle, uint32_t frequency)
240 {
241         int ret = HAL_RADIO_ERROR_NONE;
242         hal_radio_s *handle = (hal_radio_s *)hal_radio_handle;
243
244         HAL_RADIO_RETURN_IF_FAILED(handle, HAL_RADIO_ERROR_INVALID_PARAMETER);
245         HAL_RADIO_RETURN_IF_FAILED(handle->funcs, HAL_RADIO_ERROR_INVALID_PARAMETER);
246         HAL_RADIO_RETURN_IF_FAILED(handle->funcs->set_frequency, HAL_RADIO_ERROR_NOT_IMPLEMENTED);
247
248         ret = handle->funcs->set_frequency(handle->backend, frequency);
249         if (ret != HAL_RADIO_ERROR_NONE)
250                 _E("backend set_frequency failed[0x%x]", ret);
251
252         return ret;
253 }
254
255 hal_radio_error_t hal_radio_get_signal_strength(void *hal_radio_handle, int32_t *strength)
256 {
257         int ret = HAL_RADIO_ERROR_NONE;
258         hal_radio_s *handle = (hal_radio_s *)hal_radio_handle;
259         int32_t get_strength = 0;
260
261         HAL_RADIO_RETURN_IF_FAILED(handle, HAL_RADIO_ERROR_INVALID_PARAMETER);
262         HAL_RADIO_RETURN_IF_FAILED(strength, HAL_RADIO_ERROR_INVALID_PARAMETER);
263         HAL_RADIO_RETURN_IF_FAILED(handle->funcs, HAL_RADIO_ERROR_INVALID_PARAMETER);
264         HAL_RADIO_RETURN_IF_FAILED(handle->funcs->get_signal_strength, HAL_RADIO_ERROR_NOT_IMPLEMENTED);
265
266         ret = handle->funcs->get_signal_strength(handle->backend, &get_strength);
267         if (ret != HAL_RADIO_ERROR_NONE) {
268                 _E("backend get_signal_strength failed[0x%x]", ret);
269                 get_strength = 0;
270         }
271
272         *strength = get_strength;
273
274         return ret;
275 }