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--;
332 struct gatt_service_info *serv_info = l1->data;
333 if (serv_info == NULL) {
334 BT_ERR("service info value is NULL");
335 g_dbus_method_invocation_return_value(invocation, NULL);
339 /* Prepare inner builder for GattService1 interface */
340 BT_DBG("Creating builder for service");
341 svc_builder = g_variant_builder_new(
342 G_VARIANT_TYPE("a{sa{sv}}"));
343 inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
345 g_variant_builder_add(inner_builder, "{sv}", "UUID",
346 g_variant_new_string(serv_info->service_uuid));
348 g_variant_builder_add(inner_builder, "{sv}", "Primary",
349 g_variant_new_boolean(serv_info->is_svc_primary));
352 inner_builder1 = g_variant_builder_new(G_VARIANT_TYPE("ao"));
353 BT_DBG("Adding Charatarisitcs list");
354 for (l4 = serv_info->char_data; l4 != NULL; l4 = l4->next) {
355 struct gatt_char_info *char_info = l4->data;
356 g_variant_builder_add(inner_builder1, "o",
357 char_info->char_path);
358 BT_DBG("%s", char_info->char_path);
361 svc_char = g_variant_new("ao", inner_builder1);
362 g_variant_builder_add(inner_builder, "{sv}", "Characteristics",
365 g_variant_builder_add(svc_builder, "{sa{sv}}",
369 g_variant_builder_add(builder, "{oa{sa{sv}}}",
370 serv_info->serv_path,
373 g_variant_builder_unref(inner_builder1);
375 /* Prepare inner builder for GattCharacteristic1 interface */
377 GSList *l2 = serv_info->char_data;
378 BT_DBG("Creating builder for characteristics \n");
381 BT_DBG("characteristic data is NULL");
383 for (l2 = serv_info->char_data; l2 != NULL; l2 = l2->next) {
385 GVariantBuilder *char_builder = NULL;
386 GVariantBuilder *inner_builder = NULL;
387 GVariantBuilder *builder1 = NULL;
388 GVariantBuilder *builder2 = NULL;
389 GVariantBuilder *builder3 = NULL;
390 GVariant *char_val = NULL;
391 GVariant *flags_val = NULL;
392 GVariant *char_desc = NULL;
393 char *unicast = NULL;
394 gboolean notify = FALSE;
397 char_builder = g_variant_builder_new(
400 inner_builder = g_variant_builder_new(
404 struct gatt_char_info *char_info = l2->data;
405 if (char_info == NULL) {
406 BT_ERR("char_info is NULL");
411 g_variant_builder_add(inner_builder, "{sv}", "UUID",
412 g_variant_new_string(char_info->char_uuid));
414 g_variant_builder_add(inner_builder, "{sv}", "Service",
415 g_variant_new("o", serv_info->serv_path));
417 builder1 = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
419 if (char_info->char_value != NULL) {
420 for (i = 0; i < char_info->value_length; i++) {
421 g_variant_builder_add(builder1, "y",
422 char_info->char_value[i]);
424 char_val = g_variant_new("ay", builder1);
425 g_variant_builder_add(inner_builder, "{sv}",
429 builder2 = g_variant_builder_new(G_VARIANT_TYPE("as"));
431 for (i = 0; i < char_info->flags_length; i++) {
432 g_variant_builder_add(builder2, "s",
433 char_info->char_flags[i]);
436 flags_val = g_variant_new("as", builder2);
437 g_variant_builder_add(inner_builder, "{sv}", "Flags",
441 g_variant_builder_add(inner_builder, "{sv}", "Notifying",
442 g_variant_new("b", notify));
445 unicast = g_strdup("00:00:00:00:00:00");
446 g_variant_builder_add(inner_builder, "{sv}", "Unicast",
447 g_variant_new("s", unicast));
450 builder3 = g_variant_builder_new(G_VARIANT_TYPE("ao"));
451 BT_DBG("Adding Descriptors list");
453 for (l4 = char_info->desc_data; l4 != NULL; l4 = l4->next) {
454 struct gatt_desc_info *desc_info = l4->data;
455 g_variant_builder_add(builder3, "o",
456 desc_info->desc_path);
457 BT_DBG("%s", desc_info->desc_path);
460 char_desc = g_variant_new("ao", builder3);
461 g_variant_builder_add(inner_builder, "{sv}", "Descriptors",
464 g_variant_builder_add(char_builder, "{sa{sv}}",
465 GATT_CHAR_INTERFACE , inner_builder);
466 g_variant_builder_add(builder, "{oa{sa{sv}}}",
467 char_info->char_path, char_builder);
469 /*Prepare inner builder for GattDescriptor1 interface*/
471 GSList *l3 = char_info->desc_data;
474 BT_DBG("descriptor data is NULL");
476 for (l3 = char_info->desc_data; l3 != NULL; l3 = l3->next) {
478 BT_DBG("Creating builder for descriptor \n");
480 GVariantBuilder *desc_builder = NULL;
481 GVariantBuilder *inner_builder = NULL;
482 GVariantBuilder *builder1 = NULL;
483 GVariantBuilder *builder2 = NULL;
484 GVariant *desc_val = NULL;
486 desc_builder = g_variant_builder_new(
489 inner_builder = g_variant_builder_new(
493 struct gatt_desc_info *desc_info = l3->data;
494 if (desc_info == NULL) {
495 BT_ERR("desc_info is NULL");
500 g_variant_builder_add(inner_builder,
502 g_variant_new_string(
503 desc_info->desc_uuid));
506 g_variant_builder_add(inner_builder, "{sv}",
509 char_info->char_path));
512 builder1 = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
514 if (desc_info->desc_value != NULL) {
515 for (i = 0; i < desc_info->value_length; i++) {
516 g_variant_builder_add(builder1, "y",
517 desc_info->desc_value[i]);
519 desc_val = g_variant_new("ay", builder1);
520 g_variant_builder_add(inner_builder, "{sv}",
525 builder2 = g_variant_builder_new(G_VARIANT_TYPE("as"));
527 for (i = 0; i < desc_info->flags_length; i++) {
528 g_variant_builder_add(builder2, "s",
529 desc_info->desc_flags[i]);
532 flags_val = g_variant_new("as", builder2);
533 g_variant_builder_add(inner_builder, "{sv}", "Flags",
536 g_variant_builder_add(desc_builder, "{sa{sv}}",
540 g_variant_builder_add(builder, "{oa{sa{sv}}}",
541 desc_info->desc_path,
544 /*unref descriptor builder pointers*/
545 g_variant_builder_unref(builder1);
546 g_variant_builder_unref(builder2);
547 g_variant_builder_unref(inner_builder);
548 g_variant_builder_unref(desc_builder);
553 /*unref char builder pointers*/
554 g_variant_builder_unref(builder1);
555 g_variant_builder_unref(builder2);
556 g_variant_builder_unref(builder3);
557 g_variant_builder_unref(inner_builder);
558 g_variant_builder_unref(char_builder);
561 /*unref service builder pointers*/
562 g_variant_builder_unref(inner_builder);
563 g_variant_builder_unref(svc_builder);
566 /* Return builder as method reply */
567 BT_DBG("Sending gatt service builder values to Bluez");
568 g_dbus_method_invocation_return_value(invocation,
575 static struct gatt_service_info *__bt_gatt_find_gatt_service_from_char(const char *char_path)
579 for (l1 = gatt_services; l1 != NULL; l1 = l1->next) {
580 struct gatt_service_info *serv_info = l1->data;
582 for (l2 = serv_info->char_data; l2 != NULL; l2 = l2->next) {
583 struct gatt_char_info *char_info = l2->data;
585 if (g_strcmp0(char_info->char_path, char_path)
590 BT_ERR("Gatt service not found");
594 static struct gatt_service_info *__bt_gatt_find_gatt_service_from_desc(const char *desc_path)
596 GSList *l1, *l2, *l3;
598 for (l1 = gatt_services; l1 != NULL; l1 = l1->next) {
599 struct gatt_service_info *serv_info = l1->data;
601 for (l2 = serv_info->char_data; l2 != NULL; l2 = l2->next) {
602 struct gatt_char_info *char_info = l2->data;
604 for (l3 = char_info->desc_data; l3 != NULL; l3 = l3->next) {
605 struct gatt_desc_info *desc_info = l3->data;
607 if (g_strcmp0(desc_info->desc_path, desc_path)
613 BT_ERR("Gatt service not found");
617 static void __bt_gatt_char_method_call(GDBusConnection *connection,
619 const gchar *object_path,
620 const gchar *interface_name,
621 const gchar *method_name,
622 GVariant *parameters,
623 GDBusMethodInvocation *invocation,
627 if (g_strcmp0(method_name, "ReadValue") == 0) {
631 bt_gatt_read_req_t read_req = {0, };
632 bt_user_info_t *user_info = NULL;
633 struct gatt_req_info *req_info = NULL;
634 struct gatt_service_info *svc_info = NULL;
638 g_variant_get(parameters, "(&syq)", &addr, &req_id, &offset);
640 BT_DBG("Application path = %s", object_path);
642 BT_DBG("Remote Device address number = %s", addr);
643 BT_DBG("Request id = %d, Offset = %d", req_id, offset);
645 BT_DBG("Sender = %s", sender);
647 read_req.att_handle = g_strdup(object_path);
648 read_req.address = g_strdup(addr);
649 read_req.req_id = req_id;
650 read_req.offset = offset;
651 svc_info = __bt_gatt_find_gatt_service_from_char(object_path);
652 if (svc_info != NULL) {
653 read_req.service_handle = g_strdup(svc_info->serv_path);
654 user_info = _bt_get_user_data(BT_COMMON);
656 GVariant *param = NULL;
659 /* Store requets information */
660 req_info = g_new0(struct gatt_req_info, 1);
661 req_info->attr_path = g_strdup(object_path);
662 req_info->svc_path = g_strdup(read_req.service_handle);
663 req_info->request_id = req_id;
664 req_info->offset = offset;
665 req_info->context = invocation;
666 gatt_requests = g_slist_append(gatt_requests, req_info);
668 if (user_info != NULL) {
670 BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED,
671 BLUETOOTH_ERROR_NONE, &read_req,
672 user_info->cb, user_info->user_data);
675 param = g_variant_new("(sssyq)",
677 read_req.service_handle,
681 __bt_send_event_to_hps(BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED, param);
685 if (read_req.att_handle)
686 g_free(read_req.att_handle);
687 if (read_req.address)
688 g_free(read_req.address);
689 if (read_req.service_handle)
690 g_free(read_req.service_handle);
692 } else if (g_strcmp0(method_name, "WriteValue") == 0) {
693 GVariant *var = NULL;
697 bt_gatt_value_change_t value_change = {0, };
698 bt_user_info_t *user_info = NULL;
700 struct gatt_service_info *svc_info = NULL;
701 struct gatt_req_info *req_info = NULL;
703 GVariant *param = NULL;
706 BT_DBG("WriteValue");
707 BT_DBG("Application path = %s", object_path);
708 BT_DBG("Sender = %s", sender);
710 g_variant_get(parameters, "(&syq@ay)", &addr, &req_id, &offset, &var);
712 value_change.att_handle = g_strdup(object_path);
713 value_change.address = g_strdup(addr);
714 svc_info = __bt_gatt_find_gatt_service_from_char(object_path);
715 if (svc_info == NULL) {
716 g_variant_unref(var);
717 g_dbus_method_invocation_return_value(invocation, NULL);
721 value_change.service_handle = g_strdup(svc_info->serv_path);
722 value_change.offset = offset;
723 value_change.req_id = req_id;
725 len = g_variant_get_size(var);
729 value_change.att_value = (guint8 *)malloc(len);
730 if (!value_change.att_value) {
731 BT_ERR("att_value is NULL");
732 g_variant_unref(var);
733 g_dbus_method_invocation_return_value(invocation, NULL);
737 data = (char *)g_variant_get_data(var);
738 memcpy(value_change.att_value, data, len);
741 value_change.val_len = len;
743 /* Store requets information */
744 req_info = g_new0(struct gatt_req_info, 1);
745 req_info->attr_path = g_strdup(object_path);
746 req_info->svc_path = g_strdup(value_change.service_handle);
747 req_info->request_id = req_id;
748 req_info->offset = offset;
749 req_info->context = invocation;
750 gatt_requests = g_slist_append(gatt_requests, req_info);
752 user_info = _bt_get_user_data(BT_COMMON);
753 if (user_info != NULL) {
755 BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED,
756 BLUETOOTH_ERROR_NONE, &value_change,
757 user_info->cb, user_info->user_data);
762 svc_path = g_strdup(svc_info->serv_path);
763 param = g_variant_new("(sssyq@ay)",
770 __bt_send_event_to_hps(BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED, param);
775 g_variant_unref(var);
777 } else if (g_strcmp0(method_name, "StartNotify") == 0) {
778 bt_user_info_t *user_info = NULL;
779 bt_gatt_char_notify_change_t notify_change = {0, };
780 BT_DBG("StartNotify");
781 user_info = _bt_get_user_data(BT_COMMON);
782 if (user_info != NULL) {
783 struct gatt_service_info *svc_info = NULL;
784 svc_info = __bt_gatt_find_gatt_service_from_char(object_path);
786 notify_change.service_handle = g_strdup(svc_info->serv_path);
787 notify_change.att_handle = g_strdup(object_path);
788 notify_change.att_notify = TRUE;
790 BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_STATE_CHANGED,
791 BLUETOOTH_ERROR_NONE, ¬ify_change,
792 user_info->cb, user_info->user_data);
795 } else if (g_strcmp0(method_name, "StopNotify") == 0) {
796 bt_user_info_t *user_info = NULL;
797 bt_gatt_char_notify_change_t notify_change = {0, };
798 BT_DBG("StopNotify");
799 user_info = _bt_get_user_data(BT_COMMON);
800 if (user_info != NULL) {
801 struct gatt_service_info *svc_info = NULL;
802 svc_info = __bt_gatt_find_gatt_service_from_char(object_path);
804 notify_change.service_handle = g_strdup(svc_info->serv_path);
805 notify_change.att_handle = g_strdup(object_path);
806 notify_change.att_notify = FALSE;
808 BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_STATE_CHANGED,
809 BLUETOOTH_ERROR_NONE, ¬ify_change,
810 user_info->cb, user_info->user_data);
813 } else if (g_strcmp0(method_name, "IndicateConfirm") == 0) {
815 bt_gatt_indicate_confirm_t confirm = {0, };
816 bt_user_info_t *user_info = NULL;
817 gboolean complete = 0;
818 struct gatt_service_info *svc_info = NULL;
820 BT_DBG("IndicateConfirm");
821 BT_DBG("Application path = %s", object_path);
822 BT_DBG("Sender = %s", sender);
824 g_variant_get(parameters, "(&sb)", &addr, &complete);
826 BT_DBG("Remote Device address number = %s", addr);
827 confirm.att_handle = g_strdup(object_path);
828 confirm.address = g_strdup(addr);
829 confirm.complete = complete;
831 svc_info = __bt_gatt_find_gatt_service_from_char(object_path);
832 if (svc_info != NULL) {
833 confirm.service_handle = g_strdup(svc_info->serv_path);
834 user_info = _bt_get_user_data(BT_COMMON);
836 if (user_info != NULL) {
838 BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_COMPLETED,
839 BLUETOOTH_ERROR_NONE, &confirm,
840 user_info->cb, user_info->user_data);
844 g_dbus_method_invocation_return_value(invocation, NULL);
847 static void __bt_gatt_desc_method_call(GDBusConnection *connection,
849 const gchar *object_path,
850 const gchar *interface_name,
851 const gchar *method_name,
852 GVariant *parameters,
853 GDBusMethodInvocation *invocation,
856 if (g_strcmp0(method_name, "ReadValue") == 0) {
860 bt_gatt_read_req_t read_req = {0, };
861 bt_user_info_t *user_info = NULL;
862 struct gatt_req_info *req_info = NULL;
863 struct gatt_service_info *svc_info = NULL;
866 g_variant_get(parameters, "(&syq)", &addr, &req_id, &offset);
868 BT_DBG("Application path = %s", object_path);
870 BT_DBG("Remote Device address number = %s", addr);
871 BT_DBG("Request id = %d, Offset = %d", req_id, offset);
873 BT_DBG("Sender = %s", sender);
875 read_req.att_handle = g_strdup(object_path);
876 read_req.address = g_strdup(addr);
877 read_req.req_id = req_id;
878 read_req.offset = offset;
879 svc_info = __bt_gatt_find_gatt_service_from_desc(object_path);
880 if (svc_info != NULL) {
881 read_req.service_handle = g_strdup(svc_info->serv_path);
882 user_info = _bt_get_user_data(BT_COMMON);
884 /* Store requets information */
885 req_info = g_new0(struct gatt_req_info, 1);
886 req_info->attr_path = g_strdup(object_path);
887 req_info->svc_path = g_strdup(read_req.service_handle);
888 req_info->request_id = req_id;
889 req_info->offset = offset;
890 req_info->context = invocation;
891 gatt_requests = g_slist_append(gatt_requests, req_info);
893 if (user_info != NULL) {
895 BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED,
896 BLUETOOTH_ERROR_NONE, &read_req,
897 user_info->cb, user_info->user_data);
901 if (read_req.att_handle)
902 g_free(read_req.att_handle);
903 if (read_req.address)
904 g_free(read_req.address);
905 if (read_req.service_handle)
906 g_free(read_req.service_handle);
909 } else if (g_strcmp0(method_name, "WriteValue") == 0) {
910 GVariant *var = NULL;
914 bt_gatt_value_change_t value_change = {0, };
915 bt_user_info_t *user_info = NULL;
917 struct gatt_service_info *svc_info = NULL;
918 struct gatt_req_info *req_info = NULL;
920 BT_DBG("WriteValue");
921 BT_DBG("Application path = %s", object_path);
922 BT_DBG("Sender = %s", sender);
924 g_variant_get(parameters, "(&syq@ay)", &addr, &req_id, &offset, &var);
926 value_change.att_handle = g_strdup(object_path);
927 value_change.address = g_strdup(addr);
928 svc_info = __bt_gatt_find_gatt_service_from_desc(object_path);
929 if (svc_info == NULL) {
930 g_variant_unref(var);
931 g_dbus_method_invocation_return_value(invocation, NULL);
935 value_change.service_handle = g_strdup(svc_info->serv_path);
936 value_change.offset = offset;
937 value_change.req_id = req_id;
939 len = g_variant_get_size(var);
943 value_change.att_value = (guint8 *)malloc(len);
944 if (!value_change.att_value) {
945 BT_ERR("att_value is NULL");
946 g_variant_unref(var);
947 g_dbus_method_invocation_return_value(invocation, NULL);
950 data = (char *)g_variant_get_data(var);
951 memcpy(value_change.att_value, data, len);
953 g_variant_unref(var);
955 value_change.val_len = len;
957 /* Store requets information */
958 req_info = g_new0(struct gatt_req_info, 1);
959 req_info->attr_path = g_strdup(object_path);
960 req_info->svc_path = g_strdup(value_change.service_handle);
961 req_info->request_id = req_id;
962 req_info->offset = offset;
963 req_info->context = invocation;
964 gatt_requests = g_slist_append(gatt_requests, req_info);
966 user_info = _bt_get_user_data(BT_COMMON);
967 if (user_info != NULL) {
969 BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED,
970 BLUETOOTH_ERROR_NONE, &value_change,
971 user_info->cb, user_info->user_data);
975 g_dbus_method_invocation_return_value(invocation, NULL);
978 gboolean __bt_gatt_emit_interface_removed(gchar *object_path, gchar *interface)
981 GError *error = NULL;
982 GVariantBuilder *array_builder;
984 array_builder = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
985 g_variant_builder_init(array_builder, G_VARIANT_TYPE("as"));
986 g_variant_builder_add(array_builder, "s", interface);
988 ret = g_dbus_connection_emit_signal(g_conn, NULL, "/",
989 "org.freedesktop.Dbus.Objectmanager",
991 g_variant_new("(oas)",
992 object_path, array_builder),
997 /* dbus gives error cause */
998 BT_ERR("d-bus api failure: errcode[%x], message[%s]",
999 error->code, error->message);
1000 g_clear_error(&error);
1003 g_variant_builder_unref(array_builder);
1008 static const GDBusInterfaceVTable desc_interface_vtable = {
1009 __bt_gatt_desc_method_call,
1014 static const GDBusInterfaceVTable char_interface_vtable = {
1015 __bt_gatt_char_method_call,
1020 static const GDBusInterfaceVTable serv_interface_vtable = {
1026 static const GDBusInterfaceVTable manager_interface_vtable = {
1027 __bt_gatt_manager_method_call,
1032 static GDBusNodeInfo *__bt_gatt_create_method_node_info(
1033 const gchar *introspection_data)
1036 GDBusNodeInfo *node_info = NULL;
1038 if (introspection_data == NULL)
1042 BT_DBG("Create new node info");
1043 node_info = g_dbus_node_info_new_for_xml(introspection_data, &err);
1046 BT_ERR("Unable to create node: %s", err->message);
1047 g_clear_error(&err);
1054 static struct gatt_service_info *__bt_gatt_find_gatt_service_info(
1055 const char *service_path)
1059 for (l = gatt_services; l != NULL; l = l->next) {
1060 struct gatt_service_info *info = l->data;
1062 if (g_strcmp0(info->serv_path, service_path) == 0)
1065 BT_ERR("Gatt service not found");
1069 static struct gatt_char_info *__bt_gatt_find_gatt_char_info(
1070 const char *service_path, const char *char_path)
1074 for (l1 = gatt_services; l1 != NULL; l1 = l1->next) {
1075 struct gatt_service_info *serv_info = l1->data;
1077 if (g_strcmp0(serv_info->serv_path, service_path) == 0) {
1079 for (l2 = serv_info->char_data; l2 != NULL; l2 = l2->next) {
1080 struct gatt_char_info *char_info = l2->data;
1082 if (g_strcmp0(char_info->char_path, char_path)
1086 BT_ERR("Gatt characteristic not found");
1090 BT_ERR("Gatt service not found");
1094 static struct gatt_desc_info *__bt_gatt_find_gatt_desc_info(
1095 const char *serv_path, const char *char_path,
1096 const char *desc_path)
1098 GSList *l1, *l2, *l3;
1100 for (l1 = gatt_services; l1 != NULL; l1 = l1->next) {
1101 struct gatt_service_info *serv_info = l1->data;
1103 if (g_strcmp0(serv_info->serv_path, serv_path) == 0) {
1104 for (l2 = serv_info->char_data; l2 != NULL; l2 = l2->next) {
1105 struct gatt_char_info *char_info = l2->data;
1107 if (g_strcmp0(char_info->char_path, char_path)
1109 for (l3 = char_info->desc_data; l3 != NULL; l3 = l3->next) {
1110 struct gatt_desc_info *desc_info = l3->data;
1111 if (g_strcmp0(desc_info->desc_path,
1120 BT_ERR("Gatt descriptor not found");
1124 static struct gatt_req_info *__bt_gatt_find_request_info(guint request_id)
1128 for (l = gatt_requests; l != NULL; l = l->next) {
1129 struct gatt_req_info *req_info = l->data;
1131 if (req_info && req_info->request_id == request_id) {
1135 BT_ERR("Gatt Request not found");
1139 static GDBusProxy *__bt_gatt_gdbus_init_manager_proxy(const gchar *service,
1140 const gchar *path, const gchar *interface)
1146 g_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM,
1151 BT_ERR("Unable to connect to gdbus: %s", err->message);
1152 g_clear_error(&err);
1157 proxy = g_dbus_proxy_new_sync(g_conn,
1158 G_DBUS_PROXY_FLAGS_NONE, NULL,
1160 interface, NULL, &err);
1164 BT_ERR("Unable to create proxy: %s", err->message);
1165 g_clear_error(&err);
1169 manager_gproxy = proxy;
1174 static GDBusProxy *__bt_gatt_gdbus_get_manager_proxy(const gchar *service,
1175 const gchar *path, const gchar *interface)
1177 return (manager_gproxy) ? manager_gproxy :
1178 __bt_gatt_gdbus_init_manager_proxy(service,
1182 int bluetooth_gatt_convert_prop2string(
1183 bt_gatt_characteristic_property_t properties,
1184 char *char_properties[])
1188 if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_BROADCAST) {
1189 char_properties[flag_count] = g_strdup("broadcast");
1192 if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ) {
1193 char_properties[flag_count] = g_strdup("read");
1196 if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE_NO_RESPONSE) {
1197 char_properties[flag_count] = g_strdup("write-without-response");
1200 if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE) {
1201 char_properties[flag_count] = g_strdup("write");
1204 if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_NOTIFY) {
1205 char_properties[flag_count] = g_strdup("notify");
1208 if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_INDICATE) {
1209 char_properties[flag_count] = g_strdup("indicate");
1212 if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_SIGNED_WRITE) {
1213 char_properties[flag_count] = g_strdup("authenticated-signed-writes");
1216 if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_RELIABLE_WRITE) {
1217 char_properties[flag_count] = g_strdup("reliable-write");
1220 if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITABLE_AUXILIARIES) {
1221 char_properties[flag_count] = g_strdup("writable-auxiliaries");
1224 if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_READ) {
1225 char_properties[flag_count] = g_strdup("encrypt-read");
1228 if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_WRITE) {
1229 char_properties[flag_count] = g_strdup("encrypt-write");
1232 if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_READ) {
1233 char_properties[flag_count] = g_strdup("encrypt-authenticated-read");
1236 if (properties & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_WRITE) {
1237 char_properties[flag_count] = g_strdup("encrypt-authenticated-write");
1241 if (flag_count == 0) {
1242 char_properties[flag_count] = g_strdup("read");
1249 int bluetooth_gatt_convert_perm2string(
1250 bt_gatt_permission_t properties,
1251 char *char_properties[])
1255 if (properties & BLUETOOTH_GATT_PERMISSION_READ) {
1256 char_properties[flag_count] = g_strdup("read");
1259 if (properties & BLUETOOTH_GATT_PERMISSION_WRITE) {
1260 char_properties[flag_count] = g_strdup("write");
1263 if (properties & BLUETOOTH_GATT_PERMISSION_ENCRYPT_READ) {
1264 char_properties[flag_count] = g_strdup("encrypt-read");
1267 if (properties & BLUETOOTH_GATT_PERMISSION_ENCRYPT_WRITE) {
1268 char_properties[flag_count] = g_strdup("encrypt-write");
1271 if (properties & BLUETOOTH_GATT_PERMISSION_ENCRYPT_AUTHENTICATED_READ) {
1272 char_properties[flag_count] = g_strdup("encrypt-authenticated-read");
1275 if (properties & BLUETOOTH_GATT_PERMISSION_ENCRYPT_AUTHENTICATED_WRITE) {
1276 char_properties[flag_count] = g_strdup("encrypt-authenticated-write");
1280 if (flag_count == 0) {
1281 char_properties[flag_count] = g_strdup("read");
1288 static void __bt_gatt_set_service_state(const char *service_path,
1291 struct gatt_service_info *svc_info = NULL;
1292 svc_info = __bt_gatt_find_gatt_service_info(service_path);
1294 if (svc_info != NULL) {
1295 BT_DBG("Updating the gatt service register state %d", state);
1296 svc_info->is_svc_registered = state;
1300 BT_DBG("gatt service not found");
1303 static gboolean __bt_gatt_get_service_state(const char *service_path)
1305 struct gatt_service_info *svc_info = NULL;
1307 svc_info = __bt_gatt_find_gatt_service_info(service_path);
1309 if (svc_info != NULL) {
1310 BT_DBG("Return the state of the gatt service %d",
1311 svc_info->is_svc_registered);
1312 return svc_info->is_svc_registered;
1315 BT_DBG("gatt service info is NULL");
1319 void get_service_cb(GObject *object, GAsyncResult *res, gpointer user_data)
1321 GError *error = NULL;
1323 GVariantIter *iter = NULL;
1324 const gchar *key = NULL;
1325 GVariant *value = NULL;
1326 const gchar *service = NULL;
1327 const gchar *characteristic = NULL;
1328 const gchar *descriptor = NULL;
1332 result = g_dbus_proxy_call_finish(manager_gproxy, res, &error);
1334 if (result == NULL) {
1335 /* dBUS-RPC is failed */
1336 BT_ERR("Dbus-RPC is failed\n");
1338 if (error != NULL) {
1339 /* dBUS gives error cause */
1340 BT_ERR("D-Bus API failure: errCode[%x], message[%s]\n",
1341 error->code, error->message);
1342 g_clear_error(&error);
1345 char *char_cmp = NULL;
1346 g_variant_get(result, "(a{sv})", &iter);
1347 char_cmp = g_strdup_printf("Characteristic%d", n_char);
1349 while (g_variant_iter_loop(iter, "{sv}", &key, &value)) {
1350 if (g_strcmp0(key, "Service") == 0) {
1351 service = g_variant_get_string(value, NULL);
1352 BT_DBG("Service %s", service);
1353 } else if (g_strcmp0(key, char_cmp) == 0) {
1354 characteristic = g_variant_get_string(value, NULL);
1356 char_cmp = g_strdup_printf("Characteristic%d", ++n_char);
1357 BT_DBG("%s", characteristic);
1358 } else if (g_strcmp0(key, "Descriptor") == 0) {
1359 descriptor = g_variant_get_string(value, NULL);
1360 BT_DBG("Descriptor %s", descriptor);
1363 g_variant_iter_free(iter);
1365 /* TODO: Store the service informationa and
1366 * Send respponse to CAPI layer. */
1368 g_variant_unref(result);
1373 void register_application_cb(GObject *object, GAsyncResult *res, gpointer user_data)
1375 BT_INFO("RegisterApplication is completed");
1377 GError *error = NULL;
1380 register_pending_cnt = 0;
1382 result = g_dbus_proxy_call_finish(manager_gproxy, res, &error);
1384 if (result == NULL) {
1385 /* dBUS-RPC is failed */
1386 BT_ERR("Dbus-RPC is failed\n");
1388 if (error != NULL) {
1389 /* dBUS gives error cause */
1390 BT_ERR("D-Bus API failure: errCode[%x], message[%s]\n",
1391 error->code, error->message);
1392 g_clear_error(&error);
1397 void unregister_application_cb(GObject *object, GAsyncResult *res,
1400 BT_INFO("UnregisterApplication is completed");
1402 GError *error = NULL;
1405 result = g_dbus_proxy_call_finish(manager_gproxy, res, &error);
1407 if (result == NULL) {
1408 /* dBUS-RPC is failed */
1409 BT_ERR("Dbus-RPC is failed\n");
1411 if (error != NULL) {
1412 /* dBUS gives error cause */
1413 BT_ERR("D-Bus API failure: errCode[%x], message[%s]\n",
1414 error->code, error->message);
1415 g_clear_error(&error);
1420 static int __bt_gatt_unregister_service(const char *service_path)
1422 if (!__bt_gatt_get_service_state(service_path)) {
1423 BT_DBG("service not registered \n");
1424 return BLUETOOTH_ERROR_NOT_FOUND;
1427 return BLUETOOTH_ERROR_NONE;
1430 BT_EXPORT_API int bluetooth_gatt_unregister_application(void)
1432 GDBusProxy *proxy = NULL;
1434 if (is_server_started) {
1435 proxy = __bt_gatt_gdbus_get_manager_proxy("org.bluez",
1436 "/org/bluez/hci0", GATT_MNGR_INTERFACE);
1438 if (proxy == NULL || app_path == NULL)
1439 return BLUETOOTH_ERROR_INTERNAL;
1441 BT_INFO("UnregisterApplication");
1443 /* Async Call to Unregister Service */
1444 g_dbus_proxy_call(proxy,
1445 "UnregisterApplication",
1446 g_variant_new("(o)",
1448 G_DBUS_CALL_FLAGS_NONE, -1,
1450 (GAsyncReadyCallback) unregister_application_cb,
1453 is_server_started = false;
1454 return BLUETOOTH_ERROR_NONE;
1457 BT_INFO("GATT server not started");
1458 return BLUETOOTH_ERROR_NONE;
1461 static GDBusConnection *__bt_gatt_get_gdbus_connection(void)
1463 GDBusConnection *local_system_gconn = NULL;
1467 if (g_conn == NULL) {
1468 address = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
1469 if (address == NULL) {
1471 BT_ERR("Failed to get bus address: %s", err->message);
1472 g_clear_error(&err);
1477 g_conn = g_dbus_connection_new_for_address_sync(address,
1478 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
1479 G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
1480 NULL, /* GDBusAuthObserver */
1485 BT_ERR("Unable to connect to dbus: %s", err->message);
1486 g_clear_error(&err);
1490 } else if (g_dbus_connection_is_closed(g_conn)) {
1491 address = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
1492 if (address == NULL) {
1494 BT_ERR("Failed to get bus address: %s", err->message);
1495 g_clear_error(&err);
1500 local_system_gconn = g_dbus_connection_new_for_address_sync(address,
1501 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
1502 G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
1503 NULL, /* GDBusAuthObserver */
1507 if (!local_system_gconn) {
1508 BT_ERR("Unable to connect to dbus: %s", err->message);
1509 g_clear_error(&err);
1512 g_conn = local_system_gconn;
1518 BT_EXPORT_API int bluetooth_gatt_init(void)
1520 GDBusConnection *conn;
1521 GError *error = NULL;
1522 GDBusNodeInfo *node_info = NULL;
1524 if (app_path != NULL) {
1525 BT_ERR("app path already exists! initialized");
1526 return BLUETOOTH_ERROR_ALREADY_INITIALIZED;
1529 if (owner_id == 0) {
1530 owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
1531 BT_GATT_SERVICE_NAME,
1532 G_BUS_NAME_OWNER_FLAGS_NONE,
1533 NULL, NULL, NULL, NULL, NULL);
1536 BT_DBG("owner_id is [%d]", owner_id);
1537 app_path = g_strdup_printf("/com/%d", getpid());
1541 conn = __bt_gatt_get_gdbus_connection();
1543 BT_ERR("Unable to get connection");
1547 /* Register ObjectManager interface */
1548 node_info = __bt_gatt_create_method_node_info(
1549 manager_introspection_xml);
1551 if (node_info == NULL) {
1552 BT_ERR("failed to get node info");
1556 if (manager_id == 0) {
1557 BT_INFO("manager_id does not exists");
1559 manager_id = g_dbus_connection_register_object(g_conn, app_path,
1560 node_info->interfaces[0],
1561 &manager_interface_vtable,
1562 NULL, NULL, &error);
1565 if (manager_id == 0) {
1566 BT_ERR("failed to register: %s", error->message);
1567 g_error_free(error);
1571 return BLUETOOTH_ERROR_NONE;
1575 g_bus_unown_name(owner_id);
1582 __bt_gatt_close_gdbus_connection();
1584 return BLUETOOTH_ERROR_INTERNAL;
1587 BT_EXPORT_API int bluetooth_gatt_deinit()
1589 int ret = BLUETOOTH_ERROR_NONE;
1590 /* Unown gdbus bus */
1592 /* remove/unregister all services */
1593 BT_DBG("removing all registered gatt service\n");
1594 bluetooth_gatt_delete_services();
1596 /* unregister the exported interface for object manager */
1597 g_dbus_connection_unregister_object(g_conn,
1602 ret = bluetooth_gatt_unregister_application();
1603 if (ret != BLUETOOTH_ERROR_NONE)
1604 BT_ERR("Fail to unregister application\n");
1606 g_bus_unown_name(owner_id);
1612 BT_DBG("Gatt service deinitialized \n");
1614 g_slist_free(gatt_services);
1615 gatt_services = NULL;
1617 g_object_unref(manager_gproxy);
1618 manager_gproxy = NULL;
1620 __bt_gatt_close_gdbus_connection();
1625 __bt_gatt_close_gdbus_connection();
1627 return BLUETOOTH_ERROR_NOT_FOUND;
1630 BT_EXPORT_API int bluetooth_gatt_add_service(const char *svc_uuid,
1633 GError *error = NULL;
1635 GDBusNodeInfo *node_info;
1637 GVariantBuilder *builder = NULL;
1638 GVariantBuilder *builder1 = NULL;
1639 GVariantBuilder *inner_builder = NULL;
1640 gboolean svc_primary = TRUE;
1641 struct gatt_service_info *serv_info = NULL;
1643 node_info = __bt_gatt_create_method_node_info(
1644 service_introspection_xml);
1646 if (node_info == NULL)
1647 return BLUETOOTH_ERROR_INTERNAL;
1649 path = g_strdup_printf("%s"GATT_SERV_OBJECT_PATH"%d", app_path, serv_id++);
1650 BT_DBG("gatt service path is [%s]", path);
1652 object_id = g_dbus_connection_register_object(g_conn, path,
1653 node_info->interfaces[0],
1654 &serv_interface_vtable,
1655 NULL, NULL, &error);
1657 if (object_id == 0) {
1658 BT_ERR("failed to register: %s", error->message);
1659 g_error_free(error);
1662 return BLUETOOTH_ERROR_INTERNAL;
1665 /* Add object_id/gatt service information; it's required at the time of
1666 * service unregister and Getmanagedobjects
1668 serv_info = g_new0(struct gatt_service_info, 1);
1670 serv_info->serv_path = g_strdup(path);
1671 serv_info->serv_id = object_id;
1672 serv_info->service_uuid = g_strdup(svc_uuid);
1673 serv_info->is_svc_registered = FALSE;
1674 serv_info->is_svc_primary = svc_primary;
1676 gatt_services = g_slist_append(gatt_services, serv_info);
1678 /* emit interfacesadded signal here for service path */
1679 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
1680 inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1682 g_variant_builder_add(inner_builder, "{sv}",
1683 "UUID", g_variant_new_string(svc_uuid));
1685 g_variant_builder_add(inner_builder, "{sv}",
1686 "Primary", g_variant_new_boolean(svc_primary));
1688 builder1 = g_variant_builder_new(G_VARIANT_TYPE("ao"));
1690 g_variant_builder_add(inner_builder, "{sv}", "Characteristics",
1691 g_variant_new("ao", builder1));
1693 g_variant_builder_add(builder, "{sa{sv}}",
1694 GATT_SERV_INTERFACE, inner_builder);
1696 g_dbus_connection_emit_signal(g_conn, NULL, "/",
1697 "org.freedesktop.Dbus.ObjectManager",
1699 g_variant_new("(oa{sa{sv}})",
1705 *svc_path = g_strdup(path);
1708 g_variant_builder_unref(inner_builder);
1709 g_variant_builder_unref(builder);
1710 g_variant_builder_unref(builder1);
1712 return BLUETOOTH_ERROR_NONE;
1715 BT_EXPORT_API int bluetooth_gatt_add_new_characteristic(
1716 const char *svc_path, const char *char_uuid,
1717 bt_gatt_permission_t permissions,
1718 bt_gatt_characteristic_property_t properties,
1722 GError *error = NULL;
1724 GDBusNodeInfo *node_info;
1726 GVariantBuilder *builder = NULL;
1727 GVariantBuilder *inner_builder = NULL;
1728 struct gatt_service_info *serv_info = NULL;
1729 struct gatt_char_info *char_info = NULL;
1730 GVariantBuilder *builder2 = NULL;
1731 GVariantBuilder *builder3 = NULL;
1732 GVariant *flags_val = NULL;
1734 char *char_flags[NUMBER_OF_FLAGS];
1739 new_service = FALSE;
1742 BT_DBG("gatt svc_path path is [%s]", svc_path);
1743 serv_info = __bt_gatt_find_gatt_service_info(svc_path);
1744 if (serv_info == NULL)
1745 return BLUETOOTH_ERROR_INVALID_PARAM;
1747 node_info = __bt_gatt_create_method_node_info(
1748 characteristics_introspection_xml);
1750 if (node_info == NULL)
1751 return BLUETOOTH_ERROR_INTERNAL;
1753 path = g_strdup_printf("%s/characteristic%d", svc_path, char_id++);
1754 BT_DBG("gatt characteristic path is [%s]", path);
1756 object_id = g_dbus_connection_register_object(g_conn, path,
1757 node_info->interfaces[0],
1758 &char_interface_vtable,
1759 NULL, NULL, &error);
1761 if (object_id == 0) {
1762 BT_ERR("failed to register: %s", error->message);
1763 g_error_free(error);
1766 return BLUETOOTH_ERROR_INTERNAL;
1769 if (permissions & BLUETOOTH_GATT_PERMISSION_ENCRYPT_READ)
1770 properties |= BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_READ;
1771 if (permissions & BLUETOOTH_GATT_PERMISSION_ENCRYPT_AUTHENTICATED_READ)
1772 properties |= BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_READ;
1773 if (permissions & BLUETOOTH_GATT_PERMISSION_ENCRYPT_WRITE)
1774 properties |= BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_WRITE;
1775 if (permissions & BLUETOOTH_GATT_PERMISSION_ENCRYPT_AUTHENTICATED_WRITE)
1776 properties |= BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_WRITE;
1778 flag_count = bluetooth_gatt_convert_prop2string(properties, char_flags);
1780 char_info = g_new0(struct gatt_char_info, 1);
1782 char_info->char_path = g_strdup(path);
1783 char_info->char_id = object_id;
1784 char_info->char_uuid = g_strdup(char_uuid);
1786 for (i = 0; i < flag_count; i++) {
1787 char_info->char_flags[i] = char_flags[i];
1790 char_info->flags_length = flag_count;
1792 serv_info->char_data = g_slist_append(serv_info->char_data, char_info);
1794 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
1795 inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1797 g_variant_builder_add(inner_builder, "{sv}", "UUID",
1798 g_variant_new("s", char_uuid));
1799 g_variant_builder_add(inner_builder, "{sv}", "Service",
1800 g_variant_new("o", svc_path));
1802 builder2 = g_variant_builder_new(G_VARIANT_TYPE("as"));
1804 for (i = 0; i < flag_count; i++) {
1805 g_variant_builder_add(builder2, "s", char_flags[i]);
1808 flags_val = g_variant_new("as", builder2);
1809 g_variant_builder_add(inner_builder, "{sv}", "Flags",
1812 builder3 = g_variant_builder_new(G_VARIANT_TYPE("ao"));
1814 g_variant_builder_add(inner_builder, "{sv}", "Descriptors",
1815 g_variant_new("ao", builder3));
1817 g_variant_builder_add(builder, "{sa{sv}}",
1818 GATT_CHAR_INTERFACE,
1821 g_dbus_connection_emit_signal(g_conn, NULL, "/",
1822 "org.freedesktop.Dbus.ObjectManager",
1824 g_variant_new("(oa{sa{sv}})",
1828 *char_path = g_strdup(path);
1834 g_variant_builder_unref(inner_builder);
1835 g_variant_builder_unref(builder);
1836 g_variant_builder_unref(builder2);
1837 g_variant_builder_unref(builder3);
1839 return BLUETOOTH_ERROR_NONE;
1842 BT_EXPORT_API int bluetooth_gatt_set_characteristic_value(
1843 const char *characteristic, const char *char_value,
1846 gchar **line_argv = NULL;
1847 char *serv_path = NULL;
1848 struct gatt_char_info *char_info = NULL;
1849 GVariantBuilder *builder1 = NULL;
1850 GVariantBuilder *builder = NULL;
1851 GVariantBuilder *inner_builder = NULL;
1852 GVariant *char_val = NULL;
1853 GError *error = NULL;
1855 int res = BLUETOOTH_ERROR_NONE;
1857 line_argv = g_strsplit_set(characteristic, "/", 0);
1858 serv_path = g_strdup_printf("/%s/%s/%s", line_argv[1], line_argv[2], line_argv[3]);
1860 char_info = __bt_gatt_find_gatt_char_info(serv_path, characteristic);
1862 if (char_info == NULL) {
1863 /* Fix : RESOURCE_LEAK */
1864 res = BLUETOOTH_ERROR_INVALID_PARAM;
1868 char_info->value_length = value_length;
1870 char_info->char_value = (char *)malloc(value_length);
1871 /* Fix : NULL_RETURNS */
1872 if (char_info->char_value == NULL) {
1873 res = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1877 for (i = 0; i < value_length; i++)
1878 char_info->char_value[i] = char_value[i];
1880 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
1881 inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1883 builder1 = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
1885 for (i = 0; i < value_length; i++) {
1886 g_variant_builder_add(builder1, "y", char_value[i]);
1889 char_val = g_variant_new("ay", builder1);
1890 g_variant_builder_add(inner_builder, "{sv}", "Value", char_val);
1892 g_variant_builder_add(builder, "{sa{sv}}",
1893 GATT_CHAR_INTERFACE,
1896 g_dbus_connection_emit_signal(g_conn, NULL, "/",
1897 "org.freedesktop.Dbus.ObjectManager",
1899 g_variant_new("(oa{sa{sv}})",
1900 char_info->char_path, builder),
1903 g_variant_builder_unref(inner_builder);
1904 g_variant_builder_unref(builder);
1905 g_variant_builder_unref(builder1);
1907 g_strfreev(line_argv);
1913 BT_EXPORT_API int bluetooth_gatt_add_descriptor(
1914 const char *char_path, const char *desc_uuid,
1915 bt_gatt_permission_t permissions,
1918 static int desc_id = 1;
1919 GError *error = NULL;
1921 GDBusNodeInfo *node_info;
1923 GVariantBuilder *builder = NULL;
1924 GVariantBuilder *inner_builder = NULL;
1925 struct gatt_char_info *char_info = NULL;
1926 struct gatt_desc_info *desc_info = NULL;
1927 gchar **line_argv = NULL;
1929 GVariantBuilder *builder2 = NULL;
1930 GVariant *flags_val = NULL;
1932 char *desc_flags[NUMBER_OF_FLAGS];
1940 line_argv = g_strsplit_set(char_path, "/", 0);
1941 serv_path = g_strdup_printf("/%s/%s/%s", line_argv[1], line_argv[2], line_argv[3]);
1943 char_info = __bt_gatt_find_gatt_char_info(serv_path, char_path);
1944 if (char_info == NULL) {
1945 g_strfreev(line_argv);
1947 return BLUETOOTH_ERROR_INVALID_PARAM;
1950 node_info = __bt_gatt_create_method_node_info(
1951 descriptor_introspection_xml);
1953 if (node_info == NULL) {
1954 g_strfreev(line_argv);
1956 return BLUETOOTH_ERROR_INTERNAL;
1959 path = g_strdup_printf("%s/descriptor%d", char_path, desc_id++);
1960 BT_DBG("gatt descriptor path is [%s]", path);
1962 object_id = g_dbus_connection_register_object(g_conn, path,
1963 node_info->interfaces[0],
1964 &desc_interface_vtable,
1965 NULL, NULL, &error);
1967 if (object_id == 0) {
1968 BT_ERR("failed to register: %s", error->message);
1969 g_error_free(error);
1971 g_strfreev(line_argv);
1974 return BLUETOOTH_ERROR_INTERNAL;
1977 flag_count = bluetooth_gatt_convert_perm2string(permissions, desc_flags);
1979 desc_info = g_new0(struct gatt_desc_info, 1);
1981 desc_info->desc_path = g_strdup(path);
1982 desc_info->desc_id = object_id;
1983 desc_info->desc_uuid = g_strdup(desc_uuid);
1985 for (i = 0; i < flag_count; i++) {
1986 desc_info->desc_flags[i] = desc_flags[i];
1989 desc_info->flags_length = flag_count;
1991 char_info->desc_data = g_slist_append(char_info->desc_data, desc_info);
1993 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
1994 inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1996 g_variant_builder_add(inner_builder, "{sv}", "UUID",
1997 g_variant_new("s", desc_uuid));
1998 g_variant_builder_add(inner_builder, "{sv}", "Characteristic",
1999 g_variant_new("o", char_path));
2001 builder2 = g_variant_builder_new(G_VARIANT_TYPE("as"));
2003 for (i = 0; i < flag_count; i++) {
2004 g_variant_builder_add(builder2, "s", desc_flags[i]);
2007 flags_val = g_variant_new("as", builder2);
2008 g_variant_builder_add(inner_builder, "{sv}", "Flags",
2011 g_variant_builder_add(builder, "{sa{sv}}",
2012 GATT_DESC_INTERFACE,
2015 g_dbus_connection_emit_signal(g_conn, NULL, "/",
2016 "org.freedesktop.Dbus.ObjectManager",
2018 g_variant_new("(oa{sa{sv}})",
2022 *desc_path = g_strdup(path);
2026 g_strfreev(line_argv);
2027 g_variant_builder_unref(inner_builder);
2028 g_variant_builder_unref(builder);
2030 return BLUETOOTH_ERROR_NONE;
2033 BT_EXPORT_API int bluetooth_gatt_set_descriptor_value(
2034 const char *desc_path, const char *desc_value,
2037 GError *error = NULL;
2038 GVariantBuilder *builder = NULL;
2039 GVariantBuilder *inner_builder = NULL;
2040 GVariantBuilder *builder1 = NULL;
2041 struct gatt_desc_info *desc_info = NULL;
2042 gchar **line_argv = NULL;
2044 GVariant *desc_val = NULL;
2045 char *serv_path = NULL;
2048 line_argv = g_strsplit_set(desc_path, "/", 0);
2049 serv_path = g_strdup_printf("/%s/%s/%s", line_argv[1], line_argv[2], line_argv[3]);
2050 char_path = g_strdup_printf("%s/%s", serv_path, line_argv[4]);
2052 desc_info = __bt_gatt_find_gatt_desc_info(serv_path, char_path, desc_path);
2054 /* Free the allocated memory */
2055 g_strfreev(line_argv);
2059 /* Fix : NULL_RETURNS */
2060 retv_if(desc_info == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
2062 desc_info->desc_value = (char *)malloc(value_length);
2064 /* Fix : NULL_RETURNS */
2065 retv_if(desc_info->desc_value == NULL, BLUETOOTH_ERROR_MEMORY_ALLOCATION);
2067 for (i = 0; i < value_length; i++)
2068 desc_info->desc_value[i] = desc_value[i];
2070 desc_info->value_length = value_length;
2072 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
2073 inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
2075 builder1 = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
2077 for (i = 0; i < value_length; i++) {
2078 g_variant_builder_add(builder1, "y", desc_value[i]);
2080 desc_val = g_variant_new("ay", builder1);
2081 g_variant_builder_add(inner_builder, "{sv}", "Value", desc_val);
2083 g_variant_builder_add(builder, "{sa{sv}}",
2084 GATT_DESC_INTERFACE,
2087 g_dbus_connection_emit_signal(g_conn, NULL, "/",
2088 "org.freedesktop.Dbus.ObjectManager",
2090 g_variant_new("(oa{sa{sv}})",
2091 desc_info->desc_path, builder),
2094 g_variant_builder_unref(inner_builder);
2095 g_variant_builder_unref(builder);
2096 g_variant_builder_unref(builder1);
2098 return BLUETOOTH_ERROR_NONE;
2101 int bluetooth_gatt_get_service(const char *svc_uuid)
2103 GDBusProxy *proxy = NULL;
2106 proxy = __bt_gatt_gdbus_get_manager_proxy("org.bluez",
2107 "/org/bluez/hci0", GATT_MNGR_INTERFACE);
2109 return BLUETOOTH_ERROR_INTERNAL;
2111 uuid = g_strdup(svc_uuid);
2113 g_dbus_proxy_call(proxy,
2115 g_variant_new("(s)",
2117 G_DBUS_CALL_FLAGS_NONE, -1,
2119 (GAsyncReadyCallback) get_service_cb,
2124 return BLUETOOTH_ERROR_NONE;
2127 BT_EXPORT_API int bluetooth_gatt_register_service(
2128 const char *svc_path)
2130 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_GATT_REGISTER_SERVICE)
2131 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
2132 BT_ERR("Don't have aprivilege to use this API");
2133 return BLUETOOTH_ERROR_PERMISSION_DEINED;
2136 register_pending_cnt++;
2138 if (__bt_gatt_get_service_state(svc_path)) {
2139 BT_DBG("service already registered \n");
2140 return BLUETOOTH_ERROR_NONE;
2143 __bt_gatt_set_service_state(svc_path, TRUE);
2145 return BLUETOOTH_ERROR_NONE;
2148 BT_EXPORT_API int bluetooth_gatt_register_application(void)
2150 GDBusProxy *proxy = NULL;
2152 if (!is_server_started) {
2154 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_GATT_REGISTER_APPLICATION)
2155 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
2156 BT_ERR("Don't have aprivilege to use this API");
2157 return BLUETOOTH_ERROR_PERMISSION_DEINED;
2160 proxy = __bt_gatt_gdbus_get_manager_proxy("org.bluez",
2161 "/org/bluez/hci0", GATT_MNGR_INTERFACE);
2162 if (proxy == NULL || app_path == NULL)
2163 return BLUETOOTH_ERROR_INTERNAL;
2165 BT_INFO("RegisterApplication");
2167 g_dbus_proxy_call(proxy,
2168 "RegisterApplication",
2169 g_variant_new("(oa{sv})",
2171 G_DBUS_CALL_FLAGS_NONE, -1,
2173 (GAsyncReadyCallback) register_application_cb,
2176 is_server_started = true;
2178 return BLUETOOTH_ERROR_NONE;
2181 BT_INFO("Already RegisterApplication");
2183 return BLUETOOTH_ERROR_NONE;
2186 BT_EXPORT_API int bluetooth_gatt_delete_services(void)
2189 int error = BLUETOOTH_ERROR_NONE;
2193 for (l = gatt_services; l != NULL; l = l->next) {
2194 struct gatt_service_info *info = l->data;
2195 BT_DBG("svc_path is %s", info->serv_path);
2196 if (bluetooth_gatt_unregister_service(info->serv_path)
2197 != BLUETOOTH_ERROR_NONE) {
2198 error = BLUETOOTH_ERROR_INTERNAL;
2199 BT_DBG(" Error in removing service %s \n",
2203 BT_DBG(" All services removed successfully.\n ");
2205 BT_DBG(" There are no registered services.\n ");
2208 g_slist_free(gatt_services);
2209 gatt_services = NULL;
2212 if (error != BLUETOOTH_ERROR_NONE)
2215 return BLUETOOTH_ERROR_NONE;
2218 BT_EXPORT_API int bluetooth_gatt_update_characteristic(
2219 const char *char_path, const char* char_value,
2222 GVariantBuilder *outer_builder;
2223 GVariantBuilder *inner_builder;
2224 GVariantBuilder *invalidated_builder;
2225 GVariant *update_value = NULL;
2226 GError *error = NULL;
2227 gboolean ret = FALSE;
2228 int err = BLUETOOTH_ERROR_NONE;
2230 gchar **line_argv = NULL;
2231 gchar *serv_path = NULL;
2233 line_argv = g_strsplit_set(char_path, "/", 0);
2234 serv_path = g_strdup_printf("/%s/%s/%s", line_argv[1], line_argv[2], line_argv[3]);
2236 if (!__bt_gatt_get_service_state(serv_path)) {
2237 BT_DBG("service not registered for this characteristic \n");
2239 g_strfreev(line_argv);
2240 return BLUETOOTH_ERROR_INTERNAL;
2243 outer_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
2244 invalidated_builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
2246 inner_builder = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
2247 for (i = 0; i < value_length; i++) {
2248 g_variant_builder_add(inner_builder, "y", char_value[i]);
2251 update_value = g_variant_new("ay", inner_builder);
2253 outer_builder = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
2254 g_variant_builder_add(outer_builder, "{sv}", "Value",
2257 BT_DBG("Updating characteristic value \n");
2258 ret = g_dbus_connection_emit_signal(g_conn, NULL,
2260 "org.freedesktop.DBus.Properties",
2261 "PropertiesChanged",
2262 g_variant_new("(sa{sv}as)",
2263 "org.bluez.GattCharacteristic1",
2264 outer_builder, invalidated_builder),
2268 if (error != NULL) {
2269 BT_ERR("D-Bus API failure: errCode[%x], \
2271 error->code, error->message);
2272 g_clear_error(&error);
2274 err = BLUETOOTH_ERROR_INTERNAL;
2276 struct gatt_char_info *char_info = NULL;
2278 char_info = __bt_gatt_find_gatt_char_info(serv_path, char_path);
2279 if (char_info == NULL) {
2281 g_strfreev(line_argv);
2282 g_variant_builder_unref(inner_builder);
2283 g_variant_builder_unref(outer_builder);
2284 g_variant_builder_unref(invalidated_builder);
2286 return BLUETOOTH_ERROR_INVALID_DATA;
2289 char_info->value_length = value_length;
2291 char_info->char_value = (char *)realloc(char_info->char_value, value_length);
2292 if (char_info->char_value) {
2293 for (i = 0; i < value_length; i++) {
2294 char_info->char_value[i] = char_value[i];
2300 g_strfreev(line_argv);
2301 g_variant_builder_unref(inner_builder);
2302 g_variant_builder_unref(outer_builder);
2303 g_variant_builder_unref(invalidated_builder);
2308 static void __bt_gatt_free_descriptor_info(struct gatt_desc_info *desc_info)
2315 g_free(desc_info->desc_path);
2316 g_free(desc_info->desc_uuid);
2317 g_free(desc_info->desc_value);
2319 for (i = 0; i < desc_info->flags_length; i++)
2320 g_free(desc_info->desc_flags[i]);
2325 static void __bt_gatt_free_characteristic_info(struct gatt_char_info *char_info)
2332 g_free(char_info->char_path);
2333 g_free(char_info->char_uuid);
2334 g_free(char_info->char_value);
2336 for (i = 0; i < char_info->flags_length; i++)
2337 g_free(char_info->char_flags[i]);
2342 static void __bt_gatt_free_service_info(struct gatt_service_info *svc_info)
2347 g_free(svc_info->serv_path);
2348 g_free(svc_info->service_uuid);
2352 BT_EXPORT_API int bluetooth_gatt_unregister_service(const char *svc_path)
2355 struct gatt_service_info *svc_info;
2357 int err = BLUETOOTH_ERROR_NONE;
2359 BT_DBG("svc_path %s", svc_path);
2360 svc_info = __bt_gatt_find_gatt_service_info(svc_path);
2363 BT_ERR("Unable to find service info");
2364 return BLUETOOTH_ERROR_NOT_FOUND;
2367 err = __bt_gatt_unregister_service(svc_path);
2368 if (err != BLUETOOTH_ERROR_NONE) {
2369 BT_DBG("Could not unregister application");
2373 for (l = svc_info->char_data; l != NULL; l = l->next) {
2374 struct gatt_char_info *char_info = l->data;
2376 if (char_info == NULL)
2379 for (l1 = char_info->desc_data; l1 != NULL; l1 = l1->next) {
2380 struct gatt_desc_info *desc_info = l1->data;
2382 if (desc_info == NULL)
2385 ret = g_dbus_connection_unregister_object(g_conn,
2386 desc_info->desc_id);
2388 __bt_gatt_emit_interface_removed(
2389 desc_info->desc_path,
2390 GATT_DESC_INTERFACE);
2392 err = BLUETOOTH_ERROR_INTERNAL;
2395 /* list remove & free */
2396 char_info->desc_data = g_slist_remove(char_info->desc_data, desc_info);
2397 __bt_gatt_free_descriptor_info(desc_info);
2400 g_slist_free(char_info->desc_data);
2401 char_info->desc_data = NULL;
2403 ret = g_dbus_connection_unregister_object(g_conn,
2404 char_info->char_id);
2406 __bt_gatt_emit_interface_removed(char_info->char_path,
2407 GATT_CHAR_INTERFACE);
2409 err = BLUETOOTH_ERROR_INTERNAL;
2412 /* list remove & free */
2413 svc_info->char_data = g_slist_remove(svc_info->char_data, char_info);
2414 __bt_gatt_free_characteristic_info(char_info);
2417 g_slist_free(svc_info->char_data);
2418 svc_info->char_data = NULL;
2420 ret = g_dbus_connection_unregister_object(g_conn, svc_info->serv_id);
2422 __bt_gatt_emit_interface_removed(svc_info->serv_path,
2423 GATT_SERV_INTERFACE);
2425 err = BLUETOOTH_ERROR_INTERNAL;
2428 ret = g_dbus_connection_unregister_object(g_conn, svc_info->prop_id);
2430 BT_DBG("Unregistered the service on properties interface");
2433 /* list remove & free */
2434 gatt_services = g_slist_remove(gatt_services, svc_info);
2435 __bt_gatt_free_service_info(svc_info);
2437 new_service = FALSE;
2439 if (gatt_services == NULL) {
2441 } else if (gatt_services->next == NULL) {
2448 BT_EXPORT_API int bluetooth_gatt_send_response(int request_id, guint req_type,
2449 int resp_state, int offset, char *value, int value_length)
2451 struct gatt_req_info *req_info = NULL;
2453 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_GATT_SEND_RESPONSE)
2454 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
2455 BT_ERR("Don't have aprivilege to use this API");
2456 return BLUETOOTH_ERROR_PERMISSION_DEINED;
2459 req_info = __bt_gatt_find_request_info(request_id);
2462 if (resp_state != BLUETOOTH_ERROR_NONE) {
2463 g_dbus_method_invocation_return_dbus_error(req_info->context,
2464 "org.bluez.Error.Failed", "Application Error");
2466 gatt_requests = g_slist_remove(gatt_requests, req_info);
2468 req_info->context = NULL;
2469 if (req_info->attr_path)
2470 g_free(req_info->attr_path);
2471 if (req_info->svc_path)
2472 g_free(req_info->svc_path);
2475 return BLUETOOTH_ERROR_NONE;
2477 if (req_type == BLUETOOTH_GATT_ATT_REQUEST_TYPE_READ) {
2479 GVariantBuilder *inner_builder = NULL;
2480 inner_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
2481 if (value_length > 0 && value != NULL) {
2482 for (i = 0; i < value_length; i++)
2483 g_variant_builder_add(inner_builder, "y", value[i]);
2485 g_dbus_method_invocation_return_value(req_info->context,
2486 g_variant_new("(ay)", inner_builder));
2487 g_variant_builder_unref(inner_builder);
2489 g_dbus_method_invocation_return_value(req_info->context, NULL);
2491 gatt_requests = g_slist_remove(gatt_requests, req_info);
2493 req_info->context = NULL;
2494 if (req_info->attr_path)
2495 g_free(req_info->attr_path);
2496 if (req_info->svc_path)
2497 g_free(req_info->svc_path);
2500 return BLUETOOTH_ERROR_INTERNAL;
2503 return BLUETOOTH_ERROR_NONE;
2506 BT_EXPORT_API int bluetooth_gatt_server_set_notification(const char *char_path,
2507 bluetooth_device_address_t *unicast_address)
2509 GVariantBuilder *outer_builder;
2510 GVariantBuilder *invalidated_builder;
2511 GError *error = NULL;
2512 gboolean notify = TRUE;
2513 gboolean ret = TRUE;
2514 int err = BLUETOOTH_ERROR_NONE;
2515 gchar **line_argv = NULL;
2516 gchar *serv_path = NULL;
2517 char addr[20] = { 0 };
2519 line_argv = g_strsplit_set(char_path, "/", 0);
2520 serv_path = g_strdup_printf("/%s/%s/%s", line_argv[1], line_argv[2], line_argv[3]);
2522 if (!__bt_gatt_get_service_state(serv_path)) {
2523 BT_DBG("service not registered for this characteristic \n");
2524 g_strfreev(line_argv);
2525 return BLUETOOTH_ERROR_INTERNAL;
2528 outer_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
2529 invalidated_builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
2531 g_variant_builder_add(outer_builder, "{sv}", "Notifying",
2532 g_variant_new("b", notify));
2534 if (unicast_address) {
2535 _bt_convert_addr_type_to_string(addr,
2536 (unsigned char *)unicast_address->addr);
2538 g_variant_builder_add(outer_builder, "{sv}", "Unicast",
2539 g_variant_new("s", addr));
2541 BT_DBG("Set characteristic Notification \n");
2542 ret = g_dbus_connection_emit_signal(g_conn, NULL,
2544 "org.freedesktop.DBus.Properties",
2545 "PropertiesChanged",
2546 g_variant_new("(sa{sv}as)",
2547 "org.bluez.GattCharacteristic1",
2548 outer_builder, invalidated_builder),
2552 if (error != NULL) {
2553 BT_ERR("D-Bus API failure: errCode[%x], \
2555 error->code, error->message);
2556 g_clear_error(&error);
2558 err = BLUETOOTH_ERROR_INTERNAL;
2561 g_strfreev(line_argv);
2562 g_variant_builder_unref(outer_builder);
2563 g_variant_builder_unref(invalidated_builder);