Modify build feature names as Tizen 3.0 build option naming rule
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-common.c
index 515d4ce..db71e3a 100644 (file)
@@ -37,7 +37,7 @@
 #include "bt-request-sender.h"
 #include "bt-event-handler.h"
 
-#ifdef TIZEN_DPM_ENABLE
+#ifdef TIZEN_FEATURE_BT_DPM
 #include "bt-dpm.h"
 #endif
 
@@ -50,6 +50,8 @@ static guint bus_id;
 
 static GDBusConnection *system_gconn = NULL;
 
+static gboolean bt_enabled = FALSE;
+
 #define DBUS_TIMEOUT 20 * 1000 /* 20 Seconds */
 
 GDBusConnection *g_bus_get_private_conn(void)
@@ -98,13 +100,10 @@ GDBusConnection *_bt_gdbus_init_system_gconn(void)
 
 GDBusConnection *_bt_gdbus_get_system_gconn(void)
 {
-
-
-       if (system_gconn == NULL) {
+       if (system_gconn == NULL)
                system_gconn = _bt_gdbus_init_system_gconn();
-       } else if (g_dbus_connection_is_closed(system_gconn)) {
+       else if (g_dbus_connection_is_closed(system_gconn))
                system_gconn = g_bus_get_private_conn();
-       }
 
        return system_gconn;
 }
@@ -222,21 +221,40 @@ void _bt_divide_device_class(bluetooth_device_class_t *device_class,
 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[0] != '\0') {
+                       if (ptr[0] != ':')
+                               return;
+
+                       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);
 
-        for (i = 0; i < BT_ADDRESS_LENGTH_MAX; i++) {
-                addr[i] = strtol(address, &ptr, 16);
-                if (ptr[0] != '\0') {
-                        if (ptr[0] != ':')
-                                return;
+       len = strlen(address);
+       ret_if(len != BT_ADDRESS_STRING_SIZE - 1);
+
+       strncpy(addr, address, len);
 
-                        address = ptr + 1;
-                }
-        }
+       addr[len-1] = 'X';
+       addr[len-2] = 'X';
+
+       return;
 }
 
 void _bt_convert_addr_type_to_string(char *address,
@@ -339,6 +357,7 @@ static void __new_connection_method(GDBusConnection *connection,
                char addr[20];
                bluetooth_device_address_t  remote_addr1;
                bt_new_connection_cb cb = user_data;
+               char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
                int fd;
 
                g_variant_get(parameters, "(oha{sv})", &obj_path, &index,
@@ -364,11 +383,12 @@ static void __new_connection_method(GDBusConnection *connection,
                        g_error_free(err);
                        return;
                }
-               BT_INFO("Object Path %s", obj_path);
 
-               _bt_device_path_to_address(obj_path, addr);
+               _bt_convert_device_path_to_address(obj_path, addr);
                _bt_convert_addr_string_to_type(remote_addr1.addr, (const char *)addr);
-               BT_INFO("fd: %d, address %s", fd, addr);
+
+               _bt_convert_addr_string_to_secure_string(secure_address, addr);
+               BT_INFO("fd: %d, address %s", fd, secure_address);
 
                g_dbus_method_invocation_return_value(invocation, NULL);
 
@@ -992,9 +1012,7 @@ int _bt_discover_services(char *address, char *uuid, void *cb,
                }
                if (ret)
                        g_variant_unref(ret);
-
                g_object_unref(adapter_proxy);
-
                object_path = _bt_get_device_object_path(address);
                if (object_path == NULL)
                        return BLUETOOTH_ERROR_INTERNAL;
@@ -1020,6 +1038,7 @@ int _bt_cancel_discovers(char *address)
        char *object_path;
        GDBusProxy *proxy;
        GDBusProxy *adapter_proxy;
+       GVariant *ret = NULL;
        GError *err = NULL;
        object_path = _bt_get_device_object_path(address);
        if (object_path == NULL) {
@@ -1038,16 +1057,14 @@ int _bt_cancel_discovers(char *address)
                }
                if (ret)
                        g_variant_unref(ret);
-
                g_object_unref(adapter_proxy);
-
                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);
-       g_dbus_proxy_call_sync(proxy, "CancelDiscovery",
+       ret = g_dbus_proxy_call_sync(proxy, "CancelDiscovery",
                NULL,
                G_DBUS_CALL_FLAGS_NONE,
                DBUS_TIMEOUT, NULL,
@@ -1057,6 +1074,8 @@ int _bt_cancel_discovers(char *address)
                g_clear_error(&err);
                return BLUETOOTH_ERROR_INTERNAL;
        }
+       if (ret)
+               g_variant_unref(ret);
        if (proxy)
                g_object_unref(proxy);
        return BLUETOOTH_ERROR_NONE;
@@ -1276,10 +1295,12 @@ int _bt_get_adapter_path(GDBusConnection *conn, char *path)
        result = g_dbus_proxy_call_sync(manager_proxy, "DefaultAdapter", NULL,
                        G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
        if (!result) {
-               if (err != NULL)
-                       BT_ERR("Fail to get DefaultAdapter (Error: %s)", err->message);
-               else
+               if (err != NULL) {
+                       if (!g_strrstr(err->message, "ServiceUnknown"))
+                               BT_ERR("Fail to get DefaultAdapter (Error: %s)", err->message);
+               } else {
                        BT_ERR("Fail to get DefaultAdapter");
+               }
 
                goto fail;
        }
@@ -1333,9 +1354,8 @@ 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);
        }
@@ -1351,9 +1371,8 @@ static char *__bt_extract_device_path(GVariantIter *iter, char *address)
                retv_if(object_path == NULL, NULL);
                _bt_convert_device_path_to_address(object_path, device_address);
 
-               if (g_strcmp0(address, device_address) == 0) {
+               if (g_strcmp0(address, device_address) == 0)
                        return g_strdup(object_path);
-               }
        }
        return NULL;
 }
