summaryrefslogtreecommitdiff
path: root/src/radio_hal_interface.c
blob: cd62ba948ebf3b133b05283446761caee071c631 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
 * radio_hal_interface.c
 *
 * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

#include <dlfcn.h>
#include "mm_radio_utils.h"
#include "radio_hal_interface.h"

#define LIB_TIZEN_RADIO PATH_LIBDIR"/libtizen-radio.so"

static int __convert_error_code(int code, char *func_name)
{
	int ret = RADIO_ERROR_NONE;
	char *msg = "RADIO_ERROR_NONE";
	MMRADIO_LOG_DEBUG("[%s] Enter code :%x", func_name, code);

	switch (code) {

	case RADIO_ERROR_NONE:
		ret = MM_ERROR_NONE;
		msg = "MM_ERROR_NONE";
		break;
	case RADIO_ERROR_INVALID_PARAMETER:
		ret = MM_ERROR_COMMON_INVALID_ARGUMENT;
		msg = "MM_ERROR_COMMON_INVALID_ARGUMENT";
		break;
	case RADIO_ERROR_INVALID_STATE:
		ret = MM_ERROR_RADIO_INVALID_STATE;
		msg = "MM_ERROR_RADIO_INVALID_STATE";
		break;
	case RADIO_ERROR_OUT_OF_MEMORY:
		ret = MM_ERROR_RADIO_NO_FREE_SPACE;
		msg = "MM_ERROR_RADIO_NO_FREE_SPACE";
		break;
	case RADIO_ERROR_PERMISSION_DENIED:
	case RADIO_ERROR_DEVICE_NOT_PREPARED:
		ret = MM_ERROR_RADIO_PERMISSION_DENIED;
		msg = "MM_ERROR_RADIO_PERMISSION_DENIED";
		break;
	case RADIO_ERROR_DEVICE_NOT_OPENED:
		ret = MM_ERROR_RADIO_DEVICE_NOT_OPENED;
		msg = "MM_ERROR_RADIO_DEVICE_NOT_OPENED";
		break;
	case RADIO_ERROR_NOT_SUPPORTED:
	case RADIO_ERROR_DEVICE_NOT_SUPPORTED:
	case RADIO_ERROR_DEVICE_NOT_FOUND:
		ret = MM_ERROR_RADIO_DEVICE_NOT_FOUND;
		msg = "MM_ERROR_RADIO_DEVICE_NOT_FOUND";
		break;
	case RADIO_ERROR_NO_ANTENNA:
		ret = MM_ERROR_RADIO_NO_ANTENNA;
		msg = "MM_ERROR_RADIO_NO_ANTENNA";
		break;
	case RADIO_ERROR_UNKNOWN:
	case RADIO_ERROR_INTERNAL:
	default:
		ret = MM_ERROR_RADIO_INTERNAL;
		msg = "MM_ERROR_RADIO_INTERNAL";
		break;
	}
	MMRADIO_LOG_ERROR("[%s] %s(0x%08x) : core fw error(0x%x)", func_name, msg, ret, code);
	return ret;
}

