[OTP] Add support for OACP Read
[platform/core/connectivity/bluetooth-frwk.git] / bt-otp / bt-otpserver.c
index 9501f3e..48fce49 100644 (file)
@@ -26,6 +26,7 @@
 #include <langinfo.h>
 #include <inttypes.h>
 #include <errno.h>
+#include <gio/gunixfdlist.h>
 
 #include "bt-otpserver.h"
 #include "bluetooth-api.h"
@@ -60,6 +61,7 @@ static GDBusConnection *g_conn;
 
 static int property_sub_id = -1;
 static int adapter_sub_id = -1;
+static int device_sub_id = -1;
 static guint g_owner_id = 0;
 
 struct otp_char_info {
@@ -87,15 +89,27 @@ struct object_metadata {
        uint32_t props;
 };
 
-static struct object_metadata *selected_object;
+struct oacp_operation {
+       char *remote_address;
+       uint32_t offset;
+       uint32_t length;
+       uint8_t opcode;
+       uint8_t mode;
+       int fd;
+};
+
+static struct object_metadata *selected_object = NULL;
 static uint64_t object_id = OBJECT_START_ID;
 static GSList *otp_object_list = NULL;
 static GSList *otp_char_list = NULL;
 static guint obj_curr_index;
 static int adv_handle = 0;
+static gboolean OACP_indicate = FALSE;
 static gboolean OLCP_indicate = FALSE;
 char *directory = NULL;
 gboolean mutiple_obj_support = false;
+static gboolean otc_connection_status = FALSE;
+struct oacp_operation *oacp_read = NULL;
 
 static const gchar otp_introspection_xml[] =
 "<node name='/'>"
@@ -107,12 +121,18 @@ static const gchar otp_introspection_xml[] =
 "              <method name='disable'>"
 "                      <arg type='i' name='status' direction='out'/>"
 "              </method>"
+"     <method name='NewConnection'>"
+"          <arg type='o' name='object' direction='in'/>"
+"          <arg type='h' name='fd' direction='in'/>"
+"     </method>"
 "      </interface>"
 "</node>";
 
 void _bt_otp_deinit_event_receiver(void);
 void _bt_otp_unregister_interface(void);
 void update_obj_metadata_charc_value(struct object_metadata *object);
+void _bt_convert_device_path_to_address(const char *device_path,
+                                               char *device_address);
 
 static void delete_all_objects(void)
 {
@@ -465,6 +485,66 @@ int _bt_otp_set_advertising_data(void)
        return 0;
 }
 
+void _bt_otp_start_write_on_fd()
+{
+       char buf[BT_L2CAP_BUFFER_LEN];
+       int written = 0;
+       int read = 0;
+       FILE *fp;
+       char file_path[BT_FILE_PATH_MAX_LEN] = {0, };
+       int length;
+
+       if (!selected_object) {
+               BT_DBG("Object not selected");
+               goto fail;
+       }
+
+       snprintf(file_path, sizeof(file_path), "%s%s",
+                                       directory, selected_object->name);
+       BT_DBG("file_path = [%s]", file_path);
+
+       fp = fopen(file_path, "r");
+       if (!fp) {
+               BT_DBG("fopen() failed : %s", strerror(errno));
+               goto fail;
+       }
+
+       BT_DBG("length [%d]", oacp_read->length);
+       length = oacp_read->length;
+       if (length > BT_L2CAP_BUFFER_LEN) {
+               int offset = oacp_read->offset;
+               int len = 0;
+               int written_len = 0;
+
+               while (length > 0) {
+                       if (length < BT_L2CAP_BUFFER_LEN)
+                               len = length;
+                       else
+                               len = BT_L2CAP_BUFFER_LEN;
+
+                       fseek(fp, offset, SEEK_SET);
+                       read = fread(buf, 1, len, fp);
+
+                       written = write(oacp_read->fd, buf, len);
+                       BT_DBG("read [%d], Written [%d], len [%d], offset [%d], length [%d], written_len [%d]",
+                                                                               read, written, len, offset, length, written_len);
+                       length -= len;
+                       offset += len;
+                       written_len += len;
+               }
+       } else {
+               read = fread(buf, 1, length, fp);
+               written = write(oacp_read->fd, buf, oacp_read->length);
+               BT_DBG("read [%d], Written [%d]", read, written);
+       }
+
+       fclose(fp);
+fail:
+       g_free(oacp_read->remote_address);
+       g_free(oacp_read);
+       oacp_read = NULL;
+}
+
 static void _bt_otp_method(GDBusConnection *connection,
                const gchar *sender,
                const gchar *object_path,
@@ -537,7 +617,7 @@ static void _bt_otp_method(GDBusConnection *connection,
                        object->curr_size = (uint32_t) st.st_size;
                        object->alloc_size = (uint32_t) st.st_size;
                        object->id = object_id;
-                       object->props = OBJECT_READ | OBJECT_WRITE;
+                       object->props = OBJECT_READ;
 
                        otp_object_list = g_slist_append(otp_object_list,
                                                                object);
@@ -558,7 +638,8 @@ static void _bt_otp_method(GDBusConnection *connection,
                if (!mutiple_obj_support) {
                        BT_INFO("Server supports single object");
                        selected_object = (struct object_metadata *) g_slist_nth_data(otp_object_list, 0);
-                       update_obj_metadata_charc_value(selected_object);
+                       if (selected_object)
+                               update_obj_metadata_charc_value(selected_object);
                }
 
                BT_DBG("advertsing");
@@ -575,6 +656,44 @@ fail:
                g_dbus_method_invocation_return_value(invocation,
                                                g_variant_new("(i)", status));
                _bt_otp_exit();
+
+       } else if (g_strcmp0(method_name, "NewConnection") == 0) {
+               int index;
+               GDBusMessage *msg;
+               GUnixFDList *fd_list;
+               char *dev_path;
+               char address[BT_ADDRESS_STRING_SIZE] = { 0 };
+               int fd;
+
+               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);
+               if (fd_list == NULL) {
+                       BT_ERR("fd_list is NULL");
+                       return;
+               }
+
+               fd = g_unix_fd_list_get(fd_list, index, NULL);
+               if (fd == -1) {
+                       BT_ERR("Invalid fd return");
+                       return;
+               }
+
+               _bt_convert_device_path_to_address(dev_path, address);
+
+               BT_INFO("OTC Connected fd: %d, address %s", fd, address);
+               if (!oacp_read) {
+                       /* OTC Connected, but no on going request */
+                       goto done;
+               }
+               oacp_read->fd = fd;
+               otc_connection_status = TRUE;
+
+               if (oacp_read->opcode == OACP_READ)
+                       _bt_otp_start_write_on_fd();
+done:
+               g_dbus_method_invocation_return_value(invocation, NULL);
        }
        BT_DBG("-");
 }
@@ -666,10 +785,222 @@ void _bt_otp_unregister_interface(void)
        return;
 }
 
