2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
23 #include "bt-httpproxy.h"
24 #include "bluetooth-api.h"
26 #include <libsoup/soup.h>
28 #ifdef TIZEN_FEATURE_BT_HPS
31 #define LOG_TAG "BLUETOOTH_HPS"
33 #define BT_INFO(fmt, arg...) SLOGI(fmt, ##arg)
34 #define BT_ERR(fmt, arg...) SLOGE(fmt, ##arg)
35 #define BT_DBG(fmt, arg...) SLOGD(fmt, ##arg)
37 char *hps_obj_path = NULL;
38 char *http_uri_obj_path = NULL;
39 char *http_hdr_obj_path = NULL;
40 char *http_entity_obj_path = NULL;
41 char *http_cp_obj_path = NULL;
42 char *http_status_obj_path = NULL;
43 char *http_status_desc_obj_path = NULL;
44 char *http_security_obj_path = NULL;
46 static GMainLoop *main_loop;
47 static guint property_sub_id;
48 static guint adapter_sub_id;
49 static http_request_state req_state;
52 struct hps_notify_read_info {
59 struct hps_char_info {
65 static GSList *hps_notify_read_list = NULL;
66 static GSList *hps_char_list = NULL;
69 static GDBusConnection *conn;
70 static GDBusConnection *g_conn;
71 static guint g_owner_id = 0;
74 char *g_header = NULL;
75 char *g_entity = NULL;
77 static SoupSession *hps_soup_session = NULL;
78 static SoupMessage *hps_soup_msg = NULL;
81 static const gchar hps_introspection_xml[] =
83 " <interface name='org.projectx.httpproxy_service'>"
84 " <method name='enable'>"
85 " <arg type='y' name='status' direction='out'/>"
87 " <method name='disable'>"
88 " <arg type='y' name='status' direction='out'/>"
94 static void _bt_hps_set_char_value(const char *obj_path, const char* value, int value_length, int offset);
96 static void _hps_convert_address_to_hex(bluetooth_device_address_t *addr_hex, const char *addr_str)
98 char *ptr1, *ptr2, *ptr3, *ptr4, *ptr5;
100 if (addr_str == NULL || addr_str[0] == '\0')
103 addr_hex->addr[0] = strtol(addr_str, &ptr5, 16);
104 addr_hex->addr[1] = strtol(ptr5 + 1, &ptr4, 16);
105 addr_hex->addr[2] = strtol(ptr4 + 1, &ptr3, 16);
106 addr_hex->addr[3] = strtol(ptr3 + 1, &ptr2, 16);
107 addr_hex->addr[4] = strtol(ptr2 + 1, &ptr1, 16);
108 addr_hex->addr[5] = strtol(ptr1 + 1, NULL, 16);
111 static char *__hps_convert_uuid_to_uuid128(const char *uuid)
121 uuid128 = g_strdup_printf("0000%s-0000-1000-8000-00805F9B34FB", uuid);
126 uuid128 = g_strdup_printf("%s-0000-1000-8000-00805F9B34FB", uuid);
131 uuid128 = strdup(uuid);
141 static void _bt_hps_send_status_notification(unsigned short http_status,
142 unsigned char data_status,
143 bluetooth_device_address_t *unicast_address)
145 char status[3] = {0x00};
146 int ret = BLUETOOTH_ERROR_NONE;
150 status[0] = http_status & 0xFF;
151 status[1] = (http_status >> 8) & 0xFF;
152 status[2] = data_status;
153 BT_DBG("Status %d %04x", http_status, http_status);
155 /* Store the status value */
156 _bt_hps_set_char_value(http_status_obj_path, status, 3, 0);
158 /* Send unicast notification */
159 ret = bluetooth_gatt_server_set_notification(http_status_obj_path, unicast_address);
160 if (ret != BLUETOOTH_ERROR_NONE) {
161 BT_ERR("_bt_hps_send_status_notification failed");
164 ret = bluetooth_gatt_update_characteristic(http_status_obj_path, status, 3);
165 if (ret != BLUETOOTH_ERROR_NONE) {
166 BT_ERR("_bt_hps_send_status_notification failed");
172 static void _bt_httpproxy_method(GDBusConnection *connection,
174 const gchar *object_path,
175 const gchar *interface_name,
176 const gchar *method_name,
177 GVariant *parameters,
178 GDBusMethodInvocation *invocation,
183 BT_DBG("Method[%s] Object Path[%s] Interface Name[%s]",
184 method_name, object_path, interface_name);
186 if (g_strcmp0(method_name, "enable") == 0) {
187 g_dbus_method_invocation_return_value(invocation, g_variant_new("(y)", status));
188 } else if (g_strcmp0(method_name, "disable") == 0) {
190 g_dbus_method_invocation_return_value(invocation, g_variant_new("(y)", status));
196 static const GDBusInterfaceVTable hps_method_table = {
197 _bt_httpproxy_method,
202 static void _bt_hps_on_bus_acquired(GDBusConnection *connection, const gchar *name, gpointer user_data)
204 GDBusNodeInfo *node_info = NULL;
206 GError *error = NULL;
212 node_info = g_dbus_node_info_new_for_xml(hps_introspection_xml, &error);
214 BT_ERR("Failed to install: %s", error->message);
215 g_clear_error(&error);
219 object_id = g_dbus_connection_register_object(connection, BT_HPS_OBJECT_PATH,
220 node_info->interfaces[0],
223 g_dbus_node_info_unref(node_info);
224 if (object_id == 0) {
225 BT_ERR("Failed to register method table: %s", error->message);
232 static void _bt_hps_on_name_acquired(GDBusConnection *connection,
240 static void _bt_hps_on_name_lost(GDBusConnection *connection,
245 g_object_unref(g_conn);
247 g_bus_unown_name(g_owner_id);
252 int _bt_hps_register_interface(void)
258 owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
260 G_BUS_NAME_OWNER_FLAGS_NONE,
261 _bt_hps_on_bus_acquired, _bt_hps_on_name_acquired, _bt_hps_on_name_lost,
263 g_owner_id = owner_id;
264 BT_DBG("owner_id is [%d]\n", owner_id);
266 return BLUETOOTH_ERROR_NONE;
269 void _bt_hps_unregister_interface(void)
273 g_object_unref(g_conn);
275 g_bus_unown_name(g_owner_id);
281 static struct hps_char_info *hps_get_char_value(const char *path)
285 for (tmp = hps_char_list; tmp != NULL; tmp = tmp->next) {
287 struct hps_char_info *char_info = tmp->data;
288 if (!g_strcmp0(char_info->char_path, path))
295 static int char_info_cmp(gconstpointer a1, gconstpointer a2)
297 const struct hps_char_info *attrib1 = a1;
298 const struct hps_char_info *attrib2 = a2;
300 return g_strcmp0(attrib1->char_path, attrib2->char_path);
303 static int notify_info_cmp(gconstpointer a1, gconstpointer a2)
305 const struct hps_notify_read_info *attrib1 = a1;
306 const struct hps_notify_read_info *attrib2 = a2;
308 return g_strcmp0(attrib1->char_path, attrib2->char_path);
311 static void _bt_hps_set_char_value(const char *obj_path, const char* value, int value_length, int offset)
317 for (tmp = hps_char_list; tmp != NULL; tmp = tmp->next) {
319 struct hps_char_info *char_info = tmp->data;
320 if (!g_strcmp0(char_info->char_path, obj_path)) {
322 if (char_info->char_value == NULL) {
323 char_info->char_value = g_malloc0(offset + value_length);
324 char_info->value_length = offset + value_length;
325 } else if (char_info->value_length >= offset + value_length) {
326 /* Just change from offset */
327 memcpy(&char_info->char_value[offset], value, value_length);
329 /* Values crossing pervious allocated limit realloc */
330 data = g_memdup(char_info->char_value, char_info->value_length);
331 char_info->char_value = g_try_realloc(char_info->char_value, offset + value_length);
332 memcpy(char_info->char_value, data, char_info->value_length);
333 memcpy(&char_info->char_value[offset], value, value_length);
334 char_info->value_length = offset + value_length;
337 hps_char_list = g_slist_insert_sorted(hps_char_list,
338 char_info, char_info_cmp);
346 static void _bt_hps_set_notify_read_status(const char *obj_path,
347 guint offset_status, guint read_status, int https_status)
349 struct hps_notify_read_info *notify_read_info = NULL;
352 for (tmp = hps_notify_read_list; tmp != NULL; tmp = tmp->next) {
354 notify_read_info = tmp->data;
355 if (!g_strcmp0(notify_read_info->char_path, obj_path)) {
356 notify_read_info->read_status = read_status;
357 notify_read_info->offset_status = offset_status;
358 notify_read_info->https_status = https_status;
359 hps_notify_read_list = g_slist_insert_sorted(hps_notify_read_list,
360 notify_read_info, notify_info_cmp);
366 if (!hps_notify_read_list) {
367 /* Store Notification information */
368 notify_read_info = g_new0(struct hps_notify_read_info, 1);
369 if (notify_read_info) {
370 notify_read_info->char_path = g_strdup(obj_path);
371 notify_read_info->read_status = read_status;
372 notify_read_info->offset_status = offset_status;
373 notify_read_info->https_status = https_status;
374 hps_notify_read_list = g_slist_append(hps_notify_read_list, notify_read_info);
378 /* Store Notification information */
379 notify_read_info = g_new0(struct hps_notify_read_info, 1);
380 if (notify_read_info) {
381 notify_read_info->char_path = g_strdup(obj_path);
382 notify_read_info->read_status = read_status;
383 notify_read_info->offset_status = offset_status;
384 notify_read_info->https_status = https_status;
385 hps_notify_read_list = g_slist_append(hps_notify_read_list, notify_read_info);
391 static struct hps_notify_read_info *_bt_hps_get_notify_read_status(const char *obj_path)
395 for (tmp = hps_notify_read_list; tmp != NULL; tmp = tmp->next) {
397 struct hps_notify_read_info *notify_read_info = tmp->data;
398 if (!g_strcmp0(notify_read_info->char_path, obj_path))
399 return notify_read_info;
406 static void delete_all_characterisitc(void)
409 for (tmp = hps_char_list; tmp != NULL; tmp = tmp->next) {
411 struct hps_char_info *char_info = tmp->data;
412 if (char_info->char_path)
413 g_free(char_info->char_path);
414 if (char_info->char_value)
415 g_free(char_info->char_value);
416 hps_char_list = g_slist_delete_link(hps_char_list, tmp->data);
419 g_slist_free(hps_char_list);
420 hps_char_list = NULL;
423 static void delete_all_notify_read_status(void)
426 for (tmp = hps_notify_read_list; tmp != NULL; tmp = tmp->next) {
428 struct hps_notify_read_info *notify_read_info = tmp->data;
429 if (notify_read_info->char_path)
430 g_free(notify_read_info->char_path);
431 hps_notify_read_list = g_slist_delete_link(hps_notify_read_list, tmp->data);
434 g_slist_free(hps_notify_read_list);
435 hps_notify_read_list = NULL;
438 static void delete_notify_read_status(const char *obj_path)
441 for (tmp = hps_notify_read_list; tmp != NULL; tmp = tmp->next) {
443 struct hps_notify_read_info *notify_read_info = tmp->data;
444 if (!g_strcmp0(notify_read_info->char_path, obj_path)) {
445 if (notify_read_info->char_path)
446 g_free(notify_read_info->char_path);
447 hps_notify_read_list = g_slist_delete_link(hps_notify_read_list, tmp->data);
455 int _bt_hps_uri_write_cb(char *uri, int len, int offset)
457 if ((len < 1) || (len > MAX_URI_LENGTH)) {
458 BT_ERR("Wrong URI length %d", len);
459 return BLUETOOTH_ERROR_INTERNAL;
462 /* g_uri will be used commonly for all HTTP methods whereever applicable */
465 g_uri = g_strndup(uri, len);
467 _bt_hps_set_char_value(http_uri_obj_path, g_uri, len, offset);
469 return BLUETOOTH_ERROR_NONE;
472 int _bt_hps_http_header_write_cb(char *header, int len, int offset)
474 if ((len < 1) || (len > MAX_HEADER_LENGTH)) {
475 BT_ERR("Wrong Header length %d", len);
476 return BLUETOOTH_ERROR_INTERNAL;
479 /* g_header will be used commonly for all HTTP methods where ever applicable
480 general-header, request-header, entity-header
484 g_header = g_strndup(header, len);
486 _bt_hps_set_char_value(http_hdr_obj_path, g_header, len, offset);
489 return BLUETOOTH_ERROR_NONE;
492 int _bt_hps_entity_body_write_cb(char *entity, int len, int offset)
494 if ((len < 1) || (len > MAX_ENTITY_LENGTH)) {
495 BT_ERR("Wrong Entity length %d", len);
496 return BLUETOOTH_ERROR_INTERNAL;
499 /* g_entity will be used commonly for all HTTP methods whereever applicable */
502 g_entity = g_strndup(entity, len);
504 _bt_hps_set_char_value(http_entity_obj_path, g_entity, len, offset);
507 return BLUETOOTH_ERROR_NONE;
511 int _bt_hps_read_cb(const char *obj_path, char **value, int *len)
513 struct hps_char_info *info = NULL;
514 struct hps_notify_read_info *notify_read_info = NULL;
515 guint data_status = -1;
517 gboolean is_header = FALSE;
520 BT_ERR("Wrong Obj path");
524 if (!g_strcmp0(http_hdr_obj_path, obj_path))
527 info = hps_get_char_value(obj_path);
530 if (info->char_value == NULL || info->value_length == 0)
533 notify_read_info = _bt_hps_get_notify_read_status(obj_path);
534 if (notify_read_info && notify_read_info->read_status != DS_BODY_RECEIVED &&
535 notify_read_info->read_status != DS_HEADER_RECEIVED) {
536 offset = notify_read_info->offset_status;
537 if ((info->value_length - offset) > 0 &&
538 (info->value_length - offset) > MAX_ENTITY_LENGTH) {
540 data_status = DS_HEADER_TRUNCATED;
542 data_status = DS_BODY_TRUNCATED;
543 _bt_hps_set_notify_read_status(obj_path, offset + MAX_ENTITY_LENGTH,
544 data_status, notify_read_info->https_status);
545 *value = g_strdup(&info->char_value[offset]);
546 *len = MAX_ENTITY_LENGTH;
547 } else if ((info->value_length - offset) > 0 &&
548 (info->value_length - offset) <= MAX_ENTITY_LENGTH) {
550 data_status = DS_HEADER_RECEIVED;
552 data_status = DS_BODY_RECEIVED;
553 _bt_hps_set_notify_read_status(obj_path, offset, data_status, notify_read_info->https_status);
554 *value = g_strdup(&info->char_value[offset]);
555 *len = info->value_length - offset;
557 } else if (notify_read_info && (notify_read_info->read_status == DS_BODY_RECEIVED ||
558 notify_read_info->read_status == DS_HEADER_RECEIVED)) {
560 data_status = DS_HEADER_RECEIVED;
562 data_status = DS_BODY_RECEIVED;
563 delete_notify_read_status(obj_path);
564 *value = g_strdup(&info->char_value[offset]);
565 *len = info->value_length;
573 void _bt_hps_head_response_cb(SoupSession *session,
574 SoupMessage *msg, gpointer user_data)
576 unsigned short http_status = 0x00;
578 unsigned char status[3] = {0x00};
580 const char *device_address = user_data;
581 bluetooth_device_address_t addr_hex = { {0,} };
582 unsigned char data_status = DS_NONE;
583 _hps_convert_address_to_hex(&addr_hex, device_address);
586 if (hps_soup_session != session) {
587 BT_ERR("Wrong Session");
592 BT_ERR("Wrong Message");
597 req_state = HTTP_REQ_STATE_EXECUTED;
599 http_status = msg->status_code;
601 // Process Header in Response Body
602 if (msg->response_headers) {
604 const char *content = NULL;
605 const char *length = NULL;
608 length = soup_message_headers_get_one(msg->request_headers,
610 // Check "Content-MD5" is the right name to get header content
611 content = soup_message_headers_get_one(msg->response_headers,
613 if (content == NULL || length == NULL) {
614 BT_ERR("Wrong Response Header");
615 _bt_hps_send_status_notification(http_status, data_status, &addr_hex);
619 hdr_len = soup_message_headers_get_content_length(msg->response_headers);
621 // Write Data to Header Characteristic
623 _bt_hps_set_char_value(http_hdr_obj_path, content, hdr_len, 0);
625 bluetooth_gatt_set_characteristic_value(http_hdr_obj_path, content, hdr_len);
627 // TODO : Handle Truncated Header
629 // Write Data to Status Code Characteristic
631 data_status = (hdr_len > MAX_ENTITY_LENGTH) ? DS_HEADER_TRUNCATED : DS_HEADER_RECEIVED;
633 if (data_status == DS_BODY_TRUNCATED && SOUP_STATUS_IS_SUCCESSFUL(http_status))
634 _bt_hps_set_notify_read_status(http_hdr_obj_path, 0, data_status, http_status);
636 _bt_hps_send_status_notification(http_status, data_status, &addr_hex);
638 status[0] = http_status & 0x0F;
639 status[1] = (http_status >> 8) & 0x0F;
640 status[2] = (hdr_len > MAX_HEADER_LENGTH) ? DS_HEADER_TRUNCATED : DS_HEADER_RECEIVED;
642 bluetooth_gatt_set_characteristic_value(http_status_obj_path, status, 3);
645 BT_ERR("HEAD Response is NULL");
646 _bt_hps_send_status_notification(http_status, data_status, &addr_hex);
652 void _bt_hps_http_response_cb(SoupSession *session,
653 SoupMessage *msg, gpointer user_data)
655 unsigned short http_status = 0x00;
657 unsigned char status[3] = {0x00};
659 const char *device_address = user_data;
660 bluetooth_device_address_t addr_hex = { {0,} };
661 unsigned char data_status = DS_NONE;
662 _hps_convert_address_to_hex(&addr_hex, device_address);
665 if (hps_soup_session != session) {
666 BT_ERR("Wrong Session");
671 BT_ERR("Wrong Message");
677 req_state = HTTP_REQ_STATE_EXECUTED;
679 http_status = msg->status_code;
681 // Write Data to Status Code Characteristic
683 status[0] = http_status & 0x0F;
684 status[1] = (http_status >> 8) & 0x0F;
685 status[2] = DS_HEADER_RECEIVED;
686 bluetooth_gatt_set_characteristic_value(http_status_obj_path, status, 3);
688 data_status = DS_HEADER_RECEIVED;
689 _bt_hps_send_status_notification(http_status, data_status, &addr_hex);
695 void _bt_hps_get_response_cb(SoupSession *session,
696 SoupMessage *msg, gpointer user_data)
698 SoupBuffer *body = NULL;
699 unsigned short http_status = 0x00;
701 unsigned char status[3] = {0x00};
703 const char *device_address = user_data;
704 bluetooth_device_address_t addr_hex = { {0,} };
705 unsigned char data_status = DS_NONE;
706 _hps_convert_address_to_hex(&addr_hex, device_address);
709 if (hps_soup_session != session) {
710 BT_ERR("Wrong Session");
715 BT_ERR("Wrong Message");
721 req_state = HTTP_REQ_STATE_EXECUTED;
723 http_status = msg->status_code;
725 // Process Entity Body in Response Message
726 if (msg->response_body) {
728 body = soup_message_body_flatten(msg->response_body);
730 BT_ERR("Wrong Response Body");
732 _bt_hps_send_status_notification(http_status, data_status, &addr_hex);
736 if (body->data == NULL || body->length <= 0) {
737 BT_ERR("Wrong Response");
738 soup_buffer_free(body);
740 _bt_hps_send_status_notification(http_status, data_status, &addr_hex);
744 // Write Data to Entity Body Characteristic
746 _bt_hps_set_char_value(http_entity_obj_path, body->data, body->length, 0);
748 bluetooth_gatt_set_characteristic_value(http_entity_obj_path, body->data, body->length);
750 // TODO : Handle Truncated Entiry Body
752 // Write Data to Status Code Characteristic
754 data_status = (body->length > MAX_ENTITY_LENGTH) ? DS_BODY_TRUNCATED : DS_BODY_RECEIVED;
756 if (data_status == DS_BODY_TRUNCATED && SOUP_STATUS_IS_SUCCESSFUL(http_status))
757 _bt_hps_set_notify_read_status(http_entity_obj_path, 0, data_status, http_status);
759 _bt_hps_send_status_notification(http_status, data_status, &addr_hex);
762 status[0] = http_status & 0x0F;
763 status[1] = (http_status >> 8) & 0x0F;
764 status[2] = (body->length > MAX_HEADER_LENGTH) ? DS_BODY_TRUNCATED : DS_BODY_TRUNCATED;
766 bluetooth_gatt_set_characteristic_value(http_status_obj_path, status, 3);
768 soup_buffer_free(body);
770 BT_ERR("GET Response Body is NULL");
772 _bt_hps_send_status_notification(http_status, data_status, &addr_hex);
776 // Process Header in Response Body
777 if (msg->response_headers) {
779 const char *content = NULL;
780 const char *length = NULL;
783 length = soup_message_headers_get_one(msg->request_headers,
785 // Check "Content-MD5" is the right name to get header content
786 content = soup_message_headers_get_one(msg->response_headers,
788 if (content == NULL || length == NULL) {
789 BT_ERR("Wrong Response Header");
790 data_status = DS_NONE;
791 _bt_hps_send_status_notification(http_status, data_status, &addr_hex);
795 hdr_len = soup_message_headers_get_content_length(msg->response_headers);
796 // Write Data to Header Characteristic
798 _bt_hps_set_char_value(http_hdr_obj_path, content, hdr_len, 0);
800 bluetooth_gatt_set_characteristic_value(http_hdr_obj_path, content, hdr_len);
802 // TODO : Handle Truncated Header
804 // Write Data to Status Code Characteristic
806 data_status = (hdr_len > MAX_HEADER_LENGTH) ? DS_HEADER_TRUNCATED : DS_HEADER_RECEIVED;
808 if (data_status == DS_HEADER_TRUNCATED && SOUP_STATUS_IS_SUCCESSFUL(http_status))
809 _bt_hps_set_notify_read_status(http_hdr_obj_path, 0, data_status, http_status);
811 _bt_hps_send_status_notification(http_status, data_status, &addr_hex);
813 status[0] = http_status & 0x0F;
814 status[1] = (http_status >> 8) & 0x0F;
815 status[2] = (hdr_len > MAX_HEADER_LENGTH) ? DS_HEADER_TRUNCATED : DS_HEADER_RECEIVED;
817 bluetooth_gatt_set_characteristic_value(http_status_obj_path, status, 3);
820 BT_ERR("GET Response Header is NULL");
822 _bt_hps_send_status_notification(http_status, data_status, &addr_hex);
830 int _bt_hps_control_point_write_cb(const char *value, int len, char *addr)
832 int _bt_hps_control_point_write_cb(char *value, int len)
836 GTlsCertificate *cert = NULL;
837 GTlsCertificateFlags flags;
838 gboolean https_status = FALSE;
839 int result = BLUETOOTH_ERROR_NONE;
840 BT_INFO("Opcode %0x", opcode);
843 _bt_hps_set_char_value(http_cp_obj_path, value, len, 0);
847 case HTTP_GET_REQUEST:
848 if (req_state == HTTP_REQ_STATE_EXECUTED) {
849 req_state = HTTP_REQ_STATE_INPROGRESS;
850 hps_soup_msg = soup_message_new("GET", g_uri);
852 g_object_ref(hps_soup_msg);
853 soup_session_queue_message(hps_soup_session, hps_soup_msg, _bt_hps_get_response_cb, addr);
855 soup_session_queue_message(hps_soup_session, hps_soup_msg, _bt_hps_get_response_cb, NULL);
858 BT_ERR("HTTP GET request in progress, message dropped");
859 result = BLUETOOTH_ERROR_INTERNAL;
863 case HTTP_POST_REQUEST:
864 if (req_state == HTTP_REQ_STATE_EXECUTED) {
865 req_state = HTTP_REQ_STATE_INPROGRESS;
866 hps_soup_msg = soup_message_new("POST", g_uri);
867 if (hps_soup_msg == NULL || g_entity == NULL) {
868 BT_ERR("Soup Message NULL");
869 result = BLUETOOTH_ERROR_INTERNAL;
870 req_state = HTTP_REQ_STATE_EXECUTED;
873 soup_message_set_request(hps_soup_msg, "text/xml", SOUP_MEMORY_COPY,
874 g_entity, strlen(g_entity));
876 g_object_ref(hps_soup_msg);
877 soup_session_queue_message(hps_soup_session, hps_soup_msg, _bt_hps_http_response_cb, addr);
879 soup_session_queue_message(hps_soup_session, hps_soup_msg, _bt_hps_http_response_cb, NULL);
882 BT_ERR("HTTP POST request in progress, message dropped");
883 result = BLUETOOTH_ERROR_INTERNAL;
887 case HTTP_HEAD_REQUEST:
888 if (req_state == HTTP_REQ_STATE_EXECUTED) {
889 req_state = HTTP_REQ_STATE_INPROGRESS;
890 hps_soup_msg = soup_message_new("HEAD", g_uri);
891 if (hps_soup_msg == NULL) {
892 BT_ERR("Soup Message NULL");
893 result = BLUETOOTH_ERROR_INTERNAL;
894 req_state = HTTP_REQ_STATE_EXECUTED;
898 g_object_ref(hps_soup_msg);
899 soup_session_queue_message(hps_soup_session, hps_soup_msg, _bt_hps_head_response_cb, addr);
901 soup_session_queue_message(hps_soup_session, hps_soup_msg, _bt_hps_head_response_cb, NULL);
904 BT_ERR("HTTP HEAD request in progress, message dropped");
905 result = BLUETOOTH_ERROR_INTERNAL;
909 case HTTP_PUT_REQUEST:
910 if (req_state == HTTP_REQ_STATE_EXECUTED) {
912 req_state = HTTP_REQ_STATE_INPROGRESS;
913 hps_soup_msg = soup_message_new("PUT", g_uri);
914 if (hps_soup_msg == NULL || g_entity == NULL) {
915 BT_ERR("Soup Message NULL");
916 result = BLUETOOTH_ERROR_INTERNAL;
917 req_state = HTTP_REQ_STATE_EXECUTED;
920 buf = soup_buffer_new(SOUP_MEMORY_TAKE, g_entity, strlen(g_entity));
921 soup_message_body_append_buffer(hps_soup_msg->request_body, buf);
922 soup_message_body_set_accumulate(hps_soup_msg->request_body, FALSE);
924 g_object_ref(hps_soup_msg);
925 soup_session_queue_message(hps_soup_session, hps_soup_msg, _bt_hps_http_response_cb, addr);
927 soup_session_queue_message(hps_soup_session, hps_soup_msg, _bt_hps_http_response_cb, NULL);
931 BT_ERR("HTTP PUT request in progress, message dropped");
932 result = BLUETOOTH_ERROR_INTERNAL;
936 case HTTP_DELETE_REQUEST:
937 if (req_state == HTTP_REQ_STATE_EXECUTED) {
938 req_state = HTTP_REQ_STATE_INPROGRESS;
939 hps_soup_msg = soup_message_new("DELETE", g_uri);
940 if (hps_soup_msg == NULL) {
941 BT_ERR("Soup Message NULL");
942 result = BLUETOOTH_ERROR_INTERNAL;
943 req_state = HTTP_REQ_STATE_EXECUTED;
947 g_object_ref(hps_soup_msg);
948 soup_session_queue_message(hps_soup_session, hps_soup_msg, _bt_hps_http_response_cb, addr);
950 soup_session_queue_message(hps_soup_session, hps_soup_msg, _bt_hps_http_response_cb, NULL);
953 BT_ERR("HTTP DELETE request in progress, message dropped");
954 result = BLUETOOTH_ERROR_INTERNAL;
958 case HTTPS_GET_REQUEST:
959 if (req_state == HTTP_REQ_STATE_EXECUTED) {
960 req_state = HTTP_REQ_STATE_INPROGRESS;
961 hps_soup_msg = soup_message_new("GET", g_uri);
962 if (hps_soup_msg == NULL) {
963 BT_ERR("Soup Message NULL");
964 result = BLUETOOTH_ERROR_INTERNAL;
965 req_state = HTTP_REQ_STATE_EXECUTED;
969 g_object_ref(hps_soup_msg);
970 soup_session_queue_message(hps_soup_session, hps_soup_msg, _bt_hps_get_response_cb, addr);
972 soup_session_queue_message(hps_soup_session, hps_soup_msg, _bt_hps_get_response_cb, NULL);
974 https_status = soup_message_get_https_status(hps_soup_msg, &cert, &flags);
976 _bt_hps_set_char_value(http_security_obj_path, (const char *)&https_status, 1, 0);
978 bluetooth_gatt_set_characteristic_value(http_security_obj_path, (char *)&https_status, 1);
981 BT_ERR("HTTPS GET request in progress, message dropped");
982 result = BLUETOOTH_ERROR_INTERNAL;
986 case HTTPS_HEAD_REQUEST:
987 if (req_state == HTTP_REQ_STATE_EXECUTED) {
988 req_state = HTTP_REQ_STATE_INPROGRESS;
989 hps_soup_msg = soup_message_new("HEAD", g_uri);
990 if (hps_soup_msg == NULL) {
991 BT_ERR("Soup Message NULL");
992 result = BLUETOOTH_ERROR_INTERNAL;
993 req_state = HTTP_REQ_STATE_EXECUTED;
997 g_object_ref(hps_soup_msg);
998 soup_session_queue_message(hps_soup_session, hps_soup_msg, _bt_hps_head_response_cb, addr);
1000 soup_session_queue_message(hps_soup_session, hps_soup_msg, _bt_hps_head_response_cb, NULL);
1002 https_status = soup_message_get_https_status(hps_soup_msg, &cert, &flags);
1004 _bt_hps_set_char_value(http_security_obj_path, (const char *)&https_status, 1, 0);
1006 bluetooth_gatt_set_characteristic_value(http_security_obj_path, (char *)&https_status, 1);
1009 BT_ERR("HTTPS HEAD request in progress, message dropped");
1010 result = BLUETOOTH_ERROR_INTERNAL;
1014 case HTTPS_POST_REQUEST:
1015 if (req_state == HTTP_REQ_STATE_EXECUTED) {
1016 req_state = HTTP_REQ_STATE_INPROGRESS;
1017 hps_soup_msg = soup_message_new("POST", g_uri);
1018 if (hps_soup_msg == NULL) {
1019 BT_ERR("Soup Message NULL");
1020 result = BLUETOOTH_ERROR_INTERNAL;
1021 req_state = HTTP_REQ_STATE_EXECUTED;
1024 soup_message_set_request(hps_soup_msg, "text/xml", SOUP_MEMORY_STATIC,
1025 g_entity, strlen(g_entity));
1027 g_object_ref(hps_soup_msg);
1028 soup_session_queue_message(hps_soup_session, hps_soup_msg, _bt_hps_http_response_cb, addr);
1030 soup_session_queue_message(hps_soup_session, hps_soup_msg, _bt_hps_http_response_cb, NULL);
1033 https_status = soup_message_get_https_status(hps_soup_msg, &cert, &flags);
1035 _bt_hps_set_char_value(http_security_obj_path, (const char *)&https_status, 1, 0);
1037 bluetooth_gatt_set_characteristic_value(http_security_obj_path, (char *)&https_status, 1);
1040 BT_ERR("HTTPS POST request in progress, message dropped");
1041 result = BLUETOOTH_ERROR_INTERNAL;
1045 case HTTPS_PUT_REQUEST:
1046 if (req_state == HTTP_REQ_STATE_EXECUTED) {
1048 req_state = HTTP_REQ_STATE_INPROGRESS;
1049 hps_soup_msg = soup_message_new("PUT", g_uri);
1050 if (hps_soup_msg == NULL) {
1051 BT_ERR("Soup Message NULL");
1052 result = BLUETOOTH_ERROR_INTERNAL;
1053 req_state = HTTP_REQ_STATE_EXECUTED;
1056 buf = soup_buffer_new(SOUP_MEMORY_TAKE, g_entity, strlen(g_entity));
1057 soup_message_body_append_buffer(hps_soup_msg->request_body, buf);
1058 soup_message_body_set_accumulate(hps_soup_msg->request_body, FALSE);
1060 g_object_ref(hps_soup_msg);
1061 soup_session_queue_message(hps_soup_session, hps_soup_msg, _bt_hps_http_response_cb, addr);
1063 soup_session_queue_message(hps_soup_session, hps_soup_msg, _bt_hps_http_response_cb, NULL);
1065 https_status = soup_message_get_https_status(hps_soup_msg, &cert, &flags);
1067 _bt_hps_set_char_value(http_security_obj_path, (const char *)&https_status, 1, 0);
1069 bluetooth_gatt_set_characteristic_value(http_security_obj_path, (char *)&https_status, 1);
1072 BT_ERR("HTTPS PUT request in progress, message dropped");
1073 result = BLUETOOTH_ERROR_INTERNAL;
1077 case HTTPS_DELETE_REQUEST:
1078 if (req_state == HTTP_REQ_STATE_EXECUTED) {
1079 req_state = HTTP_REQ_STATE_INPROGRESS;
1080 hps_soup_msg = soup_message_new("DELETE", g_uri);
1081 if (hps_soup_msg == NULL) {
1082 BT_ERR("Soup Message NULL");
1083 result = BLUETOOTH_ERROR_INTERNAL;
1084 req_state = HTTP_REQ_STATE_EXECUTED;
1088 g_object_ref(hps_soup_msg);
1089 soup_session_queue_message(hps_soup_session, hps_soup_msg, _bt_hps_http_response_cb, addr);
1091 soup_session_queue_message(hps_soup_session, hps_soup_msg, _bt_hps_http_response_cb, NULL);
1094 https_status = soup_message_get_https_status(hps_soup_msg, &cert, &flags);
1096 _bt_hps_set_char_value(http_security_obj_path, (const char *)&https_status, 1, 0);
1098 bluetooth_gatt_set_characteristic_value(http_security_obj_path, (char *)&https_status, 1);
1101 BT_ERR("HTTPS DELETE request in progress, message dropped");
1102 result = BLUETOOTH_ERROR_INTERNAL;
1106 case HTTP_REQUEST_CANCEL:
1107 /* Cancel the outstanding request */
1108 if (req_state == HTTP_REQ_STATE_INPROGRESS) {
1109 req_state = HTTP_REQ_STATE_IDLE;
1110 if (hps_soup_msg == NULL) {
1111 BT_ERR("Soup Message NULL");
1112 result = BLUETOOTH_ERROR_INTERNAL;
1113 req_state = HTTP_REQ_STATE_EXECUTED;
1116 soup_session_cancel_message(hps_soup_session, hps_soup_msg, SOUP_STATUS_CANCELLED);
1117 hps_soup_msg = NULL;
1122 BT_ERR("Unknown opcode %0x", opcode);
1123 result = BLUETOOTH_ERROR_INTERNAL;
1130 void _bt_hps_security_read_cb(char *value, int len)
1132 BT_INFO("HPS Client Read the value");
1137 void _bt_hps_gatt_char_property_changed_event(GVariant *msg,
1140 int result = BLUETOOTH_ERROR_NONE;
1141 GVariantIter value_iter;
1142 const char *property = NULL;
1143 const char * char_path = NULL;
1144 const char * svc_handle = NULL;
1145 GVariant *var = NULL;
1146 GVariant *val = NULL;
1147 g_variant_iter_init(&value_iter, msg);
1149 while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &var))) {
1151 if (!g_strcmp0(property, "WriteValue")) {
1153 BT_INFO("WriteValue");
1154 BT_INFO("Type '%s'\n", g_variant_get_type_string(var));
1161 g_variant_get(var, "(&s&s&syq@ay)", &char_path,
1162 &svc_handle, &addr, &req_id, &offset, &val);
1164 len = g_variant_get_size(val);
1166 BT_DBG("Len = %d, Offset = %d", len, offset);
1168 value = (char *) g_variant_get_data(val);
1171 if (!g_strcmp0(char_path, http_uri_obj_path)) {
1173 result = _bt_hps_uri_write_cb(value, len, offset);;
1174 } else if (!g_strcmp0(char_path, http_hdr_obj_path)) {
1175 /* Retrive HEADER */
1176 result = _bt_hps_http_header_write_cb(value, len, offset);
1177 } else if (!g_strcmp0(char_path, http_entity_obj_path)) {
1178 /* Retrive ENTITY BODY */
1179 result = _bt_hps_entity_body_write_cb(value, len, offset);
1180 } else if (!g_strcmp0(char_path, http_cp_obj_path)) {
1181 result = _bt_hps_control_point_write_cb(value, len, addr);
1183 BT_ERR("Wrong Object Path %s", char_path);
1184 result = BLUETOOTH_ERROR_INTERNAL;
1186 bluetooth_gatt_send_response(req_id, BLUETOOTH_GATT_ATT_REQUEST_TYPE_WRITE, result, 0, NULL, 0);
1188 BT_ERR("Array Len 0");
1191 BT_ERR("var==NULL");
1193 } else if (!g_strcmp0(property, "ReadValue")) {
1199 int data_status = -1;
1200 BT_INFO("ReadValue");
1201 BT_INFO("Type '%s'\n", g_variant_get_type_string(var));
1203 g_variant_get(var, "(&s&s&syq)", &char_path, &svc_handle,
1204 &addr, &req_id, &offset);
1206 data_status = _bt_hps_read_cb(char_path, &value, &len);
1207 if (data_status >= DS_NONE) {
1208 struct hps_notify_read_info *notify_read_info = NULL;
1209 bluetooth_device_address_t addr_hex = { {0,} };
1210 _hps_convert_address_to_hex(&addr_hex, addr);
1211 bluetooth_gatt_send_response(req_id, BLUETOOTH_GATT_ATT_REQUEST_TYPE_READ,
1212 BLUETOOTH_ERROR_NONE, offset, value, len);
1213 notify_read_info = _bt_hps_get_notify_read_status(char_path);
1214 if (notify_read_info) {
1215 _bt_hps_send_status_notification(notify_read_info->https_status,
1216 data_status, &addr_hex);
1218 if (data_status == DS_BODY_RECEIVED ||
1219 data_status == DS_HEADER_RECEIVED) {
1220 _bt_hps_set_char_value(char_path, NULL, 0, 0);
1226 BT_ERR("ReadValue failed %s", char_path);
1227 bluetooth_gatt_send_response(req_id, BLUETOOTH_GATT_ATT_REQUEST_TYPE_READ,
1228 BLUETOOTH_ERROR_INTERNAL, offset, NULL, 0);
1235 void _bt_hps_gatt_char_property_changed_event(GVariant *msg,
1238 GVariantIter value_iter;
1239 char *property = NULL;
1240 char * char_handle = NULL;
1241 GVariant *val = NULL;
1242 int result = BLUETOOTH_ERROR_NONE;
1243 GVariant *param = NULL;
1244 g_variant_iter_init(&value_iter, msg);
1245 char_handle = g_strdup(path);
1247 while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &val))) {
1249 if (property == NULL) {
1250 BT_ERR("Property NULL");
1254 if (strcasecmp(property, "ChangedValue") == 0) {
1257 GByteArray *gp_byte_array = NULL;
1258 BT_INFO("Type '%s'\n", g_variant_get_type_string(val));
1261 gp_byte_array = g_byte_array_new();
1262 len = g_variant_get_size(val);
1263 BT_DBG("Len = %d", len);
1264 g_byte_array_append(gp_byte_array,
1265 (const guint8 *)g_variant_get_data(val), len);
1266 if (gp_byte_array->len != 0) {
1267 if (strcmp(path, http_uri_obj_path)) {
1269 _bt_hps_uri_write_cb(NULL, len);
1270 } else if (strcmp(path, http_hdr_obj_path)) {
1272 _bt_hps_http_header_write_cb(NULL, len);
1273 } else if (strcmp(path, http_entity_obj_path)) {
1274 //Retrive ENTITY BODY
1275 _bt_hps_entity_body_write_cb(NULL, len);
1276 } else if (strcmp(path, http_cp_obj_path)) {
1277 _bt_hps_control_point_write_cb(NULL, len);
1278 } else if (strcmp(path, http_security_obj_path)) {
1279 _bt_hps_security_read_cb(NULL, len);
1281 BT_ERR("Wrong Object Path %s", path);
1284 BT_ERR("Array Len 0");
1286 g_byte_array_free(gp_byte_array, TRUE);
1288 BT_ERR("val==NULL");
1292 g_free(char_handle);
1298 void _bt_hps_property_event_filter(GDBusConnection *connection,
1299 const gchar *sender_name,
1300 const gchar *object_path,
1301 const gchar *interface_name,
1302 const gchar *signal_name,
1303 GVariant *parameters,
1308 if (signal_name == NULL) {
1309 BT_ERR("Wrong Signal");
1314 if (g_strcmp0(signal_name, PROPERTIES_CHANGED) == 0) {
1316 g_variant_get(parameters, "(@a{sv}@as)", &value, NULL);
1318 _bt_hps_gatt_char_property_changed_event(value, object_path);
1320 if (g_strcmp0(interface_name, BT_HPS_PROPERTIES_INTERFACE) == 0) {
1322 g_variant_get(parameters, "(&s@a{sv}@as)", &interface_name, &value, NULL);
1324 _bt_hps_gatt_char_property_changed_event(value, object_path);
1327 //BT_ERR("Wrong Interface %s", interface_name);
1334 void _bt_hps_adapter_event_filter(GDBusConnection *connection,
1335 const gchar *sender_name,
1336 const gchar *object_path,
1337 const gchar *interface_name,
1338 const gchar *signal_name,
1339 GVariant *parameters,
1342 int result = BLUETOOTH_ERROR_NONE;
1345 if (signal_name == NULL) {
1346 BT_ERR("Wrong Signal");
1350 BT_INFO("Interface %s, Signal %s", interface_name, signal_name);
1352 if (g_strcmp0(interface_name, BT_HPS_INTERFACE_NAME) == 0) {
1354 g_variant_get(parameters, "(&s@a{sv}@as)", &interface_name, &value, NULL);
1356 if (strcasecmp(signal_name, BLE_ENABLED) == 0) {
1357 g_variant_get(parameters, "(i)", &result);
1359 if (_bt_hps_prepare_httpproxy() != BLUETOOTH_ERROR_NONE) {
1360 BT_ERR("Fail to prepare HTTP Proxy");
1364 if (_bt_hps_set_advertising_data() != BLUETOOTH_ERROR_NONE) {
1365 BT_ERR("Fail to set advertising data");
1370 BT_ERR("Wrong Signal %s", signal_name);
1377 int _bt_hps_init_event_receiver()
1379 GError *error = NULL;
1384 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
1385 if (error != NULL) {
1386 BT_ERR("ERROR: Can't get on system bus [%s]", error->message);
1387 g_clear_error(&error);
1391 property_sub_id = g_dbus_connection_signal_subscribe(conn,
1392 NULL, BT_HPS_INTERFACE_NAME,
1393 PROPERTIES_CHANGED, BT_HPS_OBJECT_PATH, NULL, 0,
1394 _bt_hps_property_event_filter,
1397 adapter_sub_id = g_dbus_connection_signal_subscribe(conn,
1398 NULL, BT_HPS_INTERFACE_NAME,
1399 BLE_ENABLED, BT_HPS_OBJECT_PATH, NULL, 0,
1400 _bt_hps_adapter_event_filter,
1406 void _bt_hps_deinit_event_receiver(void)
1413 if (property_sub_id > 0) {
1414 g_dbus_connection_signal_unsubscribe(conn, property_sub_id);
1415 property_sub_id = 0;
1418 if (adapter_sub_id) {
1419 g_dbus_connection_signal_unsubscribe(conn, adapter_sub_id);
1428 int _bt_hps_set_advertising_data(void)
1433 guint8 data[4] = {0x03, 0x02, 0x23, 0x18};
1434 bluetooth_advertising_data_t adv;
1436 BT_DBG("%x %x %x %x", data[0], data[1], data[2], data[3]);
1437 memcpy(adv.data, data, sizeof(data));
1438 ret = bluetooth_set_advertising_data(0, &adv, sizeof(data));
1439 if (ret != BLUETOOTH_ERROR_NONE) {
1440 BT_ERR("Failed to set ADV data %d", ret);
1444 ret = bluetooth_set_advertising(0, TRUE);
1445 if (ret != BLUETOOTH_ERROR_NONE) {
1446 BT_ERR("Failed to set ADV %d", ret);
1453 int _bt_hps_prepare_httpproxy(void)
1455 int ret = BLUETOOTH_ERROR_NONE;
1459 bt_gatt_characteristic_property_t props;
1461 char value[MAX_URI_LENGTH] = { 0 };
1462 struct hps_char_info *char_info = NULL;
1464 char status[3] = { 0 };
1469 ret = bluetooth_gatt_init();
1470 if (ret != BLUETOOTH_ERROR_NONE) {
1471 BT_ERR("Failed to Init GATT %d", ret);
1475 service_uuid = __hps_convert_uuid_to_uuid128(HPS_UUID);
1476 ret = bluetooth_gatt_add_service(service_uuid, &hps_obj_path);
1477 if (ret != BLUETOOTH_ERROR_NONE) {
1478 BT_ERR("Failed to add service %d", ret);
1482 /* Characteristic URI */
1483 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE;
1484 char_uuid = __hps_convert_uuid_to_uuid128(HTTP_URI_UUID);
1485 ret = bluetooth_gatt_add_new_characteristic(hps_obj_path, char_uuid, 0, props, &http_uri_obj_path);
1486 if (ret != BLUETOOTH_ERROR_NONE) {
1487 BT_ERR("Failed to add new char %d", ret);
1492 ret = bluetooth_gatt_set_characteristic_value(http_uri_obj_path, value, MAX_URI_LENGTH);
1493 if (ret != BLUETOOTH_ERROR_NONE) {
1494 BT_ERR("Failed to add new char %d", ret);
1498 /* Store requets information */
1499 char_info = g_new0(struct hps_char_info, 1);
1500 char_info->char_path = g_strdup(http_uri_obj_path);
1501 hps_char_list = g_slist_append(hps_char_list, char_info);
1502 _bt_hps_set_char_value(http_uri_obj_path, value, MAX_URI_LENGTH, 0);
1505 /* Characteristic HTTP Headers */
1506 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ |
1507 BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE;
1508 char_uuid = __hps_convert_uuid_to_uuid128(HTTP_HDR_UUID);
1509 ret = bluetooth_gatt_add_new_characteristic(hps_obj_path, char_uuid, 0, props, &http_hdr_obj_path);
1510 if (ret != BLUETOOTH_ERROR_NONE) {
1511 BT_ERR("Failed to add new char %d", ret);
1515 ret = bluetooth_gatt_set_characteristic_value(http_hdr_obj_path, value, MAX_HEADER_LENGTH);
1516 if (ret != BLUETOOTH_ERROR_NONE) {
1517 BT_ERR("Failed to add new char %d", ret);
1521 /* Store Characterisitc information */
1522 char_info = g_new0(struct hps_char_info, 1);
1523 char_info->char_path = g_strdup(http_hdr_obj_path);
1524 hps_char_list = g_slist_append(hps_char_list, char_info);
1525 _bt_hps_set_char_value(http_hdr_obj_path, value, MAX_HEADER_LENGTH, 0);
1528 /* Characteristic HTTP Entity Body */
1529 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ |
1530 BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE;
1531 char_uuid = __hps_convert_uuid_to_uuid128(HTTP_ENTITY_UUID);
1532 ret = bluetooth_gatt_add_new_characteristic(hps_obj_path, char_uuid, 0, props, &http_entity_obj_path);
1533 if (ret != BLUETOOTH_ERROR_NONE) {
1534 BT_ERR("Failed to add new char %d", ret);
1538 ret = bluetooth_gatt_set_characteristic_value(http_entity_obj_path, value, MAX_ENTITY_LENGTH);
1539 if (ret != BLUETOOTH_ERROR_NONE) {
1540 BT_ERR("Failed to add new char %d", ret);
1544 /* Store Characterisitc information */
1545 char_info = g_new0(struct hps_char_info, 1);
1546 char_info->char_path = g_strdup(http_entity_obj_path);
1547 hps_char_list = g_slist_append(hps_char_list, char_info);
1548 _bt_hps_set_char_value(http_entity_obj_path, value, MAX_ENTITY_LENGTH, 0);
1551 /* Characteristic HTTP Control Point */
1552 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE;
1553 char_uuid = __hps_convert_uuid_to_uuid128(HTTP_CP_UUID);
1554 ret = bluetooth_gatt_add_new_characteristic(hps_obj_path, char_uuid, 0, props, &http_cp_obj_path);
1555 if (ret != BLUETOOTH_ERROR_NONE) {
1556 BT_ERR("Failed to add new char %d", ret);
1560 ret = bluetooth_gatt_set_characteristic_value(http_cp_obj_path, &cp, 1);
1561 if (ret != BLUETOOTH_ERROR_NONE) {
1562 BT_ERR("Failed to add new char %d", ret);
1566 /* Store Characterisitc information */
1567 char_info = g_new0(struct hps_char_info, 1);
1568 char_info->char_path = g_strdup(http_cp_obj_path);
1569 hps_char_list = g_slist_append(hps_char_list, char_info);
1570 _bt_hps_set_char_value(http_cp_obj_path, &cp, 1, 0);
1573 /* Characteristic HTTP Status Code */
1574 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_NOTIFY;
1575 char_uuid = __hps_convert_uuid_to_uuid128(HTTP_STATUS_UUID);
1576 ret = bluetooth_gatt_add_new_characteristic(hps_obj_path, char_uuid, 0, props, &http_status_obj_path);
1577 if (ret != BLUETOOTH_ERROR_NONE) {
1578 BT_ERR("Failed to add new char %d", ret);
1582 ret = bluetooth_gatt_set_characteristic_value(http_status_obj_path, status, 3);
1583 if (ret != BLUETOOTH_ERROR_NONE) {
1584 BT_ERR("Failed to add new char %d", ret);
1588 desc_uuid = __hps_convert_uuid_to_uuid128(HTTP_STATUS_CCC_DESC_UUID);
1589 ret = bluetooth_gatt_add_descriptor(http_status_obj_path, desc_uuid,
1590 (BLUETOOTH_GATT_PERMISSION_READ | BLUETOOTH_GATT_PERMISSION_WRITE),
1591 &http_status_desc_obj_path);
1592 if (ret != BLUETOOTH_ERROR_NONE) {
1593 BT_ERR("Failed to add new char descriptor %d", ret);
1597 /* Store Characterisitc information */
1598 char_info = g_new0(struct hps_char_info, 1);
1599 char_info->char_path = g_strdup(http_status_obj_path);
1600 hps_char_list = g_slist_append(hps_char_list, char_info);
1601 _bt_hps_set_char_value(http_status_obj_path, status, 3, 0);
1604 /* Characteristic HTTPS Security */
1605 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ;
1606 char_uuid = __hps_convert_uuid_to_uuid128(HTTP_SECURITY_UUID);
1607 ret = bluetooth_gatt_add_new_characteristic(hps_obj_path, char_uuid, 0, props, &http_security_obj_path);
1608 if (ret != BLUETOOTH_ERROR_NONE) {
1609 BT_ERR("Failed to add new char %d", ret);
1613 ret = bluetooth_gatt_set_characteristic_value(http_security_obj_path, &cp, 1);
1614 if (ret != BLUETOOTH_ERROR_NONE) {
1615 BT_ERR("Failed to add new char %d", ret);
1619 /* Store Characterisitc information */
1620 char_info = g_new0(struct hps_char_info, 1);
1621 char_info->char_path = g_strdup(http_security_obj_path);
1622 hps_char_list = g_slist_append(hps_char_list, char_info);
1623 _bt_hps_set_char_value(http_security_obj_path, &cp, 1, 0);
1626 ret = bluetooth_gatt_register_service(hps_obj_path);
1627 if (ret != BLUETOOTH_ERROR_NONE) {
1628 BT_ERR("Failed to register service %d", ret);
1632 ret = bluetooth_gatt_register_application();
1633 if (ret != BLUETOOTH_ERROR_NONE) {
1634 BT_ERR("Failed to register application %d", ret);
1642 delete_all_characterisitc();
1643 delete_all_notify_read_status();
1649 static void _bt_hps_sig_handler(int sig)
1655 BT_DBG("caught signal - sigterm\n");
1658 BT_DBG("caught signal - sigint\n");
1661 BT_DBG("caught signal - sigkill\n");
1664 BT_DBG("caught signal %d and ignored\n", sig);
1669 void _bt_hps_exit(void)
1674 if (g_uri != NULL) {
1679 if (g_header != NULL) {
1684 if (g_entity != NULL) {
1689 soup_session_abort(hps_soup_session);
1690 g_assert_cmpint(G_OBJECT(hps_soup_session)->ref_count, ==, 1);
1691 g_object_unref(hps_soup_session);
1694 delete_all_characterisitc();
1697 ret = bluetooth_gatt_deinit();
1698 if (ret != BLUETOOTH_ERROR_NONE)
1699 BT_ERR("Failed to Deinit GATT %d", ret);
1701 ret = bluetooth_unregister_callback();
1702 if (ret != BLUETOOTH_ERROR_NONE)
1703 BT_ERR("Failed to Unregister callback %d", ret);
1705 _bt_hps_deinit_event_receiver();
1707 _bt_hps_unregister_interface();
1709 if (main_loop != NULL)
1710 g_main_loop_quit(main_loop);
1713 void bt_hps_event_callback(int event, bluetooth_event_param_t* param,
1716 BT_DBG("HPS event %d", event);
1720 /* HTTP Proxy Service Main loop */
1723 struct sigaction sa;
1725 BT_ERR("Starting the bt-httpproxy daemon");
1727 /* Values taken from http://www.browserscope.org/ following
1728 * the rule "Do What Every Other Modern Browser Is Doing". They seem
1729 * to significantly improve page loading time compared to soup's
1731 * Change MAX_CONNECTIONS_PER_HOST value 6 -> 12, and maxConnections is changed from 35 to 60.
1732 * Enhanced network loading speed apply tunning value. */
1733 static const int maxConnections = 60;
1734 static const int maxConnectionsPerHost = 12;
1736 memset(&sa, 0, sizeof(sa));
1737 sa.sa_handler = _bt_hps_sig_handler;
1738 sa.sa_flags = SA_SIGINFO;
1739 sigaction(SIGINT, &sa, NULL);
1740 sigaction(SIGTERM, &sa, NULL);
1741 sigaction(SIGKILL, &sa, NULL);
1744 if (bluetooth_register_callback(bt_hps_event_callback, NULL) != BLUETOOTH_ERROR_NONE) {
1745 BT_ERR("bluetooth_register_callback returned failiure");
1750 if (_bt_hps_register_interface() != BLUETOOTH_ERROR_NONE) {
1751 BT_ERR("Fail to register http proxy service");
1755 if (_bt_hps_init_event_receiver() != BLUETOOTH_ERROR_NONE) {
1756 BT_ERR("Fail to init event reciever");
1760 hps_soup_session = soup_session_async_new();
1761 if (hps_soup_session == NULL) {
1762 BT_ERR("Failed to soup_session_async_new");
1765 /* Set Soup Session Fetures */
1766 g_object_set(hps_soup_session,
1767 SOUP_SESSION_MAX_CONNS, maxConnections,
1768 SOUP_SESSION_MAX_CONNS_PER_HOST, maxConnectionsPerHost,
1769 SOUP_SESSION_ADD_FEATURE_BY_TYPE, SOUP_TYPE_CONTENT_DECODER,
1770 SOUP_SESSION_ADD_FEATURE_BY_TYPE, SOUP_TYPE_CONTENT_SNIFFER,
1771 SOUP_SESSION_ADD_FEATURE_BY_TYPE, SOUP_TYPE_PROXY_RESOLVER_DEFAULT,
1772 SOUP_SESSION_USE_THREAD_CONTEXT, TRUE,
1775 main_loop = g_main_loop_new(NULL, FALSE);
1777 g_main_loop_run(main_loop);
1779 BT_DBG("g_main_loop_quit called!");
1781 if (main_loop != NULL)
1782 g_main_loop_unref(main_loop);