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 do {                                            \
54         if (x) {                                \
55                 free(x);                        \
56                 x = NULL;                       \
57         }                                               \
58 } while (0)
59
60 #define MMRADIO_CHECK_INSTANCE(x_radio)                         \
61 do {                                                                                            \
62         if (!x_radio) {                                                                 \
63                 debug_error("radio instance is NULL\n");        \
64                 return MM_ERROR_RADIO_NOT_INITIALIZED;          \
65         }                                                                                               \
66 } while (0)
67
68 #define MMRADIO_CHECK_ARG(x_radio)                                      \
69 do {                                                                                            \
70         if (!x_radio) {                                                                 \
71                 debug_error("argument is NULL\n");                      \
72                 return MM_ERROR_COMMON_INVALID_ARGUMENT;        \
73         }                                                                                               \
74 } while (0)
75
76
77 #define MMRADIO_CHECK_INSTANCE_RETURN_VOID(x_radio)     \
78 do {                                                                                            \
79         if (!x_radio) {                                                                 \
80                 debug_error("radio instance is NULL\n");        \
81                 return;                                                                         \
82         }                                                                                               \
83 } while (0)
84
85 #define MMRADIO_CHECK_DEVICE_STATE(x_radio)                             \
86 do {                                                                                                    \
87         if (x_radio->radio_fd < 0) {                                            \
88                 debug_error("not available radio device\n");    \
89                 return MM_ERROR_RADIO_NOT_INITIALIZED;                  \
90         }                                                                                                       \
91 } while (0)
92
93 /* command locking for multithreading */
94 #define MMRADIO_CMD_LOCK(x_radio)               pthread_mutex_lock(&((mm_radio_t *)x_radio)->cmd_lock)
95 #define MMRADIO_CMD_UNLOCK(x_radio)             pthread_mutex_unlock(&((mm_radio_t *)x_radio)->cmd_lock)
96
97 #define MMRADIO_VOLUME_LOCK(x_radio)            pthread_mutex_lock(&((mm_radio_t *)x_radio)->volume_lock)
98 #define MMRADIO_VOLUME_UNLOCK(x_radio)          pthread_mutex_unlock(&((mm_radio_t *)x_radio)->volume_lock)
99
100 /* message posting */
101 #define MMRADIO_POST_MSG(x_radio, x_msgtype, x_msg_param)               \
102 do {                                                                                                                    \
103         debug_log("posting %s to application\n", #x_msgtype);           \
104         __mmradio_post_message(x_radio, x_msgtype, x_msg_param);        \
105 } while (0)
106
107 /* setting radio state */
108 #define MMRADIO_SET_STATE(x_radio, x_state)                                     \
109 do {                                                                                                            \
110         debug_log("setting mm-radio state to %d\n", x_state);   \
111         __mmradio_set_state(x_radio, x_state);                                  \
112 } while (0)
113
114 /* state */
115 #define MMRADIO_CHECK_STATE_RETURN_IF_FAIL(x_radio, x_command)                  \
116 do {                                                                                                                                    \
117         debug_log("checking radio state before doing %s\n", #x_command);        \
118         switch (__mmradio_check_state(x_radio, x_command)) {                            \
119         case MM_ERROR_RADIO_INVALID_STATE:                                                                      \
120                 return MM_ERROR_RADIO_INVALID_STATE;                                                    \
121                 break;                                                                                                                  \
122         /* NOTE : for robustness of mmfw. we won't treat it as an error */      \
123         case MM_ERROR_RADIO_NO_OP:                                                                                      \
124                 return MM_ERROR_NONE;                                                                                   \
125                 break;                                                                                                                  \
126         default:                                                                                                                        \
127                 break;                                                                                                                  \
128         }                                                                                                                                       \
129 } while (0)
130
131 #define MMRADIO_CHECK_RETURN_IF_FAIL(x_ret, x_msg)              \
132 do {                                                                                                    \
133         if (x_ret < 0) {                                                                        \
134                 debug_error("%s error\n", x_msg);                               \
135                 return x_ret;                                                                   \
136         }                                                                                                       \
137 } while (0);
138
139 #endif
140