int radio_hal_interface_init(mm_radio_hal_interface **handle)
{
	mm_radio_hal_interface *h = NULL;
	int ret = MM_ERROR_NONE;
	h = (mm_radio_hal_interface *)malloc(sizeof(mm_radio_hal_interface));

	if (!h) {
		MMRADIO_LOG_ERROR("cannot allocate memory for radio_hal interface");
		return MM_ERROR_RADIO_NO_FREE_SPACE;
	}

	h->dl_handle = dlopen(LIB_TIZEN_RADIO, RTLD_NOW);

	if (h->dl_handle) {
		h->intf.init = dlsym(h->dl_handle, "radio_init");
		h->intf.deinit = dlsym(h->dl_handle, "radio_deinit");
		h->intf.prepare = dlsym(h->dl_handle, "radio_prepare");
		h->intf.unprepare = dlsym(h->dl_handle, "radio_unprepare");
		h->intf.open = dlsym(h->dl_handle, "radio_open");
		h->intf.close = dlsym(h->dl_handle, "radio_close");
		h->intf.start = dlsym(h->dl_handle, "radio_start");
		h->intf.stop = dlsym(h->dl_handle, "radio_stop");
		h->intf.seek = dlsym(h->dl_handle, "radio_seek");
		h->intf.get_frequency = dlsym(h->dl_handle, "radio_get_frequency");
		h->intf.set_frequency = dlsym(h->dl_handle, "radio_set_frequency");
		h->intf.mute = dlsym(h->dl_handle, "radio_mute");
		h->intf.unmute = dlsym(h->dl_handle, "radio_unmute");
		h->intf.get_signal_strength = dlsym(h->dl_handle, "radio_get_signal_strength");
		h->intf.get_volume = dlsym(h->dl_handle, "radio_get_volume");
		h->intf.set_volume = dlsym(h->dl_handle, "radio_set_volume");
		h->intf.set_media_volume = dlsym(h->dl_handle, "radio_set_media_volume");

		if (h->intf.init == NULL || h->intf.deinit == NULL) {
			MMRADIO_LOG_ERROR("could not get mandatory funtion");
			goto FAIL;
		}

		if (h->intf.init) {
			ret = h->intf.init(&h->rh_handle);
			if (ret != MM_ERROR_NONE) {
				MMRADIO_LOG_ERROR("radio_hal init failed %d", ret);
				goto FAIL;
			}
		}
	} else {
		MMRADIO_LOG_ERROR("open radio hal_interface failed : %s", dlerror());
		ret = MM_ERROR_RADIO_INTERNAL;
		goto FAIL;
	}

	*handle = h;

	MMRADIO_LOG_ERROR("open radio_hal interface");

	return MM_ERROR_NONE;
FAIL:
	if (h) {
		if (h->dl_handle)
			dlclose(h->dl_handle);
		free(h);
	}

	return ret;
}

int radio_hal_interface_deinit(mm_radio_hal_interface *handle)
{
	int ret = MM_ERROR_NONE;
	MMRADIO_CHECK_ARG(handle);

	if (handle->dl_handle) {
		ret = handle->intf.deinit(handle->rh_handle);
		if (ret != RADIO_ERROR_NONE)
			return __convert_error_code(ret, (char *)__FUNCTION__);

		handle->rh_handle = NULL;

		dlclose(handle->dl_handle);
		handle->dl_handle = NULL;
	}

	free(handle);
	handle = NULL;

	return ret;
}

int radio_hal_open(mm_radio_hal_interface *radio_handle)
{
	radio_error_t ret = RADIO_ERROR_NONE;
	MMRADIO_CHECK_ARG(radio_handle);
	if (radio_handle->intf.open) {
		ret = radio_handle->intf.open(radio_handle->rh_handle);
		if (ret != RADIO_ERROR_NONE)
			return __convert_error_code(ret, (char *)__FUNCTION__);
	} else {
		MMRADIO_LOG_WARNING("radio_hal open is NULL");
		return MM_ERROR_NOT_SUPPORT_API;
	}
	return MM_ERROR_NONE;
}

int radio_hal_prepare(mm_radio_hal_interface *radio_handle)
{
	radio_error_t ret = RADIO_ERROR_NONE;
	MMRADIO_CHECK_ARG(radio_handle);
	if (radio_handle->intf.prepare) {
		ret = radio_handle->intf.prepare(radio_handle->rh_handle);
		if (ret != RADIO_ERROR_NONE)
			return __convert_error_code(ret, (char *)__FUNCTION__);
	} else {
		MMRADIO_LOG_WARNING("radio_hal prepare is NULL");
		return MM_ERROR_NOT_SUPPORT_API;
	}
	return MM_ERROR_NONE;
}

int radio_hal_unprepare(mm_radio_hal_interface *radio_handle)
{
	radio_error_t ret = RADIO_ERROR_NONE;
	MMRADIO_CHECK_ARG(radio_handle);
	if (radio_handle->intf.prepare) {
		ret = radio_handle->intf.unprepare(radio_handle->rh_handle);
		if (ret != RADIO_ERROR_NONE)
			return __convert_error_code(ret, (char *)__FUNCTION__);
	} else {
		MMRADIO_LOG_WARNING("radio_hal prepare is NULL");
		return MM_ERROR_NOT_SUPPORT_API;
	}
	return MM_ERROR_NONE;
}


