2 * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
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
8 * http://floralicense.org/license/
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.
18 #include <dbus/dbus.h>
19 #include <dbus/dbus-glib-bindings.h>
22 #include "bt-type-define.h"
24 #include "bt-dbus-method.h"
27 /**********************************************************************
29 ***********************************************************************/
31 int __bt_get_adapter_path(DBusGConnection *GConn, char *path)
36 DBusGProxy *manager_proxy = NULL;
37 char *adapter_path = NULL;
38 int ret = BT_UG_ERROR_NONE;
41 retv_if(GConn == NULL, -1);
42 retv_if(path == NULL, -1);
44 manager_proxy = dbus_g_proxy_new_for_name(GConn, BLUEZ_DBUS_NAME, "/",
47 retv_if(manager_proxy == NULL, -1);
49 dbus_g_proxy_call(manager_proxy, "DefaultAdapter", &error,
50 G_TYPE_INVALID, DBUS_TYPE_G_OBJECT_PATH, &adapter_path,
54 BT_DBG("Getting DefaultAdapter failed: [%s]\n", error->message);
56 g_object_unref(manager_proxy);
60 if (adapter_path == NULL) {
61 g_object_unref(manager_proxy);
65 len = g_strlcpy(path, adapter_path, BT_ADAPTER_PATH_LEN);
67 if (len >= BT_ADAPTER_PATH_LEN) {
68 BT_DBG("The copied len is too large");
72 g_object_unref(manager_proxy);
80 /**********************************************************************
82 ***********************************************************************/
84 DBusGProxy *_bt_get_adapter_proxy(DBusGConnection *conn)
87 DBusGProxy *adapter = NULL;
88 char adapter_path[BT_ADAPTER_PATH_LEN] = { 0 };
90 retv_if(conn == NULL, NULL);
92 if (__bt_get_adapter_path(conn, adapter_path) < 0) {
93 BT_DBG("Could not get adapter path\n");
97 adapter = dbus_g_proxy_new_for_name(conn, BLUEZ_DBUS_NAME,
98 adapter_path, ADAPTER_INTERFACE);
104 void _bt_reset_environment(void)
108 DBusGConnection *conn;
110 conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, NULL);;
111 ret_if(conn == NULL);
113 proxy = dbus_g_proxy_new_for_name(conn, BT_CORE_NAME,
114 BT_CORE_PATH, BT_CORE_INTERFACE);
116 if (dbus_g_proxy_call(proxy, "ResetAdapter", NULL,
117 G_TYPE_INVALID, G_TYPE_INVALID) == FALSE) {
118 BT_ERR("Bt core call failed");
121 dbus_g_connection_unref(conn);
126 gboolean _bt_is_profile_connected(int connected_type,
127 DBusGConnection *conn,
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;
141 retv_if(conn == NULL, FALSE);
142 retv_if(addr == NULL, FALSE);
144 adapter = _bt_get_adapter_proxy(conn);
146 retv_if(adapter == NULL, FALSE);
148 _bt_util_addr_type_to_addr_string(addr_str, addr);
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);
155 g_object_unref(adapter);
158 BT_DBG("Failed to Find device: %s\n", error->message);
163 retv_if(object_path == NULL, FALSE);
165 switch (connected_type) {
166 case BT_HEADSET_CONNECTED:
167 interface = HEADSET_INTERFACE;
169 case BT_STEREO_HEADSET_CONNECTED:
170 interface = SYNK_INTERFACE;
172 case BT_HID_CONNECTED:
173 interface = HID_INTERFACE;
176 BT_DBG("Unknown type!");
180 BT_DBG("Interface name: %s", interface);
182 proxy = dbus_g_proxy_new_for_name(conn, BLUEZ_DBUS_NAME, object_path, interface);
184 retv_if(proxy == NULL, FALSE);
186 dbus_g_proxy_call(proxy, "GetProperties", &error,
188 dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE),
189 &hash, G_TYPE_INVALID);
192 BT_DBG("Failed to get properties: %s\n", error->message);
194 g_object_unref(proxy);
199 value = g_hash_table_lookup(hash, "Connected");
200 connected = value ? g_value_get_boolean(value) : FALSE;
203 g_object_unref(proxy);