b6a0e7718e12ffea19d7d00cb23cbf69f28c100e
[platform/core/connectivity/bluetooth-frwk.git] / bt-core / bt-core-dbus-handler.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * 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
18 #include <gio/gio.h>
19 #include <vconf.h>
20 #include <vconf-keys.h>
21
22 #include "bt-core-adapter.h"
23 #include "bt-core-common.h"
24 #include "bt-core-dbus-handler.h"
25 #include "bt-internal-types.h"
26 #include "bt-core-noti-handler.h"
27 #include "bt-core-main.h"
28
29 #define BT_SERVICE_NAME         "org.projectx.bt"
30 #define BT_SERVICE_PATH         "/org/projectx/bt_service"
31
32 #ifdef HPS_FEATURE
33 #define BT_HPS_SERVICE_NAME "org.projectx.httpproxy"
34 #define BT_HPS_OBJECT_PATH "/org/projectx/httpproxy"
35 #define BT_HPS_INTERFACE_NAME "org.projectx.httpproxy_service"
36 #endif
37
38 static GDBusConnection *service_gconn;
39 static GDBusProxy *service_gproxy;
40 #ifdef HPS_FEATURE
41 static GDBusProxy *hps_gproxy;
42 #endif
43
44 void _bt_core_fill_garray_from_variant(GVariant *var, GArray *param)
45 {
46         char *data;
47         int size;
48
49         size = g_variant_get_size(var);
50         if (size > 0) {
51                 data = (char *)g_variant_get_data(var);
52                 if (data)
53                         param = g_array_append_vals(param, data, size);
54
55         }
56 }
57
58 GDBusConnection * _bt_core_get_gdbus_connection(void)
59 {
60         GError *err = NULL;
61
62         if (service_gconn == NULL)
63                 service_gconn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
64
65         if (!service_gconn) {
66                 if (err) {
67                         BT_ERR("Unable to connect to dbus: %s", err->message);
68                         g_clear_error(&err);
69                 }
70                 return NULL;
71         }
72
73         return service_gconn;
74 }
75
76 static GDBusProxy *__bt_core_gdbus_init_service_proxy(void)
77 {
78         GDBusProxy *proxy;
79         GError *err = NULL;
80         GDBusConnection *conn;
81
82         conn = _bt_core_get_gdbus_connection();
83         if (!conn)
84                 return NULL;
85
86         proxy =  g_dbus_proxy_new_sync(conn,
87                         G_DBUS_PROXY_FLAGS_NONE, NULL,
88                         BT_SERVICE_NAME,
89                         BT_SERVICE_PATH,
90                         BT_SERVICE_NAME,
91                         NULL, &err);
92         if (!proxy) {
93                 if (err) {
94                          BT_ERR("Unable to create proxy: %s", err->message);
95                          g_clear_error(&err);
96                 }
97
98                 return NULL;
99         }
100
101         service_gproxy = proxy;
102
103         return proxy;
104 }
105
106 GDBusProxy *_bt_core_gdbus_get_service_proxy(void)
107 {
108         return (service_gproxy) ? service_gproxy : __bt_core_gdbus_init_service_proxy();
109 }
110
111 #ifdef HPS_FEATURE
112 int _bt_core_start_httpproxy(void)
113 {
114         GVariant *variant = NULL;
115         unsigned char enabled;
116
117         BT_DBG(" ");
118
119         hps_gproxy = _bt_core_gdbus_get_hps_proxy();
120         if (!hps_gproxy) {
121                 BT_DBG("Couldn't get service proxy");
122                 return -1;
123         }
124
125         variant = g_dbus_proxy_call_sync(hps_gproxy, "enable",
126                                 NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL);
127         if (variant) {
128                 g_variant_get(variant, "(y)", &enabled);
129                 BT_ERR("HPS enabled status 0x%x", enabled);
130         }
131         return 0;
132 }
133
134 int _bt_core_stop_httpproxy(void)
135 {
136         GVariant *variant = NULL;
137         unsigned char enabled;
138
139         BT_DBG(" ");
140
141         hps_gproxy = _bt_core_gdbus_get_hps_proxy();
142         if (!hps_gproxy) {
143                 BT_DBG("Couldn't get service proxy");
144                 return -1;
145         }
146
147         variant = g_dbus_proxy_call_sync(hps_gproxy, "disable",
148                                 NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL);
149         if (variant) {
150                 g_variant_get(variant, "(y)", &enabled);
151                 BT_ERR("HPS disabled status 0x%x", enabled);
152         }
153         return 0;
154 }
155
156 static GDBusProxy *_bt_core_gdbus_init_hps_proxy(void)
157 {
158         GDBusProxy *proxy;
159         GError *err = NULL;
160         GDBusConnection *conn;
161
162         BT_DBG(" ");
163
164         conn = _bt_core_get_gdbus_connection();
165         if (!conn)
166                 return NULL;
167
168         proxy =  g_dbus_proxy_new_sync(conn,
169                         G_DBUS_PROXY_FLAGS_NONE, NULL,
170                         BT_HPS_SERVICE_NAME,
171                         BT_HPS_OBJECT_PATH,
172                         BT_HPS_INTERFACE_NAME,
173                         NULL, &err);
174         if (proxy == NULL) {
175                 if (err) {
176                          BT_ERR("Unable to create proxy: %s", err->message);
177                          g_clear_error(&err);
178                 }
179                 return NULL;
180         }
181
182         hps_gproxy = proxy;
183
184         return proxy;
185 }
186
187 GDBusProxy *_bt_core_gdbus_get_hps_proxy(void)
188 {
189         return (hps_gproxy) ? hps_gproxy : _bt_core_gdbus_init_hps_proxy();
190 }
191 #endif
192
193 void _bt_core_gdbus_deinit_proxys(void)
194 {
195         BT_DBG("");
196
197         if (service_gproxy) {
198                 g_object_unref(service_gproxy);
199                 service_gproxy = NULL;
200         }
201
202 #ifdef HPS_FEATURE
203         if (hps_gproxy) {
204                 g_object_unref(hps_gproxy);
205                 hps_gproxy = NULL;
206         }
207 #endif
208
209         if (service_gconn) {
210                 g_object_unref(service_gconn);
211                 service_gconn = NULL;
212         }
213 }
214
215 int _bt_core_service_request(int service_type, int service_function,
216                         GArray *in_param1, GArray *in_param2,
217                         GArray *in_param3, GArray *in_param4,
218                         GArray **out_param1)
219 {
220         GDBusProxy  *proxy;
221         GVariant *ret;
222         GVariant *param1;
223         GVariant *param2;
224         GVariant *param3;
225         GVariant *param4;
226         GVariant *param5;
227
228         int result = BLUETOOTH_ERROR_NONE;
229         GError *error = NULL;
230         GArray *in_param5 = NULL;
231
232         int retry = 5;
233
234         proxy = _bt_core_gdbus_get_service_proxy();
235         if (!proxy)
236                 return BLUETOOTH_ERROR_INTERNAL;
237         in_param5 = g_array_new(TRUE, TRUE, sizeof(gchar));
238
239         while (--retry >= 0) {
240                 param1 = g_variant_new_from_data((const GVariantType *)"ay",
241                                         in_param1->data, in_param1->len,
242                                         TRUE, NULL, NULL);
243                 param2 = g_variant_new_from_data((const GVariantType *)"ay",
244                                         in_param2->data, in_param2->len,
245                                         TRUE, NULL, NULL);
246                 param3 = g_variant_new_from_data((const GVariantType *)"ay",
247                                         in_param3->data, in_param3->len,
248                                         TRUE, NULL, NULL);
249                 param4 = g_variant_new_from_data((const GVariantType *)"ay",
250                                         in_param4->data, in_param4->len,
251                                         TRUE, NULL, NULL);
252                 param5 = g_variant_new_from_data((const GVariantType *)"ay",
253                                         in_param5->data, in_param5->len,
254                                         TRUE, NULL, NULL);
255
256                 ret = g_dbus_proxy_call_sync(proxy, "service_request",
257                                         g_variant_new("(iii@ay@ay@ay@ay@ay)",
258                                                 service_type, service_function,
259                                                 BT_SYNC_REQ, param1,
260                                                 param2, param3,
261                                                 param4, param5),
262                                         G_DBUS_CALL_FLAGS_NONE, 2000,
263                                         NULL, &error);
264                 if (ret == NULL && error != NULL) {
265                         if (error->code == G_IO_ERROR_TIMED_OUT) {
266                                 BT_ERR("D-Bus Timed out.");
267                                 g_clear_error(&error);
268                                 continue;
269                         }
270                 }
271
272                 break;
273         }
274
275         g_array_free(in_param5, TRUE);
276
277         if (ret == NULL) {
278                 /* dBUS-RPC is failed */
279                 BT_ERR("dBUS-RPC is failed");
280
281                 if (error != NULL) {
282                         /* dBUS gives error cause */
283                         BT_ERR("D-Bus API failure: errCode[%x], message[%s]",
284                                error->code, error->message);
285
286                         g_clear_error(&error);
287                 } else {
288                         /* dBUS does not give error cause dBUS-RPC is failed */
289                         BT_ERR("error returned was NULL");
290                 }
291
292                 return BLUETOOTH_ERROR_INTERNAL;
293         }
294
295         param1 = NULL;
296
297         g_variant_get(ret, "(iv)", &result, &param1);
298
299         if (param1) {
300                 *out_param1 = g_array_new(TRUE, TRUE, sizeof(gchar));
301                 _bt_core_fill_garray_from_variant(param1, *out_param1);
302                 g_variant_unref(param1);
303         }
304
305         g_variant_unref(ret);
306
307         return result;
308 }
309
310 static const gchar bt_core_introspection_xml[] =
311 "<node name='/'>"
312 "  <interface name='org.projectx.btcore'>"
313 "     <method name='EnableAdapter'>"
314 "     </method>"
315 "     <method name='DisableAdapter'>"
316 "     </method>"
317 "     <method name='RecoverAdapter'>"
318 "     </method>"
319 "     <method name='ResetAdapter'>"
320 "     </method>"
321 "     <method name='EnableAdapterLe'>"
322 "     </method>"
323 "     <method name='DisableAdapterLe'>"
324 "     </method>"
325 "     <method name='EnableCore'>"
326 "     </method>"
327 "         <method name='SetTransferValue'>"
328 "          <arg type='b' name='value' direction='in'/>"
329 "         </method>"
330 "     <method name='FactoryTestMode'>"
331 "          <arg type='s' name='type' direction='in'/>"
332 "          <arg type='s' name='arg' direction='in'/>"
333 "          <arg type='i' name='ret' direction='out'/>"
334 "     </method>"
335 " </interface>"
336 "</node>";
337
338 static guint obj_id, sig_id1, sig_id2, sig_id3;
339
340 static void __bt_core_dbus_method(GDBusConnection *connection,
341                         const gchar *sender,
342                         const gchar *object_path,
343                         const gchar *interface_name,
344                         const gchar *method_name,
345                         GVariant *parameters,
346                         GDBusMethodInvocation *invocation,
347                         gpointer user_data)
348 {
349         gboolean ret;
350
351         BT_DBG("method %s", method_name);
352
353         if (g_strcmp0(method_name, "EnableAdapter") == 0) {
354                 ret = _bt_core_enable_adapter();
355         } else if (g_strcmp0(method_name, "DisableAdapter") == 0) {
356                 ret = _bt_core_disable_adapter();
357         } else if (g_strcmp0(method_name, "RecoverAdapter") == 0) {
358                 ret = _bt_core_recover_adapter();
359         } else if (g_strcmp0(method_name, "ResetAdapter") == 0) {
360                 ret = __bt_core_reset_adapter();
361         } else if (g_strcmp0(method_name, "EnableAdapterLe") == 0) {
362                 ret = _bt_core_enable_adapter_le();
363         } else if (g_strcmp0(method_name, "DisableAdapterLe") == 0) {
364                 ret = _bt_core_disable_adapter_le();
365         } else if (g_strcmp0(method_name, "EnableCore") == 0) {
366                 ret = _bt_core_enable_core();
367         } else if (g_strcmp0(method_name, "SetTransferValue") == 0) {
368                 gboolean value = FALSE;
369
370                 g_variant_get(parameters, "(b)", &value);
371                 BT_DBG("Transfer value: %d", value);
372
373                 ret = _bt_core_set_transfer_value(value);
374         } else if (g_strcmp0(method_name, "FactoryTestMode") == 0) {
375                 const char *type = NULL;
376                 const char *arg = NULL;
377
378                 g_variant_get(parameters, "(&s&s)", &type, &arg);
379                 ret = _bt_core_factory_test_mode(type, arg);
380                 g_dbus_method_invocation_return_value(invocation,
381                                 g_variant_new("(i)", ret));
382                  return;
383         } else {
384                 ret = FALSE;
385         }
386
387         if (!ret) {
388                 GQuark quark = g_quark_from_string("bt-core");
389                 GError *err = g_error_new(quark, 0, "Failed");
390                 g_dbus_method_invocation_return_gerror(invocation, err);
391                 g_error_free(err);
392         } else {
393                 g_dbus_method_invocation_return_value(invocation, NULL);
394         }
395
396         BT_DBG("-");
397 }
398
399 static const GDBusInterfaceVTable method_table = {
400         __bt_core_dbus_method,
401         NULL,
402         NULL,
403 };
404
405 static GDBusNodeInfo *__bt_core_create_node_info(
406                                         const gchar *introspection_data)
407 {
408         GError *err = NULL;
409         GDBusNodeInfo *node_info = NULL;
410
411         if (introspection_data == NULL)
412                 return NULL;
413
414         node_info = g_dbus_node_info_new_for_xml(introspection_data, &err);
415
416         if (err) {
417                 BT_ERR("Unable to create node: %s", err->message);
418                 g_clear_error(&err);
419         }
420         return node_info;
421 }
422
423 gboolean __is_interface_and_signal_valid(const gchar *interface_name,
424                                                 const gchar *signal_name)
425 {
426         if (g_strcmp0(interface_name, "org.freedesktop.DBus") &&
427                 g_strcmp0(interface_name, "org.freedesktop.DBus.ObjectManager"))
428                 return FALSE;
429
430         if (g_strcmp0(signal_name, "NameOwnerChanged") &&
431                 g_strcmp0(signal_name, "InterfacesAdded") &&
432                 g_strcmp0(signal_name, "InterfacesRemoved"))
433                 return FALSE;
434
435         return TRUE;
436 }
437
438 static void __handle_name_owner_changed(const char *name)
439 {
440         gboolean flight_mode_status;
441
442         BT_DBG("");
443
444         flight_mode_status = _bt_core_is_flight_mode_enabled();
445
446         if (flight_mode_status == FALSE && _bt_is_flightmode_request() == TRUE) {
447                 BT_DBG("flightmode requested");
448                 return;
449         }
450
451         if ((g_strcmp0(name, "org.bluez") == 0) ||
452                 (g_strcmp0(name, "org.projectx.bt") == 0)) {
453                 BT_DBG("%s is terminated", name);
454                 if (_bt_check_terminating_condition() == TRUE) {
455                         _bt_disable_adapter();
456                         _bt_disable_adapter_le();
457                         _bt_core_terminate();
458                 }
459         }
460 }
461
462 static void __bt_core_event_filter(GDBusConnection *connection,
463                                                  const gchar *sender_name,
464                                                  const gchar *object_path,
465                                                  const gchar *interface_name,
466                                                  const gchar *signal_name,
467                                                  GVariant *parameters,
468                                                  gpointer user_data)
469 {
470         if (!__is_interface_and_signal_valid(interface_name, signal_name))
471                 return;
472
473         if (!g_strcmp0(signal_name, "InterfacesAdded")) {
474                 char *obj_path = NULL;
475                 GVariant *optional_param;
476
477                 g_variant_get(parameters, "(&o@a{sa{sv}})",
478                                                 &obj_path, &optional_param);
479
480                 if (g_strcmp0(obj_path, "/org/bluez/hci0") == 0)
481                         _bt_core_adapter_added_cb();
482
483         } else if (!g_strcmp0(signal_name, "InterfacesRemoved")) {
484                 char *obj_path = NULL;
485                 GVariant *optional_param;
486
487                 g_variant_get(parameters, "(&o@as)", &obj_path,
488                                                         &optional_param);
489
490                 if (g_strcmp0(obj_path, "/org/bluez/hci0") == 0)
491                         _bt_core_adapter_removed_cb();
492
493         } else { /* NameOwnerChanged */
494                 const char *name = NULL;
495                 const char *old_owner = NULL;
496                 const char *new_owner = NULL;
497
498                 g_variant_get(parameters, "(&s&s&s)", &name, &old_owner,
499                                                                 &new_owner);
500
501                 if (new_owner != NULL && *new_owner == '\0')
502                         __handle_name_owner_changed(name);
503         }
504 }
505
506 gboolean _bt_core_register_dbus(void)
507 {
508         GError *error = NULL;
509         guint owner_id;
510         GDBusNodeInfo *node_info;
511         gchar *path;
512         GDBusConnection *conn;
513
514         conn = _bt_core_get_gdbus_connection();
515         if (!conn)
516                 return FALSE;
517
518         owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
519                                 BT_CORE_NAME,
520                                 G_BUS_NAME_OWNER_FLAGS_NONE,
521                                 NULL, NULL, NULL,
522                                 NULL, NULL);
523
524         BT_DBG("owner_id is [%d]", owner_id);
525
526         node_info = __bt_core_create_node_info(bt_core_introspection_xml);
527         if (node_info == NULL)
528                 return FALSE;
529
530         path = g_strdup(BT_CORE_PATH);
531         BT_DBG("path is [%s]", path);
532
533         obj_id = g_dbus_connection_register_object(conn, path,
534                                         node_info->interfaces[0],
535                                         &method_table,
536                                         NULL, NULL, &error);
537         if (obj_id == 0) {
538                 BT_ERR("Failed to register: %s", error->message);
539                 g_error_free(error);
540                 g_free(path);
541                 return FALSE;
542         }
543
544         g_free(path);
545
546         sig_id1 = g_dbus_connection_signal_subscribe(conn,
547                                 NULL, "org.freedesktop.DBus",
548                                 "NameOwnerChanged", NULL, NULL, 0,
549                                 __bt_core_event_filter, NULL, NULL);
550         sig_id2 = g_dbus_connection_signal_subscribe(conn,
551                                 NULL, "org.freedesktop.DBus.ObjectManager",
552                                 "InterfacesAdded", NULL, NULL,
553                                 0, __bt_core_event_filter, NULL, NULL);
554         sig_id2 = g_dbus_connection_signal_subscribe(conn,
555                                 NULL, "org.freedesktop.DBus.ObjectManager",
556                                 "InterfacesRemoved", NULL,
557                                 NULL, 0, __bt_core_event_filter, NULL, NULL);
558
559         return TRUE;
560 }
561
562 void  _bt_core_unregister_dbus(void)
563 {
564         GDBusConnection *conn;
565
566         BT_DBG("");
567
568         conn = _bt_core_get_gdbus_connection();
569         if (!conn)
570                 return;
571
572         if (obj_id > 0) {
573                 g_dbus_connection_unregister_object(conn, obj_id);
574                 obj_id = 0;
575         }
576
577         if (sig_id1 > 0) {
578                 g_dbus_connection_signal_unsubscribe(conn, sig_id1);
579                 sig_id1 = 0;
580         }
581         if (sig_id2 > 0) {
582                 g_dbus_connection_signal_unsubscribe(conn, sig_id2);
583                 sig_id2 = 0;
584         }
585         if (sig_id3 > 0) {
586                 g_dbus_connection_signal_unsubscribe(conn, sig_id3);
587                 sig_id3 = 0;
588         }
589 }