Remove unused include statement
[platform/core/multimedia/libmm-sound.git] / mm_sound_device.c
1 /*
2  * libmm-sound
3  *
4  * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Sangchul Lee <sc11.lee@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <stdlib.h>
23 #include <unistd.h>
24
25 #include <mm_debug.h>
26
27 #include "include/mm_sound.h"
28 #include "include/mm_sound_device.h"
29 #include "include/mm_sound_client.h"
30
31 #define VOLUME_TYPE_LEN 64
32
33
34 static int _check_for_valid_mask (mm_sound_device_flags_e flags)
35 {
36         int ret = MM_ERROR_NONE;
37         bool at_least_cond = false;
38
39         if (flags > 0 && flags <= MM_SOUND_DEVICE_ALL_FLAG) {
40                 if (flags & MM_SOUND_DEVICE_IO_DIRECTION_IN_FLAG)
41                         at_least_cond = true;
42                 if (!at_least_cond && (flags & MM_SOUND_DEVICE_IO_DIRECTION_OUT_FLAG))
43                         at_least_cond = true;
44                 if (!at_least_cond && (flags & MM_SOUND_DEVICE_IO_DIRECTION_BOTH_FLAG))
45                         at_least_cond = true;
46                 if (!at_least_cond && (flags & MM_SOUND_DEVICE_TYPE_INTERNAL_FLAG))
47                         at_least_cond = true;
48                 if (!at_least_cond && (flags & MM_SOUND_DEVICE_TYPE_EXTERNAL_FLAG))
49                         at_least_cond = true;
50                 if (!at_least_cond && (flags & MM_SOUND_DEVICE_STATE_DEACTIVATED_FLAG))
51                         at_least_cond = true;
52                 if (!at_least_cond && (flags & MM_SOUND_DEVICE_STATE_ACTIVATED_FLAG))
53                         at_least_cond = true;
54         } else {
55                 ret = MM_ERROR_INVALID_ARGUMENT;
56         }
57         if (!at_least_cond) {
58                 ret = MM_ERROR_INVALID_ARGUMENT;
59         }
60         if (ret) {
61                 debug_error("flags[0x%x] is not valid\n", flags);
62         }
63         return ret;
64 }
65
66 static int __convert_device_type_to_enum (char *device_type, mm_sound_device_type_e *device_type_enum)
67 {
68         int ret = MM_ERROR_NONE;
69
70         if (!device_type || !device_type_enum) {
71                 return MM_ERROR_INVALID_ARGUMENT;
72         }
73
74         if (!strncmp(device_type, "builtin-speaker", VOLUME_TYPE_LEN)) {
75                 *device_type_enum = MM_SOUND_DEVICE_TYPE_BUILTIN_SPEAKER;
76         } else if (!strncmp(device_type, "builtin-receiver", VOLUME_TYPE_LEN)) {
77                 *device_type_enum = MM_SOUND_DEVICE_TYPE_BUILTIN_RECEIVER;
78         } else if (!strncmp(device_type, "builtin-mic", VOLUME_TYPE_LEN)) {
79                 *device_type_enum = MM_SOUND_DEVICE_TYPE_BUILTIN_MIC;
80         } else if (!strncmp(device_type, "audio-jack", VOLUME_TYPE_LEN)) {
81                 *device_type_enum = MM_SOUND_DEVICE_TYPE_AUDIOJACK;
82         } else if (!strncmp(device_type, "bt", VOLUME_TYPE_LEN)) {
83                 *device_type_enum = MM_SOUND_DEVICE_TYPE_BLUETOOTH;
84         } else if (!strncmp(device_type, "hdmi", VOLUME_TYPE_LEN)) {
85                 *device_type_enum = MM_SOUND_DEVICE_TYPE_HDMI;
86         } else if (!strncmp(device_type, "forwarding", VOLUME_TYPE_LEN)) {
87                 *device_type_enum = MM_SOUND_DEVICE_TYPE_MIRRORING;
88         } else if (!strncmp(device_type, "usb-audio", VOLUME_TYPE_LEN)) {
89                 *device_type_enum = MM_SOUND_DEVICE_TYPE_USB_AUDIO;
90         } else {
91                 ret = MM_ERROR_INVALID_ARGUMENT;
92                 debug_error("not supported device_type(%s), err(0x%08x)", device_type, ret);
93         }
94
95         return ret;
96 }
97
98 EXPORT_API
99 int mm_sound_add_device_connected_callback(mm_sound_device_flags_e flags, mm_sound_device_connected_cb func, void *user_data, unsigned int *subs_id)
100 {
101         int ret = MM_ERROR_NONE;
102
103         if (func == NULL || subs_id == NULL) {
104                 debug_error("argument is not valid\n");
105                 return MM_ERROR_INVALID_ARGUMENT;
106         }
107         ret = _check_for_valid_mask(flags);
108         if (ret == MM_ERROR_NONE) {
109                 ret = mm_sound_client_add_device_connected_callback(flags, func, user_data, subs_id);
110                 if (ret < 0) {
111                         debug_error("Could not add device connected callback, ret = %x\n", ret);
112                 }
113         }
114
115         return ret;
116 }
117
118 EXPORT_API
119 int mm_sound_remove_device_connected_callback(unsigned int subs_id)
120 {
121         int ret = MM_ERROR_NONE;
122
123         ret = mm_sound_client_remove_device_connected_callback(subs_id);
124         if (ret < 0) {
125                 debug_error("Could not remove device connected callback, ret = %x\n", ret);
126         }
127
128         return ret;
129 }
130
131 EXPORT_API
132 int mm_sound_add_device_information_changed_callback(mm_sound_device_flags_e flags, mm_sound_device_info_changed_cb func, void *user_data, unsigned int *subs_id)
133 {
134         int ret = MM_ERROR_NONE;
135
136         if (func == NULL || subs_id == NULL) {
137                 debug_error("argument is not valid\n");
138                 return MM_ERROR_INVALID_ARGUMENT;
139         }
140         ret = _check_for_valid_mask(flags);
141         if (ret == MM_ERROR_NONE) {
142                 ret = mm_sound_client_add_device_info_changed_callback(flags, func, user_data, subs_id);
143                 if (ret < 0) {
144                         debug_error("Could not add device information changed callback, ret = %x\n", ret);
145                 }
146         }
147
148         return ret;
149 }
150
151 EXPORT_API
152 int mm_sound_remove_device_information_changed_callback(unsigned int subs_id)
153 {
154         int ret = MM_ERROR_NONE;
155
156         ret = mm_sound_client_remove_device_info_changed_callback(subs_id);
157         if (ret < 0) {
158                 debug_error("Could not remove device information changed callback, ret = %x\n", ret);
159         }
160
161         return ret;
162 }
163
164 EXPORT_API
165 int mm_sound_get_current_device_list(mm_sound_device_flags_e flags, MMSoundDeviceList_t *device_list)
166 {
167         int ret = MM_ERROR_NONE;
168         mm_sound_device_list_t *_device_list;
169
170         if (!device_list) {
171                 return MM_ERROR_INVALID_ARGUMENT;
172         }
173         ret = _check_for_valid_mask(flags);
174         if (ret != MM_ERROR_NONE) {
175                 debug_error("mask[0x%x] is invalid, ret=0x%x", flags, ret);
176                 return ret;
177         }
178
179         if (!(_device_list = g_malloc0(sizeof(mm_sound_device_list_t)))) {
180                 debug_error("[Client] Allocate device list failed");
181                 return MM_ERROR_SOUND_INTERNAL;
182         }
183
184         _device_list->is_new_device_list = true;
185
186         ret = mm_sound_client_get_current_connected_device_list(flags, _device_list);
187         if (ret < 0) {
188                 debug_error("Could not get current connected device list, ret = %x\n", ret);
189                 g_free(_device_list);
190         } else {
191                 *device_list = _device_list;
192         }
193
194         return ret;
195 }
196
197 EXPORT_API
198 int mm_sound_free_device_list(MMSoundDeviceList_t device_list)
199 {
200         mm_sound_device_list_t *device_list_t = NULL;
201
202         if (!device_list) {
203                 return MM_ERROR_INVALID_ARGUMENT;
204         }
205         device_list_t = (mm_sound_device_list_t*) device_list;
206         g_list_free_full(device_list_t->list, g_free);
207         g_free(device_list_t);
208
209         return MM_ERROR_NONE;
210 }
211
212 EXPORT_API
213 int mm_sound_get_next_device (MMSoundDeviceList_t device_list, MMSoundDevice_t *device)
214 {
215         int ret = MM_ERROR_NONE;
216         mm_sound_device_list_t *device_list_t = NULL;
217         GList *node = NULL;
218         if (!device_list || !device) {
219                 return MM_ERROR_INVALID_ARGUMENT;
220         }
221         device_list_t = (mm_sound_device_list_t*) device_list;
222         if (device_list_t->is_new_device_list) {
223                 node = g_list_first(device_list_t->list);
224         } else {
225                 node = g_list_next(device_list_t->list);
226         }
227         if (!node) {
228                 ret = MM_ERROR_SOUND_NO_DATA;
229         } else {
230                 if (device_list_t->is_new_device_list) {
231                         device_list_t->is_new_device_list = false;
232                 } else {
233                         device_list_t->list = node;
234                 }
235                 *device = (mm_sound_device_t*)node->data;
236                 debug_log("next device[0x%x]\n", *device);
237         }
238         return ret;
239 }
240
241 EXPORT_API
242 int mm_sound_get_prev_device (MMSoundDeviceList_t device_list, MMSoundDevice_t *device)
243 {
244         int ret = MM_ERROR_NONE;
245         mm_sound_device_list_t *device_list_t = NULL;
246         GList *node = NULL;
247         if (!device_list || !device) {
248                 return MM_ERROR_INVALID_ARGUMENT;
249         }
250         device_list_t = (mm_sound_device_list_t*) device_list;
251         node = g_list_previous(device_list_t->list);
252         if (!node) {
253                 ret = MM_ERROR_SOUND_NO_DATA;
254                 debug_error("Could not get previous device, ret = %x\n", ret);
255         } else {
256                 device_list_t->list = node;
257                 *device = (mm_sound_device_t*)node->data;
258                 debug_log("previous device[0x%x]\n", *device);
259         }
260         return ret;
261 }
262
263 EXPORT_API
264 int mm_sound_get_device_type(MMSoundDevice_t device_h, mm_sound_device_type_e *type)
265 {
266         mm_sound_device_t *device = (mm_sound_device_t*)device_h;
267         if(!device || !type) {
268                 debug_error("invalid argument\n");
269                 return MM_ERROR_INVALID_ARGUMENT;
270         }
271         __convert_device_type_to_enum(device->type, type);
272         debug_log("device_handle:0x%x, type:%d\n", device, *type);
273
274         return MM_ERROR_NONE;
275 }
276
277 EXPORT_API
278 int mm_sound_get_device_io_direction(MMSoundDevice_t device_h, mm_sound_device_io_direction_e *io_direction)
279 {
280         mm_sound_device_t *device = (mm_sound_device_t*)device_h;
281         if(!device) {
282                 debug_error("invalid handle\n");
283                 return MM_ERROR_INVALID_ARGUMENT;
284         }
285         *io_direction = device->io_direction;
286         debug_log("device_handle:0x%x, io_direction:%d (1:IN,2:OUT,3:INOUT)\n", device, *io_direction);
287
288         return MM_ERROR_NONE;
289 }
290
291 EXPORT_API
292 int mm_sound_get_device_id(MMSoundDevice_t device_h, int *id)
293 {
294         mm_sound_device_t *device = (mm_sound_device_t*)device_h;
295         if(!device) {
296                 debug_error("invalid handle\n");
297                 return MM_ERROR_INVALID_ARGUMENT;
298         }
299         *id = device->id;
300         debug_log("device_handle:0x%x, id:%d\n", device, *id);
301
302         return MM_ERROR_NONE;
303 }
304
305 EXPORT_API
306 int mm_sound_get_device_state(MMSoundDevice_t device_h, mm_sound_device_state_e *state)
307 {
308         mm_sound_device_t *device = (mm_sound_device_t*)device_h;
309         if(!device) {
310                 debug_error("invalid handle\n");
311                 return MM_ERROR_INVALID_ARGUMENT;
312         }
313         *state = device->state;
314         debug_log("device_handle:0x%x, state:%d (0:INACTIVATED,1:ACTIVATED)\n", device, *state);
315
316         return MM_ERROR_NONE;
317 }
318
319 EXPORT_API
320 int mm_sound_get_device_name(MMSoundDevice_t device_h, char **name)
321 {
322         mm_sound_device_t *device = (mm_sound_device_t*)device_h;
323         if(!device) {
324                 debug_error("invalid handle\n");
325                 return MM_ERROR_INVALID_ARGUMENT;
326         }
327         *name = device->name;
328         debug_log("device_handle:0x%x, name:%s\n", device, *name);
329
330         return MM_ERROR_NONE;
331 }
332