Tizen 2.1 base
[apps/native/ug-bluetooth-efl.git] / src / libraries / bt-dbus-method.c
1 /*
2  * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Flora License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://floralicense.org/license/
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <glib.h>
18 #include <dbus/dbus.h>
19 #include <dbus/dbus-glib-bindings.h>
20
21 #include "bt-debug.h"
22 #include "bt-type-define.h"
23 #include "bt-util.h"
24 #include "bt-dbus-method.h"
25
26
27 /**********************************************************************
28 *                                                 Static Functions
29 ***********************************************************************/
30
31 int __bt_get_adapter_path(DBusGConnection *GConn, char *path)
32 {
33         FN_START;
34
35         GError *error = NULL;
36         DBusGProxy *manager_proxy = NULL;
37         char *adapter_path = NULL;
38         int ret = BT_UG_ERROR_NONE;
39         gsize len = 0;
40
41         retv_if(GConn == NULL, -1);
42         retv_if(path == NULL, -1);
43
44         manager_proxy = dbus_g_proxy_new_for_name(GConn, BLUEZ_DBUS_NAME, "/",
45                         "org.bluez.Manager");
46
47         retv_if(manager_proxy == NULL, -1);
48
49         dbus_g_proxy_call(manager_proxy, "DefaultAdapter", &error,
50                         G_TYPE_INVALID, DBUS_TYPE_G_OBJECT_PATH, &adapter_path,
51                         G_TYPE_INVALID);
52
53         if (error != NULL) {
54                 BT_DBG("Getting DefaultAdapter failed: [%s]\n", error->message);
55                 g_error_free(error);
56                 g_object_unref(manager_proxy);
57                 return -1;
58         }
59
60         if (adapter_path == NULL) {
61                 g_object_unref(manager_proxy);
62                 return -1;
63         }
64
65         len = g_strlcpy(path, adapter_path, BT_ADAPTER_PATH_LEN);
66
67         if (len >= BT_ADAPTER_PATH_LEN) {
68                 BT_DBG("The copied len is too large");
69                 ret = -1;
70         }
71
72         g_object_unref(manager_proxy);
73         g_free(adapter_path);
74
75         FN_END;
76         return ret;
77 }
78
79
80 /**********************************************************************
81 *                                                Common Functions
82 ***********************************************************************/
83
84 DBusGProxy *_bt_get_adapter_proxy(DBusGConnection *conn)
85 {
86         FN_START;
87         DBusGProxy *adapter = NULL;
88         char adapter_path[BT_ADAPTER_PATH_LEN] = { 0 };
89
90         retv_if(conn == NULL, NULL);
91
92         if (__bt_get_adapter_path(conn, adapter_path) < 0) {
93                 BT_DBG("Could not get adapter path\n");
94                 return NULL;
95         }
96
97         adapter = dbus_g_proxy_new_for_name(conn, BLUEZ_DBUS_NAME,
98                                                                 adapter_path, ADAPTER_INTERFACE);
99
100         FN_END;
101         return adapter;
102 }
103
104 void _bt_reset_environment(void)
105 {
106         FN_START;
107         DBusGProxy *proxy;
108         DBusGConnection *conn;
109
110         conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, NULL);;
111         ret_if(conn == NULL);
112
113         proxy = dbus_g_proxy_new_for_name(conn, BT_CORE_NAME,
114                         BT_CORE_PATH, BT_CORE_INTERFACE);
115
116         if (dbus_g_proxy_call(proxy, "ResetAdapter", NULL,
117                                         G_TYPE_INVALID, G_TYPE_INVALID) == FALSE) {
118                  BT_ERR("Bt core call failed");
119         }
120
121         dbus_g_connection_unref(conn);
122
123         FN_END;
124 }
125
126 gboolean _bt_is_profile_connected(int connected_type,
127                                 DBusGConnection *conn,
128                                 unsigned char *addr)
129 {
130         FN_START;
131         char *object_path = NULL;
132         char addr_str[BT_ADDRESS_STR_LEN + 1] = { 0 };
133         gboolean connected = FALSE;
134         DBusGProxy *proxy = NULL;
135         DBusGProxy *adapter = NULL;
136         GError *error = NULL;
137         GHashTable *hash = NULL;
138         GValue *value = NULL;
139         char *interface = NULL;
140
141         retv_if(conn == NULL, FALSE);
142         retv_if(addr == NULL, FALSE);
143
144         adapter = _bt_get_adapter_proxy(conn);
145
146         retv_if(adapter == NULL, FALSE);
147
148         _bt_util_addr_type_to_addr_string(addr_str, addr);
149
150         dbus_g_proxy_call(adapter, "FindDevice",
151                           &error, G_TYPE_STRING, addr_str,
152                           G_TYPE_INVALID, DBUS_TYPE_G_OBJECT_PATH,
153                           &object_path, G_TYPE_INVALID);
154
155         g_object_unref(adapter);
156
157         if (error != NULL) {
158                 BT_DBG("Failed to Find device: %s\n", error->message);
159                 g_error_free(error);
160                 return FALSE;
161         }
162
163         retv_if(object_path == NULL, FALSE);
164
165         switch (connected_type) {
166         case BT_HEADSET_CONNECTED:
167                 interface = HEADSET_INTERFACE;
168                 break;
169         case BT_STEREO_HEADSET_CONNECTED:
170                 interface = SYNK_INTERFACE;
171                 break;
172         case BT_HID_CONNECTED:
173                 interface = HID_INTERFACE;
174                 break;
175         default:
176                 BT_DBG("Unknown type!");
177                 return FALSE;
178         }
179
180         BT_DBG("Interface name: %s", interface);
181
182         proxy = dbus_g_proxy_new_for_name(conn, BLUEZ_DBUS_NAME, object_path, interface);
183
184         retv_if(proxy == NULL, FALSE);
185
186         dbus_g_proxy_call(proxy, "GetProperties", &error,
187                                 G_TYPE_INVALID,
188                                 dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE),
189                                 &hash, G_TYPE_INVALID);
190
191         if (error != NULL) {
192                 BT_DBG("Failed to get properties: %s\n", error->message);
193                 g_error_free(error);
194                 g_object_unref(proxy);
195                 return FALSE;
196         }
197
198         if (hash != NULL) {
199                 value = g_hash_table_lookup(hash, "Connected");
200                 connected = value ? g_value_get_boolean(value) : FALSE;
201         }
202
203         g_object_unref(proxy);
204         FN_END;
205         return connected;
206 }