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