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] = { 0xDC, 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 snprintf(file_path, sizeof(file_path), "%s%s",
514 directory, selected_object->name);
515 BT_DBG("file_path = [%s]", file_path);
517 fp = fopen(file_path, "r");
519 BT_DBG("fopen() failed : %s", strerror(errno));
523 BT_DBG("length [%d]", oacp_op->length);
524 length = oacp_op->length;
527 if (length < BT_L2CAP_BUFFER_LEN)
530 len = BT_L2CAP_BUFFER_LEN;
532 read = fread(buf, 1, len, fp);
533 written = write(oacp_op->fd, buf, len);
540 BT_DBG("read [%d], written [%d], rem_len [%d]",
541 read, written, length);
548 static bool __bt_otc_connection_timeout_cb(gpointer user_data)
550 int err = BLUETOOTH_ERROR_NONE;
551 char *remote_addr = oacp_op->remote_address;
553 err = _bt_otp_open_otc_and_listen(remote_addr, "DisconnectOtc");
554 if (err != BLUETOOTH_ERROR_NONE)
555 BT_ERR("Disconnect OTC failed");
560 static gboolean __server_data_received_cb(GIOChannel *chan, GIOCondition cond,
563 char *remote_addr = oacp_op->remote_address;
564 GIOStatus status = G_IO_STATUS_NORMAL;
573 fd = g_io_channel_unix_get_fd(chan);
574 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
575 otc_connection_status = FALSE;
576 BT_ERR("OTC disconnected: %d", fd);
578 g_source_remove(server_watch_id);
582 buffer = g_malloc0(BT_L2CAP_BUFFER_LEN + 1);
584 status = g_io_channel_read_chars(chan, buffer,
587 if (status != G_IO_STATUS_NORMAL) {
588 BT_ERR("IO Channel read is failed with %d", status);
592 otc_connection_status = FALSE;
593 BT_ERR("IO Channel read error [%s]", err->message);
594 if (status == G_IO_STATUS_ERROR) {
595 BT_ERR("cond : %d", cond);
598 g_source_remove(server_watch_id);
606 BT_DBG("Received data length %d, remote_addr = %s", len, remote_addr);
609 char file_path[BT_FILE_PATH_MAX_LEN] = {0, };
612 if (!selected_object) {
613 BT_DBG("Object not selected");
617 snprintf(file_path, sizeof(file_path), "%s%s",
618 directory, selected_object->name);
620 BT_DBG("file_path = [%s]", file_path);
621 fp = fopen(file_path, "w");
623 BT_DBG("fopen() failed : %s", strerror(errno));
629 if (oacp_op->length_sofar <= oacp_op->length) {
630 written = fwrite(buffer, 1, len, oacp_op->fp);
631 oacp_op->length_sofar += written;
632 BT_DBG("written [%d], length_sofar [%lu], received_buff_len [%d], size [%lu]",
633 written, oacp_op->length_sofar, len, oacp_op->length);
636 if (timeout_id > 0) {
637 g_source_remove(timeout_id);
638 timeout_id = g_timeout_add(BT_OACP_MAX_TIMEOUT,
639 (GSourceFunc)__bt_otc_connection_timeout_cb, NULL);
646 static void _bt_otp_start_read_on_fd()
649 data_io = g_io_channel_unix_new(oacp_op->fd);
651 g_io_channel_set_encoding(data_io, NULL, NULL);
652 g_io_channel_set_flags(data_io, G_IO_FLAG_NONBLOCK, NULL);
654 server_watch_id = g_io_add_watch(data_io,
655 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
656 __server_data_received_cb, NULL);
659 g_source_remove(timeout_id);
661 timeout_id = g_timeout_add(BT_OACP_MAX_TIMEOUT,
662 (GSourceFunc)__bt_otc_connection_timeout_cb, NULL);
665 static void _bt_otp_method(GDBusConnection *connection,
667 const gchar *object_path,
668 const gchar *interface_name,
669 const gchar *method_name,
670 GVariant *parameters,
671 GDBusMethodInvocation *invocation,
675 int status = BLUETOOTH_ERROR_NONE;
677 BT_DBG("Method[%s] Object Path[%s] Interface Name[%s]",
678 method_name, object_path, interface_name);
680 if (g_strcmp0(method_name, "enable") == 0) {
682 GError *error = NULL;
683 const gchar *filename = NULL;
684 char absolute_path[BT_FILE_PATH_MAX_LEN];
685 GSList *list = NULL, *l = NULL;
687 struct object_metadata *object = NULL;
689 g_variant_get(parameters, "(s)", &directory);
690 BT_DBG("Directory = [%s]", directory);
692 dir = g_dir_open(directory, 0, &error);
694 BT_ERR("Failed to open directory: %s", error->message);
696 status = BLUETOOTH_ERROR_INVALID_DIRECTORY;
700 while ((filename = g_dir_read_name(dir))) {
701 list = g_slist_append(list, (gpointer) filename);
707 BT_DBG("No object found in given directory");
708 status = BLUETOOTH_ERROR_NO_OBJECTS_FOUND;
712 if (g_slist_length(list) > 1)
713 mutiple_obj_support = true;
715 for (l = list; l != NULL; l = l->next) {
716 if (!l->data) continue;
717 snprintf(absolute_path, sizeof(absolute_path), "%s%s", directory,
720 BT_INFO("filename: %s, absoulte_path: %s",
721 (char *)l->data, absolute_path);
723 if (stat(absolute_path, &st) == -1) {
724 BT_INFO("stat failed: (%d)\n", errno);
728 object = g_new0(struct object_metadata, 1);
730 object->name = g_strdup((const gchar *)l->data);
731 object->type = _otp_convert_uuid_to_uuid128(UNSUPPORTED_OBJECT_TYPE_UUID);
732 object->first_created = st.st_ctime;
733 object->last_modified = st.st_ctime;
734 object->curr_size = (uint32_t) st.st_size;
735 object->alloc_size = (uint32_t) st.st_size;
736 object->id = object_id;
737 object->props = OBJECT_READ | OBJECT_WRITE |
738 OBJECT_EXECUTE | OBJECT_DELETE;
740 otp_object_list = g_slist_append(otp_object_list,
747 if (_bt_otp_prepare_ots() != BLUETOOTH_ERROR_NONE) {
748 BT_ERR("Fail to prepare OTP Proxy");
749 status = BLUETOOTH_ERROR_INTERNAL;
753 /* If single object is supported, make that as
754 * selected object and update the metadata for the same.
756 if (!mutiple_obj_support) {
757 BT_INFO("Server supports single object");
758 selected_object = (struct object_metadata *) g_slist_nth_data(otp_object_list, 0);
760 update_obj_metadata_charc_value(selected_object);
763 BT_DBG("advertsing");
764 if (_bt_otp_set_advertising_data() != BLUETOOTH_ERROR_NONE) {
765 BT_ERR("Fail to set advertising data");
766 status = BLUETOOTH_ERROR_INTERNAL;
770 g_dbus_method_invocation_return_value(invocation,
771 g_variant_new("(i)", status));
773 } else if (g_strcmp0(method_name, "disable") == 0) {
774 g_dbus_method_invocation_return_value(invocation,
775 g_variant_new("(i)", status));
778 } else if (g_strcmp0(method_name, "NewConnection") == 0) {
781 GUnixFDList *fd_list;
783 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
786 g_variant_get(parameters, "(oh)", &dev_path, &index);
788 msg = g_dbus_method_invocation_get_message(invocation);
789 fd_list = g_dbus_message_get_unix_fd_list(msg);
790 if (fd_list == NULL) {
791 BT_ERR("fd_list is NULL");
795 fd = g_unix_fd_list_get(fd_list, index, NULL);
797 BT_ERR("Invalid fd return");
801 _bt_convert_device_path_to_address(dev_path, address);
803 BT_INFO("OTC Connected fd: %d, address %s", fd, address);
804 otc_connection_status = TRUE;
808 if (oacp_op->opcode == OACP_READ)
809 _bt_otp_start_write_on_fd();
810 else if (oacp_op->opcode == OACP_WRITE)
811 _bt_otp_start_read_on_fd();
813 g_dbus_method_invocation_return_value(invocation, NULL);
818 static const GDBusInterfaceVTable otp_method_table = {
824 static void _bt_otp_on_bus_acquired(GDBusConnection *connection,
825 const gchar *name, gpointer user_data)
828 GError *error = NULL;
834 object_id = g_dbus_connection_register_object(connection,
836 otp_node_info->interfaces[0],
839 if (object_id == 0) {
840 BT_ERR("Failed to register method table: %s", error->message);
842 g_dbus_node_info_unref(otp_node_info);
848 static void _bt_otp_on_name_acquired(GDBusConnection *connection,
855 static void _bt_otp_on_name_lost(GDBusConnection *connection,
860 g_object_unref(g_conn);
862 g_dbus_node_info_unref(otp_node_info);
863 g_bus_unown_name(g_owner_id);
866 int _bt_otp_register_interface(void)
869 GError *error = NULL;
872 otp_node_info = g_dbus_node_info_new_for_xml(otp_introspection_xml, &error);
873 if (!otp_node_info) {
874 BT_ERR("Failed to install: %s", error->message);
875 return BLUETOOTH_ERROR_INTERNAL;
878 owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
880 G_BUS_NAME_OWNER_FLAGS_NONE,
881 _bt_otp_on_bus_acquired,
882 _bt_otp_on_name_acquired,
883 _bt_otp_on_name_lost,
885 g_owner_id = owner_id;
886 BT_DBG("owner_id is [%d]\n", owner_id);
889 return BLUETOOTH_ERROR_NONE;
892 void _bt_otp_unregister_interface(void)
896 g_object_unref(g_conn);
898 g_dbus_node_info_unref(otp_node_info);
899 g_bus_unown_name(g_owner_id);
905 void _bt_convert_device_path_to_address(const char *device_path,
906 char *device_address)
908 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
911 dev_addr = strstr(device_path, "dev_");
912 if (dev_addr != NULL) {
915 g_strlcpy(address, dev_addr, sizeof(address));
917 while ((pos = strchr(address, '_')) != NULL)
920 g_strlcpy(device_address, address, BT_ADDRESS_STRING_SIZE);
924 static char *__bt_extract_device_path(GVariantIter *iter, char *address)
926 char *object_path = NULL;
927 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
929 /* Parse the signature: oa{sa{sv}}} */
930 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path,
933 BT_ERR("Unable to get object path");
936 _bt_convert_device_path_to_address(object_path, device_address);
937 if (g_strcmp0(address, device_address) == 0)
938 return g_strdup(object_path);
942 BT_ERR("Unable to get object path");
946 char *_bt_otp_get_device_object_path(char *address)
949 GDBusProxy *proxy = NULL;
950 GVariant *result = NULL;
951 GVariantIter *iter = NULL;
952 char *object_path = NULL;
954 proxy = g_dbus_proxy_new_sync(conn,
955 G_DBUS_PROXY_FLAGS_NONE, NULL,
958 BT_MANAGER_INTERFACE,
962 BT_ERR("Unable to create proxy: %s", err->message);
966 result = g_dbus_proxy_call_sync(proxy, "GetManagedObjects", NULL,
967 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
970 BT_ERR("Fail to get GetManagedObjects (Error: %s)", err->message);
972 BT_ERR("Fail to get GetManagedObjects");
977 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
978 object_path = __bt_extract_device_path(iter, address);
980 g_variant_unref(result);
981 g_variant_iter_free(iter);
988 g_object_unref(proxy);
993 int _bt_otp_open_otc_and_listen(char *address, char *method)
996 GDBusProxy *device_proxy = NULL;
997 GVariant *result = NULL;
998 GError *error = NULL;
999 int ret = BLUETOOTH_ERROR_NONE;
1002 return BLUETOOTH_ERROR_INTERNAL;
1004 if (g_strcmp0(method, "ListenOtc") &&
1005 g_strcmp0(method, "DisconnectOtc"))
1006 return BLUETOOTH_ERROR_INTERNAL;
1008 object_path = _bt_otp_get_device_object_path(address);
1009 if (object_path == NULL) {
1010 ret = BLUETOOTH_ERROR_NOT_PAIRED;
1014 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1015 NULL, BT_BLUEZ_NAME, object_path,
1016 BT_DEVICE_INTERFACE, NULL, NULL);
1017 if (device_proxy == NULL) {
1018 ret = BLUETOOTH_ERROR_INTERNAL;
1023 result = g_dbus_proxy_call_sync(device_proxy, method,
1025 G_DBUS_CALL_FLAGS_NONE,
1029 if (result == NULL) {
1030 if (error != NULL) {
1031 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1032 g_error_free(error);
1034 ret = BLUETOOTH_ERROR_INTERNAL;
1038 g_free(object_path);
1040 g_variant_unref(result);
1042 g_object_unref(device_proxy);
1046 static bool __bt_oacp_create_timeout_cb(gpointer user_data)
1049 g_free(oacp_create->uuid);
1050 g_free(oacp_create);
1056 static void _bt_otp_free_oacp_op()
1058 if (timeout_id > 0) {
1059 g_source_remove(timeout_id);
1064 g_free(oacp_op->remote_address);
1066 fclose(oacp_op->fp);
1072 int _bt_otp_send_launch_request(char *absolute_path)
1078 /* check ARCH 64 or 32*/
1079 if (!access(FILEPATH_ARCH_64, 0)) {
1080 BT_INFO("plugin loading for ARCH 64");
1081 handle = dlopen(HEADED_PLUGIN_FILEPATH64, RTLD_NOW);
1083 BT_INFO("plugin loading for ARCH 32");
1084 handle = dlopen(HEADED_PLUGIN_FILEPATH, RTLD_NOW);
1088 BT_ERR("Can not load plugin %s", dlerror());
1089 return BLUETOOTH_ERROR_INTERNAL;
1092 dlerror(); /* Clear any existing error */
1094 int (*fun)(char *) = (int (*)(char *))dlsym(handle,
1095 "bt_app_control_send_launch_request");
1097 if ((error = dlerror()) != NULL) {
1098 BT_ERR("Can not load symbol : %s", dlerror());
1100 return BLUETOOTH_ERROR_INTERNAL;
1103 ret = fun(absolute_path);
1109 int _bt_otp_oacp_write_cb(char *value, int len, int offset,
1110 char *remote_addr, struct indicate_info *info)
1112 int ret = OACP_SUCCESS;
1113 int err = BLUETOOTH_ERROR_NONE;
1114 int opcode = value[0];
1115 uint32_t object_offset, length, object_size;
1118 char absolute_file_path[BT_FILE_PATH_MAX_LEN] = {0, };
1120 BT_INFO("OACP Opcode 0x%d", opcode);
1122 if (!selected_object) {
1123 BT_DBG("Object not selected");
1124 ret = OACP_INVALID_OBJ;
1130 BT_INFO("OACP_CREATE");
1131 object_size = (uint32_t)(value[4] & 0xFF) << 24 |
1132 (uint32_t)(value[3] & 0xFF) << 16 |
1133 (uint32_t)(value[2] & 0xFF) << 8 |
1134 (uint32_t)(value[1] & 0xFF);
1136 uuid = g_strndup(value + 5, len - 5);
1137 BT_INFO("Size = %lu, UUID = %s", object_size, uuid);
1140 /* Create operation already going on. */
1141 ret = OACP_OPERATION_FAILED;
1144 oacp_create = g_malloc0(sizeof(struct oacp_create_operation));
1145 oacp_create->size = object_size;
1146 oacp_create->uuid = g_strdup(uuid);
1148 g_source_remove(timeout_id);
1149 timeout_id = g_timeout_add(BT_OACP_MAX_TIMEOUT,
1150 (GSourceFunc)__bt_oacp_create_timeout_cb, NULL);
1154 snprintf(absolute_file_path, sizeof(absolute_file_path),
1155 "%s%s", directory, selected_object->name);
1157 BT_DBG("absolute_file_path = [%s]", absolute_file_path);
1159 if (remove(absolute_file_path) != 0) {
1160 BT_DBG("Error: unable to delete the file");
1161 ret = OACP_OPERATION_FAILED;
1165 BT_DBG("File deleted successfully");
1166 selected_object = NULL;
1168 case OACP_CALC_CHECKSUM:
1169 ret = OACP_OPCODE_NOT_SUPPORTED;
1172 snprintf(absolute_file_path, sizeof(absolute_file_path),
1173 "file://%s%s", directory, selected_object->name);
1175 BT_DBG("absolute_file_path = [%s]", absolute_file_path);
1177 err = _bt_otp_send_launch_request(absolute_file_path);
1178 if (err != BLUETOOTH_ERROR_NONE) {
1179 BT_DBG("Error: unable to launch the file");
1180 ret = OACP_OPERATION_FAILED;
1184 BT_DBG("Successfully launched the file");
1188 object_offset = (uint32_t)(value[4] & 0xFF) << 24 |
1189 (uint32_t)(value[3] & 0xFF) << 16 |
1190 (uint32_t)(value[2] & 0xFF) << 8 |
1191 (uint32_t)(value[1] & 0xFF);
1192 length = (uint32_t)(value[8] & 0xFF) << 24 |
1193 (uint32_t)(value[7] & 0xFF) << 16 |
1194 (uint32_t)(value[6] & 0xFF) << 8 |
1195 (uint32_t)(value[5] & 0xFF);
1197 if (opcode == OACP_WRITE)
1198 mode = (uint8_t)value[9] & 0xFF;
1200 BT_INFO("Offset = %lu, Length = %lu", object_offset, length, mode);
1203 if (otc_connection_status) {
1204 /* Read/Write operation already going on. */
1205 ret = OACP_OBJECT_LOCKED;
1208 _bt_otp_free_oacp_op();
1211 oacp_op = g_malloc0(sizeof(struct oacp_operation));
1212 oacp_op->offset = object_offset;
1213 oacp_op->length = length;
1214 oacp_op->remote_address = g_strdup(remote_addr);
1215 oacp_op->mode = mode;
1216 oacp_op->opcode = opcode;
1217 oacp_op->length_sofar = 0;
1220 err = _bt_otp_open_otc_and_listen(remote_addr, "ListenOtc");
1221 if (err != BLUETOOTH_ERROR_NONE) {
1222 ret = OACP_OPERATION_FAILED;
1223 _bt_otp_free_oacp_op();
1228 ret = OACP_OPCODE_NOT_SUPPORTED;
1231 ret = OACP_OPCODE_NOT_SUPPORTED;
1235 info->resp_opcode = OACP_RESPONSE;
1236 info->req_opcode = opcode;
1237 info->result_code = ret;
1238 info->resp_param = NULL;
1239 return BLUETOOTH_ERROR_NONE;
1242 void convert_to_hex(struct object_metadata *object, char *type, char *value)
1246 BT_DBG("type : %s", type);
1248 memset(value, 0, 8);
1250 if (!g_strcmp0(type, "size")) {
1252 value[3] = (object->curr_size >> 24) & 0xFF;
1253 value[2] = (object->curr_size >> 16) & 0xFF;
1254 value[1] = (object->curr_size >> 8) & 0xFF;
1255 value[0] = object->curr_size & 0xFF;
1257 value[7] = (object->alloc_size >> 24) & 0xFF;
1258 value[6] = (object->alloc_size >> 16) & 0xFF;
1259 value[5] = (object->alloc_size >> 8) & 0xFF;
1260 value[4] = object->alloc_size & 0xFF;
1262 } else if (!g_strcmp0(type, "date")) {
1264 localtime_r(&(object->first_created), &fc_tm);
1266 value[1] = ((fc_tm.tm_year+1900) >> 8) & 0xFF;
1267 value[0] = (fc_tm.tm_year+1900) & 0xFF;
1268 value[2] = (fc_tm.tm_mon+1) & 0xFF;
1269 value[3] = fc_tm.tm_mday & 0xFF;
1270 value[4] = fc_tm.tm_hour & 0xFF;
1271 value[5] = fc_tm.tm_min & 0xFF;
1272 value[6] = fc_tm.tm_sec & 0xFF;
1274 } else if (!g_strcmp0(type, "id")) {
1276 value[5] = (object->id >> 48) & 0xFF;
1277 value[4] = (object->id >> 32) & 0xFF;
1278 value[3] = (object->id >> 24) & 0xFF;
1279 value[2] = (object->id >> 16) & 0xFF;
1280 value[1] = (object->id >> 8) & 0xFF;
1281 value[0] = object->id & 0xFF;
1283 } else if (!g_strcmp0(type, "props")) {
1284 value[3] = (object->props >> 24) & 0xFF;
1285 value[2] = (object->props >> 16) & 0xFF;
1286 value[1] = (object->props >> 8) & 0xFF;
1287 value[0] = object->props & 0xFF;
1291 void update_obj_metadata_charc_value(struct object_metadata *object)
1293 /* Value can be of maximum eight bytes */
1296 _bt_otp_set_char_value(otp_object_name_obj_path, object->name,
1297 strlen(object->name));
1298 _bt_otp_set_char_value(otp_object_type_obj_path, object->type,
1299 strlen(object->type));
1301 convert_to_hex(object, "size", value);
1302 _bt_otp_set_char_value(otp_object_size_obj_path, value, 8);
1304 convert_to_hex(object, "date", value);
1305 _bt_otp_set_char_value(otp_object_first_created_obj_path, value, 7);
1306 _bt_otp_set_char_value(otp_object_last_modified_obj_path, value, 7);
1308 /* Object ID is optonal for single object server */
1309 if (mutiple_obj_support) {
1310 convert_to_hex(object, "id", value);
1311 _bt_otp_set_char_value(otp_object_id_obj_path, value, 6);
1314 convert_to_hex(object, "props", value);
1315 _bt_otp_set_char_value(otp_object_prop_obj_path, value, 4);
1318 struct object_metadata *_bt_otp_client_find_object(GSList *list, uint64_t id, guint *index)
1321 struct object_metadata *info;
1323 for (l = list; l; l = g_slist_next(l)) {
1327 if (info && (info->id == id))
1333 int _bt_otp_olcp_write_cb(char *value, int len, int offset,
1334 struct indicate_info *info)
1336 int ret = OLCP_SUCCESS;
1337 int opcode = value[0];
1338 struct object_metadata *object;
1342 BT_INFO("OLCP Opcode 0x%d", opcode);
1344 if (!otp_object_list) {
1351 object = (struct object_metadata *) g_slist_nth_data(otp_object_list, 0);
1353 ret = OLCP_OUT_OF_BOUNDS;
1356 update_obj_metadata_charc_value(object);
1357 selected_object = object;
1361 len = g_slist_length(otp_object_list);
1362 object = (struct object_metadata *) g_slist_nth_data(otp_object_list, len-1);
1364 ret = OLCP_OUT_OF_BOUNDS;
1367 update_obj_metadata_charc_value(object);
1368 selected_object = object;
1369 obj_curr_index = len-1;
1372 if (obj_curr_index == 0) {
1373 ret = OLCP_OUT_OF_BOUNDS;
1376 object = (struct object_metadata *) g_slist_nth_data(otp_object_list, obj_curr_index-1);
1378 ret = OLCP_OUT_OF_BOUNDS;
1381 update_obj_metadata_charc_value(object);
1382 selected_object = object;
1383 obj_curr_index -= 1;
1386 object = (struct object_metadata *) g_slist_nth_data(otp_object_list, obj_curr_index+1);
1388 ret = OLCP_OUT_OF_BOUNDS;
1391 update_obj_metadata_charc_value(object);
1392 selected_object = object;
1393 obj_curr_index += 1;
1396 object_id = (uint64_t)(value[6] & 0xFF) << 40 |
1397 (uint64_t)(value[5] & 0xFF) << 32 |
1398 (uint64_t)(value[4] & 0xFF) << 24 |
1399 (uint64_t)(value[3] & 0xFF) << 16 |
1400 (uint64_t)(value[2] & 0xFF) << 8 |
1401 (uint64_t)(value[1] & 0xFF);
1402 BT_INFO("Object ID [%llu]", object_id);
1403 if (selected_object && selected_object->id == object_id)
1406 object = _bt_otp_client_find_object(otp_object_list, object_id, &index);
1408 ret = OLCP_OJECT_ID_NOT_FOUND;
1411 update_obj_metadata_charc_value(object);
1412 selected_object = object;
1413 obj_curr_index = index - 1;
1416 case OLCP_REQ_NO_OBJ:
1417 case OLCP_CLEAR_MARKING:
1419 ret = OLCP_OPCODE_NOT_SUPPORTED;
1423 info->resp_opcode = OLCP_RESPONSE;
1424 info->req_opcode = opcode;
1425 info->result_code = ret;
1426 info->resp_param = NULL;
1427 return BLUETOOTH_ERROR_NONE;
1430 int _bt_otp_obj_name_cb(char *value, int len)
1432 int ret = BLUETOOTH_ERROR_NONE;
1434 struct object_metadata *object = NULL;
1441 filename = g_strndup(value, len);
1442 length = len + strlen(BT_OTP_BASE_DIR_PATH) + 1;
1443 file_path = malloc(length);
1445 snprintf(file_path, length, "%s%s",
1446 BT_OTP_BASE_DIR_PATH, filename);
1447 BT_DBG("file_path = [%s]", file_path);
1449 fp = fopen(file_path, "a");
1451 BT_DBG("fopen() failed : %s", strerror(errno));
1452 ret = BLUETOOTH_ERROR_INTERNAL;
1456 if (stat(file_path, &st) == -1) {
1457 BT_INFO("stat failed: (%d)\n", errno);
1458 ret = BLUETOOTH_ERROR_INTERNAL;
1462 object = g_new0(struct object_metadata, 1);
1464 object->name = g_strdup(filename);
1465 object->type = _otp_convert_uuid_to_uuid128(oacp_create->uuid);
1466 object->first_created = st.st_ctime;
1467 object->last_modified = st.st_ctime;
1468 object->curr_size = (uint32_t)st.st_size;
1469 object->alloc_size = oacp_create->size;
1470 object->id = object_id;
1471 object->props = OBJECT_READ | OBJECT_WRITE;
1473 otp_object_list = g_slist_append(otp_object_list,
1476 update_obj_metadata_charc_value(object);
1477 selected_object = object;
1478 obj_curr_index = g_slist_length(otp_object_list) - 1;
1485 g_free(oacp_create->uuid);
1486 g_free(oacp_create);
1492 static struct otp_char_info *otp_get_char_value(const char *path)
1496 for (tmp = otp_char_list; tmp != NULL; tmp = tmp->next) {
1498 struct otp_char_info *char_info = tmp->data;
1499 if (!g_strcmp0(char_info->char_path, path))
1507 int _bt_otp_read_cb(const char *obj_path, char **value, int *len)
1509 struct otp_char_info *info = NULL;
1512 BT_ERR("Wrong Obj path");
1513 return BLUETOOTH_ERROR_INTERNAL;
1516 if (g_strcmp0(obj_path, otp_feature_obj_path)) {
1517 if (!selected_object) {
1518 return BLUETOOTH_ERROR_OBJECT_NOT_SELECTED;
1522 info = otp_get_char_value(obj_path);
1524 if (info->char_value == NULL || info->value_length == 0)
1525 return BLUETOOTH_ERROR_INTERNAL;
1527 *len = info->value_length;
1528 *value = (char *)malloc(sizeof(char)*(*len));
1529 memcpy(*value, info->char_value, *len);
1531 return BLUETOOTH_ERROR_NONE;
1533 return BLUETOOTH_ERROR_INTERNAL;
1537 static void _otp_convert_address_to_hex(bluetooth_device_address_t *addr_hex,
1538 const char *addr_str)
1541 unsigned int addr[BLUETOOTH_ADDRESS_LENGTH] = { 0, };
1543 if (addr_str == NULL || addr_str[0] == '\0')
1546 i = sscanf(addr_str, "%X:%X:%X:%X:%X:%X", &addr[0], &addr[1],
1547 &addr[2], &addr[3], &addr[4], &addr[5]);
1548 if (i != BLUETOOTH_ADDRESS_LENGTH)
1549 BT_ERR("Invalid format string - [%s]", addr_str);
1551 for (i = 0; i < BLUETOOTH_ADDRESS_LENGTH; i++)
1552 addr_hex->addr[i] = (unsigned char)addr[i];
1555 static void _bt_otp_send_indication(const char *obj_path,
1556 struct indicate_info *info,
1557 bluetooth_device_address_t *remote_address)
1559 int ret = BLUETOOTH_ERROR_NONE;
1560 char value[7] = {0x00};
1564 value[0] = info->resp_opcode & 0xFF;
1565 value[1] = info->req_opcode & 0xFF;
1566 value[2] = info->result_code & 0xFF;
1567 if (info->resp_param) {
1568 value[6] = (info->resp_param[3] >> 24) & 0xFF;
1569 value[5] = (info->resp_param[4] >> 16) & 0xFF;
1570 value[4] = (info->resp_param[5] >> 8) & 0xFF;
1571 value[3] = info->resp_param[6] & 0xFF;
1574 BT_DBG("Opcode: %d", value[1]);
1576 /* Store the status value */
1577 _bt_otp_set_char_value(obj_path, value, 7);
1579 /* Send indication */
1580 ret = bluetooth_gatt_server_set_notification(obj_path, remote_address);
1581 if (ret != BLUETOOTH_ERROR_NONE) {
1582 BT_ERR("_bt_otp_send_control_point_indication failed");
1585 ret = bluetooth_gatt_update_characteristic(obj_path, value, 7);
1586 if (ret != BLUETOOTH_ERROR_NONE) {
1587 BT_ERR("_bt_otp_send_control_point_indication failed");
1592 void _bt_otp_gatt_char_property_changed_event(GVariant *msg,
1595 int result = BLUETOOTH_ERROR_NONE;
1596 GVariantIter value_iter;
1597 const char *property = NULL;
1598 const char *char_path = NULL;
1599 const char *svc_handle = NULL;
1600 GVariant *var = NULL;
1601 GVariant *val = NULL;
1602 g_variant_iter_init(&value_iter, msg);
1604 while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &var))) {
1606 if (property == NULL) {
1607 BT_ERR("Property NULL");
1611 if (!g_strcmp0(property, "WriteValue")) {
1613 BT_INFO("WriteValue");
1614 BT_INFO("Type '%s'\n", g_variant_get_type_string(var));
1617 bluetooth_device_address_t addr_hex = { {0,} };
1622 struct indicate_info info;
1623 g_variant_get(var, "(&s&s&syq@ay)",
1624 &char_path, &svc_handle,
1625 &addr, &req_id, &offset, &val);
1627 len = g_variant_get_size(val);
1629 BT_DBG("Len = %d, BT_ADDR = %s", len, addr);
1631 value = (char *) g_variant_get_data(val);
1632 _otp_convert_address_to_hex(&addr_hex, addr);
1635 if (!g_strcmp0(char_path, otp_oacp_obj_path)) {
1636 result = _bt_otp_oacp_write_cb(value, len, offset, addr, &info);
1637 } else if (!g_strcmp0(char_path, otp_olcp_obj_path)) {
1638 result = _bt_otp_olcp_write_cb(value, len, offset, &info);
1639 } else if (!g_strcmp0(char_path, otp_object_name_obj_path)) {
1641 /* OACP_CREATE is ongoing */
1642 result = _bt_otp_obj_name_cb(value, len);
1644 /* Dont permit writting object name except while creating object.
1645 * As this is directly pointing local objects.
1647 result = BLUETOOTH_ERROR_WRITE_REQUEST_REJECTED;
1649 } else if (!g_strcmp0(char_path, otp_object_first_created_obj_path)) {
1650 _bt_otp_set_char_value(otp_object_first_created_obj_path, value, len);
1651 } else if (!g_strcmp0(char_path, otp_object_last_modified_obj_path)) {
1652 _bt_otp_set_char_value(otp_object_last_modified_obj_path, value, len);
1654 BT_ERR("Wrong Object Path %s", char_path);
1655 result = BLUETOOTH_ERROR_INTERNAL;
1657 bluetooth_gatt_send_response(req_id,
1658 BLUETOOTH_GATT_ATT_REQUEST_TYPE_WRITE,
1659 result, 0, NULL, 0);
1661 /* Send indication for CPs */
1662 if (!g_strcmp0(char_path, otp_oacp_obj_path)) {
1663 if (OACP_indicate) {
1664 _bt_otp_send_indication(char_path, &info, &addr_hex);
1666 } else if (!g_strcmp0(char_path, otp_olcp_obj_path)) {
1667 if (OLCP_indicate) {
1668 _bt_otp_send_indication(char_path, &info, &addr_hex);
1672 BT_ERR("Array Len 0");
1675 BT_ERR("var==NULL");
1677 } else if (!g_strcmp0(property, "ReadValue")) {
1683 result = BLUETOOTH_ERROR_NONE;
1685 BT_INFO("ReadValue");
1686 BT_INFO("Type '%s'\n", g_variant_get_type_string(var));
1688 g_variant_get(var, "(&s&s&syq)", &char_path,
1689 &svc_handle, &addr, &req_id, &offset);
1691 result = _bt_otp_read_cb(char_path, &value, &len);
1693 if (result != BLUETOOTH_ERROR_NONE) {
1694 BT_ERR("ReadValue failed %s", char_path);
1695 bluetooth_gatt_send_response(req_id,
1696 BLUETOOTH_GATT_ATT_REQUEST_TYPE_READ,
1697 result, offset, NULL, 0);
1699 bluetooth_gatt_send_response(req_id,
1700 BLUETOOTH_GATT_ATT_REQUEST_TYPE_READ,
1701 result, offset, value, len);
1705 } else if (!g_strcmp0(property, "NotificationStateChanged")) {
1706 gboolean indicate = FALSE;
1708 g_variant_get(var, "(&s&sb)", &char_path,
1709 &svc_handle, &indicate);
1711 BT_INFO("%s : [%s]", property,
1712 indicate ? "StartNotify" : "StopNotify");
1713 BT_INFO("Type '%s'\n", g_variant_get_type_string(var));
1715 if (!g_strcmp0(char_path, otp_oacp_obj_path)) {
1716 OACP_indicate = indicate;
1717 } else if (!g_strcmp0(char_path, otp_olcp_obj_path)) {
1718 OLCP_indicate = indicate;
1725 void _bt_otp_property_event_filter(GDBusConnection *connection,
1726 const gchar *sender_name,
1727 const gchar *object_path,
1728 const gchar *interface_name,
1729 const gchar *signal_name,
1730 GVariant *parameters,
1735 if (signal_name == NULL) {
1736 BT_ERR("Wrong Signal");
1740 if (g_strcmp0(signal_name, PROPERTIES_CHANGED) == 0) {
1742 g_variant_get(parameters, "(@a{sv}@as)", &value, NULL);
1743 _bt_otp_gatt_char_property_changed_event(value, object_path);
1747 void _bt_otp_adapter_event_filter(GDBusConnection *connection,
1748 const gchar *sender_name,
1749 const gchar *object_path,
1750 const gchar *interface_name,
1751 const gchar *signal_name,
1752 GVariant *parameters,
1755 if (signal_name == NULL) {
1756 BT_ERR("Wrong Signal");
1760 BT_INFO("Interface %s, Signal %s", interface_name, signal_name);
1762 if (g_strcmp0(interface_name, BT_OTP_INTERFACE_NAME) == 0) {
1763 if (strcasecmp(signal_name, BLE_DISABLED) == 0) {
1769 void _bt_otc_disconnected_cb(GDBusConnection *connection,
1770 const gchar *sender_name,
1771 const gchar *object_path,
1772 const gchar *interface_name,
1773 const gchar *signal_name,
1774 GVariant *parameters,
1777 if (signal_name == NULL) {
1778 BT_ERR("Wrong Signal");
1782 BT_INFO("Interface %s, Signal %s", interface_name, signal_name);
1784 if (g_strcmp0(interface_name, BT_DEVICE_INTERFACE) == 0) {
1785 if (strcasecmp(signal_name, OTC_DISCONNECTED) == 0) {
1786 BT_DBG("OTC Channel Disconnected dev_path[%s]",
1788 otc_connection_status = FALSE;
1789 _bt_otp_free_oacp_op();
1794 int _bt_otp_init_event_receiver()
1797 GError *error = NULL;
1800 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
1801 if (error != NULL) {
1802 BT_ERR("ERROR: Can't get on system bus [%s]",
1804 g_clear_error(&error);
1808 property_sub_id = g_dbus_connection_signal_subscribe(conn,
1810 BT_OTP_INTERFACE_NAME,
1812 BT_OTP_OBJECT_PATH, NULL, 0,
1813 _bt_otp_property_event_filter,
1816 adapter_sub_id = g_dbus_connection_signal_subscribe(conn,
1818 BT_OTP_INTERFACE_NAME,
1820 BT_OTP_OBJECT_PATH, NULL, 0,
1821 _bt_otp_adapter_event_filter,
1824 device_sub_id = g_dbus_connection_signal_subscribe(conn,
1825 NULL, BT_DEVICE_INTERFACE,
1826 OTC_DISCONNECTED, NULL, NULL, 0,
1827 _bt_otc_disconnected_cb,
1834 void _bt_otp_deinit_event_receiver(void)
1838 g_dbus_connection_signal_unsubscribe(conn, property_sub_id);
1839 g_dbus_connection_signal_unsubscribe(conn, adapter_sub_id);
1840 g_dbus_connection_signal_unsubscribe(conn, device_sub_id);
1846 static void _bt_otp_sig_handler(int sig)
1851 BT_DBG("caught signal - sigterm\n");
1854 BT_DBG("caught signal - sigint\n");
1857 BT_DBG("caught signal - sigkill\n");
1860 BT_DBG("caught signal %d and ignored\n", sig);
1866 /* OTP Service Main loop */
1869 struct sigaction sa;
1870 BT_ERR("Starting the bt-otp daemon");
1872 memset(&sa, 0, sizeof(sa));
1873 sa.sa_handler = _bt_otp_sig_handler;
1874 sa.sa_flags = SA_SIGINFO;
1875 sigaction(SIGINT, &sa, NULL);
1876 sigaction(SIGTERM, &sa, NULL);
1877 sigaction(SIGKILL, &sa, NULL);
1879 if (_bt_otp_register_interface() != BLUETOOTH_ERROR_NONE) {
1880 BT_ERR("Fail to register otp service");
1884 if (_bt_otp_init_event_receiver() != BLUETOOTH_ERROR_NONE) {
1885 BT_ERR("Fail to init event reciever");
1889 main_loop = g_main_loop_new(NULL, FALSE);
1891 g_main_loop_run(main_loop);
1893 BT_DBG("g_main_loop_quit called!");
1895 if (main_loop != NULL) {
1896 g_main_loop_unref(main_loop);