Merge "[warnings] Fix build warnings" into tizen
[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 void __bt_gatt_char_method_call(GDBusConnection *connection,
561                                         const gchar *sender,
562                                         const gchar *object_path,
563                                         const gchar *interface_name,
564                                         const gchar *method_name,
565                                         GVariant *parameters,
566                                         GDBusMethodInvocation *invocation,
567                                         gpointer user_data)
568 {
569
570         if (g_strcmp0(method_name, "ReadValue") == 0) {
571                 gchar *addr = NULL;
572                 guint8 req_id = 1;
573                 guint16 offset = 0;
574                 bt_gatt_read_req_t read_req = {0, };
575                 bt_user_info_t *user_info = NULL;
576                 struct gatt_req_info *req_info = NULL;
577                 struct gatt_service_info *svc_info = NULL;
578
579                 BT_DBG("ReadValue");
580
581                 g_variant_get(parameters, "(&syq)", &addr, &req_id, &offset);
582
583                 BT_DBG("Application path = %s", object_path);
584
585                 BT_DBG("Remote Device address number = %s", addr);
586                 BT_DBG("Request id = %d, Offset = %d", req_id, offset);
587
588                 BT_DBG("Sender = %s", sender);
589
590                 read_req.att_handle = g_strdup(object_path);
591                 read_req.address = g_strdup(addr);
592                 read_req.req_id = req_id;
593                 read_req.offset = offset;
594                 svc_info = __bt_gatt_find_gatt_service_from_char(object_path);
595                 if (svc_info != NULL) {
596                         read_req.service_handle = g_strdup(svc_info->serv_path);
597                         user_info = _bt_get_user_data(BT_COMMON);
598 #ifdef HPS_FEATURE
599                         GVariant *param = NULL;
600 #endif
601
602                         /* Store requets information */
603                         req_info = g_new0(struct gatt_req_info, 1);
604                         req_info->attr_path = g_strdup(object_path);
605                         req_info->svc_path = g_strdup(read_req.service_handle);
606                         req_info->request_id= req_id;
607                         req_info->offset = offset;
608                         req_info->context = invocation;
609                         gatt_requests = g_slist_append(gatt_requests, req_info);
610
611                         if (user_info != NULL) {
612                                 _bt_common_event_cb(
613                                         BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED,
614                                         BLUETOOTH_ERROR_NONE, &read_req,
615                                         user_info->cb, user_info->user_data);
616                         }
617 #ifdef HPS_FEATURE
618                         param = g_variant_new("(sssyq)",
619                                         read_req.att_handle,
620                                         read_req.service_handle,
621                                         read_req.address,
622                                         read_req.req_id,
623                                         read_req.offset);
624                         __bt_send_event_to_hps(BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED, param);
625 #endif
626                 }
627
628                 if (read_req.att_handle)
629                         g_free(read_req.att_handle);
630                 if (read_req.address)
631                         g_free(read_req.address);
632                 if (read_req.service_handle)
633                         g_free(read_req.service_handle);
634                 return;
635         } else if (g_strcmp0(method_name, "WriteValue") == 0) {
636                 GVariant *var = NULL;
637                 gchar *addr = NULL;
638                 guint8 req_id = 0;
639                 guint16 offset = 0;
640                 bt_gatt_value_change_t value_change = {0, };
641                 bt_user_info_t *user_info = NULL;
642                 int len = 0;
643                 struct gatt_service_info *svc_info = NULL;
644                 struct gatt_req_info *req_info = NULL;
645 #ifdef HPS_FEATURE
646                 GVariant *param = NULL;
647 #endif
648
649                 BT_DBG("WriteValue");
650                 BT_DBG("Application path = %s", object_path);
651                 BT_DBG("Sender = %s", sender);
652
653                 g_variant_get(parameters, "(&syq@ay)", &addr, &req_id, &offset, &var);
654
655                 value_change.att_handle = g_strdup(object_path);
656                 value_change.address = g_strdup(addr);
657                 svc_info = __bt_gatt_find_gatt_service_from_char(object_path);
658                 if (svc_info == NULL) {
659                         g_variant_unref(var);
660                         g_dbus_method_invocation_return_value(invocation, NULL);
661                         return;
662                 }
663
664                 value_change.service_handle = g_strdup(svc_info->serv_path);
665                 value_change.offset = offset;
666                 value_change.req_id = req_id;
667
668                 len = g_variant_get_size(var);
669                 if (len > 0) {
670                         char *data;
671
672                         value_change.att_value = (guint8 *)malloc(len);
673                         if (!value_change.att_value) {
674                                 BT_ERR("att_value is NULL");
675                                 g_variant_unref(var);
676                                 g_dbus_method_invocation_return_value(invocation, NULL);
677                                 return;
678                         }
679
680                         data = (char *)g_variant_get_data(var);
681                         memcpy(value_change.att_value, data, len);
682                 }
683
684                 value_change.val_len = len;
685
686                 /* Store requets information */
687                 req_info = g_new0(struct gatt_req_info, 1);
688                 req_info->attr_path = g_strdup(object_path);
689                 req_info->svc_path = g_strdup(value_change.service_handle);
690                 req_info->request_id= req_id;
691                 req_info->offset = offset;
692                 req_info->context = invocation;
693                 gatt_requests = g_slist_append(gatt_requests, req_info);
694
695                 user_info = _bt_get_user_data(BT_COMMON);
696                 if (user_info != NULL) {
697                         _bt_common_event_cb(
698                                 BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED,
699                                 BLUETOOTH_ERROR_NONE, &value_change,
700                                 user_info->cb, user_info->user_data);
701                 }
702 #ifdef HPS_FEATURE
703                 if (len > 0) {
704                         gchar *svc_path;
705                         svc_path = g_strdup(svc_info->serv_path);
706                         param = g_variant_new("(sssyq@ay)",
707                                         object_path,
708                                         svc_path,
709                                         addr,
710                                         req_id,
711                                         offset,
712                                         var);
713                         __bt_send_event_to_hps(BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED, param);
714                         if (svc_path)
715                                 g_free(svc_path);
716                 }
717 #endif
718                 g_variant_unref(var);
719                 return;
720         } else if (g_strcmp0(method_name, "StartNotify") == 0) {
721                 bt_user_info_t *user_info = NULL;
722                 bt_gatt_char_notify_change_t notify_change = {0, };
723                 BT_DBG("StartNotify");
724                 user_info = _bt_get_user_data(BT_COMMON);
725                 if (user_info != NULL) {
726                         struct gatt_service_info *svc_info = NULL;
727                         svc_info = __bt_gatt_find_gatt_service_from_char(object_path);
728                         if (svc_info) {
729                                 notify_change.service_handle = g_strdup(svc_info->serv_path);
730                                 notify_change.att_handle = g_strdup(object_path);
731                                 notify_change.att_notify = TRUE;
732                                 _bt_common_event_cb(
733                                         BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_STATE_CHANGED,
734                                         BLUETOOTH_ERROR_NONE, &notify_change,
735                                         user_info->cb, user_info->user_data);
736                         }
737                 }
738         } else if (g_strcmp0(method_name, "StopNotify") == 0) {
739                 bt_user_info_t *user_info = NULL;
740                 bt_gatt_char_notify_change_t notify_change = {0, };
741                 BT_DBG("StopNotify");
742                 user_info = _bt_get_user_data(BT_COMMON);
743                 if (user_info != NULL) {
744                         struct gatt_service_info *svc_info = NULL;
745                         svc_info = __bt_gatt_find_gatt_service_from_char(object_path);
746                         if (svc_info) {
747                                 notify_change.service_handle = g_strdup(svc_info->serv_path);
748                                 notify_change.att_handle = g_strdup(object_path);
749                                 notify_change.att_notify = FALSE;
750                                 _bt_common_event_cb(
751                                         BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_STATE_CHANGED,
752                                         BLUETOOTH_ERROR_NONE, &notify_change,
753                                         user_info->cb, user_info->user_data);
754                         }
755                 }
756         } else if (g_strcmp0(method_name, "IndicateConfirm") == 0) {
757                 gchar *addr = NULL;
758                 bt_gatt_indicate_confirm_t confirm = {0, };
759                 bt_user_info_t *user_info = NULL;
760                 gboolean complete = 0;
761                 struct gatt_service_info *svc_info = NULL;
762
763                 BT_DBG("IndicateConfirm");
764                 BT_DBG("Application path = %s", object_path);
765                 BT_DBG("Sender = %s", sender);
766
767                 g_variant_get(parameters, "(&sb)", &addr, &complete);
768
769                 BT_DBG("Remote Device address number = %s", addr);
770                 confirm.att_handle = g_strdup(object_path);
771                 confirm.address = g_strdup(addr);
772                 confirm.complete = complete;
773
774                 svc_info = __bt_gatt_find_gatt_service_from_char(object_path);
775                 if (svc_info != NULL) {
776                         confirm.service_handle = g_strdup(svc_info->serv_path);
777                         user_info = _bt_get_user_data(BT_COMMON);
778
779                         if (user_info != NULL) {
780                                 _bt_common_event_cb(
781                                         BLUETOOTH_EVENT_GATT_SERVER_INDICATE_CONFIRMED,
782                                         BLUETOOTH_ERROR_NONE, &confirm,
783                                         user_info->cb, user_info->user_data);
784                         }
785                 }
786         }
787         g_dbus_method_invocation_return_value(invocation, NULL);
788 }
789
790 static void __bt_gatt_desc_method_call(GDBusConnection *connection,
791                                         const gchar *sender,
792                                         const gchar *object_path,
793                                         const gchar *interface_name,
794                                         const gchar *method_name,
795                                         GVariant *parameters,
796                                         GDBusMethodInvocation *invocation,
797                                         gpointer user_data)
798 {
799         if (g_strcmp0(method_name, "ReadValue") == 0) {
800                 gchar *addr = NULL;
801                 guint8 req_id = 1;
802                 guint16 offset = 0;
803                 bt_gatt_read_req_t read_req = {0, };
804                 bt_user_info_t *user_info = NULL;
805                 struct gatt_req_info *req_info = NULL;
806                 struct gatt_service_info *svc_info = NULL;
807                 BT_DBG("ReadValue");
808
809                 g_variant_get(parameters, "(&syq)", &addr, &req_id, &offset);
810
811                 BT_DBG("Application path = %s", object_path);
812
813                 BT_DBG("Remote Device address number = %s", addr);
814                 BT_DBG("Request id = %d, Offset = %d", req_id, offset);
815
816                 BT_DBG("Sender = %s", sender);
817
818                 read_req.att_handle = g_strdup(object_path);
819                 read_req.address = g_strdup(addr);
820                 read_req.req_id = req_id;
821                 read_req.offset = offset;
822                 svc_info = __bt_gatt_find_gatt_service_from_desc(object_path);
823                 if (svc_info != NULL) {
824                         read_req.service_handle = g_strdup(svc_info->serv_path);
825                         user_info = _bt_get_user_data(BT_COMMON);
826
827                         /* Store requets information */
828                         req_info = g_new0(struct gatt_req_info, 1);
829                         req_info->attr_path = g_strdup(object_path);
830                         req_info->svc_path = g_strdup(read_req.service_handle);
831                         req_info->request_id= req_id;
832                         req_info->offset = offset;
833                         req_info->context = invocation;
834                         gatt_requests = g_slist_append(gatt_requests, req_info);
835
836                         if (user_info != NULL) {
837                                 _bt_common_event_cb(
838                                         BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED,
839                                         BLUETOOTH_ERROR_NONE, &read_req,
840                                         user_info->cb, user_info->user_data);
841                         }
842                 }
843
844                 if (read_req.att_handle)
845                         g_free(read_req.att_handle);
846                 if (read_req.address)
847                         g_free(read_req.address);
848                 if (read_req.service_handle)
849                         g_free(read_req.service_handle);
850
851                 return;
852         } else if (g_strcmp0(method_name, "WriteValue") == 0) {
853                 GVariant *var = NULL;
854                 gchar *addr = NULL;
855                 guint8 req_id = 0;
856                 guint16 offset = 0;
857                 bt_gatt_value_change_t value_change = {0, };
858                 bt_user_info_t *user_info = NULL;
859                 int len = 0;
860                 struct gatt_service_info *svc_info = NULL;
861                 struct gatt_req_info *req_info = NULL;
862
863                 BT_DBG("WriteValue");
864                 BT_DBG("Application path = %s", object_path);
865                 BT_DBG("Sender = %s", sender);
866
867                 g_variant_get(parameters, "(&syq@ay)", &addr, &req_id, &offset, &var);
868
869                 value_change.att_handle = g_strdup(object_path);
870                 value_change.address = g_strdup(addr);
871                 svc_info = __bt_gatt_find_gatt_service_from_desc(object_path);
872                 if (svc_info == NULL) {
873                         g_variant_unref(var);
874                         g_dbus_method_invocation_return_value(invocation, NULL);
875                         return;
876                 }
877
878                 value_change.service_handle = g_strdup(svc_info->serv_path);
879                 value_change.offset = offset;
880                 value_change.req_id = req_id;
881
882                 len = g_variant_get_size(var);
883                 if (len > 0) {
884                         char *data;
885
886                         value_change.att_value = (guint8 *)malloc(len);
887                         if (!value_change.att_value) {
888                                 BT_ERR("att_value is NULL");
889                                 g_variant_unref(var);
890                                 g_dbus_method_invocation_return_value(invocation, NULL);
891                                 return;
892                         }
893                         data = (char *)g_variant_get_data(var);
894                         memcpy(value_change.att_value, data, len);
895                 }
896                 g_variant_unref(var);
897
898                 value_change.val_len = len;
899
900                 /* Store requets information */
901                 req_info = g_new0(struct gatt_req_info, 1);
902                 req_info->attr_path = g_strdup(object_path);
903                 req_info->svc_path = g_strdup(value_change.service_handle);
904                 req_info->request_id= req_id;
905                 req_info->offset = offset;
906                 req_info->context = invocation;
907                 gatt_requests = g_slist_append(gatt_requests, req_info);
908
909                 user_info = _bt_get_user_data(BT_COMMON);
910                 if (user_info != NULL) {
911                         _bt_common_event_cb(
912                                 BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED,
913                                 BLUETOOTH_ERROR_NONE, &value_change,
914                                 user_info->cb, user_info->user_data);
915                 }
916                 return;
917         }
918         g_dbus_method_invocation_return_value(invocation, NULL);
919 }
920
921 gboolean __bt_gatt_emit_interface_removed(gchar *object_path, gchar *interface)
922 {
923         gboolean ret;
924         GError *error = NULL;
925         GVariantBuilder *array_builder;
926
927         array_builder = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
928         g_variant_builder_init(array_builder, G_VARIANT_TYPE ("as"));
929         g_variant_builder_add(array_builder, "s", interface);
930
931         ret = g_dbus_connection_emit_signal(g_conn, NULL, "/",
932                                         "org.freedesktop.Dbus.Objectmanager",
933                                         "InterfacesRemoved",
934                                         g_variant_new ("(oas)",
935                                         object_path, array_builder),
936                                         &error);
937
938         if (!ret) {
939                 if (error != NULL) {
940                         /* dbus gives error cause */
941                         BT_ERR("d-bus api failure: errcode[%x], message[%s]",
942                                 error->code, error->message);
943                         g_clear_error(&error);
944                 }
945         }
946         g_variant_builder_unref(array_builder);
947
948         return ret;
949 }
950
951 static const GDBusInterfaceVTable desc_interface_vtable = {
952         __bt_gatt_desc_method_call,
953         NULL,
954         NULL,
955 };
956
957 static const GDBusInterfaceVTable char_interface_vtable = {
958         __bt_gatt_char_method_call,
959         NULL,
960         NULL,
961 };
962
963 static const GDBusInterfaceVTable serv_interface_vtable = {
964         __bt_gatt_serv_method_call,
965         NULL,
966         NULL,
967 };
968
969
970 static GDBusNodeInfo *__bt_gatt_create_method_node_info(
971                                 const gchar *introspection_data)
972 {
973         GError *err = NULL;
974         GDBusNodeInfo *node_info = NULL;
975
976         if (introspection_data == NULL)
977                 return NULL;
978
979         node_info = g_dbus_node_info_new_for_xml(introspection_data, &err);
980
981         if (err) {
982                 BT_ERR("Unable to create node: %s", err->message);
983                 g_clear_error(&err);
984         }
985         return node_info;
986 }
987
988 static struct gatt_service_info *__bt_gatt_find_gatt_service_info(
989                         const char *service_path)
990 {
991         GSList *l;
992
993         for (l = gatt_services; l != NULL; l = l->next) {
994                 struct gatt_service_info *info = l->data;
995
996                 if (g_strcmp0(info->serv_path, service_path) == 0)
997                         return info;
998         }
999         BT_ERR("Gatt service not found");
1000         return NULL;
1001 }
1002
1003 static struct gatt_char_info *__bt_gatt_find_gatt_char_info(
1004                         const char *service_path, const char *char_path)
1005 {
1006         GSList *l1, *l2;
1007
1008         for (l1 = gatt_services; l1 != NULL; l1 = l1->next) {
1009                 struct gatt_service_info *serv_info = l1->data;
1010
1011                 if (g_strcmp0(serv_info->serv_path, service_path) == 0) {
1012
1013                         for (l2 = serv_info->char_data; l2 != NULL; l2 = l2->next) {
1014                                 struct gatt_char_info *char_info = l2->data;
1015
1016                                 if (g_strcmp0(char_info->char_path, char_path)
1017                                                         == 0)
1018                                         return char_info;
1019                         }
1020                         BT_ERR("Gatt characteristic not found");
1021                         return NULL;
1022                 }
1023         }
1024         BT_ERR("Gatt service not found");
1025         return NULL;
1026 }
1027
1028 static struct gatt_desc_info *__bt_gatt_find_gatt_desc_info(
1029                         const char *serv_path, const char *char_path,
1030                         const char *desc_path)
1031 {
1032         GSList *l1, *l2, *l3;
1033
1034         for (l1 = gatt_services; l1 != NULL; l1 = l1->next) {
1035                 struct gatt_service_info *serv_info = l1->data;
1036
1037                 if (g_strcmp0(serv_info->serv_path, serv_path) == 0) {
1038                         for (l2 = serv_info->char_data; l2 != NULL; l2 = l2->next) {
1039                                 struct gatt_char_info *char_info = l2->data;
1040
1041                                 if (g_strcmp0(char_info->char_path, char_path)
1042                                                         == 0) {
1043                                         for (l3 = char_info->desc_data; l3 != NULL; l3 = l3->next) {
1044                                                 struct gatt_desc_info *desc_info = l3->data;
1045                                                 if (g_strcmp0(desc_info->desc_path,
1046                                                         desc_path) == 0) {
1047                                                         return desc_info;
1048                                                 }
1049                                         }
1050                                 }
1051                         }
1052                 }
1053         }
1054         BT_ERR("Gatt descriptor not found");
1055         return NULL;
1056 }
1057
1058 static struct gatt_req_info *__bt_gatt_find_request_info(guint request_id)
1059 {
1060         GSList *l;
1061
1062         for (l = gatt_requests; l != NULL; l = l->next) {
1063                 struct gatt_req_info *req_info = l->data;
1064
1065                 if (req_info && req_info->request_id == request_id) {
1066                         return req_info;
1067                 }
1068         }
1069         BT_ERR("Gatt Request not found");
1070         return NULL;
1071 }
1072
1073 static int char_info_cmp(gconstpointer a1, gconstpointer a2)
1074 {
1075         const struct gatt_char_info *attrib1 = a1;
1076         const struct gatt_char_info *attrib2 = a2;
1077
1078         return g_strcmp0(attrib1->char_path, attrib2->char_path);
1079 }
1080
1081 static int desc_info_cmp(gconstpointer a1, gconstpointer a2)
1082 {
1083         const struct gatt_desc_info *attrib1 = a1;
1084         const struct gatt_desc_info *attrib2 = a2;
1085
1086         return g_strcmp0(attrib1->desc_path, attrib2->desc_path);
1087 }
1088
1089 static GDBusProxy *__bt_gatt_gdbus_init_manager_proxy(const gchar *service,
1090                                 const gchar *path, const gchar *interface)
1091 {
1092         GDBusProxy *proxy;
1093         GError *err = NULL;
1094
1095         if (g_conn == NULL)
1096                 g_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM,
1097                                                         NULL, &err);
1098
1099         if (!g_conn) {
1100                 if (err) {
1101                         BT_ERR("Unable to connect to gdbus: %s", err->message);
1102                         g_clear_error(&err);
1103                 }
1104                 return NULL;
1105         }
1106
1107         proxy =  g_dbus_proxy_new_sync(g_conn,
1108                         G_DBUS_PROXY_FLAGS_NONE, NULL,
1109                         service, path,
1110                         interface, NULL, &err);
1111
1112         if (!proxy) {
1113                 if (err) {
1114                         BT_ERR("Unable to create proxy: %s", err->message);
1115                         g_clear_error(&err);
1116                 }
1117                 return NULL;
1118         }
1119         manager_gproxy = proxy;
1120
1121         return proxy;
1122 }
1123
1124 static GDBusProxy *__bt_gatt_gdbus_get_manager_proxy(const gchar *service,
1125                                 const gchar *path, const gchar *interface)
1126 {
1127         return (manager_gproxy) ? manager_gproxy :
1128                         __bt_gatt_gdbus_init_manager_proxy(service,
1129                                 path, interface);
1130 }
1131
1132 int bluetooth_gatt_convert_prop2string(
1133                         bt_gatt_characteristic_property_t properties,
1134                         char *char_properties[])
1135 {
1136         int flag_count = 0;
1137
1138         if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_BROADCAST) {
1139                 char_properties[flag_count] = g_strdup("broadcast");
1140                 flag_count++;
1141         }
1142         if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ) {
1143                 char_properties[flag_count] = g_strdup("read");
1144                 flag_count++;
1145         }
1146         if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE_NO_RESPONSE) {
1147                 char_properties[flag_count] = g_strdup("write-without-response");
1148                 flag_count++;
1149         }
1150         if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE) {
1151                 char_properties[flag_count] = g_strdup("write");
1152                 flag_count++;
1153         }
1154         if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_NOTIFY) {
1155                 char_properties[flag_count] = g_strdup("notify");
1156                 flag_count++;
1157         }
1158         if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_INDICATE) {
1159                 char_properties[flag_count] = g_strdup("indicate");
1160                 flag_count++;
1161         }
1162         if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_SIGNED_WRITE) {
1163                 char_properties[flag_count] = g_strdup("authenticated-signed-writes");
1164                 flag_count++;
1165         }
1166
1167         if (flag_count == 0) {
1168                 char_properties[flag_count] = g_strdup("read");
1169                 flag_count++;
1170         }
1171
1172         return flag_count;
1173 }
1174
1175 static void __bt_gatt_set_service_state(const char *service_path,
1176                         gboolean state)
1177 {
1178         struct gatt_service_info *svc_info = NULL;
1179         svc_info = __bt_gatt_find_gatt_service_info(service_path);
1180
1181         if (svc_info != NULL) {
1182                 BT_DBG("Updating the gatt service register state %d", state);
1183                 svc_info->is_svc_registered = state;
1184                 return;
1185         }
1186
1187         BT_DBG("gatt service not found");
1188 }
1189
1190 static gboolean __bt_gatt_get_service_state(const char *service_path)
1191 {
1192         struct gatt_service_info *svc_info = NULL;
1193
1194         svc_info = __bt_gatt_find_gatt_service_info(service_path);
1195
1196         if (svc_info != NULL) {
1197                 BT_DBG("Return the state of the gatt service %d",
1198                         svc_info->is_svc_registered);
1199                 return svc_info->is_svc_registered;
1200         }
1201
1202         BT_DBG("gatt service info is NULL");
1203         return FALSE;
1204 }
1205
1206 void get_service_cb(GObject *object, GAsyncResult *res, gpointer user_data)
1207 {
1208         GError *error = NULL;
1209         GVariant *result;
1210         GVariantIter *iter = NULL;
1211         const gchar *key = NULL;
1212         GVariant *value = NULL;
1213         const gchar *service = NULL;
1214         const gchar *characteristic = NULL;
1215         const gchar *descriptor = NULL;
1216         int n_char = 1;
1217
1218         BT_DBG(" ");
1219         result = g_dbus_proxy_call_finish(manager_gproxy, res, &error);
1220
1221         if (result == NULL) {
1222                 /* dBUS-RPC is failed */
1223                 BT_ERR("Dbus-RPC is failed\n");
1224
1225                 if (error != NULL) {
1226                 /* dBUS gives error cause */
1227                         BT_ERR("D-Bus API failure: errCode[%x], message[%s]\n",
1228                                                 error->code, error->message);
1229                         g_clear_error(&error);
1230                 }
1231         } else {
1232                 char *char_cmp = NULL;
1233                 g_variant_get (result, "(a{sv})", &iter);
1234                 char_cmp = g_strdup_printf("Characteristic%d", n_char);
1235
1236                 while (g_variant_iter_loop(iter, "{sv}", &key, &value)) {
1237                         if (g_strcmp0(key, "Service") == 0) {
1238                                 service = g_variant_get_string(value, NULL);
1239                                 BT_DBG("Service %s", service);
1240                         } else if (g_strcmp0(key, char_cmp) == 0) {
1241                                 characteristic = g_variant_get_string(value, NULL);
1242                                 g_free(char_cmp);
1243                                 char_cmp = g_strdup_printf("Characteristic%d", ++n_char);
1244                                 BT_DBG("%s", characteristic);
1245                         } else if (g_strcmp0(key, "Descriptor") == 0) {
1246                                 descriptor = g_variant_get_string(value, NULL);
1247                                 BT_DBG("Descriptor %s", descriptor);
1248                         }
1249                 }
1250                 /* TODO: Store the service informationa and
1251                  * Send respponse to CAPI layer. */
1252
1253                 g_variant_unref(result);
1254                 if (char_cmp)
1255                         g_free(char_cmp);
1256         }
1257 }
1258 void register_service_cb(GObject *object, GAsyncResult *res, gpointer user_data)
1259 {
1260         BT_DBG("register_service_cb\n");
1261
1262         GError *error = NULL;
1263         GVariant *result;
1264
1265         register_pending_cnt = 0;
1266
1267         result = g_dbus_proxy_call_finish(manager_gproxy, res, &error);
1268
1269         if (result == NULL) {
1270                 /* dBUS-RPC is failed */
1271                 BT_ERR("Dbus-RPC is failed\n");
1272
1273                 if (error != NULL) {
1274                 /* dBUS gives error cause */
1275                         BT_ERR("D-Bus API failure: errCode[%x], message[%s]\n",
1276                                                 error->code, error->message);
1277                         g_clear_error(&error);
1278                 }
1279         }
1280 }
1281
1282 void unregister_service_cb(GObject *object, GAsyncResult *res,
1283                 gpointer user_data)
1284 {
1285         BT_DBG("unregister_service_cb\n");
1286
1287         GError *error = NULL;
1288         GVariant *result;
1289
1290         result = g_dbus_proxy_call_finish(manager_gproxy, res, &error);
1291
1292         if (result == NULL) {
1293                 /* dBUS-RPC is failed */
1294                 BT_ERR("Dbus-RPC is failed\n");
1295
1296                 if (error != NULL) {
1297                         /* dBUS gives error cause */
1298                         BT_ERR("D-Bus API failure: errCode[%x], message[%s]\n",
1299                                         error->code, error->message);
1300                         g_clear_error(&error);
1301                 }
1302         }
1303 }
1304
1305 static int __bt_gatt_unregister_service(const char *service_path)
1306 {
1307         if (!__bt_gatt_get_service_state(service_path)) {
1308                 BT_DBG("service not registered \n");
1309                 return BLUETOOTH_ERROR_NOT_FOUND;
1310         }
1311
1312         GDBusProxy *proxy = NULL;
1313
1314         proxy = __bt_gatt_gdbus_get_manager_proxy("org.bluez",
1315                                         "/org/bluez/hci0", GATT_MNGR_INTERFACE);
1316
1317         if (proxy == NULL)
1318                 return BLUETOOTH_ERROR_INTERNAL;
1319
1320         /* Async Call to Unregister Service */
1321         g_dbus_proxy_call(proxy,
1322                                 "UnregisterService",
1323                                 g_variant_new("(o)",
1324                                 service_path),
1325                                 G_DBUS_CALL_FLAGS_NONE, -1,
1326                                 NULL,
1327                                 (GAsyncReadyCallback) unregister_service_cb,
1328                                 NULL);
1329
1330         __bt_gatt_set_service_state(service_path, FALSE);
1331
1332         return BLUETOOTH_ERROR_NONE;
1333 }
1334
1335 static GDBusConnection *__bt_gatt_get_gdbus_connection(void)
1336 {
1337         GDBusConnection *local_system_gconn = NULL;
1338         GError *err = NULL;
1339
1340         if (g_conn == NULL) {
1341                 g_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
1342                 if (!g_conn) {
1343                         if (err) {
1344                                 BT_ERR("Unable to connect to dbus: %s", err->message);
1345                                 g_clear_error(&err);
1346                         }
1347                         g_conn = NULL;
1348                 }
1349         } else if (g_dbus_connection_is_closed(g_conn)) {
1350                 local_system_gconn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
1351
1352                 if (!local_system_gconn) {
1353                         BT_ERR("Unable to connect to dbus: %s", err->message);
1354                         g_clear_error(&err);
1355                 }
1356
1357                 g_conn = local_system_gconn;
1358         }
1359
1360         return g_conn;
1361 }
1362
1363 BT_EXPORT_API int bluetooth_gatt_init(void)
1364 {
1365         GDBusConnection *conn;
1366
1367         owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
1368                                 BT_GATT_SERVICE_NAME,
1369                                 G_BUS_NAME_OWNER_FLAGS_NONE,
1370                                 NULL, NULL, NULL, NULL, NULL);
1371
1372         BT_DBG("owner_id is [%d]", owner_id);
1373
1374         serv_id = 1;
1375
1376         conn = __bt_gatt_get_gdbus_connection();
1377         if (!conn) {
1378                 BT_ERR("Unable to get connection");
1379                 return BLUETOOTH_ERROR_INTERNAL;
1380         }
1381
1382         return BLUETOOTH_ERROR_NONE;
1383 }
1384
1385 BT_EXPORT_API int bluetooth_gatt_deinit()
1386 {
1387         /* Unown gdbus bus */
1388         if (owner_id) {
1389
1390                 /* remove/unregister all services */
1391                 BT_DBG("removing all registered gatt service\n");
1392                 bluetooth_gatt_delete_services();
1393
1394                 g_bus_unown_name(owner_id);
1395
1396                 BT_DBG("Gatt service deinitialized \n");
1397
1398                 g_slist_free(gatt_services);
1399                 gatt_services = NULL;
1400
1401                 return BLUETOOTH_ERROR_NONE;
1402         }
1403
1404         return BLUETOOTH_ERROR_NOT_FOUND;
1405 }
1406
1407 BT_EXPORT_API int bluetooth_gatt_add_service(const char *svc_uuid,
1408                         char **svc_path)
1409 {
1410         GError *error = NULL;
1411         guint object_id;
1412         GDBusNodeInfo *node_info;
1413         gchar *path = NULL;
1414         GVariantBuilder *builder = NULL;
1415         GVariantBuilder *builder1 = NULL;
1416         GVariantBuilder *inner_builder = NULL;
1417         gboolean svc_primary = TRUE;
1418         struct gatt_service_info *serv_info = NULL;
1419
1420         node_info = __bt_gatt_create_method_node_info(
1421                                         service_introspection_xml);
1422
1423         if (node_info == NULL)
1424                 return BLUETOOTH_ERROR_INTERNAL;
1425
1426         path = g_strdup_printf(GATT_SERV_OBJECT_PATH"%d", serv_id++);
1427         BT_DBG("gatt service path is [%s]", path);
1428
1429         object_id = g_dbus_connection_register_object(g_conn, path,
1430                                         node_info->interfaces[0],
1431                                         &serv_interface_vtable,
1432                                         NULL, NULL, &error);
1433
1434         if (object_id == 0) {
1435                 BT_ERR("failed to register: %s", error->message);
1436                 g_error_free(error);
1437                 g_free(path);
1438
1439                 return BLUETOOTH_ERROR_INTERNAL;
1440         }
1441
1442         /* Add object_id/gatt service information; it's required at the time of
1443          *  service unregister and Getmanagedobjects
1444          */
1445         serv_info = g_new0(struct gatt_service_info, 1);
1446
1447         serv_info->serv_path = g_strdup(path);
1448         serv_info->serv_id = object_id;
1449         serv_info->service_uuid = g_strdup(svc_uuid);
1450         serv_info->is_svc_registered = FALSE;
1451         serv_info->is_svc_primary = svc_primary;
1452
1453         gatt_services = g_slist_append(gatt_services, serv_info);
1454
1455         /* emit interfacesadded signal here for service path */
1456         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
1457         inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1458
1459         g_variant_builder_add(inner_builder, "{sv}",
1460                 "UUID", g_variant_new_string(svc_uuid));
1461
1462         g_variant_builder_add(inner_builder, "{sv}",
1463                 "Primary", g_variant_new_boolean(svc_primary));
1464
1465         builder1 = g_variant_builder_new(G_VARIANT_TYPE("ao"));
1466
1467         g_variant_builder_add(inner_builder, "{sv}", "Characteristics",
1468                                 g_variant_new("ao", builder1));
1469
1470         g_variant_builder_add(builder, "{sa{sv}}",
1471                 GATT_SERV_INTERFACE, inner_builder);
1472
1473         g_dbus_connection_emit_signal(g_conn, NULL, "/",
1474                                 "org.freedesktop.Dbus.ObjectManager",
1475                                 "InterfacesAdded",
1476                                 g_variant_new("(oa{sa{sv}})",
1477                                 path, builder),
1478                                 &error);
1479
1480         new_service = TRUE;
1481
1482         *svc_path = g_strdup(path);
1483
1484         g_free(path);
1485         g_variant_builder_unref(inner_builder);
1486         g_variant_builder_unref(builder);
1487         g_variant_builder_unref(builder1);
1488
1489         return BLUETOOTH_ERROR_NONE;
1490 }
1491
1492 BT_EXPORT_API int bluetooth_gatt_add_new_characteristic(
1493                         const char *svc_path, const char *char_uuid,
1494                         bt_gatt_characteristic_property_t properties,
1495                         char **char_path)
1496 {
1497         static int char_id;
1498         GError *error = NULL;
1499         guint object_id;
1500         GDBusNodeInfo *node_info;
1501         gchar *path = NULL;
1502         GVariantBuilder *builder = NULL;
1503         GVariantBuilder *inner_builder = NULL;
1504         struct gatt_service_info *serv_info = NULL;
1505         struct gatt_char_info *char_info = NULL;
1506         GVariantBuilder *builder2 = NULL;
1507         GVariantBuilder *builder3 = NULL;
1508         GVariant *flags_val = NULL;
1509         int i = 0;
1510         char *char_flags[NUMBER_OF_FLAGS];
1511         int flag_count = 0;
1512
1513         if (new_service) {
1514                 char_id = 1;
1515                 new_service = FALSE;
1516         }
1517
1518         BT_DBG("gatt svc_path path is [%s]", svc_path);
1519         serv_info = __bt_gatt_find_gatt_service_info(svc_path);
1520         if (serv_info == NULL)
1521                 return BLUETOOTH_ERROR_INVALID_PARAM;
1522
1523         node_info = __bt_gatt_create_method_node_info(
1524                                         characteristics_introspection_xml);
1525
1526         if (node_info == NULL)
1527                 return BLUETOOTH_ERROR_INTERNAL;
1528
1529         path = g_strdup_printf("%s/characteristic%d", svc_path, char_id++);
1530         BT_DBG("gatt characteristic path is [%s]", path);
1531
1532         object_id = g_dbus_connection_register_object(g_conn, path,
1533                                         node_info->interfaces[0],
1534                                         &char_interface_vtable,
1535                                         NULL, NULL, &error);
1536
1537         if (object_id == 0) {
1538                 BT_ERR("failed to register: %s", error->message);
1539                 g_error_free(error);
1540                 g_free(path);
1541
1542                 return BLUETOOTH_ERROR_INTERNAL;
1543         }
1544
1545         flag_count = bluetooth_gatt_convert_prop2string(properties, char_flags);
1546
1547         char_info = g_new0(struct gatt_char_info, 1);
1548
1549         char_info->char_path = g_strdup(path);
1550         char_info->char_id = object_id;
1551         char_info->char_uuid = g_strdup(char_uuid);
1552
1553         for (i = 0; i < flag_count; i++) {
1554                 char_info->char_flags[i] = char_flags[i];
1555                 }
1556
1557         char_info->flags_length = flag_count;
1558
1559         serv_info->char_data = g_slist_append(serv_info->char_data, char_info);
1560
1561         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
1562         inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1563
1564         g_variant_builder_add(inner_builder, "{sv}", "UUID",
1565                                 g_variant_new("s", char_uuid));
1566         g_variant_builder_add(inner_builder, "{sv}", "Service",
1567                                 g_variant_new("o", svc_path));
1568
1569         builder2 = g_variant_builder_new(G_VARIANT_TYPE("as"));
1570
1571         for (i = 0; i < flag_count; i++) {
1572                 g_variant_builder_add(builder2, "s", char_flags[i]);
1573         }
1574
1575         flags_val = g_variant_new("as", builder2);
1576         g_variant_builder_add(inner_builder, "{sv}", "Flags",
1577                                 flags_val);
1578
1579         builder3 = g_variant_builder_new(G_VARIANT_TYPE("ao"));
1580
1581         g_variant_builder_add(inner_builder, "{sv}", "Descriptors",
1582                                 g_variant_new("ao", builder3));
1583
1584         g_variant_builder_add(builder, "{sa{sv}}",
1585                                 GATT_CHAR_INTERFACE,
1586                                 inner_builder);
1587
1588         g_dbus_connection_emit_signal(g_conn, NULL, "/",
1589                                 "org.freedesktop.Dbus.ObjectManager",
1590                                 "InterfacesAdded",
1591                                 g_variant_new("(oa{sa{sv}})",
1592                                 path, builder),
1593                                 &error);
1594
1595         *char_path = g_strdup(path);
1596
1597         new_char = TRUE;
1598
1599         g_free(path);
1600
1601         g_variant_builder_unref(inner_builder);
1602         g_variant_builder_unref(builder);
1603         g_variant_builder_unref(builder2);
1604         g_variant_builder_unref(builder3);
1605
1606         return BLUETOOTH_ERROR_NONE;
1607 }
1608
1609 BT_EXPORT_API int bluetooth_gatt_set_characteristic_value(
1610                         const char *characteristic, const char *char_value,
1611                         int     value_length)
1612 {
1613         gchar **line_argv = NULL;
1614         char *serv_path = NULL;
1615         struct gatt_char_info *char_info = NULL;
1616         GVariantBuilder *builder1 = NULL;
1617         GVariantBuilder *builder = NULL;
1618         GVariantBuilder *inner_builder = NULL;
1619         GVariant *char_val = NULL;
1620         GError *error = NULL;
1621         int i = 0;
1622         int res = BLUETOOTH_ERROR_NONE;
1623
1624         line_argv = g_strsplit_set(characteristic, "/", 0);
1625         serv_path = g_strdup_printf("/%s", line_argv[1]);
1626
1627         char_info = __bt_gatt_find_gatt_char_info(serv_path, characteristic);
1628
1629         if (char_info == NULL) {
1630                 /* Fix : RESOURCE_LEAK */
1631                 res = BLUETOOTH_ERROR_INVALID_PARAM;
1632                 goto done;
1633         }
1634
1635         char_info->value_length = value_length;
1636
1637         char_info->char_value = (char *)malloc(value_length);
1638         /* Fix : NULL_RETURNS */
1639         if (char_info->char_value == NULL) {
1640                 res = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1641                 goto done;
1642         }
1643
1644         for (i = 0; i < value_length; i++)
1645                 char_info->char_value[i] = char_value[i];
1646
1647         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
1648         inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1649
1650         builder1 = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
1651
1652         for (i = 0; i < value_length; i++) {
1653                 g_variant_builder_add(builder1, "y", char_value[i]);
1654         }
1655
1656         char_val = g_variant_new("ay", builder1);
1657                 g_variant_builder_add(inner_builder, "{sv}", "Value", char_val);
1658
1659         g_variant_builder_add(builder, "{sa{sv}}",
1660                         GATT_CHAR_INTERFACE,
1661                         inner_builder);
1662
1663         g_dbus_connection_emit_signal(g_conn, NULL, "/",
1664                         "org.freedesktop.Dbus.ObjectManager",
1665                         "InterfacesAdded",
1666                         g_variant_new("(oa{sa{sv}})",
1667                         char_info->char_path, builder),
1668                         &error);
1669
1670         g_variant_builder_unref(inner_builder);
1671         g_variant_builder_unref(builder);
1672         g_variant_builder_unref(builder1);
1673 done:
1674         g_strfreev(line_argv);
1675         g_free(serv_path);
1676
1677         return res;
1678 }
1679
1680 BT_EXPORT_API int bluetooth_gatt_add_descriptor(
1681                         const char *char_path, const char *desc_uuid,
1682                         char **desc_path)
1683 {
1684         static int desc_id = 1;
1685         GError *error = NULL;
1686         guint object_id;
1687         GDBusNodeInfo *node_info;
1688         gchar *path = NULL;
1689         GVariantBuilder *builder = NULL;
1690         GVariantBuilder *inner_builder = NULL;
1691         struct gatt_char_info *char_info = NULL;
1692         struct gatt_desc_info *desc_info = NULL;
1693         gchar **line_argv = NULL;
1694         char *serv_path;
1695
1696         if (new_char) {
1697                 desc_id = 1;
1698                 new_char = FALSE;
1699         }
1700
1701         line_argv = g_strsplit_set(char_path, "/", 0);
1702         serv_path = g_strdup_printf("/%s", line_argv[1]);
1703
1704         char_info = __bt_gatt_find_gatt_char_info(serv_path, char_path);
1705         if (char_info == NULL) {
1706                 g_strfreev(line_argv);
1707                 return BLUETOOTH_ERROR_INVALID_PARAM;
1708         }
1709
1710         node_info = __bt_gatt_create_method_node_info(
1711                                         descriptor_introspection_xml);
1712
1713         if (node_info == NULL) {
1714                 g_strfreev(line_argv);
1715                 return BLUETOOTH_ERROR_INTERNAL;
1716         }
1717
1718         path = g_strdup_printf("%s/descriptor%d", char_path, desc_id++);
1719         BT_DBG("gatt descriptor path is [%s]", path);
1720
1721         object_id = g_dbus_connection_register_object(g_conn, path,
1722                                 node_info->interfaces[0],
1723                                 &desc_interface_vtable,
1724                                 NULL, NULL, &error);
1725
1726         if (object_id == 0) {
1727                 BT_ERR("failed to register: %s", error->message);
1728                 g_error_free(error);
1729                 g_free(path);
1730                 g_strfreev(line_argv);
1731
1732                 return BLUETOOTH_ERROR_INTERNAL;
1733         }
1734
1735         desc_info = g_new0(struct gatt_desc_info, 1);
1736
1737         desc_info->desc_path = g_strdup(path);
1738         desc_info->desc_id = object_id;
1739         desc_info->desc_uuid = g_strdup(desc_uuid);
1740
1741         char_info->desc_data = g_slist_append(char_info->desc_data, desc_info);
1742
1743         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
1744         inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1745
1746         g_variant_builder_add(inner_builder, "{sv}", "UUID",
1747                                 g_variant_new("s", desc_uuid));
1748         g_variant_builder_add(inner_builder, "{sv}", "Characteristic",
1749                                 g_variant_new("o", char_path));
1750
1751         g_variant_builder_add(builder, "{sa{sv}}",
1752                                 GATT_DESC_INTERFACE,
1753                                 inner_builder);
1754
1755         g_dbus_connection_emit_signal(g_conn, NULL, "/",
1756                                 "org.freedesktop.Dbus.ObjectManager",
1757                                 "InterfacesAdded",
1758                                 g_variant_new("(oa{sa{sv}})",
1759                                 path, builder),
1760                                 &error);
1761
1762         *desc_path = g_strdup(path);
1763
1764         g_free(path);
1765         g_strfreev(line_argv);
1766         g_variant_builder_unref(inner_builder);
1767         g_variant_builder_unref(builder);
1768
1769         return BLUETOOTH_ERROR_NONE;
1770 }
1771
1772 BT_EXPORT_API int bluetooth_gatt_set_descriptor_value(
1773                         const char *desc_path, const char *desc_value,
1774                         int value_length)
1775 {
1776         GError *error = NULL;
1777         GVariantBuilder *builder = NULL;
1778         GVariantBuilder *inner_builder = NULL;
1779         GVariantBuilder *builder1 = NULL;
1780         struct gatt_desc_info *desc_info = NULL;
1781         gchar **line_argv = NULL;
1782         char *char_path;
1783         GVariant *desc_val = NULL;
1784         char *serv_path = NULL;
1785         int i ;
1786
1787         line_argv = g_strsplit_set(desc_path, "/", 0);
1788         serv_path = g_strdup_printf("/%s", line_argv[1]);
1789         char_path = g_strdup_printf("%s/%s", serv_path, line_argv[2]);
1790
1791         desc_info = __bt_gatt_find_gatt_desc_info(serv_path, char_path, desc_path);
1792
1793         /* Free the allocated memory */
1794         g_strfreev(line_argv);
1795         g_free(serv_path);
1796         g_free(char_path);
1797
1798         /* Fix : NULL_RETURNS */
1799         retv_if(desc_info == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1800
1801         desc_info->desc_value = (char *)malloc(value_length);
1802
1803         /* Fix : NULL_RETURNS */
1804         retv_if(desc_info->desc_value == NULL, BLUETOOTH_ERROR_MEMORY_ALLOCATION);
1805
1806         for (i = 0; i < value_length; i++)
1807                 desc_info->desc_value[i] = desc_value[i];
1808
1809         desc_info->value_length = value_length;
1810
1811         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
1812         inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1813
1814         builder1 = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
1815
1816         for (i = 0; i < value_length; i++) {
1817                 g_variant_builder_add(builder1, "y", desc_value[i]);
1818         }
1819         desc_val = g_variant_new("ay", builder1);
1820         g_variant_builder_add(inner_builder, "{sv}", "Value", desc_val);
1821
1822         g_variant_builder_add(builder, "{sa{sv}}",
1823                                 GATT_DESC_INTERFACE,
1824                                 inner_builder);
1825
1826         g_dbus_connection_emit_signal(g_conn, NULL, "/",
1827                                 "org.freedesktop.Dbus.ObjectManager",
1828                                 "InterfacesAdded",
1829                                 g_variant_new("(oa{sa{sv}})",
1830                                 desc_info->desc_path, builder),
1831                                 &error);
1832
1833         g_variant_builder_unref(inner_builder);
1834         g_variant_builder_unref(builder);
1835         g_variant_builder_unref(builder1);
1836
1837         return BLUETOOTH_ERROR_NONE;
1838 }
1839
1840 int bluetooth_gatt_get_service(const char *svc_uuid)
1841 {
1842         GDBusProxy *proxy = NULL;
1843         gchar *uuid = NULL;
1844
1845         proxy = __bt_gatt_gdbus_get_manager_proxy("org.bluez",
1846                                         "/org/bluez/hci0", GATT_MNGR_INTERFACE);
1847         if (proxy == NULL)
1848                 return BLUETOOTH_ERROR_INTERNAL;
1849
1850         uuid = g_strdup(svc_uuid);
1851
1852         g_dbus_proxy_call(proxy,
1853                         "GetService",
1854                         g_variant_new("(s)",
1855                         uuid),
1856                         G_DBUS_CALL_FLAGS_NONE, -1,
1857                         NULL,
1858                         (GAsyncReadyCallback) get_service_cb,
1859                         NULL);
1860
1861         g_free(uuid);
1862
1863         return BLUETOOTH_ERROR_NONE;
1864 }
1865
1866 BT_EXPORT_API int bluetooth_gatt_register_service(
1867                         const char *svc_path)
1868 {
1869         GDBusProxy *proxy = NULL;
1870         gchar *path = NULL;
1871
1872         register_pending_cnt++;
1873
1874         if (__bt_gatt_get_service_state(svc_path)) {
1875                 BT_DBG("service already registered \n");
1876                 return BLUETOOTH_ERROR_NONE;
1877         }
1878
1879         proxy = __bt_gatt_gdbus_get_manager_proxy("org.bluez",
1880                                         "/org/bluez/hci0", GATT_MNGR_INTERFACE);
1881         if (proxy == NULL)
1882                 return BLUETOOTH_ERROR_INTERNAL;
1883
1884         path = g_strdup(svc_path);
1885
1886         g_dbus_proxy_call(proxy,
1887                         "RegisterService",
1888                         g_variant_new("(oa{sv})",
1889                         path, NULL),
1890                         G_DBUS_CALL_FLAGS_NONE, -1,
1891                         NULL,
1892                         (GAsyncReadyCallback) register_service_cb,
1893                         NULL);
1894
1895         __bt_gatt_set_service_state(svc_path, TRUE);
1896
1897         g_free(path);
1898
1899         return BLUETOOTH_ERROR_NONE;
1900 }
1901
1902 BT_EXPORT_API int bluetooth_gatt_delete_services(void)
1903 {
1904         GSList *l;
1905         int error = BLUETOOTH_ERROR_NONE;
1906         l = gatt_services;
1907
1908         if (l != NULL) {
1909                 for (l = gatt_services; l != NULL; l = l->next) {
1910                         struct gatt_service_info *info = l->data;
1911                         BT_DBG("svc_path is %s", info->serv_path);
1912                         if (bluetooth_gatt_unregister_service(info->serv_path)
1913                                         != BLUETOOTH_ERROR_NONE) {
1914                                 error = BLUETOOTH_ERROR_INTERNAL;
1915                                 BT_DBG(" Error in removing service %s \n",
1916                                                  info->serv_path);
1917                         }
1918                 }
1919                 BT_DBG(" All services removed successfully.\n ");
1920         }
1921         else {
1922                 BT_DBG(" There are no registered services.\n ");
1923         }
1924
1925         g_slist_free(gatt_services);
1926         gatt_services = NULL;
1927         serv_id = 1;
1928
1929         if (error != BLUETOOTH_ERROR_NONE)
1930                 return error;
1931
1932         return BLUETOOTH_ERROR_NONE;
1933 }
1934
1935 BT_EXPORT_API int bluetooth_gatt_update_characteristic(
1936                         const char *char_path, const char* char_value,
1937                         int value_length)
1938 {
1939         GVariantBuilder *outer_builder;
1940         GVariantBuilder *inner_builder;
1941         GVariantBuilder *invalidated_builder;
1942         GVariant *update_value = NULL;
1943         GError *error = NULL;
1944         gboolean ret = FALSE;
1945         int err = BLUETOOTH_ERROR_NONE;
1946         int i = 0;
1947         gchar **line_argv = NULL;
1948         gchar *serv_path = NULL;
1949
1950         line_argv = g_strsplit_set(char_path, "/", 0);
1951         serv_path = g_strdup_printf("/%s", line_argv[1]);
1952
1953         if (!__bt_gatt_get_service_state(serv_path)) {
1954                 BT_DBG("service not registered for this characteristic \n");
1955                 g_strfreev(line_argv);
1956                 return BLUETOOTH_ERROR_INTERNAL;
1957         }
1958
1959         outer_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1960         invalidated_builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1961
1962         inner_builder = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
1963         for (i = 0; i < value_length; i++) {
1964                 g_variant_builder_add(inner_builder, "y", char_value[i]);
1965         }
1966
1967         update_value = g_variant_new("ay", inner_builder);
1968
1969         outer_builder = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
1970         g_variant_builder_add(outer_builder, "{sv}", "Value",
1971                                         update_value);
1972
1973         BT_DBG("Updating characteristic value \n");
1974         ret = g_dbus_connection_emit_signal(g_conn, NULL,
1975                                         char_path,
1976                                         "org.freedesktop.DBus.Properties",
1977                                         "PropertiesChanged",
1978                                         g_variant_new("(sa{sv}as)",
1979                                         "org.bluez.GattCharacteristic1",
1980                                         outer_builder, invalidated_builder),
1981                                         &error);
1982
1983         if (!ret) {
1984                 if (error != NULL) {
1985                         BT_ERR("D-Bus API failure: errCode[%x], \
1986                                         message[%s]",
1987                                         error->code, error->message);
1988                         g_clear_error(&error);
1989                 }
1990                 err = BLUETOOTH_ERROR_INTERNAL;
1991         } else {
1992                 struct gatt_char_info *char_info = NULL;
1993
1994                 char_info = __bt_gatt_find_gatt_char_info(serv_path, char_path);
1995                 if (char_info == NULL) {
1996                         return BLUETOOTH_ERROR_INVALID_DATA;
1997                 }
1998
1999                 char_info->value_length = value_length;
2000
2001                 char_info->char_value = (char *)realloc(char_info->char_value, value_length);
2002                 if (char_info->char_value) {
2003                         for (i = 0; i < value_length; i++) {
2004                                 char_info->char_value[i] = char_value[i];
2005                         }
2006                 }
2007         }
2008
2009         g_strfreev(line_argv);
2010         g_variant_builder_unref(inner_builder);
2011         g_variant_builder_unref(outer_builder);
2012         g_variant_builder_unref(invalidated_builder);
2013
2014         return err;
2015 }
2016
2017 BT_EXPORT_API int bluetooth_gatt_unregister_service(const char *svc_path)
2018 {
2019         GSList *l, *l1;
2020         struct gatt_service_info *svc_info;
2021         gboolean ret;
2022         int err = BLUETOOTH_ERROR_NONE;
2023         GSList *tmp;
2024
2025         BT_DBG("svc_path %s", svc_path);
2026         svc_info = __bt_gatt_find_gatt_service_info(svc_path);
2027
2028         if (!svc_info) {
2029                 BT_ERR("Unable to find service info");
2030                 return BLUETOOTH_ERROR_NOT_FOUND;
2031         }
2032
2033         err = __bt_gatt_unregister_service(svc_path);
2034         if (err != BLUETOOTH_ERROR_NONE) {
2035                 BT_DBG("Could not unregister application");
2036                 return err;
2037         }
2038
2039         for (l = svc_info->char_data; l != NULL; l = l->next) {
2040                 struct gatt_char_info *char_info = l->data;
2041
2042                 for (l1 = char_info->desc_data; l1 != NULL; l1 = l1->next) {
2043                         struct gatt_desc_info *desc_info = l1->data;
2044
2045                         ret = g_dbus_connection_unregister_object(g_conn,
2046                                                 desc_info->desc_id);
2047                         if (ret) {
2048                                 __bt_gatt_emit_interface_removed(
2049                                                 desc_info->desc_path,
2050                                                 GATT_DESC_INTERFACE);
2051                         } else {
2052                                 err = BLUETOOTH_ERROR_INTERNAL;
2053                         }
2054                 }
2055                 ret = g_dbus_connection_unregister_object(g_conn,
2056                                         char_info->char_id);
2057                 if (ret) {
2058                         __bt_gatt_emit_interface_removed(char_info->char_path,
2059                                                 GATT_CHAR_INTERFACE);
2060                 } else {
2061                         err = BLUETOOTH_ERROR_INTERNAL;
2062                 }
2063         }
2064         ret = g_dbus_connection_unregister_object(g_conn, svc_info->serv_id);
2065         if (ret) {
2066                 __bt_gatt_emit_interface_removed(svc_info->serv_path,
2067                                                 GATT_SERV_INTERFACE);
2068         } else {
2069                 err = BLUETOOTH_ERROR_INTERNAL;
2070         }
2071
2072         ret = g_dbus_connection_unregister_object(g_conn, svc_info->prop_id);
2073         if (ret) {
2074                 BT_DBG("Unregistered the service on properties interface");
2075         }
2076
2077         for (tmp = gatt_services; tmp != NULL; tmp = tmp->next) {
2078                 struct gatt_service_info *info = tmp->data;
2079
2080                 if (g_strcmp0(info->serv_path, svc_path) == 0) {
2081                         gatt_services = g_slist_delete_link(gatt_services, tmp->data);
2082                 }
2083         }
2084
2085         new_service = FALSE;
2086
2087         if (gatt_services->next == NULL)
2088                 serv_id--;
2089
2090         return err;
2091 }
2092
2093 BT_EXPORT_API int bluetooth_gatt_send_response(int request_id, guint req_type,
2094                                         int resp_state, int offset, char *value, int value_length)
2095 {
2096         struct gatt_req_info *req_info = NULL;
2097
2098         req_info = __bt_gatt_find_request_info(request_id);
2099
2100         if (req_info) {
2101                 if (resp_state != BLUETOOTH_ERROR_NONE) {
2102
2103                         GQuark quark = g_quark_from_string("gatt-server");
2104                         GError *err = g_error_new(quark, 0, "Application Error");
2105                         g_dbus_method_invocation_return_gerror(req_info->context, err);
2106                         g_error_free(err);
2107
2108                         gatt_requests = g_slist_remove(gatt_requests, req_info);
2109
2110                         req_info->context = NULL;
2111                         if (req_info->attr_path)
2112                                 g_free(req_info->attr_path);
2113                         if (req_info->svc_path)
2114                                 g_free(req_info->svc_path);
2115                         g_free(req_info);
2116
2117                         return BLUETOOTH_ERROR_NONE;
2118                 }
2119                 if (req_type == BLUETOOTH_GATT_ATT_REQUEST_TYPE_READ) {
2120                         int i;
2121                         GVariantBuilder *inner_builder = NULL;
2122                         inner_builder = g_variant_builder_new(G_VARIANT_TYPE ("ay"));
2123                         if (value_length > 0 && value != NULL) {
2124                                 for (i = 0; i < value_length; i++)
2125                                         g_variant_builder_add(inner_builder, "y", value[i]);
2126                         }
2127                         g_dbus_method_invocation_return_value(req_info->context,
2128                                                 g_variant_new("(ay)", inner_builder));
2129                         g_variant_builder_unref(inner_builder);
2130                 } else {
2131                         g_dbus_method_invocation_return_value(req_info->context, NULL);
2132                 }
2133                 gatt_requests = g_slist_remove(gatt_requests, req_info);
2134
2135                 req_info->context = NULL;
2136                 if (req_info->attr_path)
2137                         g_free(req_info->attr_path);
2138                 if (req_info->svc_path)
2139                         g_free(req_info->svc_path);
2140                 g_free(req_info);
2141         } else {
2142                 return BLUETOOTH_ERROR_INTERNAL;
2143         }
2144
2145         return BLUETOOTH_ERROR_NONE;
2146 }
2147
2148 BT_EXPORT_API int bluetooth_gatt_server_set_notification(const char *char_path,
2149                                                 bluetooth_device_address_t *unicast_address)
2150 {
2151         GVariantBuilder *outer_builder;
2152         GVariantBuilder *invalidated_builder;
2153         GError *error = NULL;
2154         gboolean notify = TRUE;
2155         gboolean ret = TRUE;
2156         int err = BLUETOOTH_ERROR_NONE;
2157         gchar **line_argv = NULL;
2158         gchar *serv_path = NULL;
2159         char addr[20] = { 0 };
2160
2161         line_argv = g_strsplit_set(char_path, "/", 0);
2162         serv_path = g_strdup_printf("/%s", line_argv[1]);
2163
2164         if (!__bt_gatt_get_service_state(serv_path)) {
2165                 BT_DBG("service not registered for this characteristic \n");
2166                 g_strfreev(line_argv);
2167                 return BLUETOOTH_ERROR_INTERNAL;
2168         }
2169
2170         outer_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
2171         invalidated_builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
2172
2173         g_variant_builder_add(outer_builder, "{sv}", "Notifying",
2174                                         g_variant_new("b", notify));
2175
2176         if (unicast_address) {
2177                 _bt_convert_addr_type_to_string(addr,
2178                                         (unsigned char *)unicast_address->addr);
2179         }
2180         g_variant_builder_add(outer_builder, "{sv}", "Unicast",
2181                                 g_variant_new("s", addr));
2182
2183         BT_DBG("Set characteristic Notification \n");
2184         ret = g_dbus_connection_emit_signal(g_conn, NULL,
2185                                         char_path,
2186                                         "org.freedesktop.DBus.Properties",
2187                                         "PropertiesChanged",
2188                                         g_variant_new("(sa{sv}as)",
2189                                         "org.bluez.GattCharacteristic1",
2190                                         outer_builder, invalidated_builder),
2191                                         &error);
2192
2193         if (!ret) {
2194                 if (error != NULL) {
2195                         BT_ERR("D-Bus API failure: errCode[%x], \
2196                                         message[%s]",
2197                                         error->code, error->message);
2198                         g_clear_error(&error);
2199                 }
2200                 err = BLUETOOTH_ERROR_INTERNAL;
2201         }
2202
2203         g_strfreev(line_argv);
2204         g_variant_builder_unref(outer_builder);
2205         g_variant_builder_unref(invalidated_builder);
2206
2207         return err;
2208 }