int radio_hal_close(mm_radio_hal_interface *radio_handle)
{
	radio_error_t ret = RADIO_ERROR_NONE;
	MMRADIO_CHECK_ARG(radio_handle);
	if (radio_handle->intf.close) {
		ret = radio_handle->intf.close(radio_handle->rh_handle);
		if (ret != RADIO_ERROR_NONE)
			return __convert_error_code(ret, (char *)__FUNCTION__);
	} else {
		MMRADIO_LOG_WARNING("radio_hal close is NULL");
		return MM_ERROR_NOT_SUPPORT_API;
	}
	return MM_ERROR_NONE;
}

int radio_hal_start(mm_radio_hal_interface *radio_handle)
{
	radio_error_t ret = RADIO_ERROR_NONE;
	MMRADIO_CHECK_ARG(radio_handle);
	if (radio_handle->intf.start) {
		ret = radio_handle->intf.start(radio_handle->rh_handle);
		if (ret != RADIO_ERROR_NONE)
			return __convert_error_code(ret, (char *)__FUNCTION__);
	} else {
		MMRADIO_LOG_WARNING("radio_hal start is NULL");
		return MM_ERROR_NOT_SUPPORT_API;
	}
	return MM_ERROR_NONE;
}

int radio_hal_stop(mm_radio_hal_interface *radio_handle)
{
	radio_error_t ret = RADIO_ERROR_NONE;
	MMRADIO_CHECK_ARG(radio_handle);
	if (radio_handle->intf.stop) {
		ret = radio_handle->intf.stop(radio_handle->rh_handle);
		if (ret != RADIO_ERROR_NONE)
			return __convert_error_code(ret, (char *)__FUNCTION__);
	} else {
		MMRADIO_LOG_WARNING("radio_hal stop is NULL");
		return MM_ERROR_NOT_SUPPORT_API;
	}
	return MM_ERROR_NONE;
}


int radio_hal_seek(mm_radio_hal_interface *radio_handle, seek_direction_type_t direction)
{
	radio_error_t ret = RADIO_ERROR_NONE;
	MMRADIO_CHECK_ARG(radio_handle);
	if (radio_handle->intf.seek) {
		ret = radio_handle->intf.seek(radio_handle->rh_handle, (radio_seek_direction_type_t)direction);
		if (ret != RADIO_ERROR_NONE)
			return __convert_error_code(ret, (char *)__FUNCTION__);
	} else {
		MMRADIO_LOG_WARNING("radio_hal seek is NULL");
		return MM_ERROR_NOT_SUPPORT_API;
	}
	return MM_ERROR_NONE;
}

int radio_hal_get_frequency(mm_radio_hal_interface *radio_handle, uint32_t *frequency)
{
	radio_error_t ret = RADIO_ERROR_NONE;
	MMRADIO_CHECK_ARG(radio_handle);
	if (radio_handle->intf.get_frequency) {
		ret = radio_handle->intf.get_frequency(radio_handle->rh_handle, frequency);
		if (ret != RADIO_ERROR_NONE)
			return __convert_error_code(ret, (char *)__FUNCTION__);
	} else {
		MMRADIO_LOG_WARNING("radio_hal get_frequency is NULL");
		return MM_ERROR_NOT_SUPPORT_API;
	}
	return MM_ERROR_NONE;
}

int radio_hal_set_frequency(mm_radio_hal_interface *radio_handle, uint32_t frequency)
{
	radio_error_t ret = RADIO_ERROR_NONE;
	MMRADIO_CHECK_ARG(radio_handle);
	if (radio_handle->intf.set_frequency) {
		ret = radio_handle->intf.set_frequency(radio_handle->rh_handle, frequency);
		if (ret != RADIO_ERROR_NONE)
			return __convert_error_code(ret, (char *)__FUNCTION__);
	} else {
		MMRADIO_LOG_WARNING("radio_hal get_frequency is NULL");
		return MM_ERROR_NOT_SUPPORT_API;
	}
	return MM_ERROR_NONE;
}

