cleanup of acquire notification data for stop notify
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-gatt-service.c
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *              http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include<gio/gio.h>
19 #include<glib.h>
20 #include<glib/gprintf.h>
21 #include<stdlib.h>
22 #include<unistd.h>
23 #include<stdint.h>
24 #include<stdbool.h>
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <gio/gunixfdlist.h>
28
29 #include "bt-common.h"
30 /* TODO_40 : 4.0 merge - Need to check why includes bt-event-handler.h */
31 #include "bt-event-handler.h"
32 #include "bt-internal-types.h"
33
34
35 #include "bluetooth-gatt-server-api.h"
36 #include "bt-request-sender.h"
37 #define BT_GATT_ATT_UUID_LEN_MAX 50
38 #define BT_GATT_SERVER_DBUS_NAME_LEN_MAX 50
39
40 static GSList *gatt_characteristic_server_notify_list = NULL;;
41
42 /* Common defintions to follow , applicable for both
43    GATT_DIRECT and RELAY */
44
45
46 #define NUMBER_OF_FLAGS 10
47
48
49 int bluetooth_gatt_convert_prop2string(
50                         bt_gatt_characteristic_property_t properties,
51                         char *char_properties[])
52 {
53         int flag_count = 0;
54
55         if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_BROADCAST) {
56                 char_properties[flag_count] = g_strdup("broadcast");
57                 flag_count++;
58         }
59         if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ) {
60                 char_properties[flag_count] = g_strdup("read");
61                 flag_count++;
62         }
63         if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE_NO_RESPONSE) {
64                 char_properties[flag_count] = g_strdup("write-without-response");
65                 flag_count++;
66         }
67         if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE) {
68                 char_properties[flag_count] = g_strdup("write");
69                 flag_count++;
70         }
71         if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_NOTIFY) {
72                 char_properties[flag_count] = g_strdup("notify");
73                 flag_count++;
74         }
75         if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_INDICATE) {
76                 char_properties[flag_count] = g_strdup("indicate");
77                 flag_count++;
78         }
79         if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_SIGNED_WRITE) {
80                 char_properties[flag_count] = g_strdup("authenticated-signed-writes");
81                 flag_count++;
82         }
83         if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_RELIABLE_WRITE) {
84                 char_properties[flag_count] = g_strdup("reliable-write");
85                 flag_count++;
86         }
87         if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITABLE_AUXILIARIES) {
88                 char_properties[flag_count] = g_strdup("writable-auxiliaries");
89                 flag_count++;
90         }
91         if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_READ) {
92                 char_properties[flag_count] = g_strdup("encrypt-read");
93                 flag_count++;
94         }
95         if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_WRITE) {
96                 char_properties[flag_count] = g_strdup("encrypt-write");
97                 flag_count++;
98         }
99         if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_READ) {
100                 char_properties[flag_count] = g_strdup("encrypt-authenticated-read");
101                 flag_count++;
102         }
103         if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_WRITE) {
104                 char_properties[flag_count] = g_strdup("encrypt-authenticated-write");
105                 flag_count++;
106         }
107
108         if (flag_count == 0) {
109                 char_properties[flag_count] = g_strdup("read");
110                 flag_count++;
111         }
112
113         return flag_count;
114 }
115
116 int bluetooth_gatt_convert_perm2string(
117                         bt_gatt_permission_t properties,
118                         char *char_properties[])
119 {
120         int flag_count = 0;
121
122         if (properties & BLUETOOTH_GATT_PERMISSION_READ) {
123                 char_properties[flag_count] = g_strdup("read");
124                 flag_count++;
125         }
126         if (properties & BLUETOOTH_GATT_PERMISSION_WRITE) {
127                 char_properties[flag_count] = g_strdup("write");
128                 flag_count++;
129         }
130         if (properties & BLUETOOTH_GATT_PERMISSION_ENCRYPT_READ) {
131                 char_properties[flag_count] = g_strdup("encrypt-read");
132                 flag_count++;
133         }
134         if (properties & BLUETOOTH_GATT_PERMISSION_ENCRYPT_WRITE) {
135                 char_properties[flag_count] = g_strdup("encrypt-write");
136                 flag_count++;
137         }
138         if (properties & BLUETOOTH_GATT_PERMISSION_ENCRYPT_AUTHENTICATED_READ) {
139                 char_properties[flag_count] = g_strdup("encrypt-authenticated-read");
140                 flag_count++;
141         }
142         if (properties & BLUETOOTH_GATT_PERMISSION_ENCRYPT_AUTHENTICATED_WRITE) {
143                 char_properties[flag_count] = g_strdup("encrypt-authenticated-write");
144                 flag_count++;
145         }
146
147         if (flag_count == 0) {
148                 char_properties[flag_count] = g_strdup("read");
149                 flag_count++;
150         }
151
152         return flag_count;
153 }
154
155
156 #define NUMBER_OF_FLAGS 10
157
158 GDBusConnection *g_conn;
159 guint owner_id;
160 guint manager_id;
161 static gboolean new_service = FALSE;
162 static gboolean new_char = FALSE;
163 static int serv_id = 1;
164 static bool is_server_started = false;
165
166 GCancellable *register_cancel;
167
168 /* Introspection data for the service we are exporting */
169 static const gchar service_introspection_xml[] =
170 "<node name='/'>"
171 "  <interface name='org.freedesktop.DBus.Properties'>"
172 "    <property type='s' name='UUID' access='read'>"
173 "    </property>"
174 "        <property type='b' name='primary' access='read'>"
175 "        </property>"
176 "        <property type='o' name='Device' access='read'>"
177 "        </property>"
178 "        <property type='ao' name='Characteristics' access='read'>"
179 "        </property>"
180 "        <property type='s' name='Includes' access='read'>"
181 "        </property>"
182 "  </interface>"
183 "</node>";
184
185 /* Introspection data for the characteristics we are exporting */
186 static const gchar characteristics_introspection_xml[] =
187 "<node name='/'>"
188 "  <interface name='org.bluez.GattCharacteristic1'>"
189 "        <method name='ReadValue'>"
190 "               <arg type='s' name='address' direction='in'/>"
191 "               <arg type='u' name='id' direction='in'/>"
192 "               <arg type='q' name='offset' direction='in'/>"
193 "               <arg type='ay' name='Value' direction='out'/>"
194 "        </method>"
195 "        <method name='WriteValue'>"
196 "               <arg type='s' name='address' direction='in'/>"
197 "               <arg type='u' name='id' direction='in'/>"
198 "               <arg type='q' name='offset' direction='in'/>"
199 "               <arg type='b' name='response_needed' direction='in'/>"
200 "               <arg type='ay' name='value' direction='in'/>"
201 "        </method>"
202 "        <method name='StartNotify'>"
203 "        </method>"
204 "        <method name='StopNotify'>"
205 "        </method>"
206 "        <method name='IndicateConfirm'>"
207 "               <arg type='s' name='address' direction='in'/>"
208 "               <arg type='b' name='complete' direction='in'/>"
209 "        </method>"
210 "  </interface>"
211 "  <interface name='org.freedesktop.DBus.Properties'>"
212 "    <property type='s' name='UUID' access='read'>"
213 "    </property>"
214 "    <property type='o' name='Service' access='read'>"
215 "    </property>"
216 "    <property type='ay' name='Value' access='readwrite'>"
217 "    </property>"
218 "        <property type='b' name='Notifying' access='read'>"
219 "        </property>"
220 "    <property type='as' name='Flags' access='read'>"
221 "    </property>"
222 "    <property type='s' name='Unicast' access='read'>"
223 "    </property>"
224 "        <property type='ao' name='Descriptors' access='read'>"
225 "        </property>"
226 "  </interface>"
227 "</node>";
228
229 /* Introspection data for the descriptor we are exporting */
230 static const gchar descriptor_introspection_xml[] =
231 "<node name='/'>"
232 "  <interface name='org.bluez.GattDescriptor1'>"
233 "        <method name='ReadValue'>"
234 "               <arg type='s' name='address' direction='in'/>"
235 "               <arg type='u' name='id' direction='in'/>"
236 "               <arg type='q' name='offset' direction='in'/>"
237 "               <arg type='ay' name='Value' direction='out'/>"
238 "        </method>"
239 "        <method name='WriteValue'>"
240 "               <arg type='s' name='address' direction='in'/>"
241 "               <arg type='u' name='id' direction='in'/>"
242 "               <arg type='q' name='offset' direction='in'/>"
243 "               <arg type='b' name='response_needed' direction='in'/>"
244 "               <arg type='ay' name='value' direction='in'/>"
245 "        </method>"
246 "  </interface>"
247 "  <interface name='org.freedesktop.DBus.Properties'>"
248 "    <property type='s' name='UUID' access='read'>"
249 "    </property>"
250 "    <property type='o' name='Characteristic' access='read'>"
251 "    </property>"
252 "    <property type='ay' name='Value' access='read'>"
253 "    </property>"
254 "    <property type='as' name='Flags' access='read'>"
255 "    </property>"
256 "  </interface>"
257 "</node>";
258
259 static const gchar manager_introspection_xml[] =
260 "<node name='/'>"
261 "  <interface name='org.freedesktop.DBus.ObjectManager'>"
262 "    <method name='GetManagedObjects'>"
263 "     <arg type='a{oa{sa{sv}}}' name='object_paths_interfaces_and_properties' direction='out'/>"
264 "        </method>"
265 "  </interface>"
266 "</node>";
267
268 struct gatt_service_info {
269         gchar *serv_path;
270         guint serv_id;
271         gchar *service_uuid;
272         guint manager_id;
273         GSList *char_data;
274         gboolean is_svc_registered;
275         gboolean is_svc_primary;
276 };
277
278 struct gatt_char_info {
279         gchar *char_path;
280         guint char_id;
281         gchar *char_uuid;
282         gchar *char_value;
283         gchar *char_flags[NUMBER_OF_FLAGS];
284         int value_length;
285         int flags_length;
286         GSList *desc_data;
287 };
288
289 struct gatt_desc_info {
290         gchar *desc_path;
291         guint desc_id;
292         gchar *desc_uuid;
293         gchar *desc_value;
294         gchar *desc_flags[NUMBER_OF_FLAGS];
295         int value_length;
296         int flags_length;
297 };
298
299 struct gatt_req_info {
300         gchar *attr_path;
301         gchar *svc_path;
302         guint  request_id;
303         guint  offset;
304         GDBusMethodInvocation *context;
305 };
306
307 static GSList *gatt_services = NULL;
308 static GSList *gatt_requests = NULL;
309 static gchar *app_path = NULL;
310
311 #define BT_GATT_SERVICE_NAME    "org.frwk.gatt_service"
312 #define BT_GATT_SERVICE_PATH "/org/frwk/gatt_service"
313
314 #define GATT_SERV_OBJECT_PATH   "/service"
315
316 #define GATT_MNGR_INTERFACE             "org.bluez.GattManager1"
317 #define GATT_SERV_INTERFACE             "org.bluez.GattService1"
318 #define GATT_CHAR_INTERFACE             "org.bluez.GattCharacteristic1"
319 #define GATT_DESC_INTERFACE             "org.bluez.GattDescriptor1"
320
321 #ifdef TIZEN_FEATURE_BT_HPS
322 #define BT_HPS_OBJECT_PATH "/org/projectx/httpproxy"
323 #define BT_HPS_INTERFACE_NAME "org.projectx.httpproxy_service"
324 #define PROPERTIES_CHANGED "PropertiesChanged"
325 #define BT_HPS_PROPERTIES_INTERFACE "org.freedesktop.DBus.Properties"
326 #endif
327
328 #ifdef TIZEN_FEATURE_BT_OTP
329 #define BT_OTP_OBJECT_PATH              "/org/projectx/otp"
330 #define BT_OTP_INTERFACE_NAME           "org.projectx.otp_service"
331 #define PROPERTIES_CHANGED              "PropertiesChanged"
332 #define BT_OTP_PROPERTIES_INTERFACE     "org.freedesktop.DBus.Properties"
333 #endif
334
335 static GDBusProxy *manager_gproxy = NULL;
336
337 static struct gatt_char_info *__bt_gatt_find_gatt_char_info(
338                         const char *service_path, const char *char_path);
339 static struct gatt_desc_info *__bt_gatt_find_gatt_desc_info(
340                         const char *serv_path, const char *char_path,
341                         const char *desc_path);
342
343 static struct gatt_req_info *__bt_gatt_find_request_info(guint request_id);
344 static int __bt_gatt_unregister_service(struct gatt_service_info *svc_info);
345
346
347
348 typedef struct {
349         int write_fd;
350         int relpy_fd;
351         int mtu;
352         int att_hand;
353         char *path ;
354 } bluetooth_gatt_acquire_notify_info_t;
355
356
357 static int bluetooth_get_characteristic_fd(int att_handle , char *path)
358 {
359         GSList *l;
360
361         BT_INFO("request found  path [%s] att_handle [ %d]", path, att_handle);
362         for (l = gatt_characteristic_server_notify_list; l != NULL; l = l->next) {
363                 bluetooth_gatt_acquire_notify_info_t *info = l->data;
364                 BT_INFO(" sid [ %d]" , info->att_hand);
365                 if (info->att_hand == att_handle)
366                         return info->write_fd;
367         }
368         return -1;
369 }
370
371 static bluetooth_gatt_acquire_notify_info_t * bluetooth_get_characteristic_info_from_path(int att_handle)
372 {
373         GSList *l;
374
375         BT_INFO("request found  att_handle [ %d]", att_handle);
376         for (l = gatt_characteristic_server_notify_list; l != NULL; l = l->next) {
377                 bluetooth_gatt_acquire_notify_info_t *info = l->data;
378                 BT_INFO(" sid [ %d]" , info->att_hand);
379                 if (info->att_hand == att_handle)
380                         return info;
381         }
382         return NULL;
383 }
384
385
386 static void bluetooth_characteristic_info_free(bluetooth_gatt_acquire_notify_info_t *chr_info)
387 {
388                 g_free(chr_info);
389 }
390
391 static gboolean bluetooth_gatt_write_channel_watch_cb(GIOChannel *gio,
392                                         GIOCondition cond, gpointer data)
393 {
394         BT_INFO("+");
395
396         bluetooth_gatt_acquire_notify_info_t *chr_info = (bluetooth_gatt_acquire_notify_info_t *)data;
397
398         if (!chr_info) {
399                 BT_INFO("chr_info is NULL");
400                 return FALSE;
401         }
402
403         if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
404                 BT_ERR("Error : GIOCondition %d, []", cond);;
405                 g_io_channel_shutdown(gio, TRUE, NULL);
406                 g_io_channel_unref(gio);
407
408                 if (g_slist_find(gatt_characteristic_server_notify_list, chr_info)) {
409                         BT_INFO("found char_info in the list");
410                         gatt_characteristic_server_notify_list = g_slist_remove(gatt_characteristic_server_notify_list, chr_info);
411                         bluetooth_characteristic_info_free(chr_info);
412                 }
413
414                 return FALSE;
415         }
416
417         if (g_slist_find(gatt_characteristic_server_notify_list, chr_info) == NULL) {
418                 BT_INFO("chr_info is not in the list");
419                 return FALSE;
420         }
421
422         return TRUE;
423 }
424
425 static int bluetooth_gatt_write_characteristics_value_to_fd_(
426                          int fd, const guint8 *value, int length,
427                         gpointer user_data)
428 {
429
430                 int written;
431                 int att_result = BLUETOOTH_ERROR_NONE;
432
433                 BT_CHECK_PARAMETER(value, return);
434
435                 written = write(fd, value, length);
436                 if (written != length) {
437                         att_result = BLUETOOTH_ERROR_INTERNAL;
438                         BT_INFO("write data failed  %d is ", written);
439                 } else
440                    BT_INFO("write data %s is sucess ", value);
441
442                 return att_result;
443 }
444
445 #ifdef TIZEN_FEATURE_BT_HPS
446 static int __bt_send_event_to_hps(int event, GVariant *var)
447 {
448         GError *error = NULL;
449         GVariant *parameters;
450         GDBusMessage *msg = NULL;
451
452         BT_DBG(" ");
453
454         retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
455
456         if (event == BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED) {
457                 GVariantBuilder *inner_builder;
458                 GVariantBuilder *invalidated_builder;
459
460                 BT_DBG("BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED");
461                 inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
462
463                 g_variant_builder_add(inner_builder, "{sv}", "WriteValue", var);
464
465                 invalidated_builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
466
467                 parameters = g_variant_new("(a{sv}as)", inner_builder, invalidated_builder);
468                 g_variant_builder_unref(invalidated_builder);
469                 g_variant_builder_unref(inner_builder);
470         } else if (event == BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED) {
471                 GVariantBuilder *inner_builder;
472                 GVariantBuilder *invalidated_builder;
473
474                 BT_DBG("BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED");
475                 inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
476
477                 g_variant_builder_add(inner_builder, "{sv}", "ReadValue", var);
478
479                 invalidated_builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
480
481                 parameters = g_variant_new("(a{sv}as)", inner_builder, invalidated_builder);
482                 g_variant_builder_unref(invalidated_builder);
483                 g_variant_builder_unref(inner_builder);
484         } else {
485                 g_varaiant_unref(var);
486         }
487
488         msg = g_dbus_message_new_signal(BT_HPS_OBJECT_PATH, BT_HPS_INTERFACE_NAME, PROPERTIES_CHANGED);
489         g_dbus_message_set_body(msg, parameters);
490         if (!g_dbus_connection_send_message(g_conn, msg, G_DBUS_SEND_MESSAGE_FLAGS_NONE, 0, NULL)) {
491                 if (error != NULL) {
492                         BT_ERR("D-Bus API failure: errCode[%x], \
493                                         message[%s]",
494                                         error->code, error->message);
495                         g_clear_error(&error);
496                 }
497                 return BLUETOOTH_ERROR_INTERNAL;
498         }
499         return BLUETOOTH_ERROR_NONE;
500 }
501 #endif
502
503 #ifdef TIZEN_FEATURE_BT_OTP
504 static int __bt_send_event_to_otp(int event, GVariant *var)
505 {
506         GError *error = NULL;
507         GVariant *parameters = NULL;
508         GDBusMessage *msg = NULL;
509
510         BT_DBG(" ");
511
512         retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
513
514         if (event == BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED) {
515                 GVariantBuilder *inner_builder;
516                 GVariantBuilder *invalidated_builder;
517
518                 BT_DBG("BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED");
519                 inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
520
521                 g_variant_builder_add(inner_builder, "{sv}", "WriteValue", var);
522
523                 invalidated_builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
524
525                 parameters = g_variant_new("(a{sv}as)", inner_builder, invalidated_builder);
526                 g_variant_builder_unref(invalidated_builder);
527                 g_variant_builder_unref(inner_builder);
528         } else if (event == BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED) {
529                 GVariantBuilder *inner_builder;
530                 GVariantBuilder *invalidated_builder;
531
532                 BT_DBG("BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED");
533                 inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
534
535                 g_variant_builder_add(inner_builder, "{sv}", "ReadValue", var);
536
537                 invalidated_builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
538
539                 parameters = g_variant_new("(a{sv}as)", inner_builder, invalidated_builder);
540                 g_variant_builder_unref(invalidated_builder);
541                 g_variant_builder_unref(inner_builder);
542         } else if (event == BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_STATE_CHANGED) {
543                 GVariantBuilder *inner_builder;
544                 GVariantBuilder *invalidated_builder;
545
546                 BT_DBG("BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_STATE_CHANGED");
547                 inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
548
549                 g_variant_builder_add(inner_builder, "{sv}", "NotificationStateChanged", var);
550
551                 invalidated_builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
552
553                 parameters = g_variant_new("(a{sv}as)", inner_builder, invalidated_builder);
554                 g_variant_builder_unref(invalidated_builder);
555                 g_variant_builder_unref(inner_builder);
556         }
557
558         msg = g_dbus_message_new_signal(BT_OTP_OBJECT_PATH, BT_OTP_INTERFACE_NAME, PROPERTIES_CHANGED);
559         g_dbus_message_set_body(msg, parameters);
560         if (!g_dbus_connection_send_message(g_conn, msg, G_DBUS_SEND_MESSAGE_FLAGS_NONE, 0, NULL)) {
561                 if (error != NULL) {
562                         BT_ERR("D-Bus API failure: errCode[%x], \
563                                         message[%s]",
564                                         error->code, error->message);
565                         g_clear_error(&error);
566                 }
567                 return BLUETOOTH_ERROR_INTERNAL;
568         }
569         return BLUETOOTH_ERROR_NONE;
570 }
571 #endif
572
573 static void __bt_gatt_manager_method_call(GDBusConnection *connection,
574                                         const gchar *sender,
575                                         const gchar *object_path,
576                                         const gchar *interface_name,
577                                         const gchar *method_name,
578                                         GVariant *parameters,
579                                         GDBusMethodInvocation *invocation,
580                                         gpointer user_data)
581 {
582         if (g_strcmp0(method_name, "GetManagedObjects") == 0) {
583                 BT_DBG("Getting values for service, chars and descriptors");
584
585                 int svc_index = 0;
586                 GVariantBuilder *builder = NULL;
587                 GVariantBuilder *inner_builder1 = NULL;
588                 GVariant *svc_char = NULL;
589                 GSList *char_list = NULL;
590                 GSList *desc_list = NULL;
591
592                 /* Main Builder */
593                 builder = g_variant_builder_new(G_VARIANT_TYPE("a{oa{sa{sv}}}"));
594
595                 /* Prepare inner builder for GattService1 interface */
596                 svc_index = g_slist_length(gatt_services) - 1;
597                 for (; svc_index >= 0; svc_index--) {
598                         GVariantBuilder *svc_builder = NULL;
599                         GVariantBuilder *inner_builder = NULL;
600                         struct gatt_service_info *serv_info = NULL;
601
602                         serv_info = g_slist_nth_data(gatt_services, svc_index);
603                         if (serv_info == NULL) {
604                                 BT_ERR("serv_info is NULL");
605                                 continue;
606                         }
607
608                         /* Prepare inner builder for GattService1 interface */
609                         BT_DBG("Creating builder for service : %s", serv_info->service_uuid);
610                         svc_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
611                         inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
612
613                         g_variant_builder_add(inner_builder, "{sv}", "UUID",
614                                         g_variant_new_string(serv_info->service_uuid));
615                         g_variant_builder_add(inner_builder, "{sv}", "Primary",
616                                         g_variant_new_boolean(serv_info->is_svc_primary));
617
618                         /* Characteristics */
619                         inner_builder1 = g_variant_builder_new(G_VARIANT_TYPE("ao"));
620
621                         BT_DBG("Adding Charatarisitcs list");
622                         for (char_list = serv_info->char_data; char_list != NULL; char_list = char_list->next) {
623                                 struct gatt_char_info *char_info = char_list->data;
624                                 g_variant_builder_add(inner_builder1, "o", char_info->char_path);
625                                 BT_DBG("%s", char_info->char_path);
626                         }
627
628                         svc_char = g_variant_new("ao", inner_builder1);
629                         g_variant_builder_add(inner_builder, "{sv}", "Characteristics", svc_char);
630                         g_variant_builder_add(svc_builder, "{sa{sv}}", GATT_SERV_INTERFACE, inner_builder);
631                         g_variant_builder_add(builder, "{oa{sa{sv}}}", serv_info->serv_path, svc_builder);
632                         g_variant_builder_unref(inner_builder1);
633
634                         /* Prepare inner builder for GattCharacteristic1 interface */
635                         for (char_list = serv_info->char_data; char_list != NULL; char_list = char_list->next) {
636                                 GVariantBuilder *char_builder = NULL;
637                                 GVariantBuilder *inner_builder = NULL;
638                                 GVariantBuilder *builder1 = NULL;
639                                 GVariantBuilder *builder2 = NULL;
640                                 GVariantBuilder *builder3 = NULL;
641                                 GVariant *char_val = NULL;
642                                 GVariant *flags_val = NULL;
643                                 GVariant *char_desc = NULL;
644                                 char *unicast = NULL;
645                                 gboolean notify = FALSE;
646                                 int i = 0;
647                                 struct gatt_char_info *char_info = char_list->data;
648
649                                 if (char_info == NULL) {
650                                         BT_ERR("char_info is NULL");
651                                         continue;
652                                 }
653
654                                 BT_DBG("Creating builder for characteristic : %s", char_info->char_uuid);
655                                 char_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
656                                 inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
657
658                                 /* UUID */
659                                 g_variant_builder_add(inner_builder, "{sv}", "UUID",
660                                                 g_variant_new_string(char_info->char_uuid));
661
662                                 /* Service */
663                                 g_variant_builder_add(inner_builder, "{sv}", "Service",
664                                                 g_variant_new("o", serv_info->serv_path));
665
666                                 /* Value */
667                                 builder1 = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
668                                 if (char_info->char_value != NULL) {
669                                         for (i = 0; i < char_info->value_length; i++) {
670                                                 g_variant_builder_add(builder1, "y", char_info->char_value[i]);
671                                         }
672                                         char_val = g_variant_new("ay", builder1);
673                                         g_variant_builder_add(inner_builder, "{sv}", "Value", char_val);
674                                 }
675
676                                 /*Flags*/
677                                 builder2 = g_variant_builder_new(G_VARIANT_TYPE("as"));
678                                 for (i = 0; i < char_info->flags_length; i++) {
679                                         g_variant_builder_add(builder2, "s", char_info->char_flags[i]);
680                                 }
681                                 flags_val = g_variant_new("as", builder2);
682                                 g_variant_builder_add(inner_builder, "{sv}", "Flags", flags_val);
683
684                                 /* Notifying */
685                                 g_variant_builder_add(inner_builder, "{sv}", "Notifying", g_variant_new("b", notify));
686
687                                 /* Unicast */
688                                 unicast = g_strdup("00:00:00:00:00:00");
689                                 g_variant_builder_add(inner_builder, "{sv}", "Unicast", g_variant_new("s", unicast));
690
691                                 /*Descriptors*/
692                                 builder3 = g_variant_builder_new(G_VARIANT_TYPE("ao"));
693                                 BT_DBG("Adding Descriptors list");
694                                 for (desc_list = char_info->desc_data; desc_list != NULL; desc_list = desc_list->next) {
695                                         struct gatt_desc_info *desc_info = desc_list->data;
696                                         g_variant_builder_add(builder3, "o", desc_info->desc_path);
697                                         BT_DBG("%s", desc_info->desc_path);
698                                 }
699
700                                 char_desc = g_variant_new("ao", builder3);
701                                 g_variant_builder_add(inner_builder, "{sv}", "Descriptors", char_desc);
702                                 g_variant_builder_add(char_builder, "{sa{sv}}", GATT_CHAR_INTERFACE , inner_builder);
703                                 g_variant_builder_add(builder, "{oa{sa{sv}}}", char_info->char_path, char_builder);
704
705                                 /*Prepare inner builder for GattDescriptor1 interface*/
706                                 for (desc_list = char_info->desc_data; desc_list != NULL; desc_list = desc_list->next) {
707                                         GVariantBuilder *desc_builder = NULL;
708                                         GVariantBuilder *inner_builder = NULL;
709                                         GVariantBuilder *builder1 = NULL;
710                                         GVariantBuilder *builder2 = NULL;
711                                         GVariant *desc_val = NULL;
712                                         struct gatt_desc_info *desc_info = desc_list->data;
713
714                                         if (desc_info == NULL) {
715                                                 BT_ERR("desc_info is NULL");
716                                                 continue;
717                                         }
718
719                                         BT_DBG("Creating builder for descriptor : %s", desc_info->desc_uuid);
720                                         desc_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
721                                         inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
722
723                                         /* UUID */
724                                         g_variant_builder_add(inner_builder, "{sv}", "UUID",
725                                                         g_variant_new_string(desc_info->desc_uuid));
726
727                                         /* Characteristic */
728                                         g_variant_builder_add(inner_builder, "{sv}", "Characteristic",
729                                                         g_variant_new("o", char_info->char_path));
730
731                                         /* Value */
732                                         builder1 = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
733                                         if (desc_info->desc_value != NULL) {
734                                                 for (i = 0; i < desc_info->value_length; i++) {
735                                                         g_variant_builder_add(builder1, "y", desc_info->desc_value[i]);
736                                                 }
737                                                 desc_val = g_variant_new("ay", builder1);
738                                                 g_variant_builder_add(inner_builder, "{sv}", "Value", desc_val);
739                                         }
740
741                                         /* Flags */
742                                         builder2 = g_variant_builder_new(G_VARIANT_TYPE("as"));
743                                         for (i = 0; i < desc_info->flags_length; i++) {
744                                                 g_variant_builder_add(builder2, "s", desc_info->desc_flags[i]);
745                                         }
746                                         flags_val = g_variant_new("as", builder2);
747                                         g_variant_builder_add(inner_builder, "{sv}", "Flags", flags_val);
748
749                                         g_variant_builder_add(desc_builder, "{sa{sv}}", GATT_DESC_INTERFACE,
750                                                         inner_builder);
751                                         g_variant_builder_add(builder, "{oa{sa{sv}}}", desc_info->desc_path,
752                                                         desc_builder);
753
754                                         /* unref descriptor builder pointers */
755                                         g_variant_builder_unref(builder1);
756                                         g_variant_builder_unref(builder2);
757                                         g_variant_builder_unref(inner_builder);
758                                         g_variant_builder_unref(desc_builder);
759                                 }
760
761                                 if (unicast)
762                                         g_free(unicast);
763
764                                 /* unref char builder pointers */
765                                 g_variant_builder_unref(builder1);
766                                 g_variant_builder_unref(builder2);
767                                 g_variant_builder_unref(builder3);
768                                 g_variant_builder_unref(inner_builder);
769                                 g_variant_builder_unref(char_builder);
770                         }
771
772                         /* unref service builder pointers */
773                         g_variant_builder_unref(inner_builder);
774                         g_variant_builder_unref(svc_builder);
775                 }
776
777                 /* Return builder as method reply */
778                 BT_DBG("Sending gatt service builder values to Bluez");
779                 g_dbus_method_invocation_return_value(invocation,
780                                 g_variant_new("(a{oa{sa{sv}}})", builder));
781                 g_variant_builder_unref(builder);
782         }
783 }
784
785 static struct gatt_service_info *__bt_gatt_find_gatt_service_from_char(const char *char_path)
786 {
787         GSList *l1, *l2;
788
789         for (l1 = gatt_services; l1 != NULL; l1 = l1->next) {
790                 struct gatt_service_info *serv_info = l1->data;
791
792                 for (l2 = serv_info->char_data; l2 != NULL; l2 = l2->next) {
793                         struct gatt_char_info *char_info = l2->data;
794
795                         if (g_strcmp0(char_info->char_path, char_path) == 0)
796                                 return serv_info;
797                 }
798         }
799         BT_ERR("Gatt service not found");
800         return NULL;
801 }
802
803 static struct gatt_service_info *__bt_gatt_find_gatt_service_from_desc(const char *desc_path)
804 {
805         GSList *l1, *l2, *l3;
806
807         for (l1 = gatt_services; l1 != NULL; l1 = l1->next) {
808                 struct gatt_service_info *serv_info = l1->data;
809
810                 for (l2 = serv_info->char_data; l2 != NULL; l2 = l2->next) {
811                         struct gatt_char_info *char_info = l2->data;
812
813                         for (l3 = char_info->desc_data; l3 != NULL; l3 = l3->next) {
814                                 struct gatt_desc_info *desc_info = l3->data;
815
816                                 if (g_strcmp0(desc_info->desc_path, desc_path) == 0)
817                                         return serv_info;
818                         }
819                 }
820         }
821         BT_ERR("Gatt service not found");
822         return NULL;
823 }
824
825 static void __bt_gatt_char_method_call(GDBusConnection *connection,
826                                         const gchar *sender,
827                                         const gchar *object_path,
828                                         const gchar *interface_name,
829                                         const gchar *method_name,
830                                         GVariant *parameters,
831                                         GDBusMethodInvocation *invocation,
832                                         gpointer user_data)
833 {
834
835         if (g_strcmp0(method_name, "ReadValue") == 0) {
836                 gchar *addr = NULL;
837                 guint req_id = 0;
838                 guint16 offset = 0;
839                 bt_gatt_read_req_t read_req = {0, };
840                 bt_user_info_t *user_info = NULL;
841                 struct gatt_req_info *req_info = NULL;
842                 struct gatt_service_info *svc_info = NULL;
843 #if defined(TIZEN_FEATURE_BT_HPS) || defined(TIZEN_FEATURE_BT_OTP)
844                 GVariant *param = NULL;
845 #endif
846
847                 BT_DBG("Application path = %s", object_path);
848                 BT_DBG("Sender = %s", sender);
849
850                 user_info = _bt_get_user_data(BT_COMMON);
851                 if (user_info == NULL) {
852                         BT_INFO("No callback is set for %s", object_path);
853                         g_dbus_method_invocation_return_value(invocation, NULL);
854                         return;
855                 }
856
857                 svc_info = __bt_gatt_find_gatt_service_from_char(object_path);
858                 if (svc_info == NULL) {
859                         BT_ERR("Coudn't find service for %s", object_path);
860                         g_dbus_method_invocation_return_value(invocation, NULL);
861                         return;
862                 }
863
864                 g_variant_get(parameters, "(&suq)", &addr, &req_id, &offset);
865                 BT_DBG("Request id = %u, Offset = %u", req_id, offset);
866
867                 read_req.att_handle = (char *)object_path;
868                 read_req.address = addr;
869                 read_req.req_id = req_id;
870                 read_req.offset = offset;
871                 read_req.service_handle = svc_info->serv_path;
872
873                 /* Store requets information */
874                 req_info = g_new0(struct gatt_req_info, 1);
875                 req_info->attr_path = g_strdup(object_path);
876                 req_info->svc_path = g_strdup(read_req.service_handle);
877                 req_info->request_id = req_id;
878                 req_info->offset = offset;
879                 req_info->context = invocation;
880                 gatt_requests = g_slist_append(gatt_requests, req_info);
881
882 #if defined(TIZEN_FEATURE_BT_HPS) || defined(TIZEN_FEATURE_BT_OTP)
883                 param = g_variant_new("(sssyq)",
884                                 read_req.att_handle,
885                                 read_req.service_handle,
886                                 read_req.address,
887                                 read_req.req_id,
888                                 read_req.offset);
889 #ifdef TIZEN_FEATURE_BT_HPS
890                 __bt_send_event_to_hps(BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED, param);
891 #endif
892 #ifdef TIZEN_FEATURE_BT_OTP
893                 __bt_send_event_to_otp(BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED, param);
894 #endif
895 #endif
896
897                 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED,
898                                         BLUETOOTH_ERROR_NONE, &read_req,
899                                         user_info->cb, user_info->user_data);
900                 return;
901         } else if (g_strcmp0(method_name, "WriteValue") == 0) {
902                 GVariant *var = NULL;
903                 gchar *addr = NULL;
904                 guint req_id = 0;
905                 guint16 offset = 0;
906                 gboolean response_needed = FALSE;
907                 bt_gatt_value_change_t value_change = {0, };
908                 bt_user_info_t *user_info = NULL;
909                 int len = 0;
910                 struct gatt_service_info *svc_info = NULL;
911                 struct gatt_req_info *req_info = NULL;
912 #if defined(TIZEN_FEATURE_BT_HPS) || defined(TIZEN_FEATURE_BT_OTP)
913                 GVariant *param = NULL;
914 #endif
915
916                 BT_DBG("WriteValue");
917                 BT_DBG("Application path = %s", object_path);
918                 BT_DBG("Sender = %s", sender);
919
920                 g_variant_get(parameters, "(&suqb@ay)",
921                                 &addr, &req_id, &offset, &response_needed, &var);
922                 BT_DBG("Request id = %u, Offset = %u", req_id, offset);
923
924                 user_info = _bt_get_user_data(BT_COMMON);
925                 if (!user_info) {
926                         BT_INFO("No callback is set for %s", object_path);
927                         g_variant_unref(var);
928                         if (response_needed)
929                                 g_dbus_method_invocation_return_value(invocation, NULL);
930                         else
931                                 g_object_unref(invocation);
932                         return;
933                 }
934
935                 svc_info = __bt_gatt_find_gatt_service_from_char(object_path);
936                 if (svc_info == NULL) {
937                         BT_ERR("Coudn't find service for %s", object_path);
938                         g_variant_unref(var);
939                         if (response_needed)
940                                 g_dbus_method_invocation_return_value(invocation, NULL);
941                         else
942                                 g_object_unref(invocation);
943                         return;
944                 }
945
946                 value_change.att_handle = (char *)object_path;
947                 value_change.address = addr;
948                 value_change.service_handle = svc_info->serv_path;
949                 value_change.offset = offset;
950                 value_change.req_id = req_id;
951                 value_change.response_needed = response_needed;
952
953                 len = g_variant_get_size(var);
954                 if (len > 0) {
955                         char *data;
956
957                         value_change.att_value = (guint8 *)g_malloc(len);
958
959                         data = (char *)g_variant_get_data(var);
960                         memcpy(value_change.att_value, data, len);
961                 }
962                 value_change.val_len = len;
963
964                 if (response_needed) {
965                         /* Store requets information */
966                         req_info = g_new0(struct gatt_req_info, 1);
967                         req_info->attr_path = g_strdup(object_path);
968                         req_info->svc_path = g_strdup(value_change.service_handle);
969                         req_info->request_id = req_id;
970                         req_info->offset = offset;
971                         req_info->context = invocation;
972                         gatt_requests = g_slist_append(gatt_requests, req_info);
973                 } else {
974                         g_object_unref(invocation);
975                 }
976
977 #if defined(TIZEN_FEATURE_BT_HPS) || defined(TIZEN_FEATURE_BT_OTP)
978                 if (len > 0) {
979                         gchar *svc_path;
980                         svc_path = g_strdup(svc_info->serv_path);
981                         param = g_variant_new("(sssyq@ay)",
982                                         object_path,
983                                         svc_path,
984                                         addr,
985                                         req_id,
986                                         offset,
987                                         var);
988 #ifdef TIZEN_FEATURE_BT_HPS
989                         __bt_send_event_to_hps(BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED, param);
990 #endif
991 #ifdef TIZEN_FEATURE_BT_OTP
992                         __bt_send_event_to_otp(BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED, param);
993 #endif
994                         if (svc_path)
995                                 g_free(svc_path);
996                 }
997 #endif
998
999                 _bt_common_event_cb(
1000                         BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED,
1001                         BLUETOOTH_ERROR_NONE, &value_change,
1002                         user_info->cb, user_info->user_data);
1003
1004                 g_free(value_change.att_value);
1005                 g_variant_unref(var);
1006                 return;
1007         } else if (g_strcmp0(method_name, "StartNotify") == 0) {
1008                 bt_user_info_t *user_info = NULL;
1009                 bt_gatt_char_notify_change_t notify_change = {0, };
1010 #if TIZEN_FEATURE_BT_OTP
1011                 GVariant *param = NULL;
1012 #endif
1013                 BT_DBG("StartNotify");
1014                 user_info = _bt_get_user_data(BT_COMMON);
1015                 if (user_info != NULL) {
1016                         struct gatt_service_info *svc_info = NULL;
1017                         svc_info = __bt_gatt_find_gatt_service_from_char(object_path);
1018                         if (svc_info) {
1019                                 notify_change.service_handle = svc_info->serv_path;
1020                                 notify_change.att_handle = (char *)object_path;
1021                                 notify_change.att_notify = TRUE;
1022 #if TIZEN_FEATURE_BT_OTP
1023                                 param = g_variant_new("(ssb)",
1024                                 notify_change.att_handle,
1025                                 notify_change.service_handle,
1026                                 notify_change.att_notify);
1027                                 __bt_send_event_to_otp(BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_STATE_CHANGED, param);
1028 #endif
1029                                 _bt_common_event_cb(
1030                                         BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_STATE_CHANGED,
1031                                         BLUETOOTH_ERROR_NONE, &notify_change,
1032                                         user_info->cb, user_info->user_data);
1033                         }
1034                 }
1035                 g_object_unref(invocation);
1036                 return;
1037         } else if (g_strcmp0(method_name, "StopNotify") == 0) {
1038                 bt_user_info_t *user_info = NULL;
1039                 bt_gatt_char_notify_change_t notify_change = {0, };
1040 #if TIZEN_FEATURE_BT_OTP
1041                 GVariant *param = NULL;
1042 #endif
1043                 BT_DBG("StopNotify");
1044                 user_info = _bt_get_user_data(BT_COMMON);
1045                 if (user_info != NULL) {
1046                         struct gatt_service_info *svc_info = NULL;
1047                         svc_info = __bt_gatt_find_gatt_service_from_char(object_path);
1048                         if (svc_info) {
1049                                 notify_change.service_handle = svc_info->serv_path;
1050                                 notify_change.att_handle = (char *)object_path;
1051                                 notify_change.att_notify = FALSE;
1052 #if TIZEN_FEATURE_BT_OTP
1053                                 param = g_variant_new("(ssb)",
1054                                 notify_change.att_handle,
1055                                 notify_change.service_handle,
1056                                 notify_change.att_notify);
1057                                 __bt_send_event_to_otp(BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_STATE_CHANGED, param);
1058 #endif
1059                                 _bt_common_event_cb(
1060                                         BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_STATE_CHANGED,
1061                                         BLUETOOTH_ERROR_NONE, &notify_change,
1062                                         user_info->cb, user_info->user_data);
1063                         }
1064                 }
1065                 g_object_unref(invocation);
1066                 return;
1067         } else if (g_strcmp0(method_name, "IndicateConfirm") == 0) {
1068                 gchar *addr = NULL;
1069                 bt_gatt_indicate_confirm_t confirm = {0, };
1070                 bt_user_info_t *user_info = NULL;
1071                 gboolean complete = FALSE;
1072                 struct gatt_service_info *svc_info = NULL;
1073
1074                 BT_DBG("IndicateConfirm");
1075                 BT_DBG("Application path = %s", object_path);
1076                 BT_DBG("Sender = %s", sender);
1077
1078                 g_variant_get(parameters, "(&sb)", &addr, &complete);
1079                 BT_DBG("Remote Device address number = %s", addr);
1080
1081                 confirm.att_handle = (char *)object_path;
1082                 confirm.address = addr;
1083                 confirm.complete = complete;
1084
1085                 svc_info = __bt_gatt_find_gatt_service_from_char(object_path);
1086                 if (svc_info != NULL) {
1087                         confirm.service_handle = svc_info->serv_path;
1088
1089                         user_info = _bt_get_user_data(BT_COMMON);
1090                         if (user_info != NULL) {
1091                                 _bt_common_event_cb(
1092                                         BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_COMPLETED,
1093                                         BLUETOOTH_ERROR_NONE, &confirm,
1094                                         user_info->cb, user_info->user_data);
1095                         }
1096                 }
1097         }
1098
1099         g_dbus_method_invocation_return_value(invocation, NULL);
1100 }
1101
1102 static void __bt_gatt_desc_method_call(GDBusConnection *connection,
1103                                         const gchar *sender,
1104                                         const gchar *object_path,
1105                                         const gchar *interface_name,
1106                                         const gchar *method_name,
1107                                         GVariant *parameters,
1108                                         GDBusMethodInvocation *invocation,
1109                                         gpointer user_data)
1110 {
1111         if (g_strcmp0(method_name, "ReadValue") == 0) {
1112                 gchar *addr = NULL;
1113                 guint req_id = 0;
1114                 guint16 offset = 0;
1115                 bt_gatt_read_req_t read_req = {0, };
1116                 bt_user_info_t *user_info = NULL;
1117                 struct gatt_req_info *req_info = NULL;
1118                 struct gatt_service_info *svc_info = NULL;
1119
1120                 BT_DBG("ReadValue");
1121                 BT_DBG("Application path = %s", object_path);
1122                 BT_DBG("Sender = %s", sender);
1123
1124                 user_info = _bt_get_user_data(BT_COMMON);
1125                 if (user_info == NULL) {
1126                         BT_INFO("No callback is set for %s", object_path);
1127                         g_dbus_method_invocation_return_value(invocation, NULL);
1128                         return;
1129                 }
1130
1131                 svc_info = __bt_gatt_find_gatt_service_from_desc(object_path);
1132                 if (svc_info == NULL) {
1133                         BT_ERR("Coudn't find service for %s", object_path);
1134                         g_dbus_method_invocation_return_value(invocation, NULL);
1135                         return;
1136                 }
1137
1138                 g_variant_get(parameters, "(&suq)", &addr, &req_id, &offset);
1139                 BT_DBG("Request id = %u, Offset = %u", req_id, offset);
1140
1141                 read_req.att_handle = (char *)object_path;
1142                 read_req.address = addr;
1143                 read_req.req_id = req_id;
1144                 read_req.offset = offset;
1145                 read_req.service_handle = svc_info->serv_path;
1146
1147                 /* Store requets information */
1148                 req_info = g_new0(struct gatt_req_info, 1);
1149                 req_info->attr_path = g_strdup(object_path);
1150                 req_info->svc_path = g_strdup(read_req.service_handle);
1151                 req_info->request_id = req_id;
1152                 req_info->offset = offset;
1153                 req_info->context = invocation;
1154                 gatt_requests = g_slist_append(gatt_requests, req_info);
1155
1156                 _bt_common_event_cb(
1157                                 BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED,
1158                                 BLUETOOTH_ERROR_NONE, &read_req,
1159                                 user_info->cb, user_info->user_data);
1160
1161                 return;
1162         } else if (g_strcmp0(method_name, "WriteValue") == 0) {
1163                 GVariant *var = NULL;
1164                 gchar *addr = NULL;
1165                 guint req_id = 0;
1166                 guint16 offset = 0;
1167                 gboolean response_needed = FALSE;
1168                 bt_gatt_value_change_t value_change = {0, };
1169                 bt_user_info_t *user_info = NULL;
1170                 int len = 0;
1171                 struct gatt_service_info *svc_info = NULL;
1172                 struct gatt_req_info *req_info = NULL;
1173
1174                 BT_DBG("WriteValue");
1175                 BT_DBG("Application path = %s", object_path);
1176                 BT_DBG("Sender = %s", sender);
1177
1178                 g_variant_get(parameters, "(&suqb@ay)",
1179                                 &addr, &req_id, &offset, &response_needed, &var);
1180                 BT_DBG("Request id = %u, Offset = %u", req_id, offset);
1181
1182                 user_info = _bt_get_user_data(BT_COMMON);
1183                 if (user_info == NULL) {
1184                         BT_INFO("No callback is set for %s", object_path);
1185                         g_variant_unref(var);
1186                         if (response_needed)
1187                                 g_dbus_method_invocation_return_value(invocation, NULL);
1188                         else
1189                                 g_object_unref(invocation);
1190                         return;
1191                 }
1192
1193                 svc_info = __bt_gatt_find_gatt_service_from_desc(object_path);
1194                 if (svc_info == NULL) {
1195                         BT_ERR("Coudn't find service for %s", object_path);
1196                         g_variant_unref(var);
1197                         if (response_needed)
1198                                 g_dbus_method_invocation_return_value(invocation, NULL);
1199                         else
1200                                 g_object_unref(invocation);
1201                         return;
1202                 }
1203
1204                 value_change.att_handle = (char *)object_path;
1205                 value_change.address = addr;
1206                 value_change.service_handle = svc_info->serv_path;
1207                 value_change.offset = offset;
1208                 value_change.req_id = req_id;
1209                 value_change.response_needed = response_needed;
1210
1211                 len = g_variant_get_size(var);
1212                 if (len > 0) {
1213                         char *data;
1214
1215                         value_change.att_value = (guint8 *)g_malloc(len);
1216
1217                         data = (char *)g_variant_get_data(var);
1218                         memcpy(value_change.att_value, data, len);
1219                 }
1220                 value_change.val_len = len;
1221
1222                 if (response_needed) {
1223                         /* Store requets information */
1224                         req_info = g_new0(struct gatt_req_info, 1);
1225                         req_info->attr_path = g_strdup(object_path);
1226                         req_info->svc_path = g_strdup(value_change.service_handle);
1227                         req_info->request_id = req_id;
1228                         req_info->offset = offset;
1229                         req_info->context = invocation;
1230                         gatt_requests = g_slist_append(gatt_requests, req_info);
1231                 } else {
1232                         g_object_unref(invocation);
1233                 }
1234
1235                 _bt_common_event_cb(
1236                         BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED,
1237                         BLUETOOTH_ERROR_NONE, &value_change,
1238                         user_info->cb, user_info->user_data);
1239
1240                 g_free(value_change.att_value);
1241                 g_variant_unref(var);
1242                 return;
1243         }
1244 }
1245
1246 gboolean __bt_gatt_emit_interface_removed(gchar *object_path, gchar *interface)
1247 {
1248         gboolean ret;
1249         GError *error = NULL;
1250         GVariantBuilder *array_builder;
1251
1252         array_builder = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
1253         g_variant_builder_init(array_builder, G_VARIANT_TYPE("as"));
1254         g_variant_builder_add(array_builder, "s", interface);
1255
1256         ret = g_dbus_connection_emit_signal(g_conn, NULL, "/",
1257                                         "org.freedesktop.Dbus.Objectmanager",
1258                                         "InterfacesRemoved",
1259                                         g_variant_new("(oas)",
1260                                         object_path, array_builder),
1261                                         &error);
1262
1263         if (!ret) {
1264                 if (error != NULL) {
1265                         /* dbus gives error cause */
1266                         BT_ERR("d-bus api failure: errcode[%x], message[%s]",
1267                                 error->code, error->message);
1268                         g_clear_error(&error);
1269                 }
1270         }
1271         g_variant_builder_unref(array_builder);
1272
1273         return ret;
1274 }
1275
1276 static const GDBusInterfaceVTable desc_interface_vtable = {
1277         __bt_gatt_desc_method_call,
1278         NULL,
1279         NULL,
1280         { 0 }
1281 };
1282
1283 static const GDBusInterfaceVTable char_interface_vtable = {
1284         __bt_gatt_char_method_call,
1285         NULL,
1286         NULL,
1287         { 0 }
1288 };
1289
1290 static const GDBusInterfaceVTable serv_interface_vtable = {
1291         NULL,
1292         NULL,
1293         NULL,
1294         { 0 }
1295 };
1296
1297 static const GDBusInterfaceVTable manager_interface_vtable = {
1298         __bt_gatt_manager_method_call,
1299         NULL,
1300         NULL,
1301         { 0 }
1302 };
1303
1304 static GDBusNodeInfo *__bt_gatt_create_method_node_info(
1305                                 const gchar *introspection_data)
1306 {
1307         GError *err = NULL;
1308         GDBusNodeInfo *node_info = NULL;
1309
1310         if (introspection_data == NULL)
1311                 return NULL;
1312
1313
1314         BT_DBG("Create new node info");
1315         node_info = g_dbus_node_info_new_for_xml(introspection_data, &err);
1316
1317         if (err) {
1318                 BT_ERR("Unable to create node: %s", err->message);
1319                 g_clear_error(&err);
1320                 return NULL;
1321         }
1322
1323         return node_info;
1324 }
1325
1326 static struct gatt_service_info *__bt_gatt_find_gatt_service_info(
1327                         const char *service_path)
1328 {
1329         GSList *l;
1330
1331         for (l = gatt_services; l != NULL; l = l->next) {
1332                 struct gatt_service_info *info = l->data;
1333
1334                 if (g_strcmp0(info->serv_path, service_path) == 0)
1335                         return info;
1336         }
1337         BT_ERR("Gatt service not found");
1338         return NULL;
1339 }
1340
1341 static struct gatt_char_info *__bt_gatt_find_gatt_char_info(
1342                         const char *service_path, const char *char_path)
1343 {
1344         GSList *l1, *l2;
1345
1346         for (l1 = gatt_services; l1 != NULL; l1 = l1->next) {
1347                 struct gatt_service_info *serv_info = l1->data;
1348
1349                 if (g_strcmp0(serv_info->serv_path, service_path) == 0) {
1350
1351                         for (l2 = serv_info->char_data; l2 != NULL; l2 = l2->next) {
1352                                 struct gatt_char_info *char_info = l2->data;
1353
1354                                 if (g_strcmp0(char_info->char_path, char_path) == 0)
1355                                         return char_info;
1356                         }
1357                         BT_ERR("Gatt characteristic not found");
1358                         return NULL;
1359                 }
1360         }
1361         BT_ERR("Gatt service not found");
1362         return NULL;
1363 }
1364
1365 static struct gatt_desc_info *__bt_gatt_find_gatt_desc_info(
1366                         const char *serv_path, const char *char_path,
1367                         const char *desc_path)
1368 {
1369         GSList *l1, *l2, *l3;
1370
1371         for (l1 = gatt_services; l1 != NULL; l1 = l1->next) {
1372                 struct gatt_service_info *serv_info = l1->data;
1373
1374                 if (g_strcmp0(serv_info->serv_path, serv_path) == 0) {
1375                         for (l2 = serv_info->char_data; l2 != NULL; l2 = l2->next) {
1376                                 struct gatt_char_info *char_info = l2->data;
1377
1378                                 if (g_strcmp0(char_info->char_path, char_path) == 0) {
1379                                         for (l3 = char_info->desc_data; l3 != NULL; l3 = l3->next) {
1380                                                 struct gatt_desc_info *desc_info = l3->data;
1381                                                 if (g_strcmp0(desc_info->desc_path,
1382                                                         desc_path) == 0) {
1383                                                         return desc_info;
1384                                                 }
1385                                         }
1386                                 }
1387                         }
1388                 }
1389         }
1390         BT_ERR("Gatt descriptor not found");
1391         return NULL;
1392 }
1393
1394 static struct gatt_req_info *__bt_gatt_find_request_info(guint request_id)
1395 {
1396         GSList *l;
1397
1398         for (l = gatt_requests; l != NULL; l = l->next) {
1399                 struct gatt_req_info *req_info = l->data;
1400
1401                 if (req_info && req_info->request_id == request_id)
1402                         return req_info;
1403         }
1404         BT_ERR("Gatt Request not found");
1405         return NULL;
1406 }
1407
1408 static GDBusProxy *__bt_gatt_gdbus_init_manager_proxy(const gchar *service,
1409                                 const gchar *path, const gchar *interface)
1410 {
1411         GDBusProxy *proxy;
1412         GError *err = NULL;
1413
1414         g_conn = _bt_get_system_shared_conn();
1415         if (!g_conn) {
1416                 BT_ERR("Unable to get connection");
1417                 return NULL;
1418         }
1419
1420         proxy =  g_dbus_proxy_new_sync(g_conn,
1421                         G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES, NULL,
1422                         service, path,
1423                         interface, NULL, &err);
1424
1425         if (!proxy) {
1426                 if (err) {
1427                         BT_ERR("Unable to create proxy: %s", err->message);
1428                         g_clear_error(&err);
1429                 }
1430                 return NULL;
1431         }
1432         manager_gproxy = proxy;
1433
1434         return proxy;
1435 }
1436
1437 static GDBusProxy *__bt_gatt_gdbus_get_manager_proxy(const gchar *service,
1438                                 const gchar *path, const gchar *interface)
1439 {
1440         return (manager_gproxy) ? manager_gproxy :
1441                         __bt_gatt_gdbus_init_manager_proxy(service,
1442                                 path, interface);
1443 }
1444
1445 static gboolean __bt_gatt_is_service_registered(const char *service_path)
1446 {
1447         struct gatt_service_info *svc_info = NULL;
1448
1449         svc_info = __bt_gatt_find_gatt_service_info(service_path);
1450
1451         if (svc_info != NULL) {
1452                 BT_DBG("Return the state of the gatt service %d",
1453                         svc_info->is_svc_registered);
1454                 return svc_info->is_svc_registered;
1455         }
1456
1457         BT_DBG("gatt service info is NULL");
1458         return FALSE;
1459 }
1460
1461 void get_service_cb(GObject *object, GAsyncResult *res, gpointer user_data)
1462 {
1463         GError *error = NULL;
1464         GVariant *result;
1465         GVariantIter *iter = NULL;
1466         const gchar *key = NULL;
1467         GVariant *value = NULL;
1468         const gchar *service = NULL;
1469         const gchar *characteristic = NULL;
1470         const gchar *descriptor = NULL;
1471         int n_char = 1;
1472
1473         BT_DBG(" ");
1474         result = g_dbus_proxy_call_finish(G_DBUS_PROXY(object), res, &error);
1475
1476         if (result == NULL) {
1477                 BT_ERR("Dbus-RPC is failed");
1478                 if (error != NULL) {
1479                         BT_ERR("D-Bus API failure: errCode[%x], message[%s]",
1480                                         error->code, error->message);
1481                         g_clear_error(&error);
1482                 }
1483         } else {
1484                 char *char_cmp = NULL;
1485                 g_variant_get(result, "(a{sv})", &iter);
1486                 char_cmp = g_strdup_printf("Characteristic%d", n_char);
1487
1488                 while (g_variant_iter_loop(iter, "{sv}", &key, &value)) {
1489                         if (g_strcmp0(key, "Service") == 0) {
1490                                 service = g_variant_get_string(value, NULL);
1491                                 BT_DBG("Service %s", service);
1492                         } else if (g_strcmp0(key, char_cmp) == 0) {
1493                                 characteristic = g_variant_get_string(value, NULL);
1494                                 g_free(char_cmp);
1495                                 char_cmp = g_strdup_printf("Characteristic%d", ++n_char);
1496                                 BT_DBG("%s", characteristic);
1497                         } else if (g_strcmp0(key, "Descriptor") == 0) {
1498                                 descriptor = g_variant_get_string(value, NULL);
1499                                 BT_DBG("Descriptor %s", descriptor);
1500                         }
1501                 }
1502                 g_variant_iter_free(iter);
1503
1504                 /* TODO: Store the service informationa and
1505                  * Send respponse to CAPI layer. */
1506
1507                 g_variant_unref(result);
1508                 if (char_cmp)
1509                         g_free(char_cmp);
1510         }
1511 }
1512 void register_application_cb(GObject *object, GAsyncResult *res, gpointer user_data)
1513 {
1514         BT_INFO("RegisterApplication is completed");
1515
1516         GError *error = NULL;
1517         GVariant *result;
1518
1519         if (register_cancel) {
1520                 g_object_unref(register_cancel);
1521                 register_cancel = NULL;
1522         }
1523
1524         result = g_dbus_proxy_call_finish(G_DBUS_PROXY(object), res, &error);
1525         if (result == NULL) {
1526                 BT_ERR("Dbus-RPC is failed");
1527                 if (error != NULL) {
1528                         BT_ERR("D-Bus API failure: errCode[%x], message[%s]",
1529                                         error->code, error->message);
1530                         g_clear_error(&error);
1531                 }
1532                 is_server_started = false;
1533         } else {
1534                 g_variant_unref(result);
1535         }
1536 }
1537
1538 BT_EXPORT_API int bluetooth_gatt_unregister_application(void)
1539 {
1540         GDBusProxy *proxy = NULL;
1541
1542         if (is_server_started) {
1543                 GVariant *ret;
1544                 GError *err = NULL;
1545
1546                 if (app_path == NULL) {
1547                         BT_ERR("app_path is NULL");
1548                         return BLUETOOTH_ERROR_INTERNAL;
1549                 }
1550
1551                 proxy = __bt_gatt_gdbus_get_manager_proxy("org.bluez",
1552                                 "/org/bluez/hci0", GATT_MNGR_INTERFACE);
1553                 if (proxy == NULL) {
1554                         BT_ERR("proxy is NULL");
1555                         return BLUETOOTH_ERROR_INTERNAL;
1556                 }
1557
1558                 BT_INFO("UnregisterApplication");
1559
1560                 /* Async Call to Unregister Service */
1561                 ret = g_dbus_proxy_call_sync(proxy, "UnregisterApplication",
1562                                 g_variant_new("(o)", app_path), G_DBUS_CALL_FLAGS_NONE, -1,
1563                                 NULL, &err);
1564                 if (ret == NULL) {
1565                         BT_ERR("dBUS-RPC is failed");
1566                         if (err != NULL) {
1567                                 BT_ERR("D-Bus API failure: errCode[%x], message[%s]",
1568                                                 err->code, err->message);
1569                                 g_clear_error(&err);
1570                         }
1571                         return BLUETOOTH_ERROR_INTERNAL;
1572                 }
1573                 g_variant_unref(ret);
1574
1575                 is_server_started = false;
1576
1577                 BT_INFO("UnregisterApplication is completed");
1578
1579                 return BLUETOOTH_ERROR_NONE;
1580         }
1581
1582         BT_INFO("GATT server not started");
1583         return BLUETOOTH_ERROR_NONE;
1584 }
1585
1586 BT_EXPORT_API int bluetooth_gatt_init(void)
1587 {
1588         GError *error = NULL;
1589         GDBusNodeInfo *node_info = NULL;
1590
1591         if (app_path != NULL) {
1592                 BT_ERR("app path already exists! initialized");
1593                 return BLUETOOTH_ERROR_ALREADY_INITIALIZED;
1594         }
1595
1596         g_conn = _bt_get_system_shared_conn();
1597         if (!g_conn) {
1598                 BT_ERR("Unable to get connection");
1599                 goto failed;
1600         }
1601
1602         if (owner_id == 0) {
1603                 gchar *name = g_strdup_printf("%s.p%d", BT_GATT_SERVICE_NAME, getpid());
1604                 BT_DBG("well-known name: %s", name);
1605
1606                 owner_id = g_bus_own_name_on_connection(g_conn, name,
1607                                         G_BUS_NAME_OWNER_FLAGS_NONE,
1608                                         NULL, NULL, NULL, NULL);
1609                 g_free(name);
1610         }
1611         BT_DBG("owner_id is [%d]", owner_id);
1612
1613         app_path = g_strdup_printf("/com/%d", getpid());
1614
1615         serv_id = 1;
1616
1617         /* Register ObjectManager interface */
1618         node_info = __bt_gatt_create_method_node_info(
1619                                         manager_introspection_xml);
1620         if (node_info == NULL) {
1621                 BT_ERR("failed to get node info");
1622                 goto failed;
1623         }
1624
1625         if (manager_id == 0) {
1626                 BT_INFO("manager_id does not exists");
1627
1628                 manager_id = g_dbus_connection_register_object(g_conn, app_path,
1629                                                         node_info->interfaces[0],
1630                                                         &manager_interface_vtable,
1631                                                         NULL, NULL, &error);
1632         }
1633         g_dbus_node_info_unref(node_info);
1634         if (manager_id == 0) {
1635                 BT_ERR("failed to register: %s", error->message);
1636                 g_error_free(error);
1637                 goto failed;
1638         }
1639
1640         return BLUETOOTH_ERROR_NONE;
1641
1642 failed:
1643         if (owner_id)
1644                 g_bus_unown_name(owner_id);
1645
1646         g_free(app_path);
1647
1648         app_path = NULL;
1649         owner_id = 0;
1650
1651         return BLUETOOTH_ERROR_INTERNAL;
1652 }
1653
1654 BT_EXPORT_API int bluetooth_gatt_deinit()
1655 {
1656         int ret = BLUETOOTH_ERROR_NONE;
1657
1658         if (register_cancel) {
1659                 g_cancellable_cancel(register_cancel);
1660                 g_object_unref(register_cancel);
1661                 register_cancel = NULL;
1662         }
1663
1664         if (owner_id == 0) {
1665                 BT_ERR("owner_id is zero");
1666                 return BLUETOOTH_ERROR_NOT_FOUND;
1667         }
1668
1669         BT_DBG("Removing all registered gatt services");
1670         bluetooth_gatt_delete_services();
1671
1672         /* Unregister the exported interface for object manager */
1673         if (manager_id) {
1674                 g_dbus_connection_unregister_object(g_conn, manager_id);
1675                 manager_id = 0;
1676         }
1677
1678         ret = bluetooth_gatt_unregister_application();
1679         if (ret != BLUETOOTH_ERROR_NONE) {
1680                 BT_ERR("Fail to unregister application");
1681         }
1682
1683         g_bus_unown_name(owner_id);
1684         owner_id = 0;
1685
1686         g_free(app_path);
1687         app_path = NULL;
1688
1689         if (manager_gproxy) {
1690                 g_object_unref(manager_gproxy);
1691                 manager_gproxy = NULL;
1692         }
1693
1694         BT_DBG("-");
1695         return ret;
1696 }
1697
1698 BT_EXPORT_API int bluetooth_gatt_add_service(const char *svc_uuid,
1699                         char **svc_path)
1700 {
1701         GError *error = NULL;
1702         guint object_id;
1703         GDBusNodeInfo *node_info;
1704         gchar *path = NULL;
1705         GVariantBuilder *builder = NULL;
1706         GVariantBuilder *builder1 = NULL;
1707         GVariantBuilder *inner_builder = NULL;
1708         gboolean svc_primary = TRUE;
1709         struct gatt_service_info *serv_info = NULL;
1710
1711         node_info = __bt_gatt_create_method_node_info(
1712                                         service_introspection_xml);
1713         if (node_info == NULL)
1714                 return BLUETOOTH_ERROR_INTERNAL;
1715
1716         path = g_strdup_printf("%s"GATT_SERV_OBJECT_PATH"%d", app_path, serv_id++);
1717         BT_DBG("gatt service path is [%s]", path);
1718
1719         object_id = g_dbus_connection_register_object(g_conn, path,
1720                                         node_info->interfaces[0],
1721                                         &serv_interface_vtable,
1722                                         NULL, NULL, &error);
1723         g_dbus_node_info_unref(node_info);
1724
1725         if (object_id == 0) {
1726                 BT_ERR("failed to register: %s", error->message);
1727                 g_error_free(error);
1728                 g_free(path);
1729
1730                 return BLUETOOTH_ERROR_INTERNAL;
1731         }
1732
1733         /* Add object_id/gatt service information; it's required at the time of
1734          *  service unregister and Getmanagedobjects
1735          */
1736         serv_info = g_new0(struct gatt_service_info, 1);
1737
1738         serv_info->serv_path = g_strdup(path);
1739         serv_info->serv_id = object_id;
1740         serv_info->service_uuid = g_strdup(svc_uuid);
1741         serv_info->is_svc_registered = FALSE;
1742         serv_info->is_svc_primary = svc_primary;
1743
1744         gatt_services = g_slist_append(gatt_services, serv_info);
1745
1746         /* emit interfacesadded signal here for service path */
1747         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
1748         inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1749
1750         g_variant_builder_add(inner_builder, "{sv}",
1751                 "UUID", g_variant_new_string(svc_uuid));
1752
1753         g_variant_builder_add(inner_builder, "{sv}",
1754                 "Primary", g_variant_new_boolean(svc_primary));
1755
1756         builder1 = g_variant_builder_new(G_VARIANT_TYPE("ao"));
1757
1758         g_variant_builder_add(inner_builder, "{sv}", "Characteristics",
1759                                 g_variant_new("ao", builder1));
1760
1761         g_variant_builder_add(builder, "{sa{sv}}",
1762                 GATT_SERV_INTERFACE, inner_builder);
1763
1764         g_dbus_connection_emit_signal(g_conn, NULL, "/",
1765                                 "org.freedesktop.Dbus.ObjectManager",
1766                                 "InterfacesAdded",
1767                                 g_variant_new("(oa{sa{sv}})",
1768                                 path, builder),
1769                                 &error);
1770         if (error != NULL) {
1771                 /* dbus gives error cause */
1772                 BT_ERR("d-bus api failure: errcode[%x], message[%s]",
1773                                 error->code, error->message);
1774                 g_clear_error(&error);
1775         }
1776
1777         new_service = TRUE;
1778
1779         *svc_path = g_strdup(path);
1780
1781         g_free(path);
1782         g_variant_builder_unref(inner_builder);
1783         g_variant_builder_unref(builder);
1784         g_variant_builder_unref(builder1);
1785
1786         return BLUETOOTH_ERROR_NONE;
1787 }
1788
1789 BT_EXPORT_API int bluetooth_gatt_add_new_characteristic(
1790                         const char *svc_path, const char *char_uuid,
1791                         bt_gatt_permission_t permissions,
1792                         bt_gatt_characteristic_property_t properties,
1793                         char **char_path)
1794 {
1795         static int char_id;
1796         GError *error = NULL;
1797         guint object_id;
1798         GDBusNodeInfo *node_info;
1799         gchar *path = NULL;
1800         GVariantBuilder *builder = NULL;
1801         GVariantBuilder *inner_builder = NULL;
1802         struct gatt_service_info *serv_info = NULL;
1803         struct gatt_char_info *char_info = NULL;
1804         GVariantBuilder *builder2 = NULL;
1805         GVariantBuilder *builder3 = NULL;
1806         GVariant *flags_val = NULL;
1807         int i = 0;
1808         char *char_flags[NUMBER_OF_FLAGS];
1809         int flag_count = 0;
1810
1811         if (new_service) {
1812                 char_id = 1;
1813                 new_service = FALSE;
1814         }
1815
1816         BT_DBG("gatt svc_path path is [%s]", svc_path);
1817         serv_info = __bt_gatt_find_gatt_service_info(svc_path);
1818         if (serv_info == NULL)
1819                 return BLUETOOTH_ERROR_INVALID_PARAM;
1820
1821         node_info = __bt_gatt_create_method_node_info(
1822                                         characteristics_introspection_xml);
1823         if (node_info == NULL)
1824                 return BLUETOOTH_ERROR_INTERNAL;
1825
1826         path = g_strdup_printf("%s/characteristic%d", svc_path, char_id++);
1827         BT_DBG("gatt characteristic path is [%s]", path);
1828
1829         object_id = g_dbus_connection_register_object(g_conn, path,
1830                                         node_info->interfaces[0],
1831                                         &char_interface_vtable,
1832                                         NULL, NULL, &error);
1833         g_dbus_node_info_unref(node_info);
1834
1835         if (object_id == 0) {
1836                 BT_ERR("failed to register: %s", error->message);
1837                 g_error_free(error);
1838                 g_free(path);
1839
1840                 return BLUETOOTH_ERROR_INTERNAL;
1841         }
1842
1843         if (permissions & BLUETOOTH_GATT_PERMISSION_ENCRYPT_READ)
1844                 properties |= BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_READ;
1845         if (permissions & BLUETOOTH_GATT_PERMISSION_ENCRYPT_AUTHENTICATED_READ)
1846                 properties |= BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_READ;
1847         if (permissions & BLUETOOTH_GATT_PERMISSION_ENCRYPT_WRITE)
1848                 properties |= BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_WRITE;
1849         if (permissions & BLUETOOTH_GATT_PERMISSION_ENCRYPT_AUTHENTICATED_WRITE)
1850                 properties |= BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_WRITE;
1851
1852         flag_count = bluetooth_gatt_convert_prop2string(properties, char_flags);
1853
1854         char_info = g_new0(struct gatt_char_info, 1);
1855
1856         char_info->char_path = g_strdup(path);
1857         char_info->char_id = object_id;
1858         char_info->char_uuid = g_strdup(char_uuid);
1859
1860         for (i = 0; i < flag_count; i++)
1861                 char_info->char_flags[i] = char_flags[i];
1862
1863
1864         char_info->flags_length = flag_count;
1865
1866         serv_info->char_data = g_slist_append(serv_info->char_data, char_info);
1867
1868         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
1869         inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1870
1871         g_variant_builder_add(inner_builder, "{sv}", "UUID",
1872                                 g_variant_new("s", char_uuid));
1873         g_variant_builder_add(inner_builder, "{sv}", "Service",
1874                                 g_variant_new("o", svc_path));
1875
1876         builder2 = g_variant_builder_new(G_VARIANT_TYPE("as"));
1877
1878         for (i = 0; i < flag_count; i++)
1879                 g_variant_builder_add(builder2, "s", char_flags[i]);
1880
1881         flags_val = g_variant_new("as", builder2);
1882         g_variant_builder_add(inner_builder, "{sv}", "Flags",
1883                                 flags_val);
1884
1885         builder3 = g_variant_builder_new(G_VARIANT_TYPE("ao"));
1886
1887         g_variant_builder_add(inner_builder, "{sv}", "Descriptors",
1888                                 g_variant_new("ao", builder3));
1889
1890         g_variant_builder_add(builder, "{sa{sv}}",
1891                                 GATT_CHAR_INTERFACE,
1892                                 inner_builder);
1893
1894         g_dbus_connection_emit_signal(g_conn, NULL, "/",
1895                                 "org.freedesktop.Dbus.ObjectManager",
1896                                 "InterfacesAdded",
1897                                 g_variant_new("(oa{sa{sv}})",
1898                                 path, builder),
1899                                 &error);
1900         if (error) {
1901                 /* dBUS gives error cause */
1902                 BT_ERR("Could not Emit Signal: errCode[%x], message[%s]",
1903                                 error->code, error->message);
1904                 g_clear_error(&error);
1905         }
1906
1907         *char_path = g_strdup(path);
1908
1909         new_char = TRUE;
1910
1911         g_free(path);
1912
1913         g_variant_builder_unref(inner_builder);
1914         g_variant_builder_unref(builder);
1915         g_variant_builder_unref(builder2);
1916         g_variant_builder_unref(builder3);
1917
1918         return BLUETOOTH_ERROR_NONE;
1919 }
1920
1921 BT_EXPORT_API int bluetooth_gatt_set_characteristic_value(
1922                         const char *characteristic, const char *char_value,
1923                         int     value_length)
1924 {
1925         gchar **line_argv = NULL;
1926         char *serv_path = NULL;
1927         struct gatt_char_info *char_info = NULL;
1928         GVariantBuilder *builder1 = NULL;
1929         GVariantBuilder *builder = NULL;
1930         GVariantBuilder *inner_builder = NULL;
1931         GVariant *char_val = NULL;
1932         GError *error = NULL;
1933         int i = 0;
1934         int res = BLUETOOTH_ERROR_NONE;
1935
1936         line_argv = g_strsplit_set(characteristic, "/", 0);
1937         serv_path = g_strdup_printf("/%s/%s/%s", line_argv[1], line_argv[2], line_argv[3]);
1938
1939         char_info = __bt_gatt_find_gatt_char_info(serv_path, characteristic);
1940
1941         if (char_info == NULL) {
1942                 /* Fix : RESOURCE_LEAK */
1943                 res = BLUETOOTH_ERROR_INVALID_PARAM;
1944                 goto done;
1945         }
1946
1947         char_info->value_length = value_length;
1948
1949         char_info->char_value = (char *)malloc(value_length);
1950         /* Fix : NULL_RETURNS */
1951         if (char_info->char_value == NULL) {
1952                 res = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1953                 goto done;
1954         }
1955
1956         for (i = 0; i < value_length; i++)
1957                 char_info->char_value[i] = char_value[i];
1958
1959         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
1960         inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1961
1962         builder1 = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
1963
1964         for (i = 0; i < value_length; i++)
1965                 g_variant_builder_add(builder1, "y", char_value[i]);
1966
1967         char_val = g_variant_new("ay", builder1);
1968         g_variant_builder_add(inner_builder, "{sv}", "Value", char_val);
1969
1970         g_variant_builder_add(builder, "{sa{sv}}",
1971                         GATT_CHAR_INTERFACE,
1972                         inner_builder);
1973
1974         g_dbus_connection_emit_signal(g_conn, NULL, "/",
1975                         "org.freedesktop.Dbus.ObjectManager",
1976                         "InterfacesAdded",
1977                         g_variant_new("(oa{sa{sv}})",
1978                                 char_info->char_path, builder),
1979                         &error);
1980
1981         if (error) {
1982                 /* dBUS gives error cause */
1983                 BT_ERR("Could not Emit Signal: errCode[%x], message[%s]",
1984                                 error->code, error->message);
1985                 g_clear_error(&error);
1986         }
1987         g_variant_builder_unref(inner_builder);
1988         g_variant_builder_unref(builder);
1989         g_variant_builder_unref(builder1);
1990 done:
1991         g_strfreev(line_argv);
1992         g_free(serv_path);
1993
1994         return res;
1995 }
1996
1997 BT_EXPORT_API int bluetooth_gatt_add_descriptor(
1998                         const char *char_path, const char *desc_uuid,
1999                         bt_gatt_permission_t permissions,
2000                         char **desc_path)
2001 {
2002         static int desc_id = 1;
2003         GError *error = NULL;
2004         guint object_id;
2005         GDBusNodeInfo *node_info;
2006         gchar *path = NULL;
2007         GVariantBuilder *builder = NULL;
2008         GVariantBuilder *inner_builder = NULL;
2009         struct gatt_char_info *char_info = NULL;
2010         struct gatt_desc_info *desc_info = NULL;
2011         gchar **line_argv = NULL;
2012         char *serv_path;
2013         GVariantBuilder *builder2 = NULL;
2014         GVariant *flags_val = NULL;
2015         int i = 0;
2016         char *desc_flags[NUMBER_OF_FLAGS];
2017         int flag_count = 0;
2018
2019         if (new_char) {
2020                 desc_id = 1;
2021                 new_char = FALSE;
2022         }
2023
2024         line_argv = g_strsplit_set(char_path, "/", 0);
2025         serv_path = g_strdup_printf("/%s/%s/%s", line_argv[1], line_argv[2], line_argv[3]);
2026
2027         char_info = __bt_gatt_find_gatt_char_info(serv_path, char_path);
2028         if (char_info == NULL) {
2029                 g_strfreev(line_argv);
2030                 g_free(serv_path);
2031                 return BLUETOOTH_ERROR_INVALID_PARAM;
2032         }
2033
2034         node_info = __bt_gatt_create_method_node_info(
2035                                         descriptor_introspection_xml);
2036         if (node_info == NULL) {
2037                 g_strfreev(line_argv);
2038                 g_free(serv_path);
2039                 return BLUETOOTH_ERROR_INTERNAL;
2040         }
2041
2042         path = g_strdup_printf("%s/descriptor%d", char_path, desc_id++);
2043         BT_DBG("gatt descriptor path is [%s]", path);
2044
2045         object_id = g_dbus_connection_register_object(g_conn, path,
2046                                 node_info->interfaces[0],
2047                                 &desc_interface_vtable,
2048                                 NULL, NULL, &error);
2049         g_dbus_node_info_unref(node_info);
2050
2051         if (object_id == 0) {
2052                 BT_ERR("failed to register: %s", error->message);
2053                 g_error_free(error);
2054                 g_free(path);
2055                 g_strfreev(line_argv);
2056                 g_free(serv_path);
2057
2058                 return BLUETOOTH_ERROR_INTERNAL;
2059         }
2060
2061         flag_count = bluetooth_gatt_convert_perm2string(permissions, desc_flags);
2062
2063         desc_info = g_new0(struct gatt_desc_info, 1);
2064
2065         desc_info->desc_path = g_strdup(path);
2066         desc_info->desc_id = object_id;
2067         desc_info->desc_uuid = g_strdup(desc_uuid);
2068
2069         for (i = 0; i < flag_count; i++)
2070                 desc_info->desc_flags[i] = desc_flags[i];
2071
2072         desc_info->flags_length = flag_count;
2073
2074         char_info->desc_data = g_slist_append(char_info->desc_data, desc_info);
2075
2076         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
2077         inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
2078
2079         g_variant_builder_add(inner_builder, "{sv}", "UUID",
2080                                 g_variant_new("s", desc_uuid));
2081         g_variant_builder_add(inner_builder, "{sv}", "Characteristic",
2082                                 g_variant_new("o", char_path));
2083
2084         builder2 = g_variant_builder_new(G_VARIANT_TYPE("as"));
2085
2086         for (i = 0; i < flag_count; i++)
2087                 g_variant_builder_add(builder2, "s", desc_flags[i]);
2088
2089         flags_val = g_variant_new("as", builder2);
2090         g_variant_builder_add(inner_builder, "{sv}", "Flags",
2091                                 flags_val);
2092
2093         g_variant_builder_add(builder, "{sa{sv}}",
2094                                 GATT_DESC_INTERFACE,
2095                                 inner_builder);
2096
2097         g_dbus_connection_emit_signal(g_conn, NULL, "/",
2098                                 "org.freedesktop.Dbus.ObjectManager",
2099                                 "InterfacesAdded",
2100                                 g_variant_new("(oa{sa{sv}})",
2101                                 path, builder),
2102                                 &error);
2103         if (error) {
2104                 /* dBUS gives error cause */
2105                 BT_ERR("Could not Emit Signal: errCode[%x], message[%s]",
2106                                 error->code, error->message);
2107                 g_clear_error(&error);
2108         }
2109
2110         *desc_path = g_strdup(path);
2111
2112         g_free(path);
2113         g_free(serv_path);
2114         g_strfreev(line_argv);
2115         g_variant_builder_unref(inner_builder);
2116         g_variant_builder_unref(builder);
2117         g_variant_builder_unref(builder2);
2118
2119         return BLUETOOTH_ERROR_NONE;
2120 }
2121
2122 BT_EXPORT_API int bluetooth_gatt_set_descriptor_value(
2123                         const char *desc_path, const char *desc_value,
2124                         int value_length)
2125 {
2126         GError *error = NULL;
2127         GVariantBuilder *builder = NULL;
2128         GVariantBuilder *inner_builder = NULL;
2129         GVariantBuilder *builder1 = NULL;
2130         struct gatt_desc_info *desc_info = NULL;
2131         gchar **line_argv = NULL;
2132         char *char_path;
2133         GVariant *desc_val = NULL;
2134         char *serv_path = NULL;
2135         int i ;
2136
2137         line_argv = g_strsplit_set(desc_path, "/", 0);
2138         serv_path = g_strdup_printf("/%s/%s/%s", line_argv[1], line_argv[2], line_argv[3]);
2139         char_path = g_strdup_printf("%s/%s", serv_path, line_argv[4]);
2140
2141         desc_info = __bt_gatt_find_gatt_desc_info(serv_path, char_path, desc_path);
2142
2143         /* Free the allocated memory */
2144         g_strfreev(line_argv);
2145         g_free(serv_path);
2146         g_free(char_path);
2147
2148         /* Fix : NULL_RETURNS */
2149         retv_if(desc_info == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
2150
2151         desc_info->desc_value = (char *)malloc(value_length);
2152
2153         /* Fix : NULL_RETURNS */
2154         retv_if(desc_info->desc_value == NULL, BLUETOOTH_ERROR_MEMORY_ALLOCATION);
2155
2156         for (i = 0; i < value_length; i++)
2157                 desc_info->desc_value[i] = desc_value[i];
2158
2159         desc_info->value_length = value_length;
2160
2161         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
2162         inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
2163
2164         builder1 = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
2165
2166         for (i = 0; i < value_length; i++)
2167                 g_variant_builder_add(builder1, "y", desc_value[i]);
2168
2169         desc_val = g_variant_new("ay", builder1);
2170         g_variant_builder_add(inner_builder, "{sv}", "Value", desc_val);
2171
2172         g_variant_builder_add(builder, "{sa{sv}}",
2173                                 GATT_DESC_INTERFACE,
2174                                 inner_builder);
2175
2176         g_dbus_connection_emit_signal(g_conn, NULL, "/",
2177                                 "org.freedesktop.Dbus.ObjectManager",
2178                                 "InterfacesAdded",
2179                                 g_variant_new("(oa{sa{sv}})",
2180                                         desc_info->desc_path, builder),
2181                                 &error);
2182
2183         if (error != NULL) {
2184                 BT_ERR("D-Bus API failure: errCode[%x], \
2185                                 message[%s]",
2186                                 error->code, error->message);
2187                 g_clear_error(&error);
2188         }
2189
2190         g_variant_builder_unref(inner_builder);
2191         g_variant_builder_unref(builder);
2192         g_variant_builder_unref(builder1);
2193
2194         return BLUETOOTH_ERROR_NONE;
2195 }
2196
2197 int bluetooth_gatt_get_service(const char *svc_uuid)
2198 {
2199         GDBusProxy *proxy = NULL;
2200         gchar *uuid = NULL;
2201
2202         proxy = __bt_gatt_gdbus_get_manager_proxy("org.bluez",
2203                                         "/org/bluez/hci0", GATT_MNGR_INTERFACE);
2204         if (proxy == NULL)
2205                 return BLUETOOTH_ERROR_INTERNAL;
2206
2207         uuid = g_strdup(svc_uuid);
2208
2209         g_dbus_proxy_call(proxy,
2210                         "GetService",
2211                         g_variant_new("(s)",
2212                         uuid),
2213                         G_DBUS_CALL_FLAGS_NONE, -1,
2214                         NULL,
2215                         (GAsyncReadyCallback) get_service_cb,
2216                         NULL);
2217
2218         g_free(uuid);
2219
2220         return BLUETOOTH_ERROR_NONE;
2221 }
2222
2223 BT_EXPORT_API int bluetooth_gatt_register_service(const char *svc_path)
2224 {
2225         struct gatt_service_info *svc_info = NULL;
2226
2227         if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_GATT_REGISTER_SERVICE)
2228                         == BLUETOOTH_ERROR_PERMISSION_DEINED) {
2229                 BT_ERR("Don't have aprivilege to use this API");
2230                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
2231         }
2232
2233         svc_info = __bt_gatt_find_gatt_service_info(svc_path);
2234         if (svc_info == NULL) {
2235                 BT_ERR("Cannot find service [%s]", svc_path);
2236                 return BLUETOOTH_ERROR_INTERNAL;
2237         }
2238         svc_info->is_svc_registered = TRUE;
2239
2240         return BLUETOOTH_ERROR_NONE;
2241 }
2242
2243 BT_EXPORT_API int bluetooth_gatt_register_application(void)
2244 {
2245         GDBusProxy *proxy = NULL;
2246
2247         if (!is_server_started) {
2248                 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_GATT_REGISTER_APPLICATION)
2249                                 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
2250                         BT_ERR("Don't have aprivilege to use this API");
2251                         return BLUETOOTH_ERROR_PERMISSION_DEINED;
2252                 }
2253
2254                 if (g_slist_length(gatt_services) == 0) {
2255                         BT_ERR("There is no registered service");
2256                         return BLUETOOTH_ERROR_INTERNAL;
2257                 }
2258
2259                 if (app_path == NULL) {
2260                         BT_ERR("app_path is NULL");
2261                         return BLUETOOTH_ERROR_INTERNAL;
2262                 }
2263
2264                 proxy = __bt_gatt_gdbus_get_manager_proxy("org.bluez",
2265                                 "/org/bluez/hci0", GATT_MNGR_INTERFACE);
2266                 if (proxy == NULL) {
2267                         BT_ERR("proxy is NULL");
2268                         return BLUETOOTH_ERROR_INTERNAL;
2269                 }
2270
2271                 BT_INFO("RegisterApplication");
2272
2273                 if (register_cancel) {
2274                         g_cancellable_cancel(register_cancel);
2275                         g_object_unref(register_cancel);
2276                 }
2277                 register_cancel = g_cancellable_new();
2278
2279                 g_dbus_proxy_call(proxy, "RegisterApplication",
2280                                 g_variant_new("(oa{sv})", app_path, NULL),
2281                                 G_DBUS_CALL_FLAGS_NONE, -1, register_cancel,
2282                                 (GAsyncReadyCallback)register_application_cb, NULL);
2283
2284                 is_server_started = true;
2285
2286                 return BLUETOOTH_ERROR_NONE;
2287         }
2288
2289         BT_INFO("Already RegisterApplication");
2290
2291         return BLUETOOTH_ERROR_NONE;
2292 }
2293
2294 BT_EXPORT_API int bluetooth_gatt_delete_services(void)
2295 {
2296         GSList *l;
2297         int ret = BLUETOOTH_ERROR_NONE;
2298
2299         if (gatt_services == NULL) {
2300                 BT_DBG("There are no registered services");
2301                 serv_id = 1;
2302                 return ret;
2303         }
2304
2305         for (l = gatt_services; l != NULL; ) {
2306                 struct gatt_service_info *info = l->data;
2307
2308                 // In __bt_gatt_unregister_service, current node will be removed.
2309                 // Go forward to next node before calling __bt_gatt_unregister_service.
2310                 l = l->next;
2311                 if (__bt_gatt_unregister_service(info) != BLUETOOTH_ERROR_NONE) {
2312                         ret = BLUETOOTH_ERROR_INTERNAL;
2313                 }
2314         }
2315         BT_INFO("All services are removed : %d", ret);
2316
2317         g_slist_free(gatt_services);
2318         gatt_services = NULL;
2319         serv_id = 1;
2320
2321         return ret;
2322 }
2323
2324 BT_EXPORT_API int bluetooth_gatt_update_characteristic(
2325                         const char *char_path, const char* char_value,
2326                         int value_length)
2327 {
2328         GVariantBuilder *outer_builder;
2329         GVariantBuilder *inner_builder;
2330         GVariantBuilder *invalidated_builder;
2331         GVariant *update_value = NULL;
2332         GError *error = NULL;
2333         gboolean ret = FALSE;
2334         int err = BLUETOOTH_ERROR_NONE;
2335         int i = 0;
2336         gchar **line_argv = NULL;
2337         gchar *serv_path = NULL;
2338         const char *value = NULL;
2339
2340         line_argv = g_strsplit_set(char_path, "/", 0);
2341         serv_path = g_strdup_printf("/%s/%s/%s", line_argv[1], line_argv[2], line_argv[3]);
2342
2343         if (!__bt_gatt_is_service_registered(serv_path)) {
2344                 BT_DBG("service not registered for this characteristic");
2345                 g_free(serv_path);
2346                 g_strfreev(line_argv);
2347                 return BLUETOOTH_ERROR_INTERNAL;
2348         }
2349
2350         outer_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
2351         invalidated_builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
2352
2353         inner_builder = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
2354         for (i = 0; i < value_length; i++)
2355                 g_variant_builder_add(inner_builder, "y", char_value[i]);
2356
2357         update_value = g_variant_new("ay", inner_builder);
2358
2359         g_variant_builder_add(outer_builder, "{sv}", "Value",
2360                                         update_value);
2361
2362         BT_DBG("Updating characteristic value");
2363         ret = g_dbus_connection_emit_signal(g_conn, NULL,
2364                                         char_path,
2365                                         "org.freedesktop.DBus.Properties",
2366                                         "PropertiesChanged",
2367                                         g_variant_new("(sa{sv}as)",
2368                                         "org.bluez.GattCharacteristic1",
2369                                         outer_builder, invalidated_builder),
2370                                         &error);
2371
2372         if (!ret) {
2373                 if (error != NULL) {
2374                         BT_ERR("D-Bus API failure: errCode[%x], \
2375                                         message[%s]",
2376                                         error->code, error->message);
2377                         g_clear_error(&error);
2378                 }
2379                 err = BLUETOOTH_ERROR_INTERNAL;
2380         } else {
2381                 struct gatt_char_info *char_info = NULL;
2382
2383                 char_info = __bt_gatt_find_gatt_char_info(serv_path, char_path);
2384                 if (char_info == NULL) {
2385                         g_free(serv_path);
2386                         g_strfreev(line_argv);
2387                         g_variant_builder_unref(inner_builder);
2388                         g_variant_builder_unref(outer_builder);
2389                         g_variant_builder_unref(invalidated_builder);
2390
2391                         return BLUETOOTH_ERROR_INVALID_DATA;
2392                 }
2393
2394                 char_info->value_length = value_length;
2395
2396                 value = (char *)realloc(char_info->char_value, value_length);
2397                 if (value == NULL) {
2398                         g_free(serv_path);
2399                         g_strfreev(line_argv);
2400                         g_variant_builder_unref(inner_builder);
2401                         g_variant_builder_unref(outer_builder);
2402                         g_variant_builder_unref(invalidated_builder);
2403
2404                         return BLUETOOTH_ERROR_MEMORY_ALLOCATION;
2405                 }
2406
2407                 char_info->char_value = (char*)value;
2408                 if (char_info->char_value) {
2409                         for (i = 0; i < value_length; i++)
2410                                 char_info->char_value[i] = char_value[i];
2411                 }
2412         }
2413
2414         g_free(serv_path);
2415         g_strfreev(line_argv);
2416         g_variant_builder_unref(inner_builder);
2417         g_variant_builder_unref(outer_builder);
2418         g_variant_builder_unref(invalidated_builder);
2419
2420         return err;
2421 }
2422
2423 static void __bt_gatt_free_descriptor_info(struct gatt_desc_info *desc_info)
2424 {
2425         int i;
2426
2427         if (!desc_info)
2428                 return;
2429
2430         g_free(desc_info->desc_path);
2431         g_free(desc_info->desc_uuid);
2432         g_free(desc_info->desc_value);
2433
2434         for (i = 0; i < desc_info->flags_length; i++)
2435                 g_free(desc_info->desc_flags[i]);
2436
2437         g_free(desc_info);
2438 }
2439
2440 static void __bt_gatt_free_characteristic_info(struct gatt_char_info *char_info)
2441 {
2442         int i;
2443
2444         if (!char_info)
2445                 return;
2446
2447         g_free(char_info->char_path);
2448         g_free(char_info->char_uuid);
2449         g_free(char_info->char_value);
2450
2451         for (i = 0; i < char_info->flags_length; i++)
2452                 g_free(char_info->char_flags[i]);
2453
2454         g_free(char_info);
2455 }
2456
2457 static void __bt_gatt_free_service_info(struct gatt_service_info *svc_info)
2458 {
2459         if (!svc_info)
2460                 return;
2461
2462         g_free(svc_info->serv_path);
2463         g_free(svc_info->service_uuid);
2464         g_free(svc_info);
2465 }
2466
2467 static void __desc_info_free(gpointer data, gpointer user_data)
2468 {
2469         struct gatt_desc_info *desc_info = data;
2470         int *err = user_data;
2471         int ret;
2472
2473         if (desc_info == NULL)
2474                 return;
2475
2476         ret = g_dbus_connection_unregister_object(g_conn, desc_info->desc_id);
2477         if (ret) {
2478                 __bt_gatt_emit_interface_removed(desc_info->desc_path, GATT_DESC_INTERFACE);
2479         } else {
2480                 *err = BLUETOOTH_ERROR_INTERNAL;
2481         }
2482         __bt_gatt_free_descriptor_info(desc_info);
2483 }
2484
2485 static void __char_info_free(gpointer data, gpointer user_data)
2486 {
2487         struct gatt_char_info *char_info = data;
2488         int *err = user_data;
2489         int ret;
2490
2491         if (char_info == NULL)
2492                 return;
2493
2494         g_slist_foreach(char_info->desc_data, __desc_info_free, user_data);
2495         g_slist_free(char_info->desc_data);
2496         char_info->desc_data = NULL;
2497
2498         ret = g_dbus_connection_unregister_object(g_conn, char_info->char_id);
2499         if (ret) {
2500                 __bt_gatt_emit_interface_removed(char_info->char_path, GATT_CHAR_INTERFACE);
2501         } else {
2502                 *err = BLUETOOTH_ERROR_INTERNAL;
2503         }
2504         __bt_gatt_free_characteristic_info(char_info);
2505 }
2506
2507 static int __bt_gatt_unregister_service(struct gatt_service_info *svc_info)
2508 {
2509         int ret = BLUETOOTH_ERROR_NONE;
2510
2511         if (svc_info == NULL) {
2512                 BT_ERR("svc_info is NULL");
2513                 return BLUETOOTH_ERROR_NOT_FOUND;
2514         }
2515
2516         if (svc_info->is_svc_registered == FALSE) {
2517                 BT_ERR("%s is not registered", svc_info->serv_path);
2518                 return BLUETOOTH_ERROR_NOT_FOUND;
2519         }
2520
2521         BT_DBG("svc_path %s", svc_info->serv_path);
2522
2523         g_slist_foreach(svc_info->char_data, __char_info_free, &ret);
2524         g_slist_free(svc_info->char_data);
2525         svc_info->char_data = NULL;
2526
2527         if (g_dbus_connection_unregister_object(g_conn, svc_info->serv_id) == FALSE) {
2528                 BT_ERR("Cannot unregister object for [%s]", svc_info->serv_path);
2529                 ret = BLUETOOTH_ERROR_INTERNAL;
2530         } else {
2531                 __bt_gatt_emit_interface_removed(svc_info->serv_path, GATT_SERV_INTERFACE);
2532         }
2533
2534         gatt_services = g_slist_remove(gatt_services, svc_info);
2535         __bt_gatt_free_service_info(svc_info);
2536
2537         new_service = FALSE;
2538
2539         if (gatt_services == NULL) {
2540                 serv_id = 1;
2541         } else if (gatt_services->next == NULL) {
2542                 serv_id--;
2543         }
2544
2545         return ret;
2546 }
2547
2548 BT_EXPORT_API int bluetooth_gatt_unregister_service(const char *svc_path)
2549 {
2550         struct gatt_service_info *svc_info;
2551         int ret = BLUETOOTH_ERROR_NONE;
2552
2553         BT_DBG("+");
2554
2555         svc_info = __bt_gatt_find_gatt_service_info(svc_path);
2556
2557         ret = __bt_gatt_unregister_service(svc_info);
2558         if (ret != BLUETOOTH_ERROR_NONE) {
2559                 BT_ERR("Could not unregister service [%s]", svc_path);
2560         }
2561
2562         BT_DBG("-");
2563         return ret;
2564 }
2565
2566 BT_EXPORT_API int bluetooth_gatt_send_response(int request_id, guint req_type,
2567                                         int resp_state, int offset, char *value, int value_length)
2568 {
2569         struct gatt_req_info *req_info = NULL;
2570
2571         if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_GATT_SEND_RESPONSE)
2572                         == BLUETOOTH_ERROR_PERMISSION_DEINED) {
2573                 BT_ERR("Don't have aprivilege to use this API");
2574                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
2575         }
2576
2577         req_info = __bt_gatt_find_request_info(request_id);
2578         if (req_info == NULL) {
2579                 BT_ERR("Coundn't find request id [%d]", request_id);
2580                 return BLUETOOTH_ERROR_INTERNAL;
2581         }
2582
2583         if (resp_state != BLUETOOTH_ATT_ERROR_NONE) {
2584                 BT_ERR("resp_state is 0x%X", resp_state);
2585                 char err_msg[20] = { 0, };
2586                 g_snprintf(err_msg, sizeof(err_msg), "ATT error: 0x%02x", resp_state);
2587                 g_dbus_method_invocation_return_dbus_error(req_info->context,
2588                                                 "org.bluez.Error.Failed", err_msg);
2589
2590                 gatt_requests = g_slist_remove(gatt_requests, req_info);
2591
2592                 req_info->context = NULL;
2593                 if (req_info->attr_path)
2594                         g_free(req_info->attr_path);
2595                 if (req_info->svc_path)
2596                         g_free(req_info->svc_path);
2597                 g_free(req_info);
2598
2599                 return BLUETOOTH_ERROR_NONE;
2600         }
2601
2602         if (req_type == BLUETOOTH_GATT_ATT_REQUEST_TYPE_READ) {
2603                 int i;
2604                 GVariantBuilder *inner_builder = NULL;
2605                 inner_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
2606                 if (value_length > 0 && value != NULL) {
2607                         for (i = 0; i < value_length; i++)
2608                                 g_variant_builder_add(inner_builder, "y", value[i]);
2609                 }
2610                 g_dbus_method_invocation_return_value(req_info->context,
2611                                 g_variant_new("(ay)", inner_builder));
2612                 g_variant_builder_unref(inner_builder);
2613         } else {
2614                 g_dbus_method_invocation_return_value(req_info->context, NULL);
2615         }
2616         gatt_requests = g_slist_remove(gatt_requests, req_info);
2617
2618         req_info->context = NULL;
2619         if (req_info->attr_path)
2620                 g_free(req_info->attr_path);
2621         if (req_info->svc_path)
2622                 g_free(req_info->svc_path);
2623         g_free(req_info);
2624
2625         return BLUETOOTH_ERROR_NONE;
2626 }
2627
2628 BT_EXPORT_API int bluetooth_gatt_server_set_notification(const char *char_path,
2629                                                 bluetooth_device_address_t *unicast_address)
2630 {
2631         GVariantBuilder *outer_builder;
2632         GVariantBuilder *invalidated_builder;
2633         GError *error = NULL;
2634         gboolean notify = TRUE;
2635         gboolean ret = TRUE;
2636         int err = BLUETOOTH_ERROR_NONE;
2637         gchar **line_argv = NULL;
2638         gchar *serv_path = NULL;
2639         char addr[20] = { 0 };
2640
2641         line_argv = g_strsplit_set(char_path, "/", 0);
2642         serv_path = g_strdup_printf("/%s/%s/%s", line_argv[1], line_argv[2], line_argv[3]);
2643
2644         if (!__bt_gatt_is_service_registered(serv_path)) {
2645                 BT_DBG("service not registered for this characteristic");
2646                 g_free(serv_path);
2647                 g_strfreev(line_argv);
2648                 return BLUETOOTH_ERROR_INTERNAL;
2649         }
2650
2651         g_free(serv_path);
2652
2653         outer_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
2654         invalidated_builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
2655
2656         g_variant_builder_add(outer_builder, "{sv}", "Notifying",
2657                                         g_variant_new("b", notify));
2658
2659         if (unicast_address) {
2660                 _bt_convert_addr_type_to_string(addr,
2661                                         (unsigned char *)unicast_address->addr);
2662         }
2663         g_variant_builder_add(outer_builder, "{sv}", "Unicast",
2664                                 g_variant_new("s", addr));
2665
2666         BT_DBG("Set characteristic Notification");
2667         ret = g_dbus_connection_emit_signal(g_conn, NULL,
2668                                         char_path,
2669                                         "org.freedesktop.DBus.Properties",
2670                                         "PropertiesChanged",
2671                                         g_variant_new("(sa{sv}as)",
2672                                         "org.bluez.GattCharacteristic1",
2673                                         outer_builder, invalidated_builder),
2674                                         &error);
2675
2676         if (!ret) {
2677                 if (error != NULL) {
2678                         BT_ERR("D-Bus API failure: errCode[%x], \
2679                                         message[%s]",
2680                                         error->code, error->message);
2681                         g_clear_error(&error);
2682                 }
2683                 err = BLUETOOTH_ERROR_INTERNAL;
2684         }
2685
2686         g_strfreev(line_argv);
2687         g_variant_builder_unref(outer_builder);
2688         g_variant_builder_unref(invalidated_builder);
2689
2690         return err;
2691 }
2692
2693
2694 #if 0
2695 BT_EXPORT_API int bluetooth_gatt_register_application(int instance_id)
2696 {
2697         BT_INIT_PARAMS();
2698
2699         if (!is_server_started) {
2700
2701                 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_GATT_REGISTER_APPLICATION)
2702                                 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
2703                         BT_ERR("Don't have aprivilege to use this API");
2704                         return BLUETOOTH_ERROR_PERMISSION_DEINED;
2705                 }
2706
2707                 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
2708                 g_array_append_vals(in_param1, &instance_id, sizeof(int));
2709
2710                 ret = _bt_send_request(BT_BLUEZ_SERVICE, BT_GATT_REGISTER_APPLICATION,
2711                                 in_param1, in_param2, in_param3, in_param4, &out_param);
2712                 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
2713
2714                 if (ret != BLUETOOTH_ERROR_NONE) {
2715                         BT_ERR("Register application failed");
2716                         return ret;
2717                 }
2718                 is_server_started = true;
2719
2720                 return BLUETOOTH_ERROR_NONE;
2721         }
2722
2723         BT_INFO("Already RegisterApplication");
2724         return BLUETOOTH_ERROR_NONE;
2725 }
2726 #endif
2727
2728 BT_EXPORT_API int bluetooth_gatt_server_init(int *instance_id, gatt_server_cb_func_ptr callback_ptr,
2729                                                 void *user_data)
2730 {
2731         int ret = BLUETOOTH_ERROR_NONE;
2732
2733         BT_INIT_PARAMS();
2734         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
2735
2736         /* Register event handler for GATT */
2737         ret = _bt_register_event(BT_GATT_SERVER_EVENT, (void *)callback_ptr, user_data);
2738
2739         if (ret != BLUETOOTH_ERROR_NONE &&
2740                         ret != BLUETOOTH_ERROR_ALREADY_INITIALIZED) {
2741                 BT_ERR("Fail to init the event handler");
2742                 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
2743                 goto done;
2744         }
2745
2746         ret = _bt_send_request(BT_BLUEZ_SERVICE, BT_GATT_SERVER_REGISTER,
2747                         in_param1, in_param2, in_param3, in_param4, &out_param);
2748
2749         /* App ID -1 is invalid */
2750         if (ret != BLUETOOTH_ERROR_NONE) {
2751                 BT_INFO("GATT Server Registration failed result [%d]", ret);
2752                 *instance_id = -1;
2753         } else {
2754                 *instance_id = g_array_index(out_param, int, 0);
2755                 BT_INFO("GATT Server Registered successfully: App Instance ID [%d]", *instance_id);
2756         }
2757
2758 done:
2759         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
2760
2761         BT_INFO("GATT Server instance ID obtained [%d]", *instance_id);
2762         return ret;
2763 }
2764
2765 BT_EXPORT_API int bluetooth_gatt_server_deinit(void)
2766 {
2767         int ret;
2768         BT_INFO("GATT Server Deinitialize");
2769         /* Unregister the event */
2770         ret = _bt_unregister_event(BT_GATT_SERVER_EVENT);
2771
2772         if (ret != BLUETOOTH_ERROR_NONE) {
2773                 BT_ERR("Fail to deinit the event handler");
2774                 return ret;
2775         }
2776
2777         _bt_set_user_data(BT_GATT_SERVER, NULL, NULL);
2778
2779         return ret;
2780 }
2781
2782 BT_EXPORT_API int bluetooth_gatt_server_add_service(const char *svc_uuid, int type, int numhandles,
2783                 int instance_id, int *service_handle)
2784 {
2785         BT_CHECK_ENABLED(return);
2786         BT_CHECK_PARAMETER(svc_uuid, return);
2787
2788         int result;
2789         char uuid[BT_GATT_ATT_UUID_LEN_MAX + 1];
2790
2791         g_strlcpy(uuid, svc_uuid, sizeof(uuid));
2792
2793         BT_INIT_PARAMS();
2794         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
2795
2796         g_array_append_vals(in_param1, &type, sizeof(int));
2797         g_array_append_vals(in_param2, &numhandles, sizeof(int));
2798         g_array_append_vals(in_param3, uuid, BT_GATT_ATT_UUID_LEN_MAX + 1);
2799         g_array_append_vals(in_param4, &instance_id, sizeof(int));
2800
2801         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_GATT_SERVER_ADD_SERVICE,
2802                         in_param1, in_param2, in_param3, in_param4, &out_param);
2803
2804         /* ATT handle 0 is reserved, hence it can not be used by app.
2805            It will be used to indicate error in regsitering attribute */
2806         if (result != BLUETOOTH_ERROR_NONE)
2807                 *service_handle = 0;
2808         else
2809                 *service_handle = g_array_index(out_param, int, 0);
2810
2811         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
2812
2813         return result;
2814 }
2815
2816 BT_EXPORT_API int bluetooth_gatt_server_add_new_characteristic(const char *char_uuid,
2817                 const bluetooth_gatt_server_attribute_params_t *param,
2818                                                 int *char_handle)
2819 {
2820         BT_CHECK_ENABLED(return);
2821         BT_CHECK_PARAMETER(char_uuid, return);
2822         BT_CHECK_PARAMETER(param, return);
2823
2824         int result;
2825         char uuid[BT_GATT_ATT_UUID_LEN_MAX + 1];
2826         int flag_count = 0;
2827         char *char_flags[NUMBER_OF_FLAGS];
2828
2829         g_strlcpy(uuid, char_uuid, sizeof(uuid));
2830         flag_count = bluetooth_gatt_convert_prop2string(param->properties, char_flags);
2831         BT_INFO("Flag count [%d]", flag_count);
2832
2833         BT_INIT_PARAMS();
2834         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
2835
2836         g_array_append_vals(in_param1, param, sizeof(bluetooth_gatt_server_attribute_params_t));
2837         g_array_append_vals(in_param2, uuid, BT_GATT_ATT_UUID_LEN_MAX + 1);
2838
2839         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_GATT_SERVER_ADD_CHARACTERISTIC,
2840                         in_param1, in_param2, in_param3, in_param4, &out_param);
2841
2842         /* ATT handle 0 is reserved, hence it can not be used by app.
2843            It will be used to indicate error in regsitering attribute */
2844         if (result != BLUETOOTH_ERROR_NONE) {
2845                 BT_ERR("GATT Server Add characteristic failed.. result [%d]", result);
2846                 *char_handle = 0;
2847         } else {
2848                 *char_handle = g_array_index(out_param, int, 0);
2849                 BT_DBG("GATT Server Add characteristic success result [%d] char chandle [%d]", result, *char_handle);
2850         }
2851
2852         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
2853         return result;
2854 }
2855
2856 BT_EXPORT_API int bluetooth_gatt_server_add_descriptor(const char *desc_uuid, bt_gatt_permission_t permissions,
2857                 int service_handle, int instance_id, int *descriptor_handle)
2858 {
2859         BT_CHECK_ENABLED(return);
2860         BT_CHECK_PARAMETER(desc_uuid, return);
2861
2862         int result;
2863         char uuid[BT_GATT_ATT_UUID_LEN_MAX + 1];
2864
2865         g_strlcpy(uuid, desc_uuid, sizeof(uuid));
2866
2867         BT_INIT_PARAMS();
2868         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
2869
2870         g_array_append_vals(in_param1, &service_handle, sizeof(int));
2871         g_array_append_vals(in_param2, &instance_id, sizeof(int));
2872         g_array_append_vals(in_param3, &permissions, sizeof(bt_gatt_permission_t));
2873         g_array_append_vals(in_param4, uuid, BT_GATT_ATT_UUID_LEN_MAX + 1);
2874
2875         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_GATT_SERVER_ADD_DESCRIPTOR,
2876                         in_param1, in_param2, in_param3, in_param4, &out_param);
2877
2878         /* ATT handle 0 is reserved, hence it can not be used by app.
2879            It will be used to indicate error in regsitering attribute */
2880         if (result != BLUETOOTH_ERROR_NONE) {
2881                 BT_ERR("GATT Server Add Descriptor failed.. result [%d] desc handle [%d]", result, *descriptor_handle);
2882                 *descriptor_handle = 0;
2883         } else {
2884                 *descriptor_handle = g_array_index(out_param, int, 0);
2885                 BT_INFO("GATT Server Add Descriptor Successful.. result [%d] desc handle [%d]", result, *descriptor_handle);
2886         }
2887
2888         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
2889
2890         return result;
2891 }
2892
2893 BT_EXPORT_API int bluetooth_gatt_server_start_service(int service_handle, int instance_id)
2894 {
2895         BT_CHECK_ENABLED(return);
2896         int result;
2897
2898         BT_INIT_PARAMS();
2899         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
2900
2901         g_array_append_vals(in_param1, &service_handle, sizeof(int));
2902         g_array_append_vals(in_param2, &instance_id, sizeof(int));
2903
2904         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_GATT_SERVER_START_SERVICE,
2905                         in_param1, in_param2, in_param3, in_param4, &out_param);
2906
2907         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
2908
2909         return result;
2910 }
2911
2912 BT_EXPORT_API int bluetooth_gatt_server_send_response(const bluetooth_gatt_server_response_params_t *param,
2913                 const bluetooth_gatt_att_data_t *value)
2914 {
2915         BT_CHECK_PARAMETER(param, return);
2916         BT_CHECK_PARAMETER(value, return);
2917         BT_CHECK_ENABLED(return);
2918         int result;
2919
2920         BT_INIT_PARAMS();
2921         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
2922
2923         g_array_append_vals(in_param1, value, sizeof(bluetooth_gatt_att_data_t));
2924         g_array_append_vals(in_param2, param, sizeof(bluetooth_gatt_server_response_params_t));
2925
2926         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_GATT_SERVER_SEND_RESPONSE,
2927                         in_param1, in_param2, in_param3, in_param4, &out_param);
2928
2929         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
2930
2931         return result;
2932
2933 }
2934
2935 BT_EXPORT_API int bluetooth_gatt_server_send_indication(bluetooth_device_address_t *addr_hex,
2936                 const bluetooth_gatt_server_indication_params_t *param,
2937                 const bluetooth_gatt_att_data_t *att_value)
2938 {
2939         BT_CHECK_PARAMETER(param, return);
2940         BT_CHECK_PARAMETER(att_value, return);
2941         BT_CHECK_ENABLED(return);
2942         int result = 0 ;
2943         char addr[BLUETOOTH_ADDRESS_STRING_LENGTH] ;
2944         int fd = -1;
2945
2946         BT_INIT_PARAMS();
2947         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
2948
2949         g_array_append_vals(in_param1, att_value, sizeof(bluetooth_gatt_att_data_t));
2950         g_array_append_vals(in_param2, param, sizeof(bluetooth_gatt_server_indication_params_t));
2951         g_array_append_vals(in_param3, addr_hex, sizeof(bluetooth_device_address_t));
2952
2953         _bt_convert_addr_type_to_string(addr, addr_hex->addr);
2954         fd =  bluetooth_get_characteristic_fd(param->atrribute_handle, addr);
2955
2956         if (fd > -1)
2957                 result  = bluetooth_gatt_write_characteristics_value_to_fd_(fd, att_value->data, att_value->length, NULL);
2958         else
2959                 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_GATT_SERVER_SEND_INDICATION,
2960                                 in_param1, in_param2, in_param3, in_param4, &out_param);
2961
2962         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
2963
2964         return result;
2965 }
2966
2967 BT_EXPORT_API int bluetooth_gatt_server_stop_service(int service_handle, int instance_id)
2968 {
2969         BT_CHECK_ENABLED(return);
2970         int result;
2971
2972         BT_INIT_PARAMS();
2973         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
2974
2975         g_array_append_vals(in_param1, &service_handle, sizeof(int));
2976         g_array_append_vals(in_param2, &instance_id, sizeof(int));
2977
2978         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_GATT_SERVER_STOP_SERVICE,
2979                         in_param1, in_param2, in_param3, in_param4, &out_param);
2980
2981         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
2982
2983         return result;
2984 }
2985
2986 BT_EXPORT_API int bluetooth_gatt_server_delete_service(int service_handle, int instance_id)
2987 {
2988         BT_CHECK_ENABLED(return);
2989         int result;
2990
2991         BT_INIT_PARAMS();
2992         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
2993
2994         g_array_append_vals(in_param1, &service_handle, sizeof(int));
2995         g_array_append_vals(in_param2, &instance_id, sizeof(int));
2996
2997         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_GATT_SERVER_DELETE_SERVICE,
2998                         in_param1, in_param2, in_param3, in_param4, &out_param);
2999
3000         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
3001
3002         return result;
3003 }
3004
3005 /* Tizen Platform Specific */
3006 BT_EXPORT_API int bluetooth_gatt_server_update_characteristic(int instance_id,
3007                                 const bluetooth_gatt_server_update_value_t *value)
3008 {
3009         BT_CHECK_ENABLED(return);
3010         BT_CHECK_PARAMETER(value, return);
3011         int result;
3012
3013         BT_INIT_PARAMS();
3014         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
3015
3016         g_array_append_vals(in_param1, &instance_id, sizeof(int));
3017         g_array_append_vals(in_param2, value, sizeof(bluetooth_gatt_server_update_value_t));
3018
3019         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_GATT_SERVER_UPDATE_VALUE,
3020                         in_param1, in_param2, in_param3, in_param4, &out_param);
3021
3022         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
3023
3024         return result;
3025 }
3026
3027 BT_EXPORT_API int bluetooth_gatt_server_unregister(int instance_id)
3028 {
3029         BT_CHECK_ENABLED(return);
3030         int result;
3031
3032         BT_INIT_PARAMS();
3033         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
3034
3035         g_array_append_vals(in_param1, &instance_id, sizeof(int));
3036
3037         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_GATT_SERVER_DEREGISTER,
3038                         in_param1, in_param2, in_param3, in_param4, &out_param);
3039
3040         if (result != BLUETOOTH_ERROR_NONE)
3041                 BT_INFO("GATT Server Unregistration failed result [%d]", result);
3042         else
3043                 BT_INFO("GATT Server Unregistration successful");
3044
3045         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
3046         return result;
3047 }
3048
3049
3050 static gboolean bluetooth_gatt_server_acquire_channel_write_cb(GIOChannel *gio,
3051                                         GIOCondition cond, gpointer data)
3052 {
3053
3054         bluetooth_gatt_server_acquire_write_info_t *write_data =  (bluetooth_gatt_server_acquire_write_info_t*)data;
3055
3056         BT_INFO("FD io write data  received  remote adress  [%s]\n", write_data->address);
3057
3058         if (cond & G_IO_IN) {
3059                 GIOStatus status = G_IO_STATUS_NORMAL;
3060                 GError *err = NULL;
3061                 char *buffer = NULL;
3062                 gsize len = 0;
3063                 int BUF = BLUETOOTH_GATT_ATT_DATA_LENGTH_MAX;
3064
3065                 buffer = g_malloc0(BUF);
3066
3067                 status = g_io_channel_read_chars(gio, buffer,
3068                                 BUF, &len, &err);
3069
3070                 if (status != G_IO_STATUS_NORMAL) {
3071                         BT_ERR("IO Channel read is failed with %d", status);
3072                         g_free(buffer);
3073                         if (err) {
3074                                 BT_ERR("IO Channel read error [%s]", err->message);
3075                                 if (status == G_IO_STATUS_ERROR) {
3076                                         BT_ERR("cond : %d", cond);
3077                                         g_error_free(err);
3078                                         g_io_channel_shutdown(gio, TRUE, NULL);
3079                                         g_io_channel_unref(gio);
3080
3081                                         return FALSE;
3082                                 }
3083                                 g_error_free(err);
3084                         }
3085                         return FALSE;
3086                 }
3087
3088                 if (len > 0) {
3089
3090                         BT_INFO(" FD io sending  value changed %s %zd \n", buffer, len);
3091
3092
3093                         bluetooth_gatt_server_write_requested_info_t write_info;
3094                         if (len < BLUETOOTH_GATT_ATT_DATA_LENGTH_MAX)
3095                         memcpy(write_info.data.data, buffer, len);
3096
3097                         write_info.length = len;
3098                         write_info.need_resp = false;
3099                         write_info.attribute_handle = write_data->attribute_handle;
3100                         //memcpy()
3101                         _bt_convert_addr_string_to_type(write_info.device_address.addr,  write_data->address);
3102                         write_info.connection_id = write_data->connection_id;
3103                         write_info.offset = write_data->offset;
3104                         write_info.request_id = -2;
3105
3106                         BT_INFO("ACQUIRING EVENT \n");
3107
3108                         bt_event_info_t *event_info;
3109                         event_info = _bt_event_get_cb_data(BT_GATT_SERVER_EVENT);
3110
3111                         if (event_info) {
3112
3113                                 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED,
3114                                                         BLUETOOTH_ERROR_NONE, &write_info,
3115                                                 event_info->cb, event_info->user_data);
3116                         } else {
3117                                 BT_ERR("eventinfo failed");
3118                         }
3119
3120
3121                 }
3122                 g_free(buffer);
3123
3124                 return TRUE;
3125         }
3126
3127         if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
3128                 BT_ERR("Error : GIOCondition %d, ]", cond);
3129                 g_io_channel_shutdown(gio, TRUE, NULL);
3130                 g_io_channel_unref(gio);
3131
3132                 return FALSE;
3133         }
3134
3135         return TRUE;
3136 }
3137
3138 void  bluetooth_gatt_server_send_acquire_write_response(GVariant * parameters)
3139 {
3140         int con_id  =  -1;
3141         int tran_id  =  -1;
3142         int att_han  =  -1;
3143         int pipefd[2] = {-1,};
3144         int mtu  = -1;
3145         int offset  = -1;
3146         char err_msg[512] = {'\0'};
3147         GIOChannel *channel = NULL;
3148         char *addr = NULL;
3149         int result =  -1;
3150
3151         g_variant_get(parameters, "(iiiiii&s)",
3152                                         &result,
3153                                         &con_id,
3154                                         &tran_id,
3155                                         &att_han,
3156                                         &mtu,
3157                                         &offset,
3158                                         &addr);
3159
3160         BT_DBG("GATT Server  Acquire Write From Remote Client [%s]", addr);
3161         BT_DBG("GATT ServerAcquire  Conn ID:   [%d]", con_id);
3162         BT_DBG("GATT Server Acquire write  att handle:[%d]", att_han);
3163         BT_DBG("GATT Server Acquire Write Offset:    [%d]", offset);
3164
3165
3166         if (socketpair(AF_UNIX, SOCK_STREAM, 0, pipefd) < 0) {
3167                         strerror_r(errno, err_msg, sizeof(err_msg));
3168                         BT_ERR("socketpair(): %s", err_msg);
3169                         return ;
3170         }
3171
3172         BT_INIT_PARAMS();
3173         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
3174
3175         //param1 = g_array_new(TRUE, TRUE, sizeof(gchar));
3176         bluetooth_gatt_server_acquire_response_params_t  data;
3177         data.req_type  = BLUETOOTH_GATT_REQUEST_TYPE_ACQUIRE_WRITE;
3178         data.fd = pipefd[1];
3179         data.mtu = mtu;
3180         data.request_id = tran_id;
3181
3182         bluetooth_gatt_server_acquire_write_info_t  *write_info = g_malloc0(sizeof(bluetooth_gatt_server_acquire_write_info_t))  ;
3183
3184         write_info->attribute_handle = att_han;
3185         write_info->connection_id  = tran_id;
3186         write_info->offset = offset;
3187
3188          memcpy(write_info->address,  addr ,  BLUETOOTH_ADDRESS_STRING_LENGTH);
3189
3190         BT_INFO("FD read %d   remote address  [%s ] \n", pipefd[0], addr);
3191
3192
3193         channel = g_io_channel_unix_new(pipefd[0]);
3194         g_io_channel_set_encoding(channel, NULL, NULL);
3195         g_io_channel_set_buffered(channel, FALSE);
3196         g_io_channel_set_close_on_unref(channel, TRUE);
3197         g_io_channel_set_flags(channel, G_IO_FLAG_NONBLOCK, NULL);
3198         g_io_add_watch(channel, (G_IO_IN | G_IO_ERR | G_IO_HUP),
3199                         bluetooth_gatt_server_acquire_channel_write_cb, write_info);
3200
3201
3202          GUnixFDList *fd_list = g_unix_fd_list_new();
3203          GError *error = NULL;
3204
3205         g_unix_fd_list_append(fd_list, pipefd[1], &error);
3206         g_assert_no_error(error);
3207         close(pipefd[1]);
3208
3209         g_array_append_vals(in_param1, &data, sizeof(bluetooth_gatt_server_acquire_response_params_t));
3210
3211         BT_INFO("Sending event BT_GATT_SERVER_ACQURE_WRITE_RESPONSE file descriptor value [%d] [ %s],", data.fd, addr);
3212
3213         result = _bt_send_request_with_unix_fd_list(BT_BLUEZ_SERVICE, BT_GATT_SERVER_ACQURE_WRITE_RESPONSE,
3214                         in_param1, in_param2, in_param3, in_param4, fd_list, &out_param, NULL);
3215
3216         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
3217
3218 }
3219
3220
3221
3222 void  bluetooth_gatt_server_send_acquire_notify_response(GVariant * parameters, bt_event_info_t *event_info)
3223 {
3224                         int con_id  =  -1;
3225                         int tran_id  =  -1;
3226                         int att_han  =  -1;
3227                         int pipefd[2] = {-1,};
3228                         int mtu  = -1;
3229                         int offset  = -1;
3230                         char err_msg[512] = {'\0'};
3231                         GIOChannel *channel = NULL;
3232                         int result =  -1;
3233                         int fd = -1;
3234                         bluetooth_gatt_acquire_notify_info_t *chr_info;
3235                         const char *address = NULL;
3236
3237                         g_variant_get(parameters, "(iiiiiis)",
3238                                                         &result,
3239                                                         &con_id,
3240                                                         &tran_id,
3241                                                         &att_han,
3242                                                         &mtu,
3243                                                         &offset,
3244                                                         &address);
3245
3246                                 BT_DBG("GATT ServerAcquire  Conn ID:   [%d]", con_id);
3247                                 BT_DBG("GATT Server Acquire notify  att handle:[%d]", att_han);
3248                                 BT_DBG("GATT Server Acquire Notify Offset:    [%d]", offset);
3249                                 BT_DBG("GATT Server Acquire Notify address:    [%s]", address);
3250
3251
3252                         if (socketpair(AF_UNIX, SOCK_STREAM, 0, pipefd) < 0) {
3253                                         strerror_r(errno, err_msg, sizeof(err_msg));
3254                                         BT_ERR("socketpair(): %s", err_msg);
3255                                         return ;
3256                                 }
3257
3258                                 fd = pipefd[0];
3259
3260                                 BT_INIT_PARAMS();
3261                                 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
3262
3263                                 //param1 = g_array_new(TRUE, TRUE, sizeof(gchar));
3264                                 bluetooth_gatt_server_acquire_response_params_t  data;
3265                                 data.req_type  = BLUETOOTH_GATT_REQUEST_TYPE_ACQUIRE_NOTIFY;
3266                                 data.fd = pipefd[1];
3267                                 data.mtu = mtu;
3268                                 data.request_id = tran_id;
3269
3270                                 BT_INFO("FD write %d   characterstics path   \n", pipefd[0]);
3271
3272                                 chr_info = bluetooth_get_characteristic_info_from_path(att_han);
3273                                 if (!chr_info) {
3274                                         chr_info = g_malloc0(sizeof(bluetooth_gatt_acquire_notify_info_t));
3275                                         chr_info->write_fd = fd;
3276                                         chr_info->att_hand = att_han;
3277
3278                                         gatt_characteristic_server_notify_list = g_slist_append(gatt_characteristic_server_notify_list, chr_info);
3279                                 } else
3280                                         chr_info->write_fd = fd;
3281
3282
3283                                 BT_INFO("setting up g_io channel");
3284                                 channel = g_io_channel_unix_new(fd);
3285                                 g_io_channel_set_encoding(channel, NULL, NULL);
3286                                 g_io_channel_set_buffered(channel, FALSE);
3287                                 g_io_channel_set_close_on_unref(channel, TRUE);
3288                                 g_io_channel_set_flags(channel, G_IO_FLAG_NONBLOCK, NULL);
3289                                 g_io_add_watch(channel, (G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL),
3290                                                 bluetooth_gatt_write_channel_watch_cb, chr_info);
3291
3292
3293
3294
3295                                  GUnixFDList *fd_list = g_unix_fd_list_new();
3296                                  GError *error = NULL;
3297
3298                                 g_unix_fd_list_append(fd_list, pipefd[1], &error);
3299                                 g_assert_no_error(error);
3300                                 close(pipefd[1]);
3301
3302                                 g_array_append_vals(in_param1, &data, sizeof(bluetooth_gatt_server_acquire_response_params_t));
3303
3304                                 BT_INFO("Sending event BT_GATT_SERVER_ACQUIRE_NOTIFY_RESPONSE file descriptor value [%d] ", data.fd);
3305
3306                                 result = _bt_send_request_with_unix_fd_list(BT_BLUEZ_SERVICE, BT_GATT_SERVER_ACQUIRE_NOTIFY_RESPONSE,
3307                                                 in_param1, in_param2, in_param3, in_param4, fd_list, &out_param, NULL);
3308
3309                                 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
3310
3311
3312                                 //send
3313                                 if (result == BLUETOOTH_ERROR_NONE) {
3314
3315                                         BT_INFO("sending gatt server notification state changed event");
3316                                         bluetooth_gatt_server_notification_changed_t info;
3317                                         bluetooth_device_address_t dev_address = { {0} };
3318                                         memset(&info, 0x00, sizeof(bluetooth_gatt_server_notification_changed_t));
3319
3320                                         _bt_convert_addr_string_to_type(dev_address.addr, address);
3321                                         memcpy(info.device_address.addr,
3322                                                         dev_address.addr,
3323                                                         BLUETOOTH_ADDRESS_LENGTH);
3324                                         info.handle = att_han;
3325                                         info.notification = TRUE;
3326
3327                                         _bt_gatt_server_event_cb(BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_STATE_CHANGED,
3328                                                         result, &info,
3329                                                         event_info->cb, event_info->user_data);
3330
3331                                 }
3332 }
3333
3334 void cleanup_gatt_acquire_fd(int handle)
3335 {
3336         bluetooth_gatt_acquire_notify_info_t *chr_info = NULL;
3337
3338         BT_INFO("+");
3339
3340         chr_info = bluetooth_get_characteristic_info_from_path(handle);
3341
3342         if (chr_info != NULL) {
3343                 BT_INFO("GATT Server: acquire notification char info found");
3344
3345                 if (chr_info->write_fd >= 0) {
3346                         BT_INFO("closing fd");
3347                         close(chr_info->write_fd);
3348                 }
3349
3350                 BT_INFO("Removing char_info from the list");
3351                 gatt_characteristic_server_notify_list = g_slist_remove(gatt_characteristic_server_notify_list, chr_info);
3352                 bluetooth_characteristic_info_free(chr_info);
3353         }
3354 }