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 "bt-otpserver.h"
32 #include "bluetooth-api.h"
36 #define LOG_TAG "BLUETOOTH_OTP"
38 #define BT_INFO(fmt, arg...) SLOGI(fmt, ##arg)
39 #define BT_ERR(fmt, arg...) SLOGE(fmt, ##arg)
40 #define BT_DBG(fmt, arg...) SLOGD(fmt, ##arg)
42 /* OTP object paths */
43 char *otp_obj_path = NULL;
44 char *otp_feature_obj_path = NULL;
45 char *otp_object_name_obj_path = NULL;
46 char *otp_object_type_obj_path = NULL;
47 char *otp_object_size_obj_path = NULL;
48 char *otp_object_first_created_obj_path = NULL;
49 char *otp_object_last_modified_obj_path = NULL;
50 char *otp_object_id_obj_path = NULL;
51 char *otp_object_prop_obj_path = NULL;
52 char *otp_oacp_obj_path = NULL;
53 char *otp_olcp_obj_path = NULL;
54 char *otp_oacp_desc_obj_path = NULL;
55 char *otp_olcp_desc_obj_path = NULL;
57 static GMainLoop *main_loop;
58 GDBusNodeInfo *otp_node_info = NULL;
59 static GDBusConnection *conn;
60 static GDBusConnection *g_conn;
62 static int property_sub_id = -1;
63 static int adapter_sub_id = -1;
64 static int device_sub_id = -1;
65 static guint g_owner_id = 0;
67 struct otp_char_info {
73 struct indicate_info {
81 struct object_metadata {
92 struct oacp_operation {
101 struct oacp_create_operation {
102 char *remote_address;
106 time_t first_created;
109 static struct object_metadata *selected_object = NULL;
110 static uint64_t object_id = OBJECT_START_ID;
111 static GSList *otp_object_list = NULL;
112 static GSList *otp_char_list = NULL;
113 static guint obj_curr_index;
114 static int adv_handle = 0;
115 static gboolean OACP_indicate = FALSE;
116 static gboolean OLCP_indicate = FALSE;
117 char *directory = NULL;
118 gboolean mutiple_obj_support = false;
119 static gboolean otc_connection_status = FALSE;
120 struct oacp_operation *oacp_read = NULL;
121 struct oacp_create_operation *oacp_create = NULL;
122 unsigned int timeout_id;
124 static const gchar otp_introspection_xml[] =
126 " <interface name='org.projectx.otp_service'>"
127 " <method name='enable'>"
128 " <arg type='s' name='directory'/>"
129 " <arg type='i' name='status' direction='out'/>"
131 " <method name='disable'>"
132 " <arg type='i' name='status' direction='out'/>"
134 " <method name='NewConnection'>"
135 " <arg type='o' name='object' direction='in'/>"
136 " <arg type='h' name='fd' direction='in'/>"
141 void _bt_otp_deinit_event_receiver(void);
142 void _bt_otp_unregister_interface(void);
143 void update_obj_metadata_charc_value(struct object_metadata *object);
144 void _bt_convert_device_path_to_address(const char *device_path,
145 char *device_address);
147 static void delete_all_objects(void)
150 for (tmp = otp_object_list; tmp != NULL; tmp = tmp->next) {
152 struct object_metadata *obj_info = tmp->data;
154 g_free(obj_info->name);
156 g_free(obj_info->type);
157 otp_object_list = g_slist_delete_link(otp_object_list, tmp->data);
160 g_slist_free(otp_object_list);
161 otp_object_list = NULL;
164 static void delete_all_characterisitc(void)
167 for (tmp = otp_char_list; tmp != NULL; tmp = tmp->next) {
169 struct otp_char_info *char_info = tmp->data;
170 if (char_info->char_path)
171 g_free(char_info->char_path);
172 if (char_info->char_value)
173 g_free(char_info->char_value);
174 otp_char_list = g_slist_delete_link(otp_char_list, tmp->data);
177 g_slist_free(otp_char_list);
178 otp_char_list = NULL;
181 void _bt_otp_exit(void)
187 delete_all_characterisitc();
190 delete_all_objects();
192 ret = bluetooth_gatt_deinit();
193 if (ret != BLUETOOTH_ERROR_NONE)
194 BT_ERR("Failed to Deinit GATT %d", ret);
196 _bt_otp_deinit_event_receiver();
198 _bt_otp_unregister_interface();
200 /* TODO: Advertising is not getting stopped by this API.
201 * This is because OTP_SERVER_DEINIT dbus call is blocking
202 * BT_SET_ADVERTISING_DATA dbus call. But now advertisment
203 * is stopped because of terminated process logic.
205 ret = bluetooth_set_advertising(adv_handle, FALSE);
206 if (ret != BLUETOOTH_ERROR_NONE)
207 BT_ERR("Failed to stop ADV %d", ret);
209 if (main_loop != NULL) {
210 g_main_loop_quit(main_loop);
214 static void _bt_otp_set_char_value(const char *obj_path,
215 const char *value, int value_length)
221 for (tmp = otp_char_list; tmp != NULL; tmp = tmp->next) {
223 struct otp_char_info *char_info = tmp->data;
224 if (!g_strcmp0(char_info->char_path, obj_path)) {
225 char_info->char_value = g_try_realloc(char_info->char_value, value_length);
226 if (char_info->char_value) {
227 memcpy(char_info->char_value, value, value_length);
228 char_info->value_length = value_length;
237 int add_new_characteristic(const char *char_uuid, bt_gatt_permission_t perms,
238 bt_gatt_characteristic_property_t props, char **obj_path)
240 int ret = BLUETOOTH_ERROR_NONE;
241 struct otp_char_info *char_info = NULL;
243 ret = bluetooth_gatt_add_new_characteristic(otp_obj_path,
244 char_uuid, perms, props, obj_path);
245 if (ret != BLUETOOTH_ERROR_NONE) {
246 BT_ERR("Failed to add new char %d", ret);
250 char_info = g_new0(struct otp_char_info, 1);
251 char_info->char_path = g_strdup(*obj_path);
252 otp_char_list = g_slist_append(otp_char_list, char_info);
257 static char *_otp_convert_uuid_to_uuid128(const char *uuid)
267 uuid128 = g_strdup_printf("0000%s-0000-1000-8000-00805f9b34fb",
273 uuid128 = g_strdup_printf("%s-0000-1000-8000-00805f9b34fb",
279 uuid128 = strdup(uuid);
289 int _bt_otp_prepare_ots(void)
292 int ret = BLUETOOTH_ERROR_NONE;
296 bt_gatt_characteristic_property_t props;
297 bt_gatt_permission_t perms;
298 char supp_feat[OTP_FEATURE_LENGTH] = { 0x88, 0x00, 0x00, 0x00,
299 0x80, 0x00, 0x00, 0x00 };
301 ret = bluetooth_gatt_init();
302 if (ret != BLUETOOTH_ERROR_NONE) {
303 BT_ERR("Failed to Init GATT %d", ret);
307 service_uuid = _otp_convert_uuid_to_uuid128(OTP_UUID);
308 ret = bluetooth_gatt_add_service(service_uuid, &otp_obj_path);
309 if (ret != BLUETOOTH_ERROR_NONE) {
310 BT_ERR("Failed to add service %d", ret);
314 /* Characteristic OTP Feature */
315 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ;
316 perms = BLUETOOTH_GATT_PERMISSION_READ;
317 char_uuid = _otp_convert_uuid_to_uuid128(OTP_FEATURE_UUID);
318 ret = add_new_characteristic(char_uuid, perms, props,
319 &otp_feature_obj_path);
320 if (ret != BLUETOOTH_ERROR_NONE)
323 ret = bluetooth_gatt_set_characteristic_value(otp_feature_obj_path,
324 supp_feat, OTP_FEATURE_LENGTH);
325 if (ret != BLUETOOTH_ERROR_NONE) {
326 BT_ERR("Failed to set char value %d", ret);
330 _bt_otp_set_char_value(otp_feature_obj_path, supp_feat,
333 /* Characteristic Object Name */
334 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ |
335 BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE;
336 perms = BLUETOOTH_GATT_PERMISSION_READ |
337 BLUETOOTH_GATT_PERMISSION_WRITE;
338 char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_NAME_UUID);
339 ret = add_new_characteristic(char_uuid, perms, props,
340 &otp_object_name_obj_path);
341 if (ret != BLUETOOTH_ERROR_NONE)
344 /* Characteristic Object Type */
345 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ;
346 perms = BLUETOOTH_GATT_PERMISSION_READ;
347 char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_TYPE_UUID);
348 ret = add_new_characteristic(char_uuid, perms, props,
349 &otp_object_type_obj_path);
350 if (ret != BLUETOOTH_ERROR_NONE)
353 /* Characteristic Object Size */
354 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ;
355 perms = BLUETOOTH_GATT_PERMISSION_READ;
356 char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_SIZE_UUID);
357 ret = add_new_characteristic(char_uuid, perms, props,
358 &otp_object_size_obj_path);
359 if (ret != BLUETOOTH_ERROR_NONE)
362 /* Characteristic Object First-Created */
363 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ |
364 BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE;
365 perms = BLUETOOTH_GATT_PERMISSION_READ |
366 BLUETOOTH_GATT_PERMISSION_WRITE;
367 char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_FIRST_CREATED_UUID);
368 ret = add_new_characteristic(char_uuid, perms, props,
369 &otp_object_first_created_obj_path);
370 if (ret != BLUETOOTH_ERROR_NONE)
373 /* Characteristic Object Last-Modified */
374 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ |
375 BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE;
376 perms = BLUETOOTH_GATT_PERMISSION_READ |
377 BLUETOOTH_GATT_PERMISSION_WRITE;
378 char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_LAST_MODIFIED_UUID);
379 ret = add_new_characteristic(char_uuid, perms, props,
380 &otp_object_last_modified_obj_path);
381 if (ret != BLUETOOTH_ERROR_NONE)
384 /* Object ID is mandatory for mutiple object server */
385 if (mutiple_obj_support) {
386 /* Characteristic Object ID */
387 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ;
388 perms = BLUETOOTH_GATT_PERMISSION_READ;
389 char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_ID_UUID);
390 ret = add_new_characteristic(char_uuid, perms, props,
391 &otp_object_id_obj_path);
392 if (ret != BLUETOOTH_ERROR_NONE)
396 /* Characteristic Object Properties */
397 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ;
398 perms = BLUETOOTH_GATT_PERMISSION_READ;
399 char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_PROP_UUID);
400 ret = add_new_characteristic(char_uuid, perms, props,
401 &otp_object_prop_obj_path);
402 if (ret != BLUETOOTH_ERROR_NONE)
405 /* Characteristic OACP */
406 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE |
407 BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_INDICATE;
408 perms = BLUETOOTH_GATT_PERMISSION_WRITE;
409 char_uuid = _otp_convert_uuid_to_uuid128(OTP_OACP_UUID);
410 ret = add_new_characteristic(char_uuid, perms, props,
412 if (ret != BLUETOOTH_ERROR_NONE)
416 desc_uuid = _otp_convert_uuid_to_uuid128(OTP_CP_CCC_DESC_UUID);
417 perms = BLUETOOTH_GATT_PERMISSION_READ |
418 BLUETOOTH_GATT_PERMISSION_WRITE;
419 ret = bluetooth_gatt_add_descriptor(otp_oacp_obj_path, desc_uuid,
420 perms, &otp_oacp_desc_obj_path);
421 if (ret != BLUETOOTH_ERROR_NONE) {
422 BT_ERR("Failed to add new char descriptor %d", ret);
426 /* OLCP Characteristics is not required
427 * for single object server
429 if (mutiple_obj_support) {
430 /* Characteristic OLCP */
431 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE |
432 BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_INDICATE;
433 perms = BLUETOOTH_GATT_PERMISSION_WRITE;
434 char_uuid = _otp_convert_uuid_to_uuid128(OTP_OLCP_UUID);
435 ret = add_new_characteristic(char_uuid, perms, props,
437 if (ret != BLUETOOTH_ERROR_NONE)
441 desc_uuid = _otp_convert_uuid_to_uuid128(OTP_CP_CCC_DESC_UUID);
442 perms = BLUETOOTH_GATT_PERMISSION_READ |
443 BLUETOOTH_GATT_PERMISSION_WRITE;
444 ret = bluetooth_gatt_add_descriptor(otp_olcp_obj_path, desc_uuid,
445 perms, &otp_olcp_desc_obj_path);
446 if (ret != BLUETOOTH_ERROR_NONE) {
447 BT_ERR("Failed to add new char descriptor %d", ret);
452 /* Register service */
453 ret = bluetooth_gatt_register_service(otp_obj_path);
454 if (ret != BLUETOOTH_ERROR_NONE) {
455 BT_ERR("Failed to register service %d", ret);
459 /* Register Application */
460 ret = bluetooth_gatt_register_application();
461 if (ret != BLUETOOTH_ERROR_NONE) {
462 BT_ERR("Failed to register application %d", ret);
470 delete_all_characterisitc();
474 int _bt_otp_set_advertising_data(void)
480 guint8 data[4] = {0x03, 0x02, 0x25, 0x18};
481 bluetooth_advertising_data_t adv;
483 BT_DBG("%x %x %x %x", data[0], data[1], data[2], data[3]);
484 memcpy(adv.data, data, sizeof(data));
485 ret = bluetooth_set_advertising_data(adv_handle, &adv, sizeof(data));
486 if (ret != BLUETOOTH_ERROR_NONE) {
487 BT_ERR("Failed to set ADV data %d", ret);
491 ret = bluetooth_set_advertising(adv_handle, TRUE);
492 if (ret != BLUETOOTH_ERROR_NONE) {
493 BT_ERR("Failed to set ADV %d", ret);
500 void _bt_otp_start_write_on_fd()
502 char buf[BT_L2CAP_BUFFER_LEN];
507 char file_path[BT_FILE_PATH_MAX_LEN] = {0, };
510 if (!selected_object) {
511 BT_DBG("Object not selected");
515 snprintf(file_path, sizeof(file_path), "%s%s",
516 directory, selected_object->name);
517 BT_DBG("file_path = [%s]", file_path);
519 fp = fopen(file_path, "r");
521 BT_DBG("fopen() failed : %s", strerror(errno));
525 BT_DBG("length [%d]", oacp_read->length);
526 length = oacp_read->length;
529 if (length < BT_L2CAP_BUFFER_LEN)
532 len = BT_L2CAP_BUFFER_LEN;
534 read = fread(buf, 1, len, fp);
535 written = write(oacp_read->fd, buf, len);
538 BT_DBG("read [%d], written [%d], rem_len [%d]",
539 read, written, length);
544 g_free(oacp_read->remote_address);
549 static void _bt_otp_method(GDBusConnection *connection,
551 const gchar *object_path,
552 const gchar *interface_name,
553 const gchar *method_name,
554 GVariant *parameters,
555 GDBusMethodInvocation *invocation,
559 int status = BLUETOOTH_ERROR_NONE;
561 BT_DBG("Method[%s] Object Path[%s] Interface Name[%s]",
562 method_name, object_path, interface_name);
564 if (g_strcmp0(method_name, "enable") == 0) {
566 GError *error = NULL;
567 const gchar *filename = NULL;
568 char absolute_path[ABSOLUTE_PATH_MAX_LENGTH];
569 GSList *list = NULL, *l = NULL;
571 struct object_metadata *object = NULL;
573 g_variant_get(parameters, "(s)", &directory);
574 BT_DBG("Directory = [%s]", directory);
576 dir = g_dir_open(directory, 0, &error);
578 BT_ERR("Failed to open directory: %s", error->message);
580 status = BLUETOOTH_ERROR_INVALID_DIRECTORY;
584 while ((filename = g_dir_read_name(dir))) {
585 list = g_slist_append(list, (gpointer) filename);
591 BT_DBG("No object found in given directory");
592 status = BLUETOOTH_ERROR_NO_OBJECTS_FOUND;
596 if (g_slist_length(list) > 1)
597 mutiple_obj_support = true;
599 for (l = list; l != NULL; l = l->next) {
600 if (!l->data) continue;
601 snprintf(absolute_path, sizeof(absolute_path), "%s%s", directory,
604 BT_INFO("filename: %s, absoulte_path: %s",
605 (char *)l->data, absolute_path);
607 if (stat(absolute_path, &st) == -1) {
608 BT_INFO("stat failed: (%d)\n", errno);
612 object = g_new0(struct object_metadata, 1);
614 object->name = g_strdup((const gchar *)l->data);
615 object->type = _otp_convert_uuid_to_uuid128(UNSUPPORTED_OBJECT_TYPE_UUID);
616 object->first_created = st.st_ctime;
617 object->last_modified = st.st_ctime;
618 object->curr_size = (uint32_t) st.st_size;
619 object->alloc_size = (uint32_t) st.st_size;
620 object->id = object_id;
621 object->props = OBJECT_READ;
623 otp_object_list = g_slist_append(otp_object_list,
630 if (_bt_otp_prepare_ots() != BLUETOOTH_ERROR_NONE) {
631 BT_ERR("Fail to prepare OTP Proxy");
632 status = BLUETOOTH_ERROR_INTERNAL;
636 /* If single object is supported, make that as
637 * selected object and update the metadata for the same.
639 if (!mutiple_obj_support) {
640 BT_INFO("Server supports single object");
641 selected_object = (struct object_metadata *) g_slist_nth_data(otp_object_list, 0);
643 update_obj_metadata_charc_value(selected_object);
646 BT_DBG("advertsing");
647 if (_bt_otp_set_advertising_data() != BLUETOOTH_ERROR_NONE) {
648 BT_ERR("Fail to set advertising data");
649 status = BLUETOOTH_ERROR_INTERNAL;
653 g_dbus_method_invocation_return_value(invocation,
654 g_variant_new("(i)", status));
656 } else if (g_strcmp0(method_name, "disable") == 0) {
657 g_dbus_method_invocation_return_value(invocation,
658 g_variant_new("(i)", status));
661 } else if (g_strcmp0(method_name, "NewConnection") == 0) {
664 GUnixFDList *fd_list;
666 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
669 g_variant_get(parameters, "(oh)", &dev_path, &index);
671 msg = g_dbus_method_invocation_get_message(invocation);
672 fd_list = g_dbus_message_get_unix_fd_list(msg);
673 if (fd_list == NULL) {
674 BT_ERR("fd_list is NULL");
678 fd = g_unix_fd_list_get(fd_list, index, NULL);
680 BT_ERR("Invalid fd return");
684 _bt_convert_device_path_to_address(dev_path, address);
686 BT_INFO("OTC Connected fd: %d, address %s", fd, address);
689 otc_connection_status = TRUE;
691 if (oacp_read->opcode == OACP_READ)
692 _bt_otp_start_write_on_fd();
694 g_dbus_method_invocation_return_value(invocation, NULL);
699 static const GDBusInterfaceVTable otp_method_table = {
705 static void _bt_otp_on_bus_acquired(GDBusConnection *connection,
706 const gchar *name, gpointer user_data)
709 GError *error = NULL;
715 object_id = g_dbus_connection_register_object(connection,
717 otp_node_info->interfaces[0],
720 if (object_id == 0) {
721 BT_ERR("Failed to register method table: %s", error->message);
723 g_dbus_node_info_unref(otp_node_info);
729 static void _bt_otp_on_name_acquired(GDBusConnection *connection,
736 static void _bt_otp_on_name_lost(GDBusConnection *connection,
741 g_object_unref(g_conn);
743 g_dbus_node_info_unref(otp_node_info);
744 g_bus_unown_name(g_owner_id);
747 int _bt_otp_register_interface(void)
750 GError *error = NULL;
753 otp_node_info = g_dbus_node_info_new_for_xml(otp_introspection_xml, &error);
754 if (!otp_node_info) {
755 BT_ERR("Failed to install: %s", error->message);
756 return BLUETOOTH_ERROR_INTERNAL;
759 owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
761 G_BUS_NAME_OWNER_FLAGS_NONE,
762 _bt_otp_on_bus_acquired,
763 _bt_otp_on_name_acquired,
764 _bt_otp_on_name_lost,
766 g_owner_id = owner_id;
767 BT_DBG("owner_id is [%d]\n", owner_id);
770 return BLUETOOTH_ERROR_NONE;
773 void _bt_otp_unregister_interface(void)
777 g_object_unref(g_conn);
779 g_dbus_node_info_unref(otp_node_info);
780 g_bus_unown_name(g_owner_id);
786 void _bt_convert_device_path_to_address(const char *device_path,
787 char *device_address)
789 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
792 dev_addr = strstr(device_path, "dev_");
793 if (dev_addr != NULL) {
796 g_strlcpy(address, dev_addr, sizeof(address));
798 while ((pos = strchr(address, '_')) != NULL)
801 g_strlcpy(device_address, address, BT_ADDRESS_STRING_SIZE);
805 static char *__bt_extract_device_path(GVariantIter *iter, char *address)
807 char *object_path = NULL;
808 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
810 /* Parse the signature: oa{sa{sv}}} */
811 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path,
814 BT_ERR("Unable to get object path");
817 _bt_convert_device_path_to_address(object_path, device_address);
818 if (g_strcmp0(address, device_address) == 0)
819 return g_strdup(object_path);
823 BT_ERR("Unable to get object path");
827 char *_bt_otp_get_device_object_path(char *address)
830 GDBusProxy *proxy = NULL;
831 GVariant *result = NULL;
832 GVariantIter *iter = NULL;
833 char *object_path = NULL;
835 proxy = g_dbus_proxy_new_sync(conn,
836 G_DBUS_PROXY_FLAGS_NONE, NULL,
839 BT_MANAGER_INTERFACE,
843 BT_ERR("Unable to create proxy: %s", err->message);
847 result = g_dbus_proxy_call_sync(proxy, "GetManagedObjects", NULL,
848 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
851 BT_ERR("Fail to get GetManagedObjects (Error: %s)", err->message);
853 BT_ERR("Fail to get GetManagedObjects");
858 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
859 object_path = __bt_extract_device_path(iter, address);
861 g_variant_unref(result);
862 g_variant_iter_free(iter);
869 g_object_unref(proxy);
874 int _bt_otp_open_otc_and_listen(char *address)
877 GDBusProxy *device_proxy = NULL;
878 GVariant *result = NULL;
879 GError *error = NULL;
880 int ret = BLUETOOTH_ERROR_NONE;
882 object_path = _bt_otp_get_device_object_path(address);
883 if (object_path == NULL) {
884 ret = BLUETOOTH_ERROR_NOT_PAIRED;
888 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
889 NULL, BT_BLUEZ_NAME, object_path,
890 BT_DEVICE_INTERFACE, NULL, NULL);
891 if (device_proxy == NULL) {
892 ret = BLUETOOTH_ERROR_INTERNAL;
897 result = g_dbus_proxy_call_sync(device_proxy, "ListenOtc",
899 G_DBUS_CALL_FLAGS_NONE,
903 if (result == NULL) {
905 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
908 ret = BLUETOOTH_ERROR_INTERNAL;
914 g_variant_unref(result);
916 g_object_unref(device_proxy);
920 static bool __bt_oacp_create_timeout_cb(gpointer user_data)
923 g_free(oacp_create->uuid);
930 int _bt_otp_oacp_write_cb(char *value, int len, int offset,
931 char *remote_addr, struct indicate_info *info)
933 int ret = OACP_SUCCESS;
934 int app_err = BLUETOOTH_ERROR_NONE;
935 int opcode = value[0];
936 uint32_t object_offset, length, object_size;
939 BT_INFO("OACP Opcode 0x%d", opcode);
941 if (!otp_object_list) {
942 ret = OACP_INVALID_OBJ;
948 BT_INFO("OACP_CREATE");
949 object_size = (uint32_t)(value[4] & 0xFF) << 24 |
950 (uint32_t)(value[3] & 0xFF) << 16 |
951 (uint32_t)(value[2] & 0xFF) << 8 |
952 (uint32_t)(value[1] & 0xFF);
954 uuid = g_strndup(value + 5, len - 5);
955 BT_INFO("Size = %lu, UUID = %s", object_size, uuid);
958 /* Create operation already going on. */
959 ret = OACP_OPERATION_FAILED;
962 oacp_create = g_malloc0(sizeof(struct oacp_create_operation));
963 oacp_create->size = object_size;
964 oacp_create->uuid = g_strdup(uuid);
966 g_source_remove(timeout_id);
967 timeout_id = g_timeout_add(BT_OACP_CREATE_MAX_TIMEOUT,
968 (GSourceFunc)__bt_oacp_create_timeout_cb, NULL);
973 ret = OACP_OPCODE_NOT_SUPPORTED;
975 case OACP_CALC_CHECKSUM:
976 ret = OACP_OPCODE_NOT_SUPPORTED;
979 ret = OACP_OPCODE_NOT_SUPPORTED;
982 object_offset = (uint32_t)(value[4] & 0xFF) << 24 |
983 (uint32_t)(value[3] & 0xFF) << 16 |
984 (uint32_t)(value[2] & 0xFF) << 8 |
985 (uint32_t)(value[1] & 0xFF);
986 length = (uint32_t)(value[8] & 0xFF) << 24 |
987 (uint32_t)(value[7] & 0xFF) << 16 |
988 (uint32_t)(value[6] & 0xFF) << 8 |
989 (uint32_t)(value[5] & 0xFF);
991 BT_INFO("Offset = %lu, Length = %lu", object_offset, length);
993 if (oacp_read && otc_connection_status) {
994 /* Read operation already going on. */
995 ret = OACP_OBJECT_LOCKED;
998 oacp_read = g_malloc0(sizeof(struct oacp_operation));
999 oacp_read->offset = object_offset;
1000 oacp_read->length = length;
1001 oacp_read->remote_address = g_strdup(remote_addr);
1002 oacp_read->opcode = OACP_READ;
1004 app_err = _bt_otp_open_otc_and_listen(remote_addr);
1005 if (app_err != BLUETOOTH_ERROR_NONE) {
1006 ret = OACP_OPERATION_FAILED;
1007 g_free(oacp_read->remote_address);
1015 ret = OACP_OPCODE_NOT_SUPPORTED;
1018 ret = OACP_OPCODE_NOT_SUPPORTED;
1021 ret = OACP_OPCODE_NOT_SUPPORTED;
1025 info->resp_opcode = OACP_RESPONSE;
1026 info->req_opcode = opcode;
1027 info->result_code = ret;
1028 info->resp_param = NULL;
1032 void convert_to_hex(struct object_metadata *object, char *type, char *value)
1036 BT_DBG("type : %s", type);
1038 memset(value, 0, 8);
1040 if (!g_strcmp0(type, "size")) {
1042 value[3] = (object->curr_size >> 24) & 0xFF;
1043 value[2] = (object->curr_size >> 16) & 0xFF;
1044 value[1] = (object->curr_size >> 8) & 0xFF;
1045 value[0] = object->curr_size & 0xFF;
1047 value[7] = (object->alloc_size >> 24) & 0xFF;
1048 value[6] = (object->alloc_size >> 16) & 0xFF;
1049 value[5] = (object->alloc_size >> 8) & 0xFF;
1050 value[4] = object->alloc_size & 0xFF;
1052 } else if (!g_strcmp0(type, "date")) {
1054 localtime_r(&(object->first_created), &fc_tm);
1056 value[1] = ((fc_tm.tm_year+1900) >> 8) & 0xFF;
1057 value[0] = (fc_tm.tm_year+1900) & 0xFF;
1058 value[2] = (fc_tm.tm_mon+1) & 0xFF;
1059 value[3] = fc_tm.tm_mday & 0xFF;
1060 value[4] = fc_tm.tm_hour & 0xFF;
1061 value[5] = fc_tm.tm_min & 0xFF;
1062 value[6] = fc_tm.tm_sec & 0xFF;
1064 } else if (!g_strcmp0(type, "id")) {
1066 value[5] = (object->id >> 48) & 0xFF;
1067 value[4] = (object->id >> 32) & 0xFF;
1068 value[3] = (object->id >> 24) & 0xFF;
1069 value[2] = (object->id >> 16) & 0xFF;
1070 value[1] = (object->id >> 8) & 0xFF;
1071 value[0] = object->id & 0xFF;
1073 } else if (!g_strcmp0(type, "props")) {
1074 value[3] = (object->props >> 24) & 0xFF;
1075 value[2] = (object->props >> 16) & 0xFF;
1076 value[1] = (object->props >> 8) & 0xFF;
1077 value[0] = object->props & 0xFF;
1081 void update_obj_metadata_charc_value(struct object_metadata *object)
1083 /* Value can be of maximum eight bytes */
1086 _bt_otp_set_char_value(otp_object_name_obj_path, object->name,
1087 strlen(object->name));
1088 _bt_otp_set_char_value(otp_object_type_obj_path, object->type,
1089 strlen(object->type));
1091 convert_to_hex(object, "size", value);
1092 _bt_otp_set_char_value(otp_object_size_obj_path, value, 8);
1094 convert_to_hex(object, "date", value);
1095 _bt_otp_set_char_value(otp_object_first_created_obj_path, value, 7);
1096 _bt_otp_set_char_value(otp_object_last_modified_obj_path, value, 7);
1098 /* Object ID is optonal for single object server */
1099 if (mutiple_obj_support) {
1100 convert_to_hex(object, "id", value);
1101 _bt_otp_set_char_value(otp_object_id_obj_path, value, 6);
1104 convert_to_hex(object, "props", value);
1105 _bt_otp_set_char_value(otp_object_prop_obj_path, value, 4);
1108 struct object_metadata *_bt_otp_client_find_object(GSList *list, uint64_t id, guint *index)
1111 struct object_metadata *info;
1113 for (l = list; l; l = g_slist_next(l)) {
1117 if (info && (info->id == id))
1123 int _bt_otp_olcp_write_cb(char *value, int len, int offset,
1124 struct indicate_info *info)
1126 int ret = OLCP_SUCCESS;
1127 int opcode = value[0];
1128 struct object_metadata *object;
1132 BT_INFO("OLCP Opcode 0x%d", opcode);
1134 if (!otp_object_list) {
1141 object = (struct object_metadata *) g_slist_nth_data(otp_object_list, 0);
1143 ret = OLCP_OUT_OF_BOUNDS;
1146 update_obj_metadata_charc_value(object);
1147 selected_object = object;
1151 len = g_slist_length(otp_object_list);
1152 object = (struct object_metadata *) g_slist_nth_data(otp_object_list, len-1);
1154 ret = OLCP_OUT_OF_BOUNDS;
1157 update_obj_metadata_charc_value(object);
1158 selected_object = object;
1159 obj_curr_index = len-1;
1162 if (obj_curr_index == 0) {
1163 ret = OLCP_OUT_OF_BOUNDS;
1166 object = (struct object_metadata *) g_slist_nth_data(otp_object_list, obj_curr_index-1);
1168 ret = OLCP_OUT_OF_BOUNDS;
1171 update_obj_metadata_charc_value(object);
1172 selected_object = object;
1173 obj_curr_index -= 1;
1176 object = (struct object_metadata *) g_slist_nth_data(otp_object_list, obj_curr_index+1);
1178 ret = OLCP_OUT_OF_BOUNDS;
1181 update_obj_metadata_charc_value(object);
1182 selected_object = object;
1183 obj_curr_index += 1;
1186 object_id = (uint64_t)(value[6] & 0xFF) << 40 |
1187 (uint64_t)(value[5] & 0xFF) << 32 |
1188 (uint64_t)(value[4] & 0xFF) << 24 |
1189 (uint64_t)(value[3] & 0xFF) << 16 |
1190 (uint64_t)(value[2] & 0xFF) << 8 |
1191 (uint64_t)(value[1] & 0xFF);
1192 BT_INFO("Object ID [%llu]", object_id);
1193 if (selected_object && selected_object->id == object_id)
1196 object = _bt_otp_client_find_object(otp_object_list, object_id, &index);
1198 ret = OLCP_OJECT_ID_NOT_FOUND;
1201 update_obj_metadata_charc_value(object);
1202 selected_object = object;
1203 obj_curr_index = index - 1;
1206 case OLCP_REQ_NO_OBJ:
1207 case OLCP_CLEAR_MARKING:
1209 ret = OLCP_OPCODE_NOT_SUPPORTED;
1213 info->resp_opcode = OLCP_RESPONSE;
1214 info->req_opcode = opcode;
1215 info->result_code = ret;
1216 info->resp_param = NULL;
1217 return BLUETOOTH_ERROR_NONE;
1220 int _bt_otp_obj_name_cb(char *value, int len)
1222 int ret = BLUETOOTH_ERROR_NONE;
1224 struct object_metadata *object = NULL;
1231 filename = g_strndup(value, len);
1232 length = len + strlen(BT_OTP_BASE_DIR_PATH) + 1;
1233 file_path = malloc(length);
1235 snprintf(file_path, length, "%s%s",
1236 BT_OTP_BASE_DIR_PATH, filename);
1237 BT_DBG("file_path = [%s]", file_path);
1239 fp = fopen(file_path, "a");
1241 BT_DBG("fopen() failed : %s", strerror(errno));
1242 ret = BLUETOOTH_ERROR_INTERNAL;
1246 if (stat(file_path, &st) == -1) {
1247 BT_INFO("stat failed: (%d)\n", errno);
1248 ret = BLUETOOTH_ERROR_INTERNAL;
1252 object = g_new0(struct object_metadata, 1);
1254 object->name = g_strdup(filename);
1255 object->type = _otp_convert_uuid_to_uuid128(oacp_create->uuid);
1256 object->first_created = st.st_ctime;
1257 object->last_modified = st.st_ctime;
1258 object->curr_size = (uint32_t)st.st_size;
1259 object->alloc_size = oacp_create->size;
1260 object->id = object_id;
1261 object->props = OBJECT_READ | OBJECT_WRITE;
1263 otp_object_list = g_slist_append(otp_object_list,
1266 update_obj_metadata_charc_value(object);
1267 selected_object = object;
1268 obj_curr_index = g_slist_length(otp_object_list) - 1;
1275 g_free(oacp_create->uuid);
1276 g_free(oacp_create);
1282 static struct otp_char_info *otp_get_char_value(const char *path)
1286 for (tmp = otp_char_list; tmp != NULL; tmp = tmp->next) {
1288 struct otp_char_info *char_info = tmp->data;
1289 if (!g_strcmp0(char_info->char_path, path))
1297 int _bt_otp_read_cb(const char *obj_path, char **value, int *len)
1299 struct otp_char_info *info = NULL;
1302 BT_ERR("Wrong Obj path");
1303 return BLUETOOTH_ERROR_INTERNAL;
1306 if (g_strcmp0(obj_path, otp_feature_obj_path)) {
1307 if (!selected_object) {
1308 return BLUETOOTH_ERROR_OBJECT_NOT_SELECTED;
1312 info = otp_get_char_value(obj_path);
1314 if (info->char_value == NULL || info->value_length == 0)
1315 return BLUETOOTH_ERROR_INTERNAL;
1317 *len = info->value_length;
1318 *value = (char *)malloc(sizeof(char)*(*len));
1319 memcpy(*value, info->char_value, *len);
1321 return BLUETOOTH_ERROR_NONE;
1323 return BLUETOOTH_ERROR_INTERNAL;
1327 static void _otp_convert_address_to_hex(bluetooth_device_address_t *addr_hex,
1328 const char *addr_str)
1331 unsigned int addr[BLUETOOTH_ADDRESS_LENGTH] = { 0, };
1333 if (addr_str == NULL || addr_str[0] == '\0')
1336 i = sscanf(addr_str, "%X:%X:%X:%X:%X:%X", &addr[0], &addr[1],
1337 &addr[2], &addr[3], &addr[4], &addr[5]);
1338 if (i != BLUETOOTH_ADDRESS_LENGTH)
1339 BT_ERR("Invalid format string - [%s]", addr_str);
1341 for (i = 0; i < BLUETOOTH_ADDRESS_LENGTH; i++)
1342 addr_hex->addr[i] = (unsigned char)addr[i];
1345 static void _bt_otp_send_indication(const char *obj_path,
1346 struct indicate_info *info,
1347 bluetooth_device_address_t *remote_address)
1349 int ret = BLUETOOTH_ERROR_NONE;
1350 char value[7] = {0x00};
1354 value[0] = info->resp_opcode & 0xFF;
1355 value[1] = info->req_opcode & 0xFF;
1356 value[2] = info->result_code & 0xFF;
1357 if (info->resp_param) {
1358 value[6] = (info->resp_param[3] >> 24) & 0xFF;
1359 value[5] = (info->resp_param[4] >> 16) & 0xFF;
1360 value[4] = (info->resp_param[5] >> 8) & 0xFF;
1361 value[3] = info->resp_param[6] & 0xFF;
1364 BT_DBG("Opcode: %d", value[1]);
1366 /* Store the status value */
1367 _bt_otp_set_char_value(obj_path, value, 7);
1369 /* Send indication */
1370 ret = bluetooth_gatt_server_set_notification(obj_path, remote_address);
1371 if (ret != BLUETOOTH_ERROR_NONE) {
1372 BT_ERR("_bt_otp_send_control_point_indication failed");
1375 ret = bluetooth_gatt_update_characteristic(obj_path, value, 7);
1376 if (ret != BLUETOOTH_ERROR_NONE) {
1377 BT_ERR("_bt_otp_send_control_point_indication failed");
1382 void _bt_otp_gatt_char_property_changed_event(GVariant *msg,
1385 int result = BLUETOOTH_ERROR_NONE;
1386 GVariantIter value_iter;
1387 const char *property = NULL;
1388 const char *char_path = NULL;
1389 const char *svc_handle = NULL;
1390 GVariant *var = NULL;
1391 GVariant *val = NULL;
1392 g_variant_iter_init(&value_iter, msg);
1394 while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &var))) {
1396 if (property == NULL) {
1397 BT_ERR("Property NULL");
1401 if (!g_strcmp0(property, "WriteValue")) {
1403 BT_INFO("WriteValue");
1404 BT_INFO("Type '%s'\n", g_variant_get_type_string(var));
1407 bluetooth_device_address_t addr_hex = { {0,} };
1412 struct indicate_info info;
1413 g_variant_get(var, "(&s&s&syq@ay)",
1414 &char_path, &svc_handle,
1415 &addr, &req_id, &offset, &val);
1417 len = g_variant_get_size(val);
1419 BT_DBG("Len = %d, BT_ADDR = %s", len, addr);
1421 value = (char *) g_variant_get_data(val);
1422 _otp_convert_address_to_hex(&addr_hex, addr);
1425 if (!g_strcmp0(char_path, otp_oacp_obj_path)) {
1426 result = _bt_otp_oacp_write_cb(value, len, offset, addr, &info);
1427 } else if (!g_strcmp0(char_path, otp_olcp_obj_path)) {
1428 result = _bt_otp_olcp_write_cb(value, len, offset, &info);
1429 } else if (!g_strcmp0(char_path, otp_object_name_obj_path)) {
1431 /* OACP_CREATE is ongoing */
1432 result = _bt_otp_obj_name_cb(value, len);
1434 /* Dont permit writting object name except while creating object.
1435 * As this is directly pointing local objects.
1437 result = BLUETOOTH_ERROR_WRITE_REQUEST_REJECTED;
1439 } else if (!g_strcmp0(char_path, otp_object_first_created_obj_path)) {
1440 _bt_otp_set_char_value(otp_object_first_created_obj_path, value, len);
1441 } else if (!g_strcmp0(char_path, otp_object_last_modified_obj_path)) {
1442 _bt_otp_set_char_value(otp_object_last_modified_obj_path, value, len);
1444 BT_ERR("Wrong Object Path %s", char_path);
1445 result = BLUETOOTH_ERROR_INTERNAL;
1447 bluetooth_gatt_send_response(req_id,
1448 BLUETOOTH_GATT_ATT_REQUEST_TYPE_WRITE,
1449 result, 0, NULL, 0);
1451 /* Send indication for CPs */
1452 if (!g_strcmp0(char_path, otp_oacp_obj_path)) {
1453 if (OACP_indicate) {
1454 _bt_otp_send_indication(char_path, &info, &addr_hex);
1456 } else if (!g_strcmp0(char_path, otp_olcp_obj_path)) {
1457 if (OLCP_indicate) {
1458 _bt_otp_send_indication(char_path, &info, &addr_hex);
1462 BT_ERR("Array Len 0");
1465 BT_ERR("var==NULL");
1467 } else if (!g_strcmp0(property, "ReadValue")) {
1473 result = BLUETOOTH_ERROR_NONE;
1475 BT_INFO("ReadValue");
1476 BT_INFO("Type '%s'\n", g_variant_get_type_string(var));
1478 g_variant_get(var, "(&s&s&syq)", &char_path,
1479 &svc_handle, &addr, &req_id, &offset);
1481 result = _bt_otp_read_cb(char_path, &value, &len);
1483 if (result != BLUETOOTH_ERROR_NONE) {
1484 BT_ERR("ReadValue failed %s", char_path);
1485 bluetooth_gatt_send_response(req_id,
1486 BLUETOOTH_GATT_ATT_REQUEST_TYPE_READ,
1487 result, offset, NULL, 0);
1489 bluetooth_gatt_send_response(req_id,
1490 BLUETOOTH_GATT_ATT_REQUEST_TYPE_READ,
1491 result, offset, value, len);
1495 } else if (!g_strcmp0(property, "NotificationStateChanged")) {
1496 gboolean indicate = FALSE;
1498 g_variant_get(var, "(&s&sb)", &char_path,
1499 &svc_handle, &indicate);
1501 BT_INFO("%s : [%s]", property,
1502 indicate ? "StartNotify" : "StopNotify");
1503 BT_INFO("Type '%s'\n", g_variant_get_type_string(var));
1505 if (!g_strcmp0(char_path, otp_oacp_obj_path)) {
1506 OACP_indicate = indicate;
1507 } else if (!g_strcmp0(char_path, otp_olcp_obj_path)) {
1508 OLCP_indicate = indicate;
1515 void _bt_otp_property_event_filter(GDBusConnection *connection,
1516 const gchar *sender_name,
1517 const gchar *object_path,
1518 const gchar *interface_name,
1519 const gchar *signal_name,
1520 GVariant *parameters,
1525 if (signal_name == NULL) {
1526 BT_ERR("Wrong Signal");
1530 if (g_strcmp0(signal_name, PROPERTIES_CHANGED) == 0) {
1532 g_variant_get(parameters, "(@a{sv}@as)", &value, NULL);
1533 _bt_otp_gatt_char_property_changed_event(value, object_path);
1537 void _bt_otp_adapter_event_filter(GDBusConnection *connection,
1538 const gchar *sender_name,
1539 const gchar *object_path,
1540 const gchar *interface_name,
1541 const gchar *signal_name,
1542 GVariant *parameters,
1545 if (signal_name == NULL) {
1546 BT_ERR("Wrong Signal");
1550 BT_INFO("Interface %s, Signal %s", interface_name, signal_name);
1552 if (g_strcmp0(interface_name, BT_OTP_INTERFACE_NAME) == 0) {
1553 if (strcasecmp(signal_name, BLE_DISABLED) == 0) {
1559 void _bt_otc_disconnected_cb(GDBusConnection *connection,
1560 const gchar *sender_name,
1561 const gchar *object_path,
1562 const gchar *interface_name,
1563 const gchar *signal_name,
1564 GVariant *parameters,
1567 if (signal_name == NULL) {
1568 BT_ERR("Wrong Signal");
1572 BT_INFO("Interface %s, Signal %s", interface_name, signal_name);
1574 if (g_strcmp0(interface_name, BT_DEVICE_INTERFACE) == 0) {
1575 if (strcasecmp(signal_name, OTC_DISCONNECTED) == 0) {
1576 BT_DBG("OTC Channel Disconnected dev_path[%s]",
1578 otc_connection_status = FALSE;
1580 g_free(oacp_read->remote_address);
1587 int _bt_otp_init_event_receiver()
1590 GError *error = NULL;
1593 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
1594 if (error != NULL) {
1595 BT_ERR("ERROR: Can't get on system bus [%s]",
1597 g_clear_error(&error);
1601 property_sub_id = g_dbus_connection_signal_subscribe(conn,
1603 BT_OTP_INTERFACE_NAME,
1605 BT_OTP_OBJECT_PATH, NULL, 0,
1606 _bt_otp_property_event_filter,
1609 adapter_sub_id = g_dbus_connection_signal_subscribe(conn,
1611 BT_OTP_INTERFACE_NAME,
1613 BT_OTP_OBJECT_PATH, NULL, 0,
1614 _bt_otp_adapter_event_filter,
1617 device_sub_id = g_dbus_connection_signal_subscribe(conn,
1618 NULL, BT_DEVICE_INTERFACE,
1619 OTC_DISCONNECTED, NULL, NULL, 0,
1620 _bt_otc_disconnected_cb,
1627 void _bt_otp_deinit_event_receiver(void)
1631 g_dbus_connection_signal_unsubscribe(conn, property_sub_id);
1632 g_dbus_connection_signal_unsubscribe(conn, adapter_sub_id);
1633 g_dbus_connection_signal_unsubscribe(conn, device_sub_id);
1639 static void _bt_otp_sig_handler(int sig)
1644 BT_DBG("caught signal - sigterm\n");
1647 BT_DBG("caught signal - sigint\n");
1650 BT_DBG("caught signal - sigkill\n");
1653 BT_DBG("caught signal %d and ignored\n", sig);
1659 /* OTP Service Main loop */
1662 struct sigaction sa;
1663 BT_ERR("Starting the bt-otp daemon");
1665 memset(&sa, 0, sizeof(sa));
1666 sa.sa_handler = _bt_otp_sig_handler;
1667 sa.sa_flags = SA_SIGINFO;
1668 sigaction(SIGINT, &sa, NULL);
1669 sigaction(SIGTERM, &sa, NULL);
1670 sigaction(SIGKILL, &sa, NULL);
1672 if (_bt_otp_register_interface() != BLUETOOTH_ERROR_NONE) {
1673 BT_ERR("Fail to register otp service");
1677 if (_bt_otp_init_event_receiver() != BLUETOOTH_ERROR_NONE) {
1678 BT_ERR("Fail to init event reciever");
1682 main_loop = g_main_loop_new(NULL, FALSE);
1684 g_main_loop_run(main_loop);
1686 BT_DBG("g_main_loop_quit called!");
1688 if (main_loop != NULL) {
1689 g_main_loop_unref(main_loop);