Enhance debug message
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-common.c
index 0874e84..5575ed9 100644 (file)
@@ -23,6 +23,7 @@
 #include <errno.h>
 #include <termios.h>
 #include <net_connection.h>
+#include <dbus/dbus.h>
 #include <glib.h>
 #include <dlog.h>
 #include <fcntl.h>
 #include "bluetooth-api.h"
 #include "bt-service-common.h"
 #include "bt-service-agent.h"
-#include "bt-internal-types.h"
 
 static GDBusConnection *system_conn;
 static GDBusConnection *session_conn;
 static GDBusProxy *manager_proxy;
 static GDBusProxy *adapter_proxy;
-static GDBusProxy *ipsp_proxy;
 static void *net_conn;
 
 static GDBusProxy *adapter_properties_proxy;
@@ -50,6 +49,8 @@ GDBusConnection *_bt_gdbus_init_system_gconn(void)
 {
        GError *error = NULL;
 
+       dbus_threads_init_default();
+
        if (system_conn != NULL)
                return system_conn;
 
@@ -88,6 +89,11 @@ GDBusConnection *_bt_gdbus_init_session_gconn(void)
 {
        GError *error = NULL;
 
+       if (!g_thread_supported())
+               g_thread_init(NULL);
+
+       dbus_threads_init_default();
+
        if (session_conn != NULL)
                return session_conn;
 
@@ -127,6 +133,8 @@ 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);
 
@@ -151,6 +159,8 @@ static GDBusProxy *__bt_init_adapter_proxy(void)
        GDBusProxy *proxy;
        char *adapter_path = NULL;
 
+       dbus_threads_init_default();
+
        g_conn = _bt_gdbus_get_system_gconn();
        retv_if(g_conn == NULL, NULL);
 
@@ -173,32 +183,6 @@ static GDBusProxy *__bt_init_adapter_proxy(void)
        return proxy;
 }
 
-static GDBusProxy *__bt_init_ipsp_proxy(void)
-{
-       BT_DBG("+");
-
-       GDBusConnection *g_conn;
-       GDBusProxy *proxy;
-
-
-       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_IPSP_NAME,
-                                                               "/org/projectx/bt_ipsp", BT_IPSP_INTERFACE,  NULL, NULL);
-
-       if (!proxy) {
-               BT_ERR("Unable to get proxy");
-               return NULL;
-       }
-
-       ipsp_proxy = proxy;
-
-       BT_DBG("-");
-       return proxy;
-}
-
 static GDBusProxy *__bt_init_adapter_properties_proxy(void)
 {
        GDBusConnection *g_conn;
@@ -206,6 +190,8 @@ static GDBusProxy *__bt_init_adapter_properties_proxy(void)
        GDBusProxy *proxy;
        char *adapter_path = NULL;
 
+       dbus_threads_init_default();
+
        g_conn = _bt_gdbus_get_system_gconn();
        retv_if(g_conn == NULL, NULL);
 
@@ -241,21 +227,6 @@ GDBusProxy *_bt_get_manager_proxy(void)
        return  __bt_init_manager_proxy();
 }
 
-GDBusProxy *_bt_get_ipsp_proxy(void)
-{
-       if (ipsp_proxy) {
-               const char *path =  g_dbus_proxy_get_object_path(ipsp_proxy);
-               if (path == NULL) {
-                       BT_ERR("Already proxy released hence creating new proxy");
-                       return  __bt_init_ipsp_proxy();
-               }
-
-               return ipsp_proxy;
-       }
-       return  __bt_init_ipsp_proxy();
-
-}
-
 static void *__bt_init_net_conn(void)
 {
        int result;
@@ -403,29 +374,6 @@ void _bt_deinit_proxys(void)
        }
 }
 
-GDBusProxy *_bt_gdbus_get_hid_agent_proxy(void)
-{
-       GDBusConnection *conn;
-       GDBusProxy *proxy;
-       GError *err = NULL;
-
-       conn = _bt_gdbus_get_system_gconn();
-       retv_if(conn == NULL, NULL);
-
-       proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE, NULL,
-                                       BT_HID_SERVICE_NAME, BT_HID_AGENT_OBJECT_PATH,
-                                       BT_HID_SERVICE_INTERFACE, NULL, &err);
-       if (proxy == NULL) {
-               if (err != NULL) {
-                       ERR("Unable to create proxy: %s", err->message);
-                       g_clear_error(&err);
-               }
-               return NULL;
-       }
-
-       return proxy;
-}
-
 void _bt_convert_device_path_to_address(const char *device_path,
                                                char *device_address)
 {
@@ -483,8 +431,14 @@ void _bt_convert_addr_string_to_secure_string(char *addr,
        strncpy(addr, address, len);
        addr[len] = '\0';
 
-       addr[len-1] = 'X';
-       addr[len-2] = 'X';
+       addr[len-7] = 'X';
+       addr[len-8] = 'X';
+       addr[len-10] = 'X';
+       addr[len-11] = 'X';
+       addr[len-13] = 'X';
+       addr[len-14] = 'X';
+       addr[len-16] = 'X';
+       addr[len-17] = 'X';
 
        return;
 }
@@ -660,30 +614,57 @@ int _bt_set_non_blocking_tty(int sk)
        return BLUETOOTH_ERROR_NONE;
 }
 
