[Bluetooth][OTP-Server] Handle object browsing 40/126240/6
authorGowtham Anandha Babu <gowtham.ab@samsung.com>
Thu, 20 Apr 2017 15:03:53 +0000 (20:33 +0530)
committerGowtham Anandha Babu <gowtham.ab@samsung.com>
Fri, 21 Apr 2017 12:45:45 +0000 (18:15 +0530)
1) Expose and advertise OTS service
2) Handle GATT & OLCP operations on server side

Change-Id: If5940cbf37d0216329210d6e0a1ffb83c7e6b904
Signed-off-by: Gowtham Anandha Babu <gowtham.ab@samsung.com>
bt-api/bt-gatt-service.c
bt-api/bt-otp.c
bt-core/bt-core-adapter.c
bt-core/bt-core-dbus-handler.c
bt-core/include/bt-core-dbus-handler.h
bt-otp/bt-otpserver.c
bt-otp/bt-otpserver.h
include/bluetooth-api.h

index 81a6ecc..51e63b5 100644 (file)
@@ -198,6 +198,13 @@ static gchar *app_path = NULL;
 #define BT_HPS_PROPERTIES_INTERFACE "org.freedesktop.DBus.Properties"
 #endif
 
+#ifdef TIZEN_FEATURE_BT_OTP
+#define BT_OTP_OBJECT_PATH             "/org/projectx/otp"
+#define BT_OTP_INTERFACE_NAME          "org.projectx.otp_service"
+#define PROPERTIES_CHANGED             "PropertiesChanged"
+#define BT_OTP_PROPERTIES_INTERFACE    "org.freedesktop.DBus.Properties"
+#endif
+
 static GDBusProxy *manager_gproxy = NULL;
 
 static struct gatt_char_info *__bt_gatt_find_gatt_char_info(
@@ -292,6 +299,76 @@ static int __bt_send_event_to_hps(int event, GVariant *var)
 }
 #endif
 
