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] = { 0x0C, 0x00, 0x00, 0x00,
267 0x00, 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 sprintf(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: %s (%d)\n",
513 strerror(errno), errno);
517 object = g_new0(struct object_metadata, 1);
519 object->name = g_strdup((const gchar *)l->data);
520 object->type = _otp_convert_uuid_to_uuid128(UNSUPPORTED_OBJECT_TYPE_UUID);
521 object->first_created = st.st_ctime;
522 object->last_modified = st.st_ctime;
523 object->curr_size = (uint32_t) st.st_size;
524 object->alloc_size = (uint32_t) st.st_size;
525 object->id = object_id;
526 object->props = OBJECT_READ | OBJECT_WRITE;
528 otp_object_list = g_slist_append(otp_object_list,
537 if (_bt_otp_prepare_ots() != BLUETOOTH_ERROR_NONE) {
538 BT_ERR("Fail to prepare OTP Proxy");
539 status = BLUETOOTH_ERROR_INTERNAL;
543 BT_DBG("advertsing");
544 if (_bt_otp_set_advertising_data() != BLUETOOTH_ERROR_NONE) {
545 BT_ERR("Fail to set advertising data");
546 status = BLUETOOTH_ERROR_INTERNAL;
550 g_dbus_method_invocation_return_value(invocation,
551 g_variant_new("(i)", status));
553 } else if (g_strcmp0(method_name, "disable") == 0) {
554 g_dbus_method_invocation_return_value(invocation,
555 g_variant_new("(i)", status));
561 static const GDBusInterfaceVTable otp_method_table = {
567 static void _bt_otp_on_bus_acquired(GDBusConnection *connection,
568 const gchar *name, gpointer user_data)
571 GError *error = NULL;
577 object_id = g_dbus_connection_register_object(connection,
579 otp_node_info->interfaces[0],
582 if (object_id == 0) {
583 BT_ERR("Failed to register method table: %s", error->message);
585 g_dbus_node_info_unref(otp_node_info);
591 static void _bt_otp_on_name_acquired(GDBusConnection *connection,
598 static void _bt_otp_on_name_lost(GDBusConnection *connection,
603 g_object_unref(g_conn);
605 g_dbus_node_info_unref(otp_node_info);
606 g_bus_unown_name(g_owner_id);
609 int _bt_otp_register_interface(void)
612 GError *error = NULL;
615 otp_node_info = g_dbus_node_info_new_for_xml(otp_introspection_xml, &error);
616 if (!otp_node_info) {
617 BT_ERR("Failed to install: %s", error->message);
618 return BLUETOOTH_ERROR_INTERNAL;
621 owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
623 G_BUS_NAME_OWNER_FLAGS_NONE,
624 _bt_otp_on_bus_acquired,
625 _bt_otp_on_name_acquired,
626 _bt_otp_on_name_lost,
628 g_owner_id = owner_id;
629 BT_DBG("owner_id is [%d]\n", owner_id);
632 return BLUETOOTH_ERROR_NONE;
635 void _bt_otp_unregister_interface(void)
639 g_object_unref(g_conn);
641 g_dbus_node_info_unref(otp_node_info);
642 g_bus_unown_name(g_owner_id);
649 void convert_to_hex(struct object_metadata *object, char *type, char *value)
653 BT_DBG("type : %s", type);
657 if (!g_strcmp0(type, "size")) {
659 value[3] = (object->curr_size >> 24) & 0xFF;
660 value[2] = (object->curr_size >> 16) & 0xFF;
661 value[1] = (object->curr_size >> 8) & 0xFF;
662 value[0] = object->curr_size & 0xFF;
664 value[7] = (object->alloc_size >> 24) & 0xFF;
665 value[6] = (object->alloc_size >> 16) & 0xFF;
666 value[5] = (object->alloc_size >> 8) & 0xFF;
667 value[4] = object->alloc_size & 0xFF;
669 } else if (!g_strcmp0(type, "date")) {
671 tm = localtime(&(object->first_created));
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;
681 } else if (!g_strcmp0(type, "id")) {
683 value[5] = (object->id >> 48) & 0xFF;
684 value[4] = (object->id >> 32) & 0xFF;
685 value[3] = (object->id >> 24) & 0xFF;
686 value[2] = (object->id >> 16) & 0xFF;
687 value[1] = (object->id >> 8) & 0xFF;
688 value[0] = object->id & 0xFF;
690 } else if (!g_strcmp0(type, "props")) {
691 value[3] = (object->props >> 24) & 0xFF;
692 value[2] = (object->props >> 16) & 0xFF;
693 value[1] = (object->props >> 8) & 0xFF;
694 value[0] = object->props & 0xFF;
698 void update_obj_metadata_charc_value(struct object_metadata *object)
700 /* Value can be of maximum eight bytes */
703 _bt_otp_set_char_value(otp_object_name_obj_path, object->name,
704 strlen(object->name));
705 _bt_otp_set_char_value(otp_object_type_obj_path, object->type,
706 strlen(object->type));
708 convert_to_hex(object, "size", value);
709 _bt_otp_set_char_value(otp_object_size_obj_path, value, 8);
711 convert_to_hex(object, "date", value);
712 _bt_otp_set_char_value(otp_object_first_created_obj_path, value, 7);
713 _bt_otp_set_char_value(otp_object_last_modified_obj_path, value, 7);
715 convert_to_hex(object, "id", value);
716 _bt_otp_set_char_value(otp_object_id_obj_path, value, 6);
718 convert_to_hex(object, "props", value);
719 _bt_otp_set_char_value(otp_object_prop_obj_path, value, 4);
722 int _bt_otp_olcp_write_cb(char *value, int len, int offset,
723 struct indicate_info *info)
725 int ret = OLCP_SUCCESS;
726 int opcode = value[0];
727 struct object_metadata *object;
729 BT_INFO("OLCP Opcode 0x%d", opcode);
731 if (!otp_object_list) {
738 object = (struct object_metadata *) g_slist_nth_data(otp_object_list, 0);
740 ret = OLCP_OUT_OF_BOUNDS;
743 update_obj_metadata_charc_value(object);
744 selected_object = object;
748 len = g_slist_length(otp_object_list);
749 object = (struct object_metadata *) g_slist_nth_data(otp_object_list, len-1);
751 ret = OLCP_OUT_OF_BOUNDS;
754 update_obj_metadata_charc_value(object);
755 selected_object = object;
756 obj_curr_index = len-1;
758 case OLCP_PREVIOUS: {
759 if (obj_curr_index == 0) {
760 ret = OLCP_OUT_OF_BOUNDS;
763 object = (struct object_metadata *) g_slist_nth_data(otp_object_list, obj_curr_index-1);
765 ret = OLCP_OUT_OF_BOUNDS;
768 update_obj_metadata_charc_value(object);
769 selected_object = object;
773 object = (struct object_metadata *) g_slist_nth_data(otp_object_list, obj_curr_index+1);
775 ret = OLCP_OUT_OF_BOUNDS;
778 update_obj_metadata_charc_value(object);
779 selected_object = object;
784 case OLCP_REQ_NO_OBJ:
785 case OLCP_CLEAR_MARKING:
787 ret = OLCP_OPCODE_NOT_SUPPORTED;
791 info->resp_opcode = OLCP_RESPONSE;
792 info->req_opcode = opcode;
793 info->result_code = ret;
794 info->resp_param = NULL;
795 return BLUETOOTH_ERROR_NONE;
798 static struct otp_char_info *otp_get_char_value(const char *path)
802 for (tmp = otp_char_list; tmp != NULL; tmp = tmp->next) {
804 struct otp_char_info *char_info = tmp->data;
805 if (!g_strcmp0(char_info->char_path, path))
813 int _bt_otp_read_cb(const char *obj_path, char **value, int *len)
815 struct otp_char_info *info = NULL;
818 BT_ERR("Wrong Obj path");
819 return BLUETOOTH_ERROR_INTERNAL;
822 if (g_strcmp0(obj_path, otp_feature_obj_path)) {
823 if (!selected_object) {
824 return BLUETOOTH_ERROR_OBJECT_NOT_SELECTED;
828 info = otp_get_char_value(obj_path);
830 if (info->char_value == NULL || info->value_length == 0)
831 return BLUETOOTH_ERROR_INTERNAL;
833 *len = info->value_length;
834 *value = (char *)malloc(sizeof(char *)*(*len));
835 memcpy(*value, info->char_value, *len);
837 return BLUETOOTH_ERROR_NONE;
839 return BLUETOOTH_ERROR_INTERNAL;
843 static void _otp_convert_address_to_hex(bluetooth_device_address_t *addr_hex,
844 const char *addr_str)
847 unsigned int addr[BLUETOOTH_ADDRESS_LENGTH] = { 0, };
849 if (addr_str == NULL || addr_str[0] == '\0')
852 i = sscanf(addr_str, "%X:%X:%X:%X:%X:%X", &addr[0], &addr[1],
853 &addr[2], &addr[3], &addr[4], &addr[5]);
854 if (i != BLUETOOTH_ADDRESS_LENGTH)
855 BT_ERR("Invalid format string - [%s]", addr_str);
857 for (i = 0; i < BLUETOOTH_ADDRESS_LENGTH; i++)
858 addr_hex->addr[i] = (unsigned char)addr[i];
861 static void _bt_otp_send_indication(const char *obj_path,
862 struct indicate_info *info,
863 bluetooth_device_address_t *remote_address)
865 int ret = BLUETOOTH_ERROR_NONE;
866 char value[7] = {0x00};
870 value[0] = info->resp_opcode & 0xFF;
871 value[1] = info->req_opcode & 0xFF;
872 value[2] = info->result_code & 0xFF;
873 if (info->resp_param) {
874 value[6] = (info->resp_param[3] >> 24) & 0xFF;
875 value[5] = (info->resp_param[4] >> 16) & 0xFF;
876 value[4] = (info->resp_param[5] >> 8) & 0xFF;
877 value[3] = info->resp_param[6] & 0xFF;
880 BT_DBG("Opcode: %d", value[1]);
882 /* Store the status value */
883 _bt_otp_set_char_value(obj_path, value, 7);
885 /* Send indication */
886 ret = bluetooth_gatt_server_set_notification(obj_path, remote_address);
887 if (ret != BLUETOOTH_ERROR_NONE) {
888 BT_ERR("_bt_otp_send_control_point_indication failed");
891 ret = bluetooth_gatt_update_characteristic(obj_path, value, 7);
892 if (ret != BLUETOOTH_ERROR_NONE) {
893 BT_ERR("_bt_otp_send_control_point_indication failed");
898 void _bt_otp_gatt_char_property_changed_event(GVariant *msg,
901 int result = BLUETOOTH_ERROR_NONE;
902 GVariantIter value_iter;
903 const char *property = NULL;
904 const char *char_path = NULL;
905 const char *svc_handle = NULL;
906 GVariant *var = NULL;
907 GVariant *val = NULL;
908 g_variant_iter_init(&value_iter, msg);
910 while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &var))) {
912 if (property == NULL) {
913 BT_ERR("Property NULL");
917 if (!g_strcmp0(property, "WriteValue")) {
919 BT_INFO("WriteValue");
920 BT_INFO("Type '%s'\n", g_variant_get_type_string(var));
923 bluetooth_device_address_t addr_hex = { {0,} };
928 struct indicate_info info;
929 g_variant_get(var, "(&s&s&syq@ay)",
930 &char_path, &svc_handle,
931 &addr, &req_id, &offset, &val);
933 len = g_variant_get_size(val);
935 BT_DBG("Len = %d, BT_ADDR = %s", len, addr);
937 value = (char *) g_variant_get_data(val);
938 _otp_convert_address_to_hex(&addr_hex, addr);
941 if (!g_strcmp0(char_path, otp_oacp_obj_path)) {
942 /* TODO: Handle OACP Control
945 } else if (!g_strcmp0(char_path, otp_olcp_obj_path)) {
946 result = _bt_otp_olcp_write_cb(value, len, offset, &info);
948 BT_ERR("Wrong Object Path %s", char_path);
949 result = BLUETOOTH_ERROR_INTERNAL;
951 bluetooth_gatt_send_response(req_id,
952 BLUETOOTH_GATT_ATT_REQUEST_TYPE_WRITE,
955 /* Send indication for CPs */
956 if (!g_strcmp0(char_path, otp_oacp_obj_path)) {
957 /* Handle OACP Indication */
958 } else if (!g_strcmp0(char_path, otp_olcp_obj_path)) {
960 _bt_otp_send_indication(char_path, &info, &addr_hex);
964 BT_ERR("Array Len 0");
969 } else if (!g_strcmp0(property, "ReadValue")) {
975 result = BLUETOOTH_ERROR_NONE;
977 BT_INFO("ReadValue");
978 BT_INFO("Type '%s'\n", g_variant_get_type_string(var));
980 g_variant_get(var, "(&s&s&syq)", &char_path,
981 &svc_handle, &addr, &req_id, &offset);
983 result = _bt_otp_read_cb(char_path, &value, &len);
985 if (result != BLUETOOTH_ERROR_NONE) {
986 BT_ERR("ReadValue failed %s", char_path);
987 bluetooth_gatt_send_response(req_id,
988 BLUETOOTH_GATT_ATT_REQUEST_TYPE_READ,
989 result, offset, NULL, 0);
991 bluetooth_gatt_send_response(req_id,
992 BLUETOOTH_GATT_ATT_REQUEST_TYPE_READ,
993 result, offset, value, len);
997 } else if (!g_strcmp0(property, "NotificationStateChanged")) {
998 gboolean indicate = FALSE;
1000 g_variant_get(var, "(&s&sb)", &char_path,
1001 &svc_handle, &indicate);
1003 BT_INFO("%s : [%s]", property,
1004 indicate ? "StartNotify" : "StopNotify");
1005 BT_INFO("Type '%s'\n", g_variant_get_type_string(var));
1007 if (!g_strcmp0(char_path, otp_oacp_obj_path)) {
1008 /* Handle OACP notification */
1009 } else if (!g_strcmp0(char_path, otp_olcp_obj_path)) {
1010 OLCP_indicate = indicate;
1017 void _bt_otp_property_event_filter(GDBusConnection *connection,
1018 const gchar *sender_name,
1019 const gchar *object_path,
1020 const gchar *interface_name,
1021 const gchar *signal_name,
1022 GVariant *parameters,
1027 if (signal_name == NULL) {
1028 BT_ERR("Wrong Signal");
1032 if (g_strcmp0(signal_name, PROPERTIES_CHANGED) == 0) {
1034 g_variant_get(parameters, "(@a{sv}@as)", &value, NULL);
1035 _bt_otp_gatt_char_property_changed_event(value, object_path);
1039 void _bt_otp_adapter_event_filter(GDBusConnection *connection,
1040 const gchar *sender_name,
1041 const gchar *object_path,
1042 const gchar *interface_name,
1043 const gchar *signal_name,
1044 GVariant *parameters,
1047 if (signal_name == NULL) {
1048 BT_ERR("Wrong Signal");
1052 BT_INFO("Interface %s, Signal %s", interface_name, signal_name);
1054 if (g_strcmp0(interface_name, BT_OTP_INTERFACE_NAME) == 0) {
1055 if (strcasecmp(signal_name, BLE_DISABLED) == 0) {
1061 int _bt_otp_init_event_receiver()
1064 GError *error = NULL;
1067 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
1068 if (error != NULL) {
1069 BT_ERR("ERROR: Can't get on system bus [%s]",
1071 g_clear_error(&error);
1075 property_sub_id = g_dbus_connection_signal_subscribe(conn,
1077 BT_OTP_INTERFACE_NAME,
1079 BT_OTP_OBJECT_PATH, NULL, 0,
1080 _bt_otp_property_event_filter,
1083 adapter_sub_id = g_dbus_connection_signal_subscribe(conn,
1085 BT_OTP_INTERFACE_NAME,
1087 BT_OTP_OBJECT_PATH, NULL, 0,
1088 _bt_otp_adapter_event_filter,
1095 void _bt_otp_deinit_event_receiver(void)
1099 g_dbus_connection_signal_unsubscribe(conn, property_sub_id);
1100 g_dbus_connection_signal_unsubscribe(conn, adapter_sub_id);
1106 static void _bt_otp_sig_handler(int sig)
1111 BT_DBG("caught signal - sigterm\n");
1114 BT_DBG("caught signal - sigint\n");
1117 BT_DBG("caught signal - sigkill\n");
1120 BT_DBG("caught signal %d and ignored\n", sig);
1126 /* OTP Service Main loop */
1129 struct sigaction sa;
1130 BT_ERR("Starting the bt-otp daemon");
1132 memset(&sa, 0, sizeof(sa));
1133 sa.sa_handler = _bt_otp_sig_handler;
1134 sa.sa_flags = SA_SIGINFO;
1135 sigaction(SIGINT, &sa, NULL);
1136 sigaction(SIGTERM, &sa, NULL);
1137 sigaction(SIGKILL, &sa, NULL);
1139 if (_bt_otp_register_interface() != BLUETOOTH_ERROR_NONE) {
1140 BT_ERR("Fail to register otp service");
1144 if (_bt_otp_init_event_receiver() != BLUETOOTH_ERROR_NONE) {
1145 BT_ERR("Fail to init event reciever");
1149 main_loop = g_main_loop_new(NULL, FALSE);
1151 g_main_loop_run(main_loop);
1153 BT_DBG("g_main_loop_quit called!");
1155 if (main_loop != NULL) {
1156 g_main_loop_unref(main_loop);