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