+#ifdef TIZEN_FEATURE_BT_OTP
+static int __bt_send_event_to_otp(int event, GVariant *var)
+{
+       GError *error = NULL;
+       GVariant *parameters = NULL;
+       GDBusMessage *msg = NULL;
+
+       BT_DBG(" ");
+
+       retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
+
+       if (event == BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED) {
+               GVariantBuilder *inner_builder;
+               GVariantBuilder *invalidated_builder;
+
+               BT_DBG("BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED");
+               inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
+
+               g_variant_builder_add(inner_builder, "{sv}", "WriteValue", var);
+
+               invalidated_builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
+
+               parameters = g_variant_new("(a{sv}as)", inner_builder, invalidated_builder);
+               g_variant_builder_unref(invalidated_builder);
+               g_variant_builder_unref(inner_builder);
+       } else if (event == BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED) {
+               GVariantBuilder *inner_builder;
+               GVariantBuilder *invalidated_builder;
+
+               BT_DBG("BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED");
+               inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
+
+               g_variant_builder_add(inner_builder, "{sv}", "ReadValue", var);
+
+               invalidated_builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
+
+               parameters = g_variant_new("(a{sv}as)", inner_builder, invalidated_builder);
+               g_variant_builder_unref(invalidated_builder);
+               g_variant_builder_unref(inner_builder);
+       } else if (event == BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_STATE_CHANGED) {
+               GVariantBuilder *inner_builder;
+               GVariantBuilder *invalidated_builder;
+
+               BT_DBG("BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_STATE_CHANGED");
+               inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
+
+               g_variant_builder_add(inner_builder, "{sv}", "NotificationStateChanged", var);
+
+               invalidated_builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
+
+               parameters = g_variant_new("(a{sv}as)", inner_builder, invalidated_builder);
+               g_variant_builder_unref(invalidated_builder);
+               g_variant_builder_unref(inner_builder);
+       }
+
+       msg = g_dbus_message_new_signal(BT_OTP_OBJECT_PATH, BT_OTP_INTERFACE_NAME, PROPERTIES_CHANGED);
+       g_dbus_message_set_body(msg, parameters);
+       if (!g_dbus_connection_send_message(g_conn, msg, G_DBUS_SEND_MESSAGE_FLAGS_NONE, 0, NULL)) {
+               if (error != NULL) {
+                       BT_ERR("D-Bus API failure: errCode[%x], \
+                                       message[%s]",
+                                       error->code, error->message);
+                       g_clear_error(&error);
+               }
+               return BLUETOOTH_ERROR_INTERNAL;
+       }
+       return BLUETOOTH_ERROR_NONE;
+}
+#endif
+
 static void __bt_gatt_manager_method_call(GDBusConnection *connection,
                                        const gchar *sender,
                                        const gchar *object_path,
@@ -640,7 +717,7 @@ static void __bt_gatt_char_method_call(GDBusConnection *connection,
                bt_user_info_t *user_info = NULL;
                struct gatt_req_info *req_info = NULL;
                struct gatt_service_info *svc_info = NULL;
-#ifdef TIZEN_FEATURE_BT_HPS
+#if defined(TIZEN_FEATURE_BT_HPS) || defined(TIZEN_FEATURE_BT_OTP)
                GVariant *param = NULL;
 #endif
 
@@ -683,16 +760,20 @@ static void __bt_gatt_char_method_call(GDBusConnection *connection,
                                        BLUETOOTH_ERROR_NONE, &read_req,
                                        user_info->cb, user_info->user_data);
 
-#ifdef TIZEN_FEATURE_BT_HPS
+#if defined(TIZEN_FEATURE_BT_HPS) || defined(TIZEN_FEATURE_BT_OTP)
                param = g_variant_new("(sssyq)",
                                read_req.att_handle,
                                read_req.service_handle,
                                read_req.address,
                                read_req.req_id,
                                read_req.offset);
+#ifdef TIZEN_FEATURE_BT_HPS
                __bt_send_event_to_hps(BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED, param);
 #endif
-
+#ifdef TIZEN_FEATURE_BT_OTP
+               __bt_send_event_to_otp(BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED, param);
+#endif
+#endif
                return;
        } else if (g_strcmp0(method_name, "WriteValue") == 0) {
                GVariant *var = NULL;
@@ -705,7 +786,7 @@ static void __bt_gatt_char_method_call(GDBusConnection *connection,
                int len = 0;
                struct gatt_service_info *svc_info = NULL;
                struct gatt_req_info *req_info = NULL;
-#ifdef TIZEN_FEATURE_BT_HPS
+#if defined(TIZEN_FEATURE_BT_HPS) || defined(TIZEN_FEATURE_BT_OTP)
                GVariant *param = NULL;
 #endif
 
@@ -775,7 +856,7 @@ static void __bt_gatt_char_method_call(GDBusConnection *connection,
                        BLUETOOTH_ERROR_NONE, &value_change,
                        user_info->cb, user_info->user_data);
 
-#ifdef TIZEN_FEATURE_BT_HPS
+#if defined(TIZEN_FEATURE_BT_HPS) || defined(TIZEN_FEATURE_BT_OTP)
                if (len > 0) {
                        gchar *svc_path;
                        svc_path = g_strdup(svc_info->serv_path);
@@ -786,7 +867,12 @@ static void __bt_gatt_char_method_call(GDBusConnection *connection,
                                        req_id,
                                        offset,
                                        var);
+#ifdef TIZEN_FEATURE_BT_HPS
                        __bt_send_event_to_hps(BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED, param);
+#endif
+#ifdef TIZEN_FEATURE_BT_OTP
+                       __bt_send_event_to_otp(BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED, param);
+#endif
                        if (svc_path)
                                g_free(svc_path);
                }
@@ -798,6 +884,9 @@ static void __bt_gatt_char_method_call(GDBusConnection *connection,
        } else if (g_strcmp0(method_name, "StartNotify") == 0) {
                bt_user_info_t *user_info = NULL;
                bt_gatt_char_notify_change_t notify_change = {0, };
+#if TIZEN_FEATURE_BT_OTP
+               GVariant *param = NULL;
+#endif
                BT_DBG("StartNotify");
                user_info = _bt_get_user_data(BT_COMMON);
                if (user_info != NULL) {
@@ -811,11 +900,21 @@ static void __bt_gatt_char_method_call(GDBusConnection *connection,
                                        BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_STATE_CHANGED,
                                        BLUETOOTH_ERROR_NONE, &notify_change,
                                        user_info->cb, user_info->user_data);
+#if TIZEN_FEATURE_BT_OTP
+                               param = g_variant_new("(ssb)",
+                               notify_change.att_handle,
+                               notify_change.service_handle,
+                               notify_change.att_notify);
+                               __bt_send_event_to_otp(BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_STATE_CHANGED, param);
+#endif
                        }
                }
        } else if (g_strcmp0(method_name, "StopNotify") == 0) {
                bt_user_info_t *user_info = NULL;
                bt_gatt_char_notify_change_t notify_change = {0, };
+#if TIZEN_FEATURE_BT_OTP
+               GVariant *param = NULL;
+#endif
                BT_DBG("StopNotify");
                user_info = _bt_get_user_data(BT_COMMON);
                if (user_info != NULL) {
@@ -829,6 +928,13 @@ static void __bt_gatt_char_method_call(GDBusConnection *connection,
                                        BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_STATE_CHANGED,
                                        BLUETOOTH_ERROR_NONE, &notify_change,
                                        user_info->cb, user_info->user_data);
+#if TIZEN_FEATURE_BT_OTP
+                               param = g_variant_new("(ssb)",
+                               notify_change.att_handle,
+                               notify_change.service_handle,
+                               notify_change.att_notify);
+                               __bt_send_event_to_otp(BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_STATE_CHANGED, param);
+#endif
                        }
                }
        } else if (g_strcmp0(method_name, "IndicateConfirm") == 0) {
@@ -2495,8 +2601,28 @@ BT_EXPORT_API int bluetooth_gatt_send_response(int request_id, guint req_type,
        if (resp_state != BLUETOOTH_ERROR_NONE) {
                BT_ERR("resp_state is 0x%X", resp_state);
 
-               g_dbus_method_invocation_return_dbus_error(req_info->context,
-                               "org.bluez.Error.Failed", "Application Error");
+               switch (resp_state) {
+               case BLUETOOTH_ERROR_WRITE_REQUEST_REJECTED:
+                       g_dbus_method_invocation_return_dbus_error(req_info->context,
+                       "org.bluez.Error.Failed", "Write Request Rejected");
+                       break;
+               case BLUETOOTH_ERROR_OBJECT_NOT_SELECTED:
+                       g_dbus_method_invocation_return_dbus_error(req_info->context,
+                       "org.bluez.Error.Failed", "Object Not Selected");
+                       break;
+               case BLUETOOTH_ERROR_CONCURRENCY_LIMIT_EXCEEDED:
+                       g_dbus_method_invocation_return_dbus_error(req_info->context,
+                       "org.bluez.Error.Failed", "Concurrency Limit Exceeded");
+                       break;
+               case BLUETOOTH_ERROR_OBJECT_NAME_EXISITS:
+                       g_dbus_method_invocation_return_dbus_error(req_info->context,
+                       "org.bluez.Error.Failed", "Object Name Already Exists");
+                       break;
+               default:
+                       g_dbus_method_invocation_return_dbus_error(req_info->context,
+                       "org.bluez.Error.Failed", "Application Error");
+                       break;
+               }
 
                gatt_requests = g_slist_remove(gatt_requests, req_info);
 
index f10d0d7..85cf974 100644 (file)
@@ -42,7 +42,8 @@ BT_EXPORT_API int bluetooth_otp_server_init(const char *directory)
        else
                dir_name[0] = '\0';
 
-       g_array_append_vals(in_param1, dir_name, BLUETOOTH_MAX_OTP_SERVER_DIR_NAME);
+       g_array_append_vals(in_param1, dir_name,
+                       BLUETOOTH_MAX_OTP_SERVER_DIR_NAME);
 
        result = _bt_send_request_async(BT_BLUEZ_SERVICE, BT_OTP_SERVER_INIT,
                in_param1, in_param2, in_param3, in_param4,
index f42caa9..1ea2b8c 100644 (file)
 #define BT_CORE_IDLE_TERM_TIME 200 /* 200ms */
 #define BT_CORE_CHECK_ADAPTER_OBJECT_PATH_MAX 50
 
+#ifdef TIZEN_FEATURE_BT_OTP
+#define BT_OTP_OBJECT_PATH     "/org/projectx/otp"
+#define BT_OTP_INTERFACE_NAME  "org.projectx.otp_service"
+#define BLE_DISABLED           "LeDisabled"
+#endif
 
 static bt_status_t adapter_status = BT_DEACTIVATED;
 static bt_le_status_t adapter_le_status = BT_LE_DEACTIVATED;
@@ -341,6 +346,24 @@ int _bt_enable_adapter_le(void)
        return 0;
 }
 
+#ifdef TIZEN_FEATURE_BT_OTP
+static void _bt_core_stop_otp()
+{
+       GError *error = NULL;
+       GDBusMessage *msg = NULL;
+       GDBusConnection *conn = _bt_core_get_gdbus_connection();
+       msg = g_dbus_message_new_signal(BT_OTP_OBJECT_PATH, BT_OTP_INTERFACE_NAME, BLE_DISABLED);
+       if (!g_dbus_connection_send_message(conn, msg, G_DBUS_SEND_MESSAGE_FLAGS_NONE, 0, NULL)) {
+               if (error != NULL) {
+                       BT_ERR("Failed to send BLE_DISABLED signal to OTP : errCode[%x], \
+                                       message[%s]",
+                                       error->code, error->message);
+                       g_clear_error(&error);
+               }
+       }
+}
+#endif
+
 int _bt_disable_adapter_le(void)
 {
        BT_DBG("+");
index 41f15b0..34c34c7 100644 (file)
 #define BT_HPS_INTERFACE_NAME "org.projectx.httpproxy_service"
 #endif
 
-#ifdef TIZEN_FEATURE_BT_OTP
-#define BT_OTP_SERVICE_NAME    "org.projectx.otp"
-#define BT_OTP_OBJECT_PATH     "/org/projectx/otp"
-#define BT_OTP_INTERFACE_NAME  "org.projectx.otp_service"
-#endif
-
 static GDBusConnection *service_gconn;
 static GDBusProxy *service_gproxy;
 #ifdef TIZEN_FEATURE_BT_HPS
 static GDBusProxy *hps_gproxy;
 #endif
 
-#ifdef TIZEN_FEATURE_BT_OTP
-static GDBusProxy *otp_gproxy;
-#endif
-
 void _bt_core_fill_garray_from_variant(GVariant *var, GArray *param)
 {
        char *data;
@@ -208,69 +198,6 @@ GDBusProxy *_bt_core_gdbus_get_hps_proxy(void)
 }
 #endif
 
-#ifdef TIZEN_FEATURE_BT_OTP
-static GDBusProxy *_bt_core_gdbus_init_otp_proxy(void)
-{
-       GDBusProxy *proxy;
-       GError *err = NULL;
-       GDBusConnection *conn;
-
-       BT_DBG(" ");
-
-       conn = _bt_core_get_gdbus_connection();
-       if (!conn)
-               return NULL;
-
-       proxy =  g_dbus_proxy_new_sync(conn,
-                       G_DBUS_PROXY_FLAGS_NONE, NULL,
-                       BT_OTP_SERVICE_NAME,
-                       BT_OTP_OBJECT_PATH,
-                       BT_OTP_INTERFACE_NAME,
-                       NULL, &err);
-       if (proxy == NULL) {
-               if (err) {
-                        BT_ERR("Unable to create proxy: %s", err->message);
-                        g_clear_error(&err);
-               }
-               return NULL;
-       }
-
-       otp_gproxy = proxy;
-
-       return proxy;
-}
-
-GDBusProxy *_bt_core_gdbus_get_otp_proxy(void)
-{
-       return (otp_gproxy) ? otp_gproxy : _bt_core_gdbus_init_otp_proxy();
-}
-
-int _bt_core_stop_otp(void)
-{
-       GVariant *variant = NULL;
-       unsigned char enabled;
-       GError *err = NULL;
-       BT_DBG(" ");
-
-       otp_gproxy = _bt_core_gdbus_get_otp_proxy();
-       if (!otp_gproxy) {
-               BT_DBG("Couldn't get service proxy");
-               return -1;
-       }
-
-       variant = g_dbus_proxy_call_sync(otp_gproxy, "disable",
-                               NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
-       if (err) {
-               BT_ERR("Error : %s" , err->message);
-               g_clear_error(&err);
-       }
-       if (variant) {
-               g_variant_get(variant, "(y)", &enabled);
-               BT_ERR("OTP disabled status 0x%x", enabled);
-       }
-       return 0;
-}
-#endif
 void _bt_core_gdbus_deinit_proxys(void)
 {
        BT_DBG("");
@@ -287,13 +214,6 @@ void _bt_core_gdbus_deinit_proxys(void)
        }
 #endif
 
-#ifdef TIZEN_FEATURE_BT_OTP
-       if (otp_gproxy) {
-               g_object_unref(otp_gproxy);
-               otp_gproxy = NULL;
-       }
-#endif
-
        if (service_gconn) {
                g_object_unref(service_gconn);
                service_gconn = NULL;
index 25d0bed..dfaef71 100755 (executable)
@@ -53,9 +53,6 @@ GDBusProxy *_bt_core_gdbus_get_hps_proxy(void);
 int _bt_core_start_httpproxy(void);
 int _bt_core_stop_httpproxy(void);
 #endif
-#ifdef TIZEN_FEATURE_BT_OTP
-int _bt_core_stop_otp(void);
-#endif
 void _bt_core_gdbus_deinit_proxys(void);
 
 GDBusConnection * _bt_core_get_gdbus_connection(void);
index b2c0f0d..2bc0f60 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #define BT_ERR(fmt, arg...) SLOGE(fmt, ##arg)
 #define BT_DBG(fmt, arg...) SLOGD(fmt, ##arg)
 
+/* OTP object paths */
+char *otp_obj_path = NULL;
+char *otp_feature_obj_path = NULL;
+char *otp_object_name_obj_path = NULL;
+char *otp_object_type_obj_path = NULL;
+char *otp_object_size_obj_path = NULL;
+char *otp_object_first_created_obj_path = NULL;
+char *otp_object_last_modified_obj_path = NULL;
+char *otp_object_id_obj_path = NULL;
+char *otp_object_prop_obj_path = NULL;
+char *otp_oacp_obj_path = NULL;
+char *otp_olcp_obj_path = NULL;
+char *otp_oacp_desc_obj_path = NULL;
+char *otp_olcp_desc_obj_path = NULL;
+
 static GMainLoop *main_loop;
 GDBusNodeInfo *otp_node_info = NULL;
 static GDBusConnection *conn;
 static GDBusConnection *g_conn;
 
 static int property_sub_id = -1;
+static int adapter_sub_id = -1;
 static guint g_owner_id = 0;
 
+struct otp_char_info {
+       gchar *char_path;
+       gchar *char_value;
+       int value_length;
+};
+
+struct indicate_info {
+       uint8_t resp_opcode;
+       uint8_t req_opcode;
+       uint8_t result_code;
+       uint8_t *resp_param;
+};
+
+/* Object metadata */
+struct object_metadata {
+       gchar *name;
+       gchar *type;
+       uint32_t curr_size;
+       uint32_t alloc_size;
+       time_t first_created;
+       time_t last_modified;
+       uint64_t id;
+       uint32_t props;
+};
+
+static struct object_metadata *selected_object;
+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 OLCP_indicate = FALSE;
 char *directory = NULL;
 
 static const gchar otp_introspection_xml[] =
@@ -64,11 +112,51 @@ static const gchar otp_introspection_xml[] =
 void _bt_otp_deinit_event_receiver(void);
 void _bt_otp_unregister_interface(void);
 
+static void delete_all_objects(void)
+{
+       GSList *tmp = NULL;
+       for (tmp = otp_object_list; tmp != NULL; tmp = tmp->next) {
+               if (tmp->data) {
+                       struct object_metadata *obj_info = tmp->data;
+                       if (obj_info->name)
+                               g_free(obj_info->name);
+                       if (obj_info->type)
+                               g_free(obj_info->type);
+                       otp_object_list = g_slist_delete_link(otp_object_list, tmp->data);
+               }
+       }
+       g_slist_free(otp_object_list);
+       otp_object_list = NULL;
+}
+
+static void delete_all_characterisitc(void)
+{
+       GSList *tmp = NULL;
+       for (tmp = otp_char_list; tmp != NULL; tmp = tmp->next) {
+               if (tmp->data) {
+                       struct otp_char_info *char_info = tmp->data;
+                       if (char_info->char_path)
+                               g_free(char_info->char_path);
+                       if (char_info->char_value)
+                               g_free(char_info->char_value);
+                       otp_char_list = g_slist_delete_link(otp_char_list, tmp->data);
+               }
+       }
+       g_slist_free(otp_char_list);
+       otp_char_list = NULL;
+}
+
 void _bt_otp_exit(void)
 {
        int ret;
        BT_DBG("");
 
+       if (otp_char_list)
+               delete_all_characterisitc();
+
+       if (otp_object_list)
+               delete_all_objects();
+
        ret = bluetooth_gatt_deinit();
        if (ret != BLUETOOTH_ERROR_NONE)
                BT_ERR("Failed to Deinit GATT %d", ret);
@@ -77,11 +165,296 @@ void _bt_otp_exit(void)
 
        _bt_otp_unregister_interface();
 
+       /* TODO: Advertising is not getting stopped by this API.
+        * This is because OTP_SERVER_DEINIT dbus call is blocking
+        * BT_SET_ADVERTISING_DATA dbus call. But now advertisment
+        * is stopped because of terminated process logic.
+        */
+       ret = bluetooth_set_advertising(adv_handle, FALSE);
+       if (ret != BLUETOOTH_ERROR_NONE)
+               BT_ERR("Failed to stop ADV %d", ret);
+
        if (main_loop != NULL) {
                g_main_loop_quit(main_loop);
        }
 }
 
+static void _bt_otp_set_char_value(const char *obj_path,
+                               const char *value, int value_length)
+{
+       GSList *tmp = NULL;
+
+       if (!value)
+               return;
+       for (tmp = otp_char_list; tmp != NULL; tmp = tmp->next) {
+               if (tmp->data) {
+                       struct otp_char_info *char_info = tmp->data;
+                       if (!g_strcmp0(char_info->char_path, obj_path)) {
+                               char_info->char_value = g_try_realloc(char_info->char_value, value_length);
+                               if (char_info->char_value) {
+                                       memcpy(char_info->char_value, value, value_length);
+                                       char_info->value_length = value_length;
+                               }
+                               return;
+                       }
+               }
+       }
+       return;
+}
+
+int add_new_characteristic(const char *char_uuid, bt_gatt_permission_t perms,
+               bt_gatt_characteristic_property_t props, char **obj_path)
+{
+       int ret = BLUETOOTH_ERROR_NONE;
+       struct otp_char_info *char_info = NULL;
+
+       ret = bluetooth_gatt_add_new_characteristic(otp_obj_path,
+                                       char_uuid, perms, props, obj_path);
+       if (ret != BLUETOOTH_ERROR_NONE) {
+               BT_ERR("Failed to add new char %d", ret);
+               return ret;
+       }
+
+       char_info = g_new0(struct otp_char_info, 1);
+       char_info->char_path = g_strdup(*obj_path);
+       otp_char_list = g_slist_append(otp_char_list, char_info);
+
+       return ret;
+}
+
+static char *_otp_convert_uuid_to_uuid128(const char *uuid)
+{
+       int len;
+       char *uuid128;
+
+       len = strlen(uuid);
+
+       switch (len) {
+       case 4:
+               /* UUID 16bits */
+               uuid128 = g_strdup_printf("0000%s-0000-1000-8000-00805f9b34fb",
+                                                                       uuid);
+               break;
+
+       case 8:
+               /* UUID 32bits */
+               uuid128 = g_strdup_printf("%s-0000-1000-8000-00805f9b34fb",
+                                                                       uuid);
+               break;
+
+       case 36:
+               /* UUID 128bits */
+               uuid128 = strdup(uuid);
+               break;
+
+       default:
+               return NULL;
+       }
+
+       return uuid128;
+}
+
+int _bt_otp_prepare_ots(void)
+{
+       BT_DBG("+");
+       int ret = BLUETOOTH_ERROR_NONE;
+       char *service_uuid;
+       char *char_uuid;
+       char *desc_uuid;
+       bt_gatt_characteristic_property_t props;
+       bt_gatt_permission_t perms;
+       char supp_feat[OTP_FEATURE_LENGTH] = { 0x0C, 0x00, 0x00, 0x00,
+                                               0x00, 0x00, 0x00, 0x00 };
+
+       ret = bluetooth_gatt_init();
+       if (ret != BLUETOOTH_ERROR_NONE) {
+               BT_ERR("Failed to Init GATT %d", ret);
+               goto fail;
+       }
+
+       service_uuid = _otp_convert_uuid_to_uuid128(OTP_UUID);
+       ret = bluetooth_gatt_add_service(service_uuid, &otp_obj_path);
+       if (ret != BLUETOOTH_ERROR_NONE) {
+               BT_ERR("Failed to add service %d", ret);
+               goto fail;
+       }
+
+       /* Characteristic OTP Feature */
+       props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ;
+       perms = BLUETOOTH_GATT_PERMISSION_READ;
+       char_uuid = _otp_convert_uuid_to_uuid128(OTP_FEATURE_UUID);
+       ret = add_new_characteristic(char_uuid, perms, props,
+                                               &otp_feature_obj_path);
+       if (ret != BLUETOOTH_ERROR_NONE)
+               goto fail;
+
+       ret = bluetooth_gatt_set_characteristic_value(otp_feature_obj_path,
+                                               supp_feat, OTP_FEATURE_LENGTH);
+       if (ret != BLUETOOTH_ERROR_NONE) {
+               BT_ERR("Failed to set char value %d", ret);
+               return ret;
+       }
+
+       _bt_otp_set_char_value(otp_feature_obj_path, supp_feat,
+                                               OTP_FEATURE_LENGTH);
+
+       /* Characteristic Object Name */
+       props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ;
+       perms = BLUETOOTH_GATT_PERMISSION_READ;
+       char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_NAME_UUID);
+       ret = add_new_characteristic(char_uuid, perms, props,
+                                       &otp_object_name_obj_path);
+       if (ret != BLUETOOTH_ERROR_NONE)
+               goto fail;
+
+       /* Characteristic Object Type */
+       props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ;
+       perms = BLUETOOTH_GATT_PERMISSION_READ;
+       char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_TYPE_UUID);
+       ret = add_new_characteristic(char_uuid, perms, props,
+                                       &otp_object_type_obj_path);
+       if (ret != BLUETOOTH_ERROR_NONE)
+               goto fail;
+
+       /* Characteristic Object Size */
+       props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ;
+       perms = BLUETOOTH_GATT_PERMISSION_READ;
+       char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_SIZE_UUID);
+       ret = add_new_characteristic(char_uuid, perms, props,
+                                       &otp_object_size_obj_path);
+       if (ret != BLUETOOTH_ERROR_NONE)
+               goto fail;
+
+       /* Characteristic Object First-Created */
+       props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ |
+               BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE;
+       perms = BLUETOOTH_GATT_PERMISSION_READ |
+               BLUETOOTH_GATT_PERMISSION_WRITE;
+       char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_FIRST_CREATED_UUID);
+       ret = add_new_characteristic(char_uuid, perms, props,
+                                       &otp_object_first_created_obj_path);
+       if (ret != BLUETOOTH_ERROR_NONE)
+               goto fail;
+
+       /* Characteristic Object Last-Modified */
+       props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ |
+               BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE;
+       perms = BLUETOOTH_GATT_PERMISSION_READ |
+               BLUETOOTH_GATT_PERMISSION_WRITE;
+       char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_LAST_MODIFIED_UUID);
+       ret = add_new_characteristic(char_uuid, perms, props,
+                               &otp_object_last_modified_obj_path);
+       if (ret != BLUETOOTH_ERROR_NONE)
+               goto fail;
+
+       /* Characteristic Object ID */
+       props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ;
+       perms = BLUETOOTH_GATT_PERMISSION_READ;
+       char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_ID_UUID);
+       ret = add_new_characteristic(char_uuid, perms, props,
+                                       &otp_object_id_obj_path);
+       if (ret != BLUETOOTH_ERROR_NONE)
+               goto fail;
+
+       /* Characteristic Object Properties */
+       props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ;
+       perms = BLUETOOTH_GATT_PERMISSION_READ;
+       char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_PROP_UUID);
+       ret = add_new_characteristic(char_uuid, perms, props,
+                                       &otp_object_prop_obj_path);
+       if (ret != BLUETOOTH_ERROR_NONE)
+               goto fail;
+
+       /* Characteristic OACP */
+       props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE |
+               BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_INDICATE;
+       perms = BLUETOOTH_GATT_PERMISSION_WRITE;
+       char_uuid = _otp_convert_uuid_to_uuid128(OTP_OACP_UUID);
+       ret = add_new_characteristic(char_uuid, perms, props,
+                                               &otp_oacp_obj_path);
+       if (ret != BLUETOOTH_ERROR_NONE)
+               goto fail;
+
+       /* CCCD for OACP */
+       desc_uuid = _otp_convert_uuid_to_uuid128(OTP_CP_CCC_DESC_UUID);
+       perms = BLUETOOTH_GATT_PERMISSION_READ |
+               BLUETOOTH_GATT_PERMISSION_WRITE;
+       ret = bluetooth_gatt_add_descriptor(otp_oacp_obj_path, desc_uuid,
+                                               perms, &otp_oacp_desc_obj_path);
+       if (ret != BLUETOOTH_ERROR_NONE) {
+               BT_ERR("Failed to add new char descriptor %d", ret);
+               goto fail;
+       }
+
+       /* Characteristic OLCP */
+       props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE |
+               BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_INDICATE;
+       perms = BLUETOOTH_GATT_PERMISSION_WRITE;
+       char_uuid = _otp_convert_uuid_to_uuid128(OTP_OLCP_UUID);
+       ret = add_new_characteristic(char_uuid, perms, props,
+                                               &otp_olcp_obj_path);
+       if (ret != BLUETOOTH_ERROR_NONE)
+               goto fail;
+
+       /* CCCD for OLCP */
+       desc_uuid = _otp_convert_uuid_to_uuid128(OTP_CP_CCC_DESC_UUID);
+       perms = BLUETOOTH_GATT_PERMISSION_READ |
+               BLUETOOTH_GATT_PERMISSION_WRITE;
+       ret = bluetooth_gatt_add_descriptor(otp_olcp_obj_path, desc_uuid,
+                                               perms, &otp_olcp_desc_obj_path);
+       if (ret != BLUETOOTH_ERROR_NONE) {
+               BT_ERR("Failed to add new char descriptor %d", ret);
+               goto fail;
+       }
+
+       /* Register service */
+       ret = bluetooth_gatt_register_service(otp_obj_path);
+       if (ret != BLUETOOTH_ERROR_NONE) {
+               BT_ERR("Failed to register service %d", ret);
+               goto fail;
+       }
+
+       /* Register Application */
+       ret = bluetooth_gatt_register_application();
+       if (ret != BLUETOOTH_ERROR_NONE) {
+               BT_ERR("Failed to register application %d", ret);
+               goto fail;
+       }
+
+       BT_DBG("-");
+       return ret;
+
+fail:
+       delete_all_characterisitc();
+       return ret;
+}
+
+int _bt_otp_set_advertising_data(void)
+{
+       int ret;
+       BT_DBG("");
+
+       /* OTP UUID */
+       guint8 data[4]  = {0x03, 0x02, 0x25, 0x18};
+       bluetooth_advertising_data_t adv;
+
+       BT_DBG("%x %x %x %x", data[0], data[1], data[2], data[3]);
+       memcpy(adv.data, data, sizeof(data));
+       ret = bluetooth_set_advertising_data(adv_handle, &adv, sizeof(data));
+       if (ret != BLUETOOTH_ERROR_NONE) {
+               BT_ERR("Failed to set ADV data %d", ret);
+               return ret;
+       }
+
+       ret = bluetooth_set_advertising(adv_handle, TRUE);
+       if (ret != BLUETOOTH_ERROR_NONE) {
+               BT_ERR("Failed to set ADV %d", ret);
+               return ret;
+       }
+
+       return 0;
+}
+
 static void _bt_otp_method(GDBusConnection *connection,
                const gchar *sender,
                const gchar *object_path,
@@ -101,7 +474,10 @@ static void _bt_otp_method(GDBusConnection *connection,
                GDir *dir = NULL;
                GError *error = NULL;
                const gchar *filename = NULL;
-               GSList *list = NULL;
+               char absolute_path[ABSOLUTE_PATH_MAX_LENGTH];
+               GSList *list = NULL, *l = NULL;
+               struct stat st;
+               struct object_metadata *object = NULL;
 
                g_variant_get(parameters, "(s)", &directory);
                BT_DBG("Directory = [%s]", directory);
@@ -118,16 +494,65 @@ static void _bt_otp_method(GDBusConnection *connection,
                        list = g_slist_append(list, (gpointer) filename);
                }
 
-               /* TODO: Extract metadata from these objects and cache it in internal structure */
+               if (!list) {
+                       BT_DBG("No object found in given directory");
+                       status = BLUETOOTH_ERROR_NO_OBJECTS_FOUND;
+                       goto fail;
+               }
+
+               for (l = list; l != NULL; l = l->next) {
+                       if (!l->data) continue;
+                       sprintf(absolute_path, "%s%s", directory,
+                                                       (char *)l->data);
+
+                       BT_INFO("filename: %s, absoulte_path: %s",
+                                       (char *)l->data, absolute_path);
+
+                       if (stat(absolute_path, &st) == -1) {
+                               BT_INFO("stat failed: %s (%d)\n",
+                                               strerror(errno), errno);
+                               continue;
+                       }
+
+                       object = g_new0(struct object_metadata, 1);
 
-               /* TODO: Expose all OTS Characteristics via RegisterApplication */
+                       object->name = g_strdup((const gchar *)l->data);
+                       object->type = _otp_convert_uuid_to_uuid128(UNSUPPORTED_OBJECT_TYPE_UUID);
+                       object->first_created = st.st_ctime;
+                       object->last_modified = st.st_ctime;
+                       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;
 
-               /* TODO: Advertise with OTS_UUID */
+                       otp_object_list = g_slist_append(otp_object_list,
+                                                               object);
+
+                       object_id++;
+               }
+
+               g_dir_close(dir);
+
+               BT_DBG("preparing");
+               if (_bt_otp_prepare_ots() != BLUETOOTH_ERROR_NONE) {
+                       BT_ERR("Fail to prepare OTP Proxy");
+                       status = BLUETOOTH_ERROR_INTERNAL;
+                       goto fail;
+               }
+
+               BT_DBG("advertsing");
+               if (_bt_otp_set_advertising_data() != BLUETOOTH_ERROR_NONE) {
+                       BT_ERR("Fail to set advertising data");
+                       status = BLUETOOTH_ERROR_INTERNAL;
+                       goto fail;
+               }
 fail:
-               g_dbus_method_invocation_return_value(invocation, g_variant_new("(i)", status));
+               g_dbus_method_invocation_return_value(invocation,
+                                               g_variant_new("(i)", status));
 
        } else if (g_strcmp0(method_name, "disable") == 0) {
-               g_dbus_method_invocation_return_value(invocation, g_variant_new("(i)", status));
+               g_dbus_method_invocation_return_value(invocation,
+                                               g_variant_new("(i)", status));
                _bt_otp_exit();
        }
        BT_DBG("-");
@@ -139,7 +564,8 @@ static const GDBusInterfaceVTable otp_method_table = {
        NULL,
 };
 
-static void _bt_otp_on_bus_acquired(GDBusConnection *connection, const gchar *name, gpointer user_data)
+static void _bt_otp_on_bus_acquired(GDBusConnection *connection,
+                               const gchar *name, gpointer user_data)
 {
        guint object_id;
        GError *error = NULL;
@@ -148,7 +574,8 @@ static void _bt_otp_on_bus_acquired(GDBusConnection *connection, const gchar *na
 
        g_conn = connection;
 
-       object_id = g_dbus_connection_register_object(connection, BT_OTP_OBJECT_PATH,
+       object_id = g_dbus_connection_register_object(connection,
+                                               BT_OTP_OBJECT_PATH,
                                                otp_node_info->interfaces[0],
                                                &otp_method_table,
                                                NULL, NULL, &error);
@@ -194,7 +621,9 @@ int _bt_otp_register_interface(void)
        owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
                                BT_OTP_SERVICE_NAME,
                                G_BUS_NAME_OWNER_FLAGS_NONE,
-                               _bt_otp_on_bus_acquired, _bt_otp_on_name_acquired, _bt_otp_on_name_lost,
+                               _bt_otp_on_bus_acquired,
+                               _bt_otp_on_name_acquired,
+                               _bt_otp_on_name_lost,
                                NULL, NULL);
        g_owner_id = owner_id;
        BT_DBG("owner_id is [%d]\n", owner_id);
@@ -216,6 +645,375 @@ void _bt_otp_unregister_interface(void)
        return;
 }
 
+
+void convert_to_hex(struct object_metadata *object, char *type, char *value)
+{
+       struct tm *tm;
+
+       BT_DBG("type : %s", type);
+
+       memset(value, 0, 8);
+
+       if (!g_strcmp0(type, "size")) {
+
+               value[3] = (object->curr_size >> 24) & 0xFF;
+               value[2] = (object->curr_size >> 16) & 0xFF;
+               value[1] = (object->curr_size >> 8) & 0xFF;
+               value[0] = object->curr_size & 0xFF;
+
+               value[7] = (object->alloc_size >> 24) & 0xFF;
+               value[6] = (object->alloc_size >> 16) & 0xFF;
+               value[5] = (object->alloc_size >> 8) & 0xFF;
+               value[4] = object->alloc_size & 0xFF;
+
+       } else if (!g_strcmp0(type, "date")) {
+
+               tm = localtime(&(object->first_created));
+
+               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;
+
+       } else if (!g_strcmp0(type, "id")) {
+
+               value[5] = (object->id >> 48) & 0xFF;
+               value[4] = (object->id >> 32) & 0xFF;
+               value[3] = (object->id >> 24) & 0xFF;
+               value[2] = (object->id >> 16) & 0xFF;
+               value[1] = (object->id >> 8) & 0xFF;
+               value[0] = object->id & 0xFF;
+
+       } else if (!g_strcmp0(type, "props")) {
+               value[3] = (object->props >> 24) & 0xFF;
+               value[2] = (object->props >> 16) & 0xFF;
+               value[1] = (object->props >> 8) & 0xFF;
+               value[0] = object->props & 0xFF;
+       }
+}
+
+void update_obj_metadata_charc_value(struct object_metadata *object)
+{
+       /* Value can be of maximum eight bytes */
+       char value[8];
+
+       _bt_otp_set_char_value(otp_object_name_obj_path, object->name,
+                                                       strlen(object->name));
+       _bt_otp_set_char_value(otp_object_type_obj_path, object->type,
+                                                       strlen(object->type));
+
+       convert_to_hex(object, "size", value);
+       _bt_otp_set_char_value(otp_object_size_obj_path, value, 8);
+
+       convert_to_hex(object, "date", value);
+       _bt_otp_set_char_value(otp_object_first_created_obj_path, value, 7);
+       _bt_otp_set_char_value(otp_object_last_modified_obj_path, value, 7);
+
+       convert_to_hex(object, "id", value);
+       _bt_otp_set_char_value(otp_object_id_obj_path, value, 6);
+
+       convert_to_hex(object, "props", value);
+       _bt_otp_set_char_value(otp_object_prop_obj_path, value, 4);
+}
+
+int _bt_otp_olcp_write_cb(char *value, int len, int offset,
+                                       struct indicate_info *info)
+{
+       int ret = OLCP_SUCCESS;
+       int opcode = value[0];
+       struct object_metadata *object;
+
+       BT_INFO("OLCP Opcode 0x%d", opcode);
+
+       if (!otp_object_list) {
+               ret = OLCP_NO_OBJ;
+               goto fail;
+       }
+
+       switch (opcode) {
+       case OLCP_FIRST: {
+               object = (struct object_metadata *) g_slist_nth_data(otp_object_list, 0);
+               if (!object) {
+                       ret = OLCP_OUT_OF_BOUNDS;
+                       goto fail;
+               }
+               update_obj_metadata_charc_value(object);
+               selected_object = object;
+               obj_curr_index = 0;
+       } break;
+       case OLCP_LAST: {
+               len = g_slist_length(otp_object_list);
+               object = (struct object_metadata *) g_slist_nth_data(otp_object_list, len-1);
+               if (!object) {
+                       ret = OLCP_OUT_OF_BOUNDS;
+                       goto fail;
+               }
+               update_obj_metadata_charc_value(object);
+               selected_object = object;
+               obj_curr_index = len-1;
+       } break;
+       case OLCP_PREVIOUS: {
+               if (obj_curr_index == 0) {
+                       ret = OLCP_OUT_OF_BOUNDS;
+                       goto fail;
+               }
+               object = (struct object_metadata *) g_slist_nth_data(otp_object_list, obj_curr_index-1);
+               if (!object) {
+                       ret = OLCP_OUT_OF_BOUNDS;
+                       goto fail;
+               }
+               update_obj_metadata_charc_value(object);
+               selected_object = object;
+               obj_curr_index -= 1;
+       } break;
+       case OLCP_NEXT: {
+               object = (struct object_metadata *) g_slist_nth_data(otp_object_list, obj_curr_index+1);
+               if (!object) {
+                       ret = OLCP_OUT_OF_BOUNDS;
+                       goto fail;
+               }
+               update_obj_metadata_charc_value(object);
+               selected_object = object;
+               obj_curr_index += 1;
+       } break;
+       case OLCP_GOTO:
+       case OLCP_ORDER:
+       case OLCP_REQ_NO_OBJ:
+       case OLCP_CLEAR_MARKING:
+       default:
+               ret = OLCP_OPCODE_NOT_SUPPORTED;
+               break;
+       }
+fail:
+       info->resp_opcode = OLCP_RESPONSE;
+       info->req_opcode = opcode;
+       info->result_code = ret;
+       info->resp_param = NULL;
+       return BLUETOOTH_ERROR_NONE;
+}
+
+static struct otp_char_info *otp_get_char_value(const char *path)
+{
+       GSList *tmp = NULL;
+
+       for (tmp = otp_char_list; tmp != NULL; tmp = tmp->next) {
+               if (tmp->data) {
+                       struct otp_char_info *char_info = tmp->data;
+                       if (!g_strcmp0(char_info->char_path, path))
+                               return char_info;
+               }
+       }
+
+       return NULL;
+}
+
+int _bt_otp_read_cb(const char *obj_path, char **value, int *len)
+{
+       struct otp_char_info *info = NULL;
+
+       if (!obj_path) {
+               BT_ERR("Wrong Obj path");
+               return BLUETOOTH_ERROR_INTERNAL;
+       }
+
+       if (g_strcmp0(obj_path, otp_feature_obj_path)) {
+               if (!selected_object) {
+                       return BLUETOOTH_ERROR_OBJECT_NOT_SELECTED;
+               }
+       }
+
+       info = otp_get_char_value(obj_path);
+       if (info) {
+               if (info->char_value == NULL || info->value_length == 0)
+                       return BLUETOOTH_ERROR_INTERNAL;
+
+               *len = info->value_length;
+               *value = (char *)malloc(sizeof(char *)*(*len));
+               memcpy(*value, info->char_value, *len);
+
+               return BLUETOOTH_ERROR_NONE;
+       } else {
+               return BLUETOOTH_ERROR_INTERNAL;
+       }
+}
+
+static void _otp_convert_address_to_hex(bluetooth_device_address_t *addr_hex,
+                                                       const char *addr_str)
+{
+       int i = 0;
+       unsigned int addr[BLUETOOTH_ADDRESS_LENGTH] = { 0, };
+
+       if (addr_str == NULL || addr_str[0] == '\0')
+               return;
+
+       i = sscanf(addr_str, "%X:%X:%X:%X:%X:%X", &addr[0], &addr[1],
+                               &addr[2], &addr[3], &addr[4], &addr[5]);
+       if (i != BLUETOOTH_ADDRESS_LENGTH)
+               BT_ERR("Invalid format string - [%s]", addr_str);
+
+       for (i = 0; i < BLUETOOTH_ADDRESS_LENGTH; i++)
+               addr_hex->addr[i] = (unsigned char)addr[i];
+}
+
+static void _bt_otp_send_indication(const char *obj_path,
+                               struct indicate_info *info,
+                               bluetooth_device_address_t *remote_address)
+{
+       int ret = BLUETOOTH_ERROR_NONE;
+       char value[7] = {0x00};
+
+       BT_DBG("");
+
+       value[0] = info->resp_opcode & 0xFF;
+       value[1] = info->req_opcode & 0xFF;
+       value[2] = info->result_code & 0xFF;
+       if (info->resp_param) {
+               value[6] = (info->resp_param[3] >> 24) & 0xFF;
+               value[5] = (info->resp_param[4] >> 16) & 0xFF;
+               value[4] = (info->resp_param[5] >> 8) & 0xFF;
+               value[3] = info->resp_param[6] & 0xFF;
+       }
+
+       BT_DBG("Opcode: %d", value[1]);
+
+       /* Store the status value */
+       _bt_otp_set_char_value(obj_path, value, 7);
+
+       /* Send indication */
+       ret = bluetooth_gatt_server_set_notification(obj_path, remote_address);
+       if (ret != BLUETOOTH_ERROR_NONE) {
+               BT_ERR("_bt_otp_send_control_point_indication failed");
+               return;
+       }
+       ret = bluetooth_gatt_update_characteristic(obj_path, value, 7);
+       if (ret != BLUETOOTH_ERROR_NONE) {
+               BT_ERR("_bt_otp_send_control_point_indication failed");
+               return;
+       }
+}
+
+void _bt_otp_gatt_char_property_changed_event(GVariant *msg,
+                               const char *path)
+{
+       int result = BLUETOOTH_ERROR_NONE;
+       GVariantIter value_iter;
+       const char *property = NULL;
+       const char *char_path = NULL;
+       const char *svc_handle = NULL;
+       GVariant *var = NULL;
+       GVariant *val = NULL;
+       g_variant_iter_init(&value_iter, msg);
+
+       while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &var))) {
+
+               if (property == NULL) {
+                       BT_ERR("Property NULL");
+                       return;
+               }
+
+               if (!g_strcmp0(property, "WriteValue")) {
+                       int len = 0;
+                       BT_INFO("WriteValue");
+                       BT_INFO("Type '%s'\n", g_variant_get_type_string(var));
+
+                       if (var) {
+                               bluetooth_device_address_t addr_hex = { {0,} };
+                               gchar *addr = NULL;
+                               guint8 req_id = 1;
+                               guint16 offset = 0;
+                               char *value = NULL;
+                               struct indicate_info info;
+                               g_variant_get(var, "(&s&s&syq@ay)",
+                                               &char_path, &svc_handle,
+                                               &addr, &req_id, &offset, &val);
+
+                               len = g_variant_get_size(val);
+
+                               BT_DBG("Len = %d, BT_ADDR = %s", len, addr);
+
+                               value = (char *) g_variant_get_data(val);
+                               _otp_convert_address_to_hex(&addr_hex, addr);
+
+                               if (len != 0) {
+                                       if (!g_strcmp0(char_path, otp_oacp_obj_path)) {
+                                               /* TODO: Handle OACP Control
+                                                * Point requests
+                                                */
+                                       } else if (!g_strcmp0(char_path, otp_olcp_obj_path)) {
+                                               result = _bt_otp_olcp_write_cb(value, len, offset, &info);
+                                       } else {
+                                               BT_ERR("Wrong Object Path %s", char_path);
+                                               result = BLUETOOTH_ERROR_INTERNAL;
+                                       }
+                                       bluetooth_gatt_send_response(req_id,
+                                       BLUETOOTH_GATT_ATT_REQUEST_TYPE_WRITE,
+                                                       result, 0, NULL, 0);
+
+                                       /* Send indication for CPs */
+                                       if (!g_strcmp0(char_path, otp_oacp_obj_path)) {
+                                               /* Handle OACP Indication */
+                                       } else if (!g_strcmp0(char_path, otp_olcp_obj_path)) {
+                                               if (OLCP_indicate) {
+                                                       _bt_otp_send_indication(char_path, &info, &addr_hex);
+                                               }
+                                       }
+                               } else {
+                                       BT_ERR("Array Len 0");
+                               }
+                       } else {
+                               BT_ERR("var==NULL");
+                       }
+               } else if (!g_strcmp0(property, "ReadValue")) {
+                       gchar *addr = NULL;
+                       guint8 req_id = 1;
+                       guint16 offset = 0;
+                       char *value = NULL;
+                       int len = 0;
+                       result = BLUETOOTH_ERROR_NONE;
+
+                       BT_INFO("ReadValue");
+                       BT_INFO("Type '%s'\n", g_variant_get_type_string(var));
+
+                       g_variant_get(var, "(&s&s&syq)", &char_path,
+                                       &svc_handle, &addr, &req_id, &offset);
+
+                       result = _bt_otp_read_cb(char_path, &value, &len);
+
+                       if (result != BLUETOOTH_ERROR_NONE) {
+                               BT_ERR("ReadValue failed %s", char_path);
+                               bluetooth_gatt_send_response(req_id,
+                               BLUETOOTH_GATT_ATT_REQUEST_TYPE_READ,
+                                               result, offset, NULL, 0);
+                       } else {
+                               bluetooth_gatt_send_response(req_id,
+                               BLUETOOTH_GATT_ATT_REQUEST_TYPE_READ,
+                                               result, offset, value, len);
+                               if (value)
+                                       g_free(value);
+                       }
+               } else if (!g_strcmp0(property, "NotificationStateChanged")) {
+                       gboolean indicate = FALSE;
+
+                       g_variant_get(var, "(&s&sb)", &char_path,
+                                               &svc_handle, &indicate);
+
+                       BT_INFO("%s : [%s]", property,
+                               indicate ? "StartNotify" : "StopNotify");
+                       BT_INFO("Type '%s'\n", g_variant_get_type_string(var));
+
+                       if (!g_strcmp0(char_path, otp_oacp_obj_path)) {
+                               /* Handle OACP notification */
+                       } else if (!g_strcmp0(char_path, otp_olcp_obj_path)) {
+                               OLCP_indicate = indicate;
+                       }
+               }
+       }
+       return;
+}
+
 void _bt_otp_property_event_filter(GDBusConnection *connection,
                                        const gchar *sender_name,
                                        const gchar *object_path,
@@ -234,7 +1032,29 @@ void _bt_otp_property_event_filter(GDBusConnection *connection,
        if (g_strcmp0(signal_name, PROPERTIES_CHANGED) == 0) {
 
                g_variant_get(parameters, "(@a{sv}@as)", &value, NULL);
-               /* TODO: Handle READ/WRITE request from client */
+               _bt_otp_gatt_char_property_changed_event(value, object_path);
+       }
+}
+
+void _bt_otp_adapter_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)
+{
+       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_OTP_INTERFACE_NAME) == 0) {
+               if (strcasecmp(signal_name, BLE_DISABLED) == 0) {
+                       _bt_otp_exit();
+               }
        }
 }
 
