Modify build feature names as Tizen 3.0 build option naming rule
[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 TIZEN_FEATURE_BT_HPS
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 TIZEN_FEATURE_BT_HPS
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 TIZEN_FEATURE_BT_HPS
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 TIZEN_FEATURE_BT_HPS
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 TIZEN_FEATURE_BT_HPS
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 TIZEN_FEATURE_BT_HPS
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         BT_ERR("Gatt Request not found");
1141         return NULL;
1142 }
1143
1144 static GDBusProxy *__bt_gatt_gdbus_init_manager_proxy(const gchar *service,
1145                                 const gchar *path, const gchar *interface)
1146 {
1147         GDBusProxy *proxy;
1148         GError *err = NULL;
1149
1150         if (g_conn == NULL)
1151                 g_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM,
1152                                                         NULL, &err);
1153
1154         if (!g_conn) {
1155                 if (err) {
1156                         BT_ERR("Unable to connect to gdbus: %s", err->message);
1157                         g_clear_error(&err);
1158                 }
1159                 return NULL;
1160         }
1161
1162         proxy =  g_dbus_proxy_new_sync(g_conn,
1163                         G_DBUS_PROXY_FLAGS_NONE, NULL,
1164                         service, path,
1165                         interface, NULL, &err);
1166
1167         if (!proxy) {
1168                 if (err) {
1169                         BT_ERR("Unable to create proxy: %s", err->message);
1170                         g_clear_error(&err);
1171                 }
1172                 return NULL;
1173         }
1174         manager_gproxy = proxy;
1175
1176         return proxy;
1177 }
1178
1179 static GDBusProxy *__bt_gatt_gdbus_get_manager_proxy(const gchar *service,
1180                                 const gchar *path, const gchar *interface)
1181 {
1182         return (manager_gproxy) ? manager_gproxy :
1183                         __bt_gatt_gdbus_init_manager_proxy(service,
1184                                 path, interface);
1185 }
1186
1187 int bluetooth_gatt_convert_prop2string(
1188                         bt_gatt_characteristic_property_t properties,
1189                         char *char_properties[])
1190 {
1191         int flag_count = 0;
1192
1193         if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_BROADCAST) {
1194                 char_properties[flag_count] = g_strdup("broadcast");
1195                 flag_count++;
1196         }
1197         if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ) {
1198                 char_properties[flag_count] = g_strdup("read");
1199                 flag_count++;
1200         }
1201         if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE_NO_RESPONSE) {
1202                 char_properties[flag_count] = g_strdup("write-without-response");
1203                 flag_count++;
1204         }
1205         if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE) {
1206                 char_properties[flag_count] = g_strdup("write");
1207                 flag_count++;
1208         }
1209         if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_NOTIFY) {
1210                 char_properties[flag_count] = g_strdup("notify");
1211                 flag_count++;
1212         }
1213         if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_INDICATE) {
1214                 char_properties[flag_count] = g_strdup("indicate");
1215                 flag_count++;
1216         }
1217         if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_SIGNED_WRITE) {
1218                 char_properties[flag_count] = g_strdup("authenticated-signed-writes");
1219                 flag_count++;
1220         }
1221         if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_RELIABLE_WRITE) {
1222                 char_properties[flag_count] = g_strdup("reliable-write");
1223                 flag_count++;
1224         }
1225         if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITABLE_AUXILIARIES) {
1226                 char_properties[flag_count] = g_strdup("writable-auxiliaries");
1227                 flag_count++;
1228         }
1229         if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_READ) {
1230                 char_properties[flag_count] = g_strdup("encrypt-read");
1231                 flag_count++;
1232         }
1233         if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_WRITE) {
1234                 char_properties[flag_count] = g_strdup("encrypt-write");
1235                 flag_count++;
1236         }
1237         if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_READ) {
1238                 char_properties[flag_count] = g_strdup("encrypt-authenticated-read");
1239                 flag_count++;
1240         }
1241         if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_WRITE) {
1242                 char_properties[flag_count] = g_strdup("encrypt-authenticated-write");
1243                 flag_count++;
1244         }
1245
1246         if (flag_count == 0) {
1247                 char_properties[flag_count] = g_strdup("read");
1248                 flag_count++;
1249         }
1250
1251         return flag_count;
1252 }
1253
1254 int bluetooth_gatt_convert_perm2string(
1255                         bt_gatt_permission_t properties,
1256                         char *char_properties[])
1257 {
1258         int flag_count = 0;
1259
1260         if (properties & BLUETOOTH_GATT_PERMISSION_READ) {
1261                 char_properties[flag_count] = g_strdup("read");
1262                 flag_count++;
1263         }
1264         if (properties & BLUETOOTH_GATT_PERMISSION_WRITE) {
1265                 char_properties[flag_count] = g_strdup("write");
1266                 flag_count++;
1267         }
1268         if (properties & BLUETOOTH_GATT_PERMISSION_ENCRYPT_READ) {
1269                 char_properties[flag_count] = g_strdup("encrypt-read");
1270                 flag_count++;
1271         }
1272         if (properties & BLUETOOTH_GATT_PERMISSION_ENCRYPT_WRITE) {
1273                 char_properties[flag_count] = g_strdup("encrypt-write");
1274                 flag_count++;
1275         }
1276         if (properties & BLUETOOTH_GATT_PERMISSION_ENCRYPT_AUTHENTICATED_READ) {
1277                 char_properties[flag_count] = g_strdup("encrypt-authenticated-read");
1278                 flag_count++;
1279         }
1280         if (properties & BLUETOOTH_GATT_PERMISSION_ENCRYPT_AUTHENTICATED_WRITE) {
1281                 char_properties[flag_count] = g_strdup("encrypt-authenticated-write");
1282                 flag_count++;
1283         }
1284
1285         if (flag_count == 0) {
1286                 char_properties[flag_count] = g_strdup("read");
1287                 flag_count++;
1288         }
1289
1290         return flag_count;
1291 }
1292
1293 static void __bt_gatt_set_service_state(const char *service_path,
1294                         gboolean state)
1295 {
1296         struct gatt_service_info *svc_info = NULL;
1297         svc_info = __bt_gatt_find_gatt_service_info(service_path);
1298
1299         if (svc_info != NULL) {
1300                 BT_DBG("Updating the gatt service register state %d", state);
1301                 svc_info->is_svc_registered = state;
1302                 return;
1303         }
1304
1305         BT_DBG("gatt service not found");
1306 }
1307
1308 static gboolean __bt_gatt_get_service_state(const char *service_path)
1309 {
1310         struct gatt_service_info *svc_info = NULL;
1311
1312         svc_info = __bt_gatt_find_gatt_service_info(service_path);
1313
1314         if (svc_info != NULL) {
1315                 BT_DBG("Return the state of the gatt service %d",
1316                         svc_info->is_svc_registered);
1317                 return svc_info->is_svc_registered;
1318         }
1319
1320         BT_DBG("gatt service info is NULL");
1321         return FALSE;
1322 }
1323
1324 void get_service_cb(GObject *object, GAsyncResult *res, gpointer user_data)
1325 {
1326         GError *error = NULL;
1327         GVariant *result;
1328         GVariantIter *iter = NULL;
1329         const gchar *key = NULL;
1330         GVariant *value = NULL;
1331         const gchar *service = NULL;
1332         const gchar *characteristic = NULL;
1333         const gchar *descriptor = NULL;
1334         int n_char = 1;
1335
1336         BT_DBG(" ");
1337         result = g_dbus_proxy_call_finish(manager_gproxy, res, &error);
1338
1339         if (result == NULL) {
1340                 /* dBUS-RPC is failed */
1341                 BT_ERR("Dbus-RPC is failed\n");
1342
1343                 if (error != NULL) {
1344                 /* dBUS gives error cause */
1345                         BT_ERR("D-Bus API failure: errCode[%x], message[%s]\n",
1346                                                 error->code, error->message);
1347                         g_clear_error(&error);
1348                 }
1349         } else {
1350                 char *char_cmp = NULL;
1351                 g_variant_get(result, "(a{sv})", &iter);
1352                 char_cmp = g_strdup_printf("Characteristic%d", n_char);
1353
1354                 while (g_variant_iter_loop(iter, "{sv}", &key, &value)) {
1355                         if (g_strcmp0(key, "Service") == 0) {
1356                                 service = g_variant_get_string(value, NULL);
1357                                 BT_DBG("Service %s", service);
1358                         } else if (g_strcmp0(key, char_cmp) == 0) {
1359                                 characteristic = g_variant_get_string(value, NULL);
1360                                 g_free(char_cmp);
1361                                 char_cmp = g_strdup_printf("Characteristic%d", ++n_char);
1362                                 BT_DBG("%s", characteristic);
1363                         } else if (g_strcmp0(key, "Descriptor") == 0) {
1364                                 descriptor = g_variant_get_string(value, NULL);
1365                                 BT_DBG("Descriptor %s", descriptor);
1366                         }
1367                 }
1368                 g_variant_iter_free(iter);
1369
1370                 /* TODO: Store the service informationa and
1371                  * Send respponse to CAPI layer. */
1372
1373                 g_variant_unref(result);
1374                 if (char_cmp)
1375                         g_free(char_cmp);
1376         }
1377 }
1378 void register_application_cb(GObject *object, GAsyncResult *res, gpointer user_data)
1379 {
1380         BT_INFO("RegisterApplication is completed");
1381
1382         GError *error = NULL;
1383         GVariant *result;
1384
1385         register_pending_cnt = 0;
1386
1387         result = g_dbus_proxy_call_finish(manager_gproxy, res, &error);
1388
1389         if (result == NULL) {
1390                 /* dBUS-RPC is failed */
1391                 BT_ERR("Dbus-RPC is failed\n");
1392
1393                 if (error != NULL) {
1394                 /* dBUS gives error cause */
1395                         BT_ERR("D-Bus API failure: errCode[%x], message[%s]\n",
1396                                                 error->code, error->message);
1397                         g_clear_error(&error);
1398                 }
1399         } else {
1400                 g_variant_unref(result);
1401         }
1402 }
1403
1404 void unregister_application_cb(GObject *object, GAsyncResult *res,
1405                 gpointer user_data)
1406 {
1407         BT_INFO("UnregisterApplication is completed");
1408
1409         GError *error = NULL;
1410         GVariant *result;
1411
1412         result = g_dbus_proxy_call_finish(manager_gproxy, res, &error);
1413
1414         if (result == NULL) {
1415                 /* dBUS-RPC is failed */
1416                 BT_ERR("Dbus-RPC is failed\n");
1417
1418                 if (error != NULL) {
1419                         /* dBUS gives error cause */
1420                         BT_ERR("D-Bus API failure: errCode[%x], message[%s]\n",
1421                                         error->code, error->message);
1422                         g_clear_error(&error);
1423                 }
1424         } else {
1425                 g_variant_unref(result);
1426         }
1427 }
1428
1429 static int __bt_gatt_unregister_service(const char *service_path)
1430 {
1431         if (!__bt_gatt_get_service_state(service_path)) {
1432                 BT_DBG("service not registered \n");
1433                 return BLUETOOTH_ERROR_NOT_FOUND;
1434         }
1435
1436         return BLUETOOTH_ERROR_NONE;
1437 }
1438
1439 BT_EXPORT_API int bluetooth_gatt_unregister_application(void)
1440 {
1441         GDBusProxy *proxy = NULL;
1442
1443         if (is_server_started) {
1444                 proxy = __bt_gatt_gdbus_get_manager_proxy("org.bluez",
1445                                 "/org/bluez/hci0", GATT_MNGR_INTERFACE);
1446
1447                 if (proxy == NULL || app_path == NULL)
1448                         return BLUETOOTH_ERROR_INTERNAL;
1449
1450                 BT_INFO("UnregisterApplication");
1451
1452                 /* Async Call to Unregister Service */
1453                 g_dbus_proxy_call(proxy,
1454                                 "UnregisterApplication",
1455                                 g_variant_new("(o)",
1456                                         app_path),
1457                                 G_DBUS_CALL_FLAGS_NONE, -1,
1458                                 NULL,
1459                                 (GAsyncReadyCallback) unregister_application_cb,
1460                                 NULL);
1461
1462                 is_server_started = false;
1463                 return BLUETOOTH_ERROR_NONE;
1464         }
1465
1466         BT_INFO("GATT server not started");
1467         return BLUETOOTH_ERROR_NONE;
1468 }
1469
1470 static GDBusConnection *__bt_gatt_get_gdbus_connection(void)
1471 {
1472         GDBusConnection *local_system_gconn = NULL;
1473         char *address;
1474         GError *err = NULL;
1475
1476         if (g_conn == NULL) {
1477                 address = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
1478                 if (address == NULL) {
1479                         if (err) {
1480                                 BT_ERR("Failed to get bus address: %s", err->message);
1481                                 g_clear_error(&err);
1482                         }
1483                         return NULL;
1484                 }
1485
1486                 g_conn = g_dbus_connection_new_for_address_sync(address,
1487                                         G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
1488                                         G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
1489                                         NULL, /* GDBusAuthObserver */
1490                                         NULL,
1491                                         &err);
1492                 if (!g_conn) {
1493                         if (err) {
1494                                 BT_ERR("Unable to connect to dbus: %s", err->message);
1495                                 g_clear_error(&err);
1496                         }
1497                         return NULL;
1498                 }
1499         } else if (g_dbus_connection_is_closed(g_conn)) {
1500                 address = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
1501                 if (address == NULL) {
1502                         if (err) {
1503                                 BT_ERR("Failed to get bus address: %s", err->message);
1504                                 g_clear_error(&err);
1505                         }
1506                         return NULL;
1507                 }
1508
1509                 local_system_gconn = g_dbus_connection_new_for_address_sync(address,
1510                                         G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
1511                                         G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
1512                                         NULL, /* GDBusAuthObserver */
1513                                         NULL,
1514                                         &err);
1515
1516                 if (!local_system_gconn) {
1517                         BT_ERR("Unable to connect to dbus: %s", err->message);
1518                         g_clear_error(&err);
1519                 }
1520
1521                 g_conn = local_system_gconn;
1522         }
1523
1524         return g_conn;
1525 }
1526
1527 BT_EXPORT_API int bluetooth_gatt_init(void)
1528 {
1529         GDBusConnection *conn;
1530         GError *error = NULL;
1531         GDBusNodeInfo *node_info = NULL;
1532
1533         if (app_path != NULL) {
1534                 BT_ERR("app path already exists! initialized");
1535                 return BLUETOOTH_ERROR_ALREADY_INITIALIZED;
1536         }
1537
1538         if (owner_id == 0) {
1539                 owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
1540                                         BT_GATT_SERVICE_NAME,
1541                                         G_BUS_NAME_OWNER_FLAGS_NONE,
1542                                         NULL, NULL, NULL, NULL, NULL);
1543         }
1544
1545         BT_DBG("owner_id is [%d]", owner_id);
1546         app_path = g_strdup_printf("/com/%d", getpid());
1547
1548         serv_id = 1;
1549
1550         conn = __bt_gatt_get_gdbus_connection();
1551         if (!conn) {
1552                 BT_ERR("Unable to get connection");
1553                 goto failed;
1554         }
1555
1556         /* Register ObjectManager interface */
1557         node_info = __bt_gatt_create_method_node_info(
1558                                         manager_introspection_xml);
1559
1560         if (node_info == NULL) {
1561                 BT_ERR("failed to get node info");
1562                 goto failed;
1563         }
1564
1565         if (manager_id == 0) {
1566                 BT_INFO("manager_id does not exists");
1567
1568                 manager_id = g_dbus_connection_register_object(g_conn, app_path,
1569                                                         node_info->interfaces[0],
1570                                                         &manager_interface_vtable,
1571                                                         NULL, NULL, &error);
1572         }
1573
1574         if (manager_id == 0) {
1575                 BT_ERR("failed to register: %s", error->message);
1576                 g_error_free(error);
1577                 goto failed;
1578         }
1579
1580         return BLUETOOTH_ERROR_NONE;
1581
1582 failed:
1583         if (owner_id)
1584                 g_bus_unown_name(owner_id);
1585
1586         g_free(app_path);
1587
1588         app_path = NULL;
1589         owner_id = 0;
1590
1591         __bt_gatt_close_gdbus_connection();
1592
1593         return BLUETOOTH_ERROR_INTERNAL;
1594 }
1595
1596 BT_EXPORT_API int bluetooth_gatt_deinit()
1597 {
1598         int ret = BLUETOOTH_ERROR_NONE;
1599         /* Unown gdbus bus */
1600         if (owner_id) {
1601                 /* remove/unregister all services */
1602                 BT_DBG("removing all registered gatt service\n");
1603                 bluetooth_gatt_delete_services();
1604
1605                 /* unregister the exported interface for object manager */
1606                 g_dbus_connection_unregister_object(g_conn,
1607                                         manager_id);
1608
1609                 manager_id = 0;
1610
1611                 ret = bluetooth_gatt_unregister_application();
1612                 if (ret != BLUETOOTH_ERROR_NONE)
1613                         BT_ERR("Fail to unregister application\n");
1614
1615                 g_bus_unown_name(owner_id);
1616                 owner_id = 0;
1617
1618                 g_free(app_path);
1619                 app_path = NULL;
1620
1621                 BT_DBG("Gatt service deinitialized \n");
1622
1623                 g_slist_free(gatt_services);
1624                 gatt_services = NULL;
1625
1626                 g_object_unref(manager_gproxy);
1627                 manager_gproxy = NULL;
1628
1629                 __bt_gatt_close_gdbus_connection();
1630
1631                 return ret;
1632         }
1633
1634         __bt_gatt_close_gdbus_connection();
1635
1636         return BLUETOOTH_ERROR_NOT_FOUND;
1637 }
1638
1639 BT_EXPORT_API int bluetooth_gatt_add_service(const char *svc_uuid,
1640                         char **svc_path)
1641 {
1642         GError *error = NULL;
1643         guint object_id;
1644         GDBusNodeInfo *node_info;
1645         gchar *path = NULL;
1646         GVariantBuilder *builder = NULL;
1647         GVariantBuilder *builder1 = NULL;
1648         GVariantBuilder *inner_builder = NULL;
1649         gboolean svc_primary = TRUE;
1650         struct gatt_service_info *serv_info = NULL;
1651
1652         node_info = __bt_gatt_create_method_node_info(
1653                                         service_introspection_xml);
1654
1655         if (node_info == NULL)
1656                 return BLUETOOTH_ERROR_INTERNAL;
1657
1658         path = g_strdup_printf("%s"GATT_SERV_OBJECT_PATH"%d", app_path, serv_id++);
1659         BT_DBG("gatt service path is [%s]", path);
1660
1661         object_id = g_dbus_connection_register_object(g_conn, path,
1662                                         node_info->interfaces[0],
1663                                         &serv_interface_vtable,
1664                                         NULL, NULL, &error);
1665
1666         if (object_id == 0) {
1667                 BT_ERR("failed to register: %s", error->message);
1668                 g_error_free(error);
1669                 g_free(path);
1670
1671                 return BLUETOOTH_ERROR_INTERNAL;
1672         }
1673
1674         /* Add object_id/gatt service information; it's required at the time of
1675          *  service unregister and Getmanagedobjects
1676          */
1677         serv_info = g_new0(struct gatt_service_info, 1);
1678
1679         serv_info->serv_path = g_strdup(path);
1680         serv_info->serv_id = object_id;
1681         serv_info->service_uuid = g_strdup(svc_uuid);
1682         serv_info->is_svc_registered = FALSE;
1683         serv_info->is_svc_primary = svc_primary;
1684
1685         gatt_services = g_slist_append(gatt_services, serv_info);
1686
1687         /* emit interfacesadded signal here for service path */
1688         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
1689         inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1690
1691         g_variant_builder_add(inner_builder, "{sv}",
1692                 "UUID", g_variant_new_string(svc_uuid));
1693
1694         g_variant_builder_add(inner_builder, "{sv}",
1695                 "Primary", g_variant_new_boolean(svc_primary));
1696
1697         builder1 = g_variant_builder_new(G_VARIANT_TYPE("ao"));
1698
1699         g_variant_builder_add(inner_builder, "{sv}", "Characteristics",
1700                                 g_variant_new("ao", builder1));
1701
1702         g_variant_builder_add(builder, "{sa{sv}}",
1703                 GATT_SERV_INTERFACE, inner_builder);
1704
1705         g_dbus_connection_emit_signal(g_conn, NULL, "/",
1706                                 "org.freedesktop.Dbus.ObjectManager",
1707                                 "InterfacesAdded",
1708                                 g_variant_new("(oa{sa{sv}})",
1709                                 path, builder),
1710                                 &error);
1711
1712         new_service = TRUE;
1713
1714         *svc_path = g_strdup(path);
1715
1716         g_free(path);
1717         g_variant_builder_unref(inner_builder);
1718         g_variant_builder_unref(builder);
1719         g_variant_builder_unref(builder1);
1720
1721         return BLUETOOTH_ERROR_NONE;
1722 }
1723
1724 BT_EXPORT_API int bluetooth_gatt_add_new_characteristic(
1725                         const char *svc_path, const char *char_uuid,
1726                         bt_gatt_permission_t permissions,
1727                         bt_gatt_characteristic_property_t properties,
1728                         char **char_path)
1729 {
1730         static int char_id;
1731         GError *error = NULL;
1732         guint object_id;
1733         GDBusNodeInfo *node_info;
1734         gchar *path = NULL;
1735         GVariantBuilder *builder = NULL;
1736         GVariantBuilder *inner_builder = NULL;
1737         struct gatt_service_info *serv_info = NULL;
1738         struct gatt_char_info *char_info = NULL;
1739         GVariantBuilder *builder2 = NULL;
1740         GVariantBuilder *builder3 = NULL;
1741         GVariant *flags_val = NULL;
1742         int i = 0;
1743         char *char_flags[NUMBER_OF_FLAGS];
1744         int flag_count = 0;
1745
1746         if (new_service) {
1747                 char_id = 1;
1748                 new_service = FALSE;
1749         }
1750
1751         BT_DBG("gatt svc_path path is [%s]", svc_path);
1752         serv_info = __bt_gatt_find_gatt_service_info(svc_path);
1753         if (serv_info == NULL)
1754                 return BLUETOOTH_ERROR_INVALID_PARAM;
1755
1756         node_info = __bt_gatt_create_method_node_info(
1757                                         characteristics_introspection_xml);
1758
1759         if (node_info == NULL)
1760                 return BLUETOOTH_ERROR_INTERNAL;
1761
1762         path = g_strdup_printf("%s/characteristic%d", svc_path, char_id++);
1763         BT_DBG("gatt characteristic path is [%s]", path);
1764
1765         object_id = g_dbus_connection_register_object(g_conn, path,
1766                                         node_info->interfaces[0],
1767                                         &char_interface_vtable,
1768                                         NULL, NULL, &error);
1769
1770         if (object_id == 0) {
1771                 BT_ERR("failed to register: %s", error->message);
1772                 g_error_free(error);
1773                 g_free(path);
1774
1775                 return BLUETOOTH_ERROR_INTERNAL;
1776         }
1777
1778         if (permissions & BLUETOOTH_GATT_PERMISSION_ENCRYPT_READ)
1779                 properties |= BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_READ;
1780         if (permissions & BLUETOOTH_GATT_PERMISSION_ENCRYPT_AUTHENTICATED_READ)
1781                 properties |= BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_READ;
1782         if (permissions & BLUETOOTH_GATT_PERMISSION_ENCRYPT_WRITE)
1783                 properties |= BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_WRITE;
1784         if (permissions & BLUETOOTH_GATT_PERMISSION_ENCRYPT_AUTHENTICATED_WRITE)
1785                 properties |= BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_WRITE;
1786
1787         flag_count = bluetooth_gatt_convert_prop2string(properties, char_flags);
1788
1789         char_info = g_new0(struct gatt_char_info, 1);
1790
1791         char_info->char_path = g_strdup(path);
1792         char_info->char_id = object_id;
1793         char_info->char_uuid = g_strdup(char_uuid);
1794
1795         for (i = 0; i < flag_count; i++)
1796                 char_info->char_flags[i] = char_flags[i];
1797
1798
1799         char_info->flags_length = flag_count;
1800
1801         serv_info->char_data = g_slist_append(serv_info->char_data, char_info);
1802
1803         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
1804         inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1805
1806         g_variant_builder_add(inner_builder, "{sv}", "UUID",
1807                                 g_variant_new("s", char_uuid));
1808         g_variant_builder_add(inner_builder, "{sv}", "Service",
1809                                 g_variant_new("o", svc_path));
1810
1811         builder2 = g_variant_builder_new(G_VARIANT_TYPE("as"));
1812
1813         for (i = 0; i < flag_count; i++)
1814                 g_variant_builder_add(builder2, "s", char_flags[i]);
1815
1816         flags_val = g_variant_new("as", builder2);
1817         g_variant_builder_add(inner_builder, "{sv}", "Flags",
1818                                 flags_val);
1819
1820         builder3 = g_variant_builder_new(G_VARIANT_TYPE("ao"));
1821
1822         g_variant_builder_add(inner_builder, "{sv}", "Descriptors",
1823                                 g_variant_new("ao", builder3));
1824
1825         g_variant_builder_add(builder, "{sa{sv}}",
1826                                 GATT_CHAR_INTERFACE,
1827                                 inner_builder);
1828
1829         g_dbus_connection_emit_signal(g_conn, NULL, "/",
1830                                 "org.freedesktop.Dbus.ObjectManager",
1831                                 "InterfacesAdded",
1832                                 g_variant_new("(oa{sa{sv}})",
1833                                 path, builder),
1834                                 &error);
1835
1836         *char_path = g_strdup(path);
1837
1838         new_char = TRUE;
1839
1840         g_free(path);
1841
1842         g_variant_builder_unref(inner_builder);
1843         g_variant_builder_unref(builder);
1844         g_variant_builder_unref(builder2);
1845         g_variant_builder_unref(builder3);
1846
1847         return BLUETOOTH_ERROR_NONE;
1848 }
1849
1850 BT_EXPORT_API int bluetooth_gatt_set_characteristic_value(
1851                         const char *characteristic, const char *char_value,
1852                         int     value_length)
1853 {
1854         gchar **line_argv = NULL;
1855         char *serv_path = NULL;
1856         struct gatt_char_info *char_info = NULL;
1857         GVariantBuilder *builder1 = NULL;
1858         GVariantBuilder *builder = NULL;
1859         GVariantBuilder *inner_builder = NULL;
1860         GVariant *char_val = NULL;
1861         GError *error = NULL;
1862         int i = 0;
1863         int res = BLUETOOTH_ERROR_NONE;
1864
1865         line_argv = g_strsplit_set(characteristic, "/", 0);
1866         serv_path = g_strdup_printf("/%s/%s/%s", line_argv[1], line_argv[2], line_argv[3]);
1867
1868         char_info = __bt_gatt_find_gatt_char_info(serv_path, characteristic);
1869
1870         if (char_info == NULL) {
1871                 /* Fix : RESOURCE_LEAK */
1872                 res = BLUETOOTH_ERROR_INVALID_PARAM;
1873                 goto done;
1874         }
1875
1876         char_info->value_length = value_length;
1877
1878         char_info->char_value = (char *)malloc(value_length);
1879         /* Fix : NULL_RETURNS */
1880         if (char_info->char_value == NULL) {
1881                 res = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1882                 goto done;
1883         }
1884
1885         for (i = 0; i < value_length; i++)
1886                 char_info->char_value[i] = char_value[i];
1887
1888         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
1889         inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1890
1891         builder1 = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
1892
1893         for (i = 0; i < value_length; i++)
1894                 g_variant_builder_add(builder1, "y", char_value[i]);
1895
1896         char_val = g_variant_new("ay", builder1);
1897                 g_variant_builder_add(inner_builder, "{sv}", "Value", char_val);
1898
1899         g_variant_builder_add(builder, "{sa{sv}}",
1900                         GATT_CHAR_INTERFACE,
1901                         inner_builder);
1902
1903         g_dbus_connection_emit_signal(g_conn, NULL, "/",
1904                         "org.freedesktop.Dbus.ObjectManager",
1905                         "InterfacesAdded",
1906                         g_variant_new("(oa{sa{sv}})",
1907                         char_info->char_path, builder),
1908                         &error);
1909
1910         g_variant_builder_unref(inner_builder);
1911         g_variant_builder_unref(builder);
1912         g_variant_builder_unref(builder1);
1913 done:
1914         g_strfreev(line_argv);
1915         g_free(serv_path);
1916
1917         return res;
1918 }
1919
1920 BT_EXPORT_API int bluetooth_gatt_add_descriptor(
1921                         const char *char_path, const char *desc_uuid,
1922                         bt_gatt_permission_t permissions,
1923                         char **desc_path)
1924 {
1925         static int desc_id = 1;
1926         GError *error = NULL;
1927         guint object_id;
1928         GDBusNodeInfo *node_info;
1929         gchar *path = NULL;
1930         GVariantBuilder *builder = NULL;
1931         GVariantBuilder *inner_builder = NULL;
1932         struct gatt_char_info *char_info = NULL;
1933         struct gatt_desc_info *desc_info = NULL;
1934         gchar **line_argv = NULL;
1935         char *serv_path;
1936         GVariantBuilder *builder2 = NULL;
1937         GVariant *flags_val = NULL;
1938         int i = 0;
1939         char *desc_flags[NUMBER_OF_FLAGS];
1940         int flag_count = 0;
1941
1942         if (new_char) {
1943                 desc_id = 1;
1944                 new_char = FALSE;
1945         }
1946
1947         line_argv = g_strsplit_set(char_path, "/", 0);
1948         serv_path = g_strdup_printf("/%s/%s/%s", line_argv[1], line_argv[2], line_argv[3]);
1949
1950         char_info = __bt_gatt_find_gatt_char_info(serv_path, char_path);
1951         if (char_info == NULL) {
1952                 g_strfreev(line_argv);
1953                 g_free(serv_path);
1954                 return BLUETOOTH_ERROR_INVALID_PARAM;
1955         }
1956
1957         node_info = __bt_gatt_create_method_node_info(
1958                                         descriptor_introspection_xml);
1959
1960         if (node_info == NULL) {
1961                 g_strfreev(line_argv);
1962                 g_free(serv_path);
1963                 return BLUETOOTH_ERROR_INTERNAL;
1964         }
1965
1966         path = g_strdup_printf("%s/descriptor%d", char_path, desc_id++);
1967         BT_DBG("gatt descriptor path is [%s]", path);
1968
1969         object_id = g_dbus_connection_register_object(g_conn, path,
1970                                 node_info->interfaces[0],
1971                                 &desc_interface_vtable,
1972                                 NULL, NULL, &error);
1973
1974         if (object_id == 0) {
1975                 BT_ERR("failed to register: %s", error->message);
1976                 g_error_free(error);
1977                 g_free(path);
1978                 g_strfreev(line_argv);
1979                 g_free(serv_path);
1980
1981                 return BLUETOOTH_ERROR_INTERNAL;
1982         }
1983
1984         flag_count = bluetooth_gatt_convert_perm2string(permissions, desc_flags);
1985
1986         desc_info = g_new0(struct gatt_desc_info, 1);
1987
1988         desc_info->desc_path = g_strdup(path);
1989         desc_info->desc_id = object_id;
1990         desc_info->desc_uuid = g_strdup(desc_uuid);
1991
1992         for (i = 0; i < flag_count; i++)
1993                 desc_info->desc_flags[i] = desc_flags[i];
1994
1995         desc_info->flags_length = flag_count;
1996
1997         char_info->desc_data = g_slist_append(char_info->desc_data, desc_info);
1998
1999         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
2000         inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
2001
2002         g_variant_builder_add(inner_builder, "{sv}", "UUID",
2003                                 g_variant_new("s", desc_uuid));
2004         g_variant_builder_add(inner_builder, "{sv}", "Characteristic",
2005                                 g_variant_new("o", char_path));
2006
2007         builder2 = g_variant_builder_new(G_VARIANT_TYPE("as"));
2008
2009         for (i = 0; i < flag_count; i++)
2010                 g_variant_builder_add(builder2, "s", desc_flags[i]);
2011
2012         flags_val = g_variant_new("as", builder2);
2013         g_variant_builder_add(inner_builder, "{sv}", "Flags",
2014                                 flags_val);
2015
2016         g_variant_builder_add(builder, "{sa{sv}}",
2017                                 GATT_DESC_INTERFACE,
2018                                 inner_builder);
2019
2020         g_dbus_connection_emit_signal(g_conn, NULL, "/",
2021                                 "org.freedesktop.Dbus.ObjectManager",
2022                                 "InterfacesAdded",
2023                                 g_variant_new("(oa{sa{sv}})",
2024                                 path, builder),
2025                                 &error);
2026
2027         *desc_path = g_strdup(path);
2028
2029         g_free(path);
2030         g_free(serv_path);
2031         g_strfreev(line_argv);
2032         g_variant_builder_unref(inner_builder);
2033         g_variant_builder_unref(builder);
2034
2035         return BLUETOOTH_ERROR_NONE;
2036 }
2037
2038 BT_EXPORT_API int bluetooth_gatt_set_descriptor_value(
2039                         const char *desc_path, const char *desc_value,
2040                         int value_length)
2041 {
2042         GError *error = NULL;
2043         GVariantBuilder *builder = NULL;
2044         GVariantBuilder *inner_builder = NULL;
2045         GVariantBuilder *builder1 = NULL;
2046         struct gatt_desc_info *desc_info = NULL;
2047         gchar **line_argv = NULL;
2048         char *char_path;
2049         GVariant *desc_val = NULL;
2050         char *serv_path = NULL;
2051         int i ;
2052
2053         line_argv = g_strsplit_set(desc_path, "/", 0);
2054         serv_path = g_strdup_printf("/%s/%s/%s", line_argv[1], line_argv[2], line_argv[3]);
2055         char_path = g_strdup_printf("%s/%s", serv_path, line_argv[4]);
2056
2057         desc_info = __bt_gatt_find_gatt_desc_info(serv_path, char_path, desc_path);
2058
2059         /* Free the allocated memory */
2060         g_strfreev(line_argv);
2061         g_free(serv_path);
2062         g_free(char_path);
2063
2064         /* Fix : NULL_RETURNS */
2065         retv_if(desc_info == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
2066
2067         desc_info->desc_value = (char *)malloc(value_length);
2068
2069         /* Fix : NULL_RETURNS */
2070         retv_if(desc_info->desc_value == NULL, BLUETOOTH_ERROR_MEMORY_ALLOCATION);
2071
2072         for (i = 0; i < value_length; i++)
2073                 desc_info->desc_value[i] = desc_value[i];
2074
2075         desc_info->value_length = value_length;
2076
2077         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
2078         inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
2079
2080         builder1 = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
2081
2082         for (i = 0; i < value_length; i++)
2083                 g_variant_builder_add(builder1, "y", desc_value[i]);
2084
2085         desc_val = g_variant_new("ay", builder1);
2086         g_variant_builder_add(inner_builder, "{sv}", "Value", desc_val);
2087
2088         g_variant_builder_add(builder, "{sa{sv}}",
2089                                 GATT_DESC_INTERFACE,
2090                                 inner_builder);
2091
2092         g_dbus_connection_emit_signal(g_conn, NULL, "/",
2093                                 "org.freedesktop.Dbus.ObjectManager",
2094                                 "InterfacesAdded",
2095                                 g_variant_new("(oa{sa{sv}})",
2096                                 desc_info->desc_path, builder),
2097                                 &error);
2098
2099         g_variant_builder_unref(inner_builder);
2100         g_variant_builder_unref(builder);
2101         g_variant_builder_unref(builder1);
2102
2103         return BLUETOOTH_ERROR_NONE;
2104 }
2105
2106 int bluetooth_gatt_get_service(const char *svc_uuid)
2107 {
2108         GDBusProxy *proxy = NULL;
2109         gchar *uuid = NULL;
2110
2111         proxy = __bt_gatt_gdbus_get_manager_proxy("org.bluez",
2112                                         "/org/bluez/hci0", GATT_MNGR_INTERFACE);
2113         if (proxy == NULL)
2114                 return BLUETOOTH_ERROR_INTERNAL;
2115
2116         uuid = g_strdup(svc_uuid);
2117
2118         g_dbus_proxy_call(proxy,
2119                         "GetService",
2120                         g_variant_new("(s)",
2121                         uuid),
2122                         G_DBUS_CALL_FLAGS_NONE, -1,
2123                         NULL,
2124                         (GAsyncReadyCallback) get_service_cb,
2125                         NULL);
2126
2127         g_free(uuid);
2128
2129         return BLUETOOTH_ERROR_NONE;
2130 }
2131
2132 BT_EXPORT_API int bluetooth_gatt_register_service(
2133                         const char *svc_path)
2134 {
2135         if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_GATT_REGISTER_SERVICE)
2136                         == BLUETOOTH_ERROR_PERMISSION_DEINED) {
2137                 BT_ERR("Don't have aprivilege to use this API");
2138                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
2139         }
2140
2141         register_pending_cnt++;
2142
2143         if (__bt_gatt_get_service_state(svc_path)) {
2144                 BT_DBG("service already registered \n");
2145                 return BLUETOOTH_ERROR_NONE;
2146         }
2147
2148         __bt_gatt_set_service_state(svc_path, TRUE);
2149
2150         return BLUETOOTH_ERROR_NONE;
2151 }
2152
2153 BT_EXPORT_API int bluetooth_gatt_register_application(void)
2154 {
2155         GDBusProxy *proxy = NULL;
2156
2157         if (!is_server_started) {
2158
2159                 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_GATT_REGISTER_APPLICATION)
2160                                 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
2161                         BT_ERR("Don't have aprivilege to use this API");
2162                         return BLUETOOTH_ERROR_PERMISSION_DEINED;
2163                 }
2164
2165                 proxy = __bt_gatt_gdbus_get_manager_proxy("org.bluez",
2166                                 "/org/bluez/hci0", GATT_MNGR_INTERFACE);
2167                 if (proxy == NULL || app_path == NULL)
2168                         return BLUETOOTH_ERROR_INTERNAL;
2169
2170                 BT_INFO("RegisterApplication");
2171
2172                 g_dbus_proxy_call(proxy,
2173                                 "RegisterApplication",
2174                                 g_variant_new("(oa{sv})",
2175                                         app_path, NULL),
2176                                 G_DBUS_CALL_FLAGS_NONE, -1,
2177                                 NULL,
2178                                 (GAsyncReadyCallback) register_application_cb,
2179                                 NULL);
2180
2181                 is_server_started = true;
2182
2183                 return BLUETOOTH_ERROR_NONE;
2184         }
2185
2186         BT_INFO("Already RegisterApplication");
2187
2188         return BLUETOOTH_ERROR_NONE;
2189 }
2190
2191 BT_EXPORT_API int bluetooth_gatt_delete_services(void)
2192 {
2193         GSList *l;
2194         int error = BLUETOOTH_ERROR_NONE;
2195         l = gatt_services;
2196
2197         if (l != NULL) {
2198                 for (l = gatt_services; l != NULL; l = l->next) {
2199                         struct gatt_service_info *info = l->data;
2200                         BT_DBG("svc_path is %s", info->serv_path);
2201                         if (bluetooth_gatt_unregister_service(info->serv_path)
2202                                         != BLUETOOTH_ERROR_NONE) {
2203                                 error = BLUETOOTH_ERROR_INTERNAL;
2204                                 BT_DBG(" Error in removing service %s \n",
2205                                                  info->serv_path);
2206                         }
2207                 }
2208                 BT_DBG(" All services removed successfully.\n ");
2209         } else {
2210                 BT_DBG(" There are no registered services.\n ");
2211         }
2212
2213         g_slist_free(gatt_services);
2214         gatt_services = NULL;
2215         serv_id = 1;
2216
2217         if (error != BLUETOOTH_ERROR_NONE)
2218                 return error;
2219
2220         return BLUETOOTH_ERROR_NONE;
2221 }
2222
2223 BT_EXPORT_API int bluetooth_gatt_update_characteristic(
2224                         const char *char_path, const char* char_value,
2225                         int value_length)
2226 {
2227         GVariantBuilder *outer_builder;
2228         GVariantBuilder *inner_builder;
2229         GVariantBuilder *invalidated_builder;
2230         GVariant *update_value = NULL;
2231         GError *error = NULL;
2232         gboolean ret = FALSE;
2233         int err = BLUETOOTH_ERROR_NONE;
2234         int i = 0;
2235         gchar **line_argv = NULL;
2236         gchar *serv_path = NULL;
2237
2238         line_argv = g_strsplit_set(char_path, "/", 0);
2239         serv_path = g_strdup_printf("/%s/%s/%s", line_argv[1], line_argv[2], line_argv[3]);
2240
2241         if (!__bt_gatt_get_service_state(serv_path)) {
2242                 BT_DBG("service not registered for this characteristic \n");
2243                 g_free(serv_path);
2244                 g_strfreev(line_argv);
2245                 return BLUETOOTH_ERROR_INTERNAL;
2246         }
2247
2248         outer_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
2249         invalidated_builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
2250
2251         inner_builder = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
2252         for (i = 0; i < value_length; i++)
2253                 g_variant_builder_add(inner_builder, "y", char_value[i]);
2254
2255         update_value = g_variant_new("ay", inner_builder);
2256
2257         outer_builder = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
2258         g_variant_builder_add(outer_builder, "{sv}", "Value",
2259                                         update_value);
2260
2261         BT_DBG("Updating characteristic value \n");
2262         ret = g_dbus_connection_emit_signal(g_conn, NULL,
2263                                         char_path,
2264                                         "org.freedesktop.DBus.Properties",
2265                                         "PropertiesChanged",
2266                                         g_variant_new("(sa{sv}as)",
2267                                         "org.bluez.GattCharacteristic1",
2268                                         outer_builder, invalidated_builder),
2269                                         &error);
2270
2271         if (!ret) {
2272                 if (error != NULL) {
2273                         BT_ERR("D-Bus API failure: errCode[%x], \
2274                                         message[%s]",
2275                                         error->code, error->message);
2276                         g_clear_error(&error);
2277                 }
2278                 err = BLUETOOTH_ERROR_INTERNAL;
2279         } else {
2280                 struct gatt_char_info *char_info = NULL;
2281
2282                 char_info = __bt_gatt_find_gatt_char_info(serv_path, char_path);
2283                 if (char_info == NULL) {
2284                         g_free(serv_path);
2285                         g_strfreev(line_argv);
2286                         g_variant_builder_unref(inner_builder);
2287                         g_variant_builder_unref(outer_builder);
2288                         g_variant_builder_unref(invalidated_builder);
2289
2290                         return BLUETOOTH_ERROR_INVALID_DATA;
2291                 }
2292
2293                 char_info->value_length = value_length;
2294
2295                 char_info->char_value = (char *)realloc(char_info->char_value, value_length);
2296                 if (char_info->char_value) {
2297                         for (i = 0; i < value_length; i++)
2298                                 char_info->char_value[i] = char_value[i];
2299                 }
2300         }
2301
2302         g_free(serv_path);
2303         g_strfreev(line_argv);
2304         g_variant_builder_unref(inner_builder);
2305         g_variant_builder_unref(outer_builder);
2306         g_variant_builder_unref(invalidated_builder);
2307
2308         return err;
2309 }
2310
2311 static void __bt_gatt_free_descriptor_info(struct gatt_desc_info *desc_info)
2312 {
2313         int i;
2314
2315         if (!desc_info)
2316                 return;
2317
2318         g_free(desc_info->desc_path);
2319         g_free(desc_info->desc_uuid);
2320         g_free(desc_info->desc_value);
2321
2322         for (i = 0; i < desc_info->flags_length; i++)
2323                 g_free(desc_info->desc_flags[i]);
2324
2325         g_free(desc_info);
2326 }
2327
2328 static void __bt_gatt_free_characteristic_info(struct gatt_char_info *char_info)
2329 {
2330         int i;
2331
2332         if (!char_info)
2333                 return;
2334
2335         g_free(char_info->char_path);
2336         g_free(char_info->char_uuid);
2337         g_free(char_info->char_value);
2338
2339         for (i = 0; i < char_info->flags_length; i++)
2340                 g_free(char_info->char_flags[i]);
2341
2342         g_free(char_info);
2343 }
2344
2345 static void __bt_gatt_free_service_info(struct gatt_service_info *svc_info)
2346 {
2347         if (!svc_info)
2348                 return;
2349
2350         g_free(svc_info->serv_path);
2351         g_free(svc_info->service_uuid);
2352         g_free(svc_info);
2353 }
2354
2355 BT_EXPORT_API int bluetooth_gatt_unregister_service(const char *svc_path)
2356 {
2357         GSList *l, *l1;
2358         struct gatt_service_info *svc_info;
2359         gboolean ret;
2360         int err = BLUETOOTH_ERROR_NONE;
2361
2362         BT_DBG("svc_path %s", svc_path);
2363         svc_info = __bt_gatt_find_gatt_service_info(svc_path);
2364
2365         if (!svc_info) {
2366                 BT_ERR("Unable to find service info");
2367                 return BLUETOOTH_ERROR_NOT_FOUND;
2368         }
2369
2370         err = __bt_gatt_unregister_service(svc_path);
2371         if (err != BLUETOOTH_ERROR_NONE) {
2372                 BT_DBG("Could not unregister application");
2373                 return err;
2374         }
2375
2376         for (l = svc_info->char_data; l != NULL; l = l->next) {
2377                 struct gatt_char_info *char_info = l->data;
2378
2379                 if (char_info == NULL)
2380                         break;
2381
2382                 for (l1 = char_info->desc_data; l1 != NULL; l1 = l1->next) {
2383                         struct gatt_desc_info *desc_info = l1->data;
2384
2385                         if (desc_info == NULL)
2386                                 break;
2387
2388                         ret = g_dbus_connection_unregister_object(g_conn,
2389                                                 desc_info->desc_id);
2390                         if (ret) {
2391                                 __bt_gatt_emit_interface_removed(
2392                                                 desc_info->desc_path,
2393                                                 GATT_DESC_INTERFACE);
2394                         } else {
2395                                 err = BLUETOOTH_ERROR_INTERNAL;
2396                         }
2397
2398                         /* list remove & free */
2399                         char_info->desc_data = g_slist_remove(char_info->desc_data, desc_info);
2400                         __bt_gatt_free_descriptor_info(desc_info);
2401                 }
2402
2403                 g_slist_free(char_info->desc_data);
2404                 char_info->desc_data = NULL;
2405
2406                 ret = g_dbus_connection_unregister_object(g_conn,
2407                                         char_info->char_id);
2408                 if (ret) {
2409                         __bt_gatt_emit_interface_removed(char_info->char_path,
2410                                                 GATT_CHAR_INTERFACE);
2411                 } else {
2412                         err = BLUETOOTH_ERROR_INTERNAL;
2413                 }
2414
2415                 /* list remove & free */
2416                 svc_info->char_data = g_slist_remove(svc_info->char_data, char_info);
2417                 __bt_gatt_free_characteristic_info(char_info);
2418         }
2419
2420         g_slist_free(svc_info->char_data);
2421         svc_info->char_data = NULL;
2422
2423         ret = g_dbus_connection_unregister_object(g_conn, svc_info->serv_id);
2424         if (ret) {
2425                 __bt_gatt_emit_interface_removed(svc_info->serv_path,
2426                                                 GATT_SERV_INTERFACE);
2427         } else {
2428                 err = BLUETOOTH_ERROR_INTERNAL;
2429         }
2430
2431         ret = g_dbus_connection_unregister_object(g_conn, svc_info->prop_id);
2432         if (ret)
2433                 BT_DBG("Unregistered the service on properties interface");
2434
2435         /* list remove & free */
2436         gatt_services = g_slist_remove(gatt_services, svc_info);
2437         __bt_gatt_free_service_info(svc_info);
2438
2439         new_service = FALSE;
2440
2441         if (gatt_services == NULL)
2442                 serv_id = 1;
2443         else if (gatt_services->next == NULL)
2444                 serv_id--;
2445
2446         return err;
2447 }
2448
2449 BT_EXPORT_API int bluetooth_gatt_send_response(int request_id, guint req_type,
2450                                         int resp_state, int offset, char *value, int value_length)
2451 {
2452         struct gatt_req_info *req_info = NULL;
2453
2454         if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_GATT_SEND_RESPONSE)
2455                         == BLUETOOTH_ERROR_PERMISSION_DEINED) {
2456                 BT_ERR("Don't have aprivilege to use this API");
2457                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
2458         }
2459
2460         req_info = __bt_gatt_find_request_info(request_id);
2461
2462         if (req_info) {
2463                 if (resp_state != BLUETOOTH_ERROR_NONE) {
2464                         g_dbus_method_invocation_return_dbus_error(req_info->context,
2465                                                 "org.bluez.Error.Failed", "Application Error");
2466
2467                         gatt_requests = g_slist_remove(gatt_requests, req_info);
2468
2469                         req_info->context = NULL;
2470                         if (req_info->attr_path)
2471                                 g_free(req_info->attr_path);
2472                         if (req_info->svc_path)
2473                                 g_free(req_info->svc_path);
2474                         g_free(req_info);
2475
2476                         return BLUETOOTH_ERROR_NONE;
2477                 }
2478                 if (req_type == BLUETOOTH_GATT_ATT_REQUEST_TYPE_READ) {
2479                         int i;
2480                         GVariantBuilder *inner_builder = NULL;
2481                         inner_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
2482                         if (value_length > 0 && value != NULL) {
2483                                 for (i = 0; i < value_length; i++)
2484                                         g_variant_builder_add(inner_builder, "y", value[i]);
2485                         }
2486                         g_dbus_method_invocation_return_value(req_info->context,
2487                                                 g_variant_new("(ay)", inner_builder));
2488                         g_variant_builder_unref(inner_builder);
2489                 } else {
2490                         g_dbus_method_invocation_return_value(req_info->context, NULL);
2491                 }
2492                 gatt_requests = g_slist_remove(gatt_requests, req_info);
2493
2494                 req_info->context = NULL;
2495                 if (req_info->attr_path)
2496                         g_free(req_info->attr_path);
2497                 if (req_info->svc_path)
2498                         g_free(req_info->svc_path);
2499                 g_free(req_info);
2500         } else {
2501                 return BLUETOOTH_ERROR_INTERNAL;
2502         }
2503
2504         return BLUETOOTH_ERROR_NONE;
2505 }
2506
2507 BT_EXPORT_API int bluetooth_gatt_server_set_notification(const char *char_path,
2508                                                 bluetooth_device_address_t *unicast_address)
2509 {
2510         GVariantBuilder *outer_builder;
2511         GVariantBuilder *invalidated_builder;
2512         GError *error = NULL;
2513         gboolean notify = TRUE;
2514         gboolean ret = TRUE;
2515         int err = BLUETOOTH_ERROR_NONE;
2516         gchar **line_argv = NULL;
2517         gchar *serv_path = NULL;
2518         char addr[20] = { 0 };
2519
2520         line_argv = g_strsplit_set(char_path, "/", 0);
2521         serv_path = g_strdup_printf("/%s/%s/%s", line_argv[1], line_argv[2], line_argv[3]);
2522
2523         if (!__bt_gatt_get_service_state(serv_path)) {
2524                 BT_DBG("service not registered for this characteristic \n");
2525                 g_free(serv_path);
2526                 g_strfreev(line_argv);
2527                 return BLUETOOTH_ERROR_INTERNAL;
2528         }
2529
2530         g_free(serv_path);
2531
2532         outer_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
2533         invalidated_builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
2534
2535         g_variant_builder_add(outer_builder, "{sv}", "Notifying",
2536                                         g_variant_new("b", notify));
2537
2538         if (unicast_address) {
2539                 _bt_convert_addr_type_to_string(addr,
2540                                         (unsigned char *)unicast_address->addr);
2541         }
2542         g_variant_builder_add(outer_builder, "{sv}", "Unicast",
2543                                 g_variant_new("s", addr));
2544
2545         BT_DBG("Set characteristic Notification \n");
2546         ret = g_dbus_connection_emit_signal(g_conn, NULL,
2547                                         char_path,
2548                                         "org.freedesktop.DBus.Properties",
2549                                         "PropertiesChanged",
2550                                         g_variant_new("(sa{sv}as)",
2551                                         "org.bluez.GattCharacteristic1",
2552                                         outer_builder, invalidated_builder),
2553                                         &error);
2554
2555         if (!ret) {
2556                 if (error != NULL) {
2557                         BT_ERR("D-Bus API failure: errCode[%x], \
2558                                         message[%s]",
2559                                         error->code, error->message);
2560                         g_clear_error(&error);
2561                 }
2562                 err = BLUETOOTH_ERROR_INTERNAL;
2563         }
2564
2565         g_strfreev(line_argv);
2566         g_variant_builder_unref(outer_builder);
2567         g_variant_builder_unref(invalidated_builder);
2568
2569         return err;
2570 }