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.
30 #include "bt-otpserver.h"
31 #include "bluetooth-api.h"
35 #define LOG_TAG "BLUETOOTH_OTP"
37 #define BT_INFO(fmt, arg...) SLOGI(fmt, ##arg)
38 #define BT_ERR(fmt, arg...) SLOGE(fmt, ##arg)
39 #define BT_DBG(fmt, arg...) SLOGD(fmt, ##arg)
41 /* OTP object paths */
42 char *otp_obj_path = NULL;
43 char *otp_feature_obj_path = NULL;
44 char *otp_object_name_obj_path = NULL;
45 char *otp_object_type_obj_path = NULL;
46 char *otp_object_size_obj_path = NULL;
47 char *otp_object_first_created_obj_path = NULL;
48 char *otp_object_last_modified_obj_path = NULL;
49 char *otp_object_id_obj_path = NULL;
50 char *otp_object_prop_obj_path = NULL;
51 char *otp_oacp_obj_path = NULL;
52 char *otp_olcp_obj_path = NULL;
53 char *otp_oacp_desc_obj_path = NULL;
54 char *otp_olcp_desc_obj_path = NULL;
56 static GMainLoop *main_loop;
57 GDBusNodeInfo *otp_node_info = NULL;
58 static GDBusConnection *conn;
59 static GDBusConnection *g_conn;
61 static int property_sub_id = -1;
62 static int adapter_sub_id = -1;
63 static guint g_owner_id = 0;
65 struct otp_char_info {
71 struct indicate_info {
79 struct object_metadata {
90 static struct object_metadata *selected_object;
91 static uint64_t object_id = OBJECT_START_ID;
92 static GSList *otp_object_list = NULL;
93 static GSList *otp_char_list = NULL;
94 static guint obj_curr_index;
95 static int adv_handle = 0;
96 static gboolean OLCP_indicate = FALSE;
97 char *directory = NULL;
99 static const gchar otp_introspection_xml[] =
101 " <interface name='org.projectx.otp_service'>"
102 " <method name='enable'>"
103 " <arg type='s' name='directory'/>"
104 " <arg type='i' name='status' direction='out'/>"
106 " <method name='disable'>"
107 " <arg type='i' name='status' direction='out'/>"
112 void _bt_otp_deinit_event_receiver(void);
113 void _bt_otp_unregister_interface(void);
115 static void delete_all_objects(void)
118 for (tmp = otp_object_list; tmp != NULL; tmp = tmp->next) {
120 struct object_metadata *obj_info = tmp->data;
122 g_free(obj_info->name);
124 g_free(obj_info->type);
125 otp_object_list = g_slist_delete_link(otp_object_list, tmp->data);
128 g_slist_free(otp_object_list);
129 otp_object_list = NULL;
132 static void delete_all_characterisitc(void)
135 for (tmp = otp_char_list; tmp != NULL; tmp = tmp->next) {
137 struct otp_char_info *char_info = tmp->data;
138 if (char_info->char_path)
139 g_free(char_info->char_path);
140 if (char_info->char_value)
141 g_free(char_info->char_value);
142 otp_char_list = g_slist_delete_link(otp_char_list, tmp->data);
145 g_slist_free(otp_char_list);
146 otp_char_list = NULL;
149 void _bt_otp_exit(void)
155 delete_all_characterisitc();
158 delete_all_objects();
160 ret = bluetooth_gatt_deinit();
161 if (ret != BLUETOOTH_ERROR_NONE)
162 BT_ERR("Failed to Deinit GATT %d", ret);
164 _bt_otp_deinit_event_receiver();
166 _bt_otp_unregister_interface();
168 /* TODO: Advertising is not getting stopped by this API.
169 * This is because OTP_SERVER_DEINIT dbus call is blocking
170 * BT_SET_ADVERTISING_DATA dbus call. But now advertisment
171 * is stopped because of terminated process logic.
173 ret = bluetooth_set_advertising(adv_handle, FALSE);
174 if (ret != BLUETOOTH_ERROR_NONE)
175 BT_ERR("Failed to stop ADV %d", ret);
177 if (main_loop != NULL) {
178 g_main_loop_quit(main_loop);
182 static void _bt_otp_set_char_value(const char *obj_path,
183 const char *value, int value_length)
189 for (tmp = otp_char_list; tmp != NULL; tmp = tmp->next) {
191 struct otp_char_info *char_info = tmp->data;
192 if (!g_strcmp0(char_info->char_path, obj_path)) {
193 char_info->char_value = g_try_realloc(char_info->char_value, value_length);
194 if (char_info->char_value) {
195 memcpy(char_info->char_value, value, value_length);
196 char_info->value_length = value_length;
205 int add_new_characteristic(const char *char_uuid, bt_gatt_permission_t perms,
206 bt_gatt_characteristic_property_t props, char **obj_path)
208 int ret = BLUETOOTH_ERROR_NONE;
209 struct otp_char_info *char_info = NULL;
211 ret = bluetooth_gatt_add_new_characteristic(otp_obj_path,
212 char_uuid, perms, props, obj_path);
213 if (ret != BLUETOOTH_ERROR_NONE) {
214 BT_ERR("Failed to add new char %d", ret);
218 char_info = g_new0(struct otp_char_info, 1);
219 char_info->char_path = g_strdup(*obj_path);
220 otp_char_list = g_slist_append(otp_char_list, char_info);
225 static char *_otp_convert_uuid_to_uuid128(const char *uuid)
235 uuid128 = g_strdup_printf("0000%s-0000-1000-8000-00805f9b34fb",
241 uuid128 = g_strdup_printf("%s-0000-1000-8000-00805f9b34fb",
247 uuid128 = strdup(uuid);
257 int _bt_otp_prepare_ots(void)
260 int ret = BLUETOOTH_ERROR_NONE;
264 bt_gatt_characteristic_property_t props;
265 bt_gatt_permission_t perms;
266 char supp_feat[OTP_FEATURE_LENGTH] = { 0x08, 0x00, 0x00, 0x00,
267 0x80, 0x00, 0x00, 0x00 };
269 ret = bluetooth_gatt_init();
270 if (ret != BLUETOOTH_ERROR_NONE) {
271 BT_ERR("Failed to Init GATT %d", ret);
275 service_uuid = _otp_convert_uuid_to_uuid128(OTP_UUID);
276 ret = bluetooth_gatt_add_service(service_uuid, &otp_obj_path);
277 if (ret != BLUETOOTH_ERROR_NONE) {
278 BT_ERR("Failed to add service %d", ret);
282 /* Characteristic OTP Feature */
283 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ;
284 perms = BLUETOOTH_GATT_PERMISSION_READ;
285 char_uuid = _otp_convert_uuid_to_uuid128(OTP_FEATURE_UUID);
286 ret = add_new_characteristic(char_uuid, perms, props,
287 &otp_feature_obj_path);
288 if (ret != BLUETOOTH_ERROR_NONE)
291 ret = bluetooth_gatt_set_characteristic_value(otp_feature_obj_path,
292 supp_feat, OTP_FEATURE_LENGTH);
293 if (ret != BLUETOOTH_ERROR_NONE) {
294 BT_ERR("Failed to set char value %d", ret);
298 _bt_otp_set_char_value(otp_feature_obj_path, supp_feat,
301 /* Characteristic Object Name */
302 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ;
303 perms = BLUETOOTH_GATT_PERMISSION_READ;
304 char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_NAME_UUID);
305 ret = add_new_characteristic(char_uuid, perms, props,
306 &otp_object_name_obj_path);
307 if (ret != BLUETOOTH_ERROR_NONE)
310 /* Characteristic Object Type */
311 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ;
312 perms = BLUETOOTH_GATT_PERMISSION_READ;
313 char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_TYPE_UUID);
314 ret = add_new_characteristic(char_uuid, perms, props,
315 &otp_object_type_obj_path);
316 if (ret != BLUETOOTH_ERROR_NONE)
319 /* Characteristic Object Size */
320 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ;
321 perms = BLUETOOTH_GATT_PERMISSION_READ;
322 char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_SIZE_UUID);
323 ret = add_new_characteristic(char_uuid, perms, props,
324 &otp_object_size_obj_path);
325 if (ret != BLUETOOTH_ERROR_NONE)
328 /* Characteristic Object First-Created */
329 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ |
330 BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE;
331 perms = BLUETOOTH_GATT_PERMISSION_READ |
332 BLUETOOTH_GATT_PERMISSION_WRITE;
333 char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_FIRST_CREATED_UUID);
334 ret = add_new_characteristic(char_uuid, perms, props,
335 &otp_object_first_created_obj_path);
336 if (ret != BLUETOOTH_ERROR_NONE)
339 /* Characteristic Object Last-Modified */
340 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ |
341 BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE;
342 perms = BLUETOOTH_GATT_PERMISSION_READ |
343 BLUETOOTH_GATT_PERMISSION_WRITE;
344 char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_LAST_MODIFIED_UUID);
345 ret = add_new_characteristic(char_uuid, perms, props,
346 &otp_object_last_modified_obj_path);
347 if (ret != BLUETOOTH_ERROR_NONE)
350 /* Characteristic Object ID */
351 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ;
352 perms = BLUETOOTH_GATT_PERMISSION_READ;
353 char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_ID_UUID);
354 ret = add_new_characteristic(char_uuid, perms, props,
355 &otp_object_id_obj_path);
356 if (ret != BLUETOOTH_ERROR_NONE)
359 /* Characteristic Object Properties */
360 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ;
361 perms = BLUETOOTH_GATT_PERMISSION_READ;
362 char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_PROP_UUID);
363 ret = add_new_characteristic(char_uuid, perms, props,
364 &otp_object_prop_obj_path);
365 if (ret != BLUETOOTH_ERROR_NONE)
368 /* Characteristic OACP */
369 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE |
370 BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_INDICATE;
371 perms = BLUETOOTH_GATT_PERMISSION_WRITE;
372 char_uuid = _otp_convert_uuid_to_uuid128(OTP_OACP_UUID);
373 ret = add_new_characteristic(char_uuid, perms, props,
375 if (ret != BLUETOOTH_ERROR_NONE)
379 desc_uuid = _otp_convert_uuid_to_uuid128(OTP_CP_CCC_DESC_UUID);
380 perms = BLUETOOTH_GATT_PERMISSION_READ |
381 BLUETOOTH_GATT_PERMISSION_WRITE;
382 ret = bluetooth_gatt_add_descriptor(otp_oacp_obj_path, desc_uuid,
383 perms, &otp_oacp_desc_obj_path);
384 if (ret != BLUETOOTH_ERROR_NONE) {
385 BT_ERR("Failed to add new char descriptor %d", ret);
389 /* Characteristic OLCP */
390 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE |
391 BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_INDICATE;
392 perms = BLUETOOTH_GATT_PERMISSION_WRITE;
393 char_uuid = _otp_convert_uuid_to_uuid128(OTP_OLCP_UUID);
394 ret = add_new_characteristic(char_uuid, perms, props,
396 if (ret != BLUETOOTH_ERROR_NONE)
400 desc_uuid = _otp_convert_uuid_to_uuid128(OTP_CP_CCC_DESC_UUID);
401 perms = BLUETOOTH_GATT_PERMISSION_READ |
402 BLUETOOTH_GATT_PERMISSION_WRITE;
403 ret = bluetooth_gatt_add_descriptor(otp_olcp_obj_path, desc_uuid,
404 perms, &otp_olcp_desc_obj_path);
405 if (ret != BLUETOOTH_ERROR_NONE) {
406 BT_ERR("Failed to add new char descriptor %d", ret);
410 /* Register service */
411 ret = bluetooth_gatt_register_service(otp_obj_path);
412 if (ret != BLUETOOTH_ERROR_NONE) {
413 BT_ERR("Failed to register service %d", ret);
417 /* Register Application */
418 ret = bluetooth_gatt_register_application();
419 if (ret != BLUETOOTH_ERROR_NONE) {
420 BT_ERR("Failed to register application %d", ret);
428 delete_all_characterisitc();
432 int _bt_otp_set_advertising_data(void)
438 guint8 data[4] = {0x03, 0x02, 0x25, 0x18};
439 bluetooth_advertising_data_t adv;
441 BT_DBG("%x %x %x %x", data[0], data[1], data[2], data[3]);
442 memcpy(adv.data, data, sizeof(data));
443 ret = bluetooth_set_advertising_data(adv_handle, &adv, sizeof(data));
444 if (ret != BLUETOOTH_ERROR_NONE) {
445 BT_ERR("Failed to set ADV data %d", ret);
449 ret = bluetooth_set_advertising(adv_handle, TRUE);
450 if (ret != BLUETOOTH_ERROR_NONE) {
451 BT_ERR("Failed to set ADV %d", ret);
458 static void _bt_otp_method(GDBusConnection *connection,
460 const gchar *object_path,
461 const gchar *interface_name,
462 const gchar *method_name,
463 GVariant *parameters,
464 GDBusMethodInvocation *invocation,
468 int status = BLUETOOTH_ERROR_NONE;
470 BT_DBG("Method[%s] Object Path[%s] Interface Name[%s]",
471 method_name, object_path, interface_name);
473 if (g_strcmp0(method_name, "enable") == 0) {
475 GError *error = NULL;
476 const gchar *filename = NULL;
477 char absolute_path[ABSOLUTE_PATH_MAX_LENGTH];
478 GSList *list = NULL, *l = NULL;
480 struct object_metadata *object = NULL;
482 g_variant_get(parameters, "(s)", &directory);
483 BT_DBG("Directory = [%s]", directory);
485 dir = g_dir_open(directory, 0, &error);
487 BT_ERR("Failed to open directory: %s", error->message);
489 status = BLUETOOTH_ERROR_INVALID_DIRECTORY;
493 while ((filename = g_dir_read_name(dir))) {
494 list = g_slist_append(list, (gpointer) filename);
498 BT_DBG("No object found in given directory");
499 status = BLUETOOTH_ERROR_NO_OBJECTS_FOUND;
503 for (l = list; l != NULL; l = l->next) {
504 if (!l->data) continue;
505 snprintf(absolute_path, sizeof(absolute_path), "%s%s", directory,
508 BT_INFO("filename: %s, absoulte_path: %s",
509 (char *)l->data, absolute_path);
511 if (stat(absolute_path, &st) == -1) {
512 BT_INFO("stat failed: (%d)\n", errno);
516 object = g_new0(struct object_metadata, 1);
518 object->name = g_strdup((const gchar *)l->data);
519 object->type = _otp_convert_uuid_to_uuid128(UNSUPPORTED_OBJECT_TYPE_UUID);
520 object->first_created = st.st_ctime;
521 object->last_modified = st.st_ctime;
522 object->curr_size = (uint32_t) st.st_size;
523 object->alloc_size = (uint32_t) st.st_size;
524 object->id = object_id;
525 object->props = OBJECT_READ | OBJECT_WRITE;
527 otp_object_list = g_slist_append(otp_object_list,
536 if (_bt_otp_prepare_ots() != BLUETOOTH_ERROR_NONE) {
537 BT_ERR("Fail to prepare OTP Proxy");
538 status = BLUETOOTH_ERROR_INTERNAL;
542 BT_DBG("advertsing");
543 if (_bt_otp_set_advertising_data() != BLUETOOTH_ERROR_NONE) {
544 BT_ERR("Fail to set advertising data");
545 status = BLUETOOTH_ERROR_INTERNAL;
549 g_dbus_method_invocation_return_value(invocation,
550 g_variant_new("(i)", status));
552 } else if (g_strcmp0(method_name, "disable") == 0) {
553 g_dbus_method_invocation_return_value(invocation,
554 g_variant_new("(i)", status));
560 static const GDBusInterfaceVTable otp_method_table = {
566 static void _bt_otp_on_bus_acquired(GDBusConnection *connection,
567 const gchar *name, gpointer user_data)
570 GError *error = NULL;
576 object_id = g_dbus_connection_register_object(connection,
578 otp_node_info->interfaces[0],
581 if (object_id == 0) {
582 BT_ERR("Failed to register method table: %s", error->message);
584 g_dbus_node_info_unref(otp_node_info);
590 static void _bt_otp_on_name_acquired(GDBusConnection *connection,
597 static void _bt_otp_on_name_lost(GDBusConnection *connection,
602 g_object_unref(g_conn);
604 g_dbus_node_info_unref(otp_node_info);
605 g_bus_unown_name(g_owner_id);
608 int _bt_otp_register_interface(void)
611 GError *error = NULL;
614 otp_node_info = g_dbus_node_info_new_for_xml(otp_introspection_xml, &error);
615 if (!otp_node_info) {
616 BT_ERR("Failed to install: %s", error->message);
617 return BLUETOOTH_ERROR_INTERNAL;
620 owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
622 G_BUS_NAME_OWNER_FLAGS_NONE,
623 _bt_otp_on_bus_acquired,
624 _bt_otp_on_name_acquired,
625 _bt_otp_on_name_lost,
627 g_owner_id = owner_id;
628 BT_DBG("owner_id is [%d]\n", owner_id);
631 return BLUETOOTH_ERROR_NONE;
634 void _bt_otp_unregister_interface(void)
638 g_object_unref(g_conn);
640 g_dbus_node_info_unref(otp_node_info);
641 g_bus_unown_name(g_owner_id);
648 void convert_to_hex(struct object_metadata *object, char *type, char *value)
650 struct tm *tm = NULL;
652 BT_DBG("type : %s", type);
656 if (!g_strcmp0(type, "size")) {
658 value[3] = (object->curr_size >> 24) & 0xFF;
659 value[2] = (object->curr_size >> 16) & 0xFF;
660 value[1] = (object->curr_size >> 8) & 0xFF;
661 value[0] = object->curr_size & 0xFF;
663 value[7] = (object->alloc_size >> 24) & 0xFF;
664 value[6] = (object->alloc_size >> 16) & 0xFF;
665 value[5] = (object->alloc_size >> 8) & 0xFF;
666 value[4] = object->alloc_size & 0xFF;
668 } else if (!g_strcmp0(type, "date")) {
670 localtime_r(&(object->first_created), tm);
673 value[1] = ((tm->tm_year+1900) >> 8) & 0xFF;
674 value[0] = (tm->tm_year+1900) & 0xFF;
675 value[2] = (tm->tm_mon+1) & 0xFF;
676 value[3] = tm->tm_mday & 0xFF;
677 value[4] = tm->tm_hour & 0xFF;
678 value[5] = tm->tm_min & 0xFF;
679 value[6] = tm->tm_sec & 0xFF;
682 } else if (!g_strcmp0(type, "id")) {
684 value[5] = (object->id >> 48) & 0xFF;
685 value[4] = (object->id >> 32) & 0xFF;
686 value[3] = (object->id >> 24) & 0xFF;
687 value[2] = (object->id >> 16) & 0xFF;
688 value[1] = (object->id >> 8) & 0xFF;
689 value[0] = object->id & 0xFF;
691 } else if (!g_strcmp0(type, "props")) {
692 value[3] = (object->props >> 24) & 0xFF;
693 value[2] = (object->props >> 16) & 0xFF;
694 value[1] = (object->props >> 8) & 0xFF;
695 value[0] = object->props & 0xFF;
699 void update_obj_metadata_charc_value(struct object_metadata *object)
701 /* Value can be of maximum eight bytes */
704 _bt_otp_set_char_value(otp_object_name_obj_path, object->name,
705 strlen(object->name));
706 _bt_otp_set_char_value(otp_object_type_obj_path, object->type,
707 strlen(object->type));
709 convert_to_hex(object, "size", value);
710 _bt_otp_set_char_value(otp_object_size_obj_path, value, 8);
712 convert_to_hex(object, "date", value);
713 _bt_otp_set_char_value(otp_object_first_created_obj_path, value, 7);
714 _bt_otp_set_char_value(otp_object_last_modified_obj_path, value, 7);
716 convert_to_hex(object, "id", value);
717 _bt_otp_set_char_value(otp_object_id_obj_path, value, 6);
719 convert_to_hex(object, "props", value);
720 _bt_otp_set_char_value(otp_object_prop_obj_path, value, 4);
723 int _bt_otp_olcp_write_cb(char *value, int len, int offset,
724 struct indicate_info *info)
726 int ret = OLCP_SUCCESS;
727 int opcode = value[0];
728 struct object_metadata *object;
730 BT_INFO("OLCP Opcode 0x%d", opcode);
732 if (!otp_object_list) {
739 object = (struct object_metadata *) g_slist_nth_data(otp_object_list, 0);
741 ret = OLCP_OUT_OF_BOUNDS;
744 update_obj_metadata_charc_value(object);
745 selected_object = object;
749 len = g_slist_length(otp_object_list);
750 object = (struct object_metadata *) g_slist_nth_data(otp_object_list, len-1);
752 ret = OLCP_OUT_OF_BOUNDS;
755 update_obj_metadata_charc_value(object);
756 selected_object = object;
757 obj_curr_index = len-1;
759 case OLCP_PREVIOUS: {
760 if (obj_curr_index == 0) {
761 ret = OLCP_OUT_OF_BOUNDS;
764 object = (struct object_metadata *) g_slist_nth_data(otp_object_list, obj_curr_index-1);
766 ret = OLCP_OUT_OF_BOUNDS;
769 update_obj_metadata_charc_value(object);
770 selected_object = object;
774 object = (struct object_metadata *) g_slist_nth_data(otp_object_list, obj_curr_index+1);
776 ret = OLCP_OUT_OF_BOUNDS;
779 update_obj_metadata_charc_value(object);
780 selected_object = object;
785 case OLCP_REQ_NO_OBJ:
786 case OLCP_CLEAR_MARKING:
788 ret = OLCP_OPCODE_NOT_SUPPORTED;
792 info->resp_opcode = OLCP_RESPONSE;
793 info->req_opcode = opcode;
794 info->result_code = ret;
795 info->resp_param = NULL;
796 return BLUETOOTH_ERROR_NONE;
799 static struct otp_char_info *otp_get_char_value(const char *path)
803 for (tmp = otp_char_list; tmp != NULL; tmp = tmp->next) {
805 struct otp_char_info *char_info = tmp->data;
806 if (!g_strcmp0(char_info->char_path, path))
814 int _bt_otp_read_cb(const char *obj_path, char **value, int *len)
816 struct otp_char_info *info = NULL;
819 BT_ERR("Wrong Obj path");
820 return BLUETOOTH_ERROR_INTERNAL;
823 if (g_strcmp0(obj_path, otp_feature_obj_path)) {
824 if (!selected_object) {
825 return BLUETOOTH_ERROR_OBJECT_NOT_SELECTED;
829 info = otp_get_char_value(obj_path);
831 if (info->char_value == NULL || info->value_length == 0)
832 return BLUETOOTH_ERROR_INTERNAL;
834 *len = info->value_length;
835 *value = (char *)malloc(sizeof(char)*(*len));
836 memcpy(*value, info->char_value, *len);
838 return BLUETOOTH_ERROR_NONE;
840 return BLUETOOTH_ERROR_INTERNAL;
844 static void _otp_convert_address_to_hex(bluetooth_device_address_t *addr_hex,
845 const char *addr_str)
848 unsigned int addr[BLUETOOTH_ADDRESS_LENGTH] = { 0, };
850 if (addr_str == NULL || addr_str[0] == '\0')
853 i = sscanf(addr_str, "%X:%X:%X:%X:%X:%X", &addr[0], &addr[1],
854 &addr[2], &addr[3], &addr[4], &addr[5]);
855 if (i != BLUETOOTH_ADDRESS_LENGTH)
856 BT_ERR("Invalid format string - [%s]", addr_str);
858 for (i = 0; i < BLUETOOTH_ADDRESS_LENGTH; i++)
859 addr_hex->addr[i] = (unsigned char)addr[i];
862 static void _bt_otp_send_indication(const char *obj_path,
863 struct indicate_info *info,
864 bluetooth_device_address_t *remote_address)
866 int ret = BLUETOOTH_ERROR_NONE;
867 char value[7] = {0x00};
871 value[0] = info->resp_opcode & 0xFF;
872 value[1] = info->req_opcode & 0xFF;
873 value[2] = info->result_code & 0xFF;
874 if (info->resp_param) {
875 value[6] = (info->resp_param[3] >> 24) & 0xFF;
876 value[5] = (info->resp_param[4] >> 16) & 0xFF;
877 value[4] = (info->resp_param[5] >> 8) & 0xFF;
878 value[3] = info->resp_param[6] & 0xFF;
881 BT_DBG("Opcode: %d", value[1]);
883 /* Store the status value */
884 _bt_otp_set_char_value(obj_path, value, 7);
886 /* Send indication */
887 ret = bluetooth_gatt_server_set_notification(obj_path, remote_address);
888 if (ret != BLUETOOTH_ERROR_NONE) {
889 BT_ERR("_bt_otp_send_control_point_indication failed");
892 ret = bluetooth_gatt_update_characteristic(obj_path, value, 7);
893 if (ret != BLUETOOTH_ERROR_NONE) {
894 BT_ERR("_bt_otp_send_control_point_indication failed");
899 void _bt_otp_gatt_char_property_changed_event(GVariant *msg,
902 int result = BLUETOOTH_ERROR_NONE;
903 GVariantIter value_iter;
904 const char *property = NULL;
905 const char *char_path = NULL;
906 const char *svc_handle = NULL;
907 GVariant *var = NULL;
908 GVariant *val = NULL;
909 g_variant_iter_init(&value_iter, msg);
911 while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &var))) {
913 if (property == NULL) {
914 BT_ERR("Property NULL");
918 if (!g_strcmp0(property, "WriteValue")) {
920 BT_INFO("WriteValue");
921 BT_INFO("Type '%s'\n", g_variant_get_type_string(var));
924 bluetooth_device_address_t addr_hex = { {0,} };
929 struct indicate_info info;
930 g_variant_get(var, "(&s&s&syq@ay)",
931 &char_path, &svc_handle,
932 &addr, &req_id, &offset, &val);
934 len = g_variant_get_size(val);
936 BT_DBG("Len = %d, BT_ADDR = %s", len, addr);
938 value = (char *) g_variant_get_data(val);
939 _otp_convert_address_to_hex(&addr_hex, addr);
942 if (!g_strcmp0(char_path, otp_oacp_obj_path)) {
943 /* TODO: Handle OACP Control
946 } else if (!g_strcmp0(char_path, otp_olcp_obj_path)) {
947 result = _bt_otp_olcp_write_cb(value, len, offset, &info);
949 BT_ERR("Wrong Object Path %s", char_path);
950 result = BLUETOOTH_ERROR_INTERNAL;
952 bluetooth_gatt_send_response(req_id,
953 BLUETOOTH_GATT_ATT_REQUEST_TYPE_WRITE,
956 /* Send indication for CPs */
957 if (!g_strcmp0(char_path, otp_oacp_obj_path)) {
958 /* Handle OACP Indication */
959 } else if (!g_strcmp0(char_path, otp_olcp_obj_path)) {
961 _bt_otp_send_indication(char_path, &info, &addr_hex);
965 BT_ERR("Array Len 0");
970 } else if (!g_strcmp0(property, "ReadValue")) {
976 result = BLUETOOTH_ERROR_NONE;
978 BT_INFO("ReadValue");
979 BT_INFO("Type '%s'\n", g_variant_get_type_string(var));
981 g_variant_get(var, "(&s&s&syq)", &char_path,
982 &svc_handle, &addr, &req_id, &offset);
984 result = _bt_otp_read_cb(char_path, &value, &len);
986 if (result != BLUETOOTH_ERROR_NONE) {
987 BT_ERR("ReadValue failed %s", char_path);
988 bluetooth_gatt_send_response(req_id,
989 BLUETOOTH_GATT_ATT_REQUEST_TYPE_READ,
990 result, offset, NULL, 0);
992 bluetooth_gatt_send_response(req_id,
993 BLUETOOTH_GATT_ATT_REQUEST_TYPE_READ,
994 result, offset, value, len);
998 } else if (!g_strcmp0(property, "NotificationStateChanged")) {
999 gboolean indicate = FALSE;
1001 g_variant_get(var, "(&s&sb)", &char_path,
1002 &svc_handle, &indicate);
1004 BT_INFO("%s : [%s]", property,
1005 indicate ? "StartNotify" : "StopNotify");
1006 BT_INFO("Type '%s'\n", g_variant_get_type_string(var));
1008 if (!g_strcmp0(char_path, otp_oacp_obj_path)) {
1009 /* Handle OACP notification */
1010 } else if (!g_strcmp0(char_path, otp_olcp_obj_path)) {
1011 OLCP_indicate = indicate;
1018 void _bt_otp_property_event_filter(GDBusConnection *connection,
1019 const gchar *sender_name,
1020 const gchar *object_path,
1021 const gchar *interface_name,
1022 const gchar *signal_name,
1023 GVariant *parameters,
1028 if (signal_name == NULL) {
1029 BT_ERR("Wrong Signal");
1033 if (g_strcmp0(signal_name, PROPERTIES_CHANGED) == 0) {
1035 g_variant_get(parameters, "(@a{sv}@as)", &value, NULL);
1036 _bt_otp_gatt_char_property_changed_event(value, object_path);
1040 void _bt_otp_adapter_event_filter(GDBusConnection *connection,
1041 const gchar *sender_name,
1042 const gchar *object_path,
1043 const gchar *interface_name,
1044 const gchar *signal_name,
1045 GVariant *parameters,
1048 if (signal_name == NULL) {
1049 BT_ERR("Wrong Signal");
1053 BT_INFO("Interface %s, Signal %s", interface_name, signal_name);
1055 if (g_strcmp0(interface_name, BT_OTP_INTERFACE_NAME) == 0) {
1056 if (strcasecmp(signal_name, BLE_DISABLED) == 0) {
1062 int _bt_otp_init_event_receiver()
1065 GError *error = NULL;
1068 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
1069 if (error != NULL) {
1070 BT_ERR("ERROR: Can't get on system bus [%s]",
1072 g_clear_error(&error);
1076 property_sub_id = g_dbus_connection_signal_subscribe(conn,
1078 BT_OTP_INTERFACE_NAME,
1080 BT_OTP_OBJECT_PATH, NULL, 0,
1081 _bt_otp_property_event_filter,
1084 adapter_sub_id = g_dbus_connection_signal_subscribe(conn,
1086 BT_OTP_INTERFACE_NAME,
1088 BT_OTP_OBJECT_PATH, NULL, 0,
1089 _bt_otp_adapter_event_filter,
1096 void _bt_otp_deinit_event_receiver(void)
1100 g_dbus_connection_signal_unsubscribe(conn, property_sub_id);
1101 g_dbus_connection_signal_unsubscribe(conn, adapter_sub_id);
1107 static void _bt_otp_sig_handler(int sig)
1112 BT_DBG("caught signal - sigterm\n");
1115 BT_DBG("caught signal - sigint\n");
1118 BT_DBG("caught signal - sigkill\n");
1121 BT_DBG("caught signal %d and ignored\n", sig);
1127 /* OTP Service Main loop */
1130 struct sigaction sa;
1131 BT_ERR("Starting the bt-otp daemon");
1133 memset(&sa, 0, sizeof(sa));
1134 sa.sa_handler = _bt_otp_sig_handler;
1135 sa.sa_flags = SA_SIGINFO;
1136 sigaction(SIGINT, &sa, NULL);
1137 sigaction(SIGTERM, &sa, NULL);
1138 sigaction(SIGKILL, &sa, NULL);
1140 if (_bt_otp_register_interface() != BLUETOOTH_ERROR_NONE) {
1141 BT_ERR("Fail to register otp service");
1145 if (_bt_otp_init_event_receiver() != BLUETOOTH_ERROR_NONE) {
1146 BT_ERR("Fail to init event reciever");
1150 main_loop = g_main_loop_new(NULL, FALSE);
1152 g_main_loop_run(main_loop);
1154 BT_DBG("g_main_loop_quit called!");
1156 if (main_loop != NULL) {
1157 g_main_loop_unref(main_loop);