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"
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"
38 static GDBusConnection *service_gconn;
39 static GDBusProxy *service_gproxy;
40 #ifdef TIZEN_FEATURE_BT_HPS
41 static GDBusProxy *hps_gproxy;
43 static gboolean factory_test_mode;
45 void _bt_core_fill_garray_from_variant(GVariant *var, GArray *param)
50 size = g_variant_get_size(var);
52 data = (char *)g_variant_get_data(var);
54 param = g_array_append_vals(param, data, size);
59 GDBusConnection * _bt_core_get_gdbus_connection(void)
63 if (service_gconn == NULL)
64 service_gconn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
68 BT_ERR("Unable to connect to dbus: %s", err->message);
77 static GDBusProxy *__bt_core_gdbus_init_service_proxy(void)
81 GDBusConnection *conn;
83 conn = _bt_core_get_gdbus_connection();
87 proxy = g_dbus_proxy_new_sync(conn,
88 G_DBUS_PROXY_FLAGS_NONE, NULL,
95 BT_ERR("Unable to create proxy: %s", err->message);
102 service_gproxy = proxy;
107 GDBusProxy *_bt_core_gdbus_get_service_proxy(void)
109 return (service_gproxy) ? service_gproxy : __bt_core_gdbus_init_service_proxy();
112 #ifdef TIZEN_FEATURE_BT_HPS
113 int _bt_core_start_httpproxy(void)
115 GVariant *variant = NULL;
116 unsigned char enabled;
120 hps_gproxy = _bt_core_gdbus_get_hps_proxy();
122 BT_DBG("Couldn't get service proxy");
126 variant = g_dbus_proxy_call_sync(hps_gproxy, "enable",
127 NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
129 BT_ERR("Error : %s" , err->message);
133 g_variant_get(variant, "(y)", &enabled);
134 BT_ERR("HPS enabled status 0x%x", enabled);
139 int _bt_core_stop_httpproxy(void)
141 GVariant *variant = NULL;
142 unsigned char enabled;
146 hps_gproxy = _bt_core_gdbus_get_hps_proxy();
148 BT_DBG("Couldn't get service proxy");
152 variant = g_dbus_proxy_call_sync(hps_gproxy, "disable",
153 NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
155 BT_ERR("Error : %s" , err->message);
159 g_variant_get(variant, "(y)", &enabled);
160 BT_ERR("HPS disabled status 0x%x", enabled);
165 static GDBusProxy *_bt_core_gdbus_init_hps_proxy(void)
169 GDBusConnection *conn;
173 conn = _bt_core_get_gdbus_connection();
177 proxy = g_dbus_proxy_new_sync(conn,
178 G_DBUS_PROXY_FLAGS_NONE, NULL,
181 BT_HPS_INTERFACE_NAME,
185 BT_ERR("Unable to create proxy: %s", err->message);
196 GDBusProxy *_bt_core_gdbus_get_hps_proxy(void)
198 return (hps_gproxy) ? hps_gproxy : _bt_core_gdbus_init_hps_proxy();
202 void _bt_core_gdbus_deinit_proxys(void)
206 if (service_gproxy) {
207 g_object_unref(service_gproxy);
208 service_gproxy = NULL;
211 #ifdef TIZEN_FEATURE_BT_HPS
213 g_object_unref(hps_gproxy);
219 g_object_unref(service_gconn);
220 service_gconn = NULL;
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,
230 GVariant *ret = NULL;
237 int result = BLUETOOTH_ERROR_NONE;
238 GError *error = NULL;
239 GArray *in_param5 = NULL;
243 proxy = _bt_core_gdbus_get_service_proxy();
245 return BLUETOOTH_ERROR_INTERNAL;
246 in_param5 = g_array_new(TRUE, TRUE, sizeof(gchar));
248 while (--retry >= 0) {
249 param1 = g_variant_new_from_data((const GVariantType *)"ay",
250 in_param1->data, in_param1->len,
252 param2 = g_variant_new_from_data((const GVariantType *)"ay",
253 in_param2->data, in_param2->len,
255 param3 = g_variant_new_from_data((const GVariantType *)"ay",
256 in_param3->data, in_param3->len,
258 param4 = g_variant_new_from_data((const GVariantType *)"ay",
259 in_param4->data, in_param4->len,
261 param5 = g_variant_new_from_data((const GVariantType *)"ay",
262 in_param5->data, in_param5->len,
265 ret = g_dbus_proxy_call_sync(proxy, "service_request",
266 g_variant_new("(iii@ay@ay@ay@ay@ay)",
267 service_type, service_function,
271 G_DBUS_CALL_FLAGS_NONE, 2000,
273 if (ret == NULL && error != NULL) {
274 if (error->code == G_IO_ERROR_TIMED_OUT) {
275 BT_ERR("D-Bus Timed out.");
276 g_clear_error(&error);
284 g_array_free(in_param5, TRUE);
287 /* dBUS-RPC is failed */
288 BT_ERR("dBUS-RPC is failed");
291 /* dBUS gives error cause */
292 BT_ERR("D-Bus API failure: errCode[%x], message[%s]",
293 error->code, error->message);
295 g_clear_error(&error);
297 /* dBUS does not give error cause dBUS-RPC is failed */
298 BT_ERR("error returned was NULL");
301 return BLUETOOTH_ERROR_INTERNAL;
306 g_variant_get(ret, "(iv)", &result, ¶m1);
309 *out_param1 = g_array_new(TRUE, TRUE, sizeof(gchar));
310 _bt_core_fill_garray_from_variant(param1, *out_param1);
311 g_variant_unref(param1);
314 g_variant_unref(ret);
319 static const gchar bt_core_introspection_xml[] =
321 " <interface name='org.projectx.btcore'>"
322 " <method name='EnableAdapter'>"
324 " <method name='DisableAdapter'>"
326 " <method name='RecoverAdapter'>"
328 " <method name='ResetAdapter'>"
330 " <method name='EnableAdapterLe'>"
332 " <method name='DisableAdapterLe'>"
334 " <method name='EnableCore'>"
336 " <method name='SetTransferValue'>"
337 " <arg type='b' name='value' direction='in'/>"
339 " <method name='FactoryTestMode'>"
340 " <arg type='s' name='type' direction='in'/>"
341 " <arg type='s' name='arg' direction='in'/>"
342 " <arg type='i' name='ret' direction='out'/>"
347 static guint obj_id, sig_id1, sig_id2, sig_id3;
349 static void __bt_core_dbus_method(GDBusConnection *connection,
351 const gchar *object_path,
352 const gchar *interface_name,
353 const gchar *method_name,
354 GVariant *parameters,
355 GDBusMethodInvocation *invocation,
360 BT_DBG("method %s", method_name);
362 if (g_strcmp0(method_name, "EnableAdapter") == 0) {
363 ret = _bt_core_enable_adapter();
364 } else if (g_strcmp0(method_name, "DisableAdapter") == 0) {
365 ret = _bt_core_disable_adapter();
366 } else if (g_strcmp0(method_name, "RecoverAdapter") == 0) {
367 ret = _bt_core_recover_adapter();
368 } else if (g_strcmp0(method_name, "ResetAdapter") == 0) {
369 ret = __bt_core_reset_adapter();
370 } else if (g_strcmp0(method_name, "EnableAdapterLe") == 0) {
371 ret = _bt_core_enable_adapter_le();
372 } else if (g_strcmp0(method_name, "DisableAdapterLe") == 0) {
373 ret = _bt_core_disable_adapter_le();
374 } else if (g_strcmp0(method_name, "EnableCore") == 0) {
375 ret = _bt_core_enable_core();
376 } else if (g_strcmp0(method_name, "SetTransferValue") == 0) {
377 gboolean value = FALSE;
379 g_variant_get(parameters, "(b)", &value);
380 BT_DBG("Transfer value: %d", value);
382 ret = _bt_core_set_transfer_value(value);
383 } else if (g_strcmp0(method_name, "FactoryTestMode") == 0) {
384 const char *type = NULL;
385 const char *arg = NULL;
387 g_variant_get(parameters, "(&s&s)", &type, &arg);
388 ret = _bt_core_factory_test_mode(type, arg);
389 factory_test_mode = TRUE;
392 g_dbus_method_invocation_return_value(invocation,
393 g_variant_new("(i)", ret));
395 _bt_core_terminate();
403 GQuark quark = g_quark_from_string("bt-core");
404 GError *err = g_error_new(quark, 0, "Failed");
405 g_dbus_method_invocation_return_gerror(invocation, err);
408 g_dbus_method_invocation_return_value(invocation, NULL);
414 static const GDBusInterfaceVTable method_table = {
415 __bt_core_dbus_method,
420 static GDBusNodeInfo *__bt_core_create_node_info(
421 const gchar *introspection_data)
424 GDBusNodeInfo *node_info = NULL;
426 if (introspection_data == NULL)
429 node_info = g_dbus_node_info_new_for_xml(introspection_data, &err);
432 BT_ERR("Unable to create node: %s", err->message);
438 gboolean __is_interface_and_signal_valid(const gchar *interface_name,
439 const gchar *signal_name)
441 if (g_strcmp0(interface_name, "org.freedesktop.DBus") &&
442 g_strcmp0(interface_name, "org.freedesktop.DBus.ObjectManager"))
445 if (g_strcmp0(signal_name, "NameOwnerChanged") &&
446 g_strcmp0(signal_name, "InterfacesAdded") &&
447 g_strcmp0(signal_name, "InterfacesRemoved"))
453 static void __handle_name_owner_changed(const char *name)
455 gboolean flight_mode_status;
457 flight_mode_status = _bt_core_is_flight_mode_enabled();
459 if (flight_mode_status == FALSE && _bt_is_flightmode_request() == TRUE) {
460 BT_DBG("flightmode requested");
464 if ((g_strcmp0(name, "org.bluez") == 0) ||
465 (g_strcmp0(name, "org.projectx.bt") == 0)) {
466 BT_DBG("%s is terminated", name);
467 if (_bt_check_terminating_condition() == TRUE && factory_test_mode == FALSE) {
468 _bt_disable_adapter();
469 _bt_disable_adapter_le();
470 _bt_core_terminate();
475 static void __bt_core_event_filter(GDBusConnection *connection,
476 const gchar *sender_name,
477 const gchar *object_path,
478 const gchar *interface_name,
479 const gchar *signal_name,
480 GVariant *parameters,
483 if (!__is_interface_and_signal_valid(interface_name, signal_name))
486 if (!g_strcmp0(signal_name, "InterfacesAdded")) {
487 char *obj_path = NULL;
488 GVariant *optional_param;
490 g_variant_get(parameters, "(&o@a{sa{sv}})",
491 &obj_path, &optional_param);
493 if (g_strcmp0(obj_path, "/org/bluez/hci0") == 0)
494 _bt_core_adapter_added_cb();
496 } else if (!g_strcmp0(signal_name, "InterfacesRemoved")) {
497 char *obj_path = NULL;
498 GVariant *optional_param;
500 g_variant_get(parameters, "(&o@as)", &obj_path,
503 if (g_strcmp0(obj_path, "/org/bluez/hci0") == 0)
504 _bt_core_adapter_removed_cb();
506 } else { /* NameOwnerChanged */
507 const char *name = NULL;
508 const char *old_owner = NULL;
509 const char *new_owner = NULL;
511 g_variant_get(parameters, "(&s&s&s)", &name, &old_owner,
514 if (new_owner != NULL && *new_owner == '\0')
515 __handle_name_owner_changed(name);
519 gboolean _bt_core_register_dbus(void)
521 GError *error = NULL;
523 GDBusNodeInfo *node_info;
525 GDBusConnection *conn;
527 conn = _bt_core_get_gdbus_connection();
531 owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
533 G_BUS_NAME_OWNER_FLAGS_NONE,
537 BT_DBG("owner_id is [%d]", owner_id);
539 node_info = __bt_core_create_node_info(bt_core_introspection_xml);
540 if (node_info == NULL)
543 path = g_strdup(BT_CORE_PATH);
544 BT_DBG("path is [%s]", path);
546 obj_id = g_dbus_connection_register_object(conn, path,
547 node_info->interfaces[0],
550 g_dbus_node_info_unref(node_info);
552 BT_ERR("Failed to register: %s", error->message);
560 sig_id1 = g_dbus_connection_signal_subscribe(conn,
561 NULL, "org.freedesktop.DBus",
562 "NameOwnerChanged", NULL, NULL, 0,
563 __bt_core_event_filter, NULL, NULL);
564 sig_id2 = g_dbus_connection_signal_subscribe(conn,
565 NULL, "org.freedesktop.DBus.ObjectManager",
566 "InterfacesAdded", NULL, NULL,
567 0, __bt_core_event_filter, NULL, NULL);
568 sig_id2 = g_dbus_connection_signal_subscribe(conn,
569 NULL, "org.freedesktop.DBus.ObjectManager",
570 "InterfacesRemoved", NULL,
571 NULL, 0, __bt_core_event_filter, NULL, NULL);
576 void _bt_core_unregister_dbus(void)
578 GDBusConnection *conn;
582 conn = _bt_core_get_gdbus_connection();
587 g_dbus_connection_unregister_object(conn, obj_id);
592 g_dbus_connection_signal_unsubscribe(conn, sig_id1);
596 g_dbus_connection_signal_unsubscribe(conn, sig_id2);
600 g_dbus_connection_signal_unsubscribe(conn, sig_id3);