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