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