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>
31 #include <arpa/inet.h>
33 #include "bt-otpserver.h"
34 #include "bluetooth-api.h"
38 #define LOG_TAG "BLUETOOTH_OTP"
40 #define BT_INFO(fmt, arg...) SLOGI(fmt, ##arg)
41 #define BT_ERR(fmt, arg...) SLOGE(fmt, ##arg)
42 #define BT_DBG(fmt, arg...) SLOGD(fmt, ##arg)
44 /* OTP object paths */
45 char *otp_obj_path = NULL;
46 char *otp_feature_obj_path = NULL;
47 char *otp_object_name_obj_path = NULL;
48 char *otp_object_type_obj_path = NULL;
49 char *otp_object_size_obj_path = NULL;
50 char *otp_object_first_created_obj_path = NULL;
51 char *otp_object_last_modified_obj_path = NULL;
52 char *otp_object_id_obj_path = NULL;
53 char *otp_object_prop_obj_path = NULL;
54 char *otp_oacp_obj_path = NULL;
55 char *otp_olcp_obj_path = NULL;
56 char *otp_oacp_desc_obj_path = NULL;
57 char *otp_olcp_desc_obj_path = NULL;
59 static GMainLoop *main_loop;
60 GDBusNodeInfo *otp_node_info = NULL;
61 static GDBusConnection *conn;
62 static GDBusConnection *g_conn;
64 static int property_sub_id = -1;
65 static int adapter_sub_id = -1;
66 static int device_sub_id = -1;
67 static guint g_owner_id = 0;
68 static guint server_watch_id = 0;
70 struct otp_char_info {
76 struct indicate_info {
84 struct object_metadata {
95 struct oacp_operation {
100 uint32_t length_sofar;
106 static struct object_metadata *selected_object = NULL;
107 static uint64_t object_id = OBJECT_START_ID;
108 static GSList *otp_object_list = NULL;
109 static GSList *otp_char_list = NULL;
110 static guint curr_obj_index;
111 static int adv_handle = 0;
112 static gboolean OACP_indicate = FALSE;
113 static gboolean OLCP_indicate = FALSE;
114 char *directory = NULL;
115 gboolean mutiple_obj_support = false;
116 static gboolean otc_connection_status = FALSE;
117 struct oacp_operation *oacp_op = NULL;
118 unsigned int timeout_id;
119 unsigned int oacp_create_timeout_id;
120 uint64_t curr_obj_id, prev_obj_id;
122 static const gchar otp_introspection_xml[] =
124 " <interface name='org.projectx.otp_service'>"
125 " <method name='enable'>"
126 " <arg type='s' name='directory'/>"
127 " <arg type='i' name='status' direction='out'/>"
129 " <method name='disable'>"
130 " <arg type='i' name='status' direction='out'/>"
132 " <method name='NewConnection'>"
133 " <arg type='o' name='object' direction='in'/>"
134 " <arg type='h' name='fd' direction='in'/>"
139 void _bt_otp_deinit_event_receiver(void);
140 void _bt_otp_unregister_interface(void);
141 void update_obj_metadata_charc_value(struct object_metadata *object);
142 void _bt_convert_device_path_to_address(const char *device_path,
143 char *device_address);
144 int _bt_otp_open_otc_and_listen(char *address, char *method);
145 void _bt_otp_delete_empty_file();
146 struct object_metadata *_bt_otp_client_find_object(GSList *list,
147 uint64_t id, guint *index);
149 static void delete_all_objects(void)
152 for (tmp = otp_object_list; tmp != NULL; tmp = tmp->next) {
154 struct object_metadata *obj_info = tmp->data;
156 g_free(obj_info->name);
158 g_free(obj_info->type);
159 otp_object_list = g_slist_delete_link(otp_object_list, tmp->data);
162 g_slist_free(otp_object_list);
163 otp_object_list = NULL;
166 static void delete_all_characterisitc(void)
169 for (tmp = otp_char_list; tmp != NULL; tmp = tmp->next) {
171 struct otp_char_info *char_info = tmp->data;
172 if (char_info->char_path)
173 g_free(char_info->char_path);
174 if (char_info->char_value)
175 g_free(char_info->char_value);
176 otp_char_list = g_slist_delete_link(otp_char_list, tmp->data);
179 g_slist_free(otp_char_list);
180 otp_char_list = NULL;
183 void _bt_otp_exit(void)
189 delete_all_characterisitc();
192 delete_all_objects();
194 ret = bluetooth_gatt_deinit();
195 if (ret != BLUETOOTH_ERROR_NONE)
196 BT_ERR("Failed to Deinit GATT %d", ret);
198 _bt_otp_deinit_event_receiver();
200 _bt_otp_unregister_interface();
202 /* TODO: Advertising is not getting stopped by this API.
203 * This is because OTP_SERVER_DEINIT dbus call is blocking
204 * BT_SET_ADVERTISING_DATA dbus call. But now advertisment
205 * is stopped because of terminated process logic.
207 ret = bluetooth_set_advertising(adv_handle, FALSE);
208 if (ret != BLUETOOTH_ERROR_NONE)
209 BT_ERR("Failed to stop ADV %d", ret);
211 if (main_loop != NULL) {
212 g_main_loop_quit(main_loop);
216 static void _bt_otp_set_char_value(const char *obj_path,
217 const char *value, int value_length)
223 for (tmp = otp_char_list; tmp != NULL; tmp = tmp->next) {
225 struct otp_char_info *char_info = tmp->data;
226 if (!g_strcmp0(char_info->char_path, obj_path)) {
227 char_info->char_value = g_try_realloc(char_info->char_value, value_length);
228 if (char_info->char_value) {
229 memcpy(char_info->char_value, value, value_length);
230 char_info->value_length = value_length;
239 int add_new_characteristic(const char *char_uuid, bt_gatt_permission_t perms,
240 bt_gatt_characteristic_property_t props, char **obj_path)
242 int ret = BLUETOOTH_ERROR_NONE;
243 struct otp_char_info *char_info = NULL;
245 ret = bluetooth_gatt_add_new_characteristic(otp_obj_path,
246 char_uuid, perms, props, obj_path);
247 if (ret != BLUETOOTH_ERROR_NONE) {
248 BT_ERR("Failed to add new char %d", ret);
252 char_info = g_new0(struct otp_char_info, 1);
253 char_info->char_path = g_strdup(*obj_path);
254 otp_char_list = g_slist_append(otp_char_list, char_info);
259 static char *_otp_convert_uuid_to_uuid128(const char *uuid)
269 uuid128 = g_strdup_printf("0000%s-0000-1000-8000-00805f9b34fb",
275 uuid128 = g_strdup_printf("%s-0000-1000-8000-00805f9b34fb",
281 uuid128 = strdup(uuid);
291 int _bt_otp_prepare_ots(void)
294 int ret = BLUETOOTH_ERROR_NONE;
298 bt_gatt_characteristic_property_t props;
299 bt_gatt_permission_t perms;
300 char supp_feat[OTP_FEATURE_LENGTH] = { 0x3B, 0x00, 0x00, 0x00,
301 0x01, 0x00, 0x00, 0x00 };
303 ret = bluetooth_gatt_init();
304 if (ret != BLUETOOTH_ERROR_NONE) {
305 BT_ERR("Failed to Init GATT %d", ret);
309 service_uuid = _otp_convert_uuid_to_uuid128(OTP_UUID);
310 ret = bluetooth_gatt_add_service(service_uuid, &otp_obj_path);
311 if (ret != BLUETOOTH_ERROR_NONE) {
312 BT_ERR("Failed to add service %d", ret);
316 /* Characteristic OTP Feature */
317 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ;
318 perms = BLUETOOTH_GATT_PERMISSION_READ;
319 char_uuid = _otp_convert_uuid_to_uuid128(OTP_FEATURE_UUID);
320 ret = add_new_characteristic(char_uuid, perms, props,
321 &otp_feature_obj_path);
322 if (ret != BLUETOOTH_ERROR_NONE)
325 ret = bluetooth_gatt_set_characteristic_value(otp_feature_obj_path,
326 supp_feat, OTP_FEATURE_LENGTH);
327 if (ret != BLUETOOTH_ERROR_NONE) {
328 BT_ERR("Failed to set char value %d", ret);
332 _bt_otp_set_char_value(otp_feature_obj_path, supp_feat,
335 /* Characteristic Object Name */
336 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ |
337 BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE;
338 perms = BLUETOOTH_GATT_PERMISSION_READ |
339 BLUETOOTH_GATT_PERMISSION_WRITE;
340 char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_NAME_UUID);
341 ret = add_new_characteristic(char_uuid, perms, props,
342 &otp_object_name_obj_path);
343 if (ret != BLUETOOTH_ERROR_NONE)
346 /* Characteristic Object Type */
347 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ;
348 perms = BLUETOOTH_GATT_PERMISSION_READ;
349 char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_TYPE_UUID);
350 ret = add_new_characteristic(char_uuid, perms, props,
351 &otp_object_type_obj_path);
352 if (ret != BLUETOOTH_ERROR_NONE)
355 /* Characteristic Object Size */
356 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ;
357 perms = BLUETOOTH_GATT_PERMISSION_READ;
358 char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_SIZE_UUID);
359 ret = add_new_characteristic(char_uuid, perms, props,
360 &otp_object_size_obj_path);
361 if (ret != BLUETOOTH_ERROR_NONE)
364 /* Characteristic Object First-Created */
365 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ |
366 BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE;
367 perms = BLUETOOTH_GATT_PERMISSION_READ |
368 BLUETOOTH_GATT_PERMISSION_WRITE;
369 char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_FIRST_CREATED_UUID);
370 ret = add_new_characteristic(char_uuid, perms, props,
371 &otp_object_first_created_obj_path);
372 if (ret != BLUETOOTH_ERROR_NONE)
375 /* Characteristic Object Last-Modified */
376 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ |
377 BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE;
378 perms = BLUETOOTH_GATT_PERMISSION_READ |
379 BLUETOOTH_GATT_PERMISSION_WRITE;
380 char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_LAST_MODIFIED_UUID);
381 ret = add_new_characteristic(char_uuid, perms, props,
382 &otp_object_last_modified_obj_path);
383 if (ret != BLUETOOTH_ERROR_NONE)
386 /* Object ID is mandatory for mutiple object server */
387 if (mutiple_obj_support) {
388 /* Characteristic Object ID */
389 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ;
390 perms = BLUETOOTH_GATT_PERMISSION_READ;
391 char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_ID_UUID);
392 ret = add_new_characteristic(char_uuid, perms, props,
393 &otp_object_id_obj_path);
394 if (ret != BLUETOOTH_ERROR_NONE)
398 /* Characteristic Object Properties */
399 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ |
400 BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE;
401 perms = BLUETOOTH_GATT_PERMISSION_READ |
402 BLUETOOTH_GATT_PERMISSION_WRITE;
403 char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_PROP_UUID);
404 ret = add_new_characteristic(char_uuid, perms, props,
405 &otp_object_prop_obj_path);
406 if (ret != BLUETOOTH_ERROR_NONE)
409 /* Characteristic OACP */
410 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE |
411 BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_INDICATE;
412 perms = BLUETOOTH_GATT_PERMISSION_WRITE;
413 char_uuid = _otp_convert_uuid_to_uuid128(OTP_OACP_UUID);
414 ret = add_new_characteristic(char_uuid, perms, props,
416 if (ret != BLUETOOTH_ERROR_NONE)
420 desc_uuid = _otp_convert_uuid_to_uuid128(OTP_CP_CCC_DESC_UUID);
421 perms = BLUETOOTH_GATT_PERMISSION_READ |
422 BLUETOOTH_GATT_PERMISSION_WRITE;
423 ret = bluetooth_gatt_add_descriptor(otp_oacp_obj_path, desc_uuid,
424 perms, &otp_oacp_desc_obj_path);
425 if (ret != BLUETOOTH_ERROR_NONE) {
426 BT_ERR("Failed to add new char descriptor %d", ret);
430 /* OLCP Characteristics is not required
431 * for single object server
433 if (mutiple_obj_support) {
434 /* Characteristic OLCP */
435 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE |
436 BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_INDICATE;
437 perms = BLUETOOTH_GATT_PERMISSION_WRITE;
438 char_uuid = _otp_convert_uuid_to_uuid128(OTP_OLCP_UUID);
439 ret = add_new_characteristic(char_uuid, perms, props,
441 if (ret != BLUETOOTH_ERROR_NONE)
445 desc_uuid = _otp_convert_uuid_to_uuid128(OTP_CP_CCC_DESC_UUID);
446 perms = BLUETOOTH_GATT_PERMISSION_READ |
447 BLUETOOTH_GATT_PERMISSION_WRITE;
448 ret = bluetooth_gatt_add_descriptor(otp_olcp_obj_path, desc_uuid,
449 perms, &otp_olcp_desc_obj_path);
450 if (ret != BLUETOOTH_ERROR_NONE) {
451 BT_ERR("Failed to add new char descriptor %d", ret);
456 /* Register service */
457 ret = bluetooth_gatt_register_service(otp_obj_path);
458 if (ret != BLUETOOTH_ERROR_NONE) {
459 BT_ERR("Failed to register service %d", ret);
463 /* Register Application */
464 ret = bluetooth_gatt_register_application();
465 if (ret != BLUETOOTH_ERROR_NONE) {
466 BT_ERR("Failed to register application %d", ret);
474 delete_all_characterisitc();
478 int _bt_otp_set_advertising_data(void)
484 guint8 data[4] = {0x03, 0x02, 0x25, 0x18};
485 bluetooth_advertising_data_t adv;
487 BT_DBG("%x %x %x %x", data[0], data[1], data[2], data[3]);
488 memcpy(adv.data, data, sizeof(data));
489 ret = bluetooth_set_advertising_data(adv_handle, &adv, sizeof(data));
490 if (ret != BLUETOOTH_ERROR_NONE) {
491 BT_ERR("Failed to set ADV data %d", ret);
495 ret = bluetooth_set_advertising(adv_handle, TRUE);
496 if (ret != BLUETOOTH_ERROR_NONE) {
497 BT_ERR("Failed to set ADV %d", ret);
504 void _bt_otp_start_write_on_fd()
506 char buf[BT_L2CAP_BUFFER_LEN];
511 char file_path[BT_FILE_PATH_MAX_LEN] = {0, };
514 snprintf(file_path, sizeof(file_path), "%s%s",
515 directory, selected_object->name);
516 BT_DBG("file_path = [%s]", file_path);
518 fp = fopen(file_path, "r");
520 BT_DBG("fopen() failed : %s", strerror(errno));
524 BT_DBG("length [%d]", oacp_op->length);
525 length = oacp_op->length;
528 if (length < BT_L2CAP_BUFFER_LEN)
531 len = BT_L2CAP_BUFFER_LEN;
533 read = fread(buf, 1, len, fp);
534 written = write(oacp_op->fd, buf, len);
541 BT_DBG("read [%d], written [%d], rem_len [%d]",
542 read, written, length);
549 static bool __bt_otc_connection_timeout_cb(gpointer user_data)
551 int err = BLUETOOTH_ERROR_NONE;
552 char *remote_addr = oacp_op->remote_address;
554 err = _bt_otp_open_otc_and_listen(remote_addr, "DisconnectOtc");
555 if (err != BLUETOOTH_ERROR_NONE)
556 BT_ERR("Disconnect OTC failed");
561 static gboolean __server_data_received_cb(GIOChannel *chan, GIOCondition cond,
564 char *remote_addr = oacp_op->remote_address;
565 GIOStatus status = G_IO_STATUS_NORMAL;
574 fd = g_io_channel_unix_get_fd(chan);
575 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
576 otc_connection_status = FALSE;
577 BT_ERR("OTC disconnected: %d", fd);
579 g_source_remove(server_watch_id);
583 buffer = g_malloc0(BT_L2CAP_BUFFER_LEN + 1);
585 status = g_io_channel_read_chars(chan, buffer,
588 if (status != G_IO_STATUS_NORMAL) {
589 BT_ERR("IO Channel read is failed with %d", status);
593 otc_connection_status = FALSE;
594 BT_ERR("IO Channel read error [%s]", err->message);
595 if (status == G_IO_STATUS_ERROR) {
596 BT_ERR("cond : %d", cond);
599 g_source_remove(server_watch_id);
607 BT_DBG("Received data length %d, remote_addr = %s", len, remote_addr);
610 char file_path[BT_FILE_PATH_MAX_LEN] = {0, };
613 if (!selected_object) {
614 BT_DBG("Object not selected");
618 snprintf(file_path, sizeof(file_path), "%s%s",
619 directory, selected_object->name);
621 BT_DBG("file_path = [%s]", file_path);
622 fp = fopen(file_path, "w");
624 BT_DBG("fopen() failed : %s", strerror(errno));
630 if (oacp_op->length_sofar <= oacp_op->length) {
631 written = fwrite(buffer, 1, len, oacp_op->fp);
632 oacp_op->length_sofar += written;
633 BT_DBG("written [%d], length_sofar [%lu], received_buff_len [%d], size [%lu]",
634 written, oacp_op->length_sofar, len, oacp_op->length);
637 if (timeout_id > 0) {
638 g_source_remove(timeout_id);
639 timeout_id = g_timeout_add(BT_OACP_MAX_TIMEOUT,
640 (GSourceFunc)__bt_otc_connection_timeout_cb, NULL);
647 static void _bt_otp_start_read_on_fd()
650 data_io = g_io_channel_unix_new(oacp_op->fd);
652 g_io_channel_set_encoding(data_io, NULL, NULL);
653 g_io_channel_set_flags(data_io, G_IO_FLAG_NONBLOCK, NULL);
655 server_watch_id = g_io_add_watch(data_io,
656 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
657 __server_data_received_cb, NULL);
660 g_source_remove(timeout_id);
662 timeout_id = g_timeout_add(BT_OACP_MAX_TIMEOUT,
663 (GSourceFunc)__bt_otc_connection_timeout_cb, NULL);
666 static void _bt_otp_method(GDBusConnection *connection,
668 const gchar *object_path,
669 const gchar *interface_name,
670 const gchar *method_name,
671 GVariant *parameters,
672 GDBusMethodInvocation *invocation,
676 int status = BLUETOOTH_ERROR_NONE;
678 BT_DBG("Method[%s] Object Path[%s] Interface Name[%s]",
679 method_name, object_path, interface_name);
681 if (g_strcmp0(method_name, "enable") == 0) {
683 GError *error = NULL;
684 const gchar *filename = NULL;
685 char absolute_path[BT_FILE_PATH_MAX_LEN];
686 GSList *list = NULL, *l = NULL;
688 struct object_metadata *object = NULL;
690 g_variant_get(parameters, "(s)", &directory);
691 BT_DBG("Directory = [%s]", directory);
693 dir = g_dir_open(directory, 0, &error);
695 BT_ERR("Failed to open directory: %s", error->message);
697 status = BLUETOOTH_ERROR_INVALID_DIRECTORY;
701 while ((filename = g_dir_read_name(dir))) {
702 list = g_slist_append(list, (gpointer) filename);
708 BT_DBG("No object found in given directory");
709 status = BLUETOOTH_ERROR_NO_OBJECTS_FOUND;
713 if (g_slist_length(list) > 1)
714 mutiple_obj_support = true;
716 for (l = list; l != NULL; l = l->next) {
717 if (!l->data) continue;
718 snprintf(absolute_path, sizeof(absolute_path), "%s%s", directory,
721 BT_INFO("filename: %s, absoulte_path: %s",
722 (char *)l->data, absolute_path);
724 if (stat(absolute_path, &st) == -1) {
725 BT_INFO("stat failed: (%d)\n", errno);
729 object = g_new0(struct object_metadata, 1);
731 object->name = g_strdup((const gchar *)l->data);
732 object->type = g_strdup(UNSUPPORTED_OBJECT_TYPE_UUID);
733 object->first_created = st.st_ctime;
734 object->last_modified = st.st_ctime;
735 object->curr_size = (uint32_t) st.st_size;
736 object->alloc_size = (uint32_t) st.st_size;
737 object->id = object_id;
738 object->props = OBJECT_READ | OBJECT_WRITE |
739 OBJECT_EXECUTE | OBJECT_DELETE;
741 otp_object_list = g_slist_append(otp_object_list,
748 if (_bt_otp_prepare_ots() != BLUETOOTH_ERROR_NONE) {
749 BT_ERR("Fail to prepare OTP Proxy");
750 status = BLUETOOTH_ERROR_INTERNAL;
754 /* If single object is supported, make that as
755 * selected object and update the metadata for the same.
757 if (!mutiple_obj_support) {
758 BT_INFO("Server supports single object");
759 selected_object = (struct object_metadata *) g_slist_nth_data(otp_object_list, 0);
761 update_obj_metadata_charc_value(selected_object);
764 BT_DBG("advertsing");
765 if (_bt_otp_set_advertising_data() != BLUETOOTH_ERROR_NONE) {
766 BT_ERR("Fail to set advertising data");
767 status = BLUETOOTH_ERROR_INTERNAL;
771 g_dbus_method_invocation_return_value(invocation,
772 g_variant_new("(i)", status));
774 } else if (g_strcmp0(method_name, "disable") == 0) {
775 g_dbus_method_invocation_return_value(invocation,
776 g_variant_new("(i)", status));
779 } else if (g_strcmp0(method_name, "NewConnection") == 0) {
782 GUnixFDList *fd_list;
784 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
787 g_variant_get(parameters, "(oh)", &dev_path, &index);
789 msg = g_dbus_method_invocation_get_message(invocation);
790 fd_list = g_dbus_message_get_unix_fd_list(msg);
791 if (fd_list == NULL) {
792 BT_ERR("fd_list is NULL");
796 fd = g_unix_fd_list_get(fd_list, index, NULL);
798 BT_ERR("Invalid fd return");
802 _bt_convert_device_path_to_address(dev_path, address);
804 BT_INFO("OTC Connected fd: %d, address %s", fd, address);
805 otc_connection_status = TRUE;
809 if (oacp_op->opcode == OACP_READ)
810 _bt_otp_start_write_on_fd();
811 else if (oacp_op->opcode == OACP_WRITE)
812 _bt_otp_start_read_on_fd();
814 g_dbus_method_invocation_return_value(invocation, NULL);
819 static const GDBusInterfaceVTable otp_method_table = {
825 static void _bt_otp_on_bus_acquired(GDBusConnection *connection,
826 const gchar *name, gpointer user_data)
829 GError *error = NULL;
835 object_id = g_dbus_connection_register_object(connection,
837 otp_node_info->interfaces[0],
840 if (object_id == 0) {
841 BT_ERR("Failed to register method table: %s", error->message);
843 g_dbus_node_info_unref(otp_node_info);
849 static void _bt_otp_on_name_acquired(GDBusConnection *connection,
856 static void _bt_otp_on_name_lost(GDBusConnection *connection,
861 g_object_unref(g_conn);
863 g_dbus_node_info_unref(otp_node_info);
864 g_bus_unown_name(g_owner_id);
867 int _bt_otp_register_interface(void)
870 GError *error = NULL;
873 otp_node_info = g_dbus_node_info_new_for_xml(otp_introspection_xml, &error);
874 if (!otp_node_info) {
875 BT_ERR("Failed to install: %s", error->message);
876 return BLUETOOTH_ERROR_INTERNAL;
879 owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
881 G_BUS_NAME_OWNER_FLAGS_NONE,
882 _bt_otp_on_bus_acquired,
883 _bt_otp_on_name_acquired,
884 _bt_otp_on_name_lost,
886 g_owner_id = owner_id;
887 BT_DBG("owner_id is [%d]\n", owner_id);
890 return BLUETOOTH_ERROR_NONE;
893 void _bt_otp_unregister_interface(void)
897 g_object_unref(g_conn);
899 g_dbus_node_info_unref(otp_node_info);
900 g_bus_unown_name(g_owner_id);
906 void _bt_convert_device_path_to_address(const char *device_path,
907 char *device_address)
909 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
912 dev_addr = strstr(device_path, "dev_");
913 if (dev_addr != NULL) {
916 g_strlcpy(address, dev_addr, sizeof(address));
918 while ((pos = strchr(address, '_')) != NULL)
921 g_strlcpy(device_address, address, BT_ADDRESS_STRING_SIZE);
925 static char *__bt_extract_device_path(GVariantIter *iter, char *address)
927 char *object_path = NULL;
928 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
930 /* Parse the signature: oa{sa{sv}}} */
931 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path,
934 BT_ERR("Unable to get object path");
937 _bt_convert_device_path_to_address(object_path, device_address);
938 if (g_strcmp0(address, device_address) == 0)
939 return g_strdup(object_path);
943 BT_ERR("Unable to get object path");
947 char *_bt_otp_get_device_object_path(char *address)
950 GDBusProxy *proxy = NULL;
951 GVariant *result = NULL;
952 GVariantIter *iter = NULL;
953 char *object_path = NULL;
955 proxy = g_dbus_proxy_new_sync(conn,
956 G_DBUS_PROXY_FLAGS_NONE, NULL,
959 BT_MANAGER_INTERFACE,
963 BT_ERR("Unable to create proxy: %s", err->message);
967 result = g_dbus_proxy_call_sync(proxy, "GetManagedObjects", NULL,
968 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
971 BT_ERR("Fail to get GetManagedObjects (Error: %s)", err->message);
973 BT_ERR("Fail to get GetManagedObjects");
978 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
979 object_path = __bt_extract_device_path(iter, address);
981 g_variant_unref(result);
982 g_variant_iter_free(iter);
989 g_object_unref(proxy);
994 int _bt_otp_open_otc_and_listen(char *address, char *method)
997 GDBusProxy *device_proxy = NULL;
998 GVariant *result = NULL;
999 GError *error = NULL;
1000 int ret = BLUETOOTH_ERROR_NONE;
1003 return BLUETOOTH_ERROR_INTERNAL;
1005 if (g_strcmp0(method, "ListenOtc") &&
1006 g_strcmp0(method, "DisconnectOtc"))
1007 return BLUETOOTH_ERROR_INTERNAL;
1009 object_path = _bt_otp_get_device_object_path(address);
1010 if (object_path == NULL) {
1011 ret = BLUETOOTH_ERROR_NOT_PAIRED;
1015 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1016 NULL, BT_BLUEZ_NAME, object_path,
1017 BT_DEVICE_INTERFACE, NULL, NULL);
1018 if (device_proxy == NULL) {
1019 ret = BLUETOOTH_ERROR_INTERNAL;
1024 result = g_dbus_proxy_call_sync(device_proxy, method,
1026 G_DBUS_CALL_FLAGS_NONE,
1030 if (result == NULL) {
1031 if (error != NULL) {
1032 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1033 g_error_free(error);
1035 ret = BLUETOOTH_ERROR_INTERNAL;
1039 g_free(object_path);
1041 g_variant_unref(result);
1043 g_object_unref(device_proxy);
1047 static bool __bt_oacp_create_timeout_cb(gpointer user_data)
1049 /* Delete the EMPTY object */
1050 _bt_otp_delete_empty_file();
1054 static void _bt_otp_free_oacp_op()
1056 if (timeout_id > 0) {
1057 g_source_remove(timeout_id);
1062 g_free(oacp_op->remote_address);
1064 fclose(oacp_op->fp);
1070 int _bt_otp_send_launch_request(char *absolute_path)
1076 /* check ARCH 64 or 32*/
1077 if (!access(FILEPATH_ARCH_64, 0)) {
1078 BT_INFO("plugin loading for ARCH 64");
1079 handle = dlopen(HEADED_PLUGIN_FILEPATH64, RTLD_NOW);
1081 BT_INFO("plugin loading for ARCH 32");
1082 handle = dlopen(HEADED_PLUGIN_FILEPATH, RTLD_NOW);
1086 BT_ERR("Can not load plugin %s", dlerror());
1087 return BLUETOOTH_ERROR_INTERNAL;
1090 dlerror(); /* Clear any existing error */
1092 int (*fun)(char *) = (int (*)(char *))dlsym(handle,
1093 "bt_app_control_send_launch_request");
1095 if ((error = dlerror()) != NULL) {
1096 BT_ERR("Can not load symbol : %s", dlerror());
1098 return BLUETOOTH_ERROR_INTERNAL;
1101 ret = fun(absolute_path);
1107 char *_bt_otp_uuid_convert_hex_to_string(char *value, uint32_t length)
1111 unsigned short data1;
1112 unsigned short data2;
1113 unsigned short data3;
1115 unsigned short data5;
1118 uuid = (char *) g_malloc0(2 * length * sizeof(char));
1123 memcpy(&data1, &value[0], 2);
1124 snprintf(uuid, n, "%.4x", ntohs(data1));
1127 memcpy(&data0, &value[0], 4);
1128 snprintf(uuid, n, "%.8x", ntohl(data0));
1131 memcpy(&data0, &value[0], 4);
1132 memcpy(&data1, &value[4], 2);
1133 memcpy(&data2, &value[6], 2);
1134 memcpy(&data3, &value[8], 2);
1135 memcpy(&data4, &value[10], 4);
1136 memcpy(&data5, &value[14], 2);
1138 snprintf(uuid, n + 4, "%.8x-%.4x-%.4x-%.4x-%.8x%.4x",
1139 ntohl(data0), ntohs(data1),
1140 ntohs(data2), ntohs(data3),
1141 ntohl(data4), ntohs(data5));
1151 int _bt_otp_create_empty_file(uint32_t size, char *uuid)
1153 int ret = BLUETOOTH_ERROR_NONE;
1154 struct object_metadata *object = NULL;
1156 char absolute_file_path[BT_FILE_PATH_MAX_LEN] = {0, };
1159 snprintf(absolute_file_path, sizeof(absolute_file_path),
1160 "%s%s", directory, BT_OTP_EMPTY_FILENAME);
1161 BT_DBG("file_path = [%s]", absolute_file_path);
1163 fp = fopen(absolute_file_path, "a");
1165 BT_DBG("fopen() failed : %s", strerror(errno));
1166 ret = BLUETOOTH_ERROR_INTERNAL;
1170 if (stat(absolute_file_path, &st) == -1) {
1171 BT_INFO("stat failed: (%d)\n", errno);
1172 ret = BLUETOOTH_ERROR_INTERNAL;
1176 /* Store current object id.
1177 * Incase of OACP Create fail, need to restore
1180 prev_obj_id = selected_object->id;
1182 object = g_new0(struct object_metadata, 1);
1184 object->name = g_strdup(BT_OTP_EMPTY_FILENAME);
1185 object->type = g_strdup(uuid);
1186 object->first_created = st.st_ctime;
1187 object->last_modified = st.st_ctime;
1188 object->curr_size = (uint32_t)st.st_size;
1189 object->alloc_size = size;
1190 object->id = object_id;
1191 object->props = OBJECT_READ | OBJECT_WRITE;
1193 otp_object_list = g_slist_append(otp_object_list,
1196 update_obj_metadata_charc_value(object);
1197 selected_object = object;
1198 curr_obj_index = g_slist_length(otp_object_list) - 1;
1199 curr_obj_id = selected_object->id;
1208 void _bt_otp_delete_empty_file()
1210 struct object_metadata *object = NULL;
1212 char absolute_file_path[BT_FILE_PATH_MAX_LEN] = {0, };
1214 object = _bt_otp_client_find_object(otp_object_list, curr_obj_id, &index);
1218 otp_object_list = g_slist_remove(otp_object_list, object);
1220 snprintf(absolute_file_path, sizeof(absolute_file_path),
1221 "%s%s", directory, object->name);
1223 if (remove(absolute_file_path) != 0) {
1224 BT_DBG("Error: unable to delete the file");
1228 object = _bt_otp_client_find_object(otp_object_list, prev_obj_id, &index);
1229 update_obj_metadata_charc_value(object);
1230 selected_object = object;
1231 curr_obj_index = index;
1235 int _bt_otp_oacp_write_cb(char *value, int len, int offset,
1236 char *remote_addr, struct indicate_info *info)
1238 int ret = OACP_SUCCESS;
1239 int err = BLUETOOTH_ERROR_NONE;
1240 int opcode = value[0];
1241 uint32_t object_offset, length, object_size;
1244 char absolute_file_path[BT_FILE_PATH_MAX_LEN] = {0, };
1246 BT_INFO("OACP Opcode 0x%d", opcode);
1248 if (!selected_object) {
1249 BT_DBG("Object not selected");
1250 ret = OACP_INVALID_OBJ;
1256 BT_INFO("OACP_CREATE");
1258 BT_DBG("Error: invalid param");
1259 ret = OACP_INVALID_PARAM;
1262 /* UUIDs can be 2/4/16 bytes long.
1263 * So based on remaining len, determine uuid len.
1267 uuid = _bt_otp_uuid_convert_hex_to_string(value + 1, length);
1268 object_size = (uint32_t)(value[length + 4] & 0xFF) << 24 |
1269 (uint32_t)(value[length + 3] & 0xFF) << 16 |
1270 (uint32_t)(value[length + 2] & 0xFF) << 8 |
1271 (uint32_t)(value[length + 1] & 0xFF);
1273 BT_INFO("Size = %u, UUID = %s", object_size, uuid);
1275 err = _bt_otp_create_empty_file(object_size, uuid);
1276 if (err != BLUETOOTH_ERROR_NONE) {
1277 BT_ERR("Failed to create empty file");
1278 ret = OACP_OPERATION_FAILED;
1282 if (oacp_create_timeout_id > 0)
1283 g_source_remove(oacp_create_timeout_id);
1284 oacp_create_timeout_id = g_timeout_add(BT_OACP_MAX_TIMEOUT,
1285 (GSourceFunc)__bt_oacp_create_timeout_cb, NULL);
1288 if (opcode == OACP_DELETE &&
1289 ~(selected_object->props & OBJECT_DELETE)) {
1290 ret = OACP_PROCEDURE_NOT_SUPPORTED;
1293 snprintf(absolute_file_path, sizeof(absolute_file_path),
1294 "%s%s", directory, selected_object->name);
1296 BT_DBG("absolute_file_path = [%s]", absolute_file_path);
1298 if (remove(absolute_file_path) != 0) {
1299 BT_DBG("Error: unable to delete the file");
1300 ret = OACP_OPERATION_FAILED;
1304 BT_DBG("File deleted successfully");
1305 selected_object = NULL;
1307 case OACP_CALC_CHECKSUM:
1308 ret = OACP_OPCODE_NOT_SUPPORTED;
1311 if (opcode == OACP_EXECUTE &&
1312 ~(selected_object->props & OBJECT_EXECUTE)) {
1313 ret = OACP_PROCEDURE_NOT_SUPPORTED;
1316 snprintf(absolute_file_path, sizeof(absolute_file_path),
1317 "file://%s%s", directory, selected_object->name);
1319 BT_DBG("absolute_file_path = [%s]", absolute_file_path);
1321 err = _bt_otp_send_launch_request(absolute_file_path);
1322 if (err != BLUETOOTH_ERROR_NONE) {
1323 BT_DBG("Error: unable to launch the file");
1324 ret = OACP_OPERATION_FAILED;
1328 BT_DBG("Successfully launched the file");
1332 if (opcode == OACP_WRITE &&
1333 ~(selected_object->props & OBJECT_WRITE)) {
1334 ret = OACP_PROCEDURE_NOT_SUPPORTED;
1338 if (opcode == OACP_READ &&
1339 ~(selected_object->props & OBJECT_READ)) {
1340 ret = OACP_PROCEDURE_NOT_SUPPORTED;
1344 object_offset = (uint32_t)(value[4] & 0xFF) << 24 |
1345 (uint32_t)(value[3] & 0xFF) << 16 |
1346 (uint32_t)(value[2] & 0xFF) << 8 |
1347 (uint32_t)(value[1] & 0xFF);
1348 length = (uint32_t)(value[8] & 0xFF) << 24 |
1349 (uint32_t)(value[7] & 0xFF) << 16 |
1350 (uint32_t)(value[6] & 0xFF) << 8 |
1351 (uint32_t)(value[5] & 0xFF);
1353 if (opcode == OACP_WRITE)
1354 mode = (uint8_t)value[9] & 0xFF;
1356 BT_INFO("Offset = %lu, Length = %lu", object_offset, length, mode);
1359 if (otc_connection_status) {
1360 /* Read/Write operation already going on. */
1361 ret = OACP_OBJECT_LOCKED;
1364 _bt_otp_free_oacp_op();
1367 oacp_op = g_malloc0(sizeof(struct oacp_operation));
1368 oacp_op->offset = object_offset;
1369 oacp_op->length = length;
1370 oacp_op->remote_address = g_strdup(remote_addr);
1371 oacp_op->mode = mode;
1372 oacp_op->opcode = opcode;
1373 oacp_op->length_sofar = 0;
1376 err = _bt_otp_open_otc_and_listen(remote_addr, "ListenOtc");
1377 if (err != BLUETOOTH_ERROR_NONE) {
1378 ret = OACP_CHANNEL_UNAVAILABLE;
1379 _bt_otp_free_oacp_op();
1384 ret = OACP_OPCODE_NOT_SUPPORTED;
1387 ret = OACP_OPCODE_NOT_SUPPORTED;
1391 info->resp_opcode = OACP_RESPONSE;
1392 info->req_opcode = opcode;
1393 info->result_code = ret;
1394 info->resp_param = NULL;
1395 return BLUETOOTH_ERROR_NONE;
1398 int _bt_otp_uuid_convert_string_to_hex(char *uuid, char *value)
1401 uint32_t data0, data4;
1402 uint16_t data1, data2, data3, data5;
1405 BT_ERR("Object Type UUID NULL");
1414 sscanf(uuid, "%04hx", &data1);
1415 data1 = htons(data1);
1416 memcpy(value, &data1, 2);
1422 sscanf(uuid, "%08x", &data0);
1423 data0 = htonl(data0);
1424 memcpy(value, &data0, 4);
1430 sscanf(uuid, "%08x-%04hx-%04hx-%04hx-%08x%04hx",
1431 &data0, &data1, &data2,
1432 &data3, &data4, &data5);
1434 data0 = htonl(data0);
1435 data1 = htons(data1);
1436 data2 = htons(data2);
1437 data3 = htons(data3);
1438 data4 = htonl(data4);
1439 data5 = htons(data5);
1441 memcpy(value, &data0, 4);
1442 memcpy(value+4, &data1, 2);
1443 memcpy(value+6, &data2, 2);
1444 memcpy(value+8, &data3, 2);
1445 memcpy(value+10, &data4, 4);
1446 memcpy(value+14, &data5, 2);
1457 void convert_to_hex(struct object_metadata *object, char *metadata, char *value)
1461 BT_DBG("Metadata : %s", metadata);
1463 memset(value, 0, 16);
1465 if (!g_strcmp0(metadata, "size")) {
1467 value[3] = (object->curr_size >> 24) & 0xFF;
1468 value[2] = (object->curr_size >> 16) & 0xFF;
1469 value[1] = (object->curr_size >> 8) & 0xFF;
1470 value[0] = object->curr_size & 0xFF;
1472 value[7] = (object->alloc_size >> 24) & 0xFF;
1473 value[6] = (object->alloc_size >> 16) & 0xFF;
1474 value[5] = (object->alloc_size >> 8) & 0xFF;
1475 value[4] = object->alloc_size & 0xFF;
1477 } else if (!g_strcmp0(metadata, "date")) {
1479 localtime_r(&(object->first_created), &fc_tm);
1481 value[1] = ((fc_tm.tm_year+1900) >> 8) & 0xFF;
1482 value[0] = (fc_tm.tm_year+1900) & 0xFF;
1483 value[2] = (fc_tm.tm_mon+1) & 0xFF;
1484 value[3] = fc_tm.tm_mday & 0xFF;
1485 value[4] = fc_tm.tm_hour & 0xFF;
1486 value[5] = fc_tm.tm_min & 0xFF;
1487 value[6] = fc_tm.tm_sec & 0xFF;
1489 } else if (!g_strcmp0(metadata, "id")) {
1491 value[5] = (object->id >> 48) & 0xFF;
1492 value[4] = (object->id >> 32) & 0xFF;
1493 value[3] = (object->id >> 24) & 0xFF;
1494 value[2] = (object->id >> 16) & 0xFF;
1495 value[1] = (object->id >> 8) & 0xFF;
1496 value[0] = object->id & 0xFF;
1498 } else if (!g_strcmp0(metadata, "props")) {
1499 value[3] = (object->props >> 24) & 0xFF;
1500 value[2] = (object->props >> 16) & 0xFF;
1501 value[1] = (object->props >> 8) & 0xFF;
1502 value[0] = object->props & 0xFF;
1506 void update_obj_metadata_charc_value(struct object_metadata *object)
1508 /* Value can be of maximum 16 bytes */
1512 _bt_otp_set_char_value(otp_object_name_obj_path, object->name,
1513 strlen(object->name));
1515 uuid_len = _bt_otp_uuid_convert_string_to_hex(object->type, value);
1516 _bt_otp_set_char_value(otp_object_type_obj_path, value, uuid_len);
1518 convert_to_hex(object, "size", value);
1519 _bt_otp_set_char_value(otp_object_size_obj_path, value, 8);
1521 convert_to_hex(object, "date", value);
1522 _bt_otp_set_char_value(otp_object_first_created_obj_path, value, 7);
1523 _bt_otp_set_char_value(otp_object_last_modified_obj_path, value, 7);
1525 /* Object ID is optonal for single object server */
1526 if (mutiple_obj_support) {
1527 convert_to_hex(object, "id", value);
1528 _bt_otp_set_char_value(otp_object_id_obj_path, value, 6);
1531 convert_to_hex(object, "props", value);
1532 _bt_otp_set_char_value(otp_object_prop_obj_path, value, 4);
1535 struct object_metadata *_bt_otp_client_find_object(GSList *list, uint64_t id, guint *index)
1538 struct object_metadata *info;
1540 for (l = list; l; l = g_slist_next(l)) {
1544 if (info && (info->id == id))
1550 int _bt_otp_olcp_write_cb(char *value, int len, int offset,
1551 struct indicate_info *info)
1553 int ret = OLCP_SUCCESS;
1554 int opcode = value[0];
1555 struct object_metadata *object;
1559 BT_INFO("OLCP Opcode 0x%d", opcode);
1561 if (!otp_object_list) {
1568 object = (struct object_metadata *) g_slist_nth_data(otp_object_list, 0);
1570 ret = OLCP_OUT_OF_BOUNDS;
1573 update_obj_metadata_charc_value(object);
1574 selected_object = object;
1578 len = g_slist_length(otp_object_list);
1579 object = (struct object_metadata *) g_slist_nth_data(otp_object_list, len-1);
1581 ret = OLCP_OUT_OF_BOUNDS;
1584 update_obj_metadata_charc_value(object);
1585 selected_object = object;
1586 curr_obj_index = len-1;
1589 if (curr_obj_index == 0) {
1590 ret = OLCP_OUT_OF_BOUNDS;
1593 object = (struct object_metadata *) g_slist_nth_data(otp_object_list, curr_obj_index-1);
1595 ret = OLCP_OUT_OF_BOUNDS;
1598 update_obj_metadata_charc_value(object);
1599 selected_object = object;
1600 curr_obj_index -= 1;
1603 object = (struct object_metadata *) g_slist_nth_data(otp_object_list, curr_obj_index+1);
1605 ret = OLCP_OUT_OF_BOUNDS;
1608 update_obj_metadata_charc_value(object);
1609 selected_object = object;
1610 curr_obj_index += 1;
1613 object_id = (uint64_t)(value[6] & 0xFF) << 40 |
1614 (uint64_t)(value[5] & 0xFF) << 32 |
1615 (uint64_t)(value[4] & 0xFF) << 24 |
1616 (uint64_t)(value[3] & 0xFF) << 16 |
1617 (uint64_t)(value[2] & 0xFF) << 8 |
1618 (uint64_t)(value[1] & 0xFF);
1619 BT_INFO("Object ID [%llu]", object_id);
1620 if (selected_object && selected_object->id == object_id)
1623 object = _bt_otp_client_find_object(otp_object_list, object_id, &index);
1625 ret = OLCP_OJECT_ID_NOT_FOUND;
1628 update_obj_metadata_charc_value(object);
1629 selected_object = object;
1630 curr_obj_index = index - 1;
1633 case OLCP_REQ_NO_OBJ:
1634 case OLCP_CLEAR_MARKING:
1636 ret = OLCP_OPCODE_NOT_SUPPORTED;
1640 info->resp_opcode = OLCP_RESPONSE;
1641 info->req_opcode = opcode;
1642 info->result_code = ret;
1643 info->resp_param = NULL;
1644 return BLUETOOTH_ERROR_NONE;
1647 int _bt_otp_obj_name_write_cb(char *value, int len)
1649 struct object_metadata *object;
1651 char new_abs_filepath[BT_FILE_PATH_MAX_LEN] = {0, };
1652 char old_abs_filepath[BT_FILE_PATH_MAX_LEN] = {0, };
1653 int ret = BLUETOOTH_ERROR_NONE;
1655 object = (struct object_metadata *) g_slist_nth_data(otp_object_list, curr_obj_index);
1657 return BLUETOOTH_ERROR_WRITE_REQUEST_REJECTED;
1659 filename = g_strndup(value, len);
1660 snprintf(new_abs_filepath, strlen(new_abs_filepath), "%s%s",
1661 directory, filename);
1663 snprintf(old_abs_filepath, strlen(old_abs_filepath), "%s%s",
1664 directory, object->name);
1666 if (rename(old_abs_filepath, new_abs_filepath)) {
1667 ret = OBJECT_NAME_ALREADY_EXISTS;
1671 memcpy(object->name, value, len);
1672 _bt_otp_set_char_value(otp_object_name_obj_path, value, len);
1674 if (oacp_create_timeout_id > 0)
1675 g_source_remove(oacp_create_timeout_id);
1682 int _bt_otp_obj_first_created_write_cb(char *value, int len)
1684 struct object_metadata *object;
1688 object = (struct object_metadata *) g_slist_nth_data(otp_object_list, curr_obj_index);
1690 return BLUETOOTH_ERROR_WRITE_REQUEST_REJECTED;
1692 year = (uint16_t)(value[1] & 0xFF) << 8 |
1693 (uint16_t)(value[0] & 0xFF);
1694 tm.tm_year = year-1900;
1695 tm.tm_mon = value[2] & 0xFF;
1696 tm.tm_mon = tm.tm_mon-1;
1697 tm.tm_mday = value[3] & 0xFF;
1698 tm.tm_hour = value[4] & 0xFF;
1699 tm.tm_min = value[5] & 0xFF;
1700 tm.tm_sec = value[6] & 0xFF;
1702 object->first_created = mktime(&tm);
1703 _bt_otp_set_char_value(otp_object_first_created_obj_path, value, len);
1705 return BLUETOOTH_ERROR_NONE;
1708 int _bt_otp_obj_last_modified_write_cb(char *value, int len)
1710 struct object_metadata *object;
1714 object = (struct object_metadata *) g_slist_nth_data(otp_object_list, curr_obj_index);
1716 return BLUETOOTH_ERROR_WRITE_REQUEST_REJECTED;
1718 year = (uint16_t)(value[1] & 0xFF) << 8 |
1719 (uint16_t)(value[0] & 0xFF);
1720 tm.tm_year = year-1900;
1721 tm.tm_mon = value[2] & 0xFF;
1722 tm.tm_mon = tm.tm_mon-1;
1723 tm.tm_mday = value[3] & 0xFF;
1724 tm.tm_hour = value[4] & 0xFF;
1725 tm.tm_min = value[5] & 0xFF;
1726 tm.tm_sec = value[6] & 0xFF;
1728 object->last_modified = mktime(&tm);
1729 _bt_otp_set_char_value(otp_object_last_modified_obj_path, value, len);
1731 return BLUETOOTH_ERROR_NONE;
1734 int _bt_otp_obj_props_write_cb(char *value, int len)
1736 struct object_metadata *object;
1739 /* Any attempt to write RFU bits is error */
1740 if (value[1] || value[2] || value[3])
1741 return BLUETOOTH_ERROR_WRITE_REQUEST_REJECTED;
1743 object = (struct object_metadata *) g_slist_nth_data(otp_object_list, curr_obj_index);
1745 return BLUETOOTH_ERROR_WRITE_REQUEST_REJECTED;
1747 props = (uint32_t)(value[3] & 0xFF) << 24 |
1748 (uint32_t)(value[2] & 0xFF) << 16 |
1749 (uint32_t)(value[1] & 0xFF) << 8 |
1750 (uint32_t)(value[0] & 0xFF);
1752 object->props = props;
1753 _bt_otp_set_char_value(otp_object_prop_obj_path, value, len);
1755 return BLUETOOTH_ERROR_NONE;
1758 static struct otp_char_info *otp_get_char_value(const char *path)
1762 for (tmp = otp_char_list; tmp != NULL; tmp = tmp->next) {
1764 struct otp_char_info *char_info = tmp->data;
1765 if (!g_strcmp0(char_info->char_path, path))
1773 int _bt_otp_read_cb(const char *obj_path, char **value, int *len)
1775 struct otp_char_info *info = NULL;
1778 BT_ERR("Wrong Obj path");
1779 return BLUETOOTH_ERROR_INTERNAL;
1782 if (g_strcmp0(obj_path, otp_feature_obj_path)) {
1783 if (!selected_object) {
1784 return BLUETOOTH_ERROR_OBJECT_NOT_SELECTED;
1788 info = otp_get_char_value(obj_path);
1790 if (info->char_value == NULL || info->value_length == 0)
1791 return BLUETOOTH_ERROR_INTERNAL;
1793 *len = info->value_length;
1794 *value = (char *)malloc(sizeof(char)*(*len));
1795 memcpy(*value, info->char_value, *len);
1797 return BLUETOOTH_ERROR_NONE;
1799 return BLUETOOTH_ERROR_INTERNAL;
1803 static void _otp_convert_address_to_hex(bluetooth_device_address_t *addr_hex,
1804 const char *addr_str)
1807 unsigned int addr[BLUETOOTH_ADDRESS_LENGTH] = { 0, };
1809 if (addr_str == NULL || addr_str[0] == '\0')
1812 i = sscanf(addr_str, "%X:%X:%X:%X:%X:%X", &addr[0], &addr[1],
1813 &addr[2], &addr[3], &addr[4], &addr[5]);
1814 if (i != BLUETOOTH_ADDRESS_LENGTH)
1815 BT_ERR("Invalid format string - [%s]", addr_str);
1817 for (i = 0; i < BLUETOOTH_ADDRESS_LENGTH; i++)
1818 addr_hex->addr[i] = (unsigned char)addr[i];
1821 static void _bt_otp_send_indication(const char *obj_path,
1822 struct indicate_info *info,
1823 bluetooth_device_address_t *remote_address)
1825 int ret = BLUETOOTH_ERROR_NONE;
1826 char value[7] = {0x00};
1827 int length = OTP_INDICATION_LEN_WITHOUT_RESP;
1831 value[0] = info->resp_opcode & 0xFF;
1832 value[1] = info->req_opcode & 0xFF;
1833 value[2] = info->result_code & 0xFF;
1834 if (info->resp_param) {
1835 value[6] = (info->resp_param[3] >> 24) & 0xFF;
1836 value[5] = (info->resp_param[4] >> 16) & 0xFF;
1837 value[4] = (info->resp_param[5] >> 8) & 0xFF;
1838 value[3] = info->resp_param[6] & 0xFF;
1839 length = OTP_INDICATION_LEN_WITH_RESP;
1842 BT_DBG("Opcode: %d", value[1]);
1844 /* Store the status value */
1845 _bt_otp_set_char_value(obj_path, value, length);
1847 /* Send indication */
1848 ret = bluetooth_gatt_server_set_notification(obj_path, remote_address);
1849 if (ret != BLUETOOTH_ERROR_NONE) {
1850 BT_ERR("_bt_otp_send_control_point_indication failed");
1853 ret = bluetooth_gatt_update_characteristic(obj_path, value, length);
1854 if (ret != BLUETOOTH_ERROR_NONE) {
1855 BT_ERR("_bt_otp_send_control_point_indication failed");
1860 void _bt_otp_gatt_char_property_changed_event(GVariant *msg,
1863 int result = BLUETOOTH_ERROR_NONE;
1864 GVariantIter value_iter;
1865 const char *property = NULL;
1866 const char *char_path = NULL;
1867 const char *svc_handle = NULL;
1868 GVariant *var = NULL;
1869 GVariant *val = NULL;
1870 g_variant_iter_init(&value_iter, msg);
1872 while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &var))) {
1874 if (property == NULL) {
1875 BT_ERR("Property NULL");
1879 if (!g_strcmp0(property, "WriteValue")) {
1881 BT_INFO("WriteValue");
1882 BT_INFO("Type '%s'\n", g_variant_get_type_string(var));
1885 bluetooth_device_address_t addr_hex = { {0,} };
1890 struct indicate_info info;
1892 g_variant_get(var, "(&s&s&syq@ay)",
1893 &char_path, &svc_handle,
1894 &addr, &req_id, &offset, &val);
1896 len = g_variant_get_size(val);
1898 BT_DBG("Len = %d, BT_ADDR = %s", len, addr);
1900 value = (char *) g_variant_get_data(val);
1901 _otp_convert_address_to_hex(&addr_hex, addr);
1904 if (!g_strcmp0(char_path, otp_oacp_obj_path)) {
1906 result = BLUETOOTH_ERROR_CCC_IMPROPERLY_CONFIGURED;
1908 result = _bt_otp_oacp_write_cb(value, len, offset, addr, &info);
1909 } else if (!g_strcmp0(char_path, otp_olcp_obj_path)) {
1911 result = BLUETOOTH_ERROR_CCC_IMPROPERLY_CONFIGURED;
1913 result = _bt_otp_olcp_write_cb(value, len, offset, &info);
1914 } else if (!g_strcmp0(char_path, otp_object_name_obj_path)) {
1915 result = _bt_otp_obj_name_write_cb(value, len);
1916 } else if (!g_strcmp0(char_path, otp_object_first_created_obj_path)) {
1917 result = _bt_otp_obj_first_created_write_cb(value, len);
1918 } else if (!g_strcmp0(char_path, otp_object_last_modified_obj_path)) {
1919 result = _bt_otp_obj_last_modified_write_cb(value, len);
1920 } else if (!g_strcmp0(char_path, otp_object_prop_obj_path)) {
1921 result = _bt_otp_obj_props_write_cb(value, len);
1923 BT_ERR("Wrong Object Path %s", char_path);
1924 result = BLUETOOTH_ERROR_INTERNAL;
1926 bluetooth_gatt_send_response(req_id,
1927 BLUETOOTH_GATT_ATT_REQUEST_TYPE_WRITE,
1928 result, 0, NULL, 0);
1930 /* Send indication for CPs */
1931 if (!g_strcmp0(char_path, otp_oacp_obj_path)) {
1932 if (OACP_indicate) {
1933 _bt_otp_send_indication(char_path, &info, &addr_hex);
1935 } else if (!g_strcmp0(char_path, otp_olcp_obj_path)) {
1936 if (OLCP_indicate) {
1937 _bt_otp_send_indication(char_path, &info, &addr_hex);
1941 BT_ERR("Array Len 0");
1944 BT_ERR("var==NULL");
1946 } else if (!g_strcmp0(property, "ReadValue")) {
1952 result = BLUETOOTH_ERROR_NONE;
1954 BT_INFO("ReadValue");
1955 BT_INFO("Type '%s'\n", g_variant_get_type_string(var));
1957 g_variant_get(var, "(&s&s&syq)", &char_path,
1958 &svc_handle, &addr, &req_id, &offset);
1960 result = _bt_otp_read_cb(char_path, &value, &len);
1962 if (result != BLUETOOTH_ERROR_NONE) {
1963 BT_ERR("ReadValue failed %s", char_path);
1964 bluetooth_gatt_send_response(req_id,
1965 BLUETOOTH_GATT_ATT_REQUEST_TYPE_READ,
1966 result, offset, NULL, 0);
1968 bluetooth_gatt_send_response(req_id,
1969 BLUETOOTH_GATT_ATT_REQUEST_TYPE_READ,
1970 result, offset, value, len);
1974 } else if (!g_strcmp0(property, "NotificationStateChanged")) {
1975 gboolean indicate = FALSE;
1977 g_variant_get(var, "(&s&sb)", &char_path,
1978 &svc_handle, &indicate);
1980 BT_INFO("%s : [%s]", property,
1981 indicate ? "StartNotify" : "StopNotify");
1982 BT_INFO("Type '%s'\n", g_variant_get_type_string(var));
1984 if (!g_strcmp0(char_path, otp_oacp_obj_path)) {
1985 OACP_indicate = indicate;
1986 } else if (!g_strcmp0(char_path, otp_olcp_obj_path)) {
1987 OLCP_indicate = indicate;
1994 void _bt_otp_property_event_filter(GDBusConnection *connection,
1995 const gchar *sender_name,
1996 const gchar *object_path,
1997 const gchar *interface_name,
1998 const gchar *signal_name,
1999 GVariant *parameters,
2004 if (signal_name == NULL) {
2005 BT_ERR("Wrong Signal");
2009 if (g_strcmp0(signal_name, PROPERTIES_CHANGED) == 0) {
2011 g_variant_get(parameters, "(@a{sv}@as)", &value, NULL);
2012 _bt_otp_gatt_char_property_changed_event(value, object_path);
2016 void _bt_otp_adapter_event_filter(GDBusConnection *connection,
2017 const gchar *sender_name,
2018 const gchar *object_path,
2019 const gchar *interface_name,
2020 const gchar *signal_name,
2021 GVariant *parameters,
2024 if (signal_name == NULL) {
2025 BT_ERR("Wrong Signal");
2029 BT_INFO("Interface %s, Signal %s", interface_name, signal_name);
2031 if (g_strcmp0(interface_name, BT_OTP_INTERFACE_NAME) == 0) {
2032 if (strcasecmp(signal_name, BLE_DISABLED) == 0) {
2038 void _bt_otc_disconnected_cb(GDBusConnection *connection,
2039 const gchar *sender_name,
2040 const gchar *object_path,
2041 const gchar *interface_name,
2042 const gchar *signal_name,
2043 GVariant *parameters,
2046 if (signal_name == NULL) {
2047 BT_ERR("Wrong Signal");
2051 BT_INFO("Interface %s, Signal %s", interface_name, signal_name);
2053 if (g_strcmp0(interface_name, BT_DEVICE_INTERFACE) == 0) {
2054 if (strcasecmp(signal_name, OTC_DISCONNECTED) == 0) {
2055 BT_DBG("OTC Channel Disconnected dev_path[%s]",
2057 otc_connection_status = FALSE;
2058 _bt_otp_free_oacp_op();
2063 int _bt_otp_init_event_receiver()
2066 GError *error = NULL;
2069 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
2070 if (error != NULL) {
2071 BT_ERR("ERROR: Can't get on system bus [%s]",
2073 g_clear_error(&error);
2077 property_sub_id = g_dbus_connection_signal_subscribe(conn,
2079 BT_OTP_INTERFACE_NAME,
2081 BT_OTP_OBJECT_PATH, NULL, 0,
2082 _bt_otp_property_event_filter,
2085 adapter_sub_id = g_dbus_connection_signal_subscribe(conn,
2087 BT_OTP_INTERFACE_NAME,
2089 BT_OTP_OBJECT_PATH, NULL, 0,
2090 _bt_otp_adapter_event_filter,
2093 device_sub_id = g_dbus_connection_signal_subscribe(conn,
2094 NULL, BT_DEVICE_INTERFACE,
2095 OTC_DISCONNECTED, NULL, NULL, 0,
2096 _bt_otc_disconnected_cb,
2103 void _bt_otp_deinit_event_receiver(void)
2107 g_dbus_connection_signal_unsubscribe(conn, property_sub_id);
2108 g_dbus_connection_signal_unsubscribe(conn, adapter_sub_id);
2109 g_dbus_connection_signal_unsubscribe(conn, device_sub_id);
2115 static void _bt_otp_sig_handler(int sig)
2120 BT_DBG("caught signal - sigterm\n");
2123 BT_DBG("caught signal - sigint\n");
2126 BT_DBG("caught signal - sigkill\n");
2129 BT_DBG("caught signal %d and ignored\n", sig);
2135 /* OTP Service Main loop */
2138 struct sigaction sa;
2139 BT_ERR("Starting the bt-otp daemon");
2141 memset(&sa, 0, sizeof(sa));
2142 sa.sa_handler = _bt_otp_sig_handler;
2143 sa.sa_flags = SA_SIGINFO;
2144 sigaction(SIGINT, &sa, NULL);
2145 sigaction(SIGTERM, &sa, NULL);
2146 sigaction(SIGKILL, &sa, NULL);
2148 if (_bt_otp_register_interface() != BLUETOOTH_ERROR_NONE) {
2149 BT_ERR("Fail to register otp service");
2153 if (_bt_otp_init_event_receiver() != BLUETOOTH_ERROR_NONE) {
2154 BT_ERR("Fail to init event reciever");
2158 main_loop = g_main_loop_new(NULL, FALSE);
2160 g_main_loop_run(main_loop);
2162 BT_DBG("g_main_loop_quit called!");
2164 if (main_loop != NULL) {
2165 g_main_loop_unref(main_loop);