Merge branch 'tizen_3.0' into tizen
[platform/core/multimedia/libmm-radio.git] / src / include / mm_radio_utils.h
1 /*
2  * mm_radio_utils.h
3  *
4  * Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
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
20 #ifndef __MM_RADIO_UTILS_H__
21 #define __MM_RADIO_UTILS_H__
22
23 #include <assert.h>
24 #include <mm_types.h>
25 #include <mm_error.h>
26 #include <mm_debug.h>
27 #include <mm_message.h>
28
29 /* radio log */
30 #define MMRADIO_LOG_FENTER              debug_fenter
31 #define MMRADIO_LOG_FLEAVE              debug_fleave
32 #define MMRADIO_LOG_DEBUG               debug_log
33 #define MMRADIO_LOG_ERROR               debug_error
34 #define MMRADIO_LOG_WARNING             debug_warning
35 #define MMRADIO_LOG_CRITICAL    debug_critical
36 #define MMRADIO_SLOG_DEBUG              debug_log /* secure_debug_log */
37
38 /* general */
39 #ifndef ARRAY_SIZE
40 #define ARRAY_SIZE(arr)         (sizeof(arr) / sizeof((arr)[0]))
41 #endif
42
43 #ifndef TRUE
44 #define TRUE 1
45 #endif
46 #ifndef FALSE
47 #define FALSE 0
48 #endif
49
50 #define MMRADIO_MAX_INT (2147483647)
51
52 #define MMRADIO_FREEIF(x) \
53 if ( x ) \
54         free( x ); \
55 x = NULL;
56
57 #define MMRADIO_CHECK_INSTANCE( x_radio ) \
58 if ( ! x_radio ) \
59 { \
60         debug_error("radio instance is NULL\n"); \
61         return MM_ERROR_RADIO_NOT_INITIALIZED; \
62 }
63
64 #define MMRADIO_CHECK_ARG( x_radio ) \
65 if ( ! x_radio ) \
66 { \
67         debug_error("argument is NULL\n"); \
68         return MM_ERROR_COMMON_INVALID_ARGUMENT; \
69 }
70
71
72 #define MMRADIO_CHECK_INSTANCE_RETURN_VOID( x_radio ) \
73 if ( ! x_radio ) \
74 { \
75         debug_error("radio instance is NULL\n"); \
76         return; \
77 }
78
79 #define MMRADIO_CHECK_DEVICE_STATE( x_radio ) \
80 if ( x_radio->radio_fd < 0 ) \
81 { \
82         debug_error("not available radio device\n"); \
83         return MM_ERROR_RADIO_NOT_INITIALIZED;\
84 }
85
86 /* command locking for multithreading */
87 #define MMRADIO_CMD_LOCK(x_radio)               pthread_mutex_lock(&((mm_radio_t *)x_radio)->cmd_lock)
88 #define MMRADIO_CMD_UNLOCK(x_radio)             pthread_mutex_unlock(&((mm_radio_t *)x_radio)->cmd_lock)
89
90 #define MMRADIO_VOLUME_LOCK(x_radio)            pthread_mutex_lock(&((mm_radio_t *)x_radio)->volume_lock)
91 #define MMRADIO_VOLUME_UNLOCK(x_radio)          pthread_mutex_unlock(&((mm_radio_t *)x_radio)->volume_lock)
92
93 /* message posting */
94 #define MMRADIO_POST_MSG( x_radio, x_msgtype, x_msg_param ) \
95 debug_log("posting %s to application\n", #x_msgtype); \
96 __mmradio_post_message(x_radio, x_msgtype, x_msg_param);
97
98 /* setting radio state */
99 #define MMRADIO_SET_STATE( x_radio, x_state ) \
100 debug_log("setting mm-radio state to %d\n", x_state); \
101 __mmradio_set_state(x_radio, x_state);
102
103 /* state */
104 #define MMRADIO_CHECK_STATE_RETURN_IF_FAIL( x_radio, x_command ) \
105 debug_log("checking radio state before doing %s\n", #x_command); \
106 switch ( __mmradio_check_state(x_radio, x_command) ) \
107 { \
108         case MM_ERROR_RADIO_INVALID_STATE: \
109                 return MM_ERROR_RADIO_INVALID_STATE; \
110         break; \
111         /* NOTE : for robustness of mmfw. we won't treat it as an error */ \
112         case MM_ERROR_RADIO_NO_OP: \
113                 return MM_ERROR_NONE; \
114         break; \
115         default: \
116         break; \
117 }
118
119 #define MMRADIO_CHECK_RETURN_IF_FAIL( x_ret, x_msg ) \
120 do \
121 { \
122         if( x_ret < 0 ) \
123         { \
124                 debug_error("%s error\n", x_msg);\
125                 return x_ret; \
126         } \
127 } while( 0 ) ;
128
129 #endif
130