HPS: Add support to use offset parameter. 47/92547/2
authorh.sandeep <h.sandeep@samsung.com>
Mon, 17 Oct 2016 10:02:37 +0000 (15:32 +0530)
committerh.sandeep <h.sandeep@samsung.com>
Mon, 21 Nov 2016 04:48:10 +0000 (10:18 +0530)
This patch adds offset feature for read/write to
HPS attributes.

Change-Id: Iaa83a2a2ec8aee324e7562ac1a5bc763a958cf7f
Signed-off-by: h.sandeep <h.sandeep@samsung.com>
bt-httpproxy/bt-httpproxy.c

index a345501..2303be2 100644 (file)
@@ -92,7 +92,7 @@ static const gchar hps_introspection_xml[] =
 "</node>";
 
 #ifdef HPS_GATT_DB
-static void _bt_hps_set_char_value(const char *obj_path, const char* value, int value_length);
+static void _bt_hps_set_char_value(const char *obj_path, const char* value, int value_length, int offset);
 
 static void _hps_convert_address_to_hex(bluetooth_device_address_t *addr_hex, const char *addr_str)
 {
@@ -152,7 +152,7 @@ static void _bt_hps_send_status_notification(unsigned short http_status,
        BT_DBG("Status %d %04x", http_status, http_status);
 
        /* Store the status value */
-       _bt_hps_set_char_value(http_status_obj_path, status, 3);
+       _bt_hps_set_char_value(http_status_obj_path, status, 3, 0);
 
        /* Send unicast notification */
        ret = bluetooth_gatt_server_set_notification(http_status_obj_path, unicast_address);
@@ -308,7 +308,7 @@ static int notify_info_cmp(gconstpointer a1, gconstpointer a2)
        return g_strcmp0(attrib1->char_path, attrib2->char_path);
 }
 
-static void _bt_hps_set_char_value(const char *obj_path, const char* value, int value_length)
+static void _bt_hps_set_char_value(const char *obj_path, const char* value, int value_length, int offset)
 {
        GSList *tmp = NULL;
        if (!value)
@@ -318,10 +318,25 @@ static void _bt_hps_set_char_value(const char *obj_path, const char* value, int
                if (tmp->data) {
                        struct hps_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);
+                               gchar *str = NULL;
+                               if (offset > 0) {
+                                       str = g_strdup(char_info->char_value);
+                                       char_info->char_value = g_try_realloc(char_info->char_value, offset + value_length);
+                               } else {
+                                       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;
+                                       if (str) {
+                                               memcpy(char_info->char_value, str, strlen(str));
+                                               g_free(str);
+                                       }
+                                       if (offset > 0) {
+                                               memcpy(&char_info->char_value[offset], value, value_length);
+                                               char_info->value_length = offset + value_length;
+                                       } else {
+                                               memcpy(char_info->char_value, value, value_length);
+                                               char_info->value_length = value_length;
+                                       }
                                        hps_char_list = g_slist_insert_sorted(hps_char_list,
                                                                        char_info, char_info_cmp);
                                }
@@ -441,7 +456,7 @@ static void delete_notify_read_status(const char *obj_path)
 }
 #endif
 
-int _bt_hps_uri_write_cb(char *uri, int len)
+int _bt_hps_uri_write_cb(char *uri, int len, int offset)
 {
        if ((len < 1) || (len > MAX_URI_LENGTH)) {
                BT_ERR("Wrong URI length %d", len);
@@ -453,12 +468,12 @@ int _bt_hps_uri_write_cb(char *uri, int len)
                g_free(g_uri);
        g_uri = g_strndup(uri, len);
 #ifdef HPS_GATT_DB
-       _bt_hps_set_char_value(http_uri_obj_path, g_uri, len);
+       _bt_hps_set_char_value(http_uri_obj_path, g_uri, len, offset);
 #endif
        return BLUETOOTH_ERROR_NONE;
 }
 
-int _bt_hps_http_header_write_cb(char *header, int len)
+int _bt_hps_http_header_write_cb(char *header, int len, int offset)
 {
        if ((len < 1) || (len > MAX_HEADER_LENGTH)) {
                BT_ERR("Wrong Header length %d", len);
@@ -472,13 +487,13 @@ int _bt_hps_http_header_write_cb(char *header, int len)
                g_free(g_header);
        g_header = g_strndup(header, len);
 #ifdef HPS_GATT_DB
-       _bt_hps_set_char_value(http_hdr_obj_path, g_header, len);
+       _bt_hps_set_char_value(http_hdr_obj_path, g_header, len, offset);
 #endif
 
        return BLUETOOTH_ERROR_NONE;
 }
 
-int _bt_hps_entity_body_write_cb(char *entity, int len)
+int _bt_hps_entity_body_write_cb(char *entity, int len, int offset)
 {
        if ((len < 1) || (len > MAX_ENTITY_LENGTH)) {
                BT_ERR("Wrong Entity length %d", len);
@@ -490,7 +505,7 @@ int _bt_hps_entity_body_write_cb(char *entity, int len)
                g_free(g_entity);
        g_entity = g_strndup(entity, len);
 #ifdef HPS_GATT_DB
-       _bt_hps_set_char_value(http_entity_obj_path, g_entity, len);
+       _bt_hps_set_char_value(http_entity_obj_path, g_entity, len, offset);
 #endif
 
        return BLUETOOTH_ERROR_NONE;
@@ -541,7 +556,7 @@ int _bt_hps_read_cb(const char *obj_path, char **value, int *len)
                                        data_status = DS_BODY_RECEIVED;
                                _bt_hps_set_notify_read_status(obj_path, offset, data_status, notify_read_info->https_status);
                                *value = g_strdup(&info->char_value[offset]);
-                               *len = info->value_length;
+                               *len = info->value_length - offset;
                        }
                } else if (notify_read_info && (notify_read_info->read_status == DS_BODY_RECEIVED ||
                                                notify_read_info->read_status == DS_HEADER_RECEIVED)) {
@@ -609,7 +624,7 @@ void _bt_hps_head_response_cb(SoupSession *session,
 
                // Write Data to Header Characteristic
 #ifdef HPS_GATT_DB
-               _bt_hps_set_char_value(http_hdr_obj_path, content, hdr_len);
+               _bt_hps_set_char_value(http_hdr_obj_path, content, hdr_len, 0);
 #else
                bluetooth_gatt_set_characteristic_value(http_hdr_obj_path, content, hdr_len);
 #endif
@@ -731,7 +746,7 @@ void _bt_hps_get_response_cb(SoupSession *session,
                }
                // Write Data to Entity Body Characteristic
 #ifdef HPS_GATT_DB
-               _bt_hps_set_char_value(http_entity_obj_path, body->data, body->length);
+               _bt_hps_set_char_value(http_entity_obj_path, body->data, body->length, 0);
 #else
                bluetooth_gatt_set_characteristic_value(http_entity_obj_path, body->data, body->length);
 #endif
@@ -782,7 +797,7 @@ void _bt_hps_get_response_cb(SoupSession *session,
                hdr_len = soup_message_headers_get_content_length(msg->response_headers);
                // Write Data to Header Characteristic
 #ifdef HPS_GATT_DB
-               _bt_hps_set_char_value(http_hdr_obj_path, content, hdr_len);
+               _bt_hps_set_char_value(http_hdr_obj_path, content, hdr_len, 0);
 #else
                bluetooth_gatt_set_characteristic_value(http_hdr_obj_path, content, hdr_len);
 #endif
@@ -826,7 +841,7 @@ int _bt_hps_control_point_write_cb(char *value, int len)
        BT_INFO("Opcode %0x", opcode);
 
 #ifdef HPS_GATT_DB
-       _bt_hps_set_char_value(http_cp_obj_path, value, len);
+       _bt_hps_set_char_value(http_cp_obj_path, value, len, 0);
 #endif
 
        switch (opcode) {
@@ -959,7 +974,7 @@ int _bt_hps_control_point_write_cb(char *value, int len)
 #endif
                        https_status = soup_message_get_https_status(hps_soup_msg, &cert, &flags);
 #ifdef HPS_GATT_DB
-                       _bt_hps_set_char_value(http_security_obj_path, (const char *)&https_status, 1);
+                       _bt_hps_set_char_value(http_security_obj_path, (const char *)&https_status, 1, 0);
 #else
                        bluetooth_gatt_set_characteristic_value(http_security_obj_path, (char *)&https_status, 1);
 #endif
@@ -987,7 +1002,7 @@ int _bt_hps_control_point_write_cb(char *value, int len)
 #endif
                        https_status = soup_message_get_https_status(hps_soup_msg, &cert, &flags);
 #ifdef HPS_GATT_DB
-                       _bt_hps_set_char_value(http_security_obj_path, (const char *)&https_status, 1);
+                       _bt_hps_set_char_value(http_security_obj_path, (const char *)&https_status, 1, 0);
 #else
                        bluetooth_gatt_set_characteristic_value(http_security_obj_path, (char *)&https_status, 1);
 #endif
@@ -1018,7 +1033,7 @@ int _bt_hps_control_point_write_cb(char *value, int len)
 #endif
                        https_status = soup_message_get_https_status(hps_soup_msg, &cert, &flags);
 #ifdef HPS_GATT_DB
-                       _bt_hps_set_char_value(http_security_obj_path, (const char *)&https_status, 1);
+                       _bt_hps_set_char_value(http_security_obj_path, (const char *)&https_status, 1, 0);
 #else
                        bluetooth_gatt_set_characteristic_value(http_security_obj_path, (char *)&https_status, 1);
 #endif
@@ -1050,7 +1065,7 @@ int _bt_hps_control_point_write_cb(char *value, int len)
 #endif
                        https_status = soup_message_get_https_status(hps_soup_msg, &cert, &flags);
 #ifdef HPS_GATT_DB
-                       _bt_hps_set_char_value(http_security_obj_path, (const char *)&https_status, 1);
+                       _bt_hps_set_char_value(http_security_obj_path, (const char *)&https_status, 1, 0);
 #else
                        bluetooth_gatt_set_characteristic_value(http_security_obj_path, (char *)&https_status, 1);
 #endif
@@ -1079,7 +1094,7 @@ int _bt_hps_control_point_write_cb(char *value, int len)
 
                        https_status = soup_message_get_https_status(hps_soup_msg, &cert, &flags);
 #ifdef HPS_GATT_DB
-                       _bt_hps_set_char_value(http_security_obj_path, (const char *)&https_status, 1);
+                       _bt_hps_set_char_value(http_security_obj_path, (const char *)&https_status, 1, 0);
 #else
                        bluetooth_gatt_set_characteristic_value(http_security_obj_path, (char *)&https_status, 1);
 #endif
@@ -1154,20 +1169,20 @@ void _bt_hps_gatt_char_property_changed_event(GVariant *msg,
 
                                len = g_variant_get_size(val);
 
-                               BT_DBG("Len = %d", len);
+                               BT_DBG("Len = %d, Offset = %d", len, offset);
 
                                value = (char *) g_variant_get_data(val);
 
                                if (len != 0) {
                                        if (!g_strcmp0(char_path, http_uri_obj_path)) {
                                                /* Retrive URI */
-                                               result = _bt_hps_uri_write_cb(value, len);
+                                               result = _bt_hps_uri_write_cb(value, len, offset);;
                                        } else if (!g_strcmp0(char_path, http_hdr_obj_path)) {
                                                /* Retrive HEADER */
-                                               result = _bt_hps_http_header_write_cb(value, len);
+                                               result = _bt_hps_http_header_write_cb(value, len, offset);
                                        } else if (!g_strcmp0(char_path, http_entity_obj_path)) {
                                                /* Retrive ENTITY BODY */
-                                               result = _bt_hps_entity_body_write_cb(value, len);
+                                               result = _bt_hps_entity_body_write_cb(value, len, offset);
                                        } else if (!g_strcmp0(char_path, http_cp_obj_path)) {
                                                result = _bt_hps_control_point_write_cb(value, len, addr);
                                        } else {
@@ -1208,7 +1223,7 @@ void _bt_hps_gatt_char_property_changed_event(GVariant *msg,
                                } else {
                                        if (data_status == DS_BODY_RECEIVED ||
                                                data_status == DS_HEADER_RECEIVED) {
-                                               _bt_hps_set_char_value(char_path, NULL, 0);
+                                               _bt_hps_set_char_value(char_path, NULL, 0, 0);
                                        }
                                }
                                if (value)
@@ -1485,8 +1500,8 @@ int _bt_hps_prepare_httpproxy(void)
        /* Store requets information */
        char_info = g_new0(struct hps_char_info, 1);
        char_info->char_path = g_strdup(http_uri_obj_path);
-       _bt_hps_set_char_value(http_uri_obj_path, value, MAX_URI_LENGTH);
        hps_char_list = g_slist_append(hps_char_list, char_info);
+       _bt_hps_set_char_value(http_uri_obj_path, value, MAX_URI_LENGTH, 0);
 #endif
 
        /* Characteristic HTTP Headers */
@@ -1508,8 +1523,8 @@ int _bt_hps_prepare_httpproxy(void)
        /* Store Characterisitc information */
        char_info = g_new0(struct hps_char_info, 1);
        char_info->char_path = g_strdup(http_hdr_obj_path);
-       _bt_hps_set_char_value(http_hdr_obj_path, value, MAX_HEADER_LENGTH);
        hps_char_list = g_slist_append(hps_char_list, char_info);
+       _bt_hps_set_char_value(http_hdr_obj_path, value, MAX_HEADER_LENGTH, 0);
 #endif
 
        /* Characteristic HTTP Entity Body */
@@ -1531,8 +1546,8 @@ int _bt_hps_prepare_httpproxy(void)
        /* Store Characterisitc information */
        char_info = g_new0(struct hps_char_info, 1);
        char_info->char_path = g_strdup(http_entity_obj_path);
-       _bt_hps_set_char_value(http_entity_obj_path, value, MAX_ENTITY_LENGTH);
        hps_char_list = g_slist_append(hps_char_list, char_info);
+       _bt_hps_set_char_value(http_entity_obj_path, value, MAX_ENTITY_LENGTH, 0);
 #endif
 
        /* Characteristic HTTP Control Point */
@@ -1553,8 +1568,8 @@ int _bt_hps_prepare_httpproxy(void)
        /* Store Characterisitc information */
        char_info = g_new0(struct hps_char_info, 1);
        char_info->char_path = g_strdup(http_cp_obj_path);
-       _bt_hps_set_char_value(http_cp_obj_path, &cp, 1);
        hps_char_list = g_slist_append(hps_char_list, char_info);
+       _bt_hps_set_char_value(http_cp_obj_path, &cp, 1, 0);
 #endif
 
        /* Characteristic HTTP Status Code */
@@ -1584,8 +1599,8 @@ int _bt_hps_prepare_httpproxy(void)
        /* Store Characterisitc information */
        char_info = g_new0(struct hps_char_info, 1);
        char_info->char_path = g_strdup(http_status_obj_path);
-       _bt_hps_set_char_value(http_status_obj_path, status, 3);
        hps_char_list = g_slist_append(hps_char_list, char_info);
+       _bt_hps_set_char_value(http_status_obj_path, status, 3, 0);
 #endif
 
        /* Characteristic HTTPS Security */
@@ -1606,8 +1621,8 @@ int _bt_hps_prepare_httpproxy(void)
        /* Store Characterisitc information */
        char_info = g_new0(struct hps_char_info, 1);
        char_info->char_path = g_strdup(http_security_obj_path);
-       _bt_hps_set_char_value(http_security_obj_path, &cp, 1);
        hps_char_list = g_slist_append(hps_char_list, char_info);
+       _bt_hps_set_char_value(http_security_obj_path, &cp, 1, 0);
 #endif
 
        ret = bluetooth_gatt_register_service(hps_obj_path);