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