+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;
+               }
+               _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;
-       GDBusProxy *proxy;
-       GError *error = NULL;
+       GVariantIter *iter = NULL;
 
-       proxy = _bt_get_adapter_proxy();
-       retv_if(proxy == NULL, NULL);
+       conn = _bt_gdbus_get_system_gconn();
+       retv_if(conn == NULL, NULL);
 
-       result = g_dbus_proxy_call_sync(proxy, "FindDevice",
-                               g_variant_new("(s)", address),
+       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, &error);
-       if (result == NULL) {
-               if (error) {
-                       BT_ERR("FindDevice Fail: %s", error->message);
-                       g_clear_error(&error);
-               }
+                               -1,
+                               NULL,
+                               NULL);
+       if (!result) {
+               BT_ERR("Can't get managed objects");
                return NULL;
        }
 
-       g_variant_get(result, "(o)", &object_path);
-
+       /* 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;
 }
@@ -716,16 +697,20 @@ char *_bt_get_profile_uuid128(bt_profile_type_t profile_type)
 
 const char *_bt_convert_uuid_to_string(const char *uuid)
 {
+#define SHORT_UUID_COUNT       199
+#define LONG_UUID_COUNT                17
+
        if (!uuid)
                return NULL;
 
-       int i;
        int offset = 0;
        int uuid_len = 4;
-       static struct {
+       typedef struct {
                const char *uuid;
                const char *specification_name;
-       } bt_uuid_name[] = {
+       } uuid_name_s;
+       static uuid_name_s short_uuid_name[SHORT_UUID_COUNT] = {
+               // List should be sorted by UUID
                /* BT Classic Services */
                {"1101", "Serial Port Service"},
                {"1102", "LAN Access Using PPP Service"},
@@ -816,6 +801,17 @@ const char *_bt_convert_uuid_to_string(const char *uuid)
                {"181D", "Weight Scale"},
                {"181E", "Bond Management"},
                {"181F", "Continuous Glucose Monitoring"},
+               {"1820", "Internet Protocol Support Service"},
+               {"1821", "Indoor Positioning"},
+               {"1822", "Pulse Oximeter Service"},
+               {"1823", "HTTP Proxy"},
+               {"1824", "Transport Discovery"},
+               {"1825", "Object Transfer Service"},
+               {"1826", "Fitness Machine"},
+               {"1827", "Mesh Provisioning Service"},
+               {"1828", "Mesh Proxy Service"},
+               {"1829", "Reconnection Configuration"},
+               {"183A", "Insulin Delivery"},
 
                /* GATT Declarations */
                {"2800", "Primary Service Declaration"},
@@ -897,25 +893,60 @@ const char *_bt_convert_uuid_to_string(const char *uuid)
                {"2A68", "Navigation"},
                {"2A6D", "Pressure"},
                {"2A6E", "Temperature"},
