Merge "Fixed NotifyAcquired property for gatt indication" into tizen
[platform/core/connectivity/bluetooth-frwk.git] / bt-oal / bluez_hal / src / bt-hal-gatt-server.c
1 /*
2  * BLUETOOOTH HAL
3  *
4  * Copyright (c) 2015 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:  Anupam Roy <anupam.r@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *              http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <hardware/bluetooth.h>
23 #include <hardware/bt_gatt.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <gio/gio.h>
29 #include <glib.h>
30 #include <dlog.h>
31 #include <vconf.h>
32 #include <stdbool.h>
33 #include <stddef.h>
34 #include <string.h>
35 #include <fcntl.h>
36
37 #include "bt-hal.h"
38 #include "bt-hal-log.h"
39 #include "bt-hal-msg.h"
40 #include "bt-hal-utils.h"
41 #include "bt-hal-dbus-common-utils.h"
42
43 #include "bt-hal-adapter-le.h"
44 #include "bt-hal-event-receiver.h"
45
46 #define NUMBER_OF_FLAGS 10
47
48 #define GATT_SERV_INTERFACE             "org.bluez.GattService1"
49 #define GATT_CHAR_INTERFACE             "org.bluez.GattCharacteristic1"
50 #define GATT_DESC_INTERFACE             "org.bluez.GattDescriptor1"
51
52 #define BT_GATT_SERVICE_NAME    "org.frwk.gatt_service"
53 #define GATT_SERV_OBJECT_PATH   "/service"
54
55 static GDBusProxy *manager_gproxy = NULL;
56
57 /************************************************************************************
58  **  Static variables
59  ************************************************************************************/
60 extern const btgatt_callbacks_t *bt_gatt_callbacks;
61 guint owner_id;
62 GDBusConnection *g_conn = NULL;
63 //GDBusConnection *conn = NULL;
64 GDBusNodeInfo *manager_node_info = NULL;
65 guint manager_id;
66
67 /* Global handles which needs to be incremented during each addition */
68 static int gatt_service_handle = 10;
69 static int gatt_char_handle = 20;
70 static int gatt_desc_handle = 30;
71
72 struct gatt_service_info {
73         gchar *serv_path;
74         guint serv_id;
75         gchar *service_uuid;
76         guint manager_id;
77         guint prop_id;
78         GSList *char_data;
79         gboolean is_svc_registered;
80         gboolean is_svc_primary;
81         int service_handle;
82 };
83
84 struct gatt_char_info {
85         gchar *char_path;
86         guint char_id;
87         gchar *char_uuid;
88         gchar *char_value;
89         gchar *char_flags[NUMBER_OF_FLAGS];
90         int value_length;
91         int flags_length;
92         int char_handle;
93         GSList *desc_data;
94 };
95
96 struct gatt_desc_info {
97         gchar *desc_path;
98         guint desc_id;
99         gchar *desc_uuid;
100         gchar *desc_value;
101         gchar *desc_flags[NUMBER_OF_FLAGS];
102         int value_length;
103         int flags_length;
104         int desc_handle;
105 };
106
107 /**
108  * GATT Server Request type
109  */
110 typedef enum {
111         BT_HAL_GATT_REQUEST_TYPE_READ = 0x00,       /* Read Requested */
112         BT_HAL_GATT_REQUEST_TYPE_WRITE = 0x01,      /* Write Requested */
113         BT_HAL_GATT_REQUEST_TYPE_EXEC_WRITE = 0x02, /* Exec Write Requested */
114         BT_HAL_GATT_REQUEST_TYPE_ACQUIRE_WRITE = 0x03, /* Exec Write Requested */
115         BT_HAL_GATT_REQUEST_TYPE_ACQUIRE_NOTIFY = 0x04, /* Exec Write Requested */
116 } bt_gatt_request_type_e;
117
118 struct gatt_req_info {
119         gchar *attr_path;
120         gchar *svc_path;
121         guint  request_id;
122         guint  offset;
123         bt_gatt_request_type_e request_type;  /* Read or Write request */
124         GDBusMethodInvocation *context;
125 };
126
127 struct gatt_server_app {
128         int slot;
129         char *app_path;
130         GSList *services;
131 };
132
133 /* Linked List of gatt server app's */
134 static GSList *gatt_server_apps = NULL;
135
136 static GSList *gatt_services = NULL;
137
138 static int conn_id = 0;
139
140 /* Linked List of connected Remote GATT clients */
141 static GSList *gatt_client_info_list = NULL;
142
143 /* GATT Client Info List Structure */
144 struct gatt_client_info_t {
145         int connection_id;                               /* This value will uniquely identify a GATT client-server connection */
146         int instance_id;                                 /* This value unique identifies a GATT server instance */
147         char *addr;                                      /* Remote GATT client address */
148         GSList *gatt_req_info_list;                              /* List of transactions per Connection*/
149 };
150
151 static handle_stack_msg event_cb = NULL;
152
153 typedef struct {
154         uint32_t instance_data;
155         bt_uuid_t uuid;
156 } hal_register_server_data;
157
158 typedef struct {
159         uint32_t instance_data;
160         uint32_t srvc_hdl;
161         bt_uuid_t uuid;
162         uint8_t is_primary;
163 } hal_gatt_service_added;
164
165 typedef struct {
166         uint32_t instance_data;
167         uint32_t srvc_hdl;
168 } hal_gatt_service_started;
169
170 typedef struct {
171         uint32_t instance_data;
172         uint32_t srvc_hdl;
173 } hal_gatt_service_deleted;
174
175 typedef struct {
176         uint32_t instance_data;
177         uint32_t srvc_hdl;
178         uint32_t char_hdl;
179         bt_uuid_t uuid;
180 } hal_gatt_char_added;
181
182 typedef struct {
183         uint32_t instance_data;
184         uint32_t srvc_hdl;
185         uint32_t desc_hdl;
186         bt_uuid_t uuid;
187 } hal_gatt_desc_added;
188
189 #define CHECK_BTGATT_INIT() if (bt_gatt_callbacks == NULL)\
190 {\
191         ERR("%s: BTGATT not initialized", __FUNCTION__);\
192         return BT_STATUS_NOT_READY;\
193 } else {\
194         DBG("%s", __FUNCTION__);\
195 }
196
197 static gboolean __bt_hal_gatt_service_added_cb(gpointer user_data);
198
199 static void __bt_hal_gatt_deinit(char *app_path);
200
201 static void __bt_hal_register_application_cb(GObject *object,
202                                 GAsyncResult *res, gpointer user_data);
203
204 static void __bt_hal_unregister_application_cb(GObject *object, GAsyncResult *res,
205                 gpointer user_data);
206
207 static GDBusProxy *__bt_gatt_gdbus_get_gatt_manager_proxy(const gchar *service,
208                 const gchar *path, const gchar *interface);
209
210 /* Introspection data for the service we are exporting */
211 static const gchar service_introspection_xml[] =
212 "<node name='/'>"
213 "  <interface name='org.freedesktop.DBus.Properties'>"
214 "    <property type='s' name='UUID' access='read'>"
215 "    </property>"
216 "        <property type='b' name='primary' access='read'>"
217 "        </property>"
218 "        <property type='o' name='Device' access='read'>"
219 "        </property>"
220 "        <property type='ao' name='Characteristics' access='read'>"
221 "        </property>"
222 "        <property type='s' name='Includes' access='read'>"
223 "        </property>"
224 "  </interface>"
225 "</node>";
226
227 /* Introspection data for the characteristics we are exporting */
228 static const gchar characteristics_introspection_xml[] =
229 "<node name='/'>"
230 "  <interface name='org.bluez.GattCharacteristic1'>"
231 "        <method name='ReadValue'>"
232 "               <arg type='s' name='address' direction='in'/>"
233 "               <arg type='u' name='id' direction='in'/>"
234 "               <arg type='q' name='offset' direction='in'/>"
235 "               <arg type='ay' name='Value' direction='out'/>"
236 "        </method>"
237 "        <method name='WriteValue'>"
238 "               <arg type='s' name='address' direction='in'/>"
239 "               <arg type='u' name='id' direction='in'/>"
240 "               <arg type='q' name='offset' direction='in'/>"
241 "               <arg type='b' name='response_needed' direction='in'/>"
242 "               <arg type='ay' name='value' direction='in'/>"
243 "        </method>"
244 "       <method name ='AcquireWrite'>"
245 "      <arg type='a{sv}' name='properties' direction='in'/>"
246 "               <arg type='h' name='fd' direction='out'/>"
247 "               <arg type='q' name='mtu' direction='out'/>"
248 "        </method>"
249 "       <method name = 'AcquireNotify'>"
250 "               <arg type='a{sv}' name='properties' direction='in'/>"
251 "               <arg type='h' name='fd' direction='out'/>"
252 "               <arg type='q' name='mtu' direction='out'/>"
253 "        </method>"
254 "        <method name='StartNotify'>"
255 "        </method>"
256 "        <method name='StopNotify'>"
257 "        </method>"
258 "        <method name='IndicateConfirm'>"
259 "               <arg type='s' name='address' direction='in'/>"
260 "               <arg type='b' name='complete' direction='in'/>"
261 "        </method>"
262 "  </interface>"
263 "  <interface name='org.freedesktop.DBus.Properties'>"
264 "    <property type='s' name='UUID' access='read'>"
265 "    </property>"
266 "    <property type='o' name='Service' access='read'>"
267 "    </property>"
268 "    <property type='ay' name='Value' access='readwrite'>"
269 "    </property>"
270 "        <property type='b' name='Notifying' access='read'>"
271 "        </property>"
272 "    <property type='as' name='Flags' access='read'>"
273 "    </property>"
274 "    <property type='s' name='Unicast' access='read'>"
275 "    </property>"
276 "        <property type='ao' name='Descriptors' access='read'>"
277 "        </property>"
278 "        <property type='b' name='WriteAcquired' access='read'>"
279 "        </property>"
280 "        <property type='b' name='NotifyAcquired' access='read'>"
281 "        </property>"
282 "  </interface>"
283 "</node>";
284
285 /* Introspection data for the descriptor we are exporting */
286 static const gchar descriptor_introspection_xml[] =
287 "<node name='/'>"
288 "  <interface name='org.bluez.GattDescriptor1'>"
289 "        <method name='ReadValue'>"
290 "               <arg type='s' name='address' direction='in'/>"
291 "               <arg type='u' name='id' direction='in'/>"
292 "               <arg type='q' name='offset' direction='in'/>"
293 "               <arg type='ay' name='Value' direction='out'/>"
294 "        </method>"
295 "        <method name='WriteValue'>"
296 "               <arg type='s' name='address' direction='in'/>"
297 "               <arg type='u' name='id' direction='in'/>"
298 "               <arg type='q' name='offset' direction='in'/>"
299 "               <arg type='b' name='response_needed' direction='in'/>"
300 "               <arg type='ay' name='value' direction='in'/>"
301 "        </method>"
302 "  </interface>"
303 "  <interface name='org.freedesktop.DBus.Properties'>"
304 "    <property type='s' name='UUID' access='read'>"
305 "    </property>"
306 "    <property type='o' name='Characteristic' access='read'>"
307 "    </property>"
308 "    <property type='ay' name='Value' access='read'>"
309 "    </property>"
310 "    <property type='as' name='Flags' access='read'>"
311 "    </property>"
312 "  </interface>"
313 "</node>";
314
315
316 static const gchar manager_introspection_xml[] =
317 "<node name='/'>"
318 "  <interface name='org.freedesktop.DBus.ObjectManager'>"
319 "    <method name='GetManagedObjects'>"
320 "     <arg type='a{oa{sa{sv}}}' name='object_paths_interfaces_and_properties' direction='out'/>"
321 "        </method>"
322 "  </interface>"
323 "</node>";
324
325 static GSList *_bt_get_service_list_from_server(int instance)
326 {
327         GSList *l;
328         INFO("Number of GATT Server apps [%d]", g_slist_length(gatt_server_apps));
329         INFO("Find App with slot [%d]", instance);
330
331         for (l = gatt_server_apps; l; l = g_slist_next(l)) {
332                 struct gatt_server_app *app = (struct gatt_server_app *)l->data;
333
334                 if (app->slot == instance) {
335                         INFO("App slot [%d] Found, Number of services registered [%d]",
336                                         app->slot, g_slist_length(app->services));
337                         return app->services;
338                 }
339         }
340         return NULL;
341 }
342
343 static void _bt_remote_service_from_gatt_server(int instance, int service_handle)
344 {
345         GSList *l;
346         GSList *l1;
347
348         for (l = gatt_server_apps; l; l = g_slist_next(l)) {
349                 struct gatt_server_app *app = (struct gatt_server_app *)l->data;
350
351                 if (app->slot == instance) {
352                         for (l1 = app->services; l1; l1 = g_slist_next(l1)) {
353                                 struct gatt_service_info *srv = (struct gatt_service_info *)l1->data;
354                                 if (srv->service_handle == service_handle) {
355                                         app->services = g_slist_remove(app->services, srv);
356                                         return;
357                                 }
358                         }
359                 }
360         }
361 }
362
363 static void _bt_hal_update_gatt_service_in_gatt_server(int slot, struct gatt_service_info *serv_info)
364 {
365         GSList *l;
366         for (l = gatt_server_apps; l; l = g_slist_next(l)) {
367                 struct gatt_server_app *app = (struct gatt_server_app *)l->data;
368
369                 if (app->slot == slot) {
370                         INFO("Updating service in GATT server's service list service handle [%d] slot [%d]",
371                                 serv_info->service_handle, slot);
372                         app->services = g_slist_append(app->services, serv_info);
373                 }
374         }
375 }
376
377 static struct gatt_client_info_t *__bt_find_remote_gatt_client_info(char *address)
378 {
379         GSList *l;
380         struct gatt_client_info_t *info = NULL;
381
382         for (l = gatt_client_info_list; l != NULL; l = g_slist_next(l)) {
383                 info = (struct gatt_client_info_t*)l->data;
384                 if (info == NULL)
385                         continue;
386
387                 if (!g_strcmp0(info->addr, address)) {
388                         INFO("Remote GATT client found addr[%s]", info->addr);
389                         return info;
390                 }
391         }
392         return NULL;
393 }
394
395 static struct gatt_client_info_t *__bt_find_remote_gatt_client_info_from_conn(int conn_id)
396 {
397         GSList *l;
398         struct gatt_client_info_t *info = NULL;
399
400         for (l = gatt_client_info_list; l != NULL; l = g_slist_next(l)) {
401                 info = (struct gatt_client_info_t*)l->data;
402                 if (info == NULL)
403                         continue;
404
405                 if (info->connection_id == conn_id) {
406                         INFO("Remote GATT client found addr[%s]", info->addr);
407                         return info;
408                 }
409         }
410         return NULL;
411 }
412
413 static struct gatt_req_info *__bt_find_remote_gatt_client_request_info(int conn_id, int trans_id)
414 {
415         GSList *l;
416         GSList *l1;
417
418         struct gatt_client_info_t *info = NULL;
419         struct gatt_req_info *info1 = NULL;
420
421         for (l = gatt_client_info_list; l != NULL; l = g_slist_next(l)) {
422                 info = (struct gatt_client_info_t*)l->data;
423
424                 if (info == NULL)
425                         continue;
426                 if (info->connection_id == conn_id) {
427
428                         for (l1 = info->gatt_req_info_list; l1; l1 = g_slist_next(l1)) {
429
430                                 info1 = (struct gatt_req_info*)l1->data;
431                                 if (info1 == NULL)
432                                         continue;
433
434                                 if (info1->request_id == trans_id) {
435                                         INFO("Remote GATT client found addr[%s]", info->addr);
436                                         return info1;
437                                 }
438                         }
439                 }
440         }
441         return NULL;
442 }
443
444 void _bt_hal_gatt_connected_state_event(gboolean is_connected, char *address)
445 {
446         struct hal_ev_gatt_server_connected ev;
447         struct gatt_client_info_t *conn_info = NULL;
448         int instance = -1;
449         memset(&ev, 0, sizeof(ev));
450
451         /* Find Server Instance */
452         _bt_hal_get_gatt_server_instance_initialized(&instance);
453         if (instance == -1) {
454                 ERR("Not even a single GATT server is registered");
455                 return;
456         }
457
458         /* Convert address to hex */
459         _bt_hal_convert_addr_string_to_type(ev.bdaddr, address);
460
461         /* Create Connection ID */
462         /* Check if device is already in connected list */
463         conn_info = __bt_find_remote_gatt_client_info(address);
464
465
466         /* If disconnected, and conn info found, then remove conn info */
467         if (is_connected == FALSE) {
468                 DBG("GATT Disconnected");
469                 if (conn_info) {
470                         INFO("Remove GATT client info from List..");
471                         /* Remove info from List */
472                         gatt_client_info_list = g_slist_remove(gatt_client_info_list, conn_info);
473                         INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
474
475                         if (!event_cb)
476                                 ERR("GATT callback not registered");
477                         else {
478                                 DBG("GATT callback registered: server if [%d] Is Connected [%d] addr[%s] conn ID [%d]",
479                                                 conn_info->instance_id, is_connected, conn_info->addr, conn_info->connection_id);
480                                 ev.conn_id = conn_info->connection_id;
481                                 ev.server_instance = conn_info->instance_id;
482                                 ev.connected = is_connected;
483
484                                 event_cb(HAL_EV_GATT_SERVER_CONNECTED, (void *)&ev, sizeof(ev));
485                         }
486                         g_free(conn_info->addr);
487                         g_free(conn_info);
488                 }
489                 /* If connected, and conn info NOT found, then add conn info */
490         } else {
491                 if (!conn_info) {
492                         /* Save Connection info */
493                         conn_info = g_new0(struct gatt_client_info_t, 1);
494                         conn_info->addr = g_strdup(address);
495                         INFO("Added GATT client addr[%s]", conn_info->addr);
496                         conn_info->connection_id = ++conn_id;
497                         conn_info->instance_id = instance;
498                         gatt_client_info_list = g_slist_append(gatt_client_info_list, conn_info);
499                         INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
500
501                         if (!event_cb)
502                                 ERR("GATT callback not registered");
503                         else {
504                                 DBG("GATT callback registered: server if [%d] Is Connected [%d] addr[%s] conn ID [%d]",
505                                                 conn_info->instance_id, is_connected, conn_info->addr, conn_info->connection_id);
506                                 ev.conn_id = conn_info->connection_id;
507                                 ev.server_instance = conn_info->instance_id;
508                                 ev.connected = is_connected;
509
510                                 event_cb(HAL_EV_GATT_SERVER_CONNECTED, (void *)&ev, sizeof(ev));
511                         }
512                 }
513         }
514         /* Send GATT connected or disconnected event */
515 }
516
517 static struct gatt_service_info *__bt_gatt_find_gatt_service_from_char(const char *char_path, int *char_hdl)
518 {
519         GSList *l1, *l2;
520
521         for (l1 = gatt_services; l1 != NULL; l1 = l1->next) {
522                 struct gatt_service_info *serv_info = l1->data;
523
524                 for (l2 = serv_info->char_data; l2 != NULL; l2 = l2->next) {
525                         struct gatt_char_info *char_info = l2->data;
526
527                         if (g_strcmp0(char_info->char_path, char_path)
528                                         == 0) {
529                                 *char_hdl = char_info->char_handle;
530                                 return serv_info;
531                         }
532                 }
533         }
534         ERR("Gatt service not found");
535         return NULL;
536 }
537
538 static char *__bt_gatt_find_char_path_from_handle(int char_hdl)
539 {
540         GSList *l1, *l2;
541
542         for (l1 = gatt_services; l1 != NULL; l1 = l1->next) {
543                 struct gatt_service_info *serv_info = l1->data;
544
545                 for (l2 = serv_info->char_data; l2 != NULL; l2 = l2->next) {
546                         struct gatt_char_info *char_info = l2->data;
547
548                         if (char_info->char_handle == char_hdl)
549                                 return char_info->char_path;
550                 }
551         }
552         ERR("Not found");
553         return NULL;
554 }
555
556 static struct gatt_char_info *__bt_gatt_find_char_info_from_handle(int char_hdl)
557 {
558         GSList *l1, *l2;
559
560         for (l1 = gatt_services; l1 != NULL; l1 = l1->next) {
561                 struct gatt_service_info *serv_info = l1->data;
562
563                 for (l2 = serv_info->char_data; l2 != NULL; l2 = l2->next) {
564                         struct gatt_char_info *char_info = l2->data;
565
566                         if (char_info->char_handle == char_hdl)
567                                 return char_info;
568                 }
569         }
570         ERR("Not found");
571         return NULL;
572 }
573
574 static struct gatt_service_info *__bt_gatt_find_gatt_service_from_desc(const char *desc_path, int *desc_hdl)
575 {
576         GSList *l1, *l2, *l3;
577
578         for (l1 = gatt_services; l1 != NULL; l1 = l1->next) {
579                 struct gatt_service_info *serv_info = l1->data;
580
581                 for (l2 = serv_info->char_data; l2 != NULL; l2 = l2->next) {
582                         struct gatt_char_info *char_info = l2->data;
583
584                         for (l3 = char_info->desc_data; l3 != NULL; l3 = l3->next) {
585                                 struct gatt_desc_info *desc_info = l3->data;
586
587                                 if (g_strcmp0(desc_info->desc_path, desc_path)
588                                                 == 0) {
589                                         *desc_hdl = desc_info->desc_handle;
590                                         return serv_info;
591                                 }
592                         }
593                 }
594         }
595         ERR("Gatt service not found");
596         return NULL;
597 }
598
599 static void __bt_gatt_manager_method_call(GDBusConnection *connection,
600                 const gchar *sender,
601                 const gchar *object_path,
602                 const gchar *interface_name,
603                 const gchar *method_name,
604                 GVariant *parameters,
605                 GDBusMethodInvocation *invocation,
606                 gpointer user_data)
607 {
608         GSList *l = NULL;
609
610         if (g_strcmp0(method_name, "GetManagedObjects") == 0) {
611                 GVariantBuilder *builder;
612                 GVariantBuilder *inner_builder1 = NULL;
613                 GVariant *svc_char = NULL;
614                 GSList *l4;
615                 GSList *gatt_services = NULL;
616                 int *instance;
617                 instance = (int*)user_data;
618                 gboolean writeAcquired = FALSE;
619                 gboolean notifyAcquired = FALSE;
620
621                 DBG("Getting values for service, chars and descriptors");
622                 DBG("GATT Server App for which services are requested [%d]", *instance);
623
624                 /*Main Builder */
625                 builder = g_variant_builder_new(
626                                 G_VARIANT_TYPE("a{oa{sa{sv}}}"));
627
628
629                 gatt_services = _bt_get_service_list_from_server(*instance);
630
631                 if (g_slist_length(gatt_services) == 0) {
632                         ERR("No registered GATT services!!!!");
633                         g_dbus_method_invocation_return_value(invocation, NULL);
634                         g_variant_builder_unref(builder);
635                         return;
636                 }
637
638                 for (l = gatt_services; l != NULL; l = l->next) {
639                         GVariantBuilder *svc_builder = NULL;
640                         GVariantBuilder *inner_builder = NULL;
641                         struct gatt_service_info *serv_info = l->data;
642                         INFO("GATT Service fetched handle [%d]", serv_info->service_handle);
643
644                         /* Prepare inner builder for GattService1 interface */
645                         DBG("Creating builder for service");
646                         svc_builder = g_variant_builder_new(
647                                         G_VARIANT_TYPE("a{sa{sv}}"));
648                         inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
649
650                         g_variant_builder_add(inner_builder, "{sv}", "UUID",
651                                         g_variant_new_string(serv_info->service_uuid));
652
653                         g_variant_builder_add(inner_builder, "{sv}", "Primary",
654                                         g_variant_new_boolean(serv_info->is_svc_primary));
655
656                         /*Characteristics*/
657                         inner_builder1 = g_variant_builder_new(G_VARIANT_TYPE("ao"));
658                         DBG("Adding Charatarisitcs list");
659                         for (l4 = serv_info->char_data; l4 != NULL; l4 = l4->next) {
660                                 struct gatt_char_info *char_info = l4->data;
661                                 INFO("GATT Char handle [%d] found in GATT service handle [%d]",
662                                                 char_info->char_handle, serv_info->service_handle);
663                                 g_variant_builder_add(inner_builder1, "o",
664                                                 char_info->char_path);
665                                 DBG("%s", char_info->char_path);
666                         }
667
668                         svc_char = g_variant_new("ao", inner_builder1);
669                         g_variant_builder_add(inner_builder, "{sv}", "Characteristics",
670                                         svc_char);
671
672                         g_variant_builder_add(svc_builder, "{sa{sv}}",
673                                         GATT_SERV_INTERFACE,
674                                         inner_builder);
675
676                         g_variant_builder_add(builder, "{oa{sa{sv}}}",
677                                         serv_info->serv_path,
678                                         svc_builder);
679
680                         g_variant_builder_unref(inner_builder1);
681
682                         /* Prepare inner builder for GattCharacteristic1 interface */
683
684                         GSList *l2 = serv_info->char_data;
685                         DBG("Creating builder for characteristics \n");
686
687                         if (l2 == NULL)
688                                 DBG("characteristic data is NULL");
689
690                         for (l2 = serv_info->char_data; l2 != NULL; l2 = l2->next) {
691
692                                 GVariantBuilder *char_builder = NULL;
693                                 GVariantBuilder *inner_builder = NULL;
694                                 GVariantBuilder *builder1 = NULL;
695                                 GVariantBuilder *builder2 = NULL;
696                                 GVariantBuilder *builder3 = NULL;
697                                 GVariant *char_val = NULL;
698                                 GVariant *flags_val = NULL;
699                                 GVariant *char_desc = NULL;
700                                 char *unicast = NULL;
701                                 gboolean notify = FALSE;
702                                 int i = 0;
703
704                                 char_builder = g_variant_builder_new(
705                                                 G_VARIANT_TYPE(
706                                                         "a{sa{sv}}"));
707                                 inner_builder = g_variant_builder_new(
708                                                 G_VARIANT_TYPE(
709                                                         "a{sv}"));
710
711                                 struct gatt_char_info *char_info = l2->data;
712                                 if (char_info == NULL) {
713                                         ERR("char_info is NULL");
714                                         continue;
715                                 }
716
717                                 /*Uuid*/
718                                 g_variant_builder_add(inner_builder, "{sv}", "UUID",
719                                                 g_variant_new_string(char_info->char_uuid));
720                                 /*Service*/
721                                 g_variant_builder_add(inner_builder, "{sv}", "Service",
722                                                 g_variant_new("o", serv_info->serv_path));
723                                 /*Value*/
724                                 builder1 = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
725
726                                 if (char_info->char_value != NULL) {
727                                         for (i = 0; i < char_info->value_length; i++) {
728                                                 g_variant_builder_add(builder1, "y",
729                                                                 char_info->char_value[i]);
730                                         }
731                                         char_val = g_variant_new("ay", builder1);
732                                         g_variant_builder_add(inner_builder, "{sv}",
733                                                         "Value", char_val);
734                                 }
735                                 /*Flags*/
736                                 builder2 = g_variant_builder_new(G_VARIANT_TYPE("as"));
737                                 for (i = 0; i < char_info->flags_length; i++) {
738                                         g_variant_builder_add(builder2, "s",
739                                                         char_info->char_flags[i]);
740
741                                         if (strncmp(char_info->char_flags[i], "write-without-response", 16) == 0) {
742                                                 DBG("setting WriteAcquired property");
743                                                 writeAcquired = TRUE;
744                                         }
745
746                                         if (strncmp(char_info->char_flags[i], "notify", 4) == 0) {
747                                                 DBG("setting NotifyAcquired property");
748                                                 notifyAcquired = TRUE;
749                                         }
750                                 }
751
752                                 flags_val = g_variant_new("as", builder2);
753                                 g_variant_builder_add(inner_builder, "{sv}", "Flags",
754                                                 flags_val);
755
756                                 /* Notifying */
757                                 g_variant_builder_add(inner_builder, "{sv}", "Notifying",
758                                                 g_variant_new("b", notify));
759
760                                 /* WriteAcquired */
761                                 if (writeAcquired == TRUE) {
762                                         DBG("adding WriteAcquired property");
763                                         g_variant_builder_add(inner_builder, "{sv}", "WriteAcquired",
764                                                                         g_variant_new("b", writeAcquired));
765                                 }
766
767                                 /* NotifyAcquired */
768                                 if (notifyAcquired == TRUE) {
769                                         DBG("adding NotifyAcquired property");
770                                         g_variant_builder_add(inner_builder, "{sv}", "NotifyAcquired",
771                                                         g_variant_new("b", notifyAcquired));
772                                 }
773
774                                 /* Unicast */
775                                 unicast = g_strdup("00:00:00:00:00:00");
776                                 g_variant_builder_add(inner_builder, "{sv}", "Unicast",
777                                                 g_variant_new("s", unicast));
778
779                                 /*Descriptors*/
780                                 builder3 = g_variant_builder_new(G_VARIANT_TYPE("ao"));
781                                 DBG("Adding Descriptors list");
782
783                                 for (l4 = char_info->desc_data; l4 != NULL; l4 = l4->next) {
784                                         struct gatt_desc_info *desc_info = l4->data;
785                                         INFO("GATT Descriptor handle [%d] found inside GATT Char handle [%d] found in GATT service handle [%d]",
786                                                         desc_info->desc_handle, char_info->char_handle, serv_info->service_handle);
787                                         g_variant_builder_add(builder3, "o",
788                                                         desc_info->desc_path);
789                                         DBG("%s", desc_info->desc_path);
790                                 }
791
792                                 char_desc = g_variant_new("ao", builder3);
793                                 g_variant_builder_add(inner_builder, "{sv}", "Descriptors",
794                                                 char_desc);
795
796                                 g_variant_builder_add(char_builder, "{sa{sv}}",
797                                                 GATT_CHAR_INTERFACE , inner_builder);
798                                 g_variant_builder_add(builder, "{oa{sa{sv}}}",
799                                                 char_info->char_path, char_builder);
800
801                                 /*Prepare inner builder for GattDescriptor1 interface*/
802
803                                 GSList *l3 = char_info->desc_data;
804
805                                 if (l3 == NULL)
806                                         DBG("descriptor data is NULL");
807
808                                 for (l3 = char_info->desc_data; l3 != NULL; l3 = l3->next) {
809
810                                         DBG("Creating builder for descriptor \n");
811
812                                         GVariantBuilder *desc_builder = NULL;
813                                         GVariantBuilder *inner_builder = NULL;
814                                         GVariantBuilder *builder1 = NULL;
815                                         GVariantBuilder *builder2 = NULL;
816                                         GVariant *desc_val = NULL;
817
818                                         desc_builder = g_variant_builder_new(
819                                                         G_VARIANT_TYPE(
820                                                                 "a{sa{sv}}"));
821                                         inner_builder = g_variant_builder_new(
822                                                         G_VARIANT_TYPE(
823                                                                 "a{sv}"));
824
825                                         struct gatt_desc_info *desc_info = l3->data;
826                                         if (desc_info == NULL) {
827                                                 ERR("desc_info is NULL");
828                                                 continue;
829                                         }
830
831                                         /*Uuid*/
832                                         g_variant_builder_add(inner_builder,
833                                                         "{sv}", "UUID",
834                                                         g_variant_new_string(
835                                                                 desc_info->desc_uuid));
836
837                                         /*Characteristic*/
838                                         g_variant_builder_add(inner_builder, "{sv}",
839                                                         "Characteristic",
840                                                         g_variant_new("o",
841                                                                 char_info->char_path));
842
843                                         /*Value*/
844                                         builder1 = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
845
846                                         if (desc_info->desc_value != NULL) {
847                                                 for (i = 0; i < desc_info->value_length; i++) {
848                                                         g_variant_builder_add(builder1, "y",
849                                                                         desc_info->desc_value[i]);
850                                                 }
851                                                 desc_val = g_variant_new("ay", builder1);
852                                                 g_variant_builder_add(inner_builder, "{sv}",
853                                                                 "Value", desc_val);
854                                         }
855
856                                         /*Flags*/
857                                         builder2 = g_variant_builder_new(G_VARIANT_TYPE("as"));
858
859                                         for (i = 0; i < desc_info->flags_length; i++) {
860                                                 g_variant_builder_add(builder2, "s",
861                                                                 desc_info->desc_flags[i]);
862                                         }
863
864                                         flags_val = g_variant_new("as", builder2);
865                                         g_variant_builder_add(inner_builder, "{sv}", "Flags",
866                                                         flags_val);
867
868                                         g_variant_builder_add(desc_builder, "{sa{sv}}",
869                                                         GATT_DESC_INTERFACE,
870                                                         inner_builder);
871
872                                         g_variant_builder_add(builder, "{oa{sa{sv}}}",
873                                                         desc_info->desc_path,
874                                                         desc_builder);
875
876                                         /*unref descriptor builder pointers*/
877                                         g_variant_builder_unref(builder1);
878                                         g_variant_builder_unref(builder2);
879                                         g_variant_builder_unref(inner_builder);
880                                         g_variant_builder_unref(desc_builder);
881                                 }
882
883                                 if (unicast)
884                                         g_free(unicast);
885                                 /*unref char builder pointers*/
886                                 g_variant_builder_unref(builder1);
887                                 g_variant_builder_unref(builder2);
888                                 g_variant_builder_unref(builder3);
889                                 g_variant_builder_unref(inner_builder);
890                                 g_variant_builder_unref(char_builder);
891                         }
892
893                         /*unref service builder pointers*/
894                         g_variant_builder_unref(inner_builder);
895                         g_variant_builder_unref(svc_builder);
896
897                 }
898                 /* Return builder as method reply */
899                 DBG("Sending gatt service builder values to Bluez");
900                 g_dbus_method_invocation_return_value(invocation,
901                                 g_variant_new(
902                                         "(a{oa{sa{sv}}})",
903                                         builder));
904         }
905
906         /* Free User Data */
907         g_free(user_data);
908 }
909
910 static void __bt_gatt_desc_method_call(GDBusConnection *connection,
911                 const gchar *sender,
912                 const gchar *object_path,
913                 const gchar *interface_name,
914                 const gchar *method_name,
915                 GVariant *parameters,
916                 GDBusMethodInvocation *invocation,
917                 gpointer user_data)
918 {
919
920         if (g_strcmp0(method_name, "ReadValue") == 0) {
921                 struct hal_ev_gatt_server_read_req ev;
922
923                 gchar *addr = NULL;
924                 guint req_id = 0;
925                 guint16 offset = 0;
926                 struct gatt_client_info_t *conn_info = NULL;
927                 struct gatt_req_info *req_info = NULL;
928                 struct gatt_service_info *svc_info = NULL;
929                 int desc_hdl = -1;
930
931                 DBG("ReadValue");
932                 DBG("Application path = %s", object_path);
933                 DBG("Sender = %s", sender);
934
935                 svc_info = __bt_gatt_find_gatt_service_from_desc(object_path, &desc_hdl);
936                 if (svc_info == NULL) {
937                         ERR("Coudn't find service for %s", object_path);
938                         g_dbus_method_invocation_return_value(invocation, NULL);
939                         return;
940                 }
941
942                 g_variant_get(parameters, "(&suq)", &addr, &req_id, &offset);
943                 DBG("Request id = %u, Offset = %u", req_id, offset);
944
945                 /* Check if device is already in connected list */
946                 conn_info = __bt_find_remote_gatt_client_info(addr);
947
948                 if (conn_info == NULL) {
949                         ERR("Coudn't find Connection info for %s", addr);
950                         g_dbus_method_invocation_return_value(invocation, NULL);
951                         return;
952                 }
953
954                 if (!event_cb) {
955                         ERR("GATT callback NOT registered");
956                         g_dbus_method_invocation_return_value(invocation, NULL);
957                         return;
958                 }
959
960                 /* Store requests information */
961                 req_info = g_new0(struct gatt_req_info, 1);
962                 req_info->attr_path = g_strdup(object_path);
963                 req_info->svc_path = g_strdup(svc_info->serv_path);
964                 req_info->request_id = req_id;
965                 req_info->offset = offset;
966                 req_info->context = invocation;
967
968                 /* Append request info in list of requests for the particular connection */
969                 conn_info->gatt_req_info_list = g_slist_append(conn_info->gatt_req_info_list, req_info);
970
971                 /* Send HAL event */
972                 memset(&ev, 0, sizeof(ev));
973                 ev.conn_id = conn_info->connection_id;
974                 ev.trans_id = req_id;
975                 ev.att_handle = desc_hdl;
976                 ev.offset = offset;
977                 ev.is_long = false; /* TODO*/
978
979                 /* Convert address to hex */
980                 _bt_hal_convert_addr_string_to_type(ev.bdaddr, addr);
981
982                 event_cb(HAL_EV_GATT_READ_REQUESTED, (void *)&ev, sizeof(ev));
983                 return;
984         } else if (g_strcmp0(method_name, "WriteValue") == 0) {
985
986                 GVariant *var = NULL;
987                 gchar *addr = NULL;
988                 guint req_id = 0;
989                 guint16 offset = 0;
990                 gboolean response_needed = FALSE;
991                 struct hal_ev_gatt_server_write_req ev;
992                 int desc_hdl = -1;
993                 int len;
994
995                 struct gatt_service_info *svc_info = NULL;
996                 struct gatt_client_info_t *conn_info = NULL;
997                 struct gatt_req_info *req_info = NULL;
998
999                 memset(&ev, 0, sizeof(ev));
1000
1001                 DBG("WriteValue");
1002                 DBG("Application path = %s", object_path);
1003                 DBG("Sender = %s", sender);
1004
1005                 g_variant_get(parameters, "(&suqb@ay)",
1006                                 &addr, &req_id, &offset, &response_needed, &var);
1007                 DBG("Request id = %u, Offset = %u", req_id, offset);
1008
1009                 /* Check if device is already in connected list */
1010                 conn_info = __bt_find_remote_gatt_client_info(addr);
1011
1012                 svc_info = __bt_gatt_find_gatt_service_from_desc(object_path, &desc_hdl);
1013
1014                 if (conn_info == NULL || svc_info == NULL || event_cb == NULL) {
1015                         g_variant_unref(var);
1016                         if (response_needed)
1017                                 g_dbus_method_invocation_return_value(invocation, NULL);
1018                         else
1019                                 g_object_unref(invocation);
1020                         return;
1021                 }
1022
1023                 len = g_variant_get_size(var);
1024                 if (len > 0 && len < 600) {
1025                         char *data;
1026                         data = (char *)g_variant_get_data(var);
1027                         memcpy(ev.value, data, len);
1028                         ev.length = len;
1029                 }
1030                 if (response_needed) {
1031                         /* Store request information */
1032                         req_info = g_new0(struct gatt_req_info, 1);
1033                         req_info->attr_path = g_strdup(object_path);
1034                         req_info->svc_path = g_strdup(svc_info->serv_path);
1035                         req_info->request_id = req_id;
1036                         req_info->offset = offset;
1037                         req_info->context = invocation;
1038
1039                         /* Append request info in list of requests for the particular connection */
1040                         conn_info->gatt_req_info_list = g_slist_append(conn_info->gatt_req_info_list, req_info);
1041                 } else {
1042                         g_object_unref(invocation);
1043                 }
1044
1045                 /* Send HAL event */
1046                 ev.conn_id = conn_info->connection_id;
1047                 ev.trans_id = req_id;
1048                 ev.att_handle = desc_hdl;
1049                 ev.offset = offset;
1050                 ev.need_rsp = response_needed;
1051                 ev.is_prep = 0;
1052
1053                 /* Convert address to hex */
1054                 _bt_hal_convert_addr_string_to_type(ev.bdaddr, addr);
1055
1056                 event_cb(HAL_EV_GATT_WRITE_REQUESTED, (void *)&ev, sizeof(ev));
1057
1058                 g_variant_unref(var);
1059                 return;
1060         }
1061 }
1062
1063 static void __bt_gatt_char_method_call(GDBusConnection *connection,
1064                 const gchar *sender,
1065                 const gchar *object_path,
1066                 const gchar *interface_name,
1067                 const gchar *method_name,
1068                 GVariant *parameters,
1069                 GDBusMethodInvocation *invocation,
1070                 gpointer user_data)
1071 {
1072         if (g_strcmp0(method_name, "ReadValue") == 0) {
1073                 gchar *addr = NULL;
1074                 guint req_id = 0;
1075                 guint16 offset = 0;
1076                 struct hal_ev_gatt_server_read_req ev;
1077                 int char_hdl = -1;
1078
1079                 struct gatt_req_info *req_info = NULL;
1080                 struct gatt_client_info_t *conn_info = NULL;
1081                 struct gatt_service_info *svc_info = NULL;
1082
1083                 g_variant_get(parameters, "(&suq)",
1084                                         &addr, &req_id, &offset);
1085
1086                 DBG("Request id = %u, Offset = %u", req_id, offset);
1087                 DBG("Application path = %s", object_path);
1088                 DBG("Sender = %s", sender);
1089                 DBG("Address = %s", addr);
1090
1091                 /* Check if device is already in connected list */
1092                 conn_info = __bt_find_remote_gatt_client_info(addr);
1093
1094                 svc_info = __bt_gatt_find_gatt_service_from_char(object_path, &char_hdl);
1095
1096                 if (svc_info == NULL || conn_info == NULL) {
1097                         g_dbus_method_invocation_return_value(invocation, NULL);
1098                         return;
1099                 }
1100
1101                 if (!event_cb) {
1102                         ERR("GATT callback NOT registered");
1103                         g_dbus_method_invocation_return_value(invocation, NULL);
1104                         return;
1105                 }
1106
1107                 /* Store requets information */
1108                 req_info = g_new0(struct gatt_req_info, 1);
1109                 req_info->attr_path = g_strdup(object_path);
1110                 req_info->svc_path = g_strdup(svc_info->serv_path);
1111                 req_info->request_id = req_id;
1112                 req_info->request_type = BT_HAL_GATT_REQUEST_TYPE_READ;
1113                 req_info->offset = offset;
1114                 req_info->context = invocation;
1115
1116                 /* Append request info in list of requests for the particular connection */
1117                 conn_info->gatt_req_info_list = g_slist_append(conn_info->gatt_req_info_list, req_info);
1118
1119                 /* Send HAL event */
1120                 memset(&ev, 0, sizeof(ev));
1121                 ev.conn_id = conn_info->connection_id;
1122                 ev.trans_id = req_id;
1123                 ev.att_handle = char_hdl;
1124                 ev.offset = offset;
1125                 ev.is_long = false; /* TODO*/
1126
1127                 /* Convert address to hex */
1128                 _bt_hal_convert_addr_string_to_type(ev.bdaddr, addr);
1129                 event_cb(HAL_EV_GATT_READ_REQUESTED, (void *)&ev, sizeof(ev));
1130                 return;
1131         } else if (g_strcmp0(method_name, "WriteValue") == 0) {
1132                 GVariant *var = NULL;
1133                 gchar *addr = NULL;
1134                 guint req_id = 0;
1135                 guint16 offset = 0;
1136                 gboolean response_needed = FALSE;
1137                 struct hal_ev_gatt_server_write_req ev;
1138                 int char_hdl = -1;
1139                 int len;
1140
1141                 struct gatt_service_info *svc_info = NULL;
1142                 struct gatt_req_info *req_info = NULL;
1143                 struct gatt_client_info_t *conn_info = NULL;
1144
1145                 memset(&ev, 0, sizeof(ev));
1146
1147                 DBG("WriteValue");
1148                 DBG("Application path = %s", object_path);
1149                 DBG("Sender = %s", sender);
1150
1151                 g_variant_get(parameters, "(&suqb@ay)",
1152                                 &addr, &req_id, &offset, &response_needed, &var);
1153                 DBG("Request id = %u, Offset = %u", req_id, offset);
1154
1155                 svc_info = __bt_gatt_find_gatt_service_from_char(object_path, &char_hdl);
1156
1157                 /* Check if device is already in connected list */
1158                 conn_info = __bt_find_remote_gatt_client_info(addr);
1159
1160                 if (svc_info == NULL || conn_info == NULL || event_cb == NULL) {
1161                         g_variant_unref(var);
1162                         if (response_needed)
1163                                 g_dbus_method_invocation_return_value(invocation, NULL);
1164                         else
1165                                 g_object_unref(invocation);
1166                         return;
1167                 }
1168
1169                 len = g_variant_get_size(var);
1170                 if (len > 0 && len < 600) {
1171                         char *data;
1172                         data = (char *)g_variant_get_data(var);
1173                         memcpy(ev.value, data, len);
1174                         ev.length = len;
1175                 }
1176
1177                 if (response_needed) {
1178                         /* Store requets information */
1179                         req_info = g_new0(struct gatt_req_info, 1);
1180                         req_info->attr_path = g_strdup(object_path);
1181                         req_info->svc_path = g_strdup(svc_info->serv_path);
1182                         req_info->request_id = req_id;
1183                         req_info->request_type = BT_HAL_GATT_REQUEST_TYPE_WRITE;
1184                         req_info->offset = offset;
1185                         req_info->context = invocation;
1186
1187                         /* Append request info in list of requests for the particular connection */
1188                         conn_info->gatt_req_info_list = g_slist_append(conn_info->gatt_req_info_list, req_info);
1189
1190                 } else {
1191                         g_object_unref(invocation);
1192                 }
1193
1194                 /* Send HAL event */
1195                 ev.conn_id = conn_info->connection_id;
1196                 ev.trans_id = req_id;
1197                 ev.att_handle = char_hdl;
1198                 ev.offset = offset;
1199                 ev.need_rsp = response_needed;
1200                 ev.is_prep = 0;
1201
1202                 /* Convert address to hex */
1203                 _bt_hal_convert_addr_string_to_type(ev.bdaddr, addr);
1204
1205                 event_cb(HAL_EV_GATT_WRITE_REQUESTED, (void *)&ev, sizeof(ev));
1206
1207                 g_variant_unref(var);
1208                 return;
1209
1210         } else if (g_strcmp0(method_name, "StartNotify") == 0) {
1211                 DBG("StartNotify");
1212 #ifdef TIZEN_BT_HAL
1213                 struct gatt_service_info *svc_info = NULL;
1214                 struct hal_ev_gatt_server_notifcation_change ev;
1215                 int char_hdl = -1;
1216
1217                 svc_info = __bt_gatt_find_gatt_service_from_char(object_path, &char_hdl);
1218                 if (svc_info == NULL || event_cb == NULL)
1219                         return;
1220
1221                 /* Send HAL event */
1222                 memset(&ev, 0, sizeof(ev));
1223                 ev.conn_id = -1;  /*TODO Bluez does not provide remote GATT client address, so no conn_id */
1224                 ev.trans_id = -1; /*TODO Bluez does not provide request id or transacion ID */
1225                 ev.att_handle = char_hdl;
1226                 ev.notify = true;
1227
1228                 /* Convert address to hex */
1229                 _bt_hal_convert_addr_string_to_type(ev.bdaddr, "00:00:00:00:00"); /* TODO Bluez Does not provide address of GATT client */
1230
1231                 event_cb(HAL_EV_GATT_NOTIFICATION_CHANGE, (void *)&ev, sizeof(ev));
1232 #endif
1233
1234         } else if (g_strcmp0(method_name, "StopNotify") == 0) {
1235                 DBG("StopNotify");
1236 #ifdef TIZEN_BT_HAL
1237                 struct gatt_service_info *svc_info = NULL;
1238                 struct hal_ev_gatt_server_notifcation_change ev;
1239                 int char_hdl = -1;
1240
1241                 svc_info = __bt_gatt_find_gatt_service_from_char(object_path, &char_hdl);
1242                 if (svc_info == NULL || event_cb == NULL)
1243                         return;
1244
1245                 /* Send HAL event */
1246                 memset(&ev, 0, sizeof(ev));
1247                 ev.conn_id = -1;  /*TODO Bluez does not provide remote GATT client address, so no conn_id */
1248                 ev.trans_id = -1; /*TODO Bluez does not provide request id or transacion ID */
1249                 ev.att_handle = char_hdl;
1250                 ev.notify = false;
1251
1252                 /* Convert address to hex */
1253                 _bt_hal_convert_addr_string_to_type(ev.bdaddr, "00:00:00:00:00"); /* TODO Bluez DOes not provide address of GATT client */
1254
1255                 event_cb(HAL_EV_GATT_NOTIFICATION_CHANGE, (void *)&ev, sizeof(ev));
1256 #endif
1257
1258         } else if (g_strcmp0(method_name, "IndicateConfirm") == 0) {
1259                 gchar *addr = NULL;
1260                 gboolean complete = FALSE;
1261                 int char_hdl = -1;
1262
1263                 struct gatt_service_info *svc_info = NULL;
1264                 struct gatt_client_info_t *conn_info = NULL;
1265
1266                 struct hal_ev_gatt_server_indicate_cfm ev;
1267
1268                 DBG("IndicateConfirm");
1269                 DBG("Application path = %s", object_path);
1270                 DBG("Sender = %s", sender);
1271
1272                 g_variant_get(parameters, "(&sb)", &addr, &complete);
1273                 DBG("Remote Device address number = %s", addr);
1274                 DBG("Is Indicate confirmation for last device [%d]", complete);
1275
1276                 /* Check if device is already in connected list */
1277                 conn_info = __bt_find_remote_gatt_client_info(addr);
1278
1279                 svc_info = __bt_gatt_find_gatt_service_from_char(object_path, &char_hdl);
1280
1281                 if (svc_info == NULL || conn_info == NULL
1282                         || event_cb == NULL) {
1283                         return;
1284                 }
1285
1286                 /* Send HAL event */
1287                 memset(&ev, 0, sizeof(ev));
1288                 ev.conn_id = conn_info->connection_id;
1289                 ev.trans_id = -1; /*TODO Bluez does not provide Transaction ID or request ID */
1290                 ev.att_handle = char_hdl;
1291
1292                 /* Convert address to hex */
1293                 _bt_hal_convert_addr_string_to_type(ev.bdaddr, addr);
1294
1295                 event_cb(HAL_EV_GATT_INDICATE_CFM, (void *)&ev, sizeof(ev));
1296         } else if (g_strcmp0(method_name, "AcquireWrite") == 0) {
1297
1298                                 uint16_t mtu = 512;
1299                                 int char_hdl = -1;
1300                                 struct hal_ev_gatt_server_acquire_write_res  ev;
1301                                 struct gatt_service_info *svc_info = NULL;
1302                                 struct gatt_req_info *req_info = NULL;
1303                                 struct gatt_client_info_t *conn_info = NULL;
1304                                 char * dev_path = NULL;
1305                                 char * link = 0;
1306                                 char  addr[BT_HAL_ADDRESS_STRING_SIZE];
1307
1308                                 DBG("AcquireWrite");
1309                                 DBG("Application path = %s", object_path);
1310                                 DBG("Sender = %s", sender);
1311
1312                                 GVariantIter* iter;
1313                                 g_variant_get(parameters, "(a{sv})", &iter);
1314                                 char* key = NULL;
1315                                 GVariant* value = NULL;
1316                                 while (g_variant_iter_loop(iter, "{sv}", &key, &value)) {
1317                                                 if (g_strcmp0(key, "MTU") == 0)
1318                                                         g_variant_get(value, "i", &mtu);
1319                                                 else if (g_strcmp0(key, "link") == 0)
1320                                                         g_variant_get(value, "s", &link);
1321                                                 else if (g_strcmp0(key, "device") == 0)
1322                                                         g_variant_get(value, "o", &dev_path);
1323                                 }
1324
1325                                 DBG("AcquireWrite   values retived");
1326                                 DBG(" path  %s LINK = %s, MTU = %u", addr, link, mtu);
1327
1328                                 svc_info = __bt_gatt_find_gatt_service_from_char(object_path, &char_hdl);
1329
1330                                 _bt_hal_convert_device_path_to_address(dev_path, addr);
1331
1332                                 DBG("remote address %s", addr);
1333
1334                                 /* Check if device is already in connected list */
1335                                 conn_info = __bt_find_remote_gatt_client_info(addr);
1336
1337                                 if (conn_info == NULL) {
1338                                         ERR("Cleint info not found\n");
1339                                         goto done;
1340                                 }
1341
1342                                 if (svc_info == NULL) {
1343                                         ERR("svc_info  info not found\n");
1344                                         goto done;
1345                                 }
1346
1347                                 /* Store requets information */
1348                                 req_info = g_new0(struct gatt_req_info, 1);
1349                                 req_info->attr_path = g_strdup(object_path);
1350                                 req_info->svc_path = g_strdup(svc_info->serv_path);
1351                                 req_info->request_id = conn_info->connection_id;
1352                                 req_info->request_type = BT_HAL_GATT_REQUEST_TYPE_ACQUIRE_WRITE;
1353                                 req_info->offset = mtu;
1354                                 req_info->context = invocation;
1355
1356                                 /* Append request info in list of requests for the particular connection */
1357                                 conn_info->gatt_req_info_list = g_slist_append(conn_info->gatt_req_info_list, req_info);
1358
1359                                 /* Send HAL event */
1360                                 memset(&ev, 0, sizeof(ev));
1361                                 ev.conn_id = conn_info->connection_id;
1362                                 ev.char_handl = char_hdl;
1363                                 ev.mtu = mtu;
1364                                 ev.trans_id = conn_info->connection_id;
1365                                 _bt_hal_convert_addr_string_to_type(ev.bdaddr, addr);
1366                                 event_cb(HAL_EV_GATT_SERVER_ACQUIRE_WRITE_RES, (void *)&ev, sizeof(ev));
1367
1368                                 DBG("HAL_EV_GATT_ACQUIRE_WRITE_RES   called");
1369
1370
1371                         return;
1372                 } else if (g_strcmp0(method_name, "AcquireNotify") == 0) {
1373
1374                                 uint16_t mtu = 512;
1375                                 int char_hdl = -1;
1376                                 struct hal_ev_gatt_server_acquire_notify  ev;
1377                                 struct gatt_service_info *svc_info = NULL;
1378                                 struct gatt_client_info_t *conn_info = NULL;
1379                                 struct gatt_req_info *req_info = NULL;
1380
1381                                 DBG("AcquireNotify");
1382                                 DBG("Application path = %s", object_path);
1383                                 DBG("Sender = %s", sender);
1384
1385                                 GVariantIter* iter;
1386                                 g_variant_get(parameters, "(a{sv})", &iter);
1387                                 char* key = NULL;
1388                                 GVariant* value = NULL;
1389                                 while (g_variant_iter_loop(iter, "{sv}", &key, &value)) {
1390                                                 if (g_strcmp0(key, "MTU") == 0)
1391                                                         g_variant_get(value, "q", &mtu);
1392                                 }
1393
1394                                 DBG("AcquireNotify  values retived");
1395                                 DBG(" MTU = %u",  mtu);
1396
1397                                 svc_info = __bt_gatt_find_gatt_service_from_char(object_path, &char_hdl);
1398
1399                         if (svc_info == NULL) {
1400                                         ERR("svc_info  info not found\n");
1401                                         goto done;
1402                                 }
1403
1404                                 /* Store requets information */
1405                                 req_info = g_new0(struct gatt_req_info, 1);
1406                                 req_info->attr_path = g_strdup(object_path);
1407                                 req_info->svc_path = g_strdup(svc_info->serv_path);
1408                                 req_info->request_id = 33;
1409                                 req_info->request_type = BT_HAL_GATT_REQUEST_TYPE_ACQUIRE_NOTIFY;
1410                                 req_info->offset = mtu;
1411                                 req_info->context = invocation;
1412
1413                                 conn_info = g_new0(struct gatt_client_info_t, 1);
1414                                 conn_info->addr = g_strdup(object_path);
1415                                 INFO("Added GATT client path[%s]", conn_info->addr);
1416                                 conn_info->connection_id = 33;
1417                                 conn_info->instance_id = 33;
1418                                 /* Append request info in list of requests for the particular connection */
1419                                 conn_info->gatt_req_info_list = g_slist_append(conn_info->gatt_req_info_list, req_info);
1420                                 gatt_client_info_list = g_slist_append(gatt_client_info_list, conn_info);
1421
1422                                 /* Send HAL event */
1423                                 memset(&ev, 0, sizeof(ev));
1424                                 ev.conn_id = 33;
1425                                 ev.char_handl = char_hdl;
1426                                 ev.mtu = mtu;
1427                                 ev.trans_id = 33;
1428
1429                                 event_cb(HAL_EV_GATT_SERVER_ACQUIRE_NOTIFY_RES, (void *)&ev, sizeof(ev));
1430
1431                                 DBG("HAL_EV_GATT_ACQUIRE_NOTIFY_RES   called");
1432
1433                                 return;
1434         }
1435
1436 done:
1437         g_dbus_method_invocation_return_value(invocation, NULL);
1438 }
1439
1440 static gboolean __bt_hal_gatt_emit_interface_removed(gchar *object_path, gchar *interface)
1441 {
1442         gboolean ret;
1443         GError *error = NULL;
1444         GVariantBuilder *array_builder;
1445
1446         array_builder = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
1447         g_variant_builder_init(array_builder, G_VARIANT_TYPE("as"));
1448         g_variant_builder_add(array_builder, "s", interface);
1449
1450         ret = g_dbus_connection_emit_signal(g_conn, NULL, "/",
1451                         "org.freedesktop.Dbus.Objectmanager",
1452                         "InterfacesRemoved",
1453                         g_variant_new("(oas)",
1454                                 object_path, array_builder),
1455                         &error);
1456
1457         if (!ret) {
1458                 if (error != NULL) {
1459                         /* dbus gives error cause */
1460                         ERR("d-bus api failure: errcode[%x], message[%s]",
1461                                         error->code, error->message);
1462                         g_clear_error(&error);
1463                 }
1464         }
1465         g_variant_builder_unref(array_builder);
1466
1467         return ret;
1468 }
1469
1470 static void __bt_hal_gatt_free_descriptor_info(struct gatt_desc_info *desc_info)
1471 {
1472         int i;
1473
1474         if (!desc_info)
1475                 return;
1476
1477         g_free(desc_info->desc_path);
1478         g_free(desc_info->desc_uuid);
1479         g_free(desc_info->desc_value);
1480
1481         for (i = 0; i < desc_info->flags_length; i++)
1482                 g_free(desc_info->desc_flags[i]);
1483
1484         g_free(desc_info);
1485 }
1486
1487 static void __bt_hal_gatt_free_characteristic_info(struct gatt_char_info *char_info)
1488 {
1489         int i;
1490
1491         if (!char_info)
1492                 return;
1493
1494         g_free(char_info->char_path);
1495         g_free(char_info->char_uuid);
1496         g_free(char_info->char_value);
1497
1498         for (i = 0; i < char_info->flags_length; i++)
1499                 g_free(char_info->char_flags[i]);
1500
1501         g_free(char_info);
1502 }
1503
1504
1505 static void __bt_hal_gatt_free_service_info(struct gatt_service_info *svc_info)
1506 {
1507         if (!svc_info)
1508                 return;
1509
1510         g_free(svc_info->serv_path);
1511         g_free(svc_info->service_uuid);
1512         g_free(svc_info);
1513 }
1514
1515 static const GDBusInterfaceVTable desc_interface_vtable = {
1516         __bt_gatt_desc_method_call,
1517         NULL,
1518         NULL,
1519 };
1520
1521 static const GDBusInterfaceVTable char_interface_vtable = {
1522         __bt_gatt_char_method_call,
1523         NULL,
1524         NULL,
1525 };
1526
1527 static const GDBusInterfaceVTable serv_interface_vtable = {
1528         NULL,
1529         NULL,
1530         NULL,
1531 };
1532
1533 static const GDBusInterfaceVTable manager_interface_vtable = {
1534         __bt_gatt_manager_method_call,
1535         NULL,
1536         NULL
1537 };
1538
1539
1540 static GDBusNodeInfo *__bt_gatt_create_method_node_info(
1541                 const gchar *introspection_data)
1542 {
1543         GError *err = NULL;
1544         GDBusNodeInfo *node_info = NULL;
1545
1546         if (introspection_data == NULL)
1547                 return NULL;
1548
1549
1550         DBG("Create new node info");
1551         node_info = g_dbus_node_info_new_for_xml(introspection_data, &err);
1552
1553         if (err) {
1554                 ERR("Unable to create node: %s", err->message);
1555                 g_clear_error(&err);
1556                 return NULL;
1557         }
1558
1559         return node_info;
1560 }
1561
1562 /* To send stack event to hal-av handler */
1563 void _bt_hal_register_gatt_server_handler_cb(handle_stack_msg cb)
1564 {
1565         event_cb = cb;
1566 }
1567
1568 void _bt_hal_unregister_gatt_server_handler_cb(void)
1569 {
1570         event_cb = NULL;
1571 }
1572
1573 static gboolean __bt_hal_gatt_desc_added_cb(gpointer user_data)
1574 {
1575         struct hal_ev_gatt_desc_added ev;
1576         hal_gatt_desc_added *data = (hal_gatt_desc_added*) user_data;
1577
1578         /* Prepare to GATT characteristic added event */
1579         memset(&ev, 0, sizeof(ev));
1580         ev.status = BT_STATUS_SUCCESS;
1581         ev.server_instance = data->instance_data;
1582         memcpy(ev.desc_uuid, data->uuid.uu, sizeof(data->uuid.uu));
1583         ev.service_handle = data->srvc_hdl;
1584         ev.desc_handle = data->desc_hdl;
1585
1586         if (!event_cb)
1587                 ERR("GATT Descriptor Added callback registered");
1588         else {
1589                 DBG("GATT Descriptor Added: server if [%d] Service handle [%d] Descriptor Handle [%d]",
1590                                 data->instance_data, data->srvc_hdl, data->desc_hdl);
1591
1592                 event_cb(HAL_EV_GATT_DESC_ADDED, (void *)&ev, sizeof(ev));
1593         }
1594
1595         g_free(data);
1596         return FALSE;
1597 }
1598
1599 static gboolean __bt_hal_gatt_char_added_cb(gpointer user_data)
1600 {
1601         struct hal_ev_gatt_char_added ev;
1602         hal_gatt_char_added *data = (hal_gatt_char_added*) user_data;
1603
1604         /* Prepare to GATT characteristic added event */
1605         memset(&ev, 0, sizeof(ev));
1606         ev.status = BT_STATUS_SUCCESS;
1607         ev.server_instance = data->instance_data;
1608         memcpy(ev.char_uuid, data->uuid.uu, sizeof(data->uuid.uu));
1609         ev.service_handle = data->srvc_hdl;
1610         ev.char_handle = data->char_hdl;
1611
1612         if (!event_cb)
1613                 ERR("GATT Characteristic Added callback registered");
1614         else {
1615                 DBG("GATT Characteristic Added: server if [%d] Service handle [%d] Characteristic handle [%d]",
1616                                 data->instance_data, data->srvc_hdl, data->char_hdl);
1617
1618                 event_cb(HAL_EV_GATT_CHAR_ADDED, (void *)&ev, sizeof(ev));
1619         }
1620
1621         g_free(data);
1622         return FALSE;
1623 }
1624
1625 static gboolean __bt_hal_gatt_service_added_cb(gpointer user_data)
1626 {
1627         struct hal_ev_gatt_service_added ev;
1628         hal_gatt_service_added *data = (hal_gatt_service_added*) user_data;
1629
1630         /* Prepare to GATT Service added event */
1631         memset(&ev, 0, sizeof(ev));
1632         ev.status = BT_STATUS_SUCCESS;
1633         ev.server_instance = data->instance_data;
1634         memcpy(ev.svc_uuid, data->uuid.uu, sizeof(data->uuid.uu));
1635         ev.service_handle = data->srvc_hdl;
1636         ev.is_primary = data->is_primary;
1637
1638         if (!event_cb)
1639                 ERR("GATT Service Added callback registered");
1640         else {
1641                 DBG("GATT Service Added: server if [%d] Service handle [%d]",
1642                                 data->instance_data,  data->srvc_hdl);
1643                 event_cb(HAL_EV_GATT_SERVICE_ADDED, (void *)&ev, sizeof(ev));
1644         }
1645
1646         g_free(data);
1647         return FALSE;
1648 }
1649
1650 static gboolean __bt_hal_gatt_service_started_cb(gpointer user_data)
1651 {
1652         struct hal_ev_gatt_service_started ev;
1653         hal_gatt_service_started *data = (hal_gatt_service_started*) user_data;
1654
1655         /* Prepare to GATT Service added event */
1656         memset(&ev, 0, sizeof(ev));
1657         ev.status = BT_STATUS_SUCCESS;
1658         ev.server_instance = data->instance_data;
1659         ev.service_handle = data->srvc_hdl;
1660
1661         if (!event_cb)
1662                 ERR("GATT Service Started callback registered");
1663         else {
1664                 DBG("GATT Service Started: server if [%d] Service handle [%d]",
1665                                 data->instance_data,  data->srvc_hdl);
1666                 event_cb(HAL_EV_GATT_SERVICE_STARTED, (void *)&ev, sizeof(ev));
1667         }
1668
1669         g_free(data);
1670         return FALSE;
1671 }
1672
1673 static gboolean __bt_hal_gatt_service_deleted_cb(gpointer user_data)
1674 {
1675         struct hal_ev_gatt_service_deleted ev;
1676         hal_gatt_service_deleted *data = (hal_gatt_service_deleted*) user_data;
1677
1678         /* Prepare to GATT Service added event */
1679         memset(&ev, 0, sizeof(ev));
1680         ev.status = BT_STATUS_SUCCESS;
1681         ev.server_instance = data->instance_data;
1682         ev.service_handle = data->srvc_hdl;
1683
1684         if (!event_cb)
1685                 ERR("GATT Service Deleted callback registered");
1686         else {
1687                 DBG("GATT Service Deleted: server if [%d] Service handle [%d]",
1688                                 data->instance_data,  data->srvc_hdl);
1689                 event_cb(HAL_EV_GATT_SERVICE_DELETED, (void *)&ev, sizeof(ev));
1690         }
1691
1692         g_free(data);
1693         return FALSE;
1694 }
1695
1696 static gboolean __bt_hal_register_slot_id_cb(gpointer user_data)
1697 {
1698         struct hal_ev_server_instance_registered ev;
1699         hal_register_server_data *data = (hal_register_server_data*) user_data;
1700
1701         /* Prepare to send AV connecting event */
1702         memset(&ev, 0, sizeof(ev));
1703         ev.status = BT_STATUS_SUCCESS;
1704         ev.server_instance = data->instance_data;
1705         memcpy(ev.app_uuid, data->uuid.uu, sizeof(ev.app_uuid));
1706
1707         if (!event_cb)
1708                 ERR("GATT Register Server Instance Callback not registered");
1709         else {
1710                 DBG("Server Instance is registered!! server if [%d]", data->instance_data);
1711                 event_cb(HAL_EV_SERVER_INSTANCE_INITIALIZED, (void *)&ev, sizeof(ev));
1712         }
1713
1714         g_free(data);
1715         return FALSE;
1716 }
1717
1718 static bt_status_t gatt_server_register_app(bt_uuid_t *uuid)
1719 {
1720         CHECK_BTGATT_INIT();
1721         int status = BT_STATUS_FAIL;
1722         int server_if;
1723         DBG("Register server instance request");
1724         hal_register_server_data *user_data = g_malloc0(sizeof(hal_register_server_data));
1725
1726         /* Check if slot available */
1727         server_if = _bt_hal_get_available_adv_slot_id(uuid);
1728
1729         if (server_if == -1) {
1730                 ERR("Allocation of server instance failed");
1731                 g_free(user_data);
1732                 return status;
1733         } else {
1734                 user_data->instance_data = server_if;
1735                 DBG("Allocated new Advertising slot with Stack [%d]", user_data->instance_data);
1736         }
1737
1738         /*
1739          * As we need to provide async callback to user from HAL, simply schedule a
1740          * callback method which will carry actual result
1741          */
1742         memcpy(user_data->uuid.uu, uuid->uu, sizeof(bt_uuid_t));
1743         g_idle_add(__bt_hal_register_slot_id_cb, (gpointer)user_data);
1744
1745         /* If available, then return success, else return error */
1746         return BT_STATUS_SUCCESS;
1747 }
1748
1749 static void _bt_hal_remove_gatt_server_from_list(int server_if)
1750 {
1751         GSList *l;
1752         struct gatt_server_app *info = NULL;
1753
1754         for (l = gatt_server_apps; l != NULL;) {
1755                 info = (struct gatt_server_app*)l->data;
1756                 l = g_slist_next(l);
1757                 if (info == NULL)
1758                         continue;
1759                 if (info->slot == server_if) {
1760                         INFO("Found Matching GATT Server in List path[%s] slot [%d]",
1761                                 info->app_path, info->slot);
1762
1763                         /* Only if all services are deleted from the GATT Server, then only Unregister it.
1764                         Reason: it is possible, GATT Server app oly wants to disable multi advertising
1765                         In above case, only advertising block will be deallocated, Gatt Server will remain
1766                         unaffected */
1767                         if (info->services == NULL) {
1768                                 gatt_server_apps = g_slist_remove(gatt_server_apps, (gpointer)info);
1769                                 INFO("Total gatt server apps still existing after removing above is [%d]",
1770                                                 g_slist_length(gatt_server_apps));
1771
1772                                 /* DBUS Unregister only for current app */
1773                                 __bt_hal_gatt_deinit(info->app_path);
1774
1775                                 g_free(info->app_path);
1776                                 g_free(info);
1777                                 return;
1778                         } else {
1779                                 INFO("GATT Server still has services count[%d] in it..Can not remove it!!!",
1780                                         g_slist_length(info->services));
1781                         }
1782                 }
1783         }
1784 }
1785
1786 static bt_status_t gatt_server_unregister_app(int server_if)
1787 {
1788         CHECK_BTGATT_INIT();
1789         DBG("Un-Register server instance request [%d]", server_if);
1790
1791         if (_bt_hal_is_advertising_in_slot(server_if) == FALSE)
1792                 _bt_hal_free_server_slot(server_if);
1793
1794         /* If server_if belongs to a GATT Server, then delete the GATT server from List */
1795         _bt_hal_remove_gatt_server_from_list(server_if);
1796         return BT_STATUS_SUCCESS;
1797 }
1798
1799 static bt_status_t gatt_server_open(int server_if, const bt_bdaddr_t *bd_addr, bool is_direct)
1800 {
1801         CHECK_BTGATT_INIT();
1802         return BT_STATUS_SUCCESS;
1803 }
1804
1805 static void __le_disconnection_req_cb(GDBusProxy *proxy, GAsyncResult *res,
1806                 gpointer user_data)
1807 {
1808         GError *g_error = NULL;
1809         GVariant *reply = NULL;
1810
1811         DBG("+");
1812
1813         reply = g_dbus_proxy_call_finish(proxy, res, &g_error);
1814         g_object_unref(proxy);
1815         if (reply == NULL) {
1816                 ERR("Disconnect LE Dbus Call Error");
1817                 if (g_error) {
1818                         ERR("Error: %s\n", g_error->message);
1819                         g_clear_error(&g_error);
1820                 }
1821         }
1822         g_variant_unref(reply);
1823 }
1824
1825
1826 static bt_status_t gatt_server_close(int server_if, const bt_bdaddr_t *bd_addr, int conn_id)
1827 {
1828         CHECK_BTGATT_INIT();
1829
1830         char device_address[BT_HAL_ADDRESS_STRING_SIZE] = { 0 };
1831         gchar *device_path;
1832         GDBusProxy *device_proxy;
1833         GDBusConnection *g_conn;
1834         struct gatt_client_info_t *conn_info = NULL;
1835
1836         INFO("+");
1837
1838         if (NULL == bd_addr) {
1839                 ERR("bd_addr is NULL");
1840                 return BT_STATUS_PARM_INVALID;
1841         }
1842
1843         /* GDBUS Connection Info validate */
1844         g_conn = _bt_hal_get_system_gconn();
1845         if (g_conn == NULL) {
1846                 ERR("Could not get System DBUS Connection");
1847                 return  BT_STATUS_FAIL;
1848         }
1849
1850         /* Connection Info validate */
1851         conn_info = __bt_find_remote_gatt_client_info_from_conn(conn_id);
1852         if (conn_info == NULL) {
1853                 ERR("No Connection Inforamtion!!!");
1854                 return BT_STATUS_FAIL;
1855         }
1856
1857         _bt_hal_convert_addr_type_to_string(device_address,
1858                                 (unsigned char *)bd_addr->address);
1859
1860         //check if connection has the same device address
1861         if (g_strcmp0(conn_info->addr, device_address)  != 0) {
1862                 ERR("device address [%s] does not match", device_address);
1863                 return BT_STATUS_FAIL;
1864         }
1865
1866         device_path = _bt_hal_get_device_object_path(conn_info->addr);
1867         if (device_path == NULL) {
1868                 ERR("device_path is NULL");
1869                 return BT_STATUS_PARM_INVALID;
1870         }
1871
1872         device_proxy = g_dbus_proxy_new_sync(g_conn, G_DBUS_PROXY_FLAGS_NONE,
1873                         NULL, BT_HAL_BLUEZ_NAME,
1874                         device_path, BT_HAL_DEVICE_INTERFACE,  NULL, NULL);
1875
1876
1877         g_free(device_path);
1878         if (device_proxy == NULL)
1879                 return BT_STATUS_FAIL;
1880
1881         INFO("Disconnect LE [%s]", device_address);
1882
1883         g_dbus_proxy_call(device_proxy, "DisconnectLE",
1884                 NULL,
1885                 G_DBUS_CALL_FLAGS_NONE,
1886                 BT_HAL_MAX_DBUS_TIMEOUT,
1887                 NULL,
1888                 (GAsyncReadyCallback)__le_disconnection_req_cb, conn_info);
1889
1890         INFO("-");
1891         return BT_STATUS_SUCCESS;
1892 }
1893
1894 static char* __bt_hal_convert_uuid_to_string(bt_uuid_t *srvc_id)
1895 {
1896         char uuid_buf[5];
1897         const char *uuid;
1898
1899         uuid = btuuid2str(srvc_id->uu);
1900         DBG("Original UUID [%s]", uuid);
1901
1902         if (_bt_hal_uuid_is_standard(srvc_id) == TRUE) {
1903                 /* Extract Standard UUID string */
1904                 memcpy(uuid_buf, &uuid[4], 4);
1905                 uuid_buf[4] = '\0';
1906                 DBG("Converted string [%s]", uuid_buf);
1907                 return g_strdup(uuid_buf);
1908         } else
1909                 return strdup(uuid);
1910 }
1911
1912 static int __bt_hal_add_service_to_dbus(char *app_path, int slot, btgatt_srvc_id_t *srvc_id)
1913 {
1914         gboolean ret;
1915         /* For GATT service specific */
1916         GDBusNodeInfo *node_info;
1917         guint object_id;
1918         gchar *path = NULL;
1919         struct gatt_service_info *serv_info = NULL;
1920         GVariantBuilder *builder = NULL;
1921         GVariantBuilder *builder1 = NULL;
1922         GVariantBuilder *inner_builder = NULL;
1923         gboolean svc_primary = TRUE;
1924         GError *error = NULL;
1925         hal_gatt_service_added *user_data = NULL;
1926         DBG("Service add to DBUS slot [%d]", slot);
1927
1928         node_info = __bt_gatt_create_method_node_info(
1929                         service_introspection_xml);
1930
1931         if (node_info == NULL)
1932                 return BT_STATUS_FAIL;
1933
1934         DBG("Add new GATT Service: Current GATT Service handle [%d]", gatt_service_handle);
1935         path = g_strdup_printf("%s"GATT_SERV_OBJECT_PATH"%d", app_path, ++gatt_service_handle);
1936         DBG("gatt service path is [%s]", path);
1937
1938         object_id = g_dbus_connection_register_object(g_conn, path,
1939                         node_info->interfaces[0],
1940                         &serv_interface_vtable,
1941                         NULL, NULL, &error);
1942
1943         if (object_id == 0) {
1944                 ERR("failed to register: %s", error->message);
1945                 g_error_free(error);
1946                 g_free(path);
1947                 return BT_STATUS_FAIL;
1948         }
1949         /* Add object_id/gatt service information; it's required at the time of
1950          *  service unregister and Getmanagedobjects
1951          */
1952         serv_info = g_new0(struct gatt_service_info, 1);
1953
1954         serv_info->serv_path = g_strdup(path);
1955         serv_info->serv_id = object_id;
1956         serv_info->service_uuid = __bt_hal_convert_uuid_to_string(&srvc_id->id.uuid);//g_strdup(btuuid2str(srvc_id->id.uuid.uu));
1957         serv_info->is_svc_registered = FALSE;
1958         serv_info->is_svc_primary = svc_primary;
1959         DBG("Service Handle to be added is [%d]", gatt_service_handle);
1960         serv_info->service_handle = gatt_service_handle;
1961
1962         /* Update service in GATT Server service List */
1963         gatt_services = g_slist_append(gatt_services, serv_info);
1964
1965         /* emit interfacesadded signal here for service path */
1966         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
1967         inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1968
1969         g_variant_builder_add(inner_builder, "{sv}",
1970                         "UUID", g_variant_new_string(btuuid2str(srvc_id->id.uuid.uu)));
1971
1972         g_variant_builder_add(inner_builder, "{sv}",
1973                         "Primary", g_variant_new_boolean(svc_primary));
1974
1975         builder1 = g_variant_builder_new(G_VARIANT_TYPE("ao"));
1976
1977         g_variant_builder_add(inner_builder, "{sv}", "Characteristics",
1978                         g_variant_new("ao", builder1));
1979
1980         g_variant_builder_add(builder, "{sa{sv}}",
1981                         GATT_SERV_INTERFACE, inner_builder);
1982
1983         ret = g_dbus_connection_emit_signal(g_conn, NULL, "/",
1984                         "org.freedesktop.Dbus.ObjectManager",
1985                         "InterfacesAdded",
1986                         g_variant_new("(oa{sa{sv}})",
1987                                 path, builder),
1988                         &error);
1989         if (!ret) {
1990                 if (error) {
1991                         /* dbus gives error cause */
1992                         ERR("d-bus api failure: errcode[%x], message[%s]",
1993                                         error->code, error->message);
1994                         g_clear_error(&error);
1995                 }
1996         }
1997
1998         /* Send Service handle to application */
1999         user_data = g_malloc0(sizeof(hal_gatt_service_added));
2000         user_data->srvc_hdl = serv_info->service_handle;
2001         user_data->instance_data = slot;
2002         memcpy(user_data->uuid.uu, srvc_id->id.uuid.uu, sizeof(srvc_id->id.uuid.uu));
2003         g_idle_add(__bt_hal_gatt_service_added_cb, (gpointer)user_data);
2004
2005         /* Save newly created service in GATT Server's service list */
2006         _bt_hal_update_gatt_service_in_gatt_server(slot, serv_info);
2007
2008         /* Free data */
2009         g_free(path);
2010         g_variant_builder_unref(inner_builder);
2011         g_variant_builder_unref(builder);
2012         g_variant_builder_unref(builder1);
2013         return BT_STATUS_SUCCESS;
2014 }
2015
2016 static void __bt_hal_unregister_application_cb(GObject *object, GAsyncResult *res,
2017                 gpointer user_data)
2018 {
2019         char *app_path = (char*)user_data;
2020         INFO("UnregisterApplication is completed app [%s]", app_path);
2021         GError *error = NULL;
2022         GVariant *result = NULL;
2023
2024         if (manager_gproxy)
2025         result = g_dbus_proxy_call_finish(manager_gproxy, res, &error);
2026
2027         if (result == NULL) {
2028                 /* dBUS-RPC is failed */
2029                 ERR("Dbus-RPC is failed\n");
2030
2031                 if (error != NULL) {
2032                         /* dBUS gives error cause */
2033                         ERR("D-Bus API failure: errCode[%x], message[%s]\n",
2034                                         error->code, error->message);
2035                         g_clear_error(&error);
2036                 }
2037         } else {
2038                 g_variant_unref(result);
2039         }
2040         g_free(app_path);
2041 }
2042
2043 static void __bt_hal_gatt_deinit(char *app_path)
2044 {
2045         GDBusProxy *proxy = NULL;
2046         char *data;
2047         INFO("+");
2048
2049         /* Step1: Remove requested App */
2050         proxy = __bt_gatt_gdbus_get_gatt_manager_proxy("org.bluez",
2051                         "/org/bluez/hci0", "org.bluez.GattManager1");
2052
2053         if (proxy == NULL)
2054                 return;
2055
2056         INFO("UnregisterApplication : path [%s]", app_path);
2057
2058         /* Async Call to Unregister Service */
2059         data = g_strdup(app_path);
2060         g_dbus_proxy_call(proxy,
2061                         "UnregisterApplication",
2062                         g_variant_new("(o)",
2063                                 app_path),
2064                         G_DBUS_CALL_FLAGS_NONE, -1,
2065                         NULL,
2066                         (GAsyncReadyCallback)__bt_hal_unregister_application_cb,
2067                         (gpointer)data);
2068
2069         /* If requested app is last GATT Server app, then clean all resources */
2070         if (gatt_server_apps == NULL) {
2071                 INFO("All GATT servers are removed, clean all DBUS resources");
2072                 if (owner_id) {
2073                         /* unregister the exported interface for object manager */
2074                         g_dbus_connection_unregister_object(g_conn, manager_id);
2075                         manager_id = 0;
2076                         g_bus_unown_name(owner_id);
2077                         owner_id = 0;
2078
2079                         g_object_unref(manager_gproxy);
2080                         manager_gproxy = NULL;
2081                 }
2082         }
2083         INFO("-");
2084 }
2085
2086 static int __bt_hal_gatt_init(void)
2087 {
2088         DBG("+");
2089
2090         /* Only once for ALL GATT Servers */
2091         if (owner_id == 0) {
2092                 GError *err = NULL;
2093                 gchar *name = NULL;
2094
2095                 g_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
2096                 if (!g_conn) {
2097                         if (err) {
2098                                 ERR("Unable to connect to gdbus: %s", err->message);
2099                                 g_clear_error(&err);
2100                         }
2101                         return BT_STATUS_FAIL;
2102                 }
2103
2104                 name = g_strdup_printf("%s.p%d", BT_GATT_SERVICE_NAME, getpid());
2105                 INFO("well-known name: %s", name);
2106
2107                 owner_id = g_bus_own_name_on_connection(g_conn, name,
2108                                         G_BUS_NAME_OWNER_FLAGS_NONE,
2109                                         NULL, NULL, NULL, NULL);
2110         }
2111         INFO("Owner ID [%d]", owner_id);
2112
2113         /* Only once for ALL GATT Servers */
2114         if (manager_node_info == NULL) {
2115                 /* Register ObjectManager interface */
2116                 manager_node_info = __bt_gatt_create_method_node_info(
2117                                 manager_introspection_xml);
2118
2119                 if (manager_node_info == NULL) {
2120                         ERR("failed to get node info");
2121                         return BT_STATUS_FAIL;
2122                 }
2123         }
2124
2125         INFO("-");
2126         return BT_STATUS_SUCCESS;
2127 }
2128
2129 static void _bt_hal_is_gatt_server_initialzed(int slot, char **app_path)
2130 {
2131         GSList *l;
2132
2133         for (l = gatt_server_apps; l; l = g_slist_next(l)) {
2134                 struct gatt_server_app *app = (struct gatt_server_app *)l->data;
2135                 if (app->slot == slot) {
2136                         INFO("GATT Server app found app path [%s] instance [%d]",
2137                                         app->app_path, app->slot);
2138                         *app_path = app->app_path;
2139                         return;
2140                 }
2141         }
2142         /* GATT Server not found */
2143         *app_path = NULL;
2144 }
2145
2146 static void _bt_hal_update_gatt_server_path(int slot, char *app_path)
2147 {
2148         if (app_path == NULL)
2149                 return;
2150
2151         struct gatt_server_app *app = g_malloc0(sizeof(struct gatt_server_app));
2152         app->app_path = g_strdup(app_path);
2153         app->slot = slot;
2154         gatt_server_apps = g_slist_append(gatt_server_apps, app);
2155         INFO("GATT Server: Path [%s] Slot [%d]-> Updated", app_path, slot);
2156
2157 }
2158
2159
2160 static bt_status_t gatt_server_add_service(int server_if, btgatt_srvc_id_t *srvc_id,
2161                 int num_handles)
2162 {
2163         CHECK_BTGATT_INIT();
2164         char *app_path = NULL;
2165         GError *error = NULL;
2166         int *app_id = NULL;
2167
2168         int result = BT_STATUS_SUCCESS;
2169
2170         DBG("GATT Server Add service request: Instance ID[%d] Is Primary [%d] UUID [%s]",
2171                         server_if, srvc_id->is_primary, btuuid2str(srvc_id->id.uuid.uu));
2172
2173         /* Check if this GATT server Application is already registered with DBUS */
2174         _bt_hal_is_gatt_server_initialzed(server_if, &app_path);
2175
2176         if (app_path != NULL) {
2177                 DBG("GATT server path is already defined [%s]", app_path);
2178                 return __bt_hal_add_service_to_dbus(app_path, server_if, srvc_id);
2179         } else {
2180                 DBG("GATT server application path for instance [%d] is not defined yet", server_if);
2181                 result = __bt_hal_gatt_init();
2182                 if (result != BT_STATUS_SUCCESS)
2183                         return result;
2184
2185                 /* Only once for each GATT Server */
2186                 app_path = g_strdup_printf("/com/%d", server_if);
2187
2188                 app_id = g_malloc0(sizeof(int));
2189                 *app_id = server_if;
2190
2191                 manager_id = g_dbus_connection_register_object(g_conn, app_path,
2192                                 manager_node_info->interfaces[0],
2193                                 &manager_interface_vtable,
2194                                 (gpointer)app_id, NULL, &error);
2195
2196                 if (manager_id == 0) {
2197                         ERR("failed to register: %s", error->message);
2198                         g_error_free(error);
2199                         goto failed;
2200                 }
2201                 INFO("manager_id [%d]", manager_id);
2202
2203                 /* For current GATT Server, app_path is created, save it in Table */
2204                 _bt_hal_update_gatt_server_path(server_if, app_path);
2205
2206                 /* Add GATT Service to DBUS */
2207                 if (__bt_hal_add_service_to_dbus(app_path, server_if, srvc_id) != BT_STATUS_SUCCESS)
2208                         goto failed;
2209
2210                 g_free(app_path);
2211         }
2212
2213         INFO("Successfully added service");
2214         return BT_STATUS_SUCCESS;
2215 failed:
2216
2217         g_free(app_id);
2218
2219         if (app_path)
2220                 g_free(app_path);
2221         INFO("Service addition failed!!");
2222         return BT_STATUS_FAIL;
2223 }
2224
2225 static bt_status_t gatt_server_add_included_service(int server_if, int service_handle,
2226                 int included_handle)
2227 {
2228         CHECK_BTGATT_INIT();
2229         return BT_STATUS_SUCCESS;
2230 }
2231
2232
2233 static gboolean __bt_is_service_last_in_server_list(int instance, int service_handle)
2234 {
2235         GSList *l;
2236         GSList *gatt_services = NULL;
2237         int len;
2238         struct gatt_service_info *info = NULL;
2239
2240         gatt_services =  _bt_get_service_list_from_server(instance);
2241
2242         len = g_slist_length(gatt_services);
2243         l = g_slist_nth(gatt_services, len -1);
2244         if (NULL == l)
2245                 return FALSE;
2246
2247         info = l->data;
2248
2249         if (info->service_handle == service_handle)
2250                 return TRUE;
2251         return FALSE;
2252 }
2253
2254 static struct gatt_service_info *__bt_gatt_find_gatt_service_info(int instance,
2255                 int service_handle)
2256 {
2257         GSList *l;
2258         GSList *gatt_services = NULL;
2259         INFO("Find Service: svc handle [%d] from GATT Server slot[%d]", service_handle, instance);
2260
2261         gatt_services = _bt_get_service_list_from_server(instance);
2262
2263         for (l = gatt_services; l != NULL; l = g_slist_next(l)) {
2264                 struct gatt_service_info *info = l->data;
2265                 INFO("Got one service with handle [%d]", info->service_handle);
2266                 if (info->service_handle == service_handle)
2267                         return info;
2268         }
2269         ERR("Gatt service with handle [%d] not found", service_handle);
2270         return NULL;
2271 }
2272
2273 static bt_status_t gatt_server_add_characteristic(int slot, int service_handle,
2274                 bt_uuid_t *uuid, int properties,
2275                 int permissions)
2276 {
2277         gboolean ret;
2278         GError *error = NULL;
2279         guint object_id;
2280         GDBusNodeInfo *node_info;
2281         gchar *path = NULL;
2282         GVariantBuilder *builder = NULL;
2283         GVariantBuilder *inner_builder = NULL;
2284         struct gatt_service_info *serv_info = NULL;
2285         struct gatt_char_info *char_info = NULL;
2286         GVariantBuilder *builder2 = NULL;
2287         GVariantBuilder *builder3 = NULL;
2288         GVariant *flags_val = NULL;
2289         int i = 0;
2290         char *char_flags[NUMBER_OF_FLAGS];
2291         int flag_count = 0;
2292         hal_gatt_char_added *user_data = NULL;
2293         int *app_id;
2294
2295         CHECK_BTGATT_INIT();
2296         DBG("Add new characteristic to GATT Service handle [%d]", service_handle);
2297         DBG("Properties of new charateristic [%d] Permission [%d]", properties, permissions);
2298
2299         serv_info = __bt_gatt_find_gatt_service_info(slot, service_handle);
2300         if (serv_info == NULL)
2301                 return BT_STATUS_FAIL;
2302
2303         node_info = __bt_gatt_create_method_node_info(
2304                         characteristics_introspection_xml);
2305
2306         if (node_info == NULL)
2307                 return BT_STATUS_FAIL;
2308
2309         DBG("Add new GATT characteristic: Current GATT char handle [%d]", gatt_char_handle);
2310         path = g_strdup_printf("%s/characteristic%d", serv_info->serv_path, ++gatt_char_handle);
2311         DBG("gatt characteristic path is [%s]", path);
2312
2313         app_id = g_malloc0(sizeof(int));
2314         *app_id = slot;
2315
2316         object_id = g_dbus_connection_register_object(g_conn, path,
2317                         node_info->interfaces[0],
2318                         &char_interface_vtable,
2319                         (gpointer)app_id, NULL, &error);
2320
2321         if (object_id == 0) {
2322                 ERR("failed to register: %s", error->message);
2323                 g_error_free(error);
2324                 g_free(path);
2325                 g_free(app_id);
2326                 return BT_STATUS_FAIL;
2327         }
2328
2329         if (permissions & BT_HAL_GATT_PERMISSION_ENCRYPT_READ)
2330                 properties |= BT_HAL_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_READ;
2331         if (permissions & BT_HAL_GATT_PERMISSION_ENCRYPT_AUTHENTICATED_READ)
2332                 properties |= BT_HAL_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_READ;
2333         if (permissions & BT_HAL_GATT_PERMISSION_ENCRYPT_WRITE)
2334                 properties |= BT_HAL_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_WRITE;
2335         if (permissions & BT_HAL_GATT_PERMISSION_ENCRYPT_AUTHENTICATED_WRITE)
2336                 properties |= BT_HAL_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_WRITE;
2337
2338         flag_count = bt_hal_gatt_convert_prop2string(properties, char_flags);
2339
2340         char_info = g_new0(struct gatt_char_info, 1);
2341
2342         char_info->char_path = g_strdup(path);
2343         char_info->char_id = object_id;
2344         char_info->char_uuid = __bt_hal_convert_uuid_to_string(uuid);//g_strdup(btuuid2str(uuid->uu));
2345         for (i = 0; i < flag_count; i++)
2346                 char_info->char_flags[i] = char_flags[i];
2347
2348
2349         char_info->flags_length = flag_count;
2350         char_info->char_handle = gatt_char_handle;
2351
2352
2353         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
2354         inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
2355
2356         g_variant_builder_add(inner_builder, "{sv}", "UUID",
2357                         g_variant_new("s", char_info->char_uuid));
2358         g_variant_builder_add(inner_builder, "{sv}", "Service",
2359                         g_variant_new("o", serv_info->serv_path));
2360
2361         builder2 = g_variant_builder_new(G_VARIANT_TYPE("as"));
2362
2363         for (i = 0; i < flag_count; i++)
2364                 g_variant_builder_add(builder2, "s", char_flags[i]);
2365
2366         flags_val = g_variant_new("as", builder2);
2367         g_variant_builder_add(inner_builder, "{sv}", "Flags",
2368                         flags_val);
2369
2370         builder3 = g_variant_builder_new(G_VARIANT_TYPE("ao"));
2371
2372         g_variant_builder_add(inner_builder, "{sv}", "Descriptors",
2373                         g_variant_new("ao", builder3));
2374
2375         g_variant_builder_add(builder, "{sa{sv}}",
2376                         GATT_CHAR_INTERFACE,
2377                         inner_builder);
2378
2379         ret = g_dbus_connection_emit_signal(g_conn, NULL, "/",
2380                         "org.freedesktop.Dbus.ObjectManager",
2381                         "InterfacesAdded",
2382                         g_variant_new("(oa{sa{sv}})",
2383                                 path, builder),
2384                         &error);
2385         if (!ret) {
2386                 if (error) {
2387                         /* dbus gives error cause */
2388                         ERR("d-bus api failure: errcode[%x], message[%s]",
2389                                         error->code, error->message);
2390                         g_clear_error(&error);
2391                 }
2392         }
2393
2394         //*char_path = g_strdup(path);
2395
2396         //new_char = TRUE;
2397
2398
2399         /* Send Service handle to application */
2400         user_data = g_malloc0(sizeof(hal_gatt_char_added));
2401         user_data->srvc_hdl = serv_info->service_handle;
2402         user_data->char_hdl = gatt_char_handle;
2403         user_data->instance_data = slot;
2404         memcpy(user_data->uuid.uu, uuid->uu, sizeof(uuid->uu));
2405         g_idle_add(__bt_hal_gatt_char_added_cb, (gpointer)user_data);
2406
2407         /* Save newly created charatcristic to GATT Server's service's characteristic  list */
2408         serv_info->char_data = g_slist_append(serv_info->char_data, char_info);
2409
2410         /* Free data */
2411         g_free(path);
2412
2413         g_variant_builder_unref(inner_builder);
2414         g_variant_builder_unref(builder);
2415         g_variant_builder_unref(builder2);
2416         g_variant_builder_unref(builder3);
2417
2418         return BT_STATUS_SUCCESS;
2419 }
2420
2421 static bt_status_t gatt_server_add_descriptor(int slot, int service_handle, bt_uuid_t *uuid,
2422                 int permissions)
2423 {
2424         CHECK_BTGATT_INIT();
2425
2426 //      static int desc_id = 1;
2427         GError *error = NULL;
2428         guint object_id;
2429         GDBusNodeInfo *node_info;
2430         gchar *path = NULL;
2431         GVariantBuilder *builder = NULL;
2432         GVariantBuilder *inner_builder = NULL;
2433
2434         struct gatt_char_info *char_info = NULL;
2435         struct gatt_desc_info *desc_info = NULL;
2436         struct gatt_service_info *serv_info = NULL;
2437
2438         gchar **line_argv = NULL;
2439         char *serv_path;
2440         char *char_path = NULL;
2441         GSList *l;
2442
2443         GVariantBuilder *builder2 = NULL;
2444         GVariant *flags_val = NULL;
2445         int i = 0;
2446         char *desc_flags[NUMBER_OF_FLAGS];
2447         int flag_count = 0;
2448         int *app_id;
2449
2450         hal_gatt_desc_added *user_data = NULL;
2451 #if 0
2452         if (new_char) {
2453                 desc_id = 1;
2454                 new_char = FALSE;
2455         }
2456 #endif
2457         /* Fetch service data for the GATT server */
2458         serv_info = __bt_gatt_find_gatt_service_info(slot, service_handle);
2459         if (serv_info == NULL)
2460                 return BT_STATUS_FAIL;
2461
2462         /* Fetch list of characteristics from the service info */
2463         l = serv_info->char_data;
2464
2465         /* Fetch last char info from the characteristic list */
2466         char_info = g_slist_last(l)->data;
2467         if (char_info == NULL)
2468                 return BT_STATUS_FAIL;
2469
2470         /* Fetch characteristic path from char info */
2471         char_path = char_info->char_path;
2472
2473         line_argv = g_strsplit_set(char_path, "/", 0);
2474         serv_path = g_strdup_printf("/%s/%s/%s", line_argv[1], line_argv[2], line_argv[3]);
2475
2476
2477         node_info = __bt_gatt_create_method_node_info(
2478                         descriptor_introspection_xml);
2479
2480         if (node_info == NULL) {
2481                 g_strfreev(line_argv);
2482                 g_free(serv_path);
2483                 return BT_STATUS_FAIL;
2484         }
2485
2486         DBG("Add new Descriptor: Current GATT desc handle [%d]", gatt_desc_handle);
2487
2488         path = g_strdup_printf("%s/descriptor%d", char_path, ++gatt_desc_handle);
2489         DBG("gatt descriptor path is [%s]", path);
2490
2491         app_id = g_malloc0(sizeof(int));
2492         *app_id = slot;
2493
2494         object_id = g_dbus_connection_register_object(g_conn, path,
2495                         node_info->interfaces[0],
2496                         &desc_interface_vtable,
2497                         (gpointer)app_id, NULL, &error);
2498
2499         if (object_id == 0) {
2500                 ERR("failed to register: %s", error->message);
2501                 g_error_free(error);
2502                 g_free(path);
2503                 g_strfreev(line_argv);
2504                 g_free(serv_path);
2505                 g_free(app_id);
2506                 return BT_STATUS_FAIL;
2507         }
2508
2509         flag_count = bt_hal_gatt_convert_perm2string(permissions, desc_flags);
2510
2511         desc_info = g_new0(struct gatt_desc_info, 1);
2512
2513         desc_info->desc_path = g_strdup(path);
2514         desc_info->desc_id = object_id;
2515         desc_info->desc_uuid = __bt_hal_convert_uuid_to_string(uuid);//g_strdup(btuuid2str(uuid->uu));
2516
2517         for (i = 0; i < flag_count; i++)
2518                 desc_info->desc_flags[i] = desc_flags[i];
2519
2520         desc_info->flags_length = flag_count;
2521         desc_info->desc_handle = gatt_desc_handle;
2522
2523
2524         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
2525         inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
2526
2527         g_variant_builder_add(inner_builder, "{sv}", "UUID",
2528                         g_variant_new("s", btuuid2str(uuid->uu)));
2529         g_variant_builder_add(inner_builder, "{sv}", "Characteristic",
2530                         g_variant_new("o", char_path));
2531
2532         builder2 = g_variant_builder_new(G_VARIANT_TYPE("as"));
2533
2534         for (i = 0; i < flag_count; i++)
2535                 g_variant_builder_add(builder2, "s", desc_flags[i]);
2536
2537         flags_val = g_variant_new("as", builder2);
2538         g_variant_builder_add(inner_builder, "{sv}", "Flags",
2539                         flags_val);
2540
2541         g_variant_builder_add(builder, "{sa{sv}}",
2542                         GATT_DESC_INTERFACE,
2543                         inner_builder);
2544
2545         g_dbus_connection_emit_signal(g_conn, NULL, "/",
2546                         "org.freedesktop.Dbus.ObjectManager",
2547                         "InterfacesAdded",
2548                         g_variant_new("(oa{sa{sv}})",
2549                                 path, builder),
2550                         &error);
2551         if (error != NULL) {
2552                 ERR("g_dbus_connection_emit_signal failed: errCode[%x], message[%s]",
2553                                 error->code, error->message);
2554                 g_clear_error(&error);
2555         }
2556
2557         //*desc_path = g_strdup(path);
2558
2559         /* Save newly created descriptor to GATT server's service's characteristic */
2560         char_info->desc_data = g_slist_append(char_info->desc_data, desc_info);
2561
2562         /* Send descriptor handle to application */
2563         user_data = g_malloc0(sizeof(hal_gatt_desc_added));
2564         user_data->srvc_hdl = serv_info->service_handle;
2565         user_data->desc_hdl = gatt_desc_handle;
2566         user_data->instance_data = slot;
2567         memcpy(user_data->uuid.uu, uuid->uu, sizeof(uuid->uu));
2568         g_idle_add(__bt_hal_gatt_desc_added_cb, (gpointer)user_data);
2569
2570         /* Free data */
2571         g_free(path);
2572         g_free(serv_path);
2573         g_strfreev(line_argv);
2574         g_variant_builder_unref(inner_builder);
2575         g_variant_builder_unref(builder);
2576         return BT_STATUS_SUCCESS;
2577 }
2578
2579 static void __bt_hal_register_application_cb(GObject *object, GAsyncResult *res, gpointer user_data)
2580 {
2581         GError *error = NULL;
2582         GVariant *result;
2583         char *data = (char*) user_data;
2584         INFO("RegisterApplication is completed path [%s]", data);
2585
2586         result = g_dbus_proxy_call_finish(manager_gproxy, res, &error);
2587
2588         if (result == NULL) {
2589                 /* dBUS-RPC is failed */
2590                 ERR("Dbus-RPC is failed\n");
2591
2592                 if (error != NULL) {
2593                         /* dBUS gives error cause */
2594                         ERR("D-Bus API failure: errCode[%x], message[%s]\n",
2595                                         error->code, error->message);
2596                         g_clear_error(&error);
2597                 }
2598         } else {
2599                 g_variant_unref(result);
2600         }
2601         g_free(data);
2602 }
2603
2604 static GDBusProxy *__bt_hal_gatt_gdbus_init_manager_proxy(const gchar *service,
2605                 const gchar *path, const gchar *interface)
2606 {
2607         GDBusProxy *proxy;
2608         GError *err = NULL;
2609
2610         if (g_conn == NULL)
2611                 g_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM,
2612                                 NULL, &err);
2613
2614         if (!g_conn) {
2615                 if (err) {
2616                         ERR("Unable to connect to gdbus: %s", err->message);
2617                         g_clear_error(&err);
2618                 }
2619                 return NULL;
2620         }
2621
2622         proxy =  g_dbus_proxy_new_sync(g_conn,
2623                         G_DBUS_PROXY_FLAGS_NONE, NULL,
2624                         service, path,
2625                         interface, NULL, &err);
2626
2627         if (!proxy) {
2628                 if (err) {
2629                         ERR("Unable to create proxy: %s", err->message);
2630                         g_clear_error(&err);
2631                 }
2632                 return NULL;
2633         }
2634         manager_gproxy = proxy;
2635
2636         return proxy;
2637 }
2638
2639 static GDBusProxy *__bt_gatt_gdbus_get_gatt_manager_proxy(const gchar *service,
2640                 const gchar *path, const gchar *interface)
2641 {
2642         return (manager_gproxy) ? manager_gproxy :
2643                 __bt_hal_gatt_gdbus_init_manager_proxy(service,
2644                                 path, interface);
2645 }
2646
2647 static void __bt_register_application_to_dbus(int slot)
2648 {
2649         GDBusProxy *proxy = NULL;
2650         char *app_path = NULL;
2651         char *data;
2652         DBG("RegisterApplication slot [%d]", slot);
2653
2654         /* It is impossible that app path is still not initialized */
2655         _bt_hal_is_gatt_server_initialzed(slot, &app_path);
2656
2657         proxy = __bt_gatt_gdbus_get_gatt_manager_proxy("org.bluez",
2658                         "/org/bluez/hci0", "org.bluez.GattManager1");
2659
2660         data = g_strdup(app_path);
2661         g_dbus_proxy_call(proxy,
2662                         "RegisterApplication",
2663                         g_variant_new("(oa{sv})",
2664                                 app_path, NULL),
2665                         G_DBUS_CALL_FLAGS_NONE, -1,
2666                         NULL,
2667                         (GAsyncReadyCallback)__bt_hal_register_application_cb,
2668                         (gpointer)data);
2669         INFO("GATT server started");
2670 }
2671
2672 static bt_status_t gatt_server_start_service(int server_if, int service_handle, int transport)
2673 {
2674         CHECK_BTGATT_INIT();
2675         struct gatt_service_info *serv_info = NULL;
2676         hal_gatt_service_started *user_data = NULL;
2677         DBG("Start GATT Service svc hdl [%d], slot [%d]", service_handle, server_if);
2678
2679         /* Fetch service data for the GATT server */
2680         serv_info = __bt_gatt_find_gatt_service_info(server_if, service_handle);
2681         if (serv_info == NULL)
2682                 return BT_STATUS_FAIL;
2683
2684         if (serv_info->is_svc_registered)
2685                 DBG("service already registered \n");
2686
2687         serv_info->is_svc_registered = TRUE;
2688
2689         /* Send Service handle to application */
2690         user_data = g_malloc0(sizeof(hal_gatt_service_started));
2691         user_data->srvc_hdl = serv_info->service_handle;
2692         user_data->instance_data = server_if;
2693         g_idle_add(__bt_hal_gatt_service_started_cb, (gpointer)user_data);
2694
2695         /* If this is nth Service that is started, then register application at this point */
2696         if (__bt_is_service_last_in_server_list(server_if, service_handle)) {
2697                 DBG("This is the last service started from the GATT Server's list of services handle [%d]",
2698                         service_handle);
2699                 __bt_register_application_to_dbus(server_if);
2700         }
2701
2702         return BT_STATUS_SUCCESS;
2703 }
2704
2705 static bt_status_t gatt_server_stop_service(int server_if, int service_handle)
2706 {
2707         CHECK_BTGATT_INIT();
2708         INFO("Stop service successful");
2709         return BT_STATUS_SUCCESS;
2710 }
2711
2712 static bt_status_t gatt_server_delete_service(int server_if, int service_handle)
2713 {
2714         CHECK_BTGATT_INIT();
2715         struct gatt_service_info *serv_info = NULL;
2716         hal_gatt_service_deleted *user_data = NULL;
2717         GSList *l = NULL;
2718         GSList *l1 = NULL;
2719         int err = BT_STATUS_SUCCESS;
2720         int ret = BT_STATUS_SUCCESS;
2721         INFO("Slot [%d] service handle [%d]", server_if, service_handle);
2722
2723         /* Fetch service data for the GATT server */
2724         serv_info = __bt_gatt_find_gatt_service_info(server_if, service_handle);
2725         if (serv_info == NULL) {
2726                 ERR("Could not find service info svc handle [%d] server slot [%d]",
2727                                 service_handle, server_if);
2728                 return BT_STATUS_FAIL;
2729         }
2730
2731         if (serv_info->is_svc_registered == FALSE) {
2732                 ERR("service Not registered path [%s] handle [%d]",
2733                         serv_info->serv_path, service_handle);
2734         }
2735
2736         for (l = serv_info->char_data; l != NULL; l = g_slist_next(l)) {
2737                 struct gatt_char_info *char_info = l->data;
2738
2739                 if (char_info == NULL)
2740                         break;
2741
2742                 for (l1 = char_info->desc_data; l1 != NULL; l1 = g_slist_next(l1)) {
2743                         struct gatt_desc_info *desc_info = l1->data;
2744
2745                         if (desc_info == NULL)
2746                                 break;
2747
2748                         ret = g_dbus_connection_unregister_object(g_conn,
2749                                         desc_info->desc_id);
2750                         if (ret) {
2751                                 __bt_hal_gatt_emit_interface_removed(
2752                                                 desc_info->desc_path,
2753                                                 GATT_DESC_INTERFACE);
2754                         } else {
2755                                 err = BT_STATUS_FAIL;
2756                         }
2757
2758                         /* list remove & free */
2759                         char_info->desc_data = g_slist_remove(char_info->desc_data, desc_info);
2760                         __bt_hal_gatt_free_descriptor_info(desc_info);
2761                 }
2762
2763                 g_slist_free(char_info->desc_data);
2764                 char_info->desc_data = NULL;
2765
2766                 ret = g_dbus_connection_unregister_object(g_conn,
2767                                 char_info->char_id);
2768                 if (ret) {
2769                         __bt_hal_gatt_emit_interface_removed(char_info->char_path,
2770                                         GATT_CHAR_INTERFACE);
2771                 } else {
2772                         INFO("Err");
2773                         err = BT_STATUS_FAIL;
2774                 }
2775
2776                 /* list remove & free */
2777                 serv_info->char_data = g_slist_remove(serv_info->char_data, char_info);
2778                 __bt_hal_gatt_free_characteristic_info(char_info);
2779         }
2780
2781         g_slist_free(serv_info->char_data);
2782         serv_info->char_data = NULL;
2783
2784         ret = g_dbus_connection_unregister_object(g_conn, serv_info->serv_id);
2785         if (ret) {
2786                 __bt_hal_gatt_emit_interface_removed(serv_info->serv_path,
2787                                 GATT_SERV_INTERFACE);
2788         } else {
2789                 INFO("Failed!!");
2790                 err = BT_STATUS_FAIL;
2791         }
2792
2793         ret = g_dbus_connection_unregister_object(g_conn, serv_info->prop_id);
2794         if (ret)
2795                 DBG("Unregistered the service on properties interface");
2796
2797         /* Remove from global list */
2798         gatt_services = g_slist_remove(gatt_services, serv_info);
2799         INFO("After removing from global list total service dount [%d]", g_slist_length(gatt_services));
2800
2801         /* Remove from GATT Server's list of services */
2802         _bt_remote_service_from_gatt_server(server_if, service_handle);
2803
2804         if (gatt_services == NULL)
2805                 INFO("All GATT Services of all GATT Servers are unregistered");
2806
2807         if (err == BT_STATUS_SUCCESS) {
2808                 INFO("Send GATT Service deleted Event");
2809                 /* Send Service handle to application */
2810                 user_data = g_malloc0(sizeof(hal_gatt_service_deleted));
2811                 user_data->srvc_hdl = serv_info->service_handle;
2812                 user_data->instance_data = server_if;
2813                 g_idle_add(__bt_hal_gatt_service_deleted_cb, (gpointer)user_data);
2814         }
2815
2816         /* Free the service */
2817         __bt_hal_gatt_free_service_info(serv_info);
2818         return err;
2819 }
2820
2821 static gboolean __bt_gatt_get_service_state(const char *service_path)
2822 {
2823         struct gatt_service_info *svc_info = NULL;
2824         GSList *l = NULL;
2825         for (l = gatt_services; l; l = g_slist_next(l)) {
2826
2827                 svc_info = (struct gatt_service_info *)l->data;
2828
2829
2830                 if (!g_strcmp0(svc_info->serv_path, service_path)) {
2831                         DBG("Return the state of the gatt service %d",
2832                                         svc_info->is_svc_registered);
2833                         return svc_info->is_svc_registered;
2834                 }
2835         }
2836
2837         DBG("gatt service info is NULL");
2838         return FALSE;
2839 }
2840
2841 static bt_status_t gatt_server_send_indication(int server_if, int attribute_handle, int conn_id,
2842                 int len, int confirm, char* p_value)
2843 {
2844         CHECK_BTGATT_INIT();
2845
2846         /* For Notifying */
2847         GVariantBuilder *outer_builder;
2848         GVariantBuilder *invalidated_builder;
2849
2850         /* For Value update via PropertyChange */
2851         GVariantBuilder *outer_builder1;
2852         GVariantBuilder *inner_builder1;
2853         GVariantBuilder *invalidated_builder1;
2854         GVariant *update_value = NULL;
2855
2856         /* Other variables */
2857         struct gatt_client_info_t *conn_info = NULL;
2858         gchar *serv_path = NULL;
2859         char *char_path = NULL;
2860         gchar **line_argv = NULL;
2861         gboolean notify = TRUE;
2862         gboolean ret = TRUE;
2863         int err = BT_STATUS_SUCCESS;
2864         char addr[20];
2865         GError *error = NULL;
2866         int i;
2867
2868         memset(addr, 0x00, sizeof(addr));
2869
2870         conn_info = __bt_find_remote_gatt_client_info_from_conn(conn_id);
2871         if (conn_info == NULL) {
2872                 ERR("No Connection Inforamtion!!!");
2873                 return BT_STATUS_FAIL;
2874         }
2875
2876         DBG("Send Indication to GATT client addr [%s]", conn_info->addr);
2877
2878         char_path = __bt_gatt_find_char_path_from_handle(attribute_handle);
2879         if (char_path == NULL)
2880                 return BT_STATUS_FAIL;
2881
2882         line_argv = g_strsplit_set(char_path, "/", 0);
2883         serv_path = g_strdup_printf("/%s/%s/%s", line_argv[1], line_argv[2], line_argv[3]);
2884
2885         if (!__bt_gatt_get_service_state(serv_path)) {
2886                 DBG("service not registered for this characteristic \n");
2887                 g_free(serv_path);
2888                 g_strfreev(line_argv);
2889                 return BT_STATUS_FAIL;
2890         }
2891
2892         outer_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
2893         invalidated_builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
2894
2895         g_variant_builder_add(outer_builder, "{sv}", "Notifying",
2896                         g_variant_new("b", notify));
2897
2898         _bt_hal_convert_addr_type_to_string(addr, (unsigned char *)conn_info->addr);
2899
2900         g_variant_builder_add(outer_builder, "{sv}", "Unicast",
2901                         g_variant_new("s", addr));
2902
2903         DBG("Set characteristic Notification \n");
2904         ret = g_dbus_connection_emit_signal(g_conn, NULL,
2905                         char_path,
2906                         "org.freedesktop.DBus.Properties",
2907                         "PropertiesChanged",
2908                         g_variant_new("(sa{sv}as)",
2909                                 "org.bluez.GattCharacteristic1",
2910                                 outer_builder, invalidated_builder),
2911                         &error);
2912
2913         if (!ret) {
2914                 if (error != NULL) {
2915                         ERR("D-Bus API failure: errCode[%x], \
2916                                         message[%s]",
2917                                         error->code, error->message);
2918                         g_clear_error(&error);
2919                 }
2920                 err = BT_STATUS_FAIL;
2921         }
2922
2923         g_variant_builder_unref(outer_builder);
2924         g_variant_builder_unref(invalidated_builder);
2925
2926
2927         /* Notifying Done, now update Value to Bluez via PropertyChanged */
2928         invalidated_builder1 = g_variant_builder_new(G_VARIANT_TYPE("as"));
2929
2930         inner_builder1 = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
2931         for (i = 0; i < len; i++)
2932                 g_variant_builder_add(inner_builder1, "y", p_value[i]);
2933
2934         update_value = g_variant_new("ay", inner_builder1);
2935
2936
2937         outer_builder1 = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
2938         g_variant_builder_add(outer_builder1, "{sv}", "Value",
2939                         update_value);
2940
2941         DBG("Updating characteristic value \n");
2942         ret = g_dbus_connection_emit_signal(g_conn, NULL,
2943                         char_path,
2944                         "org.freedesktop.DBus.Properties",
2945                         "PropertiesChanged",
2946                         g_variant_new("(sa{sv}as)",
2947                                 "org.bluez.GattCharacteristic1",
2948                                 outer_builder1, invalidated_builder1),
2949                         &error);
2950
2951         if (!ret) {
2952                 if (error != NULL) {
2953                         ERR("D-Bus API failure: errCode[%x], \
2954                                         message[%s]",
2955                                         error->code, error->message);
2956                         g_clear_error(&error);
2957                 }
2958                 err = BT_STATUS_FAIL;
2959         } else {
2960                 struct gatt_char_info *char_info = NULL;
2961
2962                 char_info = __bt_gatt_find_char_info_from_handle(attribute_handle);
2963                 if (char_info == NULL) {
2964                         g_free(serv_path);
2965                         g_strfreev(line_argv);
2966                         g_variant_builder_unref(inner_builder1);
2967                         g_variant_builder_unref(outer_builder1);
2968                         g_variant_builder_unref(invalidated_builder1);
2969
2970                         return BT_STATUS_FAIL;
2971                 }
2972
2973                 char_info->value_length = len;
2974
2975                 char_info->char_value = (char *)realloc(char_info->char_value, len);
2976                 if (char_info->char_value) {
2977                         for (i = 0; i < len; i++)
2978                                 char_info->char_value[i] = p_value[i];
2979                 }
2980         }
2981
2982         g_free(serv_path);
2983         g_strfreev(line_argv);
2984         g_variant_builder_unref(inner_builder1);
2985         g_variant_builder_unref(outer_builder1);
2986         g_variant_builder_unref(invalidated_builder1);
2987
2988         return err;
2989 }
2990
2991 static bt_status_t gatt_server_send_response(int conn_id, int trans_id,
2992                 int status, btgatt_response_t *response)
2993 {
2994         CHECK_BTGATT_INIT();
2995
2996         struct gatt_req_info *req_info = NULL;
2997         struct gatt_client_info_t *conn_info = NULL;
2998         int i;
2999
3000         DBG("GATT Server Send Response Conn ID [%d]", conn_id);
3001
3002         conn_info = __bt_find_remote_gatt_client_info_from_conn(conn_id);
3003         if (conn_info == NULL) {
3004                 ERR("No Connection Inforamtion!!!");
3005                 return BT_STATUS_FAIL;
3006         }
3007
3008         req_info =  __bt_find_remote_gatt_client_request_info(conn_id, trans_id);
3009         if (req_info == NULL) {
3010                 ERR("No Request Inforamtion!!!");
3011                 return BT_STATUS_FAIL;
3012         }
3013
3014         if (status != BT_STATUS_SUCCESS) {
3015                 ERR("resp_state is 0x%X", status);
3016
3017                 g_dbus_method_invocation_return_dbus_error(req_info->context,
3018                                 "org.bluez.Error.Failed", "Application Error");
3019
3020                 conn_info->gatt_req_info_list = g_slist_remove(conn_info->gatt_req_info_list, req_info);
3021
3022                 req_info->context = NULL;
3023                 if (req_info->attr_path)
3024                         g_free(req_info->attr_path);
3025                 if (req_info->svc_path)
3026                         g_free(req_info->svc_path);
3027                 g_free(req_info);
3028
3029                 return BT_STATUS_SUCCESS;
3030         }
3031
3032         DBG("Reponse Value length [%d]", response->attr_value.len);
3033         DBG("Request type: [%d]", req_info->request_type);
3034
3035         /* DEBUG */
3036         for (i = 0; i < response->attr_value.len; i++)
3037                 DBG("Resonse [%d] = [0x%x]", i, response->attr_value.value[i]);
3038
3039         if (req_info->request_type == BT_HAL_GATT_REQUEST_TYPE_READ) {
3040                 GVariantBuilder *inner_builder = NULL;
3041                 inner_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
3042
3043                 if (response->attr_value.len > 0) {
3044                         for (i = 0; i < response->attr_value.len; i++)
3045                                 g_variant_builder_add(inner_builder, "y", response->attr_value.value[i]);
3046                 }
3047                 g_dbus_method_invocation_return_value(req_info->context,
3048                                 g_variant_new("(ay)", inner_builder));
3049
3050                 g_variant_builder_unref(inner_builder);
3051         } else {
3052                 g_dbus_method_invocation_return_value(req_info->context, NULL);
3053         }
3054         conn_info->gatt_req_info_list = g_slist_remove(conn_info->gatt_req_info_list, req_info);
3055
3056         req_info->context = NULL;
3057         if (req_info->attr_path)
3058                 g_free(req_info->attr_path);
3059         if (req_info->svc_path)
3060                 g_free(req_info->svc_path);
3061         g_free(req_info);
3062
3063         return BT_STATUS_SUCCESS;
3064 }
3065
3066
3067 static bt_status_t gatt_server_send_acquire_response(int conn_id, int trans_id,
3068                 int status, int fd, int mtu, void *fdlist)
3069 {
3070         CHECK_BTGATT_INIT();
3071
3072         struct gatt_req_info *req_info = NULL;
3073         struct gatt_client_info_t *conn_info = NULL;
3074
3075         DBG("GATT Server Send Response Conn ID [%d]", conn_id);
3076
3077         conn_info = __bt_find_remote_gatt_client_info_from_conn(conn_id);
3078         if (conn_info == NULL) {
3079                 ERR("No Connection Inforamtion!!!");
3080                 return BT_STATUS_FAIL;
3081         }
3082
3083         req_info =  __bt_find_remote_gatt_client_request_info(conn_id, trans_id);
3084         if (req_info == NULL) {
3085                 ERR("No Request Inforamtion!!!");
3086                 return BT_STATUS_FAIL;
3087         }
3088
3089         if (status != BT_STATUS_SUCCESS) {
3090                 ERR("resp_state is 0x%X", status);
3091
3092                 g_dbus_method_invocation_return_dbus_error(req_info->context,
3093                                 "org.bluez.Error.Failed", "Application Error");
3094
3095                 conn_info->gatt_req_info_list = g_slist_remove(conn_info->gatt_req_info_list, req_info);
3096
3097                 req_info->context = NULL;
3098                 if (req_info->attr_path)
3099                         g_free(req_info->attr_path);
3100                 if (req_info->svc_path)
3101                         g_free(req_info->svc_path);
3102                 g_free(req_info);
3103
3104                 return BT_STATUS_SUCCESS;
3105         }
3106
3107
3108          if (req_info->request_type == BT_HAL_GATT_REQUEST_TYPE_ACQUIRE_WRITE) {
3109
3110                 INFO("GATT Server Send Response BT_HAL_GATT_REQUEST_TYPE_ACQUIRE_WRITE to bluez  %d\n", fd);
3111                 g_dbus_method_invocation_return_value_with_unix_fd_list(
3112                                 req_info->context, g_variant_new("(hq)", 0, mtu), (GUnixFDList*)fdlist);
3113
3114         } else if (req_info->request_type == BT_HAL_GATT_REQUEST_TYPE_ACQUIRE_NOTIFY) {
3115
3116                 INFO("GATT Server Send Response BT_HAL_GATT_REQUEST_TYPE_ACQUIRE_NOTIFY  to bluez  %d\n", fd);
3117                 g_dbus_method_invocation_return_value_with_unix_fd_list(
3118                                 req_info->context, g_variant_new("(hq)", 0, mtu), (GUnixFDList*)fdlist);
3119
3120         } else {
3121                 g_dbus_method_invocation_return_value(req_info->context, NULL);
3122         }
3123         conn_info->gatt_req_info_list = g_slist_remove(conn_info->gatt_req_info_list, req_info);
3124
3125         req_info->context = NULL;
3126         if (req_info->attr_path)
3127                 g_free(req_info->attr_path);
3128         if (req_info->svc_path)
3129                 g_free(req_info->svc_path);
3130         g_free(req_info);
3131
3132         return BT_STATUS_SUCCESS;
3133 }
3134
3135
3136 static bt_status_t gatt_server_update_att_value(int server_if, int attribute_handle,
3137                 int value_length, char* att_value)
3138 {
3139         CHECK_BTGATT_INIT();
3140
3141         /* Other variables */
3142         char *char_path = NULL;
3143         gboolean ret = TRUE;
3144         GError *error = NULL;
3145
3146         GVariantBuilder *outer_builder;
3147         GVariantBuilder *inner_builder;
3148         GVariantBuilder *invalidated_builder;
3149         GVariant *update_value = NULL;
3150         int err = BT_STATUS_SUCCESS;
3151         int i = 0;
3152         gchar **line_argv = NULL;
3153         gchar *serv_path = NULL;
3154
3155         char_path = __bt_gatt_find_char_path_from_handle(attribute_handle);
3156         if (char_path == NULL)
3157                 return BT_STATUS_FAIL;
3158
3159         line_argv = g_strsplit_set(char_path, "/", 0);
3160         serv_path = g_strdup_printf("/%s/%s/%s", line_argv[1], line_argv[2], line_argv[3]);
3161
3162         if (!__bt_gatt_get_service_state(serv_path)) {
3163                 DBG("service not registered for this characteristic \n");
3164                 g_free(serv_path);
3165                 g_strfreev(line_argv);
3166                 return BT_STATUS_FAIL;
3167         }
3168
3169         g_free(serv_path);
3170         line_argv = g_strsplit_set(char_path, "/", 0);
3171         serv_path = g_strdup_printf("/%s/%s/%s", line_argv[1], line_argv[2], line_argv[3]);
3172
3173         if (!__bt_gatt_get_service_state(serv_path)) {
3174                 DBG("service not registered for this characteristic \n");
3175                 g_free(serv_path);
3176                 g_strfreev(line_argv);
3177                 return BT_STATUS_FAIL;
3178         }
3179
3180         outer_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
3181         invalidated_builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
3182
3183         inner_builder = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
3184         for (i = 0; i < value_length; i++)
3185                 g_variant_builder_add(inner_builder, "y", att_value[i]);
3186
3187         update_value = g_variant_new("ay", inner_builder);
3188
3189         g_variant_builder_add(outer_builder, "{sv}", "Value",
3190                         update_value);
3191
3192         DBG("Updating characteristic value \n");
3193         ret = g_dbus_connection_emit_signal(g_conn, NULL,
3194                         char_path,
3195                         "org.freedesktop.DBus.Properties",
3196                         "PropertiesChanged",
3197                         g_variant_new("(sa{sv}as)",
3198                                 "org.bluez.GattCharacteristic1",
3199                                 outer_builder, invalidated_builder),
3200                         &error);
3201
3202         if (!ret) {
3203                 if (error != NULL) {
3204                         ERR("D-Bus API failure: errCode[%x], \
3205                                         message[%s]",
3206                                         error->code, error->message);
3207                         g_clear_error(&error);
3208                 }
3209                 err = BT_STATUS_FAIL;
3210         } else {
3211                 struct gatt_char_info *char_info = NULL;
3212
3213                 char_info = __bt_gatt_find_char_info_from_handle(attribute_handle);
3214                 if (char_info == NULL) {
3215                         g_free(serv_path);
3216                         g_strfreev(line_argv);
3217                         g_variant_builder_unref(inner_builder);
3218                         g_variant_builder_unref(outer_builder);
3219                         g_variant_builder_unref(invalidated_builder);
3220
3221                         return BT_STATUS_FAIL;
3222                 }
3223
3224                 char_info->value_length = value_length;
3225
3226                 char_info->char_value = (char *)realloc(char_info->char_value, value_length);
3227                 if (char_info->char_value) {
3228                         for (i = 0; i < value_length; i++)
3229                                 char_info->char_value[i] = att_value[i];
3230                 }
3231         }
3232
3233         /* Free data */
3234         g_free(serv_path);
3235         g_strfreev(line_argv);
3236         g_variant_builder_unref(inner_builder);
3237         g_variant_builder_unref(outer_builder);
3238         g_variant_builder_unref(invalidated_builder);
3239
3240         return err;
3241 }
3242
3243 static bt_status_t gatt_server_listen(int server_if, bool start)
3244 {
3245         CHECK_BTGATT_INIT();
3246         /* Send Data to LE Module */
3247         return _bt_hal_enable_advertising(server_if, start, FALSE);
3248 }
3249
3250 static bt_status_t gatt_server_set_adv_data(int server_if, bool set_scan_rsp, bool include_name,
3251                 bool include_txpower, int min_interval, int max_interval, int appearance,
3252                 uint16_t manufacturer_len, char* manufacturer_data,
3253                 uint16_t service_data_len, char* service_data,
3254                 uint16_t service_uuid_len, char* service_uuid)
3255 {
3256         CHECK_BTGATT_INIT();
3257         return BT_STATUS_SUCCESS;
3258 }
3259
3260 static bt_status_t gatt_server_multi_adv_enable(int server_if)
3261 {
3262         CHECK_BTGATT_INIT();
3263         /* Send Enable Advertising request to LE Module */
3264         return _bt_hal_enable_advertising(server_if, TRUE, TRUE);
3265 }
3266
3267 static bt_status_t gatt_server_multi_adv_update(int server_if, int min_interval, int max_interval, int adv_type,
3268                 int chnl_map, int tx_power, int timeout_s)
3269 {
3270         CHECK_BTGATT_INIT();
3271         DBG("+");
3272         /* Send Advertising parameters to LE Module */
3273         return _bt_hal_set_advertising_params(server_if, min_interval, max_interval, adv_type,
3274                         chnl_map, tx_power, timeout_s);
3275 }
3276
3277 static bt_status_t gatt_server_multi_adv_set_inst_data(btgatt_adv_param_setup_t adv_param_setup)
3278 {
3279         CHECK_BTGATT_INIT();
3280         DBG("+");
3281         /* Send Data to LE Module */
3282         return _bt_hal_set_advertising_data(adv_param_setup);
3283 }
3284
3285 static bt_status_t gatt_server_multi_adv_disable(int server_if)
3286 {
3287         CHECK_BTGATT_INIT();
3288         /* Send Data to LE Module */
3289         return _bt_hal_enable_advertising(server_if, FALSE, TRUE);
3290 }
3291
3292 static bt_status_t gatt_server_get_mtu_size(int conn_id, int *mtu_size)
3293 {
3294         CHECK_BTGATT_INIT();
3295         char *object_path = NULL;
3296
3297         GDBusProxy *device_proxy;
3298         GError *error = NULL;
3299         GVariant *value;
3300         GVariant *tmp_value;
3301         GDBusConnection *conn;
3302         GVariant *result = NULL;
3303         int ret = BT_STATUS_SUCCESS;
3304         struct gatt_client_info_t *conn_info = NULL;
3305         unsigned int mtu;
3306
3307         if (mtu_size == NULL)
3308                 return BT_STATUS_PARM_INVALID;
3309
3310         /* GDBUS Connection Info validate */
3311         conn = _bt_hal_get_system_gconn();
3312         if (conn == NULL) {
3313                 ERR("Could not get System DBUS Connection");
3314                 return  BT_STATUS_FAIL;
3315         }
3316
3317         /* Connection Info validate */
3318         conn_info = __bt_find_remote_gatt_client_info_from_conn(conn_id);
3319         if (conn_info == NULL) {
3320                 ERR("No Connection Inforamtion!!!");
3321                 return BT_STATUS_FAIL;
3322         }
3323
3324
3325         object_path = _bt_hal_get_device_object_path(conn_info->addr);
3326         if (object_path == NULL)
3327                 return BT_STATUS_FAIL;
3328
3329         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3330                         NULL, BT_HAL_BLUEZ_NAME, object_path,
3331                         BT_HAL_PROPERTIES_INTERFACE,  NULL, NULL);
3332
3333         g_free(object_path);
3334         if (device_proxy == NULL)
3335                 return BT_STATUS_FAIL;
3336
3337         result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
3338                         g_variant_new("(s)", BT_HAL_DEVICE_INTERFACE),
3339                         G_DBUS_CALL_FLAGS_NONE,
3340                         -1,
3341                         NULL,
3342                         &error);
3343         if (result == NULL) {
3344                 if (error != NULL) {
3345                         ERR("Error occured in Proxy call [%s]\n", error->message);
3346                         g_error_free(error);
3347                 }
3348                 g_object_unref(device_proxy);
3349                 return BT_STATUS_FAIL;
3350         }
3351
3352         g_variant_get(result , "(@a{sv})", &value);
3353         g_variant_unref(result);
3354
3355         tmp_value = g_variant_lookup_value(value, "AttMtu", G_VARIANT_TYPE_UINT16);
3356         if (tmp_value == NULL) {
3357                 g_object_unref(device_proxy);
3358                 g_variant_unref(value);
3359                 return BT_STATUS_FAIL;
3360         }
3361
3362         mtu = g_variant_get_uint16(tmp_value);
3363
3364         DBG("ATT MTU : [%d]", mtu);
3365
3366         g_variant_unref(tmp_value);
3367         g_variant_unref(value);
3368         g_object_unref(device_proxy);
3369
3370         *mtu_size = (int) mtu;
3371
3372         return ret;
3373 }
3374
3375 const btgatt_server_interface_t btgatt_server_interface = {
3376         .register_server = gatt_server_register_app,
3377         .unregister_server = gatt_server_unregister_app,
3378         .connect = gatt_server_open,
3379         .disconnect = gatt_server_close,
3380         .add_service = gatt_server_add_service,
3381         .add_included_service = gatt_server_add_included_service,
3382         .add_characteristic = gatt_server_add_characteristic,
3383         .add_descriptor = gatt_server_add_descriptor,
3384         .start_service = gatt_server_start_service,
3385         .stop_service = gatt_server_stop_service,
3386         .delete_service = gatt_server_delete_service,
3387         .send_indication = gatt_server_send_indication,
3388         .send_response = gatt_server_send_response,
3389         .update_att_value = gatt_server_update_att_value,
3390         .listen = gatt_server_listen,
3391         .set_adv_data = gatt_server_set_adv_data,
3392         .multi_adv_enable = gatt_server_multi_adv_enable,
3393         .multi_adv_update = gatt_server_multi_adv_update,
3394         .multi_adv_set_inst_data = gatt_server_multi_adv_set_inst_data,
3395         .multi_adv_disable = gatt_server_multi_adv_disable,
3396         .get_att_mtu = gatt_server_get_mtu_size,
3397         .send_response_acquire = gatt_server_send_acquire_response
3398 };