BLE Gatt Server Socket notify implementation.
[platform/core/connectivity/bluetooth-frwk.git] / bt-oal / bluez_hal / src / bt-hal-gatt-server.c
index 3d47474..b90ed57 100644 (file)
@@ -111,6 +111,8 @@ typedef enum {
        BT_HAL_GATT_REQUEST_TYPE_READ = 0x00,       /* Read Requested */
        BT_HAL_GATT_REQUEST_TYPE_WRITE = 0x01,      /* Write Requested */
        BT_HAL_GATT_REQUEST_TYPE_EXEC_WRITE = 0x02, /* Exec Write Requested */
+       BT_HAL_GATT_REQUEST_TYPE_ACQUIRE_WRITE = 0x03, /* Exec Write Requested */
+       BT_HAL_GATT_REQUEST_TYPE_ACQUIRE_NOTIFY = 0x04, /* Exec Write Requested */
 } bt_gatt_request_type_e;
 
 struct gatt_req_info {
@@ -239,6 +241,16 @@ static const gchar characteristics_introspection_xml[] =
 "               <arg type='b' name='response_needed' direction='in'/>"
 "               <arg type='ay' name='value' direction='in'/>"
 "        </method>"
+"       <method name ='AcquireWrite'>"
+"      <arg type='a{sv}' name='properties' direction='in'/>"
+"               <arg type='h' name='fd' direction='out'/>"
+"               <arg type='q' name='mtu' direction='out'/>"
+"        </method>"
+"       <method name = 'AcquireNotify'>"
+"               <arg type='a{sv}' name='properties' direction='in'/>"
+"               <arg type='h' name='fd' direction='out'/>"
+"               <arg type='q' name='mtu' direction='out'/>"
+"        </method>"
 "        <method name='StartNotify'>"
 "        </method>"
 "        <method name='StopNotify'>"
@@ -263,6 +275,10 @@ static const gchar characteristics_introspection_xml[] =
 "    </property>"
 "        <property type='ao' name='Descriptors' access='read'>"
 "        </property>"
+"        <property type='b' name='WriteAcquired' access='read'>"
+"        </property>"
+"        <property type='b' name='NotifyAcquired' access='read'>"
+"        </property>"
 "  </interface>"
 "</node>";
 
@@ -599,6 +615,8 @@ static void __bt_gatt_manager_method_call(GDBusConnection *connection,
                GSList *gatt_services = NULL;
                int *instance;
                instance = (int*)user_data;
+               gboolean writeAcquired = FALSE;
+               gboolean notifyAcquired = FALSE;
 
                DBG("Getting values for service, chars and descriptors");
                DBG("GATT Server App for which services are requested [%d]", *instance);
@@ -613,6 +631,7 @@ static void __bt_gatt_manager_method_call(GDBusConnection *connection,
                if (g_slist_length(gatt_services) == 0) {
                        ERR("No registered GATT services!!!!");
                        g_dbus_method_invocation_return_value(invocation, NULL);
+                       g_variant_builder_unref(builder);
                        return;
                }
 
@@ -718,6 +737,11 @@ static void __bt_gatt_manager_method_call(GDBusConnection *connection,
                                for (i = 0; i < char_info->flags_length; i++) {
                                        g_variant_builder_add(builder2, "s",
                                                        char_info->char_flags[i]);
+                                       if (strncmp(char_info->char_flags[i], "write-without-response", 16) == 0)
+                                               writeAcquired = TRUE;
+
+                                       if (strncmp(char_info->char_flags[i], "notify", 4) == 0)
+                                               notifyAcquired = TRUE;
                                }
 
                                flags_val = g_variant_new("as", builder2);
@@ -729,6 +753,14 @@ static void __bt_gatt_manager_method_call(GDBusConnection *connection,
                                                g_variant_new("b", notify));
 
                                /* Unicast */
+                               g_variant_builder_add(inner_builder, "{sv}", "WriteAcquired",
+                                                                       g_variant_new("b", writeAcquired));
+
+                               /* NotifyAcquired */
+                                                       g_variant_builder_add(inner_builder, "{sv}", "NotifyAcquired",
+                                                                       g_variant_new("b", notifyAcquired));
+
+                               /* Unicast */
                                unicast = g_strdup("00:00:00:00:00:00");
                                g_variant_builder_add(inner_builder, "{sv}", "Unicast",
                                                g_variant_new("s", unicast));
@@ -1064,6 +1096,7 @@ static void __bt_gatt_char_method_call(GDBusConnection *connection,
                req_info->attr_path = g_strdup(object_path);
                req_info->svc_path = g_strdup(svc_info->serv_path);
                req_info->request_id = req_id;
+               req_info->request_type = BT_HAL_GATT_REQUEST_TYPE_READ;
                req_info->offset = offset;
                req_info->context = invocation;
 
@@ -1135,6 +1168,7 @@ static void __bt_gatt_char_method_call(GDBusConnection *connection,
                        req_info->attr_path = g_strdup(object_path);
                        req_info->svc_path = g_strdup(svc_info->serv_path);
                        req_info->request_id = req_id;
+                       req_info->request_type = BT_HAL_GATT_REQUEST_TYPE_WRITE;
                        req_info->offset = offset;
                        req_info->context = invocation;
 
@@ -1247,8 +1281,149 @@ static void __bt_gatt_char_method_call(GDBusConnection *connection,
                _bt_hal_convert_addr_string_to_type(ev.bdaddr, addr);
 
                event_cb(HAL_EV_GATT_INDICATE_CFM, (void *)&ev, sizeof(ev));
+       } else if (g_strcmp0(method_name, "AcquireWrite") == 0) {
+
+                               uint16_t mtu = 512;
+                               int char_hdl = -1;
+                               struct hal_ev_gatt_server_acquire_write_res  ev;
+                               struct gatt_service_info *svc_info = NULL;
+                               struct gatt_req_info *req_info = NULL;
+                               struct gatt_client_info_t *conn_info = NULL;
+                               char * dev_path = NULL;
+                               char * link = 0;
+                               char  addr[BT_HAL_ADDRESS_STRING_SIZE];
+
+                               DBG("AcquireWrite");
+                               DBG("Application path = %s", object_path);
+                               DBG("Sender = %s", sender);
+
+                               GVariantIter* iter;
+                               g_variant_get(parameters, "(a{sv})", &iter);
+                               char* key = NULL;
+                               GVariant* value = NULL;
+                               while (g_variant_iter_loop(iter, "{sv}", &key, &value)) {
+                                               if (g_strcmp0(key, "MTU") == 0)
+                                                       g_variant_get(value, "i", &mtu);
+                                               else if (g_strcmp0(key, "link") == 0)
+                                                       g_variant_get(value, "s", &link);
+                                               else if (g_strcmp0(key, "device") == 0)
+                                                       g_variant_get(value, "o", &dev_path);
+                               }
+
+                               DBG("AcquireWrite   values retived");
+                               DBG(" path  %s LINK = %s, MTU = %u", addr, link, mtu);
+
+                               svc_info = __bt_gatt_find_gatt_service_from_char(object_path, &char_hdl);
+
+                               _bt_hal_convert_device_path_to_address(dev_path, addr);
+
+                               DBG("remote adress %s", addr);
+
+                               /* Check if device is already in connected list */
+                               conn_info = __bt_find_remote_gatt_client_info(addr);
+
+                               if (conn_info == NULL) {
+                                       ERR("Cleint info not found\n");
+                                       goto done;
+                               }
+
+                               if (svc_info == NULL) {
+                                       ERR("svc_info  info not found\n");
+                                       goto done;
+                               }
+
+                               /* Store requets information */
+                               req_info = g_new0(struct gatt_req_info, 1);
+                               req_info->attr_path = g_strdup(object_path);
+                               req_info->svc_path = g_strdup(svc_info->serv_path);
+                               req_info->request_id = conn_info->connection_id;
+                               req_info->request_type = BT_HAL_GATT_REQUEST_TYPE_ACQUIRE_WRITE;
+                               req_info->offset = mtu;
+                               req_info->context = invocation;
+
+                               /* Append request info in list of requests for the particular connection */
+                               conn_info->gatt_req_info_list = g_slist_append(conn_info->gatt_req_info_list, req_info);
+
+                               /* Send HAL event */
+                               memset(&ev, 0, sizeof(ev));
+                               ev.conn_id = conn_info->connection_id;
+                               ev.char_handl = char_hdl;
+                               ev.mtu = mtu;
+                               ev.trans_id = conn_info->connection_id;
+                               _bt_hal_convert_addr_string_to_type(ev.bdaddr, addr);
+                               event_cb(HAL_EV_GATT_SERVER_ACQUIRE_WRITE_RES, (void *)&ev, sizeof(ev));
+
+                               DBG("HAL_EV_GATT_ACQUIRE_WRITE_RES   called");
+
+
+                       return;
+               } else if (g_strcmp0(method_name, "AcquireNotify") == 0) {
+
+                               uint16_t mtu = 512;
+                               int char_hdl = -1;
+                               struct hal_ev_gatt_server_acquire_notify  ev;
+                               struct gatt_service_info *svc_info = NULL;
+                               struct gatt_client_info_t *conn_info = NULL;
+                               struct gatt_req_info *req_info = NULL;
+                               char * link = 0;
+                               int len = 0;
+
+                               DBG("AcquireNotify");
+                               DBG("Application path = %s", object_path);
+                               DBG("Sender = %s", sender);
+
+                               GVariantIter* iter;
+                               g_variant_get(parameters, "(a{sv})", &iter);
+                               char* key = NULL;
+                               GVariant* value = NULL;
+                               while (g_variant_iter_loop(iter, "{sv}", &key, &value)) {
+                                               if (g_strcmp0(key, "MTU") == 0)
+                                                       g_variant_get(value, "q", &mtu);
+                               }
+
+                               DBG("AcquireNotify  values retived");
+                               DBG(" MTU = %u",  mtu);
+
+                               svc_info = __bt_gatt_find_gatt_service_from_char(object_path, &char_hdl);
+
+                       if (svc_info == NULL) {
+                                       ERR("svc_info  info not found\n");
+                                       goto done;
+                               }
+
+                               /* Store requets information */
+                               req_info = g_new0(struct gatt_req_info, 1);
+                               req_info->attr_path = g_strdup(object_path);
+                               req_info->svc_path = g_strdup(svc_info->serv_path);
+                               req_info->request_id = 33;
+                               req_info->request_type = BT_HAL_GATT_REQUEST_TYPE_ACQUIRE_NOTIFY;
+                               req_info->offset = mtu;
+                               req_info->context = invocation;
+
+                               conn_info = g_new0(struct gatt_client_info_t, 1);
+                               conn_info->addr = g_strdup(object_path);
+                               INFO("Added GATT client path[%s]", conn_info->addr);
+                               conn_info->connection_id = 33;
+                               conn_info->instance_id = 33;
+                               /* Append request info in list of requests for the particular connection */
+                               conn_info->gatt_req_info_list = g_slist_append(conn_info->gatt_req_info_list, req_info);
+                               gatt_client_info_list = g_slist_append(gatt_client_info_list, conn_info);
+
+                               /* Send HAL event */
+                               memset(&ev, 0, sizeof(ev));
+                               ev.conn_id = 33;
+                               ev.char_handl = char_hdl;
+                               ev.mtu = mtu;
+                               ev.trans_id = 33;
+
+                               event_cb(HAL_EV_GATT_SERVER_ACQUIRE_NOTIFY_RES, (void *)&ev, sizeof(ev));
+
+                               DBG("HAL_EV_GATT_ACQUIRE_NOTIFY_RES   called");
+
+                               return;
        }
 
+done:
        g_dbus_method_invocation_return_value(invocation, NULL);
 }
 
@@ -1726,6 +1901,7 @@ static char* __bt_hal_convert_uuid_to_string(bt_uuid_t *srvc_id)
 
 int __bt_hal_add_service_to_dbus(char *app_path, int slot, btgatt_srvc_id_t *srvc_id)
 {
+       gboolean ret;
        /* For GATT service specific */
        GDBusNodeInfo *node_info;
        guint object_id;
@@ -1794,12 +1970,20 @@ int __bt_hal_add_service_to_dbus(char *app_path, int slot, btgatt_srvc_id_t *srv
        g_variant_builder_add(builder, "{sa{sv}}",
                        GATT_SERV_INTERFACE, inner_builder);
 
-       g_dbus_connection_emit_signal(g_conn, NULL, "/",
+       ret = g_dbus_connection_emit_signal(g_conn, NULL, "/",
                        "org.freedesktop.Dbus.ObjectManager",
                        "InterfacesAdded",
                        g_variant_new("(oa{sa{sv}})",
                                path, builder),
                        &error);
+       if (!ret) {
+               if (error) {
+                       /* dbus gives error cause */
+                       ERR("d-bus api failure: errcode[%x], message[%s]",
+                                       error->code, error->message);
+                       g_clear_error(&error);
+               }
+       }
 
        /* Send Service handle to application */
        user_data = g_malloc0(sizeof(hal_gatt_service_added));
@@ -2078,6 +2262,7 @@ static bt_status_t gatt_server_add_characteristic(int slot, int service_handle,
                bt_uuid_t *uuid, int properties,
                int permissions)
 {
+       gboolean ret;
        GError *error = NULL;
        guint object_id;
        GDBusNodeInfo *node_info;
@@ -2179,12 +2364,20 @@ static bt_status_t gatt_server_add_characteristic(int slot, int service_handle,
                        GATT_CHAR_INTERFACE,
                        inner_builder);
 
-       g_dbus_connection_emit_signal(g_conn, NULL, "/",
+       ret = g_dbus_connection_emit_signal(g_conn, NULL, "/",
                        "org.freedesktop.Dbus.ObjectManager",
                        "InterfacesAdded",
                        g_variant_new("(oa{sa{sv}})",
                                path, builder),
                        &error);
+       if (!ret) {
+               if (error) {
+                       /* dbus gives error cause */
+                       ERR("d-bus api failure: errcode[%x], message[%s]",
+                                       error->code, error->message);
+                       g_clear_error(&error);
+               }
+       }
 
        //*char_path = g_strdup(path);
 
@@ -2343,6 +2536,11 @@ static bt_status_t gatt_server_add_descriptor(int slot, int service_handle, bt_u
                        g_variant_new("(oa{sa{sv}})",
                                path, builder),
                        &error);
+       if (error != NULL) {
+               ERR("g_dbus_connection_emit_signal failed: errCode[%x], message[%s]",
+                               error->code, error->message);
+               g_clear_error(&error);
+       }
 
        //*desc_path = g_strdup(path);
 
@@ -2612,11 +2810,12 @@ static gboolean __bt_gatt_get_service_state(const char *service_path)
 {
        struct gatt_service_info *svc_info = NULL;
        GSList *l = NULL;
-
        for (l = gatt_services; l; l = g_slist_next(l)) {
 
                svc_info = (struct gatt_service_info *)l->data;
-               if (svc_info->serv_path == service_path) {
+
+
+               if (!g_strcmp0(svc_info->serv_path, service_path)) {
                        DBG("Return the state of the gatt service %d",
                                        svc_info->is_svc_registered);
                        return svc_info->is_svc_registered;
@@ -2852,6 +3051,77 @@ static bt_status_t gatt_server_send_response(int conn_id, int trans_id,
        return BT_STATUS_SUCCESS;
 }
 
+
+static bt_status_t gatt_server_send_acquire_response(int conn_id, int trans_id,
+               int status, int fd, int mtu, void *fdlist)
+{
+       CHECK_BTGATT_INIT();
+
+       struct gatt_req_info *req_info = NULL;
+       struct gatt_client_info_t *conn_info = NULL;
+       int i;
+
+       DBG("GATT Server Send Response Conn ID [%d]", conn_id);
+
+       conn_info = __bt_find_remote_gatt_client_info_from_conn(conn_id);
+       if (conn_info == NULL) {
+               ERR("No Connection Inforamtion!!!");
+               return BT_STATUS_FAIL;
+       }
+
+       req_info =  __bt_find_remote_gatt_client_request_info(conn_id, trans_id);
+       if (req_info == NULL) {
+               ERR("No Request Inforamtion!!!");
+               return BT_STATUS_FAIL;
+       }
+
+       if (status != BT_STATUS_SUCCESS) {
+               ERR("resp_state is 0x%X", status);
+
+               g_dbus_method_invocation_return_dbus_error(req_info->context,
+                               "org.bluez.Error.Failed", "Application Error");
+
+               conn_info->gatt_req_info_list = g_slist_remove(conn_info->gatt_req_info_list, req_info);
+
+               req_info->context = NULL;
+               if (req_info->attr_path)
+                       g_free(req_info->attr_path);
+               if (req_info->svc_path)
+                       g_free(req_info->svc_path);
+               g_free(req_info);
+
+               return BT_STATUS_SUCCESS;
+       }
+
+
+        if (req_info->request_type == BT_HAL_GATT_REQUEST_TYPE_ACQUIRE_WRITE) {
+
+               INFO("GATT Server Send Response BT_HAL_GATT_REQUEST_TYPE_ACQUIRE_WRITE to bluez  %d\n", fd);
+               g_dbus_method_invocation_return_value_with_unix_fd_list(
+                               req_info->context, g_variant_new("(hq)", 0, mtu), (GUnixFDList*)fdlist);
+
+       } else if (req_info->request_type == BT_HAL_GATT_REQUEST_TYPE_ACQUIRE_NOTIFY) {
+
+               INFO("GATT Server Send Response BT_HAL_GATT_REQUEST_TYPE_ACQUIRE_NOTIFY  to bluez  %d\n", fd);
+               g_dbus_method_invocation_return_value_with_unix_fd_list(
+                               req_info->context, g_variant_new("(hq)", 0, mtu), (GUnixFDList*)fdlist);
+
+       } else {
+               g_dbus_method_invocation_return_value(req_info->context, NULL);
+       }
+       conn_info->gatt_req_info_list = g_slist_remove(conn_info->gatt_req_info_list, req_info);
+
+       req_info->context = NULL;
+       if (req_info->attr_path)
+               g_free(req_info->attr_path);
+       if (req_info->svc_path)
+               g_free(req_info->svc_path);
+       g_free(req_info);
+
+       return BT_STATUS_SUCCESS;
+}
+
+
 static bt_status_t gatt_server_update_att_value(int server_if, int attribute_handle,
                int value_length, char* att_value)
 {
@@ -2905,7 +3175,6 @@ static bt_status_t gatt_server_update_att_value(int server_if, int attribute_han
 
        update_value = g_variant_new("ay", inner_builder);
 
-       outer_builder = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
        g_variant_builder_add(outer_builder, "{sv}", "Value",
                        update_value);
 
@@ -3113,5 +3382,6 @@ const btgatt_server_interface_t btgatt_server_interface = {
        gatt_server_multi_adv_update,
        gatt_server_multi_adv_set_inst_data,
        gatt_server_multi_adv_disable,
-       gatt_server_get_mtu_size
+       gatt_server_get_mtu_size,
+       gatt_server_send_acquire_response
 };