4 * Copyright (c) 2015 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Anupam Roy <anupam.r@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
22 #include <hardware/bluetooth.h>
23 #include <hardware/bt_gatt.h>
38 #include "bt-hal-log.h"
39 #include "bt-hal-msg.h"
40 #include "bt-hal-utils.h"
41 #include "bt-hal-dbus-common-utils.h"
43 #include "bt-hal-adapter-le.h"
44 #include "bt-hal-event-receiver.h"
46 #define NUMBER_OF_FLAGS 10
48 #define GATT_SERV_INTERFACE "org.bluez.GattService1"
49 #define GATT_CHAR_INTERFACE "org.bluez.GattCharacteristic1"
50 #define GATT_DESC_INTERFACE "org.bluez.GattDescriptor1"
52 #define BT_GATT_SERVICE_NAME "org.frwk.gatt_service"
53 #define GATT_SERV_OBJECT_PATH "/service"
55 static GDBusProxy *manager_gproxy = NULL;
57 /************************************************************************************
59 ************************************************************************************/
60 extern const btgatt_callbacks_t *bt_gatt_callbacks;
62 GDBusConnection *g_conn = NULL;
63 //GDBusConnection *conn = NULL;
64 GDBusNodeInfo *manager_node_info = NULL;
67 /* Global handles which needs to be incremented during each addition */
68 static int gatt_service_handle = 10;
69 static int gatt_char_handle = 20;
70 static int gatt_desc_handle = 30;
72 struct gatt_service_info {
79 gboolean is_svc_registered;
80 gboolean is_svc_primary;
84 struct gatt_char_info {
89 gchar *char_flags[NUMBER_OF_FLAGS];
96 struct gatt_desc_info {
101 gchar *desc_flags[NUMBER_OF_FLAGS];
108 * GATT Server Request type
111 BT_HAL_GATT_REQUEST_TYPE_READ = 0x00, /* Read Requested */
112 BT_HAL_GATT_REQUEST_TYPE_WRITE = 0x01, /* Write Requested */
113 BT_HAL_GATT_REQUEST_TYPE_EXEC_WRITE = 0x02, /* Exec Write Requested */
114 } bt_gatt_request_type_e;
116 struct gatt_req_info {
121 bt_gatt_request_type_e request_type; /* Read or Write request */
122 GDBusMethodInvocation *context;
125 struct gatt_server_app {
131 /* Linked List of gatt server app's */
132 static GSList *gatt_server_apps = NULL;
134 static GSList *gatt_services = NULL;
136 static int conn_id = 0;
138 /* Linked List of connected Remote GATT clients */
139 static GSList *gatt_client_info_list = NULL;
141 /* GATT Client Info List Structure */
142 struct gatt_client_info_t {
143 int connection_id; /* This value will uniquely identify a GATT client-server connection */
144 int instance_id; /* This value unique identifies a GATT server instance */
145 char *addr; /* Remote GATT client address */
146 GSList *gatt_req_info_list; /* List of transactions per Connection*/
149 static handle_stack_msg event_cb = NULL;
152 uint32_t instance_data;
154 } hal_register_server_data;
157 uint32_t instance_data;
161 } hal_gatt_service_added;
164 uint32_t instance_data;
166 } hal_gatt_service_started;
169 uint32_t instance_data;
171 } hal_gatt_service_deleted;
174 uint32_t instance_data;
178 } hal_gatt_char_added;
181 uint32_t instance_data;
185 } hal_gatt_desc_added;
187 #define CHECK_BTGATT_INIT() if (bt_gatt_callbacks == NULL)\
189 ERR("%s: BTGATT not initialized", __FUNCTION__);\
190 return BT_STATUS_NOT_READY;\
192 DBG("%s", __FUNCTION__);\
195 static gboolean __bt_hal_gatt_service_added_cb(gpointer user_data);
197 static void __bt_hal_gatt_deinit(char *app_path);
199 static void __bt_hal_register_application_cb(GObject *object,
200 GAsyncResult *res, gpointer user_data);
202 static void __bt_hal_unregister_application_cb(GObject *object, GAsyncResult *res,
205 static GDBusProxy *__bt_gatt_gdbus_get_gatt_manager_proxy(const gchar *service,
206 const gchar *path, const gchar *interface);
208 /* Introspection data for the service we are exporting */
209 static const gchar service_introspection_xml[] =
211 " <interface name='org.freedesktop.DBus.Properties'>"
212 " <property type='s' name='UUID' access='read'>"
214 " <property type='b' name='primary' access='read'>"
216 " <property type='o' name='Device' access='read'>"
218 " <property type='ao' name='Characteristics' access='read'>"
220 " <property type='s' name='Includes' access='read'>"
225 /* Introspection data for the characteristics we are exporting */
226 static const gchar characteristics_introspection_xml[] =
228 " <interface name='org.bluez.GattCharacteristic1'>"
229 " <method name='ReadValue'>"
230 " <arg type='s' name='address' direction='in'/>"
231 " <arg type='y' name='id' direction='in'/>"
232 " <arg type='q' name='offset' direction='in'/>"
233 " <arg type='ay' name='Value' direction='out'/>"
235 " <method name='WriteValue'>"
236 " <arg type='s' name='address' direction='in'/>"
237 " <arg type='y' name='id' direction='in'/>"
238 " <arg type='q' name='offset' direction='in'/>"
239 " <arg type='ay' name='value' direction='in'/>"
241 " <method name='StartNotify'>"
243 " <method name='StopNotify'>"
245 " <method name='IndicateConfirm'>"
246 " <arg type='s' name='address' direction='in'/>"
247 " <arg type='b' name='complete' direction='in'/>"
250 " <interface name='org.freedesktop.DBus.Properties'>"
251 " <property type='s' name='UUID' access='read'>"
253 " <property type='o' name='Service' access='read'>"
255 " <property type='ay' name='Value' access='readwrite'>"
257 " <property type='b' name='Notifying' access='read'>"
259 " <property type='as' name='Flags' access='read'>"
261 " <property type='s' name='Unicast' access='read'>"
263 " <property type='ao' name='Descriptors' access='read'>"
268 /* Introspection data for the descriptor we are exporting */
269 static const gchar descriptor_introspection_xml[] =
271 " <interface name='org.bluez.GattDescriptor1'>"
272 " <method name='ReadValue'>"
273 " <arg type='s' name='address' direction='in'/>"
274 " <arg type='u' name='id' direction='in'/>"
275 " <arg type='q' name='offset' direction='in'/>"
276 " <arg type='ay' name='Value' direction='out'/>"
278 " <method name='WriteValue'>"
279 " <arg type='s' name='address' direction='in'/>"
280 " <arg type='u' name='id' direction='in'/>"
281 " <arg type='q' name='offset' direction='in'/>"
282 " <arg type='b' name='response_needed' direction='in'/>"
283 " <arg type='ay' name='value' direction='in'/>"
286 " <interface name='org.freedesktop.DBus.Properties'>"
287 " <property type='s' name='UUID' access='read'>"
289 " <property type='o' name='Characteristic' access='read'>"
291 " <property type='ay' name='Value' access='read'>"
293 " <property type='as' name='Flags' access='read'>"
299 static const gchar manager_introspection_xml[] =
301 " <interface name='org.freedesktop.DBus.ObjectManager'>"
302 " <method name='GetManagedObjects'>"
303 " <arg type='a{oa{sa{sv}}}' name='object_paths_interfaces_and_properties' direction='out'/>"
308 GSList *_bt_get_service_list_from_server(int instance)
311 INFO("Number of GATT Server apps [%d]", g_slist_length(gatt_server_apps));
312 INFO("Find App with slot [%d]", instance);
314 for (l = gatt_server_apps; l; l = g_slist_next(l)) {
315 struct gatt_server_app *app = (struct gatt_server_app *)l->data;
317 if (app->slot == instance) {
318 INFO("App slot [%d] Found, Number of services registered [%d]",
319 app->slot, g_slist_length(app->services));
320 return app->services;
326 void _bt_remote_service_from_gatt_server(int instance, int service_handle)
331 for (l = gatt_server_apps; l; l = g_slist_next(l)) {
332 struct gatt_server_app *app = (struct gatt_server_app *)l->data;
334 if (app->slot == instance) {
335 for (l1 = app->services; l1; l1 = g_slist_next(l1)) {
336 struct gatt_service_info *srv = (struct gatt_service_info *)l1->data;
337 if (srv->service_handle == service_handle) {
338 app->services = g_slist_remove(app->services, srv);
346 void _bt_hal_update_gatt_service_in_gatt_server(int slot, struct gatt_service_info *serv_info)
349 for (l = gatt_server_apps; l; l = g_slist_next(l)) {
350 struct gatt_server_app *app = (struct gatt_server_app *)l->data;
352 if (app->slot == slot) {
353 INFO("Updating service in GATT server's service list service handle [%d] slot [%d]",
354 serv_info->service_handle, slot);
355 app->services = g_slist_append(app->services, serv_info);
360 static struct gatt_client_info_t *__bt_find_remote_gatt_client_info(char *address)
363 struct gatt_client_info_t *info = NULL;
365 for (l = gatt_client_info_list; l != NULL; l = g_slist_next(l)) {
366 info = (struct gatt_client_info_t*)l->data;
370 if (!g_strcmp0(info->addr, address)) {
371 INFO("Remote GATT client found addr[%s]", info->addr);
378 static struct gatt_client_info_t *__bt_find_remote_gatt_client_info_from_conn(int conn_id)
381 struct gatt_client_info_t *info = NULL;
383 for (l = gatt_client_info_list; l != NULL; l = g_slist_next(l)) {
384 info = (struct gatt_client_info_t*)l->data;
388 if (info->connection_id == conn_id) {
389 INFO("Remote GATT client found addr[%s]", info->addr);
396 static struct gatt_req_info *__bt_find_remote_gatt_client_request_info(int conn_id, int trans_id)
401 struct gatt_client_info_t *info = NULL;
402 struct gatt_req_info *info1 = NULL;
404 for (l = gatt_client_info_list; l != NULL; l = g_slist_next(l)) {
405 info = (struct gatt_client_info_t*)l->data;
409 if (info->connection_id == conn_id) {
411 for (l1 = info->gatt_req_info_list; l1; l1 = g_slist_next(l1)) {
413 info1 = (struct gatt_req_info*)l1->data;
417 if (info1->request_id == trans_id) {
418 INFO("Remote GATT client found addr[%s]", info->addr);
427 void _bt_hal_gatt_connected_state_event(gboolean is_connected, char *address)
429 struct hal_ev_gatt_server_connected ev;
430 struct gatt_client_info_t *conn_info = NULL;
432 memset(&ev, 0, sizeof(ev));
434 /* Find Server Instance */
435 _bt_hal_get_gatt_server_instance_initialized(&instance);
436 if (instance == -1) {
437 ERR("Not even a single GATT server is registered");
441 /* Convert address to hex */
442 _bt_hal_convert_addr_string_to_type(ev.bdaddr, address);
444 /* Create Connection ID */
445 /* Check if device is already in connected list */
446 conn_info = __bt_find_remote_gatt_client_info(address);
449 /* If disconnected, and conn info found, then remove conn info */
450 if (is_connected == FALSE) {
451 DBG("GATT Disconnected");
453 INFO("Remove GATT client info from List..");
454 /* Remove info from List */
455 gatt_client_info_list = g_slist_remove(gatt_client_info_list, conn_info);
456 INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
459 ERR("GATT callback not registered");
461 DBG("GATT callback registered: server if [%d] Is Connected [%d] addr[%s] conn ID [%d]",
462 conn_info->instance_id, is_connected, conn_info->addr, conn_info->connection_id);
463 ev.conn_id = conn_info->connection_id;
464 ev.server_instance = conn_info->instance_id;
465 ev.connected = is_connected;
467 event_cb(HAL_EV_GATT_SERVER_CONNECTED, (void *)&ev, sizeof(ev));
469 g_free(conn_info->addr);
472 /* If connected, and conn info NOT found, then add conn info */
475 /* Save Connection info */
476 conn_info = g_new0(struct gatt_client_info_t, 1);
477 conn_info->addr = g_strdup(address);
478 INFO("Added GATT client addr[%s]", conn_info->addr);
479 conn_info->connection_id = ++conn_id;
480 conn_info->instance_id = instance;
481 gatt_client_info_list = g_slist_append(gatt_client_info_list, conn_info);
482 INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
485 ERR("GATT callback not registered");
487 DBG("GATT callback registered: server if [%d] Is Connected [%d] addr[%s] conn ID [%d]",
488 conn_info->instance_id, is_connected, conn_info->addr, conn_info->connection_id);
489 ev.conn_id = conn_info->connection_id;
490 ev.server_instance = conn_info->instance_id;
491 ev.connected = is_connected;
493 event_cb(HAL_EV_GATT_SERVER_CONNECTED, (void *)&ev, sizeof(ev));
497 /* Send GATT connected or disconnected event */
500 static struct gatt_service_info *__bt_gatt_find_gatt_service_from_char(const char *char_path, int *char_hdl)
504 for (l1 = gatt_services; l1 != NULL; l1 = l1->next) {
505 struct gatt_service_info *serv_info = l1->data;
507 for (l2 = serv_info->char_data; l2 != NULL; l2 = l2->next) {
508 struct gatt_char_info *char_info = l2->data;
510 if (g_strcmp0(char_info->char_path, char_path)
512 *char_hdl = char_info->char_handle;
517 ERR("Gatt service not found");
521 char *__bt_gatt_find_char_path_from_handle(int char_hdl)
525 for (l1 = gatt_services; l1 != NULL; l1 = l1->next) {
526 struct gatt_service_info *serv_info = l1->data;
528 for (l2 = serv_info->char_data; l2 != NULL; l2 = l2->next) {
529 struct gatt_char_info *char_info = l2->data;
531 if (char_info->char_handle == char_hdl)
532 return char_info->char_path;
539 struct gatt_char_info *__bt_gatt_find_char_info_from_handle(int char_hdl)
543 for (l1 = gatt_services; l1 != NULL; l1 = l1->next) {
544 struct gatt_service_info *serv_info = l1->data;
546 for (l2 = serv_info->char_data; l2 != NULL; l2 = l2->next) {
547 struct gatt_char_info *char_info = l2->data;
549 if (char_info->char_handle == char_hdl)
557 static struct gatt_service_info *__bt_gatt_find_gatt_service_from_desc(const char *desc_path, int *desc_hdl)
559 GSList *l1, *l2, *l3;
561 for (l1 = gatt_services; l1 != NULL; l1 = l1->next) {
562 struct gatt_service_info *serv_info = l1->data;
564 for (l2 = serv_info->char_data; l2 != NULL; l2 = l2->next) {
565 struct gatt_char_info *char_info = l2->data;
567 for (l3 = char_info->desc_data; l3 != NULL; l3 = l3->next) {
568 struct gatt_desc_info *desc_info = l3->data;
570 if (g_strcmp0(desc_info->desc_path, desc_path)
572 *desc_hdl = desc_info->desc_handle;
578 ERR("Gatt service not found");
582 static void __bt_gatt_manager_method_call(GDBusConnection *connection,
584 const gchar *object_path,
585 const gchar *interface_name,
586 const gchar *method_name,
587 GVariant *parameters,
588 GDBusMethodInvocation *invocation,
593 if (g_strcmp0(method_name, "GetManagedObjects") == 0) {
594 GVariantBuilder *builder;
595 GVariantBuilder *inner_builder1 = NULL;
596 GVariant *svc_char = NULL;
598 GSList *gatt_services = NULL;
600 instance = (int*)user_data;
602 DBG("Getting values for service, chars and descriptors");
603 DBG("GATT Server App for which services are requested [%d]", *instance);
606 builder = g_variant_builder_new(
607 G_VARIANT_TYPE("a{oa{sa{sv}}}"));
610 gatt_services = _bt_get_service_list_from_server(*instance);
612 if (g_slist_length(gatt_services) == 0) {
613 ERR("No registered GATT services!!!!");
614 g_dbus_method_invocation_return_value(invocation, NULL);
618 for (l = gatt_services; l != NULL; l = l->next) {
619 GVariantBuilder *svc_builder = NULL;
620 GVariantBuilder *inner_builder = NULL;
621 struct gatt_service_info *serv_info = l->data;
622 INFO("GATT Service fetched handle [%d]", serv_info->service_handle);
624 /* Prepare inner builder for GattService1 interface */
625 DBG("Creating builder for service");
626 svc_builder = g_variant_builder_new(
627 G_VARIANT_TYPE("a{sa{sv}}"));
628 inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
630 g_variant_builder_add(inner_builder, "{sv}", "UUID",
631 g_variant_new_string(serv_info->service_uuid));
633 g_variant_builder_add(inner_builder, "{sv}", "Primary",
634 g_variant_new_boolean(serv_info->is_svc_primary));
637 inner_builder1 = g_variant_builder_new(G_VARIANT_TYPE("ao"));
638 DBG("Adding Charatarisitcs list");
639 for (l4 = serv_info->char_data; l4 != NULL; l4 = l4->next) {
640 struct gatt_char_info *char_info = l4->data;
641 INFO("GATT Char handle [%d] found in GATT service handle [%d]",
642 char_info->char_handle, serv_info->service_handle);
643 g_variant_builder_add(inner_builder1, "o",
644 char_info->char_path);
645 DBG("%s", char_info->char_path);
648 svc_char = g_variant_new("ao", inner_builder1);
649 g_variant_builder_add(inner_builder, "{sv}", "Characteristics",
652 g_variant_builder_add(svc_builder, "{sa{sv}}",
656 g_variant_builder_add(builder, "{oa{sa{sv}}}",
657 serv_info->serv_path,
660 g_variant_builder_unref(inner_builder1);
662 /* Prepare inner builder for GattCharacteristic1 interface */
664 GSList *l2 = serv_info->char_data;
665 DBG("Creating builder for characteristics \n");
668 DBG("characteristic data is NULL");
670 for (l2 = serv_info->char_data; l2 != NULL; l2 = l2->next) {
672 GVariantBuilder *char_builder = NULL;
673 GVariantBuilder *inner_builder = NULL;
674 GVariantBuilder *builder1 = NULL;
675 GVariantBuilder *builder2 = NULL;
676 GVariantBuilder *builder3 = NULL;
677 GVariant *char_val = NULL;
678 GVariant *flags_val = NULL;
679 GVariant *char_desc = NULL;
680 char *unicast = NULL;
681 gboolean notify = FALSE;
684 char_builder = g_variant_builder_new(
687 inner_builder = g_variant_builder_new(
691 struct gatt_char_info *char_info = l2->data;
692 if (char_info == NULL) {
693 ERR("char_info is NULL");
698 g_variant_builder_add(inner_builder, "{sv}", "UUID",
699 g_variant_new_string(char_info->char_uuid));
701 g_variant_builder_add(inner_builder, "{sv}", "Service",
702 g_variant_new("o", serv_info->serv_path));
704 builder1 = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
706 if (char_info->char_value != NULL) {
707 for (i = 0; i < char_info->value_length; i++) {
708 g_variant_builder_add(builder1, "y",
709 char_info->char_value[i]);
711 char_val = g_variant_new("ay", builder1);
712 g_variant_builder_add(inner_builder, "{sv}",
716 builder2 = g_variant_builder_new(G_VARIANT_TYPE("as"));
717 for (i = 0; i < char_info->flags_length; i++) {
718 g_variant_builder_add(builder2, "s",
719 char_info->char_flags[i]);
722 flags_val = g_variant_new("as", builder2);
723 g_variant_builder_add(inner_builder, "{sv}", "Flags",
727 g_variant_builder_add(inner_builder, "{sv}", "Notifying",
728 g_variant_new("b", notify));
731 unicast = g_strdup("00:00:00:00:00:00");
732 g_variant_builder_add(inner_builder, "{sv}", "Unicast",
733 g_variant_new("s", unicast));
736 builder3 = g_variant_builder_new(G_VARIANT_TYPE("ao"));
737 DBG("Adding Descriptors list");
739 for (l4 = char_info->desc_data; l4 != NULL; l4 = l4->next) {
740 struct gatt_desc_info *desc_info = l4->data;
741 INFO("GATT Descriptor handle [%d] found inside GATT Char handle [%d] found in GATT service handle [%d]",
742 desc_info->desc_handle, char_info->char_handle, serv_info->service_handle);
743 g_variant_builder_add(builder3, "o",
744 desc_info->desc_path);
745 DBG("%s", desc_info->desc_path);
748 char_desc = g_variant_new("ao", builder3);
749 g_variant_builder_add(inner_builder, "{sv}", "Descriptors",
752 g_variant_builder_add(char_builder, "{sa{sv}}",
753 GATT_CHAR_INTERFACE , inner_builder);
754 g_variant_builder_add(builder, "{oa{sa{sv}}}",
755 char_info->char_path, char_builder);
757 /*Prepare inner builder for GattDescriptor1 interface*/
759 GSList *l3 = char_info->desc_data;
762 DBG("descriptor data is NULL");
764 for (l3 = char_info->desc_data; l3 != NULL; l3 = l3->next) {
766 DBG("Creating builder for descriptor \n");
768 GVariantBuilder *desc_builder = NULL;
769 GVariantBuilder *inner_builder = NULL;
770 GVariantBuilder *builder1 = NULL;
771 GVariantBuilder *builder2 = NULL;
772 GVariant *desc_val = NULL;
774 desc_builder = g_variant_builder_new(
777 inner_builder = g_variant_builder_new(
781 struct gatt_desc_info *desc_info = l3->data;
782 if (desc_info == NULL) {
783 ERR("desc_info is NULL");
788 g_variant_builder_add(inner_builder,
790 g_variant_new_string(
791 desc_info->desc_uuid));
794 g_variant_builder_add(inner_builder, "{sv}",
797 char_info->char_path));
800 builder1 = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
802 if (desc_info->desc_value != NULL) {
803 for (i = 0; i < desc_info->value_length; i++) {
804 g_variant_builder_add(builder1, "y",
805 desc_info->desc_value[i]);
807 desc_val = g_variant_new("ay", builder1);
808 g_variant_builder_add(inner_builder, "{sv}",
813 builder2 = g_variant_builder_new(G_VARIANT_TYPE("as"));
815 for (i = 0; i < desc_info->flags_length; i++) {
816 g_variant_builder_add(builder2, "s",
817 desc_info->desc_flags[i]);
820 flags_val = g_variant_new("as", builder2);
821 g_variant_builder_add(inner_builder, "{sv}", "Flags",
824 g_variant_builder_add(desc_builder, "{sa{sv}}",
828 g_variant_builder_add(builder, "{oa{sa{sv}}}",
829 desc_info->desc_path,
832 /*unref descriptor builder pointers*/
833 g_variant_builder_unref(builder1);
834 g_variant_builder_unref(builder2);
835 g_variant_builder_unref(inner_builder);
836 g_variant_builder_unref(desc_builder);
841 /*unref char builder pointers*/
842 g_variant_builder_unref(builder1);
843 g_variant_builder_unref(builder2);
844 g_variant_builder_unref(builder3);
845 g_variant_builder_unref(inner_builder);
846 g_variant_builder_unref(char_builder);
849 /*unref service builder pointers*/
850 g_variant_builder_unref(inner_builder);
851 g_variant_builder_unref(svc_builder);
854 /* Return builder as method reply */
855 DBG("Sending gatt service builder values to Bluez");
856 g_dbus_method_invocation_return_value(invocation,
866 static void __bt_gatt_desc_method_call(GDBusConnection *connection,
868 const gchar *object_path,
869 const gchar *interface_name,
870 const gchar *method_name,
871 GVariant *parameters,
872 GDBusMethodInvocation *invocation,
876 if (g_strcmp0(method_name, "ReadValue") == 0) {
877 struct hal_ev_gatt_server_read_req ev;
882 struct gatt_client_info_t *conn_info = NULL;
883 struct gatt_req_info *req_info = NULL;
884 struct gatt_service_info *svc_info = NULL;
888 DBG("Application path = %s", object_path);
889 DBG("Sender = %s", sender);
891 svc_info = __bt_gatt_find_gatt_service_from_desc(object_path, &desc_hdl);
892 if (svc_info == NULL) {
893 ERR("Coudn't find service for %s", object_path);
894 g_dbus_method_invocation_return_value(invocation, NULL);
898 g_variant_get(parameters, "(&suq)", &addr, &req_id, &offset);
899 DBG("Request id = %u, Offset = %u", req_id, offset);
901 /* Check if device is already in connected list */
902 conn_info = __bt_find_remote_gatt_client_info(addr);
904 if (conn_info == NULL) {
905 ERR("Coudn't find Connection info for %s", addr);
906 g_dbus_method_invocation_return_value(invocation, NULL);
911 ERR("GATT callback NOT registered");
912 g_dbus_method_invocation_return_value(invocation, NULL);
916 /* Store requests information */
917 req_info = g_new0(struct gatt_req_info, 1);
918 req_info->attr_path = g_strdup(object_path);
919 req_info->svc_path = g_strdup(svc_info->serv_path);
920 req_info->request_id = req_id;
921 req_info->offset = offset;
922 req_info->context = invocation;
924 /* Append request info in list of requests for the particular connection */
925 conn_info->gatt_req_info_list = g_slist_append(conn_info->gatt_req_info_list, req_info);
928 memset(&ev, 0, sizeof(ev));
929 ev.conn_id = conn_info->connection_id;
930 ev.trans_id = req_id;
931 ev.att_handle = desc_hdl;
933 ev.is_long = false; /* TODO*/
935 /* Convert address to hex */
936 _bt_hal_convert_addr_string_to_type(ev.bdaddr, addr);
938 event_cb(HAL_EV_GATT_READ_REQUESTED, (void *)&ev, sizeof(ev));
940 } else if (g_strcmp0(method_name, "WriteValue") == 0) {
942 GVariant *var = NULL;
946 gboolean response_needed = FALSE;
947 struct hal_ev_gatt_server_write_req ev;
951 struct gatt_service_info *svc_info = NULL;
952 struct gatt_client_info_t *conn_info = NULL;
953 struct gatt_req_info *req_info = NULL;
956 DBG("Application path = %s", object_path);
957 DBG("Sender = %s", sender);
959 g_variant_get(parameters, "(&suqb@ay)",
960 &addr, &req_id, &offset, &response_needed, &var);
961 DBG("Request id = %u, Offset = %u", req_id, offset);
963 /* Check if device is already in connected list */
964 conn_info = __bt_find_remote_gatt_client_info(addr);
966 svc_info = __bt_gatt_find_gatt_service_from_desc(object_path, &desc_hdl);
968 if (conn_info == NULL || svc_info == NULL || event_cb == NULL) {
969 g_variant_unref(var);
971 g_dbus_method_invocation_return_value(invocation, NULL);
973 g_object_unref(invocation);
977 len = g_variant_get_size(var);
980 data = (char *)g_variant_get_data(var);
981 memcpy(ev.value, data, len);
984 if (response_needed) {
985 /* Store request information */
986 req_info = g_new0(struct gatt_req_info, 1);
987 req_info->attr_path = g_strdup(object_path);
988 req_info->svc_path = g_strdup(svc_info->serv_path);
989 req_info->request_id = req_id;
990 req_info->offset = offset;
991 req_info->context = invocation;
993 /* Append request info in list of requests for the particular connection */
994 conn_info->gatt_req_info_list = g_slist_append(conn_info->gatt_req_info_list, req_info);
996 g_object_unref(invocation);
1000 memset(&ev, 0, sizeof(ev));
1001 ev.conn_id = conn_info->connection_id;
1002 ev.trans_id = req_id;
1003 ev.att_handle = desc_hdl;
1005 ev.need_rsp = response_needed;
1008 /* Convert address to hex */
1009 _bt_hal_convert_addr_string_to_type(ev.bdaddr, addr);
1011 event_cb(HAL_EV_GATT_WRITE_REQUESTED, (void *)&ev, sizeof(ev));
1013 g_variant_unref(var);
1018 static void __bt_gatt_char_method_call(GDBusConnection *connection,
1019 const gchar *sender,
1020 const gchar *object_path,
1021 const gchar *interface_name,
1022 const gchar *method_name,
1023 GVariant *parameters,
1024 GDBusMethodInvocation *invocation,
1027 if (g_strcmp0(method_name, "ReadValue") == 0) {
1031 struct hal_ev_gatt_server_read_req ev;
1034 struct gatt_req_info *req_info = NULL;
1035 struct gatt_client_info_t *conn_info = NULL;
1036 struct gatt_service_info *svc_info = NULL;
1038 DBG("Application path = %s", object_path);
1039 DBG("Sender = %s", sender);
1041 /* Check if device is already in connected list */
1042 conn_info = __bt_find_remote_gatt_client_info(addr);
1044 svc_info = __bt_gatt_find_gatt_service_from_char(object_path, &char_hdl);
1046 if (svc_info == NULL || conn_info == NULL) {
1047 g_dbus_method_invocation_return_value(invocation, NULL);
1052 ERR("GATT callback NOT registered");
1053 g_dbus_method_invocation_return_value(invocation, NULL);
1057 g_variant_get(parameters, "(&suq)", &addr, &req_id, &offset);
1058 DBG("Request id = %u, Offset = %u", req_id, offset);
1060 /* Store requets information */
1061 req_info = g_new0(struct gatt_req_info, 1);
1062 req_info->attr_path = g_strdup(object_path);
1063 req_info->svc_path = g_strdup(svc_info->serv_path);
1064 req_info->request_id = req_id;
1065 req_info->offset = offset;
1066 req_info->context = invocation;
1068 /* Append request info in list of requests for the particular connection */
1069 conn_info->gatt_req_info_list = g_slist_append(conn_info->gatt_req_info_list, req_info);
1071 /* Send HAL event */
1072 memset(&ev, 0, sizeof(ev));
1073 ev.conn_id = conn_info->connection_id;
1074 ev.trans_id = req_id;
1075 ev.att_handle = char_hdl;
1077 ev.is_long = false; /* TODO*/
1079 /* Convert address to hex */
1080 _bt_hal_convert_addr_string_to_type(ev.bdaddr, addr);
1082 event_cb(HAL_EV_GATT_READ_REQUESTED, (void *)&ev, sizeof(ev));
1084 } else if (g_strcmp0(method_name, "WriteValue") == 0) {
1085 GVariant *var = NULL;
1089 gboolean response_needed = FALSE;
1090 struct hal_ev_gatt_server_write_req ev;
1093 struct gatt_service_info *svc_info = NULL;
1094 struct gatt_req_info *req_info = NULL;
1095 struct gatt_client_info_t *conn_info = NULL;
1098 DBG("Application path = %s", object_path);
1099 DBG("Sender = %s", sender);
1101 g_variant_get(parameters, "(&suqb@ay)",
1102 &addr, &req_id, &offset, &response_needed, &var);
1103 DBG("Request id = %u, Offset = %u", req_id, offset);
1105 svc_info = __bt_gatt_find_gatt_service_from_char(object_path, &char_hdl);
1107 /* Check if device is already in connected list */
1108 conn_info = __bt_find_remote_gatt_client_info(addr);
1110 if (svc_info == NULL || conn_info == NULL || event_cb == NULL) {
1111 g_variant_unref(var);
1112 if (response_needed)
1113 g_dbus_method_invocation_return_value(invocation, NULL);
1115 g_object_unref(invocation);
1119 if (response_needed) {
1120 /* Store requets information */
1121 req_info = g_new0(struct gatt_req_info, 1);
1122 req_info->attr_path = g_strdup(object_path);
1123 req_info->svc_path = g_strdup(svc_info->serv_path);
1124 req_info->request_id = req_id;
1125 req_info->offset = offset;
1126 req_info->context = invocation;
1128 /* Append request info in list of requests for the particular connection */
1129 conn_info->gatt_req_info_list = g_slist_append(conn_info->gatt_req_info_list, req_info);
1132 g_object_unref(invocation);
1135 /* Send HAL event */
1136 memset(&ev, 0, sizeof(ev));
1137 ev.conn_id = conn_info->connection_id;
1138 ev.trans_id = req_id;
1139 ev.att_handle = char_hdl;
1141 ev.need_rsp = response_needed;
1144 /* Convert address to hex */
1145 _bt_hal_convert_addr_string_to_type(ev.bdaddr, addr);
1147 event_cb(HAL_EV_GATT_WRITE_REQUESTED, (void *)&ev, sizeof(ev));
1149 g_variant_unref(var);
1152 } else if (g_strcmp0(method_name, "StartNotify") == 0) {
1155 struct gatt_service_info *svc_info = NULL;
1156 struct hal_ev_gatt_server_notifcation_change ev;
1159 svc_info = __bt_gatt_find_gatt_service_from_char(object_path, &char_hdl);
1160 if (svc_info == NULL || event_cb == NULL)
1163 /* Send HAL event */
1164 memset(&ev, 0, sizeof(ev));
1165 ev.conn_id = -1; /*TODO Bluez does not provide remote GATT client address, so no conn_id */
1166 ev.trans_id = -1; /*TODO Bluez does not provide request id or transacion ID */
1167 ev.att_handle = char_hdl;
1170 /* Convert address to hex */
1171 _bt_hal_convert_addr_string_to_type(ev.bdaddr, "00:00:00:00:00"); /* TODO Bluez Does not provide address of GATT client */
1173 event_cb(HAL_EV_GATT_NOTIFICATION_CHANGE, (void *)&ev, sizeof(ev));
1176 } else if (g_strcmp0(method_name, "StopNotify") == 0) {
1179 struct gatt_service_info *svc_info = NULL;
1180 struct hal_ev_gatt_server_notifcation_change ev;
1183 svc_info = __bt_gatt_find_gatt_service_from_char(object_path, &char_hdl);
1184 if (svc_info == NULL || event_cb == NULL)
1187 /* Send HAL event */
1188 memset(&ev, 0, sizeof(ev));
1189 ev.conn_id = -1; /*TODO Bluez does not provide remote GATT client address, so no conn_id */
1190 ev.trans_id = -1; /*TODO Bluez does not provide request id or transacion ID */
1191 ev.att_handle = char_hdl;
1194 /* Convert address to hex */
1195 _bt_hal_convert_addr_string_to_type(ev.bdaddr, "00:00:00:00:00"); /* TODO Bluez DOes not provide address of GATT client */
1197 event_cb(HAL_EV_GATT_NOTIFICATION_CHANGE, (void *)&ev, sizeof(ev));
1200 } else if (g_strcmp0(method_name, "IndicateConfirm") == 0) {
1202 gboolean complete = FALSE;
1205 struct gatt_service_info *svc_info = NULL;
1206 struct gatt_client_info_t *conn_info = NULL;
1208 struct hal_ev_gatt_server_indicate_cfm ev;
1210 DBG("IndicateConfirm");
1211 DBG("Application path = %s", object_path);
1212 DBG("Sender = %s", sender);
1214 g_variant_get(parameters, "(&sb)", &addr, &complete);
1215 DBG("Remote Device address number = %s", addr);
1216 DBG("Is Indicate confirmation for last device [%d]", complete);
1218 /* Check if device is already in connected list */
1219 conn_info = __bt_find_remote_gatt_client_info(addr);
1221 svc_info = __bt_gatt_find_gatt_service_from_char(object_path, &char_hdl);
1223 if (svc_info == NULL || conn_info == NULL
1224 || event_cb == NULL) {
1228 /* Send HAL event */
1229 memset(&ev, 0, sizeof(ev));
1230 ev.conn_id = conn_info->connection_id;
1231 ev.trans_id = -1; /*TODO Bluez does not provide Transaction ID or request ID */
1232 ev.att_handle = char_hdl;
1234 /* Convert address to hex */
1235 _bt_hal_convert_addr_string_to_type(ev.bdaddr, addr);
1237 event_cb(HAL_EV_GATT_INDICATE_CFM, (void *)&ev, sizeof(ev));
1240 g_dbus_method_invocation_return_value(invocation, NULL);
1243 gboolean __bt_hal_gatt_emit_interface_removed(gchar *object_path, gchar *interface)
1246 GError *error = NULL;
1247 GVariantBuilder *array_builder;
1249 array_builder = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
1250 g_variant_builder_init(array_builder, G_VARIANT_TYPE("as"));
1251 g_variant_builder_add(array_builder, "s", interface);
1253 ret = g_dbus_connection_emit_signal(g_conn, NULL, "/",
1254 "org.freedesktop.Dbus.Objectmanager",
1255 "InterfacesRemoved",
1256 g_variant_new("(oas)",
1257 object_path, array_builder),
1261 if (error != NULL) {
1262 /* dbus gives error cause */
1263 ERR("d-bus api failure: errcode[%x], message[%s]",
1264 error->code, error->message);
1265 g_clear_error(&error);
1268 g_variant_builder_unref(array_builder);
1273 static void __bt_hal_gatt_free_descriptor_info(struct gatt_desc_info *desc_info)
1280 g_free(desc_info->desc_path);
1281 g_free(desc_info->desc_uuid);
1282 g_free(desc_info->desc_value);
1284 for (i = 0; i < desc_info->flags_length; i++)
1285 g_free(desc_info->desc_flags[i]);
1290 static void __bt_hal_gatt_free_characteristic_info(struct gatt_char_info *char_info)
1297 g_free(char_info->char_path);
1298 g_free(char_info->char_uuid);
1299 g_free(char_info->char_value);
1301 for (i = 0; i < char_info->flags_length; i++)
1302 g_free(char_info->char_flags[i]);
1308 static void __bt_hal_gatt_free_service_info(struct gatt_service_info *svc_info)
1313 g_free(svc_info->serv_path);
1314 g_free(svc_info->service_uuid);
1318 static const GDBusInterfaceVTable desc_interface_vtable = {
1319 __bt_gatt_desc_method_call,
1324 static const GDBusInterfaceVTable char_interface_vtable = {
1325 __bt_gatt_char_method_call,
1330 static const GDBusInterfaceVTable serv_interface_vtable = {
1336 static const GDBusInterfaceVTable manager_interface_vtable = {
1337 __bt_gatt_manager_method_call,
1343 static GDBusNodeInfo *__bt_gatt_create_method_node_info(
1344 const gchar *introspection_data)
1347 GDBusNodeInfo *node_info = NULL;
1349 if (introspection_data == NULL)
1353 DBG("Create new node info");
1354 node_info = g_dbus_node_info_new_for_xml(introspection_data, &err);
1357 ERR("Unable to create node: %s", err->message);
1358 g_clear_error(&err);
1365 /* To send stack event to hal-av handler */
1366 void _bt_hal_register_gatt_server_handler_cb(handle_stack_msg cb)
1371 void _bt_hal_unregister_gatt_server_handler_cb(void)
1376 static gboolean __bt_hal_gatt_desc_added_cb(gpointer user_data)
1378 struct hal_ev_gatt_desc_added ev;
1379 hal_gatt_desc_added *data = (hal_gatt_desc_added*) user_data;
1381 /* Prepare to GATT characteristic added event */
1382 memset(&ev, 0, sizeof(ev));
1383 ev.status = BT_STATUS_SUCCESS;
1384 ev.server_instance = data->instance_data;
1385 memcpy(ev.desc_uuid, data->uuid.uu, sizeof(data->uuid.uu));
1386 ev.service_handle = data->srvc_hdl;
1387 ev.desc_handle = data->desc_hdl;
1390 ERR("GATT Descriptor Added callback registered");
1392 DBG("GATT Descriptor Added: server if [%d] Service handle [%d] Descriptor Handle [%d]",
1393 data->instance_data, data->srvc_hdl, data->desc_hdl);
1395 event_cb(HAL_EV_GATT_DESC_ADDED, (void *)&ev, sizeof(ev));
1402 static gboolean __bt_hal_gatt_char_added_cb(gpointer user_data)
1404 struct hal_ev_gatt_char_added ev;
1405 hal_gatt_char_added *data = (hal_gatt_char_added*) user_data;
1407 /* Prepare to GATT characteristic added event */
1408 memset(&ev, 0, sizeof(ev));
1409 ev.status = BT_STATUS_SUCCESS;
1410 ev.server_instance = data->instance_data;
1411 memcpy(ev.char_uuid, data->uuid.uu, sizeof(data->uuid.uu));
1412 ev.service_handle = data->srvc_hdl;
1413 ev.char_handle = data->char_hdl;
1416 ERR("GATT Characteristic Added callback registered");
1418 DBG("GATT Characteristic Added: server if [%d] Service handle [%d] Characteristic handle [%d]",
1419 data->instance_data, data->srvc_hdl, data->char_hdl);
1421 event_cb(HAL_EV_GATT_CHAR_ADDED, (void *)&ev, sizeof(ev));
1428 static gboolean __bt_hal_gatt_service_added_cb(gpointer user_data)
1430 struct hal_ev_gatt_service_added ev;
1431 hal_gatt_service_added *data = (hal_gatt_service_added*) user_data;
1433 /* Prepare to GATT Service added event */
1434 memset(&ev, 0, sizeof(ev));
1435 ev.status = BT_STATUS_SUCCESS;
1436 ev.server_instance = data->instance_data;
1437 memcpy(ev.svc_uuid, data->uuid.uu, sizeof(data->uuid.uu));
1438 ev.service_handle = data->srvc_hdl;
1439 ev.is_primary = data->is_primary;
1442 ERR("GATT Service Added callback registered");
1444 DBG("GATT Service Added: server if [%d] Service handle [%d]",
1445 data->instance_data, data->srvc_hdl);
1446 event_cb(HAL_EV_GATT_SERVICE_ADDED, (void *)&ev, sizeof(ev));
1453 static gboolean __bt_hal_gatt_service_started_cb(gpointer user_data)
1455 struct hal_ev_gatt_service_started ev;
1456 hal_gatt_service_started *data = (hal_gatt_service_started*) user_data;
1458 /* Prepare to GATT Service added event */
1459 memset(&ev, 0, sizeof(ev));
1460 ev.status = BT_STATUS_SUCCESS;
1461 ev.server_instance = data->instance_data;
1462 ev.service_handle = data->srvc_hdl;
1465 ERR("GATT Service Started callback registered");
1467 DBG("GATT Service Started: server if [%d] Service handle [%d]",
1468 data->instance_data, data->srvc_hdl);
1469 event_cb(HAL_EV_GATT_SERVICE_STARTED, (void *)&ev, sizeof(ev));
1476 static gboolean __bt_hal_gatt_service_deleted_cb(gpointer user_data)
1478 struct hal_ev_gatt_service_deleted ev;
1479 hal_gatt_service_deleted *data = (hal_gatt_service_deleted*) user_data;
1481 /* Prepare to GATT Service added event */
1482 memset(&ev, 0, sizeof(ev));
1483 ev.status = BT_STATUS_SUCCESS;
1484 ev.server_instance = data->instance_data;
1485 ev.service_handle = data->srvc_hdl;
1488 ERR("GATT Service Deleted callback registered");
1490 DBG("GATT Service Deleted: server if [%d] Service handle [%d]",
1491 data->instance_data, data->srvc_hdl);
1492 event_cb(HAL_EV_GATT_SERVICE_DELETED, (void *)&ev, sizeof(ev));
1499 static gboolean __bt_hal_register_slot_id_cb(gpointer user_data)
1501 struct hal_ev_server_instance_registered ev;
1502 hal_register_server_data *data = (hal_register_server_data*) user_data;
1504 /* Prepare to send AV connecting event */
1505 memset(&ev, 0, sizeof(ev));
1506 ev.status = BT_STATUS_SUCCESS;
1507 ev.server_instance = data->instance_data;
1508 memcpy(ev.app_uuid, data->uuid.uu, sizeof(ev.app_uuid));
1511 ERR("GATT Register Server Instance Callback not registered");
1513 DBG("Server Instance is registered!! server if [%d]", data->instance_data);
1514 event_cb(HAL_EV_SERVER_INSTANCE_INITIALIZED, (void *)&ev, sizeof(ev));
1521 static bt_status_t gatt_server_register_app(bt_uuid_t *uuid)
1523 CHECK_BTGATT_INIT();
1524 int status = BT_STATUS_FAIL;
1526 DBG("Register server instance request");
1527 hal_register_server_data *user_data = g_malloc0(sizeof(hal_register_server_data));
1529 /* Check if slot available */
1530 server_if = _bt_hal_get_available_adv_slot_id(uuid);
1532 if (server_if == -1) {
1533 ERR("Allocation of server instance failed");
1537 user_data->instance_data = server_if;
1538 DBG("Allocated new Advertising slot with Stack [%d]", user_data->instance_data);
1542 * As we need to provide async callback to user from HAL, simply schedule a
1543 * callback method which will carry actual result
1545 memcpy(user_data->uuid.uu, uuid->uu, sizeof(bt_uuid_t));
1546 g_idle_add(__bt_hal_register_slot_id_cb, (gpointer)user_data);
1548 /* If available, then return success, else return error */
1549 return BT_STATUS_SUCCESS;
1552 void _bt_hal_remove_gatt_server_from_list(int server_if)
1555 struct gatt_server_app *info = NULL;
1557 for (l = gatt_server_apps; l != NULL;) {
1558 info = (struct gatt_server_app*)l->data;
1559 l = g_slist_next(l);
1562 if (info->slot == server_if) {
1563 INFO("Found Matching GATT Server in List path[%s] slot [%d]",
1564 info->app_path, info->slot);
1566 /* Only if all services are deleted from the GATT Server, then only Unregister it.
1567 Reason: it is possible, GATT Server app oly wants to disable multi advertising
1568 In above case, only advertising block will be deallocated, Gatt Server will remain
1570 if (info->services == NULL) {
1571 gatt_server_apps = g_slist_remove(gatt_server_apps, (gpointer)info);
1572 INFO("Total gatt server apps still existing after removing above is [%d]",
1573 g_slist_length(gatt_server_apps));
1575 /* DBUS Unregister only for current app */
1576 __bt_hal_gatt_deinit(info->app_path);
1578 g_free(info->app_path);
1582 INFO("GATT Server still has services count[%d] in it..Can not remove it!!!",
1583 g_slist_length(info->services));
1589 static bt_status_t gatt_server_unregister_app(int server_if)
1591 CHECK_BTGATT_INIT();
1592 DBG("Un-Register server instance request [%d]", server_if);
1594 if (_bt_hal_is_advertising_in_slot(server_if) == FALSE)
1595 _bt_hal_free_server_slot(server_if);
1597 /* If server_if belongs to a GATT Server, then delete the GATT server from List */
1598 _bt_hal_remove_gatt_server_from_list(server_if);
1599 return BT_STATUS_SUCCESS;
1602 static bt_status_t gatt_server_open(int server_if, const bt_bdaddr_t *bd_addr, bool is_direct)
1604 CHECK_BTGATT_INIT();
1605 return BT_STATUS_SUCCESS;
1608 static bt_status_t gatt_server_close(int server_if, const bt_bdaddr_t *bd_addr, int conn_id)
1610 CHECK_BTGATT_INIT();
1611 return BT_STATUS_SUCCESS;
1614 static void __bt_gatt_close_gdbus_connection(void)
1622 if (!g_dbus_connection_flush_sync(g_conn, NULL, &err)) {
1623 ERR("Fail to flush the connection: %s", err->message);
1628 if (!g_dbus_connection_close_sync(g_conn, NULL, &err)) {
1630 ERR("Fail to close the dbus connection: %s", err->message);
1635 g_object_unref(g_conn);
1640 static GDBusConnection *__bt_gatt_get_gdbus_connection(void)
1642 GDBusConnection *local_system_gconn = NULL;
1646 if (g_conn == NULL) {
1647 address = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
1648 if (address == NULL) {
1650 ERR("Failed to get bus address: %s", err->message);
1651 g_clear_error(&err);
1656 g_conn = g_dbus_connection_new_for_address_sync(address,
1657 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
1658 G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
1659 NULL, /* GDBusAuthObserver */
1664 ERR("Unable to connect to dbus: %s", err->message);
1665 g_clear_error(&err);
1669 } else if (g_dbus_connection_is_closed(g_conn)) {
1670 address = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
1671 if (address == NULL) {
1673 ERR("Failed to get bus address: %s", err->message);
1674 g_clear_error(&err);
1679 local_system_gconn = g_dbus_connection_new_for_address_sync(address,
1680 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
1681 G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
1682 NULL, /* GDBusAuthObserver */
1686 if (!local_system_gconn) {
1687 ERR("Unable to connect to dbus: %s", err->message);
1688 g_clear_error(&err);
1691 g_conn = local_system_gconn;
1697 static char* __bt_hal_convert_uuid_to_string(bt_uuid_t *srvc_id)
1702 uuid = btuuid2str(srvc_id->uu);
1703 DBG("Original UUID [%s]", uuid);
1705 if (_bt_hal_uuid_is_standard(srvc_id) == TRUE) {
1706 /* Extract Standard UUID string */
1707 memcpy(uuid_buf, &uuid[4], 4);
1709 DBG("Converted string [%s]", uuid_buf);
1710 return g_strdup(uuid_buf);
1712 return strdup(uuid);
1715 int __bt_hal_add_service_to_dbus(char *app_path, int slot, btgatt_srvc_id_t *srvc_id)
1717 /* For GATT service specific */
1718 GDBusNodeInfo *node_info;
1721 struct gatt_service_info *serv_info = NULL;
1722 GVariantBuilder *builder = NULL;
1723 GVariantBuilder *builder1 = NULL;
1724 GVariantBuilder *inner_builder = NULL;
1725 gboolean svc_primary = TRUE;
1726 GError *error = NULL;
1727 hal_gatt_service_added *user_data = NULL;
1728 DBG("Service add to DBUS slot [%d]", slot);
1730 node_info = __bt_gatt_create_method_node_info(
1731 service_introspection_xml);
1733 if (node_info == NULL)
1734 return BT_STATUS_FAIL;
1736 DBG("Add new GATT Service: Current GATT Service handle [%d]", gatt_service_handle);
1737 path = g_strdup_printf("%s"GATT_SERV_OBJECT_PATH"%d", app_path, ++gatt_service_handle);
1738 DBG("gatt service path is [%s]", path);
1740 object_id = g_dbus_connection_register_object(g_conn, path,
1741 node_info->interfaces[0],
1742 &serv_interface_vtable,
1743 NULL, NULL, &error);
1745 if (object_id == 0) {
1746 ERR("failed to register: %s", error->message);
1747 g_error_free(error);
1749 return BT_STATUS_FAIL;
1751 /* Add object_id/gatt service information; it's required at the time of
1752 * service unregister and Getmanagedobjects
1754 serv_info = g_new0(struct gatt_service_info, 1);
1756 serv_info->serv_path = g_strdup(path);
1757 serv_info->serv_id = object_id;
1758 serv_info->service_uuid = __bt_hal_convert_uuid_to_string(&srvc_id->id.uuid);//g_strdup(btuuid2str(srvc_id->id.uuid.uu));
1759 serv_info->is_svc_registered = FALSE;
1760 serv_info->is_svc_primary = svc_primary;
1761 DBG("Service Handle to be added is [%d]", gatt_service_handle);
1762 serv_info->service_handle = gatt_service_handle;
1764 /* Update service in GATT Server service List */
1765 gatt_services = g_slist_append(gatt_services, serv_info);
1767 /* emit interfacesadded signal here for service path */
1768 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
1769 inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1771 g_variant_builder_add(inner_builder, "{sv}",
1772 "UUID", g_variant_new_string(btuuid2str(srvc_id->id.uuid.uu)));
1774 g_variant_builder_add(inner_builder, "{sv}",
1775 "Primary", g_variant_new_boolean(svc_primary));
1777 builder1 = g_variant_builder_new(G_VARIANT_TYPE("ao"));
1779 g_variant_builder_add(inner_builder, "{sv}", "Characteristics",
1780 g_variant_new("ao", builder1));
1782 g_variant_builder_add(builder, "{sa{sv}}",
1783 GATT_SERV_INTERFACE, inner_builder);
1785 g_dbus_connection_emit_signal(g_conn, NULL, "/",
1786 "org.freedesktop.Dbus.ObjectManager",
1788 g_variant_new("(oa{sa{sv}})",
1792 /* Send Service handle to application */
1793 user_data = g_malloc0(sizeof(hal_gatt_service_added));
1794 user_data->srvc_hdl = serv_info->service_handle;
1795 user_data->instance_data = slot;
1796 memcpy(user_data->uuid.uu, srvc_id->id.uuid.uu, sizeof(srvc_id->id.uuid.uu));
1797 g_idle_add(__bt_hal_gatt_service_added_cb, (gpointer)user_data);
1799 /* Save newly created service in GATT Server's service list */
1800 _bt_hal_update_gatt_service_in_gatt_server(slot, serv_info);
1804 g_variant_builder_unref(inner_builder);
1805 g_variant_builder_unref(builder);
1806 g_variant_builder_unref(builder1);
1807 return BT_STATUS_SUCCESS;
1810 static void __bt_hal_unregister_application_cb(GObject *object, GAsyncResult *res,
1813 char *app_path = (char*)user_data;
1814 INFO("UnregisterApplication is completed app [%s]", app_path);
1815 GError *error = NULL;
1818 result = g_dbus_proxy_call_finish(manager_gproxy, res, &error);
1820 if (result == NULL) {
1821 /* dBUS-RPC is failed */
1822 ERR("Dbus-RPC is failed\n");
1824 if (error != NULL) {
1825 /* dBUS gives error cause */
1826 ERR("D-Bus API failure: errCode[%x], message[%s]\n",
1827 error->code, error->message);
1828 g_clear_error(&error);
1831 g_variant_unref(result);
1836 static void __bt_hal_gatt_deinit(char *app_path)
1838 GDBusProxy *proxy = NULL;
1842 /* Step1: Remove requested App */
1843 proxy = __bt_gatt_gdbus_get_gatt_manager_proxy("org.bluez",
1844 "/org/bluez/hci0", "org.bluez.GattManager1");
1849 INFO("UnregisterApplication : path [%s]", app_path);
1851 /* Async Call to Unregister Service */
1852 data = g_strdup(app_path);
1853 g_dbus_proxy_call(proxy,
1854 "UnregisterApplication",
1855 g_variant_new("(o)",
1857 G_DBUS_CALL_FLAGS_NONE, -1,
1859 (GAsyncReadyCallback)__bt_hal_unregister_application_cb,
1862 /* If requested app is last GATT Server app, then clean all resources */
1863 if (gatt_server_apps == NULL) {
1864 INFO("All GATT servers are removed, clean all DBUS resources");
1866 /* unregister the exported interface for object manager
1867 g_conn and manager_id are common for all GATT servers */
1868 g_dbus_connection_unregister_object(g_conn,
1871 g_bus_unown_name(owner_id);
1874 g_object_unref(manager_gproxy);
1875 manager_gproxy = NULL;
1877 /* Close the GDBUS connection */
1878 __bt_gatt_close_gdbus_connection();
1884 int __bt_hal_gatt_init(void)
1886 GDBusConnection *conn = NULL;
1888 /* Only once for ALL GATT Servers */
1889 if (owner_id == 0) {
1890 owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
1891 BT_GATT_SERVICE_NAME,
1892 G_BUS_NAME_OWNER_FLAGS_NONE,
1893 NULL, NULL, NULL, NULL, NULL);
1895 INFO("Owner ID [%d]", owner_id);
1897 /* Only once for ALL GATT Servers conn = g_conn(global)*/
1898 conn = __bt_gatt_get_gdbus_connection();
1900 ERR("Unable to get connection");
1901 return BT_STATUS_FAIL;
1904 /* Only once for ALL GATT Servers */
1905 if (manager_node_info == NULL) {
1906 /* Register ObjectManager interface */
1907 manager_node_info = __bt_gatt_create_method_node_info(
1908 manager_introspection_xml);
1910 if (manager_node_info == NULL) {
1911 ERR("failed to get node info");
1912 return BT_STATUS_FAIL;
1917 return BT_STATUS_SUCCESS;
1920 void _bt_hal_is_gatt_server_initialzed(int slot, char **app_path)
1924 for (l = gatt_server_apps; l; l = g_slist_next(l)) {
1925 struct gatt_server_app *app = (struct gatt_server_app *)l->data;
1926 if (app->slot == slot) {
1927 INFO("GATT Server app found app path [%s] instance [%d]",
1928 app->app_path, app->slot);
1929 *app_path = app->app_path;
1933 /* GATT Server not found */
1937 void _bt_hal_update_gatt_server_path(int slot, char *app_path)
1939 if (app_path == NULL)
1942 struct gatt_server_app *app = g_malloc0(sizeof(struct gatt_server_app));
1943 app->app_path = g_strdup(app_path);
1945 gatt_server_apps = g_slist_append(gatt_server_apps, app);
1946 INFO("GATT Server: Path [%s] Slot [%d]-> Updated", app_path, slot);
1951 static bt_status_t gatt_server_add_service(int server_if, btgatt_srvc_id_t *srvc_id,
1954 CHECK_BTGATT_INIT();
1955 char *app_path = NULL;
1956 GError *error = NULL;
1960 int result = BT_STATUS_SUCCESS;
1962 DBG("GATT Server Add service request: Instance ID[%d] Is Primary [%d] UUID [%s]",
1963 server_if, srvc_id->is_primary, btuuid2str(srvc_id->id.uuid.uu));
1965 /* Check if this GATT server Application is already registered with DBUS */
1966 _bt_hal_is_gatt_server_initialzed(server_if, &app_path);
1968 if (app_path != NULL) {
1969 DBG("GATT server path is already defined [%s]", app_path);
1970 return __bt_hal_add_service_to_dbus(app_path, server_if, srvc_id);
1972 DBG("GATT server application path for instance [%d] is not defined yet", server_if);
1973 result = __bt_hal_gatt_init();
1974 if (result != BT_STATUS_SUCCESS)
1977 /* Only once for each GATT Server */
1978 app_path = g_strdup_printf("/com/%d", server_if);
1980 app_id = g_malloc0(sizeof(int));
1981 *app_id = server_if;
1983 manager_id = g_dbus_connection_register_object(g_conn, app_path,
1984 manager_node_info->interfaces[0],
1985 &manager_interface_vtable,
1986 (gpointer)app_id, NULL, &error);
1988 if (manager_id == 0) {
1989 ERR("failed to register: %s", error->message);
1990 g_error_free(error);
1994 /* For current GATT Server, app_path is created, save it in Table */
1995 _bt_hal_update_gatt_server_path(server_if, app_path);
1997 /* Add GATT Service to DBUS */
1998 if (__bt_hal_add_service_to_dbus(app_path, server_if, srvc_id) != BT_STATUS_SUCCESS)
2004 INFO("Successfully added service");
2005 return BT_STATUS_SUCCESS;
2012 INFO("Service addition failed!!");
2013 return BT_STATUS_FAIL;
2016 static bt_status_t gatt_server_add_included_service(int server_if, int service_handle,
2017 int included_handle)
2019 CHECK_BTGATT_INIT();
2020 return BT_STATUS_SUCCESS;
2024 static gboolean __bt_is_service_last_in_server_list(int instance, int service_handle)
2027 GSList *gatt_services = NULL;
2029 struct gatt_service_info *info = NULL;
2031 gatt_services = _bt_get_service_list_from_server(instance);
2033 len = g_slist_length(gatt_services);
2034 l = g_slist_nth(gatt_services, len -1);
2038 if (info->service_handle == service_handle)
2043 static struct gatt_service_info *__bt_gatt_find_gatt_service_info(int instance,
2047 GSList *gatt_services = NULL;
2048 INFO("Find Service: svc handle [%d] from GATT Server slot[%d]", service_handle, instance);
2050 gatt_services = _bt_get_service_list_from_server(instance);
2052 for (l = gatt_services; l != NULL; l = g_slist_next(l)) {
2053 struct gatt_service_info *info = l->data;
2054 INFO("Got one service with handle [%d]", info->service_handle);
2055 if (info->service_handle == service_handle)
2058 ERR("Gatt service with handle [%d] not found", service_handle);
2062 static bt_status_t gatt_server_add_characteristic(int slot, int service_handle,
2063 bt_uuid_t *uuid, int properties,
2066 GError *error = NULL;
2068 GDBusNodeInfo *node_info;
2070 GVariantBuilder *builder = NULL;
2071 GVariantBuilder *inner_builder = NULL;
2072 struct gatt_service_info *serv_info = NULL;
2073 struct gatt_char_info *char_info = NULL;
2074 GVariantBuilder *builder2 = NULL;
2075 GVariantBuilder *builder3 = NULL;
2076 GVariant *flags_val = NULL;
2078 char *char_flags[NUMBER_OF_FLAGS];
2080 hal_gatt_char_added *user_data = NULL;
2083 CHECK_BTGATT_INIT();
2084 DBG("Add new characteristic to GATT Service handle [%d]", service_handle);
2085 DBG("Properties of new charateristic [%d] Permission [%d]", properties, permissions);
2087 serv_info = __bt_gatt_find_gatt_service_info(slot, service_handle);
2088 if (serv_info == NULL)
2089 return BT_STATUS_FAIL;
2091 node_info = __bt_gatt_create_method_node_info(
2092 characteristics_introspection_xml);
2094 if (node_info == NULL)
2095 return BT_STATUS_FAIL;
2097 DBG("Add new GATT characteristic: Current GATT char handle [%d]", gatt_char_handle);
2098 path = g_strdup_printf("%s/characteristic%d", serv_info->serv_path, ++gatt_char_handle);
2099 DBG("gatt characteristic path is [%s]", path);
2101 app_id = g_malloc0(sizeof(int));
2104 object_id = g_dbus_connection_register_object(g_conn, path,
2105 node_info->interfaces[0],
2106 &char_interface_vtable,
2107 (gpointer)app_id, NULL, &error);
2109 if (object_id == 0) {
2110 ERR("failed to register: %s", error->message);
2111 g_error_free(error);
2114 return BT_STATUS_FAIL;
2117 if (permissions & BT_HAL_GATT_PERMISSION_ENCRYPT_READ)
2118 properties |= BT_HAL_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_READ;
2119 if (permissions & BT_HAL_GATT_PERMISSION_ENCRYPT_AUTHENTICATED_READ)
2120 properties |= BT_HAL_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_READ;
2121 if (permissions & BT_HAL_GATT_PERMISSION_ENCRYPT_WRITE)
2122 properties |= BT_HAL_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_WRITE;
2123 if (permissions & BT_HAL_GATT_PERMISSION_ENCRYPT_AUTHENTICATED_WRITE)
2124 properties |= BT_HAL_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_WRITE;
2126 flag_count = bt_hal_gatt_convert_prop2string(properties, char_flags);
2128 char_info = g_new0(struct gatt_char_info, 1);
2130 char_info->char_path = g_strdup(path);
2131 char_info->char_id = object_id;
2132 char_info->char_uuid = __bt_hal_convert_uuid_to_string(uuid);//g_strdup(btuuid2str(uuid->uu));
2133 for (i = 0; i < flag_count; i++)
2134 char_info->char_flags[i] = char_flags[i];
2137 char_info->flags_length = flag_count;
2138 char_info->char_handle = gatt_char_handle;
2141 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
2142 inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
2144 g_variant_builder_add(inner_builder, "{sv}", "UUID",
2145 g_variant_new("s", char_info->char_uuid));
2146 g_variant_builder_add(inner_builder, "{sv}", "Service",
2147 g_variant_new("o", serv_info->serv_path));
2149 builder2 = g_variant_builder_new(G_VARIANT_TYPE("as"));
2151 for (i = 0; i < flag_count; i++)
2152 g_variant_builder_add(builder2, "s", char_flags[i]);
2154 flags_val = g_variant_new("as", builder2);
2155 g_variant_builder_add(inner_builder, "{sv}", "Flags",
2158 builder3 = g_variant_builder_new(G_VARIANT_TYPE("ao"));
2160 g_variant_builder_add(inner_builder, "{sv}", "Descriptors",
2161 g_variant_new("ao", builder3));
2163 g_variant_builder_add(builder, "{sa{sv}}",
2164 GATT_CHAR_INTERFACE,
2167 g_dbus_connection_emit_signal(g_conn, NULL, "/",
2168 "org.freedesktop.Dbus.ObjectManager",
2170 g_variant_new("(oa{sa{sv}})",
2174 //*char_path = g_strdup(path);
2179 /* Send Service handle to application */
2180 user_data = g_malloc0(sizeof(hal_gatt_char_added));
2181 user_data->srvc_hdl = serv_info->service_handle;
2182 user_data->char_hdl = gatt_char_handle;
2183 user_data->instance_data = slot;
2184 memcpy(user_data->uuid.uu, uuid->uu, sizeof(uuid->uu));
2185 g_idle_add(__bt_hal_gatt_char_added_cb, (gpointer)user_data);
2187 /* Save newly created charatcristic to GATT Server's service's characteristic list */
2188 serv_info->char_data = g_slist_append(serv_info->char_data, char_info);
2193 g_variant_builder_unref(inner_builder);
2194 g_variant_builder_unref(builder);
2195 g_variant_builder_unref(builder2);
2196 g_variant_builder_unref(builder3);
2198 return BT_STATUS_SUCCESS;
2201 static bt_status_t gatt_server_add_descriptor(int slot, int service_handle, bt_uuid_t *uuid,
2204 CHECK_BTGATT_INIT();
2206 // static int desc_id = 1;
2207 GError *error = NULL;
2209 GDBusNodeInfo *node_info;
2211 GVariantBuilder *builder = NULL;
2212 GVariantBuilder *inner_builder = NULL;
2214 struct gatt_char_info *char_info = NULL;
2215 struct gatt_desc_info *desc_info = NULL;
2216 struct gatt_service_info *serv_info = NULL;
2218 gchar **line_argv = NULL;
2220 char *char_path = NULL;
2223 GVariantBuilder *builder2 = NULL;
2224 GVariant *flags_val = NULL;
2226 char *desc_flags[NUMBER_OF_FLAGS];
2230 hal_gatt_desc_added *user_data = NULL;
2237 /* Fetch service data for the GATT server */
2238 serv_info = __bt_gatt_find_gatt_service_info(slot, service_handle);
2239 if (serv_info == NULL)
2240 return BT_STATUS_FAIL;
2242 /* Fetch list of characteristics from the service info */
2243 l = serv_info->char_data;
2245 /* Fetch last char info from the characteristic list */
2246 char_info = g_slist_last(l)->data;
2247 if (char_info == NULL)
2248 return BT_STATUS_FAIL;
2250 /* Fetch characteristic path from char info */
2251 char_path = char_info->char_path;
2253 line_argv = g_strsplit_set(char_path, "/", 0);
2254 serv_path = g_strdup_printf("/%s/%s/%s", line_argv[1], line_argv[2], line_argv[3]);
2257 node_info = __bt_gatt_create_method_node_info(
2258 descriptor_introspection_xml);
2260 if (node_info == NULL) {
2261 g_strfreev(line_argv);
2263 return BT_STATUS_FAIL;
2266 DBG("Add new Descriptor: Current GATT desc handle [%d]", gatt_desc_handle);
2268 path = g_strdup_printf("%s/descriptor%d", char_path, ++gatt_desc_handle);
2269 DBG("gatt descriptor path is [%s]", path);
2271 app_id = g_malloc0(sizeof(int));
2274 object_id = g_dbus_connection_register_object(g_conn, path,
2275 node_info->interfaces[0],
2276 &desc_interface_vtable,
2277 (gpointer)app_id, NULL, &error);
2279 if (object_id == 0) {
2280 ERR("failed to register: %s", error->message);
2281 g_error_free(error);
2283 g_strfreev(line_argv);
2286 return BT_STATUS_FAIL;
2289 flag_count = bt_hal_gatt_convert_perm2string(permissions, desc_flags);
2291 desc_info = g_new0(struct gatt_desc_info, 1);
2293 desc_info->desc_path = g_strdup(path);
2294 desc_info->desc_id = object_id;
2295 desc_info->desc_uuid = __bt_hal_convert_uuid_to_string(uuid);//g_strdup(btuuid2str(uuid->uu));
2297 for (i = 0; i < flag_count; i++)
2298 desc_info->desc_flags[i] = desc_flags[i];
2300 desc_info->flags_length = flag_count;
2301 desc_info->desc_handle = gatt_desc_handle;
2304 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
2305 inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
2307 g_variant_builder_add(inner_builder, "{sv}", "UUID",
2308 g_variant_new("s", btuuid2str(uuid->uu)));
2309 g_variant_builder_add(inner_builder, "{sv}", "Characteristic",
2310 g_variant_new("o", char_path));
2312 builder2 = g_variant_builder_new(G_VARIANT_TYPE("as"));
2314 for (i = 0; i < flag_count; i++)
2315 g_variant_builder_add(builder2, "s", desc_flags[i]);
2317 flags_val = g_variant_new("as", builder2);
2318 g_variant_builder_add(inner_builder, "{sv}", "Flags",
2321 g_variant_builder_add(builder, "{sa{sv}}",
2322 GATT_DESC_INTERFACE,
2325 g_dbus_connection_emit_signal(g_conn, NULL, "/",
2326 "org.freedesktop.Dbus.ObjectManager",
2328 g_variant_new("(oa{sa{sv}})",
2332 //*desc_path = g_strdup(path);
2334 /* Save newly created descriptor to GATT server's service's characteristic */
2335 char_info->desc_data = g_slist_append(char_info->desc_data, desc_info);
2337 /* Send descriptor handle to application */
2338 user_data = g_malloc0(sizeof(hal_gatt_desc_added));
2339 user_data->srvc_hdl = serv_info->service_handle;
2340 user_data->desc_hdl = gatt_desc_handle;
2341 user_data->instance_data = slot;
2342 memcpy(user_data->uuid.uu, uuid->uu, sizeof(uuid->uu));
2343 g_idle_add(__bt_hal_gatt_desc_added_cb, (gpointer)user_data);
2348 g_strfreev(line_argv);
2349 g_variant_builder_unref(inner_builder);
2350 g_variant_builder_unref(builder);
2351 return BT_STATUS_SUCCESS;
2354 static void __bt_hal_register_application_cb(GObject *object, GAsyncResult *res, gpointer user_data)
2356 GError *error = NULL;
2358 char *data = (char*) user_data;
2359 INFO("RegisterApplication is completed path [%s]", data);
2361 result = g_dbus_proxy_call_finish(manager_gproxy, res, &error);
2363 if (result == NULL) {
2364 /* dBUS-RPC is failed */
2365 ERR("Dbus-RPC is failed\n");
2367 if (error != NULL) {
2368 /* dBUS gives error cause */
2369 ERR("D-Bus API failure: errCode[%x], message[%s]\n",
2370 error->code, error->message);
2371 g_clear_error(&error);
2374 g_variant_unref(result);
2379 static GDBusProxy *__bt_hal_gatt_gdbus_init_manager_proxy(const gchar *service,
2380 const gchar *path, const gchar *interface)
2386 g_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM,
2391 ERR("Unable to connect to gdbus: %s", err->message);
2392 g_clear_error(&err);
2397 proxy = g_dbus_proxy_new_sync(g_conn,
2398 G_DBUS_PROXY_FLAGS_NONE, NULL,
2400 interface, NULL, &err);
2404 ERR("Unable to create proxy: %s", err->message);
2405 g_clear_error(&err);
2409 manager_gproxy = proxy;
2414 static GDBusProxy *__bt_gatt_gdbus_get_gatt_manager_proxy(const gchar *service,
2415 const gchar *path, const gchar *interface)
2417 return (manager_gproxy) ? manager_gproxy :
2418 __bt_hal_gatt_gdbus_init_manager_proxy(service,
2422 static void __bt_register_application_to_dbus(int slot)
2424 GDBusProxy *proxy = NULL;
2425 char *app_path = NULL;
2427 DBG("RegisterApplication slot [%d]", slot);
2429 /* It is impossible that app path is still not initialized */
2430 _bt_hal_is_gatt_server_initialzed(slot, &app_path);
2432 proxy = __bt_gatt_gdbus_get_gatt_manager_proxy("org.bluez",
2433 "/org/bluez/hci0", "org.bluez.GattManager1");
2435 data = g_strdup(app_path);
2436 g_dbus_proxy_call(proxy,
2437 "RegisterApplication",
2438 g_variant_new("(oa{sv})",
2440 G_DBUS_CALL_FLAGS_NONE, -1,
2442 (GAsyncReadyCallback)__bt_hal_register_application_cb,
2444 INFO("GATT server started");
2447 static bt_status_t gatt_server_start_service(int server_if, int service_handle, int transport)
2449 CHECK_BTGATT_INIT();
2450 struct gatt_service_info *serv_info = NULL;
2451 hal_gatt_service_started *user_data = NULL;
2452 DBG("Start GATT Service svc hdl [%d], slot [%d]", service_handle, server_if);
2454 /* Fetch service data for the GATT server */
2455 serv_info = __bt_gatt_find_gatt_service_info(server_if, service_handle);
2456 if (serv_info == NULL)
2457 return BT_STATUS_FAIL;
2459 if (serv_info->is_svc_registered)
2460 DBG("service already registered \n");
2462 serv_info->is_svc_registered = TRUE;
2464 /* Send Service handle to application */
2465 user_data = g_malloc0(sizeof(hal_gatt_service_started));
2466 user_data->srvc_hdl = serv_info->service_handle;
2467 user_data->instance_data = server_if;
2468 g_idle_add(__bt_hal_gatt_service_started_cb, (gpointer)user_data);
2470 /* If this is nth Service that is started, then register application at this point */
2471 if (__bt_is_service_last_in_server_list(server_if, service_handle)) {
2472 DBG("This is the last service started from the GATT Server's list of services handle [%d]",
2474 __bt_register_application_to_dbus(server_if);
2477 return BT_STATUS_SUCCESS;
2480 static bt_status_t gatt_server_stop_service(int server_if, int service_handle)
2482 CHECK_BTGATT_INIT();
2483 INFO("Stop service successful");
2484 return BT_STATUS_SUCCESS;
2487 static bt_status_t gatt_server_delete_service(int server_if, int service_handle)
2489 CHECK_BTGATT_INIT();
2490 struct gatt_service_info *serv_info = NULL;
2491 hal_gatt_service_deleted *user_data = NULL;
2494 int err = BT_STATUS_SUCCESS;
2495 int ret = BT_STATUS_SUCCESS;
2496 INFO("Slot [%d] service handle [%d]", server_if, service_handle);
2498 /* Fetch service data for the GATT server */
2499 serv_info = __bt_gatt_find_gatt_service_info(server_if, service_handle);
2500 if (serv_info == NULL) {
2501 ERR("Could not find service info svc handle [%d] server slot [%d]",
2502 service_handle, server_if);
2503 return BT_STATUS_FAIL;
2506 if (serv_info->is_svc_registered == FALSE) {
2507 ERR("service Not registered path [%s] handle [%d]",
2508 serv_info->serv_path, service_handle);
2511 for (l = serv_info->char_data; l != NULL; l = g_slist_next(l)) {
2512 struct gatt_char_info *char_info = l->data;
2514 if (char_info == NULL)
2517 for (l1 = char_info->desc_data; l1 != NULL; l1 = g_slist_next(l1)) {
2518 struct gatt_desc_info *desc_info = l1->data;
2520 if (desc_info == NULL)
2523 ret = g_dbus_connection_unregister_object(g_conn,
2524 desc_info->desc_id);
2526 __bt_hal_gatt_emit_interface_removed(
2527 desc_info->desc_path,
2528 GATT_DESC_INTERFACE);
2530 err = BT_STATUS_FAIL;
2533 /* list remove & free */
2534 char_info->desc_data = g_slist_remove(char_info->desc_data, desc_info);
2535 __bt_hal_gatt_free_descriptor_info(desc_info);
2538 g_slist_free(char_info->desc_data);
2539 char_info->desc_data = NULL;
2541 ret = g_dbus_connection_unregister_object(g_conn,
2542 char_info->char_id);
2544 __bt_hal_gatt_emit_interface_removed(char_info->char_path,
2545 GATT_CHAR_INTERFACE);
2548 err = BT_STATUS_FAIL;
2551 /* list remove & free */
2552 serv_info->char_data = g_slist_remove(serv_info->char_data, char_info);
2553 __bt_hal_gatt_free_characteristic_info(char_info);
2556 g_slist_free(serv_info->char_data);
2557 serv_info->char_data = NULL;
2559 ret = g_dbus_connection_unregister_object(g_conn, serv_info->serv_id);
2561 __bt_hal_gatt_emit_interface_removed(serv_info->serv_path,
2562 GATT_SERV_INTERFACE);
2565 err = BT_STATUS_FAIL;
2568 ret = g_dbus_connection_unregister_object(g_conn, serv_info->prop_id);
2570 DBG("Unregistered the service on properties interface");
2572 /* Remove from global list */
2573 gatt_services = g_slist_remove(gatt_services, serv_info);
2574 INFO("After removing from global list total service dount [%d]", g_slist_length(gatt_services));
2576 /* Remove from GATT Server's list of services */
2577 _bt_remote_service_from_gatt_server(server_if, service_handle);
2579 if (gatt_services == NULL)
2580 INFO("All GATT Services of all GATT Servers are unregistered");
2582 if (err == BT_STATUS_SUCCESS) {
2583 INFO("Send GATT Service deleted Event");
2584 /* Send Service handle to application */
2585 user_data = g_malloc0(sizeof(hal_gatt_service_deleted));
2586 user_data->srvc_hdl = serv_info->service_handle;
2587 user_data->instance_data = server_if;
2588 g_idle_add(__bt_hal_gatt_service_deleted_cb, (gpointer)user_data);
2591 /* Free the service */
2592 __bt_hal_gatt_free_service_info(serv_info);
2596 static gboolean __bt_gatt_get_service_state(const char *service_path)
2598 struct gatt_service_info *svc_info = NULL;
2601 for (l = gatt_services; l; l = g_slist_next(l)) {
2603 svc_info = (struct gatt_service_info *)l->data;
2604 if (svc_info->serv_path == service_path) {
2605 DBG("Return the state of the gatt service %d",
2606 svc_info->is_svc_registered);
2607 return svc_info->is_svc_registered;
2611 DBG("gatt service info is NULL");
2615 static bt_status_t gatt_server_send_indication(int server_if, int attribute_handle, int conn_id,
2616 int len, int confirm, char* p_value)
2618 CHECK_BTGATT_INIT();
2621 GVariantBuilder *outer_builder;
2622 GVariantBuilder *invalidated_builder;
2624 /* For Value update via PropertyChange */
2625 GVariantBuilder *outer_builder1;
2626 GVariantBuilder *inner_builder1;
2627 GVariantBuilder *invalidated_builder1;
2628 GVariant *update_value = NULL;
2630 /* Other variables */
2631 struct gatt_client_info_t *conn_info = NULL;
2632 gchar *serv_path = NULL;
2633 char *char_path = NULL;
2634 gchar **line_argv = NULL;
2635 gboolean notify = TRUE;
2636 gboolean ret = TRUE;
2637 int err = BT_STATUS_SUCCESS;
2639 GError *error = NULL;
2642 memset(addr, 0x00, sizeof(addr));
2644 conn_info = __bt_find_remote_gatt_client_info_from_conn(conn_id);
2645 if (conn_info == NULL) {
2646 ERR("No Connection Inforamtion!!!");
2647 return BT_STATUS_FAIL;
2650 DBG("Send Indication to GATT client addr [%s]", conn_info->addr);
2652 char_path = __bt_gatt_find_char_path_from_handle(attribute_handle);
2653 if (char_path == NULL)
2654 return BT_STATUS_FAIL;
2656 line_argv = g_strsplit_set(char_path, "/", 0);
2657 serv_path = g_strdup_printf("/%s/%s/%s", line_argv[1], line_argv[2], line_argv[3]);
2659 if (!__bt_gatt_get_service_state(serv_path)) {
2660 DBG("service not registered for this characteristic \n");
2662 g_strfreev(line_argv);
2663 return BT_STATUS_FAIL;
2666 outer_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
2667 invalidated_builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
2669 g_variant_builder_add(outer_builder, "{sv}", "Notifying",
2670 g_variant_new("b", notify));
2672 _bt_hal_convert_addr_type_to_string(addr, (unsigned char *)conn_info->addr);
2674 g_variant_builder_add(outer_builder, "{sv}", "Unicast",
2675 g_variant_new("s", addr));
2677 DBG("Set characteristic Notification \n");
2678 ret = g_dbus_connection_emit_signal(g_conn, NULL,
2680 "org.freedesktop.DBus.Properties",
2681 "PropertiesChanged",
2682 g_variant_new("(sa{sv}as)",
2683 "org.bluez.GattCharacteristic1",
2684 outer_builder, invalidated_builder),
2688 if (error != NULL) {
2689 ERR("D-Bus API failure: errCode[%x], \
2691 error->code, error->message);
2692 g_clear_error(&error);
2694 err = BT_STATUS_FAIL;
2697 g_variant_builder_unref(outer_builder);
2698 g_variant_builder_unref(invalidated_builder);
2701 /* Notifying Done, now update Value to Bluez via PropertyChanged */
2702 invalidated_builder1 = g_variant_builder_new(G_VARIANT_TYPE("as"));
2704 inner_builder1 = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
2705 for (i = 0; i < len; i++)
2706 g_variant_builder_add(inner_builder1, "y", p_value[i]);
2708 update_value = g_variant_new("ay", inner_builder1);
2711 outer_builder1 = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
2712 g_variant_builder_add(outer_builder1, "{sv}", "Value",
2715 DBG("Updating characteristic value \n");
2716 ret = g_dbus_connection_emit_signal(g_conn, NULL,
2718 "org.freedesktop.DBus.Properties",
2719 "PropertiesChanged",
2720 g_variant_new("(sa{sv}as)",
2721 "org.bluez.GattCharacteristic1",
2722 outer_builder1, invalidated_builder1),
2726 if (error != NULL) {
2727 ERR("D-Bus API failure: errCode[%x], \
2729 error->code, error->message);
2730 g_clear_error(&error);
2732 err = BT_STATUS_FAIL;
2734 struct gatt_char_info *char_info = NULL;
2736 char_info = __bt_gatt_find_char_info_from_handle(attribute_handle);
2737 if (char_info == NULL) {
2739 g_strfreev(line_argv);
2740 g_variant_builder_unref(inner_builder1);
2741 g_variant_builder_unref(outer_builder1);
2742 g_variant_builder_unref(invalidated_builder1);
2744 return BT_STATUS_FAIL;
2747 char_info->value_length = len;
2749 char_info->char_value = (char *)realloc(char_info->char_value, len);
2750 if (char_info->char_value) {
2751 for (i = 0; i < len; i++)
2752 char_info->char_value[i] = p_value[i];
2757 g_strfreev(line_argv);
2758 g_variant_builder_unref(inner_builder1);
2759 g_variant_builder_unref(outer_builder1);
2760 g_variant_builder_unref(invalidated_builder1);
2765 static bt_status_t gatt_server_send_response(int conn_id, int trans_id,
2766 int status, btgatt_response_t *response)
2768 CHECK_BTGATT_INIT();
2770 struct gatt_req_info *req_info = NULL;
2771 struct gatt_client_info_t *conn_info = NULL;
2774 DBG("GATT Server Send Response Conn ID [%d]", conn_id);
2776 conn_info = __bt_find_remote_gatt_client_info_from_conn(conn_id);
2777 if (conn_info == NULL) {
2778 ERR("No Connection Inforamtion!!!");
2779 return BT_STATUS_FAIL;
2782 req_info = __bt_find_remote_gatt_client_request_info(conn_id, trans_id);
2783 if (req_info == NULL) {
2784 ERR("No Request Inforamtion!!!");
2785 return BT_STATUS_FAIL;
2788 if (status != BT_STATUS_SUCCESS) {
2789 ERR("resp_state is 0x%X", status);
2791 g_dbus_method_invocation_return_dbus_error(req_info->context,
2792 "org.bluez.Error.Failed", "Application Error");
2794 conn_info->gatt_req_info_list = g_slist_remove(conn_info->gatt_req_info_list, req_info);
2796 req_info->context = NULL;
2797 if (req_info->attr_path)
2798 g_free(req_info->attr_path);
2799 if (req_info->svc_path)
2800 g_free(req_info->svc_path);
2803 return BT_STATUS_SUCCESS;
2806 DBG("Reponse Value length [%d]", response->attr_value.len);
2808 for (i = 0; i < response->attr_value.len; i++)
2809 DBG("Resonse [%d] = [0x%x]", response->attr_value.value[i]);
2812 if (req_info->request_type == BT_HAL_GATT_REQUEST_TYPE_READ) {
2813 GVariantBuilder *inner_builder = NULL;
2814 inner_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
2816 if (response->attr_value.len > 0) {
2817 for (i = 0; i < response->attr_value.len; i++)
2818 g_variant_builder_add(inner_builder, "y", response->attr_value.value[i]);
2820 g_dbus_method_invocation_return_value(req_info->context,
2821 g_variant_new("(ay)", inner_builder));
2823 g_variant_builder_unref(inner_builder);
2825 g_dbus_method_invocation_return_value(req_info->context, NULL);
2827 conn_info->gatt_req_info_list = g_slist_remove(conn_info->gatt_req_info_list, req_info);
2829 req_info->context = NULL;
2830 if (req_info->attr_path)
2831 g_free(req_info->attr_path);
2832 if (req_info->svc_path)
2833 g_free(req_info->svc_path);
2836 return BT_STATUS_SUCCESS;
2839 static bt_status_t gatt_server_update_att_value(int server_if, int attribute_handle,
2840 int value_length, char* att_value)
2842 CHECK_BTGATT_INIT();
2844 /* Other variables */
2845 char *char_path = NULL;
2846 gboolean ret = TRUE;
2847 GError *error = NULL;
2849 GVariantBuilder *outer_builder;
2850 GVariantBuilder *inner_builder;
2851 GVariantBuilder *invalidated_builder;
2852 GVariant *update_value = NULL;
2853 int err = BT_STATUS_SUCCESS;
2855 gchar **line_argv = NULL;
2856 gchar *serv_path = NULL;
2858 char_path = __bt_gatt_find_char_path_from_handle(attribute_handle);
2859 if (char_path == NULL)
2860 return BT_STATUS_FAIL;
2862 line_argv = g_strsplit_set(char_path, "/", 0);
2863 serv_path = g_strdup_printf("/%s/%s/%s", line_argv[1], line_argv[2], line_argv[3]);
2865 if (!__bt_gatt_get_service_state(serv_path)) {
2866 DBG("service not registered for this characteristic \n");
2868 g_strfreev(line_argv);
2869 return BT_STATUS_FAIL;
2873 line_argv = g_strsplit_set(char_path, "/", 0);
2874 serv_path = g_strdup_printf("/%s/%s/%s", line_argv[1], line_argv[2], line_argv[3]);
2876 if (!__bt_gatt_get_service_state(serv_path)) {
2877 DBG("service not registered for this characteristic \n");
2879 g_strfreev(line_argv);
2880 return BT_STATUS_FAIL;
2883 outer_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
2884 invalidated_builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
2886 inner_builder = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
2887 for (i = 0; i < value_length; i++)
2888 g_variant_builder_add(inner_builder, "y", att_value[i]);
2890 update_value = g_variant_new("ay", inner_builder);
2892 outer_builder = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
2893 g_variant_builder_add(outer_builder, "{sv}", "Value",
2896 DBG("Updating characteristic value \n");
2897 ret = g_dbus_connection_emit_signal(g_conn, NULL,
2899 "org.freedesktop.DBus.Properties",
2900 "PropertiesChanged",
2901 g_variant_new("(sa{sv}as)",
2902 "org.bluez.GattCharacteristic1",
2903 outer_builder, invalidated_builder),
2907 if (error != NULL) {
2908 ERR("D-Bus API failure: errCode[%x], \
2910 error->code, error->message);
2911 g_clear_error(&error);
2913 err = BT_STATUS_FAIL;
2915 struct gatt_char_info *char_info = NULL;
2917 char_info = __bt_gatt_find_char_info_from_handle(attribute_handle);
2918 if (char_info == NULL) {
2920 g_strfreev(line_argv);
2921 g_variant_builder_unref(inner_builder);
2922 g_variant_builder_unref(outer_builder);
2923 g_variant_builder_unref(invalidated_builder);
2925 return BT_STATUS_FAIL;
2928 char_info->value_length = value_length;
2930 char_info->char_value = (char *)realloc(char_info->char_value, value_length);
2931 if (char_info->char_value) {
2932 for (i = 0; i < value_length; i++)
2933 char_info->char_value[i] = att_value[i];
2939 g_strfreev(line_argv);
2940 g_variant_builder_unref(inner_builder);
2941 g_variant_builder_unref(outer_builder);
2942 g_variant_builder_unref(invalidated_builder);
2947 static bt_status_t gatt_server_listen(int server_if, bool start)
2949 CHECK_BTGATT_INIT();
2950 /* Send Data to LE Module */
2951 return _bt_hal_enable_advertising(server_if, start, FALSE);
2954 static bt_status_t gatt_server_set_adv_data(int server_if, bool set_scan_rsp, bool include_name,
2955 bool include_txpower, int min_interval, int max_interval, int appearance,
2956 uint16_t manufacturer_len, char* manufacturer_data,
2957 uint16_t service_data_len, char* service_data,
2958 uint16_t service_uuid_len, char* service_uuid)
2960 CHECK_BTGATT_INIT();
2961 return BT_STATUS_SUCCESS;
2964 static bt_status_t gatt_server_multi_adv_enable(int server_if)
2966 CHECK_BTGATT_INIT();
2967 /* Send Enable Advertising request to LE Module */
2968 return _bt_hal_enable_advertising(server_if, TRUE, TRUE);
2971 static bt_status_t gatt_server_multi_adv_update(int server_if, int min_interval, int max_interval, int adv_type,
2972 int chnl_map, int tx_power, int timeout_s)
2974 CHECK_BTGATT_INIT();
2976 /* Send Advertising parameters to LE Module */
2977 return _bt_hal_set_advertising_params(server_if, min_interval, max_interval, adv_type,
2978 chnl_map, tx_power, timeout_s);
2981 static bt_status_t gatt_server_multi_adv_set_inst_data(btgatt_adv_param_setup_t adv_param_setup)
2983 CHECK_BTGATT_INIT();
2985 /* Send Data to LE Module */
2986 return _bt_hal_set_advertising_data(adv_param_setup);
2989 static bt_status_t gatt_server_multi_adv_disable(int server_if)
2991 CHECK_BTGATT_INIT();
2992 /* Send Data to LE Module */
2993 return _bt_hal_enable_advertising(server_if, FALSE, TRUE);
2996 static bt_status_t gatt_server_get_mtu_size(int conn_id, int *mtu_size)
2998 CHECK_BTGATT_INIT();
2999 char *object_path = NULL;
3001 GDBusProxy *device_proxy;
3002 GError *error = NULL;
3004 GVariant *tmp_value;
3005 GDBusConnection *conn;
3006 GVariant *result = NULL;
3007 int ret = BT_STATUS_SUCCESS;
3008 struct gatt_client_info_t *conn_info = NULL;
3011 if (mtu_size == NULL)
3012 return BT_STATUS_PARM_INVALID;
3014 /* GDBUS Connection Info validate */
3015 conn = _bt_hal_get_system_gconn();
3017 ERR("Could not get System DBUS Connection");
3018 return BT_STATUS_FAIL;
3021 /* Connection Info validate */
3022 conn_info = __bt_find_remote_gatt_client_info_from_conn(conn_id);
3023 if (conn_info == NULL) {
3024 ERR("No Connection Inforamtion!!!");
3025 return BT_STATUS_FAIL;
3029 object_path = _bt_hal_get_device_object_path(conn_info->addr);
3030 if (object_path == NULL)
3031 return BT_STATUS_FAIL;
3033 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3034 NULL, BT_HAL_BLUEZ_NAME, object_path,
3035 BT_HAL_PROPERTIES_INTERFACE, NULL, NULL);
3037 g_free(object_path);
3038 if (device_proxy == NULL)
3039 return BT_STATUS_FAIL;
3041 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
3042 g_variant_new("(s)", BT_HAL_DEVICE_INTERFACE),
3043 G_DBUS_CALL_FLAGS_NONE,
3047 if (result == NULL) {
3048 if (error != NULL) {
3049 ERR("Error occured in Proxy call [%s]\n", error->message);
3050 g_error_free(error);
3052 g_object_unref(device_proxy);
3053 return BT_STATUS_FAIL;
3056 g_variant_get(result , "(@a{sv})", &value);
3057 g_variant_unref(result);
3059 tmp_value = g_variant_lookup_value(value, "AttMtu", G_VARIANT_TYPE_UINT16);
3060 if (tmp_value == NULL) {
3061 g_object_unref(device_proxy);
3062 g_variant_unref(value);
3063 return BT_STATUS_FAIL;
3066 mtu = g_variant_get_uint16(tmp_value);
3068 DBG("ATT MTU : [%d]", mtu);
3070 g_variant_unref(tmp_value);
3071 g_variant_unref(value);
3072 g_object_unref(device_proxy);
3074 *mtu_size = (int) mtu;
3079 const btgatt_server_interface_t btgatt_server_interface = {
3080 gatt_server_register_app,
3081 gatt_server_unregister_app,
3084 gatt_server_add_service,
3085 gatt_server_add_included_service,
3086 gatt_server_add_characteristic,
3087 gatt_server_add_descriptor,
3088 gatt_server_start_service,
3089 gatt_server_stop_service,
3090 gatt_server_delete_service,
3091 gatt_server_send_indication,
3092 gatt_server_send_response,
3093 gatt_server_update_att_value,
3095 gatt_server_set_adv_data,
3096 gatt_server_multi_adv_enable,
3097 gatt_server_multi_adv_update,
3098 gatt_server_multi_adv_set_inst_data,
3099 gatt_server_multi_adv_disable,
3100 gatt_server_get_mtu_size