Add the memory free functions for gatt info data
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-gatt-service.c
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *              http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include<gio/gio.h>
19 #include<glib.h>
20 #include<glib/gprintf.h>
21 #include<stdlib.h>
22 #include<unistd.h>
23 #include<stdint.h>
24 #include<stdbool.h>
25
26 #include "bt-common.h"
27 #include "bt-internal-types.h"
28
29 #define NUMBER_OF_FLAGS 10
30
31 GDBusConnection *g_conn;
32 GDBusNodeInfo *obj_info;
33 guint owner_id;
34 guint manager_id;
35 static gboolean new_service = FALSE;
36 static gboolean new_char = FALSE;
37 static int serv_id = 1;
38 static int register_pending_cnt = 0;
39 static bool is_server_started = false;
40
41 /* Introspection data for the service we are exporting */
42 static const gchar service_introspection_xml[] =
43 "<node name='/'>"
44 "  <interface name='org.freedesktop.DBus.Properties'>"
45 "    <property type='s' name='UUID' access='read'>"
46 "    </property>"
47 "        <property type='b' name='primary' access='read'>"
48 "        </property>"
49 "        <property type='o' name='Device' access='read'>"
50 "        </property>"
51 "        <property type='ao' name='Characteristics' access='read'>"
52 "        </property>"
53 "        <property type='s' name='Includes' access='read'>"
54 "        </property>"
55 "  </interface>"
56 "</node>";
57
58 /* Introspection data for the characteristics we are exporting */
59 static const gchar characteristics_introspection_xml[] =
60 "<node name='/'>"
61 "  <interface name='org.bluez.GattCharacteristic1'>"
62 "        <method name='ReadValue'>"
63 "               <arg type='s' name='address' direction='in'/>"
64 "               <arg type='y' name='id' direction='in'/>"
65 "               <arg type='q' name='offset' direction='in'/>"
66 "               <arg type='ay' name='Value' direction='out'/>"
67 "        </method>"
68 "        <method name='WriteValue'>"
69 "               <arg type='s' name='address' direction='in'/>"
70 "               <arg type='y' name='id' direction='in'/>"
71 "               <arg type='q' name='offset' direction='in'/>"
72 "               <arg type='ay' name='value' direction='in'/>"
73 "        </method>"
74 "        <method name='StartNotify'>"
75 "        </method>"
76 "        <method name='StopNotify'>"
77 "        </method>"
78 "        <method name='IndicateConfirm'>"
79 "               <arg type='s' name='address' direction='in'/>"
80 "               <arg type='b' name='complete' direction='in'/>"
81 "        </method>"
82 "  </interface>"
83 "  <interface name='org.freedesktop.DBus.Properties'>"
84 "    <property type='s' name='UUID' access='read'>"
85 "    </property>"
86 "    <property type='o' name='Service' access='read'>"
87 "    </property>"
88 "    <property type='ay' name='Value' access='readwrite'>"
89 "    </property>"
90 "        <property type='b' name='Notifying' access='read'>"
91 "        </property>"
92 "    <property type='as' name='Flags' access='read'>"
93 "    </property>"
94 "    <property type='s' name='Unicast' access='read'>"
95 "    </property>"
96 "        <property type='ao' name='Descriptors' access='read'>"
97 "        </property>"
98 "  </interface>"
99 "</node>";
100
101 /* Introspection data for the descriptor we are exporting */
102 static const gchar descriptor_introspection_xml[] =
103 "<node name='/'>"
104 "  <interface name='org.bluez.GattDescriptor1'>"
105 "        <method name='ReadValue'>"
106 "               <arg type='s' name='address' direction='in'/>"
107 "               <arg type='y' name='id' direction='in'/>"
108 "               <arg type='q' name='offset' direction='in'/>"
109 "               <arg type='ay' name='Value' direction='out'/>"
110 "        </method>"
111 "        <method name='WriteValue'>"
112 "               <arg type='s' name='address' direction='in'/>"
113 "               <arg type='y' name='id' direction='in'/>"
114 "               <arg type='q' name='offset' direction='in'/>"
115 "               <arg type='ay' name='value' direction='in'/>"
116 "        </method>"
117 "  </interface>"
118 "  <interface name='org.freedesktop.DBus.Properties'>"
119 "    <property type='s' name='UUID' access='read'>"
120 "    </property>"
121 "    <property type='o' name='Characteristic' access='read'>"
122 "    </property>"
123 "    <property type='ay' name='Value' access='read'>"
124 "    </property>"
125 "    <property type='as' name='Flags' access='read'>"
126 "    </property>"
127 "  </interface>"
128 "</node>";
129
130 static const gchar manager_introspection_xml[] =
131 "<node name='/'>"
132 "  <interface name='org.freedesktop.DBus.ObjectManager'>"
133 "    <method name='GetManagedObjects'>"
134 "     <arg type='a{oa{sa{sv}}}' name='object_paths_interfaces_and_properties' direction='out'/>"
135 "        </method>"
136 "  </interface>"
137 "</node>";
138
139 struct gatt_service_info {
140         gchar *serv_path;
141         guint serv_id;
142         gchar *service_uuid;
143         guint manager_id;
144         guint prop_id;
145         GSList *char_data;
146         gboolean is_svc_registered;
147         gboolean is_svc_primary;
148 };
149
150 struct gatt_char_info {
151         gchar *char_path;
152         guint char_id;
153         gchar *char_uuid;
154         gchar *char_value;
155         gchar *char_flags[NUMBER_OF_FLAGS];
156         int value_length;
157         int flags_length;
158         GSList *desc_data;
159 };
160
161 struct gatt_desc_info {
162         gchar *desc_path;
163         guint desc_id;
164         gchar *desc_uuid;
165         gchar *desc_value;
166         gchar *desc_flags[NUMBER_OF_FLAGS];
167         int value_length;
168         int flags_length;
169 };
170
171 struct gatt_req_info {
172         gchar *attr_path;
173         gchar *svc_path;
174         guint  request_id;
175         guint  offset;
176         GDBusMethodInvocation *context;
177 };
178
179 static GSList *gatt_services = NULL;
180 static GSList *gatt_requests = NULL;
181 static gchar *app_path = NULL;
182
183 #define BT_GATT_SERVICE_NAME    "org.frwk.gatt_service"
184 #define BT_GATT_SERVICE_PATH "/org/frwk/gatt_service"
185
186 #define GATT_SERV_OBJECT_PATH   "/service"
187
188 #define GATT_MNGR_INTERFACE             "org.bluez.GattManager1"
189 #define GATT_SERV_INTERFACE             "org.bluez.GattService1"
190 #define GATT_CHAR_INTERFACE             "org.bluez.GattCharacteristic1"
191 #define GATT_DESC_INTERFACE             "org.bluez.GattDescriptor1"
192
193 #ifdef HPS_FEATURE
194 #define BT_HPS_OBJECT_PATH "/org/projectx/httpproxy"
195 #define BT_HPS_INTERFACE_NAME "org.projectx.httpproxy_service"
196 #define PROPERTIES_CHANGED "PropertiesChanged"
197 #define BT_HPS_PROPERTIES_INTERFACE "org.freedesktop.DBus.Properties"
198 #endif
199
200 static GDBusProxy *manager_gproxy = NULL;
201
202 static struct gatt_char_info *__bt_gatt_find_gatt_char_info(
203                         const char *service_path, const char *char_path);
204 static struct gatt_desc_info *__bt_gatt_find_gatt_desc_info(
205                         const char *serv_path, const char *char_path,
206                         const char *desc_path);
207
208 static struct gatt_req_info *__bt_gatt_find_request_info(guint request_id);
209
210 static void __bt_gatt_close_gdbus_connection(void)
211 {
212         GError *err = NULL;
213
214         BT_DBG("+");
215
216         ret_if(g_conn == NULL);
217
218         if (!g_dbus_connection_flush_sync(g_conn, NULL, &err)) {
219                 BT_ERR("Fail to flush the connection: %s", err->message);
220                 g_error_free(err);
221                 err = NULL;
222         }
223
224         if (!g_dbus_connection_close_sync(g_conn, NULL, &err)) {
225                 if (err) {
226                         BT_ERR("Fail to close the dbus connection: %s", err->message);
227                         g_error_free(err);
228                 }
229         }
230
231         g_object_unref(g_conn);
232
233         g_conn = NULL;
234
235         BT_DBG("-");
236 }
237
238 #ifdef HPS_FEATURE
239 static int __bt_send_event_to_hps(int event, GVariant *var)
240 {
241         GError *error = NULL;
242         GVariant *parameters;
243         GDBusMessage *msg = NULL;
244
245         BT_DBG(" ");
246
247         retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
248
249         if (event == BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED) {
250                 GVariantBuilder *inner_builder;
251                 GVariantBuilder *invalidated_builder;
252
253                 BT_DBG("BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED");
254                 inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
255
256                 g_variant_builder_add(inner_builder, "{sv}", "WriteValue", var);
257
258                 invalidated_builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
259
260                 parameters = g_variant_new("(a{sv}as)", inner_builder, invalidated_builder);
261                 g_variant_builder_unref(invalidated_builder);
262                 g_variant_builder_unref(inner_builder);
263         } else if (BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED) {
264                 GVariantBuilder *inner_builder;
265                 GVariantBuilder *invalidated_builder;
266
267                 BT_DBG("BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED");
268                 inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
269
270                 g_variant_builder_add(inner_builder, "{sv}", "ReadValue", var);
271
272                 invalidated_builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
273
274                 parameters = g_variant_new("(a{sv}as)", inner_builder, invalidated_builder);
275                 g_variant_builder_unref(invalidated_builder);
276                 g_variant_builder_unref(inner_builder);
277         }
278
279         msg = g_dbus_message_new_signal(BT_HPS_OBJECT_PATH, BT_HPS_INTERFACE_NAME, PROPERTIES_CHANGED);
280         g_dbus_message_set_body(msg, parameters);
281         if (!g_dbus_connection_send_message(g_conn, msg, G_DBUS_SEND_MESSAGE_FLAGS_NONE, 0, NULL)) {
282                 if (error != NULL) {
283                         BT_ERR("D-Bus API failure: errCode[%x], \
284                                         message[%s]",
285                                         error->code, error->message);
286                         g_clear_error(&error);
287                 }
288                 return BLUETOOTH_ERROR_INTERNAL;
289         }
290         return BLUETOOTH_ERROR_NONE;
291 }
292 #endif
293
294 static void __bt_gatt_manager_method_call(GDBusConnection *connection,
295                                         const gchar *sender,
296                                         const gchar *object_path,
297                                         const gchar *interface_name,
298                                         const gchar *method_name,
299                                         GVariant *parameters,
300                                         GDBusMethodInvocation *invocation,
301                                         gpointer user_data)
302 {
303         GSList *l1;
304         int len = 0;
305         int i = 0;
306
307         if (g_strcmp0(method_name, "GetManagedObjects") == 0) {
308                 BT_DBG("Getting values for service, chars and descriptors");
309
310                 GVariantBuilder *builder;
311                 GVariantBuilder *inner_builder1 = NULL;
312                 GVariant *svc_char = NULL;
313                 GSList *l4;
314                 /*Main Builder */
315                 builder = g_variant_builder_new(
316                                 G_VARIANT_TYPE("a{oa{sa{sv}}}"));
317
318                 /* Prepare inner builder for GattService1 interface */
319
320                 len = g_slist_length(gatt_services);
321
322                 for (i = 0; i <= len; i++) {
323                         GVariantBuilder *svc_builder = NULL;
324                         GVariantBuilder *inner_builder = NULL;
325
326                         if (register_pending_cnt > 1) {
327                                 l1 = g_slist_nth(gatt_services, len - register_pending_cnt);
328                         } else {
329                                 l1 = g_slist_last(gatt_services);
330                         }
331                         register_pending_cnt--;
332
333                         struct gatt_service_info *serv_info = l1->data;
334                         if (serv_info == NULL) {
335                                 BT_ERR("service info value is NULL");
336                                 g_dbus_method_invocation_return_value(invocation, NULL);
337                                 return;
338                         }
339
340                         /* Prepare inner builder for GattService1 interface */
341                         BT_DBG("Creating builder for service");
342                         svc_builder = g_variant_builder_new(
343                                                 G_VARIANT_TYPE("a{sa{sv}}"));
344                         inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
345
346                         g_variant_builder_add(inner_builder, "{sv}", "UUID",
347                                         g_variant_new_string(serv_info->service_uuid));
348
349                         g_variant_builder_add(inner_builder, "{sv}", "Primary",
350                                         g_variant_new_boolean(serv_info->is_svc_primary));
351
352                         /*Characteristics*/
353                         inner_builder1 = g_variant_builder_new(G_VARIANT_TYPE("ao"));
354                         BT_DBG("Adding Charatarisitcs list");
355                         for (l4 = serv_info->char_data; l4 != NULL; l4 = l4->next) {
356                                 struct gatt_char_info *char_info = l4->data;
357                                         g_variant_builder_add(inner_builder1, "o",
358                                                 char_info->char_path);
359                                         BT_DBG("%s", char_info->char_path);
360                         }
361
362                         svc_char = g_variant_new("ao", inner_builder1);
363                         g_variant_builder_add(inner_builder, "{sv}", "Characteristics",
364                                                 svc_char);
365
366                         g_variant_builder_add(svc_builder, "{sa{sv}}",
367                                                                 GATT_SERV_INTERFACE,
368                                                                 inner_builder);
369
370                         g_variant_builder_add(builder, "{oa{sa{sv}}}",
371                                                                 serv_info->serv_path,
372                                                                 svc_builder);
373
374                         g_variant_builder_unref(inner_builder1);
375
376                         /* Prepare inner builder for GattCharacteristic1 interface */
377
378                         GSList *l2 = serv_info->char_data;
379                         BT_DBG("Creating builder for characteristics \n");
380
381                         if (l2 == NULL)
382                                 BT_DBG("characteristic data is NULL");
383
384                         for (l2 = serv_info->char_data; l2 != NULL; l2 = l2->next) {
385
386                                 GVariantBuilder *char_builder = NULL;
387                                 GVariantBuilder *inner_builder = NULL;
388                                 GVariantBuilder *builder1 = NULL;
389                                 GVariantBuilder *builder2 = NULL;
390                                 GVariantBuilder *builder3 = NULL;
391                                 GVariant *char_val = NULL;
392                                 GVariant *flags_val = NULL;
393                                 GVariant *char_desc = NULL;
394                                 char *unicast = NULL;
395                                 gboolean notify = FALSE;
396                                 int i = 0;
397
398                                 char_builder = g_variant_builder_new(
399                                                                 G_VARIANT_TYPE(
400                                                                         "a{sa{sv}}"));
401                                 inner_builder = g_variant_builder_new(
402                                                                 G_VARIANT_TYPE(
403                                                                         "a{sv}"));
404
405                                 struct gatt_char_info *char_info = l2->data;
406                                 if (char_info == NULL) {
407                                         BT_ERR("char_info is NULL");
408                                         continue;
409                                 }
410
411                                 /*Uuid*/
412                                 g_variant_builder_add(inner_builder, "{sv}", "UUID",
413                                         g_variant_new_string(char_info->char_uuid));
414                                 /*Service*/
415                                 g_variant_builder_add(inner_builder, "{sv}", "Service",
416                                         g_variant_new("o", serv_info->serv_path));
417                                 /*Value*/
418                                 builder1 = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
419
420                                 if (char_info->char_value != NULL) {
421                                         for (i = 0; i < char_info->value_length; i++) {
422                                                 g_variant_builder_add(builder1, "y",
423                                                         char_info->char_value[i]);
424                                         }
425                                         char_val = g_variant_new("ay", builder1);
426                                         g_variant_builder_add(inner_builder, "{sv}",
427                                                         "Value", char_val);
428                                 }
429                                 /*Flags*/
430                                 builder2 = g_variant_builder_new(G_VARIANT_TYPE("as"));
431
432                                 for (i = 0; i < char_info->flags_length; i++) {
433                                         g_variant_builder_add(builder2, "s",
434                                                 char_info->char_flags[i]);
435                                 }
436
437                                 flags_val = g_variant_new("as", builder2);
438                                 g_variant_builder_add(inner_builder, "{sv}", "Flags",
439                                                         flags_val);
440
441                                 /* Notifying */
442                                 g_variant_builder_add(inner_builder, "{sv}", "Notifying",
443                                                         g_variant_new("b", notify));
444
445                                 /* Unicast */
446                                 unicast = g_strdup("00:00:00:00:00:00");
447                                 g_variant_builder_add(inner_builder, "{sv}", "Unicast",
448                                                         g_variant_new("s", unicast));
449
450                                 /*Descriptors*/
451                                 builder3 = g_variant_builder_new(G_VARIANT_TYPE("ao"));
452                                 BT_DBG("Adding Descriptors list");
453
454                                 for (l4 = char_info->desc_data; l4 != NULL; l4 = l4->next) {
455                                         struct gatt_desc_info *desc_info = l4->data;
456                                                 g_variant_builder_add(builder3, "o",
457                                                         desc_info->desc_path);
458                                                 BT_DBG("%s", desc_info->desc_path);
459                                 }
460
461                                 char_desc = g_variant_new("ao", builder3);
462                                 g_variant_builder_add(inner_builder, "{sv}", "Descriptors",
463                                                         char_desc);
464
465                                 g_variant_builder_add(char_builder, "{sa{sv}}",
466                                                 GATT_CHAR_INTERFACE , inner_builder);
467                                 g_variant_builder_add(builder, "{oa{sa{sv}}}",
468                                                 char_info->char_path, char_builder);
469
470                                 /*Prepare inner builder for GattDescriptor1 interface*/
471
472                                 GSList *l3 = char_info->desc_data;
473
474                                 if (l3 == NULL)
475                                         BT_DBG("descriptor data is NULL");
476
477                                 for (l3 = char_info->desc_data; l3 != NULL; l3 = l3->next) {
478
479                                         BT_DBG("Creating builder for descriptor \n");
480
481                                         GVariantBuilder *desc_builder = NULL;
482                                         GVariantBuilder *inner_builder = NULL;
483                                         GVariantBuilder *builder1 = NULL;
484                                         GVariantBuilder *builder2 = NULL;
485                                         GVariant *desc_val = NULL;
486
487                                         desc_builder = g_variant_builder_new(
488                                                                 G_VARIANT_TYPE(
489                                                                 "a{sa{sv}}"));
490                                         inner_builder = g_variant_builder_new(
491                                                                 G_VARIANT_TYPE(
492                                                                 "a{sv}"));
493
494                                         struct gatt_desc_info *desc_info = l3->data;
495                                         if (desc_info == NULL) {
496                                                 BT_ERR("desc_info is NULL");
497                                                 continue;
498                                         }
499
500                                         /*Uuid*/
501                                         g_variant_builder_add(inner_builder,
502                                                 "{sv}", "UUID",
503                                                 g_variant_new_string(
504                                                         desc_info->desc_uuid));
505
506                                         /*Characteristic*/
507                                         g_variant_builder_add(inner_builder, "{sv}",
508                                                 "Characteristic",
509                                                 g_variant_new("o",
510                                                         char_info->char_path));
511
512                                         /*Value*/
513                                         builder1 = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
514
515                                         if (desc_info->desc_value != NULL) {
516                                                 for (i = 0; i < desc_info->value_length; i++) {
517                                                         g_variant_builder_add(builder1, "y",
518                                                                 desc_info->desc_value[i]);
519                                                 }
520                                                 desc_val = g_variant_new("ay", builder1);
521                                                 g_variant_builder_add(inner_builder, "{sv}",
522                                                                 "Value", desc_val);
523                                         }
524
525                                         /*Flags*/
526                                         builder2 = g_variant_builder_new(G_VARIANT_TYPE("as"));
527
528                                         for (i = 0; i < desc_info->flags_length; i++) {
529                                                 g_variant_builder_add(builder2, "s",
530                                                         desc_info->desc_flags[i]);
531                                         }
532
533                                         flags_val = g_variant_new("as", builder2);
534                                         g_variant_builder_add(inner_builder, "{sv}", "Flags",
535                                                                 flags_val);
536
537                                         g_variant_builder_add(desc_builder, "{sa{sv}}",
538                                                         GATT_DESC_INTERFACE,
539                                                         inner_builder);
540
541                                         g_variant_builder_add(builder, "{oa{sa{sv}}}",
542                                                         desc_info->desc_path,
543                                                         desc_builder);
544
545                                         /*unref descriptor builder pointers*/
546                                         g_variant_builder_unref(builder1);
547                                         g_variant_builder_unref(builder2);
548                                         g_variant_builder_unref(inner_builder);
549                                         g_variant_builder_unref(desc_builder);
550                                 }
551
552                                 if (unicast)
553                                         g_free(unicast);
554                                 /*unref char builder pointers*/
555                                 g_variant_builder_unref(builder1);
556                                 g_variant_builder_unref(builder2);
557                                 g_variant_builder_unref(builder3);
558                                 g_variant_builder_unref(inner_builder);
559                                 g_variant_builder_unref(char_builder);
560                         }
561
562                         /*unref service builder pointers*/
563                         g_variant_builder_unref(inner_builder);
564                         g_variant_builder_unref(svc_builder);
565                 }
566
567                 /* Return builder as method reply */
568                 BT_DBG("Sending gatt service builder values to Bluez");
569                 g_dbus_method_invocation_return_value(invocation,
570                                                 g_variant_new(
571                                                 "(a{oa{sa{sv}}})",
572                                                 builder));
573         }
574 }
575
576 static struct gatt_service_info *__bt_gatt_find_gatt_service_from_char(const char *char_path)
577 {
578         GSList *l1, *l2;
579
580         for (l1 = gatt_services; l1 != NULL; l1 = l1->next) {
581                 struct gatt_service_info *serv_info = l1->data;
582
583                 for (l2 = serv_info->char_data; l2 != NULL; l2 = l2->next) {
584                         struct gatt_char_info *char_info = l2->data;
585
586                         if (g_strcmp0(char_info->char_path, char_path)
587                                                 == 0)
588                                 return serv_info;
589                 }
590         }
591         BT_ERR("Gatt service not found");
592         return NULL;
593 }
594
595 static struct gatt_service_info *__bt_gatt_find_gatt_service_from_desc(const char *desc_path)
596 {
597         GSList *l1, *l2, *l3;
598
599         for (l1 = gatt_services; l1 != NULL; l1 = l1->next) {
600                 struct gatt_service_info *serv_info = l1->data;
601
602                 for (l2 = serv_info->char_data; l2 != NULL; l2 = l2->next) {
603                         struct gatt_char_info *char_info = l2->data;
604
605                         for (l3 = char_info->desc_data; l3 != NULL; l3 = l3->next) {
606                                 struct gatt_desc_info *desc_info = l3->data;
607
608                                 if (g_strcmp0(desc_info->desc_path, desc_path)
609                                                         == 0)
610                                         return serv_info;
611                         }
612                 }
613         }
614         BT_ERR("Gatt service not found");
615         return NULL;
616 }
617
618 static void __bt_gatt_char_method_call(GDBusConnection *connection,
619                                         const gchar *sender,
620                                         const gchar *object_path,
621                                         const gchar *interface_name,
622                                         const gchar *method_name,
623                                         GVariant *parameters,
624                                         GDBusMethodInvocation *invocation,
625                                         gpointer user_data)
626 {
627
628         if (g_strcmp0(method_name, "ReadValue") == 0) {
629                 gchar *addr = NULL;
630                 guint8 req_id = 1;
631                 guint16 offset = 0;
632                 bt_gatt_read_req_t read_req = {0, };
633                 bt_user_info_t *user_info = NULL;
634                 struct gatt_req_info *req_info = NULL;
635                 struct gatt_service_info *svc_info = NULL;
636
637                 BT_DBG("ReadValue");
638
639                 g_variant_get(parameters, "(&syq)", &addr, &req_id, &offset);
640
641                 BT_DBG("Application path = %s", object_path);
642
643                 BT_DBG("Remote Device address number = %s", addr);
644                 BT_DBG("Request id = %d, Offset = %d", req_id, offset);
645
646                 BT_DBG("Sender = %s", sender);
647
648                 read_req.att_handle = g_strdup(object_path);
649                 read_req.address = g_strdup(addr);
650                 read_req.req_id = req_id;
651                 read_req.offset = offset;
652                 svc_info = __bt_gatt_find_gatt_service_from_char(object_path);
653                 if (svc_info != NULL) {
654                         read_req.service_handle = g_strdup(svc_info->serv_path);
655                         user_info = _bt_get_user_data(BT_COMMON);
656 #ifdef HPS_FEATURE
657                         GVariant *param = NULL;
658 #endif
659
660                         /* Store requets information */
661                         req_info = g_new0(struct gatt_req_info, 1);
662                         req_info->attr_path = g_strdup(object_path);
663                         req_info->svc_path = g_strdup(read_req.service_handle);
664                         req_info->request_id = req_id;
665                         req_info->offset = offset;
666                         req_info->context = invocation;
667                         gatt_requests = g_slist_append(gatt_requests, req_info);
668
669                         if (user_info != NULL) {
670                                 _bt_common_event_cb(
671                                         BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED,
672                                         BLUETOOTH_ERROR_NONE, &read_req,
673                                         user_info->cb, user_info->user_data);
674                         }
675 #ifdef HPS_FEATURE
676                         param = g_variant_new("(sssyq)",
677                                         read_req.att_handle,
678                                         read_req.service_handle,
679                                         read_req.address,
680                                         read_req.req_id,
681                                         read_req.offset);
682                         __bt_send_event_to_hps(BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED, param);
683 #endif
684                 }
685
686                 if (read_req.att_handle)
687                         g_free(read_req.att_handle);
688                 if (read_req.address)
689                         g_free(read_req.address);
690                 if (read_req.service_handle)
691                         g_free(read_req.service_handle);
692                 return;
693         } else if (g_strcmp0(method_name, "WriteValue") == 0) {
694                 GVariant *var = NULL;
695                 gchar *addr = NULL;
696                 guint8 req_id = 0;
697                 guint16 offset = 0;
698                 bt_gatt_value_change_t value_change = {0, };
699                 bt_user_info_t *user_info = NULL;
700                 int len = 0;
701                 struct gatt_service_info *svc_info = NULL;
702                 struct gatt_req_info *req_info = NULL;
703 #ifdef HPS_FEATURE
704                 GVariant *param = NULL;
705 #endif
706
707                 BT_DBG("WriteValue");
708                 BT_DBG("Application path = %s", object_path);
709                 BT_DBG("Sender = %s", sender);
710
711                 g_variant_get(parameters, "(&syq@ay)", &addr, &req_id, &offset, &var);
712
713                 value_change.att_handle = g_strdup(object_path);
714                 value_change.address = g_strdup(addr);
715                 svc_info = __bt_gatt_find_gatt_service_from_char(object_path);
716                 if (svc_info == NULL) {
717                         g_variant_unref(var);
718                         g_dbus_method_invocation_return_value(invocation, NULL);
719                         return;
720                 }
721
722                 value_change.service_handle = g_strdup(svc_info->serv_path);
723                 value_change.offset = offset;
724                 value_change.req_id = req_id;
725
726                 len = g_variant_get_size(var);
727                 if (len > 0) {
728                         char *data;
729
730                         value_change.att_value = (guint8 *)malloc(len);
731                         if (!value_change.att_value) {
732                                 BT_ERR("att_value is NULL");
733                                 g_variant_unref(var);
734                                 g_dbus_method_invocation_return_value(invocation, NULL);
735                                 return;
736                         }
737
738                         data = (char *)g_variant_get_data(var);
739                         memcpy(value_change.att_value, data, len);
740                 }
741
742                 value_change.val_len = len;
743
744                 /* Store requets information */
745                 req_info = g_new0(struct gatt_req_info, 1);
746                 req_info->attr_path = g_strdup(object_path);
747                 req_info->svc_path = g_strdup(value_change.service_handle);
748                 req_info->request_id = req_id;
749                 req_info->offset = offset;
750                 req_info->context = invocation;
751                 gatt_requests = g_slist_append(gatt_requests, req_info);
752
753                 user_info = _bt_get_user_data(BT_COMMON);
754                 if (user_info != NULL) {
755                         _bt_common_event_cb(
756                                 BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED,
757                                 BLUETOOTH_ERROR_NONE, &value_change,
758                                 user_info->cb, user_info->user_data);
759                 }
760 #ifdef HPS_FEATURE
761                 if (len > 0) {
762                         gchar *svc_path;
763                         svc_path = g_strdup(svc_info->serv_path);
764                         param = g_variant_new("(sssyq@ay)",
765                                         object_path,
766                                         svc_path,
767                                         addr,
768                                         req_id,
769                                         offset,
770                                         var);
771                         __bt_send_event_to_hps(BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED, param);
772                         if (svc_path)
773                                 g_free(svc_path);
774                 }
775 #endif
776                 g_variant_unref(var);
777                 return;
778         } else if (g_strcmp0(method_name, "StartNotify") == 0) {
779                 bt_user_info_t *user_info = NULL;
780                 bt_gatt_char_notify_change_t notify_change = {0, };
781                 BT_DBG("StartNotify");
782                 user_info = _bt_get_user_data(BT_COMMON);
783                 if (user_info != NULL) {
784                         struct gatt_service_info *svc_info = NULL;
785                         svc_info = __bt_gatt_find_gatt_service_from_char(object_path);
786                         if (svc_info) {
787                                 notify_change.service_handle = g_strdup(svc_info->serv_path);
788                                 notify_change.att_handle = g_strdup(object_path);
789                                 notify_change.att_notify = TRUE;
790                                 _bt_common_event_cb(
791                                         BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_STATE_CHANGED,
792                                         BLUETOOTH_ERROR_NONE, &notify_change,
793                                         user_info->cb, user_info->user_data);
794                         }
795                 }
796         } else if (g_strcmp0(method_name, "StopNotify") == 0) {
797                 bt_user_info_t *user_info = NULL;
798                 bt_gatt_char_notify_change_t notify_change = {0, };
799                 BT_DBG("StopNotify");
800                 user_info = _bt_get_user_data(BT_COMMON);
801                 if (user_info != NULL) {
802                         struct gatt_service_info *svc_info = NULL;
803                         svc_info = __bt_gatt_find_gatt_service_from_char(object_path);
804                         if (svc_info) {
805                                 notify_change.service_handle = g_strdup(svc_info->serv_path);
806                                 notify_change.att_handle = g_strdup(object_path);
807                                 notify_change.att_notify = FALSE;
808                                 _bt_common_event_cb(
809                                         BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_STATE_CHANGED,
810                                         BLUETOOTH_ERROR_NONE, &notify_change,
811                                         user_info->cb, user_info->user_data);
812                         }
813                 }
814         } else if (g_strcmp0(method_name, "IndicateConfirm") == 0) {
815                 gchar *addr = NULL;
816                 bt_gatt_indicate_confirm_t confirm = {0, };
817                 bt_user_info_t *user_info = NULL;
818                 gboolean complete = 0;
819                 struct gatt_service_info *svc_info = NULL;
820
821                 BT_DBG("IndicateConfirm");
822                 BT_DBG("Application path = %s", object_path);
823                 BT_DBG("Sender = %s", sender);
824
825                 g_variant_get(parameters, "(&sb)", &addr, &complete);
826
827                 BT_DBG("Remote Device address number = %s", addr);
828                 confirm.att_handle = g_strdup(object_path);
829                 confirm.address = g_strdup(addr);
830                 confirm.complete = complete;
831
832                 svc_info = __bt_gatt_find_gatt_service_from_char(object_path);
833                 if (svc_info != NULL) {
834                         confirm.service_handle = g_strdup(svc_info->serv_path);
835                         user_info = _bt_get_user_data(BT_COMMON);
836
837                         if (user_info != NULL) {
838                                 _bt_common_event_cb(
839                                         BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_COMPLETED,
840                                         BLUETOOTH_ERROR_NONE, &confirm,
841                                         user_info->cb, user_info->user_data);
842                         }
843                 }
844         }
845         g_dbus_method_invocation_return_value(invocation, NULL);
846 }
847
848 static void __bt_gatt_desc_method_call(GDBusConnection *connection,
849                                         const gchar *sender,
850                                         const gchar *object_path,
851                                         const gchar *interface_name,
852                                         const gchar *method_name,
853                                         GVariant *parameters,
854                                         GDBusMethodInvocation *invocation,
855                                         gpointer user_data)
856 {
857         if (g_strcmp0(method_name, "ReadValue") == 0) {
858                 gchar *addr = NULL;
859                 guint8 req_id = 1;
860                 guint16 offset = 0;
861                 bt_gatt_read_req_t read_req = {0, };
862                 bt_user_info_t *user_info = NULL;
863                 struct gatt_req_info *req_info = NULL;
864                 struct gatt_service_info *svc_info = NULL;
865                 BT_DBG("ReadValue");
866
867                 g_variant_get(parameters, "(&syq)", &addr, &req_id, &offset);
868
869                 BT_DBG("Application path = %s", object_path);
870
871                 BT_DBG("Remote Device address number = %s", addr);
872                 BT_DBG("Request id = %d, Offset = %d", req_id, offset);
873
874                 BT_DBG("Sender = %s", sender);
875
876                 read_req.att_handle = g_strdup(object_path);
877                 read_req.address = g_strdup(addr);
878                 read_req.req_id = req_id;
879                 read_req.offset = offset;
880                 svc_info = __bt_gatt_find_gatt_service_from_desc(object_path);
881                 if (svc_info != NULL) {
882                         read_req.service_handle = g_strdup(svc_info->serv_path);
883                         user_info = _bt_get_user_data(BT_COMMON);
884
885                         /* Store requets information */
886                         req_info = g_new0(struct gatt_req_info, 1);
887                         req_info->attr_path = g_strdup(object_path);
888                         req_info->svc_path = g_strdup(read_req.service_handle);
889                         req_info->request_id = req_id;
890                         req_info->offset = offset;
891                         req_info->context = invocation;
892                         gatt_requests = g_slist_append(gatt_requests, req_info);
893
894                         if (user_info != NULL) {
895                                 _bt_common_event_cb(
896                                         BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED,
897                                         BLUETOOTH_ERROR_NONE, &read_req,
898                                         user_info->cb, user_info->user_data);
899                         }
900                 }
901
902                 if (read_req.att_handle)
903                         g_free(read_req.att_handle);
904                 if (read_req.address)
905                         g_free(read_req.address);
906                 if (read_req.service_handle)
907                         g_free(read_req.service_handle);
908
909                 return;
910         } else if (g_strcmp0(method_name, "WriteValue") == 0) {
911                 GVariant *var = NULL;
912                 gchar *addr = NULL;
913                 guint8 req_id = 0;
914                 guint16 offset = 0;
915                 bt_gatt_value_change_t value_change = {0, };
916                 bt_user_info_t *user_info = NULL;
917                 int len = 0;
918                 struct gatt_service_info *svc_info = NULL;
919                 struct gatt_req_info *req_info = NULL;
920
921                 BT_DBG("WriteValue");
922                 BT_DBG("Application path = %s", object_path);
923                 BT_DBG("Sender = %s", sender);
924
925                 g_variant_get(parameters, "(&syq@ay)", &addr, &req_id, &offset, &var);
926
927                 value_change.att_handle = g_strdup(object_path);
928                 value_change.address = g_strdup(addr);
929                 svc_info = __bt_gatt_find_gatt_service_from_desc(object_path);
930                 if (svc_info == NULL) {
931                         g_variant_unref(var);
932                         g_dbus_method_invocation_return_value(invocation, NULL);
933                         return;
934                 }
935
936                 value_change.service_handle = g_strdup(svc_info->serv_path);
937                 value_change.offset = offset;
938                 value_change.req_id = req_id;
939
940                 len = g_variant_get_size(var);
941                 if (len > 0) {
942                         char *data;
943
944                         value_change.att_value = (guint8 *)malloc(len);
945                         if (!value_change.att_value) {
946                                 BT_ERR("att_value is NULL");
947                                 g_variant_unref(var);
948                                 g_dbus_method_invocation_return_value(invocation, NULL);
949                                 return;
950                         }
951                         data = (char *)g_variant_get_data(var);
952                         memcpy(value_change.att_value, data, len);
953                 }
954                 g_variant_unref(var);
955
956                 value_change.val_len = len;
957
958                 /* Store requets information */
959                 req_info = g_new0(struct gatt_req_info, 1);
960                 req_info->attr_path = g_strdup(object_path);
961                 req_info->svc_path = g_strdup(value_change.service_handle);
962                 req_info->request_id = req_id;
963                 req_info->offset = offset;
964                 req_info->context = invocation;
965                 gatt_requests = g_slist_append(gatt_requests, req_info);
966
967                 user_info = _bt_get_user_data(BT_COMMON);
968                 if (user_info != NULL) {
969                         _bt_common_event_cb(
970                                 BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED,
971                                 BLUETOOTH_ERROR_NONE, &value_change,
972                                 user_info->cb, user_info->user_data);
973                 }
974                 return;
975         }
976         g_dbus_method_invocation_return_value(invocation, NULL);
977 }
978
979 gboolean __bt_gatt_emit_interface_removed(gchar *object_path, gchar *interface)
980 {
981         gboolean ret;
982         GError *error = NULL;
983         GVariantBuilder *array_builder;
984
985         array_builder = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
986         g_variant_builder_init(array_builder, G_VARIANT_TYPE("as"));
987         g_variant_builder_add(array_builder, "s", interface);
988
989         ret = g_dbus_connection_emit_signal(g_conn, NULL, "/",
990                                         "org.freedesktop.Dbus.Objectmanager",
991                                         "InterfacesRemoved",
992                                         g_variant_new("(oas)",
993                                         object_path, array_builder),
994                                         &error);
995
996         if (!ret) {
997                 if (error != NULL) {
998                         /* dbus gives error cause */
999                         BT_ERR("d-bus api failure: errcode[%x], message[%s]",
1000                                 error->code, error->message);
1001                         g_clear_error(&error);
1002                 }
1003         }
1004         g_variant_builder_unref(array_builder);
1005
1006         return ret;
1007 }
1008
1009 static const GDBusInterfaceVTable desc_interface_vtable = {
1010         __bt_gatt_desc_method_call,
1011         NULL,
1012         NULL,
1013 };
1014
1015 static const GDBusInterfaceVTable char_interface_vtable = {
1016         __bt_gatt_char_method_call,
1017         NULL,
1018         NULL,
1019 };
1020
1021 static const GDBusInterfaceVTable serv_interface_vtable = {
1022         NULL,
1023         NULL,
1024         NULL,
1025 };
1026
1027 static const GDBusInterfaceVTable manager_interface_vtable = {
1028         __bt_gatt_manager_method_call,
1029         NULL,
1030         NULL
1031 };
1032
1033 static GDBusNodeInfo *__bt_gatt_create_method_node_info(
1034                                 const gchar *introspection_data)
1035 {
1036         GError *err = NULL;
1037
1038         if (introspection_data == NULL)
1039                 return NULL;
1040
1041         if (obj_info == NULL) {
1042                 BT_DBG("Create new node info");
1043                 obj_info = g_dbus_node_info_new_for_xml(introspection_data, &err);
1044
1045                 if (err) {
1046                         BT_ERR("Unable to create node: %s", err->message);
1047                         g_clear_error(&err);
1048                         return NULL;
1049                 }
1050         }
1051
1052         return obj_info;
1053 }
1054
1055 static struct gatt_service_info *__bt_gatt_find_gatt_service_info(
1056                         const char *service_path)
1057 {
1058         GSList *l;
1059
1060         for (l = gatt_services; l != NULL; l = l->next) {
1061                 struct gatt_service_info *info = l->data;
1062
1063                 if (g_strcmp0(info->serv_path, service_path) == 0)
1064                         return info;
1065         }
1066         BT_ERR("Gatt service not found");
1067         return NULL;
1068 }
1069
1070 static struct gatt_char_info *__bt_gatt_find_gatt_char_info(
1071                         const char *service_path, const char *char_path)
1072 {
1073         GSList *l1, *l2;
1074
1075         for (l1 = gatt_services; l1 != NULL; l1 = l1->next) {
1076                 struct gatt_service_info *serv_info = l1->data;
1077
1078                 if (g_strcmp0(serv_info->serv_path, service_path) == 0) {
1079
1080                         for (l2 = serv_info->char_data; l2 != NULL; l2 = l2->next) {
1081                                 struct gatt_char_info *char_info = l2->data;
1082
1083                                 if (g_strcmp0(char_info->char_path, char_path)
1084                                                         == 0)
1085                                         return char_info;
1086                         }
1087                         BT_ERR("Gatt characteristic not found");
1088                         return NULL;
1089                 }
1090         }
1091         BT_ERR("Gatt service not found");
1092         return NULL;
1093 }
1094
1095 static struct gatt_desc_info *__bt_gatt_find_gatt_desc_info(
1096                         const char *serv_path, const char *char_path,
1097                         const char *desc_path)
1098 {
1099         GSList *l1, *l2, *l3;
1100
1101         for (l1 = gatt_services; l1 != NULL; l1 = l1->next) {
1102                 struct gatt_service_info *serv_info = l1->data;
1103
1104                 if (g_strcmp0(serv_info->serv_path, serv_path) == 0) {
1105                         for (l2 = serv_info->char_data; l2 != NULL; l2 = l2->next) {
1106                                 struct gatt_char_info *char_info = l2->data;
1107
1108                                 if (g_strcmp0(char_info->char_path, char_path)
1109                                                         == 0) {
1110                                         for (l3 = char_info->desc_data; l3 != NULL; l3 = l3->next) {
1111                                                 struct gatt_desc_info *desc_info = l3->data;
1112                                                 if (g_strcmp0(desc_info->desc_path,
1113                                                         desc_path) == 0) {
1114                                                         return desc_info;
1115                                                 }
1116                                         }
1117                                 }
1118                         }
1119                 }
1120         }
1121         BT_ERR("Gatt descriptor not found");
1122         return NULL;
1123 }
1124
1125 static struct gatt_req_info *__bt_gatt_find_request_info(guint request_id)
1126 {
1127         GSList *l;
1128
1129         for (l = gatt_requests; l != NULL; l = l->next) {
1130                 struct gatt_req_info *req_info = l->data;
1131
1132                 if (req_info && req_info->request_id == request_id) {
1133                         return req_info;
1134                 }
1135         }
1136         BT_ERR("Gatt Request not found");
1137         return NULL;
1138 }
1139
1140 static GDBusProxy *__bt_gatt_gdbus_init_manager_proxy(const gchar *service,
1141                                 const gchar *path, const gchar *interface)
1142 {
1143         GDBusProxy *proxy;
1144         GError *err = NULL;
1145
1146         if (g_conn == NULL)
1147                 g_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM,
1148                                                         NULL, &err);
1149
1150         if (!g_conn) {
1151                 if (err) {
1152                         BT_ERR("Unable to connect to gdbus: %s", err->message);
1153                         g_clear_error(&err);
1154                 }
1155                 return NULL;
1156         }
1157
1158         proxy =  g_dbus_proxy_new_sync(g_conn,
1159                         G_DBUS_PROXY_FLAGS_NONE, NULL,
1160                         service, path,
1161                         interface, NULL, &err);
1162
1163         if (!proxy) {
1164                 if (err) {
1165                         BT_ERR("Unable to create proxy: %s", err->message);
1166                         g_clear_error(&err);
1167                 }
1168                 return NULL;
1169         }
1170         manager_gproxy = proxy;
1171
1172         return proxy;
1173 }
1174
1175 static GDBusProxy *__bt_gatt_gdbus_get_manager_proxy(const gchar *service,
1176                                 const gchar *path, const gchar *interface)
1177 {
1178         return (manager_gproxy) ? manager_gproxy :
1179                         __bt_gatt_gdbus_init_manager_proxy(service,
1180                                 path, interface);
1181 }
1182
1183 int bluetooth_gatt_convert_prop2string(
1184                         bt_gatt_characteristic_property_t properties,
1185                         char *char_properties[])
1186 {
1187         int flag_count = 0;
1188
1189         if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_BROADCAST) {
1190                 char_properties[flag_count] = g_strdup("broadcast");
1191                 flag_count++;
1192         }
1193         if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ) {
1194                 char_properties[flag_count] = g_strdup("read");
1195                 flag_count++;
1196         }
1197         if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE_NO_RESPONSE) {
1198                 char_properties[flag_count] = g_strdup("write-without-response");
1199                 flag_count++;
1200         }
1201         if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE) {
1202                 char_properties[flag_count] = g_strdup("write");
1203                 flag_count++;
1204         }
1205         if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_NOTIFY) {
1206                 char_properties[flag_count] = g_strdup("notify");
1207                 flag_count++;
1208         }
1209         if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_INDICATE) {
1210                 char_properties[flag_count] = g_strdup("indicate");
1211                 flag_count++;
1212         }
1213         if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_SIGNED_WRITE) {
1214                 char_properties[flag_count] = g_strdup("authenticated-signed-writes");
1215                 flag_count++;
1216         }
1217         if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_RELIABLE_WRITE) {
1218                 char_properties[flag_count] = g_strdup("reliable-write");
1219                 flag_count++;
1220         }
1221         if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITABLE_AUXILIARIES) {
1222                 char_properties[flag_count] = g_strdup("writable-auxiliaries");
1223                 flag_count++;
1224         }
1225         if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_READ) {
1226                 char_properties[flag_count] = g_strdup("encrypt-read");
1227                 flag_count++;
1228         }
1229         if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_WRITE) {
1230                 char_properties[flag_count] = g_strdup("encrypt-write");
1231                 flag_count++;
1232         }
1233         if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_READ) {
1234                 char_properties[flag_count] = g_strdup("encrypt-authenticated-read");
1235                 flag_count++;
1236         }
1237         if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_WRITE) {
1238                 char_properties[flag_count] = g_strdup("encrypt-authenticated-write");
1239                 flag_count++;
1240         }
1241
1242         if (flag_count == 0) {
1243                 char_properties[flag_count] = g_strdup("read");
1244                 flag_count++;
1245         }
1246
1247         return flag_count;
1248 }
1249
1250 int bluetooth_gatt_convert_perm2string(
1251                         bt_gatt_permission_t properties,
1252                         char *char_properties[])
1253 {
1254         int flag_count = 0;
1255
1256         if (properties & BLUETOOTH_GATT_PERMISSION_READ) {
1257                 char_properties[flag_count] = g_strdup("read");
1258                 flag_count++;
1259         }
1260         if (properties & BLUETOOTH_GATT_PERMISSION_WRITE) {
1261                 char_properties[flag_count] = g_strdup("write");
1262                 flag_count++;
1263         }
1264         if (properties & BLUETOOTH_GATT_PERMISSION_ENCRYPT_READ) {
1265                 char_properties[flag_count] = g_strdup("encrypt-read");
1266                 flag_count++;
1267         }
1268         if (properties & BLUETOOTH_GATT_PERMISSION_ENCRYPT_WRITE) {
1269                 char_properties[flag_count] = g_strdup("encrypt-write");
1270                 flag_count++;
1271         }
1272         if (properties & BLUETOOTH_GATT_PERMISSION_ENCRYPT_AUTHENTICATED_READ) {
1273                 char_properties[flag_count] = g_strdup("encrypt-authenticated-read");
1274                 flag_count++;
1275         }
1276         if (properties & BLUETOOTH_GATT_PERMISSION_ENCRYPT_AUTHENTICATED_WRITE) {
1277                 char_properties[flag_count] = g_strdup("encrypt-authenticated-write");
1278                 flag_count++;
1279         }
1280
1281         if (flag_count == 0) {
1282                 char_properties[flag_count] = g_strdup("read");
1283                 flag_count++;
1284         }
1285
1286         return flag_count;
1287 }
1288
1289 static void __bt_gatt_set_service_state(const char *service_path,
1290                         gboolean state)
1291 {
1292         struct gatt_service_info *svc_info = NULL;
1293         svc_info = __bt_gatt_find_gatt_service_info(service_path);
1294
1295         if (svc_info != NULL) {
1296                 BT_DBG("Updating the gatt service register state %d", state);
1297                 svc_info->is_svc_registered = state;
1298                 return;
1299         }
1300
1301         BT_DBG("gatt service not found");
1302 }
1303
1304 static gboolean __bt_gatt_get_service_state(const char *service_path)
1305 {
1306         struct gatt_service_info *svc_info = NULL;
1307
1308         svc_info = __bt_gatt_find_gatt_service_info(service_path);
1309
1310         if (svc_info != NULL) {
1311                 BT_DBG("Return the state of the gatt service %d",
1312                         svc_info->is_svc_registered);
1313                 return svc_info->is_svc_registered;
1314         }
1315
1316         BT_DBG("gatt service info is NULL");
1317         return FALSE;
1318 }
1319
1320 void get_service_cb(GObject *object, GAsyncResult *res, gpointer user_data)
1321 {
1322         GError *error = NULL;
1323         GVariant *result;
1324         GVariantIter *iter = NULL;
1325         const gchar *key = NULL;
1326         GVariant *value = NULL;
1327         const gchar *service = NULL;
1328         const gchar *characteristic = NULL;
1329         const gchar *descriptor = NULL;
1330         int n_char = 1;
1331
1332         BT_DBG(" ");
1333         result = g_dbus_proxy_call_finish(manager_gproxy, res, &error);
1334
1335         if (result == NULL) {
1336                 /* dBUS-RPC is failed */
1337                 BT_ERR("Dbus-RPC is failed\n");
1338
1339                 if (error != NULL) {
1340                 /* dBUS gives error cause */
1341                         BT_ERR("D-Bus API failure: errCode[%x], message[%s]\n",
1342                                                 error->code, error->message);
1343                         g_clear_error(&error);
1344                 }
1345         } else {
1346                 char *char_cmp = NULL;
1347                 g_variant_get(result, "(a{sv})", &iter);
1348                 char_cmp = g_strdup_printf("Characteristic%d", n_char);
1349
1350                 while (g_variant_iter_loop(iter, "{sv}", &key, &value)) {
1351                         if (g_strcmp0(key, "Service") == 0) {
1352                                 service = g_variant_get_string(value, NULL);
1353                                 BT_DBG("Service %s", service);
1354                         } else if (g_strcmp0(key, char_cmp) == 0) {
1355                                 characteristic = g_variant_get_string(value, NULL);
1356                                 g_free(char_cmp);
1357                                 char_cmp = g_strdup_printf("Characteristic%d", ++n_char);
1358                                 BT_DBG("%s", characteristic);
1359                         } else if (g_strcmp0(key, "Descriptor") == 0) {
1360                                 descriptor = g_variant_get_string(value, NULL);
1361                                 BT_DBG("Descriptor %s", descriptor);
1362                         }
1363                 }
1364                 g_variant_iter_free(iter);
1365
1366                 /* TODO: Store the service informationa and
1367                  * Send respponse to CAPI layer. */
1368
1369                 g_variant_unref(result);
1370                 if (char_cmp)
1371                         g_free(char_cmp);
1372         }
1373 }
1374 void register_application_cb(GObject *object, GAsyncResult *res, gpointer user_data)
1375 {
1376         BT_INFO("RegisterApplication is completed");
1377
1378         GError *error = NULL;
1379         GVariant *result;
1380
1381         register_pending_cnt = 0;
1382
1383         result = g_dbus_proxy_call_finish(manager_gproxy, res, &error);
1384
1385         if (result == NULL) {
1386                 /* dBUS-RPC is failed */
1387                 BT_ERR("Dbus-RPC is failed\n");
1388
1389                 if (error != NULL) {
1390                 /* dBUS gives error cause */
1391                         BT_ERR("D-Bus API failure: errCode[%x], message[%s]\n",
1392                                                 error->code, error->message);
1393                         g_clear_error(&error);
1394                 }
1395         }
1396 }
1397
1398 void unregister_application_cb(GObject *object, GAsyncResult *res,
1399                 gpointer user_data)
1400 {
1401         BT_INFO("UnregisterApplication is completed");
1402
1403         GError *error = NULL;
1404         GVariant *result;
1405
1406         result = g_dbus_proxy_call_finish(manager_gproxy, res, &error);
1407
1408         if (result == NULL) {
1409                 /* dBUS-RPC is failed */
1410                 BT_ERR("Dbus-RPC is failed\n");
1411
1412                 if (error != NULL) {
1413                         /* dBUS gives error cause */
1414                         BT_ERR("D-Bus API failure: errCode[%x], message[%s]\n",
1415                                         error->code, error->message);
1416                         g_clear_error(&error);
1417                 }
1418         }
1419 }
1420
1421 static int __bt_gatt_unregister_service(const char *service_path)
1422 {
1423         if (!__bt_gatt_get_service_state(service_path)) {
1424                 BT_DBG("service not registered \n");
1425                 return BLUETOOTH_ERROR_NOT_FOUND;
1426         }
1427
1428         return BLUETOOTH_ERROR_NONE;
1429 }
1430
1431 BT_EXPORT_API int bluetooth_gatt_unregister_application(void)
1432 {
1433         GDBusProxy *proxy = NULL;
1434
1435         if (is_server_started) {
1436                 proxy = __bt_gatt_gdbus_get_manager_proxy("org.bluez",
1437                                 "/org/bluez/hci0", GATT_MNGR_INTERFACE);
1438
1439                 if (proxy == NULL || app_path == NULL)
1440                         return BLUETOOTH_ERROR_INTERNAL;
1441
1442                 BT_INFO("UnregisterApplication");
1443
1444                 /* Async Call to Unregister Service */
1445                 g_dbus_proxy_call(proxy,
1446                                 "UnregisterApplication",
1447                                 g_variant_new("(o)",
1448                                         app_path),
1449                                 G_DBUS_CALL_FLAGS_NONE, -1,
1450                                 NULL,
1451                                 (GAsyncReadyCallback) unregister_application_cb,
1452                                 NULL);
1453
1454                 is_server_started = false;
1455                 return BLUETOOTH_ERROR_NONE;
1456         }
1457
1458         BT_INFO("GATT server not started");
1459         return BLUETOOTH_ERROR_NONE;
1460 }
1461
1462 static GDBusConnection *__bt_gatt_get_gdbus_connection(void)
1463 {
1464         GDBusConnection *local_system_gconn = NULL;
1465         char *address;
1466         GError *err = NULL;
1467
1468         if (g_conn == NULL) {
1469                 address = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
1470                 if (address == NULL) {
1471                         if (err) {
1472                                 BT_ERR("Failed to get bus address: %s", err->message);
1473                                 g_clear_error(&err);
1474                         }
1475                         return NULL;
1476                 }
1477
1478                 g_conn = g_dbus_connection_new_for_address_sync(address,
1479                                         G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
1480                                         G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
1481                                         NULL, /* GDBusAuthObserver */
1482                                         NULL,
1483                                         &err);
1484                 if (!g_conn) {
1485                         if (err) {
1486                                 BT_ERR("Unable to connect to dbus: %s", err->message);
1487                                 g_clear_error(&err);
1488                         }
1489                         return NULL;
1490                 }
1491         } else if (g_dbus_connection_is_closed(g_conn)) {
1492                 address = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
1493                 if (address == NULL) {
1494                         if (err) {
1495                                 BT_ERR("Failed to get bus address: %s", err->message);
1496                                 g_clear_error(&err);
1497                         }
1498                         return NULL;
1499                 }
1500
1501                 local_system_gconn = g_dbus_connection_new_for_address_sync(address,
1502                                         G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
1503                                         G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
1504                                         NULL, /* GDBusAuthObserver */
1505                                         NULL,
1506                                         &err);
1507
1508                 if (!local_system_gconn) {
1509                         BT_ERR("Unable to connect to dbus: %s", err->message);
1510                         g_clear_error(&err);
1511                 }
1512
1513                 g_conn = local_system_gconn;
1514         }
1515
1516         return g_conn;
1517 }
1518
1519 BT_EXPORT_API int bluetooth_gatt_init(void)
1520 {
1521         GDBusConnection *conn;
1522         GError *error = NULL;
1523         GDBusNodeInfo *node_info = NULL;
1524
1525         if (app_path != NULL) {
1526                 BT_ERR("app path already exists! initialized");
1527                 return BLUETOOTH_ERROR_ALREADY_INITIALIZED;
1528         }
1529
1530         if (owner_id == 0) {
1531                 owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
1532                                         BT_GATT_SERVICE_NAME,
1533                                         G_BUS_NAME_OWNER_FLAGS_NONE,
1534                                         NULL, NULL, NULL, NULL, NULL);
1535         }
1536
1537         BT_DBG("owner_id is [%d]", owner_id);
1538         app_path = g_strdup_printf("/com/%d", getpid());
1539
1540         serv_id = 1;
1541
1542         conn = __bt_gatt_get_gdbus_connection();
1543         if (!conn) {
1544                 BT_ERR("Unable to get connection");
1545                 goto failed;
1546         }
1547
1548         /* Register ObjectManager interface */
1549         node_info = __bt_gatt_create_method_node_info(
1550                                         manager_introspection_xml);
1551
1552         if (node_info == NULL) {
1553                 BT_ERR("failed to get node info");
1554                 goto failed;
1555         }
1556
1557         if (manager_id == 0) {
1558                 BT_INFO("manager_id does not exists");
1559
1560                 manager_id = g_dbus_connection_register_object(g_conn, app_path,
1561                                                         node_info->interfaces[0],
1562                                                         &manager_interface_vtable,
1563                                                         NULL, NULL, &error);
1564         }
1565
1566         if (manager_id == 0) {
1567                 BT_ERR("failed to register: %s", error->message);
1568                 g_error_free(error);
1569                 goto failed;
1570         }
1571
1572         return BLUETOOTH_ERROR_NONE;
1573
1574 failed:
1575         if (obj_info)
1576                 g_dbus_node_info_unref(obj_info);
1577
1578         if (owner_id)
1579                 g_bus_unown_name(owner_id);
1580
1581         g_free(app_path);
1582
1583         obj_info = NULL;
1584         app_path = NULL;
1585         owner_id = 0;
1586
1587         __bt_gatt_close_gdbus_connection();
1588
1589         return BLUETOOTH_ERROR_INTERNAL;
1590 }
1591
1592 BT_EXPORT_API int bluetooth_gatt_deinit()
1593 {
1594         int ret = BLUETOOTH_ERROR_NONE;
1595         /* Unown gdbus bus */
1596         if (owner_id) {
1597                 /* remove/unregister all services */
1598                 BT_DBG("removing all registered gatt service\n");
1599                 bluetooth_gatt_delete_services();
1600
1601                 /* unregister the exported interface for object manager */
1602                 g_dbus_connection_unregister_object(g_conn,
1603                                         manager_id);
1604
1605                 manager_id = 0;
1606
1607                 ret = bluetooth_gatt_unregister_application();
1608                 if (ret != BLUETOOTH_ERROR_NONE)
1609                         BT_ERR("Fail to unregister application\n");
1610
1611                 g_bus_unown_name(owner_id);
1612                 owner_id = 0;
1613
1614                 g_free(app_path);
1615                 app_path = NULL;
1616
1617                 BT_DBG("Gatt service deinitialized \n");
1618
1619                 g_slist_free(gatt_services);
1620                 gatt_services = NULL;
1621
1622                 g_object_unref(manager_gproxy);
1623                 manager_gproxy = NULL;
1624
1625                 /* Temperary block under codes to avoid TC blocking issue.
1626                     But we should unref node info in later. */
1627 #if 0
1628                 g_dbus_node_info_unref(obj_info);
1629                 obj_info = NULL;
1630 #endif
1631                 __bt_gatt_close_gdbus_connection();
1632
1633                 return ret;
1634         }
1635
1636         __bt_gatt_close_gdbus_connection();
1637
1638         return BLUETOOTH_ERROR_NOT_FOUND;
1639 }
1640
1641 BT_EXPORT_API int bluetooth_gatt_add_service(const char *svc_uuid,
1642                         char **svc_path)
1643 {
1644         GError *error = NULL;
1645         guint object_id;
1646         GDBusNodeInfo *node_info;
1647         gchar *path = NULL;
1648         GVariantBuilder *builder = NULL;
1649         GVariantBuilder *builder1 = NULL;
1650         GVariantBuilder *inner_builder = NULL;
1651         gboolean svc_primary = TRUE;
1652         struct gatt_service_info *serv_info = NULL;
1653
1654         node_info = __bt_gatt_create_method_node_info(
1655                                         service_introspection_xml);
1656
1657         if (node_info == NULL)
1658                 return BLUETOOTH_ERROR_INTERNAL;
1659
1660         path = g_strdup_printf("%s"GATT_SERV_OBJECT_PATH"%d", app_path, serv_id++);
1661         BT_DBG("gatt service path is [%s]", path);
1662
1663         object_id = g_dbus_connection_register_object(g_conn, path,
1664                                         node_info->interfaces[0],
1665                                         &serv_interface_vtable,
1666                                         NULL, NULL, &error);
1667
1668         if (object_id == 0) {
1669                 BT_ERR("failed to register: %s", error->message);
1670                 g_error_free(error);
1671                 g_free(path);
1672
1673                 return BLUETOOTH_ERROR_INTERNAL;
1674         }
1675
1676         /* Add object_id/gatt service information; it's required at the time of
1677          *  service unregister and Getmanagedobjects
1678          */
1679         serv_info = g_new0(struct gatt_service_info, 1);
1680
1681         serv_info->serv_path = g_strdup(path);
1682         serv_info->serv_id = object_id;
1683         serv_info->service_uuid = g_strdup(svc_uuid);
1684         serv_info->is_svc_registered = FALSE;
1685         serv_info->is_svc_primary = svc_primary;
1686
1687         gatt_services = g_slist_append(gatt_services, serv_info);
1688
1689         /* emit interfacesadded signal here for service path */
1690         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
1691         inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1692
1693         g_variant_builder_add(inner_builder, "{sv}",
1694                 "UUID", g_variant_new_string(svc_uuid));
1695
1696         g_variant_builder_add(inner_builder, "{sv}",
1697                 "Primary", g_variant_new_boolean(svc_primary));
1698
1699         builder1 = g_variant_builder_new(G_VARIANT_TYPE("ao"));
1700
1701         g_variant_builder_add(inner_builder, "{sv}", "Characteristics",
1702                                 g_variant_new("ao", builder1));
1703
1704         g_variant_builder_add(builder, "{sa{sv}}",
1705                 GATT_SERV_INTERFACE, inner_builder);
1706
1707         g_dbus_connection_emit_signal(g_conn, NULL, "/",
1708                                 "org.freedesktop.Dbus.ObjectManager",
1709                                 "InterfacesAdded",
1710                                 g_variant_new("(oa{sa{sv}})",
1711                                 path, builder),
1712                                 &error);
1713
1714         new_service = TRUE;
1715
1716         *svc_path = g_strdup(path);
1717
1718         g_free(path);
1719         g_variant_builder_unref(inner_builder);
1720         g_variant_builder_unref(builder);
1721         g_variant_builder_unref(builder1);
1722
1723         return BLUETOOTH_ERROR_NONE;
1724 }
1725
1726 BT_EXPORT_API int bluetooth_gatt_add_new_characteristic(
1727                         const char *svc_path, const char *char_uuid,
1728                         bt_gatt_permission_t permissions,
1729                         bt_gatt_characteristic_property_t properties,
1730                         char **char_path)
1731 {
1732         static int char_id;
1733         GError *error = NULL;
1734         guint object_id;
1735         GDBusNodeInfo *node_info;
1736         gchar *path = NULL;
1737         GVariantBuilder *builder = NULL;
1738         GVariantBuilder *inner_builder = NULL;
1739         struct gatt_service_info *serv_info = NULL;
1740         struct gatt_char_info *char_info = NULL;
1741         GVariantBuilder *builder2 = NULL;
1742         GVariantBuilder *builder3 = NULL;
1743         GVariant *flags_val = NULL;
1744         int i = 0;
1745         char *char_flags[NUMBER_OF_FLAGS];
1746         int flag_count = 0;
1747
1748         if (new_service) {
1749                 char_id = 1;
1750                 new_service = FALSE;
1751         }
1752
1753         BT_DBG("gatt svc_path path is [%s]", svc_path);
1754         serv_info = __bt_gatt_find_gatt_service_info(svc_path);
1755         if (serv_info == NULL)
1756                 return BLUETOOTH_ERROR_INVALID_PARAM;
1757
1758         node_info = __bt_gatt_create_method_node_info(
1759                                         characteristics_introspection_xml);
1760
1761         if (node_info == NULL)
1762                 return BLUETOOTH_ERROR_INTERNAL;
1763
1764         path = g_strdup_printf("%s/characteristic%d", svc_path, char_id++);
1765         BT_DBG("gatt characteristic path is [%s]", path);
1766
1767         object_id = g_dbus_connection_register_object(g_conn, path,
1768                                         node_info->interfaces[0],
1769                                         &char_interface_vtable,
1770                                         NULL, NULL, &error);
1771
1772         if (object_id == 0) {
1773                 BT_ERR("failed to register: %s", error->message);
1774                 g_error_free(error);
1775                 g_free(path);
1776
1777                 return BLUETOOTH_ERROR_INTERNAL;
1778         }
1779
1780         if (permissions & BLUETOOTH_GATT_PERMISSION_ENCRYPT_READ)
1781                 properties |= BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_READ;
1782         if (permissions & BLUETOOTH_GATT_PERMISSION_ENCRYPT_AUTHENTICATED_READ)
1783                 properties |= BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_READ;
1784         if (permissions & BLUETOOTH_GATT_PERMISSION_ENCRYPT_WRITE)
1785                 properties |= BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_WRITE;
1786         if (permissions & BLUETOOTH_GATT_PERMISSION_ENCRYPT_AUTHENTICATED_WRITE)
1787                 properties |= BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_WRITE;
1788
1789         flag_count = bluetooth_gatt_convert_prop2string(properties, char_flags);
1790
1791         char_info = g_new0(struct gatt_char_info, 1);
1792
1793         char_info->char_path = g_strdup(path);
1794         char_info->char_id = object_id;
1795         char_info->char_uuid = g_strdup(char_uuid);
1796
1797         for (i = 0; i < flag_count; i++) {
1798                 char_info->char_flags[i] = char_flags[i];
1799                 }
1800
1801         char_info->flags_length = flag_count;
1802
1803         serv_info->char_data = g_slist_append(serv_info->char_data, char_info);
1804
1805         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
1806         inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1807
1808         g_variant_builder_add(inner_builder, "{sv}", "UUID",
1809                                 g_variant_new("s", char_uuid));
1810         g_variant_builder_add(inner_builder, "{sv}", "Service",
1811                                 g_variant_new("o", svc_path));
1812
1813         builder2 = g_variant_builder_new(G_VARIANT_TYPE("as"));
1814
1815         for (i = 0; i < flag_count; i++) {
1816                 g_variant_builder_add(builder2, "s", char_flags[i]);
1817         }
1818
1819         flags_val = g_variant_new("as", builder2);
1820         g_variant_builder_add(inner_builder, "{sv}", "Flags",
1821                                 flags_val);
1822
1823         builder3 = g_variant_builder_new(G_VARIANT_TYPE("ao"));
1824
1825         g_variant_builder_add(inner_builder, "{sv}", "Descriptors",
1826                                 g_variant_new("ao", builder3));
1827
1828         g_variant_builder_add(builder, "{sa{sv}}",
1829                                 GATT_CHAR_INTERFACE,
1830                                 inner_builder);
1831
1832         g_dbus_connection_emit_signal(g_conn, NULL, "/",
1833                                 "org.freedesktop.Dbus.ObjectManager",
1834                                 "InterfacesAdded",
1835                                 g_variant_new("(oa{sa{sv}})",
1836                                 path, builder),
1837                                 &error);
1838
1839         *char_path = g_strdup(path);
1840
1841         new_char = TRUE;
1842
1843         g_free(path);
1844
1845         g_variant_builder_unref(inner_builder);
1846         g_variant_builder_unref(builder);
1847         g_variant_builder_unref(builder2);
1848         g_variant_builder_unref(builder3);
1849
1850         return BLUETOOTH_ERROR_NONE;
1851 }
1852
1853 BT_EXPORT_API int bluetooth_gatt_set_characteristic_value(
1854                         const char *characteristic, const char *char_value,
1855                         int     value_length)
1856 {
1857         gchar **line_argv = NULL;
1858         char *serv_path = NULL;
1859         struct gatt_char_info *char_info = NULL;
1860         GVariantBuilder *builder1 = NULL;
1861         GVariantBuilder *builder = NULL;
1862         GVariantBuilder *inner_builder = NULL;
1863         GVariant *char_val = NULL;
1864         GError *error = NULL;
1865         int i = 0;
1866         int res = BLUETOOTH_ERROR_NONE;
1867
1868         line_argv = g_strsplit_set(characteristic, "/", 0);
1869         serv_path = g_strdup_printf("/%s/%s/%s", line_argv[1], line_argv[2], line_argv[3]);
1870
1871         char_info = __bt_gatt_find_gatt_char_info(serv_path, characteristic);
1872
1873         if (char_info == NULL) {
1874                 /* Fix : RESOURCE_LEAK */
1875                 res = BLUETOOTH_ERROR_INVALID_PARAM;
1876                 goto done;
1877         }
1878
1879         char_info->value_length = value_length;
1880
1881         char_info->char_value = (char *)malloc(value_length);
1882         /* Fix : NULL_RETURNS */
1883         if (char_info->char_value == NULL) {
1884                 res = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1885                 goto done;
1886         }
1887
1888         for (i = 0; i < value_length; i++)
1889                 char_info->char_value[i] = char_value[i];
1890
1891         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
1892         inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1893
1894         builder1 = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
1895
1896         for (i = 0; i < value_length; i++) {
1897                 g_variant_builder_add(builder1, "y", char_value[i]);
1898         }
1899
1900         char_val = g_variant_new("ay", builder1);
1901                 g_variant_builder_add(inner_builder, "{sv}", "Value", char_val);
1902
1903         g_variant_builder_add(builder, "{sa{sv}}",
1904                         GATT_CHAR_INTERFACE,
1905                         inner_builder);
1906
1907         g_dbus_connection_emit_signal(g_conn, NULL, "/",
1908                         "org.freedesktop.Dbus.ObjectManager",
1909                         "InterfacesAdded",
1910                         g_variant_new("(oa{sa{sv}})",
1911                         char_info->char_path, builder),
1912                         &error);
1913
1914         g_variant_builder_unref(inner_builder);
1915         g_variant_builder_unref(builder);
1916         g_variant_builder_unref(builder1);
1917 done:
1918         g_strfreev(line_argv);
1919         g_free(serv_path);
1920
1921         return res;
1922 }
1923
1924 BT_EXPORT_API int bluetooth_gatt_add_descriptor(
1925                         const char *char_path, const char *desc_uuid,
1926                         bt_gatt_permission_t permissions,
1927                         char **desc_path)
1928 {
1929         static int desc_id = 1;
1930         GError *error = NULL;
1931         guint object_id;
1932         GDBusNodeInfo *node_info;
1933         gchar *path = NULL;
1934         GVariantBuilder *builder = NULL;
1935         GVariantBuilder *inner_builder = NULL;
1936         struct gatt_char_info *char_info = NULL;
1937         struct gatt_desc_info *desc_info = NULL;
1938         gchar **line_argv = NULL;
1939         char *serv_path;
1940         GVariantBuilder *builder2 = NULL;
1941         GVariant *flags_val = NULL;
1942         int i = 0;
1943         char *desc_flags[NUMBER_OF_FLAGS];
1944         int flag_count = 0;
1945
1946         if (new_char) {
1947                 desc_id = 1;
1948                 new_char = FALSE;
1949         }
1950
1951         line_argv = g_strsplit_set(char_path, "/", 0);
1952         serv_path = g_strdup_printf("/%s/%s/%s", line_argv[1], line_argv[2], line_argv[3]);
1953
1954         char_info = __bt_gatt_find_gatt_char_info(serv_path, char_path);
1955         if (char_info == NULL) {
1956                 g_strfreev(line_argv);
1957                 return BLUETOOTH_ERROR_INVALID_PARAM;
1958         }
1959
1960         node_info = __bt_gatt_create_method_node_info(
1961                                         descriptor_introspection_xml);
1962
1963         if (node_info == NULL) {
1964                 g_strfreev(line_argv);
1965                 return BLUETOOTH_ERROR_INTERNAL;
1966         }
1967
1968         path = g_strdup_printf("%s/descriptor%d", char_path, desc_id++);
1969         BT_DBG("gatt descriptor path is [%s]", path);
1970
1971         object_id = g_dbus_connection_register_object(g_conn, path,
1972                                 node_info->interfaces[0],
1973                                 &desc_interface_vtable,
1974                                 NULL, NULL, &error);
1975
1976         if (object_id == 0) {
1977                 BT_ERR("failed to register: %s", error->message);
1978                 g_error_free(error);
1979                 g_free(path);
1980                 g_strfreev(line_argv);
1981
1982                 return BLUETOOTH_ERROR_INTERNAL;
1983         }
1984
1985         flag_count = bluetooth_gatt_convert_perm2string(permissions, desc_flags);
1986
1987         desc_info = g_new0(struct gatt_desc_info, 1);
1988
1989         desc_info->desc_path = g_strdup(path);
1990         desc_info->desc_id = object_id;
1991         desc_info->desc_uuid = g_strdup(desc_uuid);
1992
1993         for (i = 0; i < flag_count; i++) {
1994                 desc_info->desc_flags[i] = desc_flags[i];
1995                 }
1996
1997         desc_info->flags_length = flag_count;
1998
1999         char_info->desc_data = g_slist_append(char_info->desc_data, desc_info);
2000
2001         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
2002         inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
2003
2004         g_variant_builder_add(inner_builder, "{sv}", "UUID",
2005                                 g_variant_new("s", desc_uuid));
2006         g_variant_builder_add(inner_builder, "{sv}", "Characteristic",
2007                                 g_variant_new("o", char_path));
2008
2009         builder2 = g_variant_builder_new(G_VARIANT_TYPE("as"));
2010
2011         for (i = 0; i < flag_count; i++) {
2012                 g_variant_builder_add(builder2, "s", desc_flags[i]);
2013         }
2014
2015         flags_val = g_variant_new("as", builder2);
2016         g_variant_builder_add(inner_builder, "{sv}", "Flags",
2017                                 flags_val);
2018
2019         g_variant_builder_add(builder, "{sa{sv}}",
2020                                 GATT_DESC_INTERFACE,
2021                                 inner_builder);
2022
2023         g_dbus_connection_emit_signal(g_conn, NULL, "/",
2024                                 "org.freedesktop.Dbus.ObjectManager",
2025                                 "InterfacesAdded",
2026                                 g_variant_new("(oa{sa{sv}})",
2027                                 path, builder),
2028                                 &error);
2029
2030         *desc_path = g_strdup(path);
2031
2032         g_free(path);
2033         g_strfreev(line_argv);
2034         g_variant_builder_unref(inner_builder);
2035         g_variant_builder_unref(builder);
2036
2037         return BLUETOOTH_ERROR_NONE;
2038 }
2039
2040 BT_EXPORT_API int bluetooth_gatt_set_descriptor_value(
2041                         const char *desc_path, const char *desc_value,
2042                         int value_length)
2043 {
2044         GError *error = NULL;
2045         GVariantBuilder *builder = NULL;
2046         GVariantBuilder *inner_builder = NULL;
2047         GVariantBuilder *builder1 = NULL;
2048         struct gatt_desc_info *desc_info = NULL;
2049         gchar **line_argv = NULL;
2050         char *char_path;
2051         GVariant *desc_val = NULL;
2052         char *serv_path = NULL;
2053         int i ;
2054
2055         line_argv = g_strsplit_set(desc_path, "/", 0);
2056         serv_path = g_strdup_printf("/%s/%s/%s", line_argv[1], line_argv[2], line_argv[3]);
2057         char_path = g_strdup_printf("%s/%s", serv_path, line_argv[4]);
2058
2059         desc_info = __bt_gatt_find_gatt_desc_info(serv_path, char_path, desc_path);
2060
2061         /* Free the allocated memory */
2062         g_strfreev(line_argv);
2063         g_free(serv_path);
2064         g_free(char_path);
2065
2066         /* Fix : NULL_RETURNS */
2067         retv_if(desc_info == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
2068
2069         desc_info->desc_value = (char *)malloc(value_length);
2070
2071         /* Fix : NULL_RETURNS */
2072         retv_if(desc_info->desc_value == NULL, BLUETOOTH_ERROR_MEMORY_ALLOCATION);
2073
2074         for (i = 0; i < value_length; i++)
2075                 desc_info->desc_value[i] = desc_value[i];
2076
2077         desc_info->value_length = value_length;
2078
2079         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
2080         inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
2081
2082         builder1 = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
2083
2084         for (i = 0; i < value_length; i++) {
2085                 g_variant_builder_add(builder1, "y", desc_value[i]);
2086         }
2087         desc_val = g_variant_new("ay", builder1);
2088         g_variant_builder_add(inner_builder, "{sv}", "Value", desc_val);
2089
2090         g_variant_builder_add(builder, "{sa{sv}}",
2091                                 GATT_DESC_INTERFACE,
2092                                 inner_builder);
2093
2094         g_dbus_connection_emit_signal(g_conn, NULL, "/",
2095                                 "org.freedesktop.Dbus.ObjectManager",
2096                                 "InterfacesAdded",
2097                                 g_variant_new("(oa{sa{sv}})",
2098                                 desc_info->desc_path, builder),
2099                                 &error);
2100
2101         g_variant_builder_unref(inner_builder);
2102         g_variant_builder_unref(builder);
2103         g_variant_builder_unref(builder1);
2104
2105         return BLUETOOTH_ERROR_NONE;
2106 }
2107
2108 int bluetooth_gatt_get_service(const char *svc_uuid)
2109 {
2110         GDBusProxy *proxy = NULL;
2111         gchar *uuid = NULL;
2112
2113         proxy = __bt_gatt_gdbus_get_manager_proxy("org.bluez",
2114                                         "/org/bluez/hci0", GATT_MNGR_INTERFACE);
2115         if (proxy == NULL)
2116                 return BLUETOOTH_ERROR_INTERNAL;
2117
2118         uuid = g_strdup(svc_uuid);
2119
2120         g_dbus_proxy_call(proxy,
2121                         "GetService",
2122                         g_variant_new("(s)",
2123                         uuid),
2124                         G_DBUS_CALL_FLAGS_NONE, -1,
2125                         NULL,
2126                         (GAsyncReadyCallback) get_service_cb,
2127                         NULL);
2128
2129         g_free(uuid);
2130
2131         return BLUETOOTH_ERROR_NONE;
2132 }
2133
2134 BT_EXPORT_API int bluetooth_gatt_register_service(
2135                         const char *svc_path)
2136 {
2137         if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_GATT_REGISTER_SERVICE)
2138                         == BLUETOOTH_ERROR_PERMISSION_DEINED) {
2139                 BT_ERR("Don't have aprivilege to use this API");
2140                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
2141         }
2142
2143         register_pending_cnt++;
2144
2145         if (__bt_gatt_get_service_state(svc_path)) {
2146                 BT_DBG("service already registered \n");
2147                 return BLUETOOTH_ERROR_NONE;
2148         }
2149
2150         __bt_gatt_set_service_state(svc_path, TRUE);
2151
2152         return BLUETOOTH_ERROR_NONE;
2153 }
2154
2155 BT_EXPORT_API int bluetooth_gatt_register_application(void)
2156 {
2157         GDBusProxy *proxy = NULL;
2158
2159         if (!is_server_started) {
2160
2161                 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_GATT_REGISTER_APPLICATION)
2162                                 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
2163                         BT_ERR("Don't have aprivilege to use this API");
2164                         return BLUETOOTH_ERROR_PERMISSION_DEINED;
2165                 }
2166
2167                 proxy = __bt_gatt_gdbus_get_manager_proxy("org.bluez",
2168                                 "/org/bluez/hci0", GATT_MNGR_INTERFACE);
2169                 if (proxy == NULL || app_path == NULL)
2170                         return BLUETOOTH_ERROR_INTERNAL;
2171
2172                 BT_INFO("RegisterApplication");
2173
2174                 g_dbus_proxy_call(proxy,
2175                                 "RegisterApplication",
2176                                 g_variant_new("(oa{sv})",
2177                                         app_path, NULL),
2178                                 G_DBUS_CALL_FLAGS_NONE, -1,
2179                                 NULL,
2180                                 (GAsyncReadyCallback) register_application_cb,
2181                                 NULL);
2182
2183                 is_server_started = true;
2184
2185                 return BLUETOOTH_ERROR_NONE;
2186         }
2187
2188         BT_INFO("Already RegisterApplication");
2189
2190         return BLUETOOTH_ERROR_NONE;
2191 }
2192
2193 BT_EXPORT_API int bluetooth_gatt_delete_services(void)
2194 {
2195         GSList *l;
2196         int error = BLUETOOTH_ERROR_NONE;
2197         l = gatt_services;
2198
2199         if (l != NULL) {
2200                 for (l = gatt_services; l != NULL; l = l->next) {
2201                         struct gatt_service_info *info = l->data;
2202                         BT_DBG("svc_path is %s", info->serv_path);
2203                         if (bluetooth_gatt_unregister_service(info->serv_path)
2204                                         != BLUETOOTH_ERROR_NONE) {
2205                                 error = BLUETOOTH_ERROR_INTERNAL;
2206                                 BT_DBG(" Error in removing service %s \n",
2207                                                  info->serv_path);
2208                         }
2209                 }
2210                 BT_DBG(" All services removed successfully.\n ");
2211         } else {
2212                 BT_DBG(" There are no registered services.\n ");
2213         }
2214
2215         g_slist_free(gatt_services);
2216         gatt_services = NULL;
2217         serv_id = 1;
2218
2219         if (error != BLUETOOTH_ERROR_NONE)
2220                 return error;
2221
2222         return BLUETOOTH_ERROR_NONE;
2223 }
2224
2225 BT_EXPORT_API int bluetooth_gatt_update_characteristic(
2226                         const char *char_path, const char* char_value,
2227                         int value_length)
2228 {
2229         GVariantBuilder *outer_builder;
2230         GVariantBuilder *inner_builder;
2231         GVariantBuilder *invalidated_builder;
2232         GVariant *update_value = NULL;
2233         GError *error = NULL;
2234         gboolean ret = FALSE;
2235         int err = BLUETOOTH_ERROR_NONE;
2236         int i = 0;
2237         gchar **line_argv = NULL;
2238         gchar *serv_path = NULL;
2239
2240         line_argv = g_strsplit_set(char_path, "/", 0);
2241         serv_path = g_strdup_printf("/%s/%s/%s", line_argv[1], line_argv[2], line_argv[3]);
2242
2243         if (!__bt_gatt_get_service_state(serv_path)) {
2244                 BT_DBG("service not registered for this characteristic \n");
2245                 g_strfreev(line_argv);
2246                 return BLUETOOTH_ERROR_INTERNAL;
2247         }
2248
2249         outer_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
2250         invalidated_builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
2251
2252         inner_builder = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
2253         for (i = 0; i < value_length; i++) {
2254                 g_variant_builder_add(inner_builder, "y", char_value[i]);
2255         }
2256
2257         update_value = g_variant_new("ay", inner_builder);
2258
2259         outer_builder = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
2260         g_variant_builder_add(outer_builder, "{sv}", "Value",
2261                                         update_value);
2262
2263         BT_DBG("Updating characteristic value \n");
2264         ret = g_dbus_connection_emit_signal(g_conn, NULL,
2265                                         char_path,
2266                                         "org.freedesktop.DBus.Properties",
2267                                         "PropertiesChanged",
2268                                         g_variant_new("(sa{sv}as)",
2269                                         "org.bluez.GattCharacteristic1",
2270                                         outer_builder, invalidated_builder),
2271                                         &error);
2272
2273         if (!ret) {
2274                 if (error != NULL) {
2275                         BT_ERR("D-Bus API failure: errCode[%x], \
2276                                         message[%s]",
2277                                         error->code, error->message);
2278                         g_clear_error(&error);
2279                 }
2280                 err = BLUETOOTH_ERROR_INTERNAL;
2281         } else {
2282                 struct gatt_char_info *char_info = NULL;
2283
2284                 char_info = __bt_gatt_find_gatt_char_info(serv_path, char_path);
2285                 if (char_info == NULL) {
2286                         return BLUETOOTH_ERROR_INVALID_DATA;
2287                 }
2288
2289                 char_info->value_length = value_length;
2290
2291                 char_info->char_value = (char *)realloc(char_info->char_value, value_length);
2292                 if (char_info->char_value) {
2293                         for (i = 0; i < value_length; i++) {
2294                                 char_info->char_value[i] = char_value[i];
2295                         }
2296                 }
2297         }
2298
2299         g_strfreev(line_argv);
2300         g_variant_builder_unref(inner_builder);
2301         g_variant_builder_unref(outer_builder);
2302         g_variant_builder_unref(invalidated_builder);
2303
2304         return err;
2305 }
2306
2307 static void __bt_gatt_free_descriptor_info(struct gatt_desc_info *desc_info)
2308 {
2309         int i;
2310
2311         if (!desc_info)
2312                 return;
2313
2314         g_free(desc_info->desc_path);
2315         g_free(desc_info->desc_uuid);
2316         g_free(desc_info->desc_value);
2317
2318         for (i = 0; i < desc_info->flags_length; i++)
2319                 g_free(desc_info->desc_flags[i]);
2320
2321         g_free(desc_info);
2322 }
2323
2324 static void __bt_gatt_free_characteristic_info(struct gatt_char_info *char_info)
2325 {
2326         int i;
2327
2328         if (!char_info)
2329                 return;
2330
2331         g_free(char_info->char_path);
2332         g_free(char_info->char_uuid);
2333         g_free(char_info->char_value);
2334
2335         for (i = 0; i < char_info->flags_length; i++)
2336                 g_free(char_info->char_flags[i]);
2337
2338         g_free(char_info);
2339 }
2340
2341 static void __bt_gatt_free_service_info(struct gatt_service_info *svc_info)
2342 {
2343         if (!svc_info)
2344                 return;
2345
2346         g_free(svc_info->serv_path);
2347         g_free(svc_info->service_uuid);
2348         g_free(svc_info);
2349 }
2350
2351 BT_EXPORT_API int bluetooth_gatt_unregister_service(const char *svc_path)
2352 {
2353         GSList *l, *l1;
2354         struct gatt_service_info *svc_info;
2355         gboolean ret;
2356         int err = BLUETOOTH_ERROR_NONE;
2357
2358         BT_DBG("svc_path %s", svc_path);
2359         svc_info = __bt_gatt_find_gatt_service_info(svc_path);
2360
2361         if (!svc_info) {
2362                 BT_ERR("Unable to find service info");
2363                 return BLUETOOTH_ERROR_NOT_FOUND;
2364         }
2365
2366         err = __bt_gatt_unregister_service(svc_path);
2367         if (err != BLUETOOTH_ERROR_NONE) {
2368                 BT_DBG("Could not unregister application");
2369                 return err;
2370         }
2371
2372         for (l = svc_info->char_data; l != NULL; l = l->next) {
2373                 struct gatt_char_info *char_info = l->data;
2374
2375                 if (char_info == NULL)
2376                         break;
2377
2378                 for (l1 = char_info->desc_data; l1 != NULL; l1 = l1->next) {
2379                         struct gatt_desc_info *desc_info = l1->data;
2380
2381                         if (desc_info == NULL)
2382                                 break;
2383
2384                         ret = g_dbus_connection_unregister_object(g_conn,
2385                                                 desc_info->desc_id);
2386                         if (ret) {
2387                                 __bt_gatt_emit_interface_removed(
2388                                                 desc_info->desc_path,
2389                                                 GATT_DESC_INTERFACE);
2390                         } else {
2391                                 err = BLUETOOTH_ERROR_INTERNAL;
2392                         }
2393
2394                         /* list remove & free */
2395                         char_info->desc_data = g_slist_remove(char_info->desc_data, desc_info);
2396                         __bt_gatt_free_descriptor_info(desc_info);
2397                 }
2398
2399                 g_slist_free(char_info->desc_data);
2400                 char_info->desc_data = NULL;
2401
2402                 ret = g_dbus_connection_unregister_object(g_conn,
2403                                         char_info->char_id);
2404                 if (ret) {
2405                         __bt_gatt_emit_interface_removed(char_info->char_path,
2406                                                 GATT_CHAR_INTERFACE);
2407                 } else {
2408                         err = BLUETOOTH_ERROR_INTERNAL;
2409                 }
2410
2411                 /* list remove & free */
2412                 svc_info->char_data = g_slist_remove(svc_info->char_data, char_info);
2413                 __bt_gatt_free_characteristic_info(char_info);
2414         }
2415
2416         g_slist_free(svc_info->char_data);
2417         svc_info->char_data = NULL;
2418
2419         ret = g_dbus_connection_unregister_object(g_conn, svc_info->serv_id);
2420         if (ret) {
2421                 __bt_gatt_emit_interface_removed(svc_info->serv_path,
2422                                                 GATT_SERV_INTERFACE);
2423         } else {
2424                 err = BLUETOOTH_ERROR_INTERNAL;
2425         }
2426
2427         ret = g_dbus_connection_unregister_object(g_conn, svc_info->prop_id);
2428         if (ret) {
2429                 BT_DBG("Unregistered the service on properties interface");
2430         }
2431
2432         /* list remove & free */
2433         gatt_services = g_slist_remove(gatt_services, svc_info);
2434         __bt_gatt_free_service_info(svc_info);
2435
2436         new_service = FALSE;
2437
2438         if (gatt_services == NULL) {
2439                 serv_id = 1;
2440         } else if (gatt_services->next == NULL) {
2441                 serv_id--;
2442         }
2443
2444         return err;
2445 }
2446
2447 BT_EXPORT_API int bluetooth_gatt_send_response(int request_id, guint req_type,
2448                                         int resp_state, int offset, char *value, int value_length)
2449 {
2450         struct gatt_req_info *req_info = NULL;
2451
2452         if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_GATT_SEND_RESPONSE)
2453                         == BLUETOOTH_ERROR_PERMISSION_DEINED) {
2454                 BT_ERR("Don't have aprivilege to use this API");
2455                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
2456         }
2457
2458         req_info = __bt_gatt_find_request_info(request_id);
2459
2460         if (req_info) {
2461                 if (resp_state != BLUETOOTH_ERROR_NONE) {
2462
2463                         GQuark quark = g_quark_from_string("gatt-server");
2464                         GError *err = g_error_new(quark, 0, "Application Error");
2465                         g_dbus_method_invocation_return_gerror(req_info->context, err);
2466                         g_error_free(err);
2467
2468                         gatt_requests = g_slist_remove(gatt_requests, req_info);
2469
2470                         req_info->context = NULL;
2471                         if (req_info->attr_path)
2472                                 g_free(req_info->attr_path);
2473                         if (req_info->svc_path)
2474                                 g_free(req_info->svc_path);
2475                         g_free(req_info);
2476
2477                         return BLUETOOTH_ERROR_NONE;
2478                 }
2479                 if (req_type == BLUETOOTH_GATT_ATT_REQUEST_TYPE_READ) {
2480                         int i;
2481                         GVariantBuilder *inner_builder = NULL;
2482                         inner_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
2483                         if (value_length > 0 && value != NULL) {
2484                                 for (i = 0; i < value_length; i++)
2485                                         g_variant_builder_add(inner_builder, "y", value[i]);
2486                         }
2487                         g_dbus_method_invocation_return_value(req_info->context,
2488                                                 g_variant_new("(ay)", inner_builder));
2489                         g_variant_builder_unref(inner_builder);
2490                 } else {
2491                         g_dbus_method_invocation_return_value(req_info->context, NULL);
2492                 }
2493                 gatt_requests = g_slist_remove(gatt_requests, req_info);
2494
2495                 req_info->context = NULL;
2496                 if (req_info->attr_path)
2497                         g_free(req_info->attr_path);
2498                 if (req_info->svc_path)
2499                         g_free(req_info->svc_path);
2500                 g_free(req_info);
2501         } else {
2502                 return BLUETOOTH_ERROR_INTERNAL;
2503         }
2504
2505         return BLUETOOTH_ERROR_NONE;
2506 }
2507
2508 BT_EXPORT_API int bluetooth_gatt_server_set_notification(const char *char_path,
2509                                                 bluetooth_device_address_t *unicast_address)
2510 {
2511         GVariantBuilder *outer_builder;
2512         GVariantBuilder *invalidated_builder;
2513         GError *error = NULL;
2514         gboolean notify = TRUE;
2515         gboolean ret = TRUE;
2516         int err = BLUETOOTH_ERROR_NONE;
2517         gchar **line_argv = NULL;
2518         gchar *serv_path = NULL;
2519         char addr[20] = { 0 };
2520
2521         line_argv = g_strsplit_set(char_path, "/", 0);
2522         serv_path = g_strdup_printf("/%s/%s/%s", line_argv[1], line_argv[2], line_argv[3]);
2523
2524         if (!__bt_gatt_get_service_state(serv_path)) {
2525                 BT_DBG("service not registered for this characteristic \n");
2526                 g_strfreev(line_argv);
2527                 return BLUETOOTH_ERROR_INTERNAL;
2528         }
2529
2530         outer_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
2531         invalidated_builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
2532
2533         g_variant_builder_add(outer_builder, "{sv}", "Notifying",
2534                                         g_variant_new("b", notify));
2535
2536         if (unicast_address) {
2537                 _bt_convert_addr_type_to_string(addr,
2538                                         (unsigned char *)unicast_address->addr);
2539         }
2540         g_variant_builder_add(outer_builder, "{sv}", "Unicast",
2541                                 g_variant_new("s", addr));
2542
2543         BT_DBG("Set characteristic Notification \n");
2544         ret = g_dbus_connection_emit_signal(g_conn, NULL,
2545                                         char_path,
2546                                         "org.freedesktop.DBus.Properties",
2547                                         "PropertiesChanged",
2548                                         g_variant_new("(sa{sv}as)",
2549                                         "org.bluez.GattCharacteristic1",
2550                                         outer_builder, invalidated_builder),
2551                                         &error);
2552
2553         if (!ret) {
2554                 if (error != NULL) {
2555                         BT_ERR("D-Bus API failure: errCode[%x], \
2556                                         message[%s]",
2557                                         error->code, error->message);
2558                         g_clear_error(&error);
2559                 }
2560                 err = BLUETOOTH_ERROR_INTERNAL;
2561         }
2562
2563         g_strfreev(line_argv);
2564         g_variant_builder_unref(outer_builder);
2565         g_variant_builder_unref(invalidated_builder);
2566
2567         return err;
2568 }