/*
- * bluetooth-frwk
- *
- * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
#include <stdio.h>
#include <string.h>
-#include <dbus/dbus-glib-lowlevel.h>
-#include <dbus/dbus-glib.h>
+#include <glib.h>
+#include <dlog.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <termios.h>
+#include <net_connection.h>
#include <dbus/dbus.h>
#include <glib.h>
#include <dlog.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
+#include <net_connection.h>
+#include <bundle.h>
+#include <eventsystem.h>
#include "bluetooth-api.h"
#include "bt-service-common.h"
#include "bt-service-agent.h"
-static DBusGConnection *system_conn;
-static DBusGConnection *session_conn;
-static DBusGProxy *manager_proxy;
-static DBusGProxy *adapter_proxy;
+static GDBusConnection *system_conn;
+static GDBusConnection *session_conn;
+static GDBusProxy *manager_proxy;
+static GDBusProxy *adapter_proxy;
+static void *net_conn;
+
+static GDBusProxy *adapter_properties_proxy;
-static DBusGProxy *__bt_init_manager_proxy(void)
+GDBusConnection *_bt_gdbus_init_system_gconn(void)
{
- DBusGProxy *proxy;
+ GError *error = NULL;
- g_type_init();
+ dbus_threads_init_default();
+
+ if (system_conn != NULL)
+ return system_conn;
+
+ system_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
+
+ if (!system_conn) {
+ BT_ERR("Unable to connect to dbus: %s", error->message);
+ g_clear_error(&error);
+ }
+
+ return system_conn;
+}
+
+GDBusConnection *_bt_gdbus_get_system_gconn(void)
+{
+ GDBusConnection *local_system_gconn = NULL;
+ GError *error = NULL;
if (system_conn == NULL) {
- system_conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, NULL);
- retv_if(system_conn == NULL, NULL);
+ system_conn = _bt_gdbus_init_system_gconn();
+ } else if (g_dbus_connection_is_closed(system_conn)) {
+ local_system_gconn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
+
+ if (!local_system_gconn) {
+ BT_ERR("Unable to connect to dbus: %s", error->message);
+ g_clear_error(&error);
+ }
+
+ system_conn = local_system_gconn;
}
- proxy = dbus_g_proxy_new_for_name(system_conn, BT_BLUEZ_NAME,
- BT_MANAGER_PATH, BT_MANAGER_INTERFACE);
+ return system_conn;
+}
- retv_if(proxy == NULL, NULL);
+GDBusConnection *_bt_gdbus_init_session_gconn(void)
+{
+ GError *error = NULL;
+
+ dbus_threads_init_default();
+
+ if (session_conn != NULL)
+ return session_conn;
+
+ session_conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
+
+ if (!session_conn) {
+ BT_ERR("Unable to connect to dbus: %s", error->message);
+ g_clear_error(&error);
+ }
+
+ return session_conn;
+}
+
+GDBusConnection *_bt_gdbus_get_session_gconn(void)
+{
+ GDBusConnection *local_session_gconn = NULL;
+ GError *error = NULL;
+
+ if (session_conn == NULL) {
+ session_conn = _bt_gdbus_init_session_gconn();
+ } else if (g_dbus_connection_is_closed(session_conn)) {
+ local_session_gconn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
+
+ if (!local_session_gconn) {
+ BT_ERR("Unable to connect to dbus: %s", error->message);
+ g_clear_error(&error);
+ }
+
+ session_conn = local_session_gconn;
+ }
+
+ return session_conn;
+}
+
+static GDBusProxy *__bt_init_manager_proxy(void)
+{
+ GDBusConnection *g_conn;
+ GDBusProxy *proxy;
+
+ dbus_threads_init_default();
+
+ g_conn = _bt_gdbus_get_system_gconn();
+ retv_if(g_conn == NULL, NULL);
+
+ proxy = g_dbus_proxy_new_sync(g_conn, G_DBUS_PROXY_FLAGS_NONE,
+ NULL, BT_BLUEZ_NAME,
+ BT_MANAGER_PATH, BT_MANAGER_INTERFACE, NULL, NULL);
+
+ if (!proxy) {
+ BT_ERR("Unable to get proxy");
+ return NULL;
+ }
manager_proxy = proxy;
return proxy;
}
-static DBusGProxy *__bt_init_adapter_proxy(void)
+static GDBusProxy *__bt_init_adapter_proxy(void)
{
- DBusGProxy *manager_proxy;
- DBusGProxy *proxy;
+ GDBusConnection *g_conn;
+ GDBusProxy *manager_proxy;
+ GDBusProxy *proxy;
char *adapter_path = NULL;
- g_type_init();
+ dbus_threads_init_default();
- if (system_conn == NULL) {
- system_conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, NULL);
- retv_if(system_conn == NULL, NULL);
- }
+ g_conn = _bt_gdbus_get_system_gconn();
+ retv_if(g_conn == NULL, NULL);
manager_proxy = _bt_get_manager_proxy();
retv_if(manager_proxy == NULL, NULL);
- if (!dbus_g_proxy_call(manager_proxy, "DefaultAdapter", NULL,
- G_TYPE_INVALID, DBUS_TYPE_G_OBJECT_PATH,
- &adapter_path, G_TYPE_INVALID)) {
- BT_ERR("Fait to get DefaultAdapter");
- return NULL;
- }
-
- adapter_path = g_strdup(adapter_path);
+ adapter_path = _bt_get_adapter_path();
retv_if(adapter_path == NULL, NULL);
- proxy = dbus_g_proxy_new_for_name(system_conn, BT_BLUEZ_NAME,
- adapter_path, BT_ADAPTER_INTERFACE);
+ proxy = g_dbus_proxy_new_sync(g_conn, G_DBUS_PROXY_FLAGS_NONE,
+ NULL, BT_BLUEZ_NAME,
+ adapter_path, BT_ADAPTER_INTERFACE, NULL, NULL);
g_free(adapter_path);
return proxy;
}
-DBusGConnection *__bt_init_system_gconn(void)
+static GDBusProxy *__bt_init_adapter_properties_proxy(void)
{
- g_type_init();
+ GDBusConnection *g_conn;
+ GDBusProxy *manager_proxy;
+ GDBusProxy *proxy;
+ char *adapter_path = NULL;
- if (system_conn == NULL)
- system_conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, NULL);
+ dbus_threads_init_default();
- return system_conn;
+ g_conn = _bt_gdbus_get_system_gconn();
+ retv_if(g_conn == NULL, NULL);
+
+ manager_proxy = _bt_get_manager_proxy();
+ retv_if(manager_proxy == NULL, NULL);
+
+ adapter_path = _bt_get_adapter_path();
+ retv_if(adapter_path == NULL, NULL);
+
+ proxy = g_dbus_proxy_new_sync(g_conn, G_DBUS_PROXY_FLAGS_NONE,
+ NULL, BT_BLUEZ_NAME,
+ adapter_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
+
+ g_free(adapter_path);
+
+ retv_if(proxy == NULL, NULL);
+
+ adapter_properties_proxy = proxy;
+
+ return proxy;
}
-DBusGConnection *__bt_init_session_conn(void)
+GDBusProxy *_bt_get_manager_proxy(void)
{
- if (session_conn == NULL)
- session_conn = dbus_g_bus_get(DBUS_BUS_SESSION, NULL);
-
- return session_conn;
+ if (manager_proxy) {
+ const gchar *path = g_dbus_proxy_get_object_path(manager_proxy);
+ if (path == NULL) {
+ BT_ERR("Already proxy released hence creating new proxy");
+ return __bt_init_manager_proxy();
+ }
+ return manager_proxy;
+ }
+ return __bt_init_manager_proxy();
}
-DBusGConnection *_bt_get_session_gconn(void)
+static void *__bt_init_net_conn(void)
{
- return (session_conn) ? session_conn : __bt_init_session_conn();
+ int result;
+ connection_h connection = NULL;
+
+ if (net_conn == NULL) {
+ result = connection_create(&connection);
+
+ if (result != CONNECTION_ERROR_NONE ||
+ connection == NULL) {
+ BT_DBG("connection_create() failed: %d", result);
+ net_conn = NULL;
+ return NULL;
+ }
+ net_conn = connection;
+ }
+ return net_conn;
}
-DBusGConnection *_bt_get_system_gconn(void)
+void *_bt_get_net_conn(void)
{
- return (system_conn) ? system_conn : __bt_init_system_gconn();
+ return (net_conn) ? net_conn : __bt_init_net_conn();
}
-DBusConnection *_bt_get_system_conn(void)
+GDBusProxy *_bt_get_adapter_proxy(void)
{
- DBusGConnection *g_conn;
+ if (adapter_proxy) {
+ const char *path = g_dbus_proxy_get_object_path(adapter_proxy);
+ if (path == NULL) {
+ BT_ERR("Already proxy released hence creating new proxy");
+ return __bt_init_adapter_proxy();
+ }
- if (system_conn == NULL) {
- g_conn = __bt_init_system_gconn();
- } else {
- g_conn = system_conn;
+ return adapter_proxy;
}
+ return __bt_init_adapter_proxy();
- retv_if(g_conn == NULL, NULL);
-
- return dbus_g_connection_get_connection(g_conn);
}
-DBusGProxy *_bt_get_manager_proxy(void)
+GDBusProxy *_bt_get_adapter_properties_proxy(void)
{
- return (manager_proxy) ? manager_proxy : __bt_init_manager_proxy();
+ return (adapter_properties_proxy) ? adapter_properties_proxy :
+ __bt_init_adapter_properties_proxy();
}
-DBusGProxy *_bt_get_adapter_proxy(void)
+static char *__bt_extract_adapter_path(GVariantIter *iter)
{
- return (adapter_proxy) ? adapter_proxy : __bt_init_adapter_proxy();
+ char *object_path = NULL;
+ GVariantIter *interface_iter;
+ GVariantIter *svc_iter;
+ char *interface_str = NULL;
+
+ /* Parse the signature: oa{sa{sv}}} */
+ while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path,
+ &interface_iter)) {
+
+ if (object_path == NULL)
+ continue;
+
+ while (g_variant_iter_loop(interface_iter, "{&sa{sv}}",
+ &interface_str, &svc_iter)) {
+ if (g_strcmp0(interface_str, "org.bluez.Adapter1") != 0)
+ continue;
+
+ BT_DBG("Object Path: %s", object_path);
+ g_variant_iter_free(svc_iter);
+ g_variant_iter_free(interface_iter);
+ return g_strdup(object_path);
+ }
+ }
+ return NULL;
}
char *_bt_get_adapter_path(void)
{
+ GDBusConnection *conn;
+ GDBusProxy *manager_proxy;
+ GVariant *result = NULL;
+ GVariantIter *iter = NULL;
char *adapter_path = NULL;
- DBusGProxy *proxy;
- GError *err = NULL;
-
- proxy = _bt_get_manager_proxy();
-
- if (!dbus_g_proxy_call(proxy, "DefaultAdapter", &err,
- G_TYPE_INVALID, DBUS_TYPE_G_OBJECT_PATH,
- &adapter_path, G_TYPE_INVALID)) {
- if (err != NULL) {
- BT_ERR("Getting DefaultAdapter failed: [%s]\n",
- err->message);
- g_error_free(err);
- }
+
+ conn = _bt_gdbus_get_system_gconn();
+ retv_if(conn == NULL, NULL);
+
+ manager_proxy = _bt_get_manager_proxy();
+ retv_if(manager_proxy == NULL, NULL);
+
+ result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
+ NULL,
+ G_DBUS_CALL_FLAGS_NONE,
+ -1,
+ NULL,
+ NULL);
+ if (!result) {
+ BT_ERR("Can't get managed objects");
return NULL;
}
+ /* signature of GetManagedObjects: a{oa{sa{sv}}} */
+ g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
+
+ adapter_path = __bt_extract_adapter_path(iter);
+ g_variant_iter_free(iter);
+ g_variant_unref(result);
return adapter_path;
}
g_object_unref(adapter_proxy);
adapter_proxy = NULL;
}
+ if (adapter_properties_proxy) {
+ g_object_unref(adapter_properties_proxy);
+ adapter_properties_proxy = NULL;
+ }
}
void _bt_deinit_proxys(void)
{
-
+ int ret;
_bt_deinit_bluez_proxy();
if (system_conn) {
- dbus_g_connection_unref(system_conn);
+ g_object_unref(system_conn);
system_conn = NULL;
}
if (session_conn) {
- dbus_g_connection_unref(session_conn);
+ g_object_unref(session_conn);
session_conn = NULL;
}
+ if (net_conn) {
+ ret = connection_destroy(net_conn);
+ net_conn = NULL;
+ if (ret != 0)
+ BT_ERR("connection_destroy failed : %d", ret);
+ }
}
void _bt_convert_device_path_to_address(const char *device_path,
dev_addr += 4;
g_strlcpy(address, dev_addr, sizeof(address));
- while ((pos = strchr(address, '_')) != NULL) {
+ while ((pos = strchr(address, '_')) != NULL)
*pos = ':';
- }
g_strlcpy(device_address, address, BT_ADDRESS_STRING_SIZE);
}
void _bt_convert_addr_string_to_type(unsigned char *addr,
const char *address)
{
- int i;
- char *ptr = NULL;
+ int i;
+ char *ptr = NULL;
ret_if(address == NULL);
ret_if(addr == NULL);
- for (i = 0; i < BT_ADDRESS_LENGTH_MAX; i++) {
- addr[i] = strtol(address, &ptr, 16);
- if (ptr != NULL) {
- if (ptr[0] != ':')
- return;
+ for (i = 0; i < BT_ADDRESS_LENGTH_MAX; i++) {
+ addr[i] = strtol(address, &ptr, 16);
+ if (ptr[0] != '\0') {
+ if (ptr[0] != ':')
+ return;
- address = ptr + 1;
- }
- }
+ address = ptr + 1;
+ }
+ }
+}
+
+void _bt_convert_addr_string_to_secure_string(char *addr,
+ const char *address)
+{
+ int len;
+
+ ret_if(address == NULL);
+ ret_if(addr == NULL);
+
+ len = strlen(address);
+ ret_if(len != BT_ADDRESS_STRING_SIZE - 1);
+
+ strncpy(addr, address, len);
+
+ addr[len-1] = 'X';
+ addr[len-2] = 'X';
+
+ return;
}
void _bt_convert_addr_type_to_string(char *address,
g_free(dev_info->address);
g_free(dev_info->name);
+ g_free(dev_info->manufacturer_data);
if (dev_info->uuids) {
- for (i = 0; dev_info->uuids[i] != NULL; i++)
+ for (i = 0; i < dev_info->uuid_count && dev_info->uuids[i]; i++)
g_free(dev_info->uuids[i]);
g_free(dev_info->uuids);
g_free(dev_info);
}
-int _bt_register_osp_server_in_agent(int type, char *uuid)
+void _bt_free_le_device_info(bt_remote_le_dev_info_t *le_dev_info)
{
- if (!_bt_agent_register_osp_server( type, uuid))
+ ret_if(le_dev_info == NULL);
+
+ g_free(le_dev_info->adv_data);
+ g_free(le_dev_info);
+}
+
+int _bt_copy_utf8_string(char *dest, const char *src, unsigned int length)
+{
+ int i;
+ const char *p = src;
+ char *next;
+ int count;
+
+ if (dest == NULL || src == NULL)
+ return BLUETOOTH_ERROR_INVALID_PARAM;
+
+ BT_DBG("+src : %s", src);
+ BT_DBG("+dest : %s", dest);
+
+ i = 0;
+ while (*p != '\0' && i < length) {
+ next = g_utf8_next_char(p);
+ count = next - p;
+
+ while (count > 0 && ((i + count) < length)) {
+ dest[i++] = *p;
+ p++;
+ count--;
+ }
+ p = next;
+ }
+ return BLUETOOTH_ERROR_NONE;
+}
+
+gboolean _bt_utf8_validate(char *name)
+{
+ BT_DBG("+");
+ gunichar2 *u16;
+ glong items_written = 0;
+
+ if (FALSE == g_utf8_validate(name, -1, NULL))
+ return FALSE;
+
+ u16 = g_utf8_to_utf16(name, -1, NULL, &items_written, NULL);
+ if (u16 == NULL)
+ return FALSE;
+
+ g_free(u16);
+
+ if (items_written != g_utf8_strlen(name, -1))
+ return FALSE;
+
+ BT_DBG("-");
+ return TRUE;
+}
+
+int _bt_register_osp_server_in_agent(int type, char *uuid, char *path, int fd)
+{
+ BT_DBG("+");
+ if (!_bt_agent_register_osp_server(type, uuid, path, fd))
return BLUETOOTH_ERROR_INTERNAL;
return BLUETOOTH_ERROR_NONE;
int _bt_unregister_osp_server_in_agent(int type, char *uuid)
{
- if (!_bt_agent_unregister_osp_server( type, uuid))
+ BT_DBG("+");
+ if (!_bt_agent_unregister_osp_server(type, uuid))
return BLUETOOTH_ERROR_INTERNAL;
return BLUETOOTH_ERROR_NONE;
if (arg < 0)
return -errno;
- if (arg & O_NONBLOCK) {
+ if (arg & O_NONBLOCK)
BT_ERR("Already Non-blocking \n");
- }
arg |= O_NONBLOCK;
return BLUETOOTH_ERROR_NONE;
}
-gboolean _bt_is_headset_class(int dev_class)
-{
- gboolean is_headset = FALSE;
-
- switch ((dev_class & 0x1f00) >> 8) {
- case 0x04:
- switch ((dev_class & 0xfc) >> 2) {
- case 0x01:
- case 0x02:
- /* Headset */
- is_headset = TRUE;
- break;
- case 0x06:
- /* Headphone */
- is_headset = TRUE;
- break;
- case 0x0b: /* VCR */
- case 0x0c: /* Video Camera */
- case 0x0d: /* Camcorder */
- break;
- default:
- /* Other audio device */
- is_headset = TRUE;
- break;
+static char *__bt_extract_device_path(GVariantIter *iter, char *address)
+{
+ char *object_path = NULL;
+ char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
+
+ /* Parse the signature: oa{sa{sv}}} */
+ while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path,
+ NULL)) {
+ if (!object_path) {
+ BT_ERR("Unable to get object path");
+ return NULL;
}
- break;
+ _bt_convert_device_path_to_address(object_path, device_address);
+ if (g_strcmp0(address, device_address) == 0)
+ return g_strdup(object_path);
+
+ }
+
+ BT_ERR("Unable to get object path");
+ return NULL;
+}
+
+char *_bt_get_device_object_path(char *address)
+{
+ char *object_path = NULL;
+ GDBusConnection *conn;
+ GDBusProxy *manager_proxy;
+ GVariant *result = NULL;
+ GVariantIter *iter = NULL;
+
+ conn = _bt_gdbus_get_system_gconn();
+ retv_if(conn == NULL, NULL);
+
+ manager_proxy = _bt_get_manager_proxy();
+ retv_if(manager_proxy == NULL, NULL);
+
+ result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
+ NULL,
+ G_DBUS_CALL_FLAGS_NONE,
+ -1,
+ NULL,
+ NULL);
+ if (!result) {
+ BT_ERR("Can't get managed objects");
+ return NULL;
}
- return is_headset;
+ /* signature of GetManagedObjects: a{oa{sa{sv}}} */
+ g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
+ object_path = __bt_extract_device_path(iter, address);
+ g_variant_iter_free(iter);
+ g_variant_unref(result);
+ return object_path;
}
+char *_bt_get_profile_uuid128(bt_profile_type_t profile_type)
+{
+ switch (profile_type) {
+ case BT_PROFILE_CONN_RFCOMM:
+ return strdup(RFCOMM_UUID_STR);
+ case BT_PROFILE_CONN_A2DP:
+ return strdup(A2DP_SINK_UUID);
+ case BT_PROFILE_CONN_A2DP_SINK:
+ return strdup(A2DP_SOURCE_UUID);
+ case BT_PROFILE_CONN_HSP:
+ return strdup(HFP_HS_UUID);
+ case BT_PROFILE_CONN_HID:
+ return strdup(HID_UUID);
+ case BT_PROFILE_CONN_NAP:
+ return strdup(NAP_UUID);
+ case BT_PROFILE_CONN_HFG:
+ return strdup(HFP_AG_UUID);
+ case BT_PROFILE_CONN_PBAP:
+ return strdup(PBAP_UUID);
+ case BT_PROFILE_CONN_GATT:
+ case BT_PROFILE_CONN_ALL: /* NULL UUID will connect to both the audio profiles*/
+ default:
+ return NULL;
+ };
+}
+
+char *_bt_convert_error_to_string(int error)
+{
+ switch (error) {
+ case BLUETOOTH_ERROR_CANCEL:
+ return "CANCELLED";
+ case BLUETOOTH_ERROR_INVALID_PARAM:
+ return "INVALID_PARAMETER";
+ case BLUETOOTH_ERROR_INVALID_DATA:
+ return "INVALID DATA";
+ case BLUETOOTH_ERROR_MEMORY_ALLOCATION:
+ case BLUETOOTH_ERROR_OUT_OF_MEMORY:
+ return "OUT_OF_MEMORY";
+ case BLUETOOTH_ERROR_TIMEOUT:
+ return "TIMEOUT";
+ case BLUETOOTH_ERROR_NO_RESOURCES:
+ return "NO_RESOURCES";
+ case BLUETOOTH_ERROR_INTERNAL:
+ return "INTERNAL";
+ case BLUETOOTH_ERROR_NOT_SUPPORT:
+ return "NOT_SUPPORT";
+ case BLUETOOTH_ERROR_DEVICE_NOT_ENABLED:
+ return "NOT_ENABLED";
+ case BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED:
+ return "ALREADY_ENABLED";
+ case BLUETOOTH_ERROR_DEVICE_BUSY:
+ return "DEVICE_BUSY";
+ case BLUETOOTH_ERROR_ACCESS_DENIED:
+ return "ACCESS_DENIED";
+ case BLUETOOTH_ERROR_MAX_CLIENT:
+ return "MAX_CLIENT";
+ case BLUETOOTH_ERROR_NOT_FOUND:
+ return "NOT_FOUND";
+ case BLUETOOTH_ERROR_SERVICE_SEARCH_ERROR:
+ return "SERVICE_SEARCH_ERROR";
+ case BLUETOOTH_ERROR_PARING_FAILED:
+ return "PARING_FAILED";
+ case BLUETOOTH_ERROR_NOT_PAIRED:
+ return "NOT_PAIRED";
+ case BLUETOOTH_ERROR_SERVICE_NOT_FOUND:
+ return "SERVICE_NOT_FOUND";
+ case BLUETOOTH_ERROR_NOT_CONNECTED:
+ return "NOT_CONNECTED";
+ case BLUETOOTH_ERROR_ALREADY_CONNECT:
+ return "ALREADY_CONNECT";
+ case BLUETOOTH_ERROR_CONNECTION_BUSY:
+ return "CONNECTION_BUSY";
+ case BLUETOOTH_ERROR_CONNECTION_ERROR:
+ return "CONNECTION_ERROR";
+ case BLUETOOTH_ERROR_MAX_CONNECTION:
+ return "MAX_CONNECTION";
+ case BLUETOOTH_ERROR_NOT_IN_OPERATION:
+ return "NOT_IN_OPERATION";
+ case BLUETOOTH_ERROR_CANCEL_BY_USER:
+ return "CANCEL_BY_USER";
+ case BLUETOOTH_ERROR_REGISTRATION_FAILED:
+ return "REGISTRATION_FAILED";
+ case BLUETOOTH_ERROR_IN_PROGRESS:
+ return "IN_PROGRESS";
+ case BLUETOOTH_ERROR_AUTHENTICATION_FAILED:
+ return "AUTHENTICATION_FAILED";
+ case BLUETOOTH_ERROR_HOST_DOWN:
+ return "HOST_DOWN";
+ case BLUETOOTH_ERROR_END_OF_DEVICE_LIST:
+ return "END_OF_DEVICE_LIST";
+ case BLUETOOTH_ERROR_AGENT_ALREADY_EXIST:
+ return "AGENT_ALREADY_EXIST";
+ case BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST:
+ return "AGENT_DOES_NOT_EXIST";
+ case BLUETOOTH_ERROR_ALREADY_INITIALIZED:
+ return "ALREADY_INITIALIZED";
+ case BLUETOOTH_ERROR_PERMISSION_DEINED:
+ return "PERMISSION_DEINED";
+ case BLUETOOTH_ERROR_ALREADY_DEACTIVATED:
+ return "ALREADY_DEACTIVATED";
+ case BLUETOOTH_ERROR_NOT_INITIALIZED:
+ return "NOT_INITIALIZED";
+ case BLUETOOTH_ERROR_AUTHENTICATION_REJECTED:
+ return "AUTHENTICATION REJECTED";
+ default:
+ return "UNKNOWN";
+ }
+}
+
+char * _bt_convert_disc_reason_to_string(int reason)
+{
+ switch (reason) {
+ case (int)BLUETOOTH_ERROR_PAGE_TIMEOUT:
+ return "Page timeout";
+ case (int)BLUETOOTH_ERROR_CONNECTION_TIMEOUT:
+ return "Link loss";
+ case (int)BLUETOOTH_ERROR_PIN_OR_KEY_MISSING:
+ return "PIN or Key missing";
+ case (int)BLUETOOTH_ERROR_LOCAL_HOST_TERM:
+ return "Connection terminated by local host";
+ case (int)BLUETOOTH_ERROR_REMOTE_USER_TERM:
+ case (int)BLUETOOTH_ERROR_REMOTE_LOW_RESOURCES:
+ case (int)BLUETOOTH_ERROR_REMOTE_POWER_OFF:
+ return "Remote user terminated connection";
+ case (int)BLUETOOTH_ERROR_AUTH_FAILURE:
+ return "Authentication Failure";
+ case (int)BLUETOOTH_ERROR_REPEATED_ATTEMPTS:
+ return "Repeated attempts";
+ case (int)BLUETOOTH_ERROR_LMP_RESPONSE_TIMEOUT:
+ return "LMP response timeout";
+ case (int)BLUETOOTH_ERROR_CONNECTION_FAILED_TO_BE_ESTABLISHED:
+ return "Connection failed to be established";
+ default:
+ return "Unknown";
+ }
+}
+
+void _bt_logging_connection(gboolean connect, int addr_type)
+{
+ static int le_conn = 0;
+ static int le_disc = 0;
+ static int edr_conn = 0;
+ static int edr_disc = 0;
+
+ if (connect) {
+ if (addr_type)
+ le_conn++;
+ else
+ edr_conn++;
+ } else {
+ if (addr_type)
+ le_disc++;
+ else
+ edr_disc++;
+ }
+
+ BT_INFO("[PM] Number of LE conn: %d disc: %d, Number of BR/EDR conn: %d disc: %d",
+ le_conn, le_disc, edr_conn, edr_disc);
+}
+
+void _bt_swap_byte_ordering(char *data, int data_len)
+{
+ char temp;
+ int i, j;
+ int half = data_len / 2;
+
+ ret_if(data == NULL);
+ /* Swap to opposite endian */
+ for (i = 0, j = data_len - 1; i < half; i++, j--) {
+ temp = data[i];
+ data[i] = data[j];
+ data[j] = temp;
+ }
+}
+
+int _bt_byte_arr_cmp(const char *data1, const char *data2, int data_len)
+{
+ int i;
+
+ retv_if(data1 == NULL, -1);
+ retv_if(data2 == NULL, -1);
+ for (i = 0; i < data_len; i++) {
+ if (data1[i] != data2[i])
+ return data1[i] - data2[i];
+ }
+ return 0;
+}
+int _bt_byte_arr_cmp_with_mask(const char *data1, const char *data2,
+ const char *mask, int data_len)
+{
+ int i;
+ char a, b;
+
+ retv_if(data1 == NULL, -1);
+ retv_if(data2 == NULL, -1);
+ retv_if(mask == NULL, -1);
+ for (i = 0; i < data_len; i++) {
+ a = data1[i] & mask[i];
+ b = data2[i] & mask[i];
+ if (a != b)
+ return (int)(a - b);
+ }
+ return 0;
+}
+
+int _bt_eventsystem_set_value(const char *event, const char *key, const char *value)
+{
+ int ret = ES_R_OK;
+/* Send event system event in bt-core process because bt-service's permission is not system in now */
+
+#if 0
+ bundle *b = NULL;
+
+ b = bundle_create();
+
+ bundle_add_str(b, key, value);
+
+ ret = eventsystem_send_system_event(event, b);
+
+ BT_DBG("eventsystem_send_system_event result: %d", ret);
+
+ bundle_free(b);
+#endif
+ return ret;
+}
+
+void __bt_get_auth_info(GVariant *reply, char *auth_info)
+{
+ int cursor;
+ GVariant *tmp_value;
+ char *manufacturer_data = NULL;
+ int manufacturer_data_len;
+ gboolean is_alias_set;
+ GVariantIter *value_iter;
+ guint8 m_value;
+ int i = 0;
+
+ tmp_value = g_variant_lookup_value(reply, "IsAliasSet",
+ G_VARIANT_TYPE_BOOLEAN);
+ if (tmp_value) {
+ is_alias_set = g_variant_get_boolean(tmp_value);
+ g_variant_unref(tmp_value);
+ } else {
+ is_alias_set = FALSE;
+ }
+ if (is_alias_set == FALSE) {
+ tmp_value = g_variant_lookup_value(reply, "ManufacturerDataLen",
+ G_VARIANT_TYPE_UINT16);
+ if (tmp_value) {
+ manufacturer_data_len = g_variant_get_uint16(tmp_value);
+ if (manufacturer_data_len >
+ BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX) {
+ BT_ERR("manufacturer_data_len is too long");
+ manufacturer_data_len = BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX;
+ }
+ g_variant_unref(tmp_value);
+ } else
+ manufacturer_data_len = 0;
+
+ tmp_value = g_variant_lookup_value(reply, "ManufacturerData",
+ G_VARIANT_TYPE_ARRAY);
+ if (tmp_value) {
+ if ((manufacturer_data_len == 0) ||
+ manufacturer_data_len != g_variant_get_size(tmp_value)) {
+ BT_ERR("manufacturer data length doesn't match");
+ manufacturer_data_len = 0;
+ manufacturer_data = NULL;
+ } else {
+ manufacturer_data = g_malloc0(manufacturer_data_len);
+ g_variant_get(tmp_value, "ay", &value_iter);
+ while (g_variant_iter_loop(value_iter, "y", &m_value))
+ manufacturer_data[i++] = m_value;
+ }
+ g_variant_unref(tmp_value);
+ } else {
+ BT_INFO("manufacture data is not a G_VARIANT_TYPE_ARRAY ");
+ manufacturer_data_len = 0;
+ manufacturer_data = NULL;
+ }
+ /*minimum Size of the samsung specific manufacturer data is greater than 30 */
+ if (manufacturer_data_len < 30) {
+ g_free(manufacturer_data);
+ return;
+ }
+ if (manufacturer_data[0] != 0x00 || manufacturer_data[1] != 0x75) {
+ BT_DBG("This is not a samsung specific manufaturer data");
+ g_free(manufacturer_data);
+ return;
+ }
+
+ /* 2 samsung (0x00 0x75) + 1 (control and version) + 1 (service ID) +
+ 1 (discovery version) + 1 (associated service ID)
+ 2 (Proxamity and locality) + 2 (Device type and icon) */
+
+ cursor = 10;
+
+ memcpy(auth_info, &(manufacturer_data[cursor]), 5);
+ }
+ g_free(manufacturer_data);
+}
+
+int _bt_convert_gerror(GError *g_error)
+{
+ int ret = BLUETOOTH_ERROR_INTERNAL;
+ gchar *str;
+
+ if (!g_dbus_error_strip_remote_error(g_error))
+ return ret;
+
+ str = g_error->message;
+
+ if (g_strcmp0(str, "Connection refused") == 0)
+ ret = BLUETOOTH_ERROR_AUTHENTICATION_REJECTED;
+ else if (g_strcmp0(str, "Connection timed out") == 0)
+ ret = BLUETOOTH_ERROR_TIMEOUT;
+ else if (g_strcmp0(str, "In Progress") == 0)
+ ret = BLUETOOTH_ERROR_IN_PROGRESS;
+
+ return ret;
+}