external connection: add HAL for external connection
[platform/adaptation/emulator/device-manager-plugin-emul.git] / hw / dbus.c
1 /*
2  * device-node
3  *
4  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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 #include "dbus.h"
20 #include "shared.h"
21
22 int register_dbus_signal(const char *object,
23                 const char *iface, const char *signal,
24                 GDBusSignalCallback callback, void *data, int *id)
25 {
26         GError *err = NULL;
27         GDBusConnection *conn;
28         int sid;
29
30 #if !GLIB_CHECK_VERSION(2, 35, 0)
31         g_type_init();
32 #endif
33
34         conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
35         if (!conn) {
36                 _E("fail to get dbus connection : %s", err->message);
37                 g_clear_error(&err);
38                 return -EPERM;
39         }
40
41         /* subscribe signal */
42         sid = g_dbus_connection_signal_subscribe(conn,
43                         NULL, iface, signal, object,
44                         NULL, G_DBUS_SIGNAL_FLAGS_NONE,
45                         callback, data, NULL);
46         if (sid == 0) {
47                 _E("fail to connect %s signal", signal);
48                 return -EPERM;
49         }
50
51         if (id)
52                 *id = sid;
53
54         return 0;
55 }
56
57 void unregister_dbus_signal(int *id)
58 {
59         GError *err = NULL;
60         GDBusConnection *conn;
61
62         if (!id || *id <= 0)
63                 return;
64
65         conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
66         if (!conn) {
67                 _E("fail to get dbus connection : %s", err->message);
68                 g_clear_error(&err);
69                 return;
70         }
71
72         /* unsubscribe signal */
73         g_dbus_connection_signal_unsubscribe(conn, *id);
74         *id = 0;
75 }