+void _bt_convert_device_path_to_address(const char *device_path,
+                                               char *device_address)
+{
+       char address[BT_ADDRESS_STRING_SIZE] = { 0 };
+       char *dev_addr;
+
+       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);
+       }
+}
+
+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_otp_get_device_object_path(char *address)
+{
+       GError *err = NULL;
+       GDBusProxy *proxy = NULL;
+       GVariant *result = NULL;
+       GVariantIter *iter = NULL;
+       char *object_path = NULL;
+
+       proxy =  g_dbus_proxy_new_sync(conn,
+                       G_DBUS_PROXY_FLAGS_NONE, NULL,
+                       BT_BLUEZ_NAME,
+                       BT_MANAGER_PATH,
+                       BT_MANAGER_INTERFACE,
+                       NULL, &err);
+
+       if (!proxy) {
+               BT_ERR("Unable to create proxy: %s", err->message);
+               goto fail;
+       }
+
+       result = g_dbus_proxy_call_sync(proxy, "GetManagedObjects", NULL,
+                       G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
+       if (!result) {
+               if (err != NULL)
+                       BT_ERR("Fail to get GetManagedObjects (Error: %s)", err->message);
+               else
+                       BT_ERR("Fail to get GetManagedObjects");
+
+               goto fail;
+       }
+
+       g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
+       object_path = __bt_extract_device_path(iter, address);
+
+       g_variant_unref(result);
+       g_variant_iter_free(iter);
+
+fail:
+       if (err)
+               g_clear_error(&err);
+
+       if (proxy)
+               g_object_unref(proxy);
+
+       return object_path;
+}
+
+int _bt_otp_open_otc_and_listen(char *address)
+{
+       char *object_path;
+       GDBusProxy *device_proxy = NULL;
+       GVariant *result = NULL;
+       GError *error = NULL;
+       int ret = BLUETOOTH_ERROR_NONE;
+
+       object_path = _bt_otp_get_device_object_path(address);
+       if (object_path == NULL) {
+               ret = BLUETOOTH_ERROR_NOT_PAIRED;
+               goto fail;
+       }
+
+       device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
+                                       NULL, BT_BLUEZ_NAME, object_path,
+                                       BT_DEVICE_INTERFACE,  NULL, NULL);
+       if (device_proxy == NULL) {
+               ret = BLUETOOTH_ERROR_INTERNAL;
+               goto fail;
+       }
+
+
+       result = g_dbus_proxy_call_sync(device_proxy, "ListenOtc",
+                               NULL,
+                               G_DBUS_CALL_FLAGS_NONE,
+                               -1,
+                               NULL,
+                               &error);
+       if (result == NULL) {
+               if (error != NULL) {
+                       BT_ERR("Error occured in Proxy call [%s]\n", error->message);
+                       g_error_free(error);
+               }
+               ret = BLUETOOTH_ERROR_INTERNAL;
+       }
+fail:
+       if (object_path)
+               g_free(object_path);
+       if (result)
+               g_variant_unref(result);
+       if (device_proxy)
+               g_object_unref(device_proxy);
+       return ret;
+}
+
+int _bt_otp_oacp_write_cb(char *value, int len, int offset,
+                                                       char *remote_addr, struct indicate_info *info)
+{
+       int ret = OACP_SUCCESS;
+       int app_err = BLUETOOTH_ERROR_NONE;
+       int opcode = value[0];
+       uint32_t object_offset, length;
+
+       BT_INFO("OACP Opcode 0x%d", opcode);
+
+       if (!otp_object_list) {
+               ret = OACP_INVALID_OBJ;
+               goto fail;
+       }
+
+       switch (opcode) {
+       case OACP_CREATE:
+               ret = OACP_OPCODE_NOT_SUPPORTED;
+               break;
+       case OACP_DELETE:
+               ret = OACP_OPCODE_NOT_SUPPORTED;
+               break;
+       case OACP_CALC_CHECKSUM:
+               ret = OACP_OPCODE_NOT_SUPPORTED;
+               break;
+       case OACP_EXECUTE:
+               ret = OACP_OPCODE_NOT_SUPPORTED;
+               break;
+       case OACP_READ:
+               object_offset = (uint32_t)(value[4] & 0xFF) << 24 |
+                               (uint32_t)(value[3] & 0xFF) << 16 |
+                               (uint32_t)(value[2] & 0xFF) << 8  |
+                               (uint32_t)(value[1] & 0xFF);
+               length = (uint32_t)(value[8] & 0xFF) << 24 |
+                       (uint32_t)(value[7] & 0xFF) << 16 |
+                       (uint32_t)(value[6] & 0xFF) << 8  |
+                       (uint32_t)(value[5] & 0xFF);
+
+               BT_INFO("Offset = %lu, Length = %lu", object_offset, length);
+
+               if (oacp_read && otc_connection_status) {
+                       /* Read operation already going on. */
+                       ret = OACP_OBJECT_LOCKED;
+                       goto fail;
+               }
+               oacp_read = g_malloc0(sizeof(struct oacp_operation));
+               oacp_read->offset = object_offset;
+               oacp_read->length = length;
+               oacp_read->remote_address = g_strdup(remote_addr);
+               oacp_read->opcode = OACP_READ;
+
+               app_err = _bt_otp_open_otc_and_listen(remote_addr);
+               if (app_err != BLUETOOTH_ERROR_NONE) {
+                       ret = OACP_OPERATION_FAILED;
+                       g_free(oacp_read->remote_address);
+                       g_free(oacp_read);
+                       oacp_read = NULL;
+                       goto fail;
+               }
+               ret = OACP_SUCCESS;
+               break;
+       case OACP_WRITE:
+               ret = OACP_OPCODE_NOT_SUPPORTED;
+               break;
+       case OACP_ABORT:
+               ret = OACP_OPCODE_NOT_SUPPORTED;
+               break;
+       default:
+               ret = OACP_OPCODE_NOT_SUPPORTED;
+               break;
+       }
+fail:
+       info->resp_opcode = OACP_RESPONSE;
+       info->req_opcode = opcode;
+       info->result_code = ret;
+       info->resp_param = NULL;
+       return app_err;
+}
 
 void convert_to_hex(struct object_metadata *object, char *type, char *value)
 {
-       struct tm *tm = NULL;
+       struct tm fc_tm;
 
        BT_DBG("type : %s", type);
 
@@ -689,17 +1020,15 @@ void convert_to_hex(struct object_metadata *object, char *type, char *value)
 
        } else if (!g_strcmp0(type, "date")) {
 
-               localtime_r(&(object->first_created), tm);
+               localtime_r(&(object->first_created), &fc_tm);
 
-               if (tm) {
-                       value[1] = ((tm->tm_year+1900) >> 8) & 0xFF;
-                       value[0] = (tm->tm_year+1900) & 0xFF;
-                       value[2] = (tm->tm_mon+1) & 0xFF;
-                       value[3] = tm->tm_mday & 0xFF;
-                       value[4] = tm->tm_hour & 0xFF;
-                       value[5] = tm->tm_min & 0xFF;
-                       value[6] = tm->tm_sec & 0xFF;
-               }
+               value[1] = ((fc_tm.tm_year+1900) >> 8) & 0xFF;
+               value[0] = (fc_tm.tm_year+1900) & 0xFF;
+               value[2] = (fc_tm.tm_mon+1) & 0xFF;
+               value[3] = fc_tm.tm_mday & 0xFF;
+               value[4] = fc_tm.tm_hour & 0xFF;
+               value[5] = fc_tm.tm_min & 0xFF;
+               value[6] = fc_tm.tm_sec & 0xFF;
 
        } else if (!g_strcmp0(type, "id")) {
 
@@ -965,9 +1294,7 @@ void _bt_otp_gatt_char_property_changed_event(GVariant *msg,
 
                                if (len != 0) {
                                        if (!g_strcmp0(char_path, otp_oacp_obj_path)) {
-                                               /* TODO: Handle OACP Control
-                                                * Point requests
-                                                */
+                                               result = _bt_otp_oacp_write_cb(value, len, offset, addr, &info);
                                        } else if (!g_strcmp0(char_path, otp_olcp_obj_path)) {
                                                result = _bt_otp_olcp_write_cb(value, len, offset, &info);
                                        } else {
@@ -980,7 +1307,9 @@ void _bt_otp_gatt_char_property_changed_event(GVariant *msg,
 
                                        /* Send indication for CPs */
                                        if (!g_strcmp0(char_path, otp_oacp_obj_path)) {
-                                               /* Handle OACP Indication */
+                                               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) {
                                                        _bt_otp_send_indication(char_path, &info, &addr_hex);
@@ -1031,7 +1360,7 @@ void _bt_otp_gatt_char_property_changed_event(GVariant *msg,
                        BT_INFO("Type '%s'\n", g_variant_get_type_string(var));
 
                        if (!g_strcmp0(char_path, otp_oacp_obj_path)) {
-                               /* Handle OACP notification */
+                               OACP_indicate = indicate;
                        } else if (!g_strcmp0(char_path, otp_olcp_obj_path)) {
                                OLCP_indicate = indicate;
                        }
@@ -1084,6 +1413,34 @@ void _bt_otp_adapter_event_filter(GDBusConnection *connection,
        }
 }
 
+void _bt_otc_disconnected_cb(GDBusConnection *connection,
+                                       const gchar *sender_name,
+                                       const gchar *object_path,
+                                       const gchar *interface_name,
+                                       const gchar *signal_name,
+                                       GVariant *parameters,
+                                       gpointer user_data)
+{
+       if (signal_name == NULL) {
+               BT_ERR("Wrong Signal");
+               return;
+       }
+
+       BT_INFO("Interface %s, Signal %s", interface_name, signal_name);
+
+       if (g_strcmp0(interface_name, BT_DEVICE_INTERFACE) == 0) {
+               if (strcasecmp(signal_name, OTC_DISCONNECTED) == 0) {
+                       BT_DBG("OTC Channel Disconnected dev_path[%s]",
+                                                                                       object_path);
+                       otc_connection_status = FALSE;
+                       if (oacp_read) {
+                               g_free(oacp_read->remote_address);
+                               g_free(oacp_read);
+                       }
+               }
+       }
+}
+
 int _bt_otp_init_event_receiver()
 {
        BT_DBG("+");
@@ -1114,6 +1471,12 @@ int _bt_otp_init_event_receiver()
                                _bt_otp_adapter_event_filter,
                                NULL, NULL);
 
+       device_sub_id = g_dbus_connection_signal_subscribe(conn,
+                                       NULL, BT_DEVICE_INTERFACE,
+                                       OTC_DISCONNECTED, NULL, NULL, 0,
+                                       _bt_otc_disconnected_cb,
+                                       NULL, NULL);
+
        BT_DBG("-");
        return 0;
 }
@@ -1124,6 +1487,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);
        conn = NULL;
 
        BT_DBG("-");