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 static struct object_metadata *selected_object = NULL;
102 static uint64_t object_id = OBJECT_START_ID;
103 static GSList *otp_object_list = NULL;
104 static GSList *otp_char_list = NULL;
105 static guint obj_curr_index;
106 static int adv_handle = 0;
107 static gboolean OACP_indicate = FALSE;
108 static gboolean OLCP_indicate = FALSE;
109 char *directory = NULL;
110 gboolean mutiple_obj_support = false;
111 static gboolean otc_connection_status = FALSE;
112 struct oacp_operation *oacp_read = NULL;
114 static const gchar otp_introspection_xml[] =
116 " <interface name='org.projectx.otp_service'>"
117 " <method name='enable'>"
118 " <arg type='s' name='directory'/>"
119 " <arg type='i' name='status' direction='out'/>"
121 " <method name='disable'>"
122 " <arg type='i' name='status' direction='out'/>"
124 " <method name='NewConnection'>"
125 " <arg type='o' name='object' direction='in'/>"
126 " <arg type='h' name='fd' direction='in'/>"
131 void _bt_otp_deinit_event_receiver(void);
132 void _bt_otp_unregister_interface(void);
133 void update_obj_metadata_charc_value(struct object_metadata *object);
134 void _bt_convert_device_path_to_address(const char *device_path,
135 char *device_address);
137 static void delete_all_objects(void)
140 for (tmp = otp_object_list; tmp != NULL; tmp = tmp->next) {
142 struct object_metadata *obj_info = tmp->data;
144 g_free(obj_info->name);
146 g_free(obj_info->type);
147 otp_object_list = g_slist_delete_link(otp_object_list, tmp->data);
150 g_slist_free(otp_object_list);
151 otp_object_list = NULL;
154 static void delete_all_characterisitc(void)
157 for (tmp = otp_char_list; tmp != NULL; tmp = tmp->next) {
159 struct otp_char_info *char_info = tmp->data;
160 if (char_info->char_path)
161 g_free(char_info->char_path);
162 if (char_info->char_value)
163 g_free(char_info->char_value);
164 otp_char_list = g_slist_delete_link(otp_char_list, tmp->data);
167 g_slist_free(otp_char_list);
168 otp_char_list = NULL;
171 void _bt_otp_exit(void)
177 delete_all_characterisitc();
180 delete_all_objects();
182 ret = bluetooth_gatt_deinit();
183 if (ret != BLUETOOTH_ERROR_NONE)
184 BT_ERR("Failed to Deinit GATT %d", ret);
186 _bt_otp_deinit_event_receiver();
188 _bt_otp_unregister_interface();
190 /* TODO: Advertising is not getting stopped by this API.
191 * This is because OTP_SERVER_DEINIT dbus call is blocking
192 * BT_SET_ADVERTISING_DATA dbus call. But now advertisment
193 * is stopped because of terminated process logic.
195 ret = bluetooth_set_advertising(adv_handle, FALSE);
196 if (ret != BLUETOOTH_ERROR_NONE)
197 BT_ERR("Failed to stop ADV %d", ret);
199 if (main_loop != NULL) {
200 g_main_loop_quit(main_loop);
204 static void _bt_otp_set_char_value(const char *obj_path,
205 const char *value, int value_length)
211 for (tmp = otp_char_list; tmp != NULL; tmp = tmp->next) {
213 struct otp_char_info *char_info = tmp->data;
214 if (!g_strcmp0(char_info->char_path, obj_path)) {
215 char_info->char_value = g_try_realloc(char_info->char_value, value_length);
216 if (char_info->char_value) {
217 memcpy(char_info->char_value, value, value_length);
218 char_info->value_length = value_length;
227 int add_new_characteristic(const char *char_uuid, bt_gatt_permission_t perms,
228 bt_gatt_characteristic_property_t props, char **obj_path)
230 int ret = BLUETOOTH_ERROR_NONE;
231 struct otp_char_info *char_info = NULL;
233 ret = bluetooth_gatt_add_new_characteristic(otp_obj_path,
234 char_uuid, perms, props, obj_path);
235 if (ret != BLUETOOTH_ERROR_NONE) {
236 BT_ERR("Failed to add new char %d", ret);
240 char_info = g_new0(struct otp_char_info, 1);
241 char_info->char_path = g_strdup(*obj_path);
242 otp_char_list = g_slist_append(otp_char_list, char_info);
247 static char *_otp_convert_uuid_to_uuid128(const char *uuid)
257 uuid128 = g_strdup_printf("0000%s-0000-1000-8000-00805f9b34fb",
263 uuid128 = g_strdup_printf("%s-0000-1000-8000-00805f9b34fb",
269 uuid128 = strdup(uuid);
279 int _bt_otp_prepare_ots(void)
282 int ret = BLUETOOTH_ERROR_NONE;
286 bt_gatt_characteristic_property_t props;
287 bt_gatt_permission_t perms;
288 char supp_feat[OTP_FEATURE_LENGTH] = { 0x08, 0x00, 0x00, 0x00,
289 0x80, 0x00, 0x00, 0x00 };
291 ret = bluetooth_gatt_init();
292 if (ret != BLUETOOTH_ERROR_NONE) {
293 BT_ERR("Failed to Init GATT %d", ret);
297 service_uuid = _otp_convert_uuid_to_uuid128(OTP_UUID);
298 ret = bluetooth_gatt_add_service(service_uuid, &otp_obj_path);
299 if (ret != BLUETOOTH_ERROR_NONE) {
300 BT_ERR("Failed to add service %d", ret);
304 /* Characteristic OTP Feature */
305 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ;
306 perms = BLUETOOTH_GATT_PERMISSION_READ;
307 char_uuid = _otp_convert_uuid_to_uuid128(OTP_FEATURE_UUID);
308 ret = add_new_characteristic(char_uuid, perms, props,
309 &otp_feature_obj_path);
310 if (ret != BLUETOOTH_ERROR_NONE)
313 ret = bluetooth_gatt_set_characteristic_value(otp_feature_obj_path,
314 supp_feat, OTP_FEATURE_LENGTH);
315 if (ret != BLUETOOTH_ERROR_NONE) {
316 BT_ERR("Failed to set char value %d", ret);
320 _bt_otp_set_char_value(otp_feature_obj_path, supp_feat,
323 /* Characteristic Object Name */
324 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ;
325 perms = BLUETOOTH_GATT_PERMISSION_READ;
326 char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_NAME_UUID);
327 ret = add_new_characteristic(char_uuid, perms, props,
328 &otp_object_name_obj_path);
329 if (ret != BLUETOOTH_ERROR_NONE)
332 /* Characteristic Object Type */
333 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ;
334 perms = BLUETOOTH_GATT_PERMISSION_READ;
335 char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_TYPE_UUID);
336 ret = add_new_characteristic(char_uuid, perms, props,
337 &otp_object_type_obj_path);
338 if (ret != BLUETOOTH_ERROR_NONE)
341 /* Characteristic Object Size */
342 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ;
343 perms = BLUETOOTH_GATT_PERMISSION_READ;
344 char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_SIZE_UUID);
345 ret = add_new_characteristic(char_uuid, perms, props,
346 &otp_object_size_obj_path);
347 if (ret != BLUETOOTH_ERROR_NONE)
350 /* Characteristic Object First-Created */
351 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ |
352 BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE;
353 perms = BLUETOOTH_GATT_PERMISSION_READ |
354 BLUETOOTH_GATT_PERMISSION_WRITE;
355 char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_FIRST_CREATED_UUID);
356 ret = add_new_characteristic(char_uuid, perms, props,
357 &otp_object_first_created_obj_path);
358 if (ret != BLUETOOTH_ERROR_NONE)
361 /* Characteristic Object Last-Modified */
362 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ |
363 BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE;
364 perms = BLUETOOTH_GATT_PERMISSION_READ |
365 BLUETOOTH_GATT_PERMISSION_WRITE;
366 char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_LAST_MODIFIED_UUID);
367 ret = add_new_characteristic(char_uuid, perms, props,
368 &otp_object_last_modified_obj_path);
369 if (ret != BLUETOOTH_ERROR_NONE)
372 /* Object ID is mandatory for mutiple object server */
373 if (mutiple_obj_support) {
374 /* Characteristic Object ID */
375 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ;
376 perms = BLUETOOTH_GATT_PERMISSION_READ;
377 char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_ID_UUID);
378 ret = add_new_characteristic(char_uuid, perms, props,
379 &otp_object_id_obj_path);
380 if (ret != BLUETOOTH_ERROR_NONE)
384 /* Characteristic Object Properties */
385 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ;
386 perms = BLUETOOTH_GATT_PERMISSION_READ;
387 char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_PROP_UUID);
388 ret = add_new_characteristic(char_uuid, perms, props,
389 &otp_object_prop_obj_path);
390 if (ret != BLUETOOTH_ERROR_NONE)
393 /* Characteristic OACP */
394 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE |
395 BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_INDICATE;
396 perms = BLUETOOTH_GATT_PERMISSION_WRITE;
397 char_uuid = _otp_convert_uuid_to_uuid128(OTP_OACP_UUID);
398 ret = add_new_characteristic(char_uuid, perms, props,
400 if (ret != BLUETOOTH_ERROR_NONE)
404 desc_uuid = _otp_convert_uuid_to_uuid128(OTP_CP_CCC_DESC_UUID);
405 perms = BLUETOOTH_GATT_PERMISSION_READ |
406 BLUETOOTH_GATT_PERMISSION_WRITE;
407 ret = bluetooth_gatt_add_descriptor(otp_oacp_obj_path, desc_uuid,
408 perms, &otp_oacp_desc_obj_path);
409 if (ret != BLUETOOTH_ERROR_NONE) {
410 BT_ERR("Failed to add new char descriptor %d", ret);
414 /* OLCP Characteristics is not required
415 * for single object server
417 if (mutiple_obj_support) {
418 /* Characteristic OLCP */
419 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE |
420 BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_INDICATE;
421 perms = BLUETOOTH_GATT_PERMISSION_WRITE;
422 char_uuid = _otp_convert_uuid_to_uuid128(OTP_OLCP_UUID);
423 ret = add_new_characteristic(char_uuid, perms, props,
425 if (ret != BLUETOOTH_ERROR_NONE)
429 desc_uuid = _otp_convert_uuid_to_uuid128(OTP_CP_CCC_DESC_UUID);
430 perms = BLUETOOTH_GATT_PERMISSION_READ |
431 BLUETOOTH_GATT_PERMISSION_WRITE;
432 ret = bluetooth_gatt_add_descriptor(otp_olcp_obj_path, desc_uuid,
433 perms, &otp_olcp_desc_obj_path);
434 if (ret != BLUETOOTH_ERROR_NONE) {
435 BT_ERR("Failed to add new char descriptor %d", ret);
440 /* Register service */
441 ret = bluetooth_gatt_register_service(otp_obj_path);
442 if (ret != BLUETOOTH_ERROR_NONE) {
443 BT_ERR("Failed to register service %d", ret);
447 /* Register Application */
448 ret = bluetooth_gatt_register_application();
449 if (ret != BLUETOOTH_ERROR_NONE) {
450 BT_ERR("Failed to register application %d", ret);
458 delete_all_characterisitc();
462 int _bt_otp_set_advertising_data(void)
468 guint8 data[4] = {0x03, 0x02, 0x25, 0x18};
469 bluetooth_advertising_data_t adv;
471 BT_DBG("%x %x %x %x", data[0], data[1], data[2], data[3]);
472 memcpy(adv.data, data, sizeof(data));
473 ret = bluetooth_set_advertising_data(adv_handle, &adv, sizeof(data));
474 if (ret != BLUETOOTH_ERROR_NONE) {
475 BT_ERR("Failed to set ADV data %d", ret);
479 ret = bluetooth_set_advertising(adv_handle, TRUE);
480 if (ret != BLUETOOTH_ERROR_NONE) {
481 BT_ERR("Failed to set ADV %d", ret);
488 void _bt_otp_start_write_on_fd()
490 char buf[BT_L2CAP_BUFFER_LEN];
495 char file_path[BT_FILE_PATH_MAX_LEN] = {0, };
498 if (!selected_object) {
499 BT_DBG("Object not selected");
503 snprintf(file_path, sizeof(file_path), "%s%s",
504 directory, selected_object->name);
505 BT_DBG("file_path = [%s]", file_path);
507 fp = fopen(file_path, "r");
509 BT_DBG("fopen() failed : %s", strerror(errno));
513 BT_DBG("length [%d]", oacp_read->length);
514 length = oacp_read->length;
517 if (length < BT_L2CAP_BUFFER_LEN)
520 len = BT_L2CAP_BUFFER_LEN;
522 read = fread(buf, 1, len, fp);
523 written = write(oacp_read->fd, buf, len);
526 BT_DBG("read [%d], written [%d], rem_len [%d]",
527 read, written, length);
532 g_free(oacp_read->remote_address);
537 static void _bt_otp_method(GDBusConnection *connection,
539 const gchar *object_path,
540 const gchar *interface_name,
541 const gchar *method_name,
542 GVariant *parameters,
543 GDBusMethodInvocation *invocation,
547 int status = BLUETOOTH_ERROR_NONE;
549 BT_DBG("Method[%s] Object Path[%s] Interface Name[%s]",
550 method_name, object_path, interface_name);
552 if (g_strcmp0(method_name, "enable") == 0) {
554 GError *error = NULL;
555 const gchar *filename = NULL;
556 char absolute_path[ABSOLUTE_PATH_MAX_LENGTH];
557 GSList *list = NULL, *l = NULL;
559 struct object_metadata *object = NULL;
561 g_variant_get(parameters, "(s)", &directory);
562 BT_DBG("Directory = [%s]", directory);
564 dir = g_dir_open(directory, 0, &error);
566 BT_ERR("Failed to open directory: %s", error->message);
568 status = BLUETOOTH_ERROR_INVALID_DIRECTORY;
572 while ((filename = g_dir_read_name(dir))) {
573 list = g_slist_append(list, (gpointer) filename);
579 BT_DBG("No object found in given directory");
580 status = BLUETOOTH_ERROR_NO_OBJECTS_FOUND;
584 if (g_slist_length(list) > 1)
585 mutiple_obj_support = true;
587 for (l = list; l != NULL; l = l->next) {
588 if (!l->data) continue;
589 snprintf(absolute_path, sizeof(absolute_path), "%s%s", directory,
592 BT_INFO("filename: %s, absoulte_path: %s",
593 (char *)l->data, absolute_path);
595 if (stat(absolute_path, &st) == -1) {
596 BT_INFO("stat failed: (%d)\n", errno);
600 object = g_new0(struct object_metadata, 1);
602 object->name = g_strdup((const gchar *)l->data);
603 object->type = _otp_convert_uuid_to_uuid128(UNSUPPORTED_OBJECT_TYPE_UUID);
604 object->first_created = st.st_ctime;
605 object->last_modified = st.st_ctime;
606 object->curr_size = (uint32_t) st.st_size;
607 object->alloc_size = (uint32_t) st.st_size;
608 object->id = object_id;
609 object->props = OBJECT_READ;
611 otp_object_list = g_slist_append(otp_object_list,
618 if (_bt_otp_prepare_ots() != BLUETOOTH_ERROR_NONE) {
619 BT_ERR("Fail to prepare OTP Proxy");
620 status = BLUETOOTH_ERROR_INTERNAL;
624 /* If single object is supported, make that as
625 * selected object and update the metadata for the same.
627 if (!mutiple_obj_support) {
628 BT_INFO("Server supports single object");
629 selected_object = (struct object_metadata *) g_slist_nth_data(otp_object_list, 0);
631 update_obj_metadata_charc_value(selected_object);
634 BT_DBG("advertsing");
635 if (_bt_otp_set_advertising_data() != BLUETOOTH_ERROR_NONE) {
636 BT_ERR("Fail to set advertising data");
637 status = BLUETOOTH_ERROR_INTERNAL;
641 g_dbus_method_invocation_return_value(invocation,
642 g_variant_new("(i)", status));
644 } else if (g_strcmp0(method_name, "disable") == 0) {
645 g_dbus_method_invocation_return_value(invocation,
646 g_variant_new("(i)", status));
649 } else if (g_strcmp0(method_name, "NewConnection") == 0) {
652 GUnixFDList *fd_list;
654 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
657 g_variant_get(parameters, "(oh)", &dev_path, &index);
659 msg = g_dbus_method_invocation_get_message(invocation);
660 fd_list = g_dbus_message_get_unix_fd_list(msg);
661 if (fd_list == NULL) {
662 BT_ERR("fd_list is NULL");
666 fd = g_unix_fd_list_get(fd_list, index, NULL);
668 BT_ERR("Invalid fd return");
672 _bt_convert_device_path_to_address(dev_path, address);
674 BT_INFO("OTC Connected fd: %d, address %s", fd, address);
676 /* OTC Connected, but no on going request */
680 otc_connection_status = TRUE;
682 if (oacp_read->opcode == OACP_READ)
683 _bt_otp_start_write_on_fd();
685 g_dbus_method_invocation_return_value(invocation, NULL);
690 static const GDBusInterfaceVTable otp_method_table = {
696 static void _bt_otp_on_bus_acquired(GDBusConnection *connection,
697 const gchar *name, gpointer user_data)
700 GError *error = NULL;
706 object_id = g_dbus_connection_register_object(connection,
708 otp_node_info->interfaces[0],
711 if (object_id == 0) {
712 BT_ERR("Failed to register method table: %s", error->message);
714 g_dbus_node_info_unref(otp_node_info);
720 static void _bt_otp_on_name_acquired(GDBusConnection *connection,
727 static void _bt_otp_on_name_lost(GDBusConnection *connection,
732 g_object_unref(g_conn);
734 g_dbus_node_info_unref(otp_node_info);
735 g_bus_unown_name(g_owner_id);
738 int _bt_otp_register_interface(void)
741 GError *error = NULL;
744 otp_node_info = g_dbus_node_info_new_for_xml(otp_introspection_xml, &error);
745 if (!otp_node_info) {
746 BT_ERR("Failed to install: %s", error->message);
747 return BLUETOOTH_ERROR_INTERNAL;
750 owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
752 G_BUS_NAME_OWNER_FLAGS_NONE,
753 _bt_otp_on_bus_acquired,
754 _bt_otp_on_name_acquired,
755 _bt_otp_on_name_lost,
757 g_owner_id = owner_id;
758 BT_DBG("owner_id is [%d]\n", owner_id);
761 return BLUETOOTH_ERROR_NONE;
764 void _bt_otp_unregister_interface(void)
768 g_object_unref(g_conn);
770 g_dbus_node_info_unref(otp_node_info);
771 g_bus_unown_name(g_owner_id);
777 void _bt_convert_device_path_to_address(const char *device_path,
778 char *device_address)
780 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
783 dev_addr = strstr(device_path, "dev_");
784 if (dev_addr != NULL) {
787 g_strlcpy(address, dev_addr, sizeof(address));
789 while ((pos = strchr(address, '_')) != NULL)
792 g_strlcpy(device_address, address, BT_ADDRESS_STRING_SIZE);
796 static char *__bt_extract_device_path(GVariantIter *iter, char *address)
798 char *object_path = NULL;
799 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
801 /* Parse the signature: oa{sa{sv}}} */
802 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path,
805 BT_ERR("Unable to get object path");
808 _bt_convert_device_path_to_address(object_path, device_address);
809 if (g_strcmp0(address, device_address) == 0)
810 return g_strdup(object_path);
814 BT_ERR("Unable to get object path");
818 char *_bt_otp_get_device_object_path(char *address)
821 GDBusProxy *proxy = NULL;
822 GVariant *result = NULL;
823 GVariantIter *iter = NULL;
824 char *object_path = NULL;
826 proxy = g_dbus_proxy_new_sync(conn,
827 G_DBUS_PROXY_FLAGS_NONE, NULL,
830 BT_MANAGER_INTERFACE,
834 BT_ERR("Unable to create proxy: %s", err->message);
838 result = g_dbus_proxy_call_sync(proxy, "GetManagedObjects", NULL,
839 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
842 BT_ERR("Fail to get GetManagedObjects (Error: %s)", err->message);
844 BT_ERR("Fail to get GetManagedObjects");
849 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
850 object_path = __bt_extract_device_path(iter, address);
852 g_variant_unref(result);
853 g_variant_iter_free(iter);
860 g_object_unref(proxy);
865 int _bt_otp_open_otc_and_listen(char *address)
868 GDBusProxy *device_proxy = NULL;
869 GVariant *result = NULL;
870 GError *error = NULL;
871 int ret = BLUETOOTH_ERROR_NONE;
873 object_path = _bt_otp_get_device_object_path(address);
874 if (object_path == NULL) {
875 ret = BLUETOOTH_ERROR_NOT_PAIRED;
879 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
880 NULL, BT_BLUEZ_NAME, object_path,
881 BT_DEVICE_INTERFACE, NULL, NULL);
882 if (device_proxy == NULL) {
883 ret = BLUETOOTH_ERROR_INTERNAL;
888 result = g_dbus_proxy_call_sync(device_proxy, "ListenOtc",
890 G_DBUS_CALL_FLAGS_NONE,
894 if (result == NULL) {
896 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
899 ret = BLUETOOTH_ERROR_INTERNAL;
905 g_variant_unref(result);
907 g_object_unref(device_proxy);
911 int _bt_otp_oacp_write_cb(char *value, int len, int offset,
912 char *remote_addr, struct indicate_info *info)
914 int ret = OACP_SUCCESS;
915 int app_err = BLUETOOTH_ERROR_NONE;
916 int opcode = value[0];
917 uint32_t object_offset, length;
919 BT_INFO("OACP Opcode 0x%d", opcode);
921 if (!otp_object_list) {
922 ret = OACP_INVALID_OBJ;
928 ret = OACP_OPCODE_NOT_SUPPORTED;
931 ret = OACP_OPCODE_NOT_SUPPORTED;
933 case OACP_CALC_CHECKSUM:
934 ret = OACP_OPCODE_NOT_SUPPORTED;
937 ret = OACP_OPCODE_NOT_SUPPORTED;
940 object_offset = (uint32_t)(value[4] & 0xFF) << 24 |
941 (uint32_t)(value[3] & 0xFF) << 16 |
942 (uint32_t)(value[2] & 0xFF) << 8 |
943 (uint32_t)(value[1] & 0xFF);
944 length = (uint32_t)(value[8] & 0xFF) << 24 |
945 (uint32_t)(value[7] & 0xFF) << 16 |
946 (uint32_t)(value[6] & 0xFF) << 8 |
947 (uint32_t)(value[5] & 0xFF);
949 BT_INFO("Offset = %lu, Length = %lu", object_offset, length);
951 if (oacp_read && otc_connection_status) {
952 /* Read operation already going on. */
953 ret = OACP_OBJECT_LOCKED;
956 oacp_read = g_malloc0(sizeof(struct oacp_operation));
957 oacp_read->offset = object_offset;
958 oacp_read->length = length;
959 oacp_read->remote_address = g_strdup(remote_addr);
960 oacp_read->opcode = OACP_READ;
962 app_err = _bt_otp_open_otc_and_listen(remote_addr);
963 if (app_err != BLUETOOTH_ERROR_NONE) {
964 ret = OACP_OPERATION_FAILED;
965 g_free(oacp_read->remote_address);
973 ret = OACP_OPCODE_NOT_SUPPORTED;
976 ret = OACP_OPCODE_NOT_SUPPORTED;
979 ret = OACP_OPCODE_NOT_SUPPORTED;
983 info->resp_opcode = OACP_RESPONSE;
984 info->req_opcode = opcode;
985 info->result_code = ret;
986 info->resp_param = NULL;
990 void convert_to_hex(struct object_metadata *object, char *type, char *value)
994 BT_DBG("type : %s", type);
998 if (!g_strcmp0(type, "size")) {
1000 value[3] = (object->curr_size >> 24) & 0xFF;
1001 value[2] = (object->curr_size >> 16) & 0xFF;
1002 value[1] = (object->curr_size >> 8) & 0xFF;
1003 value[0] = object->curr_size & 0xFF;
1005 value[7] = (object->alloc_size >> 24) & 0xFF;
1006 value[6] = (object->alloc_size >> 16) & 0xFF;
1007 value[5] = (object->alloc_size >> 8) & 0xFF;
1008 value[4] = object->alloc_size & 0xFF;
1010 } else if (!g_strcmp0(type, "date")) {
1012 localtime_r(&(object->first_created), &fc_tm);
1014 value[1] = ((fc_tm.tm_year+1900) >> 8) & 0xFF;
1015 value[0] = (fc_tm.tm_year+1900) & 0xFF;
1016 value[2] = (fc_tm.tm_mon+1) & 0xFF;
1017 value[3] = fc_tm.tm_mday & 0xFF;
1018 value[4] = fc_tm.tm_hour & 0xFF;
1019 value[5] = fc_tm.tm_min & 0xFF;
1020 value[6] = fc_tm.tm_sec & 0xFF;
1022 } else if (!g_strcmp0(type, "id")) {
1024 value[5] = (object->id >> 48) & 0xFF;
1025 value[4] = (object->id >> 32) & 0xFF;
1026 value[3] = (object->id >> 24) & 0xFF;
1027 value[2] = (object->id >> 16) & 0xFF;
1028 value[1] = (object->id >> 8) & 0xFF;
1029 value[0] = object->id & 0xFF;
1031 } else if (!g_strcmp0(type, "props")) {
1032 value[3] = (object->props >> 24) & 0xFF;
1033 value[2] = (object->props >> 16) & 0xFF;
1034 value[1] = (object->props >> 8) & 0xFF;
1035 value[0] = object->props & 0xFF;
1039 void update_obj_metadata_charc_value(struct object_metadata *object)
1041 /* Value can be of maximum eight bytes */
1044 _bt_otp_set_char_value(otp_object_name_obj_path, object->name,
1045 strlen(object->name));
1046 _bt_otp_set_char_value(otp_object_type_obj_path, object->type,
1047 strlen(object->type));
1049 convert_to_hex(object, "size", value);
1050 _bt_otp_set_char_value(otp_object_size_obj_path, value, 8);
1052 convert_to_hex(object, "date", value);
1053 _bt_otp_set_char_value(otp_object_first_created_obj_path, value, 7);
1054 _bt_otp_set_char_value(otp_object_last_modified_obj_path, value, 7);
1056 /* Object ID is optonal for single object server */
1057 if (mutiple_obj_support) {
1058 convert_to_hex(object, "id", value);
1059 _bt_otp_set_char_value(otp_object_id_obj_path, value, 6);
1062 convert_to_hex(object, "props", value);
1063 _bt_otp_set_char_value(otp_object_prop_obj_path, value, 4);
1066 struct object_metadata *_bt_otp_client_find_object(GSList *list, uint64_t id, guint *index)
1069 struct object_metadata *info;
1071 for (l = list; l; l = g_slist_next(l)) {
1075 if (info && (info->id == id))
1081 int _bt_otp_olcp_write_cb(char *value, int len, int offset,
1082 struct indicate_info *info)
1084 int ret = OLCP_SUCCESS;
1085 int opcode = value[0];
1086 struct object_metadata *object;
1090 BT_INFO("OLCP Opcode 0x%d", opcode);
1092 if (!otp_object_list) {
1099 object = (struct object_metadata *) g_slist_nth_data(otp_object_list, 0);
1101 ret = OLCP_OUT_OF_BOUNDS;
1104 update_obj_metadata_charc_value(object);
1105 selected_object = object;
1109 len = g_slist_length(otp_object_list);
1110 object = (struct object_metadata *) g_slist_nth_data(otp_object_list, len-1);
1112 ret = OLCP_OUT_OF_BOUNDS;
1115 update_obj_metadata_charc_value(object);
1116 selected_object = object;
1117 obj_curr_index = len-1;
1120 if (obj_curr_index == 0) {
1121 ret = OLCP_OUT_OF_BOUNDS;
1124 object = (struct object_metadata *) g_slist_nth_data(otp_object_list, obj_curr_index-1);
1126 ret = OLCP_OUT_OF_BOUNDS;
1129 update_obj_metadata_charc_value(object);
1130 selected_object = object;
1131 obj_curr_index -= 1;
1134 object = (struct object_metadata *) g_slist_nth_data(otp_object_list, obj_curr_index+1);
1136 ret = OLCP_OUT_OF_BOUNDS;
1139 update_obj_metadata_charc_value(object);
1140 selected_object = object;
1141 obj_curr_index += 1;
1144 object_id = (uint64_t)(value[6] & 0xFF) << 40 |
1145 (uint64_t)(value[5] & 0xFF) << 32 |
1146 (uint64_t)(value[4] & 0xFF) << 24 |
1147 (uint64_t)(value[3] & 0xFF) << 16 |
1148 (uint64_t)(value[2] & 0xFF) << 8 |
1149 (uint64_t)(value[1] & 0xFF);
1150 BT_INFO("Object ID [%llu]", object_id);
1151 if (selected_object && selected_object->id == object_id)
1154 object = _bt_otp_client_find_object(otp_object_list, object_id, &index);
1156 ret = OLCP_OJECT_ID_NOT_FOUND;
1159 update_obj_metadata_charc_value(object);
1160 selected_object = object;
1161 obj_curr_index = index - 1;
1164 case OLCP_REQ_NO_OBJ:
1165 case OLCP_CLEAR_MARKING:
1167 ret = OLCP_OPCODE_NOT_SUPPORTED;
1171 info->resp_opcode = OLCP_RESPONSE;
1172 info->req_opcode = opcode;
1173 info->result_code = ret;
1174 info->resp_param = NULL;
1175 return BLUETOOTH_ERROR_NONE;
1178 static struct otp_char_info *otp_get_char_value(const char *path)
1182 for (tmp = otp_char_list; tmp != NULL; tmp = tmp->next) {
1184 struct otp_char_info *char_info = tmp->data;
1185 if (!g_strcmp0(char_info->char_path, path))
1193 int _bt_otp_read_cb(const char *obj_path, char **value, int *len)
1195 struct otp_char_info *info = NULL;
1198 BT_ERR("Wrong Obj path");
1199 return BLUETOOTH_ERROR_INTERNAL;
1202 if (g_strcmp0(obj_path, otp_feature_obj_path)) {
1203 if (!selected_object) {
1204 return BLUETOOTH_ERROR_OBJECT_NOT_SELECTED;
1208 info = otp_get_char_value(obj_path);
1210 if (info->char_value == NULL || info->value_length == 0)
1211 return BLUETOOTH_ERROR_INTERNAL;
1213 *len = info->value_length;
1214 *value = (char *)malloc(sizeof(char)*(*len));
1215 memcpy(*value, info->char_value, *len);
1217 return BLUETOOTH_ERROR_NONE;
1219 return BLUETOOTH_ERROR_INTERNAL;
1223 static void _otp_convert_address_to_hex(bluetooth_device_address_t *addr_hex,
1224 const char *addr_str)
1227 unsigned int addr[BLUETOOTH_ADDRESS_LENGTH] = { 0, };
1229 if (addr_str == NULL || addr_str[0] == '\0')
1232 i = sscanf(addr_str, "%X:%X:%X:%X:%X:%X", &addr[0], &addr[1],
1233 &addr[2], &addr[3], &addr[4], &addr[5]);
1234 if (i != BLUETOOTH_ADDRESS_LENGTH)
1235 BT_ERR("Invalid format string - [%s]", addr_str);
1237 for (i = 0; i < BLUETOOTH_ADDRESS_LENGTH; i++)
1238 addr_hex->addr[i] = (unsigned char)addr[i];
1241 static void _bt_otp_send_indication(const char *obj_path,
1242 struct indicate_info *info,
1243 bluetooth_device_address_t *remote_address)
1245 int ret = BLUETOOTH_ERROR_NONE;
1246 char value[7] = {0x00};
1250 value[0] = info->resp_opcode & 0xFF;
1251 value[1] = info->req_opcode & 0xFF;
1252 value[2] = info->result_code & 0xFF;
1253 if (info->resp_param) {
1254 value[6] = (info->resp_param[3] >> 24) & 0xFF;
1255 value[5] = (info->resp_param[4] >> 16) & 0xFF;
1256 value[4] = (info->resp_param[5] >> 8) & 0xFF;
1257 value[3] = info->resp_param[6] & 0xFF;
1260 BT_DBG("Opcode: %d", value[1]);
1262 /* Store the status value */
1263 _bt_otp_set_char_value(obj_path, value, 7);
1265 /* Send indication */
1266 ret = bluetooth_gatt_server_set_notification(obj_path, remote_address);
1267 if (ret != BLUETOOTH_ERROR_NONE) {
1268 BT_ERR("_bt_otp_send_control_point_indication failed");
1271 ret = bluetooth_gatt_update_characteristic(obj_path, value, 7);
1272 if (ret != BLUETOOTH_ERROR_NONE) {
1273 BT_ERR("_bt_otp_send_control_point_indication failed");
1278 void _bt_otp_gatt_char_property_changed_event(GVariant *msg,
1281 int result = BLUETOOTH_ERROR_NONE;
1282 GVariantIter value_iter;
1283 const char *property = NULL;
1284 const char *char_path = NULL;
1285 const char *svc_handle = NULL;
1286 GVariant *var = NULL;
1287 GVariant *val = NULL;
1288 g_variant_iter_init(&value_iter, msg);
1290 while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &var))) {
1292 if (property == NULL) {
1293 BT_ERR("Property NULL");
1297 if (!g_strcmp0(property, "WriteValue")) {
1299 BT_INFO("WriteValue");
1300 BT_INFO("Type '%s'\n", g_variant_get_type_string(var));
1303 bluetooth_device_address_t addr_hex = { {0,} };
1308 struct indicate_info info;
1309 g_variant_get(var, "(&s&s&syq@ay)",
1310 &char_path, &svc_handle,
1311 &addr, &req_id, &offset, &val);
1313 len = g_variant_get_size(val);
1315 BT_DBG("Len = %d, BT_ADDR = %s", len, addr);
1317 value = (char *) g_variant_get_data(val);
1318 _otp_convert_address_to_hex(&addr_hex, addr);
1321 if (!g_strcmp0(char_path, otp_oacp_obj_path)) {
1322 result = _bt_otp_oacp_write_cb(value, len, offset, addr, &info);
1323 } else if (!g_strcmp0(char_path, otp_olcp_obj_path)) {
1324 result = _bt_otp_olcp_write_cb(value, len, offset, &info);
1326 BT_ERR("Wrong Object Path %s", char_path);
1327 result = BLUETOOTH_ERROR_INTERNAL;
1329 bluetooth_gatt_send_response(req_id,
1330 BLUETOOTH_GATT_ATT_REQUEST_TYPE_WRITE,
1331 result, 0, NULL, 0);
1333 /* Send indication for CPs */
1334 if (!g_strcmp0(char_path, otp_oacp_obj_path)) {
1335 if (OACP_indicate) {
1336 _bt_otp_send_indication(char_path, &info, &addr_hex);
1338 } else if (!g_strcmp0(char_path, otp_olcp_obj_path)) {
1339 if (OLCP_indicate) {
1340 _bt_otp_send_indication(char_path, &info, &addr_hex);
1344 BT_ERR("Array Len 0");
1347 BT_ERR("var==NULL");
1349 } else if (!g_strcmp0(property, "ReadValue")) {
1355 result = BLUETOOTH_ERROR_NONE;
1357 BT_INFO("ReadValue");
1358 BT_INFO("Type '%s'\n", g_variant_get_type_string(var));
1360 g_variant_get(var, "(&s&s&syq)", &char_path,
1361 &svc_handle, &addr, &req_id, &offset);
1363 result = _bt_otp_read_cb(char_path, &value, &len);
1365 if (result != BLUETOOTH_ERROR_NONE) {
1366 BT_ERR("ReadValue failed %s", char_path);
1367 bluetooth_gatt_send_response(req_id,
1368 BLUETOOTH_GATT_ATT_REQUEST_TYPE_READ,
1369 result, offset, NULL, 0);
1371 bluetooth_gatt_send_response(req_id,
1372 BLUETOOTH_GATT_ATT_REQUEST_TYPE_READ,
1373 result, offset, value, len);
1377 } else if (!g_strcmp0(property, "NotificationStateChanged")) {
1378 gboolean indicate = FALSE;
1380 g_variant_get(var, "(&s&sb)", &char_path,
1381 &svc_handle, &indicate);
1383 BT_INFO("%s : [%s]", property,
1384 indicate ? "StartNotify" : "StopNotify");
1385 BT_INFO("Type '%s'\n", g_variant_get_type_string(var));
1387 if (!g_strcmp0(char_path, otp_oacp_obj_path)) {
1388 OACP_indicate = indicate;
1389 } else if (!g_strcmp0(char_path, otp_olcp_obj_path)) {
1390 OLCP_indicate = indicate;
1397 void _bt_otp_property_event_filter(GDBusConnection *connection,
1398 const gchar *sender_name,
1399 const gchar *object_path,
1400 const gchar *interface_name,
1401 const gchar *signal_name,
1402 GVariant *parameters,
1407 if (signal_name == NULL) {
1408 BT_ERR("Wrong Signal");
1412 if (g_strcmp0(signal_name, PROPERTIES_CHANGED) == 0) {
1414 g_variant_get(parameters, "(@a{sv}@as)", &value, NULL);
1415 _bt_otp_gatt_char_property_changed_event(value, object_path);
1419 void _bt_otp_adapter_event_filter(GDBusConnection *connection,
1420 const gchar *sender_name,
1421 const gchar *object_path,
1422 const gchar *interface_name,
1423 const gchar *signal_name,
1424 GVariant *parameters,
1427 if (signal_name == NULL) {
1428 BT_ERR("Wrong Signal");
1432 BT_INFO("Interface %s, Signal %s", interface_name, signal_name);
1434 if (g_strcmp0(interface_name, BT_OTP_INTERFACE_NAME) == 0) {
1435 if (strcasecmp(signal_name, BLE_DISABLED) == 0) {
1441 void _bt_otc_disconnected_cb(GDBusConnection *connection,
1442 const gchar *sender_name,
1443 const gchar *object_path,
1444 const gchar *interface_name,
1445 const gchar *signal_name,
1446 GVariant *parameters,
1449 if (signal_name == NULL) {
1450 BT_ERR("Wrong Signal");
1454 BT_INFO("Interface %s, Signal %s", interface_name, signal_name);
1456 if (g_strcmp0(interface_name, BT_DEVICE_INTERFACE) == 0) {
1457 if (strcasecmp(signal_name, OTC_DISCONNECTED) == 0) {
1458 BT_DBG("OTC Channel Disconnected dev_path[%s]",
1460 otc_connection_status = FALSE;
1462 g_free(oacp_read->remote_address);
1469 int _bt_otp_init_event_receiver()
1472 GError *error = NULL;
1475 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
1476 if (error != NULL) {
1477 BT_ERR("ERROR: Can't get on system bus [%s]",
1479 g_clear_error(&error);
1483 property_sub_id = g_dbus_connection_signal_subscribe(conn,
1485 BT_OTP_INTERFACE_NAME,
1487 BT_OTP_OBJECT_PATH, NULL, 0,
1488 _bt_otp_property_event_filter,
1491 adapter_sub_id = g_dbus_connection_signal_subscribe(conn,
1493 BT_OTP_INTERFACE_NAME,
1495 BT_OTP_OBJECT_PATH, NULL, 0,
1496 _bt_otp_adapter_event_filter,
1499 device_sub_id = g_dbus_connection_signal_subscribe(conn,
1500 NULL, BT_DEVICE_INTERFACE,
1501 OTC_DISCONNECTED, NULL, NULL, 0,
1502 _bt_otc_disconnected_cb,
1509 void _bt_otp_deinit_event_receiver(void)
1513 g_dbus_connection_signal_unsubscribe(conn, property_sub_id);
1514 g_dbus_connection_signal_unsubscribe(conn, adapter_sub_id);
1515 g_dbus_connection_signal_unsubscribe(conn, device_sub_id);
1521 static void _bt_otp_sig_handler(int sig)
1526 BT_DBG("caught signal - sigterm\n");
1529 BT_DBG("caught signal - sigint\n");
1532 BT_DBG("caught signal - sigkill\n");
1535 BT_DBG("caught signal %d and ignored\n", sig);
1541 /* OTP Service Main loop */
1544 struct sigaction sa;
1545 BT_ERR("Starting the bt-otp daemon");
1547 memset(&sa, 0, sizeof(sa));
1548 sa.sa_handler = _bt_otp_sig_handler;
1549 sa.sa_flags = SA_SIGINFO;
1550 sigaction(SIGINT, &sa, NULL);
1551 sigaction(SIGTERM, &sa, NULL);
1552 sigaction(SIGKILL, &sa, NULL);
1554 if (_bt_otp_register_interface() != BLUETOOTH_ERROR_NONE) {
1555 BT_ERR("Fail to register otp service");
1559 if (_bt_otp_init_event_receiver() != BLUETOOTH_ERROR_NONE) {
1560 BT_ERR("Fail to init event reciever");
1564 main_loop = g_main_loop_new(NULL, FALSE);
1566 g_main_loop_run(main_loop);
1568 BT_DBG("g_main_loop_quit called!");
1570 if (main_loop != NULL) {
1571 g_main_loop_unref(main_loop);