+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 *method)
+{
+ char *object_path;
+ GDBusProxy *device_proxy = NULL;
+ GVariant *result = NULL;
+ GError *error = NULL;
+ int ret = BLUETOOTH_ERROR_NONE;
+
+ if (method == NULL)
+ return BLUETOOTH_ERROR_INTERNAL;
+
+ if (g_strcmp0(method, "ListenOtc") &&
+ g_strcmp0(method, "DisconnectOtc"))
+ return BLUETOOTH_ERROR_INTERNAL;
+
+ 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, method,
+ 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;
+}
+
+static bool __bt_oacp_create_timeout_cb(gpointer user_data)
+{
+ /* Delete the EMPTY object */
+ BT_INFO("+");
+ _bt_otp_restore_old_object();
+ return FALSE;
+}
+
+static void _bt_otp_free_oacp_op()
+{
+ if (timeout_id > 0) {
+ g_source_remove(timeout_id);
+ timeout_id = 0;
+ }
+
+ if (oacp_op) {
+ g_free(oacp_op->remote_address);
+ if (oacp_op->fp)
+ fclose(oacp_op->fp);
+ g_free(oacp_op);
+ oacp_op = NULL;
+ }
+}
+
+int _bt_otp_send_launch_request(char *absolute_path)
+{
+ void *handle;
+ char *error;
+ int ret = BLUETOOTH_ERROR_NONE;
+
+ /* check ARCH 64 or 32*/
+ if (!access(FILEPATH_ARCH_64, 0)) {
+ BT_INFO("plugin loading for ARCH 64");
+ handle = dlopen(HEADED_PLUGIN_FILEPATH64, RTLD_NOW);
+ } else {
+ BT_INFO("plugin loading for ARCH 32");
+ handle = dlopen(HEADED_PLUGIN_FILEPATH, RTLD_NOW);
+ }
+
+ if (!handle) {
+ BT_ERR("Can not load plugin %s", dlerror());
+ return BLUETOOTH_ERROR_INTERNAL;
+ }
+
+ dlerror(); /* Clear any existing error */
+
+ int (*fun)(char *) = (int (*)(char *))dlsym(handle,
+ "bt_app_control_send_launch_request");
+
+ if ((error = dlerror()) != NULL) {
+ BT_ERR("Can not load symbol : %s", dlerror());
+ dlclose(handle);
+ return BLUETOOTH_ERROR_INTERNAL;
+ }
+
+ if (fun)
+ ret = fun(absolute_path);
+ dlclose(handle);
+
+ return ret;
+}
+
+char *_bt_otp_uuid_convert_hex_to_string(char *value, uint32_t length)
+{
+ char *uuid = NULL;
+ unsigned int data0;
+ unsigned short data1;
+ unsigned short data2;
+ unsigned short data3;
+ unsigned int data4;
+ unsigned short data5;
+ size_t n;
+
+ uuid = (char *) g_malloc0(2 * length * sizeof(char));
+ n = 2 * length + 1;
+
+ switch (length) {
+ case 2:
+ memcpy(&data1, &value[0], 2);
+ snprintf(uuid, n, "%.4x", ntohs(data1));
+ break;
+ case 4:
+ memcpy(&data0, &value[0], 4);
+ snprintf(uuid, n, "%.8x", ntohl(data0));
+ break;
+ case 16:
+ memcpy(&data0, &value[0], 4);
+ memcpy(&data1, &value[4], 2);
+ memcpy(&data2, &value[6], 2);
+ memcpy(&data3, &value[8], 2);
+ memcpy(&data4, &value[10], 4);
+ memcpy(&data5, &value[14], 2);
+
+ snprintf(uuid, n + 4, "%.8x-%.4x-%.4x-%.4x-%.8x%.4x",
+ ntohl(data0), ntohs(data1),
+ ntohs(data2), ntohs(data3),
+ ntohl(data4), ntohs(data5));
+ break;
+ default:
+ g_free(uuid);
+ return NULL;
+ }
+
+ return uuid;
+}
+
+void _bt_otp_create_new_object(uint32_t size, char *uuid)
+{
+ struct object_metadata *object = NULL;
+
+ /* Store current object id.
+ * Incase of OACP Create fail, need to restore
+ * it back.
+ */
+ prev_obj_id = selected_object->id;
+
+ object = g_new0(struct object_metadata, 1);
+
+ object->name = NULL;
+ object->type = g_strdup(uuid);
+ object->first_created = 0;
+ object->last_modified = 0;
+ object->curr_size = 0;
+ object->alloc_size = size;
+ object->id = object_id;
+ object->props = OBJECT_READ | OBJECT_WRITE |
+ OBJECT_EXECUTE | OBJECT_DELETE;
+
+ otp_object_list = g_slist_append(otp_object_list,
+ object);
+
+ update_obj_metadata_charc_value(object);
+ selected_object = object;
+ 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()
+{
+ struct object_metadata *object = NULL;
+ guint index = 0;
+
+ object = _bt_otp_client_find_object(otp_object_list, curr_obj_id, &index);
+ if (!object)
+ return;
+
+ otp_object_list = g_slist_remove(otp_object_list, 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;
+ object_id--;
+}
+
+int _bt_otp_oacp_write_cb(char *value, int len, int offset,
+ char *remote_addr, struct indicate_info *info)
+{
+ int ret = OACP_SUCCESS;
+ int err = BLUETOOTH_ERROR_NONE;
+ int opcode = value[0];
+ uint32_t object_offset, length, object_size;
+ uint8_t mode = 0;
+ char *uuid;
+ char absolute_file_path[BT_FILE_PATH_MAX_LEN] = {0, };
+
+ BT_INFO("OACP Opcode 0x%d", opcode);
+
+ if (!selected_object) {
+ BT_DBG("Object not selected");
+ ret = OACP_INVALID_OBJ;
+ goto fail;
+ }