Fix the prevent issue
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-common.c
index 3e61585..75ae33c 100644 (file)
@@ -126,7 +126,6 @@ bt_user_info_t *_bt_get_user_data(int type)
 void _bt_common_event_cb(int event, int result, void *param,
                                        void *callback, void *user_data)
 {
-       BT_DBG("bt_common_event_cb, event: %d", event);
        bluetooth_event_param_t bt_event = { 0, };
        bt_event.event = event;
        bt_event.result = result;
@@ -217,8 +216,7 @@ void _bt_divide_device_class(bluetooth_device_class_t *device_class,
        }
 }
 
-void _bt_convert_addr_string_to_type(unsigned char *addr,
-                                       const char *address)
+void _bt_convert_addr_string_to_type(unsigned char *addr, const char *address)
 {
        int i;
        char *ptr = NULL;
@@ -237,8 +235,7 @@ void _bt_convert_addr_string_to_type(unsigned char *addr,
        }
 }
 
-void _bt_convert_addr_string_to_secure_string(char *addr,
-                                       const char *address)
+void _bt_convert_addr_string_to_secure_string(char *addr, const char *address)
 {
        int len;
 
@@ -249,15 +246,21 @@ void _bt_convert_addr_string_to_secure_string(char *addr,
        ret_if(len != BT_ADDRESS_STRING_SIZE - 1);
 
        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;
 }
 
-void _bt_convert_addr_type_to_string(char *address,
-                               unsigned char *addr)
+void _bt_convert_addr_type_to_string(char *address, unsigned char *addr)
 {
        ret_if(address == NULL);
        ret_if(addr == NULL);
@@ -268,6 +271,15 @@ void _bt_convert_addr_type_to_string(char *address,
                        addr[3], addr[4], addr[5]);
 }
 
+void _bt_convert_addr_type_to_secure_string(char *address, unsigned char *addr)
+{
+       ret_if(address == NULL);
+       ret_if(addr == NULL);
+
+       g_snprintf(address, BT_ADDRESS_STRING_SIZE,
+                       "XX:XX:XX:XX:%2.2X:%2.2X", addr[4], addr[5]);
+}
+
 const char *_bt_convert_error_to_string(int error)
 {
        switch (error) {
@@ -433,8 +445,6 @@ const char *_bt_convert_service_function_to_string(int function)
                {BT_UPDATE_LE_CONNECTION_MODE, "BT_UPDATE_LE_CONNECTION_MODE"},
                {BT_SET_PROFILE_TRUSTED, "BT_SET_PROFILE_TRUSTED"},
                {BT_GET_PROFILE_TRUSTED, "BT_GET_PROFILE_TRUSTED"},
-               {BT_SET_PROFILE_RESTRICTED, "BT_SET_PROFILE_RESTRICTED"},
-               {BT_GET_PROFILE_RESTRICTED, "BT_GET_PROFILE_RESTRICTED"},
                {BT_HID_CONNECT, "BT_HID_CONNECT"},
                {BT_HID_DISCONNECT, "BT_HID_DISCONNECT"},
                {BT_HID_DEVICE_ACTIVATE, "BT_HID_DEVICE_ACTIVATE"},
@@ -671,7 +681,6 @@ static GDBusConnection *gconn;
 static int latest_id = -1;
 #define BT_RFCOMM_ID_MAX 245
 static gboolean id_used[BT_RFCOMM_ID_MAX];
-GDBusNodeInfo *new_conn_node;
 
 static const gchar rfcomm_agent_xml[] =
 "<node name='/'>"
@@ -859,7 +868,7 @@ static GDBusProxy *__bt_gdbus_get_device_proxy(char *object_path)
                                                BT_DEVICE_INTERFACE,
                                                NULL, &err);
 
-       if (err) {
+       if (device_gproxy == NULL && err) {
                BT_ERR("Unable to create proxy: %s", err->message);
                g_clear_error(&err);
                return NULL;
@@ -882,6 +891,7 @@ void _bt_unregister_gdbus(int object_id)
 int _bt_register_new_conn(const char *path, bt_new_connection_cb cb)
 {
        GDBusConnection *gconn;
+       GDBusNodeInfo *node_info;
        int id;
        GError *error = NULL;
 
@@ -889,16 +899,15 @@ int _bt_register_new_conn(const char *path, bt_new_connection_cb cb)
        if (gconn == NULL)
                return -1;
 
-       if (new_conn_node == NULL)
-               new_conn_node = _bt_get_gdbus_node(rfcomm_agent_xml);
-
-       if (new_conn_node == NULL)
+       node_info = _bt_get_gdbus_node(rfcomm_agent_xml);
+       if (node_info == NULL)
                return -1;
 
        id = g_dbus_connection_register_object(gconn, path,
-                                               new_conn_node->interfaces[0],
+                                               node_info->interfaces[0],
                                                &method_table,
                                                cb, NULL, &error);
+       g_dbus_node_info_unref(node_info);
        if (id == 0) {
                BT_ERR("Failed to register: %s", error->message);
                g_error_free(error);
@@ -982,6 +991,7 @@ fail:
 int _bt_register_new_conn_ex(const char *path, const char *bus_name, bt_new_connection_cb cb)
 {
        GDBusConnection *gconn;
+       GDBusNodeInfo *node_info;
        int id;
        GError *error = NULL;
 
@@ -989,16 +999,16 @@ int _bt_register_new_conn_ex(const char *path, const char *bus_name, bt_new_conn
        if (gconn == NULL)
                return -1;
 
-       if (new_conn_node == NULL)
-               new_conn_node = _bt_get_gdbus_node_ex(rfcomm_agent_xml, bus_name);
+       node_info = _bt_get_gdbus_node_ex(rfcomm_agent_xml, bus_name);
 
-       if (new_conn_node == NULL)
+       if (node_info == NULL)
                return -1;
 
        id = g_dbus_connection_register_object(gconn, path,
-                                               new_conn_node->interfaces[0],
+                                               node_info->interfaces[0],
                                                &method_table,
                                                cb, NULL, &error);
+       g_dbus_node_info_unref(node_info);
        if (id == 0) {
                BT_ERR("Failed to register: %s", error->message);
                g_error_free(error);
@@ -1453,62 +1463,53 @@ int _bt_discover_service_uuids(char *address, char *remote_uuid)
        proxy = g_dbus_proxy_new_sync(gconn, G_DBUS_PROXY_FLAGS_NONE, NULL,
                                BT_BLUEZ_NAME, object_path, BT_PROPERTIES_INTERFACE, NULL,
                                &err);
+
+       g_free(object_path);
+
        retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
        if (err) {
                BT_ERR("DBus Error: [%s]", err->message);
                g_clear_error(&err);
        }
-       ret = g_dbus_proxy_call_sync(proxy, "GetAll",
-                       g_variant_new("(s)", BT_DEVICE_INTERFACE),
-                       G_DBUS_CALL_FLAGS_NONE,
-                       DBUS_TIMEOUT, NULL,
-                       &err);
+       ret = g_dbus_proxy_call_sync(proxy, "Get",
+                       g_variant_new("(ss)", BT_DEVICE_INTERFACE, "UUIDs"),
+                       G_DBUS_CALL_FLAGS_NONE, DBUS_TIMEOUT, NULL, &err);
        if (err) {
                result = BLUETOOTH_ERROR_INTERNAL;
                BT_ERR("DBus Error : %s", err->message);
                g_clear_error(&err);
                goto done;
-       }
-       if (ret == NULL) {
-               BT_ERR("g_dbus_proxy_call_sync function return NULL");
-               result = BLUETOOTH_ERROR_INTERNAL;
-               goto done;
+       } else {
+               g_variant_get(ret, "(v)", &value);
+               uuid_value = (char **)g_variant_get_strv(value, &size);
+               BT_DBG("Size items %d", size);
        }
 
-       g_variant_get(ret, "(@a{sv})", &value);
-       g_variant_unref(ret);
-       if (value) {
-               GVariant *temp_value = g_variant_lookup_value(value, "UUIDs",
-                       G_VARIANT_TYPE_STRING_ARRAY);
-
-               if (temp_value) {
-                       size = g_variant_get_size(temp_value);
-                       if (size > 0) {
-                               uuid_value = (char **)g_variant_get_strv(temp_value, &size);
-                               BT_DBG("Size items %d", size);
-
-                               if (uuid_value) {
-                                       for (i = 0; uuid_value[i] != NULL; i++) {
-                                               BT_DBG("Remote uuids %s", uuid_value[i]);
-                                               if (strcasecmp(uuid_value[i], remote_uuid) == 0) {
-                                                       result = BLUETOOTH_ERROR_NONE;
-                                                       g_variant_unref(temp_value);
-                                                       goto done;
-                                               }
-                                       }
-                               }
+       if (uuid_value) {
+               for (i = 0; uuid_value[i] != NULL; i++) {
+                       BT_DBG("Remote uuids %s", uuid_value[i]);
+                       if (uuid_value[i] == NULL) {
+                               BT_ERR("_bt_discover_service_uuids Error Parameter are NULL..\n");
+                               continue;
+                       } else if (g_ascii_strcasecmp(uuid_value[i], remote_uuid) == 0) {
+                                       result = BLUETOOTH_ERROR_NONE;
+                                       goto done;
                        }
-                       g_variant_unref(temp_value);
                }
        }
+
 done:
        if (proxy)
                g_object_unref(proxy);
-       if (value)
-               g_variant_unref(value);
        if (uuid_value)
                g_free(uuid_value);
 
+       if (value)
+               g_variant_unref(value);
+
+       if (ret)
+               g_variant_unref(ret);
+
        BT_DBG("-");
        return result;
 }
@@ -1533,44 +1534,34 @@ int _bt_get_cod_by_address(char *address, bluetooth_device_class_t *dev_class)
        proxy = g_dbus_proxy_new_sync(gconn, G_DBUS_PROXY_FLAGS_NONE, NULL,
                                BT_BLUEZ_NAME, object_path, BT_PROPERTIES_INTERFACE, NULL,
                                &err);
+
+       g_free(object_path);
+
        retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
        if (err) {
                BT_ERR("DBus Error: [%s]", err->message);
                g_clear_error(&err);
        }
 
-       result = g_dbus_proxy_call_sync(proxy, "GetAll",
-                       g_variant_new("(s)", BT_DEVICE_INTERFACE),
-                       G_DBUS_CALL_FLAGS_NONE,
-                       DBUS_TIMEOUT, NULL,
-                       &err);
+       result = g_dbus_proxy_call_sync(proxy, "Get",
+                       g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Class"),
+                       G_DBUS_CALL_FLAGS_NONE, DBUS_TIMEOUT, NULL, &err);
        if (err) {
                ret = BLUETOOTH_ERROR_INTERNAL;
                BT_ERR("DBus Error : %s", err->message);
                g_clear_error(&err);
                goto done;
-       }
-       if (result == NULL) {
-               BT_ERR("g_dbus_proxy_call_sync function return NULL");
-               ret = BLUETOOTH_ERROR_INTERNAL;
-               goto done;
-       }
-       g_variant_get(result, "(@a{sv})", &value);
-       g_variant_unref(result);
-       if (value) {
-               GVariant *temp_value = g_variant_lookup_value(value, "Class",
-                       G_VARIANT_TYPE_UINT32);
-               class = g_variant_get_uint32(temp_value);
+       } else {
+               g_variant_get(result, "(v)", &value);
+               class = g_variant_get_uint32(value);
                _bt_divide_device_class(dev_class, class);
-               if (temp_value)
-                       g_variant_unref(temp_value);
+               g_variant_unref(value);
+               g_variant_unref(result);
        }
 
 done:
        if (proxy)
                g_object_unref(proxy);
-       if (value)
-               g_variant_unref(value);
 
        BT_DBG("-");
        return ret;
@@ -1621,6 +1612,30 @@ int _bt_disconnect_profile(char *address, char *uuid, void *cb,
        return BLUETOOTH_ERROR_NONE;
 }
 
+int _bt_disconnect_ext_profile(char *address, char *path)
+{
+       GDBusProxy *proxy;
+       char *object_path;
+
+       object_path = _bt_get_device_object_path(address);
+       if (object_path == NULL)
+               return BLUETOOTH_ERROR_INTERNAL;
+
+       proxy = __bt_gdbus_get_device_proxy(object_path);
+       g_free(object_path);
+       if (proxy == NULL) {
+               BT_ERR("Error while getting proxy");
+               return BLUETOOTH_ERROR_INTERNAL;
+       }
+
+       g_dbus_proxy_call(proxy, "DisconnectExtProfile",
+                       g_variant_new("(o)", path),
+                       G_DBUS_CALL_FLAGS_NONE,
+                       DBUS_TIMEOUT, NULL, NULL, NULL);
+       BT_DBG("-");
+       return BLUETOOTH_ERROR_NONE;
+}
+
 int _bt_get_adapter_path(GDBusConnection *conn, char *path)
 {
        GError *err = NULL;
@@ -1689,6 +1704,35 @@ fail:
 
 }
 
+void _bt_convert_device_path_to_addr_type(const char *device_path,
+                                         unsigned char *addr)
+{
+       char *dev_addr;
+       char address[BT_ADDRESS_STRING_SIZE] = { 0 };
+       int i;
+       char *addr_ptr = address;
+       char *ptr = NULL;
+
+       ret_if(device_path == NULL);
+       ret_if(addr == NULL);
+
+       dev_addr = strstr(device_path, "dev_");
+       if (dev_addr != NULL) {
+               dev_addr += 4;
+               g_strlcpy(addr_ptr, dev_addr, BT_ADDRESS_STRING_SIZE);
+
+               for (i = 0; i < BT_ADDRESS_LENGTH_MAX; i++) {
+                       addr[i] = strtol(addr_ptr, &ptr, 16);
+                       if (ptr[0] != '\0') {
+                               if (ptr[0] != '_')
+                                       return;
+
+                               addr_ptr = ptr + 1;
+                       }
+               }
+       }
+}
+
 void _bt_convert_device_path_to_address(const char *device_path,
                                                char *device_address)
 {
@@ -1881,9 +1925,14 @@ GVariant *_bt_get_managed_objects(void)
        if (error) {
                BT_ERR("Unable to create proxy: %s", error->message);
                g_clear_error(&error);
+               if (manager_proxy)
+                       g_object_unref(manager_proxy);
                return NULL;
        }
 
+       if (!manager_proxy)
+               return NULL;
+
        result = g_dbus_proxy_call_sync(manager_proxy,
                        "GetManagedObjects", NULL,
                        G_DBUS_CALL_FLAGS_NONE, -1,
@@ -1916,7 +1965,7 @@ void _bt_set_adapter_internal_status(gboolean enabled)
        bt_enabled = enabled;
 }
 
-int _bt_get_uuid_specification_name(const char *uuid, char **name)
+BT_EXPORT_API int bluetooth_get_uuid_name(const char *uuid, char **name)
 {
        int i;
        int offset = 0;
@@ -1925,6 +1974,67 @@ int _bt_get_uuid_specification_name(const char *uuid, char **name)
                const char *uuid;
                const char *specification_name;
        } bt_uuid_name[] = {
+               /* BT Classic Services */
+               {"1101", "Serial Port Service"},
+               {"1102", "LAN Access Using PPP Service"},
+               {"1103", "Dialup Networking Service"},
+               {"1104", "IrMCSync Service"},
+               {"1105", "OBEX Object Push Service"},
+               {"1106", "OBEX File Transfer Service"},
+               {"1107", "IrMC Sync Command Service"},
+               {"1108", "Headset Service"},
+               {"1109", "Cordless Telephony Service"},
+               {"110A", "Audio Source Service"},
+               {"110B", "Audio Sink Service"},
+               {"110C", "AV Remote Control Target Service"},
+               {"110D", "Advanced Audio Distribution Profile"},
+               {"110E", "AV Remote Control Service"},
+               {"110F", "Video Conferencing Service"},
+               {"1110", "Intercom Service"},
+               {"1111", "Fax Service"},
+               {"1112", "Headset Audio Gateway Service"},
+               {"1113", "WAP Service"},
+               {"1114", "WAP Client Service"},
+               {"1115", "PANU Service"},
+               {"1116", "NAP Service"},
+               {"1117", "GN Service"},
+               {"1118", "Direct Printing Service"},
+               {"1119", "Reference Printing Service"},
+               {"111A", "Basic Imaging Profile"},
+               {"111B", "Imaging Responder Service"},
+               {"111C", "Imaging Automatic Archive Service"},
+               {"111D", "Imaging Reference Objects Service"},
+               {"111E", "Handsfree Service"},
+               {"111F", "Handsfree Audio Gateway Service"},
+               {"1120", "Direct Printing Reference Objects Service"},
+               {"1121", "Reflected UI Service"},
+               {"1122", "Basic Printing Profile"},
+               {"1123", "Printing Status Service"},
+               {"1124", "Human Interface Device Service"},
+               {"1125", "Hardcopy Cable Replacement Profile"},
+               {"1126", "HCR Print Service"},
+               {"1127", "HCR Scan Service"},
+               {"112D", "SIM Access Service"},
+               {"112E", "Phonebook Access PCE Service"},
+               {"112F", "Phonebook Access PSE Service"},
+               {"1130", "Phonebook Access Profile"},
+               {"1132", "Message Access Server Service"},
+               {"1133", "Message Notification Server Service"},
+               {"1134", "Message Access Profile"},
+               {"1200", "PnP Information Service"},
+               {"1201", "Generic Networking Service"},
+               {"1202", "Generic File Transfer Service"},
+               {"1203", "Generic Audio Service"},
+               {"1204", "Generic Telephony Service"},
+               {"1205", "UPnP Service"},
+               {"1206", "UPnP Ip Service"},
+               {"1303", "Video Source Service"},
+               {"1304", "Video Sink Service"},
+               {"1305", "Video Distribution Profile"},
+               {"1400", "Health Device Profile"},
+               {"1401", "HDP Source Service"},
+               {"1402", "HDP Sink Service"},
+
                /* GATT Services */
                {"1800", "Generic Access"},
                {"1801", "Generic Attribute"},
@@ -1942,6 +2052,18 @@ int _bt_get_uuid_specification_name(const char *uuid, char **name)
                {"1810", "Blood Pressure"},
                {"1811", "Alert Notification Service"},
                {"1812", "Human Interface Device"},
+               {"1813", "Scan Parameters"},
+               {"1814", "Running Speed and Cadence"},
+               {"1815", "Automation IO"},
+               {"1816", "Cycling Speed and Cadence"},
+               {"1818", "Cycling Power"},
+               {"1819", "Location and Navigation"},
+               {"181A", "Environmental Sensing"},
+               {"181B", "Body Composition"},
+               {"181C", "User Data"},
+               {"181D", "Weight Scale"},
+               {"181E", "Bond Management"},
+               {"181F", "Continuous Glucose Monitoring"},
 
                /* GATT Declarations */
                {"2800", "Primary Service Declaration"},
@@ -1972,8 +2094,18 @@ int _bt_get_uuid_specification_name(const char *uuid, char **name)
                {"2A08", "Date Time"},
                {"2A09", "Day of Week"},
                {"2A0A", "Day Date Time"},
+               {"2A11", "Time with DST"},
+               {"2A12", "Time Accuracy"},
+               {"2A13", "Time Source"},
+               {"2A14", "Reference Time Information"},
+               {"2A16", "Time Update Control Point"},
+               {"2A17", "Time Update State"},
+               {"2A18", "Glucose Measurement"},
                {"2A19", "Battery Level"},
+               {"2A1C", "Temperature Measurement"},
+               {"2A1D", "Temperature Type"},
                {"2A1E", "Intermediate Temperature"},
+               {"2A21", "Measurement Interval"},
                {"2A23", "System ID"},
                {"2A24", "Model Number String"},
                {"2A25", "Serial Number String"},
@@ -1983,13 +2115,36 @@ int _bt_get_uuid_specification_name(const char *uuid, char **name)
                {"2A29", "Manufacturer Name String"},
                {"2A2A", "IEEE 11073-20601 Regulatory Certification Data List"},
                {"2A2B", "Current Time"},
+               {"2A34", "Glucose Measurement Context"},
+               {"2A35", "Blood Pressure Measurement"},
                {"2A37", "Heart Rate Measurement"},
                {"2A38", "Body Sensor Location"},
+               {"2A39", "Heart Rate Control Point"},
                {"2A3F", "Alert Status"},
                {"2A46", "New Alert"},
+               {"2A49", "Blood Pressure Feature"},
                {"2A4A", "HID Information"},
                {"2A4C", "HID Control Point"},
                {"2A50", "PnP ID"},
+               {"2A51", "Glucose Feature"},
+               {"2A52", "Record Access Control Point"},
+               {"2A53", "RSC Measurement"},
+               {"2A54", "RSC Feature"},
+               {"2A55", "SC Control Point"},
+               {"2A56", "Digital"},
+               {"2A58", "Analog"},
+               {"2A5A", "Aggregate"},
+               {"2A5B", "CSC Measurement"},
+               {"2A5C", "CSC Feature"},
+               {"2A5D", "Sensor Location"},
+               {"2A63", "Cycling Power Measurement"},
+               {"2A64", "Cycling Power Vector"},
+               {"2A65", "Cycling Power Feature"},
+               {"2A66", "Cycling Power Control Point"},
+               {"2A67", "Location and Speed"},
+               {"2A68", "Navigation"},
+               {"2A6D", "Pressure"},
+               {"2A6E", "Temperature"},
 
                /* Custom uuids */
                {"7905F431-B5CE-4E99-A40F-4B1E122D00D0", "Apple Notification Center Service"},
@@ -2003,6 +2158,11 @@ int _bt_get_uuid_specification_name(const char *uuid, char **name)
                {"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"},
+               {"FE53FF98-B259-4337-B56A-0EC9F82C6BAD", "Control Point"},
+               {"C2051EE0-804D-4D50-A12C-15E243852100", "Notifications Source"},
+               {"1ab7c24d-185a-45b9-90d4-f7ab1a71949a", "Samsung Health Service"},
+               {"ADE3D529-C784-4F63-A987-EB69F70EE816", "IoT OIC Service"},
                {NULL, NULL}
        };
 
@@ -2025,10 +2185,30 @@ int _bt_get_uuid_specification_name(const char *uuid, char **name)
                }
        }
 
+       BT_INFO("Unknown uuid : %s", uuid);
        *name = g_strdup("Unknown");
        return BLUETOOTH_ERROR_NONE;
 }
 
+int _bt_get_error_value_from_message(const char *error_message)
+{
+       if (error_message == NULL) {
+               BT_ERR("Error message NULL");
+               return BLUETOOTH_ERROR_INTERNAL;
+       }
+
+       BT_ERR("Error message = %s", error_message);
+
+       if (g_strrstr(error_message, BT_ERROR_OPERATION_NOT_SUPPORTED))
+               return BLUETOOTH_ERROR_NOT_IN_OPERATION;
+       else if (g_strrstr(error_message, BT_ERROR_OPERATION_NOT_ALLOWED))
+               return BLUETOOTH_ERROR_PERMISSION_DEINED;
+       else if (g_strrstr(error_message, BT_ERROR_ACCESS_DENIED))
+               return BLUETOOTH_ERROR_PERMISSION_DEINED;
+       else
+               return BLUETOOTH_ERROR_INTERNAL;
+}
+
 BT_EXPORT_API int bluetooth_is_supported(void)
 {
        int is_supported = 0;