2 * Copyright (c) 2017 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.
29 #include <gio/gunixfdlist.h>
32 #include "bt-otpserver.h"
33 #include "bluetooth-api.h"
37 #define LOG_TAG "BLUETOOTH_OTP"
39 #define BT_INFO(fmt, arg...) SLOGI(fmt, ##arg)
40 #define BT_ERR(fmt, arg...) SLOGE(fmt, ##arg)
41 #define BT_DBG(fmt, arg...) SLOGD(fmt, ##arg)
43 /* OTP object paths */
44 char *otp_obj_path = NULL;
45 char *otp_feature_obj_path = NULL;
46 char *otp_object_name_obj_path = NULL;
47 char *otp_object_type_obj_path = NULL;
48 char *otp_object_size_obj_path = NULL;
49 char *otp_object_first_created_obj_path = NULL;
50 char *otp_object_last_modified_obj_path = NULL;
51 char *otp_object_id_obj_path = NULL;
52 char *otp_object_prop_obj_path = NULL;
53 char *otp_oacp_obj_path = NULL;
54 char *otp_olcp_obj_path = NULL;
55 char *otp_oacp_desc_obj_path = NULL;
56 char *otp_olcp_desc_obj_path = NULL;
58 static GMainLoop *main_loop;
59 GDBusNodeInfo *otp_node_info = NULL;
60 static GDBusConnection *conn;
61 static GDBusConnection *g_conn;
63 static int property_sub_id = -1;
64 static int adapter_sub_id = -1;
65 static int device_sub_id = -1;
66 static guint g_owner_id = 0;
67 static guint server_watch_id = 0;
69 struct otp_char_info {
75 struct indicate_info {
83 struct object_metadata {
94 struct oacp_operation {
99 uint32_t length_sofar;
105 struct oacp_create_operation {
106 char *remote_address;
111 static struct object_metadata *selected_object = NULL;
112 static uint64_t object_id = OBJECT_START_ID;
113 static GSList *otp_object_list = NULL;
114 static GSList *otp_char_list = NULL;
115 static guint obj_curr_index;
116 static int adv_handle = 0;
117 static gboolean OACP_indicate = FALSE;
118 static gboolean OLCP_indicate = FALSE;
119 char *directory = NULL;
120 gboolean mutiple_obj_support = false;
121 static gboolean otc_connection_status = FALSE;
122 struct oacp_operation *oacp_op = NULL;
123 struct oacp_create_operation *oacp_create = NULL;
124 unsigned int timeout_id;
126 static const gchar otp_introspection_xml[] =
128 " <interface name='org.projectx.otp_service'>"
129 " <method name='enable'>"
130 " <arg type='s' name='directory'/>"
131 " <arg type='i' name='status' direction='out'/>"
133 " <method name='disable'>"
134 " <arg type='i' name='status' direction='out'/>"
136 " <method name='NewConnection'>"
137 " <arg type='o' name='object' direction='in'/>"
138 " <arg type='h' name='fd' direction='in'/>"
143 void _bt_otp_deinit_event_receiver(void);
144 void _bt_otp_unregister_interface(void);
145 void update_obj_metadata_charc_value(struct object_metadata *object);
146 void _bt_convert_device_path_to_address(const char *device_path,
147 char *device_address);
148 int _bt_otp_open_otc_and_listen(char *address, char *method);
150 static void delete_all_objects(void)
153 for (tmp = otp_object_list; tmp != NULL; tmp = tmp->next) {
155 struct object_metadata *obj_info = tmp->data;
157 g_free(obj_info->name);
159 g_free(obj_info->type);
160 otp_object_list = g_slist_delete_link(otp_object_list, tmp->data);
163 g_slist_free(otp_object_list);
164 otp_object_list = NULL;
167 static void delete_all_characterisitc(void)
170 for (tmp = otp_char_list; tmp != NULL; tmp = tmp->next) {
172 struct otp_char_info *char_info = tmp->data;
173 if (char_info->char_path)
174 g_free(char_info->char_path);
175 if (char_info->char_value)
176 g_free(char_info->char_value);
177 otp_char_list = g_slist_delete_link(otp_char_list, tmp->data);
180 g_slist_free(otp_char_list);
181 otp_char_list = NULL;
184 void _bt_otp_exit(void)
190 delete_all_characterisitc();
193 delete_all_objects();
195 ret = bluetooth_gatt_deinit();
196 if (ret != BLUETOOTH_ERROR_NONE)
197 BT_ERR("Failed to Deinit GATT %d", ret);
199 _bt_otp_deinit_event_receiver();
201 _bt_otp_unregister_interface();
203 /* TODO: Advertising is not getting stopped by this API.
204 * This is because OTP_SERVER_DEINIT dbus call is blocking
205 * BT_SET_ADVERTISING_DATA dbus call. But now advertisment
206 * is stopped because of terminated process logic.
208 ret = bluetooth_set_advertising(adv_handle, FALSE);
209 if (ret != BLUETOOTH_ERROR_NONE)
210 BT_ERR("Failed to stop ADV %d", ret);
212 if (main_loop != NULL) {
213 g_main_loop_quit(main_loop);
217 static void _bt_otp_set_char_value(const char *obj_path,
218 const char *value, int value_length)
224 for (tmp = otp_char_list; tmp != NULL; tmp = tmp->next) {
226 struct otp_char_info *char_info = tmp->data;
227 if (!g_strcmp0(char_info->char_path, obj_path)) {
228 char_info->char_value = g_try_realloc(char_info->char_value, value_length);
229 if (char_info->char_value) {
230 memcpy(char_info->char_value, value, value_length);
231 char_info->value_length = value_length;
240 int add_new_characteristic(const char *char_uuid, bt_gatt_permission_t perms,
241 bt_gatt_characteristic_property_t props, char **obj_path)
243 int ret = BLUETOOTH_ERROR_NONE;
244 struct otp_char_info *char_info = NULL;
246 ret = bluetooth_gatt_add_new_characteristic(otp_obj_path,
247 char_uuid, perms, props, obj_path);
248 if (ret != BLUETOOTH_ERROR_NONE) {
249 BT_ERR("Failed to add new char %d", ret);
253 char_info = g_new0(struct otp_char_info, 1);
254 char_info->char_path = g_strdup(*obj_path);
255 otp_char_list = g_slist_append(otp_char_list, char_info);
260 static char *_otp_convert_uuid_to_uuid128(const char *uuid)
270 uuid128 = g_strdup_printf("0000%s-0000-1000-8000-00805f9b34fb",
276 uuid128 = g_strdup_printf("%s-0000-1000-8000-00805f9b34fb",
282 uuid128 = strdup(uuid);
292 int _bt_otp_prepare_ots(void)
295 int ret = BLUETOOTH_ERROR_NONE;
299 bt_gatt_characteristic_property_t props;
300 bt_gatt_permission_t perms;
301 char supp_feat[OTP_FEATURE_LENGTH] = { 0x9C, 0x00, 0x00, 0x00,
302 0x80, 0x00, 0x00, 0x00 };
304 ret = bluetooth_gatt_init();
305 if (ret != BLUETOOTH_ERROR_NONE) {
306 BT_ERR("Failed to Init GATT %d", ret);
310 service_uuid = _otp_convert_uuid_to_uuid128(OTP_UUID);
311 ret = bluetooth_gatt_add_service(service_uuid, &otp_obj_path);
312 if (ret != BLUETOOTH_ERROR_NONE) {
313 BT_ERR("Failed to add service %d", ret);
317 /* Characteristic OTP Feature */
318 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ;
319 perms = BLUETOOTH_GATT_PERMISSION_READ;
320 char_uuid = _otp_convert_uuid_to_uuid128(OTP_FEATURE_UUID);
321 ret = add_new_characteristic(char_uuid, perms, props,
322 &otp_feature_obj_path);
323 if (ret != BLUETOOTH_ERROR_NONE)
326 ret = bluetooth_gatt_set_characteristic_value(otp_feature_obj_path,
327 supp_feat, OTP_FEATURE_LENGTH);
328 if (ret != BLUETOOTH_ERROR_NONE) {
329 BT_ERR("Failed to set char value %d", ret);
333 _bt_otp_set_char_value(otp_feature_obj_path, supp_feat,
336 /* Characteristic Object Name */
337 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ |
338 BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE;
339 perms = BLUETOOTH_GATT_PERMISSION_READ |
340 BLUETOOTH_GATT_PERMISSION_WRITE;
341 char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_NAME_UUID);
342 ret = add_new_characteristic(char_uuid, perms, props,
343 &otp_object_name_obj_path);
344 if (ret != BLUETOOTH_ERROR_NONE)
347 /* Characteristic Object Type */
348 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ;
349 perms = BLUETOOTH_GATT_PERMISSION_READ;
350 char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_TYPE_UUID);
351 ret = add_new_characteristic(char_uuid, perms, props,
352 &otp_object_type_obj_path);
353 if (ret != BLUETOOTH_ERROR_NONE)
356 /* Characteristic Object Size */
357 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ;
358 perms = BLUETOOTH_GATT_PERMISSION_READ;
359 char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_SIZE_UUID);
360 ret = add_new_characteristic(char_uuid, perms, props,
361 &otp_object_size_obj_path);
362 if (ret != BLUETOOTH_ERROR_NONE)
365 /* Characteristic Object First-Created */
366 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ |
367 BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE;
368 perms = BLUETOOTH_GATT_PERMISSION_READ |
369 BLUETOOTH_GATT_PERMISSION_WRITE;
370 char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_FIRST_CREATED_UUID);
371 ret = add_new_characteristic(char_uuid, perms, props,
372 &otp_object_first_created_obj_path);
373 if (ret != BLUETOOTH_ERROR_NONE)
376 /* Characteristic Object Last-Modified */
377 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ |
378 BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE;
379 perms = BLUETOOTH_GATT_PERMISSION_READ |
380 BLUETOOTH_GATT_PERMISSION_WRITE;
381 char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_LAST_MODIFIED_UUID);
382 ret = add_new_characteristic(char_uuid, perms, props,
383 &otp_object_last_modified_obj_path);
384 if (ret != BLUETOOTH_ERROR_NONE)
387 /* Object ID is mandatory for mutiple object server */
388 if (mutiple_obj_support) {
389 /* Characteristic Object ID */
390 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ;
391 perms = BLUETOOTH_GATT_PERMISSION_READ;
392 char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_ID_UUID);
393 ret = add_new_characteristic(char_uuid, perms, props,
394 &otp_object_id_obj_path);
395 if (ret != BLUETOOTH_ERROR_NONE)
399 /* Characteristic Object Properties */
400 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ;
401 perms = BLUETOOTH_GATT_PERMISSION_READ;
402 char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_PROP_UUID);
403 ret = add_new_characteristic(char_uuid, perms, props,
404 &otp_object_prop_obj_path);
405 if (ret != BLUETOOTH_ERROR_NONE)
408 /* Characteristic OACP */
409 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE |
410 BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_INDICATE;
411 perms = BLUETOOTH_GATT_PERMISSION_WRITE;
412 char_uuid = _otp_convert_uuid_to_uuid128(OTP_OACP_UUID);
413 ret = add_new_characteristic(char_uuid, perms, props,
415 if (ret != BLUETOOTH_ERROR_NONE)
419 desc_uuid = _otp_convert_uuid_to_uuid128(OTP_CP_CCC_DESC_UUID);
420 perms = BLUETOOTH_GATT_PERMISSION_READ |
421 BLUETOOTH_GATT_PERMISSION_WRITE;
422 ret = bluetooth_gatt_add_descriptor(otp_oacp_obj_path, desc_uuid,
423 perms, &otp_oacp_desc_obj_path);
424 if (ret != BLUETOOTH_ERROR_NONE) {
425 BT_ERR("Failed to add new char descriptor %d", ret);
429 /* OLCP Characteristics is not required
430 * for single object server
432 if (mutiple_obj_support) {
433 /* Characteristic OLCP */
434 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE |
435 BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_INDICATE;
436 perms = BLUETOOTH_GATT_PERMISSION_WRITE;
437 char_uuid = _otp_convert_uuid_to_uuid128(OTP_OLCP_UUID);
438 ret = add_new_characteristic(char_uuid, perms, props,
440 if (ret != BLUETOOTH_ERROR_NONE)
444 desc_uuid = _otp_convert_uuid_to_uuid128(OTP_CP_CCC_DESC_UUID);
445 perms = BLUETOOTH_GATT_PERMISSION_READ |
446 BLUETOOTH_GATT_PERMISSION_WRITE;
447 ret = bluetooth_gatt_add_descriptor(otp_olcp_obj_path, desc_uuid,
448 perms, &otp_olcp_desc_obj_path);
449 if (ret != BLUETOOTH_ERROR_NONE) {
450 BT_ERR("Failed to add new char descriptor %d", ret);
455 /* Register service */
456 ret = bluetooth_gatt_register_service(otp_obj_path);
457 if (ret != BLUETOOTH_ERROR_NONE) {
458 BT_ERR("Failed to register service %d", ret);
462 /* Register Application */
463 ret = bluetooth_gatt_register_application();
464 if (ret != BLUETOOTH_ERROR_NONE) {
465 BT_ERR("Failed to register application %d", ret);
473 delete_all_characterisitc();
477 int _bt_otp_set_advertising_data(void)
483 guint8 data[4] = {0x03, 0x02, 0x25, 0x18};
484 bluetooth_advertising_data_t adv;
486 BT_DBG("%x %x %x %x", data[0], data[1], data[2], data[3]);
487 memcpy(adv.data, data, sizeof(data));
488 ret = bluetooth_set_advertising_data(adv_handle, &adv, sizeof(data));
489 if (ret != BLUETOOTH_ERROR_NONE) {
490 BT_ERR("Failed to set ADV data %d", ret);
494 ret = bluetooth_set_advertising(adv_handle, TRUE);
495 if (ret != BLUETOOTH_ERROR_NONE) {
496 BT_ERR("Failed to set ADV %d", ret);
503 void _bt_otp_start_write_on_fd()
505 char buf[BT_L2CAP_BUFFER_LEN];
510 char file_path[BT_FILE_PATH_MAX_LEN] = {0, };
513 if (!selected_object) {
514 BT_DBG("Object not selected");
518 snprintf(file_path, sizeof(file_path), "%s%s",
519 directory, selected_object->name);
520 BT_DBG("file_path = [%s]", file_path);
522 fp = fopen(file_path, "r");
524 BT_DBG("fopen() failed : %s", strerror(errno));
528 BT_DBG("length [%d]", oacp_op->length);
529 length = oacp_op->length;
532 if (length < BT_L2CAP_BUFFER_LEN)
535 len = BT_L2CAP_BUFFER_LEN;
537 read = fread(buf, 1, len, fp);
538 written = write(oacp_op->fd, buf, len);
545 BT_DBG("read [%d], written [%d], rem_len [%d]",
546 read, written, length);
553 static bool __bt_otc_connection_timeout_cb(gpointer user_data)
555 int err = BLUETOOTH_ERROR_NONE;
556 char *remote_addr = oacp_op->remote_address;
558 err = _bt_otp_open_otc_and_listen(remote_addr, "DisconnectOtc");
559 if (err != BLUETOOTH_ERROR_NONE)
560 BT_ERR("Disconnect OTC failed");
565 static gboolean __server_data_received_cb(GIOChannel *chan, GIOCondition cond,
568 char *remote_addr = oacp_op->remote_address;
569 GIOStatus status = G_IO_STATUS_NORMAL;
578 fd = g_io_channel_unix_get_fd(chan);
579 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
580 otc_connection_status = FALSE;
581 BT_ERR("OTC disconnected: %d", fd);
583 g_source_remove(server_watch_id);
587 buffer = g_malloc0(BT_L2CAP_BUFFER_LEN + 1);
589 status = g_io_channel_read_chars(chan, buffer,
592 if (status != G_IO_STATUS_NORMAL) {
593 BT_ERR("IO Channel read is failed with %d", status);
597 otc_connection_status = FALSE;
598 BT_ERR("IO Channel read error [%s]", err->message);
599 if (status == G_IO_STATUS_ERROR) {
600 BT_ERR("cond : %d", cond);
603 g_source_remove(server_watch_id);
611 BT_DBG("Received data length %d, remote_addr = %s", len, remote_addr);
614 char file_path[BT_FILE_PATH_MAX_LEN] = {0, };
617 if (!selected_object) {
618 BT_DBG("Object not selected");
622 snprintf(file_path, sizeof(file_path), "%s%s",
623 directory, selected_object->name);
625 BT_DBG("file_path = [%s]", file_path);
626 fp = fopen(file_path, "w");
628 BT_DBG("fopen() failed : %s", strerror(errno));
634 if (oacp_op->length_sofar <= oacp_op->length) {
635 written = fwrite(buffer, 1, len, oacp_op->fp);
636 oacp_op->length_sofar += written;
637 BT_DBG("written [%d], length_sofar [%lu], received_buff_len [%d], size [%lu]",
638 written, oacp_op->length_sofar, len, oacp_op->length);
641 if (timeout_id > 0) {
642 g_source_remove(timeout_id);
643 timeout_id = g_timeout_add(BT_OACP_MAX_TIMEOUT,
644 (GSourceFunc)__bt_otc_connection_timeout_cb, NULL);
651 static void _bt_otp_start_read_on_fd()
654 data_io = g_io_channel_unix_new(oacp_op->fd);
656 g_io_channel_set_encoding(data_io, NULL, NULL);
657 g_io_channel_set_flags(data_io, G_IO_FLAG_NONBLOCK, NULL);
659 server_watch_id = g_io_add_watch(data_io,
660 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
661 __server_data_received_cb, NULL);
664 g_source_remove(timeout_id);
666 timeout_id = g_timeout_add(BT_OACP_MAX_TIMEOUT,
667 (GSourceFunc)__bt_otc_connection_timeout_cb, NULL);
670 static void _bt_otp_method(GDBusConnection *connection,
672 const gchar *object_path,
673 const gchar *interface_name,
674 const gchar *method_name,
675 GVariant *parameters,
676 GDBusMethodInvocation *invocation,
680 int status = BLUETOOTH_ERROR_NONE;
682 BT_DBG("Method[%s] Object Path[%s] Interface Name[%s]",
683 method_name, object_path, interface_name);
685 if (g_strcmp0(method_name, "enable") == 0) {
687 GError *error = NULL;
688 const gchar *filename = NULL;
689 char absolute_path[BT_FILE_PATH_MAX_LEN];
690 GSList *list = NULL, *l = NULL;
692 struct object_metadata *object = NULL;
694 g_variant_get(parameters, "(s)", &directory);
695 BT_DBG("Directory = [%s]", directory);
697 dir = g_dir_open(directory, 0, &error);
699 BT_ERR("Failed to open directory: %s", error->message);
701 status = BLUETOOTH_ERROR_INVALID_DIRECTORY;
705 while ((filename = g_dir_read_name(dir))) {
706 list = g_slist_append(list, (gpointer) filename);
712 BT_DBG("No object found in given directory");
713 status = BLUETOOTH_ERROR_NO_OBJECTS_FOUND;
717 if (g_slist_length(list) > 1)
718 mutiple_obj_support = true;
720 for (l = list; l != NULL; l = l->next) {
721 if (!l->data) continue;
722 snprintf(absolute_path, sizeof(absolute_path), "%s%s", directory,
725 BT_INFO("filename: %s, absoulte_path: %s",
726 (char *)l->data, absolute_path);
728 if (stat(absolute_path, &st) == -1) {
729 BT_INFO("stat failed: (%d)\n", errno);
733 object = g_new0(struct object_metadata, 1);
735 object->name = g_strdup((const gchar *)l->data);
736 object->type = _otp_convert_uuid_to_uuid128(UNSUPPORTED_OBJECT_TYPE_UUID);
737 object->first_created = st.st_ctime;
738 object->last_modified = st.st_ctime;
739 object->curr_size = (uint32_t) st.st_size;
740 object->alloc_size = (uint32_t) st.st_size;
741 object->id = object_id;
742 object->props = OBJECT_READ | OBJECT_WRITE | OBJECT_EXECUTE;
744 otp_object_list = g_slist_append(otp_object_list,
751 if (_bt_otp_prepare_ots() != BLUETOOTH_ERROR_NONE) {
752 BT_ERR("Fail to prepare OTP Proxy");
753 status = BLUETOOTH_ERROR_INTERNAL;
757 /* If single object is supported, make that as
758 * selected object and update the metadata for the same.
760 if (!mutiple_obj_support) {
761 BT_INFO("Server supports single object");
762 selected_object = (struct object_metadata *) g_slist_nth_data(otp_object_list, 0);
764 update_obj_metadata_charc_value(selected_object);
767 BT_DBG("advertsing");
768 if (_bt_otp_set_advertising_data() != BLUETOOTH_ERROR_NONE) {
769 BT_ERR("Fail to set advertising data");
770 status = BLUETOOTH_ERROR_INTERNAL;
774 g_dbus_method_invocation_return_value(invocation,
775 g_variant_new("(i)", status));
777 } else if (g_strcmp0(method_name, "disable") == 0) {
778 g_dbus_method_invocation_return_value(invocation,
779 g_variant_new("(i)", status));
782 } else if (g_strcmp0(method_name, "NewConnection") == 0) {
785 GUnixFDList *fd_list;
787 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
790 g_variant_get(parameters, "(oh)", &dev_path, &index);
792 msg = g_dbus_method_invocation_get_message(invocation);
793 fd_list = g_dbus_message_get_unix_fd_list(msg);
794 if (fd_list == NULL) {
795 BT_ERR("fd_list is NULL");
799 fd = g_unix_fd_list_get(fd_list, index, NULL);
801 BT_ERR("Invalid fd return");
805 _bt_convert_device_path_to_address(dev_path, address);
807 BT_INFO("OTC Connected fd: %d, address %s", fd, address);
808 otc_connection_status = TRUE;
812 if (oacp_op->opcode == OACP_READ)
813 _bt_otp_start_write_on_fd();
814 else if (oacp_op->opcode == OACP_WRITE)
815 _bt_otp_start_read_on_fd();
817 g_dbus_method_invocation_return_value(invocation, NULL);
822 static const GDBusInterfaceVTable otp_method_table = {
828 static void _bt_otp_on_bus_acquired(GDBusConnection *connection,
829 const gchar *name, gpointer user_data)
832 GError *error = NULL;
838 object_id = g_dbus_connection_register_object(connection,
840 otp_node_info->interfaces[0],
843 if (object_id == 0) {
844 BT_ERR("Failed to register method table: %s", error->message);
846 g_dbus_node_info_unref(otp_node_info);
852 static void _bt_otp_on_name_acquired(GDBusConnection *connection,
859 static void _bt_otp_on_name_lost(GDBusConnection *connection,
864 g_object_unref(g_conn);
866 g_dbus_node_info_unref(otp_node_info);
867 g_bus_unown_name(g_owner_id);
870 int _bt_otp_register_interface(void)
873 GError *error = NULL;
876 otp_node_info = g_dbus_node_info_new_for_xml(otp_introspection_xml, &error);
877 if (!otp_node_info) {
878 BT_ERR("Failed to install: %s", error->message);
879 return BLUETOOTH_ERROR_INTERNAL;
882 owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
884 G_BUS_NAME_OWNER_FLAGS_NONE,
885 _bt_otp_on_bus_acquired,
886 _bt_otp_on_name_acquired,
887 _bt_otp_on_name_lost,
889 g_owner_id = owner_id;
890 BT_DBG("owner_id is [%d]\n", owner_id);
893 return BLUETOOTH_ERROR_NONE;
896 void _bt_otp_unregister_interface(void)
900 g_object_unref(g_conn);
902 g_dbus_node_info_unref(otp_node_info);
903 g_bus_unown_name(g_owner_id);
909 void _bt_convert_device_path_to_address(const char *device_path,
910 char *device_address)
912 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
915 dev_addr = strstr(device_path, "dev_");
916 if (dev_addr != NULL) {
919 g_strlcpy(address, dev_addr, sizeof(address));
921 while ((pos = strchr(address, '_')) != NULL)
924 g_strlcpy(device_address, address, BT_ADDRESS_STRING_SIZE);
928 static char *__bt_extract_device_path(GVariantIter *iter, char *address)
930 char *object_path = NULL;
931 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
933 /* Parse the signature: oa{sa{sv}}} */
934 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path,
937 BT_ERR("Unable to get object path");
940 _bt_convert_device_path_to_address(object_path, device_address);
941 if (g_strcmp0(address, device_address) == 0)
942 return g_strdup(object_path);
946 BT_ERR("Unable to get object path");
950 char *_bt_otp_get_device_object_path(char *address)
953 GDBusProxy *proxy = NULL;
954 GVariant *result = NULL;
955 GVariantIter *iter = NULL;
956 char *object_path = NULL;
958 proxy = g_dbus_proxy_new_sync(conn,
959 G_DBUS_PROXY_FLAGS_NONE, NULL,
962 BT_MANAGER_INTERFACE,
966 BT_ERR("Unable to create proxy: %s", err->message);
970 result = g_dbus_proxy_call_sync(proxy, "GetManagedObjects", NULL,
971 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
974 BT_ERR("Fail to get GetManagedObjects (Error: %s)", err->message);
976 BT_ERR("Fail to get GetManagedObjects");
981 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
982 object_path = __bt_extract_device_path(iter, address);
984 g_variant_unref(result);
985 g_variant_iter_free(iter);
992 g_object_unref(proxy);
997 int _bt_otp_open_otc_and_listen(char *address, char *method)
1000 GDBusProxy *device_proxy = NULL;
1001 GVariant *result = NULL;
1002 GError *error = NULL;
1003 int ret = BLUETOOTH_ERROR_NONE;
1006 return BLUETOOTH_ERROR_INTERNAL;
1008 if (g_strcmp0(method, "ListenOtc") &&
1009 g_strcmp0(method, "DisconnectOtc"))
1010 return BLUETOOTH_ERROR_INTERNAL;
1012 object_path = _bt_otp_get_device_object_path(address);
1013 if (object_path == NULL) {
1014 ret = BLUETOOTH_ERROR_NOT_PAIRED;
1018 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1019 NULL, BT_BLUEZ_NAME, object_path,
1020 BT_DEVICE_INTERFACE, NULL, NULL);
1021 if (device_proxy == NULL) {
1022 ret = BLUETOOTH_ERROR_INTERNAL;
1027 result = g_dbus_proxy_call_sync(device_proxy, method,
1029 G_DBUS_CALL_FLAGS_NONE,
1033 if (result == NULL) {
1034 if (error != NULL) {
1035 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1036 g_error_free(error);
1038 ret = BLUETOOTH_ERROR_INTERNAL;
1042 g_free(object_path);
1044 g_variant_unref(result);
1046 g_object_unref(device_proxy);
1050 static bool __bt_oacp_create_timeout_cb(gpointer user_data)
1053 g_free(oacp_create->uuid);
1054 g_free(oacp_create);
1060 static void _bt_otp_free_oacp_op()
1062 if (timeout_id > 0) {
1063 g_source_remove(timeout_id);
1068 g_free(oacp_op->remote_address);
1070 fclose(oacp_op->fp);
1076 int _bt_otp_send_launch_request(char *absolute_path)
1082 /* check ARCH 64 or 32*/
1083 if (!access(FILEPATH_ARCH_64, 0)) {
1084 BT_INFO("plugin loading for ARCH 64");
1085 handle = dlopen(HEADED_PLUGIN_FILEPATH64, RTLD_NOW);
1087 BT_INFO("plugin loading for ARCH 32");
1088 handle = dlopen(HEADED_PLUGIN_FILEPATH, RTLD_NOW);
1092 BT_ERR("Can not load plugin %s", dlerror());
1093 return BLUETOOTH_ERROR_INTERNAL;
1096 dlerror(); /* Clear any existing error */
1098 int (*fun)(char *) = (int (*)(char *))dlsym(handle,
1099 "bt_app_control_send_launch_request");
1101 if ((error = dlerror()) != NULL) {
1102 BT_ERR("Can not load symbol : %s", dlerror());
1104 return BLUETOOTH_ERROR_INTERNAL;
1107 ret = fun(absolute_path);
1113 int _bt_otp_oacp_write_cb(char *value, int len, int offset,
1114 char *remote_addr, struct indicate_info *info)
1116 int ret = OACP_SUCCESS;
1117 int app_err = BLUETOOTH_ERROR_NONE;
1118 int opcode = value[0];
1119 uint32_t object_offset, length, object_size;
1122 char absolute_path[BT_FILE_PATH_MAX_LEN] = {0, };
1124 BT_INFO("OACP Opcode 0x%d", opcode);
1126 if (!otp_object_list) {
1127 ret = OACP_INVALID_OBJ;
1133 BT_INFO("OACP_CREATE");
1134 object_size = (uint32_t)(value[4] & 0xFF) << 24 |
1135 (uint32_t)(value[3] & 0xFF) << 16 |
1136 (uint32_t)(value[2] & 0xFF) << 8 |
1137 (uint32_t)(value[1] & 0xFF);
1139 uuid = g_strndup(value + 5, len - 5);
1140 BT_INFO("Size = %lu, UUID = %s", object_size, uuid);
1143 /* Create operation already going on. */
1144 ret = OACP_OPERATION_FAILED;
1147 oacp_create = g_malloc0(sizeof(struct oacp_create_operation));
1148 oacp_create->size = object_size;
1149 oacp_create->uuid = g_strdup(uuid);
1151 g_source_remove(timeout_id);
1152 timeout_id = g_timeout_add(BT_OACP_MAX_TIMEOUT,
1153 (GSourceFunc)__bt_oacp_create_timeout_cb, NULL);
1158 ret = OACP_OPCODE_NOT_SUPPORTED;
1160 case OACP_CALC_CHECKSUM:
1161 ret = OACP_OPCODE_NOT_SUPPORTED;
1164 if (!selected_object) {
1165 BT_DBG("Object not selected");
1166 ret = OACP_OPERATION_FAILED;
1170 snprintf(absolute_path, sizeof(absolute_path), "file://%s%s", directory,
1171 selected_object->name);
1173 BT_DBG("absolute_path = [%s]", absolute_path);
1175 app_err = _bt_otp_send_launch_request(absolute_path);
1176 if (app_err == BLUETOOTH_ERROR_NONE)
1179 ret = OACP_OPERATION_FAILED;
1183 object_offset = (uint32_t)(value[4] & 0xFF) << 24 |
1184 (uint32_t)(value[3] & 0xFF) << 16 |
1185 (uint32_t)(value[2] & 0xFF) << 8 |
1186 (uint32_t)(value[1] & 0xFF);
1187 length = (uint32_t)(value[8] & 0xFF) << 24 |
1188 (uint32_t)(value[7] & 0xFF) << 16 |
1189 (uint32_t)(value[6] & 0xFF) << 8 |
1190 (uint32_t)(value[5] & 0xFF);
1192 if (opcode == OACP_WRITE)
1193 mode = (uint8_t)value[9] & 0xFF;
1195 BT_INFO("Offset = %lu, Length = %lu", object_offset, length, mode);
1198 if (otc_connection_status) {
1199 /* Read/Write operation already going on. */
1200 ret = OACP_OBJECT_LOCKED;
1203 _bt_otp_free_oacp_op();
1206 oacp_op = g_malloc0(sizeof(struct oacp_operation));
1207 oacp_op->offset = object_offset;
1208 oacp_op->length = length;
1209 oacp_op->remote_address = g_strdup(remote_addr);
1210 oacp_op->mode = mode;
1211 oacp_op->opcode = opcode;
1212 oacp_op->length_sofar = 0;
1215 app_err = _bt_otp_open_otc_and_listen(remote_addr, "ListenOtc");
1216 if (app_err != BLUETOOTH_ERROR_NONE) {
1217 ret = OACP_OPERATION_FAILED;
1218 _bt_otp_free_oacp_op();
1224 ret = OACP_OPCODE_NOT_SUPPORTED;
1227 ret = OACP_OPCODE_NOT_SUPPORTED;
1231 info->resp_opcode = OACP_RESPONSE;
1232 info->req_opcode = opcode;
1233 info->result_code = ret;
1234 info->resp_param = NULL;
1238 void convert_to_hex(struct object_metadata *object, char *type, char *value)
1242 BT_DBG("type : %s", type);
1244 memset(value, 0, 8);
1246 if (!g_strcmp0(type, "size")) {
1248 value[3] = (object->curr_size >> 24) & 0xFF;
1249 value[2] = (object->curr_size >> 16) & 0xFF;
1250 value[1] = (object->curr_size >> 8) & 0xFF;
1251 value[0] = object->curr_size & 0xFF;
1253 value[7] = (object->alloc_size >> 24) & 0xFF;
1254 value[6] = (object->alloc_size >> 16) & 0xFF;
1255 value[5] = (object->alloc_size >> 8) & 0xFF;
1256 value[4] = object->alloc_size & 0xFF;
1258 } else if (!g_strcmp0(type, "date")) {
1260 localtime_r(&(object->first_created), &fc_tm);
1262 value[1] = ((fc_tm.tm_year+1900) >> 8) & 0xFF;
1263 value[0] = (fc_tm.tm_year+1900) & 0xFF;
1264 value[2] = (fc_tm.tm_mon+1) & 0xFF;
1265 value[3] = fc_tm.tm_mday & 0xFF;
1266 value[4] = fc_tm.tm_hour & 0xFF;
1267 value[5] = fc_tm.tm_min & 0xFF;
1268 value[6] = fc_tm.tm_sec & 0xFF;
1270 } else if (!g_strcmp0(type, "id")) {
1272 value[5] = (object->id >> 48) & 0xFF;
1273 value[4] = (object->id >> 32) & 0xFF;
1274 value[3] = (object->id >> 24) & 0xFF;
1275 value[2] = (object->id >> 16) & 0xFF;
1276 value[1] = (object->id >> 8) & 0xFF;
1277 value[0] = object->id & 0xFF;
1279 } else if (!g_strcmp0(type, "props")) {
1280 value[3] = (object->props >> 24) & 0xFF;
1281 value[2] = (object->props >> 16) & 0xFF;
1282 value[1] = (object->props >> 8) & 0xFF;
1283 value[0] = object->props & 0xFF;
1287 void update_obj_metadata_charc_value(struct object_metadata *object)
1289 /* Value can be of maximum eight bytes */
1292 _bt_otp_set_char_value(otp_object_name_obj_path, object->name,
1293 strlen(object->name));
1294 _bt_otp_set_char_value(otp_object_type_obj_path, object->type,
1295 strlen(object->type));
1297 convert_to_hex(object, "size", value);
1298 _bt_otp_set_char_value(otp_object_size_obj_path, value, 8);
1300 convert_to_hex(object, "date", value);
1301 _bt_otp_set_char_value(otp_object_first_created_obj_path, value, 7);
1302 _bt_otp_set_char_value(otp_object_last_modified_obj_path, value, 7);
1304 /* Object ID is optonal for single object server */
1305 if (mutiple_obj_support) {
1306 convert_to_hex(object, "id", value);
1307 _bt_otp_set_char_value(otp_object_id_obj_path, value, 6);
1310 convert_to_hex(object, "props", value);
1311 _bt_otp_set_char_value(otp_object_prop_obj_path, value, 4);
1314 struct object_metadata *_bt_otp_client_find_object(GSList *list, uint64_t id, guint *index)
1317 struct object_metadata *info;
1319 for (l = list; l; l = g_slist_next(l)) {
1323 if (info && (info->id == id))
1329 int _bt_otp_olcp_write_cb(char *value, int len, int offset,
1330 struct indicate_info *info)
1332 int ret = OLCP_SUCCESS;
1333 int opcode = value[0];
1334 struct object_metadata *object;
1338 BT_INFO("OLCP Opcode 0x%d", opcode);
1340 if (!otp_object_list) {
1347 object = (struct object_metadata *) g_slist_nth_data(otp_object_list, 0);
1349 ret = OLCP_OUT_OF_BOUNDS;
1352 update_obj_metadata_charc_value(object);
1353 selected_object = object;
1357 len = g_slist_length(otp_object_list);
1358 object = (struct object_metadata *) g_slist_nth_data(otp_object_list, len-1);
1360 ret = OLCP_OUT_OF_BOUNDS;
1363 update_obj_metadata_charc_value(object);
1364 selected_object = object;
1365 obj_curr_index = len-1;
1368 if (obj_curr_index == 0) {
1369 ret = OLCP_OUT_OF_BOUNDS;
1372 object = (struct object_metadata *) g_slist_nth_data(otp_object_list, obj_curr_index-1);
1374 ret = OLCP_OUT_OF_BOUNDS;
1377 update_obj_metadata_charc_value(object);
1378 selected_object = object;
1379 obj_curr_index -= 1;
1382 object = (struct object_metadata *) g_slist_nth_data(otp_object_list, obj_curr_index+1);
1384 ret = OLCP_OUT_OF_BOUNDS;
1387 update_obj_metadata_charc_value(object);
1388 selected_object = object;
1389 obj_curr_index += 1;
1392 object_id = (uint64_t)(value[6] & 0xFF) << 40 |
1393 (uint64_t)(value[5] & 0xFF) << 32 |
1394 (uint64_t)(value[4] & 0xFF) << 24 |
1395 (uint64_t)(value[3] & 0xFF) << 16 |
1396 (uint64_t)(value[2] & 0xFF) << 8 |
1397 (uint64_t)(value[1] & 0xFF);
1398 BT_INFO("Object ID [%llu]", object_id);
1399 if (selected_object && selected_object->id == object_id)
1402 object = _bt_otp_client_find_object(otp_object_list, object_id, &index);
1404 ret = OLCP_OJECT_ID_NOT_FOUND;
1407 update_obj_metadata_charc_value(object);
1408 selected_object = object;
1409 obj_curr_index = index - 1;
1412 case OLCP_REQ_NO_OBJ:
1413 case OLCP_CLEAR_MARKING:
1415 ret = OLCP_OPCODE_NOT_SUPPORTED;
1419 info->resp_opcode = OLCP_RESPONSE;
1420 info->req_opcode = opcode;
1421 info->result_code = ret;
1422 info->resp_param = NULL;
1423 return BLUETOOTH_ERROR_NONE;
1426 int _bt_otp_obj_name_cb(char *value, int len)
1428 int ret = BLUETOOTH_ERROR_NONE;
1430 struct object_metadata *object = NULL;
1437 filename = g_strndup(value, len);
1438 length = len + strlen(BT_OTP_BASE_DIR_PATH) + 1;
1439 file_path = malloc(length);
1441 snprintf(file_path, length, "%s%s",
1442 BT_OTP_BASE_DIR_PATH, filename);
1443 BT_DBG("file_path = [%s]", file_path);
1445 fp = fopen(file_path, "a");
1447 BT_DBG("fopen() failed : %s", strerror(errno));
1448 ret = BLUETOOTH_ERROR_INTERNAL;
1452 if (stat(file_path, &st) == -1) {
1453 BT_INFO("stat failed: (%d)\n", errno);
1454 ret = BLUETOOTH_ERROR_INTERNAL;
1458 object = g_new0(struct object_metadata, 1);
1460 object->name = g_strdup(filename);
1461 object->type = _otp_convert_uuid_to_uuid128(oacp_create->uuid);
1462 object->first_created = st.st_ctime;
1463 object->last_modified = st.st_ctime;
1464 object->curr_size = (uint32_t)st.st_size;
1465 object->alloc_size = oacp_create->size;
1466 object->id = object_id;
1467 object->props = OBJECT_READ | OBJECT_WRITE;
1469 otp_object_list = g_slist_append(otp_object_list,
1472 update_obj_metadata_charc_value(object);
1473 selected_object = object;
1474 obj_curr_index = g_slist_length(otp_object_list) - 1;
1481 g_free(oacp_create->uuid);
1482 g_free(oacp_create);
1488 static struct otp_char_info *otp_get_char_value(const char *path)
1492 for (tmp = otp_char_list; tmp != NULL; tmp = tmp->next) {
1494 struct otp_char_info *char_info = tmp->data;
1495 if (!g_strcmp0(char_info->char_path, path))
1503 int _bt_otp_read_cb(const char *obj_path, char **value, int *len)
1505 struct otp_char_info *info = NULL;
1508 BT_ERR("Wrong Obj path");
1509 return BLUETOOTH_ERROR_INTERNAL;
1512 if (g_strcmp0(obj_path, otp_feature_obj_path)) {
1513 if (!selected_object) {
1514 return BLUETOOTH_ERROR_OBJECT_NOT_SELECTED;
1518 info = otp_get_char_value(obj_path);
1520 if (info->char_value == NULL || info->value_length == 0)
1521 return BLUETOOTH_ERROR_INTERNAL;
1523 *len = info->value_length;
1524 *value = (char *)malloc(sizeof(char)*(*len));
1525 memcpy(*value, info->char_value, *len);
1527 return BLUETOOTH_ERROR_NONE;
1529 return BLUETOOTH_ERROR_INTERNAL;
1533 static void _otp_convert_address_to_hex(bluetooth_device_address_t *addr_hex,
1534 const char *addr_str)
1537 unsigned int addr[BLUETOOTH_ADDRESS_LENGTH] = { 0, };
1539 if (addr_str == NULL || addr_str[0] == '\0')
1542 i = sscanf(addr_str, "%X:%X:%X:%X:%X:%X", &addr[0], &addr[1],
1543 &addr[2], &addr[3], &addr[4], &addr[5]);
1544 if (i != BLUETOOTH_ADDRESS_LENGTH)
1545 BT_ERR("Invalid format string - [%s]", addr_str);
1547 for (i = 0; i < BLUETOOTH_ADDRESS_LENGTH; i++)
1548 addr_hex->addr[i] = (unsigned char)addr[i];
1551 static void _bt_otp_send_indication(const char *obj_path,
1552 struct indicate_info *info,
1553 bluetooth_device_address_t *remote_address)
1555 int ret = BLUETOOTH_ERROR_NONE;
1556 char value[7] = {0x00};
1560 value[0] = info->resp_opcode & 0xFF;
1561 value[1] = info->req_opcode & 0xFF;
1562 value[2] = info->result_code & 0xFF;
1563 if (info->resp_param) {
1564 value[6] = (info->resp_param[3] >> 24) & 0xFF;
1565 value[5] = (info->resp_param[4] >> 16) & 0xFF;
1566 value[4] = (info->resp_param[5] >> 8) & 0xFF;
1567 value[3] = info->resp_param[6] & 0xFF;
1570 BT_DBG("Opcode: %d", value[1]);
1572 /* Store the status value */
1573 _bt_otp_set_char_value(obj_path, value, 7);
1575 /* Send indication */
1576 ret = bluetooth_gatt_server_set_notification(obj_path, remote_address);
1577 if (ret != BLUETOOTH_ERROR_NONE) {
1578 BT_ERR("_bt_otp_send_control_point_indication failed");
1581 ret = bluetooth_gatt_update_characteristic(obj_path, value, 7);
1582 if (ret != BLUETOOTH_ERROR_NONE) {
1583 BT_ERR("_bt_otp_send_control_point_indication failed");
1588 void _bt_otp_gatt_char_property_changed_event(GVariant *msg,
1591 int result = BLUETOOTH_ERROR_NONE;
1592 GVariantIter value_iter;
1593 const char *property = NULL;
1594 const char *char_path = NULL;
1595 const char *svc_handle = NULL;
1596 GVariant *var = NULL;
1597 GVariant *val = NULL;
1598 g_variant_iter_init(&value_iter, msg);
1600 while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &var))) {
1602 if (property == NULL) {
1603 BT_ERR("Property NULL");
1607 if (!g_strcmp0(property, "WriteValue")) {
1609 BT_INFO("WriteValue");
1610 BT_INFO("Type '%s'\n", g_variant_get_type_string(var));
1613 bluetooth_device_address_t addr_hex = { {0,} };
1618 struct indicate_info info;
1619 g_variant_get(var, "(&s&s&syq@ay)",
1620 &char_path, &svc_handle,
1621 &addr, &req_id, &offset, &val);
1623 len = g_variant_get_size(val);
1625 BT_DBG("Len = %d, BT_ADDR = %s", len, addr);
1627 value = (char *) g_variant_get_data(val);
1628 _otp_convert_address_to_hex(&addr_hex, addr);
1631 if (!g_strcmp0(char_path, otp_oacp_obj_path)) {
1632 result = _bt_otp_oacp_write_cb(value, len, offset, addr, &info);
1633 } else if (!g_strcmp0(char_path, otp_olcp_obj_path)) {
1634 result = _bt_otp_olcp_write_cb(value, len, offset, &info);
1635 } else if (!g_strcmp0(char_path, otp_object_name_obj_path)) {
1637 /* OACP_CREATE is ongoing */
1638 result = _bt_otp_obj_name_cb(value, len);
1640 /* Dont permit writting object name except while creating object.
1641 * As this is directly pointing local objects.
1643 result = BLUETOOTH_ERROR_WRITE_REQUEST_REJECTED;
1645 } else if (!g_strcmp0(char_path, otp_object_first_created_obj_path)) {
1646 _bt_otp_set_char_value(otp_object_first_created_obj_path, value, len);
1647 } else if (!g_strcmp0(char_path, otp_object_last_modified_obj_path)) {
1648 _bt_otp_set_char_value(otp_object_last_modified_obj_path, value, len);
1650 BT_ERR("Wrong Object Path %s", char_path);
1651 result = BLUETOOTH_ERROR_INTERNAL;
1653 bluetooth_gatt_send_response(req_id,
1654 BLUETOOTH_GATT_ATT_REQUEST_TYPE_WRITE,
1655 result, 0, NULL, 0);
1657 /* Send indication for CPs */
1658 if (!g_strcmp0(char_path, otp_oacp_obj_path)) {
1659 if (OACP_indicate) {
1660 _bt_otp_send_indication(char_path, &info, &addr_hex);
1662 } else if (!g_strcmp0(char_path, otp_olcp_obj_path)) {
1663 if (OLCP_indicate) {
1664 _bt_otp_send_indication(char_path, &info, &addr_hex);
1668 BT_ERR("Array Len 0");
1671 BT_ERR("var==NULL");
1673 } else if (!g_strcmp0(property, "ReadValue")) {
1679 result = BLUETOOTH_ERROR_NONE;
1681 BT_INFO("ReadValue");
1682 BT_INFO("Type '%s'\n", g_variant_get_type_string(var));
1684 g_variant_get(var, "(&s&s&syq)", &char_path,
1685 &svc_handle, &addr, &req_id, &offset);
1687 result = _bt_otp_read_cb(char_path, &value, &len);
1689 if (result != BLUETOOTH_ERROR_NONE) {
1690 BT_ERR("ReadValue failed %s", char_path);
1691 bluetooth_gatt_send_response(req_id,
1692 BLUETOOTH_GATT_ATT_REQUEST_TYPE_READ,
1693 result, offset, NULL, 0);
1695 bluetooth_gatt_send_response(req_id,
1696 BLUETOOTH_GATT_ATT_REQUEST_TYPE_READ,
1697 result, offset, value, len);
1701 } else if (!g_strcmp0(property, "NotificationStateChanged")) {
1702 gboolean indicate = FALSE;
1704 g_variant_get(var, "(&s&sb)", &char_path,
1705 &svc_handle, &indicate);
1707 BT_INFO("%s : [%s]", property,
1708 indicate ? "StartNotify" : "StopNotify");
1709 BT_INFO("Type '%s'\n", g_variant_get_type_string(var));
1711 if (!g_strcmp0(char_path, otp_oacp_obj_path)) {
1712 OACP_indicate = indicate;
1713 } else if (!g_strcmp0(char_path, otp_olcp_obj_path)) {
1714 OLCP_indicate = indicate;
1721 void _bt_otp_property_event_filter(GDBusConnection *connection,
1722 const gchar *sender_name,
1723 const gchar *object_path,
1724 const gchar *interface_name,
1725 const gchar *signal_name,
1726 GVariant *parameters,
1731 if (signal_name == NULL) {
1732 BT_ERR("Wrong Signal");
1736 if (g_strcmp0(signal_name, PROPERTIES_CHANGED) == 0) {
1738 g_variant_get(parameters, "(@a{sv}@as)", &value, NULL);
1739 _bt_otp_gatt_char_property_changed_event(value, object_path);
1743 void _bt_otp_adapter_event_filter(GDBusConnection *connection,
1744 const gchar *sender_name,
1745 const gchar *object_path,
1746 const gchar *interface_name,
1747 const gchar *signal_name,
1748 GVariant *parameters,
1751 if (signal_name == NULL) {
1752 BT_ERR("Wrong Signal");
1756 BT_INFO("Interface %s, Signal %s", interface_name, signal_name);
1758 if (g_strcmp0(interface_name, BT_OTP_INTERFACE_NAME) == 0) {
1759 if (strcasecmp(signal_name, BLE_DISABLED) == 0) {
1765 void _bt_otc_disconnected_cb(GDBusConnection *connection,
1766 const gchar *sender_name,
1767 const gchar *object_path,
1768 const gchar *interface_name,
1769 const gchar *signal_name,
1770 GVariant *parameters,
1773 if (signal_name == NULL) {
1774 BT_ERR("Wrong Signal");
1778 BT_INFO("Interface %s, Signal %s", interface_name, signal_name);
1780 if (g_strcmp0(interface_name, BT_DEVICE_INTERFACE) == 0) {
1781 if (strcasecmp(signal_name, OTC_DISCONNECTED) == 0) {
1782 BT_DBG("OTC Channel Disconnected dev_path[%s]",
1784 otc_connection_status = FALSE;
1785 _bt_otp_free_oacp_op();
1790 int _bt_otp_init_event_receiver()
1793 GError *error = NULL;
1796 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
1797 if (error != NULL) {
1798 BT_ERR("ERROR: Can't get on system bus [%s]",
1800 g_clear_error(&error);
1804 property_sub_id = g_dbus_connection_signal_subscribe(conn,
1806 BT_OTP_INTERFACE_NAME,
1808 BT_OTP_OBJECT_PATH, NULL, 0,
1809 _bt_otp_property_event_filter,
1812 adapter_sub_id = g_dbus_connection_signal_subscribe(conn,
1814 BT_OTP_INTERFACE_NAME,
1816 BT_OTP_OBJECT_PATH, NULL, 0,
1817 _bt_otp_adapter_event_filter,
1820 device_sub_id = g_dbus_connection_signal_subscribe(conn,
1821 NULL, BT_DEVICE_INTERFACE,
1822 OTC_DISCONNECTED, NULL, NULL, 0,
1823 _bt_otc_disconnected_cb,
1830 void _bt_otp_deinit_event_receiver(void)
1834 g_dbus_connection_signal_unsubscribe(conn, property_sub_id);
1835 g_dbus_connection_signal_unsubscribe(conn, adapter_sub_id);
1836 g_dbus_connection_signal_unsubscribe(conn, device_sub_id);
1842 static void _bt_otp_sig_handler(int sig)
1847 BT_DBG("caught signal - sigterm\n");
1850 BT_DBG("caught signal - sigint\n");
1853 BT_DBG("caught signal - sigkill\n");
1856 BT_DBG("caught signal %d and ignored\n", sig);
1862 /* OTP Service Main loop */
1865 struct sigaction sa;
1866 BT_ERR("Starting the bt-otp daemon");
1868 memset(&sa, 0, sizeof(sa));
1869 sa.sa_handler = _bt_otp_sig_handler;
1870 sa.sa_flags = SA_SIGINFO;
1871 sigaction(SIGINT, &sa, NULL);
1872 sigaction(SIGTERM, &sa, NULL);
1873 sigaction(SIGKILL, &sa, NULL);
1875 if (_bt_otp_register_interface() != BLUETOOTH_ERROR_NONE) {
1876 BT_ERR("Fail to register otp service");
1880 if (_bt_otp_init_event_receiver() != BLUETOOTH_ERROR_NONE) {
1881 BT_ERR("Fail to init event reciever");
1885 main_loop = g_main_loop_new(NULL, FALSE);
1887 g_main_loop_run(main_loop);
1889 BT_DBG("g_main_loop_quit called!");
1891 if (main_loop != NULL) {
1892 g_main_loop_unref(main_loop);