@@ -246,17 +1066,27 @@ int _bt_otp_init_event_receiver()
        if (conn == NULL) {
                conn =  g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
                if (error != NULL) {
-                       BT_ERR("ERROR: Can't get on system bus [%s]", error->message);
+                       BT_ERR("ERROR: Can't get on system bus [%s]",
+                                                       error->message);
                        g_clear_error(&error);
                }
        }
 
        property_sub_id = g_dbus_connection_signal_subscribe(conn,
-               NULL, BT_OTP_INTERFACE_NAME,
-               PROPERTIES_CHANGED, BT_OTP_OBJECT_PATH, NULL, 0,
-               _bt_otp_property_event_filter,
-               NULL, NULL);
-       BT_DBG("property_sub_id = %d", property_sub_id);
+                               NULL,
+                               BT_OTP_INTERFACE_NAME,
+                               PROPERTIES_CHANGED,
+                               BT_OTP_OBJECT_PATH, NULL, 0,
+                               _bt_otp_property_event_filter,
+                               NULL, NULL);
+
+       adapter_sub_id = g_dbus_connection_signal_subscribe(conn,
+                               NULL,
+                               BT_OTP_INTERFACE_NAME,
+                               BLE_DISABLED,
+                               BT_OTP_OBJECT_PATH, NULL, 0,
+                               _bt_otp_adapter_event_filter,
+                               NULL, NULL);
 
        BT_DBG("-");
        return 0;
