8cc44939e1040d47239357793878e06e66a811a9
[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 #include <dlfcn.h>
31 #include <arpa/inet.h>
32
33 #include "bt-otpserver.h"
34 #include "bluetooth-api.h"
35
36
37 #undef LOG_TAG
38 #define LOG_TAG "BLUETOOTH_OTP"
39
40 #define BT_INFO(fmt, arg...) SLOGI(fmt, ##arg)
41 #define BT_ERR(fmt, arg...) SLOGE(fmt, ##arg)
42 #define BT_DBG(fmt, arg...) SLOGD(fmt, ##arg)
43
44 /* OTP object paths */
45 char *otp_obj_path = NULL;
46 char *otp_feature_obj_path = NULL;
47 char *otp_object_name_obj_path = NULL;
48 char *otp_object_type_obj_path = NULL;
49 char *otp_object_size_obj_path = NULL;
50 char *otp_object_first_created_obj_path = NULL;
51 char *otp_object_last_modified_obj_path = NULL;
52 char *otp_object_id_obj_path = NULL;
53 char *otp_object_prop_obj_path = NULL;
54 char *otp_oacp_obj_path = NULL;
55 char *otp_olcp_obj_path = NULL;
56 char *otp_oacp_desc_obj_path = NULL;
57 char *otp_olcp_desc_obj_path = NULL;
58
59 static GMainLoop *main_loop;
60 GDBusNodeInfo *otp_node_info = NULL;
61 static GDBusConnection *conn;
62 static GDBusConnection *g_conn;
63
64 static int property_sub_id = -1;
65 static int adapter_sub_id = -1;
66 static int device_sub_id = -1;
67 static guint g_owner_id = 0;
68 static guint server_watch_id = 0;
69
70 struct otp_char_info {
71         gchar *char_path;
72         gchar *char_value;
73         int value_length;
74 };
75
76 struct indicate_info {
77         uint8_t resp_opcode;
78         uint8_t req_opcode;
79         uint8_t result_code;
80         uint8_t *resp_param;
81 };
82
83 /* Object metadata */
84 struct object_metadata {
85         gchar *name;
86         gchar *type;
87         uint32_t curr_size;
88         uint32_t alloc_size;
89         time_t first_created;
90         time_t last_modified;
91         uint64_t id;
92         uint32_t props;
93 };
94
95 struct oacp_operation {
96         char *remote_address;
97         uint32_t offset;
98         uint32_t length;
99         uint8_t opcode;
100         uint32_t length_sofar;
101         uint8_t mode;
102         int fd;
103         FILE *fp;
104 };
105
106 struct oacp_create_operation {
107         char *remote_address;
108         char *uuid;
109         uint32_t size;
110 };
111
112 static struct object_metadata *selected_object = NULL;
113 static uint64_t object_id = OBJECT_START_ID;
114 static GSList *otp_object_list = NULL;
115 static GSList *otp_char_list = NULL;
116 static guint obj_curr_index;
117 static int adv_handle = 0;
118 static gboolean OACP_indicate = FALSE;
119 static gboolean OLCP_indicate = FALSE;
120 char *directory = NULL;
121 gboolean mutiple_obj_support = false;
122 static gboolean otc_connection_status = FALSE;
123 struct oacp_operation *oacp_op = NULL;
124 struct oacp_create_operation *oacp_create = NULL;
125 unsigned int timeout_id;
126
127 static const gchar otp_introspection_xml[] =
128 "<node name='/'>"
129 "       <interface name='org.projectx.otp_service'>"
130 "               <method name='enable'>"
131 "                       <arg type='s' name='directory'/>"
132 "                       <arg type='i' name='status' direction='out'/>"
133 "               </method>"
134 "               <method name='disable'>"
135 "                       <arg type='i' name='status' direction='out'/>"
136 "               </method>"
137 "     <method name='NewConnection'>"
138 "          <arg type='o' name='object' direction='in'/>"
139 "          <arg type='h' name='fd' direction='in'/>"
140 "     </method>"
141 "       </interface>"
142 "</node>";
143
144 void _bt_otp_deinit_event_receiver(void);
145 void _bt_otp_unregister_interface(void);
146 void update_obj_metadata_charc_value(struct object_metadata *object);
147 void _bt_convert_device_path_to_address(const char *device_path,
148                                                 char *device_address);
149 int _bt_otp_open_otc_and_listen(char *address, char *method);
150
151 static void delete_all_objects(void)
152 {
153         GSList *tmp = NULL;
154         for (tmp = otp_object_list; tmp != NULL; tmp = tmp->next) {
155                 if (tmp->data) {
156                         struct object_metadata *obj_info = tmp->data;
157                         if (obj_info->name)
158                                 g_free(obj_info->name);
159                         if (obj_info->type)
160                                 g_free(obj_info->type);
161                         otp_object_list = g_slist_delete_link(otp_object_list, tmp->data);
162                 }
163         }
164         g_slist_free(otp_object_list);
165         otp_object_list = NULL;
166 }
167
168 static void delete_all_characterisitc(void)
169 {
170         GSList *tmp = NULL;
171         for (tmp = otp_char_list; tmp != NULL; tmp = tmp->next) {
172                 if (tmp->data) {
173                         struct otp_char_info *char_info = tmp->data;
174                         if (char_info->char_path)
175                                 g_free(char_info->char_path);
176                         if (char_info->char_value)
177                                 g_free(char_info->char_value);
178                         otp_char_list = g_slist_delete_link(otp_char_list, tmp->data);
179                 }
180         }
181         g_slist_free(otp_char_list);
182         otp_char_list = NULL;
183 }
184
185 void _bt_otp_exit(void)
186 {
187         int ret;
188         BT_DBG("");
189
190         if (otp_char_list)
191                 delete_all_characterisitc();
192
193         if (otp_object_list)
194                 delete_all_objects();
195
196         ret = bluetooth_gatt_deinit();
197         if (ret != BLUETOOTH_ERROR_NONE)
198                 BT_ERR("Failed to Deinit GATT %d", ret);
199
200         _bt_otp_deinit_event_receiver();
201
202         _bt_otp_unregister_interface();
203
204         /* TODO: Advertising is not getting stopped by this API.
205          * This is because OTP_SERVER_DEINIT dbus call is blocking
206          * BT_SET_ADVERTISING_DATA dbus call. But now advertisment
207          * is stopped because of terminated process logic.
208          */
209         ret = bluetooth_set_advertising(adv_handle, FALSE);
210         if (ret != BLUETOOTH_ERROR_NONE)
211                 BT_ERR("Failed to stop ADV %d", ret);
212
213         if (main_loop != NULL) {
214                 g_main_loop_quit(main_loop);
215         }
216 }
217
218 static void _bt_otp_set_char_value(const char *obj_path,
219                                 const char *value, int value_length)
220 {
221         GSList *tmp = NULL;
222
223         if (!value)
224                 return;
225         for (tmp = otp_char_list; tmp != NULL; tmp = tmp->next) {
226                 if (tmp->data) {
227                         struct otp_char_info *char_info = tmp->data;
228                         if (!g_strcmp0(char_info->char_path, obj_path)) {
229                                 char_info->char_value = g_try_realloc(char_info->char_value, value_length);
230                                 if (char_info->char_value) {
231                                         memcpy(char_info->char_value, value, value_length);
232                                         char_info->value_length = value_length;
233                                 }
234                                 return;
235                         }
236                 }
237         }
238         return;
239 }
240
241 int add_new_characteristic(const char *char_uuid, bt_gatt_permission_t perms,
242                 bt_gatt_characteristic_property_t props, char **obj_path)
243 {
244         int ret = BLUETOOTH_ERROR_NONE;
245         struct otp_char_info *char_info = NULL;
246
247         ret = bluetooth_gatt_add_new_characteristic(otp_obj_path,
248                                         char_uuid, perms, props, obj_path);
249         if (ret != BLUETOOTH_ERROR_NONE) {
250                 BT_ERR("Failed to add new char %d", ret);
251                 return ret;
252         }
253
254         char_info = g_new0(struct otp_char_info, 1);
255         char_info->char_path = g_strdup(*obj_path);
256         otp_char_list = g_slist_append(otp_char_list, char_info);
257
258         return ret;
259 }
260
261 static char *_otp_convert_uuid_to_uuid128(const char *uuid)
262 {
263         int len;
264         char *uuid128;
265
266         len = strlen(uuid);
267
268         switch (len) {
269         case 4:
270                 /* UUID 16bits */
271                 uuid128 = g_strdup_printf("0000%s-0000-1000-8000-00805f9b34fb",
272                                                                         uuid);
273                 break;
274
275         case 8:
276                 /* UUID 32bits */
277                 uuid128 = g_strdup_printf("%s-0000-1000-8000-00805f9b34fb",
278                                                                         uuid);
279                 break;
280
281         case 36:
282                 /* UUID 128bits */
283                 uuid128 = strdup(uuid);
284                 break;
285
286         default:
287                 return NULL;
288         }
289
290         return uuid128;
291 }
292
293 int _bt_otp_prepare_ots(void)
294 {
295         BT_DBG("+");
296         int ret = BLUETOOTH_ERROR_NONE;
297         char *service_uuid;
298         char *char_uuid;
299         char *desc_uuid;
300         bt_gatt_characteristic_property_t props;
301         bt_gatt_permission_t perms;
302         char supp_feat[OTP_FEATURE_LENGTH] = { 0x3B, 0x00, 0x00, 0x00,
303                                                 0x01, 0x00, 0x00, 0x00 };
304
305         ret = bluetooth_gatt_init();
306         if (ret != BLUETOOTH_ERROR_NONE) {
307                 BT_ERR("Failed to Init GATT %d", ret);
308                 goto fail;
309         }
310
311         service_uuid = _otp_convert_uuid_to_uuid128(OTP_UUID);
312         ret = bluetooth_gatt_add_service(service_uuid, &otp_obj_path);
313         if (ret != BLUETOOTH_ERROR_NONE) {
314                 BT_ERR("Failed to add service %d", ret);
315                 goto fail;
316         }
317
318         /* Characteristic OTP Feature */
319         props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ;
320         perms = BLUETOOTH_GATT_PERMISSION_READ;
321         char_uuid = _otp_convert_uuid_to_uuid128(OTP_FEATURE_UUID);
322         ret = add_new_characteristic(char_uuid, perms, props,
323                                                 &otp_feature_obj_path);
324         if (ret != BLUETOOTH_ERROR_NONE)
325                 goto fail;
326
327         ret = bluetooth_gatt_set_characteristic_value(otp_feature_obj_path,
328                                                 supp_feat, OTP_FEATURE_LENGTH);
329         if (ret != BLUETOOTH_ERROR_NONE) {
330                 BT_ERR("Failed to set char value %d", ret);
331                 return ret;
332         }
333
334         _bt_otp_set_char_value(otp_feature_obj_path, supp_feat,
335                                                 OTP_FEATURE_LENGTH);
336
337         /* Characteristic Object Name */
338         props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ |
339                 BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE;
340         perms = BLUETOOTH_GATT_PERMISSION_READ |
341                 BLUETOOTH_GATT_PERMISSION_WRITE;
342         char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_NAME_UUID);
343         ret = add_new_characteristic(char_uuid, perms, props,
344                                         &otp_object_name_obj_path);
345         if (ret != BLUETOOTH_ERROR_NONE)
346                 goto fail;
347
348         /* Characteristic Object Type */
349         props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ;
350         perms = BLUETOOTH_GATT_PERMISSION_READ;
351         char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_TYPE_UUID);
352         ret = add_new_characteristic(char_uuid, perms, props,
353                                         &otp_object_type_obj_path);
354         if (ret != BLUETOOTH_ERROR_NONE)
355                 goto fail;
356
357         /* Characteristic Object Size */
358         props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ;
359         perms = BLUETOOTH_GATT_PERMISSION_READ;
360         char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_SIZE_UUID);
361         ret = add_new_characteristic(char_uuid, perms, props,
362                                         &otp_object_size_obj_path);
363         if (ret != BLUETOOTH_ERROR_NONE)
364                 goto fail;
365
366         /* Characteristic Object First-Created */
367         props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ |
368                 BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE;
369         perms = BLUETOOTH_GATT_PERMISSION_READ |
370                 BLUETOOTH_GATT_PERMISSION_WRITE;
371         char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_FIRST_CREATED_UUID);
372         ret = add_new_characteristic(char_uuid, perms, props,
373                                         &otp_object_first_created_obj_path);
374         if (ret != BLUETOOTH_ERROR_NONE)
375                 goto fail;
376
377         /* Characteristic Object Last-Modified */
378         props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ |
379                 BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE;
380         perms = BLUETOOTH_GATT_PERMISSION_READ |
381                 BLUETOOTH_GATT_PERMISSION_WRITE;
382         char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_LAST_MODIFIED_UUID);
383         ret = add_new_characteristic(char_uuid, perms, props,
384                                 &otp_object_last_modified_obj_path);
385         if (ret != BLUETOOTH_ERROR_NONE)
386                 goto fail;
387
388         /* Object ID is mandatory for mutiple object server */
389         if (mutiple_obj_support) {
390                 /* Characteristic Object ID */
391                 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ;
392                 perms = BLUETOOTH_GATT_PERMISSION_READ;
393                 char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_ID_UUID);
394                 ret = add_new_characteristic(char_uuid, perms, props,
395                                                 &otp_object_id_obj_path);
396                 if (ret != BLUETOOTH_ERROR_NONE)
397                         goto fail;
398         }
399
400         /* Characteristic Object Properties */
401         props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ |
402                         BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE;
403         perms = BLUETOOTH_GATT_PERMISSION_READ |
404                         BLUETOOTH_GATT_PERMISSION_WRITE;
405         char_uuid = _otp_convert_uuid_to_uuid128(OTP_OBJECT_PROP_UUID);
406         ret = add_new_characteristic(char_uuid, perms, props,
407                                         &otp_object_prop_obj_path);
408         if (ret != BLUETOOTH_ERROR_NONE)
409                 goto fail;
410
411         /* Characteristic OACP */
412         props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE |
413                 BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_INDICATE;
414         perms = BLUETOOTH_GATT_PERMISSION_WRITE;
415         char_uuid = _otp_convert_uuid_to_uuid128(OTP_OACP_UUID);
416         ret = add_new_characteristic(char_uuid, perms, props,
417                                                 &otp_oacp_obj_path);
418         if (ret != BLUETOOTH_ERROR_NONE)
419                 goto fail;
420
421         /* CCCD for OACP */
422         desc_uuid = _otp_convert_uuid_to_uuid128(OTP_CP_CCC_DESC_UUID);
423         perms = BLUETOOTH_GATT_PERMISSION_READ |
424                 BLUETOOTH_GATT_PERMISSION_WRITE;
425         ret = bluetooth_gatt_add_descriptor(otp_oacp_obj_path, desc_uuid,
426                                                 perms, &otp_oacp_desc_obj_path);
427         if (ret != BLUETOOTH_ERROR_NONE) {
428                 BT_ERR("Failed to add new char descriptor %d", ret);
429                 goto fail;
430         }
431
432         /* OLCP Characteristics is not required
433          * for single object server
434          */
435         if (mutiple_obj_support) {
436                 /* Characteristic OLCP */
437                 props = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE |
438                         BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_INDICATE;
439                 perms = BLUETOOTH_GATT_PERMISSION_WRITE;
440                 char_uuid = _otp_convert_uuid_to_uuid128(OTP_OLCP_UUID);
441                 ret = add_new_characteristic(char_uuid, perms, props,
442                                                         &otp_olcp_obj_path);
443                 if (ret != BLUETOOTH_ERROR_NONE)
444                         goto fail;
445
446                 /* CCCD for OLCP */
447                 desc_uuid = _otp_convert_uuid_to_uuid128(OTP_CP_CCC_DESC_UUID);
448                 perms = BLUETOOTH_GATT_PERMISSION_READ |
449                         BLUETOOTH_GATT_PERMISSION_WRITE;
450                 ret = bluetooth_gatt_add_descriptor(otp_olcp_obj_path, desc_uuid,
451                                                         perms, &otp_olcp_desc_obj_path);
452                 if (ret != BLUETOOTH_ERROR_NONE) {
453                         BT_ERR("Failed to add new char descriptor %d", ret);
454                         goto fail;
455                 }
456         }
457
458         /* Register service */
459         ret = bluetooth_gatt_register_service(otp_obj_path);
460         if (ret != BLUETOOTH_ERROR_NONE) {
461                 BT_ERR("Failed to register service %d", ret);
462                 goto fail;
463         }
464
465         /* Register Application */
466         ret = bluetooth_gatt_register_application();
467         if (ret != BLUETOOTH_ERROR_NONE) {
468                 BT_ERR("Failed to register application %d", ret);
469                 goto fail;
470         }
471
472         BT_DBG("-");
473         return ret;
474
475 fail:
476         delete_all_characterisitc();
477         return ret;
478 }
479
480 int _bt_otp_set_advertising_data(void)
481 {
482         int ret;
483         BT_DBG("");
484
485         /* OTP UUID */
486         guint8 data[4]  = {0x03, 0x02, 0x25, 0x18};
487         bluetooth_advertising_data_t adv;
488
489         BT_DBG("%x %x %x %x", data[0], data[1], data[2], data[3]);
490         memcpy(adv.data, data, sizeof(data));
491         ret = bluetooth_set_advertising_data(adv_handle, &adv, sizeof(data));
492         if (ret != BLUETOOTH_ERROR_NONE) {
493                 BT_ERR("Failed to set ADV data %d", ret);
494                 return ret;
495         }
496
497         ret = bluetooth_set_advertising(adv_handle, TRUE);
498         if (ret != BLUETOOTH_ERROR_NONE) {
499                 BT_ERR("Failed to set ADV %d", ret);
500                 return ret;
501         }
502
503         return 0;
504 }
505
506 void _bt_otp_start_write_on_fd()
507 {
508         char buf[BT_L2CAP_BUFFER_LEN];
509         int written;
510         int read;
511         int len;
512         FILE *fp;
513         char file_path[BT_FILE_PATH_MAX_LEN] = {0, };
514         int length;
515
516         snprintf(file_path, sizeof(file_path), "%s%s",
517                                         directory, selected_object->name);
518         BT_DBG("file_path = [%s]", file_path);
519
520         fp = fopen(file_path, "r");
521         if (!fp) {
522                 BT_DBG("fopen() failed : %s", strerror(errno));
523                 return;
524         }
525
526         BT_DBG("length [%d]", oacp_op->length);
527         length = oacp_op->length;
528
529         while (length > 0) {
530                 if (length < BT_L2CAP_BUFFER_LEN)
531                         len = length;
532                 else
533                         len = BT_L2CAP_BUFFER_LEN;
534
535                 read = fread(buf, 1, len, fp);
536                 written = write(oacp_op->fd, buf, len);
537
538                 if (written < 0)
539                         goto fail;
540
541                 length -= written;
542
543                 BT_DBG("read [%d], written [%d], rem_len [%d]",
544                                                 read, written, length);
545         }
546 fail:
547         fclose(fp);
548 }
549
550
551 static bool __bt_otc_connection_timeout_cb(gpointer user_data)
552 {
553         int err = BLUETOOTH_ERROR_NONE;
554         char *remote_addr = oacp_op->remote_address;
555
556         err = _bt_otp_open_otc_and_listen(remote_addr, "DisconnectOtc");
557         if (err != BLUETOOTH_ERROR_NONE)
558                 BT_ERR("Disconnect OTC failed");
559
560         return TRUE;
561 }
562
563 static gboolean __server_data_received_cb(GIOChannel *chan, GIOCondition cond,
564                                                                 gpointer data)
565 {
566         char *remote_addr = oacp_op->remote_address;
567         GIOStatus status = G_IO_STATUS_NORMAL;
568         GError *err = NULL;
569         char *buffer = NULL;
570         gsize len = 0;
571         int written;
572         int fd;
573
574         BT_DBG("");
575
576         fd = g_io_channel_unix_get_fd(chan);
577         if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
578                 otc_connection_status = FALSE;
579                 BT_ERR("OTC disconnected: %d", fd);
580                 close(fd);
581                 g_source_remove(server_watch_id);
582                 return FALSE;
583         }
584
585         buffer = g_malloc0(BT_L2CAP_BUFFER_LEN + 1);
586
587         status = g_io_channel_read_chars(chan, buffer,
588                                                         BT_L2CAP_BUFFER_LEN,
589                                                         &len, &err);
590         if (status != G_IO_STATUS_NORMAL) {
591                 BT_ERR("IO Channel read is failed with %d", status);
592
593                 g_free(buffer);
594                 if (err) {
595                         otc_connection_status = FALSE;
596                         BT_ERR("IO Channel read error [%s]", err->message);
597                         if (status == G_IO_STATUS_ERROR) {
598                                 BT_ERR("cond : %d", cond);
599                                 g_error_free(err);
600                                 close(fd);
601                                 g_source_remove(server_watch_id);
602                                 return FALSE;
603                         }
604                         g_error_free(err);
605                 }
606                 return TRUE;
607         }
608
609         BT_DBG("Received data length %d, remote_addr = %s", len, remote_addr);
610
611         if (!oacp_op->fp) {
612                 char file_path[BT_FILE_PATH_MAX_LEN] = {0, };
613                 FILE *fp = NULL;
614
615                 if (!selected_object) {
616                         BT_DBG("Object not selected");
617                         goto fail;
618                 }
619
620                 snprintf(file_path, sizeof(file_path), "%s%s",
621                                                 directory, selected_object->name);
622
623                 BT_DBG("file_path = [%s]", file_path);
624                 fp = fopen(file_path, "w");
625                 if (!fp) {
626                         BT_DBG("fopen() failed : %s", strerror(errno));
627                         goto fail;
628                 }
629                 oacp_op->fp = fp;
630         }
631
632         if (oacp_op->length_sofar <= oacp_op->length) {
633                 written = fwrite(buffer, 1, len, oacp_op->fp);
634                 oacp_op->length_sofar += written;
635                 BT_DBG("written [%d], length_sofar [%lu], received_buff_len [%d], size [%lu]",
636                                         written, oacp_op->length_sofar, len, oacp_op->length);
637         }
638
639         if (timeout_id > 0) {
640                 g_source_remove(timeout_id);
641                 timeout_id = g_timeout_add(BT_OACP_MAX_TIMEOUT,
642                         (GSourceFunc)__bt_otc_connection_timeout_cb, NULL);
643         }
644 fail:
645         g_free(buffer);
646         return TRUE;
647 }
648
649 static void _bt_otp_start_read_on_fd()
650 {
651         GIOChannel *data_io;
652         data_io = g_io_channel_unix_new(oacp_op->fd);
653
654         g_io_channel_set_encoding(data_io, NULL, NULL);
655         g_io_channel_set_flags(data_io, G_IO_FLAG_NONBLOCK, NULL);
656
657         server_watch_id = g_io_add_watch(data_io,
658                 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
659                 __server_data_received_cb, NULL);
660
661         if (timeout_id > 0)
662                         g_source_remove(timeout_id);
663
664         timeout_id = g_timeout_add(BT_OACP_MAX_TIMEOUT,
665                 (GSourceFunc)__bt_otc_connection_timeout_cb, NULL);
666 }
667
668 static void _bt_otp_method(GDBusConnection *connection,
669                 const gchar *sender,
670                 const gchar *object_path,
671                 const gchar *interface_name,
672                 const gchar *method_name,
673                 GVariant *parameters,
674                 GDBusMethodInvocation *invocation,
675                 gpointer user_data)
676 {
677         BT_DBG("+");
678         int status = BLUETOOTH_ERROR_NONE;
679
680         BT_DBG("Method[%s] Object Path[%s] Interface Name[%s]",
681                         method_name, object_path, interface_name);
682
683         if (g_strcmp0(method_name, "enable") == 0) {
684                 GDir *dir = NULL;
685                 GError *error = NULL;
686                 const gchar *filename = NULL;
687                 char absolute_path[BT_FILE_PATH_MAX_LEN];
688                 GSList *list = NULL, *l = NULL;
689                 struct stat st;
690                 struct object_metadata *object = NULL;
691
692                 g_variant_get(parameters, "(s)", &directory);
693                 BT_DBG("Directory = [%s]", directory);
694
695                 dir = g_dir_open(directory, 0, &error);
696                 if (!dir) {
697                         BT_ERR("Failed to open directory: %s", error->message);
698                         g_error_free(error);
699                         status = BLUETOOTH_ERROR_INVALID_DIRECTORY;
700                         goto fail;
701                 }
702
703                 while ((filename = g_dir_read_name(dir))) {
704                         list = g_slist_append(list, (gpointer) filename);
705                 }
706
707                 g_dir_close(dir);
708
709                 if (!list) {
710                         BT_DBG("No object found in given directory");
711                         status = BLUETOOTH_ERROR_NO_OBJECTS_FOUND;
712                         goto fail;
713                 }
714
715                 if (g_slist_length(list) > 1)
716                         mutiple_obj_support = true;
717
718                 for (l = list; l != NULL; l = l->next) {
719                         if (!l->data) continue;
720                         snprintf(absolute_path, sizeof(absolute_path), "%s%s", directory,
721                                                         (char *)l->data);
722
723                         BT_INFO("filename: %s, absoulte_path: %s",
724                                         (char *)l->data, absolute_path);
725
726                         if (stat(absolute_path, &st) == -1) {
727                                 BT_INFO("stat failed: (%d)\n", errno);
728                                 continue;
729                         }
730
731                         object = g_new0(struct object_metadata, 1);
732
733                         object->name = g_strdup((const gchar *)l->data);
734                         object->type = _otp_convert_uuid_to_uuid128(UNSUPPORTED_OBJECT_TYPE_UUID);
735                         object->first_created = st.st_ctime;
736                         object->last_modified = st.st_ctime;
737                         object->curr_size = (uint32_t) st.st_size;
738                         object->alloc_size = (uint32_t) st.st_size;
739                         object->id = object_id;
740                         object->props = OBJECT_READ | OBJECT_WRITE |
741                                         OBJECT_EXECUTE | OBJECT_DELETE;
742
743                         otp_object_list = g_slist_append(otp_object_list,
744                                                                 object);
745
746                         object_id++;
747                 }
748
749                 BT_DBG("preparing");
750                 if (_bt_otp_prepare_ots() != BLUETOOTH_ERROR_NONE) {
751                         BT_ERR("Fail to prepare OTP Proxy");
752                         status = BLUETOOTH_ERROR_INTERNAL;
753                         goto fail;
754                 }
755
756                 /* If single object is supported, make that as
757                  * selected object and update the metadata for the same.
758                  */
759                 if (!mutiple_obj_support) {
760                         BT_INFO("Server supports single object");
761                         selected_object = (struct object_metadata *) g_slist_nth_data(otp_object_list, 0);
762                         if (selected_object)
763                                 update_obj_metadata_charc_value(selected_object);
764                 }
765
766                 BT_DBG("advertsing");
767                 if (_bt_otp_set_advertising_data() != BLUETOOTH_ERROR_NONE) {
768                         BT_ERR("Fail to set advertising data");
769                         status = BLUETOOTH_ERROR_INTERNAL;
770                         goto fail;
771                 }
772 fail:
773                 g_dbus_method_invocation_return_value(invocation,
774                                                 g_variant_new("(i)", status));
775
776         } else if (g_strcmp0(method_name, "disable") == 0) {
777                 g_dbus_method_invocation_return_value(invocation,
778                                                 g_variant_new("(i)", status));
779                 _bt_otp_exit();
780
781         } else if (g_strcmp0(method_name, "NewConnection") == 0) {
782                 int index;
783                 GDBusMessage *msg;
784                 GUnixFDList *fd_list;
785                 char *dev_path;
786                 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
787                 int fd;
788
789                 g_variant_get(parameters, "(oh)", &dev_path, &index);
790
791                 msg = g_dbus_method_invocation_get_message(invocation);
792                 fd_list = g_dbus_message_get_unix_fd_list(msg);
793                 if (fd_list == NULL) {
794                         BT_ERR("fd_list is NULL");
795                         return;
796                 }
797
798                 fd = g_unix_fd_list_get(fd_list, index, NULL);
799                 if (fd == -1) {
800                         BT_ERR("Invalid fd return");
801                         return;
802                 }
803
804                 _bt_convert_device_path_to_address(dev_path, address);
805
806                 BT_INFO("OTC Connected fd: %d, address %s", fd, address);
807                 otc_connection_status = TRUE;
808                 if (oacp_op) {
809                         oacp_op->fd = fd;
810
811                         if (oacp_op->opcode == OACP_READ)
812                                 _bt_otp_start_write_on_fd();
813                         else if (oacp_op->opcode == OACP_WRITE)
814                                 _bt_otp_start_read_on_fd();
815                 }
816                 g_dbus_method_invocation_return_value(invocation, NULL);
817         }
818         BT_DBG("-");
819 }
820
821 static const GDBusInterfaceVTable otp_method_table = {
822         _bt_otp_method,
823         NULL,
824         NULL,
825 };
826
827 static void _bt_otp_on_bus_acquired(GDBusConnection *connection,
828                                 const gchar *name, gpointer user_data)
829 {
830         guint object_id;
831         GError *error = NULL;
832
833         BT_DBG("+");
834
835         g_conn = connection;
836
837         object_id = g_dbus_connection_register_object(connection,
838                                                 BT_OTP_OBJECT_PATH,
839                                                 otp_node_info->interfaces[0],
840                                                 &otp_method_table,
841                                                 NULL, NULL, &error);
842         if (object_id == 0) {
843                 BT_ERR("Failed to register method table: %s", error->message);
844                 g_error_free(error);
845                 g_dbus_node_info_unref(otp_node_info);
846         }
847
848         BT_DBG("-");
849 }
850
851 static void _bt_otp_on_name_acquired(GDBusConnection *connection,
852                                         const gchar     *name,
853                                         gpointer user_data)
854 {
855         BT_DBG("");
856 }
857
858 static void _bt_otp_on_name_lost(GDBusConnection *connection,
859                                 const gchar     *name,
860                                 gpointer user_data)
861 {
862         BT_DBG("");
863         g_object_unref(g_conn);
864         g_conn = NULL;
865         g_dbus_node_info_unref(otp_node_info);
866         g_bus_unown_name(g_owner_id);
867 }
868
869 int _bt_otp_register_interface(void)
870 {
871         BT_DBG("+");
872         GError *error = NULL;
873         guint owner_id;
874
875         otp_node_info = g_dbus_node_info_new_for_xml(otp_introspection_xml, &error);
876         if (!otp_node_info) {
877                 BT_ERR("Failed to install: %s", error->message);
878                 return BLUETOOTH_ERROR_INTERNAL;
879         }
880
881         owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
882                                 BT_OTP_SERVICE_NAME,
883                                 G_BUS_NAME_OWNER_FLAGS_NONE,
884                                 _bt_otp_on_bus_acquired,
885                                 _bt_otp_on_name_acquired,
886                                 _bt_otp_on_name_lost,
887                                 NULL, NULL);
888         g_owner_id = owner_id;
889         BT_DBG("owner_id is [%d]\n", owner_id);
890
891         BT_DBG("-");
892         return BLUETOOTH_ERROR_NONE;
893 }
894
895 void _bt_otp_unregister_interface(void)
896 {
897         BT_DBG("+");
898
899         g_object_unref(g_conn);
900         g_conn = NULL;
901         g_dbus_node_info_unref(otp_node_info);
902         g_bus_unown_name(g_owner_id);
903
904         BT_DBG("-");
905         return;
906 }
907
908 void _bt_convert_device_path_to_address(const char *device_path,
909                                                 char *device_address)
910 {
911         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
912         char *dev_addr;
913
914         dev_addr = strstr(device_path, "dev_");
915         if (dev_addr != NULL) {
916                 char *pos = NULL;
917                 dev_addr += 4;
918                 g_strlcpy(address, dev_addr, sizeof(address));
919
920                 while ((pos = strchr(address, '_')) != NULL)
921                         *pos = ':';
922
923                 g_strlcpy(device_address, address, BT_ADDRESS_STRING_SIZE);
924         }
925 }
926
927 static char *__bt_extract_device_path(GVariantIter *iter, char *address)
928 {
929         char *object_path = NULL;
930         char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
931
932         /* Parse the signature: oa{sa{sv}}} */
933         while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path,
934                         NULL)) {
935                 if (!object_path) {
936                         BT_ERR("Unable to get object path");
937                         return NULL;
938                 }
939                 _bt_convert_device_path_to_address(object_path, device_address);
940                 if (g_strcmp0(address, device_address) == 0)
941                         return g_strdup(object_path);
942
943         }
944
945         BT_ERR("Unable to get object path");
946         return NULL;
947 }
948
949 char *_bt_otp_get_device_object_path(char *address)
950 {
951         GError *err = NULL;
952         GDBusProxy *proxy = NULL;
953         GVariant *result = NULL;
954         GVariantIter *iter = NULL;
955         char *object_path = NULL;
956
957         proxy =  g_dbus_proxy_new_sync(conn,
958                         G_DBUS_PROXY_FLAGS_NONE, NULL,
959                         BT_BLUEZ_NAME,
960                         BT_MANAGER_PATH,
961                         BT_MANAGER_INTERFACE,
962                         NULL, &err);
963
964         if (!proxy) {
965                 BT_ERR("Unable to create proxy: %s", err->message);
966                 goto fail;
967         }
968
969         result = g_dbus_proxy_call_sync(proxy, "GetManagedObjects", NULL,
970                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
971         if (!result) {
972                 if (err != NULL)
973                         BT_ERR("Fail to get GetManagedObjects (Error: %s)", err->message);
974                 else
975                         BT_ERR("Fail to get GetManagedObjects");
976
977                 goto fail;
978         }
979
980         g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
981         object_path = __bt_extract_device_path(iter, address);
982
983         g_variant_unref(result);
984         g_variant_iter_free(iter);
985
986 fail:
987         if (err)
988                 g_clear_error(&err);
989
990         if (proxy)
991                 g_object_unref(proxy);
992
993         return object_path;
994 }
995
996 int _bt_otp_open_otc_and_listen(char *address, char *method)
997 {
998         char *object_path;
999         GDBusProxy *device_proxy = NULL;
1000         GVariant *result = NULL;
1001         GError *error = NULL;
1002         int ret = BLUETOOTH_ERROR_NONE;
1003
1004         if (method == NULL)
1005                 return BLUETOOTH_ERROR_INTERNAL;
1006
1007         if (g_strcmp0(method, "ListenOtc") &&
1008                         g_strcmp0(method, "DisconnectOtc"))
1009                 return BLUETOOTH_ERROR_INTERNAL;
1010
1011         object_path = _bt_otp_get_device_object_path(address);
1012         if (object_path == NULL) {
1013                 ret = BLUETOOTH_ERROR_NOT_PAIRED;
1014                 goto fail;
1015         }
1016
1017         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1018                                         NULL, BT_BLUEZ_NAME, object_path,
1019                                         BT_DEVICE_INTERFACE,  NULL, NULL);
1020         if (device_proxy == NULL) {
1021                 ret = BLUETOOTH_ERROR_INTERNAL;
1022                 goto fail;
1023         }
1024
1025
1026         result = g_dbus_proxy_call_sync(device_proxy, method,
1027                                 NULL,
1028                                 G_DBUS_CALL_FLAGS_NONE,
1029                                 -1,
1030                                 NULL,
1031                                 &error);
1032         if (result == NULL) {
1033                 if (error != NULL) {
1034                         BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1035                         g_error_free(error);
1036                 }
1037                 ret = BLUETOOTH_ERROR_INTERNAL;
1038         }
1039 fail:
1040         if (object_path)
1041                 g_free(object_path);
1042         if (result)
1043                 g_variant_unref(result);
1044         if (device_proxy)
1045                 g_object_unref(device_proxy);
1046         return ret;
1047 }
1048
1049 static bool __bt_oacp_create_timeout_cb(gpointer user_data)
1050 {
1051         if (oacp_create) {
1052                 g_free(oacp_create->uuid);
1053                 g_free(oacp_create);
1054                 oacp_create = NULL;
1055         }
1056         return TRUE;
1057 }
1058
1059 static void _bt_otp_free_oacp_op()
1060 {
1061         if (timeout_id > 0) {
1062                 g_source_remove(timeout_id);
1063                 timeout_id = 0;
1064         }
1065
1066         if (oacp_op) {
1067                 g_free(oacp_op->remote_address);
1068                 if (oacp_op->fp)
1069                         fclose(oacp_op->fp);
1070                 g_free(oacp_op);
1071                 oacp_op = NULL;
1072         }
1073 }
1074
1075 int _bt_otp_send_launch_request(char *absolute_path)
1076 {
1077         void *handle;
1078         char *error;
1079         int ret;
1080
1081         /* check ARCH 64 or 32*/
1082         if (!access(FILEPATH_ARCH_64, 0)) {
1083                 BT_INFO("plugin loading for ARCH 64");
1084                 handle = dlopen(HEADED_PLUGIN_FILEPATH64, RTLD_NOW);
1085         } else {
1086                 BT_INFO("plugin loading for ARCH 32");
1087                 handle = dlopen(HEADED_PLUGIN_FILEPATH, RTLD_NOW);
1088         }
1089
1090         if (!handle) {
1091                 BT_ERR("Can not load plugin %s", dlerror());
1092                 return BLUETOOTH_ERROR_INTERNAL;
1093         }
1094
1095         dlerror();      /* Clear any existing error */
1096
1097         int (*fun)(char *) = (int (*)(char *))dlsym(handle,
1098                                         "bt_app_control_send_launch_request");
1099
1100         if ((error = dlerror()) != NULL)  {
1101                 BT_ERR("Can not load symbol : %s", dlerror());
1102                 dlclose(handle);
1103                 return BLUETOOTH_ERROR_INTERNAL;
1104         }
1105
1106         ret = fun(absolute_path);
1107         dlclose(handle);
1108
1109         return ret;
1110 }
1111
1112 int _bt_otp_oacp_write_cb(char *value, int len, int offset,
1113                                                         char *remote_addr, struct indicate_info *info)
1114 {
1115         int ret = OACP_SUCCESS;
1116         int err = BLUETOOTH_ERROR_NONE;
1117         int opcode = value[0];
1118         uint32_t object_offset, length, object_size;
1119         uint8_t mode = 0;
1120         char *uuid;
1121         char absolute_file_path[BT_FILE_PATH_MAX_LEN] = {0, };
1122
1123         BT_INFO("OACP Opcode 0x%d", opcode);
1124
1125         if (!selected_object) {
1126                 BT_DBG("Object not selected");
1127                 ret = OACP_INVALID_OBJ;
1128                 goto fail;
1129         }
1130
1131         switch (opcode) {
1132         case OACP_CREATE:
1133                 BT_INFO("OACP_CREATE");
1134                 object_size = (uint32_t)(value[4] & 0xFF) << 24 |
1135                                 (uint32_t)(value[3] & 0xFF) << 16 |
1136                                 (uint32_t)(value[2] & 0xFF) << 8  |
1137                                 (uint32_t)(value[1] & 0xFF);
1138
1139                 uuid = g_strndup(value + 5, len - 5);
1140                 BT_INFO("Size = %lu, UUID = %s", object_size, uuid);
1141
1142                 if (oacp_create) {
1143                         /* Create operation already going on. */
1144                         ret = OACP_OPERATION_FAILED;
1145                         goto fail;
1146                 }
1147                 oacp_create = g_malloc0(sizeof(struct oacp_create_operation));
1148                 oacp_create->size = object_size;
1149                 oacp_create->uuid = g_strdup(uuid);
1150                 if (timeout_id > 0)
1151                         g_source_remove(timeout_id);
1152                 timeout_id = g_timeout_add(BT_OACP_MAX_TIMEOUT,
1153                         (GSourceFunc)__bt_oacp_create_timeout_cb, NULL);
1154                 g_free(uuid);
1155                 break;
1156         case OACP_DELETE:
1157                 if (opcode == OACP_DELETE &&
1158                                 ~(selected_object->props & OBJECT_DELETE)) {
1159                         ret = OACP_PROCEDURE_NOT_SUPPORTED;
1160                         goto fail;
1161                 }
1162                 snprintf(absolute_file_path, sizeof(absolute_file_path),
1163                                                 "%s%s", directory, selected_object->name);
1164
1165                 BT_DBG("absolute_file_path = [%s]", absolute_file_path);
1166
1167                 if (remove(absolute_file_path) != 0) {
1168                         BT_DBG("Error: unable to delete the file");
1169                         ret = OACP_OPERATION_FAILED;
1170                         goto fail;
1171                 }
1172
1173                 BT_DBG("File deleted successfully");
1174                 selected_object = NULL;
1175                 break;
1176         case OACP_CALC_CHECKSUM:
1177                 ret = OACP_OPCODE_NOT_SUPPORTED;
1178                 break;
1179         case OACP_EXECUTE:
1180                 if (opcode == OACP_EXECUTE &&
1181                                 ~(selected_object->props & OBJECT_EXECUTE)) {
1182                         ret = OACP_PROCEDURE_NOT_SUPPORTED;
1183                         goto fail;
1184                 }
1185                 snprintf(absolute_file_path, sizeof(absolute_file_path),
1186                                         "file://%s%s", directory, selected_object->name);
1187
1188                 BT_DBG("absolute_file_path = [%s]", absolute_file_path);
1189
1190                 err = _bt_otp_send_launch_request(absolute_file_path);
1191                 if (err != BLUETOOTH_ERROR_NONE) {
1192                         BT_DBG("Error: unable to launch the file");
1193                         ret = OACP_OPERATION_FAILED;
1194                         goto fail;
1195                 }
1196
1197                 BT_DBG("Successfully launched the file");
1198                 break;
1199         case OACP_READ:
1200         case OACP_WRITE:
1201                 if (opcode == OACP_WRITE &&
1202                                 ~(selected_object->props & OBJECT_WRITE)) {
1203                         ret = OACP_PROCEDURE_NOT_SUPPORTED;
1204                         goto fail;
1205                 }
1206
1207                 if (opcode == OACP_READ &&
1208                                 ~(selected_object->props & OBJECT_READ)) {
1209                         ret = OACP_PROCEDURE_NOT_SUPPORTED;
1210                         goto fail;
1211                 }
1212
1213                 object_offset = (uint32_t)(value[4] & 0xFF) << 24 |
1214                                 (uint32_t)(value[3] & 0xFF) << 16 |
1215                                 (uint32_t)(value[2] & 0xFF) << 8  |
1216                                 (uint32_t)(value[1] & 0xFF);
1217                 length = (uint32_t)(value[8] & 0xFF) << 24 |
1218                         (uint32_t)(value[7] & 0xFF) << 16 |
1219                         (uint32_t)(value[6] & 0xFF) << 8  |
1220                         (uint32_t)(value[5] & 0xFF);
1221
1222                 if (opcode == OACP_WRITE)
1223                         mode = (uint8_t)value[9] & 0xFF;
1224
1225                 BT_INFO("Offset = %lu, Length = %lu", object_offset, length, mode);
1226
1227                 if (oacp_op) {
1228                         if (otc_connection_status) {
1229                                 /* Read/Write operation already going on. */
1230                                 ret = OACP_OBJECT_LOCKED;
1231                                 goto fail;
1232                         }
1233                         _bt_otp_free_oacp_op();
1234                 }
1235
1236                 oacp_op = g_malloc0(sizeof(struct oacp_operation));
1237                 oacp_op->offset = object_offset;
1238                 oacp_op->length = length;
1239                 oacp_op->remote_address = g_strdup(remote_addr);
1240                 oacp_op->mode = mode;
1241                 oacp_op->opcode = opcode;
1242                 oacp_op->length_sofar = 0;
1243                 oacp_op->fp = NULL;
1244
1245                 err = _bt_otp_open_otc_and_listen(remote_addr, "ListenOtc");
1246                 if (err != BLUETOOTH_ERROR_NONE) {
1247                         ret = OACP_CHANNEL_UNAVAILABLE;
1248                         _bt_otp_free_oacp_op();
1249                         goto fail;
1250                 }
1251                 break;
1252         case OACP_ABORT:
1253                 ret = OACP_OPCODE_NOT_SUPPORTED;
1254                 break;
1255         default:
1256                 ret = OACP_OPCODE_NOT_SUPPORTED;
1257                 break;
1258         }
1259 fail:
1260         info->resp_opcode = OACP_RESPONSE;
1261         info->req_opcode = opcode;
1262         info->result_code = ret;
1263         info->resp_param = NULL;
1264         return BLUETOOTH_ERROR_NONE;
1265 }
1266
1267 void convert_to_hex(struct object_metadata *object, char *metadata, char *value)
1268 {
1269         struct tm fc_tm;
1270
1271         BT_DBG("Metadata : %s", metadata);
1272
1273         memset(value, 0, 16);
1274
1275         if (!g_strcmp0(metadata, "type")) {
1276                 /* Convert UUID string to 128 bit UUID */
1277                 uint32_t data0, data4;
1278                 uint16_t data1, data2, data3, data5;
1279
1280                 if (!object->type || sscanf(object->type,
1281                                 "%08x-%04hx-%04hx-%04hx-%08x%04hx",
1282                                 &data0, &data1, &data2,
1283                                 &data3, &data4, &data5) != 6) {
1284                         BT_ERR("Object Type UUID not updated");
1285                         return;
1286                 }
1287
1288                 data0 = htonl(data0);
1289                 data1 = htons(data1);
1290                 data2 = htons(data2);
1291                 data3 = htons(data3);
1292                 data4 = htonl(data4);
1293                 data5 = htons(data5);
1294
1295                 memcpy(value, &data0, 4);
1296                 memcpy(value+4, &data1, 2);
1297                 memcpy(value+6, &data2, 2);
1298                 memcpy(value+8, &data3, 2);
1299                 memcpy(value+10, &data4, 4);
1300                 memcpy(value+14, &data5, 2);
1301
1302         } else if (!g_strcmp0(metadata, "size")) {
1303
1304                 value[3] = (object->curr_size >> 24) & 0xFF;
1305                 value[2] = (object->curr_size >> 16) & 0xFF;
1306                 value[1] = (object->curr_size >> 8) & 0xFF;
1307                 value[0] = object->curr_size & 0xFF;
1308
1309                 value[7] = (object->alloc_size >> 24) & 0xFF;
1310                 value[6] = (object->alloc_size >> 16) & 0xFF;
1311                 value[5] = (object->alloc_size >> 8) & 0xFF;
1312                 value[4] = object->alloc_size & 0xFF;
1313
1314         } else if (!g_strcmp0(metadata, "date")) {
1315
1316                 localtime_r(&(object->first_created), &fc_tm);
1317
1318                 value[1] = ((fc_tm.tm_year+1900) >> 8) & 0xFF;
1319                 value[0] = (fc_tm.tm_year+1900) & 0xFF;
1320                 value[2] = (fc_tm.tm_mon+1) & 0xFF;
1321                 value[3] = fc_tm.tm_mday & 0xFF;
1322                 value[4] = fc_tm.tm_hour & 0xFF;
1323                 value[5] = fc_tm.tm_min & 0xFF;
1324                 value[6] = fc_tm.tm_sec & 0xFF;
1325
1326         } else if (!g_strcmp0(metadata, "id")) {
1327
1328                 value[5] = (object->id >> 48) & 0xFF;
1329                 value[4] = (object->id >> 32) & 0xFF;
1330                 value[3] = (object->id >> 24) & 0xFF;
1331                 value[2] = (object->id >> 16) & 0xFF;
1332                 value[1] = (object->id >> 8) & 0xFF;
1333                 value[0] = object->id & 0xFF;
1334
1335         } else if (!g_strcmp0(metadata, "props")) {
1336                 value[3] = (object->props >> 24) & 0xFF;
1337                 value[2] = (object->props >> 16) & 0xFF;
1338                 value[1] = (object->props >> 8) & 0xFF;
1339                 value[0] = object->props & 0xFF;
1340         }
1341 }
1342
1343 void update_obj_metadata_charc_value(struct object_metadata *object)
1344 {
1345         /* Value can be of maximum 16 bytes */
1346         char value[16];
1347
1348         _bt_otp_set_char_value(otp_object_name_obj_path, object->name,
1349                                                         strlen(object->name));
1350
1351         convert_to_hex(object, "type", value);
1352         _bt_otp_set_char_value(otp_object_type_obj_path, value, 16);
1353
1354         convert_to_hex(object, "size", value);
1355         _bt_otp_set_char_value(otp_object_size_obj_path, value, 8);
1356
1357         convert_to_hex(object, "date", value);
1358         _bt_otp_set_char_value(otp_object_first_created_obj_path, value, 7);
1359         _bt_otp_set_char_value(otp_object_last_modified_obj_path, value, 7);
1360
1361         /* Object ID is optonal for single object server */
1362         if (mutiple_obj_support) {
1363                 convert_to_hex(object, "id", value);
1364                 _bt_otp_set_char_value(otp_object_id_obj_path, value, 6);
1365         }
1366
1367         convert_to_hex(object, "props", value);
1368         _bt_otp_set_char_value(otp_object_prop_obj_path, value, 4);
1369 }
1370
1371 struct object_metadata *_bt_otp_client_find_object(GSList *list, uint64_t id, guint *index)
1372 {
1373         GSList *l;
1374         struct object_metadata *info;
1375
1376         for (l = list; l; l = g_slist_next(l)) {
1377                 (*index)++;
1378                 info = l->data;
1379
1380                 if (info && (info->id == id))
1381                         return info;
1382         }
1383         return NULL;
1384 }
1385
1386 int _bt_otp_olcp_write_cb(char *value, int len, int offset,
1387                                         struct indicate_info *info)
1388 {
1389         int ret = OLCP_SUCCESS;
1390         int opcode = value[0];
1391         struct object_metadata *object;
1392         uint64_t object_id;
1393         guint index = 0;
1394
1395         BT_INFO("OLCP Opcode 0x%d", opcode);
1396
1397         if (!otp_object_list) {
1398                 ret = OLCP_NO_OBJ;
1399                 goto fail;
1400         }
1401
1402         switch (opcode) {
1403         case OLCP_FIRST:
1404                 object = (struct object_metadata *) g_slist_nth_data(otp_object_list, 0);
1405                 if (!object) {
1406                         ret = OLCP_OUT_OF_BOUNDS;
1407                         goto fail;
1408                 }
1409                 update_obj_metadata_charc_value(object);
1410                 selected_object = object;
1411                 obj_curr_index = 0;
1412                 break;
1413         case OLCP_LAST:
1414                 len = g_slist_length(otp_object_list);
1415                 object = (struct object_metadata *) g_slist_nth_data(otp_object_list, len-1);
1416                 if (!object) {
1417                         ret = OLCP_OUT_OF_BOUNDS;
1418                         goto fail;
1419                 }
1420                 update_obj_metadata_charc_value(object);
1421                 selected_object = object;
1422                 obj_curr_index = len-1;
1423                 break;
1424         case OLCP_PREVIOUS:
1425                 if (obj_curr_index == 0) {
1426                         ret = OLCP_OUT_OF_BOUNDS;
1427                         goto fail;
1428                 }
1429                 object = (struct object_metadata *) g_slist_nth_data(otp_object_list, obj_curr_index-1);
1430                 if (!object) {
1431                         ret = OLCP_OUT_OF_BOUNDS;
1432                         goto fail;
1433                 }
1434                 update_obj_metadata_charc_value(object);
1435                 selected_object = object;
1436                 obj_curr_index -= 1;
1437                 break;
1438         case OLCP_NEXT:
1439                 object = (struct object_metadata *) g_slist_nth_data(otp_object_list, obj_curr_index+1);
1440                 if (!object) {
1441                         ret = OLCP_OUT_OF_BOUNDS;
1442                         goto fail;
1443                 }
1444                 update_obj_metadata_charc_value(object);
1445                 selected_object = object;
1446                 obj_curr_index += 1;
1447                 break;
1448         case OLCP_GOTO:
1449                 object_id = (uint64_t)(value[6] & 0xFF) << 40 |
1450                                 (uint64_t)(value[5] & 0xFF) << 32 |
1451                                 (uint64_t)(value[4] & 0xFF) << 24 |
1452                                 (uint64_t)(value[3] & 0xFF) << 16 |
1453                                 (uint64_t)(value[2] & 0xFF) << 8  |
1454                                 (uint64_t)(value[1] & 0xFF);
1455                 BT_INFO("Object ID [%llu]", object_id);
1456                 if (selected_object && selected_object->id == object_id)
1457                         goto fail;
1458
1459                 object = _bt_otp_client_find_object(otp_object_list, object_id, &index);
1460                 if (!object) {
1461                         ret = OLCP_OJECT_ID_NOT_FOUND;
1462                         goto fail;
1463                 }
1464                 update_obj_metadata_charc_value(object);
1465                 selected_object = object;
1466                 obj_curr_index = index - 1;
1467                 break;
1468         case OLCP_ORDER:
1469         case OLCP_REQ_NO_OBJ:
1470         case OLCP_CLEAR_MARKING:
1471         default:
1472                 ret = OLCP_OPCODE_NOT_SUPPORTED;
1473                 break;
1474         }
1475 fail:
1476         info->resp_opcode = OLCP_RESPONSE;
1477         info->req_opcode = opcode;
1478         info->result_code = ret;
1479         info->resp_param = NULL;
1480         return BLUETOOTH_ERROR_NONE;
1481 }
1482
1483 int _bt_otp_obj_name_cb(char *value, int len)
1484 {
1485         int ret = BLUETOOTH_ERROR_NONE;
1486
1487         struct object_metadata *object = NULL;
1488         struct stat st;
1489         char *file_path;
1490         char *filename;
1491         int length;
1492         FILE *fp = NULL;
1493
1494         filename = g_strndup(value, len);
1495         length = len + strlen(BT_OTP_BASE_DIR_PATH) + 1;
1496         file_path = malloc(length);
1497
1498         snprintf(file_path, length, "%s%s",
1499                         BT_OTP_BASE_DIR_PATH, filename);
1500         BT_DBG("file_path = [%s]", file_path);
1501
1502         fp = fopen(file_path, "a");
1503         if (!fp) {
1504                 BT_DBG("fopen() failed : %s", strerror(errno));
1505                 ret = BLUETOOTH_ERROR_INTERNAL;
1506                 goto fail;
1507         }
1508
1509         if (stat(file_path, &st) == -1) {
1510                 BT_INFO("stat failed: (%d)\n", errno);
1511                 ret = BLUETOOTH_ERROR_INTERNAL;
1512                 goto fail;
1513         }
1514
1515         object = g_new0(struct object_metadata, 1);
1516
1517         object->name = g_strdup(filename);
1518         object->type = _otp_convert_uuid_to_uuid128(oacp_create->uuid);
1519         object->first_created = st.st_ctime;
1520         object->last_modified = st.st_ctime;
1521         object->curr_size = (uint32_t)st.st_size;
1522         object->alloc_size = oacp_create->size;
1523         object->id = object_id;
1524         object->props = OBJECT_READ | OBJECT_WRITE;
1525
1526         otp_object_list = g_slist_append(otp_object_list,
1527                                                 object);
1528
1529         update_obj_metadata_charc_value(object);
1530         selected_object = object;
1531         obj_curr_index = g_slist_length(otp_object_list) - 1;
1532         object_id++;
1533 fail:
1534         if (fp)
1535                 fclose(fp);
1536         g_free(filename);
1537         free(file_path);
1538         g_free(oacp_create->uuid);
1539         g_free(oacp_create);
1540         oacp_create = NULL;
1541
1542         return ret;
1543 }
1544
1545 static struct otp_char_info *otp_get_char_value(const char *path)
1546 {
1547         GSList *tmp = NULL;
1548
1549         for (tmp = otp_char_list; tmp != NULL; tmp = tmp->next) {
1550                 if (tmp->data) {
1551                         struct otp_char_info *char_info = tmp->data;
1552                         if (!g_strcmp0(char_info->char_path, path))
1553                                 return char_info;
1554                 }
1555         }
1556
1557         return NULL;
1558 }
1559
1560 int _bt_otp_read_cb(const char *obj_path, char **value, int *len)
1561 {
1562         struct otp_char_info *info = NULL;
1563
1564         if (!obj_path) {
1565                 BT_ERR("Wrong Obj path");
1566                 return BLUETOOTH_ERROR_INTERNAL;
1567         }
1568
1569         if (g_strcmp0(obj_path, otp_feature_obj_path)) {
1570                 if (!selected_object) {
1571                         return BLUETOOTH_ERROR_OBJECT_NOT_SELECTED;
1572                 }
1573         }
1574
1575         info = otp_get_char_value(obj_path);
1576         if (info) {
1577                 if (info->char_value == NULL || info->value_length == 0)
1578                         return BLUETOOTH_ERROR_INTERNAL;
1579
1580                 *len = info->value_length;
1581                 *value = (char *)malloc(sizeof(char)*(*len));
1582                 memcpy(*value, info->char_value, *len);
1583
1584                 return BLUETOOTH_ERROR_NONE;
1585         } else {
1586                 return BLUETOOTH_ERROR_INTERNAL;
1587         }
1588 }
1589
1590 static void _otp_convert_address_to_hex(bluetooth_device_address_t *addr_hex,
1591                                                         const char *addr_str)
1592 {
1593         int i = 0;
1594         unsigned int addr[BLUETOOTH_ADDRESS_LENGTH] = { 0, };
1595
1596         if (addr_str == NULL || addr_str[0] == '\0')
1597                 return;
1598
1599         i = sscanf(addr_str, "%X:%X:%X:%X:%X:%X", &addr[0], &addr[1],
1600                                 &addr[2], &addr[3], &addr[4], &addr[5]);
1601         if (i != BLUETOOTH_ADDRESS_LENGTH)
1602                 BT_ERR("Invalid format string - [%s]", addr_str);
1603
1604         for (i = 0; i < BLUETOOTH_ADDRESS_LENGTH; i++)
1605                 addr_hex->addr[i] = (unsigned char)addr[i];
1606 }
1607
1608 static void _bt_otp_send_indication(const char *obj_path,
1609                                 struct indicate_info *info,
1610                                 bluetooth_device_address_t *remote_address)
1611 {
1612         int ret = BLUETOOTH_ERROR_NONE;
1613         char value[7] = {0x00};
1614         int length = OTP_INDICATION_LEN_WITHOUT_RESP;
1615
1616         BT_DBG("");
1617
1618         value[0] = info->resp_opcode & 0xFF;
1619         value[1] = info->req_opcode & 0xFF;
1620         value[2] = info->result_code & 0xFF;
1621         if (info->resp_param) {
1622                 value[6] = (info->resp_param[3] >> 24) & 0xFF;
1623                 value[5] = (info->resp_param[4] >> 16) & 0xFF;
1624                 value[4] = (info->resp_param[5] >> 8) & 0xFF;
1625                 value[3] = info->resp_param[6] & 0xFF;
1626                 length = OTP_INDICATION_LEN_WITH_RESP;
1627         }
1628
1629         BT_DBG("Opcode: %d", value[1]);
1630
1631         /* Store the status value */
1632         _bt_otp_set_char_value(obj_path, value, length);
1633
1634         /* Send indication */
1635         ret = bluetooth_gatt_server_set_notification(obj_path, remote_address);
1636         if (ret != BLUETOOTH_ERROR_NONE) {
1637                 BT_ERR("_bt_otp_send_control_point_indication failed");
1638                 return;
1639         }
1640         ret = bluetooth_gatt_update_characteristic(obj_path, value, length);
1641         if (ret != BLUETOOTH_ERROR_NONE) {
1642                 BT_ERR("_bt_otp_send_control_point_indication failed");
1643                 return;
1644         }
1645 }
1646
1647 void _bt_otp_gatt_char_property_changed_event(GVariant *msg,
1648                                 const char *path)
1649 {
1650         int result = BLUETOOTH_ERROR_NONE;
1651         GVariantIter value_iter;
1652         const char *property = NULL;
1653         const char *char_path = NULL;
1654         const char *svc_handle = NULL;
1655         GVariant *var = NULL;
1656         GVariant *val = NULL;
1657         g_variant_iter_init(&value_iter, msg);
1658
1659         while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &var))) {
1660
1661                 if (property == NULL) {
1662                         BT_ERR("Property NULL");
1663                         return;
1664                 }
1665
1666                 if (!g_strcmp0(property, "WriteValue")) {
1667                         int len = 0;
1668                         BT_INFO("WriteValue");
1669                         BT_INFO("Type '%s'\n", g_variant_get_type_string(var));
1670
1671                         if (var) {
1672                                 bluetooth_device_address_t addr_hex = { {0,} };
1673                                 gchar *addr = NULL;
1674                                 guint8 req_id = 1;
1675                                 guint16 offset = 0;
1676                                 char *value = NULL;
1677                                 struct indicate_info info;
1678
1679                                 g_variant_get(var, "(&s&s&syq@ay)",
1680                                                 &char_path, &svc_handle,
1681                                                 &addr, &req_id, &offset, &val);
1682
1683                                 len = g_variant_get_size(val);
1684
1685                                 BT_DBG("Len = %d, BT_ADDR = %s", len, addr);
1686
1687                                 value = (char *) g_variant_get_data(val);
1688                                 _otp_convert_address_to_hex(&addr_hex, addr);
1689
1690                                 if (len != 0) {
1691                                         if (!g_strcmp0(char_path, otp_oacp_obj_path)) {
1692                                                 if (!OACP_indicate)
1693                                                         result = BLUETOOTH_ERROR_CCC_IMPROPERLY_CONFIGURED;
1694                                                 else
1695                                                         result = _bt_otp_oacp_write_cb(value, len, offset, addr, &info);
1696                                         } else if (!g_strcmp0(char_path, otp_olcp_obj_path)) {
1697                                                 if (!OLCP_indicate)
1698                                                         result = BLUETOOTH_ERROR_CCC_IMPROPERLY_CONFIGURED;
1699                                                 else
1700                                                         result = _bt_otp_olcp_write_cb(value, len, offset, &info);
1701                                         } else if (!g_strcmp0(char_path, otp_object_name_obj_path)) {
1702                                                 if (oacp_create) {
1703                                                         /* OACP_CREATE is ongoing */
1704                                                         result = _bt_otp_obj_name_cb(value, len);
1705                                                 } else {
1706                                                         /* Dont permit writting object name except while creating object.
1707                                                          * As this is directly pointing local objects.
1708                                                          */
1709                                                         result = BLUETOOTH_ERROR_WRITE_REQUEST_REJECTED;
1710                                                 }
1711                                         } else if (!g_strcmp0(char_path, otp_object_first_created_obj_path)) {
1712                                                 _bt_otp_set_char_value(otp_object_first_created_obj_path, value, len);
1713                                         } else if (!g_strcmp0(char_path, otp_object_last_modified_obj_path)) {
1714                                                 _bt_otp_set_char_value(otp_object_last_modified_obj_path, value, len);
1715                                         } else {
1716                                                 BT_ERR("Wrong Object Path %s", char_path);
1717                                                 result = BLUETOOTH_ERROR_INTERNAL;
1718                                         }
1719                                         bluetooth_gatt_send_response(req_id,
1720                                         BLUETOOTH_GATT_ATT_REQUEST_TYPE_WRITE,
1721                                                         result, 0, NULL, 0);
1722
1723                                         /* Send indication for CPs */
1724                                         if (!g_strcmp0(char_path, otp_oacp_obj_path)) {
1725                                                 if (OACP_indicate) {
1726                                                         _bt_otp_send_indication(char_path, &info, &addr_hex);
1727                                                 }
1728                                         } else if (!g_strcmp0(char_path, otp_olcp_obj_path)) {
1729                                                 if (OLCP_indicate) {
1730                                                         _bt_otp_send_indication(char_path, &info, &addr_hex);
1731                                                 }
1732                                         }
1733                                 } else {
1734                                         BT_ERR("Array Len 0");
1735                                 }
1736                         } else {
1737                                 BT_ERR("var==NULL");
1738                         }
1739                 } else if (!g_strcmp0(property, "ReadValue")) {
1740                         gchar *addr = NULL;
1741                         guint8 req_id = 1;
1742                         guint16 offset = 0;
1743                         char *value = NULL;
1744                         int len = 0;
1745                         result = BLUETOOTH_ERROR_NONE;
1746
1747                         BT_INFO("ReadValue");
1748                         BT_INFO("Type '%s'\n", g_variant_get_type_string(var));
1749
1750                         g_variant_get(var, "(&s&s&syq)", &char_path,
1751                                         &svc_handle, &addr, &req_id, &offset);
1752
1753                         result = _bt_otp_read_cb(char_path, &value, &len);
1754
1755                         if (result != BLUETOOTH_ERROR_NONE) {
1756                                 BT_ERR("ReadValue failed %s", char_path);
1757                                 bluetooth_gatt_send_response(req_id,
1758                                 BLUETOOTH_GATT_ATT_REQUEST_TYPE_READ,
1759                                                 result, offset, NULL, 0);
1760                         } else {
1761                                 bluetooth_gatt_send_response(req_id,
1762                                 BLUETOOTH_GATT_ATT_REQUEST_TYPE_READ,
1763                                                 result, offset, value, len);
1764                                 if (value)
1765                                         g_free(value);
1766                         }
1767                 } else if (!g_strcmp0(property, "NotificationStateChanged")) {
1768                         gboolean indicate = FALSE;
1769
1770                         g_variant_get(var, "(&s&sb)", &char_path,
1771                                                 &svc_handle, &indicate);
1772
1773                         BT_INFO("%s : [%s]", property,
1774                                 indicate ? "StartNotify" : "StopNotify");
1775                         BT_INFO("Type '%s'\n", g_variant_get_type_string(var));
1776
1777                         if (!g_strcmp0(char_path, otp_oacp_obj_path)) {
1778                                 OACP_indicate = indicate;
1779                         } else if (!g_strcmp0(char_path, otp_olcp_obj_path)) {
1780                                 OLCP_indicate = indicate;
1781                         }
1782                 }
1783         }
1784         return;
1785 }
1786
1787 void _bt_otp_property_event_filter(GDBusConnection *connection,
1788                                         const gchar *sender_name,
1789                                         const gchar *object_path,
1790                                         const gchar *interface_name,
1791                                         const gchar *signal_name,
1792                                         GVariant *parameters,
1793                                         gpointer user_data)
1794 {
1795         GVariant *value;
1796
1797         if (signal_name == NULL) {
1798                 BT_ERR("Wrong Signal");
1799                 return;
1800         }
1801
1802         if (g_strcmp0(signal_name, PROPERTIES_CHANGED) == 0) {
1803
1804                 g_variant_get(parameters, "(@a{sv}@as)", &value, NULL);
1805                 _bt_otp_gatt_char_property_changed_event(value, object_path);
1806         }
1807 }
1808
1809 void _bt_otp_adapter_event_filter(GDBusConnection *connection,
1810                                         const gchar *sender_name,
1811                                         const gchar *object_path,
1812                                         const gchar *interface_name,
1813                                         const gchar *signal_name,
1814                                         GVariant *parameters,
1815                                         gpointer user_data)
1816 {
1817         if (signal_name == NULL) {
1818                 BT_ERR("Wrong Signal");
1819                 return;
1820         }
1821
1822         BT_INFO("Interface %s, Signal %s", interface_name, signal_name);
1823
1824         if (g_strcmp0(interface_name, BT_OTP_INTERFACE_NAME) == 0) {
1825                 if (strcasecmp(signal_name, BLE_DISABLED) == 0) {
1826                         _bt_otp_exit();
1827                 }
1828         }
1829 }
1830
1831 void _bt_otc_disconnected_cb(GDBusConnection *connection,
1832                                         const gchar *sender_name,
1833                                         const gchar *object_path,
1834                                         const gchar *interface_name,
1835                                         const gchar *signal_name,
1836                                         GVariant *parameters,
1837                                         gpointer user_data)
1838 {
1839         if (signal_name == NULL) {
1840                 BT_ERR("Wrong Signal");
1841                 return;
1842         }
1843
1844         BT_INFO("Interface %s, Signal %s", interface_name, signal_name);
1845
1846         if (g_strcmp0(interface_name, BT_DEVICE_INTERFACE) == 0) {
1847                 if (strcasecmp(signal_name, OTC_DISCONNECTED) == 0) {
1848                         BT_DBG("OTC Channel Disconnected dev_path[%s]",
1849                                                                 object_path);
1850                         otc_connection_status = FALSE;
1851                         _bt_otp_free_oacp_op();
1852                 }
1853         }
1854 }
1855
1856 int _bt_otp_init_event_receiver()
1857 {
1858         BT_DBG("+");
1859         GError *error = NULL;
1860
1861         if (conn == NULL) {
1862                 conn =  g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
1863                 if (error != NULL) {
1864                         BT_ERR("ERROR: Can't get on system bus [%s]",
1865                                                         error->message);
1866                         g_clear_error(&error);
1867                 }
1868         }
1869
1870         property_sub_id = g_dbus_connection_signal_subscribe(conn,
1871                                 NULL,
1872                                 BT_OTP_INTERFACE_NAME,
1873                                 PROPERTIES_CHANGED,
1874                                 BT_OTP_OBJECT_PATH, NULL, 0,
1875                                 _bt_otp_property_event_filter,
1876                                 NULL, NULL);
1877
1878         adapter_sub_id = g_dbus_connection_signal_subscribe(conn,
1879                                 NULL,
1880                                 BT_OTP_INTERFACE_NAME,
1881                                 BLE_DISABLED,
1882                                 BT_OTP_OBJECT_PATH, NULL, 0,
1883                                 _bt_otp_adapter_event_filter,
1884                                 NULL, NULL);
1885
1886         device_sub_id = g_dbus_connection_signal_subscribe(conn,
1887                                         NULL, BT_DEVICE_INTERFACE,
1888                                         OTC_DISCONNECTED, NULL, NULL, 0,
1889                                         _bt_otc_disconnected_cb,
1890                                         NULL, NULL);
1891
1892         BT_DBG("-");
1893         return 0;
1894 }
1895
1896 void _bt_otp_deinit_event_receiver(void)
1897 {
1898         BT_DBG("+");
1899
1900         g_dbus_connection_signal_unsubscribe(conn, property_sub_id);
1901         g_dbus_connection_signal_unsubscribe(conn, adapter_sub_id);
1902         g_dbus_connection_signal_unsubscribe(conn, device_sub_id);
1903         conn = NULL;
1904
1905         BT_DBG("-");
1906 }
1907
1908 static void _bt_otp_sig_handler(int sig)
1909 {
1910         BT_DBG("+");
1911         switch (sig) {
1912         case SIGTERM:
1913                 BT_DBG("caught signal - sigterm\n");
1914                 break;
1915         case SIGINT:
1916                 BT_DBG("caught signal - sigint\n");
1917                 break;
1918         case SIGKILL:
1919                 BT_DBG("caught signal - sigkill\n");
1920                 break;
1921         default:
1922                 BT_DBG("caught signal %d and ignored\n", sig);
1923                 break;
1924         }
1925         BT_DBG("-");
1926 }
1927
1928 /* OTP Service Main loop */
1929 int main(void)
1930 {
1931         struct sigaction sa;
1932         BT_ERR("Starting the bt-otp daemon");
1933
1934         memset(&sa, 0, sizeof(sa));
1935         sa.sa_handler = _bt_otp_sig_handler;
1936         sa.sa_flags = SA_SIGINFO;
1937         sigaction(SIGINT, &sa, NULL);
1938         sigaction(SIGTERM, &sa, NULL);
1939         sigaction(SIGKILL, &sa, NULL);
1940
1941         if (_bt_otp_register_interface() != BLUETOOTH_ERROR_NONE) {
1942                 BT_ERR("Fail to register otp service");
1943                 return -4;
1944         }
1945
1946         if (_bt_otp_init_event_receiver() != BLUETOOTH_ERROR_NONE) {
1947                 BT_ERR("Fail to init event reciever");
1948                 return -5;
1949         }
1950
1951         main_loop = g_main_loop_new(NULL, FALSE);
1952
1953         g_main_loop_run(main_loop);
1954
1955         BT_DBG("g_main_loop_quit called!");
1956
1957         if (main_loop != NULL) {
1958                 g_main_loop_unref(main_loop);
1959         }
1960
1961         return 0;
1962 }