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"
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);
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);
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);
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;
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);
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) {
1141 BT_ERR("Gatt Request not found");
1145 static GDBusProxy *__bt_gatt_gdbus_init_manager_proxy(const gchar *service,
1146 const gchar *path, const gchar *interface)
1152 g_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM,
1157 BT_ERR("Unable to connect to gdbus: %s", err->message);
1158 g_clear_error(&err);
1163 proxy = g_dbus_proxy_new_sync(g_conn,
1164 G_DBUS_PROXY_FLAGS_NONE, NULL,
1166 interface, NULL, &err);
1170 BT_ERR("Unable to create proxy: %s", err->message);
1171 g_clear_error(&err);
1175 manager_gproxy = proxy;
1180 static GDBusProxy *__bt_gatt_gdbus_get_manager_proxy(const gchar *service,
1181 const gchar *path, const gchar *interface)
1183 return (manager_gproxy) ? manager_gproxy :
1184 __bt_gatt_gdbus_init_manager_proxy(service,
1188 int bluetooth_gatt_convert_prop2string(
1189 bt_gatt_characteristic_property_t properties,
1190 char *char_properties[])
1194 if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_BROADCAST) {
1195 char_properties[flag_count] = g_strdup("broadcast");
1198 if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ) {
1199 char_properties[flag_count] = g_strdup("read");
1202 if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE_NO_RESPONSE) {
1203 char_properties[flag_count] = g_strdup("write-without-response");
1206 if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE) {
1207 char_properties[flag_count] = g_strdup("write");
1210 if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_NOTIFY) {
1211 char_properties[flag_count] = g_strdup("notify");
1214 if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_INDICATE) {
1215 char_properties[flag_count] = g_strdup("indicate");
1218 if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_SIGNED_WRITE) {
1219 char_properties[flag_count] = g_strdup("authenticated-signed-writes");
1222 if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_RELIABLE_WRITE) {
1223 char_properties[flag_count] = g_strdup("reliable-write");
1226 if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITABLE_AUXILIARIES) {
1227 char_properties[flag_count] = g_strdup("writable-auxiliaries");
1230 if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_READ) {
1231 char_properties[flag_count] = g_strdup("encrypt-read");
1234 if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_WRITE) {
1235 char_properties[flag_count] = g_strdup("encrypt-write");
1238 if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_READ) {
1239 char_properties[flag_count] = g_strdup("encrypt-authenticated-read");
1242 if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_WRITE) {
1243 char_properties[flag_count] = g_strdup("encrypt-authenticated-write");
1247 if (flag_count == 0) {
1248 char_properties[flag_count] = g_strdup("read");
1255 int bluetooth_gatt_convert_perm2string(
1256 bt_gatt_permission_t properties,
1257 char *char_properties[])
1261 if (properties & BLUETOOTH_GATT_PERMISSION_READ) {
1262 char_properties[flag_count] = g_strdup("read");
1265 if (properties & BLUETOOTH_GATT_PERMISSION_WRITE) {
1266 char_properties[flag_count] = g_strdup("write");
1269 if (properties & BLUETOOTH_GATT_PERMISSION_ENCRYPT_READ) {
1270 char_properties[flag_count] = g_strdup("encrypt-read");
1273 if (properties & BLUETOOTH_GATT_PERMISSION_ENCRYPT_WRITE) {
1274 char_properties[flag_count] = g_strdup("encrypt-write");
1277 if (properties & BLUETOOTH_GATT_PERMISSION_ENCRYPT_AUTHENTICATED_READ) {
1278 char_properties[flag_count] = g_strdup("encrypt-authenticated-read");
1281 if (properties & BLUETOOTH_GATT_PERMISSION_ENCRYPT_AUTHENTICATED_WRITE) {
1282 char_properties[flag_count] = g_strdup("encrypt-authenticated-write");
1286 if (flag_count == 0) {
1287 char_properties[flag_count] = g_strdup("read");
1294 static void __bt_gatt_set_service_state(const char *service_path,
1297 struct gatt_service_info *svc_info = NULL;
1298 svc_info = __bt_gatt_find_gatt_service_info(service_path);
1300 if (svc_info != NULL) {
1301 BT_DBG("Updating the gatt service register state %d", state);
1302 svc_info->is_svc_registered = state;
1306 BT_DBG("gatt service not found");
1309 static gboolean __bt_gatt_get_service_state(const char *service_path)
1311 struct gatt_service_info *svc_info = NULL;
1313 svc_info = __bt_gatt_find_gatt_service_info(service_path);
1315 if (svc_info != NULL) {
1316 BT_DBG("Return the state of the gatt service %d",
1317 svc_info->is_svc_registered);
1318 return svc_info->is_svc_registered;
1321 BT_DBG("gatt service info is NULL");
1325 void get_service_cb(GObject *object, GAsyncResult *res, gpointer user_data)
1327 GError *error = NULL;
1329 GVariantIter *iter = NULL;
1330 const gchar *key = NULL;
1331 GVariant *value = NULL;
1332 const gchar *service = NULL;
1333 const gchar *characteristic = NULL;
1334 const gchar *descriptor = NULL;
1338 result = g_dbus_proxy_call_finish(manager_gproxy, res, &error);
1340 if (result == NULL) {
1341 /* dBUS-RPC is failed */
1342 BT_ERR("Dbus-RPC is failed\n");
1344 if (error != NULL) {
1345 /* dBUS gives error cause */
1346 BT_ERR("D-Bus API failure: errCode[%x], message[%s]\n",
1347 error->code, error->message);
1348 g_clear_error(&error);
1351 char *char_cmp = NULL;
1352 g_variant_get(result, "(a{sv})", &iter);
1353 char_cmp = g_strdup_printf("Characteristic%d", n_char);
1355 while (g_variant_iter_loop(iter, "{sv}", &key, &value)) {
1356 if (g_strcmp0(key, "Service") == 0) {
1357 service = g_variant_get_string(value, NULL);
1358 BT_DBG("Service %s", service);
1359 } else if (g_strcmp0(key, char_cmp) == 0) {
1360 characteristic = g_variant_get_string(value, NULL);
1362 char_cmp = g_strdup_printf("Characteristic%d", ++n_char);
1363 BT_DBG("%s", characteristic);
1364 } else if (g_strcmp0(key, "Descriptor") == 0) {
1365 descriptor = g_variant_get_string(value, NULL);
1366 BT_DBG("Descriptor %s", descriptor);
1369 g_variant_iter_free(iter);
1371 /* TODO: Store the service informationa and
1372 * Send respponse to CAPI layer. */
1374 g_variant_unref(result);
1379 void register_application_cb(GObject *object, GAsyncResult *res, gpointer user_data)
1381 BT_INFO("RegisterApplication is completed");
1383 GError *error = NULL;
1386 register_pending_cnt = 0;
1388 result = g_dbus_proxy_call_finish(manager_gproxy, res, &error);
1390 if (result == NULL) {
1391 /* dBUS-RPC is failed */
1392 BT_ERR("Dbus-RPC is failed\n");
1394 if (error != NULL) {
1395 /* dBUS gives error cause */
1396 BT_ERR("D-Bus API failure: errCode[%x], message[%s]\n",
1397 error->code, error->message);
1398 g_clear_error(&error);
1401 g_variant_unref(result);
1405 void unregister_application_cb(GObject *object, GAsyncResult *res,
1408 BT_INFO("UnregisterApplication is completed");
1410 GError *error = NULL;
1413 result = g_dbus_proxy_call_finish(manager_gproxy, res, &error);
1415 if (result == NULL) {
1416 /* dBUS-RPC is failed */
1417 BT_ERR("Dbus-RPC is failed\n");
1419 if (error != NULL) {
1420 /* dBUS gives error cause */
1421 BT_ERR("D-Bus API failure: errCode[%x], message[%s]\n",
1422 error->code, error->message);
1423 g_clear_error(&error);
1426 g_variant_unref(result);
1430 static int __bt_gatt_unregister_service(const char *service_path)
1432 if (!__bt_gatt_get_service_state(service_path)) {
1433 BT_DBG("service not registered \n");
1434 return BLUETOOTH_ERROR_NOT_FOUND;
1437 return BLUETOOTH_ERROR_NONE;
1440 BT_EXPORT_API int bluetooth_gatt_unregister_application(void)
1442 GDBusProxy *proxy = NULL;
1444 if (is_server_started) {
1445 proxy = __bt_gatt_gdbus_get_manager_proxy("org.bluez",
1446 "/org/bluez/hci0", GATT_MNGR_INTERFACE);
1448 if (proxy == NULL || app_path == NULL)
1449 return BLUETOOTH_ERROR_INTERNAL;
1451 BT_INFO("UnregisterApplication");
1453 /* Async Call to Unregister Service */
1454 g_dbus_proxy_call(proxy,
1455 "UnregisterApplication",
1456 g_variant_new("(o)",
1458 G_DBUS_CALL_FLAGS_NONE, -1,
1460 (GAsyncReadyCallback) unregister_application_cb,
1463 is_server_started = false;
1464 return BLUETOOTH_ERROR_NONE;
1467 BT_INFO("GATT server not started");
1468 return BLUETOOTH_ERROR_NONE;
1471 static GDBusConnection *__bt_gatt_get_gdbus_connection(void)
1473 GDBusConnection *local_system_gconn = NULL;
1477 if (g_conn == NULL) {
1478 address = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
1479 if (address == NULL) {
1481 BT_ERR("Failed to get bus address: %s", err->message);
1482 g_clear_error(&err);
1487 g_conn = g_dbus_connection_new_for_address_sync(address,
1488 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
1489 G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
1490 NULL, /* GDBusAuthObserver */
1495 BT_ERR("Unable to connect to dbus: %s", err->message);
1496 g_clear_error(&err);
1500 } else if (g_dbus_connection_is_closed(g_conn)) {
1501 address = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
1502 if (address == NULL) {
1504 BT_ERR("Failed to get bus address: %s", err->message);
1505 g_clear_error(&err);
1510 local_system_gconn = g_dbus_connection_new_for_address_sync(address,
1511 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
1512 G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
1513 NULL, /* GDBusAuthObserver */
1517 if (!local_system_gconn) {
1518 BT_ERR("Unable to connect to dbus: %s", err->message);
1519 g_clear_error(&err);
1522 g_conn = local_system_gconn;
1528 BT_EXPORT_API int bluetooth_gatt_init(void)
1530 GDBusConnection *conn;
1531 GError *error = NULL;
1532 GDBusNodeInfo *node_info = NULL;
1534 if (app_path != NULL) {
1535 BT_ERR("app path already exists! initialized");
1536 return BLUETOOTH_ERROR_ALREADY_INITIALIZED;
1539 if (owner_id == 0) {
1540 owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
1541 BT_GATT_SERVICE_NAME,
1542 G_BUS_NAME_OWNER_FLAGS_NONE,
1543 NULL, NULL, NULL, NULL, NULL);
1546 BT_DBG("owner_id is [%d]", owner_id);
1547 app_path = g_strdup_printf("/com/%d", getpid());
1551 conn = __bt_gatt_get_gdbus_connection();
1553 BT_ERR("Unable to get connection");
1557 /* Register ObjectManager interface */
1558 node_info = __bt_gatt_create_method_node_info(
1559 manager_introspection_xml);
1561 if (node_info == NULL) {
1562 BT_ERR("failed to get node info");
1566 if (manager_id == 0) {
1567 BT_INFO("manager_id does not exists");
1569 manager_id = g_dbus_connection_register_object(g_conn, app_path,
1570 node_info->interfaces[0],
1571 &manager_interface_vtable,
1572 NULL, NULL, &error);
1575 if (manager_id == 0) {
1576 BT_ERR("failed to register: %s", error->message);
1577 g_error_free(error);
1581 return BLUETOOTH_ERROR_NONE;
1585 g_bus_unown_name(owner_id);
1592 __bt_gatt_close_gdbus_connection();
1594 return BLUETOOTH_ERROR_INTERNAL;
1597 BT_EXPORT_API int bluetooth_gatt_deinit()
1599 int ret = BLUETOOTH_ERROR_NONE;
1600 /* Unown gdbus bus */
1602 /* remove/unregister all services */
1603 BT_DBG("removing all registered gatt service\n");
1604 bluetooth_gatt_delete_services();
1606 /* unregister the exported interface for object manager */
1607 g_dbus_connection_unregister_object(g_conn,
1612 ret = bluetooth_gatt_unregister_application();
1613 if (ret != BLUETOOTH_ERROR_NONE)
1614 BT_ERR("Fail to unregister application\n");
1616 g_bus_unown_name(owner_id);
1622 BT_DBG("Gatt service deinitialized \n");
1624 g_slist_free(gatt_services);
1625 gatt_services = NULL;
1627 g_object_unref(manager_gproxy);
1628 manager_gproxy = NULL;
1630 __bt_gatt_close_gdbus_connection();
1635 __bt_gatt_close_gdbus_connection();
1637 return BLUETOOTH_ERROR_NOT_FOUND;
1640 BT_EXPORT_API int bluetooth_gatt_add_service(const char *svc_uuid,
1643 GError *error = NULL;
1645 GDBusNodeInfo *node_info;
1647 GVariantBuilder *builder = NULL;
1648 GVariantBuilder *builder1 = NULL;
1649 GVariantBuilder *inner_builder = NULL;
1650 gboolean svc_primary = TRUE;
1651 struct gatt_service_info *serv_info = NULL;
1653 node_info = __bt_gatt_create_method_node_info(
1654 service_introspection_xml);
1656 if (node_info == NULL)
1657 return BLUETOOTH_ERROR_INTERNAL;
1659 path = g_strdup_printf("%s"GATT_SERV_OBJECT_PATH"%d", app_path, serv_id++);
1660 BT_DBG("gatt service path is [%s]", path);
1662 object_id = g_dbus_connection_register_object(g_conn, path,
1663 node_info->interfaces[0],
1664 &serv_interface_vtable,
1665 NULL, NULL, &error);
1667 if (object_id == 0) {
1668 BT_ERR("failed to register: %s", error->message);
1669 g_error_free(error);
1672 return BLUETOOTH_ERROR_INTERNAL;
1675 /* Add object_id/gatt service information; it's required at the time of
1676 * service unregister and Getmanagedobjects
1678 serv_info = g_new0(struct gatt_service_info, 1);
1680 serv_info->serv_path = g_strdup(path);
1681 serv_info->serv_id = object_id;
1682 serv_info->service_uuid = g_strdup(svc_uuid);
1683 serv_info->is_svc_registered = FALSE;
1684 serv_info->is_svc_primary = svc_primary;
1686 gatt_services = g_slist_append(gatt_services, serv_info);
1688 /* emit interfacesadded signal here for service path */
1689 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
1690 inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1692 g_variant_builder_add(inner_builder, "{sv}",
1693 "UUID", g_variant_new_string(svc_uuid));
1695 g_variant_builder_add(inner_builder, "{sv}",
1696 "Primary", g_variant_new_boolean(svc_primary));
1698 builder1 = g_variant_builder_new(G_VARIANT_TYPE("ao"));
1700 g_variant_builder_add(inner_builder, "{sv}", "Characteristics",
1701 g_variant_new("ao", builder1));
1703 g_variant_builder_add(builder, "{sa{sv}}",
1704 GATT_SERV_INTERFACE, inner_builder);
1706 g_dbus_connection_emit_signal(g_conn, NULL, "/",
1707 "org.freedesktop.Dbus.ObjectManager",
1709 g_variant_new("(oa{sa{sv}})",
1715 *svc_path = g_strdup(path);
1718 g_variant_builder_unref(inner_builder);
1719 g_variant_builder_unref(builder);
1720 g_variant_builder_unref(builder1);
1722 return BLUETOOTH_ERROR_NONE;
1725 BT_EXPORT_API int bluetooth_gatt_add_new_characteristic(
1726 const char *svc_path, const char *char_uuid,
1727 bt_gatt_permission_t permissions,
1728 bt_gatt_characteristic_property_t properties,
1732 GError *error = NULL;
1734 GDBusNodeInfo *node_info;
1736 GVariantBuilder *builder = NULL;
1737 GVariantBuilder *inner_builder = NULL;
1738 struct gatt_service_info *serv_info = NULL;
1739 struct gatt_char_info *char_info = NULL;
1740 GVariantBuilder *builder2 = NULL;
1741 GVariantBuilder *builder3 = NULL;
1742 GVariant *flags_val = NULL;
1744 char *char_flags[NUMBER_OF_FLAGS];
1749 new_service = FALSE;
1752 BT_DBG("gatt svc_path path is [%s]", svc_path);
1753 serv_info = __bt_gatt_find_gatt_service_info(svc_path);
1754 if (serv_info == NULL)
1755 return BLUETOOTH_ERROR_INVALID_PARAM;
1757 node_info = __bt_gatt_create_method_node_info(
1758 characteristics_introspection_xml);
1760 if (node_info == NULL)
1761 return BLUETOOTH_ERROR_INTERNAL;
1763 path = g_strdup_printf("%s/characteristic%d", svc_path, char_id++);
1764 BT_DBG("gatt characteristic path is [%s]", path);
1766 object_id = g_dbus_connection_register_object(g_conn, path,
1767 node_info->interfaces[0],
1768 &char_interface_vtable,
1769 NULL, NULL, &error);
1771 if (object_id == 0) {
1772 BT_ERR("failed to register: %s", error->message);
1773 g_error_free(error);
1776 return BLUETOOTH_ERROR_INTERNAL;
1779 if (permissions & BLUETOOTH_GATT_PERMISSION_ENCRYPT_READ)
1780 properties |= BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_READ;
1781 if (permissions & BLUETOOTH_GATT_PERMISSION_ENCRYPT_AUTHENTICATED_READ)
1782 properties |= BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_READ;
1783 if (permissions & BLUETOOTH_GATT_PERMISSION_ENCRYPT_WRITE)
1784 properties |= BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_WRITE;
1785 if (permissions & BLUETOOTH_GATT_PERMISSION_ENCRYPT_AUTHENTICATED_WRITE)
1786 properties |= BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_WRITE;
1788 flag_count = bluetooth_gatt_convert_prop2string(properties, char_flags);
1790 char_info = g_new0(struct gatt_char_info, 1);
1792 char_info->char_path = g_strdup(path);
1793 char_info->char_id = object_id;
1794 char_info->char_uuid = g_strdup(char_uuid);
1796 for (i = 0; i < flag_count; i++) {
1797 char_info->char_flags[i] = char_flags[i];
1800 char_info->flags_length = flag_count;
1802 serv_info->char_data = g_slist_append(serv_info->char_data, char_info);
1804 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
1805 inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1807 g_variant_builder_add(inner_builder, "{sv}", "UUID",
1808 g_variant_new("s", char_uuid));
1809 g_variant_builder_add(inner_builder, "{sv}", "Service",
1810 g_variant_new("o", svc_path));
1812 builder2 = g_variant_builder_new(G_VARIANT_TYPE("as"));
1814 for (i = 0; i < flag_count; i++) {
1815 g_variant_builder_add(builder2, "s", char_flags[i]);
1818 flags_val = g_variant_new("as", builder2);
1819 g_variant_builder_add(inner_builder, "{sv}", "Flags",
1822 builder3 = g_variant_builder_new(G_VARIANT_TYPE("ao"));
1824 g_variant_builder_add(inner_builder, "{sv}", "Descriptors",
1825 g_variant_new("ao", builder3));
1827 g_variant_builder_add(builder, "{sa{sv}}",
1828 GATT_CHAR_INTERFACE,
1831 g_dbus_connection_emit_signal(g_conn, NULL, "/",
1832 "org.freedesktop.Dbus.ObjectManager",
1834 g_variant_new("(oa{sa{sv}})",
1838 *char_path = g_strdup(path);
1844 g_variant_builder_unref(inner_builder);
1845 g_variant_builder_unref(builder);
1846 g_variant_builder_unref(builder2);
1847 g_variant_builder_unref(builder3);
1849 return BLUETOOTH_ERROR_NONE;
1852 BT_EXPORT_API int bluetooth_gatt_set_characteristic_value(
1853 const char *characteristic, const char *char_value,
1856 gchar **line_argv = NULL;
1857 char *serv_path = NULL;
1858 struct gatt_char_info *char_info = NULL;
1859 GVariantBuilder *builder1 = NULL;
1860 GVariantBuilder *builder = NULL;
1861 GVariantBuilder *inner_builder = NULL;
1862 GVariant *char_val = NULL;
1863 GError *error = NULL;
1865 int res = BLUETOOTH_ERROR_NONE;
1867 line_argv = g_strsplit_set(characteristic, "/", 0);
1868 serv_path = g_strdup_printf("/%s/%s/%s", line_argv[1], line_argv[2], line_argv[3]);
1870 char_info = __bt_gatt_find_gatt_char_info(serv_path, characteristic);
1872 if (char_info == NULL) {
1873 /* Fix : RESOURCE_LEAK */
1874 res = BLUETOOTH_ERROR_INVALID_PARAM;
1878 char_info->value_length = value_length;
1880 char_info->char_value = (char *)malloc(value_length);
1881 /* Fix : NULL_RETURNS */
1882 if (char_info->char_value == NULL) {
1883 res = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1887 for (i = 0; i < value_length; i++)
1888 char_info->char_value[i] = char_value[i];
1890 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
1891 inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1893 builder1 = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
1895 for (i = 0; i < value_length; i++) {
1896 g_variant_builder_add(builder1, "y", char_value[i]);
1899 char_val = g_variant_new("ay", builder1);
1900 g_variant_builder_add(inner_builder, "{sv}", "Value", char_val);
1902 g_variant_builder_add(builder, "{sa{sv}}",
1903 GATT_CHAR_INTERFACE,
1906 g_dbus_connection_emit_signal(g_conn, NULL, "/",
1907 "org.freedesktop.Dbus.ObjectManager",
1909 g_variant_new("(oa{sa{sv}})",
1910 char_info->char_path, builder),
1913 g_variant_builder_unref(inner_builder);
1914 g_variant_builder_unref(builder);
1915 g_variant_builder_unref(builder1);
1917 g_strfreev(line_argv);
1923 BT_EXPORT_API int bluetooth_gatt_add_descriptor(
1924 const char *char_path, const char *desc_uuid,
1925 bt_gatt_permission_t permissions,
1928 static int desc_id = 1;
1929 GError *error = NULL;
1931 GDBusNodeInfo *node_info;
1933 GVariantBuilder *builder = NULL;
1934 GVariantBuilder *inner_builder = NULL;
1935 struct gatt_char_info *char_info = NULL;
1936 struct gatt_desc_info *desc_info = NULL;
1937 gchar **line_argv = NULL;
1939 GVariantBuilder *builder2 = NULL;
1940 GVariant *flags_val = NULL;
1942 char *desc_flags[NUMBER_OF_FLAGS];
1950 line_argv = g_strsplit_set(char_path, "/", 0);
1951 serv_path = g_strdup_printf("/%s/%s/%s", line_argv[1], line_argv[2], line_argv[3]);
1953 char_info = __bt_gatt_find_gatt_char_info(serv_path, char_path);
1954 if (char_info == NULL) {
1955 g_strfreev(line_argv);
1957 return BLUETOOTH_ERROR_INVALID_PARAM;
1960 node_info = __bt_gatt_create_method_node_info(
1961 descriptor_introspection_xml);
1963 if (node_info == NULL) {
1964 g_strfreev(line_argv);
1966 return BLUETOOTH_ERROR_INTERNAL;
1969 path = g_strdup_printf("%s/descriptor%d", char_path, desc_id++);
1970 BT_DBG("gatt descriptor path is [%s]", path);
1972 object_id = g_dbus_connection_register_object(g_conn, path,
1973 node_info->interfaces[0],
1974 &desc_interface_vtable,
1975 NULL, NULL, &error);
1977 if (object_id == 0) {
1978 BT_ERR("failed to register: %s", error->message);
1979 g_error_free(error);
1981 g_strfreev(line_argv);
1984 return BLUETOOTH_ERROR_INTERNAL;
1987 flag_count = bluetooth_gatt_convert_perm2string(permissions, desc_flags);
1989 desc_info = g_new0(struct gatt_desc_info, 1);
1991 desc_info->desc_path = g_strdup(path);
1992 desc_info->desc_id = object_id;
1993 desc_info->desc_uuid = g_strdup(desc_uuid);
1995 for (i = 0; i < flag_count; i++) {
1996 desc_info->desc_flags[i] = desc_flags[i];
1999 desc_info->flags_length = flag_count;
2001 char_info->desc_data = g_slist_append(char_info->desc_data, desc_info);
2003 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
2004 inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
2006 g_variant_builder_add(inner_builder, "{sv}", "UUID",
2007 g_variant_new("s", desc_uuid));
2008 g_variant_builder_add(inner_builder, "{sv}", "Characteristic",
2009 g_variant_new("o", char_path));
2011 builder2 = g_variant_builder_new(G_VARIANT_TYPE("as"));
2013 for (i = 0; i < flag_count; i++) {
2014 g_variant_builder_add(builder2, "s", desc_flags[i]);
2017 flags_val = g_variant_new("as", builder2);
2018 g_variant_builder_add(inner_builder, "{sv}", "Flags",
2021 g_variant_builder_add(builder, "{sa{sv}}",
2022 GATT_DESC_INTERFACE,
2025 g_dbus_connection_emit_signal(g_conn, NULL, "/",
2026 "org.freedesktop.Dbus.ObjectManager",
2028 g_variant_new("(oa{sa{sv}})",
2032 *desc_path = g_strdup(path);
2036 g_strfreev(line_argv);
2037 g_variant_builder_unref(inner_builder);
2038 g_variant_builder_unref(builder);
2040 return BLUETOOTH_ERROR_NONE;
2043 BT_EXPORT_API int bluetooth_gatt_set_descriptor_value(
2044 const char *desc_path, const char *desc_value,
2047 GError *error = NULL;
2048 GVariantBuilder *builder = NULL;
2049 GVariantBuilder *inner_builder = NULL;
2050 GVariantBuilder *builder1 = NULL;
2051 struct gatt_desc_info *desc_info = NULL;
2052 gchar **line_argv = NULL;
2054 GVariant *desc_val = NULL;
2055 char *serv_path = NULL;
2058 line_argv = g_strsplit_set(desc_path, "/", 0);
2059 serv_path = g_strdup_printf("/%s/%s/%s", line_argv[1], line_argv[2], line_argv[3]);
2060 char_path = g_strdup_printf("%s/%s", serv_path, line_argv[4]);
2062 desc_info = __bt_gatt_find_gatt_desc_info(serv_path, char_path, desc_path);
2064 /* Free the allocated memory */
2065 g_strfreev(line_argv);
2069 /* Fix : NULL_RETURNS */
2070 retv_if(desc_info == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
2072 desc_info->desc_value = (char *)malloc(value_length);
2074 /* Fix : NULL_RETURNS */
2075 retv_if(desc_info->desc_value == NULL, BLUETOOTH_ERROR_MEMORY_ALLOCATION);
2077 for (i = 0; i < value_length; i++)
2078 desc_info->desc_value[i] = desc_value[i];
2080 desc_info->value_length = value_length;
2082 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
2083 inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
2085 builder1 = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
2087 for (i = 0; i < value_length; i++) {
2088 g_variant_builder_add(builder1, "y", desc_value[i]);
2090 desc_val = g_variant_new("ay", builder1);
2091 g_variant_builder_add(inner_builder, "{sv}", "Value", desc_val);
2093 g_variant_builder_add(builder, "{sa{sv}}",
2094 GATT_DESC_INTERFACE,
2097 g_dbus_connection_emit_signal(g_conn, NULL, "/",
2098 "org.freedesktop.Dbus.ObjectManager",
2100 g_variant_new("(oa{sa{sv}})",
2101 desc_info->desc_path, builder),
2104 g_variant_builder_unref(inner_builder);
2105 g_variant_builder_unref(builder);
2106 g_variant_builder_unref(builder1);
2108 return BLUETOOTH_ERROR_NONE;
2111 int bluetooth_gatt_get_service(const char *svc_uuid)
2113 GDBusProxy *proxy = NULL;
2116 proxy = __bt_gatt_gdbus_get_manager_proxy("org.bluez",
2117 "/org/bluez/hci0", GATT_MNGR_INTERFACE);
2119 return BLUETOOTH_ERROR_INTERNAL;
2121 uuid = g_strdup(svc_uuid);
2123 g_dbus_proxy_call(proxy,
2125 g_variant_new("(s)",
2127 G_DBUS_CALL_FLAGS_NONE, -1,
2129 (GAsyncReadyCallback) get_service_cb,
2134 return BLUETOOTH_ERROR_NONE;
2137 BT_EXPORT_API int bluetooth_gatt_register_service(
2138 const char *svc_path)
2140 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_GATT_REGISTER_SERVICE)
2141 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
2142 BT_ERR("Don't have aprivilege to use this API");
2143 return BLUETOOTH_ERROR_PERMISSION_DEINED;
2146 register_pending_cnt++;
2148 if (__bt_gatt_get_service_state(svc_path)) {
2149 BT_DBG("service already registered \n");
2150 return BLUETOOTH_ERROR_NONE;
2153 __bt_gatt_set_service_state(svc_path, TRUE);
2155 return BLUETOOTH_ERROR_NONE;
2158 BT_EXPORT_API int bluetooth_gatt_register_application(void)
2160 GDBusProxy *proxy = NULL;
2162 if (!is_server_started) {
2164 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_GATT_REGISTER_APPLICATION)
2165 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
2166 BT_ERR("Don't have aprivilege to use this API");
2167 return BLUETOOTH_ERROR_PERMISSION_DEINED;
2170 proxy = __bt_gatt_gdbus_get_manager_proxy("org.bluez",
2171 "/org/bluez/hci0", GATT_MNGR_INTERFACE);
2172 if (proxy == NULL || app_path == NULL)
2173 return BLUETOOTH_ERROR_INTERNAL;
2175 BT_INFO("RegisterApplication");
2177 g_dbus_proxy_call(proxy,
2178 "RegisterApplication",
2179 g_variant_new("(oa{sv})",
2181 G_DBUS_CALL_FLAGS_NONE, -1,
2183 (GAsyncReadyCallback) register_application_cb,
2186 is_server_started = true;
2188 return BLUETOOTH_ERROR_NONE;
2191 BT_INFO("Already RegisterApplication");
2193 return BLUETOOTH_ERROR_NONE;
2196 BT_EXPORT_API int bluetooth_gatt_delete_services(void)
2199 int error = BLUETOOTH_ERROR_NONE;
2203 for (l = gatt_services; l != NULL; l = l->next) {
2204 struct gatt_service_info *info = l->data;
2205 BT_DBG("svc_path is %s", info->serv_path);
2206 if (bluetooth_gatt_unregister_service(info->serv_path)
2207 != BLUETOOTH_ERROR_NONE) {
2208 error = BLUETOOTH_ERROR_INTERNAL;
2209 BT_DBG(" Error in removing service %s \n",
2213 BT_DBG(" All services removed successfully.\n ");
2215 BT_DBG(" There are no registered services.\n ");
2218 g_slist_free(gatt_services);
2219 gatt_services = NULL;
2222 if (error != BLUETOOTH_ERROR_NONE)
2225 return BLUETOOTH_ERROR_NONE;
2228 BT_EXPORT_API int bluetooth_gatt_update_characteristic(
2229 const char *char_path, const char* char_value,
2232 GVariantBuilder *outer_builder;
2233 GVariantBuilder *inner_builder;
2234 GVariantBuilder *invalidated_builder;
2235 GVariant *update_value = NULL;
2236 GError *error = NULL;
2237 gboolean ret = FALSE;
2238 int err = BLUETOOTH_ERROR_NONE;
2240 gchar **line_argv = NULL;
2241 gchar *serv_path = NULL;
2243 line_argv = g_strsplit_set(char_path, "/", 0);
2244 serv_path = g_strdup_printf("/%s/%s/%s", line_argv[1], line_argv[2], line_argv[3]);
2246 if (!__bt_gatt_get_service_state(serv_path)) {
2247 BT_DBG("service not registered for this characteristic \n");
2249 g_strfreev(line_argv);
2250 return BLUETOOTH_ERROR_INTERNAL;
2253 outer_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
2254 invalidated_builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
2256 inner_builder = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
2257 for (i = 0; i < value_length; i++) {
2258 g_variant_builder_add(inner_builder, "y", char_value[i]);
2261 update_value = g_variant_new("ay", inner_builder);
2263 outer_builder = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
2264 g_variant_builder_add(outer_builder, "{sv}", "Value",
2267 BT_DBG("Updating characteristic value \n");
2268 ret = g_dbus_connection_emit_signal(g_conn, NULL,
2270 "org.freedesktop.DBus.Properties",
2271 "PropertiesChanged",
2272 g_variant_new("(sa{sv}as)",
2273 "org.bluez.GattCharacteristic1",
2274 outer_builder, invalidated_builder),
2278 if (error != NULL) {
2279 BT_ERR("D-Bus API failure: errCode[%x], \
2281 error->code, error->message);
2282 g_clear_error(&error);
2284 err = BLUETOOTH_ERROR_INTERNAL;
2286 struct gatt_char_info *char_info = NULL;
2288 char_info = __bt_gatt_find_gatt_char_info(serv_path, char_path);
2289 if (char_info == NULL) {
2291 g_strfreev(line_argv);
2292 g_variant_builder_unref(inner_builder);
2293 g_variant_builder_unref(outer_builder);
2294 g_variant_builder_unref(invalidated_builder);
2296 return BLUETOOTH_ERROR_INVALID_DATA;
2299 char_info->value_length = value_length;
2301 char_info->char_value = (char *)realloc(char_info->char_value, value_length);
2302 if (char_info->char_value) {
2303 for (i = 0; i < value_length; i++) {
2304 char_info->char_value[i] = char_value[i];
2310 g_strfreev(line_argv);
2311 g_variant_builder_unref(inner_builder);
2312 g_variant_builder_unref(outer_builder);
2313 g_variant_builder_unref(invalidated_builder);
2318 static void __bt_gatt_free_descriptor_info(struct gatt_desc_info *desc_info)
2325 g_free(desc_info->desc_path);
2326 g_free(desc_info->desc_uuid);
2327 g_free(desc_info->desc_value);
2329 for (i = 0; i < desc_info->flags_length; i++)
2330 g_free(desc_info->desc_flags[i]);
2335 static void __bt_gatt_free_characteristic_info(struct gatt_char_info *char_info)
2342 g_free(char_info->char_path);
2343 g_free(char_info->char_uuid);
2344 g_free(char_info->char_value);
2346 for (i = 0; i < char_info->flags_length; i++)
2347 g_free(char_info->char_flags[i]);
2352 static void __bt_gatt_free_service_info(struct gatt_service_info *svc_info)
2357 g_free(svc_info->serv_path);
2358 g_free(svc_info->service_uuid);
2362 BT_EXPORT_API int bluetooth_gatt_unregister_service(const char *svc_path)
2365 struct gatt_service_info *svc_info;
2367 int err = BLUETOOTH_ERROR_NONE;
2369 BT_DBG("svc_path %s", svc_path);
2370 svc_info = __bt_gatt_find_gatt_service_info(svc_path);
2373 BT_ERR("Unable to find service info");
2374 return BLUETOOTH_ERROR_NOT_FOUND;
2377 err = __bt_gatt_unregister_service(svc_path);
2378 if (err != BLUETOOTH_ERROR_NONE) {
2379 BT_DBG("Could not unregister application");
2383 for (l = svc_info->char_data; l != NULL; l = l->next) {
2384 struct gatt_char_info *char_info = l->data;
2386 if (char_info == NULL)
2389 for (l1 = char_info->desc_data; l1 != NULL; l1 = l1->next) {
2390 struct gatt_desc_info *desc_info = l1->data;
2392 if (desc_info == NULL)
2395 ret = g_dbus_connection_unregister_object(g_conn,
2396 desc_info->desc_id);
2398 __bt_gatt_emit_interface_removed(
2399 desc_info->desc_path,
2400 GATT_DESC_INTERFACE);
2402 err = BLUETOOTH_ERROR_INTERNAL;
2405 /* list remove & free */
2406 char_info->desc_data = g_slist_remove(char_info->desc_data, desc_info);
2407 __bt_gatt_free_descriptor_info(desc_info);
2410 g_slist_free(char_info->desc_data);
2411 char_info->desc_data = NULL;
2413 ret = g_dbus_connection_unregister_object(g_conn,
2414 char_info->char_id);
2416 __bt_gatt_emit_interface_removed(char_info->char_path,
2417 GATT_CHAR_INTERFACE);
2419 err = BLUETOOTH_ERROR_INTERNAL;
2422 /* list remove & free */
2423 svc_info->char_data = g_slist_remove(svc_info->char_data, char_info);
2424 __bt_gatt_free_characteristic_info(char_info);
2427 g_slist_free(svc_info->char_data);
2428 svc_info->char_data = NULL;
2430 ret = g_dbus_connection_unregister_object(g_conn, svc_info->serv_id);
2432 __bt_gatt_emit_interface_removed(svc_info->serv_path,
2433 GATT_SERV_INTERFACE);
2435 err = BLUETOOTH_ERROR_INTERNAL;
2438 ret = g_dbus_connection_unregister_object(g_conn, svc_info->prop_id);
2440 BT_DBG("Unregistered the service on properties interface");
2443 /* list remove & free */
2444 gatt_services = g_slist_remove(gatt_services, svc_info);
2445 __bt_gatt_free_service_info(svc_info);
2447 new_service = FALSE;
2449 if (gatt_services == NULL) {
2451 } else if (gatt_services->next == NULL) {
2458 BT_EXPORT_API int bluetooth_gatt_send_response(int request_id, guint req_type,
2459 int resp_state, int offset, char *value, int value_length)
2461 struct gatt_req_info *req_info = NULL;
2463 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_GATT_SEND_RESPONSE)
2464 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
2465 BT_ERR("Don't have aprivilege to use this API");
2466 return BLUETOOTH_ERROR_PERMISSION_DEINED;
2469 req_info = __bt_gatt_find_request_info(request_id);
2472 if (resp_state != BLUETOOTH_ERROR_NONE) {
2473 g_dbus_method_invocation_return_dbus_error(req_info->context,
2474 "org.bluez.Error.Failed", "Application Error");
2476 gatt_requests = g_slist_remove(gatt_requests, req_info);
2478 req_info->context = NULL;
2479 if (req_info->attr_path)
2480 g_free(req_info->attr_path);
2481 if (req_info->svc_path)
2482 g_free(req_info->svc_path);
2485 return BLUETOOTH_ERROR_NONE;
2487 if (req_type == BLUETOOTH_GATT_ATT_REQUEST_TYPE_READ) {
2489 GVariantBuilder *inner_builder = NULL;
2490 inner_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
2491 if (value_length > 0 && value != NULL) {
2492 for (i = 0; i < value_length; i++)
2493 g_variant_builder_add(inner_builder, "y", value[i]);
2495 g_dbus_method_invocation_return_value(req_info->context,
2496 g_variant_new("(ay)", inner_builder));
2497 g_variant_builder_unref(inner_builder);
2499 g_dbus_method_invocation_return_value(req_info->context, NULL);
2501 gatt_requests = g_slist_remove(gatt_requests, req_info);
2503 req_info->context = NULL;
2504 if (req_info->attr_path)
2505 g_free(req_info->attr_path);
2506 if (req_info->svc_path)
2507 g_free(req_info->svc_path);
2510 return BLUETOOTH_ERROR_INTERNAL;
2513 return BLUETOOTH_ERROR_NONE;
2516 BT_EXPORT_API int bluetooth_gatt_server_set_notification(const char *char_path,
2517 bluetooth_device_address_t *unicast_address)
2519 GVariantBuilder *outer_builder;
2520 GVariantBuilder *invalidated_builder;
2521 GError *error = NULL;
2522 gboolean notify = TRUE;
2523 gboolean ret = TRUE;
2524 int err = BLUETOOTH_ERROR_NONE;
2525 gchar **line_argv = NULL;
2526 gchar *serv_path = NULL;
2527 char addr[20] = { 0 };
2529 line_argv = g_strsplit_set(char_path, "/", 0);
2530 serv_path = g_strdup_printf("/%s/%s/%s", line_argv[1], line_argv[2], line_argv[3]);
2532 if (!__bt_gatt_get_service_state(serv_path)) {
2533 BT_DBG("service not registered for this characteristic \n");
2535 g_strfreev(line_argv);
2536 return BLUETOOTH_ERROR_INTERNAL;
2541 outer_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
2542 invalidated_builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
2544 g_variant_builder_add(outer_builder, "{sv}", "Notifying",
2545 g_variant_new("b", notify));
2547 if (unicast_address) {
2548 _bt_convert_addr_type_to_string(addr,
2549 (unsigned char *)unicast_address->addr);
2551 g_variant_builder_add(outer_builder, "{sv}", "Unicast",
2552 g_variant_new("s", addr));
2554 BT_DBG("Set characteristic Notification \n");
2555 ret = g_dbus_connection_emit_signal(g_conn, NULL,
2557 "org.freedesktop.DBus.Properties",
2558 "PropertiesChanged",
2559 g_variant_new("(sa{sv}as)",
2560 "org.bluez.GattCharacteristic1",
2561 outer_builder, invalidated_builder),
2565 if (error != NULL) {
2566 BT_ERR("D-Bus API failure: errCode[%x], \
2568 error->code, error->message);
2569 g_clear_error(&error);
2571 err = BLUETOOTH_ERROR_INTERNAL;
2574 g_strfreev(line_argv);
2575 g_variant_builder_unref(outer_builder);
2576 g_variant_builder_unref(invalidated_builder);