dc81b700b26b372d081f91f0e5b6b65533423e46
[platform/core/connectivity/bluetooth-frwk.git] / bt-oal / bluez_hal / src / bt-hal-device-dbus-handler.c
1 /*
2  * BLUETOOTH HAL
3  *
4  * Copyright (c) 2015 -2016 Samsung Electronics Co., Ltd All Rights Reserved.
5  *
6  * Contact: Anupam Roy <anupam.r@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 <stdio.h>
23 #include <stdlib.h>
24 #include <stdbool.h>
25 #include <string.h>
26
27 #include <dbus/dbus-glib.h>
28 #include <dbus/dbus.h>
29 #include <glib.h>
30 #include <gio/gio.h>
31 #include <dlog.h>
32 #include <vconf.h>
33
34 #include <syspopup_caller.h>
35 #include <bundle_internal.h>
36
37 /* BT HAL Headers */
38 #include "bt-hal.h"
39 #include "bt-hal-log.h"
40 #include "bt-hal-msg.h"
41 #include "bt-hal-internal.h"
42 #include "bt-hal-event-receiver.h"
43 #include "bt-hal-dbus-common-utils.h"
44
45 #include "bt-hal-adapter-dbus-handler.h"
46 #include "bt-hal-device-dbus-handler.h"
47 #include "bt-hal-event-receiver.h"
48 #include "bt-hal-agent.h"
49
50 static handle_stack_msg event_cb = NULL;
51
52 /* Forward Delcaration */
53 static void __bt_hal_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res, gpointer user_data);
54
55 static void __bt_hal_unbond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
56                                         gpointer user_data);
57
58 int _bt_hal_device_create_bond(const bt_bdaddr_t *bd_addr)
59 {
60         GDBusProxy *proxy;
61         char address[BT_HAL_ADDRESS_STRING_SIZE] = { 0 };
62         int transport = 0;
63
64         GDBusConnection *conn;
65         char *device_path = NULL;
66         GDBusProxy *adapter_proxy;
67         GError *error = NULL;
68         struct hal_ev_bond_state_changed ev;
69         memset(&ev, 0, sizeof(ev));
70         DBG("+");
71
72         DBG("Transport [%d] Add[0x%x] [0x%x][0x%x][0x%x][0x%x][0x%x]",
73                         transport, bd_addr->address[0], bd_addr->address[1],
74                         bd_addr->address[2], bd_addr->address[3],
75                         bd_addr->address[4], bd_addr->address[5]);
76         conn = _bt_get_system_gconn();
77         if (!conn) {
78                 DBG("Could not get DBUS connection!");
79                 return BT_STATUS_FAIL;
80         }
81
82         _bt_convert_addr_type_to_string(address, bd_addr->address);
83         device_path = _bt_get_device_object_path(address);
84
85         if (device_path == NULL) {
86                 ERR("No searched device, attempt to create device");
87                 GVariant *ret = NULL;
88                 adapter_proxy = _bt_get_adapter_proxy();
89                 if (!adapter_proxy) {
90                         ERR("Could not get Adapter Proxy");
91                         return BT_STATUS_FAIL;
92                 }
93
94                 ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
95                                 g_variant_new("(s)", address),
96                                 G_DBUS_CALL_FLAGS_NONE,
97                                 -1,
98                                 NULL,
99                                 &error);
100
101                 if (error != NULL) {
102                         ERR("CreateDevice Fail: %s", error->message);
103                         g_clear_error(&error);
104                 }
105                 if (ret)
106                         g_variant_unref(ret);
107                 device_path = _bt_get_device_object_path(address);
108
109                 if (device_path == NULL) {
110                         ERR("Device path is still not created!!");
111                         return BT_STATUS_FAIL;
112                 } else {
113                         DBG("Device_path is created[%s]", device_path);
114                 }
115         }
116         proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
117                         NULL, BT_HAL_BLUEZ_NAME,
118                         device_path, BT_HAL_DEVICE_INTERFACE,  NULL, NULL);
119
120         g_free(device_path);
121         if (!proxy) {
122                 ERR("Could not get Device Proxy");
123                 return BT_STATUS_FAIL;
124         }
125
126         g_dbus_proxy_call(proxy, "Pair",
127                         g_variant_new("(y)", transport),
128                         G_DBUS_CALL_FLAGS_NONE,
129                         BT_HAL_MAX_DBUS_TIMEOUT,
130                         NULL,
131                         (GAsyncReadyCallback)__bt_hal_bond_device_cb,
132                         NULL);
133
134         /* Prepare to send Bonding event event to HAL bluetooth */
135         ev.status = BT_STATUS_SUCCESS;
136         ev.state = BT_BOND_STATE_BONDING;
137
138         _bt_convert_addr_string_to_type(ev.bdaddr, address);
139
140         if (!event_cb)
141                 event_cb = _bt_hal_get_stack_message_handler();
142         if (event_cb) {
143                 DBG("Sending HAL_EV_BOND_STATE_CHANGED event");
144                 event_cb(HAL_EV_BOND_STATE_CHANGED, (void*)&ev, sizeof(ev));
145         }
146
147         DBG("-");
148         return BT_STATUS_SUCCESS;
149 }
150
151 int _bt_hal_device_remove_bond(const bt_bdaddr_t *bd_addr)
152 {
153         char *device_path = NULL;
154         GDBusProxy *adapter_proxy = NULL;
155         GDBusProxy *device_proxy = NULL;
156         GDBusConnection *conn;
157         GError *error = NULL;
158         GVariant *ret = NULL;
159         char address[BT_HAL_ADDRESS_STRING_SIZE] = { 0 };
160
161         DBG("Add[0x%x] [0x%x][0x%x][0x%x][0x%x][0x%x]",
162                         bd_addr->address[0], bd_addr->address[1],
163                         bd_addr->address[2], bd_addr->address[3],
164                         bd_addr->address[4], bd_addr->address[5]);
165
166         adapter_proxy = _bt_get_adapter_proxy();
167         if (!adapter_proxy) {
168                 ERR("Could not get Adapter Proxy");
169                 return BT_STATUS_FAIL;
170         }
171
172         _bt_convert_addr_type_to_string(address, bd_addr->address);
173
174         device_path = _bt_get_device_object_path(address);
175
176         /* This is a special case, bluedroid always sends success to HAL even if device is already removed
177            whereas bluez sends BLUETOOTH_ERROR_NOT_PAIRED. However we will return Failure
178            in case of bluez*/
179         if (device_path == NULL) {
180                 ERR("No paired device");
181                 return BT_STATUS_FAIL;
182         }
183
184         conn = _bt_get_system_gconn();
185         if (conn == NULL) {
186                 ERR("conn is NULL");
187                 return BT_STATUS_FAIL;
188         }
189
190
191         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
192                         NULL, BT_HAL_BLUEZ_NAME,
193                         device_path, BT_HAL_PROPERTIES_INTERFACE,  NULL, NULL);
194
195         if (device_proxy != NULL) {
196
197                 ret = g_dbus_proxy_call_sync(device_proxy, "Get",
198                                 g_variant_new("(ss)", BT_HAL_DEVICE_INTERFACE, "Paired"),
199                                 G_DBUS_CALL_FLAGS_NONE,
200                                 -1,
201                                 NULL,
202                                 &error);
203                 if (error) {
204                         ERR("Getting property failed: [%s]\n", error->message);
205                         g_error_free(error);
206                         return BT_STATUS_FAIL;
207                 } else {
208                         if (!ret) {
209                                 ERR("No paired device");
210                                 g_object_unref(device_proxy);
211                                 return BT_STATUS_FAIL;
212                         }
213                         g_variant_unref(ret);
214                 }
215                 g_object_unref(device_proxy);
216         }
217
218         g_dbus_proxy_call(adapter_proxy, "UnpairDevice",
219                         g_variant_new("(o)", device_path),
220                         G_DBUS_CALL_FLAGS_NONE,
221                         BT_HAL_MAX_DBUS_TIMEOUT,
222                         NULL,
223                         (GAsyncReadyCallback)__bt_hal_unbond_device_cb,
224                         (gpointer)device_path);
225
226         DBG("-");
227         return BT_STATUS_SUCCESS;
228 }
229
230 static void __bt_hal_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
231                                         gpointer user_data)
232 {
233         GError *err = NULL;
234         const char *device_path;
235         int result = BT_STATUS_SUCCESS;
236         struct hal_ev_bond_state_changed ev;
237         memset(&ev, 0, sizeof(ev));
238         char dev_address[18];
239         DBG("+");
240
241 #ifdef TIZEN_SYSPOPUP_SUPPORTED
242         /* Terminate ALL system popup */
243         syspopup_destroy_all();
244 #endif
245
246         g_dbus_proxy_call_finish(proxy, res, &err);
247         device_path = g_dbus_proxy_get_object_path(proxy);
248         DBG("Device path: %s", device_path);
249         _bt_convert_device_path_to_address(device_path, dev_address);
250         DBG("Remote Device address [%s]", dev_address);
251
252         if (err != NULL) {
253                 g_dbus_error_strip_remote_error(err);
254                 ERR("@@@Error occured in CreateBonding [%s]", err->message);
255                 if (g_strrstr(err->message, "Already Exists")) {
256                         ERR("Still bond existing even after remove");
257                         result = BT_STATUS_AUTH_FAILURE;
258                 } else if (g_strrstr(err->message, "Authentication Rejected")) {
259                         INFO("REJECTED");
260                         result = BT_STATUS_AUTH_REJECTED;
261                 } else if (_bt_hal_agent_is_canceled() ||
262                                 g_strrstr(err->message, "Authentication Canceled")) {
263                         INFO("Cancelled by USER");
264                         result = BT_STATUS_AUTH_FAILURE;
265                 } else if (g_strrstr(err->message, "In Progress")) {
266                         INFO("Bond in progress, cancel and retry");
267                 } else if (g_strrstr(err->message, "Authentication Failed")) {
268                         INFO("Authentication Failed");
269                         result = BT_STATUS_AUTH_FAILURE;
270                 } else if (g_strrstr(err->message, "Page Timeout")) {
271                         INFO("Page Timeout");
272                         /* This is the special case
273                            As soon as call bluetooth_bond_device, try to cancel bonding.
274                            In this case, before completing to call 'CreatePairedDevice' method
275                            the procedure is stopped. So 'Cancle' error is not return.
276                          */
277                         result = BT_STATUS_RMT_DEV_DOWN;
278                 } else if (g_strrstr(err->message, BT_HAL_TIMEOUT_MESSAGE)) {
279                         INFO("Timeout");
280                         result = BT_STATUS_FAIL;
281                 } else if (g_strrstr(err->message, "Connection Timeout")) {
282                         /* Pairing request timeout */
283                         result = BT_STATUS_RMT_DEV_DOWN;
284                 } else if (g_strrstr(err->message, "Authentication Timeout")) {
285                         /* Pairing request timeout */
286                         result = BT_STATUS_AUTH_FAILURE;
287                 } else {
288                         DBG("Default case: Pairing failed");
289                         result = BT_STATUS_AUTH_FAILURE;
290                 }
291         }
292
293         if (result == BT_STATUS_AUTH_FAILURE ||
294                         result == BT_STATUS_RMT_DEV_DOWN ||
295                         result == BT_STATUS_AUTH_REJECTED ||
296                         result == BT_STATUS_FAIL) {
297                 DBG("Bonding Failed!!");
298         } else {
299                 DBG("Bonding Success!!");
300         }
301
302         /* Prepare to send event to HAL bluetooth */
303         ev.status = result;
304         if (result == BT_STATUS_SUCCESS)
305                 ev.state = BT_BOND_STATE_BONDED;
306         else
307                 ev.state = BT_BOND_STATE_NONE;
308
309         _bt_convert_addr_string_to_type(ev.bdaddr, dev_address);
310
311         if (!event_cb)
312                 event_cb = _bt_hal_get_stack_message_handler();
313         if (event_cb) {
314                 DBG("Sending HAL_EV_BOND_STATE_CHANGED event");
315                 event_cb(HAL_EV_BOND_STATE_CHANGED, (void*)&ev, sizeof(ev));
316         }
317         DBG("-");
318 }
319
320 static void __bt_hal_unbond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
321                                         gpointer user_data)
322 {
323         GError *err = NULL;
324         char *device_path = NULL;
325         char dev_address[18];
326         int result = BT_STATUS_SUCCESS;
327         struct hal_ev_bond_state_changed ev;
328         memset(&ev, 0, sizeof(ev));
329         DBG("+");
330
331         g_dbus_proxy_call_finish(proxy, res, &err);
332
333         if (err != NULL) {
334                 ERR("Error occured in RemoveBonding [%s]\n", err->message);
335                 result = BT_STATUS_FAIL;
336         }
337
338         g_error_free(err);
339
340         /* Prepare to send event to HAL bluetooth */
341         ev.status = result;
342         ev.state = BT_BOND_STATE_NONE;
343
344         device_path = (char *)user_data;
345         _bt_convert_device_path_to_address(device_path, dev_address);
346         _bt_convert_addr_string_to_type(ev.bdaddr, dev_address);
347
348         if (!event_cb)
349                 event_cb = _bt_hal_get_stack_message_handler();
350         if (event_cb) {
351                 DBG("Sending HAL_EV_BOND_STATE_CHANGED event");
352                 event_cb(HAL_EV_BOND_STATE_CHANGED, (void*)&ev, sizeof(ev));
353         }
354         g_free(device_path);
355         DBG("-");
356 }
357
358 static gboolean __bt_device_bonded_device_info_cb(gpointer user_data)
359 {
360         /* Buffer and propety count management */
361         uint8_t buf[BT_HAL_MAX_PROPERTY_BUF_SIZE];
362         struct hal_ev_remote_device_props *ev = (void*) buf;;
363         size_t size = 0;
364
365         GVariant *result = user_data;
366         GVariantIter *property_iter;
367         GVariantIter *char_value_iter;
368
369         const gchar *address = NULL;
370         const gchar *name = NULL;
371         unsigned int cod = 0;
372         gint rssi = 0;
373         gboolean trust = FALSE;
374         gboolean paired = FALSE;
375         int connected = 0;
376         GByteArray *manufacturer_data = NULL;
377         const gchar *key;
378         GVariant *value;
379         guint8 char_value;
380         unsigned int data_len = 0;
381
382         memset(buf, 0, sizeof(buf));
383         size = sizeof(*ev);
384         ev->num_props = 0;
385         ev->status = BT_STATUS_SUCCESS;
386
387         g_variant_get(result, "(a{sv})", &property_iter);
388         while (g_variant_iter_loop(property_iter, "{sv}", &key, &value)) {
389                 if(!g_strcmp0(key, "Address")) {
390                         address = g_variant_get_string(value, NULL);
391                         DBG("Address [%s]", address);
392                         _bt_convert_addr_string_to_type(ev->bdaddr, address);
393                 } else if (!g_strcmp0(key, "Alias")) {
394                         name = g_variant_get_string(value, NULL);
395                         DBG("Alias [%s]", name);
396                         size += __bt_insert_hal_properties(buf + size,
397                                         HAL_PROP_DEVICE_FRIENDLY_NAME, strlen(name) + 1, name);
398                         ev->num_props++;
399                 } else if (!g_strcmp0(key, "Class")) {
400                         cod = g_variant_get_uint32(value);
401                         DBG("Class [%d]", cod);
402                         size += __bt_insert_hal_properties(buf + size,
403                                         HAL_PROP_DEVICE_CLASS, sizeof(unsigned int), &cod);
404                         ev->num_props++;
405                 } else if (!g_strcmp0(key, "Connected")) {
406                         connected = g_variant_get_byte(value);
407                         DBG("Connected [%d]", connected);
408                         size += __bt_insert_hal_properties(buf + size, HAL_PROP_DEVICE_CONNECTED,
409                                         sizeof(unsigned int), &connected);
410                         ev->num_props++;
411                 } else if (!g_strcmp0(key,"Paired")) {
412                         paired = g_variant_get_boolean(value);
413                         DBG("Paired [%d]", paired);
414                         size += __bt_insert_hal_properties(buf + size,
415                                         HAL_PROP_DEVICE_PAIRED, sizeof(unsigned int), &paired);
416                         ev->num_props++;
417                 } else if (!g_strcmp0(key, "Trusted")) {
418                         trust = g_variant_get_boolean(value);
419                         DBG("Trusted [%d]", trust);
420                         size += __bt_insert_hal_properties(buf + size,
421                                         HAL_PROP_DEVICE_TRUSTED, sizeof(unsigned int), &trust);
422                         ev->num_props++;
423                 } else if (!g_strcmp0(key, "Name")) {
424                         name = g_variant_get_string(value, NULL);
425                         DBG("Name [%s]", name);
426                         size += __bt_insert_hal_properties(buf + size,
427                                         HAL_PROP_DEVICE_NAME, strlen(name) + 1, name);
428                         ev->num_props++;
429                 } else if (!g_strcmp0(key, "RSSI")) {
430                         rssi = g_variant_get_int16(value);
431                         DBG("RSSI [%d]", rssi);
432                         size += __bt_insert_hal_properties(buf + size,
433                                         HAL_PROP_DEVICE_RSSI, sizeof(unsigned int), &rssi);
434                         ev->num_props++;
435                 } else if (!g_strcmp0(key, "UUIDs")) {
436                         char **uuid_value;
437                         int uuid_count = 0;
438                         gsize size1 = 0;
439                         int i =0;
440                         size1 = g_variant_get_size(value);
441                         int num_props_tmp = ev->num_props;
442                         if (size1 > 0) {
443                                 uuid_value = (char **)g_variant_get_strv(value, &size1);
444                                 for (i = 0; uuid_value[i] != NULL; i++)
445                                         uuid_count++;
446                                 /* UUID collection */
447                                 uint8_t uuids[BT_HAL_STACK_UUID_SIZE * uuid_count];
448                                 for (i = 0; uuid_value[i] != NULL; i++) {
449                                         char *uuid_str = NULL;
450                                         uint8_t uuid[BT_HAL_STACK_UUID_SIZE];
451                                         memset(uuid, 0x00, BT_HAL_STACK_UUID_SIZE);
452                                         uuid_str = g_strdup(uuid_value[i]);
453                                         DBG("UUID string [%s]\n", uuid_str);
454                                         _bt_convert_uuid_string_to_type(uuid, uuid_str);
455                                         memcpy(uuids + i * BT_HAL_STACK_UUID_SIZE, uuid, BT_HAL_STACK_UUID_SIZE);
456                                 }
457                                 size += __bt_insert_hal_properties(buf + size, HAL_PROP_DEVICE_UUIDS,
458                                                 (BT_HAL_STACK_UUID_SIZE * uuid_count), uuids);
459                                 ev->num_props = num_props_tmp + 1;
460                                 g_free(uuid_value);
461                         }
462                 } else if (!g_strcmp0(key, "ManufacturerDataLen")) {
463                         data_len = g_variant_get_uint16(value);
464                         DBG("ManufacturerDataLen [%d]", data_len);
465                 } else if (!g_strcmp0(key, "ManufacturerData")) {
466                         manufacturer_data = g_byte_array_new();
467                         g_variant_get(value, "ay", &char_value_iter);
468                         while(g_variant_iter_loop(char_value_iter, "y",  &char_value)) {
469                                 g_byte_array_append(manufacturer_data, &char_value, 1);
470                         }
471
472                         if (manufacturer_data) {
473                                 if (manufacturer_data->len > 0) {
474                                         size += __bt_insert_hal_properties(
475                                                         buf + size, HAL_PROP_DEVICE_BLE_ADV_DATA,
476                                                         manufacturer_data->len, manufacturer_data->data);
477                                         ev->num_props++;
478                                 }
479                         }
480                         g_byte_array_free(manufacturer_data, FALSE);
481                 } else {
482                         ERR("Unhandled Property:[%s]", key);
483                 }
484         }
485
486         DBG("trust: %d, paired: %d", trust, paired);
487         if (!event_cb)
488                 event_cb = _bt_hal_get_stack_message_handler();
489         if (!event_cb) {
490                 ERR("event_cb is NULL");
491                 goto done;
492         }
493
494         if ((paired == FALSE) && (trust == FALSE)) {
495                 ev->status = BT_STATUS_FAIL;
496                 ev->num_props = 0;
497                 size = sizeof(*ev);
498                 DBG("Send Remote Device properties event to HAL,"
499                                 " Num Prop [%d] total size [%d]",ev->num_props, size);
500                 event_cb(HAL_EV_REMOTE_DEVICE_PROPS, (void*) buf, size);
501         } else {
502                 if (size > 2) {
503                         DBG("Send Remote Device properties event to HAL,"
504                                 " Num Prop [%d] total size [%d]",ev->num_props, size);
505                         event_cb(HAL_EV_REMOTE_DEVICE_PROPS, (void*) buf, size);
506                 }
507         }
508
509 done:
510         g_variant_unref(result);
511         return FALSE;
512 }
513
514 int _bt_hal_dbus_get_remote_device_properties(bt_bdaddr_t *remote_addr)
515 {
516         char *device_path = NULL;
517         char address[BT_HAL_ADDRESS_STRING_SIZE] = { 0 };
518         GError *error = NULL;
519         GDBusProxy *device_proxy;
520         GDBusConnection *conn;
521         GVariant *result;
522
523         if(!remote_addr) {
524                 ERR("Invalid device address ptr received");
525                 return BT_STATUS_PARM_INVALID;
526         }
527
528         _bt_convert_addr_type_to_string(address, remote_addr->address);
529         device_path = _bt_get_device_object_path(address);
530         if (!device_path) {
531                 ERR("Device not paired");
532                 return BT_STATUS_FAIL;
533         }
534
535         conn = _bt_get_system_gconn();
536         if (!conn) {
537                 ERR("_bt_get_system_gconn failed");
538                 return BT_STATUS_FAIL;
539         }
540
541         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
542                         NULL,
543                         BT_HAL_BLUEZ_NAME,
544                         device_path,
545                         BT_HAL_PROPERTIES_INTERFACE,
546                         NULL, NULL);
547
548         if (!device_proxy) {
549                 ERR("Error creating device_proxy");
550                 g_free(device_path);
551                 return BT_STATUS_FAIL;
552         }
553
554         result = g_dbus_proxy_call_sync(device_proxy,
555                         "GetAll",
556                         g_variant_new("(s)", BT_HAL_DEVICE_INTERFACE),
557                         G_DBUS_CALL_FLAGS_NONE,
558                         -1,
559                         NULL,
560                         &error);
561
562         if (!result) {
563                 ERR("Error occured in Proxy call");
564                 if (error != NULL) {
565                         ERR("Error occured in Proxy call (Error: %s)", error->message);
566                         g_clear_error(&error);
567                 }
568                 g_object_unref(device_proxy);
569                 g_free(device_path);
570                 return BT_STATUS_FAIL;
571         }
572
573         g_object_unref(device_proxy);
574         g_free(device_path);
575         /*
576          * As we need to provide async callback to user from HAL, simply schedule a
577          * callback method which will carry actual result
578          */
579         g_idle_add(__bt_device_bonded_device_info_cb, (gpointer)result);
580
581         DBG("-");
582         return BT_STATUS_SUCCESS;
583 }
584
585 static int __bt_hal_dbus_set_remote_device_alias(bt_bdaddr_t *remote_addr, char *alias)
586 {
587         char address[BT_HAL_ADDRESS_STRING_SIZE];
588         gchar *device_path = NULL;
589         GDBusProxy *adapter_proxy;
590         GDBusProxy *device_proxy;
591         GError *error = NULL;
592         GDBusConnection *conn;
593         GVariant *result;
594
595         adapter_proxy = _bt_get_adapter_proxy();
596                 if (!adapter_proxy) {
597                         ERR("Could not get Adapter Proxy");
598                         return BT_STATUS_FAIL;
599                 }
600
601         conn = _bt_get_system_gconn();
602         if (!conn) {
603                 ERR("_bt_get_system_gconn failed");
604                 return BT_STATUS_FAIL;
605         }
606
607         _bt_convert_addr_type_to_string(address, remote_addr->address);
608         INFO("Address: %s, Alias: %s", address, alias);
609
610         device_path = _bt_get_device_object_path(address);
611         if (device_path == NULL) {
612                 ERR("No paired device");
613                 return BT_STATUS_FAIL;
614         }
615
616         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
617                         NULL, BT_HAL_BLUEZ_NAME, device_path,
618                         BT_HAL_PROPERTIES_INTERFACE, NULL, NULL);
619         g_free(device_path);
620         if (!device_proxy) {
621                 ERR("Error creating device_proxy");
622                 return BT_STATUS_FAIL;
623         }
624
625         result = g_dbus_proxy_call_sync(device_proxy, "Set",
626                         g_variant_new("(ssv)",
627                                 BT_HAL_DEVICE_INTERFACE,
628                                 "Alias", g_variant_new("s", alias)),
629                         G_DBUS_CALL_FLAGS_NONE,
630                         -1,
631                         NULL,
632                         &error);
633         g_object_unref(device_proxy);
634         if (!result) {
635                 ERR("Error occured in Proxy call");
636                 if (error != NULL) {
637                         ERR("Error occured in Proxy call (Error: %s)", error->message);
638                         g_clear_error(&error);
639                 }
640                 return BT_STATUS_FAIL;
641         }
642         g_variant_unref(result);
643
644         return BT_STATUS_SUCCESS;
645 }
646
647 /* Set Remote Device Properties */
648 int _bt_hal_dbus_set_remote_device_property(
649                 bt_bdaddr_t *remote_addr, const bt_property_t *property)
650 {
651         int result;
652
653         DBG("+");
654
655         if (remote_addr == NULL) {
656                 ERR("Invalid parameters received");
657                 return BT_STATUS_PARM_INVALID;
658         }
659
660         if (property == NULL || property->val == NULL) {
661                 ERR("Invalid parameters received");
662                 return BT_STATUS_PARM_INVALID;
663         }
664
665         switch (property->type) {
666         case BT_PROPERTY_REMOTE_FRIENDLY_NAME:
667                 result =  __bt_hal_dbus_set_remote_device_alias(
668                                 remote_addr, (char *) property->val);
669                 break;
670         default:
671                 result = BT_STATUS_UNSUPPORTED;
672         }
673
674         DBG("Result= [%d]", result);
675         DBG("-");
676         return result;
677 }