int radio_hal_get_signal_strength(mm_radio_hal_interface *radio_handle, uint32_t *strength)
{
	radio_error_t ret = RADIO_ERROR_NONE;
	MMRADIO_CHECK_ARG(radio_handle);
	if (radio_handle->intf.get_signal_strength) {
		ret = radio_handle->intf.get_signal_strength(radio_handle->rh_handle, strength);
		if (ret != RADIO_ERROR_NONE)
			return __convert_error_code(ret, (char *)__FUNCTION__);
	} else {
		MMRADIO_LOG_WARNING("radio_hal get_signal_strength is NULL");
		return MM_ERROR_NOT_SUPPORT_API;
	}
	return MM_ERROR_NONE;
}

int radio_hal_mute(mm_radio_hal_interface *radio_handle)
{
	radio_error_t ret = RADIO_ERROR_NONE;
	MMRADIO_CHECK_ARG(radio_handle);
	if (radio_handle->intf.mute) {
		ret = radio_handle->intf.mute(radio_handle->rh_handle);
		if (ret != RADIO_ERROR_NONE)
			return __convert_error_code(ret, (char *)__FUNCTION__);
	} else {
		MMRADIO_LOG_WARNING("radio_hal mute is NULL");
		return MM_ERROR_NOT_SUPPORT_API;
	}
	return MM_ERROR_NONE;
}

int radio_hal_unmute(mm_radio_hal_interface *radio_handle)
{
	radio_error_t ret = RADIO_ERROR_NONE;
	MMRADIO_CHECK_ARG(radio_handle);
	if (radio_handle->intf.unmute) {
		ret = radio_handle->intf.unmute(radio_handle->rh_handle);
		if (ret != RADIO_ERROR_NONE)
			return __convert_error_code(ret, (char *)__FUNCTION__);
	} else {
		MMRADIO_LOG_WARNING("radio_hal unmute is NULL");
		return MM_ERROR_NOT_SUPPORT_API;
	}
	return MM_ERROR_NONE;

}

int radio_hal_set_volume(mm_radio_hal_interface *radio_handle, float volume)
{
	radio_error_t ret = RADIO_ERROR_NONE;
	MMRADIO_CHECK_ARG(radio_handle);
	if (radio_handle->intf.set_volume) {
		ret = radio_handle->intf.set_volume(radio_handle->rh_handle, volume);
		if (ret != RADIO_ERROR_NONE)
			return __convert_error_code(ret, (char *)__FUNCTION__);
	} else {
		MMRADIO_LOG_WARNING("radio_hal set_volume is NULL");
		return MM_ERROR_NOT_SUPPORT_API;
	}
	return MM_ERROR_NONE;

}

int radio_hal_get_volume(mm_radio_hal_interface *radio_handle, float *volume)
{
	radio_error_t ret = RADIO_ERROR_NONE;
	MMRADIO_CHECK_ARG(radio_handle);
	if (radio_handle->intf.get_volume) {
		ret = radio_handle->intf.get_volume(radio_handle->rh_handle, volume);
		if (ret != RADIO_ERROR_NONE)
			return __convert_error_code(ret, (char *)__FUNCTION__);
	} else {
		MMRADIO_LOG_WARNING("radio_hal get_volume is NULL");
		return MM_ERROR_NOT_SUPPORT_API;
	}
	return MM_ERROR_NONE;

}

int radio_hal_set_media_volume(mm_radio_hal_interface *radio_handle, uint32_t level)
{
	radio_error_t ret = RADIO_ERROR_NONE;
	MMRADIO_CHECK_ARG(radio_handle);
	if (radio_handle->intf.set_media_volume) {
		ret = radio_handle->intf.set_media_volume(radio_handle->rh_handle, level);
		if (ret != RADIO_ERROR_NONE)
			return __convert_error_code(ret, (char *)__FUNCTION__);
	} else {
		MMRADIO_LOG_WARNING("radio_hal set_media_volume is NULL");
		return MM_ERROR_NOT_SUPPORT_API;
	}
	return MM_ERROR_NONE;
}

#ifdef __cplusplus
}
#endif /* __cplusplus */