2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <vconf-keys.h>
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"
29 #define BT_SERVICE_NAME "org.projectx.bt"
30 #define BT_SERVICE_PATH "/org/projectx/bt_service"
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"
38 static GDBusConnection *service_gconn;
39 static GDBusProxy *service_gproxy;
41 static GDBusProxy *hps_gproxy;
44 void _bt_core_fill_garray_from_variant(GVariant *var, GArray *param)
49 size = g_variant_get_size(var);
51 data = (char *)g_variant_get_data(var);
53 param = g_array_append_vals(param, data, size);
58 GDBusConnection * _bt_core_get_gdbus_connection(void)
62 if (service_gconn == NULL)
63 service_gconn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
67 BT_ERR("Unable to connect to dbus: %s", err->message);
76 static GDBusProxy *__bt_core_gdbus_init_service_proxy(void)
80 GDBusConnection *conn;
82 conn = _bt_core_get_gdbus_connection();
86 proxy = g_dbus_proxy_new_sync(conn,
87 G_DBUS_PROXY_FLAGS_NONE, NULL,
94 BT_ERR("Unable to create proxy: %s", err->message);
101 service_gproxy = proxy;
106 GDBusProxy *_bt_core_gdbus_get_service_proxy(void)
108 return (service_gproxy) ? service_gproxy : __bt_core_gdbus_init_service_proxy();
112 int _bt_core_start_httpproxy(void)
114 GVariant *variant = NULL;
115 unsigned char enabled;
119 hps_gproxy = _bt_core_gdbus_get_hps_proxy();
121 BT_DBG("Couldn't get service proxy");
125 variant = g_dbus_proxy_call_sync(hps_gproxy, "enable",
126 NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL);
128 g_variant_get(variant, "(y)", &enabled);
129 BT_ERR("HPS enabled status 0x%x", enabled);
134 int _bt_core_stop_httpproxy(void)
136 GVariant *variant = NULL;
137 unsigned char enabled;
141 hps_gproxy = _bt_core_gdbus_get_hps_proxy();
143 BT_DBG("Couldn't get service proxy");
147 variant = g_dbus_proxy_call_sync(hps_gproxy, "disable",
148 NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL);
150 g_variant_get(variant, "(y)", &enabled);
151 BT_ERR("HPS disabled status 0x%x", enabled);
156 static GDBusProxy *_bt_core_gdbus_init_hps_proxy(void)
160 GDBusConnection *conn;
164 conn = _bt_core_get_gdbus_connection();
168 proxy = g_dbus_proxy_new_sync(conn,
169 G_DBUS_PROXY_FLAGS_NONE, NULL,
172 BT_HPS_INTERFACE_NAME,
176 BT_ERR("Unable to create proxy: %s", err->message);
187 GDBusProxy *_bt_core_gdbus_get_hps_proxy(void)
189 return (hps_gproxy) ? hps_gproxy : _bt_core_gdbus_init_hps_proxy();
193 void _bt_core_gdbus_deinit_proxys(void)
197 if (service_gproxy) {
198 g_object_unref(service_gproxy);
199 service_gproxy = NULL;
204 g_object_unref(hps_gproxy);
210 g_object_unref(service_gconn);
211 service_gconn = NULL;
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,
228 int result = BLUETOOTH_ERROR_NONE;
229 GError *error = NULL;
230 GArray *in_param5 = NULL;
234 proxy = _bt_core_gdbus_get_service_proxy();
236 return BLUETOOTH_ERROR_INTERNAL;
237 in_param5 = g_array_new(TRUE, TRUE, sizeof(gchar));
239 while (--retry >= 0) {
240 param1 = g_variant_new_from_data((const GVariantType *)"ay",
241 in_param1->data, in_param1->len,
243 param2 = g_variant_new_from_data((const GVariantType *)"ay",
244 in_param2->data, in_param2->len,
246 param3 = g_variant_new_from_data((const GVariantType *)"ay",
247 in_param3->data, in_param3->len,
249 param4 = g_variant_new_from_data((const GVariantType *)"ay",
250 in_param4->data, in_param4->len,
252 param5 = g_variant_new_from_data((const GVariantType *)"ay",
253 in_param5->data, in_param5->len,
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,
262 G_DBUS_CALL_FLAGS_NONE, 2000,
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);
275 g_array_free(in_param5, TRUE);
278 /* dBUS-RPC is failed */
279 BT_ERR("dBUS-RPC is failed");
282 /* dBUS gives error cause */
283 BT_ERR("D-Bus API failure: errCode[%x], message[%s]",
284 error->code, error->message);
286 g_clear_error(&error);
288 /* dBUS does not give error cause dBUS-RPC is failed */
289 BT_ERR("error returned was NULL");
292 return BLUETOOTH_ERROR_INTERNAL;
297 g_variant_get(ret, "(iv)", &result, ¶m1);
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);
305 g_variant_unref(ret);
310 static const gchar bt_core_introspection_xml[] =
312 " <interface name='org.projectx.btcore'>"
313 " <method name='EnableAdapter'>"
315 " <method name='DisableAdapter'>"
317 " <method name='RecoverAdapter'>"
319 " <method name='ResetAdapter'>"
321 " <method name='EnableAdapterLe'>"
323 " <method name='DisableAdapterLe'>"
325 " <method name='EnableCore'>"
327 " <method name='FactoryTestMode'>"
328 " <arg type='s' name='type' direction='in'/>"
329 " <arg type='s' name='arg' direction='in'/>"
330 " <arg type='i' name='ret' direction='out'/>"
335 static guint obj_id, sig_id1, sig_id2, sig_id3;
337 static void __bt_core_dbus_method(GDBusConnection *connection,
339 const gchar *object_path,
340 const gchar *interface_name,
341 const gchar *method_name,
342 GVariant *parameters,
343 GDBusMethodInvocation *invocation,
348 BT_DBG("method %s", method_name);
350 if (g_strcmp0(method_name, "EnableAdapter") == 0) {
351 ret = _bt_core_enable_adapter();
352 } else if (g_strcmp0(method_name, "DisableAdapter") == 0) {
353 ret = _bt_core_disable_adapter();
354 } else if (g_strcmp0(method_name, "RecoverAdapter") == 0) {
355 ret = _bt_core_recover_adapter();
356 } else if (g_strcmp0(method_name, "ResetAdapter") == 0) {
357 ret = __bt_core_reset_adapter();
358 } else if (g_strcmp0(method_name, "EnableAdapterLe") == 0) {
359 ret = _bt_core_enable_adapter_le();
360 } else if (g_strcmp0(method_name, "DisableAdapterLe") == 0) {
361 ret = _bt_core_disable_adapter_le();
362 } else if (g_strcmp0(method_name, "EnableCore") == 0) {
363 ret = _bt_core_enable_core();
364 } else if (g_strcmp0(method_name, "FactoryTestMode") == 0) {
365 const char *type = NULL;
366 const char *arg = NULL;
368 g_variant_get(parameters, "(&s&s)", &type, &arg);
369 ret = _bt_core_factory_test_mode(type, arg);
370 g_dbus_method_invocation_return_value(invocation,
371 g_variant_new("(i)", ret));
378 GQuark quark = g_quark_from_string("bt-core");
379 GError *err = g_error_new(quark, 0, "Failed");
380 g_dbus_method_invocation_return_gerror(invocation, err);
383 g_dbus_method_invocation_return_value(invocation, NULL);
389 static const GDBusInterfaceVTable method_table = {
390 __bt_core_dbus_method,
395 static GDBusNodeInfo *__bt_core_create_node_info(
396 const gchar *introspection_data)
399 GDBusNodeInfo *node_info = NULL;
401 if (introspection_data == NULL)
404 node_info = g_dbus_node_info_new_for_xml(introspection_data, &err);
407 BT_ERR("Unable to create node: %s", err->message);
413 gboolean __is_interface_and_signal_valid(const gchar *interface_name,
414 const gchar *signal_name)
416 if (g_strcmp0(interface_name, "org.freedesktop.DBus") &&
417 g_strcmp0(interface_name, "org.freedesktop.DBus.ObjectManager"))
420 if (g_strcmp0(signal_name, "NameOwnerChanged") &&
421 g_strcmp0(signal_name, "InterfacesAdded") &&
422 g_strcmp0(signal_name, "InterfacesRemoved"))
428 static void __handle_name_owner_changed(const char *name)
430 gboolean flight_mode_status;
434 flight_mode_status = _bt_core_is_flight_mode_enabled();
436 if (flight_mode_status == FALSE && _bt_is_flightmode_request() == TRUE) {
437 BT_DBG("flightmode requested");
441 if ((g_strcmp0(name, "org.bluez") == 0) ||
442 (g_strcmp0(name, "org.projectx.bt") == 0)) {
443 BT_DBG("%s is terminated", name);
444 if (_bt_check_terminating_condition() == TRUE) {
445 _bt_disable_adapter();
446 _bt_disable_adapter_le();
447 _bt_core_terminate();
452 static void __bt_core_event_filter(GDBusConnection *connection,
453 const gchar *sender_name,
454 const gchar *object_path,
455 const gchar *interface_name,
456 const gchar *signal_name,
457 GVariant *parameters,
460 if (!__is_interface_and_signal_valid(interface_name, signal_name))
463 if (!g_strcmp0(signal_name, "InterfacesAdded")) {
464 char *obj_path = NULL;
465 GVariant *optional_param;
467 g_variant_get(parameters, "(&o@a{sa{sv}})",
468 &obj_path, &optional_param);
470 if (g_strcmp0(obj_path, "/org/bluez/hci0") == 0) {
471 _bt_core_adapter_added_cb();
473 } else if (!g_strcmp0(signal_name, "InterfacesRemoved")) {
474 char *obj_path = NULL;
475 GVariant *optional_param;
477 g_variant_get(parameters, "(&o@as)", &obj_path,
480 if (g_strcmp0(obj_path, "/org/bluez/hci0") == 0) {
481 _bt_core_adapter_removed_cb();
483 } else { /* NameOwnerChanged */
484 const char *name = NULL;
485 const char *old_owner = NULL;
486 const char *new_owner = NULL;
488 g_variant_get(parameters, "(&s&s&s)", &name, &old_owner,
491 if (new_owner != NULL && *new_owner == '\0')
492 __handle_name_owner_changed(name);
496 gboolean _bt_core_register_dbus(void)
498 GError *error = NULL;
500 GDBusNodeInfo *node_info;
502 GDBusConnection *conn;
504 conn = _bt_core_get_gdbus_connection();
508 owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
510 G_BUS_NAME_OWNER_FLAGS_NONE,
514 BT_DBG("owner_id is [%d]", owner_id);
516 node_info = __bt_core_create_node_info(bt_core_introspection_xml);
517 if (node_info == NULL)
520 path = g_strdup(BT_CORE_PATH);
521 BT_DBG("path is [%s]", path);
523 obj_id = g_dbus_connection_register_object(conn, path,
524 node_info->interfaces[0],
528 BT_ERR("Failed to register: %s", error->message);
536 sig_id1 = g_dbus_connection_signal_subscribe(conn,
537 NULL, "org.freedesktop.DBus",
538 "NameOwnerChanged", NULL, NULL, 0,
539 __bt_core_event_filter, NULL, NULL);
540 sig_id2 = g_dbus_connection_signal_subscribe(conn,
541 NULL, "org.freedesktop.DBus.ObjectManager",
542 "InterfacesAdded", NULL, NULL,
543 0, __bt_core_event_filter, NULL, NULL);
544 sig_id2 = g_dbus_connection_signal_subscribe(conn,
545 NULL, "org.freedesktop.DBus.ObjectManager",
546 "InterfacesRemoved", NULL,
547 NULL, 0, __bt_core_event_filter, NULL, NULL);
552 void _bt_core_unregister_dbus(void)
554 GDBusConnection *conn;
558 conn = _bt_core_get_gdbus_connection();
563 g_dbus_connection_unregister_object(conn, obj_id);
568 g_dbus_connection_signal_unsubscribe(conn, sig_id1);
572 g_dbus_connection_signal_unsubscribe(conn, sig_id2);
576 g_dbus_connection_signal_unsubscribe(conn, sig_id3);