@@ -1410,28 +1429,6 @@ fail:
        return object_path;
 }
 
-void _bt_device_path_to_address(const char *device_path, char *device_address)
-{
-       char address[BT_ADDRESS_STRING_SIZE] = { 0 };
-       char *dev_addr = NULL;
-
-       if (!device_path || !device_address)
-               return;
-
-       dev_addr = strstr(device_path, "dev_");
-       if (dev_addr != NULL) {
-               char *pos = NULL;
-               dev_addr += 4;
-               g_strlcpy(address, dev_addr, sizeof(address));
-
-               while ((pos = strchr(address, '_')) != NULL) {
-                       *pos = ':';
-               }
-
-               g_strlcpy(device_address, address, BT_ADDRESS_STRING_SIZE);
-       }
-}
-
 GDBusConnection *_bt_init_system_gdbus_conn(void)
 {
        GError *error = NULL;
@@ -1499,6 +1496,8 @@ int _bt_check_privilege(int service_type, int service_function)
 {
        int result;
 
+       BT_CHECK_ENABLED(return);
+
        BT_INIT_PARAMS();
        BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
 
@@ -1551,6 +1550,135 @@ GVariant *_bt_get_managed_objects(void)
        return result;
 }
 
+gboolean _bt_check_enabled_internal(void)
+{
+       if (bt_enabled == TRUE)
+               return TRUE;
+
+       if (bluetooth_check_adapter() == BLUETOOTH_ADAPTER_ENABLED)
+               bt_enabled = TRUE;
+
+       return bt_enabled;
+}
+
+void _bt_set_adapter_internal_status(gboolean enabled)
+{
+       bt_enabled = enabled;
+}
+
+int _bt_get_uuid_specification_name(const char *uuid, char **name)
+{
+       int i;
+       int offset = 0;
+       int uuid_len = 4;
+       static struct {
+               const char *uuid;
+               const char *specification_name;
+       } bt_uuid_name[] = {
+               /* GATT Services */
+               {"1800", "Generic Access"},
+               {"1801", "Generic Attribute"},
+               {"1802", "Immediate Alert"},
+               {"1803", "Link Loss"},
+               {"1804", "Tx Power"},
+               {"1805", "Current Time Service"},
+               {"1806", "Reference Time Update Service"},
+               {"1807", "Next DST Change Service"},
+               {"1808", "Glucose"},
+               {"1809", "Health Thermometer"},
+               {"180A", "Device Information"},
+               {"180D", "Heart Rate"},
+               {"180F", "Battery Service"},
+               {"1810", "Blood Pressure"},
+               {"1811", "Alert Notification Service"},
+               {"1812", "Human Interface Device"},
+
+               /* GATT Declarations */
+               {"2800", "Primary Service Declaration"},
+               {"2801", "Secondary Service Declaration"},
+               {"2802", "Include Declaration"},
+               {"2803", "Characteristic Declaration"},
+
+               /* GATT Descriptors */
+               {"2900", "Characteristic Extended Properties"},
+               {"2901", "Characteristic User Description"},
+               {"2902", "Client Characteristic Configuration"},
+               {"2903", "Server Characteristic Configuration"},
+               {"2904", "Characteristic Format"},
+               {"2905", "Characteristic Aggregate Formate"},
+               {"2906", "Valid Range"},
+               {"2907", "External Report Reference"},
+               {"2908", "Report Reference"},
+
+               /* GATT Characteristics */
+               {"2A00", "Device Name"},
+               {"2A01", "Appearance"},
+               {"2A02", "Peripheral Privacy Flag"},
+               {"2A03", "Reconnection Address"},
+               {"2A04", "Peripheral Preferred Connection Parameters"},
+               {"2A05", "Service Changed"},
+               {"2A06", "Alert Level"},
+               {"2A07", "Tx Power Level"},
+               {"2A08", "Date Time"},
+               {"2A09", "Day of Week"},
+               {"2A0A", "Day Date Time"},
+               {"2A19", "Battery Level"},
+               {"2A1E", "Intermediate Temperature"},
+               {"2A23", "System ID"},
+               {"2A24", "Model Number String"},
+               {"2A25", "Serial Number String"},
+               {"2A26", "Firmware Revision String"},
+               {"2A27", "Hardware Revision String"},
+               {"2A28", "Software Revision String"},
+               {"2A29", "Manufacturer Name String"},
+               {"2A2A", "IEEE 11073-20601 Regulatory Certification Data List"},
+               {"2A2B", "Current Time"},
+               {"2A37", "Heart Rate Measurement"},
+               {"2A38", "Body Sensor Location"},
+               {"2A3F", "Alert Status"},
+               {"2A46", "New Alert"},
+               {"2A4A", "HID Information"},
+               {"2A4C", "HID Control Point"},
+               {"2A50", "PnP ID"},
+
+               /* 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"},
+               {"22EAC6E9-24D6-4BB5-BE44-B36ACE7C7BFB", "Data Source"},
+               {"89D3502B-0F36-433A-8EF4-C502AD55F8DC", "Apple Media Service"},
+               {"9B3C81D8-57B1-4A8A-B8DF-0E56F7CA51C2", "Remote Command"},
+               {"2F7CABCE-808D-411F-9A0C-BB92BA96C102", "Entity Update"},
+               {"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"},
+               {NULL, NULL}
+       };
+
+       if (!uuid || !name)
+               return BLUETOOTH_ERROR_INVALID_PARAM;
+       if (strlen(uuid) == 36) {
+               if (!g_ascii_strncasecmp(uuid + 9, "0000-1000-8000-00805F9B34FB", 27))
+                       offset = 4;
+               else {
+                       offset = 0;
+                       uuid_len = 36;
+               }
+       } 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)) {
+                       *name = g_strdup(bt_uuid_name[i].specification_name);
+                       return BLUETOOTH_ERROR_NONE;
+               }
+       }
+
+       *name = g_strdup("Unknown");
+       return BLUETOOTH_ERROR_NONE;
+}
+
 BT_EXPORT_API int bluetooth_is_supported(void)
 {
        int is_supported = 0;
@@ -1612,26 +1740,29 @@ BT_EXPORT_API int bluetooth_register_callback(bluetooth_cb_func_ptr callback_ptr
        _bt_set_user_data(BT_COMMON, (void *)callback_ptr, user_data);
 
        /* Register All events */
-       ret = _bt_register_event(BT_ADAPTER_EVENT, (void *)callback_ptr, user_data);
-       if (ret != BLUETOOTH_ERROR_NONE)
-               goto fail;
-       ret = _bt_register_event(BT_DEVICE_EVENT, (void *)callback_ptr, user_data);
-       if (ret != BLUETOOTH_ERROR_NONE)
-               goto fail;
-       ret = _bt_register_event(BT_NETWORK_EVENT, (void *)callback_ptr, user_data);
-       if (ret != BLUETOOTH_ERROR_NONE)
-               goto fail;
-       ret = _bt_register_event(BT_RFCOMM_CLIENT_EVENT, (void *)callback_ptr, user_data);
-       if (ret != BLUETOOTH_ERROR_NONE)
-               goto fail;
-       ret = _bt_register_event(BT_RFCOMM_SERVER_EVENT, (void *)callback_ptr, user_data);
-       if (ret != BLUETOOTH_ERROR_NONE)
-               goto fail;
+       if (ret != BLUETOOTH_ERROR_ALREADY_INITIALIZED) {
+               ret = _bt_register_event(BT_ADAPTER_EVENT, (void *)callback_ptr, user_data);
+               if (ret != BLUETOOTH_ERROR_NONE)
+                       goto fail;
+               ret = _bt_register_event(BT_DEVICE_EVENT, (void *)callback_ptr, user_data);
+               if (ret != BLUETOOTH_ERROR_NONE)
+                       goto fail;
+               ret = _bt_register_event(BT_NETWORK_EVENT, (void *)callback_ptr, user_data);
+               if (ret != BLUETOOTH_ERROR_NONE)
+                       goto fail;
+               ret = _bt_register_event(BT_RFCOMM_CLIENT_EVENT, (void *)callback_ptr, user_data);
+               if (ret != BLUETOOTH_ERROR_NONE)
+                       goto fail;
+               ret = _bt_register_event(BT_RFCOMM_SERVER_EVENT, (void *)callback_ptr, user_data);
+               if (ret != BLUETOOTH_ERROR_NONE)
+                       goto fail;
+
 #ifdef GATT_NO_RELAY
-       ret = _bt_register_event(BT_GATT_BLUEZ_EVENT, (void *)callback_ptr, user_data);
-       if (ret != BLUETOOTH_ERROR_NONE)
-               goto fail;
+               ret = _bt_register_event(BT_GATT_BLUEZ_EVENT, (void *)callback_ptr, user_data);
+               if (ret != BLUETOOTH_ERROR_NONE)
+                       goto fail;
 #endif
+       }
 
        _bt_register_name_owner_changed();
 
@@ -1648,9 +1779,8 @@ BT_EXPORT_API int bluetooth_unregister_callback(void)
 
 
        ret = _bt_deinit_event_handler();
-       if (ret != BLUETOOTH_ERROR_NONE) {
+       if (ret != BLUETOOTH_ERROR_NONE)
                BT_ERR("Fail to deinit the event handler");
-       }
 
        _bt_unregister_name_owner_changed();