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];
494 char file_path[BT_FILE_PATH_MAX_LEN] = {0, };
497 if (!selected_object) {
498 BT_DBG("Object not selected");
502 snprintf(file_path, sizeof(file_path), "%s%s",
503 directory, selected_object->name);
504 BT_DBG("file_path = [%s]", file_path);
506 fp = fopen(file_path, "r");
508 BT_DBG("fopen() failed : %s", strerror(errno));
512 BT_DBG("length [%d]", oacp_read->length);
513 length = oacp_read->length;
514 if (length > BT_L2CAP_BUFFER_LEN) {
515 int offset = oacp_read->offset;
520 if (length < BT_L2CAP_BUFFER_LEN)
523 len = BT_L2CAP_BUFFER_LEN;
525 fseek(fp, offset, SEEK_SET);
526 read = fread(buf, 1, len, fp);
528 written = write(oacp_read->fd, buf, len);
529 BT_DBG("read [%d], Written [%d], len [%d], offset [%d], length [%d], written_len [%d]",
530 read, written, len, offset, length, written_len);
536 read = fread(buf, 1, length, fp);
537 written = write(oacp_read->fd, buf, oacp_read->length);
538 BT_DBG("read [%d], Written [%d]", read, written);
543 g_free(oacp_read->remote_address);
548 static void _bt_otp_method(GDBusConnection *connection,
550 const gchar *object_path,
551 const gchar *interface_name,
552 const gchar *method_name,
553 GVariant *parameters,
554 GDBusMethodInvocation *invocation,
558 int status = BLUETOOTH_ERROR_NONE;
560 BT_DBG("Method[%s] Object Path[%s] Interface Name[%s]",
561 method_name, object_path, interface_name);
563 if (g_strcmp0(method_name, "enable") == 0) {
565 GError *error = NULL;
566 const gchar *filename = NULL;
567 char absolute_path[ABSOLUTE_PATH_MAX_LENGTH];
568 GSList *list = NULL, *l = NULL;
570 struct object_metadata *object = NULL;
572 g_variant_get(parameters, "(s)", &directory);
573 BT_DBG("Directory = [%s]", directory);
575 dir = g_dir_open(directory, 0, &error);
577 BT_ERR("Failed to open directory: %s", error->message);
579 status = BLUETOOTH_ERROR_INVALID_DIRECTORY;
583 while ((filename = g_dir_read_name(dir))) {
584 list = g_slist_append(list, (gpointer) filename);
590 BT_DBG("No object found in given directory");
591 status = BLUETOOTH_ERROR_NO_OBJECTS_FOUND;
595 if (g_slist_length(list) > 1)
596 mutiple_obj_support = true;
598 for (l = list; l != NULL; l = l->next) {
599 if (!l->data) continue;
600 snprintf(absolute_path, sizeof(absolute_path), "%s%s", directory,
603 BT_INFO("filename: %s, absoulte_path: %s",
604 (char *)l->data, absolute_path);
606 if (stat(absolute_path, &st) == -1) {
607 BT_INFO("stat failed: (%d)\n", errno);
611 object = g_new0(struct object_metadata, 1);
613 object->name = g_strdup((const gchar *)l->data);
614 object->type = _otp_convert_uuid_to_uuid128(UNSUPPORTED_OBJECT_TYPE_UUID);
615 object->first_created = st.st_ctime;
616 object->last_modified = st.st_ctime;
617 object->curr_size = (uint32_t) st.st_size;
618 object->alloc_size = (uint32_t) st.st_size;
619 object->id = object_id;
620 object->props = OBJECT_READ;
622 otp_object_list = g_slist_append(otp_object_list,
629 if (_bt_otp_prepare_ots() != BLUETOOTH_ERROR_NONE) {
630 BT_ERR("Fail to prepare OTP Proxy");
631 status = BLUETOOTH_ERROR_INTERNAL;
635 /* If single object is supported, make that as
636 * selected object and update the metadata for the same.
638 if (!mutiple_obj_support) {
639 BT_INFO("Server supports single object");
640 selected_object = (struct object_metadata *) g_slist_nth_data(otp_object_list, 0);
642 update_obj_metadata_charc_value(selected_object);
645 BT_DBG("advertsing");
646 if (_bt_otp_set_advertising_data() != BLUETOOTH_ERROR_NONE) {
647 BT_ERR("Fail to set advertising data");
648 status = BLUETOOTH_ERROR_INTERNAL;
652 g_dbus_method_invocation_return_value(invocation,
653 g_variant_new("(i)", status));
655 } else if (g_strcmp0(method_name, "disable") == 0) {
656 g_dbus_method_invocation_return_value(invocation,
657 g_variant_new("(i)", status));
660 } else if (g_strcmp0(method_name, "NewConnection") == 0) {
663 GUnixFDList *fd_list;
665 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
668 g_variant_get(parameters, "(oh)", &dev_path, &index);
670 msg = g_dbus_method_invocation_get_message(invocation);
671 fd_list = g_dbus_message_get_unix_fd_list(msg);
672 if (fd_list == NULL) {
673 BT_ERR("fd_list is NULL");
677 fd = g_unix_fd_list_get(fd_list, index, NULL);
679 BT_ERR("Invalid fd return");
683 _bt_convert_device_path_to_address(dev_path, address);
685 BT_INFO("OTC Connected fd: %d, address %s", fd, address);
687 /* OTC Connected, but no on going request */
691 otc_connection_status = TRUE;
693 if (oacp_read->opcode == OACP_READ)
694 _bt_otp_start_write_on_fd();
696 g_dbus_method_invocation_return_value(invocation, NULL);
701 static const GDBusInterfaceVTable otp_method_table = {
707 static void _bt_otp_on_bus_acquired(GDBusConnection *connection,
708 const gchar *name, gpointer user_data)
711 GError *error = NULL;
717 object_id = g_dbus_connection_register_object(connection,
719 otp_node_info->interfaces[0],
722 if (object_id == 0) {
723 BT_ERR("Failed to register method table: %s", error->message);
725 g_dbus_node_info_unref(otp_node_info);
731 static void _bt_otp_on_name_acquired(GDBusConnection *connection,
738 static void _bt_otp_on_name_lost(GDBusConnection *connection,
743 g_object_unref(g_conn);
745 g_dbus_node_info_unref(otp_node_info);
746 g_bus_unown_name(g_owner_id);
749 int _bt_otp_register_interface(void)
752 GError *error = NULL;
755 otp_node_info = g_dbus_node_info_new_for_xml(otp_introspection_xml, &error);
756 if (!otp_node_info) {
757 BT_ERR("Failed to install: %s", error->message);
758 return BLUETOOTH_ERROR_INTERNAL;
761 owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
763 G_BUS_NAME_OWNER_FLAGS_NONE,
764 _bt_otp_on_bus_acquired,
765 _bt_otp_on_name_acquired,
766 _bt_otp_on_name_lost,
768 g_owner_id = owner_id;
769 BT_DBG("owner_id is [%d]\n", owner_id);
772 return BLUETOOTH_ERROR_NONE;
775 void _bt_otp_unregister_interface(void)
779 g_object_unref(g_conn);
781 g_dbus_node_info_unref(otp_node_info);
782 g_bus_unown_name(g_owner_id);
788 void _bt_convert_device_path_to_address(const char *device_path,
789 char *device_address)
791 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
794 dev_addr = strstr(device_path, "dev_");
795 if (dev_addr != NULL) {
798 g_strlcpy(address, dev_addr, sizeof(address));
800 while ((pos = strchr(address, '_')) != NULL)
803 g_strlcpy(device_address, address, BT_ADDRESS_STRING_SIZE);
807 static char *__bt_extract_device_path(GVariantIter *iter, char *address)
809 char *object_path = NULL;
810 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
812 /* Parse the signature: oa{sa{sv}}} */
813 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path,
816 BT_ERR("Unable to get object path");
819 _bt_convert_device_path_to_address(object_path, device_address);
820 if (g_strcmp0(address, device_address) == 0)
821 return g_strdup(object_path);
825 BT_ERR("Unable to get object path");
829 char *_bt_otp_get_device_object_path(char *address)
832 GDBusProxy *proxy = NULL;
833 GVariant *result = NULL;
834 GVariantIter *iter = NULL;
835 char *object_path = NULL;
837 proxy = g_dbus_proxy_new_sync(conn,
838 G_DBUS_PROXY_FLAGS_NONE, NULL,
841 BT_MANAGER_INTERFACE,
845 BT_ERR("Unable to create proxy: %s", err->message);
849 result = g_dbus_proxy_call_sync(proxy, "GetManagedObjects", NULL,
850 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
853 BT_ERR("Fail to get GetManagedObjects (Error: %s)", err->message);
855 BT_ERR("Fail to get GetManagedObjects");
860 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
861 object_path = __bt_extract_device_path(iter, address);
863 g_variant_unref(result);
864 g_variant_iter_free(iter);
871 g_object_unref(proxy);
876 int _bt_otp_open_otc_and_listen(char *address)
879 GDBusProxy *device_proxy = NULL;
880 GVariant *result = NULL;
881 GError *error = NULL;
882 int ret = BLUETOOTH_ERROR_NONE;
884 object_path = _bt_otp_get_device_object_path(address);
885 if (object_path == NULL) {
886 ret = BLUETOOTH_ERROR_NOT_PAIRED;
890 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
891 NULL, BT_BLUEZ_NAME, object_path,
892 BT_DEVICE_INTERFACE, NULL, NULL);
893 if (device_proxy == NULL) {
894 ret = BLUETOOTH_ERROR_INTERNAL;
899 result = g_dbus_proxy_call_sync(device_proxy, "ListenOtc",
901 G_DBUS_CALL_FLAGS_NONE,
905 if (result == NULL) {
907 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
910 ret = BLUETOOTH_ERROR_INTERNAL;
916 g_variant_unref(result);
918 g_object_unref(device_proxy);
922 int _bt_otp_oacp_write_cb(char *value, int len, int offset,
923 char *remote_addr, struct indicate_info *info)
925 int ret = OACP_SUCCESS;
926 int app_err = BLUETOOTH_ERROR_NONE;
927 int opcode = value[0];
928 uint32_t object_offset, length;
930 BT_INFO("OACP Opcode 0x%d", opcode);
932 if (!otp_object_list) {
933 ret = OACP_INVALID_OBJ;
939 ret = OACP_OPCODE_NOT_SUPPORTED;
942 ret = OACP_OPCODE_NOT_SUPPORTED;
944 case OACP_CALC_CHECKSUM:
945 ret = OACP_OPCODE_NOT_SUPPORTED;
948 ret = OACP_OPCODE_NOT_SUPPORTED;
951 object_offset = (uint32_t)(value[4] & 0xFF) << 24 |
952 (uint32_t)(value[3] & 0xFF) << 16 |
953 (uint32_t)(value[2] & 0xFF) << 8 |
954 (uint32_t)(value[1] & 0xFF);
955 length = (uint32_t)(value[8] & 0xFF) << 24 |
956 (uint32_t)(value[7] & 0xFF) << 16 |
957 (uint32_t)(value[6] & 0xFF) << 8 |
958 (uint32_t)(value[5] & 0xFF);
960 BT_INFO("Offset = %lu, Length = %lu", object_offset, length);
962 if (oacp_read && otc_connection_status) {
963 /* Read operation already going on. */
964 ret = OACP_OBJECT_LOCKED;
967 oacp_read = g_malloc0(sizeof(struct oacp_operation));
968 oacp_read->offset = object_offset;
969 oacp_read->length = length;
970 oacp_read->remote_address = g_strdup(remote_addr);
971 oacp_read->opcode = OACP_READ;
973 app_err = _bt_otp_open_otc_and_listen(remote_addr);
974 if (app_err != BLUETOOTH_ERROR_NONE) {
975 ret = OACP_OPERATION_FAILED;
976 g_free(oacp_read->remote_address);
984 ret = OACP_OPCODE_NOT_SUPPORTED;
987 ret = OACP_OPCODE_NOT_SUPPORTED;
990 ret = OACP_OPCODE_NOT_SUPPORTED;
994 info->resp_opcode = OACP_RESPONSE;
995 info->req_opcode = opcode;
996 info->result_code = ret;
997 info->resp_param = NULL;
1001 void convert_to_hex(struct object_metadata *object, char *type, char *value)
1005 BT_DBG("type : %s", type);
1007 memset(value, 0, 8);
1009 if (!g_strcmp0(type, "size")) {
1011 value[3] = (object->curr_size >> 24) & 0xFF;
1012 value[2] = (object->curr_size >> 16) & 0xFF;
1013 value[1] = (object->curr_size >> 8) & 0xFF;
1014 value[0] = object->curr_size & 0xFF;
1016 value[7] = (object->alloc_size >> 24) & 0xFF;
1017 value[6] = (object->alloc_size >> 16) & 0xFF;
1018 value[5] = (object->alloc_size >> 8) & 0xFF;
1019 value[4] = object->alloc_size & 0xFF;
1021 } else if (!g_strcmp0(type, "date")) {
1023 localtime_r(&(object->first_created), &fc_tm);
1025 value[1] = ((fc_tm.tm_year+1900) >> 8) & 0xFF;
1026 value[0] = (fc_tm.tm_year+1900) & 0xFF;
1027 value[2] = (fc_tm.tm_mon+1) & 0xFF;
1028 value[3] = fc_tm.tm_mday & 0xFF;
1029 value[4] = fc_tm.tm_hour & 0xFF;
1030 value[5] = fc_tm.tm_min & 0xFF;
1031 value[6] = fc_tm.tm_sec & 0xFF;
1033 } else if (!g_strcmp0(type, "id")) {
1035 value[5] = (object->id >> 48) & 0xFF;
1036 value[4] = (object->id >> 32) & 0xFF;
1037 value[3] = (object->id >> 24) & 0xFF;
1038 value[2] = (object->id >> 16) & 0xFF;
1039 value[1] = (object->id >> 8) & 0xFF;
1040 value[0] = object->id & 0xFF;
1042 } else if (!g_strcmp0(type, "props")) {
1043 value[3] = (object->props >> 24) & 0xFF;
1044 value[2] = (object->props >> 16) & 0xFF;
1045 value[1] = (object->props >> 8) & 0xFF;
1046 value[0] = object->props & 0xFF;
1050 void update_obj_metadata_charc_value(struct object_metadata *object)
1052 /* Value can be of maximum eight bytes */
1055 _bt_otp_set_char_value(otp_object_name_obj_path, object->name,
1056 strlen(object->name));
1057 _bt_otp_set_char_value(otp_object_type_obj_path, object->type,
1058 strlen(object->type));
1060 convert_to_hex(object, "size", value);
1061 _bt_otp_set_char_value(otp_object_size_obj_path, value, 8);
1063 convert_to_hex(object, "date", value);
1064 _bt_otp_set_char_value(otp_object_first_created_obj_path, value, 7);
1065 _bt_otp_set_char_value(otp_object_last_modified_obj_path, value, 7);
1067 /* Object ID is optonal for single object server */
1068 if (mutiple_obj_support) {
1069 convert_to_hex(object, "id", value);
1070 _bt_otp_set_char_value(otp_object_id_obj_path, value, 6);
1073 convert_to_hex(object, "props", value);
1074 _bt_otp_set_char_value(otp_object_prop_obj_path, value, 4);
1077 int _bt_otp_olcp_write_cb(char *value, int len, int offset,
1078 struct indicate_info *info)
1080 int ret = OLCP_SUCCESS;
1081 int opcode = value[0];
1082 struct object_metadata *object;
1084 BT_INFO("OLCP Opcode 0x%d", opcode);
1086 if (!otp_object_list) {
1093 object = (struct object_metadata *) g_slist_nth_data(otp_object_list, 0);
1095 ret = OLCP_OUT_OF_BOUNDS;
1098 update_obj_metadata_charc_value(object);
1099 selected_object = object;
1103 len = g_slist_length(otp_object_list);
1104 object = (struct object_metadata *) g_slist_nth_data(otp_object_list, len-1);
1106 ret = OLCP_OUT_OF_BOUNDS;
1109 update_obj_metadata_charc_value(object);
1110 selected_object = object;
1111 obj_curr_index = len-1;
1113 case OLCP_PREVIOUS: {
1114 if (obj_curr_index == 0) {
1115 ret = OLCP_OUT_OF_BOUNDS;
1118 object = (struct object_metadata *) g_slist_nth_data(otp_object_list, obj_curr_index-1);
1120 ret = OLCP_OUT_OF_BOUNDS;
1123 update_obj_metadata_charc_value(object);
1124 selected_object = object;
1125 obj_curr_index -= 1;
1128 object = (struct object_metadata *) g_slist_nth_data(otp_object_list, obj_curr_index+1);
1130 ret = OLCP_OUT_OF_BOUNDS;
1133 update_obj_metadata_charc_value(object);
1134 selected_object = object;
1135 obj_curr_index += 1;
1139 case OLCP_REQ_NO_OBJ:
1140 case OLCP_CLEAR_MARKING:
1142 ret = OLCP_OPCODE_NOT_SUPPORTED;
1146 info->resp_opcode = OLCP_RESPONSE;
1147 info->req_opcode = opcode;
1148 info->result_code = ret;
1149 info->resp_param = NULL;
1150 return BLUETOOTH_ERROR_NONE;
1153 static struct otp_char_info *otp_get_char_value(const char *path)
1157 for (tmp = otp_char_list; tmp != NULL; tmp = tmp->next) {
1159 struct otp_char_info *char_info = tmp->data;
1160 if (!g_strcmp0(char_info->char_path, path))
1168 int _bt_otp_read_cb(const char *obj_path, char **value, int *len)
1170 struct otp_char_info *info = NULL;
1173 BT_ERR("Wrong Obj path");
1174 return BLUETOOTH_ERROR_INTERNAL;
1177 if (g_strcmp0(obj_path, otp_feature_obj_path)) {
1178 if (!selected_object) {
1179 return BLUETOOTH_ERROR_OBJECT_NOT_SELECTED;
1183 info = otp_get_char_value(obj_path);
1185 if (info->char_value == NULL || info->value_length == 0)
1186 return BLUETOOTH_ERROR_INTERNAL;
1188 *len = info->value_length;
1189 *value = (char *)malloc(sizeof(char)*(*len));
1190 memcpy(*value, info->char_value, *len);
1192 return BLUETOOTH_ERROR_NONE;
1194 return BLUETOOTH_ERROR_INTERNAL;
1198 static void _otp_convert_address_to_hex(bluetooth_device_address_t *addr_hex,
1199 const char *addr_str)
1202 unsigned int addr[BLUETOOTH_ADDRESS_LENGTH] = { 0, };
1204 if (addr_str == NULL || addr_str[0] == '\0')
1207 i = sscanf(addr_str, "%X:%X:%X:%X:%X:%X", &addr[0], &addr[1],
1208 &addr[2], &addr[3], &addr[4], &addr[5]);
1209 if (i != BLUETOOTH_ADDRESS_LENGTH)
1210 BT_ERR("Invalid format string - [%s]", addr_str);
1212 for (i = 0; i < BLUETOOTH_ADDRESS_LENGTH; i++)
1213 addr_hex->addr[i] = (unsigned char)addr[i];
1216 static void _bt_otp_send_indication(const char *obj_path,
1217 struct indicate_info *info,
1218 bluetooth_device_address_t *remote_address)
1220 int ret = BLUETOOTH_ERROR_NONE;
1221 char value[7] = {0x00};
1225 value[0] = info->resp_opcode & 0xFF;
1226 value[1] = info->req_opcode & 0xFF;
1227 value[2] = info->result_code & 0xFF;
1228 if (info->resp_param) {
1229 value[6] = (info->resp_param[3] >> 24) & 0xFF;
1230 value[5] = (info->resp_param[4] >> 16) & 0xFF;
1231 value[4] = (info->resp_param[5] >> 8) & 0xFF;
1232 value[3] = info->resp_param[6] & 0xFF;
1235 BT_DBG("Opcode: %d", value[1]);
1237 /* Store the status value */
1238 _bt_otp_set_char_value(obj_path, value, 7);
1240 /* Send indication */
1241 ret = bluetooth_gatt_server_set_notification(obj_path, remote_address);
1242 if (ret != BLUETOOTH_ERROR_NONE) {
1243 BT_ERR("_bt_otp_send_control_point_indication failed");
1246 ret = bluetooth_gatt_update_characteristic(obj_path, value, 7);
1247 if (ret != BLUETOOTH_ERROR_NONE) {
1248 BT_ERR("_bt_otp_send_control_point_indication failed");
1253 void _bt_otp_gatt_char_property_changed_event(GVariant *msg,
1256 int result = BLUETOOTH_ERROR_NONE;
1257 GVariantIter value_iter;
1258 const char *property = NULL;
1259 const char *char_path = NULL;
1260 const char *svc_handle = NULL;
1261 GVariant *var = NULL;
1262 GVariant *val = NULL;
1263 g_variant_iter_init(&value_iter, msg);
1265 while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &var))) {
1267 if (property == NULL) {
1268 BT_ERR("Property NULL");
1272 if (!g_strcmp0(property, "WriteValue")) {
1274 BT_INFO("WriteValue");
1275 BT_INFO("Type '%s'\n", g_variant_get_type_string(var));
1278 bluetooth_device_address_t addr_hex = { {0,} };
1283 struct indicate_info info;
1284 g_variant_get(var, "(&s&s&syq@ay)",
1285 &char_path, &svc_handle,
1286 &addr, &req_id, &offset, &val);
1288 len = g_variant_get_size(val);
1290 BT_DBG("Len = %d, BT_ADDR = %s", len, addr);
1292 value = (char *) g_variant_get_data(val);
1293 _otp_convert_address_to_hex(&addr_hex, addr);
1296 if (!g_strcmp0(char_path, otp_oacp_obj_path)) {
1297 result = _bt_otp_oacp_write_cb(value, len, offset, addr, &info);
1298 } else if (!g_strcmp0(char_path, otp_olcp_obj_path)) {
1299 result = _bt_otp_olcp_write_cb(value, len, offset, &info);
1301 BT_ERR("Wrong Object Path %s", char_path);
1302 result = BLUETOOTH_ERROR_INTERNAL;
1304 bluetooth_gatt_send_response(req_id,
1305 BLUETOOTH_GATT_ATT_REQUEST_TYPE_WRITE,
1306 result, 0, NULL, 0);
1308 /* Send indication for CPs */
1309 if (!g_strcmp0(char_path, otp_oacp_obj_path)) {
1310 if (OACP_indicate) {
1311 _bt_otp_send_indication(char_path, &info, &addr_hex);
1313 } else if (!g_strcmp0(char_path, otp_olcp_obj_path)) {
1314 if (OLCP_indicate) {
1315 _bt_otp_send_indication(char_path, &info, &addr_hex);
1319 BT_ERR("Array Len 0");
1322 BT_ERR("var==NULL");
1324 } else if (!g_strcmp0(property, "ReadValue")) {
1330 result = BLUETOOTH_ERROR_NONE;
1332 BT_INFO("ReadValue");
1333 BT_INFO("Type '%s'\n", g_variant_get_type_string(var));
1335 g_variant_get(var, "(&s&s&syq)", &char_path,
1336 &svc_handle, &addr, &req_id, &offset);
1338 result = _bt_otp_read_cb(char_path, &value, &len);
1340 if (result != BLUETOOTH_ERROR_NONE) {
1341 BT_ERR("ReadValue failed %s", char_path);
1342 bluetooth_gatt_send_response(req_id,
1343 BLUETOOTH_GATT_ATT_REQUEST_TYPE_READ,
1344 result, offset, NULL, 0);
1346 bluetooth_gatt_send_response(req_id,
1347 BLUETOOTH_GATT_ATT_REQUEST_TYPE_READ,
1348 result, offset, value, len);
1352 } else if (!g_strcmp0(property, "NotificationStateChanged")) {
1353 gboolean indicate = FALSE;
1355 g_variant_get(var, "(&s&sb)", &char_path,
1356 &svc_handle, &indicate);
1358 BT_INFO("%s : [%s]", property,
1359 indicate ? "StartNotify" : "StopNotify");
1360 BT_INFO("Type '%s'\n", g_variant_get_type_string(var));
1362 if (!g_strcmp0(char_path, otp_oacp_obj_path)) {
1363 OACP_indicate = indicate;
1364 } else if (!g_strcmp0(char_path, otp_olcp_obj_path)) {
1365 OLCP_indicate = indicate;
1372 void _bt_otp_property_event_filter(GDBusConnection *connection,
1373 const gchar *sender_name,
1374 const gchar *object_path,
1375 const gchar *interface_name,
1376 const gchar *signal_name,
1377 GVariant *parameters,
1382 if (signal_name == NULL) {
1383 BT_ERR("Wrong Signal");
1387 if (g_strcmp0(signal_name, PROPERTIES_CHANGED) == 0) {
1389 g_variant_get(parameters, "(@a{sv}@as)", &value, NULL);
1390 _bt_otp_gatt_char_property_changed_event(value, object_path);
1394 void _bt_otp_adapter_event_filter(GDBusConnection *connection,
1395 const gchar *sender_name,
1396 const gchar *object_path,
1397 const gchar *interface_name,
1398 const gchar *signal_name,
1399 GVariant *parameters,
1402 if (signal_name == NULL) {
1403 BT_ERR("Wrong Signal");
1407 BT_INFO("Interface %s, Signal %s", interface_name, signal_name);
1409 if (g_strcmp0(interface_name, BT_OTP_INTERFACE_NAME) == 0) {
1410 if (strcasecmp(signal_name, BLE_DISABLED) == 0) {
1416 void _bt_otc_disconnected_cb(GDBusConnection *connection,
1417 const gchar *sender_name,
1418 const gchar *object_path,
1419 const gchar *interface_name,
1420 const gchar *signal_name,
1421 GVariant *parameters,
1424 if (signal_name == NULL) {
1425 BT_ERR("Wrong Signal");
1429 BT_INFO("Interface %s, Signal %s", interface_name, signal_name);
1431 if (g_strcmp0(interface_name, BT_DEVICE_INTERFACE) == 0) {
1432 if (strcasecmp(signal_name, OTC_DISCONNECTED) == 0) {
1433 BT_DBG("OTC Channel Disconnected dev_path[%s]",
1435 otc_connection_status = FALSE;
1437 g_free(oacp_read->remote_address);
1444 int _bt_otp_init_event_receiver()
1447 GError *error = NULL;
1450 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
1451 if (error != NULL) {
1452 BT_ERR("ERROR: Can't get on system bus [%s]",
1454 g_clear_error(&error);
1458 property_sub_id = g_dbus_connection_signal_subscribe(conn,
1460 BT_OTP_INTERFACE_NAME,
1462 BT_OTP_OBJECT_PATH, NULL, 0,
1463 _bt_otp_property_event_filter,
1466 adapter_sub_id = g_dbus_connection_signal_subscribe(conn,
1468 BT_OTP_INTERFACE_NAME,
1470 BT_OTP_OBJECT_PATH, NULL, 0,
1471 _bt_otp_adapter_event_filter,
1474 device_sub_id = g_dbus_connection_signal_subscribe(conn,
1475 NULL, BT_DEVICE_INTERFACE,
1476 OTC_DISCONNECTED, NULL, NULL, 0,
1477 _bt_otc_disconnected_cb,
1484 void _bt_otp_deinit_event_receiver(void)
1488 g_dbus_connection_signal_unsubscribe(conn, property_sub_id);
1489 g_dbus_connection_signal_unsubscribe(conn, adapter_sub_id);
1490 g_dbus_connection_signal_unsubscribe(conn, device_sub_id);
1496 static void _bt_otp_sig_handler(int sig)
1501 BT_DBG("caught signal - sigterm\n");
1504 BT_DBG("caught signal - sigint\n");
1507 BT_DBG("caught signal - sigkill\n");
1510 BT_DBG("caught signal %d and ignored\n", sig);
1516 /* OTP Service Main loop */
1519 struct sigaction sa;
1520 BT_ERR("Starting the bt-otp daemon");
1522 memset(&sa, 0, sizeof(sa));
1523 sa.sa_handler = _bt_otp_sig_handler;
1524 sa.sa_flags = SA_SIGINFO;
1525 sigaction(SIGINT, &sa, NULL);
1526 sigaction(SIGTERM, &sa, NULL);
1527 sigaction(SIGKILL, &sa, NULL);
1529 if (_bt_otp_register_interface() != BLUETOOTH_ERROR_NONE) {
1530 BT_ERR("Fail to register otp service");
1534 if (_bt_otp_init_event_receiver() != BLUETOOTH_ERROR_NONE) {
1535 BT_ERR("Fail to init event reciever");
1539 main_loop = g_main_loop_new(NULL, FALSE);
1541 g_main_loop_run(main_loop);
1543 BT_DBG("g_main_loop_quit called!");
1545 if (main_loop != NULL) {
1546 g_main_loop_unref(main_loop);