-
+               {"2A8E", "Height"},
+               {"2A90", "Last Name"},
+               {"2A91", "Maximum Recommended Heart Rate"},
+               {"2A92", "Resting Heart Rate"},
+               {"2A98", "Weight"},
+               {"2A9B", "Body Composition Feature"},
+               {"2A9C", "Body Composition Measurement"},
+               {"2A9D", "Weight Measurement"},
+               {"2AA2", "Language"},
+               {"2AA4", "Bond Management Control Point"},
+               {"2AA5", "Bond Management Features"},
+               {"2AA6", "Central Address Resolution"},
+               {"2AAD", "Indoor Positioning Configuration"},
+               {"2AB5", "Location Name"},
+               {"2AB6", "URI"},
+               {"2ABC", "TDS Control Point"},
+               {"2AC9", "Resolvable Private Address Only"},
+               {"2ACC", "Fitness Machine Feature"},
+               {"2ACE", "Cross Trainer Data"},
+               {"2AD3", "Training Status"},
+               {"2AD7", "Supported Heart Rate Range"},
+               {"2AD9", "Fitness Machine Control Point"},
+               {"2ADA", "Fitness Machine Status"},
+               {"2B1D", "RC Feature"},
+               {"2B1E", "RC Settings"},
+               {"2B1F", "Reconnection Configuration Control Point"},
+       };
+       static uuid_name_s long_uuid_name[LONG_UUID_COUNT] = {
+               // List should be sorted by UUID
                /* Custom uuids */
-               {"7905F431-B5CE-4E99-A40F-4B1E122D00D0", "Apple Notification Center Service"},
-               {"9FBF120D-6301-42D9-8C58-25E699A21DBD", "Notifications Source"},
-               {"69D1D8F3-45E1-49A8-9821-9BBDFDAAD9D9", "Control Point"},
+               {"1AB7C24D-185A-45B9-90D4-F7AB1A71949A", "Samsung Health Service"},
                {"22EAC6E9-24D6-4BB5-BE44-B36ACE7C7BFB", "Data Source"},
+               {"2F7CABCE-808D-411F-9A0C-BB92BA96C102", "Entity Update"},
+               {"32D1955A-E5AA-4A96-9A49-08538DA8B8F6", "Samsung Gear Fit Manager Service"},
+               {"69D1D8F3-45E1-49A8-9821-9BBDFDAAD9D9", "Control Point"},
+               {"7905F431-B5CE-4E99-A40F-4B1E122D00D0", "Apple Notification Center Service"},
                {"89D3502B-0F36-433A-8EF4-C502AD55F8DC", "Apple Media Service"},
+               {"9A3F68E0-86CE-11E5-A309-0002A5D5C51B", "Samsung Gear Manager Service"},
                {"9B3C81D8-57B1-4A8A-B8DF-0E56F7CA51C2", "Remote Command"},
-               {"2F7CABCE-808D-411F-9A0C-BB92BA96C102", "Entity Update"},
+               {"9FBF120D-6301-42D9-8C58-25E699A21DBD", "Notifications Source"},
+               {"A49EB41E-CB06-495C-9F4F-BB80A90CDF00", "Samsung Gear Manager Service"},
+               {"ADE3D529-C784-4F63-A987-EB69F70EE816", "IoT OIC Service"},
+               {"C2051EE0-804D-4D50-A12C-15E243852100", "Notifications Source"},
+               {"C2F2CC0F-C085-4DD4-BE5A-ACA3074BBC72", "Control Point"},
                {"C6B2F38C-23AB-46D8-A6AB-A3A870BBD5D7", "Entity Attribute"},
-               {"9A3F68E0-86CE-11E5-A309-0002A5D5C51B", "Samsung Gear Manager Service"},
-               {"c2f2cc0f-c085-4dd4-be5a-aca3074bbc72", "Control Point"},
-               {"cece518b-28d7-4171-92d5-76a1e249a3b9", "Notifications Source"},
-               {"32D1955A-E5AA-4A96-9A49-08538DA8B8F6", "Samsung Gear Fit Manager Service"},
+               {"CECE518B-28D7-4171-92D5-76A1E249A3B9", "Notifications Source"},
                {"FE53FF98-B259-4337-B56A-0EC9F82C6BAD", "Control Point"},
-               {"C2051EE0-804D-4D50-A12C-15E243852100", "Notifications Source"},
-               {"1ab7c24d-185a-45b9-90d4-f7ab1a71949a", "Samsung Health Service"},
-               {NULL, NULL}
        };
+       const uuid_name_s *uuid_name = short_uuid_name;
+       static const char *unknown_name = "Unknown";
+       int start = 0;
+       int end = SHORT_UUID_COUNT - 1;
+       int p;
+       int ret;
 
        if (strlen(uuid) == 36) {
                if (!g_ascii_strncasecmp(uuid + 9, "0000-1000-8000-00805F9B34FB", 27))
@@ -923,16 +954,24 @@ const char *_bt_convert_uuid_to_string(const char *uuid)
                else {
                        offset = 0;
                        uuid_len = 36;
+                       end = LONG_UUID_COUNT - 1;
+                       uuid_name = long_uuid_name;
                }
        } else if (strlen(uuid) >= 8)
                offset = 4;
 
