2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include<glib/gprintf.h>
26 #include "bt-common.h"
27 #include "bt-internal-types.h"
29 #define NUMBER_OF_FLAGS 10
31 GDBusConnection *g_conn;
34 static gboolean new_service = FALSE;
35 static gboolean new_char = FALSE;
36 static int serv_id = 1;
37 static int register_pending_cnt = 0;
38 static bool is_server_started = false;
40 /* Introspection data for the service we are exporting */
41 static const gchar service_introspection_xml[] =
43 " <interface name='org.freedesktop.DBus.Properties'>"
44 " <property type='s' name='UUID' access='read'>"
46 " <property type='b' name='primary' access='read'>"
48 " <property type='o' name='Device' access='read'>"
50 " <property type='ao' name='Characteristics' access='read'>"
52 " <property type='s' name='Includes' access='read'>"
57 /* Introspection data for the characteristics we are exporting */
58 static const gchar characteristics_introspection_xml[] =
60 " <interface name='org.bluez.GattCharacteristic1'>"
61 " <method name='ReadValue'>"
62 " <arg type='s' name='address' direction='in'/>"
63 " <arg type='y' name='id' direction='in'/>"
64 " <arg type='q' name='offset' direction='in'/>"
65 " <arg type='ay' name='Value' direction='out'/>"
67 " <method name='WriteValue'>"
68 " <arg type='s' name='address' direction='in'/>"
69 " <arg type='y' name='id' direction='in'/>"
70 " <arg type='q' name='offset' direction='in'/>"
71 " <arg type='ay' name='value' direction='in'/>"
73 " <method name='StartNotify'>"
75 " <method name='StopNotify'>"
77 " <method name='IndicateConfirm'>"
78 " <arg type='s' name='address' direction='in'/>"
79 " <arg type='b' name='complete' direction='in'/>"
82 " <interface name='org.freedesktop.DBus.Properties'>"
83 " <property type='s' name='UUID' access='read'>"
85 " <property type='o' name='Service' access='read'>"
87 " <property type='ay' name='Value' access='readwrite'>"
89 " <property type='b' name='Notifying' access='read'>"
91 " <property type='as' name='Flags' access='read'>"
93 " <property type='s' name='Unicast' access='read'>"
95 " <property type='ao' name='Descriptors' access='read'>"
100 /* Introspection data for the descriptor we are exporting */
101 static const gchar descriptor_introspection_xml[] =
103 " <interface name='org.bluez.GattDescriptor1'>"
104 " <method name='ReadValue'>"
105 " <arg type='s' name='address' direction='in'/>"
106 " <arg type='y' name='id' direction='in'/>"
107 " <arg type='q' name='offset' direction='in'/>"
108 " <arg type='ay' name='Value' direction='out'/>"
110 " <method name='WriteValue'>"
111 " <arg type='s' name='address' direction='in'/>"
112 " <arg type='y' name='id' direction='in'/>"
113 " <arg type='q' name='offset' direction='in'/>"
114 " <arg type='ay' name='value' direction='in'/>"
117 " <interface name='org.freedesktop.DBus.Properties'>"
118 " <property type='s' name='UUID' access='read'>"
120 " <property type='o' name='Characteristic' access='read'>"
122 " <property type='ay' name='Value' access='read'>"
124 " <property type='as' name='Flags' access='read'>"
129 static const gchar manager_introspection_xml[] =
131 " <interface name='org.freedesktop.DBus.ObjectManager'>"
132 " <method name='GetManagedObjects'>"
133 " <arg type='a{oa{sa{sv}}}' name='object_paths_interfaces_and_properties' direction='out'/>"
138 struct gatt_service_info {
145 gboolean is_svc_registered;
146 gboolean is_svc_primary;
149 struct gatt_char_info {
154 gchar *char_flags[NUMBER_OF_FLAGS];
160 struct gatt_desc_info {
165 gchar *desc_flags[NUMBER_OF_FLAGS];
170 struct gatt_req_info {
175 GDBusMethodInvocation *context;
178 static GSList *gatt_services = NULL;
179 static GSList *gatt_requests = NULL;
180 static gchar *app_path = NULL;
182 #define BT_GATT_SERVICE_NAME "org.frwk.gatt_service"
183 #define BT_GATT_SERVICE_PATH "/org/frwk/gatt_service"
185 #define GATT_SERV_OBJECT_PATH "/service"
187 #define GATT_MNGR_INTERFACE "org.bluez.GattManager1"
188 #define GATT_SERV_INTERFACE "org.bluez.GattService1"
189 #define GATT_CHAR_INTERFACE "org.bluez.GattCharacteristic1"
190 #define GATT_DESC_INTERFACE "org.bluez.GattDescriptor1"
192 #ifdef TIZEN_FEATURE_BT_HPS
193 #define BT_HPS_OBJECT_PATH "/org/projectx/httpproxy"
194 #define BT_HPS_INTERFACE_NAME "org.projectx.httpproxy_service"
195 #define PROPERTIES_CHANGED "PropertiesChanged"
196 #define BT_HPS_PROPERTIES_INTERFACE "org.freedesktop.DBus.Properties"
199 static GDBusProxy *manager_gproxy = NULL;
201 static struct gatt_char_info *__bt_gatt_find_gatt_char_info(
202 const char *service_path, const char *char_path);
203 static struct gatt_desc_info *__bt_gatt_find_gatt_desc_info(
204 const char *serv_path, const char *char_path,
205 const char *desc_path);
207 static struct gatt_req_info *__bt_gatt_find_request_info(guint request_id);
209 static void __bt_gatt_close_gdbus_connection(void)
215 ret_if(g_conn == NULL);
217 if (!g_dbus_connection_flush_sync(g_conn, NULL, &err)) {
218 BT_ERR("Fail to flush the connection: %s", err->message);
223 if (!g_dbus_connection_close_sync(g_conn, NULL, &err)) {
225 BT_ERR("Fail to close the dbus connection: %s", err->message);
230 g_object_unref(g_conn);
237 #ifdef TIZEN_FEATURE_BT_HPS
238 static int __bt_send_event_to_hps(int event, GVariant *var)
240 GError *error = NULL;
241 GVariant *parameters;
242 GDBusMessage *msg = NULL;
246 retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
248 if (event == BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED) {
249 GVariantBuilder *inner_builder;
250 GVariantBuilder *invalidated_builder;
252 BT_DBG("BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED");
253 inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
255 g_variant_builder_add(inner_builder, "{sv}", "WriteValue", var);
257 invalidated_builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
259 parameters = g_variant_new("(a{sv}as)", inner_builder, invalidated_builder);
260 g_variant_builder_unref(invalidated_builder);
261 g_variant_builder_unref(inner_builder);
262 } else if (BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED) {
263 GVariantBuilder *inner_builder;
264 GVariantBuilder *invalidated_builder;
266 BT_DBG("BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED");
267 inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
269 g_variant_builder_add(inner_builder, "{sv}", "ReadValue", var);
271 invalidated_builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
273 parameters = g_variant_new("(a{sv}as)", inner_builder, invalidated_builder);
274 g_variant_builder_unref(invalidated_builder);
275 g_variant_builder_unref(inner_builder);
278 msg = g_dbus_message_new_signal(BT_HPS_OBJECT_PATH, BT_HPS_INTERFACE_NAME, PROPERTIES_CHANGED);
279 g_dbus_message_set_body(msg, parameters);
280 if (!g_dbus_connection_send_message(g_conn, msg, G_DBUS_SEND_MESSAGE_FLAGS_NONE, 0, NULL)) {
282 BT_ERR("D-Bus API failure: errCode[%x], \
284 error->code, error->message);
285 g_clear_error(&error);
287 return BLUETOOTH_ERROR_INTERNAL;
289 return BLUETOOTH_ERROR_NONE;
293 static void __bt_gatt_manager_method_call(GDBusConnection *connection,
295 const gchar *object_path,
296 const gchar *interface_name,
297 const gchar *method_name,
298 GVariant *parameters,
299 GDBusMethodInvocation *invocation,
306 if (g_strcmp0(method_name, "GetManagedObjects") == 0) {
307 BT_DBG("Getting values for service, chars and descriptors");
309 GVariantBuilder *builder;
310 GVariantBuilder *inner_builder1 = NULL;
311 GVariant *svc_char = NULL;
314 builder = g_variant_builder_new(
315 G_VARIANT_TYPE("a{oa{sa{sv}}}"));
317 /* Prepare inner builder for GattService1 interface */
319 len = g_slist_length(gatt_services);
321 for (i = 0; i <= len; i++) {
322 GVariantBuilder *svc_builder = NULL;
323 GVariantBuilder *inner_builder = NULL;
325 if (register_pending_cnt > 1)
326 l1 = g_slist_nth(gatt_services, len - register_pending_cnt);
328 l1 = g_slist_last(gatt_services);
330 register_pending_cnt--;
333 BT_ERR("gatt service list is NULL");
334 g_dbus_method_invocation_return_value(invocation, NULL);
338 struct gatt_service_info *serv_info = l1->data;
339 if (serv_info == NULL) {
340 BT_ERR("service info value is NULL");
341 g_dbus_method_invocation_return_value(invocation, NULL);
345 /* Prepare inner builder for GattService1 interface */
346 BT_DBG("Creating builder for service");
347 svc_builder = g_variant_builder_new(
348 G_VARIANT_TYPE("a{sa{sv}}"));
349 inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
351 g_variant_builder_add(inner_builder, "{sv}", "UUID",
352 g_variant_new_string(serv_info->service_uuid));
354 g_variant_builder_add(inner_builder, "{sv}", "Primary",
355 g_variant_new_boolean(serv_info->is_svc_primary));
358 inner_builder1 = g_variant_builder_new(G_VARIANT_TYPE("ao"));
359 BT_DBG("Adding Charatarisitcs list");
360 for (l4 = serv_info->char_data; l4 != NULL; l4 = l4->next) {
361 struct gatt_char_info *char_info = l4->data;
362 g_variant_builder_add(inner_builder1, "o",
363 char_info->char_path);
364 BT_DBG("%s", char_info->char_path);
367 svc_char = g_variant_new("ao", inner_builder1);
368 g_variant_builder_add(inner_builder, "{sv}", "Characteristics",
371 g_variant_builder_add(svc_builder, "{sa{sv}}",
375 g_variant_builder_add(builder, "{oa{sa{sv}}}",
376 serv_info->serv_path,
379 g_variant_builder_unref(inner_builder1);
381 /* Prepare inner builder for GattCharacteristic1 interface */
383 GSList *l2 = serv_info->char_data;
384 BT_DBG("Creating builder for characteristics \n");
387 BT_DBG("characteristic data is NULL");
389 for (l2 = serv_info->char_data; l2 != NULL; l2 = l2->next) {
391 GVariantBuilder *char_builder = NULL;
392 GVariantBuilder *inner_builder = NULL;
393 GVariantBuilder *builder1 = NULL;
394 GVariantBuilder *builder2 = NULL;
395 GVariantBuilder *builder3 = NULL;
396 GVariant *char_val = NULL;
397 GVariant *flags_val = NULL;
398 GVariant *char_desc = NULL;
399 char *unicast = NULL;
400 gboolean notify = FALSE;
403 char_builder = g_variant_builder_new(
406 inner_builder = g_variant_builder_new(
410 struct gatt_char_info *char_info = l2->data;
411 if (char_info == NULL) {
412 BT_ERR("char_info is NULL");
417 g_variant_builder_add(inner_builder, "{sv}", "UUID",
418 g_variant_new_string(char_info->char_uuid));
420 g_variant_builder_add(inner_builder, "{sv}", "Service",
421 g_variant_new("o", serv_info->serv_path));
423 builder1 = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
425 if (char_info->char_value != NULL) {
426 for (i = 0; i < char_info->value_length; i++) {
427 g_variant_builder_add(builder1, "y",
428 char_info->char_value[i]);
430 char_val = g_variant_new("ay", builder1);
431 g_variant_builder_add(inner_builder, "{sv}",
435 builder2 = g_variant_builder_new(G_VARIANT_TYPE("as"));
437 for (i = 0; i < char_info->flags_length; i++) {
438 g_variant_builder_add(builder2, "s",
439 char_info->char_flags[i]);
442 flags_val = g_variant_new("as", builder2);
443 g_variant_builder_add(inner_builder, "{sv}", "Flags",
447 g_variant_builder_add(inner_builder, "{sv}", "Notifying",
448 g_variant_new("b", notify));
451 unicast = g_strdup("00:00:00:00:00:00");
452 g_variant_builder_add(inner_builder, "{sv}", "Unicast",
453 g_variant_new("s", unicast));
456 builder3 = g_variant_builder_new(G_VARIANT_TYPE("ao"));
457 BT_DBG("Adding Descriptors list");
459 for (l4 = char_info->desc_data; l4 != NULL; l4 = l4->next) {
460 struct gatt_desc_info *desc_info = l4->data;
461 g_variant_builder_add(builder3, "o",
462 desc_info->desc_path);
463 BT_DBG("%s", desc_info->desc_path);
466 char_desc = g_variant_new("ao", builder3);
467 g_variant_builder_add(inner_builder, "{sv}", "Descriptors",
470 g_variant_builder_add(char_builder, "{sa{sv}}",
471 GATT_CHAR_INTERFACE , inner_builder);
472 g_variant_builder_add(builder, "{oa{sa{sv}}}",
473 char_info->char_path, char_builder);
475 /*Prepare inner builder for GattDescriptor1 interface*/
477 GSList *l3 = char_info->desc_data;
480 BT_DBG("descriptor data is NULL");
482 for (l3 = char_info->desc_data; l3 != NULL; l3 = l3->next) {
484 BT_DBG("Creating builder for descriptor \n");
486 GVariantBuilder *desc_builder = NULL;
487 GVariantBuilder *inner_builder = NULL;
488 GVariantBuilder *builder1 = NULL;
489 GVariantBuilder *builder2 = NULL;
490 GVariant *desc_val = NULL;
492 desc_builder = g_variant_builder_new(
495 inner_builder = g_variant_builder_new(
499 struct gatt_desc_info *desc_info = l3->data;
500 if (desc_info == NULL) {
501 BT_ERR("desc_info is NULL");
506 g_variant_builder_add(inner_builder,
508 g_variant_new_string(
509 desc_info->desc_uuid));
512 g_variant_builder_add(inner_builder, "{sv}",
515 char_info->char_path));
518 builder1 = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
520 if (desc_info->desc_value != NULL) {
521 for (i = 0; i < desc_info->value_length; i++) {
522 g_variant_builder_add(builder1, "y",
523 desc_info->desc_value[i]);
525 desc_val = g_variant_new("ay", builder1);
526 g_variant_builder_add(inner_builder, "{sv}",
531 builder2 = g_variant_builder_new(G_VARIANT_TYPE("as"));
533 for (i = 0; i < desc_info->flags_length; i++) {
534 g_variant_builder_add(builder2, "s",
535 desc_info->desc_flags[i]);
538 flags_val = g_variant_new("as", builder2);
539 g_variant_builder_add(inner_builder, "{sv}", "Flags",
542 g_variant_builder_add(desc_builder, "{sa{sv}}",
546 g_variant_builder_add(builder, "{oa{sa{sv}}}",
547 desc_info->desc_path,
550 /*unref descriptor builder pointers*/
551 g_variant_builder_unref(builder1);
552 g_variant_builder_unref(builder2);
553 g_variant_builder_unref(inner_builder);
554 g_variant_builder_unref(desc_builder);
559 /*unref char builder pointers*/
560 g_variant_builder_unref(builder1);
561 g_variant_builder_unref(builder2);
562 g_variant_builder_unref(builder3);
563 g_variant_builder_unref(inner_builder);
564 g_variant_builder_unref(char_builder);
567 /*unref service builder pointers*/
568 g_variant_builder_unref(inner_builder);
569 g_variant_builder_unref(svc_builder);
572 /* Return builder as method reply */
573 BT_DBG("Sending gatt service builder values to Bluez");
574 g_dbus_method_invocation_return_value(invocation,
581 static struct gatt_service_info *__bt_gatt_find_gatt_service_from_char(const char *char_path)
585 for (l1 = gatt_services; l1 != NULL; l1 = l1->next) {
586 struct gatt_service_info *serv_info = l1->data;
588 for (l2 = serv_info->char_data; l2 != NULL; l2 = l2->next) {
589 struct gatt_char_info *char_info = l2->data;
591 if (g_strcmp0(char_info->char_path, char_path)
596 BT_ERR("Gatt service not found");
600 static struct gatt_service_info *__bt_gatt_find_gatt_service_from_desc(const char *desc_path)
602 GSList *l1, *l2, *l3;
604 for (l1 = gatt_services; l1 != NULL; l1 = l1->next) {
605 struct gatt_service_info *serv_info = l1->data;
607 for (l2 = serv_info->char_data; l2 != NULL; l2 = l2->next) {
608 struct gatt_char_info *char_info = l2->data;
610 for (l3 = char_info->desc_data; l3 != NULL; l3 = l3->next) {
611 struct gatt_desc_info *desc_info = l3->data;
613 if (g_strcmp0(desc_info->desc_path, desc_path)
619 BT_ERR("Gatt service not found");
623 static void __bt_gatt_char_method_call(GDBusConnection *connection,
625 const gchar *object_path,
626 const gchar *interface_name,
627 const gchar *method_name,
628 GVariant *parameters,
629 GDBusMethodInvocation *invocation,
633 if (g_strcmp0(method_name, "ReadValue") == 0) {
637 bt_gatt_read_req_t read_req = {0, };
638 bt_user_info_t *user_info = NULL;
639 struct gatt_req_info *req_info = NULL;
640 struct gatt_service_info *svc_info = NULL;
644 g_variant_get(parameters, "(&syq)", &addr, &req_id, &offset);
646 BT_DBG("Application path = %s", object_path);
648 BT_DBG("Remote Device address number = %s", addr);
649 BT_DBG("Request id = %d, Offset = %d", req_id, offset);
651 BT_DBG("Sender = %s", sender);
653 read_req.att_handle = g_strdup(object_path);
654 read_req.address = g_strdup(addr);
655 read_req.req_id = req_id;
656 read_req.offset = offset;
657 svc_info = __bt_gatt_find_gatt_service_from_char(object_path);
658 if (svc_info != NULL) {
659 read_req.service_handle = g_strdup(svc_info->serv_path);
660 user_info = _bt_get_user_data(BT_COMMON);
661 #ifdef TIZEN_FEATURE_BT_HPS
662 GVariant *param = NULL;
665 /* Store requets information */
666 req_info = g_new0(struct gatt_req_info, 1);
667 req_info->attr_path = g_strdup(object_path);
668 req_info->svc_path = g_strdup(read_req.service_handle);
669 req_info->request_id = req_id;
670 req_info->offset = offset;
671 req_info->context = invocation;
672 gatt_requests = g_slist_append(gatt_requests, req_info);
674 if (user_info != NULL) {
676 BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED,
677 BLUETOOTH_ERROR_NONE, &read_req,
678 user_info->cb, user_info->user_data);
680 #ifdef TIZEN_FEATURE_BT_HPS
681 param = g_variant_new("(sssyq)",
683 read_req.service_handle,
687 __bt_send_event_to_hps(BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED, param);
691 if (read_req.att_handle)
692 g_free(read_req.att_handle);
693 if (read_req.address)
694 g_free(read_req.address);
695 if (read_req.service_handle)
696 g_free(read_req.service_handle);
698 } else if (g_strcmp0(method_name, "WriteValue") == 0) {
699 GVariant *var = NULL;
703 bt_gatt_value_change_t value_change = {0, };
704 bt_user_info_t *user_info = NULL;
706 struct gatt_service_info *svc_info = NULL;
707 struct gatt_req_info *req_info = NULL;
708 #ifdef TIZEN_FEATURE_BT_HPS
709 GVariant *param = NULL;
712 BT_DBG("WriteValue");
713 BT_DBG("Application path = %s", object_path);
714 BT_DBG("Sender = %s", sender);
716 g_variant_get(parameters, "(&syq@ay)", &addr, &req_id, &offset, &var);
718 value_change.att_handle = g_strdup(object_path);
719 value_change.address = g_strdup(addr);
720 svc_info = __bt_gatt_find_gatt_service_from_char(object_path);
721 if (svc_info == NULL) {
722 g_variant_unref(var);
723 g_dbus_method_invocation_return_value(invocation, NULL);
727 value_change.service_handle = g_strdup(svc_info->serv_path);
728 value_change.offset = offset;
729 value_change.req_id = req_id;
731 len = g_variant_get_size(var);
735 value_change.att_value = (guint8 *)malloc(len);
736 if (!value_change.att_value) {
737 BT_ERR("att_value is NULL");
738 g_variant_unref(var);
739 g_dbus_method_invocation_return_value(invocation, NULL);
743 data = (char *)g_variant_get_data(var);
744 memcpy(value_change.att_value, data, len);
747 value_change.val_len = len;
749 /* Store requets information */
750 req_info = g_new0(struct gatt_req_info, 1);
751 req_info->attr_path = g_strdup(object_path);
752 req_info->svc_path = g_strdup(value_change.service_handle);
753 req_info->request_id = req_id;
754 req_info->offset = offset;
755 req_info->context = invocation;
756 gatt_requests = g_slist_append(gatt_requests, req_info);
758 user_info = _bt_get_user_data(BT_COMMON);
759 if (user_info != NULL) {
761 BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED,
762 BLUETOOTH_ERROR_NONE, &value_change,
763 user_info->cb, user_info->user_data);
765 #ifdef TIZEN_FEATURE_BT_HPS
768 svc_path = g_strdup(svc_info->serv_path);
769 param = g_variant_new("(sssyq@ay)",
776 __bt_send_event_to_hps(BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED, param);
781 g_variant_unref(var);
783 } else if (g_strcmp0(method_name, "StartNotify") == 0) {
784 bt_user_info_t *user_info = NULL;
785 bt_gatt_char_notify_change_t notify_change = {0, };
786 BT_DBG("StartNotify");
787 user_info = _bt_get_user_data(BT_COMMON);
788 if (user_info != NULL) {
789 struct gatt_service_info *svc_info = NULL;
790 svc_info = __bt_gatt_find_gatt_service_from_char(object_path);
792 notify_change.service_handle = g_strdup(svc_info->serv_path);
793 notify_change.att_handle = g_strdup(object_path);
794 notify_change.att_notify = TRUE;
796 BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_STATE_CHANGED,
797 BLUETOOTH_ERROR_NONE, ¬ify_change,
798 user_info->cb, user_info->user_data);
801 } else if (g_strcmp0(method_name, "StopNotify") == 0) {
802 bt_user_info_t *user_info = NULL;
803 bt_gatt_char_notify_change_t notify_change = {0, };
804 BT_DBG("StopNotify");
805 user_info = _bt_get_user_data(BT_COMMON);
806 if (user_info != NULL) {
807 struct gatt_service_info *svc_info = NULL;
808 svc_info = __bt_gatt_find_gatt_service_from_char(object_path);
810 notify_change.service_handle = g_strdup(svc_info->serv_path);
811 notify_change.att_handle = g_strdup(object_path);
812 notify_change.att_notify = FALSE;
814 BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_STATE_CHANGED,
815 BLUETOOTH_ERROR_NONE, ¬ify_change,
816 user_info->cb, user_info->user_data);
819 } else if (g_strcmp0(method_name, "IndicateConfirm") == 0) {
821 bt_gatt_indicate_confirm_t confirm = {0, };
822 bt_user_info_t *user_info = NULL;
823 gboolean complete = 0;
824 struct gatt_service_info *svc_info = NULL;
826 BT_DBG("IndicateConfirm");
827 BT_DBG("Application path = %s", object_path);
828 BT_DBG("Sender = %s", sender);
830 g_variant_get(parameters, "(&sb)", &addr, &complete);
832 BT_DBG("Remote Device address number = %s", addr);
833 confirm.att_handle = g_strdup(object_path);
834 confirm.address = g_strdup(addr);
835 confirm.complete = complete;
837 svc_info = __bt_gatt_find_gatt_service_from_char(object_path);
838 if (svc_info != NULL) {
839 confirm.service_handle = g_strdup(svc_info->serv_path);
840 user_info = _bt_get_user_data(BT_COMMON);
842 if (user_info != NULL) {
844 BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_COMPLETED,
845 BLUETOOTH_ERROR_NONE, &confirm,
846 user_info->cb, user_info->user_data);
850 g_dbus_method_invocation_return_value(invocation, NULL);
853 static void __bt_gatt_desc_method_call(GDBusConnection *connection,
855 const gchar *object_path,
856 const gchar *interface_name,
857 const gchar *method_name,
858 GVariant *parameters,
859 GDBusMethodInvocation *invocation,
862 if (g_strcmp0(method_name, "ReadValue") == 0) {
866 bt_gatt_read_req_t read_req = {0, };
867 bt_user_info_t *user_info = NULL;
868 struct gatt_req_info *req_info = NULL;
869 struct gatt_service_info *svc_info = NULL;
872 g_variant_get(parameters, "(&syq)", &addr, &req_id, &offset);
874 BT_DBG("Application path = %s", object_path);
876 BT_DBG("Remote Device address number = %s", addr);
877 BT_DBG("Request id = %d, Offset = %d", req_id, offset);
879 BT_DBG("Sender = %s", sender);
881 read_req.att_handle = g_strdup(object_path);
882 read_req.address = g_strdup(addr);
883 read_req.req_id = req_id;
884 read_req.offset = offset;
885 svc_info = __bt_gatt_find_gatt_service_from_desc(object_path);
886 if (svc_info != NULL) {
887 read_req.service_handle = g_strdup(svc_info->serv_path);
888 user_info = _bt_get_user_data(BT_COMMON);
890 /* Store requets information */
891 req_info = g_new0(struct gatt_req_info, 1);
892 req_info->attr_path = g_strdup(object_path);
893 req_info->svc_path = g_strdup(read_req.service_handle);
894 req_info->request_id = req_id;
895 req_info->offset = offset;
896 req_info->context = invocation;
897 gatt_requests = g_slist_append(gatt_requests, req_info);
899 if (user_info != NULL) {
901 BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED,
902 BLUETOOTH_ERROR_NONE, &read_req,
903 user_info->cb, user_info->user_data);
907 if (read_req.att_handle)
908 g_free(read_req.att_handle);
909 if (read_req.address)
910 g_free(read_req.address);
911 if (read_req.service_handle)
912 g_free(read_req.service_handle);
915 } else if (g_strcmp0(method_name, "WriteValue") == 0) {
916 GVariant *var = NULL;
920 bt_gatt_value_change_t value_change = {0, };
921 bt_user_info_t *user_info = NULL;
923 struct gatt_service_info *svc_info = NULL;
924 struct gatt_req_info *req_info = NULL;
926 BT_DBG("WriteValue");
927 BT_DBG("Application path = %s", object_path);
928 BT_DBG("Sender = %s", sender);
930 g_variant_get(parameters, "(&syq@ay)", &addr, &req_id, &offset, &var);
932 value_change.att_handle = g_strdup(object_path);
933 value_change.address = g_strdup(addr);
934 svc_info = __bt_gatt_find_gatt_service_from_desc(object_path);
935 if (svc_info == NULL) {
936 g_variant_unref(var);
937 g_dbus_method_invocation_return_value(invocation, NULL);
941 value_change.service_handle = g_strdup(svc_info->serv_path);
942 value_change.offset = offset;
943 value_change.req_id = req_id;
945 len = g_variant_get_size(var);
949 value_change.att_value = (guint8 *)malloc(len);
950 if (!value_change.att_value) {
951 BT_ERR("att_value is NULL");
952 g_variant_unref(var);
953 g_dbus_method_invocation_return_value(invocation, NULL);
956 data = (char *)g_variant_get_data(var);
957 memcpy(value_change.att_value, data, len);
959 g_variant_unref(var);
961 value_change.val_len = len;
963 /* Store requets information */
964 req_info = g_new0(struct gatt_req_info, 1);
965 req_info->attr_path = g_strdup(object_path);
966 req_info->svc_path = g_strdup(value_change.service_handle);
967 req_info->request_id = req_id;
968 req_info->offset = offset;
969 req_info->context = invocation;
970 gatt_requests = g_slist_append(gatt_requests, req_info);
972 user_info = _bt_get_user_data(BT_COMMON);
973 if (user_info != NULL) {
975 BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED,
976 BLUETOOTH_ERROR_NONE, &value_change,
977 user_info->cb, user_info->user_data);
981 g_dbus_method_invocation_return_value(invocation, NULL);
984 gboolean __bt_gatt_emit_interface_removed(gchar *object_path, gchar *interface)
987 GError *error = NULL;
988 GVariantBuilder *array_builder;
990 array_builder = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
991 g_variant_builder_init(array_builder, G_VARIANT_TYPE("as"));
992 g_variant_builder_add(array_builder, "s", interface);
994 ret = g_dbus_connection_emit_signal(g_conn, NULL, "/",
995 "org.freedesktop.Dbus.Objectmanager",
997 g_variant_new("(oas)",
998 object_path, array_builder),
1002 if (error != NULL) {
1003 /* dbus gives error cause */
1004 BT_ERR("d-bus api failure: errcode[%x], message[%s]",
1005 error->code, error->message);
1006 g_clear_error(&error);
1009 g_variant_builder_unref(array_builder);
1014 static const GDBusInterfaceVTable desc_interface_vtable = {
1015 __bt_gatt_desc_method_call,
1020 static const GDBusInterfaceVTable char_interface_vtable = {
1021 __bt_gatt_char_method_call,
1026 static const GDBusInterfaceVTable serv_interface_vtable = {
1032 static const GDBusInterfaceVTable manager_interface_vtable = {
1033 __bt_gatt_manager_method_call,
1038 static GDBusNodeInfo *__bt_gatt_create_method_node_info(
1039 const gchar *introspection_data)
1042 GDBusNodeInfo *node_info = NULL;
1044 if (introspection_data == NULL)
1048 BT_DBG("Create new node info");
1049 node_info = g_dbus_node_info_new_for_xml(introspection_data, &err);
1052 BT_ERR("Unable to create node: %s", err->message);
1053 g_clear_error(&err);
1060 static struct gatt_service_info *__bt_gatt_find_gatt_service_info(
1061 const char *service_path)
1065 for (l = gatt_services; l != NULL; l = l->next) {
1066 struct gatt_service_info *info = l->data;
1068 if (g_strcmp0(info->serv_path, service_path) == 0)
1071 BT_ERR("Gatt service not found");
1075 static struct gatt_char_info *__bt_gatt_find_gatt_char_info(
1076 const char *service_path, const char *char_path)
1080 for (l1 = gatt_services; l1 != NULL; l1 = l1->next) {
1081 struct gatt_service_info *serv_info = l1->data;
1083 if (g_strcmp0(serv_info->serv_path, service_path) == 0) {
1085 for (l2 = serv_info->char_data; l2 != NULL; l2 = l2->next) {
1086 struct gatt_char_info *char_info = l2->data;
1088 if (g_strcmp0(char_info->char_path, char_path)
1092 BT_ERR("Gatt characteristic not found");
1096 BT_ERR("Gatt service not found");
1100 static struct gatt_desc_info *__bt_gatt_find_gatt_desc_info(
1101 const char *serv_path, const char *char_path,
1102 const char *desc_path)
1104 GSList *l1, *l2, *l3;
1106 for (l1 = gatt_services; l1 != NULL; l1 = l1->next) {
1107 struct gatt_service_info *serv_info = l1->data;
1109 if (g_strcmp0(serv_info->serv_path, serv_path) == 0) {
1110 for (l2 = serv_info->char_data; l2 != NULL; l2 = l2->next) {
1111 struct gatt_char_info *char_info = l2->data;
1113 if (g_strcmp0(char_info->char_path, char_path)
1115 for (l3 = char_info->desc_data; l3 != NULL; l3 = l3->next) {
1116 struct gatt_desc_info *desc_info = l3->data;
1117 if (g_strcmp0(desc_info->desc_path,
1126 BT_ERR("Gatt descriptor not found");
1130 static struct gatt_req_info *__bt_gatt_find_request_info(guint request_id)
1134 for (l = gatt_requests; l != NULL; l = l->next) {
1135 struct gatt_req_info *req_info = l->data;
1137 if (req_info && req_info->request_id == request_id)
1140 BT_ERR("Gatt Request not found");
1144 static GDBusProxy *__bt_gatt_gdbus_init_manager_proxy(const gchar *service,
1145 const gchar *path, const gchar *interface)
1151 g_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM,
1156 BT_ERR("Unable to connect to gdbus: %s", err->message);
1157 g_clear_error(&err);
1162 proxy = g_dbus_proxy_new_sync(g_conn,
1163 G_DBUS_PROXY_FLAGS_NONE, NULL,
1165 interface, NULL, &err);
1169 BT_ERR("Unable to create proxy: %s", err->message);
1170 g_clear_error(&err);
1174 manager_gproxy = proxy;
1179 static GDBusProxy *__bt_gatt_gdbus_get_manager_proxy(const gchar *service,
1180 const gchar *path, const gchar *interface)
1182 return (manager_gproxy) ? manager_gproxy :
1183 __bt_gatt_gdbus_init_manager_proxy(service,
1187 int bluetooth_gatt_convert_prop2string(
1188 bt_gatt_characteristic_property_t properties,
1189 char *char_properties[])
1193 if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_BROADCAST) {
1194 char_properties[flag_count] = g_strdup("broadcast");
1197 if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ) {
1198 char_properties[flag_count] = g_strdup("read");
1201 if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE_NO_RESPONSE) {
1202 char_properties[flag_count] = g_strdup("write-without-response");
1205 if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE) {
1206 char_properties[flag_count] = g_strdup("write");
1209 if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_NOTIFY) {
1210 char_properties[flag_count] = g_strdup("notify");
1213 if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_INDICATE) {
1214 char_properties[flag_count] = g_strdup("indicate");
1217 if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_SIGNED_WRITE) {
1218 char_properties[flag_count] = g_strdup("authenticated-signed-writes");
1221 if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_RELIABLE_WRITE) {
1222 char_properties[flag_count] = g_strdup("reliable-write");
1225 if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITABLE_AUXILIARIES) {
1226 char_properties[flag_count] = g_strdup("writable-auxiliaries");
1229 if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_READ) {
1230 char_properties[flag_count] = g_strdup("encrypt-read");
1233 if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_WRITE) {
1234 char_properties[flag_count] = g_strdup("encrypt-write");
1237 if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_READ) {
1238 char_properties[flag_count] = g_strdup("encrypt-authenticated-read");
1241 if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_WRITE) {
1242 char_properties[flag_count] = g_strdup("encrypt-authenticated-write");
1246 if (flag_count == 0) {
1247 char_properties[flag_count] = g_strdup("read");
1254 int bluetooth_gatt_convert_perm2string(
1255 bt_gatt_permission_t properties,
1256 char *char_properties[])
1260 if (properties & BLUETOOTH_GATT_PERMISSION_READ) {
1261 char_properties[flag_count] = g_strdup("read");
1264 if (properties & BLUETOOTH_GATT_PERMISSION_WRITE) {
1265 char_properties[flag_count] = g_strdup("write");
1268 if (properties & BLUETOOTH_GATT_PERMISSION_ENCRYPT_READ) {
1269 char_properties[flag_count] = g_strdup("encrypt-read");
1272 if (properties & BLUETOOTH_GATT_PERMISSION_ENCRYPT_WRITE) {
1273 char_properties[flag_count] = g_strdup("encrypt-write");
1276 if (properties & BLUETOOTH_GATT_PERMISSION_ENCRYPT_AUTHENTICATED_READ) {
1277 char_properties[flag_count] = g_strdup("encrypt-authenticated-read");
1280 if (properties & BLUETOOTH_GATT_PERMISSION_ENCRYPT_AUTHENTICATED_WRITE) {
1281 char_properties[flag_count] = g_strdup("encrypt-authenticated-write");
1285 if (flag_count == 0) {
1286 char_properties[flag_count] = g_strdup("read");
1293 static void __bt_gatt_set_service_state(const char *service_path,
1296 struct gatt_service_info *svc_info = NULL;
1297 svc_info = __bt_gatt_find_gatt_service_info(service_path);
1299 if (svc_info != NULL) {
1300 BT_DBG("Updating the gatt service register state %d", state);
1301 svc_info->is_svc_registered = state;
1305 BT_DBG("gatt service not found");
1308 static gboolean __bt_gatt_get_service_state(const char *service_path)
1310 struct gatt_service_info *svc_info = NULL;
1312 svc_info = __bt_gatt_find_gatt_service_info(service_path);
1314 if (svc_info != NULL) {
1315 BT_DBG("Return the state of the gatt service %d",
1316 svc_info->is_svc_registered);
1317 return svc_info->is_svc_registered;
1320 BT_DBG("gatt service info is NULL");
1324 void get_service_cb(GObject *object, GAsyncResult *res, gpointer user_data)
1326 GError *error = NULL;
1328 GVariantIter *iter = NULL;
1329 const gchar *key = NULL;
1330 GVariant *value = NULL;
1331 const gchar *service = NULL;
1332 const gchar *characteristic = NULL;
1333 const gchar *descriptor = NULL;
1337 result = g_dbus_proxy_call_finish(manager_gproxy, res, &error);
1339 if (result == NULL) {
1340 /* dBUS-RPC is failed */
1341 BT_ERR("Dbus-RPC is failed\n");
1343 if (error != NULL) {
1344 /* dBUS gives error cause */
1345 BT_ERR("D-Bus API failure: errCode[%x], message[%s]\n",
1346 error->code, error->message);
1347 g_clear_error(&error);
1350 char *char_cmp = NULL;
1351 g_variant_get(result, "(a{sv})", &iter);
1352 char_cmp = g_strdup_printf("Characteristic%d", n_char);
1354 while (g_variant_iter_loop(iter, "{sv}", &key, &value)) {
1355 if (g_strcmp0(key, "Service") == 0) {
1356 service = g_variant_get_string(value, NULL);
1357 BT_DBG("Service %s", service);
1358 } else if (g_strcmp0(key, char_cmp) == 0) {
1359 characteristic = g_variant_get_string(value, NULL);
1361 char_cmp = g_strdup_printf("Characteristic%d", ++n_char);
1362 BT_DBG("%s", characteristic);
1363 } else if (g_strcmp0(key, "Descriptor") == 0) {
1364 descriptor = g_variant_get_string(value, NULL);
1365 BT_DBG("Descriptor %s", descriptor);
1368 g_variant_iter_free(iter);
1370 /* TODO: Store the service informationa and
1371 * Send respponse to CAPI layer. */
1373 g_variant_unref(result);
1378 void register_application_cb(GObject *object, GAsyncResult *res, gpointer user_data)
1380 BT_INFO("RegisterApplication is completed");
1382 GError *error = NULL;
1385 register_pending_cnt = 0;
1387 result = g_dbus_proxy_call_finish(manager_gproxy, res, &error);
1389 if (result == NULL) {
1390 /* dBUS-RPC is failed */
1391 BT_ERR("Dbus-RPC is failed\n");
1393 if (error != NULL) {
1394 /* dBUS gives error cause */
1395 BT_ERR("D-Bus API failure: errCode[%x], message[%s]\n",
1396 error->code, error->message);
1397 g_clear_error(&error);
1400 g_variant_unref(result);
1404 void unregister_application_cb(GObject *object, GAsyncResult *res,
1407 BT_INFO("UnregisterApplication is completed");
1409 GError *error = NULL;
1412 result = g_dbus_proxy_call_finish(manager_gproxy, res, &error);
1414 if (result == NULL) {
1415 /* dBUS-RPC is failed */
1416 BT_ERR("Dbus-RPC is failed\n");
1418 if (error != NULL) {
1419 /* dBUS gives error cause */
1420 BT_ERR("D-Bus API failure: errCode[%x], message[%s]\n",
1421 error->code, error->message);
1422 g_clear_error(&error);
1425 g_variant_unref(result);
1429 static int __bt_gatt_unregister_service(const char *service_path)
1431 if (!__bt_gatt_get_service_state(service_path)) {
1432 BT_DBG("service not registered \n");
1433 return BLUETOOTH_ERROR_NOT_FOUND;
1436 return BLUETOOTH_ERROR_NONE;
1439 BT_EXPORT_API int bluetooth_gatt_unregister_application(void)
1441 GDBusProxy *proxy = NULL;
1443 if (is_server_started) {
1444 proxy = __bt_gatt_gdbus_get_manager_proxy("org.bluez",
1445 "/org/bluez/hci0", GATT_MNGR_INTERFACE);
1447 if (proxy == NULL || app_path == NULL)
1448 return BLUETOOTH_ERROR_INTERNAL;
1450 BT_INFO("UnregisterApplication");
1452 /* Async Call to Unregister Service */
1453 g_dbus_proxy_call(proxy,
1454 "UnregisterApplication",
1455 g_variant_new("(o)",
1457 G_DBUS_CALL_FLAGS_NONE, -1,
1459 (GAsyncReadyCallback) unregister_application_cb,
1462 is_server_started = false;
1463 return BLUETOOTH_ERROR_NONE;
1466 BT_INFO("GATT server not started");
1467 return BLUETOOTH_ERROR_NONE;
1470 static GDBusConnection *__bt_gatt_get_gdbus_connection(void)
1472 GDBusConnection *local_system_gconn = NULL;
1476 if (g_conn == NULL) {
1477 address = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
1478 if (address == NULL) {
1480 BT_ERR("Failed to get bus address: %s", err->message);
1481 g_clear_error(&err);
1486 g_conn = g_dbus_connection_new_for_address_sync(address,
1487 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
1488 G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
1489 NULL, /* GDBusAuthObserver */
1494 BT_ERR("Unable to connect to dbus: %s", err->message);
1495 g_clear_error(&err);
1499 } else if (g_dbus_connection_is_closed(g_conn)) {
1500 address = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
1501 if (address == NULL) {
1503 BT_ERR("Failed to get bus address: %s", err->message);
1504 g_clear_error(&err);
1509 local_system_gconn = g_dbus_connection_new_for_address_sync(address,
1510 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
1511 G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
1512 NULL, /* GDBusAuthObserver */
1516 if (!local_system_gconn) {
1517 BT_ERR("Unable to connect to dbus: %s", err->message);
1518 g_clear_error(&err);
1521 g_conn = local_system_gconn;
1527 BT_EXPORT_API int bluetooth_gatt_init(void)
1529 GDBusConnection *conn;
1530 GError *error = NULL;
1531 GDBusNodeInfo *node_info = NULL;
1533 if (app_path != NULL) {
1534 BT_ERR("app path already exists! initialized");
1535 return BLUETOOTH_ERROR_ALREADY_INITIALIZED;
1538 if (owner_id == 0) {
1539 owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
1540 BT_GATT_SERVICE_NAME,
1541 G_BUS_NAME_OWNER_FLAGS_NONE,
1542 NULL, NULL, NULL, NULL, NULL);
1545 BT_DBG("owner_id is [%d]", owner_id);
1546 app_path = g_strdup_printf("/com/%d", getpid());
1550 conn = __bt_gatt_get_gdbus_connection();
1552 BT_ERR("Unable to get connection");
1556 /* Register ObjectManager interface */
1557 node_info = __bt_gatt_create_method_node_info(
1558 manager_introspection_xml);
1560 if (node_info == NULL) {
1561 BT_ERR("failed to get node info");
1565 if (manager_id == 0) {
1566 BT_INFO("manager_id does not exists");
1568 manager_id = g_dbus_connection_register_object(g_conn, app_path,
1569 node_info->interfaces[0],
1570 &manager_interface_vtable,
1571 NULL, NULL, &error);
1574 if (manager_id == 0) {
1575 BT_ERR("failed to register: %s", error->message);
1576 g_error_free(error);
1580 return BLUETOOTH_ERROR_NONE;
1584 g_bus_unown_name(owner_id);
1591 __bt_gatt_close_gdbus_connection();
1593 return BLUETOOTH_ERROR_INTERNAL;
1596 BT_EXPORT_API int bluetooth_gatt_deinit()
1598 int ret = BLUETOOTH_ERROR_NONE;
1599 /* Unown gdbus bus */
1601 /* remove/unregister all services */
1602 BT_DBG("removing all registered gatt service\n");
1603 bluetooth_gatt_delete_services();
1605 /* unregister the exported interface for object manager */
1606 g_dbus_connection_unregister_object(g_conn,
1611 ret = bluetooth_gatt_unregister_application();
1612 if (ret != BLUETOOTH_ERROR_NONE)
1613 BT_ERR("Fail to unregister application\n");
1615 g_bus_unown_name(owner_id);
1621 BT_DBG("Gatt service deinitialized \n");
1623 g_slist_free(gatt_services);
1624 gatt_services = NULL;
1626 g_object_unref(manager_gproxy);
1627 manager_gproxy = NULL;
1629 __bt_gatt_close_gdbus_connection();
1634 __bt_gatt_close_gdbus_connection();
1636 return BLUETOOTH_ERROR_NOT_FOUND;
1639 BT_EXPORT_API int bluetooth_gatt_add_service(const char *svc_uuid,
1642 GError *error = NULL;
1644 GDBusNodeInfo *node_info;
1646 GVariantBuilder *builder = NULL;
1647 GVariantBuilder *builder1 = NULL;
1648 GVariantBuilder *inner_builder = NULL;
1649 gboolean svc_primary = TRUE;
1650 struct gatt_service_info *serv_info = NULL;
1652 node_info = __bt_gatt_create_method_node_info(
1653 service_introspection_xml);
1655 if (node_info == NULL)
1656 return BLUETOOTH_ERROR_INTERNAL;
1658 path = g_strdup_printf("%s"GATT_SERV_OBJECT_PATH"%d", app_path, serv_id++);
1659 BT_DBG("gatt service path is [%s]", path);
1661 object_id = g_dbus_connection_register_object(g_conn, path,
1662 node_info->interfaces[0],
1663 &serv_interface_vtable,
1664 NULL, NULL, &error);
1666 if (object_id == 0) {
1667 BT_ERR("failed to register: %s", error->message);
1668 g_error_free(error);
1671 return BLUETOOTH_ERROR_INTERNAL;
1674 /* Add object_id/gatt service information; it's required at the time of
1675 * service unregister and Getmanagedobjects
1677 serv_info = g_new0(struct gatt_service_info, 1);
1679 serv_info->serv_path = g_strdup(path);
1680 serv_info->serv_id = object_id;
1681 serv_info->service_uuid = g_strdup(svc_uuid);
1682 serv_info->is_svc_registered = FALSE;
1683 serv_info->is_svc_primary = svc_primary;
1685 gatt_services = g_slist_append(gatt_services, serv_info);
1687 /* emit interfacesadded signal here for service path */
1688 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
1689 inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1691 g_variant_builder_add(inner_builder, "{sv}",
1692 "UUID", g_variant_new_string(svc_uuid));
1694 g_variant_builder_add(inner_builder, "{sv}",
1695 "Primary", g_variant_new_boolean(svc_primary));
1697 builder1 = g_variant_builder_new(G_VARIANT_TYPE("ao"));
1699 g_variant_builder_add(inner_builder, "{sv}", "Characteristics",
1700 g_variant_new("ao", builder1));
1702 g_variant_builder_add(builder, "{sa{sv}}",
1703 GATT_SERV_INTERFACE, inner_builder);
1705 g_dbus_connection_emit_signal(g_conn, NULL, "/",
1706 "org.freedesktop.Dbus.ObjectManager",
1708 g_variant_new("(oa{sa{sv}})",
1714 *svc_path = g_strdup(path);
1717 g_variant_builder_unref(inner_builder);
1718 g_variant_builder_unref(builder);
1719 g_variant_builder_unref(builder1);
1721 return BLUETOOTH_ERROR_NONE;
1724 BT_EXPORT_API int bluetooth_gatt_add_new_characteristic(
1725 const char *svc_path, const char *char_uuid,
1726 bt_gatt_permission_t permissions,
1727 bt_gatt_characteristic_property_t properties,
1731 GError *error = NULL;
1733 GDBusNodeInfo *node_info;
1735 GVariantBuilder *builder = NULL;
1736 GVariantBuilder *inner_builder = NULL;
1737 struct gatt_service_info *serv_info = NULL;
1738 struct gatt_char_info *char_info = NULL;
1739 GVariantBuilder *builder2 = NULL;
1740 GVariantBuilder *builder3 = NULL;
1741 GVariant *flags_val = NULL;
1743 char *char_flags[NUMBER_OF_FLAGS];
1748 new_service = FALSE;
1751 BT_DBG("gatt svc_path path is [%s]", svc_path);
1752 serv_info = __bt_gatt_find_gatt_service_info(svc_path);
1753 if (serv_info == NULL)
1754 return BLUETOOTH_ERROR_INVALID_PARAM;
1756 node_info = __bt_gatt_create_method_node_info(
1757 characteristics_introspection_xml);
1759 if (node_info == NULL)
1760 return BLUETOOTH_ERROR_INTERNAL;
1762 path = g_strdup_printf("%s/characteristic%d", svc_path, char_id++);
1763 BT_DBG("gatt characteristic path is [%s]", path);
1765 object_id = g_dbus_connection_register_object(g_conn, path,
1766 node_info->interfaces[0],
1767 &char_interface_vtable,
1768 NULL, NULL, &error);
1770 if (object_id == 0) {
1771 BT_ERR("failed to register: %s", error->message);
1772 g_error_free(error);
1775 return BLUETOOTH_ERROR_INTERNAL;
1778 if (permissions & BLUETOOTH_GATT_PERMISSION_ENCRYPT_READ)
1779 properties |= BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_READ;
1780 if (permissions & BLUETOOTH_GATT_PERMISSION_ENCRYPT_AUTHENTICATED_READ)
1781 properties |= BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_READ;
1782 if (permissions & BLUETOOTH_GATT_PERMISSION_ENCRYPT_WRITE)
1783 properties |= BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_WRITE;
1784 if (permissions & BLUETOOTH_GATT_PERMISSION_ENCRYPT_AUTHENTICATED_WRITE)
1785 properties |= BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_WRITE;
1787 flag_count = bluetooth_gatt_convert_prop2string(properties, char_flags);
1789 char_info = g_new0(struct gatt_char_info, 1);
1791 char_info->char_path = g_strdup(path);
1792 char_info->char_id = object_id;
1793 char_info->char_uuid = g_strdup(char_uuid);
1795 for (i = 0; i < flag_count; i++)
1796 char_info->char_flags[i] = char_flags[i];
1799 char_info->flags_length = flag_count;
1801 serv_info->char_data = g_slist_append(serv_info->char_data, char_info);
1803 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
1804 inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1806 g_variant_builder_add(inner_builder, "{sv}", "UUID",
1807 g_variant_new("s", char_uuid));
1808 g_variant_builder_add(inner_builder, "{sv}", "Service",
1809 g_variant_new("o", svc_path));
1811 builder2 = g_variant_builder_new(G_VARIANT_TYPE("as"));
1813 for (i = 0; i < flag_count; i++)
1814 g_variant_builder_add(builder2, "s", char_flags[i]);
1816 flags_val = g_variant_new("as", builder2);
1817 g_variant_builder_add(inner_builder, "{sv}", "Flags",
1820 builder3 = g_variant_builder_new(G_VARIANT_TYPE("ao"));
1822 g_variant_builder_add(inner_builder, "{sv}", "Descriptors",
1823 g_variant_new("ao", builder3));
1825 g_variant_builder_add(builder, "{sa{sv}}",
1826 GATT_CHAR_INTERFACE,
1829 g_dbus_connection_emit_signal(g_conn, NULL, "/",
1830 "org.freedesktop.Dbus.ObjectManager",
1832 g_variant_new("(oa{sa{sv}})",
1836 *char_path = g_strdup(path);
1842 g_variant_builder_unref(inner_builder);
1843 g_variant_builder_unref(builder);
1844 g_variant_builder_unref(builder2);
1845 g_variant_builder_unref(builder3);
1847 return BLUETOOTH_ERROR_NONE;
1850 BT_EXPORT_API int bluetooth_gatt_set_characteristic_value(
1851 const char *characteristic, const char *char_value,
1854 gchar **line_argv = NULL;
1855 char *serv_path = NULL;
1856 struct gatt_char_info *char_info = NULL;
1857 GVariantBuilder *builder1 = NULL;
1858 GVariantBuilder *builder = NULL;
1859 GVariantBuilder *inner_builder = NULL;
1860 GVariant *char_val = NULL;
1861 GError *error = NULL;
1863 int res = BLUETOOTH_ERROR_NONE;
1865 line_argv = g_strsplit_set(characteristic, "/", 0);
1866 serv_path = g_strdup_printf("/%s/%s/%s", line_argv[1], line_argv[2], line_argv[3]);
1868 char_info = __bt_gatt_find_gatt_char_info(serv_path, characteristic);
1870 if (char_info == NULL) {
1871 /* Fix : RESOURCE_LEAK */
1872 res = BLUETOOTH_ERROR_INVALID_PARAM;
1876 char_info->value_length = value_length;
1878 char_info->char_value = (char *)malloc(value_length);
1879 /* Fix : NULL_RETURNS */
1880 if (char_info->char_value == NULL) {
1881 res = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1885 for (i = 0; i < value_length; i++)
1886 char_info->char_value[i] = char_value[i];
1888 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
1889 inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1891 builder1 = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
1893 for (i = 0; i < value_length; i++)
1894 g_variant_builder_add(builder1, "y", char_value[i]);
1896 char_val = g_variant_new("ay", builder1);
1897 g_variant_builder_add(inner_builder, "{sv}", "Value", char_val);
1899 g_variant_builder_add(builder, "{sa{sv}}",
1900 GATT_CHAR_INTERFACE,
1903 g_dbus_connection_emit_signal(g_conn, NULL, "/",
1904 "org.freedesktop.Dbus.ObjectManager",
1906 g_variant_new("(oa{sa{sv}})",
1907 char_info->char_path, builder),
1910 g_variant_builder_unref(inner_builder);
1911 g_variant_builder_unref(builder);
1912 g_variant_builder_unref(builder1);
1914 g_strfreev(line_argv);
1920 BT_EXPORT_API int bluetooth_gatt_add_descriptor(
1921 const char *char_path, const char *desc_uuid,
1922 bt_gatt_permission_t permissions,
1925 static int desc_id = 1;
1926 GError *error = NULL;
1928 GDBusNodeInfo *node_info;
1930 GVariantBuilder *builder = NULL;
1931 GVariantBuilder *inner_builder = NULL;
1932 struct gatt_char_info *char_info = NULL;
1933 struct gatt_desc_info *desc_info = NULL;
1934 gchar **line_argv = NULL;
1936 GVariantBuilder *builder2 = NULL;
1937 GVariant *flags_val = NULL;
1939 char *desc_flags[NUMBER_OF_FLAGS];
1947 line_argv = g_strsplit_set(char_path, "/", 0);
1948 serv_path = g_strdup_printf("/%s/%s/%s", line_argv[1], line_argv[2], line_argv[3]);
1950 char_info = __bt_gatt_find_gatt_char_info(serv_path, char_path);
1951 if (char_info == NULL) {
1952 g_strfreev(line_argv);
1954 return BLUETOOTH_ERROR_INVALID_PARAM;
1957 node_info = __bt_gatt_create_method_node_info(
1958 descriptor_introspection_xml);
1960 if (node_info == NULL) {
1961 g_strfreev(line_argv);
1963 return BLUETOOTH_ERROR_INTERNAL;
1966 path = g_strdup_printf("%s/descriptor%d", char_path, desc_id++);
1967 BT_DBG("gatt descriptor path is [%s]", path);
1969 object_id = g_dbus_connection_register_object(g_conn, path,
1970 node_info->interfaces[0],
1971 &desc_interface_vtable,
1972 NULL, NULL, &error);
1974 if (object_id == 0) {
1975 BT_ERR("failed to register: %s", error->message);
1976 g_error_free(error);
1978 g_strfreev(line_argv);
1981 return BLUETOOTH_ERROR_INTERNAL;
1984 flag_count = bluetooth_gatt_convert_perm2string(permissions, desc_flags);
1986 desc_info = g_new0(struct gatt_desc_info, 1);
1988 desc_info->desc_path = g_strdup(path);
1989 desc_info->desc_id = object_id;
1990 desc_info->desc_uuid = g_strdup(desc_uuid);
1992 for (i = 0; i < flag_count; i++)
1993 desc_info->desc_flags[i] = desc_flags[i];
1995 desc_info->flags_length = flag_count;
1997 char_info->desc_data = g_slist_append(char_info->desc_data, desc_info);
1999 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
2000 inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
2002 g_variant_builder_add(inner_builder, "{sv}", "UUID",
2003 g_variant_new("s", desc_uuid));
2004 g_variant_builder_add(inner_builder, "{sv}", "Characteristic",
2005 g_variant_new("o", char_path));
2007 builder2 = g_variant_builder_new(G_VARIANT_TYPE("as"));
2009 for (i = 0; i < flag_count; i++)
2010 g_variant_builder_add(builder2, "s", desc_flags[i]);
2012 flags_val = g_variant_new("as", builder2);
2013 g_variant_builder_add(inner_builder, "{sv}", "Flags",
2016 g_variant_builder_add(builder, "{sa{sv}}",
2017 GATT_DESC_INTERFACE,
2020 g_dbus_connection_emit_signal(g_conn, NULL, "/",
2021 "org.freedesktop.Dbus.ObjectManager",
2023 g_variant_new("(oa{sa{sv}})",
2027 *desc_path = g_strdup(path);
2031 g_strfreev(line_argv);
2032 g_variant_builder_unref(inner_builder);
2033 g_variant_builder_unref(builder);
2035 return BLUETOOTH_ERROR_NONE;
2038 BT_EXPORT_API int bluetooth_gatt_set_descriptor_value(
2039 const char *desc_path, const char *desc_value,
2042 GError *error = NULL;
2043 GVariantBuilder *builder = NULL;
2044 GVariantBuilder *inner_builder = NULL;
2045 GVariantBuilder *builder1 = NULL;
2046 struct gatt_desc_info *desc_info = NULL;
2047 gchar **line_argv = NULL;
2049 GVariant *desc_val = NULL;
2050 char *serv_path = NULL;
2053 line_argv = g_strsplit_set(desc_path, "/", 0);
2054 serv_path = g_strdup_printf("/%s/%s/%s", line_argv[1], line_argv[2], line_argv[3]);
2055 char_path = g_strdup_printf("%s/%s", serv_path, line_argv[4]);
2057 desc_info = __bt_gatt_find_gatt_desc_info(serv_path, char_path, desc_path);
2059 /* Free the allocated memory */
2060 g_strfreev(line_argv);
2064 /* Fix : NULL_RETURNS */
2065 retv_if(desc_info == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
2067 desc_info->desc_value = (char *)malloc(value_length);
2069 /* Fix : NULL_RETURNS */
2070 retv_if(desc_info->desc_value == NULL, BLUETOOTH_ERROR_MEMORY_ALLOCATION);
2072 for (i = 0; i < value_length; i++)
2073 desc_info->desc_value[i] = desc_value[i];
2075 desc_info->value_length = value_length;
2077 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
2078 inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
2080 builder1 = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
2082 for (i = 0; i < value_length; i++)
2083 g_variant_builder_add(builder1, "y", desc_value[i]);
2085 desc_val = g_variant_new("ay", builder1);
2086 g_variant_builder_add(inner_builder, "{sv}", "Value", desc_val);
2088 g_variant_builder_add(builder, "{sa{sv}}",
2089 GATT_DESC_INTERFACE,
2092 g_dbus_connection_emit_signal(g_conn, NULL, "/",
2093 "org.freedesktop.Dbus.ObjectManager",
2095 g_variant_new("(oa{sa{sv}})",
2096 desc_info->desc_path, builder),
2099 g_variant_builder_unref(inner_builder);
2100 g_variant_builder_unref(builder);
2101 g_variant_builder_unref(builder1);
2103 return BLUETOOTH_ERROR_NONE;
2106 int bluetooth_gatt_get_service(const char *svc_uuid)
2108 GDBusProxy *proxy = NULL;
2111 proxy = __bt_gatt_gdbus_get_manager_proxy("org.bluez",
2112 "/org/bluez/hci0", GATT_MNGR_INTERFACE);
2114 return BLUETOOTH_ERROR_INTERNAL;
2116 uuid = g_strdup(svc_uuid);
2118 g_dbus_proxy_call(proxy,
2120 g_variant_new("(s)",
2122 G_DBUS_CALL_FLAGS_NONE, -1,
2124 (GAsyncReadyCallback) get_service_cb,
2129 return BLUETOOTH_ERROR_NONE;
2132 BT_EXPORT_API int bluetooth_gatt_register_service(
2133 const char *svc_path)
2135 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_GATT_REGISTER_SERVICE)
2136 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
2137 BT_ERR("Don't have aprivilege to use this API");
2138 return BLUETOOTH_ERROR_PERMISSION_DEINED;
2141 register_pending_cnt++;
2143 if (__bt_gatt_get_service_state(svc_path)) {
2144 BT_DBG("service already registered \n");
2145 return BLUETOOTH_ERROR_NONE;
2148 __bt_gatt_set_service_state(svc_path, TRUE);
2150 return BLUETOOTH_ERROR_NONE;
2153 BT_EXPORT_API int bluetooth_gatt_register_application(void)
2155 GDBusProxy *proxy = NULL;
2157 if (!is_server_started) {
2159 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_GATT_REGISTER_APPLICATION)
2160 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
2161 BT_ERR("Don't have aprivilege to use this API");
2162 return BLUETOOTH_ERROR_PERMISSION_DEINED;
2165 proxy = __bt_gatt_gdbus_get_manager_proxy("org.bluez",
2166 "/org/bluez/hci0", GATT_MNGR_INTERFACE);
2167 if (proxy == NULL || app_path == NULL)
2168 return BLUETOOTH_ERROR_INTERNAL;
2170 BT_INFO("RegisterApplication");
2172 g_dbus_proxy_call(proxy,
2173 "RegisterApplication",
2174 g_variant_new("(oa{sv})",
2176 G_DBUS_CALL_FLAGS_NONE, -1,
2178 (GAsyncReadyCallback) register_application_cb,
2181 is_server_started = true;
2183 return BLUETOOTH_ERROR_NONE;
2186 BT_INFO("Already RegisterApplication");
2188 return BLUETOOTH_ERROR_NONE;
2191 BT_EXPORT_API int bluetooth_gatt_delete_services(void)
2194 int error = BLUETOOTH_ERROR_NONE;
2198 for (l = gatt_services; l != NULL; l = l->next) {
2199 struct gatt_service_info *info = l->data;
2200 BT_DBG("svc_path is %s", info->serv_path);
2201 if (bluetooth_gatt_unregister_service(info->serv_path)
2202 != BLUETOOTH_ERROR_NONE) {
2203 error = BLUETOOTH_ERROR_INTERNAL;
2204 BT_DBG(" Error in removing service %s \n",
2208 BT_DBG(" All services removed successfully.\n ");
2210 BT_DBG(" There are no registered services.\n ");
2213 g_slist_free(gatt_services);
2214 gatt_services = NULL;
2217 if (error != BLUETOOTH_ERROR_NONE)
2220 return BLUETOOTH_ERROR_NONE;
2223 BT_EXPORT_API int bluetooth_gatt_update_characteristic(
2224 const char *char_path, const char* char_value,
2227 GVariantBuilder *outer_builder;
2228 GVariantBuilder *inner_builder;
2229 GVariantBuilder *invalidated_builder;
2230 GVariant *update_value = NULL;
2231 GError *error = NULL;
2232 gboolean ret = FALSE;
2233 int err = BLUETOOTH_ERROR_NONE;
2235 gchar **line_argv = NULL;
2236 gchar *serv_path = NULL;
2238 line_argv = g_strsplit_set(char_path, "/", 0);
2239 serv_path = g_strdup_printf("/%s/%s/%s", line_argv[1], line_argv[2], line_argv[3]);
2241 if (!__bt_gatt_get_service_state(serv_path)) {
2242 BT_DBG("service not registered for this characteristic \n");
2244 g_strfreev(line_argv);
2245 return BLUETOOTH_ERROR_INTERNAL;
2248 outer_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
2249 invalidated_builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
2251 inner_builder = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
2252 for (i = 0; i < value_length; i++)
2253 g_variant_builder_add(inner_builder, "y", char_value[i]);
2255 update_value = g_variant_new("ay", inner_builder);
2257 outer_builder = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
2258 g_variant_builder_add(outer_builder, "{sv}", "Value",
2261 BT_DBG("Updating characteristic value \n");
2262 ret = g_dbus_connection_emit_signal(g_conn, NULL,
2264 "org.freedesktop.DBus.Properties",
2265 "PropertiesChanged",
2266 g_variant_new("(sa{sv}as)",
2267 "org.bluez.GattCharacteristic1",
2268 outer_builder, invalidated_builder),
2272 if (error != NULL) {
2273 BT_ERR("D-Bus API failure: errCode[%x], \
2275 error->code, error->message);
2276 g_clear_error(&error);
2278 err = BLUETOOTH_ERROR_INTERNAL;
2280 struct gatt_char_info *char_info = NULL;
2282 char_info = __bt_gatt_find_gatt_char_info(serv_path, char_path);
2283 if (char_info == NULL) {
2285 g_strfreev(line_argv);
2286 g_variant_builder_unref(inner_builder);
2287 g_variant_builder_unref(outer_builder);
2288 g_variant_builder_unref(invalidated_builder);
2290 return BLUETOOTH_ERROR_INVALID_DATA;
2293 char_info->value_length = value_length;
2295 char_info->char_value = (char *)realloc(char_info->char_value, value_length);
2296 if (char_info->char_value) {
2297 for (i = 0; i < value_length; i++)
2298 char_info->char_value[i] = char_value[i];
2303 g_strfreev(line_argv);
2304 g_variant_builder_unref(inner_builder);
2305 g_variant_builder_unref(outer_builder);
2306 g_variant_builder_unref(invalidated_builder);
2311 static void __bt_gatt_free_descriptor_info(struct gatt_desc_info *desc_info)
2318 g_free(desc_info->desc_path);
2319 g_free(desc_info->desc_uuid);
2320 g_free(desc_info->desc_value);
2322 for (i = 0; i < desc_info->flags_length; i++)
2323 g_free(desc_info->desc_flags[i]);
2328 static void __bt_gatt_free_characteristic_info(struct gatt_char_info *char_info)
2335 g_free(char_info->char_path);
2336 g_free(char_info->char_uuid);
2337 g_free(char_info->char_value);
2339 for (i = 0; i < char_info->flags_length; i++)
2340 g_free(char_info->char_flags[i]);
2345 static void __bt_gatt_free_service_info(struct gatt_service_info *svc_info)
2350 g_free(svc_info->serv_path);
2351 g_free(svc_info->service_uuid);
2355 BT_EXPORT_API int bluetooth_gatt_unregister_service(const char *svc_path)
2358 struct gatt_service_info *svc_info;
2360 int err = BLUETOOTH_ERROR_NONE;
2362 BT_DBG("svc_path %s", svc_path);
2363 svc_info = __bt_gatt_find_gatt_service_info(svc_path);
2366 BT_ERR("Unable to find service info");
2367 return BLUETOOTH_ERROR_NOT_FOUND;
2370 err = __bt_gatt_unregister_service(svc_path);
2371 if (err != BLUETOOTH_ERROR_NONE) {
2372 BT_DBG("Could not unregister application");
2376 for (l = svc_info->char_data; l != NULL; l = l->next) {
2377 struct gatt_char_info *char_info = l->data;
2379 if (char_info == NULL)
2382 for (l1 = char_info->desc_data; l1 != NULL; l1 = l1->next) {
2383 struct gatt_desc_info *desc_info = l1->data;
2385 if (desc_info == NULL)
2388 ret = g_dbus_connection_unregister_object(g_conn,
2389 desc_info->desc_id);
2391 __bt_gatt_emit_interface_removed(
2392 desc_info->desc_path,
2393 GATT_DESC_INTERFACE);
2395 err = BLUETOOTH_ERROR_INTERNAL;
2398 /* list remove & free */
2399 char_info->desc_data = g_slist_remove(char_info->desc_data, desc_info);
2400 __bt_gatt_free_descriptor_info(desc_info);
2403 g_slist_free(char_info->desc_data);
2404 char_info->desc_data = NULL;
2406 ret = g_dbus_connection_unregister_object(g_conn,
2407 char_info->char_id);
2409 __bt_gatt_emit_interface_removed(char_info->char_path,
2410 GATT_CHAR_INTERFACE);
2412 err = BLUETOOTH_ERROR_INTERNAL;
2415 /* list remove & free */
2416 svc_info->char_data = g_slist_remove(svc_info->char_data, char_info);
2417 __bt_gatt_free_characteristic_info(char_info);
2420 g_slist_free(svc_info->char_data);
2421 svc_info->char_data = NULL;
2423 ret = g_dbus_connection_unregister_object(g_conn, svc_info->serv_id);
2425 __bt_gatt_emit_interface_removed(svc_info->serv_path,
2426 GATT_SERV_INTERFACE);
2428 err = BLUETOOTH_ERROR_INTERNAL;
2431 ret = g_dbus_connection_unregister_object(g_conn, svc_info->prop_id);
2433 BT_DBG("Unregistered the service on properties interface");
2435 /* list remove & free */
2436 gatt_services = g_slist_remove(gatt_services, svc_info);
2437 __bt_gatt_free_service_info(svc_info);
2439 new_service = FALSE;
2441 if (gatt_services == NULL)
2443 else if (gatt_services->next == NULL)
2449 BT_EXPORT_API int bluetooth_gatt_send_response(int request_id, guint req_type,
2450 int resp_state, int offset, char *value, int value_length)
2452 struct gatt_req_info *req_info = NULL;
2454 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_GATT_SEND_RESPONSE)
2455 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
2456 BT_ERR("Don't have aprivilege to use this API");
2457 return BLUETOOTH_ERROR_PERMISSION_DEINED;
2460 req_info = __bt_gatt_find_request_info(request_id);
2463 if (resp_state != BLUETOOTH_ERROR_NONE) {
2464 g_dbus_method_invocation_return_dbus_error(req_info->context,
2465 "org.bluez.Error.Failed", "Application Error");
2467 gatt_requests = g_slist_remove(gatt_requests, req_info);
2469 req_info->context = NULL;
2470 if (req_info->attr_path)
2471 g_free(req_info->attr_path);
2472 if (req_info->svc_path)
2473 g_free(req_info->svc_path);
2476 return BLUETOOTH_ERROR_NONE;
2478 if (req_type == BLUETOOTH_GATT_ATT_REQUEST_TYPE_READ) {
2480 GVariantBuilder *inner_builder = NULL;
2481 inner_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
2482 if (value_length > 0 && value != NULL) {
2483 for (i = 0; i < value_length; i++)
2484 g_variant_builder_add(inner_builder, "y", value[i]);
2486 g_dbus_method_invocation_return_value(req_info->context,
2487 g_variant_new("(ay)", inner_builder));
2488 g_variant_builder_unref(inner_builder);
2490 g_dbus_method_invocation_return_value(req_info->context, NULL);
2492 gatt_requests = g_slist_remove(gatt_requests, req_info);
2494 req_info->context = NULL;
2495 if (req_info->attr_path)
2496 g_free(req_info->attr_path);
2497 if (req_info->svc_path)
2498 g_free(req_info->svc_path);
2501 return BLUETOOTH_ERROR_INTERNAL;
2504 return BLUETOOTH_ERROR_NONE;
2507 BT_EXPORT_API int bluetooth_gatt_server_set_notification(const char *char_path,
2508 bluetooth_device_address_t *unicast_address)
2510 GVariantBuilder *outer_builder;
2511 GVariantBuilder *invalidated_builder;
2512 GError *error = NULL;
2513 gboolean notify = TRUE;
2514 gboolean ret = TRUE;
2515 int err = BLUETOOTH_ERROR_NONE;
2516 gchar **line_argv = NULL;
2517 gchar *serv_path = NULL;
2518 char addr[20] = { 0 };
2520 line_argv = g_strsplit_set(char_path, "/", 0);
2521 serv_path = g_strdup_printf("/%s/%s/%s", line_argv[1], line_argv[2], line_argv[3]);
2523 if (!__bt_gatt_get_service_state(serv_path)) {
2524 BT_DBG("service not registered for this characteristic \n");
2526 g_strfreev(line_argv);
2527 return BLUETOOTH_ERROR_INTERNAL;
2532 outer_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
2533 invalidated_builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
2535 g_variant_builder_add(outer_builder, "{sv}", "Notifying",
2536 g_variant_new("b", notify));
2538 if (unicast_address) {
2539 _bt_convert_addr_type_to_string(addr,
2540 (unsigned char *)unicast_address->addr);
2542 g_variant_builder_add(outer_builder, "{sv}", "Unicast",
2543 g_variant_new("s", addr));
2545 BT_DBG("Set characteristic Notification \n");
2546 ret = g_dbus_connection_emit_signal(g_conn, NULL,
2548 "org.freedesktop.DBus.Properties",
2549 "PropertiesChanged",
2550 g_variant_new("(sa{sv}as)",
2551 "org.bluez.GattCharacteristic1",
2552 outer_builder, invalidated_builder),
2556 if (error != NULL) {
2557 BT_ERR("D-Bus API failure: errCode[%x], \
2559 error->code, error->message);
2560 g_clear_error(&error);
2562 err = BLUETOOTH_ERROR_INTERNAL;
2565 g_strfreev(line_argv);
2566 g_variant_builder_unref(outer_builder);
2567 g_variant_builder_unref(invalidated_builder);