Add the sender name in signal subscribe function
[platform/core/connectivity/bluetooth-frwk.git] / bt-otp / bt-otpserver.c
index 5095f87..76a8d9e 100644 (file)
@@ -33,6 +33,7 @@
 #include "bt-otpserver.h"
 #include "bluetooth-api.h"
 
+#include <cynara-client.h>
 
 #undef LOG_TAG
 #define LOG_TAG "BLUETOOTH_OTP"
@@ -64,6 +65,7 @@ static GDBusConnection *g_conn;
 static int property_sub_id = -1;
 static int adapter_sub_id = -1;
 static int device_sub_id = -1;
+static int device_property_sub_id = -1;
 static guint g_owner_id = 0;
 static guint server_watch_id = 0;
 
@@ -209,9 +211,8 @@ void _bt_otp_exit(void)
        if (ret != BLUETOOTH_ERROR_NONE)
                BT_ERR("Failed to stop ADV %d", ret);
 
-       if (main_loop != NULL) {
+       if (main_loop != NULL)
                g_main_loop_quit(main_loop);
-       }
 }
 
 static void _bt_otp_set_char_value(const char *obj_path,
@@ -511,6 +512,7 @@ void _bt_otp_start_write_on_fd()
        FILE *fp;
        char file_path[BT_FILE_PATH_MAX_LEN] = {0, };
        int length;
+       char err_msg[256] = {0, };
 
        snprintf(file_path, BT_FILE_PATH_MAX_LEN, "%s%s",
                                        directory, selected_object->name);
@@ -518,7 +520,8 @@ void _bt_otp_start_write_on_fd()
 
        fp = fopen(file_path, "r");
        if (!fp) {
-               BT_DBG("fopen() failed : %s", strerror(errno));
+               cynara_strerror(errno, err_msg, sizeof(err_msg));
+               BT_ERR("fopen() failed : %s", err_msg);
                return;
        }
 
@@ -569,6 +572,7 @@ static gboolean __server_data_received_cb(GIOChannel *chan, GIOCondition cond,
        gsize len = 0;
        int written;
        int fd;
+       char err_msg[256] = {0, };
 
        BT_DBG("");
 
@@ -605,7 +609,7 @@ static gboolean __server_data_received_cb(GIOChannel *chan, GIOCondition cond,
                return TRUE;
        }
 
-       BT_DBG("Received data length %d, remote_addr = %s", len, remote_addr);
+       BT_DBG("Received data length %zu, remote_addr = %s", len, remote_addr);
 
        if (!oacp_op->fp) {
                char file_path[BT_FILE_PATH_MAX_LEN] = {0, };
@@ -622,7 +626,8 @@ static gboolean __server_data_received_cb(GIOChannel *chan, GIOCondition cond,
                BT_DBG("file_path = [%s]", file_path);
                fp = fopen(file_path, "w");
                if (!fp) {
-                       BT_DBG("fopen() failed : %s", strerror(errno));
+                       cynara_strerror(errno, err_msg, sizeof(err_msg));
+                       BT_ERR("fopen() failed : %s", err_msg);
                        goto fail;
                }
                oacp_op->fp = fp;
@@ -631,7 +636,7 @@ static gboolean __server_data_received_cb(GIOChannel *chan, GIOCondition cond,
        if (oacp_op->length_sofar <= oacp_op->length) {
                written = fwrite(buffer, 1, len, oacp_op->fp);
                oacp_op->length_sofar += written;
-               BT_DBG("written [%d], length_sofar [%lu], received_buff_len [%d], size [%lu]",
+               BT_DBG("written [%d], length_sofar [%u], received_buff_len [%zu], size [%u]",
                                        written, oacp_op->length_sofar, len, oacp_op->length);
        }
 
@@ -688,7 +693,7 @@ static void _bt_otp_method(GDBusConnection *connection,
                struct stat st;
                struct object_metadata *object = NULL;
 
-               g_variant_get(parameters, "(s)", &directory);
+               g_variant_get(parameters, "(&s)", &directory);
                BT_DBG("Directory = [%s]", directory);
 
                dir = g_dir_open(directory, 0, &error);
@@ -699,9 +704,8 @@ static void _bt_otp_method(GDBusConnection *connection,
                        goto fail;
                }
 
-               while ((filename = g_dir_read_name(dir))) {
+               while ((filename = g_dir_read_name(dir)))
                        list = g_slist_append(list, (gpointer) filename);
-               }
 
                g_dir_close(dir);
 
@@ -785,7 +789,7 @@ fail:
                char address[BT_ADDRESS_STRING_SIZE] = { 0 };
                int fd;
 
-               g_variant_get(parameters, "(oh)", &dev_path, &index);
+               g_variant_get(parameters, "(&oh)", &dev_path, &index);
 
                msg = g_dbus_method_invocation_get_message(invocation);
                fd_list = g_dbus_message_get_unix_fd_list(msg);
@@ -811,6 +815,9 @@ fail:
                                _bt_otp_start_write_on_fd();
                        else if (oacp_op->opcode == OACP_WRITE)
                                _bt_otp_start_read_on_fd();
+               } else {
+                       /* Close fd if oacp_op is NULL */
+                       close(fd);
                }
                g_dbus_method_invocation_return_value(invocation, NULL);
        }
@@ -1048,9 +1055,9 @@ fail:
 static bool __bt_oacp_create_timeout_cb(gpointer user_data)
 {
        /* Delete the EMPTY object */
-       oacp_create = FALSE;
+       BT_INFO("+");
        _bt_otp_restore_old_object();
-       return TRUE;
+       return FALSE;
 }
 
 static void _bt_otp_free_oacp_op()
@@ -1073,7 +1080,7 @@ int _bt_otp_send_launch_request(char *absolute_path)
 {
        void *handle;
        char *error;
-       int ret;
+       int ret = BLUETOOTH_ERROR_NONE;
 
        /* check ARCH 64 or 32*/
        if (!access(FILEPATH_ARCH_64, 0)) {
@@ -1092,7 +1099,7 @@ int _bt_otp_send_launch_request(char *absolute_path)
        dlerror();      /* Clear any existing error */
 
        int (*fun)(char *) = (int (*)(char *))dlsym(handle,
-                                       "bt_app_control_send_launch_request");
+                       "bt_app_control_send_launch_request");
 
        if ((error = dlerror()) != NULL)  {
                BT_ERR("Can not load symbol : %s", dlerror());
@@ -1100,7 +1107,8 @@ int _bt_otp_send_launch_request(char *absolute_path)
                return BLUETOOTH_ERROR_INTERNAL;
        }
 
-       ret = fun(absolute_path);
+       if (fun)
+               ret = fun(absolute_path);
        dlclose(handle);
 
        return ret;
@@ -1180,8 +1188,6 @@ void _bt_otp_create_new_object(uint32_t size, char *uuid)
        curr_obj_index = g_slist_length(otp_object_list) - 1;
        curr_obj_id = selected_object->id;
        object_id++;
-
-       free(uuid);
 }
 
 void _bt_otp_restore_old_object()
@@ -1197,6 +1203,11 @@ void _bt_otp_restore_old_object()
 
        index = 0;
        object = _bt_otp_client_find_object(otp_object_list, prev_obj_id, &index);
+       if (!object) {
+               BT_ERR("Object is NULL");
+               return;
+       }
+       oacp_create = FALSE;
        update_obj_metadata_charc_value(object);
        selected_object = object;
        curr_obj_index = index;
@@ -1211,7 +1222,7 @@ int _bt_otp_oacp_write_cb(char *value, int len, int offset,
        int opcode = value[0];
        uint32_t object_offset, length, object_size;
        uint8_t mode = 0;
-       char *uuid;
+       char *uuid = NULL;
        char absolute_file_path[BT_FILE_PATH_MAX_LEN] = {0, };
 
        BT_INFO("OACP Opcode 0x%d", opcode);
@@ -1245,6 +1256,7 @@ int _bt_otp_oacp_write_cb(char *value, int len, int offset,
 
                oacp_create = TRUE;
                _bt_otp_create_new_object(object_size, uuid);
+               g_free(uuid);
 
                if (oacp_create_timeout_id > 0)
                        g_source_remove(oacp_create_timeout_id);
@@ -1318,7 +1330,7 @@ int _bt_otp_oacp_write_cb(char *value, int len, int offset,
                if (opcode == OACP_WRITE)
                        mode = (uint8_t)value[9] & 0xFF;
 
-               BT_INFO("Offset = %lu, Length = %lu", object_offset, length, mode);
+               BT_INFO("Offset = %u, Length = %u", object_offset, length);
 
                if (oacp_op) {
                        if (otc_connection_status) {
@@ -1585,7 +1597,7 @@ int _bt_otp_olcp_write_cb(char *value, int len, int offset,
                                (uint64_t)(value[3] & 0xFF) << 16 |
                                (uint64_t)(value[2] & 0xFF) << 8  |
                                (uint64_t)(value[1] & 0xFF);
-               BT_INFO("Object ID [%llu]", object_id);
+               BT_INFO("Object ID [%llu]", (unsigned long long int)object_id);
                if (selected_object && selected_object->id == object_id)
                        goto fail;
 
@@ -1620,6 +1632,7 @@ int _bt_otp_obj_name_write_cb(char *value, int len)
        char new_abs_filepath[BT_FILE_PATH_MAX_LEN] = {0, };
        int ret = BLUETOOTH_ERROR_NONE;
        FILE *fp = NULL;
+       char err_msg[256] = {0, };
 
        object = (struct object_metadata *) g_slist_nth_data(otp_object_list, curr_obj_index);
        if (!object)
@@ -1642,7 +1655,8 @@ int _bt_otp_obj_name_write_cb(char *value, int len)
 
                fp = fopen(new_abs_filepath, "a");
                if (!fp) {
-                       BT_DBG("fopen() failed : %s", strerror(errno));
+                       cynara_strerror(errno, err_msg, sizeof(err_msg));
+                       BT_ERR("fopen() failed : %s", err_msg);
                        ret = BLUETOOTH_ATT_ERROR_INTERNAL;
                        goto fail;
                }
@@ -1777,7 +1791,7 @@ static struct otp_char_info *otp_get_char_value(const char *path)
        return NULL;
 }
 
-int _bt_otp_read_cb(const char *obj_path, char **value, int *len)
+int _bt_otp_read_cb(const char *obj_path, char **value, int *len, uint16_t offset)
 {
        struct otp_char_info *info = NULL;
 
@@ -1787,9 +1801,8 @@ int _bt_otp_read_cb(const char *obj_path, char **value, int *len)
        }
 
        if (g_strcmp0(obj_path, otp_feature_obj_path)) {
-               if (!selected_object) {
+               if (!selected_object)
                        return BLUETOOTH_OTP_ERROR_OBJECT_NOT_SELECTED;
-               }
        }
 
        info = otp_get_char_value(obj_path);
@@ -1804,8 +1817,14 @@ int _bt_otp_read_cb(const char *obj_path, char **value, int *len)
                if (info->char_value == NULL || info->value_length == 0)
                        return BLUETOOTH_ATT_ERROR_INTERNAL;
 
-               *len = info->value_length;
+               if (offset > info->value_length)
+                       return BLUETOOTH_ATT_ERROR_INVALID_OFFSET;
+
+               *len = info->value_length - offset;
                *value = (char *)malloc(sizeof(char)*(*len));
+               if (*value == NULL)
+                       return BLUETOOTH_ATT_ERROR_INTERNAL;
+
                memcpy(*value, info->char_value, *len);
 
                return BLUETOOTH_ATT_ERROR_NONE;
@@ -1943,17 +1962,16 @@ void _bt_otp_gatt_char_property_changed_event(GVariant *msg,
 
                                        /* Send indication for CPs */
                                        if (!g_strcmp0(char_path, otp_oacp_obj_path)) {
-                                               if (OACP_indicate) {
+                                               if (OACP_indicate)
                                                        _bt_otp_send_indication(char_path, &info, &addr_hex);
-                                               }
                                        } else if (!g_strcmp0(char_path, otp_olcp_obj_path)) {
-                                               if (OLCP_indicate) {
+                                               if (OLCP_indicate)
                                                        _bt_otp_send_indication(char_path, &info, &addr_hex);
-                                               }
                                        }
                                } else {
                                        BT_ERR("Array Len 0");
                                }
+                               g_variant_unref(val);
                        } else {
                                BT_ERR("var==NULL");
                        }
@@ -1971,7 +1989,7 @@ void _bt_otp_gatt_char_property_changed_event(GVariant *msg,
                        g_variant_get(var, "(&s&s&syq)", &char_path,
                                        &svc_handle, &addr, &req_id, &offset);
 
-                       result = _bt_otp_read_cb(char_path, &value, &len);
+                       result = _bt_otp_read_cb(char_path, &value, &len, offset);
 
                        if (result != BLUETOOTH_ATT_ERROR_NONE) {
                                BT_ERR("ReadValue failed %s", char_path);
@@ -1995,11 +2013,10 @@ void _bt_otp_gatt_char_property_changed_event(GVariant *msg,
                                indicate ? "StartNotify" : "StopNotify");
                        BT_INFO("Type '%s'\n", g_variant_get_type_string(var));
 
-                       if (!g_strcmp0(char_path, otp_oacp_obj_path)) {
+                       if (!g_strcmp0(char_path, otp_oacp_obj_path))
                                OACP_indicate = indicate;
-                       } else if (!g_strcmp0(char_path, otp_olcp_obj_path)) {
+                       else if (!g_strcmp0(char_path, otp_olcp_obj_path))
                                OLCP_indicate = indicate;
-                       }
                }
        }
        return;
@@ -2043,9 +2060,8 @@ void _bt_otp_adapter_event_filter(GDBusConnection *connection,
        BT_INFO("Interface %s, Signal %s", interface_name, signal_name);
 
        if (g_strcmp0(interface_name, BT_OTP_INTERFACE_NAME) == 0) {
-               if (strcasecmp(signal_name, BLE_DISABLED) == 0) {
+               if (strcasecmp(signal_name, BLE_DISABLED) == 0)
                        _bt_otp_exit();
-               }
        }
 }
 
@@ -2074,6 +2090,52 @@ void _bt_otc_disconnected_cb(GDBusConnection *connection,
        }
 }
 
+void _bt_otp_device_property_event_filter(GDBusConnection *connection,
+                                       const gchar *sender_name,
+                                       const gchar *object_path,
+                                       const gchar *interface_name,
+                                       const gchar *signal_name,
+                                       GVariant *parameters,
+                                       gpointer user_data)
+{
+       char *interfacename = NULL;
+       GVariant *val = NULL;
+
+       g_variant_get(parameters, "(&s@a{sv}@as)", &interfacename, &val, NULL);
+
+       if (strcasecmp(interfacename, BT_DEVICE_INTERFACE) == 0) {
+               GVariantIter value_iter;
+               GVariant *val1;
+               char *property = NULL;
+
+               g_variant_iter_init(&value_iter, val);
+               while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &val1))) {
+                       if (strcasecmp(property, "GattConnected") == 0) {
+                               gboolean gatt_connected = FALSE;
+                               char *address = NULL;
+
+                               g_variant_get(val1, "b", &gatt_connected);
+
+                               address = g_malloc0(BT_ADDRESS_STRING_SIZE);
+
+                               _bt_convert_device_path_to_address(object_path, address);
+
+                               BT_DBG("gatt_connected: %d", gatt_connected);
+                               BT_DBG("address: %s", address);
+                               if (!gatt_connected) {
+                                       if (oacp_create)
+                                               _bt_otp_restore_old_object();
+
+                                       if (oacp_create_timeout_id > 0)
+                                               g_source_remove(oacp_create_timeout_id);
+                               }
+                               g_free(address);
+                       }
+               }
+       }
+       g_variant_unref(val);
+}
+
 int _bt_otp_init_event_receiver()
 {
        BT_DBG("+");
@@ -2110,6 +2172,12 @@ int _bt_otp_init_event_receiver()
                                        _bt_otc_disconnected_cb,
                                        NULL, NULL);
 
+       device_property_sub_id = g_dbus_connection_signal_subscribe(conn,
+                               BT_BLUEZ_NAME, BT_PROPERTIES_INTERFACE,
+                               PROPERTIES_CHANGED, NULL, NULL, 0,
+                               _bt_otp_device_property_event_filter,
+                               NULL, NULL);
+
        BT_DBG("-");
        return 0;
 }
@@ -2121,6 +2189,7 @@ void _bt_otp_deinit_event_receiver(void)
        g_dbus_connection_signal_unsubscribe(conn, property_sub_id);
        g_dbus_connection_signal_unsubscribe(conn, adapter_sub_id);
        g_dbus_connection_signal_unsubscribe(conn, device_sub_id);
+       g_dbus_connection_signal_unsubscribe(conn, device_property_sub_id);
        conn = NULL;
 
        BT_DBG("-");
@@ -2175,9 +2244,8 @@ int main(void)
 
        BT_DBG("g_main_loop_quit called!");
 
-       if (main_loop != NULL) {
+       if (main_loop != NULL)
                g_main_loop_unref(main_loop);
-       }
 
        return 0;
 }