4 * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 /*===========================================================================================
24 ========================================================================================== */
32 #include <sys/ioctl.h>
38 #include <mm_message.h>
41 #include "mm_radio_priv_hal.h"
43 /*===========================================================================================
44 LOCAL DEFINITIONS AND DECLARATIONS FOR MODULE
45 ========================================================================================== */
46 /*---------------------------------------------------------------------------
47 GLOBAL CONSTANT DEFINITIONS:
48 ---------------------------------------------------------------------------*/
50 /*---------------------------------------------------------------------------
51 IMPORTED VARIABLE DECLARATIONS:
52 ---------------------------------------------------------------------------*/
54 /*---------------------------------------------------------------------------
55 IMPORTED FUNCTION DECLARATIONS:
56 ---------------------------------------------------------------------------*/
58 /*---------------------------------------------------------------------------
60 ---------------------------------------------------------------------------*/
61 #define DEFAULT_DEVICE "/dev/radio0"
64 #define DEFAULT_FREQ 107700
67 #define RADIO_FREQ_FORMAT_SET(x_freq) ((x_freq) * FREQ_FRAC)
68 #define RADIO_FREQ_FORMAT_GET(x_freq) ((x_freq) / FREQ_FRAC)
69 /* If non-zero, wrap around when at the end of the frequency range, else stop seeking */
70 #define DEFAULT_WRAP_AROUND 1
72 #define RADIO_DEFAULT_REGION MM_RADIO_REGION_GROUP_USA
73 #define READ_MAX_BUFFER_SIZE 1024
74 #define DEFAULT_MAX_MEDIA_VOLUME 15
75 /*---------------------------------------------------------------------------
76 LOCAL CONSTANT DEFINITIONS:
77 ---------------------------------------------------------------------------*/
79 /*---------------------------------------------------------------------------
80 LOCAL DATA TYPE DEFINITIONS:
81 ---------------------------------------------------------------------------*/
83 /*---------------------------------------------------------------------------
84 GLOBAL VARIABLE DEFINITIONS:
85 ---------------------------------------------------------------------------*/
88 /*---------------------------------------------------------------------------
89 LOCAL VARIABLE DEFINITIONS:
90 ---------------------------------------------------------------------------*/
91 /* radio region configuration table */
92 static const MMRadioRegion_t region_table[] = {
93 { /* Notrh America, South America, South Korea, Taiwan, Australia */
94 MM_RADIO_REGION_GROUP_USA, /* region type */
95 MM_RADIO_DEEMPHASIS_75_US, /* de-emphasis */
96 MM_RADIO_FREQ_MIN_87500_KHZ, /* min freq. */
97 MM_RADIO_FREQ_MAX_108000_KHZ, /* max freq. */
100 { /* China, Europe, Africa, Middle East, Hong Kong, India, Indonesia, Russia, Singapore */
101 MM_RADIO_REGION_GROUP_EUROPE,
102 MM_RADIO_DEEMPHASIS_50_US,
103 MM_RADIO_FREQ_MIN_87500_KHZ,
104 MM_RADIO_FREQ_MAX_108000_KHZ,
108 MM_RADIO_REGION_GROUP_JAPAN,
109 MM_RADIO_DEEMPHASIS_50_US,
110 MM_RADIO_FREQ_MIN_76100_KHZ,
111 MM_RADIO_FREQ_MAX_89900_KHZ,
116 /*---------------------------------------------------------------------------
117 LOCAL FUNCTION PROTOTYPES:
118 ---------------------------------------------------------------------------*/
119 static bool __mmradio_post_message(mm_radio_t *radio, enum MMMessageType msgtype, MMMessageParamType *param);
120 static int __mmradio_check_state(mm_radio_t *radio, MMRadioCommand command);
121 static int __mmradio_get_state(mm_radio_t *radio);
122 static bool __mmradio_set_state(mm_radio_t *radio, int new_state);
123 void _mmradio_seek_cancel(mm_radio_t *radio);
124 static void __mmradio_seek_thread(mm_radio_t *radio);
125 static void __mmradio_scan_thread(mm_radio_t *radio);
126 static bool __is_tunable_frequency(mm_radio_t *radio, int freq);
128 #ifdef TIZEN_FEATURE_SOUND_FOCUS
129 static void __mmradio_sound_focus_cb(int id, mm_sound_focus_type_e focus_type,
130 mm_sound_focus_state_e focus_state, const char *reason_for_change, int option,
131 const char *additional_info, void *user_data);
132 static void __mmradio_sound_focus_watch_cb(int id, mm_sound_focus_type_e focus_type,
133 mm_sound_focus_state_e focus_state, const char *reason_for_change,
134 const char *additional_info, void *user_data);
137 static void __mmradio_volume_changed_cb(volume_type_t type, unsigned int volume, void *user_data);
138 static int __mmradio_set_media_volume(mm_radio_t *radio, unsigned int level);
140 int _mmradio_apply_region(mm_radio_t *radio, MMRadioRegionType region, bool update)
142 int ret = MM_ERROR_NONE;
146 MMRADIO_LOG_FENTER();
148 MMRADIO_CHECK_INSTANCE(radio);
149 MMRADIO_CHECK_STATE_RETURN_IF_FAIL(radio, MMRADIO_COMMAND_SET_REGION);
151 /* if needed, radio region must be updated.
152 * Otherwise, just applying settings to device without it.
155 count = ARRAY_SIZE(region_table);
157 /* TODO: if auto is supported...get the region info. here */
159 /* update radio region settings */
160 for (index = 0; index < count; index++) {
161 /* find the region from pre-defined table */
162 if (region_table[index].country == region) {
163 radio->region_setting.country = region_table[index].country;
164 radio->region_setting.deemphasis = region_table[index].deemphasis;
165 radio->region_setting.band_min = region_table[index].band_min;
166 radio->region_setting.band_max = region_table[index].band_max;
167 radio->region_setting.channel_spacing = region_table[index].channel_spacing;
172 MMRADIO_LOG_DEBUG("setting region - country: %d, de-emphasis: %d, band range: %d ~ %d KHz\n",
173 radio->region_setting.country, radio->region_setting.deemphasis,
174 radio->region_setting.band_min, radio->region_setting.band_max);
176 MMRADIO_LOG_FLEAVE();
181 int _mmradio_create_radio(mm_radio_t *radio)
183 int ret = MM_ERROR_NONE;
185 MMRADIO_LOG_FENTER();
187 MMRADIO_CHECK_INSTANCE(radio);
188 MMRADIO_CHECK_STATE_RETURN_IF_FAIL(radio, MMRADIO_COMMAND_CREATE);
190 /* set default value */
191 radio->freq = DEFAULT_FREQ;
192 #ifdef TIZEN_FEATURE_SOUND_FOCUS
193 memset(&radio->sound_focus, 0, sizeof(mm_radio_sound_focus));
195 memset(&radio->region_setting, 0, sizeof(MMRadioRegion_t));
196 radio->local_volume = 1.0;
198 /* create command lock */
199 ret = pthread_mutex_init(&radio->cmd_lock, NULL);
201 MMRADIO_LOG_ERROR("mutex creation failed\n");
202 return MM_ERROR_RADIO_INTERNAL;
205 ret = pthread_mutex_init(&radio->volume_lock, NULL);
207 MMRADIO_LOG_ERROR("volume mutex creation failed\n");
208 return MM_ERROR_RADIO_INTERNAL;
211 MMRADIO_SET_STATE(radio, MM_RADIO_STATE_NULL);
213 #ifdef TIZEN_FEATURE_SOUND_FOCUS
214 ret = mmradio_sound_focus_register(&radio->sound_focus,
215 (mm_sound_focus_changed_cb)__mmradio_sound_focus_cb,
216 (mm_sound_focus_changed_watch_cb)__mmradio_sound_focus_watch_cb,
220 /* NOTE : we are dealing it as an error since we cannot expect it's behavior */
221 MMRADIO_LOG_ERROR("mmradio_audio_focus_register is failed\n");
222 return MM_ERROR_RADIO_INTERNAL;
226 ret = radio_hal_interface_init(&(radio->hal_inf));
228 MMRADIO_LOG_ERROR("mmradio hal interface init failed\n");
232 MMRADIO_LOG_FLEAVE();
234 return MM_ERROR_NONE;
237 int _mmradio_realize(mm_radio_t *radio)
239 int ret = MM_ERROR_NONE;
243 MMRadioRegionType region = MM_RADIO_REGION_GROUP_NONE;
245 MMRADIO_LOG_FENTER();
247 MMRADIO_CHECK_INSTANCE(radio);
248 MMRADIO_CHECK_STATE_RETURN_IF_FAIL(radio, MMRADIO_COMMAND_REALIZE);
250 ret = pthread_mutex_init(&radio->seek_cancel_mutex, NULL);
252 MMRADIO_LOG_DEBUG("Mutex creation failed %d", ret);
253 return MM_ERROR_RADIO_INTERNAL;
256 if (radio->region_setting.country == MM_RADIO_REGION_GROUP_NONE) {
257 /* not initialized yet. set it with default region */
258 region = RADIO_DEFAULT_REGION;
261 /* already initialized by application */
262 region = radio->region_setting.country;
265 ret = _mmradio_apply_region(radio, region, update);
267 #ifdef TIZEN_FEATURE_SOUND_VSTREAM
268 ret = sound_manager_create_stream_information_internal(SOUND_STREAM_TYPE_RADIO, NULL, radio, &radio->stream_info);
269 if (ret != MM_ERROR_NONE) {
270 MMRADIO_LOG_ERROR("sound_manager_create_stream_information_internal error");
271 MMRADIO_LOG_FLEAVE();
274 ret = sound_manager_create_virtual_stream(radio->stream_info, &radio->vstream);
275 if (ret != MM_ERROR_NONE) {
276 MMRADIO_LOG_ERROR("sound_manager_create_virtual_stream error");
277 MMRADIO_LOG_FLEAVE();
282 ret = sound_manager_get_max_volume(SOUND_TYPE_MEDIA, &max);
283 if (ret != MM_ERROR_NONE) {
284 MMRADIO_LOG_WARNING("sound_manager_get_max_volume error");
285 radio->max_media_volume = DEFAULT_MAX_MEDIA_VOLUME;
287 radio->max_media_volume = max;
290 ret = mm_sound_volume_get_value(VOLUME_TYPE_MEDIA, &volume);
292 if (ret != MM_ERROR_NONE)
293 MMRADIO_LOG_WARNING("failed to get MEDIA_VOLUME");
295 MMRADIO_VOLUME_LOCK(radio);
296 radio->media_volume = volume;
297 MMRADIO_VOLUME_UNLOCK(radio);
299 ret = mm_sound_add_volume_changed_callback(__mmradio_volume_changed_cb, (void *)radio, &radio->volume_subs_id);
300 if (ret != MM_ERROR_NONE)
301 MMRADIO_LOG_WARNING("mm_sound_add_volume_changed_callback error");
303 MMRADIO_SET_STATE(radio, MM_RADIO_STATE_READY);
305 MMRADIO_LOG_FLEAVE();
307 return MM_ERROR_NONE;
310 int _mmradio_unrealize(mm_radio_t *radio)
312 int ret = MM_ERROR_NONE;
314 MMRADIO_LOG_FENTER();
316 MMRADIO_CHECK_INSTANCE(radio);
317 MMRADIO_CHECK_STATE_RETURN_IF_FAIL(radio, MMRADIO_COMMAND_UNREALIZE);
319 ret = mm_sound_remove_volume_changed_callback(radio->volume_subs_id);
320 if (ret != MM_ERROR_NONE)
321 MMRADIO_LOG_WARNING("mm_sound_remove_volume_changed_callback error");
323 /*Finish if there are scans*/
324 _mmradio_stop_scan(radio);
326 /*Stop radio if started*/
327 _mmradio_stop(radio);
329 /* close radio device here !!!! */
330 radio_hal_close(radio->hal_inf);
331 radio_hal_unprepare(radio->hal_inf);
332 #ifdef TIZEN_FEATURE_SOUND_VSTREAM
333 sound_manager_destroy_virtual_stream(radio->vstream);
334 sound_manager_destroy_stream_information(radio->stream_info);
336 pthread_mutex_destroy(&radio->seek_cancel_mutex);
338 MMRADIO_SET_STATE(radio, MM_RADIO_STATE_NULL);
340 MMRADIO_LOG_FLEAVE();
345 int _mmradio_destroy(mm_radio_t *radio)
347 int ret = MM_ERROR_NONE;
348 MMRADIO_LOG_FENTER();
350 MMRADIO_CHECK_INSTANCE(radio);
351 MMRADIO_CHECK_STATE_RETURN_IF_FAIL(radio, MMRADIO_COMMAND_DESTROY);
353 _mmradio_unrealize(radio);
355 ret = radio_hal_interface_deinit(radio->hal_inf);
357 MMRADIO_LOG_ERROR("mmradio hal interface deinit failed\n");
360 #ifdef TIZEN_FEATURE_SOUND_FOCUS
361 ret = mmradio_sound_focus_deregister(&radio->sound_focus);
363 MMRADIO_LOG_ERROR("failed to deregister sound focus\n");
364 return MM_ERROR_RADIO_INTERNAL;
367 MMRADIO_LOG_FLEAVE();
369 return MM_ERROR_NONE;
372 /* unit should be KHz */
373 int _mmradio_set_frequency(mm_radio_t *radio, int freq)
375 int ret = MM_ERROR_NONE;
377 MMRADIO_LOG_FENTER();
379 MMRADIO_CHECK_INSTANCE(radio);
380 MMRADIO_CHECK_STATE_RETURN_IF_FAIL(radio, MMRADIO_COMMAND_SET_FREQ);
382 MMRADIO_LOG_DEBUG("Setting %d frequency\n", freq);
386 ret = radio_hal_set_frequency(radio->hal_inf, freq);
387 if (ret != MM_ERROR_NONE) {
388 MMRADIO_LOG_ERROR("radio_hal_set_frequency error");
389 MMRADIO_LOG_FLEAVE();
393 MMRADIO_LOG_FLEAVE();
399 int _mmradio_get_frequency(mm_radio_t *radio, int *pFreq)
401 int ret = MM_ERROR_NONE;
403 MMRADIO_LOG_FENTER();
405 MMRADIO_CHECK_INSTANCE(radio);
406 MMRADIO_CHECK_STATE_RETURN_IF_FAIL(radio, MMRADIO_COMMAND_GET_FREQ);
408 return_val_if_fail(pFreq, MM_ERROR_INVALID_ARGUMENT);
410 ret = radio_hal_get_frequency(radio->hal_inf, &freq);
411 if (ret != MM_ERROR_NONE) {
412 MMRADIO_LOG_ERROR("radio_hal_get_frequency error");
417 /* update freq in handle */
420 *pFreq = (int)radio->freq;
422 MMRADIO_LOG_FLEAVE();
427 int _mmradio_mute(mm_radio_t *radio)
429 int ret = MM_ERROR_NONE;
430 MMRADIO_LOG_FENTER();
432 MMRADIO_CHECK_INSTANCE(radio);
433 MMRADIO_CHECK_STATE_RETURN_IF_FAIL(radio, MMRADIO_COMMAND_MUTE);
435 ret = radio_hal_mute(radio->hal_inf);
436 if (ret == MM_ERROR_NOT_SUPPORT_API) {
437 MMRADIO_LOG_WARNING("radio_hal_mute is not supported");
438 } else if (ret != MM_ERROR_NONE) {
439 MMRADIO_LOG_ERROR("radio_hal_mute error");
440 MMRADIO_LOG_FLEAVE();
444 radio->is_muted = TRUE;
445 MMRADIO_LOG_FLEAVE();
450 int _mmradio_unmute(mm_radio_t *radio)
452 int ret = MM_ERROR_NONE;
453 MMRADIO_LOG_FENTER();
455 MMRADIO_CHECK_INSTANCE(radio);
456 MMRADIO_CHECK_STATE_RETURN_IF_FAIL(radio, MMRADIO_COMMAND_UNMUTE);
458 ret = radio_hal_unmute(radio->hal_inf);
459 if (ret == MM_ERROR_NOT_SUPPORT_API) {
460 MMRADIO_LOG_WARNING("radio_hal_unmute is not supported");
461 } else if (ret != MM_ERROR_NONE) {
462 MMRADIO_LOG_ERROR("radio_hal_unmute error");
463 MMRADIO_LOG_FLEAVE();
467 radio->is_muted = FALSE;
469 MMRADIO_LOG_FLEAVE();
474 int _mmradio_set_message_callback(mm_radio_t *radio, MMMessageCallback callback, void *user_param)
476 MMRADIO_LOG_FENTER();
478 MMRADIO_CHECK_INSTANCE(radio);
480 radio->msg_cb = callback;
481 radio->msg_cb_param = user_param;
483 MMRADIO_LOG_DEBUG("msg_cb : 0x%x msg_cb_param : 0x%x\n", callback, user_param);
485 MMRADIO_LOG_FLEAVE();
487 return MM_ERROR_NONE;
490 int _mmradio_get_state(mm_radio_t *radio, int *pState)
494 MMRADIO_LOG_FENTER();
496 MMRADIO_CHECK_INSTANCE(radio);
497 return_val_if_fail(pState, MM_ERROR_INVALID_ARGUMENT);
499 state = __mmradio_get_state(radio);
503 MMRADIO_LOG_FLEAVE();
505 return MM_ERROR_NONE;
508 int _mmradio_start(mm_radio_t *radio)
510 int ret = MM_ERROR_NONE;
511 unsigned int volume = 0;
513 MMRADIO_LOG_FENTER();
515 MMRADIO_CHECK_INSTANCE(radio);
516 MMRADIO_CHECK_STATE_RETURN_IF_FAIL(radio, MMRADIO_COMMAND_START);
518 MMRADIO_LOG_DEBUG("now tune to frequency : %d\n", radio->freq);
520 #ifdef TIZEN_FEATURE_SOUND_FOCUS
521 if (radio->sound_focus.handle > 0) {
522 ret = mmradio_acquire_sound_focus(&radio->sound_focus);
523 if (ret != MM_ERROR_NONE) {
524 MMRADIO_LOG_ERROR("failed to set sound focus");
530 if (!radio->is_ready) {
531 ret = radio_hal_prepare(radio->hal_inf);
532 if (ret == MM_ERROR_NOT_SUPPORT_API) {
533 MMRADIO_LOG_WARNING("radio_hal_prepare is not supported");
534 } else if (ret != MM_ERROR_NONE) {
535 MMRADIO_LOG_ERROR("radio_hal_prepare_device error");
539 ret = radio_hal_open(radio->hal_inf);
540 if (ret == MM_ERROR_NOT_SUPPORT_API) {
541 MMRADIO_LOG_WARNING("radio_hal_open is not supported");
542 } else if (ret != MM_ERROR_NONE) {
543 MMRADIO_LOG_ERROR("radio_hal_init error");
546 radio->is_ready = TRUE;
548 MMRADIO_LOG_DEBUG("radio prepared and opened");
551 ret = mm_sound_volume_get_value(VOLUME_TYPE_MEDIA, &volume);
552 if (ret != MM_ERROR_NONE)
553 MMRADIO_LOG_WARNING("failed to get MEDIA_VOLUME");
555 ret = __mmradio_set_media_volume(radio, volume);
556 if (ret != MM_ERROR_NONE) {
557 MMRADIO_LOG_ERROR("failed to media volume");
561 ret = radio_hal_start(radio->hal_inf);
562 if (ret == MM_ERROR_NOT_SUPPORT_API) {
563 MMRADIO_LOG_WARNING("radio_hal_start is not supported");
565 MMRADIO_LOG_ERROR("failed to radio_hal_start\n");
569 /* set stored frequency */
570 ret = radio_hal_set_frequency(radio->hal_inf, radio->freq);
572 MMRADIO_LOG_ERROR("failed to radio_hal_set_frequency\n");
576 #ifdef TIZEN_FEATURE_SOUND_VSTREAM
577 ret = sound_manager_start_virtual_stream(radio->vstream);
579 MMRADIO_LOG_ERROR("failed to sound_manager_start_virtual_stream\n");
584 MMRADIO_SET_STATE(radio, MM_RADIO_STATE_PLAYING);
586 MMRADIO_LOG_FLEAVE();
588 return MM_ERROR_NONE;
590 #ifdef TIZEN_FEATURE_SOUND_VSTREAM
592 sound_manager_stop_virtual_stream(radio->vstream);
595 radio_hal_close(radio->hal_inf);
597 radio_hal_unprepare(radio->hal_inf);
598 radio->is_ready = FALSE;
602 int _mmradio_stop(mm_radio_t *radio)
604 int ret = MM_ERROR_NONE;
606 MMRADIO_LOG_FENTER();
608 MMRADIO_CHECK_INSTANCE(radio);
609 MMRADIO_CHECK_STATE_RETURN_IF_FAIL(radio, MMRADIO_COMMAND_STOP);
611 radio->seek_unmute = FALSE;
612 /*cancel if any seek*/
613 _mmradio_seek_cancel(radio);
614 #ifdef TIZEN_FEATURE_SOUND_VSTREAM
615 ret = sound_manager_stop_virtual_stream(radio->vstream);
616 if (ret != MM_ERROR_NONE) {
617 MMRADIO_LOG_ERROR("failed to stop virtual_stream");
622 ret = radio_hal_stop(radio->hal_inf);
623 if (ret == MM_ERROR_NOT_SUPPORT_API) {
624 MMRADIO_LOG_WARNING("radio_hal_unmute is not supported");
626 MMRADIO_LOG_ERROR("failed to radio_hal_stop\n");
630 /* close radio device here !!!! */
631 ret = radio_hal_close(radio->hal_inf);
632 if (ret == MM_ERROR_NOT_SUPPORT_API) {
633 MMRADIO_LOG_WARNING("radio_hal_close is not supported");
634 } else if (ret != MM_ERROR_NONE) {
635 MMRADIO_LOG_ERROR("radio_hal_close_device error");
639 ret = radio_hal_unprepare(radio->hal_inf);
640 if (ret == MM_ERROR_NOT_SUPPORT_API) {
641 MMRADIO_LOG_WARNING("radio_hal_unprepare is not supported");
642 } else if (ret != MM_ERROR_NONE) {
643 MMRADIO_LOG_ERROR("radio_hal_close_device error");
647 radio->is_ready = FALSE;
649 #ifdef TIZEN_FEATURE_SOUND_FOCUS
650 if (radio->sound_focus.handle > 0) {
651 ret = mmradio_release_sound_focus(&radio->sound_focus);
653 MMRADIO_LOG_ERROR("mmradio_release_audio_focus is failed\n");
658 MMRADIO_SET_STATE(radio, MM_RADIO_STATE_READY);
660 MMRADIO_LOG_FLEAVE();
662 return MM_ERROR_NONE;
665 int _mmradio_seek(mm_radio_t *radio, MMRadioSeekDirectionType direction)
667 int ret = MM_ERROR_NONE;
669 MMRADIO_LOG_FENTER();
671 MMRADIO_CHECK_INSTANCE(radio);
672 MMRADIO_CHECK_STATE_RETURN_IF_FAIL(radio, MMRADIO_COMMAND_SEEK);
674 if (radio->is_seeking) {
675 MMRADIO_LOG_ERROR("[RADIO_ERROR_INVALID_OPERATION]radio is seeking, can't serve another request try again");
676 return MM_ERROR_RADIO_INTERNAL;
679 radio->seek_unmute = FALSE;
680 radio->is_seeking = TRUE;
681 radio->seek_cancel = FALSE;
683 if (!radio->is_muted) {
684 ret = radio_hal_mute(radio->hal_inf);
685 if (ret == MM_ERROR_NOT_SUPPORT_API) {
686 MMRADIO_LOG_WARNING("radio_hal_mute is not supported");
688 MMRADIO_LOG_ERROR("failed to radio_hal_mute\n");
691 radio->seek_unmute = TRUE;
694 MMRADIO_LOG_DEBUG("trying to seek. direction[0:UP/1:DOWN) %d\n", direction);
695 radio->seek_direction = direction;
697 ret = pthread_create(&radio->seek_thread, NULL, (void *)__mmradio_seek_thread, (void *)radio);
700 MMRADIO_LOG_DEBUG("failed create thread\n");
701 radio->is_seeking = FALSE;
702 radio->seek_cancel = TRUE;
703 if (radio->seek_unmute) {
704 ret = radio_hal_mute(radio->hal_inf);
705 if (ret == MM_ERROR_NOT_SUPPORT_API) {
706 MMRADIO_LOG_WARNING("radio_hal_mute is not supported");
708 MMRADIO_LOG_ERROR("failed to radio_hal_mute\n");
709 radio->seek_unmute = FALSE;
713 return MM_ERROR_RADIO_INTERNAL;
716 MMRADIO_LOG_FLEAVE();
718 return MM_ERROR_NONE;
721 void _mmradio_seek_cancel(mm_radio_t *radio)
723 int ret = MM_ERROR_NONE;
724 char str_error[READ_MAX_BUFFER_SIZE];
725 MMRADIO_LOG_FENTER();
727 MMRADIO_CHECK_INSTANCE_RETURN_VOID(radio);
729 /*cancel any outstanding seek request*/
730 radio->seek_cancel = TRUE;
731 if (radio->seek_thread) {
732 ret = pthread_mutex_trylock(&radio->seek_cancel_mutex);
733 MMRADIO_LOG_DEBUG("try lock ret: %s (%d)", strerror_r(ret, str_error, sizeof(str_error)), ret);
734 if (ret == EBUSY) { /* it was already locked by other */
735 MMRADIO_LOG_DEBUG("send SEEK ABORT with FMRX_PROPERTY_SEARCH_ABORT");
736 } else if (ret == 0) {
737 MMRADIO_LOG_DEBUG("trylock is successful. unlock now");
738 pthread_mutex_unlock(&radio->seek_cancel_mutex);
740 MMRADIO_LOG_ERROR("trylock is failed but Not EBUSY. ret: %d", ret);
742 MMRADIO_LOG_DEBUG("pthread_join seek_thread");
743 pthread_join(radio->seek_thread, NULL);
744 MMRADIO_LOG_DEBUG("done");
745 radio->is_seeking = FALSE;
746 radio->seek_thread = 0;
748 MMRADIO_LOG_FLEAVE();
752 int _mmradio_start_scan(mm_radio_t *radio)
754 int ret = MM_ERROR_NONE;
756 MMRADIO_LOG_FENTER();
758 MMRADIO_CHECK_INSTANCE(radio);
759 MMRADIO_CHECK_STATE_RETURN_IF_FAIL(radio, MMRADIO_COMMAND_START_SCAN);
763 radio->stop_scan = false;
765 if (!radio->is_ready) {
766 ret = radio_hal_prepare(radio->hal_inf);
767 if (ret == MM_ERROR_NOT_SUPPORT_API) {
768 MMRADIO_LOG_WARNING("radio_hal_prepare is not supported");
769 } else if (ret != MM_ERROR_NONE) {
770 MMRADIO_LOG_ERROR("radio_hal_prepare_device error");
774 ret = radio_hal_open(radio->hal_inf);
775 if (ret == MM_ERROR_NOT_SUPPORT_API) {
776 MMRADIO_LOG_WARNING("radio_hal_open is not supported");
777 } else if (ret != MM_ERROR_NONE) {
778 MMRADIO_LOG_ERROR("radio_hal_init error");
779 MMRADIO_LOG_FLEAVE();
782 radio->is_ready = TRUE;
784 MMRADIO_LOG_DEBUG("radio prepared and opened");
787 scan_tr_id = pthread_create(&radio->scan_thread, NULL, (void *)__mmradio_scan_thread, (void *)radio);
789 if (scan_tr_id != 0) {
790 MMRADIO_LOG_DEBUG("failed to create thread : scan\n");
791 return MM_ERROR_RADIO_NOT_INITIALIZED;
794 MMRADIO_SET_STATE(radio, MM_RADIO_STATE_SCANNING);
796 MMRADIO_LOG_FLEAVE();
798 return MM_ERROR_NONE;
801 int _mmradio_stop_scan(mm_radio_t *radio)
804 char str_error[READ_MAX_BUFFER_SIZE];
805 MMRADIO_LOG_FENTER();
807 MMRADIO_CHECK_INSTANCE(radio);
808 MMRADIO_CHECK_STATE_RETURN_IF_FAIL(radio, MMRADIO_COMMAND_STOP_SCAN);
810 radio->stop_scan = true;
812 if (radio->scan_thread > 0) {
813 /* make sure all the search is stopped else we'll wait till search finish which is not ideal*/
814 ret = pthread_mutex_trylock(&radio->seek_cancel_mutex);
815 MMRADIO_LOG_DEBUG("try lock ret: %s (%d)", strerror_r(ret, str_error, sizeof(str_error)), ret);
816 if (ret == EBUSY) { /* it was already locked by other */
817 MMRADIO_LOG_DEBUG("send SEEK ABORT with FMRX_PROPERTY_SEARCH_ABORT");
818 } else if (ret == 0) {
819 MMRADIO_LOG_DEBUG("trylock is successful. unlock now");
820 pthread_mutex_unlock(&radio->seek_cancel_mutex);
822 MMRADIO_LOG_ERROR("trylock is failed but Not EBUSY. ret: %d", ret);
824 MMRADIO_LOG_DEBUG("pthread_join scan_thread");
825 pthread_join(radio->scan_thread, NULL);
826 radio->scan_thread = 0;
829 MMRADIO_LOG_FLEAVE();
831 return MM_ERROR_NONE;
834 int _mm_radio_get_signal_strength(mm_radio_t *radio, int *value)
836 int ret = MM_ERROR_NONE;
837 uint32_t strength = 0;
838 MMRADIO_LOG_FENTER();
839 MMRADIO_CHECK_INSTANCE(radio);
841 return_val_if_fail(value, MM_ERROR_INVALID_ARGUMENT);
843 /* just return stored frequency if radio device is not ready */
844 ret = radio_hal_get_signal_strength(radio->hal_inf, &strength);
845 if (ret == MM_ERROR_NOT_SUPPORT_API) {
846 MMRADIO_LOG_WARNING("radio_hal_unmute is not supported");
847 } else if (ret != MM_ERROR_NONE) {
848 debug_error("radio_hal_get_signal_strength error\n");
850 MMRADIO_LOG_FLEAVE();
853 *value = (int)strength;
854 MMRADIO_LOG_FLEAVE();
855 return MM_ERROR_NONE;
858 void __mmradio_scan_thread(mm_radio_t *radio)
860 int ret = MM_ERROR_NONE;
863 MMRADIO_LOG_FENTER();
864 MMRADIO_CHECK_INSTANCE_RETURN_VOID(radio);
866 ret = radio_hal_mute(radio->hal_inf);
868 if (ret == MM_ERROR_NOT_SUPPORT_API) {
869 MMRADIO_LOG_WARNING("radio_hal_mute is not supported");
870 } else if (ret != MM_ERROR_NONE) {
871 MMRADIO_LOG_ERROR("radio_hal_mute error");
874 ret = radio_hal_set_frequency(radio->hal_inf, radio->region_setting.band_min);
876 if (ret != MM_ERROR_NONE)
879 MMRADIO_POST_MSG(radio, MM_MESSAGE_RADIO_SCAN_START, NULL);
880 MMRADIO_SET_STATE(radio, MM_RADIO_STATE_SCANNING);
882 while (!radio->stop_scan) {
884 MMMessageParamType param = { 0, };
886 MMRADIO_LOG_DEBUG("scanning....\n");
888 pthread_mutex_lock(&radio->seek_cancel_mutex);
890 if (radio->stop_scan) {
891 MMRADIO_LOG_DEBUG("scan was canceled");
892 pthread_mutex_unlock(&radio->seek_cancel_mutex);
896 ret = radio_hal_seek(radio->hal_inf, MM_RADIO_SEEK_UP);
898 pthread_mutex_unlock(&radio->seek_cancel_mutex);
900 if (ret != MM_ERROR_NONE) {
901 MMRADIO_LOG_ERROR("radio scanning error");
905 /* now we can get new frequency from radio device */
906 if (radio->stop_scan)
909 ret = radio_hal_get_frequency(radio->hal_inf, &freq);
910 if (ret != MM_ERROR_NONE) {
911 MMRADIO_LOG_ERROR("failed to get current frequency\n");
913 if (freq < prev_freq) {
914 MMRADIO_LOG_DEBUG("scanning wrapped around. stopping scan\n");
918 if (freq == prev_freq)
921 prev_freq = param.radio_scan.frequency = (int)freq;
922 MMRADIO_LOG_DEBUG("scanning : new frequency : [%d]\n", param.radio_scan.frequency);
924 /* drop if max freq is scanned */
925 if (param.radio_scan.frequency == radio->region_setting.band_max) {
926 MMRADIO_LOG_DEBUG("%d freq is dropping...and stopping scan\n", param.radio_scan.frequency);
930 if (radio->stop_scan) {
931 /* doesn't need to post */
935 MMRADIO_POST_MSG(radio, MM_MESSAGE_RADIO_SCAN_INFO, ¶m);
939 if (radio->old_state == MM_RADIO_STATE_READY) {
940 MMRADIO_LOG_DEBUG("old state is ready");
941 } else if (radio->old_state == MM_RADIO_STATE_PLAYING) {
942 MMRADIO_LOG_DEBUG("old state is playing");
943 ret = radio_hal_unmute(radio->hal_inf);
944 if (ret == MM_ERROR_NOT_SUPPORT_API) {
945 MMRADIO_LOG_WARNING("radio_hal_unmute is not supported");
946 } else if (ret != MM_ERROR_NONE) {
947 MMRADIO_LOG_ERROR("radio_hal_unmute error");
950 ret = radio_hal_set_frequency(radio->hal_inf, prev_freq);
951 if (ret == MM_ERROR_NOT_SUPPORT_API) {
952 MMRADIO_LOG_WARNING("radio_hal_set_frequency is not supported");
953 } else if (ret != MM_ERROR_NONE) {
954 MMRADIO_LOG_ERROR("radio_hal_set_frequency error");
961 radio->scan_thread = 0;
963 if (radio->old_state == MM_RADIO_STATE_PLAYING) {
964 MMRADIO_SET_STATE(radio, MM_RADIO_STATE_PLAYING);
966 /* close radio device here !!!! */
967 ret = radio_hal_close(radio->hal_inf);
968 if (ret == MM_ERROR_NOT_SUPPORT_API)
969 MMRADIO_LOG_WARNING("radio_hal_close is not supported");
970 else if (ret != MM_ERROR_NONE)
971 MMRADIO_LOG_ERROR("radio_hal_close_device error");
973 ret = radio_hal_unprepare(radio->hal_inf);
974 if (ret == MM_ERROR_NOT_SUPPORT_API)
975 MMRADIO_LOG_WARNING("radio_hal_unprepare is not supported");
976 else if (ret != MM_ERROR_NONE)
977 MMRADIO_LOG_ERROR("radio_hal_close_device error");
979 radio->is_ready = FALSE;
981 MMRADIO_SET_STATE(radio, MM_RADIO_STATE_READY);
984 if (!radio->stop_scan)
985 MMRADIO_POST_MSG(radio, MM_MESSAGE_RADIO_SCAN_FINISH, NULL);
987 MMRADIO_POST_MSG(radio, MM_MESSAGE_RADIO_SCAN_STOP, NULL);
989 MMRADIO_LOG_FLEAVE();
996 bool __is_tunable_frequency(mm_radio_t *radio, int freq)
998 MMRADIO_LOG_FENTER();
1000 MMRADIO_CHECK_INSTANCE(radio);
1002 if (freq >= radio->region_setting.band_max
1003 || freq <= radio->region_setting.band_min)
1006 MMRADIO_LOG_FLEAVE();
1011 void __mmradio_seek_thread(mm_radio_t *radio)
1013 int ret = MM_ERROR_NONE;
1015 MMMessageParamType param = {0, };
1017 MMRADIO_LOG_FENTER();
1018 MMRADIO_CHECK_INSTANCE_RETURN_VOID(radio);
1020 MMRADIO_POST_MSG(radio, MM_MESSAGE_RADIO_SEEK_START, NULL);
1022 MMRADIO_LOG_DEBUG("seeking....\n");
1024 if (!radio->seek_cancel) {
1026 MMRADIO_LOG_DEBUG("try to seek ");
1027 pthread_mutex_lock(&radio->seek_cancel_mutex);
1028 MMRADIO_LOG_DEBUG("seek start\n");
1030 if (radio->seek_cancel) {
1031 MMRADIO_LOG_DEBUG("seek was canceled so we return failure to application");
1032 pthread_mutex_unlock(&radio->seek_cancel_mutex);
1036 ret = radio_hal_seek(radio->hal_inf, radio->seek_direction);
1037 pthread_mutex_unlock(&radio->seek_cancel_mutex);
1039 MMRADIO_LOG_ERROR("radio_hal_seek failed\n");
1043 /* now we can get new frequency from radio device */
1044 ret = radio_hal_get_frequency(radio->hal_inf, &freq);
1046 MMRADIO_LOG_ERROR("failed to get current frequency\n");
1050 MMRADIO_LOG_DEBUG("found frequency\n");
1052 /* if same freq is found, ignore it and search next one. */
1053 if (freq == radio->prev_seek_freq) {
1054 MMRADIO_LOG_DEBUG("It's same with previous found one. So, trying next one. \n");
1058 /* check if it's limit freq or not */
1059 if (__is_tunable_frequency(radio, freq)) {
1060 /* now tune to new frequency */
1061 ret = radio_hal_set_frequency(radio->hal_inf, freq);
1063 MMRADIO_LOG_ERROR("failed to tune to new frequency\n");
1068 if (radio->seek_unmute) {
1069 /* now turn on radio
1070 * In the case of limit freq, tuner should be unmuted.
1071 * Otherwise, sound can't output even though application set new frequency.
1073 ret = radio_hal_unmute(radio->hal_inf);
1075 MMRADIO_LOG_ERROR("failed to tune to new frequency\n");
1078 radio->seek_unmute = FALSE;
1081 param.radio_scan.frequency = radio->prev_seek_freq = (int)freq;
1082 MMRADIO_LOG_DEBUG("seeking : new frequency : [%d]\n", param.radio_scan.frequency);
1083 MMRADIO_POST_MSG(radio, MM_MESSAGE_RADIO_SEEK_FINISH, ¶m);
1086 radio->seek_thread = 0;
1087 radio->is_seeking = FALSE;
1089 MMRADIO_LOG_FLEAVE();
1096 if (radio->seek_unmute) {
1097 /* now turn on radio
1098 * In the case of limit freq, tuner should be unmuted.
1099 * Otherwise, sound can't output even though application set new frequency.
1101 ret = radio_hal_unmute(radio->hal_inf);
1103 MMRADIO_LOG_ERROR("failed to tune to new frequency\n");
1104 radio->seek_unmute = FALSE;
1106 /* freq -1 means it's failed to seek */
1107 param.radio_scan.frequency = -1;
1108 MMRADIO_POST_MSG(radio, MM_MESSAGE_RADIO_SEEK_FINISH, ¶m);
1109 radio->is_seeking = FALSE;
1114 static bool __mmradio_post_message(mm_radio_t *radio, enum MMMessageType msgtype, MMMessageParamType *param)
1116 MMRADIO_CHECK_INSTANCE(radio);
1118 MMRADIO_LOG_FENTER();
1120 if (!radio->msg_cb) {
1121 debug_warning("failed to post a message\n");
1125 MMRADIO_LOG_DEBUG("address of msg_cb : %p\n", radio->msg_cb);
1127 radio->msg_cb(msgtype, param, radio->msg_cb_param);
1129 MMRADIO_LOG_FLEAVE();
1134 static int __mmradio_check_state(mm_radio_t *radio, MMRadioCommand command)
1136 MMRadioStateType radio_state = MM_RADIO_STATE_NUM;
1138 MMRADIO_LOG_FENTER();
1140 MMRADIO_CHECK_INSTANCE(radio);
1142 radio_state = __mmradio_get_state(radio);
1144 MMRADIO_LOG_DEBUG("incomming command : %d current state : %d\n", command, radio_state);
1147 case MMRADIO_COMMAND_CREATE:
1149 if (radio_state != 0)
1154 case MMRADIO_COMMAND_REALIZE:
1156 if (radio_state == MM_RADIO_STATE_READY ||
1157 radio_state == MM_RADIO_STATE_PLAYING ||
1158 radio_state == MM_RADIO_STATE_SCANNING)
1161 if (radio_state == 0)
1166 case MMRADIO_COMMAND_UNREALIZE:
1168 if (radio_state == MM_RADIO_STATE_NULL)
1171 /* we can call unrealize at any higher state */
1175 case MMRADIO_COMMAND_START:
1177 if (radio_state == MM_RADIO_STATE_PLAYING)
1180 if (radio_state != MM_RADIO_STATE_READY)
1185 case MMRADIO_COMMAND_STOP:
1187 if (radio_state == MM_RADIO_STATE_READY)
1190 if (radio_state != MM_RADIO_STATE_PLAYING)
1195 case MMRADIO_COMMAND_START_SCAN:
1197 if (radio_state == MM_RADIO_STATE_SCANNING)
1200 if (radio_state == MM_RADIO_STATE_NULL)
1205 case MMRADIO_COMMAND_STOP_SCAN:
1207 if (radio_state == MM_RADIO_STATE_READY)
1210 if (radio_state != MM_RADIO_STATE_SCANNING)
1215 case MMRADIO_COMMAND_DESTROY:
1216 case MMRADIO_COMMAND_MUTE:
1217 case MMRADIO_COMMAND_UNMUTE:
1218 case MMRADIO_COMMAND_SET_FREQ:
1219 case MMRADIO_COMMAND_GET_FREQ:
1220 case MMRADIO_COMMAND_SET_REGION:
1221 case MMRADIO_COMMAND_SET_VOLUME:
1222 case MMRADIO_COMMAND_GET_VOLUME:
1224 /* we can do it at any state */
1228 case MMRADIO_COMMAND_SEEK:
1230 if (radio_state != MM_RADIO_STATE_PLAYING)
1235 case MMRADIO_COMMAND_GET_REGION:
1237 if (radio_state == MM_RADIO_STATE_NULL)
1243 MMRADIO_LOG_DEBUG("not handled in FSM. don't care it\n");
1247 MMRADIO_LOG_DEBUG("status OK\n");
1249 radio->cmd = command;
1251 MMRADIO_LOG_FLEAVE();
1253 return MM_ERROR_NONE;
1256 debug_warning("invalid state. current : %d command : %d\n", radio_state, command);
1257 MMRADIO_LOG_FLEAVE();
1258 return MM_ERROR_RADIO_INVALID_STATE;
1261 debug_warning("mm-radio is in the desired state(%d). doing noting\n", radio_state);
1262 MMRADIO_LOG_FLEAVE();
1263 return MM_ERROR_RADIO_NO_OP;
1267 static bool __mmradio_set_state(mm_radio_t *radio, int new_state)
1269 MMMessageParamType msg = { 0, };
1270 int msg_type = MM_MESSAGE_UNKNOWN;
1272 MMRADIO_LOG_FENTER();
1275 debug_warning("calling set_state with invalid radio handle\n");
1279 if (radio->current_state == new_state && radio->pending_state == 0) {
1280 debug_warning("we are in same state\n");
1285 radio->old_state = radio->current_state;
1286 radio->current_state = new_state;
1288 /* fill message param */
1289 msg.state.previous = radio->old_state;
1290 msg.state.current = radio->current_state;
1292 #ifdef TIZEN_FEATURE_SOUND_FOCUS
1293 /* post message to application */
1294 switch (radio->sound_focus.by_focus_cb) {
1295 case MMRADIO_FOCUS_CB_NONE:
1297 msg_type = MM_MESSAGE_STATE_CHANGED;
1298 MMRADIO_POST_MSG(radio, msg_type, &msg);
1302 case MMRADIO_FOCUS_CB_POSTMSG:
1304 msg_type = MM_MESSAGE_STATE_INTERRUPTED;
1305 msg.union_type = MM_MSG_UNION_CODE;
1306 msg.code = radio->sound_focus.event_src;
1307 MMRADIO_POST_MSG(radio, msg_type, &msg);
1311 case MMRADIO_FOCUS_CB_SKIP_POSTMSG:
1316 msg_type = MM_MESSAGE_STATE_CHANGED;
1317 MMRADIO_POST_MSG(radio, msg_type, &msg);
1320 MMRADIO_LOG_FLEAVE();
1325 static int __mmradio_get_state(mm_radio_t *radio)
1327 MMRADIO_CHECK_INSTANCE(radio);
1329 MMRADIO_LOG_DEBUG("radio state : current : [%d] old : [%d] pending : [%d]\n",
1330 radio->current_state, radio->old_state, radio->pending_state);
1332 return radio->current_state;
1335 #ifdef TIZEN_FEATURE_SOUND_FOCUS
1336 static void __mmradio_sound_focus_cb(int id, mm_sound_focus_type_e focus_type,
1337 mm_sound_focus_state_e focus_state, const char *reason_for_change, int option,
1338 const char *additional_info, void *user_data)
1340 mm_radio_t *radio = (mm_radio_t *)user_data;
1341 enum MMMessageInterruptedCode event_source;
1342 int result = MM_ERROR_NONE;
1343 int postMsg = false;
1345 MMRADIO_LOG_FENTER();
1346 MMRADIO_CHECK_INSTANCE_RETURN_VOID(radio);
1348 mmradio_get_sound_focus_reason(focus_state, reason_for_change, FALSE, &event_source, &postMsg);
1349 radio->sound_focus.event_src = event_source;
1351 switch (focus_state) {
1352 case FOCUS_IS_RELEASED:{
1353 radio->sound_focus.cur_focus_type &= ~focus_type;
1354 radio->sound_focus.by_focus_cb = MMRADIO_FOCUS_CB_POSTMSG;
1356 result = _mmradio_stop(radio);
1358 MMRADIO_LOG_ERROR("failed to stop radio\n");
1360 MMRADIO_LOG_DEBUG("FOCUS_IS_RELEASED cur_focus_type : %d\n", radio->sound_focus.cur_focus_type);
1364 case FOCUS_IS_ACQUIRED:{
1365 MMMessageParamType msg = { 0, };
1366 msg.union_type = MM_MSG_UNION_CODE;
1367 msg.code = event_source;
1369 radio->sound_focus.cur_focus_type |= focus_type;
1371 if ((postMsg) && (FOCUS_FOR_BOTH == radio->sound_focus.cur_focus_type))
1372 MMRADIO_POST_MSG(radio, MM_MESSAGE_READY_TO_RESUME, &msg);
1374 radio->sound_focus.by_focus_cb = MMRADIO_FOCUS_CB_NONE;
1376 MMRADIO_LOG_DEBUG("FOCUS_IS_ACQUIRED cur_focus_type : %d\n", radio->sound_focus.cur_focus_type);
1381 MMRADIO_LOG_DEBUG("Unknown focus_state\n");
1385 MMRADIO_LOG_FLEAVE();
1388 static void __mmradio_sound_focus_watch_cb(int id, mm_sound_focus_type_e focus_type,
1389 mm_sound_focus_state_e focus_state, const char *reason_for_change,
1390 const char *additional_info, void *user_data)
1392 mm_radio_t *radio = (mm_radio_t *)user_data;
1393 enum MMMessageInterruptedCode event_source;
1394 int result = MM_ERROR_NONE;
1395 int postMsg = false;
1397 MMRADIO_LOG_FENTER();
1398 MMRADIO_CHECK_INSTANCE_RETURN_VOID(radio);
1400 mmradio_get_sound_focus_reason(focus_state, reason_for_change, TRUE, &event_source, &postMsg);
1401 radio->sound_focus.event_src = event_source;
1403 switch (focus_state) {
1404 case FOCUS_IS_ACQUIRED: {
1405 radio->sound_focus.cur_focus_type &= ~focus_type;
1406 radio->sound_focus.by_focus_cb = MMRADIO_FOCUS_CB_POSTMSG;
1408 result = _mmradio_stop(radio);
1410 MMRADIO_LOG_ERROR("failed to stop radio\n");
1412 MMRADIO_LOG_DEBUG("FOCUS_IS_RELEASED cur_focus_type : %d\n", radio->sound_focus.cur_focus_type);
1416 case FOCUS_IS_RELEASED: {
1417 MMMessageParamType msg = { 0, };
1418 msg.union_type = MM_MSG_UNION_CODE;
1419 msg.code = event_source;
1421 radio->sound_focus.cur_focus_type |= focus_type;
1423 if ((postMsg) && (FOCUS_FOR_BOTH == radio->sound_focus.cur_focus_type))
1424 MMRADIO_POST_MSG(radio, MM_MESSAGE_READY_TO_RESUME, &msg);
1426 radio->sound_focus.by_focus_cb = MMRADIO_FOCUS_CB_NONE;
1428 MMRADIO_LOG_DEBUG("FOCUS_IS_ACQUIRED cur_focus_type : %d\n", radio->sound_focus.cur_focus_type);
1433 MMRADIO_LOG_DEBUG("Unknown focus_state\n");
1437 MMRADIO_LOG_FLEAVE();
1441 static void __mmradio_volume_changed_cb(volume_type_t type, unsigned int volume, void *user_data)
1443 mm_radio_t *radio = (mm_radio_t *)user_data;
1444 int ret = MM_ERROR_NONE;
1445 MMRADIO_CHECK_INSTANCE_RETURN_VOID(radio);
1446 if (type == VOLUME_TYPE_MEDIA) {
1447 MMRADIO_LOG_DEBUG("Change FM Radio volume to %d", volume);
1448 ret = __mmradio_set_media_volume(radio, volume);
1449 if (ret != MM_ERROR_NONE)
1450 MMRADIO_LOG_ERROR("__mmradio_set_media_volume error");
1455 int _mmradio_get_region_type(mm_radio_t *radio, MMRadioRegionType *type)
1457 MMRADIO_LOG_FENTER();
1458 MMRADIO_CHECK_INSTANCE(radio);
1459 MMRADIO_CHECK_STATE_RETURN_IF_FAIL(radio, MMRADIO_COMMAND_GET_REGION);
1461 return_val_if_fail(type, MM_ERROR_INVALID_ARGUMENT);
1463 *type = radio->region_setting.country;
1465 MMRADIO_LOG_FLEAVE();
1466 return MM_ERROR_NONE;
1469 int _mmradio_get_region_frequency_range(mm_radio_t *radio, unsigned int *min_freq, unsigned int *max_freq)
1471 MMRADIO_LOG_FENTER();
1472 MMRADIO_CHECK_INSTANCE(radio);
1473 MMRADIO_CHECK_STATE_RETURN_IF_FAIL(radio, MMRADIO_COMMAND_GET_REGION);
1475 return_val_if_fail(min_freq && max_freq, MM_ERROR_INVALID_ARGUMENT);
1477 *min_freq = radio->region_setting.band_min;
1478 *max_freq = radio->region_setting.band_max;
1480 MMRADIO_LOG_FLEAVE();
1481 return MM_ERROR_NONE;
1484 int _mmradio_get_channel_spacing(mm_radio_t *radio, unsigned int *ch_spacing)
1486 MMRADIO_LOG_FENTER();
1487 MMRADIO_CHECK_INSTANCE(radio);
1488 MMRADIO_CHECK_STATE_RETURN_IF_FAIL(radio, MMRADIO_COMMAND_GET_REGION);
1490 return_val_if_fail(ch_spacing, MM_ERROR_INVALID_ARGUMENT);
1492 *ch_spacing = radio->region_setting.channel_spacing;
1494 MMRADIO_LOG_FLEAVE();
1495 return MM_ERROR_NONE;
1498 int _mmradio_set_volume(mm_radio_t *radio, float volume)
1500 int ret = MM_ERROR_NONE;
1502 MMRADIO_LOG_FENTER();
1504 MMRADIO_CHECK_INSTANCE(radio);
1505 MMRADIO_CHECK_STATE_RETURN_IF_FAIL(radio, MMRADIO_COMMAND_SET_VOLUME);
1507 MMRADIO_LOG_DEBUG("Setting %f volume\n", volume);
1509 MMRADIO_VOLUME_LOCK(radio);
1510 radio->local_volume = volume;
1511 MMRADIO_VOLUME_UNLOCK(radio);
1513 ret = radio_hal_set_volume(radio->hal_inf, volume);
1514 if (ret != MM_ERROR_NONE)
1515 MMRADIO_LOG_ERROR("radio_hal_set_volume error");
1517 MMRADIO_LOG_FLEAVE();
1522 int _mmradio_get_volume(mm_radio_t *radio, float *pVolume)
1524 int ret = MM_ERROR_NONE;
1526 MMRADIO_LOG_FENTER();
1528 MMRADIO_CHECK_INSTANCE(radio);
1529 MMRADIO_CHECK_STATE_RETURN_IF_FAIL(radio, MMRADIO_COMMAND_GET_VOLUME);
1531 return_val_if_fail(pVolume, MM_ERROR_INVALID_ARGUMENT);
1533 ret = radio_hal_get_volume(radio->hal_inf, &volume);
1534 if (ret != MM_ERROR_NONE) {
1535 MMRADIO_LOG_ERROR("radio_hal_get_volume error");
1540 MMRADIO_VOLUME_LOCK(radio);
1541 radio->local_volume = volume;
1542 *pVolume = (float)radio->local_volume;
1543 MMRADIO_VOLUME_UNLOCK(radio);
1545 MMRADIO_LOG_FLEAVE();
1550 static int __mmradio_set_media_volume(mm_radio_t *radio, unsigned int level)
1552 int ret = MM_ERROR_NONE;
1554 MMRADIO_LOG_FENTER();
1556 MMRADIO_CHECK_INSTANCE(radio);
1557 MMRADIO_CHECK_STATE_RETURN_IF_FAIL(radio, MMRADIO_COMMAND_SET_VOLUME);
1559 MMRADIO_LOG_DEBUG("Setting %d volume\n", level);
1561 MMRADIO_VOLUME_LOCK(radio);
1562 radio->media_volume = level;
1563 MMRADIO_VOLUME_UNLOCK(radio);
1565 ret = radio_hal_set_media_volume(radio->hal_inf, level);
1566 if (ret != MM_ERROR_NONE)
1567 MMRADIO_LOG_ERROR("radio_hal_set_media_volume error");
1569 MMRADIO_LOG_FLEAVE();