-       for (i = 0; bt_uuid_name[i].uuid; i++) {
-               if (!g_ascii_strncasecmp(uuid + offset, bt_uuid_name[i].uuid, uuid_len))
-                       return (char *)bt_uuid_name[i].specification_name;
+       while (start <= end) {
+               p = start + (end - start) / 2;
+               ret = g_ascii_strncasecmp(uuid + offset, uuid_name[p].uuid, uuid_len);
+               if (ret == 0)
+                       return uuid_name[p].specification_name;
+               else if (ret < 0)
+                       end = p - 1;
+               else
+                       start = p + 1;
        }
 
-       return "Unknown";
+       return unknown_name;
 }
 
 const char *_bt_convert_error_to_string(int error)
@@ -1013,6 +1052,8 @@ const char *_bt_convert_error_to_string(int error)
                return "NOT_INITIALIZED";
        case BLUETOOTH_ERROR_AUTHENTICATION_REJECTED:
                return "AUTHENTICATION REJECTED";
+       case BLUETOOTH_ERROR_AUTHORIZATION_REJECTED:
+               return "AUTHORIZATION REJECTED";
        default:
                return "UNKNOWN";
        }
@@ -1023,18 +1064,20 @@ const 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_AUTH_FAILURE:
+               return "Authentication Failure";
        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_CONNECTION_TIMEOUT:
+               return "Link loss";
+       case (int)BLUETOOTH_ERROR_CONNECTION_REJECTED_DUE_TO_SECURITY_REASONS:
+               return "Connection rejected due to security reasons";
        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_LOCAL_HOST_TERM:
+               return "Connection terminated by local host";
        case (int)BLUETOOTH_ERROR_REPEATED_ATTEMPTS:
                return "Repeated attempts";
        case (int)BLUETOOTH_ERROR_LMP_RESPONSE_TIMEOUT:
@@ -1054,6 +1097,24 @@ const char *_bt_convert_disc_reason_to_string(int reason)
        }
 }
 
+const char *_bt_convert_profile_state_to_string(bt_profile_state_t state)
+{
+       switch (state) {
+       case BT_PROFILE_STATE_UNAVAILABLE:
+               return "UNAVAILABLE";
+       case BT_PROFILE_STATE_DISCONNECTED:
+               return "DISCONNECTED";
+       case BT_PROFILE_STATE_CONNECTING:
+               return "CONNECTING";
+       case BT_PROFILE_STATE_CONNECTED:
+               return "CONNECTED";
+       case BT_PROFILE_STATE_DISCONNECTING:
+               return "DISCONNECTING";
+       }
+
+       return NULL;
+}
+
 const char *_bt_convert_service_function_to_string(int function)
 {
        int i;
@@ -1116,6 +1177,7 @@ const char *_bt_convert_service_function_to_string(int function)
                {BT_GET_PROFILE_CONNECTED_DEVICES, "BT_GET_PROFILE_CONNECTED_DEVICES"},
                {BT_ENABLE_FORCE_HCI_DUMP, "BT_ENABLE_FORCE_HCI_DUMP"},
                {BT_SET_PASSKEY_NOTIFICATION, "BT_SET_PASSKEY_NOTIFICATION"},
+               {BT_DISCONNECT_DEVICE, "BT_DISCONNECT_DEVICE"},
                {BT_BOND_DEVICE, "BT_BOND_DEVICE"},
                {BT_BOND_DEVICE_BY_TYPE, "BT_BOND_DEVICE_BY_TYPE"},
                {BT_CANCEL_BONDING, "BT_CANCEL_BONDING"},
@@ -1385,6 +1447,7 @@ int _bt_eventsystem_set_value(const char *event, const char *key, const char *va
 {
        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;
 
@@ -1421,7 +1484,7 @@ void __bt_get_auth_info(GVariant *reply, char *auth_info)
                is_alias_set = FALSE;
        }
        if (is_alias_set == FALSE) {
-               tmp_value = g_variant_lookup_value(reply, "ManufacturerDataLen",
+               tmp_value = g_variant_lookup_value(reply, "LegacyManufacturerDataLen",
                                                                G_VARIANT_TYPE_UINT16);
                if (tmp_value) {
                        manufacturer_data_len = g_variant_get_uint16(tmp_value);
@@ -1434,7 +1497,7 @@ void __bt_get_auth_info(GVariant *reply, char *auth_info)
                } else
                        manufacturer_data_len = 0;
 
-               tmp_value = g_variant_lookup_value(reply, "ManufacturerData",
+               tmp_value = g_variant_lookup_value(reply, "LegacyManufacturerData",
                                                                G_VARIANT_TYPE_ARRAY);
                if (tmp_value) {
                        if ((manufacturer_data_len == 0) ||