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