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 int property_sub_id = -1;
48 static int adapter_sub_id = -1;
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;
72 GDBusNodeInfo *hps_node_info = NULL;
75 char *g_header = NULL;
76 char *g_entity = NULL;
78 static SoupSession *hps_soup_session = NULL;
79 static SoupMessage *hps_soup_msg = NULL;
82 static const gchar hps_introspection_xml[] =
84 " <interface name='org.projectx.httpproxy_service'>"
85 " <method name='enable'>"
86 " <arg type='y' name='status' direction='out'/>"
88 " <method name='disable'>"
89 " <arg type='y' name='status' direction='out'/>"
95 static void _bt_hps_set_char_value(const char *obj_path, const char* value, int value_length, int offset);
97 static void _hps_convert_address_to_hex(bluetooth_device_address_t *addr_hex, const char *addr_str)
99 char *ptr1, *ptr2, *ptr3, *ptr4, *ptr5;
101 if (addr_str == NULL || addr_str[0] == '\0')
104 addr_hex->addr[0] = strtol(addr_str, &ptr5, 16);
105 addr_hex->addr[1] = strtol(ptr5 + 1, &ptr4, 16);
106 addr_hex->addr[2] = strtol(ptr4 + 1, &ptr3, 16);
107 addr_hex->addr[3] = strtol(ptr3 + 1, &ptr2, 16);
108 addr_hex->addr[4] = strtol(ptr2 + 1, &ptr1, 16);
109 addr_hex->addr[5] = strtol(ptr1 + 1, NULL, 16);
112 static char *__hps_convert_uuid_to_uuid128(const char *uuid)
122 uuid128 = g_strdup_printf("0000%s-0000-1000-8000-00805F9B34FB", uuid);
127 uuid128 = g_strdup_printf("%s-0000-1000-8000-00805F9B34FB", uuid);
132 uuid128 = strdup(uuid);
142 static void _bt_hps_send_status_notification(unsigned short http_status,
143 unsigned char data_status,
144 bluetooth_device_address_t *unicast_address)
146 char status[3] = {0x00};
147 int ret = BLUETOOTH_ERROR_NONE;
151 status[0] = http_status & 0xFF;
152 status[1] = (http_status >> 8) & 0xFF;
153 status[2] = data_status;
154 BT_DBG("Status %d %04x", http_status, http_status);
156 /* Store the status value */
157 _bt_hps_set_char_value(http_status_obj_path, status, 3, 0);
159 /* Send unicast notification */
160 ret = bluetooth_gatt_server_set_notification(http_status_obj_path, unicast_address);
161 if (ret != BLUETOOTH_ERROR_NONE) {
162 BT_ERR("_bt_hps_send_status_notification failed");
165 ret = bluetooth_gatt_update_characteristic(http_status_obj_path, status, 3);
166 if (ret != BLUETOOTH_ERROR_NONE) {
167 BT_ERR("_bt_hps_send_status_notification failed");
173 static void _bt_httpproxy_method(GDBusConnection *connection,
175 const gchar *object_path,
176 const gchar *interface_name,
177 const gchar *method_name,
178 GVariant *parameters,
179 GDBusMethodInvocation *invocation,
184 BT_DBG("Method[%s] Object Path[%s] Interface Name[%s]",
185 method_name, object_path, interface_name);
187 if (g_strcmp0(method_name, "enable") == 0) {
188 g_dbus_method_invocation_return_value(invocation, g_variant_new("(y)", status));
189 } else if (g_strcmp0(method_name, "disable") == 0) {
191 g_dbus_method_invocation_return_value(invocation, g_variant_new("(y)", status));
197 static const GDBusInterfaceVTable hps_method_table = {
198 _bt_httpproxy_method,
203 static void _bt_hps_on_bus_acquired(GDBusConnection *connection, const gchar *name, gpointer user_data)
206 GError *error = NULL;
212 object_id = g_dbus_connection_register_object(connection, BT_HPS_OBJECT_PATH,
213 hps_node_info->interfaces[0],
216 if (object_id == 0) {
217 BT_ERR("Failed to register method table: %s", error->message);
219 g_dbus_node_info_unref(hps_node_info);
225 static void _bt_hps_on_name_acquired(GDBusConnection *connection,
233 static void _bt_hps_on_name_lost(GDBusConnection *connection,
238 g_object_unref(g_conn);
240 g_dbus_node_info_unref(hps_node_info);
241 g_bus_unown_name(g_owner_id);
246 int _bt_hps_register_interface(void)
248 GError *error = NULL;
253 hps_node_info = g_dbus_node_info_new_for_xml(hps_introspection_xml, &error);
254 if (!hps_node_info) {
255 BT_ERR("Failed to install: %s", error->message);
256 return BLUETOOTH_ERROR_INTERNAL;
259 owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
261 G_BUS_NAME_OWNER_FLAGS_NONE,
262 _bt_hps_on_bus_acquired, _bt_hps_on_name_acquired, _bt_hps_on_name_lost,
264 g_owner_id = owner_id;
265 BT_DBG("owner_id is [%d]\n", owner_id);
267 return BLUETOOTH_ERROR_NONE;
270 void _bt_hps_unregister_interface(void)
274 g_object_unref(g_conn);
276 g_dbus_node_info_unref(hps_node_info);
277 g_bus_unown_name(g_owner_id);
283 static struct hps_char_info *hps_get_char_value(const char *path)
287 for (tmp = hps_char_list; tmp != NULL; tmp = tmp->next) {
289 struct hps_char_info *char_info = tmp->data;
290 if (!g_strcmp0(char_info->char_path, path))
297 static int char_info_cmp(gconstpointer a1, gconstpointer a2)
299 const struct hps_char_info *attrib1 = a1;
300 const struct hps_char_info *attrib2 = a2;
302 return g_strcmp0(attrib1->char_path, attrib2->char_path);
305 static int notify_info_cmp(gconstpointer a1, gconstpointer a2)
307 const struct hps_notify_read_info *attrib1 = a1;
308 const struct hps_notify_read_info *attrib2 = a2;
310 return g_strcmp0(attrib1->char_path, attrib2->char_path);
313 static void _bt_hps_set_char_value(const char *obj_path, const char* value, int value_length, int offset)
319 for (tmp = hps_char_list; tmp != NULL; tmp = tmp->next) {
321 struct hps_char_info *char_info = tmp->data;
322 if (!g_strcmp0(char_info->char_path, obj_path)) {
325 str = g_strdup(char_info->char_value);
326 char_info->char_value = g_try_realloc(char_info->char_value, offset + value_length);
328 char_info->char_value = g_try_realloc(char_info->char_value, value_length);
330 if (char_info->char_value) {
332 memcpy(char_info->char_value, str, strlen(str));
335 memcpy(&char_info->char_value[offset], value, value_length);
336 char_info->value_length = offset + value_length;
338 memcpy(char_info->char_value, value, value_length);
339 char_info->value_length = value_length;
341 hps_char_list = g_slist_insert_sorted(hps_char_list,
342 char_info, char_info_cmp);
352 static void _bt_hps_set_notify_read_status(const char *obj_path,
353 guint offset_status, guint read_status, int https_status)
355 struct hps_notify_read_info *notify_read_info = NULL;
358 for (tmp = hps_notify_read_list; tmp != NULL; tmp = tmp->next) {
360 notify_read_info = tmp->data;
361 if (!g_strcmp0(notify_read_info->char_path, obj_path)) {
362 notify_read_info->read_status = read_status;
363 notify_read_info->offset_status = offset_status;
364 notify_read_info->https_status = https_status;
365 hps_notify_read_list = g_slist_insert_sorted(hps_notify_read_list,
366 notify_read_info, notify_info_cmp);
372 if (!hps_notify_read_list) {
373 /* Store Notification information */
374 notify_read_info = g_new0(struct hps_notify_read_info, 1);
375 if (notify_read_info) {
376 notify_read_info->char_path = g_strdup(obj_path);
377 notify_read_info->read_status = read_status;
378 notify_read_info->offset_status = offset_status;
379 notify_read_info->https_status = https_status;
380 hps_notify_read_list = g_slist_append(hps_notify_read_list, notify_read_info);
385 /* Store Notification information */
386 notify_read_info = g_new0(struct hps_notify_read_info, 1);
387 if (notify_read_info) {
388 notify_read_info->char_path = g_strdup(obj_path);
389 notify_read_info->read_status = read_status;
390 notify_read_info->offset_status = offset_status;
391 notify_read_info->https_status = https_status;
392 hps_notify_read_list = g_slist_append(hps_notify_read_list, notify_read_info);
396 static struct hps_notify_read_info *_bt_hps_get_notify_read_status(const char *obj_path)
400 for (tmp = hps_notify_read_list; tmp != NULL; tmp = tmp->next) {
402 struct hps_notify_read_info *notify_read_info = tmp->data;
403 if (!g_strcmp0(notify_read_info->char_path, obj_path))
404 return notify_read_info;
411 static void delete_all_characterisitc(void)
414 for (tmp = hps_char_list; tmp != NULL; tmp = tmp->next) {
416 struct hps_char_info *char_info = tmp->data;
417 if (char_info->char_path)
418 g_free(char_info->char_path);
419 if (char_info->char_value)
420 g_free(char_info->char_value);
421 hps_char_list = g_slist_delete_link(hps_char_list, tmp->data);
424 g_slist_free(hps_char_list);
425 hps_char_list = NULL;
428 static void delete_all_notify_read_status(void)
431 for (tmp = hps_notify_read_list; tmp != NULL; tmp = tmp->next) {
433 struct hps_notify_read_info *notify_read_info = tmp->data;
434 if (notify_read_info->char_path)
435 g_free(notify_read_info->char_path);
436 hps_notify_read_list = g_slist_delete_link(hps_notify_read_list, tmp->data);
439 g_slist_free(hps_notify_read_list);
440 hps_notify_read_list = NULL;
443 static void delete_notify_read_status(const char *obj_path)
446 for (tmp = hps_notify_read_list; tmp != NULL; tmp = tmp->next) {
448 struct hps_notify_read_info *notify_read_info = tmp->data;
449 if (!g_strcmp0(notify_read_info->char_path, obj_path)) {
450 if (notify_read_info->char_path)
451 g_free(notify_read_info->char_path);
452 hps_notify_read_list = g_slist_delete_link(hps_notify_read_list, tmp->data);
460 int _bt_hps_uri_write_cb(char *uri, int len, int offset)
462 if ((len < 1) || (len > MAX_URI_LENGTH)) {
463 BT_ERR("Wrong URI length %d", len);
464 return BLUETOOTH_ERROR_INTERNAL;
467 /* g_uri will be used commonly for all HTTP methods whereever applicable */
470 g_uri = g_strndup(uri, len);
472 _bt_hps_set_char_value(http_uri_obj_path, g_uri, len, offset);
474 return BLUETOOTH_ERROR_NONE;
477 int _bt_hps_http_header_write_cb(char *header, int len, int offset)
479 if ((len < 1) || (len > MAX_HEADER_LENGTH)) {
480 BT_ERR("Wrong Header length %d", len);
481 return BLUETOOTH_ERROR_INTERNAL;
484 /* g_header will be used commonly for all HTTP methods where ever applicable
485 general-header, request-header, entity-header
489 g_header = g_strndup(header, len);
491 _bt_hps_set_char_value(http_hdr_obj_path, g_header, len, offset);
494 return BLUETOOTH_ERROR_NONE;
497 int _bt_hps_entity_body_write_cb(char *entity, int len, int offset)
499 if ((len < 1) || (len > MAX_ENTITY_LENGTH)) {
500 BT_ERR("Wrong Entity length %d", len);
501 return BLUETOOTH_ERROR_INTERNAL;
504 /* g_entity will be used commonly for all HTTP methods whereever applicable */
507 g_entity = g_strndup(entity, len);
509 _bt_hps_set_char_value(http_entity_obj_path, g_entity, len, offset);
512 return BLUETOOTH_ERROR_NONE;
516 int _bt_hps_read_cb(const char *obj_path, char **value, int *len)
518 struct hps_char_info *info = NULL;
519 struct hps_notify_read_info *notify_read_info = NULL;
520 guint data_status = -1;
522 gboolean is_header = FALSE;
525 BT_ERR("Wrong Obj path");
529 if (!g_strcmp0(http_hdr_obj_path, obj_path))
532 info = hps_get_char_value(obj_path);
535 if (info->char_value == NULL || info->value_length == 0)
538 notify_read_info = _bt_hps_get_notify_read_status(obj_path);
539 if (notify_read_info && notify_read_info->read_status != DS_BODY_RECEIVED &&
540 notify_read_info->read_status != DS_HEADER_RECEIVED) {
541 offset = notify_read_info->offset_status;
542 if ((info->value_length - offset) > 0 &&
543 (info->value_length - offset) > MAX_ENTITY_LENGTH) {
545 data_status = DS_HEADER_TRUNCATED;
547 data_status = DS_BODY_TRUNCATED;
548 _bt_hps_set_notify_read_status(obj_path, offset + MAX_ENTITY_LENGTH,
549 data_status, notify_read_info->https_status);
550 *value = g_strdup(&info->char_value[offset]);
551 *len = MAX_ENTITY_LENGTH;
552 } else if ((info->value_length - offset) > 0 &&
553 (info->value_length - offset) <= MAX_ENTITY_LENGTH) {
555 data_status = DS_HEADER_RECEIVED;
557 data_status = DS_BODY_RECEIVED;
558 _bt_hps_set_notify_read_status(obj_path, offset, data_status, notify_read_info->https_status);
559 *value = g_strdup(&info->char_value[offset]);
560 *len = info->value_length - offset;
562 } else if (notify_read_info && (notify_read_info->read_status == DS_BODY_RECEIVED ||
563 notify_read_info->read_status == DS_HEADER_RECEIVED)) {
565 data_status = DS_HEADER_RECEIVED;
567 data_status = DS_BODY_RECEIVED;
568 delete_notify_read_status(obj_path);
569 *value = g_strdup(&info->char_value[offset]);
570 *len = info->value_length;
578 void _bt_hps_head_response_cb(SoupSession *session,
579 SoupMessage *msg, gpointer user_data)
581 unsigned short http_status = 0x00;
583 unsigned char status[3] = {0x00};
585 const char *device_address = user_data;
586 bluetooth_device_address_t addr_hex = { {0,} };
587 unsigned char data_status = DS_NONE;
588 _hps_convert_address_to_hex(&addr_hex, device_address);
591 if (hps_soup_session != session) {
592 BT_ERR("Wrong Session");
597 BT_ERR("Wrong Message");
602 req_state = HTTP_REQ_STATE_EXECUTED;
604 http_status = msg->status_code;
606 // Process Header in Response Body
607 if (msg->response_headers) {
609 const char *content = NULL;
610 const char *length = NULL;
613 length = soup_message_headers_get_one(msg->request_headers,
615 // Check "Content-MD5" is the right name to get header content
616 content = soup_message_headers_get_one(msg->response_headers,
618 if (content == NULL || length == NULL) {
619 BT_ERR("Wrong Response Header");
620 _bt_hps_send_status_notification(http_status, data_status, &addr_hex);
624 hdr_len = soup_message_headers_get_content_length(msg->response_headers);
626 // Write Data to Header Characteristic
628 _bt_hps_set_char_value(http_hdr_obj_path, content, hdr_len, 0);
630 bluetooth_gatt_set_characteristic_value(http_hdr_obj_path, content, hdr_len);
632 // TODO : Handle Truncated Header
634 // Write Data to Status Code Characteristic
636 data_status = (hdr_len > MAX_ENTITY_LENGTH) ? DS_HEADER_TRUNCATED : DS_HEADER_RECEIVED;
638 if (data_status == DS_BODY_TRUNCATED && SOUP_STATUS_IS_SUCCESSFUL(http_status))
639 _bt_hps_set_notify_read_status(http_hdr_obj_path, 0, data_status, http_status);
641 _bt_hps_send_status_notification(http_status, data_status, &addr_hex);
643 status[0] = http_status & 0x0F;
644 status[1] = (http_status >> 8) & 0x0F;
645 status[2] = (hdr_len > MAX_HEADER_LENGTH) ? DS_HEADER_TRUNCATED : DS_HEADER_RECEIVED;
647 bluetooth_gatt_set_characteristic_value(http_status_obj_path, status, 3);
650 BT_ERR("HEAD Response is NULL");
651 _bt_hps_send_status_notification(http_status, data_status, &addr_hex);
657 void _bt_hps_http_response_cb(SoupSession *session,
658 SoupMessage *msg, gpointer user_data)
660 unsigned short http_status = 0x00;
662 unsigned char status[3] = {0x00};
664 const char *device_address = user_data;
665 bluetooth_device_address_t addr_hex = { {0,} };
666 unsigned char data_status = DS_NONE;
667 _hps_convert_address_to_hex(&addr_hex, device_address);
670 if (hps_soup_session != session) {
671 BT_ERR("Wrong Session");
676 BT_ERR("Wrong Message");
682 req_state = HTTP_REQ_STATE_EXECUTED;
684 http_status = msg->status_code;
686 // Write Data to Status Code Characteristic
688 status[0] = http_status & 0x0F;
689 status[1] = (http_status >> 8) & 0x0F;
690 status[2] = DS_HEADER_RECEIVED;
691 bluetooth_gatt_set_characteristic_value(http_status_obj_path, status, 3);
693 data_status = DS_HEADER_RECEIVED;
694 _bt_hps_send_status_notification(http_status, data_status, &addr_hex);
700 void _bt_hps_get_response_cb(SoupSession *session,
701 SoupMessage *msg, gpointer user_data)
703 SoupBuffer *body = NULL;
704 unsigned short http_status = 0x00;
706 unsigned char status[3] = {0x00};
708 const char *device_address = user_data;
709 bluetooth_device_address_t addr_hex = { {0,} };
710 unsigned char data_status = DS_NONE;
711 _hps_convert_address_to_hex(&addr_hex, device_address);
714 if (hps_soup_session != session) {
715 BT_ERR("Wrong Session");
720 BT_ERR("Wrong Message");
726 req_state = HTTP_REQ_STATE_EXECUTED;
728 http_status = msg->status_code;
730 // Process Entity Body in Response Message
731 if (msg->response_body) {
733 body = soup_message_body_flatten(msg->response_body);
735 BT_ERR("Wrong Response Body");
737 _bt_hps_send_status_notification(http_status, data_status, &addr_hex);
741 if (body->data == NULL || body->length <= 0) {
742 BT_ERR("Wrong Response");
743 soup_buffer_free(body);
745 _bt_hps_send_status_notification(http_status, data_status, &addr_hex);
749 // Write Data to Entity Body Characteristic
751 _bt_hps_set_char_value(http_entity_obj_path, body->data, body->length, 0);
753 bluetooth_gatt_set_characteristic_value(http_entity_obj_path, body->data, body->length);
755 // TODO : Handle Truncated Entiry Body
757 // Write Data to Status Code Characteristic
759 data_status = (body->length > MAX_ENTITY_LENGTH) ? DS_BODY_TRUNCATED : DS_BODY_RECEIVED;
761 if (data_status == DS_BODY_TRUNCATED && SOUP_STATUS_IS_SUCCESSFUL(http_status))
762 _bt_hps_set_notify_read_status(http_entity_obj_path, 0, data_status, http_status);
764 _bt_hps_send_status_notification(http_status, data_status, &addr_hex);
767 status[0] = http_status & 0x0F;
768 status[1] = (http_status >> 8) & 0x0F;
769 status[2] = (body->length > MAX_HEADER_LENGTH) ? DS_BODY_TRUNCATED : DS_BODY_TRUNCATED;
771 bluetooth_gatt_set_characteristic_value(http_status_obj_path, status, 3);
773 soup_buffer_free(body);
775 BT_ERR("GET Response Body is NULL");
777 _bt_hps_send_status_notification(http_status, data_status, &addr_hex);
781 // Process Header in Response Body
782 if (msg->response_headers) {
784 const char *content = NULL;
785 const char *length = NULL;
788 length = soup_message_headers_get_one(msg->request_headers,
790 // Check "Content-MD5" is the right name to get header content
791 content = soup_message_headers_get_one(msg->response_headers,
793 if (content == NULL || length == NULL) {
794 BT_ERR("Wrong Response Header");
795 data_status = DS_NONE;
796 _bt_hps_send_status_notification(http_status, data_status, &addr_hex);
800 hdr_len = soup_message_headers_get_content_length(msg->response_headers);
801 // Write Data to Header Characteristic
803 _bt_hps_set_char_value(http_hdr_obj_path, content, hdr_len, 0);
805 bluetooth_gatt_set_characteristic_value(http_hdr_obj_path, content, hdr_len);
807 // TODO : Handle Truncated Header
809 // Write Data to Status Code Characteristic
811 data_status = (hdr_len > MAX_HEADER_LENGTH) ? DS_HEADER_TRUNCATED : DS_HEADER_RECEIVED;
813 if (data_status == DS_HEADER_TRUNCATED && SOUP_STATUS_IS_SUCCESSFUL(http_status))
814 _bt_hps_set_notify_read_status(http_hdr_obj_path, 0, data_status, http_status);
816 _bt_hps_send_status_notification(http_status, data_status, &addr_hex);
818 status[0] = http_status & 0x0F;
819 status[1] = (http_status >> 8) & 0x0F;
820 status[2] = (hdr_len > MAX_HEADER_LENGTH) ? DS_HEADER_TRUNCATED : DS_HEADER_RECEIVED;
822 bluetooth_gatt_set_characteristic_value(http_status_obj_path, status, 3);
825 BT_ERR("GET Response Header is NULL");
827 _bt_hps_send_status_notification(http_status, data_status, &addr_hex);
835 int _bt_hps_control_point_write_cb(const char *value, int len, char *addr)
837 int _bt_hps_control_point_write_cb(char *value, int len)
841 GTlsCertificate *cert = NULL;
842 GTlsCertificateFlags flags;
843 gboolean https_status = FALSE;
844 int result = BLUETOOTH_ERROR_NONE;
845 BT_INFO("Opcode %0x", opcode);
848 _bt_hps_set_char_value(http_cp_obj_path, value, len, 0);
852 case HTTP_GET_REQUEST:
853 if (req_state == HTTP_REQ_STATE_EXECUTED) {
854 req_state = HTTP_REQ_STATE_INPROGRESS;
855 hps_soup_msg = soup_message_new("GET", g_uri);
857 g_object_ref(hps_soup_msg);
858 soup_session_queue_message(hps_soup_session, hps_soup_msg, _bt_hps_get_response_cb, addr);
860 soup_session_queue_message(hps_soup_session, hps_soup_msg, _bt_hps_get_response_cb, NULL);
863 BT_ERR("HTTP GET request in progress, message dropped");
864 result = BLUETOOTH_ERROR_INTERNAL;
868 case HTTP_POST_REQUEST:
869 if (req_state == HTTP_REQ_STATE_EXECUTED) {
870 req_state = HTTP_REQ_STATE_INPROGRESS;
871 hps_soup_msg = soup_message_new("POST", g_uri);
872 if (hps_soup_msg == NULL || g_entity == NULL) {
873 BT_ERR("Soup Message NULL");
874 result = BLUETOOTH_ERROR_INTERNAL;
875 req_state = HTTP_REQ_STATE_EXECUTED;
878 soup_message_set_request(hps_soup_msg, "text/xml", SOUP_MEMORY_COPY,
879 g_entity, strlen(g_entity));
881 g_object_ref(hps_soup_msg);
882 soup_session_queue_message(hps_soup_session, hps_soup_msg, _bt_hps_http_response_cb, addr);
884 soup_session_queue_message(hps_soup_session, hps_soup_msg, _bt_hps_http_response_cb, NULL);
887 BT_ERR("HTTP POST request in progress, message dropped");
888 result = BLUETOOTH_ERROR_INTERNAL;
892 case HTTP_HEAD_REQUEST:
893 if (req_state == HTTP_REQ_STATE_EXECUTED) {
894 req_state = HTTP_REQ_STATE_INPROGRESS;
895 hps_soup_msg = soup_message_new("HEAD", g_uri);
896 if (hps_soup_msg == NULL) {
897 BT_ERR("Soup Message NULL");
898 result = BLUETOOTH_ERROR_INTERNAL;
899 req_state = HTTP_REQ_STATE_EXECUTED;
903 g_object_ref(hps_soup_msg);
904 soup_session_queue_message(hps_soup_session, hps_soup_msg, _bt_hps_head_response_cb, addr);
906 soup_session_queue_message(hps_soup_session, hps_soup_msg, _bt_hps_head_response_cb, NULL);
909 BT_ERR("HTTP HEAD request in progress, message dropped");
910 result = BLUETOOTH_ERROR_INTERNAL;
914 case HTTP_PUT_REQUEST:
915 if (req_state == HTTP_REQ_STATE_EXECUTED) {
917 req_state = HTTP_REQ_STATE_INPROGRESS;
918 hps_soup_msg = soup_message_new("PUT", g_uri);
919 if (hps_soup_msg == NULL || g_entity == NULL) {
920 BT_ERR("Soup Message NULL");
921 result = BLUETOOTH_ERROR_INTERNAL;
922 req_state = HTTP_REQ_STATE_EXECUTED;
925 buf = soup_buffer_new(SOUP_MEMORY_TAKE, g_entity, strlen(g_entity));
926 soup_message_body_append_buffer(hps_soup_msg->request_body, buf);
927 soup_message_body_set_accumulate(hps_soup_msg->request_body, FALSE);
929 g_object_ref(hps_soup_msg);
930 soup_session_queue_message(hps_soup_session, hps_soup_msg, _bt_hps_http_response_cb, addr);
932 soup_session_queue_message(hps_soup_session, hps_soup_msg, _bt_hps_http_response_cb, NULL);
936 BT_ERR("HTTP PUT request in progress, message dropped");
937 result = BLUETOOTH_ERROR_INTERNAL;
941 case HTTP_DELETE_REQUEST:
942 if (req_state == HTTP_REQ_STATE_EXECUTED) {
943 req_state = HTTP_REQ_STATE_INPROGRESS;
944 hps_soup_msg = soup_message_new("DELETE", g_uri);
945 if (hps_soup_msg == NULL) {
946 BT_ERR("Soup Message NULL");
947 result = BLUETOOTH_ERROR_INTERNAL;
948 req_state = HTTP_REQ_STATE_EXECUTED;
952 g_object_ref(hps_soup_msg);
953 soup_session_queue_message(hps_soup_session, hps_soup_msg, _bt_hps_http_response_cb, addr);
955 soup_session_queue_message(hps_soup_session, hps_soup_msg, _bt_hps_http_response_cb, NULL);
958 BT_ERR("HTTP DELETE request in progress, message dropped");
959 result = BLUETOOTH_ERROR_INTERNAL;
963 case HTTPS_GET_REQUEST:
964 if (req_state == HTTP_REQ_STATE_EXECUTED) {
965 req_state = HTTP_REQ_STATE_INPROGRESS;
966 hps_soup_msg = soup_message_new("GET", g_uri);
967 if (hps_soup_msg == NULL) {
968 BT_ERR("Soup Message NULL");
969 result = BLUETOOTH_ERROR_INTERNAL;
970 req_state = HTTP_REQ_STATE_EXECUTED;
974 g_object_ref(hps_soup_msg);
975 soup_session_queue_message(hps_soup_session, hps_soup_msg, _bt_hps_get_response_cb, addr);
977 soup_session_queue_message(hps_soup_session, hps_soup_msg, _bt_hps_get_response_cb, NULL);
979 https_status = soup_message_get_https_status(hps_soup_msg, &cert, &flags);
981 _bt_hps_set_char_value(http_security_obj_path, (const char *)&https_status, 1, 0);
983 bluetooth_gatt_set_characteristic_value(http_security_obj_path, (char *)&https_status, 1);
986 BT_ERR("HTTPS GET request in progress, message dropped");
987 result = BLUETOOTH_ERROR_INTERNAL;
991 case HTTPS_HEAD_REQUEST:
992 if (req_state == HTTP_REQ_STATE_EXECUTED) {
993 req_state = HTTP_REQ_STATE_INPROGRESS;
994 hps_soup_msg = soup_message_new("HEAD", g_uri);
995 if (hps_soup_msg == NULL) {
996 BT_ERR("Soup Message NULL");
997 result = BLUETOOTH_ERROR_INTERNAL;
998 req_state = HTTP_REQ_STATE_EXECUTED;
1002 g_object_ref(hps_soup_msg);
1003 soup_session_queue_message(hps_soup_session, hps_soup_msg, _bt_hps_head_response_cb, addr);
1005 soup_session_queue_message(hps_soup_session, hps_soup_msg, _bt_hps_head_response_cb, NULL);
1007 https_status = soup_message_get_https_status(hps_soup_msg, &cert, &flags);
1009 _bt_hps_set_char_value(http_security_obj_path, (const char *)&https_status, 1, 0);
1011 bluetooth_gatt_set_characteristic_value(http_security_obj_path, (char *)&https_status, 1);
1014 BT_ERR("HTTPS HEAD request in progress, message dropped");
1015 result = BLUETOOTH_ERROR_INTERNAL;
1019 case HTTPS_POST_REQUEST:
1020 if (req_state == HTTP_REQ_STATE_EXECUTED) {
1021 req_state = HTTP_REQ_STATE_INPROGRESS;
1022 hps_soup_msg = soup_message_new("POST", g_uri);
1023 if (hps_soup_msg == NULL) {
1024 BT_ERR("Soup Message NULL");
1025 result = BLUETOOTH_ERROR_INTERNAL;
1026 req_state = HTTP_REQ_STATE_EXECUTED;
1029 soup_message_set_request(hps_soup_msg, "text/xml", SOUP_MEMORY_STATIC,
1030 g_entity, strlen(g_entity));
1032 g_object_ref(hps_soup_msg);
1033 soup_session_queue_message(hps_soup_session, hps_soup_msg, _bt_hps_http_response_cb, addr);
1035 soup_session_queue_message(hps_soup_session, hps_soup_msg, _bt_hps_http_response_cb, NULL);
1038 https_status = soup_message_get_https_status(hps_soup_msg, &cert, &flags);
1040 _bt_hps_set_char_value(http_security_obj_path, (const char *)&https_status, 1, 0);
1042 bluetooth_gatt_set_characteristic_value(http_security_obj_path, (char *)&https_status, 1);
1045 BT_ERR("HTTPS POST request in progress, message dropped");
1046 result = BLUETOOTH_ERROR_INTERNAL;
1050 case HTTPS_PUT_REQUEST:
1051 if (req_state == HTTP_REQ_STATE_EXECUTED) {
1053 req_state = HTTP_REQ_STATE_INPROGRESS;
1054 hps_soup_msg = soup_message_new("PUT", g_uri);
1055 if (hps_soup_msg == NULL) {
1056 BT_ERR("Soup Message NULL");
1057 result = BLUETOOTH_ERROR_INTERNAL;
1058 req_state = HTTP_REQ_STATE_EXECUTED;
1061 buf = soup_buffer_new(SOUP_MEMORY_TAKE, g_entity, strlen(g_entity));
1062 soup_message_body_append_buffer(hps_soup_msg->request_body, buf);
1063 soup_message_body_set_accumulate(hps_soup_msg->request_body, FALSE);
1065 g_object_ref(hps_soup_msg);
1066 soup_session_queue_message(hps_soup_session, hps_soup_msg, _bt_hps_http_response_cb, addr);
1068 soup_session_queue_message(hps_soup_session, hps_soup_msg, _bt_hps_http_response_cb, NULL);
1070 https_status = soup_message_get_https_status(hps_soup_msg, &cert, &flags);
1072 _bt_hps_set_char_value(http_security_obj_path, (const char *)&https_status, 1, 0);
1074 bluetooth_gatt_set_characteristic_value(http_security_obj_path, (char *)&https_status, 1);
1077 BT_ERR("HTTPS PUT request in progress, message dropped");
1078 result = BLUETOOTH_ERROR_INTERNAL;
1082 case HTTPS_DELETE_REQUEST:
1083 if (req_state == HTTP_REQ_STATE_EXECUTED) {
1084 req_state = HTTP_REQ_STATE_INPROGRESS;
1085 hps_soup_msg = soup_message_new("DELETE", g_uri);
1086 if (hps_soup_msg == NULL) {
1087 BT_ERR("Soup Message NULL");
1088 result = BLUETOOTH_ERROR_INTERNAL;
1089 req_state = HTTP_REQ_STATE_EXECUTED;
1093 g_object_ref(hps_soup_msg);
1094 soup_session_queue_message(hps_soup_session, hps_soup_msg, _bt_hps_http_response_cb, addr);
1096 soup_session_queue_message(hps_soup_session, hps_soup_msg, _bt_hps_http_response_cb, NULL);
1099 https_status = soup_message_get_https_status(hps_soup_msg, &cert, &flags);
1101 _bt_hps_set_char_value(http_security_obj_path, (const char *)&https_status, 1, 0);
1103 bluetooth_gatt_set_characteristic_value(http_security_obj_path, (char *)&https_status, 1);
1106 BT_ERR("HTTPS DELETE request in progress, message dropped");
1107 result = BLUETOOTH_ERROR_INTERNAL;
1111 case HTTP_REQUEST_CANCEL:
1112 /* Cancel the outstanding request */
1113 if (req_state == HTTP_REQ_STATE_INPROGRESS) {
1114 req_state = HTTP_REQ_STATE_IDLE;
1115 if (hps_soup_msg == NULL) {
1116 BT_ERR("Soup Message NULL");
1117 result = BLUETOOTH_ERROR_INTERNAL;
1118 req_state = HTTP_REQ_STATE_EXECUTED;
1121 soup_session_cancel_message(hps_soup_session, hps_soup_msg, SOUP_STATUS_CANCELLED);
1122 hps_soup_msg = NULL;
1127 BT_ERR("Unknown opcode %0x", opcode);
1128 result = BLUETOOTH_ERROR_INTERNAL;
1135 void _bt_hps_security_read_cb(char *value, int len)
1137 BT_INFO("HPS Client Read the value");
1142 void _bt_hps_gatt_char_property_changed_event(GVariant *msg,
1145 int result = BLUETOOTH_ERROR_NONE;
1146 GVariantIter value_iter;
1147 const char *property = NULL;
1148 const char * char_path = NULL;
1149 const char * svc_handle = NULL;
1150 GVariant *var = NULL;
1151 GVariant *val = NULL;
1152 g_variant_iter_init(&value_iter, msg);
1154 while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &var))) {
1156 if (property == NULL) {
1157 BT_ERR("Property NULL");
1161 if (!g_strcmp0(property, "WriteValue")) {
1163 BT_INFO("WriteValue");
1164 BT_INFO("Type '%s'\n", g_variant_get_type_string(var));
1171 g_variant_get(var, "(&s&s&syq@ay)", &char_path,
1172 &svc_handle, &addr, &req_id, &offset, &val);
1174 len = g_variant_get_size(val);
1176 BT_DBG("Len = %d, Offset = %d", len, offset);
1178 value = (char *) g_variant_get_data(val);
1181 if (!g_strcmp0(char_path, http_uri_obj_path)) {
1183 result = _bt_hps_uri_write_cb(value, len, offset);;
1184 } else if (!g_strcmp0(char_path, http_hdr_obj_path)) {
1185 /* Retrive HEADER */
1186 result = _bt_hps_http_header_write_cb(value, len, offset);
1187 } else if (!g_strcmp0(char_path, http_entity_obj_path)) {
1188 /* Retrive ENTITY BODY */
1189 result = _bt_hps_entity_body_write_cb(value, len, offset);
1190 } else if (!g_strcmp0(char_path, http_cp_obj_path)) {
1191 result = _bt_hps_control_point_write_cb(value, len, addr);
1193 BT_ERR("Wrong Object Path %s", char_path);
1194 result = BLUETOOTH_ERROR_INTERNAL;
1196 bluetooth_gatt_send_response(req_id, BLUETOOTH_GATT_ATT_REQUEST_TYPE_WRITE, result, 0, NULL, 0);
1198 BT_ERR("Array Len 0");
1201 BT_ERR("var==NULL");
1203 } else if (!g_strcmp0(property, "ReadValue")) {
1209 int data_status = -1;
1210 BT_INFO("ReadValue");
1211 BT_INFO("Type '%s'\n", g_variant_get_type_string(var));
1213 g_variant_get(var, "(&s&s&syq)", &char_path, &svc_handle,
1214 &addr, &req_id, &offset);
1216 data_status = _bt_hps_read_cb(char_path, &value, &len);
1217 if (data_status >= DS_NONE) {
1218 struct hps_notify_read_info *notify_read_info = NULL;
1219 bluetooth_device_address_t addr_hex = { {0,} };
1220 _hps_convert_address_to_hex(&addr_hex, addr);
1221 bluetooth_gatt_send_response(req_id, BLUETOOTH_GATT_ATT_REQUEST_TYPE_READ,
1222 BLUETOOTH_ERROR_NONE, offset, value, len);
1223 notify_read_info = _bt_hps_get_notify_read_status(char_path);
1224 if (notify_read_info) {
1225 _bt_hps_send_status_notification(notify_read_info->https_status,
1226 data_status, &addr_hex);
1228 if (data_status == DS_BODY_RECEIVED ||
1229 data_status == DS_HEADER_RECEIVED) {
1230 _bt_hps_set_char_value(char_path, NULL, 0, 0);
1236 BT_ERR("ReadValue failed %s", char_path);
1237 bluetooth_gatt_send_response(req_id, BLUETOOTH_GATT_ATT_REQUEST_TYPE_READ,
1238 BLUETOOTH_ERROR_INTERNAL, offset, NULL, 0);
1245 void _bt_hps_gatt_char_property_changed_event(GVariant *msg,
1248 GVariantIter value_iter;
1249 char *property = NULL;
1250 char * char_handle = NULL;
1251 GVariant *val = NULL;
1252 int result = BLUETOOTH_ERROR_NONE;
1253 GVariant *param = NULL;
1254 g_variant_iter_init(&value_iter, msg);
1255 char_handle = g_strdup(path);
1257 while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &val))) {
1259 if (property == NULL) {
1260 BT_ERR("Property NULL");
1264 if (strcasecmp(property, "ChangedValue") == 0) {
1267 GByteArray *gp_byte_array = NULL;
1268 BT_INFO("Type '%s'\n", g_variant_get_type_string(val));
1271 gp_byte_array = g_byte_array_new();
1272 len = g_variant_get_size(val);
1273 BT_DBG("Len = %d", len);
1274 g_byte_array_append(gp_byte_array,
1275 (const guint8 *)g_variant_get_data(val), len);
1276 if (gp_byte_array->len != 0) {
1277 GVariant *byte_array = NULL;
1278 byte_array = g_variant_new_from_data(
1279 G_VARIANT_TYPE_BYTESTRING,
1280 gp_byte_array->data,
1283 param = g_variant_new("(is@ay)", result, char_handle,
1286 if (strcmp(path, http_uri_obj_path)) {
1288 _bt_hps_uri_write_cb(NULL, len);
1289 } else if (strcmp(path, http_hdr_obj_path)) {
1291 _bt_hps_http_header_write_cb(NULL, len);
1292 } else if (strcmp(path, http_entity_obj_path)) {
1293 //Retrive ENTITY BODY
1294 _bt_hps_entity_body_write_cb(NULL, len);
1295 } else if (strcmp(path, http_cp_obj_path)) {
1296 _bt_hps_control_point_write_cb(NULL, len);
1297 } else if (strcmp(path, http_security_obj_path)) {
1298 _bt_hps_security_read_cb(NULL, len);
1300 BT_ERR("Wrong Object Path %s", path);
1303 BT_ERR("Array Len 0");
1305 g_byte_array_free(gp_byte_array, TRUE);
1307 BT_ERR("val==NULL");
1311 g_free(char_handle);
1317 void _bt_hps_property_event_filter(GDBusConnection *connection,
1318 const gchar *sender_name,
1319 const gchar *object_path,
1320 const gchar *interface_name,
1321 const gchar *signal_name,
1322 GVariant *parameters,
1327 if (signal_name == NULL) {
1328 BT_ERR("Wrong Signal");
1333 if (g_strcmp0(signal_name, PROPERTIES_CHANGED) == 0) {
1335 g_variant_get(parameters, "(@a{sv}@as)", &value, NULL);
1337 _bt_hps_gatt_char_property_changed_event(value, object_path);
1339 if (g_strcmp0(interface_name, BT_HPS_PROPERTIES_INTERFACE) == 0) {
1341 g_variant_get(parameters, "(&s@a{sv}@as)", &interface_name, &value, NULL);
1343 _bt_hps_gatt_char_property_changed_event(value, object_path);
1346 //BT_ERR("Wrong Interface %s", interface_name);
1353 void _bt_hps_adapter_event_filter(GDBusConnection *connection,
1354 const gchar *sender_name,
1355 const gchar *object_path,
1356 const gchar *interface_name,
1357 const gchar *signal_name,
1358 GVariant *parameters,
1361 int result = BLUETOOTH_ERROR_NONE;
1364 if (signal_name == NULL) {
1365 BT_ERR("Wrong Signal");
1369 BT_INFO("Interface %s, Signal %s", interface_name, signal_name);
1371 if (g_strcmp0(interface_name, BT_HPS_INTERFACE_NAME) == 0) {
1373 g_variant_get(parameters, "(&s@a{sv}@as)", &interface_name, &value, NULL);
1375 if (strcasecmp(signal_name, BLE_ENABLED) == 0) {
1376 g_variant_get(parameters, "(i)", &result);
1378 if (_bt_hps_prepare_httpproxy() != BLUETOOTH_ERROR_NONE) {
1379 BT_ERR("Fail to prepare HTTP Proxy");
1383 if (_bt_hps_set_advertising_data() != BLUETOOTH_ERROR_NONE) {
1384 BT_ERR("Fail to set advertising data");
1389 BT_ERR("Wrong Signal %s", signal_name);
1396 int _bt_hps_init_event_receiver()
1398 GError *error = NULL;
1403 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
1404 if (error != NULL) {
1405 BT_ERR("ERROR: Can't get on system bus [%s]", error->message);
1406 g_clear_error(&error);
1410 property_sub_id = g_dbus_connection_signal_subscribe(conn,
1411 NULL, BT_HPS_INTERFACE_NAME,
1412 PROPERTIES_CHANGED, BT_HPS_OBJECT_PATH, NULL, 0,
1413 _bt_hps_property_event_filter,
1416 adapter_sub_id = g_dbus_connection_signal_subscribe(conn,
1417 NULL, BT_HPS_INTERFACE_NAME,
1418 BLE_ENABLED, BT_HPS_OBJECT_PATH, NULL, 0,
1419 _bt_hps_adapter_event_filter,
1425 void _bt_hps_deinit_event_receiver(void)
1428 g_dbus_connection_signal_unsubscribe(conn, property_sub_id);
1429 g_dbus_connection_signal_unsubscribe(conn, adapter_sub_id);
1434 int _bt_hps_set_advertising_data(void)
1439 guint8 data[4] = {0x03, 0x02, 0x23, 0x18};
1440 bluetooth_advertising_data_t adv;
1442 BT_DBG("%x %x %x %x", data[0], data[1], data[2], data[3]);
1443 memcpy(adv.data, data, sizeof(data));
1444 ret = bluetooth_set_advertising_data(0, &adv, sizeof(data));
1445 if (ret != BLUETOOTH_ERROR_NONE) {
1446 BT_ERR("Failed to set ADV data %d", ret);
1450 ret = bluetooth_set_advertising(0, TRUE);
1451 if (ret != BLUETOOTH_ERROR_NONE) {
1452 BT_ERR("Failed to set ADV %d", ret);
1459 int _bt_hps_prepare_httpproxy(void)
1461 int ret = BLUETOOTH_ERROR_NONE;
1465 bt_gatt_characteristic_property_t props;
1467 char value[MAX_URI_LENGTH] = { 0 };
1468 struct hps_char_info *char_info = NULL;
1470 char status[3] = { 0 };
1475 ret = bluetooth_gatt_init();
1476 if (ret != BLUETOOTH_ERROR_NONE) {
1477 BT_ERR("Failed to Init GATT %d", ret);
1481 service_uuid = __hps_convert_uuid_to_uuid128(HPS_UUID);
1482 ret = bluetooth_gatt_add_service(service_uuid, &hps_obj_path);
1483 if (ret != BLUETOOTH_ERROR_NONE) {
1484 BT_ERR("Failed to add service %d", ret);
1488 /* Characteristic URI */
1489 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE;
1490 char_uuid = __hps_convert_uuid_to_uuid128(HTTP_URI_UUID);
1491 ret = bluetooth_gatt_add_new_characteristic(hps_obj_path, char_uuid, 0, props, &http_uri_obj_path);
1492 if (ret != BLUETOOTH_ERROR_NONE) {
1493 BT_ERR("Failed to add new char %d", ret);
1498 ret = bluetooth_gatt_set_characteristic_value(http_uri_obj_path, value, MAX_URI_LENGTH);
1499 if (ret != BLUETOOTH_ERROR_NONE) {
1500 BT_ERR("Failed to add new char %d", ret);
1504 /* Store requets information */
1505 char_info = g_new0(struct hps_char_info, 1);
1506 char_info->char_path = g_strdup(http_uri_obj_path);
1507 hps_char_list = g_slist_append(hps_char_list, char_info);
1508 _bt_hps_set_char_value(http_uri_obj_path, value, MAX_URI_LENGTH, 0);
1511 /* Characteristic HTTP Headers */
1512 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ |
1513 BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE;
1514 char_uuid = __hps_convert_uuid_to_uuid128(HTTP_HDR_UUID);
1515 ret = bluetooth_gatt_add_new_characteristic(hps_obj_path, char_uuid, 0, props, &http_hdr_obj_path);
1516 if (ret != BLUETOOTH_ERROR_NONE) {
1517 BT_ERR("Failed to add new char %d", ret);
1521 ret = bluetooth_gatt_set_characteristic_value(http_hdr_obj_path, value, MAX_HEADER_LENGTH);
1522 if (ret != BLUETOOTH_ERROR_NONE) {
1523 BT_ERR("Failed to add new char %d", ret);
1527 /* Store Characterisitc information */
1528 char_info = g_new0(struct hps_char_info, 1);
1529 char_info->char_path = g_strdup(http_hdr_obj_path);
1530 hps_char_list = g_slist_append(hps_char_list, char_info);
1531 _bt_hps_set_char_value(http_hdr_obj_path, value, MAX_HEADER_LENGTH, 0);
1534 /* Characteristic HTTP Entity Body */
1535 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ |
1536 BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE;
1537 char_uuid = __hps_convert_uuid_to_uuid128(HTTP_ENTITY_UUID);
1538 ret = bluetooth_gatt_add_new_characteristic(hps_obj_path, char_uuid, 0, props, &http_entity_obj_path);
1539 if (ret != BLUETOOTH_ERROR_NONE) {
1540 BT_ERR("Failed to add new char %d", ret);
1544 ret = bluetooth_gatt_set_characteristic_value(http_entity_obj_path, value, MAX_ENTITY_LENGTH);
1545 if (ret != BLUETOOTH_ERROR_NONE) {
1546 BT_ERR("Failed to add new char %d", ret);
1550 /* Store Characterisitc information */
1551 char_info = g_new0(struct hps_char_info, 1);
1552 char_info->char_path = g_strdup(http_entity_obj_path);
1553 hps_char_list = g_slist_append(hps_char_list, char_info);
1554 _bt_hps_set_char_value(http_entity_obj_path, value, MAX_ENTITY_LENGTH, 0);
1557 /* Characteristic HTTP Control Point */
1558 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE;
1559 char_uuid = __hps_convert_uuid_to_uuid128(HTTP_CP_UUID);
1560 ret = bluetooth_gatt_add_new_characteristic(hps_obj_path, char_uuid, 0, props, &http_cp_obj_path);
1561 if (ret != BLUETOOTH_ERROR_NONE) {
1562 BT_ERR("Failed to add new char %d", ret);
1566 ret = bluetooth_gatt_set_characteristic_value(http_cp_obj_path, &cp, 1);
1567 if (ret != BLUETOOTH_ERROR_NONE) {
1568 BT_ERR("Failed to add new char %d", ret);
1572 /* Store Characterisitc information */
1573 char_info = g_new0(struct hps_char_info, 1);
1574 char_info->char_path = g_strdup(http_cp_obj_path);
1575 hps_char_list = g_slist_append(hps_char_list, char_info);
1576 _bt_hps_set_char_value(http_cp_obj_path, &cp, 1, 0);
1579 /* Characteristic HTTP Status Code */
1580 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_NOTIFY;
1581 char_uuid = __hps_convert_uuid_to_uuid128(HTTP_STATUS_UUID);
1582 ret = bluetooth_gatt_add_new_characteristic(hps_obj_path, char_uuid, 0, props, &http_status_obj_path);
1583 if (ret != BLUETOOTH_ERROR_NONE) {
1584 BT_ERR("Failed to add new char %d", ret);
1588 ret = bluetooth_gatt_set_characteristic_value(http_status_obj_path, status, 3);
1589 if (ret != BLUETOOTH_ERROR_NONE) {
1590 BT_ERR("Failed to add new char %d", ret);
1594 desc_uuid = __hps_convert_uuid_to_uuid128(HTTP_STATUS_CCC_DESC_UUID);
1595 ret = bluetooth_gatt_add_descriptor(http_status_obj_path, desc_uuid,
1596 (BLUETOOTH_GATT_PERMISSION_READ | BLUETOOTH_GATT_PERMISSION_WRITE),
1597 &http_status_desc_obj_path);
1598 if (ret != BLUETOOTH_ERROR_NONE) {
1599 BT_ERR("Failed to add new char descriptor %d", ret);
1603 /* Store Characterisitc information */
1604 char_info = g_new0(struct hps_char_info, 1);
1605 char_info->char_path = g_strdup(http_status_obj_path);
1606 hps_char_list = g_slist_append(hps_char_list, char_info);
1607 _bt_hps_set_char_value(http_status_obj_path, status, 3, 0);
1610 /* Characteristic HTTPS Security */
1611 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ;
1612 char_uuid = __hps_convert_uuid_to_uuid128(HTTP_SECURITY_UUID);
1613 ret = bluetooth_gatt_add_new_characteristic(hps_obj_path, char_uuid, 0, props, &http_security_obj_path);
1614 if (ret != BLUETOOTH_ERROR_NONE) {
1615 BT_ERR("Failed to add new char %d", ret);
1619 ret = bluetooth_gatt_set_characteristic_value(http_security_obj_path, &cp, 1);
1620 if (ret != BLUETOOTH_ERROR_NONE) {
1621 BT_ERR("Failed to add new char %d", ret);
1625 /* Store Characterisitc information */
1626 char_info = g_new0(struct hps_char_info, 1);
1627 char_info->char_path = g_strdup(http_security_obj_path);
1628 hps_char_list = g_slist_append(hps_char_list, char_info);
1629 _bt_hps_set_char_value(http_security_obj_path, &cp, 1, 0);
1632 ret = bluetooth_gatt_register_service(hps_obj_path);
1633 if (ret != BLUETOOTH_ERROR_NONE) {
1634 BT_ERR("Failed to register service %d", ret);
1638 ret = bluetooth_gatt_register_application();
1639 if (ret != BLUETOOTH_ERROR_NONE) {
1640 BT_ERR("Failed to register application %d", ret);
1648 delete_all_characterisitc();
1649 delete_all_notify_read_status();
1655 static void _bt_hps_sig_handler(int sig)
1661 BT_DBG("caught signal - sigterm\n");
1664 BT_DBG("caught signal - sigint\n");
1667 BT_DBG("caught signal - sigkill\n");
1670 BT_DBG("caught signal %d and ignored\n", sig);
1675 void _bt_hps_exit(void)
1680 if (g_uri != NULL) {
1685 if (g_header != NULL) {
1690 if (g_entity != NULL) {
1695 soup_session_abort(hps_soup_session);
1696 g_assert_cmpint(G_OBJECT(hps_soup_session)->ref_count, ==, 1);
1697 g_object_unref(hps_soup_session);
1700 delete_all_characterisitc();
1703 ret = bluetooth_gatt_deinit();
1704 if (ret != BLUETOOTH_ERROR_NONE)
1705 BT_ERR("Failed to Deinit GATT %d", ret);
1707 ret = bluetooth_unregister_callback();
1708 if (ret != BLUETOOTH_ERROR_NONE)
1709 BT_ERR("Failed to Unregister callback %d", ret);
1711 _bt_hps_deinit_event_receiver();
1713 _bt_hps_unregister_interface();
1715 if (main_loop != NULL)
1716 g_main_loop_quit(main_loop);
1719 void bt_hps_event_callback(int event, bluetooth_event_param_t* param,
1722 BT_DBG("HPS event %d", event);
1726 /* HTTP Proxy Service Main loop */
1729 struct sigaction sa;
1731 BT_ERR("Starting the bt-httpproxy daemon");
1733 /* Values taken from http://www.browserscope.org/ following
1734 * the rule "Do What Every Other Modern Browser Is Doing". They seem
1735 * to significantly improve page loading time compared to soup's
1737 * Change MAX_CONNECTIONS_PER_HOST value 6 -> 12, and maxConnections is changed from 35 to 60.
1738 * Enhanced network loading speed apply tunning value. */
1739 static const int maxConnections = 60;
1740 static const int maxConnectionsPerHost = 12;
1742 memset(&sa, 0, sizeof(sa));
1743 sa.sa_handler = _bt_hps_sig_handler;
1744 sa.sa_flags = SA_SIGINFO;
1745 sigaction(SIGINT, &sa, NULL);
1746 sigaction(SIGTERM, &sa, NULL);
1747 sigaction(SIGKILL, &sa, NULL);
1750 if (bluetooth_register_callback(bt_hps_event_callback, NULL) != BLUETOOTH_ERROR_NONE) {
1751 BT_ERR("bluetooth_register_callback returned failiure");
1756 if (_bt_hps_register_interface() != BLUETOOTH_ERROR_NONE) {
1757 BT_ERR("Fail to register http proxy service");
1761 if (_bt_hps_init_event_receiver() != BLUETOOTH_ERROR_NONE) {
1762 BT_ERR("Fail to init event reciever");
1766 hps_soup_session = soup_session_async_new();
1767 if (hps_soup_session == NULL) {
1768 BT_ERR("Failed to soup_session_async_new");
1771 /* Set Soup Session Fetures */
1772 g_object_set(hps_soup_session,
1773 SOUP_SESSION_MAX_CONNS, maxConnections,
1774 SOUP_SESSION_MAX_CONNS_PER_HOST, maxConnectionsPerHost,
1775 SOUP_SESSION_ADD_FEATURE_BY_TYPE, SOUP_TYPE_CONTENT_DECODER,
1776 SOUP_SESSION_ADD_FEATURE_BY_TYPE, SOUP_TYPE_CONTENT_SNIFFER,
1777 SOUP_SESSION_ADD_FEATURE_BY_TYPE, SOUP_TYPE_PROXY_RESOLVER_DEFAULT,
1778 SOUP_SESSION_USE_THREAD_CONTEXT, TRUE,
1781 main_loop = g_main_loop_new(NULL, FALSE);
1783 g_main_loop_run(main_loop);
1785 BT_DBG("g_main_loop_quit called!");
1787 if (main_loop != NULL)
1788 g_main_loop_unref(main_loop);