[OTP] Handle Object Select request
[platform/core/connectivity/bluetooth-frwk.git] / bt-otp / bt-otpserver.c
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *              http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 #include <dlog.h>
19 #include <gio/gio.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdint.h>
24 #include <time.h>
25 #include <sys/stat.h>
26 #include <langinfo.h>
27 #include <inttypes.h>
28 #include <errno.h>
29 #include <gio/gunixfdlist.h>
30
31 #include "bt-otpserver.h"
32 #include "bluetooth-api.h"
33
34
35 #undef LOG_TAG
36 #define LOG_TAG "BLUETOOTH_OTP"
37
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)
41
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;
56
57 static GMainLoop *main_loop;
58 GDBusNodeInfo *otp_node_info = NULL;
59 static GDBusConnection *conn;
60 static GDBusConnection *g_conn;
61
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;
66
67 struct otp_char_info {
68         gchar *char_path;
69         gchar *char_value;
70         int value_length;
71 };
72
73 struct indicate_info {
74         uint8_t resp_opcode;
75         uint8_t req_opcode;
76         uint8_t result_code;
77         uint8_t *resp_param;
78 };
79
80 /* Object metadata */
81 struct object_metadata {
82         gchar *name;
83         gchar *type;
84         uint32_t curr_size;
85         uint32_t alloc_size;
86         time_t first_created;
87         time_t last_modified;
88         uint64_t id;
89         uint32_t props;
90 };
91
92 struct oacp_operation {
93         char *remote_address;
94         uint32_t offset;
95         uint32_t length;
96         uint8_t opcode;
97         uint8_t mode;
98         int fd;
99 };
100
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;
113
114 static const gchar otp_introspection_xml[] =
115 "<node name='/'>"
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'/>"
120 "               </method>"
121 "               <method name='disable'>"
122 "                       <arg type='i' name='status' direction='out'/>"
123 "               </method>"
124 "     <method name='NewConnection'>"
125 "          <arg type='o' name='object' direction='in'/>"
126 "          <arg type='h' name='fd' direction='in'/>"
127 "     </method>"
128 "       </interface>"
129 "</node>";
130
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);
136
137 static void delete_all_objects(void)
138 {
139         GSList *tmp = NULL;
140         for (tmp = otp_object_list; tmp != NULL; tmp = tmp->next) {
141                 if (tmp->data) {
142                         struct object_metadata *obj_info = tmp->data;
143                         if (obj_info->name)
144                                 g_free(obj_info->name);
145                         if (obj_info->type)
146                                 g_free(obj_info->type);
147                         otp_object_list = g_slist_delete_link(otp_object_list, tmp->data);
148                 }
149         }
150         g_slist_free(otp_object_list);
151         otp_object_list = NULL;
152 }
153
154 static void delete_all_characterisitc(void)
155 {
156         GSList *tmp = NULL;
157         for (tmp = otp_char_list; tmp != NULL; tmp = tmp->next) {
158                 if (tmp->data) {
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);
165                 }
166         }
167         g_slist_free(otp_char_list);
168         otp_char_list = NULL;
169 }
170
171 void _bt_otp_exit(void)
172 {
173         int ret;
174         BT_DBG("");
175
176         if (otp_char_list)
177                 delete_all_characterisitc();
178
179         if (otp_object_list)
180                 delete_all_objects();
181
182         ret = bluetooth_gatt_deinit();
183         if (ret != BLUETOOTH_ERROR_NONE)
184                 BT_ERR("Failed to Deinit GATT %d", ret);
185
186         _bt_otp_deinit_event_receiver();
187
188         _bt_otp_unregister_interface();
189
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.
194          */
195         ret = bluetooth_set_advertising(adv_handle, FALSE);
196         if (ret != BLUETOOTH_ERROR_NONE)
197                 BT_ERR("Failed to stop ADV %d", ret);
198
199         if (main_loop != NULL) {
200                 g_main_loop_quit(main_loop);
201         }
202 }
203
204 static void _bt_otp_set_char_value(const char *obj_path,
205                                 const char *value, int value_length)
206 {
207         GSList *tmp = NULL;
208
209         if (!value)
210                 return;
211         for (tmp = otp_char_list; tmp != NULL; tmp = tmp->next) {
212                 if (tmp->data) {
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;
219                                 }
220                                 return;
221                         }
222                 }
223         }
224         return;
225 }
226
227 int add_new_characteristic(const char *char_uuid, bt_gatt_permission_t perms,
228                 bt_gatt_characteristic_property_t props, char **obj_path)
229 {
230         int ret = BLUETOOTH_ERROR_NONE;
231         struct otp_char_info *char_info = NULL;
232
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);
237                 return ret;
238         }
239
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);
243
244         return ret;
245 }
246
247 static char *_otp_convert_uuid_to_uuid128(const char *uuid)
248 {
249         int len;
250         char *uuid128;
251
252         len = strlen(uuid);
253
254         switch (len) {
255         case 4:
256                 /* UUID 16bits */
257                 uuid128 = g_strdup_printf("0000%s-0000-1000-8000-00805f9b34fb",
258                                                                         uuid);
259                 break;
260
261         case 8:
262                 /* UUID 32bits */
263                 uuid128 = g_strdup_printf("%s-0000-1000-8000-00805f9b34fb",
264                                                                         uuid);
265                 break;
266
267         case 36:
268                 /* UUID 128bits */
269                 uuid128 = strdup(uuid);
270                 break;
271
272         default:
273                 return NULL;
274         }
275
276         return uuid128;
277 }
278
279 int _bt_otp_prepare_ots(void)
280 {
281         BT_DBG("+");
282         int ret = BLUETOOTH_ERROR_NONE;
283         char *service_uuid;
284         char *char_uuid;
285         char *desc_uuid;
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 };
290
291         ret = bluetooth_gatt_init();
292         if (ret != BLUETOOTH_ERROR_NONE) {
293                 BT_ERR("Failed to Init GATT %d", ret);
294                 goto fail;
295         }
296
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);
301                 goto fail;
302         }
303
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)
311                 goto fail;
312
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);
317                 return ret;
318         }
319
320         _bt_otp_set_char_value(otp_feature_obj_path, supp_feat,
321                                                 OTP_FEATURE_LENGTH);
322
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)
330                 goto fail;
331
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)
339                 goto fail;
340
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)
348                 goto fail;
349
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)
359                 goto fail;
360
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)
370                 goto fail;
371
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)
381                         goto fail;
382         }
383
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)
391                 goto fail;
392
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,
399                                                 &otp_oacp_obj_path);
400         if (ret != BLUETOOTH_ERROR_NONE)
401                 goto fail;
402
403         /* CCCD for OACP */
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);
411                 goto fail;
412         }
413
414         /* OLCP Characteristics is not required
415          * for single object server
416          */
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,
424                                                         &otp_olcp_obj_path);
425                 if (ret != BLUETOOTH_ERROR_NONE)
426                         goto fail;
427
428                 /* CCCD for OLCP */
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);
436                         goto fail;
437                 }
438         }
439
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);
444                 goto fail;
445         }
446
447         /* Register Application */
448         ret = bluetooth_gatt_register_application();
449         if (ret != BLUETOOTH_ERROR_NONE) {
450                 BT_ERR("Failed to register application %d", ret);
451                 goto fail;
452         }
453
454         BT_DBG("-");
455         return ret;
456
457 fail:
458         delete_all_characterisitc();
459         return ret;
460 }
461
462 int _bt_otp_set_advertising_data(void)
463 {
464         int ret;
465         BT_DBG("");
466
467         /* OTP UUID */
468         guint8 data[4]  = {0x03, 0x02, 0x25, 0x18};
469         bluetooth_advertising_data_t adv;
470
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);
476                 return ret;
477         }
478
479         ret = bluetooth_set_advertising(adv_handle, TRUE);
480         if (ret != BLUETOOTH_ERROR_NONE) {
481                 BT_ERR("Failed to set ADV %d", ret);
482                 return ret;
483         }
484
485         return 0;
486 }
487
488 void _bt_otp_start_write_on_fd()
489 {
490         char buf[BT_L2CAP_BUFFER_LEN];
491         int written;
492         int read;
493         int len;
494         FILE *fp;
495         char file_path[BT_FILE_PATH_MAX_LEN] = {0, };
496         int length;
497
498         if (!selected_object) {
499                 BT_DBG("Object not selected");
500                 goto fail;
501         }
502
503         snprintf(file_path, sizeof(file_path), "%s%s",
504                                         directory, selected_object->name);
505         BT_DBG("file_path = [%s]", file_path);
506
507         fp = fopen(file_path, "r");
508         if (!fp) {
509                 BT_DBG("fopen() failed : %s", strerror(errno));
510                 goto fail;
511         }
512
513         BT_DBG("length [%d]", oacp_read->length);
514         length = oacp_read->length;
515
516         while (length > 0) {
517                 if (length < BT_L2CAP_BUFFER_LEN)
518                         len = length;
519                 else
520                         len = BT_L2CAP_BUFFER_LEN;
521
522                 read = fread(buf, 1, len, fp);
523                 written = write(oacp_read->fd, buf, len);
524                 length -= written;
525
526                 BT_DBG("read [%d], written [%d], rem_len [%d]",
527                                                 read, written, length);
528         }
529
530         fclose(fp);
531 fail:
532         g_free(oacp_read->remote_address);
533         g_free(oacp_read);
534         oacp_read = NULL;
535 }
536
537 static void _bt_otp_method(GDBusConnection *connection,
538                 const gchar *sender,
539                 const gchar *object_path,
540                 const gchar *interface_name,
541                 const gchar *method_name,
542                 GVariant *parameters,
543                 GDBusMethodInvocation *invocation,
544                 gpointer user_data)
545 {
546         BT_DBG("+");
547         int status = BLUETOOTH_ERROR_NONE;
548
549         BT_DBG("Method[%s] Object Path[%s] Interface Name[%s]",
550                         method_name, object_path, interface_name);
551
552         if (g_strcmp0(method_name, "enable") == 0) {
553                 GDir *dir = NULL;
554                 GError *error = NULL;
555                 const gchar *filename = NULL;
556                 char absolute_path[ABSOLUTE_PATH_MAX_LENGTH];
557                 GSList *list = NULL, *l = NULL;
558                 struct stat st;
559                 struct object_metadata *object = NULL;
560
561                 g_variant_get(parameters, "(s)", &directory);
562                 BT_DBG("Directory = [%s]", directory);
563
564                 dir = g_dir_open(directory, 0, &error);
565                 if (!dir) {
566                         BT_ERR("Failed to open directory: %s", error->message);
567                         g_error_free(error);
568                         status = BLUETOOTH_ERROR_INVALID_DIRECTORY;
569                         goto fail;
570                 }
571
572                 while ((filename = g_dir_read_name(dir))) {
573                         list = g_slist_append(list, (gpointer) filename);
574                 }
575
576                 g_dir_close(dir);
577
578                 if (!list) {
579                         BT_DBG("No object found in given directory");
580                         status = BLUETOOTH_ERROR_NO_OBJECTS_FOUND;
581                         goto fail;
582                 }
583
584                 if (g_slist_length(list) > 1)
585                         mutiple_obj_support = true;
586
587                 for (l = list; l != NULL; l = l->next) {
588                         if (!l->data) continue;
589                         snprintf(absolute_path, sizeof(absolute_path), "%s%s", directory,
590                                                         (char *)l->data);
591
592                         BT_INFO("filename: %s, absoulte_path: %s",
593                                         (char *)l->data, absolute_path);
594
595                         if (stat(absolute_path, &st) == -1) {
596                                 BT_INFO("stat failed: (%d)\n", errno);
597                                 continue;
598                         }
599
600                         object = g_new0(struct object_metadata, 1);
601
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;
610
611                         otp_object_list = g_slist_append(otp_object_list,
612                                                                 object);
613
614                         object_id++;
615                 }
616
617                 BT_DBG("preparing");
618                 if (_bt_otp_prepare_ots() != BLUETOOTH_ERROR_NONE) {
619                         BT_ERR("Fail to prepare OTP Proxy");
620                         status = BLUETOOTH_ERROR_INTERNAL;
621                         goto fail;
622                 }
623
624                 /* If single object is supported, make that as
625                  * selected object and update the metadata for the same.
626                  */
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);
630                         if (selected_object)
631                                 update_obj_metadata_charc_value(selected_object);
632                 }
633
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;
638                         goto fail;
639                 }
640 fail:
641                 g_dbus_method_invocation_return_value(invocation,
642                                                 g_variant_new("(i)", status));
643
644         } else if (g_strcmp0(method_name, "disable") == 0) {
645                 g_dbus_method_invocation_return_value(invocation,
646                                                 g_variant_new("(i)", status));
647                 _bt_otp_exit();
648
649         } else if (g_strcmp0(method_name, "NewConnection") == 0) {
650                 int index;
651                 GDBusMessage *msg;
652                 GUnixFDList *fd_list;
653                 char *dev_path;
654                 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
655                 int fd;
656
657                 g_variant_get(parameters, "(oh)", &dev_path, &index);
658
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");
663                         return;
664                 }
665
666                 fd = g_unix_fd_list_get(fd_list, index, NULL);
667                 if (fd == -1) {
668                         BT_ERR("Invalid fd return");
669                         return;
670                 }
671
672                 _bt_convert_device_path_to_address(dev_path, address);
673
674                 BT_INFO("OTC Connected fd: %d, address %s", fd, address);
675                 if (!oacp_read) {
676                         /* OTC Connected, but no on going request */
677                         goto done;
678                 }
679                 oacp_read->fd = fd;
680                 otc_connection_status = TRUE;
681
682                 if (oacp_read->opcode == OACP_READ)
683                         _bt_otp_start_write_on_fd();
684 done:
685                 g_dbus_method_invocation_return_value(invocation, NULL);
686         }
687         BT_DBG("-");
688 }
689
690 static const GDBusInterfaceVTable otp_method_table = {
691         _bt_otp_method,
692         NULL,
693         NULL,
694 };
695
696 static void _bt_otp_on_bus_acquired(GDBusConnection *connection,
697                                 const gchar *name, gpointer user_data)
698 {
699         guint object_id;
700         GError *error = NULL;
701
702         BT_DBG("+");
703
704         g_conn = connection;
705
706         object_id = g_dbus_connection_register_object(connection,
707                                                 BT_OTP_OBJECT_PATH,
708                                                 otp_node_info->interfaces[0],
709                                                 &otp_method_table,
710                                                 NULL, NULL, &error);
711         if (object_id == 0) {
712                 BT_ERR("Failed to register method table: %s", error->message);
713                 g_error_free(error);
714                 g_dbus_node_info_unref(otp_node_info);
715         }
716
717         BT_DBG("-");
718 }
719
720 static void _bt_otp_on_name_acquired(GDBusConnection *connection,
721                                         const gchar     *name,
722                                         gpointer user_data)
723 {
724         BT_DBG("");
725 }
726
727 static void _bt_otp_on_name_lost(GDBusConnection *connection,
728                                 const gchar     *name,
729                                 gpointer user_data)
730 {
731         BT_DBG("");
732         g_object_unref(g_conn);
733         g_conn = NULL;
734         g_dbus_node_info_unref(otp_node_info);
735         g_bus_unown_name(g_owner_id);
736 }
737
738 int _bt_otp_register_interface(void)
739 {
740         BT_DBG("+");
741         GError *error = NULL;
742         guint owner_id;
743
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;
748         }
749
750         owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
751                                 BT_OTP_SERVICE_NAME,
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,
756                                 NULL, NULL);
757         g_owner_id = owner_id;
758         BT_DBG("owner_id is [%d]\n", owner_id);
759
760         BT_DBG("-");
761         return BLUETOOTH_ERROR_NONE;
762 }
763
764 void _bt_otp_unregister_interface(void)
765 {
766         BT_DBG("+");
767
768         g_object_unref(g_conn);
769         g_conn = NULL;
770         g_dbus_node_info_unref(otp_node_info);
771         g_bus_unown_name(g_owner_id);
772
773         BT_DBG("-");
774         return;
775 }
776
777 void _bt_convert_device_path_to_address(const char *device_path,
778                                                 char *device_address)
779 {
780         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
781         char *dev_addr;
782
783         dev_addr = strstr(device_path, "dev_");
784         if (dev_addr != NULL) {
785                 char *pos = NULL;
786                 dev_addr += 4;
787                 g_strlcpy(address, dev_addr, sizeof(address));
788
789                 while ((pos = strchr(address, '_')) != NULL)
790                         *pos = ':';
791
792                 g_strlcpy(device_address, address, BT_ADDRESS_STRING_SIZE);
793         }
794 }
795
796 static char *__bt_extract_device_path(GVariantIter *iter, char *address)
797 {
798         char *object_path = NULL;
799         char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
800
801         /* Parse the signature: oa{sa{sv}}} */
802         while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path,
803                         NULL)) {
804                 if (!object_path) {
805                         BT_ERR("Unable to get object path");
806                         return NULL;
807                 }
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);
811
812         }
813
814         BT_ERR("Unable to get object path");
815         return NULL;
816 }
817
818 char *_bt_otp_get_device_object_path(char *address)
819 {
820         GError *err = NULL;
821         GDBusProxy *proxy = NULL;
822         GVariant *result = NULL;
823         GVariantIter *iter = NULL;
824         char *object_path = NULL;
825
826         proxy =  g_dbus_proxy_new_sync(conn,
827                         G_DBUS_PROXY_FLAGS_NONE, NULL,
828                         BT_BLUEZ_NAME,
829                         BT_MANAGER_PATH,
830                         BT_MANAGER_INTERFACE,
831                         NULL, &err);
832
833         if (!proxy) {
834                 BT_ERR("Unable to create proxy: %s", err->message);
835                 goto fail;
836         }
837
838         result = g_dbus_proxy_call_sync(proxy, "GetManagedObjects", NULL,
839                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
840         if (!result) {
841                 if (err != NULL)
842                         BT_ERR("Fail to get GetManagedObjects (Error: %s)", err->message);
843                 else
844                         BT_ERR("Fail to get GetManagedObjects");
845
846                 goto fail;
847         }
848
849         g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
850         object_path = __bt_extract_device_path(iter, address);
851
852         g_variant_unref(result);
853         g_variant_iter_free(iter);
854
855 fail:
856         if (err)
857                 g_clear_error(&err);
858
859         if (proxy)
860                 g_object_unref(proxy);
861
862         return object_path;
863 }
864
865 int _bt_otp_open_otc_and_listen(char *address)
866 {
867         char *object_path;
868         GDBusProxy *device_proxy = NULL;
869         GVariant *result = NULL;
870         GError *error = NULL;
871         int ret = BLUETOOTH_ERROR_NONE;
872
873         object_path = _bt_otp_get_device_object_path(address);
874         if (object_path == NULL) {
875                 ret = BLUETOOTH_ERROR_NOT_PAIRED;
876                 goto fail;
877         }
878
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;
884                 goto fail;
885         }
886
887
888         result = g_dbus_proxy_call_sync(device_proxy, "ListenOtc",
889                                 NULL,
890                                 G_DBUS_CALL_FLAGS_NONE,
891                                 -1,
892                                 NULL,
893                                 &error);
894         if (result == NULL) {
895                 if (error != NULL) {
896                         BT_ERR("Error occured in Proxy call [%s]\n", error->message);
897                         g_error_free(error);
898                 }
899                 ret = BLUETOOTH_ERROR_INTERNAL;
900         }
901 fail:
902         if (object_path)
903                 g_free(object_path);
904         if (result)
905                 g_variant_unref(result);
906         if (device_proxy)
907                 g_object_unref(device_proxy);
908         return ret;
909 }
910
911 int _bt_otp_oacp_write_cb(char *value, int len, int offset,
912                                                         char *remote_addr, struct indicate_info *info)
913 {
914         int ret = OACP_SUCCESS;
915         int app_err = BLUETOOTH_ERROR_NONE;
916         int opcode = value[0];
917         uint32_t object_offset, length;
918
919         BT_INFO("OACP Opcode 0x%d", opcode);
920
921         if (!otp_object_list) {
922                 ret = OACP_INVALID_OBJ;
923                 goto fail;
924         }
925
926         switch (opcode) {
927         case OACP_CREATE:
928                 ret = OACP_OPCODE_NOT_SUPPORTED;
929                 break;
930         case OACP_DELETE:
931                 ret = OACP_OPCODE_NOT_SUPPORTED;
932                 break;
933         case OACP_CALC_CHECKSUM:
934                 ret = OACP_OPCODE_NOT_SUPPORTED;
935                 break;
936         case OACP_EXECUTE:
937                 ret = OACP_OPCODE_NOT_SUPPORTED;
938                 break;
939         case OACP_READ:
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);
948
949                 BT_INFO("Offset = %lu, Length = %lu", object_offset, length);
950
951                 if (oacp_read && otc_connection_status) {
952                         /* Read operation already going on. */
953                         ret = OACP_OBJECT_LOCKED;
954                         goto fail;
955                 }
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;
961
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);
966                         g_free(oacp_read);
967                         oacp_read = NULL;
968                         goto fail;
969                 }
970                 ret = OACP_SUCCESS;
971                 break;
972         case OACP_WRITE:
973                 ret = OACP_OPCODE_NOT_SUPPORTED;
974                 break;
975         case OACP_ABORT:
976                 ret = OACP_OPCODE_NOT_SUPPORTED;
977                 break;
978         default:
979                 ret = OACP_OPCODE_NOT_SUPPORTED;
980                 break;
981         }
982 fail:
983         info->resp_opcode = OACP_RESPONSE;
984         info->req_opcode = opcode;
985         info->result_code = ret;
986         info->resp_param = NULL;
987         return app_err;
988 }
989
990 void convert_to_hex(struct object_metadata *object, char *type, char *value)
991 {
992         struct tm fc_tm;
993
994         BT_DBG("type : %s", type);
995
996         memset(value, 0, 8);
997
998         if (!g_strcmp0(type, "size")) {
999
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;
1004
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;
1009
1010         } else if (!g_strcmp0(type, "date")) {
1011
1012                 localtime_r(&(object->first_created), &fc_tm);
1013
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;
1021
1022         } else if (!g_strcmp0(type, "id")) {
1023
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;
1030
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;
1036         }
1037 }
1038
1039 void update_obj_metadata_charc_value(struct object_metadata *object)
1040 {
1041         /* Value can be of maximum eight bytes */
1042         char value[8];
1043
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));
1048
1049         convert_to_hex(object, "size", value);
1050         _bt_otp_set_char_value(otp_object_size_obj_path, value, 8);
1051
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);
1055
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);
1060         }
1061
1062         convert_to_hex(object, "props", value);
1063         _bt_otp_set_char_value(otp_object_prop_obj_path, value, 4);
1064 }
1065
1066 struct object_metadata *_bt_otp_client_find_object(GSList *list, uint64_t id, guint *index)
1067 {
1068         GSList *l;
1069         struct object_metadata *info;
1070
1071         for (l = list; l; l = g_slist_next(l)) {
1072                 (*index)++;
1073                 info = l->data;
1074
1075                 if (info && (info->id == id))
1076                         return info;
1077         }
1078         return NULL;
1079 }
1080
1081 int _bt_otp_olcp_write_cb(char *value, int len, int offset,
1082                                         struct indicate_info *info)
1083 {
1084         int ret = OLCP_SUCCESS;
1085         int opcode = value[0];
1086         struct object_metadata *object;
1087         uint64_t object_id;
1088         guint index = 0;
1089
1090         BT_INFO("OLCP Opcode 0x%d", opcode);
1091
1092         if (!otp_object_list) {
1093                 ret = OLCP_NO_OBJ;
1094                 goto fail;
1095         }
1096
1097         switch (opcode) {
1098         case OLCP_FIRST:
1099                 object = (struct object_metadata *) g_slist_nth_data(otp_object_list, 0);
1100                 if (!object) {
1101                         ret = OLCP_OUT_OF_BOUNDS;
1102                         goto fail;
1103                 }
1104                 update_obj_metadata_charc_value(object);
1105                 selected_object = object;
1106                 obj_curr_index = 0;
1107                 break;
1108         case OLCP_LAST:
1109                 len = g_slist_length(otp_object_list);
1110                 object = (struct object_metadata *) g_slist_nth_data(otp_object_list, len-1);
1111                 if (!object) {
1112                         ret = OLCP_OUT_OF_BOUNDS;
1113                         goto fail;
1114                 }
1115                 update_obj_metadata_charc_value(object);
1116                 selected_object = object;
1117                 obj_curr_index = len-1;
1118                 break;
1119         case OLCP_PREVIOUS:
1120                 if (obj_curr_index == 0) {
1121                         ret = OLCP_OUT_OF_BOUNDS;
1122                         goto fail;
1123                 }
1124                 object = (struct object_metadata *) g_slist_nth_data(otp_object_list, obj_curr_index-1);
1125                 if (!object) {
1126                         ret = OLCP_OUT_OF_BOUNDS;
1127                         goto fail;
1128                 }
1129                 update_obj_metadata_charc_value(object);
1130                 selected_object = object;
1131                 obj_curr_index -= 1;
1132                 break;
1133         case OLCP_NEXT:
1134                 object = (struct object_metadata *) g_slist_nth_data(otp_object_list, obj_curr_index+1);
1135                 if (!object) {
1136                         ret = OLCP_OUT_OF_BOUNDS;
1137                         goto fail;
1138                 }
1139                 update_obj_metadata_charc_value(object);
1140                 selected_object = object;
1141                 obj_curr_index += 1;
1142                 break;
1143         case OLCP_GOTO:
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)
1152                         goto fail;
1153
1154                 object = _bt_otp_client_find_object(otp_object_list, object_id, &index);
1155                 if (!object) {
1156                         ret = OLCP_OJECT_ID_NOT_FOUND;
1157                         goto fail;
1158                 }
1159                 update_obj_metadata_charc_value(object);
1160                 selected_object = object;
1161                 obj_curr_index = index - 1;
1162                 break;
1163         case OLCP_ORDER:
1164         case OLCP_REQ_NO_OBJ:
1165         case OLCP_CLEAR_MARKING:
1166         default:
1167                 ret = OLCP_OPCODE_NOT_SUPPORTED;
1168                 break;
1169         }
1170 fail:
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;
1176 }
1177
1178 static struct otp_char_info *otp_get_char_value(const char *path)
1179 {
1180         GSList *tmp = NULL;
1181
1182         for (tmp = otp_char_list; tmp != NULL; tmp = tmp->next) {
1183                 if (tmp->data) {
1184                         struct otp_char_info *char_info = tmp->data;
1185                         if (!g_strcmp0(char_info->char_path, path))
1186                                 return char_info;
1187                 }
1188         }
1189
1190         return NULL;
1191 }
1192
1193 int _bt_otp_read_cb(const char *obj_path, char **value, int *len)
1194 {
1195         struct otp_char_info *info = NULL;
1196
1197         if (!obj_path) {
1198                 BT_ERR("Wrong Obj path");
1199                 return BLUETOOTH_ERROR_INTERNAL;
1200         }
1201
1202         if (g_strcmp0(obj_path, otp_feature_obj_path)) {
1203                 if (!selected_object) {
1204                         return BLUETOOTH_ERROR_OBJECT_NOT_SELECTED;
1205                 }
1206         }
1207
1208         info = otp_get_char_value(obj_path);
1209         if (info) {
1210                 if (info->char_value == NULL || info->value_length == 0)
1211                         return BLUETOOTH_ERROR_INTERNAL;
1212
1213                 *len = info->value_length;
1214                 *value = (char *)malloc(sizeof(char)*(*len));
1215                 memcpy(*value, info->char_value, *len);
1216
1217                 return BLUETOOTH_ERROR_NONE;
1218         } else {
1219                 return BLUETOOTH_ERROR_INTERNAL;
1220         }
1221 }
1222
1223 static void _otp_convert_address_to_hex(bluetooth_device_address_t *addr_hex,
1224                                                         const char *addr_str)
1225 {
1226         int i = 0;
1227         unsigned int addr[BLUETOOTH_ADDRESS_LENGTH] = { 0, };
1228
1229         if (addr_str == NULL || addr_str[0] == '\0')
1230                 return;
1231
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);
1236
1237         for (i = 0; i < BLUETOOTH_ADDRESS_LENGTH; i++)
1238                 addr_hex->addr[i] = (unsigned char)addr[i];
1239 }
1240
1241 static void _bt_otp_send_indication(const char *obj_path,
1242                                 struct indicate_info *info,
1243                                 bluetooth_device_address_t *remote_address)
1244 {
1245         int ret = BLUETOOTH_ERROR_NONE;
1246         char value[7] = {0x00};
1247
1248         BT_DBG("");
1249
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;
1258         }
1259
1260         BT_DBG("Opcode: %d", value[1]);
1261
1262         /* Store the status value */
1263         _bt_otp_set_char_value(obj_path, value, 7);
1264
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");
1269                 return;
1270         }
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");
1274                 return;
1275         }
1276 }
1277
1278 void _bt_otp_gatt_char_property_changed_event(GVariant *msg,
1279                                 const char *path)
1280 {
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);
1289
1290         while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &var))) {
1291
1292                 if (property == NULL) {
1293                         BT_ERR("Property NULL");
1294                         return;
1295                 }
1296
1297                 if (!g_strcmp0(property, "WriteValue")) {
1298                         int len = 0;
1299                         BT_INFO("WriteValue");
1300                         BT_INFO("Type '%s'\n", g_variant_get_type_string(var));
1301
1302                         if (var) {
1303                                 bluetooth_device_address_t addr_hex = { {0,} };
1304                                 gchar *addr = NULL;
1305                                 guint8 req_id = 1;
1306                                 guint16 offset = 0;
1307                                 char *value = NULL;
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);
1312
1313                                 len = g_variant_get_size(val);
1314
1315                                 BT_DBG("Len = %d, BT_ADDR = %s", len, addr);
1316
1317                                 value = (char *) g_variant_get_data(val);
1318                                 _otp_convert_address_to_hex(&addr_hex, addr);
1319
1320                                 if (len != 0) {
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);
1325                                         } else {
1326                                                 BT_ERR("Wrong Object Path %s", char_path);
1327                                                 result = BLUETOOTH_ERROR_INTERNAL;
1328                                         }
1329                                         bluetooth_gatt_send_response(req_id,
1330                                         BLUETOOTH_GATT_ATT_REQUEST_TYPE_WRITE,
1331                                                         result, 0, NULL, 0);
1332
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);
1337                                                 }
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);
1341                                                 }
1342                                         }
1343                                 } else {
1344                                         BT_ERR("Array Len 0");
1345                                 }
1346                         } else {
1347                                 BT_ERR("var==NULL");
1348                         }
1349                 } else if (!g_strcmp0(property, "ReadValue")) {
1350                         gchar *addr = NULL;
1351                         guint8 req_id = 1;
1352                         guint16 offset = 0;
1353                         char *value = NULL;
1354                         int len = 0;
1355                         result = BLUETOOTH_ERROR_NONE;
1356
1357                         BT_INFO("ReadValue");
1358                         BT_INFO("Type '%s'\n", g_variant_get_type_string(var));
1359
1360                         g_variant_get(var, "(&s&s&syq)", &char_path,
1361                                         &svc_handle, &addr, &req_id, &offset);
1362
1363                         result = _bt_otp_read_cb(char_path, &value, &len);
1364
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);
1370                         } else {
1371                                 bluetooth_gatt_send_response(req_id,
1372                                 BLUETOOTH_GATT_ATT_REQUEST_TYPE_READ,
1373                                                 result, offset, value, len);
1374                                 if (value)
1375                                         g_free(value);
1376                         }
1377                 } else if (!g_strcmp0(property, "NotificationStateChanged")) {
1378                         gboolean indicate = FALSE;
1379
1380                         g_variant_get(var, "(&s&sb)", &char_path,
1381                                                 &svc_handle, &indicate);
1382
1383                         BT_INFO("%s : [%s]", property,
1384                                 indicate ? "StartNotify" : "StopNotify");
1385                         BT_INFO("Type '%s'\n", g_variant_get_type_string(var));
1386
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;
1391                         }
1392                 }
1393         }
1394         return;
1395 }
1396
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,
1403                                         gpointer user_data)
1404 {
1405         GVariant *value;
1406
1407         if (signal_name == NULL) {
1408                 BT_ERR("Wrong Signal");
1409                 return;
1410         }
1411
1412         if (g_strcmp0(signal_name, PROPERTIES_CHANGED) == 0) {
1413
1414                 g_variant_get(parameters, "(@a{sv}@as)", &value, NULL);
1415                 _bt_otp_gatt_char_property_changed_event(value, object_path);
1416         }
1417 }
1418
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,
1425                                         gpointer user_data)
1426 {
1427         if (signal_name == NULL) {
1428                 BT_ERR("Wrong Signal");
1429                 return;
1430         }
1431
1432         BT_INFO("Interface %s, Signal %s", interface_name, signal_name);
1433
1434         if (g_strcmp0(interface_name, BT_OTP_INTERFACE_NAME) == 0) {
1435                 if (strcasecmp(signal_name, BLE_DISABLED) == 0) {
1436                         _bt_otp_exit();
1437                 }
1438         }
1439 }
1440
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,
1447                                         gpointer user_data)
1448 {
1449         if (signal_name == NULL) {
1450                 BT_ERR("Wrong Signal");
1451                 return;
1452         }
1453
1454         BT_INFO("Interface %s, Signal %s", interface_name, signal_name);
1455
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]",
1459                                                                                         object_path);
1460                         otc_connection_status = FALSE;
1461                         if (oacp_read) {
1462                                 g_free(oacp_read->remote_address);
1463                                 g_free(oacp_read);
1464                         }
1465                 }
1466         }
1467 }
1468
1469 int _bt_otp_init_event_receiver()
1470 {
1471         BT_DBG("+");
1472         GError *error = NULL;
1473
1474         if (conn == 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]",
1478                                                         error->message);
1479                         g_clear_error(&error);
1480                 }
1481         }
1482
1483         property_sub_id = g_dbus_connection_signal_subscribe(conn,
1484                                 NULL,
1485                                 BT_OTP_INTERFACE_NAME,
1486                                 PROPERTIES_CHANGED,
1487                                 BT_OTP_OBJECT_PATH, NULL, 0,
1488                                 _bt_otp_property_event_filter,
1489                                 NULL, NULL);
1490
1491         adapter_sub_id = g_dbus_connection_signal_subscribe(conn,
1492                                 NULL,
1493                                 BT_OTP_INTERFACE_NAME,
1494                                 BLE_DISABLED,
1495                                 BT_OTP_OBJECT_PATH, NULL, 0,
1496                                 _bt_otp_adapter_event_filter,
1497                                 NULL, NULL);
1498
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,
1503                                         NULL, NULL);
1504
1505         BT_DBG("-");
1506         return 0;
1507 }
1508
1509 void _bt_otp_deinit_event_receiver(void)
1510 {
1511         BT_DBG("+");
1512
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);
1516         conn = NULL;
1517
1518         BT_DBG("-");
1519 }
1520
1521 static void _bt_otp_sig_handler(int sig)
1522 {
1523         BT_DBG("+");
1524         switch (sig) {
1525         case SIGTERM:
1526                 BT_DBG("caught signal - sigterm\n");
1527                 break;
1528         case SIGINT:
1529                 BT_DBG("caught signal - sigint\n");
1530                 break;
1531         case SIGKILL:
1532                 BT_DBG("caught signal - sigkill\n");
1533                 break;
1534         default:
1535                 BT_DBG("caught signal %d and ignored\n", sig);
1536                 break;
1537         }
1538         BT_DBG("-");
1539 }
1540
1541 /* OTP Service Main loop */
1542 int main(void)
1543 {
1544         struct sigaction sa;
1545         BT_ERR("Starting the bt-otp daemon");
1546
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);
1553
1554         if (_bt_otp_register_interface() != BLUETOOTH_ERROR_NONE) {
1555                 BT_ERR("Fail to register otp service");
1556                 return -4;
1557         }
1558
1559         if (_bt_otp_init_event_receiver() != BLUETOOTH_ERROR_NONE) {
1560                 BT_ERR("Fail to init event reciever");
1561                 return -5;
1562         }
1563
1564         main_loop = g_main_loop_new(NULL, FALSE);
1565
1566         g_main_loop_run(main_loop);
1567
1568         BT_DBG("g_main_loop_quit called!");
1569
1570         if (main_loop != NULL) {
1571                 g_main_loop_unref(main_loop);
1572         }
1573
1574         return 0;
1575 }