@@ -267,6 +1097,7 @@ void _bt_otp_deinit_event_receiver(void)
        BT_DBG("+");
 
        g_dbus_connection_signal_unsubscribe(conn, property_sub_id);
+       g_dbus_connection_signal_unsubscribe(conn, adapter_sub_id);
        conn = NULL;
 
        BT_DBG("-");
index 046c808..498419f 100755 (executable)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #define BT_OTP_OBJECT_PATH     "/org/projectx/otp"
 #define BT_OTP_INTERFACE_NAME  "org.projectx.otp_service"
 
+#define BLE_DISABLED           "LeDisabled"
 #define PROPERTIES_CHANGED     "PropertiesChanged"
+
+#define BT_BLUEZ_NAME "org.bluez"
+
+#define BT_ADDRESS_STRING_SIZE 18
+
+/* OTP Service and Chanracteristics UUID */
+#define OTP_UUID                       "1825"
+#define OTP_FEATURE_UUID               "2abd"
+#define OTP_OBJECT_NAME_UUID           "2abe"
+#define OTP_OBJECT_TYPE_UUID           "2abf"
+#define OTP_OBJECT_SIZE_UUID           "2ac0"
+#define OTP_OBJECT_FIRST_CREATED_UUID  "2ac1"
+#define OTP_OBJECT_LAST_MODIFIED_UUID  "2ac2"
+#define OTP_OBJECT_ID_UUID             "2ac3"
+#define OTP_OBJECT_PROP_UUID           "2ac4"
+#define OTP_OACP_UUID                  "2ac5"
+#define OTP_OLCP_UUID                  "2ac6"
+#define OTP_OBJECT_LIST_FILTER_UUID    "2ac7"
+#define OTP_OBJECT_CHANGED_UUID                "2ac8"
+
+#define OTP_CP_CCC_DESC_UUID           "2902"
+
+/* OTP Error codes */
+#define WRITE_REQUEST_REJECTED         0X80
+#define OBJECT_NOT_SELECTED            0X81
+#define CONCURRENCY_LIMIT_EXCEEDED     0X82
+#define OBJECT_NAME_ALREADY_EXISTS     0X83
+
+/* OTP Object Type Custom UUIDs */
+/* In SIG Assigned numbers not available */
+#define UNSUPPORTED_OBJECT_TYPE_UUID   "000019aa-0000-1001-8000-00805f9b34fb"
+#define FIRMWARE_UUID                  "000019ab-0000-1001-8000-00805f9b34fb"
+#define ROUTE_GPX_UUID                 "000019ac-0000-1001-8000-00805f9b34fb"
+#define TRACK_GPX_UUID                 "000019ad-0000-1001-8000-00805f9b34fb"
+
+/* Object Metadata Length */
+#define OTP_FEATURE_LENGTH             8
+#define ABSOLUTE_PATH_MAX_LENGTH       200
+
+/* Object ID range defined by SIG. */
+#define OBJECT_START_ID        256
+#define OBJECT_END_ID  281474976710655
+
+/* Object Properties */
+#define OBJECT_DELETE  0x00000001
+#define OBJECT_EXECUTE 0x00000002
+#define OBJECT_READ    0x00000004
+#define OBJECT_WRITE   0x00000008
+#define OBJECT_APPEND  0x00000010
+#define OBJECT_TRUNCATE        0x00000020
+#define OBJECT_PATCH   0x00000040
+#define OBJECT_MARK    0x00000080
+
+/* OACP opcodes */
+#define OACP_CREATE            0x01
+#define OACP_DELETE            0x02
+#define OACP_CALC_CHECKSUM     0x03
+#define OACP_EXECUTE           0x04
+#define OACP_READ              0x05
+#define OACP_WRITE             0x06
+#define OACP_ABORT             0x07
+#define OACP_RESPONSE          0x60
+
+/* OACP error codes */
+#define OACP_SUCCESS                   0x01
+#define OACP_OPCODE_NOT_SUPPORTED      0x02
+#define OACP_INVALID_PARAM             0x03
+#define OACP_INSUFFICIENT_RESOURCES    0x04
+#define OACP_INVALID_OBJ               0x05
+#define OACP_CHANNEL_UNAVAILABLE       0x06
+#define OACP_UNSUPPORTED_TYPE          0x07
+#define OACP_PROCEDURE_NOT_SUPPORTED   0x08
+#define OACP_OBJECT_LOCKED             0x09
+#define OACP_OPERATION_FAILED          0x0A
+
+/* OLCP opcodes */
+#define OLCP_FIRST             0x01
+#define OLCP_LAST              0x02
+#define OLCP_PREVIOUS          0x03
+#define OLCP_NEXT              0x04
+#define OLCP_GOTO              0x05
+#define OLCP_ORDER             0x06
+#define OLCP_REQ_NO_OBJ                0x07
+#define OLCP_CLEAR_MARKING     0x08
+#define OLCP_RESPONSE          0x70
+
+/* OLCP order param values */
+#define ORDER_BY_NAME_ASCENDING                        0x01
+#define ORDER_BY_TYPE_ASCENDING                        0x02
+#define ORDER_BY_CURR_SIZE_ASCENDING           0x03
+#define ORDER_BY_FIRST_CREATED_ASCENDING       0x04
+#define ORDER_BY_LAST_MODIFIED_ASCENDING       0x05
+#define ORDER_BY_NAME_DESCENDING               0x11
+#define ORDER_BY_TYPE_DESCENDING               0x12
+#define ORDER_BY_CURR_SIZE_DESCENDING          0x13
+#define ORDER_BY_FIRST_CREATED_DESCENDING      0x14
+#define ORDER_BY_LAST_MODIFIED_DESCENDING      0x15
+
+/* OLCP error codes */
+#define OLCP_SUCCESS                   0x01
+#define OLCP_OPCODE_NOT_SUPPORTED      0x02
+#define OLCP_INVALID_PARAM             0x03
+#define OLCP_OPERATION_FAILED          0x04
+#define OLCP_OUT_OF_BOUNDS             0x05
+#define OLCP_TOO_MANY_OBJ              0x06
+#define OLCP_NO_OBJ                    0x07
+#define OLCP_OJECT_ID_NOT_FOUND                0x08
index 619b52a..c35f4dd 100644 (file)
@@ -202,7 +202,15 @@ extern "C" {
 #define BLUETOOTH_ERROR_CONNECTION_TERMINATED_DUE_TO_MIC_FAILURE 0x3d
 #define BLUETOOTH_ERROR_CONNECTION_FAILED_TO_BE_ESTABLISHED    0x3e
 
+/*
+ * Bluetooth OTP error codes
+ */
 #define BLUETOOTH_ERROR_INVALID_DIRECTORY 0x01
+#define BLUETOOTH_ERROR_NO_OBJECTS_FOUND 0x02
+#define BLUETOOTH_ERROR_WRITE_REQUEST_REJECTED 0x80
+#define BLUETOOTH_ERROR_OBJECT_NOT_SELECTED 0x81
+#define BLUETOOTH_ERROR_CONCURRENCY_LIMIT_EXCEEDED 0x82
+#define BLUETOOTH_ERROR_OBJECT_NAME_EXISITS 0x83
 
 /**
 * Device disconnect reason