Gatt server ReadValue parsing fix
[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 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 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 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 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 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                                         if (strncmp(char_info->char_flags[i], "write-without-response", 16) == 0)
741                                                 writeAcquired = TRUE;
742
743                                         if (strncmp(char_info->char_flags[i], "notify", 4) == 0)
744                                                 notifyAcquired = TRUE;
745                                 }
746
747                                 flags_val = g_variant_new("as", builder2);
748                                 g_variant_builder_add(inner_builder, "{sv}", "Flags",
749                                                 flags_val);
750
751                                 /* Notifying */
752                                 g_variant_builder_add(inner_builder, "{sv}", "Notifying",
753                                                 g_variant_new("b", notify));
754
755                                 /* Unicast */
756                                 g_variant_builder_add(inner_builder, "{sv}", "WriteAcquired",
757                                                                         g_variant_new("b", writeAcquired));
758
759                                 /* NotifyAcquired */
760                                                         g_variant_builder_add(inner_builder, "{sv}", "NotifyAcquired",
761                                                                         g_variant_new("b", notifyAcquired));
762
763                                 /* Unicast */
764                                 unicast = g_strdup("00:00:00:00:00:00");
765                                 g_variant_builder_add(inner_builder, "{sv}", "Unicast",
766                                                 g_variant_new("s", unicast));
767
768                                 /*Descriptors*/
769                                 builder3 = g_variant_builder_new(G_VARIANT_TYPE("ao"));
770                                 DBG("Adding Descriptors list");
771
772                                 for (l4 = char_info->desc_data; l4 != NULL; l4 = l4->next) {
773                                         struct gatt_desc_info *desc_info = l4->data;
774                                         INFO("GATT Descriptor handle [%d] found inside GATT Char handle [%d] found in GATT service handle [%d]",
775                                                         desc_info->desc_handle, char_info->char_handle, serv_info->service_handle);
776                                         g_variant_builder_add(builder3, "o",
777                                                         desc_info->desc_path);
778                                         DBG("%s", desc_info->desc_path);
779                                 }
780
781                                 char_desc = g_variant_new("ao", builder3);
782                                 g_variant_builder_add(inner_builder, "{sv}", "Descriptors",
783                                                 char_desc);
784
785                                 g_variant_builder_add(char_builder, "{sa{sv}}",
786                                                 GATT_CHAR_INTERFACE , inner_builder);
787                                 g_variant_builder_add(builder, "{oa{sa{sv}}}",
788                                                 char_info->char_path, char_builder);
789
790                                 /*Prepare inner builder for GattDescriptor1 interface*/
791
792                                 GSList *l3 = char_info->desc_data;
793
794                                 if (l3 == NULL)
795                                         DBG("descriptor data is NULL");
796
797                                 for (l3 = char_info->desc_data; l3 != NULL; l3 = l3->next) {
798
799                                         DBG("Creating builder for descriptor \n");
800
801                                         GVariantBuilder *desc_builder = NULL;
802                                         GVariantBuilder *inner_builder = NULL;
803                                         GVariantBuilder *builder1 = NULL;
804                                         GVariantBuilder *builder2 = NULL;
805                                         GVariant *desc_val = NULL;
806
807                                         desc_builder = g_variant_builder_new(
808                                                         G_VARIANT_TYPE(
809                                                                 "a{sa{sv}}"));
810                                         inner_builder = g_variant_builder_new(
811                                                         G_VARIANT_TYPE(
812                                                                 "a{sv}"));
813
814                                         struct gatt_desc_info *desc_info = l3->data;
815                                         if (desc_info == NULL) {
816                                                 ERR("desc_info is NULL");
817                                                 continue;
818                                         }
819
820                                         /*Uuid*/
821                                         g_variant_builder_add(inner_builder,
822                                                         "{sv}", "UUID",
823                                                         g_variant_new_string(
824                                                                 desc_info->desc_uuid));
825
826                                         /*Characteristic*/
827                                         g_variant_builder_add(inner_builder, "{sv}",
828                                                         "Characteristic",
829                                                         g_variant_new("o",
830                                                                 char_info->char_path));
831
832                                         /*Value*/
833                                         builder1 = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
834
835                                         if (desc_info->desc_value != NULL) {
836                                                 for (i = 0; i < desc_info->value_length; i++) {
837                                                         g_variant_builder_add(builder1, "y",
838                                                                         desc_info->desc_value[i]);
839                                                 }
840                                                 desc_val = g_variant_new("ay", builder1);
841                                                 g_variant_builder_add(inner_builder, "{sv}",
842                                                                 "Value", desc_val);
843                                         }
844
845                                         /*Flags*/
846                                         builder2 = g_variant_builder_new(G_VARIANT_TYPE("as"));
847
848                                         for (i = 0; i < desc_info->flags_length; i++) {
849                                                 g_variant_builder_add(builder2, "s",
850                                                                 desc_info->desc_flags[i]);
851                                         }
852
853                                         flags_val = g_variant_new("as", builder2);
854                                         g_variant_builder_add(inner_builder, "{sv}", "Flags",
855                                                         flags_val);
856
857                                         g_variant_builder_add(desc_builder, "{sa{sv}}",
858                                                         GATT_DESC_INTERFACE,
859                                                         inner_builder);
860
861                                         g_variant_builder_add(builder, "{oa{sa{sv}}}",
862                                                         desc_info->desc_path,
863                                                         desc_builder);
864
865                                         /*unref descriptor builder pointers*/
866                                         g_variant_builder_unref(builder1);
867                                         g_variant_builder_unref(builder2);
868                                         g_variant_builder_unref(inner_builder);
869                                         g_variant_builder_unref(desc_builder);
870                                 }
871
872                                 if (unicast)
873                                         g_free(unicast);
874                                 /*unref char builder pointers*/
875                                 g_variant_builder_unref(builder1);
876                                 g_variant_builder_unref(builder2);
877                                 g_variant_builder_unref(builder3);
878                                 g_variant_builder_unref(inner_builder);
879                                 g_variant_builder_unref(char_builder);
880                         }
881
882                         /*unref service builder pointers*/
883                         g_variant_builder_unref(inner_builder);
884                         g_variant_builder_unref(svc_builder);
885
886                 }
887                 /* Return builder as method reply */
888                 DBG("Sending gatt service builder values to Bluez");
889                 g_dbus_method_invocation_return_value(invocation,
890                                 g_variant_new(
891                                         "(a{oa{sa{sv}}})",
892                                         builder));
893         }
894
895         /* Free User Data */
896         g_free(user_data);
897 }
898
899 static void __bt_gatt_desc_method_call(GDBusConnection *connection,
900                 const gchar *sender,
901                 const gchar *object_path,
902                 const gchar *interface_name,
903                 const gchar *method_name,
904                 GVariant *parameters,
905                 GDBusMethodInvocation *invocation,
906                 gpointer user_data)
907 {
908
909         if (g_strcmp0(method_name, "ReadValue") == 0) {
910                 struct hal_ev_gatt_server_read_req ev;
911
912                 gchar *addr = NULL;
913                 guint req_id = 0;
914                 guint16 offset = 0;
915                 struct gatt_client_info_t *conn_info = NULL;
916                 struct gatt_req_info *req_info = NULL;
917                 struct gatt_service_info *svc_info = NULL;
918                 int desc_hdl = -1;
919
920                 DBG("ReadValue");
921                 DBG("Application path = %s", object_path);
922                 DBG("Sender = %s", sender);
923
924                 svc_info = __bt_gatt_find_gatt_service_from_desc(object_path, &desc_hdl);
925                 if (svc_info == NULL) {
926                         ERR("Coudn't find service for %s", object_path);
927                         g_dbus_method_invocation_return_value(invocation, NULL);
928                         return;
929                 }
930
931                 g_variant_get(parameters, "(&suq)", &addr, &req_id, &offset);
932                 DBG("Request id = %u, Offset = %u", req_id, offset);
933
934                 /* Check if device is already in connected list */
935                 conn_info = __bt_find_remote_gatt_client_info(addr);
936
937                 if (conn_info == NULL) {
938                         ERR("Coudn't find Connection info for %s", addr);
939                         g_dbus_method_invocation_return_value(invocation, NULL);
940                         return;
941                 }
942
943                 if (!event_cb) {
944                         ERR("GATT callback NOT registered");
945                         g_dbus_method_invocation_return_value(invocation, NULL);
946                         return;
947                 }
948
949                 /* Store requests information */
950                 req_info = g_new0(struct gatt_req_info, 1);
951                 req_info->attr_path = g_strdup(object_path);
952                 req_info->svc_path = g_strdup(svc_info->serv_path);
953                 req_info->request_id = req_id;
954                 req_info->offset = offset;
955                 req_info->context = invocation;
956
957                 /* Append request info in list of requests for the particular connection */
958                 conn_info->gatt_req_info_list = g_slist_append(conn_info->gatt_req_info_list, req_info);
959
960                 /* Send HAL event */
961                 memset(&ev, 0, sizeof(ev));
962                 ev.conn_id = conn_info->connection_id;
963                 ev.trans_id = req_id;
964                 ev.att_handle = desc_hdl;
965                 ev.offset = offset;
966                 ev.is_long = false; /* TODO*/
967
968                 /* Convert address to hex */
969                 _bt_hal_convert_addr_string_to_type(ev.bdaddr, addr);
970
971                 event_cb(HAL_EV_GATT_READ_REQUESTED, (void *)&ev, sizeof(ev));
972                 return;
973         } else if (g_strcmp0(method_name, "WriteValue") == 0) {
974
975                 GVariant *var = NULL;
976                 gchar *addr = NULL;
977                 guint req_id = 0;
978                 guint16 offset = 0;
979                 gboolean response_needed = FALSE;
980                 struct hal_ev_gatt_server_write_req ev;
981                 int desc_hdl = -1;
982                 int len;
983
984                 struct gatt_service_info *svc_info = NULL;
985                 struct gatt_client_info_t *conn_info = NULL;
986                 struct gatt_req_info *req_info = NULL;
987
988                 memset(&ev, 0, sizeof(ev));
989
990                 DBG("WriteValue");
991                 DBG("Application path = %s", object_path);
992                 DBG("Sender = %s", sender);
993
994                 g_variant_get(parameters, "(&suqb@ay)",
995                                 &addr, &req_id, &offset, &response_needed, &var);
996                 DBG("Request id = %u, Offset = %u", req_id, offset);
997
998                 /* Check if device is already in connected list */
999                 conn_info = __bt_find_remote_gatt_client_info(addr);
1000
1001                 svc_info = __bt_gatt_find_gatt_service_from_desc(object_path, &desc_hdl);
1002
1003                 if (conn_info == NULL || svc_info == NULL || event_cb == NULL) {
1004                         g_variant_unref(var);
1005                         if (response_needed)
1006                                 g_dbus_method_invocation_return_value(invocation, NULL);
1007                         else
1008                                 g_object_unref(invocation);
1009                         return;
1010                 }
1011
1012                 len = g_variant_get_size(var);
1013                 if (len > 0 && len < 600) {
1014                         char *data;
1015                         data = (char *)g_variant_get_data(var);
1016                         memcpy(ev.value, data, len);
1017                         ev.length = len;
1018                 }
1019                 if (response_needed) {
1020                         /* Store request information */
1021                         req_info = g_new0(struct gatt_req_info, 1);
1022                         req_info->attr_path = g_strdup(object_path);
1023                         req_info->svc_path = g_strdup(svc_info->serv_path);
1024                         req_info->request_id = req_id;
1025                         req_info->offset = offset;
1026                         req_info->context = invocation;
1027
1028                         /* Append request info in list of requests for the particular connection */
1029                         conn_info->gatt_req_info_list = g_slist_append(conn_info->gatt_req_info_list, req_info);
1030                 } else {
1031                         g_object_unref(invocation);
1032                 }
1033
1034                 /* Send HAL event */
1035                 ev.conn_id = conn_info->connection_id;
1036                 ev.trans_id = req_id;
1037                 ev.att_handle = desc_hdl;
1038                 ev.offset = offset;
1039                 ev.need_rsp = response_needed;
1040                 ev.is_prep = 0;
1041
1042                 /* Convert address to hex */
1043                 _bt_hal_convert_addr_string_to_type(ev.bdaddr, addr);
1044
1045                 event_cb(HAL_EV_GATT_WRITE_REQUESTED, (void *)&ev, sizeof(ev));
1046
1047                 g_variant_unref(var);
1048                 return;
1049         }
1050 }
1051
1052 static void __bt_gatt_char_method_call(GDBusConnection *connection,
1053                 const gchar *sender,
1054                 const gchar *object_path,
1055                 const gchar *interface_name,
1056                 const gchar *method_name,
1057                 GVariant *parameters,
1058                 GDBusMethodInvocation *invocation,
1059                 gpointer user_data)
1060 {
1061         if (g_strcmp0(method_name, "ReadValue") == 0) {
1062                 gchar *addr = NULL;
1063                 guint req_id = 0;
1064                 guint16 offset = 0;
1065                 struct hal_ev_gatt_server_read_req ev;
1066                 int char_hdl = -1;
1067
1068                 struct gatt_req_info *req_info = NULL;
1069                 struct gatt_client_info_t *conn_info = NULL;
1070                 struct gatt_service_info *svc_info = NULL;
1071
1072                 g_variant_get(parameters, "(&suq)",
1073                                         &addr, &req_id, &offset);
1074
1075                 DBG("Request id = %u, Offset = %u", req_id, offset);
1076                 DBG("Application path = %s", object_path);
1077                 DBG("Sender = %s", sender);
1078                 DBG("Address = %s", addr);
1079
1080                 /* Check if device is already in connected list */
1081                 conn_info = __bt_find_remote_gatt_client_info(addr);
1082
1083                 svc_info = __bt_gatt_find_gatt_service_from_char(object_path, &char_hdl);
1084
1085                 if (svc_info == NULL || conn_info == NULL) {
1086                         g_dbus_method_invocation_return_value(invocation, NULL);
1087                         return;
1088                 }
1089
1090                 if (!event_cb) {
1091                         ERR("GATT callback NOT registered");
1092                         g_dbus_method_invocation_return_value(invocation, NULL);
1093                         return;
1094                 }
1095
1096                 g_variant_get(parameters, "(&suq)", &addr, &req_id, &offset);
1097                 DBG("Request id = %u, Offset = %u", req_id, offset);
1098
1099                 /* Store requets information */
1100                 req_info = g_new0(struct gatt_req_info, 1);
1101                 req_info->attr_path = g_strdup(object_path);
1102                 req_info->svc_path = g_strdup(svc_info->serv_path);
1103                 req_info->request_id = req_id;
1104                 req_info->request_type = BT_HAL_GATT_REQUEST_TYPE_READ;
1105                 req_info->offset = offset;
1106                 req_info->context = invocation;
1107
1108                 /* Append request info in list of requests for the particular connection */
1109                 conn_info->gatt_req_info_list = g_slist_append(conn_info->gatt_req_info_list, req_info);
1110
1111                 /* Send HAL event */
1112                 memset(&ev, 0, sizeof(ev));
1113                 ev.conn_id = conn_info->connection_id;
1114                 ev.trans_id = req_id;
1115                 ev.att_handle = char_hdl;
1116                 ev.offset = offset;
1117                 ev.is_long = false; /* TODO*/
1118
1119                 /* Convert address to hex */
1120                 _bt_hal_convert_addr_string_to_type(ev.bdaddr, addr);
1121                 event_cb(HAL_EV_GATT_READ_REQUESTED, (void *)&ev, sizeof(ev));
1122                 return;
1123         } else if (g_strcmp0(method_name, "WriteValue") == 0) {
1124                 GVariant *var = NULL;
1125                 gchar *addr = NULL;
1126                 guint req_id = 0;
1127                 guint16 offset = 0;
1128                 gboolean response_needed = FALSE;
1129                 struct hal_ev_gatt_server_write_req ev;
1130                 int char_hdl = -1;
1131                 int len;
1132
1133                 struct gatt_service_info *svc_info = NULL;
1134                 struct gatt_req_info *req_info = NULL;
1135                 struct gatt_client_info_t *conn_info = NULL;
1136
1137                 memset(&ev, 0, sizeof(ev));
1138
1139                 DBG("WriteValue");
1140                 DBG("Application path = %s", object_path);
1141                 DBG("Sender = %s", sender);
1142
1143                 g_variant_get(parameters, "(&suqb@ay)",
1144                                 &addr, &req_id, &offset, &response_needed, &var);
1145                 DBG("Request id = %u, Offset = %u", req_id, offset);
1146
1147                 svc_info = __bt_gatt_find_gatt_service_from_char(object_path, &char_hdl);
1148
1149                 /* Check if device is already in connected list */
1150                 conn_info = __bt_find_remote_gatt_client_info(addr);
1151
1152                 if (svc_info == NULL || conn_info == NULL || event_cb == NULL) {
1153                         g_variant_unref(var);
1154                         if (response_needed)
1155                                 g_dbus_method_invocation_return_value(invocation, NULL);
1156                         else
1157                                 g_object_unref(invocation);
1158                         return;
1159                 }
1160
1161                 len = g_variant_get_size(var);
1162                 if (len > 0 && len < 600) {
1163                         char *data;
1164                         data = (char *)g_variant_get_data(var);
1165                         memcpy(ev.value, data, len);
1166                         ev.length = len;
1167                 }
1168
1169                 if (response_needed) {
1170                         /* Store requets information */
1171                         req_info = g_new0(struct gatt_req_info, 1);
1172                         req_info->attr_path = g_strdup(object_path);
1173                         req_info->svc_path = g_strdup(svc_info->serv_path);
1174                         req_info->request_id = req_id;
1175                         req_info->request_type = BT_HAL_GATT_REQUEST_TYPE_WRITE;
1176                         req_info->offset = offset;
1177                         req_info->context = invocation;
1178
1179                         /* Append request info in list of requests for the particular connection */
1180                         conn_info->gatt_req_info_list = g_slist_append(conn_info->gatt_req_info_list, req_info);
1181
1182                 } else {
1183                         g_object_unref(invocation);
1184                 }
1185
1186                 /* Send HAL event */
1187                 ev.conn_id = conn_info->connection_id;
1188                 ev.trans_id = req_id;
1189                 ev.att_handle = char_hdl;
1190                 ev.offset = offset;
1191                 ev.need_rsp = response_needed;
1192                 ev.is_prep = 0;
1193
1194                 /* Convert address to hex */
1195                 _bt_hal_convert_addr_string_to_type(ev.bdaddr, addr);
1196
1197                 event_cb(HAL_EV_GATT_WRITE_REQUESTED, (void *)&ev, sizeof(ev));
1198
1199                 g_variant_unref(var);
1200                 return;
1201
1202         } else if (g_strcmp0(method_name, "StartNotify") == 0) {
1203                 DBG("StartNotify");
1204 #ifdef TIZEN_BT_HAL
1205                 struct gatt_service_info *svc_info = NULL;
1206                 struct hal_ev_gatt_server_notifcation_change ev;
1207                 int char_hdl = -1;
1208
1209                 svc_info = __bt_gatt_find_gatt_service_from_char(object_path, &char_hdl);
1210                 if (svc_info == NULL || event_cb == NULL)
1211                         return;
1212
1213                 /* Send HAL event */
1214                 memset(&ev, 0, sizeof(ev));
1215                 ev.conn_id = -1;  /*TODO Bluez does not provide remote GATT client address, so no conn_id */
1216                 ev.trans_id = -1; /*TODO Bluez does not provide request id or transacion ID */
1217                 ev.att_handle = char_hdl;
1218                 ev.notify = true;
1219
1220                 /* Convert address to hex */
1221                 _bt_hal_convert_addr_string_to_type(ev.bdaddr, "00:00:00:00:00"); /* TODO Bluez Does not provide address of GATT client */
1222
1223                 event_cb(HAL_EV_GATT_NOTIFICATION_CHANGE, (void *)&ev, sizeof(ev));
1224 #endif
1225
1226         } else if (g_strcmp0(method_name, "StopNotify") == 0) {
1227                 DBG("StopNotify");
1228 #ifdef TIZEN_BT_HAL
1229                 struct gatt_service_info *svc_info = NULL;
1230                 struct hal_ev_gatt_server_notifcation_change ev;
1231                 int char_hdl = -1;
1232
1233                 svc_info = __bt_gatt_find_gatt_service_from_char(object_path, &char_hdl);
1234                 if (svc_info == NULL || event_cb == NULL)
1235                         return;
1236
1237                 /* Send HAL event */
1238                 memset(&ev, 0, sizeof(ev));
1239                 ev.conn_id = -1;  /*TODO Bluez does not provide remote GATT client address, so no conn_id */
1240                 ev.trans_id = -1; /*TODO Bluez does not provide request id or transacion ID */
1241                 ev.att_handle = char_hdl;
1242                 ev.notify = false;
1243
1244                 /* Convert address to hex */
1245                 _bt_hal_convert_addr_string_to_type(ev.bdaddr, "00:00:00:00:00"); /* TODO Bluez DOes not provide address of GATT client */
1246
1247                 event_cb(HAL_EV_GATT_NOTIFICATION_CHANGE, (void *)&ev, sizeof(ev));
1248 #endif
1249
1250         } else if (g_strcmp0(method_name, "IndicateConfirm") == 0) {
1251                 gchar *addr = NULL;
1252                 gboolean complete = FALSE;
1253                 int char_hdl = -1;
1254
1255                 struct gatt_service_info *svc_info = NULL;
1256                 struct gatt_client_info_t *conn_info = NULL;
1257
1258                 struct hal_ev_gatt_server_indicate_cfm ev;
1259
1260                 DBG("IndicateConfirm");
1261                 DBG("Application path = %s", object_path);
1262                 DBG("Sender = %s", sender);
1263
1264                 g_variant_get(parameters, "(&sb)", &addr, &complete);
1265                 DBG("Remote Device address number = %s", addr);
1266                 DBG("Is Indicate confirmation for last device [%d]", complete);
1267
1268                 /* Check if device is already in connected list */
1269                 conn_info = __bt_find_remote_gatt_client_info(addr);
1270
1271                 svc_info = __bt_gatt_find_gatt_service_from_char(object_path, &char_hdl);
1272
1273                 if (svc_info == NULL || conn_info == NULL
1274                         || event_cb == NULL) {
1275                         return;
1276                 }
1277
1278                 /* Send HAL event */
1279                 memset(&ev, 0, sizeof(ev));
1280                 ev.conn_id = conn_info->connection_id;
1281                 ev.trans_id = -1; /*TODO Bluez does not provide Transaction ID or request ID */
1282                 ev.att_handle = char_hdl;
1283
1284                 /* Convert address to hex */
1285                 _bt_hal_convert_addr_string_to_type(ev.bdaddr, addr);
1286
1287                 event_cb(HAL_EV_GATT_INDICATE_CFM, (void *)&ev, sizeof(ev));
1288         } else if (g_strcmp0(method_name, "AcquireWrite") == 0) {
1289
1290                                 uint16_t mtu = 512;
1291                                 int char_hdl = -1;
1292                                 struct hal_ev_gatt_server_acquire_write_res  ev;
1293                                 struct gatt_service_info *svc_info = NULL;
1294                                 struct gatt_req_info *req_info = NULL;
1295                                 struct gatt_client_info_t *conn_info = NULL;
1296                                 char * dev_path = NULL;
1297                                 char * link = 0;
1298                                 char  addr[BT_HAL_ADDRESS_STRING_SIZE];
1299
1300                                 DBG("AcquireWrite");
1301                                 DBG("Application path = %s", object_path);
1302                                 DBG("Sender = %s", sender);
1303
1304                                 GVariantIter* iter;
1305                                 g_variant_get(parameters, "(a{sv})", &iter);
1306                                 char* key = NULL;
1307                                 GVariant* value = NULL;
1308                                 while (g_variant_iter_loop(iter, "{sv}", &key, &value)) {
1309                                                 if (g_strcmp0(key, "MTU") == 0)
1310                                                         g_variant_get(value, "i", &mtu);
1311                                                 else if (g_strcmp0(key, "link") == 0)
1312                                                         g_variant_get(value, "s", &link);
1313                                                 else if (g_strcmp0(key, "device") == 0)
1314                                                         g_variant_get(value, "o", &dev_path);
1315                                 }
1316
1317                                 DBG("AcquireWrite   values retived");
1318                                 DBG(" path  %s LINK = %s, MTU = %u", addr, link, mtu);
1319
1320                                 svc_info = __bt_gatt_find_gatt_service_from_char(object_path, &char_hdl);
1321
1322                                 _bt_hal_convert_device_path_to_address(dev_path, addr);
1323
1324                                 DBG("remote adress %s", addr);
1325
1326                                 /* Check if device is already in connected list */
1327                                 conn_info = __bt_find_remote_gatt_client_info(addr);
1328
1329                                 if (conn_info == NULL) {
1330                                         ERR("Cleint info not found\n");
1331                                         goto done;
1332                                 }
1333
1334                                 if (svc_info == NULL) {
1335                                         ERR("svc_info  info not found\n");
1336                                         goto done;
1337                                 }
1338
1339                                 /* Store requets information */
1340                                 req_info = g_new0(struct gatt_req_info, 1);
1341                                 req_info->attr_path = g_strdup(object_path);
1342                                 req_info->svc_path = g_strdup(svc_info->serv_path);
1343                                 req_info->request_id = conn_info->connection_id;
1344                                 req_info->request_type = BT_HAL_GATT_REQUEST_TYPE_ACQUIRE_WRITE;
1345                                 req_info->offset = mtu;
1346                                 req_info->context = invocation;
1347
1348                                 /* Append request info in list of requests for the particular connection */
1349                                 conn_info->gatt_req_info_list = g_slist_append(conn_info->gatt_req_info_list, req_info);
1350
1351                                 /* Send HAL event */
1352                                 memset(&ev, 0, sizeof(ev));
1353                                 ev.conn_id = conn_info->connection_id;
1354                                 ev.char_handl = char_hdl;
1355                                 ev.mtu = mtu;
1356                                 ev.trans_id = conn_info->connection_id;
1357                                 _bt_hal_convert_addr_string_to_type(ev.bdaddr, addr);
1358                                 event_cb(HAL_EV_GATT_SERVER_ACQUIRE_WRITE_RES, (void *)&ev, sizeof(ev));
1359
1360                                 DBG("HAL_EV_GATT_ACQUIRE_WRITE_RES   called");
1361
1362
1363                         return;
1364                 } else if (g_strcmp0(method_name, "AcquireNotify") == 0) {
1365
1366                                 uint16_t mtu = 512;
1367                                 int char_hdl = -1;
1368                                 struct hal_ev_gatt_server_acquire_notify  ev;
1369                                 struct gatt_service_info *svc_info = NULL;
1370                                 struct gatt_client_info_t *conn_info = NULL;
1371                                 struct gatt_req_info *req_info = NULL;
1372                                 char * link = 0;
1373                                 int len = 0;
1374
1375                                 DBG("AcquireNotify");
1376                                 DBG("Application path = %s", object_path);
1377                                 DBG("Sender = %s", sender);
1378
1379                                 GVariantIter* iter;
1380                                 g_variant_get(parameters, "(a{sv})", &iter);
1381                                 char* key = NULL;
1382                                 GVariant* value = NULL;
1383                                 while (g_variant_iter_loop(iter, "{sv}", &key, &value)) {
1384                                                 if (g_strcmp0(key, "MTU") == 0)
1385                                                         g_variant_get(value, "q", &mtu);
1386                                 }
1387
1388                                 DBG("AcquireNotify  values retived");
1389                                 DBG(" MTU = %u",  mtu);
1390
1391                                 svc_info = __bt_gatt_find_gatt_service_from_char(object_path, &char_hdl);
1392
1393                         if (svc_info == NULL) {
1394                                         ERR("svc_info  info not found\n");
1395                                         goto done;
1396                                 }
1397
1398                                 /* Store requets information */
1399                                 req_info = g_new0(struct gatt_req_info, 1);
1400                                 req_info->attr_path = g_strdup(object_path);
1401                                 req_info->svc_path = g_strdup(svc_info->serv_path);
1402                                 req_info->request_id = 33;
1403                                 req_info->request_type = BT_HAL_GATT_REQUEST_TYPE_ACQUIRE_NOTIFY;
1404                                 req_info->offset = mtu;
1405                                 req_info->context = invocation;
1406
1407                                 conn_info = g_new0(struct gatt_client_info_t, 1);
1408                                 conn_info->addr = g_strdup(object_path);
1409                                 INFO("Added GATT client path[%s]", conn_info->addr);
1410                                 conn_info->connection_id = 33;
1411                                 conn_info->instance_id = 33;
1412                                 /* Append request info in list of requests for the particular connection */
1413                                 conn_info->gatt_req_info_list = g_slist_append(conn_info->gatt_req_info_list, req_info);
1414                                 gatt_client_info_list = g_slist_append(gatt_client_info_list, conn_info);
1415
1416                                 /* Send HAL event */
1417                                 memset(&ev, 0, sizeof(ev));
1418                                 ev.conn_id = 33;
1419                                 ev.char_handl = char_hdl;
1420                                 ev.mtu = mtu;
1421                                 ev.trans_id = 33;
1422
1423                                 event_cb(HAL_EV_GATT_SERVER_ACQUIRE_NOTIFY_RES, (void *)&ev, sizeof(ev));
1424
1425                                 DBG("HAL_EV_GATT_ACQUIRE_NOTIFY_RES   called");
1426
1427                                 return;
1428         }
1429
1430 done:
1431         g_dbus_method_invocation_return_value(invocation, NULL);
1432 }
1433
1434 gboolean __bt_hal_gatt_emit_interface_removed(gchar *object_path, gchar *interface)
1435 {
1436         gboolean ret;
1437         GError *error = NULL;
1438         GVariantBuilder *array_builder;
1439
1440         array_builder = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
1441         g_variant_builder_init(array_builder, G_VARIANT_TYPE("as"));
1442         g_variant_builder_add(array_builder, "s", interface);
1443
1444         ret = g_dbus_connection_emit_signal(g_conn, NULL, "/",
1445                         "org.freedesktop.Dbus.Objectmanager",
1446                         "InterfacesRemoved",
1447                         g_variant_new("(oas)",
1448                                 object_path, array_builder),
1449                         &error);
1450
1451         if (!ret) {
1452                 if (error != NULL) {
1453                         /* dbus gives error cause */
1454                         ERR("d-bus api failure: errcode[%x], message[%s]",
1455                                         error->code, error->message);
1456                         g_clear_error(&error);
1457                 }
1458         }
1459         g_variant_builder_unref(array_builder);
1460
1461         return ret;
1462 }
1463
1464 static void __bt_hal_gatt_free_descriptor_info(struct gatt_desc_info *desc_info)
1465 {
1466         int i;
1467
1468         if (!desc_info)
1469                 return;
1470
1471         g_free(desc_info->desc_path);
1472         g_free(desc_info->desc_uuid);
1473         g_free(desc_info->desc_value);
1474
1475         for (i = 0; i < desc_info->flags_length; i++)
1476                 g_free(desc_info->desc_flags[i]);
1477
1478         g_free(desc_info);
1479 }
1480
1481 static void __bt_hal_gatt_free_characteristic_info(struct gatt_char_info *char_info)
1482 {
1483         int i;
1484
1485         if (!char_info)
1486                 return;
1487
1488         g_free(char_info->char_path);
1489         g_free(char_info->char_uuid);
1490         g_free(char_info->char_value);
1491
1492         for (i = 0; i < char_info->flags_length; i++)
1493                 g_free(char_info->char_flags[i]);
1494
1495         g_free(char_info);
1496 }
1497
1498
1499 static void __bt_hal_gatt_free_service_info(struct gatt_service_info *svc_info)
1500 {
1501         if (!svc_info)
1502                 return;
1503
1504         g_free(svc_info->serv_path);
1505         g_free(svc_info->service_uuid);
1506         g_free(svc_info);
1507 }
1508
1509 static const GDBusInterfaceVTable desc_interface_vtable = {
1510         __bt_gatt_desc_method_call,
1511         NULL,
1512         NULL,
1513 };
1514
1515 static const GDBusInterfaceVTable char_interface_vtable = {
1516         __bt_gatt_char_method_call,
1517         NULL,
1518         NULL,
1519 };
1520
1521 static const GDBusInterfaceVTable serv_interface_vtable = {
1522         NULL,
1523         NULL,
1524         NULL,
1525 };
1526
1527 static const GDBusInterfaceVTable manager_interface_vtable = {
1528         __bt_gatt_manager_method_call,
1529         NULL,
1530         NULL
1531 };
1532
1533
1534 static GDBusNodeInfo *__bt_gatt_create_method_node_info(
1535                 const gchar *introspection_data)
1536 {
1537         GError *err = NULL;
1538         GDBusNodeInfo *node_info = NULL;
1539
1540         if (introspection_data == NULL)
1541                 return NULL;
1542
1543
1544         DBG("Create new node info");
1545         node_info = g_dbus_node_info_new_for_xml(introspection_data, &err);
1546
1547         if (err) {
1548                 ERR("Unable to create node: %s", err->message);
1549                 g_clear_error(&err);
1550                 return NULL;
1551         }
1552
1553         return node_info;
1554 }
1555
1556 /* To send stack event to hal-av handler */
1557 void _bt_hal_register_gatt_server_handler_cb(handle_stack_msg cb)
1558 {
1559         event_cb = cb;
1560 }
1561
1562 void _bt_hal_unregister_gatt_server_handler_cb(void)
1563 {
1564         event_cb = NULL;
1565 }
1566
1567 static gboolean __bt_hal_gatt_desc_added_cb(gpointer user_data)
1568 {
1569         struct hal_ev_gatt_desc_added ev;
1570         hal_gatt_desc_added *data = (hal_gatt_desc_added*) user_data;
1571
1572         /* Prepare to GATT characteristic added event */
1573         memset(&ev, 0, sizeof(ev));
1574         ev.status = BT_STATUS_SUCCESS;
1575         ev.server_instance = data->instance_data;
1576         memcpy(ev.desc_uuid, data->uuid.uu, sizeof(data->uuid.uu));
1577         ev.service_handle = data->srvc_hdl;
1578         ev.desc_handle = data->desc_hdl;
1579
1580         if (!event_cb)
1581                 ERR("GATT Descriptor Added callback registered");
1582         else {
1583                 DBG("GATT Descriptor Added: server if [%d] Service handle [%d] Descriptor Handle [%d]",
1584                                 data->instance_data, data->srvc_hdl, data->desc_hdl);
1585
1586                 event_cb(HAL_EV_GATT_DESC_ADDED, (void *)&ev, sizeof(ev));
1587         }
1588
1589         g_free(data);
1590         return FALSE;
1591 }
1592
1593 static gboolean __bt_hal_gatt_char_added_cb(gpointer user_data)
1594 {
1595         struct hal_ev_gatt_char_added ev;
1596         hal_gatt_char_added *data = (hal_gatt_char_added*) user_data;
1597
1598         /* Prepare to GATT characteristic added event */
1599         memset(&ev, 0, sizeof(ev));
1600         ev.status = BT_STATUS_SUCCESS;
1601         ev.server_instance = data->instance_data;
1602         memcpy(ev.char_uuid, data->uuid.uu, sizeof(data->uuid.uu));
1603         ev.service_handle = data->srvc_hdl;
1604         ev.char_handle = data->char_hdl;
1605
1606         if (!event_cb)
1607                 ERR("GATT Characteristic Added callback registered");
1608         else {
1609                 DBG("GATT Characteristic Added: server if [%d] Service handle [%d] Characteristic handle [%d]",
1610                                 data->instance_data, data->srvc_hdl, data->char_hdl);
1611
1612                 event_cb(HAL_EV_GATT_CHAR_ADDED, (void *)&ev, sizeof(ev));
1613         }
1614
1615         g_free(data);
1616         return FALSE;
1617 }
1618
1619 static gboolean __bt_hal_gatt_service_added_cb(gpointer user_data)
1620 {
1621         struct hal_ev_gatt_service_added ev;
1622         hal_gatt_service_added *data = (hal_gatt_service_added*) user_data;
1623
1624         /* Prepare to GATT Service added event */
1625         memset(&ev, 0, sizeof(ev));
1626         ev.status = BT_STATUS_SUCCESS;
1627         ev.server_instance = data->instance_data;
1628         memcpy(ev.svc_uuid, data->uuid.uu, sizeof(data->uuid.uu));
1629         ev.service_handle = data->srvc_hdl;
1630         ev.is_primary = data->is_primary;
1631
1632         if (!event_cb)
1633                 ERR("GATT Service Added callback registered");
1634         else {
1635                 DBG("GATT Service Added: server if [%d] Service handle [%d]",
1636                                 data->instance_data,  data->srvc_hdl);
1637                 event_cb(HAL_EV_GATT_SERVICE_ADDED, (void *)&ev, sizeof(ev));
1638         }
1639
1640         g_free(data);
1641         return FALSE;
1642 }
1643
1644 static gboolean __bt_hal_gatt_service_started_cb(gpointer user_data)
1645 {
1646         struct hal_ev_gatt_service_started ev;
1647         hal_gatt_service_started *data = (hal_gatt_service_started*) user_data;
1648
1649         /* Prepare to GATT Service added event */
1650         memset(&ev, 0, sizeof(ev));
1651         ev.status = BT_STATUS_SUCCESS;
1652         ev.server_instance = data->instance_data;
1653         ev.service_handle = data->srvc_hdl;
1654
1655         if (!event_cb)
1656                 ERR("GATT Service Started callback registered");
1657         else {
1658                 DBG("GATT Service Started: server if [%d] Service handle [%d]",
1659                                 data->instance_data,  data->srvc_hdl);
1660                 event_cb(HAL_EV_GATT_SERVICE_STARTED, (void *)&ev, sizeof(ev));
1661         }
1662
1663         g_free(data);
1664         return FALSE;
1665 }
1666
1667 static gboolean __bt_hal_gatt_service_deleted_cb(gpointer user_data)
1668 {
1669         struct hal_ev_gatt_service_deleted ev;
1670         hal_gatt_service_deleted *data = (hal_gatt_service_deleted*) user_data;
1671
1672         /* Prepare to GATT Service added event */
1673         memset(&ev, 0, sizeof(ev));
1674         ev.status = BT_STATUS_SUCCESS;
1675         ev.server_instance = data->instance_data;
1676         ev.service_handle = data->srvc_hdl;
1677
1678         if (!event_cb)
1679                 ERR("GATT Service Deleted callback registered");
1680         else {
1681                 DBG("GATT Service Deleted: server if [%d] Service handle [%d]",
1682                                 data->instance_data,  data->srvc_hdl);
1683                 event_cb(HAL_EV_GATT_SERVICE_DELETED, (void *)&ev, sizeof(ev));
1684         }
1685
1686         g_free(data);
1687         return FALSE;
1688 }
1689
1690 static gboolean __bt_hal_register_slot_id_cb(gpointer user_data)
1691 {
1692         struct hal_ev_server_instance_registered ev;
1693         hal_register_server_data *data = (hal_register_server_data*) user_data;
1694
1695         /* Prepare to send AV connecting event */
1696         memset(&ev, 0, sizeof(ev));
1697         ev.status = BT_STATUS_SUCCESS;
1698         ev.server_instance = data->instance_data;
1699         memcpy(ev.app_uuid, data->uuid.uu, sizeof(ev.app_uuid));
1700
1701         if (!event_cb)
1702                 ERR("GATT Register Server Instance Callback not registered");
1703         else {
1704                 DBG("Server Instance is registered!! server if [%d]", data->instance_data);
1705                 event_cb(HAL_EV_SERVER_INSTANCE_INITIALIZED, (void *)&ev, sizeof(ev));
1706         }
1707
1708         g_free(data);
1709         return FALSE;
1710 }
1711
1712 static bt_status_t gatt_server_register_app(bt_uuid_t *uuid)
1713 {
1714         CHECK_BTGATT_INIT();
1715         int status = BT_STATUS_FAIL;
1716         int server_if;
1717         DBG("Register server instance request");
1718         hal_register_server_data *user_data = g_malloc0(sizeof(hal_register_server_data));
1719
1720         /* Check if slot available */
1721         server_if = _bt_hal_get_available_adv_slot_id(uuid);
1722
1723         if (server_if == -1) {
1724                 ERR("Allocation of server instance failed");
1725                 g_free(user_data);
1726                 return status;
1727         } else {
1728                 user_data->instance_data = server_if;
1729                 DBG("Allocated new Advertising slot with Stack [%d]", user_data->instance_data);
1730         }
1731
1732         /*
1733          * As we need to provide async callback to user from HAL, simply schedule a
1734          * callback method which will carry actual result
1735          */
1736         memcpy(user_data->uuid.uu, uuid->uu, sizeof(bt_uuid_t));
1737         g_idle_add(__bt_hal_register_slot_id_cb, (gpointer)user_data);
1738
1739         /* If available, then return success, else return error */
1740         return BT_STATUS_SUCCESS;
1741 }
1742
1743 void _bt_hal_remove_gatt_server_from_list(int server_if)
1744 {
1745         GSList *l;
1746         struct gatt_server_app *info = NULL;
1747
1748         for (l = gatt_server_apps; l != NULL;) {
1749                 info = (struct gatt_server_app*)l->data;
1750                 l = g_slist_next(l);
1751                 if (info == NULL)
1752                         continue;
1753                 if (info->slot == server_if) {
1754                         INFO("Found Matching GATT Server in List path[%s] slot [%d]",
1755                                 info->app_path, info->slot);
1756
1757                         /* Only if all services are deleted from the GATT Server, then only Unregister it.
1758                         Reason: it is possible, GATT Server app oly wants to disable multi advertising
1759                         In above case, only advertising block will be deallocated, Gatt Server will remain
1760                         unaffected */
1761                         if (info->services == NULL) {
1762                                 gatt_server_apps = g_slist_remove(gatt_server_apps, (gpointer)info);
1763                                 INFO("Total gatt server apps still existing after removing above is [%d]",
1764                                                 g_slist_length(gatt_server_apps));
1765
1766                                 /* DBUS Unregister only for current app */
1767                                 __bt_hal_gatt_deinit(info->app_path);
1768
1769                                 g_free(info->app_path);
1770                                 g_free(info);
1771                                 return;
1772                         } else {
1773                                 INFO("GATT Server still has services count[%d] in it..Can not remove it!!!",
1774                                         g_slist_length(info->services));
1775                         }
1776                 }
1777         }
1778 }
1779
1780 static bt_status_t gatt_server_unregister_app(int server_if)
1781 {
1782         CHECK_BTGATT_INIT();
1783         DBG("Un-Register server instance request [%d]", server_if);
1784
1785         if (_bt_hal_is_advertising_in_slot(server_if) == FALSE)
1786                 _bt_hal_free_server_slot(server_if);
1787
1788         /* If server_if belongs to a GATT Server, then delete the GATT server from List */
1789         _bt_hal_remove_gatt_server_from_list(server_if);
1790         return BT_STATUS_SUCCESS;
1791 }
1792
1793 static bt_status_t gatt_server_open(int server_if, const bt_bdaddr_t *bd_addr, bool is_direct)
1794 {
1795         CHECK_BTGATT_INIT();
1796         return BT_STATUS_SUCCESS;
1797 }
1798
1799 static bt_status_t gatt_server_close(int server_if, const bt_bdaddr_t *bd_addr, int conn_id)
1800 {
1801         CHECK_BTGATT_INIT();
1802         return BT_STATUS_SUCCESS;
1803 }
1804
1805 static void __bt_gatt_close_gdbus_connection(void)
1806 {
1807         GError *err = NULL;
1808         DBG("+");
1809
1810         if (g_conn == NULL)
1811                 return;
1812
1813         if (!g_dbus_connection_flush_sync(g_conn, NULL, &err)) {
1814                 ERR("Fail to flush the connection: %s", err->message);
1815                 g_error_free(err);
1816                 err = NULL;
1817         }
1818
1819         if (!g_dbus_connection_close_sync(g_conn, NULL, &err)) {
1820                 if (err) {
1821                         ERR("Fail to close the dbus connection: %s", err->message);
1822                         g_error_free(err);
1823                 }
1824         }
1825
1826         g_object_unref(g_conn);
1827         g_conn = NULL;
1828         DBG("-");
1829 }
1830
1831 static GDBusConnection *__bt_gatt_get_gdbus_connection(void)
1832 {
1833         GDBusConnection *local_system_gconn = NULL;
1834         char *address;
1835         GError *err = NULL;
1836
1837         if (g_conn == NULL) {
1838                 address = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
1839                 if (address == NULL) {
1840                         if (err) {
1841                                 ERR("Failed to get bus address: %s", err->message);
1842                                 g_clear_error(&err);
1843                         }
1844                         return NULL;
1845                 }
1846
1847                 g_conn = g_dbus_connection_new_for_address_sync(address,
1848                                 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
1849                                 G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
1850                                 NULL, /* GDBusAuthObserver */
1851                                 NULL,
1852                                 &err);
1853                 if (!g_conn) {
1854                         if (err) {
1855                                 ERR("Unable to connect to dbus: %s", err->message);
1856                                 g_clear_error(&err);
1857                         }
1858                         return NULL;
1859                 }
1860         } else if (g_dbus_connection_is_closed(g_conn)) {
1861                 address = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
1862                 if (address == NULL) {
1863                         if (err) {
1864                                 ERR("Failed to get bus address: %s", err->message);
1865                                 g_clear_error(&err);
1866                         }
1867                         return NULL;
1868                 }
1869
1870                 local_system_gconn = g_dbus_connection_new_for_address_sync(address,
1871                                 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
1872                                 G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
1873                                 NULL, /* GDBusAuthObserver */
1874                                 NULL,
1875                                 &err);
1876
1877                 if (!local_system_gconn) {
1878                         ERR("Unable to connect to dbus: %s", err->message);
1879                         g_clear_error(&err);
1880                 }
1881
1882                 g_conn = local_system_gconn;
1883         }
1884
1885         return g_conn;
1886 }
1887
1888 static char* __bt_hal_convert_uuid_to_string(bt_uuid_t *srvc_id)
1889 {
1890         char uuid_buf[5];
1891         const char *uuid;
1892
1893         uuid = btuuid2str(srvc_id->uu);
1894         DBG("Original UUID [%s]", uuid);
1895
1896         if (_bt_hal_uuid_is_standard(srvc_id) == TRUE) {
1897                 /* Extract Standard UUID string */
1898                 memcpy(uuid_buf, &uuid[4], 4);
1899                 uuid_buf[4] = '\0';
1900                 DBG("Converted string [%s]", uuid_buf);
1901                 return g_strdup(uuid_buf);
1902         } else
1903                 return strdup(uuid);
1904 }
1905
1906 int __bt_hal_add_service_to_dbus(char *app_path, int slot, btgatt_srvc_id_t *srvc_id)
1907 {
1908         gboolean ret;
1909         /* For GATT service specific */
1910         GDBusNodeInfo *node_info;
1911         guint object_id;
1912         gchar *path = NULL;
1913         struct gatt_service_info *serv_info = NULL;
1914         GVariantBuilder *builder = NULL;
1915         GVariantBuilder *builder1 = NULL;
1916         GVariantBuilder *inner_builder = NULL;
1917         gboolean svc_primary = TRUE;
1918         GError *error = NULL;
1919         hal_gatt_service_added *user_data = NULL;
1920         DBG("Service add to DBUS slot [%d]", slot);
1921
1922         node_info = __bt_gatt_create_method_node_info(
1923                         service_introspection_xml);
1924
1925         if (node_info == NULL)
1926                 return BT_STATUS_FAIL;
1927
1928         DBG("Add new GATT Service: Current GATT Service handle [%d]", gatt_service_handle);
1929         path = g_strdup_printf("%s"GATT_SERV_OBJECT_PATH"%d", app_path, ++gatt_service_handle);
1930         DBG("gatt service path is [%s]", path);
1931
1932         object_id = g_dbus_connection_register_object(g_conn, path,
1933                         node_info->interfaces[0],
1934                         &serv_interface_vtable,
1935                         NULL, NULL, &error);
1936
1937         if (object_id == 0) {
1938                 ERR("failed to register: %s", error->message);
1939                 g_error_free(error);
1940                 g_free(path);
1941                 return BT_STATUS_FAIL;
1942         }
1943         /* Add object_id/gatt service information; it's required at the time of
1944          *  service unregister and Getmanagedobjects
1945          */
1946         serv_info = g_new0(struct gatt_service_info, 1);
1947
1948         serv_info->serv_path = g_strdup(path);
1949         serv_info->serv_id = object_id;
1950         serv_info->service_uuid = __bt_hal_convert_uuid_to_string(&srvc_id->id.uuid);//g_strdup(btuuid2str(srvc_id->id.uuid.uu));
1951         serv_info->is_svc_registered = FALSE;
1952         serv_info->is_svc_primary = svc_primary;
1953         DBG("Service Handle to be added is [%d]", gatt_service_handle);
1954         serv_info->service_handle = gatt_service_handle;
1955
1956         /* Update service in GATT Server service List */
1957         gatt_services = g_slist_append(gatt_services, serv_info);
1958
1959         /* emit interfacesadded signal here for service path */
1960         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
1961         inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1962
1963         g_variant_builder_add(inner_builder, "{sv}",
1964                         "UUID", g_variant_new_string(btuuid2str(srvc_id->id.uuid.uu)));
1965
1966         g_variant_builder_add(inner_builder, "{sv}",
1967                         "Primary", g_variant_new_boolean(svc_primary));
1968
1969         builder1 = g_variant_builder_new(G_VARIANT_TYPE("ao"));
1970
1971         g_variant_builder_add(inner_builder, "{sv}", "Characteristics",
1972                         g_variant_new("ao", builder1));
1973
1974         g_variant_builder_add(builder, "{sa{sv}}",
1975                         GATT_SERV_INTERFACE, inner_builder);
1976
1977         ret = g_dbus_connection_emit_signal(g_conn, NULL, "/",
1978                         "org.freedesktop.Dbus.ObjectManager",
1979                         "InterfacesAdded",
1980                         g_variant_new("(oa{sa{sv}})",
1981                                 path, builder),
1982                         &error);
1983         if (!ret) {
1984                 if (error) {
1985                         /* dbus gives error cause */
1986                         ERR("d-bus api failure: errcode[%x], message[%s]",
1987                                         error->code, error->message);
1988                         g_clear_error(&error);
1989                 }
1990         }
1991
1992         /* Send Service handle to application */
1993         user_data = g_malloc0(sizeof(hal_gatt_service_added));
1994         user_data->srvc_hdl = serv_info->service_handle;
1995         user_data->instance_data = slot;
1996         memcpy(user_data->uuid.uu, srvc_id->id.uuid.uu, sizeof(srvc_id->id.uuid.uu));
1997         g_idle_add(__bt_hal_gatt_service_added_cb, (gpointer)user_data);
1998
1999         /* Save newly created service in GATT Server's service list */
2000         _bt_hal_update_gatt_service_in_gatt_server(slot, serv_info);
2001
2002         /* Free data */
2003         g_free(path);
2004         g_variant_builder_unref(inner_builder);
2005         g_variant_builder_unref(builder);
2006         g_variant_builder_unref(builder1);
2007         return BT_STATUS_SUCCESS;
2008 }
2009
2010 static void __bt_hal_unregister_application_cb(GObject *object, GAsyncResult *res,
2011                 gpointer user_data)
2012 {
2013         char *app_path = (char*)user_data;
2014         INFO("UnregisterApplication is completed app [%s]", app_path);
2015         GError *error = NULL;
2016         GVariant *result = NULL;
2017
2018         if (manager_gproxy)
2019         result = g_dbus_proxy_call_finish(manager_gproxy, res, &error);
2020
2021         if (result == NULL) {
2022                 /* dBUS-RPC is failed */
2023                 ERR("Dbus-RPC is failed\n");
2024
2025                 if (error != NULL) {
2026                         /* dBUS gives error cause */
2027                         ERR("D-Bus API failure: errCode[%x], message[%s]\n",
2028                                         error->code, error->message);
2029                         g_clear_error(&error);
2030                 }
2031         } else {
2032                 g_variant_unref(result);
2033         }
2034         g_free(app_path);
2035 }
2036
2037 static void __bt_hal_gatt_deinit(char *app_path)
2038 {
2039         GDBusProxy *proxy = NULL;
2040         char *data;
2041         INFO("+");
2042
2043         /* Step1: Remove requested App */
2044         proxy = __bt_gatt_gdbus_get_gatt_manager_proxy("org.bluez",
2045                         "/org/bluez/hci0", "org.bluez.GattManager1");
2046
2047         if (proxy == NULL)
2048                 return;
2049
2050         INFO("UnregisterApplication : path [%s]", app_path);
2051
2052         /* Async Call to Unregister Service */
2053         data = g_strdup(app_path);
2054         g_dbus_proxy_call(proxy,
2055                         "UnregisterApplication",
2056                         g_variant_new("(o)",
2057                                 app_path),
2058                         G_DBUS_CALL_FLAGS_NONE, -1,
2059                         NULL,
2060                         (GAsyncReadyCallback)__bt_hal_unregister_application_cb,
2061                         (gpointer)data);
2062
2063         /* If requested app is last GATT Server app, then clean all resources */
2064         if (gatt_server_apps == NULL) {
2065                 INFO("All GATT servers are removed, clean all DBUS resources");
2066                 if (owner_id) {
2067                         /* unregister the exported interface for object manager
2068                            g_conn and manager_id are common for all GATT servers */
2069                         g_dbus_connection_unregister_object(g_conn,
2070                                         manager_id);
2071                         manager_id = 0;
2072                         g_bus_unown_name(owner_id);
2073                         owner_id = 0;
2074
2075                         g_object_unref(manager_gproxy);
2076                         manager_gproxy = NULL;
2077
2078                         /* Close the GDBUS connection */
2079                         __bt_gatt_close_gdbus_connection();
2080                 }
2081         }
2082         INFO("-");
2083 }
2084
2085 int __bt_hal_gatt_init(void)
2086 {
2087         GDBusConnection *conn = NULL;
2088         DBG("+");
2089         /* Only once for ALL GATT Servers */
2090         if (owner_id == 0) {
2091                 owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
2092                                 BT_GATT_SERVICE_NAME,
2093                                 G_BUS_NAME_OWNER_FLAGS_NONE,
2094                                 NULL, NULL, NULL, NULL, NULL);
2095         }
2096         INFO("Owner ID [%d]", owner_id);
2097
2098         /* Only once for ALL GATT Servers conn = g_conn(global)*/
2099         conn = __bt_gatt_get_gdbus_connection();
2100         if (!conn) {
2101                 ERR("Unable to get connection");
2102                 return BT_STATUS_FAIL;
2103         }
2104
2105         /* Only once for ALL GATT Servers */
2106         if (manager_node_info == NULL) {
2107                 /* Register ObjectManager interface */
2108                 manager_node_info = __bt_gatt_create_method_node_info(
2109                                 manager_introspection_xml);
2110
2111                 if (manager_node_info == NULL) {
2112                         ERR("failed to get node info");
2113                         return BT_STATUS_FAIL;
2114                 }
2115         }
2116
2117         INFO("-");
2118         return BT_STATUS_SUCCESS;
2119 }
2120
2121 void _bt_hal_is_gatt_server_initialzed(int slot, char **app_path)
2122 {
2123         GSList *l;
2124
2125         for (l = gatt_server_apps; l; l = g_slist_next(l)) {
2126                 struct gatt_server_app *app = (struct gatt_server_app *)l->data;
2127                 if (app->slot == slot) {
2128                         INFO("GATT Server app found app path [%s] instance [%d]",
2129                                         app->app_path, app->slot);
2130                         *app_path = app->app_path;
2131                         return;
2132                 }
2133         }
2134         /* GATT Server not found */
2135         *app_path = NULL;
2136 }
2137
2138 void _bt_hal_update_gatt_server_path(int slot, char *app_path)
2139 {
2140         if (app_path == NULL)
2141                 return;
2142
2143         struct gatt_server_app *app = g_malloc0(sizeof(struct gatt_server_app));
2144         app->app_path = g_strdup(app_path);
2145         app->slot = slot;
2146         gatt_server_apps = g_slist_append(gatt_server_apps, app);
2147         INFO("GATT Server: Path [%s] Slot [%d]-> Updated", app_path, slot);
2148
2149 }
2150
2151
2152 static bt_status_t gatt_server_add_service(int server_if, btgatt_srvc_id_t *srvc_id,
2153                 int num_handles)
2154 {
2155         CHECK_BTGATT_INIT();
2156         char *app_path = NULL;
2157         GError *error = NULL;
2158         int *app_id = NULL;
2159         guint manager_id;
2160
2161         int result = BT_STATUS_SUCCESS;
2162
2163         DBG("GATT Server Add service request: Instance ID[%d] Is Primary [%d] UUID [%s]",
2164                         server_if, srvc_id->is_primary, btuuid2str(srvc_id->id.uuid.uu));
2165
2166         /* Check if this GATT server Application is already registered with DBUS */
2167         _bt_hal_is_gatt_server_initialzed(server_if, &app_path);
2168
2169         if (app_path != NULL) {
2170                 DBG("GATT server path is already defined [%s]", app_path);
2171                 return __bt_hal_add_service_to_dbus(app_path, server_if, srvc_id);
2172         } else {
2173                 DBG("GATT server application path for instance [%d] is not defined yet", server_if);
2174                 result = __bt_hal_gatt_init();
2175                 if (result != BT_STATUS_SUCCESS)
2176                         return result;
2177
2178                 /* Only once for each GATT Server */
2179                 app_path = g_strdup_printf("/com/%d", server_if);
2180
2181                 app_id = g_malloc0(sizeof(int));
2182                 *app_id = server_if;
2183
2184                 manager_id = g_dbus_connection_register_object(g_conn, app_path,
2185                                 manager_node_info->interfaces[0],
2186                                 &manager_interface_vtable,
2187                                 (gpointer)app_id, NULL, &error);
2188
2189                 if (manager_id == 0) {
2190                         ERR("failed to register: %s", error->message);
2191                         g_error_free(error);
2192                         goto failed;
2193                 }
2194
2195                 /* For current GATT Server, app_path is created, save it in Table */
2196                 _bt_hal_update_gatt_server_path(server_if, app_path);
2197
2198                 /* Add GATT Service to DBUS */
2199                 if (__bt_hal_add_service_to_dbus(app_path, server_if, srvc_id) != BT_STATUS_SUCCESS)
2200                         goto failed;
2201
2202                 g_free(app_path);
2203         }
2204
2205         INFO("Successfully added service");
2206         return BT_STATUS_SUCCESS;
2207 failed:
2208
2209         g_free(app_id);
2210
2211         if (app_path)
2212                 g_free(app_path);
2213         INFO("Service addition failed!!");
2214         return BT_STATUS_FAIL;
2215 }
2216
2217 static bt_status_t gatt_server_add_included_service(int server_if, int service_handle,
2218                 int included_handle)
2219 {
2220         CHECK_BTGATT_INIT();
2221         return BT_STATUS_SUCCESS;
2222 }
2223
2224
2225 static gboolean __bt_is_service_last_in_server_list(int instance, int service_handle)
2226 {
2227         GSList *l;
2228         GSList *gatt_services = NULL;
2229         int len;
2230         struct gatt_service_info *info = NULL;
2231
2232         gatt_services =  _bt_get_service_list_from_server(instance);
2233
2234         len = g_slist_length(gatt_services);
2235         l = g_slist_nth(gatt_services, len -1);
2236         if (NULL == l)
2237                 return FALSE;
2238
2239         info = l->data;
2240
2241         if (info->service_handle == service_handle)
2242                 return TRUE;
2243         return FALSE;
2244 }
2245
2246 static struct gatt_service_info *__bt_gatt_find_gatt_service_info(int instance,
2247                 int service_handle)
2248 {
2249         GSList *l;
2250         GSList *gatt_services = NULL;
2251         INFO("Find Service: svc handle [%d] from GATT Server slot[%d]", service_handle, instance);
2252
2253         gatt_services = _bt_get_service_list_from_server(instance);
2254
2255         for (l = gatt_services; l != NULL; l = g_slist_next(l)) {
2256                 struct gatt_service_info *info = l->data;
2257                 INFO("Got one service with handle [%d]", info->service_handle);
2258                 if (info->service_handle == service_handle)
2259                         return info;
2260         }
2261         ERR("Gatt service with handle [%d] not found", service_handle);
2262         return NULL;
2263 }
2264
2265 static bt_status_t gatt_server_add_characteristic(int slot, int service_handle,
2266                 bt_uuid_t *uuid, int properties,
2267                 int permissions)
2268 {
2269         gboolean ret;
2270         GError *error = NULL;
2271         guint object_id;
2272         GDBusNodeInfo *node_info;
2273         gchar *path = NULL;
2274         GVariantBuilder *builder = NULL;
2275         GVariantBuilder *inner_builder = NULL;
2276         struct gatt_service_info *serv_info = NULL;
2277         struct gatt_char_info *char_info = NULL;
2278         GVariantBuilder *builder2 = NULL;
2279         GVariantBuilder *builder3 = NULL;
2280         GVariant *flags_val = NULL;
2281         int i = 0;
2282         char *char_flags[NUMBER_OF_FLAGS];
2283         int flag_count = 0;
2284         hal_gatt_char_added *user_data = NULL;
2285         int *app_id;
2286
2287         CHECK_BTGATT_INIT();
2288         DBG("Add new characteristic to GATT Service handle [%d]", service_handle);
2289         DBG("Properties of new charateristic [%d] Permission [%d]", properties, permissions);
2290
2291         serv_info = __bt_gatt_find_gatt_service_info(slot, service_handle);
2292         if (serv_info == NULL)
2293                 return BT_STATUS_FAIL;
2294
2295         node_info = __bt_gatt_create_method_node_info(
2296                         characteristics_introspection_xml);
2297
2298         if (node_info == NULL)
2299                 return BT_STATUS_FAIL;
2300
2301         DBG("Add new GATT characteristic: Current GATT char handle [%d]", gatt_char_handle);
2302         path = g_strdup_printf("%s/characteristic%d", serv_info->serv_path, ++gatt_char_handle);
2303         DBG("gatt characteristic path is [%s]", path);
2304
2305         app_id = g_malloc0(sizeof(int));
2306         *app_id = slot;
2307
2308         object_id = g_dbus_connection_register_object(g_conn, path,
2309                         node_info->interfaces[0],
2310                         &char_interface_vtable,
2311                         (gpointer)app_id, NULL, &error);
2312
2313         if (object_id == 0) {
2314                 ERR("failed to register: %s", error->message);
2315                 g_error_free(error);
2316                 g_free(path);
2317                 g_free(app_id);
2318                 return BT_STATUS_FAIL;
2319         }
2320
2321         if (permissions & BT_HAL_GATT_PERMISSION_ENCRYPT_READ)
2322                 properties |= BT_HAL_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_READ;
2323         if (permissions & BT_HAL_GATT_PERMISSION_ENCRYPT_AUTHENTICATED_READ)
2324                 properties |= BT_HAL_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_READ;
2325         if (permissions & BT_HAL_GATT_PERMISSION_ENCRYPT_WRITE)
2326                 properties |= BT_HAL_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_WRITE;
2327         if (permissions & BT_HAL_GATT_PERMISSION_ENCRYPT_AUTHENTICATED_WRITE)
2328                 properties |= BT_HAL_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_WRITE;
2329
2330         flag_count = bt_hal_gatt_convert_prop2string(properties, char_flags);
2331
2332         char_info = g_new0(struct gatt_char_info, 1);
2333
2334         char_info->char_path = g_strdup(path);
2335         char_info->char_id = object_id;
2336         char_info->char_uuid = __bt_hal_convert_uuid_to_string(uuid);//g_strdup(btuuid2str(uuid->uu));
2337         for (i = 0; i < flag_count; i++)
2338                 char_info->char_flags[i] = char_flags[i];
2339
2340
2341         char_info->flags_length = flag_count;
2342         char_info->char_handle = gatt_char_handle;
2343
2344
2345         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
2346         inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
2347
2348         g_variant_builder_add(inner_builder, "{sv}", "UUID",
2349                         g_variant_new("s", char_info->char_uuid));
2350         g_variant_builder_add(inner_builder, "{sv}", "Service",
2351                         g_variant_new("o", serv_info->serv_path));
2352
2353         builder2 = g_variant_builder_new(G_VARIANT_TYPE("as"));
2354
2355         for (i = 0; i < flag_count; i++)
2356                 g_variant_builder_add(builder2, "s", char_flags[i]);
2357
2358         flags_val = g_variant_new("as", builder2);
2359         g_variant_builder_add(inner_builder, "{sv}", "Flags",
2360                         flags_val);
2361
2362         builder3 = g_variant_builder_new(G_VARIANT_TYPE("ao"));
2363
2364         g_variant_builder_add(inner_builder, "{sv}", "Descriptors",
2365                         g_variant_new("ao", builder3));
2366
2367         g_variant_builder_add(builder, "{sa{sv}}",
2368                         GATT_CHAR_INTERFACE,
2369                         inner_builder);
2370
2371         ret = g_dbus_connection_emit_signal(g_conn, NULL, "/",
2372                         "org.freedesktop.Dbus.ObjectManager",
2373                         "InterfacesAdded",
2374                         g_variant_new("(oa{sa{sv}})",
2375                                 path, builder),
2376                         &error);
2377         if (!ret) {
2378                 if (error) {
2379                         /* dbus gives error cause */
2380                         ERR("d-bus api failure: errcode[%x], message[%s]",
2381                                         error->code, error->message);
2382                         g_clear_error(&error);
2383                 }
2384         }
2385
2386         //*char_path = g_strdup(path);
2387
2388         //new_char = TRUE;
2389
2390
2391         /* Send Service handle to application */
2392         user_data = g_malloc0(sizeof(hal_gatt_char_added));
2393         user_data->srvc_hdl = serv_info->service_handle;
2394         user_data->char_hdl = gatt_char_handle;
2395         user_data->instance_data = slot;
2396         memcpy(user_data->uuid.uu, uuid->uu, sizeof(uuid->uu));
2397         g_idle_add(__bt_hal_gatt_char_added_cb, (gpointer)user_data);
2398
2399         /* Save newly created charatcristic to GATT Server's service's characteristic  list */
2400         serv_info->char_data = g_slist_append(serv_info->char_data, char_info);
2401
2402         /* Free data */
2403         g_free(path);
2404
2405         g_variant_builder_unref(inner_builder);
2406         g_variant_builder_unref(builder);
2407         g_variant_builder_unref(builder2);
2408         g_variant_builder_unref(builder3);
2409
2410         return BT_STATUS_SUCCESS;
2411 }
2412
2413 static bt_status_t gatt_server_add_descriptor(int slot, int service_handle, bt_uuid_t *uuid,
2414                 int permissions)
2415 {
2416         CHECK_BTGATT_INIT();
2417
2418 //      static int desc_id = 1;
2419         GError *error = NULL;
2420         guint object_id;
2421         GDBusNodeInfo *node_info;
2422         gchar *path = NULL;
2423         GVariantBuilder *builder = NULL;
2424         GVariantBuilder *inner_builder = NULL;
2425
2426         struct gatt_char_info *char_info = NULL;
2427         struct gatt_desc_info *desc_info = NULL;
2428         struct gatt_service_info *serv_info = NULL;
2429
2430         gchar **line_argv = NULL;
2431         char *serv_path;
2432         char *char_path = NULL;
2433         GSList *l;
2434
2435         GVariantBuilder *builder2 = NULL;
2436         GVariant *flags_val = NULL;
2437         int i = 0;
2438         char *desc_flags[NUMBER_OF_FLAGS];
2439         int flag_count = 0;
2440         int *app_id;
2441
2442         hal_gatt_desc_added *user_data = NULL;
2443 #if 0
2444         if (new_char) {
2445                 desc_id = 1;
2446                 new_char = FALSE;
2447         }
2448 #endif
2449         /* Fetch service data for the GATT server */
2450         serv_info = __bt_gatt_find_gatt_service_info(slot, service_handle);
2451         if (serv_info == NULL)
2452                 return BT_STATUS_FAIL;
2453
2454         /* Fetch list of characteristics from the service info */
2455         l = serv_info->char_data;
2456
2457         /* Fetch last char info from the characteristic list */
2458         char_info = g_slist_last(l)->data;
2459         if (char_info == NULL)
2460                 return BT_STATUS_FAIL;
2461
2462         /* Fetch characteristic path from char info */
2463         char_path = char_info->char_path;
2464
2465         line_argv = g_strsplit_set(char_path, "/", 0);
2466         serv_path = g_strdup_printf("/%s/%s/%s", line_argv[1], line_argv[2], line_argv[3]);
2467
2468
2469         node_info = __bt_gatt_create_method_node_info(
2470                         descriptor_introspection_xml);
2471
2472         if (node_info == NULL) {
2473                 g_strfreev(line_argv);
2474                 g_free(serv_path);
2475                 return BT_STATUS_FAIL;
2476         }
2477
2478         DBG("Add new Descriptor: Current GATT desc handle [%d]", gatt_desc_handle);
2479
2480         path = g_strdup_printf("%s/descriptor%d", char_path, ++gatt_desc_handle);
2481         DBG("gatt descriptor path is [%s]", path);
2482
2483         app_id = g_malloc0(sizeof(int));
2484         *app_id = slot;
2485
2486         object_id = g_dbus_connection_register_object(g_conn, path,
2487                         node_info->interfaces[0],
2488                         &desc_interface_vtable,
2489                         (gpointer)app_id, NULL, &error);
2490
2491         if (object_id == 0) {
2492                 ERR("failed to register: %s", error->message);
2493                 g_error_free(error);
2494                 g_free(path);
2495                 g_strfreev(line_argv);
2496                 g_free(serv_path);
2497                 g_free(app_id);
2498                 return BT_STATUS_FAIL;
2499         }
2500
2501         flag_count = bt_hal_gatt_convert_perm2string(permissions, desc_flags);
2502
2503         desc_info = g_new0(struct gatt_desc_info, 1);
2504
2505         desc_info->desc_path = g_strdup(path);
2506         desc_info->desc_id = object_id;
2507         desc_info->desc_uuid = __bt_hal_convert_uuid_to_string(uuid);//g_strdup(btuuid2str(uuid->uu));
2508
2509         for (i = 0; i < flag_count; i++)
2510                 desc_info->desc_flags[i] = desc_flags[i];
2511
2512         desc_info->flags_length = flag_count;
2513         desc_info->desc_handle = gatt_desc_handle;
2514
2515
2516         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
2517         inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
2518
2519         g_variant_builder_add(inner_builder, "{sv}", "UUID",
2520                         g_variant_new("s", btuuid2str(uuid->uu)));
2521         g_variant_builder_add(inner_builder, "{sv}", "Characteristic",
2522                         g_variant_new("o", char_path));
2523
2524         builder2 = g_variant_builder_new(G_VARIANT_TYPE("as"));
2525
2526         for (i = 0; i < flag_count; i++)
2527                 g_variant_builder_add(builder2, "s", desc_flags[i]);
2528
2529         flags_val = g_variant_new("as", builder2);
2530         g_variant_builder_add(inner_builder, "{sv}", "Flags",
2531                         flags_val);
2532
2533         g_variant_builder_add(builder, "{sa{sv}}",
2534                         GATT_DESC_INTERFACE,
2535                         inner_builder);
2536
2537         g_dbus_connection_emit_signal(g_conn, NULL, "/",
2538                         "org.freedesktop.Dbus.ObjectManager",
2539                         "InterfacesAdded",
2540                         g_variant_new("(oa{sa{sv}})",
2541                                 path, builder),
2542                         &error);
2543         if (error != NULL) {
2544                 ERR("g_dbus_connection_emit_signal failed: errCode[%x], message[%s]",
2545                                 error->code, error->message);
2546                 g_clear_error(&error);
2547         }
2548
2549         //*desc_path = g_strdup(path);
2550
2551         /* Save newly created descriptor to GATT server's service's characteristic */
2552         char_info->desc_data = g_slist_append(char_info->desc_data, desc_info);
2553
2554         /* Send descriptor handle to application */
2555         user_data = g_malloc0(sizeof(hal_gatt_desc_added));
2556         user_data->srvc_hdl = serv_info->service_handle;
2557         user_data->desc_hdl = gatt_desc_handle;
2558         user_data->instance_data = slot;
2559         memcpy(user_data->uuid.uu, uuid->uu, sizeof(uuid->uu));
2560         g_idle_add(__bt_hal_gatt_desc_added_cb, (gpointer)user_data);
2561
2562         /* Free data */
2563         g_free(path);
2564         g_free(serv_path);
2565         g_strfreev(line_argv);
2566         g_variant_builder_unref(inner_builder);
2567         g_variant_builder_unref(builder);
2568         return BT_STATUS_SUCCESS;
2569 }
2570
2571 static void __bt_hal_register_application_cb(GObject *object, GAsyncResult *res, gpointer user_data)
2572 {
2573         GError *error = NULL;
2574         GVariant *result;
2575         char *data = (char*) user_data;
2576         INFO("RegisterApplication is completed path [%s]", data);
2577
2578         result = g_dbus_proxy_call_finish(manager_gproxy, res, &error);
2579
2580         if (result == NULL) {
2581                 /* dBUS-RPC is failed */
2582                 ERR("Dbus-RPC is failed\n");
2583
2584                 if (error != NULL) {
2585                         /* dBUS gives error cause */
2586                         ERR("D-Bus API failure: errCode[%x], message[%s]\n",
2587                                         error->code, error->message);
2588                         g_clear_error(&error);
2589                 }
2590         } else {
2591                 g_variant_unref(result);
2592         }
2593         g_free(data);
2594 }
2595
2596 static GDBusProxy *__bt_hal_gatt_gdbus_init_manager_proxy(const gchar *service,
2597                 const gchar *path, const gchar *interface)
2598 {
2599         GDBusProxy *proxy;
2600         GError *err = NULL;
2601
2602         if (g_conn == NULL)
2603                 g_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM,
2604                                 NULL, &err);
2605
2606         if (!g_conn) {
2607                 if (err) {
2608                         ERR("Unable to connect to gdbus: %s", err->message);
2609                         g_clear_error(&err);
2610                 }
2611                 return NULL;
2612         }
2613
2614         proxy =  g_dbus_proxy_new_sync(g_conn,
2615                         G_DBUS_PROXY_FLAGS_NONE, NULL,
2616                         service, path,
2617                         interface, NULL, &err);
2618
2619         if (!proxy) {
2620                 if (err) {
2621                         ERR("Unable to create proxy: %s", err->message);
2622                         g_clear_error(&err);
2623                 }
2624                 return NULL;
2625         }
2626         manager_gproxy = proxy;
2627
2628         return proxy;
2629 }
2630
2631 static GDBusProxy *__bt_gatt_gdbus_get_gatt_manager_proxy(const gchar *service,
2632                 const gchar *path, const gchar *interface)
2633 {
2634         return (manager_gproxy) ? manager_gproxy :
2635                 __bt_hal_gatt_gdbus_init_manager_proxy(service,
2636                                 path, interface);
2637 }
2638
2639 static void __bt_register_application_to_dbus(int slot)
2640 {
2641         GDBusProxy *proxy = NULL;
2642         char *app_path = NULL;
2643         char *data;
2644         DBG("RegisterApplication slot [%d]", slot);
2645
2646         /* It is impossible that app path is still not initialized */
2647         _bt_hal_is_gatt_server_initialzed(slot, &app_path);
2648
2649         proxy = __bt_gatt_gdbus_get_gatt_manager_proxy("org.bluez",
2650                         "/org/bluez/hci0", "org.bluez.GattManager1");
2651
2652         data = g_strdup(app_path);
2653         g_dbus_proxy_call(proxy,
2654                         "RegisterApplication",
2655                         g_variant_new("(oa{sv})",
2656                                 app_path, NULL),
2657                         G_DBUS_CALL_FLAGS_NONE, -1,
2658                         NULL,
2659                         (GAsyncReadyCallback)__bt_hal_register_application_cb,
2660                         (gpointer)data);
2661         INFO("GATT server started");
2662 }
2663
2664 static bt_status_t gatt_server_start_service(int server_if, int service_handle, int transport)
2665 {
2666         CHECK_BTGATT_INIT();
2667         struct gatt_service_info *serv_info = NULL;
2668         hal_gatt_service_started *user_data = NULL;
2669         DBG("Start GATT Service svc hdl [%d], slot [%d]", service_handle, server_if);
2670
2671         /* Fetch service data for the GATT server */
2672         serv_info = __bt_gatt_find_gatt_service_info(server_if, service_handle);
2673         if (serv_info == NULL)
2674                 return BT_STATUS_FAIL;
2675
2676         if (serv_info->is_svc_registered)
2677                 DBG("service already registered \n");
2678
2679         serv_info->is_svc_registered = TRUE;
2680
2681         /* Send Service handle to application */
2682         user_data = g_malloc0(sizeof(hal_gatt_service_started));
2683         user_data->srvc_hdl = serv_info->service_handle;
2684         user_data->instance_data = server_if;
2685         g_idle_add(__bt_hal_gatt_service_started_cb, (gpointer)user_data);
2686
2687         /* If this is nth Service that is started, then register application at this point */
2688         if (__bt_is_service_last_in_server_list(server_if, service_handle)) {
2689                 DBG("This is the last service started from the GATT Server's list of services handle [%d]",
2690                         service_handle);
2691                 __bt_register_application_to_dbus(server_if);
2692         }
2693
2694         return BT_STATUS_SUCCESS;
2695 }
2696
2697 static bt_status_t gatt_server_stop_service(int server_if, int service_handle)
2698 {
2699         CHECK_BTGATT_INIT();
2700         INFO("Stop service successful");
2701         return BT_STATUS_SUCCESS;
2702 }
2703
2704 static bt_status_t gatt_server_delete_service(int server_if, int service_handle)
2705 {
2706         CHECK_BTGATT_INIT();
2707         struct gatt_service_info *serv_info = NULL;
2708         hal_gatt_service_deleted *user_data = NULL;
2709         GSList *l = NULL;
2710         GSList *l1 = NULL;
2711         int err = BT_STATUS_SUCCESS;
2712         int ret = BT_STATUS_SUCCESS;
2713         INFO("Slot [%d] service handle [%d]", server_if, service_handle);
2714
2715         /* Fetch service data for the GATT server */
2716         serv_info = __bt_gatt_find_gatt_service_info(server_if, service_handle);
2717         if (serv_info == NULL) {
2718                 ERR("Could not find service info svc handle [%d] server slot [%d]",
2719                                 service_handle, server_if);
2720                 return BT_STATUS_FAIL;
2721         }
2722
2723         if (serv_info->is_svc_registered == FALSE) {
2724                 ERR("service Not registered path [%s] handle [%d]",
2725                         serv_info->serv_path, service_handle);
2726         }
2727
2728         for (l = serv_info->char_data; l != NULL; l = g_slist_next(l)) {
2729                 struct gatt_char_info *char_info = l->data;
2730
2731                 if (char_info == NULL)
2732                         break;
2733
2734                 for (l1 = char_info->desc_data; l1 != NULL; l1 = g_slist_next(l1)) {
2735                         struct gatt_desc_info *desc_info = l1->data;
2736
2737                         if (desc_info == NULL)
2738                                 break;
2739
2740                         ret = g_dbus_connection_unregister_object(g_conn,
2741                                         desc_info->desc_id);
2742                         if (ret) {
2743                                 __bt_hal_gatt_emit_interface_removed(
2744                                                 desc_info->desc_path,
2745                                                 GATT_DESC_INTERFACE);
2746                         } else {
2747                                 err = BT_STATUS_FAIL;
2748                         }
2749
2750                         /* list remove & free */
2751                         char_info->desc_data = g_slist_remove(char_info->desc_data, desc_info);
2752                         __bt_hal_gatt_free_descriptor_info(desc_info);
2753                 }
2754
2755                 g_slist_free(char_info->desc_data);
2756                 char_info->desc_data = NULL;
2757
2758                 ret = g_dbus_connection_unregister_object(g_conn,
2759                                 char_info->char_id);
2760                 if (ret) {
2761                         __bt_hal_gatt_emit_interface_removed(char_info->char_path,
2762                                         GATT_CHAR_INTERFACE);
2763                 } else {
2764                         INFO("Err");
2765                         err = BT_STATUS_FAIL;
2766                 }
2767
2768                 /* list remove & free */
2769                 serv_info->char_data = g_slist_remove(serv_info->char_data, char_info);
2770                 __bt_hal_gatt_free_characteristic_info(char_info);
2771         }
2772
2773         g_slist_free(serv_info->char_data);
2774         serv_info->char_data = NULL;
2775
2776         ret = g_dbus_connection_unregister_object(g_conn, serv_info->serv_id);
2777         if (ret) {
2778                 __bt_hal_gatt_emit_interface_removed(serv_info->serv_path,
2779                                 GATT_SERV_INTERFACE);
2780         } else {
2781                 INFO("Failed!!");
2782                 err = BT_STATUS_FAIL;
2783         }
2784
2785         ret = g_dbus_connection_unregister_object(g_conn, serv_info->prop_id);
2786         if (ret)
2787                 DBG("Unregistered the service on properties interface");
2788
2789         /* Remove from global list */
2790         gatt_services = g_slist_remove(gatt_services, serv_info);
2791         INFO("After removing from global list total service dount [%d]", g_slist_length(gatt_services));
2792
2793         /* Remove from GATT Server's list of services */
2794         _bt_remote_service_from_gatt_server(server_if, service_handle);
2795
2796         if (gatt_services == NULL)
2797                 INFO("All GATT Services of all GATT Servers are unregistered");
2798
2799         if (err == BT_STATUS_SUCCESS) {
2800                 INFO("Send GATT Service deleted Event");
2801                 /* Send Service handle to application */
2802                 user_data = g_malloc0(sizeof(hal_gatt_service_deleted));
2803                 user_data->srvc_hdl = serv_info->service_handle;
2804                 user_data->instance_data = server_if;
2805                 g_idle_add(__bt_hal_gatt_service_deleted_cb, (gpointer)user_data);
2806         }
2807
2808         /* Free the service */
2809         __bt_hal_gatt_free_service_info(serv_info);
2810         return err;
2811 }
2812
2813 static gboolean __bt_gatt_get_service_state(const char *service_path)
2814 {
2815         struct gatt_service_info *svc_info = NULL;
2816         GSList *l = NULL;
2817         for (l = gatt_services; l; l = g_slist_next(l)) {
2818
2819                 svc_info = (struct gatt_service_info *)l->data;
2820
2821
2822                 if (!g_strcmp0(svc_info->serv_path, service_path)) {
2823                         DBG("Return the state of the gatt service %d",
2824                                         svc_info->is_svc_registered);
2825                         return svc_info->is_svc_registered;
2826                 }
2827         }
2828
2829         DBG("gatt service info is NULL");
2830         return FALSE;
2831 }
2832
2833 static bt_status_t gatt_server_send_indication(int server_if, int attribute_handle, int conn_id,
2834                 int len, int confirm, char* p_value)
2835 {
2836         CHECK_BTGATT_INIT();
2837
2838         /* For Notifying */
2839         GVariantBuilder *outer_builder;
2840         GVariantBuilder *invalidated_builder;
2841
2842         /* For Value update via PropertyChange */
2843         GVariantBuilder *outer_builder1;
2844         GVariantBuilder *inner_builder1;
2845         GVariantBuilder *invalidated_builder1;
2846         GVariant *update_value = NULL;
2847
2848         /* Other variables */
2849         struct gatt_client_info_t *conn_info = NULL;
2850         gchar *serv_path = NULL;
2851         char *char_path = NULL;
2852         gchar **line_argv = NULL;
2853         gboolean notify = TRUE;
2854         gboolean ret = TRUE;
2855         int err = BT_STATUS_SUCCESS;
2856         char addr[20];
2857         GError *error = NULL;
2858         int i;
2859
2860         memset(addr, 0x00, sizeof(addr));
2861
2862         conn_info = __bt_find_remote_gatt_client_info_from_conn(conn_id);
2863         if (conn_info == NULL) {
2864                 ERR("No Connection Inforamtion!!!");
2865                 return BT_STATUS_FAIL;
2866         }
2867
2868         DBG("Send Indication to GATT client addr [%s]", conn_info->addr);
2869
2870         char_path = __bt_gatt_find_char_path_from_handle(attribute_handle);
2871         if (char_path == NULL)
2872                 return BT_STATUS_FAIL;
2873
2874         line_argv = g_strsplit_set(char_path, "/", 0);
2875         serv_path = g_strdup_printf("/%s/%s/%s", line_argv[1], line_argv[2], line_argv[3]);
2876
2877         if (!__bt_gatt_get_service_state(serv_path)) {
2878                 DBG("service not registered for this characteristic \n");
2879                 g_free(serv_path);
2880                 g_strfreev(line_argv);
2881                 return BT_STATUS_FAIL;
2882         }
2883
2884         outer_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
2885         invalidated_builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
2886
2887         g_variant_builder_add(outer_builder, "{sv}", "Notifying",
2888                         g_variant_new("b", notify));
2889
2890         _bt_hal_convert_addr_type_to_string(addr, (unsigned char *)conn_info->addr);
2891
2892         g_variant_builder_add(outer_builder, "{sv}", "Unicast",
2893                         g_variant_new("s", addr));
2894
2895         DBG("Set characteristic Notification \n");
2896         ret = g_dbus_connection_emit_signal(g_conn, NULL,
2897                         char_path,
2898                         "org.freedesktop.DBus.Properties",
2899                         "PropertiesChanged",
2900                         g_variant_new("(sa{sv}as)",
2901                                 "org.bluez.GattCharacteristic1",
2902                                 outer_builder, invalidated_builder),
2903                         &error);
2904
2905         if (!ret) {
2906                 if (error != NULL) {
2907                         ERR("D-Bus API failure: errCode[%x], \
2908                                         message[%s]",
2909                                         error->code, error->message);
2910                         g_clear_error(&error);
2911                 }
2912                 err = BT_STATUS_FAIL;
2913         }
2914
2915         g_variant_builder_unref(outer_builder);
2916         g_variant_builder_unref(invalidated_builder);
2917
2918
2919         /* Notifying Done, now update Value to Bluez via PropertyChanged */
2920         invalidated_builder1 = g_variant_builder_new(G_VARIANT_TYPE("as"));
2921
2922         inner_builder1 = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
2923         for (i = 0; i < len; i++)
2924                 g_variant_builder_add(inner_builder1, "y", p_value[i]);
2925
2926         update_value = g_variant_new("ay", inner_builder1);
2927
2928
2929         outer_builder1 = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
2930         g_variant_builder_add(outer_builder1, "{sv}", "Value",
2931                         update_value);
2932
2933         DBG("Updating characteristic value \n");
2934         ret = g_dbus_connection_emit_signal(g_conn, NULL,
2935                         char_path,
2936                         "org.freedesktop.DBus.Properties",
2937                         "PropertiesChanged",
2938                         g_variant_new("(sa{sv}as)",
2939                                 "org.bluez.GattCharacteristic1",
2940                                 outer_builder1, invalidated_builder1),
2941                         &error);
2942
2943         if (!ret) {
2944                 if (error != NULL) {
2945                         ERR("D-Bus API failure: errCode[%x], \
2946                                         message[%s]",
2947                                         error->code, error->message);
2948                         g_clear_error(&error);
2949                 }
2950                 err = BT_STATUS_FAIL;
2951         } else {
2952                 struct gatt_char_info *char_info = NULL;
2953
2954                 char_info = __bt_gatt_find_char_info_from_handle(attribute_handle);
2955                 if (char_info == NULL) {
2956                         g_free(serv_path);
2957                         g_strfreev(line_argv);
2958                         g_variant_builder_unref(inner_builder1);
2959                         g_variant_builder_unref(outer_builder1);
2960                         g_variant_builder_unref(invalidated_builder1);
2961
2962                         return BT_STATUS_FAIL;
2963                 }
2964
2965                 char_info->value_length = len;
2966
2967                 char_info->char_value = (char *)realloc(char_info->char_value, len);
2968                 if (char_info->char_value) {
2969                         for (i = 0; i < len; i++)
2970                                 char_info->char_value[i] = p_value[i];
2971                 }
2972         }
2973
2974         g_free(serv_path);
2975         g_strfreev(line_argv);
2976         g_variant_builder_unref(inner_builder1);
2977         g_variant_builder_unref(outer_builder1);
2978         g_variant_builder_unref(invalidated_builder1);
2979
2980         return err;
2981 }
2982
2983 static bt_status_t gatt_server_send_response(int conn_id, int trans_id,
2984                 int status, btgatt_response_t *response)
2985 {
2986         CHECK_BTGATT_INIT();
2987
2988         struct gatt_req_info *req_info = NULL;
2989         struct gatt_client_info_t *conn_info = NULL;
2990         int i;
2991
2992         DBG("GATT Server Send Response Conn ID [%d]", conn_id);
2993
2994         conn_info = __bt_find_remote_gatt_client_info_from_conn(conn_id);
2995         if (conn_info == NULL) {
2996                 ERR("No Connection Inforamtion!!!");
2997                 return BT_STATUS_FAIL;
2998         }
2999
3000         req_info =  __bt_find_remote_gatt_client_request_info(conn_id, trans_id);
3001         if (req_info == NULL) {
3002                 ERR("No Request Inforamtion!!!");
3003                 return BT_STATUS_FAIL;
3004         }
3005
3006         if (status != BT_STATUS_SUCCESS) {
3007                 ERR("resp_state is 0x%X", status);
3008
3009                 g_dbus_method_invocation_return_dbus_error(req_info->context,
3010                                 "org.bluez.Error.Failed", "Application Error");
3011
3012                 conn_info->gatt_req_info_list = g_slist_remove(conn_info->gatt_req_info_list, req_info);
3013
3014                 req_info->context = NULL;
3015                 if (req_info->attr_path)
3016                         g_free(req_info->attr_path);
3017                 if (req_info->svc_path)
3018                         g_free(req_info->svc_path);
3019                 g_free(req_info);
3020
3021                 return BT_STATUS_SUCCESS;
3022         }
3023
3024         DBG("Reponse Value length [%d]", response->attr_value.len);
3025         DBG("Request type: [%d]", req_info->request_type);
3026
3027         /* DEBUG */
3028         for (i = 0; i < response->attr_value.len; i++)
3029                 DBG("Resonse [%d] = [0x%x]", response->attr_value.value[i]);
3030
3031         if (req_info->request_type == BT_HAL_GATT_REQUEST_TYPE_READ) {
3032                 GVariantBuilder *inner_builder = NULL;
3033                 inner_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
3034
3035                 if (response->attr_value.len > 0) {
3036                         for (i = 0; i < response->attr_value.len; i++)
3037                                 g_variant_builder_add(inner_builder, "y", response->attr_value.value[i]);
3038                 }
3039                 g_dbus_method_invocation_return_value(req_info->context,
3040                                 g_variant_new("(ay)", inner_builder));
3041
3042                 g_variant_builder_unref(inner_builder);
3043         } else {
3044                 g_dbus_method_invocation_return_value(req_info->context, NULL);
3045         }
3046         conn_info->gatt_req_info_list = g_slist_remove(conn_info->gatt_req_info_list, req_info);
3047
3048         req_info->context = NULL;
3049         if (req_info->attr_path)
3050                 g_free(req_info->attr_path);
3051         if (req_info->svc_path)
3052                 g_free(req_info->svc_path);
3053         g_free(req_info);
3054
3055         return BT_STATUS_SUCCESS;
3056 }
3057
3058
3059 static bt_status_t gatt_server_send_acquire_response(int conn_id, int trans_id,
3060                 int status, int fd, int mtu, void *fdlist)
3061 {
3062         CHECK_BTGATT_INIT();
3063
3064         struct gatt_req_info *req_info = NULL;
3065         struct gatt_client_info_t *conn_info = NULL;
3066         int i;
3067
3068         DBG("GATT Server Send Response Conn ID [%d]", conn_id);
3069
3070         conn_info = __bt_find_remote_gatt_client_info_from_conn(conn_id);
3071         if (conn_info == NULL) {
3072                 ERR("No Connection Inforamtion!!!");
3073                 return BT_STATUS_FAIL;
3074         }
3075
3076         req_info =  __bt_find_remote_gatt_client_request_info(conn_id, trans_id);
3077         if (req_info == NULL) {
3078                 ERR("No Request Inforamtion!!!");
3079                 return BT_STATUS_FAIL;
3080         }
3081
3082         if (status != BT_STATUS_SUCCESS) {
3083                 ERR("resp_state is 0x%X", status);
3084
3085                 g_dbus_method_invocation_return_dbus_error(req_info->context,
3086                                 "org.bluez.Error.Failed", "Application Error");
3087
3088                 conn_info->gatt_req_info_list = g_slist_remove(conn_info->gatt_req_info_list, req_info);
3089
3090                 req_info->context = NULL;
3091                 if (req_info->attr_path)
3092                         g_free(req_info->attr_path);
3093                 if (req_info->svc_path)
3094                         g_free(req_info->svc_path);
3095                 g_free(req_info);
3096
3097                 return BT_STATUS_SUCCESS;
3098         }
3099
3100
3101          if (req_info->request_type == BT_HAL_GATT_REQUEST_TYPE_ACQUIRE_WRITE) {
3102
3103                 INFO("GATT Server Send Response BT_HAL_GATT_REQUEST_TYPE_ACQUIRE_WRITE to bluez  %d\n", fd);
3104                 g_dbus_method_invocation_return_value_with_unix_fd_list(
3105                                 req_info->context, g_variant_new("(hq)", 0, mtu), (GUnixFDList*)fdlist);
3106
3107         } else if (req_info->request_type == BT_HAL_GATT_REQUEST_TYPE_ACQUIRE_NOTIFY) {
3108
3109                 INFO("GATT Server Send Response BT_HAL_GATT_REQUEST_TYPE_ACQUIRE_NOTIFY  to bluez  %d\n", fd);
3110                 g_dbus_method_invocation_return_value_with_unix_fd_list(
3111                                 req_info->context, g_variant_new("(hq)", 0, mtu), (GUnixFDList*)fdlist);
3112
3113         } else {
3114                 g_dbus_method_invocation_return_value(req_info->context, NULL);
3115         }
3116         conn_info->gatt_req_info_list = g_slist_remove(conn_info->gatt_req_info_list, req_info);
3117
3118         req_info->context = NULL;
3119         if (req_info->attr_path)
3120                 g_free(req_info->attr_path);
3121         if (req_info->svc_path)
3122                 g_free(req_info->svc_path);
3123         g_free(req_info);
3124
3125         return BT_STATUS_SUCCESS;
3126 }
3127
3128
3129 static bt_status_t gatt_server_update_att_value(int server_if, int attribute_handle,
3130                 int value_length, char* att_value)
3131 {
3132         CHECK_BTGATT_INIT();
3133
3134         /* Other variables */
3135         char *char_path = NULL;
3136         gboolean ret = TRUE;
3137         GError *error = NULL;
3138
3139         GVariantBuilder *outer_builder;
3140         GVariantBuilder *inner_builder;
3141         GVariantBuilder *invalidated_builder;
3142         GVariant *update_value = NULL;
3143         int err = BT_STATUS_SUCCESS;
3144         int i = 0;
3145         gchar **line_argv = NULL;
3146         gchar *serv_path = NULL;
3147
3148         char_path = __bt_gatt_find_char_path_from_handle(attribute_handle);
3149         if (char_path == NULL)
3150                 return BT_STATUS_FAIL;
3151
3152         line_argv = g_strsplit_set(char_path, "/", 0);
3153         serv_path = g_strdup_printf("/%s/%s/%s", line_argv[1], line_argv[2], line_argv[3]);
3154
3155         if (!__bt_gatt_get_service_state(serv_path)) {
3156                 DBG("service not registered for this characteristic \n");
3157                 g_free(serv_path);
3158                 g_strfreev(line_argv);
3159                 return BT_STATUS_FAIL;
3160         }
3161
3162         g_free(serv_path);
3163         line_argv = g_strsplit_set(char_path, "/", 0);
3164         serv_path = g_strdup_printf("/%s/%s/%s", line_argv[1], line_argv[2], line_argv[3]);
3165
3166         if (!__bt_gatt_get_service_state(serv_path)) {
3167                 DBG("service not registered for this characteristic \n");
3168                 g_free(serv_path);
3169                 g_strfreev(line_argv);
3170                 return BT_STATUS_FAIL;
3171         }
3172
3173         outer_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
3174         invalidated_builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
3175
3176         inner_builder = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
3177         for (i = 0; i < value_length; i++)
3178                 g_variant_builder_add(inner_builder, "y", att_value[i]);
3179
3180         update_value = g_variant_new("ay", inner_builder);
3181
3182         g_variant_builder_add(outer_builder, "{sv}", "Value",
3183                         update_value);
3184
3185         DBG("Updating characteristic value \n");
3186         ret = g_dbus_connection_emit_signal(g_conn, NULL,
3187                         char_path,
3188                         "org.freedesktop.DBus.Properties",
3189                         "PropertiesChanged",
3190                         g_variant_new("(sa{sv}as)",
3191                                 "org.bluez.GattCharacteristic1",
3192                                 outer_builder, invalidated_builder),
3193                         &error);
3194
3195         if (!ret) {
3196                 if (error != NULL) {
3197                         ERR("D-Bus API failure: errCode[%x], \
3198                                         message[%s]",
3199                                         error->code, error->message);
3200                         g_clear_error(&error);
3201                 }
3202                 err = BT_STATUS_FAIL;
3203         } else {
3204                 struct gatt_char_info *char_info = NULL;
3205
3206                 char_info = __bt_gatt_find_char_info_from_handle(attribute_handle);
3207                 if (char_info == NULL) {
3208                         g_free(serv_path);
3209                         g_strfreev(line_argv);
3210                         g_variant_builder_unref(inner_builder);
3211                         g_variant_builder_unref(outer_builder);
3212                         g_variant_builder_unref(invalidated_builder);
3213
3214                         return BT_STATUS_FAIL;
3215                 }
3216
3217                 char_info->value_length = value_length;
3218
3219                 char_info->char_value = (char *)realloc(char_info->char_value, value_length);
3220                 if (char_info->char_value) {
3221                         for (i = 0; i < value_length; i++)
3222                                 char_info->char_value[i] = att_value[i];
3223                 }
3224         }
3225
3226         /* Free data */
3227         g_free(serv_path);
3228         g_strfreev(line_argv);
3229         g_variant_builder_unref(inner_builder);
3230         g_variant_builder_unref(outer_builder);
3231         g_variant_builder_unref(invalidated_builder);
3232
3233         return err;
3234 }
3235
3236 static bt_status_t gatt_server_listen(int server_if, bool start)
3237 {
3238         CHECK_BTGATT_INIT();
3239         /* Send Data to LE Module */
3240         return _bt_hal_enable_advertising(server_if, start, FALSE);
3241 }
3242
3243 static bt_status_t gatt_server_set_adv_data(int server_if, bool set_scan_rsp, bool include_name,
3244                 bool include_txpower, int min_interval, int max_interval, int appearance,
3245                 uint16_t manufacturer_len, char* manufacturer_data,
3246                 uint16_t service_data_len, char* service_data,
3247                 uint16_t service_uuid_len, char* service_uuid)
3248 {
3249         CHECK_BTGATT_INIT();
3250         return BT_STATUS_SUCCESS;
3251 }
3252
3253 static bt_status_t gatt_server_multi_adv_enable(int server_if)
3254 {
3255         CHECK_BTGATT_INIT();
3256         /* Send Enable Advertising request to LE Module */
3257         return _bt_hal_enable_advertising(server_if, TRUE, TRUE);
3258 }
3259
3260 static bt_status_t gatt_server_multi_adv_update(int server_if, int min_interval, int max_interval, int adv_type,
3261                 int chnl_map, int tx_power, int timeout_s)
3262 {
3263         CHECK_BTGATT_INIT();
3264         DBG("+");
3265         /* Send Advertising parameters to LE Module */
3266         return _bt_hal_set_advertising_params(server_if, min_interval, max_interval, adv_type,
3267                         chnl_map, tx_power, timeout_s);
3268 }
3269
3270 static bt_status_t gatt_server_multi_adv_set_inst_data(btgatt_adv_param_setup_t adv_param_setup)
3271 {
3272         CHECK_BTGATT_INIT();
3273         DBG("+");
3274         /* Send Data to LE Module */
3275         return _bt_hal_set_advertising_data(adv_param_setup);
3276 }
3277
3278 static bt_status_t gatt_server_multi_adv_disable(int server_if)
3279 {
3280         CHECK_BTGATT_INIT();
3281         /* Send Data to LE Module */
3282         return _bt_hal_enable_advertising(server_if, FALSE, TRUE);
3283 }
3284
3285 static bt_status_t gatt_server_get_mtu_size(int conn_id, int *mtu_size)
3286 {
3287         CHECK_BTGATT_INIT();
3288         char *object_path = NULL;
3289
3290         GDBusProxy *device_proxy;
3291         GError *error = NULL;
3292         GVariant *value;
3293         GVariant *tmp_value;
3294         GDBusConnection *conn;
3295         GVariant *result = NULL;
3296         int ret = BT_STATUS_SUCCESS;
3297         struct gatt_client_info_t *conn_info = NULL;
3298         unsigned int mtu;
3299
3300         if (mtu_size == NULL)
3301                 return BT_STATUS_PARM_INVALID;
3302
3303         /* GDBUS Connection Info validate */
3304         conn = _bt_hal_get_system_gconn();
3305         if (conn == NULL) {
3306                 ERR("Could not get System DBUS Connection");
3307                 return  BT_STATUS_FAIL;
3308         }
3309
3310         /* Connection Info validate */
3311         conn_info = __bt_find_remote_gatt_client_info_from_conn(conn_id);
3312         if (conn_info == NULL) {
3313                 ERR("No Connection Inforamtion!!!");
3314                 return BT_STATUS_FAIL;
3315         }
3316
3317
3318         object_path = _bt_hal_get_device_object_path(conn_info->addr);
3319         if (object_path == NULL)
3320                 return BT_STATUS_FAIL;
3321
3322         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3323                         NULL, BT_HAL_BLUEZ_NAME, object_path,
3324                         BT_HAL_PROPERTIES_INTERFACE,  NULL, NULL);
3325
3326         g_free(object_path);
3327         if (device_proxy == NULL)
3328                 return BT_STATUS_FAIL;
3329
3330         result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
3331                         g_variant_new("(s)", BT_HAL_DEVICE_INTERFACE),
3332                         G_DBUS_CALL_FLAGS_NONE,
3333                         -1,
3334                         NULL,
3335                         &error);
3336         if (result == NULL) {
3337                 if (error != NULL) {
3338                         ERR("Error occured in Proxy call [%s]\n", error->message);
3339                         g_error_free(error);
3340                 }
3341                 g_object_unref(device_proxy);
3342                 return BT_STATUS_FAIL;
3343         }
3344
3345         g_variant_get(result , "(@a{sv})", &value);
3346         g_variant_unref(result);
3347
3348         tmp_value = g_variant_lookup_value(value, "AttMtu", G_VARIANT_TYPE_UINT16);
3349         if (tmp_value == NULL) {
3350                 g_object_unref(device_proxy);
3351                 g_variant_unref(value);
3352                 return BT_STATUS_FAIL;
3353         }
3354
3355         mtu = g_variant_get_uint16(tmp_value);
3356
3357         DBG("ATT MTU : [%d]", mtu);
3358
3359         g_variant_unref(tmp_value);
3360         g_variant_unref(value);
3361         g_object_unref(device_proxy);
3362
3363         *mtu_size = (int) mtu;
3364
3365         return ret;
3366 }
3367
3368 const btgatt_server_interface_t btgatt_server_interface = {
3369         gatt_server_register_app,
3370         gatt_server_unregister_app,
3371         gatt_server_open,
3372         gatt_server_close,
3373         gatt_server_add_service,
3374         gatt_server_add_included_service,
3375         gatt_server_add_characteristic,
3376         gatt_server_add_descriptor,
3377         gatt_server_start_service,
3378         gatt_server_stop_service,
3379         gatt_server_delete_service,
3380         gatt_server_send_indication,
3381         gatt_server_send_response,
3382         gatt_server_update_att_value,
3383         gatt_server_listen,
3384         gatt_server_set_adv_data,
3385         gatt_server_multi_adv_enable,
3386         gatt_server_multi_adv_update,
3387         gatt_server_multi_adv_set_inst_data,
3388         gatt_server_multi_adv_disable,
3389         gatt_server_get_mtu_size,
3390         gatt_server_send_acquire_response
3391 };