Remove unused include statement
[platform/core/multimedia/libmm-sound.git] / common / mm_sound_dbus.c
1
2 #include <gio/gio.h>
3 #include <stdlib.h>
4
5 #include <mm_error.h>
6 #include <mm_debug.h>
7
8 #include "../include/mm_sound_dbus.h"
9 #include "../include/mm_sound_intf.h"
10
11 #define INTERFACE_DBUS                  "org.freedesktop.DBus.Properties"
12 #define SIGNAL_PROP_CHANGED             "PropertiesChanged"
13 #define METHOD_GET                      "Get"
14 #define METHOD_SET                      "Set"
15
16 struct callback_data {
17         mm_sound_dbus_callback user_cb;
18         void *user_data;
19         mm_sound_dbus_userdata_free free_func;
20 };
21
22 struct dbus_path {
23         char *bus_name;
24         char *object;
25         char *interface;
26 };
27
28 const struct dbus_path g_paths[AUDIO_PROVIDER_MAX] = {
29         [AUDIO_PROVIDER_SOUND_SERVER] = {
30                 .bus_name = "org.tizen.SoundServer",
31                 .object = "/org/tizen/SoundServer1",
32                 .interface ="org.tizen.SoundServer1"
33         },
34         [AUDIO_PROVIDER_FOCUS_SERVER] = {
35                 .bus_name = "org.tizen.FocusServer",
36                 .object = "/org/tizen/FocusServer1",
37                 .interface = "org.tizen.FocusServer1"
38         },
39         [AUDIO_PROVIDER_DEVICE_MANAGER] = {
40                 .bus_name = "org.pulseaudio.Server",
41                 .object = "/org/pulseaudio/DeviceManager",
42                 .interface = "org.pulseaudio.DeviceManager"
43         },
44         [AUDIO_PROVIDER_STREAM_MANAGER] = {
45                 .bus_name = "org.pulseaudio.Server",
46                 .object = "/org/pulseaudio/StreamManager",
47                 .interface = "org.pulseaudio.StreamManager"
48         },
49         [AUDIO_PROVIDER_AUDIO_CLIENT] = {
50                 .bus_name = "org.tizen.AudioClient",
51                 .object = "/org/tizen/AudioClient1",
52                 .interface = "org.tizen.AudioClient1"
53         }
54 };
55
56 const mm_sound_dbus_method_info_t g_methods[AUDIO_METHOD_MAX] = {
57         [AUDIO_METHOD_TEST] = {
58                 .name = "MethodTest1",
59         },
60         [AUDIO_METHOD_PLAY_FILE_START] = {
61                 .name = "PlayFileStart",
62         },
63         [AUDIO_METHOD_PLAY_FILE_START_WITH_STREAM_INFO] = {
64                 .name = "PlayFileStartWithStreamInfo",
65         },
66         [AUDIO_METHOD_PLAY_FILE_STOP] = {
67                 .name = "PlayFileStop",
68         },
69         [AUDIO_METHOD_PLAY_DTMF] = {
70                 .name = "PlayDTMF",
71         },
72         [AUDIO_METHOD_PLAY_DTMF_WITH_STREAM_INFO] = {
73                 .name = "PlayDTMFWithStreamInfo",
74         },
75         [AUDIO_METHOD_CLEAR_FOCUS] = {
76                 .name = "ClearFocus",
77         },
78         [AUDIO_METHOD_SET_VOLUME_LEVEL] = {
79                 .name = "SetVolumeLevel",
80         },
81         [AUDIO_METHOD_GET_CONNECTED_DEVICE_LIST] = {
82                 .name = "GetConnectedDeviceList",
83         },
84         [AUDIO_METHOD_GET_UNIQUE_ID] = {
85                 .name = "GetUniqueId",
86         },
87         [AUDIO_METHOD_REGISTER_FOCUS] = {
88                 .name = "RegisterFocus",
89         },
90         [AUDIO_METHOD_UNREGISTER_FOCUS] = {
91                 .name = "UnregisterFocus",
92         },
93         [AUDIO_METHOD_SET_FOCUS_REACQUISITION] = {
94                 .name = "SetFocusReacquisition",
95         },
96         [AUDIO_METHOD_GET_ACQUIRED_FOCUS_STREAM_TYPE] = {
97                 .name = "GetAcquiredFocusStreamType",
98         },
99         [AUDIO_METHOD_ACQUIRE_FOCUS] = {
100                 .name = "AcquireFocus",
101         },
102         [AUDIO_METHOD_RELEASE_FOCUS] = {
103                 .name = "ReleaseFocus",
104         },
105         [AUDIO_METHOD_WATCH_FOCUS] = {
106                 .name = "WatchFocus",
107         },
108         [AUDIO_METHOD_UNWATCH_FOCUS] = {
109                 .name = "UnwatchFocus",
110         },
111 };
112
113 const mm_sound_dbus_signal_info_t g_events[AUDIO_EVENT_MAX] = {
114         [AUDIO_EVENT_TEST] = {
115                 .name = "SignalTest1",
116         },
117         [AUDIO_EVENT_PLAY_FILE_END] = {
118                 .name = "PlayFileEnd",
119         },
120         [AUDIO_EVENT_VOLUME_CHANGED] = {
121                 .name = "VolumeChanged",
122         },
123         [AUDIO_EVENT_DEVICE_CONNECTED] = {
124                 .name = "DeviceConnected",
125         },
126         [AUDIO_EVENT_DEVICE_INFO_CHANGED] = {
127                 .name = "DeviceInfoChanged",
128         },
129         [AUDIO_EVENT_FOCUS_CHANGED] = {
130                 .name = "FocusChanged",
131         },
132         [AUDIO_EVENT_FOCUS_WATCH] = {
133                 .name = "FocusWatch",
134         },
135         [AUDIO_EVENT_EMERGENT_EXIT] = {
136                 .name = "EmergentExit",
137         }
138 };
139
140 /* Only For error types which is currently being used in server-side */
141 static const GDBusErrorEntry mm_sound_error_entries[] =
142 {
143         {MM_ERROR_OUT_OF_MEMORY, "org.tizen.multimedia.OutOfMemory"},
144         {MM_ERROR_OUT_OF_STORAGE, "org.tizen.multimedia.OutOfStorage"},
145         {MM_ERROR_INVALID_ARGUMENT, "org.tizen.multimedia.InvalidArgument"},
146         {MM_ERROR_POLICY_INTERNAL, "org.tizen.multimedia.PolicyInternal"},
147         {MM_ERROR_NOT_SUPPORT_API, "org.tizen.multimedia.NotSupportAPI"},
148         {MM_ERROR_POLICY_BLOCKED, "org.tizen.multimedia.PolicyBlocked"},
149         {MM_ERROR_END_OF_FILE, "org.tizen.multimedia.EndOfFile"},
150         {MM_ERROR_COMMON_OUT_OF_RANGE, "org.tizen.multimedia.common.OutOfRange"},
151         {MM_ERROR_COMMON_UNKNOWN, "org.tizen.multimedia.common.Unknown"},
152         {MM_ERROR_COMMON_NO_FREE_SPACE, "org.tizen.multimedia.common.NoFreeSpace"},
153         {MM_ERROR_SOUND_INTERNAL, "org.tizen.multimedia.audio.Internal"},
154         {MM_ERROR_SOUND_INVALID_STATE, "org.tizen.multimedia.audio.InvalidState"},
155         {MM_ERROR_SOUND_NO_FREE_SPACE, "org.tizen.multimedia.audio.NoFreeSpace"},
156         {MM_ERROR_SOUND_UNSUPPORTED_MEDIA_TYPE, "org.tizen.multimedia.audio.UnsupportedMediaType"},
157         {MM_ERROR_SOUND_INVALID_POINTER, "org.tizen.multimedia.audio.InvalidPointer"},
158         {MM_ERROR_SOUND_INVALID_FILE, "org.tizen.multimedia.audio.InvalidFile"},
159         {MM_ERROR_SOUND_FILE_NOT_FOUND, "org.tizen.multimedia.audio.FileNotFound"},
160         {MM_ERROR_SOUND_NO_DATA, "org.tizen.multimedia.audio.NoData"},
161         {MM_ERROR_SOUND_INVALID_PATH, "org.tizen.multimedia.audio.InvalidPath"},
162 };
163
164
165 /******************************************************************************************
166                 Wrapper Functions of GDbus
167 ******************************************************************************************/
168
169 static int _parse_error_msg(char *full_err_msg, char **err_name, char **err_msg)
170 {
171         char *save_p, *domain, *_err_name, *_err_msg;
172
173         if (!(domain = strtok_r(full_err_msg, ":", &save_p))) {
174                 debug_error("get domain failed");
175                 return -1;
176         }
177         if (!(_err_name = strtok_r(NULL, ":", &save_p))) {
178                 debug_error("get err name failed");
179                 return -1;
180         }
181         if (!(_err_msg = strtok_r(NULL, ":", &save_p))) {
182                 debug_error("get err msg failed");
183                 return -1;
184         }
185
186         *err_name = _err_name;
187         *err_msg = _err_msg;
188
189         return 0;
190 }
191
192 static int _convert_error_name(const char *err_name)
193 {
194         int i = 0;
195
196         for (i = 0; i < G_N_ELEMENTS(mm_sound_error_entries); i++) {
197                 if (!strcmp(mm_sound_error_entries[i].dbus_error_name, err_name)) {
198                         return mm_sound_error_entries[i].error_code;
199                 }
200         }
201
202         return MM_ERROR_COMMON_UNKNOWN;
203 }
204
205 static int _dbus_method_call(GDBusConnection* conn, const char* bus_name, const char* object, const char* intf,
206                                                         const char* method, GVariant* args, GVariant** result)
207 {
208         int ret = MM_ERROR_NONE;
209         GError *err = NULL;
210         GVariant* dbus_reply = NULL;
211
212         if (!conn || !object || !intf || !method) {
213                 debug_error("Invalid Argument");
214                 if (!conn)
215                         debug_error("conn null");
216                 if (!object)
217                         debug_error("object null");
218                 if (!intf)
219                         debug_error("intf null");
220                 if (!method)
221                         debug_error("method null");
222                 return MM_ERROR_INVALID_ARGUMENT;
223         }
224
225         debug_log("Dbus call with obj'%s' intf'%s' method'%s'", object, intf, method);
226
227         dbus_reply = g_dbus_connection_call_sync(conn, bus_name, object , intf,
228                              method, args ,
229                              NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err );
230
231         if (!dbus_reply) {
232                 char *err_name = NULL, *err_msg = NULL;
233                 debug_log("Method Call '%s.%s' Failed, %s", intf, method, err->message);
234
235                 if (_parse_error_msg(err->message,  &err_name, &err_msg) < 0) {
236                         debug_error("failed to parse error message");
237                         g_error_free(err);
238                         return MM_ERROR_SOUND_INTERNAL;
239                 }
240                 ret = _convert_error_name(err_name);
241                 g_error_free(err);
242         }
243
244         debug_log("Method Call '%s.%s' Success", intf, method);
245         *result = dbus_reply;
246
247         return ret;
248 }
249
250 #if 0
251 static int _dbus_set_property(GDBusConnection* conn, const char* bus_name, const char* object, const char* intf,
252                                                         const char* prop, GVariant* args, GVariant** result)
253 {
254         int ret = MM_ERROR_NONE;
255
256         if (!conn || !object || !intf || !prop) {
257                 debug_error("Invalid Argument");
258                 return MM_ERROR_INVALID_ARGUMENT;
259         }
260
261         debug_log("Dbus set property with obj'%s' intf'%s' prop'%s'", object, intf, prop);
262
263         if ((ret = _dbus_method_call(conn, bus_name, object, INTERFACE_DBUS, METHOD_SET,
264                                                                 g_variant_new("(ssv)", intf, prop, args), result)) != MM_ERROR_NONE) {
265                 debug_error("Dbus call for set property failed");
266         }
267
268         return ret;
269 }
270
271 static int _dbus_get_property(GDBusConnection *conn, const char* bus_name, const char* object_name,
272                                                         const char* intf_name, const char* prop, GVariant** result)
273 {
274         int ret = MM_ERROR_NONE;
275
276         if (!conn || !object_name || !intf_name || !prop) {
277                 debug_error("Invalid Argument");
278                 return MM_ERROR_INVALID_ARGUMENT;
279         }
280
281         debug_log("Dbus get property with obj'%s' intf'%s' prop'%s'", object_name, intf_name, prop);
282
283         if ((ret = _dbus_method_call(conn,
284                                        bus_name, object_name, INTERFACE_DBUS, METHOD_GET,
285                                        g_variant_new("(ss)", intf_name, prop), result)) != MM_ERROR_NONE) {
286                 debug_error("Dbus call for get property failed");
287         }
288
289         return ret;
290 }
291 #endif
292
293 static int _dbus_subscribe_signal(GDBusConnection *conn, const char* object_name, const char* intf_name,
294                                         const char* signal_name, GDBusSignalCallback signal_cb, guint *subscribe_id, void* userdata, GDestroyNotify freefunc)
295 {
296         guint subs_id = 0;
297
298         if (!conn || !object_name || !intf_name || !signal_name || !signal_cb) {
299                 debug_error("Invalid Argument");
300                 return MM_ERROR_INVALID_ARGUMENT;
301         }
302
303         debug_log("Dbus subscirbe signal with Obj'%s' Intf'%s' sig'%s'", object_name, intf_name, signal_name);
304
305         subs_id = g_dbus_connection_signal_subscribe(conn, NULL, intf_name, signal_name, object_name, \
306                          NULL , G_DBUS_SIGNAL_FLAGS_NONE , signal_cb, userdata, NULL );
307
308         if (!subs_id) {
309                 debug_error ("g_dbus_connection_signal_subscribe() failed ");
310                 return MM_ERROR_SOUND_INTERNAL;
311         }
312         if (subscribe_id)
313                 *subscribe_id = subs_id;
314
315         return MM_ERROR_NONE;
316 }
317
318 static void _dbus_unsubscribe_signal(GDBusConnection *conn, guint subs_id)
319 {
320         if (!conn || !subs_id) {
321                 debug_error("Invalid Argument");
322                 return;
323         }
324
325         g_dbus_connection_signal_unsubscribe(conn, subs_id);
326 }
327
328 static GDBusConnection* _dbus_get_connection(GBusType bustype)
329 {
330         static GDBusConnection *conn_system = NULL;
331         static GDBusConnection *conn_session = NULL;
332         GError *err = NULL;
333
334         if (bustype == G_BUS_TYPE_SYSTEM) {
335                 if (conn_system) {
336                         debug_log("Already connected to system bus");
337                 } else {
338                         debug_log("Get new connection on system bus");
339                         if (!(conn_system = g_bus_get_sync(bustype, NULL, &err))) {
340                                 debug_error ("g_dbus_get_sync() error (%s)", err->message);
341                                 g_error_free(err);
342                         }
343                 }
344                 return conn_system;
345         } else if (bustype == G_BUS_TYPE_SESSION) {
346                 if (conn_session) {
347                         debug_log("Already connected to session bus");
348                 } else {
349                         debug_log("Get new connection on session bus");
350                         if (!(conn_session = g_bus_get_sync(bustype, NULL, &err))) {
351                                 debug_error ("g_dbus_get_sync() error (%s)", err->message);
352                                 g_error_free(err);
353                         }
354                 }
355                 return conn_session;
356         } else {
357                 debug_error("Invalid bus type");
358                 return NULL;
359         }
360
361 }
362
363 #if 0
364 static void _dbus_disconnect(GDBusConnection* conn)
365 {
366         debug_fenter ();
367         g_object_unref(conn);
368         debug_fleave ();
369 }
370 #endif
371
372 /******************************************************************************************
373                 Simple Functions For Communicate with Sound-Server
374 ******************************************************************************************/
375
376 EXPORT_API
377 int mm_sound_dbus_method_call_to(audio_provider_t provider, audio_method_t method_type, GVariant *args, GVariant **result)
378 {
379         int ret = MM_ERROR_NONE;
380         GDBusConnection *conn = NULL;
381
382         if (method_type < 0 || method_type >= AUDIO_METHOD_MAX) {
383                 debug_error("Invalid method type : %d", method_type);
384                 return MM_ERROR_INVALID_ARGUMENT;
385         }
386         if (provider < 0 || provider >= AUDIO_PROVIDER_MAX) {
387                 debug_error("Invalid provider : %d", provider);
388                 return MM_ERROR_INVALID_ARGUMENT;
389         }
390
391         if (!(conn = _dbus_get_connection(G_BUS_TYPE_SYSTEM))) {
392                 debug_error("Get Dbus Connection Error");
393                 return MM_ERROR_SOUND_INTERNAL;
394         }
395
396         if((ret = _dbus_method_call(conn, g_paths[provider].bus_name,
397                                       g_paths[provider].object,
398                                       g_paths[provider].interface,
399                                       g_methods[method_type].name,
400                                       args, result)) != MM_ERROR_NONE) {
401                 debug_error("Dbus Call on Client Error");
402         }
403
404         return ret;
405 }
406
407 /* This callback only transform signal-callback to dbus non-related form (mm_sound_dbus_callback) */
408 static void _dbus_signal_callback(GDBusConnection  *connection,
409                                      const gchar      *sender_name,
410                                      const gchar      *object_path,
411                                      const gchar      *interface_name,
412                                      const gchar      *signal_name,
413                                      GVariant         *params,
414                                      gpointer          userdata)
415 {
416         struct callback_data *cb_data = (struct callback_data*) userdata;
417
418         debug_log("Signal(%s.%s) Received , Let's call Wrapper-Callback", interface_name, signal_name);
419
420         if (!strcmp(signal_name, g_events[AUDIO_EVENT_VOLUME_CHANGED].name)) {
421                 (cb_data->user_cb)(AUDIO_EVENT_VOLUME_CHANGED, params, cb_data->user_data);
422         } else if (!strcmp(signal_name, g_events[AUDIO_EVENT_DEVICE_CONNECTED].name)) {
423                 (cb_data->user_cb)(AUDIO_EVENT_DEVICE_CONNECTED, params, cb_data->user_data);
424         } else if (!strcmp(signal_name, g_events[AUDIO_EVENT_DEVICE_INFO_CHANGED].name)) {
425                 (cb_data->user_cb)(AUDIO_EVENT_DEVICE_INFO_CHANGED, params, cb_data->user_data);
426         } else if (!strcmp(signal_name, g_events[AUDIO_EVENT_FOCUS_CHANGED].name)) {
427                 (cb_data->user_cb)(AUDIO_EVENT_FOCUS_CHANGED, params, cb_data->user_data);
428         } else if (!strcmp(signal_name, g_events[AUDIO_EVENT_FOCUS_WATCH].name)) {
429                 (cb_data->user_cb)(AUDIO_EVENT_FOCUS_WATCH, params, cb_data->user_data);
430         } else if (!strcmp(signal_name, g_events[AUDIO_EVENT_PLAY_FILE_END].name)) {
431                 (cb_data->user_cb)(AUDIO_EVENT_PLAY_FILE_END, params, cb_data->user_data);
432         } else if (!strcmp(signal_name, g_events[AUDIO_EVENT_EMERGENT_EXIT].name)) {
433                 (cb_data->user_cb)(AUDIO_EVENT_EMERGENT_EXIT, params, cb_data->user_data);
434         }
435 }
436
437 static void callback_data_free_func(gpointer data)
438 {
439         struct callback_data *cb_data = (struct callback_data *) data;
440
441         if (cb_data->free_func)
442                 cb_data->free_func(cb_data->user_data);
443         g_free(cb_data);
444 }
445
446 EXPORT_API
447 int mm_sound_dbus_signal_subscribe_to(audio_provider_t provider, audio_event_t event, mm_sound_dbus_callback callback, void *userdata, mm_sound_dbus_userdata_free freefunc, unsigned *subs_id)
448 {
449         GDBusConnection *conn = NULL;
450         guint _subs_id = 0;
451         struct callback_data *cb_data = NULL;
452
453         if (!callback) {
454                 debug_error("Callback is Null");
455                 return MM_ERROR_INVALID_ARGUMENT;
456         }
457
458         if (event < 0 || event >= AUDIO_EVENT_MAX) {
459                 debug_error("Wrong event Type : %d", event);
460                 return MM_ERROR_INVALID_ARGUMENT;
461         }
462
463         if (provider < 0 || provider >= AUDIO_PROVIDER_MAX) {
464                 debug_error("Invalid provider : %d", provider);
465                 return MM_ERROR_INVALID_ARGUMENT;
466         }
467
468         if (!(conn = _dbus_get_connection(G_BUS_TYPE_SYSTEM))) {
469                 debug_error("Get Dbus Connection Error");
470                 return MM_ERROR_SOUND_INTERNAL;
471         }
472
473         cb_data = (struct callback_data*) g_malloc0(sizeof(struct callback_data));
474         cb_data->user_cb = callback;
475         cb_data->user_data = userdata;
476         cb_data->free_func = freefunc;
477
478         if(_dbus_subscribe_signal(conn, g_paths[provider].object, g_paths[provider].interface, g_events[event].name,
479                                                         _dbus_signal_callback, &_subs_id, cb_data, callback_data_free_func) != MM_ERROR_NONE) {
480                 debug_error("Dbus Subscribe on Client Error");
481                 goto fail;
482         }
483         if (subs_id)
484                 *subs_id = (unsigned int)_subs_id;
485
486         return MM_ERROR_NONE;
487
488 fail:
489         if (cb_data)
490                 free(cb_data);
491         return MM_ERROR_SOUND_INTERNAL;
492 }
493
494 EXPORT_API
495 int mm_sound_dbus_signal_unsubscribe(unsigned int subs_id)
496 {
497         GDBusConnection *conn = NULL;
498
499         if (!(conn = _dbus_get_connection(G_BUS_TYPE_SYSTEM))) {
500                 debug_error("Get Dbus Connection Error");
501                 return MM_ERROR_SOUND_INTERNAL;
502         }
503
504         _dbus_unsubscribe_signal(conn, (guint) subs_id);
505
506         return MM_ERROR_NONE;
507 }
508
509 EXPORT_API
510 int mm_sound_dbus_emit_signal(audio_provider_t provider, audio_event_t event, GVariant *param)
511 {
512         GDBusConnection *conn;
513         GError *err = NULL;
514         gboolean dbus_ret;
515         int ret = MM_ERROR_NONE;
516
517         if (event < 0 || event >= AUDIO_EVENT_MAX) {
518                 debug_error ("emit signal failed, invalid argument, event_type(%d)", event);
519                 return MM_ERROR_INVALID_ARGUMENT;
520         }
521
522         if (!(conn = _dbus_get_connection(G_BUS_TYPE_SYSTEM))) {
523                 debug_error("Get Dbus Connection Error");
524                 ret = MM_ERROR_SOUND_INTERNAL;
525                 goto end;
526         }
527
528         dbus_ret = g_dbus_connection_emit_signal (conn,
529                                                   NULL, g_paths[provider].object,
530                                                   g_paths[provider].interface, g_events[event].name,
531                                                   param, &err);
532         if (!dbus_ret) {
533                 debug_error ("g_dbus_connection_emit_signal() error (%s)", err->message);
534                 ret = MM_ERROR_SOUND_INTERNAL;
535         }
536
537 end:
538         debug_msg ("emit signal for [%s]  %s", g_events[event].name, (ret == MM_ERROR_NONE ? "success" : "failed") );
539         return ret;
540 }
541