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_HAL_GATT_REQUEST_TYPE_ACQUIRE_WRITE = 0x03, /* Exec Write Requested */
115 BT_HAL_GATT_REQUEST_TYPE_ACQUIRE_NOTIFY = 0x04, /* Exec Write Requested */
116 } bt_gatt_request_type_e;
118 struct gatt_req_info {
123 bt_gatt_request_type_e request_type; /* Read or Write request */
124 GDBusMethodInvocation *context;
127 struct gatt_server_app {
133 /* Linked List of gatt server app's */
134 static GSList *gatt_server_apps = NULL;
136 static GSList *gatt_services = NULL;
138 static int conn_id = 0;
140 /* Linked List of connected Remote GATT clients */
141 static GSList *gatt_client_info_list = NULL;
143 /* GATT Client Info List Structure */
144 struct gatt_client_info_t {
145 int connection_id; /* This value will uniquely identify a GATT client-server connection */
146 int instance_id; /* This value unique identifies a GATT server instance */
147 char *addr; /* Remote GATT client address */
148 GSList *gatt_req_info_list; /* List of transactions per Connection*/
151 static handle_stack_msg event_cb = NULL;
154 uint32_t instance_data;
156 } hal_register_server_data;
159 uint32_t instance_data;
163 } hal_gatt_service_added;
166 uint32_t instance_data;
168 } hal_gatt_service_started;
171 uint32_t instance_data;
173 } hal_gatt_service_deleted;
176 uint32_t instance_data;
180 } hal_gatt_char_added;
183 uint32_t instance_data;
187 } hal_gatt_desc_added;
189 #define CHECK_BTGATT_INIT() if (bt_gatt_callbacks == NULL)\
191 ERR("%s: BTGATT not initialized", __FUNCTION__);\
192 return BT_STATUS_NOT_READY;\
194 DBG("%s", __FUNCTION__);\
197 static gboolean __bt_hal_gatt_service_added_cb(gpointer user_data);
199 static void __bt_hal_gatt_deinit(char *app_path);
201 static void __bt_hal_register_application_cb(GObject *object,
202 GAsyncResult *res, gpointer user_data);
204 static void __bt_hal_unregister_application_cb(GObject *object, GAsyncResult *res,
207 static GDBusProxy *__bt_gatt_gdbus_get_gatt_manager_proxy(const gchar *service,
208 const gchar *path, const gchar *interface);
210 /* Introspection data for the service we are exporting */
211 static const gchar service_introspection_xml[] =
213 " <interface name='org.freedesktop.DBus.Properties'>"
214 " <property type='s' name='UUID' access='read'>"
216 " <property type='b' name='primary' access='read'>"
218 " <property type='o' name='Device' access='read'>"
220 " <property type='ao' name='Characteristics' access='read'>"
222 " <property type='s' name='Includes' access='read'>"
227 /* Introspection data for the characteristics we are exporting */
228 static const gchar characteristics_introspection_xml[] =
230 " <interface name='org.bluez.GattCharacteristic1'>"
231 " <method name='ReadValue'>"
232 " <arg type='s' name='address' direction='in'/>"
233 " <arg type='y' name='id' direction='in'/>"
234 " <arg type='q' name='offset' direction='in'/>"
235 " <arg type='ay' name='Value' direction='out'/>"
237 " <method name='WriteValue'>"
238 " <arg type='s' name='address' direction='in'/>"
239 " <arg type='u' name='id' direction='in'/>"
240 " <arg type='q' name='offset' direction='in'/>"
241 " <arg type='b' name='response_needed' direction='in'/>"
242 " <arg type='ay' name='value' direction='in'/>"
244 " <method name ='AcquireWrite'>"
245 " <arg type='a{sv}' name='properties' direction='in'/>"
246 " <arg type='h' name='fd' direction='out'/>"
247 " <arg type='q' name='mtu' direction='out'/>"
249 " <method name = 'AcquireNotify'>"
250 " <arg type='a{sv}' name='properties' direction='in'/>"
251 " <arg type='h' name='fd' direction='out'/>"
252 " <arg type='q' name='mtu' direction='out'/>"
254 " <method name='StartNotify'>"
256 " <method name='StopNotify'>"
258 " <method name='IndicateConfirm'>"
259 " <arg type='s' name='address' direction='in'/>"
260 " <arg type='b' name='complete' direction='in'/>"
263 " <interface name='org.freedesktop.DBus.Properties'>"
264 " <property type='s' name='UUID' access='read'>"
266 " <property type='o' name='Service' access='read'>"
268 " <property type='ay' name='Value' access='readwrite'>"
270 " <property type='b' name='Notifying' access='read'>"
272 " <property type='as' name='Flags' access='read'>"
274 " <property type='s' name='Unicast' access='read'>"
276 " <property type='ao' name='Descriptors' access='read'>"
278 " <property type='b' name='WriteAcquired' access='read'>"
280 " <property type='b' name='NotifyAcquired' access='read'>"
285 /* Introspection data for the descriptor we are exporting */
286 static const gchar descriptor_introspection_xml[] =
288 " <interface name='org.bluez.GattDescriptor1'>"
289 " <method name='ReadValue'>"
290 " <arg type='s' name='address' direction='in'/>"
291 " <arg type='u' name='id' direction='in'/>"
292 " <arg type='q' name='offset' direction='in'/>"
293 " <arg type='ay' name='Value' direction='out'/>"
295 " <method name='WriteValue'>"
296 " <arg type='s' name='address' direction='in'/>"
297 " <arg type='u' name='id' direction='in'/>"
298 " <arg type='q' name='offset' direction='in'/>"
299 " <arg type='b' name='response_needed' direction='in'/>"
300 " <arg type='ay' name='value' direction='in'/>"
303 " <interface name='org.freedesktop.DBus.Properties'>"
304 " <property type='s' name='UUID' access='read'>"
306 " <property type='o' name='Characteristic' access='read'>"
308 " <property type='ay' name='Value' access='read'>"
310 " <property type='as' name='Flags' access='read'>"
316 static const gchar manager_introspection_xml[] =
318 " <interface name='org.freedesktop.DBus.ObjectManager'>"
319 " <method name='GetManagedObjects'>"
320 " <arg type='a{oa{sa{sv}}}' name='object_paths_interfaces_and_properties' direction='out'/>"
325 GSList *_bt_get_service_list_from_server(int instance)
328 INFO("Number of GATT Server apps [%d]", g_slist_length(gatt_server_apps));
329 INFO("Find App with slot [%d]", instance);
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 INFO("App slot [%d] Found, Number of services registered [%d]",
336 app->slot, g_slist_length(app->services));
337 return app->services;
343 void _bt_remote_service_from_gatt_server(int instance, int service_handle)
348 for (l = gatt_server_apps; l; l = g_slist_next(l)) {
349 struct gatt_server_app *app = (struct gatt_server_app *)l->data;
351 if (app->slot == instance) {
352 for (l1 = app->services; l1; l1 = g_slist_next(l1)) {
353 struct gatt_service_info *srv = (struct gatt_service_info *)l1->data;
354 if (srv->service_handle == service_handle) {
355 app->services = g_slist_remove(app->services, srv);
363 void _bt_hal_update_gatt_service_in_gatt_server(int slot, struct gatt_service_info *serv_info)
366 for (l = gatt_server_apps; l; l = g_slist_next(l)) {
367 struct gatt_server_app *app = (struct gatt_server_app *)l->data;
369 if (app->slot == slot) {
370 INFO("Updating service in GATT server's service list service handle [%d] slot [%d]",
371 serv_info->service_handle, slot);
372 app->services = g_slist_append(app->services, serv_info);
377 static struct gatt_client_info_t *__bt_find_remote_gatt_client_info(char *address)
380 struct gatt_client_info_t *info = NULL;
382 for (l = gatt_client_info_list; l != NULL; l = g_slist_next(l)) {
383 info = (struct gatt_client_info_t*)l->data;
387 if (!g_strcmp0(info->addr, address)) {
388 INFO("Remote GATT client found addr[%s]", info->addr);
395 static struct gatt_client_info_t *__bt_find_remote_gatt_client_info_from_conn(int conn_id)
398 struct gatt_client_info_t *info = NULL;
400 for (l = gatt_client_info_list; l != NULL; l = g_slist_next(l)) {
401 info = (struct gatt_client_info_t*)l->data;
405 if (info->connection_id == conn_id) {
406 INFO("Remote GATT client found addr[%s]", info->addr);
413 static struct gatt_req_info *__bt_find_remote_gatt_client_request_info(int conn_id, int trans_id)
418 struct gatt_client_info_t *info = NULL;
419 struct gatt_req_info *info1 = NULL;
421 for (l = gatt_client_info_list; l != NULL; l = g_slist_next(l)) {
422 info = (struct gatt_client_info_t*)l->data;
426 if (info->connection_id == conn_id) {
428 for (l1 = info->gatt_req_info_list; l1; l1 = g_slist_next(l1)) {
430 info1 = (struct gatt_req_info*)l1->data;
434 if (info1->request_id == trans_id) {
435 INFO("Remote GATT client found addr[%s]", info->addr);
444 void _bt_hal_gatt_connected_state_event(gboolean is_connected, char *address)
446 struct hal_ev_gatt_server_connected ev;
447 struct gatt_client_info_t *conn_info = NULL;
449 memset(&ev, 0, sizeof(ev));
451 /* Find Server Instance */
452 _bt_hal_get_gatt_server_instance_initialized(&instance);
453 if (instance == -1) {
454 ERR("Not even a single GATT server is registered");
458 /* Convert address to hex */
459 _bt_hal_convert_addr_string_to_type(ev.bdaddr, address);
461 /* Create Connection ID */
462 /* Check if device is already in connected list */
463 conn_info = __bt_find_remote_gatt_client_info(address);
466 /* If disconnected, and conn info found, then remove conn info */
467 if (is_connected == FALSE) {
468 DBG("GATT Disconnected");
470 INFO("Remove GATT client info from List..");
471 /* Remove info from List */
472 gatt_client_info_list = g_slist_remove(gatt_client_info_list, conn_info);
473 INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
476 ERR("GATT callback not registered");
478 DBG("GATT callback registered: server if [%d] Is Connected [%d] addr[%s] conn ID [%d]",
479 conn_info->instance_id, is_connected, conn_info->addr, conn_info->connection_id);
480 ev.conn_id = conn_info->connection_id;
481 ev.server_instance = conn_info->instance_id;
482 ev.connected = is_connected;
484 event_cb(HAL_EV_GATT_SERVER_CONNECTED, (void *)&ev, sizeof(ev));
486 g_free(conn_info->addr);
489 /* If connected, and conn info NOT found, then add conn info */
492 /* Save Connection info */
493 conn_info = g_new0(struct gatt_client_info_t, 1);
494 conn_info->addr = g_strdup(address);
495 INFO("Added GATT client addr[%s]", conn_info->addr);
496 conn_info->connection_id = ++conn_id;
497 conn_info->instance_id = instance;
498 gatt_client_info_list = g_slist_append(gatt_client_info_list, conn_info);
499 INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
502 ERR("GATT callback not registered");
504 DBG("GATT callback registered: server if [%d] Is Connected [%d] addr[%s] conn ID [%d]",
505 conn_info->instance_id, is_connected, conn_info->addr, conn_info->connection_id);
506 ev.conn_id = conn_info->connection_id;
507 ev.server_instance = conn_info->instance_id;
508 ev.connected = is_connected;
510 event_cb(HAL_EV_GATT_SERVER_CONNECTED, (void *)&ev, sizeof(ev));
514 /* Send GATT connected or disconnected event */
517 static struct gatt_service_info *__bt_gatt_find_gatt_service_from_char(const char *char_path, int *char_hdl)
521 for (l1 = gatt_services; l1 != NULL; l1 = l1->next) {
522 struct gatt_service_info *serv_info = l1->data;
524 for (l2 = serv_info->char_data; l2 != NULL; l2 = l2->next) {
525 struct gatt_char_info *char_info = l2->data;
527 if (g_strcmp0(char_info->char_path, char_path)
529 *char_hdl = char_info->char_handle;
534 ERR("Gatt service not found");
538 char *__bt_gatt_find_char_path_from_handle(int char_hdl)
542 for (l1 = gatt_services; l1 != NULL; l1 = l1->next) {
543 struct gatt_service_info *serv_info = l1->data;
545 for (l2 = serv_info->char_data; l2 != NULL; l2 = l2->next) {
546 struct gatt_char_info *char_info = l2->data;
548 if (char_info->char_handle == char_hdl)
549 return char_info->char_path;
556 struct gatt_char_info *__bt_gatt_find_char_info_from_handle(int char_hdl)
560 for (l1 = gatt_services; l1 != NULL; l1 = l1->next) {
561 struct gatt_service_info *serv_info = l1->data;
563 for (l2 = serv_info->char_data; l2 != NULL; l2 = l2->next) {
564 struct gatt_char_info *char_info = l2->data;
566 if (char_info->char_handle == char_hdl)
574 static struct gatt_service_info *__bt_gatt_find_gatt_service_from_desc(const char *desc_path, int *desc_hdl)
576 GSList *l1, *l2, *l3;
578 for (l1 = gatt_services; l1 != NULL; l1 = l1->next) {
579 struct gatt_service_info *serv_info = l1->data;
581 for (l2 = serv_info->char_data; l2 != NULL; l2 = l2->next) {
582 struct gatt_char_info *char_info = l2->data;
584 for (l3 = char_info->desc_data; l3 != NULL; l3 = l3->next) {
585 struct gatt_desc_info *desc_info = l3->data;
587 if (g_strcmp0(desc_info->desc_path, desc_path)
589 *desc_hdl = desc_info->desc_handle;
595 ERR("Gatt service not found");
599 static void __bt_gatt_manager_method_call(GDBusConnection *connection,
601 const gchar *object_path,
602 const gchar *interface_name,
603 const gchar *method_name,
604 GVariant *parameters,
605 GDBusMethodInvocation *invocation,
610 if (g_strcmp0(method_name, "GetManagedObjects") == 0) {
611 GVariantBuilder *builder;
612 GVariantBuilder *inner_builder1 = NULL;
613 GVariant *svc_char = NULL;
615 GSList *gatt_services = NULL;
617 instance = (int*)user_data;
618 gboolean writeAcquired = FALSE;
619 gboolean notifyAcquired = FALSE;
621 DBG("Getting values for service, chars and descriptors");
622 DBG("GATT Server App for which services are requested [%d]", *instance);
625 builder = g_variant_builder_new(
626 G_VARIANT_TYPE("a{oa{sa{sv}}}"));
629 gatt_services = _bt_get_service_list_from_server(*instance);
631 if (g_slist_length(gatt_services) == 0) {
632 ERR("No registered GATT services!!!!");
633 g_dbus_method_invocation_return_value(invocation, NULL);
634 g_variant_builder_unref(builder);
638 for (l = gatt_services; l != NULL; l = l->next) {
639 GVariantBuilder *svc_builder = NULL;
640 GVariantBuilder *inner_builder = NULL;
641 struct gatt_service_info *serv_info = l->data;
642 INFO("GATT Service fetched handle [%d]", serv_info->service_handle);
644 /* Prepare inner builder for GattService1 interface */
645 DBG("Creating builder for service");
646 svc_builder = g_variant_builder_new(
647 G_VARIANT_TYPE("a{sa{sv}}"));
648 inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
650 g_variant_builder_add(inner_builder, "{sv}", "UUID",
651 g_variant_new_string(serv_info->service_uuid));
653 g_variant_builder_add(inner_builder, "{sv}", "Primary",
654 g_variant_new_boolean(serv_info->is_svc_primary));
657 inner_builder1 = g_variant_builder_new(G_VARIANT_TYPE("ao"));
658 DBG("Adding Charatarisitcs list");
659 for (l4 = serv_info->char_data; l4 != NULL; l4 = l4->next) {
660 struct gatt_char_info *char_info = l4->data;
661 INFO("GATT Char handle [%d] found in GATT service handle [%d]",
662 char_info->char_handle, serv_info->service_handle);
663 g_variant_builder_add(inner_builder1, "o",
664 char_info->char_path);
665 DBG("%s", char_info->char_path);
668 svc_char = g_variant_new("ao", inner_builder1);
669 g_variant_builder_add(inner_builder, "{sv}", "Characteristics",
672 g_variant_builder_add(svc_builder, "{sa{sv}}",
676 g_variant_builder_add(builder, "{oa{sa{sv}}}",
677 serv_info->serv_path,
680 g_variant_builder_unref(inner_builder1);
682 /* Prepare inner builder for GattCharacteristic1 interface */
684 GSList *l2 = serv_info->char_data;
685 DBG("Creating builder for characteristics \n");
688 DBG("characteristic data is NULL");
690 for (l2 = serv_info->char_data; l2 != NULL; l2 = l2->next) {
692 GVariantBuilder *char_builder = NULL;
693 GVariantBuilder *inner_builder = NULL;
694 GVariantBuilder *builder1 = NULL;
695 GVariantBuilder *builder2 = NULL;
696 GVariantBuilder *builder3 = NULL;
697 GVariant *char_val = NULL;
698 GVariant *flags_val = NULL;
699 GVariant *char_desc = NULL;
700 char *unicast = NULL;
701 gboolean notify = FALSE;
704 char_builder = g_variant_builder_new(
707 inner_builder = g_variant_builder_new(
711 struct gatt_char_info *char_info = l2->data;
712 if (char_info == NULL) {
713 ERR("char_info is NULL");
718 g_variant_builder_add(inner_builder, "{sv}", "UUID",
719 g_variant_new_string(char_info->char_uuid));
721 g_variant_builder_add(inner_builder, "{sv}", "Service",
722 g_variant_new("o", serv_info->serv_path));
724 builder1 = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
726 if (char_info->char_value != NULL) {
727 for (i = 0; i < char_info->value_length; i++) {
728 g_variant_builder_add(builder1, "y",
729 char_info->char_value[i]);
731 char_val = g_variant_new("ay", builder1);
732 g_variant_builder_add(inner_builder, "{sv}",
736 builder2 = g_variant_builder_new(G_VARIANT_TYPE("as"));
737 for (i = 0; i < char_info->flags_length; i++) {
738 g_variant_builder_add(builder2, "s",
739 char_info->char_flags[i]);
740 if (strncmp(char_info->char_flags[i], "write-without-response", 16) == 0)
741 writeAcquired = TRUE;
743 if (strncmp(char_info->char_flags[i], "notify", 4) == 0)
744 notifyAcquired = TRUE;
747 flags_val = g_variant_new("as", builder2);
748 g_variant_builder_add(inner_builder, "{sv}", "Flags",
752 g_variant_builder_add(inner_builder, "{sv}", "Notifying",
753 g_variant_new("b", notify));
756 g_variant_builder_add(inner_builder, "{sv}", "WriteAcquired",
757 g_variant_new("b", writeAcquired));
760 g_variant_builder_add(inner_builder, "{sv}", "NotifyAcquired",
761 g_variant_new("b", notifyAcquired));
764 unicast = g_strdup("00:00:00:00:00:00");
765 g_variant_builder_add(inner_builder, "{sv}", "Unicast",
766 g_variant_new("s", unicast));
769 builder3 = g_variant_builder_new(G_VARIANT_TYPE("ao"));
770 DBG("Adding Descriptors list");
772 for (l4 = char_info->desc_data; l4 != NULL; l4 = l4->next) {
773 struct gatt_desc_info *desc_info = l4->data;
774 INFO("GATT Descriptor handle [%d] found inside GATT Char handle [%d] found in GATT service handle [%d]",
775 desc_info->desc_handle, char_info->char_handle, serv_info->service_handle);
776 g_variant_builder_add(builder3, "o",
777 desc_info->desc_path);
778 DBG("%s", desc_info->desc_path);
781 char_desc = g_variant_new("ao", builder3);
782 g_variant_builder_add(inner_builder, "{sv}", "Descriptors",
785 g_variant_builder_add(char_builder, "{sa{sv}}",
786 GATT_CHAR_INTERFACE , inner_builder);
787 g_variant_builder_add(builder, "{oa{sa{sv}}}",
788 char_info->char_path, char_builder);
790 /*Prepare inner builder for GattDescriptor1 interface*/
792 GSList *l3 = char_info->desc_data;
795 DBG("descriptor data is NULL");
797 for (l3 = char_info->desc_data; l3 != NULL; l3 = l3->next) {
799 DBG("Creating builder for descriptor \n");
801 GVariantBuilder *desc_builder = NULL;
802 GVariantBuilder *inner_builder = NULL;
803 GVariantBuilder *builder1 = NULL;
804 GVariantBuilder *builder2 = NULL;
805 GVariant *desc_val = NULL;
807 desc_builder = g_variant_builder_new(
810 inner_builder = g_variant_builder_new(
814 struct gatt_desc_info *desc_info = l3->data;
815 if (desc_info == NULL) {
816 ERR("desc_info is NULL");
821 g_variant_builder_add(inner_builder,
823 g_variant_new_string(
824 desc_info->desc_uuid));
827 g_variant_builder_add(inner_builder, "{sv}",
830 char_info->char_path));
833 builder1 = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
835 if (desc_info->desc_value != NULL) {
836 for (i = 0; i < desc_info->value_length; i++) {
837 g_variant_builder_add(builder1, "y",
838 desc_info->desc_value[i]);
840 desc_val = g_variant_new("ay", builder1);
841 g_variant_builder_add(inner_builder, "{sv}",
846 builder2 = g_variant_builder_new(G_VARIANT_TYPE("as"));
848 for (i = 0; i < desc_info->flags_length; i++) {
849 g_variant_builder_add(builder2, "s",
850 desc_info->desc_flags[i]);
853 flags_val = g_variant_new("as", builder2);
854 g_variant_builder_add(inner_builder, "{sv}", "Flags",
857 g_variant_builder_add(desc_builder, "{sa{sv}}",
861 g_variant_builder_add(builder, "{oa{sa{sv}}}",
862 desc_info->desc_path,
865 /*unref descriptor builder pointers*/
866 g_variant_builder_unref(builder1);
867 g_variant_builder_unref(builder2);
868 g_variant_builder_unref(inner_builder);
869 g_variant_builder_unref(desc_builder);
874 /*unref char builder pointers*/
875 g_variant_builder_unref(builder1);
876 g_variant_builder_unref(builder2);
877 g_variant_builder_unref(builder3);
878 g_variant_builder_unref(inner_builder);
879 g_variant_builder_unref(char_builder);
882 /*unref service builder pointers*/
883 g_variant_builder_unref(inner_builder);
884 g_variant_builder_unref(svc_builder);
887 /* Return builder as method reply */
888 DBG("Sending gatt service builder values to Bluez");
889 g_dbus_method_invocation_return_value(invocation,
899 static void __bt_gatt_desc_method_call(GDBusConnection *connection,
901 const gchar *object_path,
902 const gchar *interface_name,
903 const gchar *method_name,
904 GVariant *parameters,
905 GDBusMethodInvocation *invocation,
909 if (g_strcmp0(method_name, "ReadValue") == 0) {
910 struct hal_ev_gatt_server_read_req ev;
915 struct gatt_client_info_t *conn_info = NULL;
916 struct gatt_req_info *req_info = NULL;
917 struct gatt_service_info *svc_info = NULL;
921 DBG("Application path = %s", object_path);
922 DBG("Sender = %s", sender);
924 svc_info = __bt_gatt_find_gatt_service_from_desc(object_path, &desc_hdl);
925 if (svc_info == NULL) {
926 ERR("Coudn't find service for %s", object_path);
927 g_dbus_method_invocation_return_value(invocation, NULL);
931 g_variant_get(parameters, "(&suq)", &addr, &req_id, &offset);
932 DBG("Request id = %u, Offset = %u", req_id, offset);
934 /* Check if device is already in connected list */
935 conn_info = __bt_find_remote_gatt_client_info(addr);
937 if (conn_info == NULL) {
938 ERR("Coudn't find Connection info for %s", addr);
939 g_dbus_method_invocation_return_value(invocation, NULL);
944 ERR("GATT callback NOT registered");
945 g_dbus_method_invocation_return_value(invocation, NULL);
949 /* Store requests information */
950 req_info = g_new0(struct gatt_req_info, 1);
951 req_info->attr_path = g_strdup(object_path);
952 req_info->svc_path = g_strdup(svc_info->serv_path);
953 req_info->request_id = req_id;
954 req_info->offset = offset;
955 req_info->context = invocation;
957 /* Append request info in list of requests for the particular connection */
958 conn_info->gatt_req_info_list = g_slist_append(conn_info->gatt_req_info_list, req_info);
961 memset(&ev, 0, sizeof(ev));
962 ev.conn_id = conn_info->connection_id;
963 ev.trans_id = req_id;
964 ev.att_handle = desc_hdl;
966 ev.is_long = false; /* TODO*/
968 /* Convert address to hex */
969 _bt_hal_convert_addr_string_to_type(ev.bdaddr, addr);
971 event_cb(HAL_EV_GATT_READ_REQUESTED, (void *)&ev, sizeof(ev));
973 } else if (g_strcmp0(method_name, "WriteValue") == 0) {
975 GVariant *var = NULL;
979 gboolean response_needed = FALSE;
980 struct hal_ev_gatt_server_write_req ev;
984 struct gatt_service_info *svc_info = NULL;
985 struct gatt_client_info_t *conn_info = NULL;
986 struct gatt_req_info *req_info = NULL;
988 memset(&ev, 0, sizeof(ev));
991 DBG("Application path = %s", object_path);
992 DBG("Sender = %s", sender);
994 g_variant_get(parameters, "(&suqb@ay)",
995 &addr, &req_id, &offset, &response_needed, &var);
996 DBG("Request id = %u, Offset = %u", req_id, offset);
998 /* Check if device is already in connected list */
999 conn_info = __bt_find_remote_gatt_client_info(addr);
1001 svc_info = __bt_gatt_find_gatt_service_from_desc(object_path, &desc_hdl);
1003 if (conn_info == NULL || svc_info == NULL || event_cb == NULL) {
1004 g_variant_unref(var);
1005 if (response_needed)
1006 g_dbus_method_invocation_return_value(invocation, NULL);
1008 g_object_unref(invocation);
1012 len = g_variant_get_size(var);
1013 if (len > 0 && len < 600) {
1015 data = (char *)g_variant_get_data(var);
1016 memcpy(ev.value, data, len);
1019 if (response_needed) {
1020 /* Store request information */
1021 req_info = g_new0(struct gatt_req_info, 1);
1022 req_info->attr_path = g_strdup(object_path);
1023 req_info->svc_path = g_strdup(svc_info->serv_path);
1024 req_info->request_id = req_id;
1025 req_info->offset = offset;
1026 req_info->context = invocation;
1028 /* Append request info in list of requests for the particular connection */
1029 conn_info->gatt_req_info_list = g_slist_append(conn_info->gatt_req_info_list, req_info);
1031 g_object_unref(invocation);
1034 /* Send HAL event */
1035 ev.conn_id = conn_info->connection_id;
1036 ev.trans_id = req_id;
1037 ev.att_handle = desc_hdl;
1039 ev.need_rsp = response_needed;
1042 /* Convert address to hex */
1043 _bt_hal_convert_addr_string_to_type(ev.bdaddr, addr);
1045 event_cb(HAL_EV_GATT_WRITE_REQUESTED, (void *)&ev, sizeof(ev));
1047 g_variant_unref(var);
1052 static void __bt_gatt_char_method_call(GDBusConnection *connection,
1053 const gchar *sender,
1054 const gchar *object_path,
1055 const gchar *interface_name,
1056 const gchar *method_name,
1057 GVariant *parameters,
1058 GDBusMethodInvocation *invocation,
1061 if (g_strcmp0(method_name, "ReadValue") == 0) {
1065 struct hal_ev_gatt_server_read_req ev;
1068 struct gatt_req_info *req_info = NULL;
1069 struct gatt_client_info_t *conn_info = NULL;
1070 struct gatt_service_info *svc_info = NULL;
1072 DBG("Application path = %s", object_path);
1073 DBG("Sender = %s", sender);
1075 /* Check if device is already in connected list */
1076 conn_info = __bt_find_remote_gatt_client_info(addr);
1078 svc_info = __bt_gatt_find_gatt_service_from_char(object_path, &char_hdl);
1080 if (svc_info == NULL || conn_info == NULL) {
1081 g_dbus_method_invocation_return_value(invocation, NULL);
1086 ERR("GATT callback NOT registered");
1087 g_dbus_method_invocation_return_value(invocation, NULL);
1091 g_variant_get(parameters, "(&suq)", &addr, &req_id, &offset);
1092 DBG("Request id = %u, Offset = %u", req_id, offset);
1094 /* Store requets information */
1095 req_info = g_new0(struct gatt_req_info, 1);
1096 req_info->attr_path = g_strdup(object_path);
1097 req_info->svc_path = g_strdup(svc_info->serv_path);
1098 req_info->request_id = req_id;
1099 req_info->request_type = BT_HAL_GATT_REQUEST_TYPE_READ;
1100 req_info->offset = offset;
1101 req_info->context = invocation;
1103 /* Append request info in list of requests for the particular connection */
1104 conn_info->gatt_req_info_list = g_slist_append(conn_info->gatt_req_info_list, req_info);
1106 /* Send HAL event */
1107 memset(&ev, 0, sizeof(ev));
1108 ev.conn_id = conn_info->connection_id;
1109 ev.trans_id = req_id;
1110 ev.att_handle = char_hdl;
1112 ev.is_long = false; /* TODO*/
1114 /* Convert address to hex */
1115 _bt_hal_convert_addr_string_to_type(ev.bdaddr, addr);
1117 event_cb(HAL_EV_GATT_READ_REQUESTED, (void *)&ev, sizeof(ev));
1119 } else if (g_strcmp0(method_name, "WriteValue") == 0) {
1120 GVariant *var = NULL;
1124 gboolean response_needed = FALSE;
1125 struct hal_ev_gatt_server_write_req ev;
1129 struct gatt_service_info *svc_info = NULL;
1130 struct gatt_req_info *req_info = NULL;
1131 struct gatt_client_info_t *conn_info = NULL;
1133 memset(&ev, 0, sizeof(ev));
1136 DBG("Application path = %s", object_path);
1137 DBG("Sender = %s", sender);
1139 g_variant_get(parameters, "(&suqb@ay)",
1140 &addr, &req_id, &offset, &response_needed, &var);
1141 DBG("Request id = %u, Offset = %u", req_id, offset);
1143 svc_info = __bt_gatt_find_gatt_service_from_char(object_path, &char_hdl);
1145 /* Check if device is already in connected list */
1146 conn_info = __bt_find_remote_gatt_client_info(addr);
1148 if (svc_info == NULL || conn_info == NULL || event_cb == NULL) {
1149 g_variant_unref(var);
1150 if (response_needed)
1151 g_dbus_method_invocation_return_value(invocation, NULL);
1153 g_object_unref(invocation);
1157 len = g_variant_get_size(var);
1158 if (len > 0 && len < 600) {
1160 data = (char *)g_variant_get_data(var);
1161 memcpy(ev.value, data, len);
1165 if (response_needed) {
1166 /* Store requets information */
1167 req_info = g_new0(struct gatt_req_info, 1);
1168 req_info->attr_path = g_strdup(object_path);
1169 req_info->svc_path = g_strdup(svc_info->serv_path);
1170 req_info->request_id = req_id;
1171 req_info->request_type = BT_HAL_GATT_REQUEST_TYPE_WRITE;
1172 req_info->offset = offset;
1173 req_info->context = invocation;
1175 /* Append request info in list of requests for the particular connection */
1176 conn_info->gatt_req_info_list = g_slist_append(conn_info->gatt_req_info_list, req_info);
1179 g_object_unref(invocation);
1182 /* Send HAL event */
1183 ev.conn_id = conn_info->connection_id;
1184 ev.trans_id = req_id;
1185 ev.att_handle = char_hdl;
1187 ev.need_rsp = response_needed;
1190 /* Convert address to hex */
1191 _bt_hal_convert_addr_string_to_type(ev.bdaddr, addr);
1193 event_cb(HAL_EV_GATT_WRITE_REQUESTED, (void *)&ev, sizeof(ev));
1195 g_variant_unref(var);
1198 } else if (g_strcmp0(method_name, "StartNotify") == 0) {
1201 struct gatt_service_info *svc_info = NULL;
1202 struct hal_ev_gatt_server_notifcation_change ev;
1205 svc_info = __bt_gatt_find_gatt_service_from_char(object_path, &char_hdl);
1206 if (svc_info == NULL || event_cb == NULL)
1209 /* Send HAL event */
1210 memset(&ev, 0, sizeof(ev));
1211 ev.conn_id = -1; /*TODO Bluez does not provide remote GATT client address, so no conn_id */
1212 ev.trans_id = -1; /*TODO Bluez does not provide request id or transacion ID */
1213 ev.att_handle = char_hdl;
1216 /* Convert address to hex */
1217 _bt_hal_convert_addr_string_to_type(ev.bdaddr, "00:00:00:00:00"); /* TODO Bluez Does not provide address of GATT client */
1219 event_cb(HAL_EV_GATT_NOTIFICATION_CHANGE, (void *)&ev, sizeof(ev));
1222 } else if (g_strcmp0(method_name, "StopNotify") == 0) {
1225 struct gatt_service_info *svc_info = NULL;
1226 struct hal_ev_gatt_server_notifcation_change ev;
1229 svc_info = __bt_gatt_find_gatt_service_from_char(object_path, &char_hdl);
1230 if (svc_info == NULL || event_cb == NULL)
1233 /* Send HAL event */
1234 memset(&ev, 0, sizeof(ev));
1235 ev.conn_id = -1; /*TODO Bluez does not provide remote GATT client address, so no conn_id */
1236 ev.trans_id = -1; /*TODO Bluez does not provide request id or transacion ID */
1237 ev.att_handle = char_hdl;
1240 /* Convert address to hex */
1241 _bt_hal_convert_addr_string_to_type(ev.bdaddr, "00:00:00:00:00"); /* TODO Bluez DOes not provide address of GATT client */
1243 event_cb(HAL_EV_GATT_NOTIFICATION_CHANGE, (void *)&ev, sizeof(ev));
1246 } else if (g_strcmp0(method_name, "IndicateConfirm") == 0) {
1248 gboolean complete = FALSE;
1251 struct gatt_service_info *svc_info = NULL;
1252 struct gatt_client_info_t *conn_info = NULL;
1254 struct hal_ev_gatt_server_indicate_cfm ev;
1256 DBG("IndicateConfirm");
1257 DBG("Application path = %s", object_path);
1258 DBG("Sender = %s", sender);
1260 g_variant_get(parameters, "(&sb)", &addr, &complete);
1261 DBG("Remote Device address number = %s", addr);
1262 DBG("Is Indicate confirmation for last device [%d]", complete);
1264 /* Check if device is already in connected list */
1265 conn_info = __bt_find_remote_gatt_client_info(addr);
1267 svc_info = __bt_gatt_find_gatt_service_from_char(object_path, &char_hdl);
1269 if (svc_info == NULL || conn_info == NULL
1270 || event_cb == NULL) {
1274 /* Send HAL event */
1275 memset(&ev, 0, sizeof(ev));
1276 ev.conn_id = conn_info->connection_id;
1277 ev.trans_id = -1; /*TODO Bluez does not provide Transaction ID or request ID */
1278 ev.att_handle = char_hdl;
1280 /* Convert address to hex */
1281 _bt_hal_convert_addr_string_to_type(ev.bdaddr, addr);
1283 event_cb(HAL_EV_GATT_INDICATE_CFM, (void *)&ev, sizeof(ev));
1284 } else if (g_strcmp0(method_name, "AcquireWrite") == 0) {
1288 struct hal_ev_gatt_server_acquire_write_res ev;
1289 struct gatt_service_info *svc_info = NULL;
1290 struct gatt_req_info *req_info = NULL;
1291 struct gatt_client_info_t *conn_info = NULL;
1292 char * dev_path = NULL;
1294 char addr[BT_HAL_ADDRESS_STRING_SIZE];
1296 DBG("AcquireWrite");
1297 DBG("Application path = %s", object_path);
1298 DBG("Sender = %s", sender);
1301 g_variant_get(parameters, "(a{sv})", &iter);
1303 GVariant* value = NULL;
1304 while (g_variant_iter_loop(iter, "{sv}", &key, &value)) {
1305 if (g_strcmp0(key, "MTU") == 0)
1306 g_variant_get(value, "i", &mtu);
1307 else if (g_strcmp0(key, "link") == 0)
1308 g_variant_get(value, "s", &link);
1309 else if (g_strcmp0(key, "device") == 0)
1310 g_variant_get(value, "o", &dev_path);
1313 DBG("AcquireWrite values retived");
1314 DBG(" path %s LINK = %s, MTU = %u", addr, link, mtu);
1316 svc_info = __bt_gatt_find_gatt_service_from_char(object_path, &char_hdl);
1318 _bt_hal_convert_device_path_to_address(dev_path, addr);
1320 DBG("remote adress %s", addr);
1322 /* Check if device is already in connected list */
1323 conn_info = __bt_find_remote_gatt_client_info(addr);
1325 if (conn_info == NULL) {
1326 ERR("Cleint info not found\n");
1330 if (svc_info == NULL) {
1331 ERR("svc_info info not found\n");
1335 /* Store requets information */
1336 req_info = g_new0(struct gatt_req_info, 1);
1337 req_info->attr_path = g_strdup(object_path);
1338 req_info->svc_path = g_strdup(svc_info->serv_path);
1339 req_info->request_id = conn_info->connection_id;
1340 req_info->request_type = BT_HAL_GATT_REQUEST_TYPE_ACQUIRE_WRITE;
1341 req_info->offset = mtu;
1342 req_info->context = invocation;
1344 /* Append request info in list of requests for the particular connection */
1345 conn_info->gatt_req_info_list = g_slist_append(conn_info->gatt_req_info_list, req_info);
1347 /* Send HAL event */
1348 memset(&ev, 0, sizeof(ev));
1349 ev.conn_id = conn_info->connection_id;
1350 ev.char_handl = char_hdl;
1352 ev.trans_id = conn_info->connection_id;
1353 _bt_hal_convert_addr_string_to_type(ev.bdaddr, addr);
1354 event_cb(HAL_EV_GATT_SERVER_ACQUIRE_WRITE_RES, (void *)&ev, sizeof(ev));
1356 DBG("HAL_EV_GATT_ACQUIRE_WRITE_RES called");
1360 } else if (g_strcmp0(method_name, "AcquireNotify") == 0) {
1364 struct hal_ev_gatt_server_acquire_notify ev;
1365 struct gatt_service_info *svc_info = NULL;
1366 struct gatt_client_info_t *conn_info = NULL;
1367 struct gatt_req_info *req_info = NULL;
1371 DBG("AcquireNotify");
1372 DBG("Application path = %s", object_path);
1373 DBG("Sender = %s", sender);
1376 g_variant_get(parameters, "(a{sv})", &iter);
1378 GVariant* value = NULL;
1379 while (g_variant_iter_loop(iter, "{sv}", &key, &value)) {
1380 if (g_strcmp0(key, "MTU") == 0)
1381 g_variant_get(value, "q", &mtu);
1384 DBG("AcquireNotify values retived");
1385 DBG(" MTU = %u", mtu);
1387 svc_info = __bt_gatt_find_gatt_service_from_char(object_path, &char_hdl);
1389 if (svc_info == NULL) {
1390 ERR("svc_info info not found\n");
1394 /* Store requets information */
1395 req_info = g_new0(struct gatt_req_info, 1);
1396 req_info->attr_path = g_strdup(object_path);
1397 req_info->svc_path = g_strdup(svc_info->serv_path);
1398 req_info->request_id = 33;
1399 req_info->request_type = BT_HAL_GATT_REQUEST_TYPE_ACQUIRE_NOTIFY;
1400 req_info->offset = mtu;
1401 req_info->context = invocation;
1403 conn_info = g_new0(struct gatt_client_info_t, 1);
1404 conn_info->addr = g_strdup(object_path);
1405 INFO("Added GATT client path[%s]", conn_info->addr);
1406 conn_info->connection_id = 33;
1407 conn_info->instance_id = 33;
1408 /* Append request info in list of requests for the particular connection */
1409 conn_info->gatt_req_info_list = g_slist_append(conn_info->gatt_req_info_list, req_info);
1410 gatt_client_info_list = g_slist_append(gatt_client_info_list, conn_info);
1412 /* Send HAL event */
1413 memset(&ev, 0, sizeof(ev));
1415 ev.char_handl = char_hdl;
1419 event_cb(HAL_EV_GATT_SERVER_ACQUIRE_NOTIFY_RES, (void *)&ev, sizeof(ev));
1421 DBG("HAL_EV_GATT_ACQUIRE_NOTIFY_RES called");
1427 g_dbus_method_invocation_return_value(invocation, NULL);
1430 gboolean __bt_hal_gatt_emit_interface_removed(gchar *object_path, gchar *interface)
1433 GError *error = NULL;
1434 GVariantBuilder *array_builder;
1436 array_builder = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
1437 g_variant_builder_init(array_builder, G_VARIANT_TYPE("as"));
1438 g_variant_builder_add(array_builder, "s", interface);
1440 ret = g_dbus_connection_emit_signal(g_conn, NULL, "/",
1441 "org.freedesktop.Dbus.Objectmanager",
1442 "InterfacesRemoved",
1443 g_variant_new("(oas)",
1444 object_path, array_builder),
1448 if (error != NULL) {
1449 /* dbus gives error cause */
1450 ERR("d-bus api failure: errcode[%x], message[%s]",
1451 error->code, error->message);
1452 g_clear_error(&error);
1455 g_variant_builder_unref(array_builder);
1460 static void __bt_hal_gatt_free_descriptor_info(struct gatt_desc_info *desc_info)
1467 g_free(desc_info->desc_path);
1468 g_free(desc_info->desc_uuid);
1469 g_free(desc_info->desc_value);
1471 for (i = 0; i < desc_info->flags_length; i++)
1472 g_free(desc_info->desc_flags[i]);
1477 static void __bt_hal_gatt_free_characteristic_info(struct gatt_char_info *char_info)
1484 g_free(char_info->char_path);
1485 g_free(char_info->char_uuid);
1486 g_free(char_info->char_value);
1488 for (i = 0; i < char_info->flags_length; i++)
1489 g_free(char_info->char_flags[i]);
1495 static void __bt_hal_gatt_free_service_info(struct gatt_service_info *svc_info)
1500 g_free(svc_info->serv_path);
1501 g_free(svc_info->service_uuid);
1505 static const GDBusInterfaceVTable desc_interface_vtable = {
1506 __bt_gatt_desc_method_call,
1511 static const GDBusInterfaceVTable char_interface_vtable = {
1512 __bt_gatt_char_method_call,
1517 static const GDBusInterfaceVTable serv_interface_vtable = {
1523 static const GDBusInterfaceVTable manager_interface_vtable = {
1524 __bt_gatt_manager_method_call,
1530 static GDBusNodeInfo *__bt_gatt_create_method_node_info(
1531 const gchar *introspection_data)
1534 GDBusNodeInfo *node_info = NULL;
1536 if (introspection_data == NULL)
1540 DBG("Create new node info");
1541 node_info = g_dbus_node_info_new_for_xml(introspection_data, &err);
1544 ERR("Unable to create node: %s", err->message);
1545 g_clear_error(&err);
1552 /* To send stack event to hal-av handler */
1553 void _bt_hal_register_gatt_server_handler_cb(handle_stack_msg cb)
1558 void _bt_hal_unregister_gatt_server_handler_cb(void)
1563 static gboolean __bt_hal_gatt_desc_added_cb(gpointer user_data)
1565 struct hal_ev_gatt_desc_added ev;
1566 hal_gatt_desc_added *data = (hal_gatt_desc_added*) user_data;
1568 /* Prepare to GATT characteristic added event */
1569 memset(&ev, 0, sizeof(ev));
1570 ev.status = BT_STATUS_SUCCESS;
1571 ev.server_instance = data->instance_data;
1572 memcpy(ev.desc_uuid, data->uuid.uu, sizeof(data->uuid.uu));
1573 ev.service_handle = data->srvc_hdl;
1574 ev.desc_handle = data->desc_hdl;
1577 ERR("GATT Descriptor Added callback registered");
1579 DBG("GATT Descriptor Added: server if [%d] Service handle [%d] Descriptor Handle [%d]",
1580 data->instance_data, data->srvc_hdl, data->desc_hdl);
1582 event_cb(HAL_EV_GATT_DESC_ADDED, (void *)&ev, sizeof(ev));
1589 static gboolean __bt_hal_gatt_char_added_cb(gpointer user_data)
1591 struct hal_ev_gatt_char_added ev;
1592 hal_gatt_char_added *data = (hal_gatt_char_added*) user_data;
1594 /* Prepare to GATT characteristic added event */
1595 memset(&ev, 0, sizeof(ev));
1596 ev.status = BT_STATUS_SUCCESS;
1597 ev.server_instance = data->instance_data;
1598 memcpy(ev.char_uuid, data->uuid.uu, sizeof(data->uuid.uu));
1599 ev.service_handle = data->srvc_hdl;
1600 ev.char_handle = data->char_hdl;
1603 ERR("GATT Characteristic Added callback registered");
1605 DBG("GATT Characteristic Added: server if [%d] Service handle [%d] Characteristic handle [%d]",
1606 data->instance_data, data->srvc_hdl, data->char_hdl);
1608 event_cb(HAL_EV_GATT_CHAR_ADDED, (void *)&ev, sizeof(ev));
1615 static gboolean __bt_hal_gatt_service_added_cb(gpointer user_data)
1617 struct hal_ev_gatt_service_added ev;
1618 hal_gatt_service_added *data = (hal_gatt_service_added*) user_data;
1620 /* Prepare to GATT Service added event */
1621 memset(&ev, 0, sizeof(ev));
1622 ev.status = BT_STATUS_SUCCESS;
1623 ev.server_instance = data->instance_data;
1624 memcpy(ev.svc_uuid, data->uuid.uu, sizeof(data->uuid.uu));
1625 ev.service_handle = data->srvc_hdl;
1626 ev.is_primary = data->is_primary;
1629 ERR("GATT Service Added callback registered");
1631 DBG("GATT Service Added: server if [%d] Service handle [%d]",
1632 data->instance_data, data->srvc_hdl);
1633 event_cb(HAL_EV_GATT_SERVICE_ADDED, (void *)&ev, sizeof(ev));
1640 static gboolean __bt_hal_gatt_service_started_cb(gpointer user_data)
1642 struct hal_ev_gatt_service_started ev;
1643 hal_gatt_service_started *data = (hal_gatt_service_started*) user_data;
1645 /* Prepare to GATT Service added event */
1646 memset(&ev, 0, sizeof(ev));
1647 ev.status = BT_STATUS_SUCCESS;
1648 ev.server_instance = data->instance_data;
1649 ev.service_handle = data->srvc_hdl;
1652 ERR("GATT Service Started callback registered");
1654 DBG("GATT Service Started: server if [%d] Service handle [%d]",
1655 data->instance_data, data->srvc_hdl);
1656 event_cb(HAL_EV_GATT_SERVICE_STARTED, (void *)&ev, sizeof(ev));
1663 static gboolean __bt_hal_gatt_service_deleted_cb(gpointer user_data)
1665 struct hal_ev_gatt_service_deleted ev;
1666 hal_gatt_service_deleted *data = (hal_gatt_service_deleted*) user_data;
1668 /* Prepare to GATT Service added event */
1669 memset(&ev, 0, sizeof(ev));
1670 ev.status = BT_STATUS_SUCCESS;
1671 ev.server_instance = data->instance_data;
1672 ev.service_handle = data->srvc_hdl;
1675 ERR("GATT Service Deleted callback registered");
1677 DBG("GATT Service Deleted: server if [%d] Service handle [%d]",
1678 data->instance_data, data->srvc_hdl);
1679 event_cb(HAL_EV_GATT_SERVICE_DELETED, (void *)&ev, sizeof(ev));
1686 static gboolean __bt_hal_register_slot_id_cb(gpointer user_data)
1688 struct hal_ev_server_instance_registered ev;
1689 hal_register_server_data *data = (hal_register_server_data*) user_data;
1691 /* Prepare to send AV connecting event */
1692 memset(&ev, 0, sizeof(ev));
1693 ev.status = BT_STATUS_SUCCESS;
1694 ev.server_instance = data->instance_data;
1695 memcpy(ev.app_uuid, data->uuid.uu, sizeof(ev.app_uuid));
1698 ERR("GATT Register Server Instance Callback not registered");
1700 DBG("Server Instance is registered!! server if [%d]", data->instance_data);
1701 event_cb(HAL_EV_SERVER_INSTANCE_INITIALIZED, (void *)&ev, sizeof(ev));
1708 static bt_status_t gatt_server_register_app(bt_uuid_t *uuid)
1710 CHECK_BTGATT_INIT();
1711 int status = BT_STATUS_FAIL;
1713 DBG("Register server instance request");
1714 hal_register_server_data *user_data = g_malloc0(sizeof(hal_register_server_data));
1716 /* Check if slot available */
1717 server_if = _bt_hal_get_available_adv_slot_id(uuid);
1719 if (server_if == -1) {
1720 ERR("Allocation of server instance failed");
1724 user_data->instance_data = server_if;
1725 DBG("Allocated new Advertising slot with Stack [%d]", user_data->instance_data);
1729 * As we need to provide async callback to user from HAL, simply schedule a
1730 * callback method which will carry actual result
1732 memcpy(user_data->uuid.uu, uuid->uu, sizeof(bt_uuid_t));
1733 g_idle_add(__bt_hal_register_slot_id_cb, (gpointer)user_data);
1735 /* If available, then return success, else return error */
1736 return BT_STATUS_SUCCESS;
1739 void _bt_hal_remove_gatt_server_from_list(int server_if)
1742 struct gatt_server_app *info = NULL;
1744 for (l = gatt_server_apps; l != NULL;) {
1745 info = (struct gatt_server_app*)l->data;
1746 l = g_slist_next(l);
1749 if (info->slot == server_if) {
1750 INFO("Found Matching GATT Server in List path[%s] slot [%d]",
1751 info->app_path, info->slot);
1753 /* Only if all services are deleted from the GATT Server, then only Unregister it.
1754 Reason: it is possible, GATT Server app oly wants to disable multi advertising
1755 In above case, only advertising block will be deallocated, Gatt Server will remain
1757 if (info->services == NULL) {
1758 gatt_server_apps = g_slist_remove(gatt_server_apps, (gpointer)info);
1759 INFO("Total gatt server apps still existing after removing above is [%d]",
1760 g_slist_length(gatt_server_apps));
1762 /* DBUS Unregister only for current app */
1763 __bt_hal_gatt_deinit(info->app_path);
1765 g_free(info->app_path);
1769 INFO("GATT Server still has services count[%d] in it..Can not remove it!!!",
1770 g_slist_length(info->services));
1776 static bt_status_t gatt_server_unregister_app(int server_if)
1778 CHECK_BTGATT_INIT();
1779 DBG("Un-Register server instance request [%d]", server_if);
1781 if (_bt_hal_is_advertising_in_slot(server_if) == FALSE)
1782 _bt_hal_free_server_slot(server_if);
1784 /* If server_if belongs to a GATT Server, then delete the GATT server from List */
1785 _bt_hal_remove_gatt_server_from_list(server_if);
1786 return BT_STATUS_SUCCESS;
1789 static bt_status_t gatt_server_open(int server_if, const bt_bdaddr_t *bd_addr, bool is_direct)
1791 CHECK_BTGATT_INIT();
1792 return BT_STATUS_SUCCESS;
1795 static bt_status_t gatt_server_close(int server_if, const bt_bdaddr_t *bd_addr, int conn_id)
1797 CHECK_BTGATT_INIT();
1798 return BT_STATUS_SUCCESS;
1801 static void __bt_gatt_close_gdbus_connection(void)
1809 if (!g_dbus_connection_flush_sync(g_conn, NULL, &err)) {
1810 ERR("Fail to flush the connection: %s", err->message);
1815 if (!g_dbus_connection_close_sync(g_conn, NULL, &err)) {
1817 ERR("Fail to close the dbus connection: %s", err->message);
1822 g_object_unref(g_conn);
1827 static GDBusConnection *__bt_gatt_get_gdbus_connection(void)
1829 GDBusConnection *local_system_gconn = NULL;
1833 if (g_conn == NULL) {
1834 address = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
1835 if (address == NULL) {
1837 ERR("Failed to get bus address: %s", err->message);
1838 g_clear_error(&err);
1843 g_conn = g_dbus_connection_new_for_address_sync(address,
1844 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
1845 G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
1846 NULL, /* GDBusAuthObserver */
1851 ERR("Unable to connect to dbus: %s", err->message);
1852 g_clear_error(&err);
1856 } else if (g_dbus_connection_is_closed(g_conn)) {
1857 address = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
1858 if (address == NULL) {
1860 ERR("Failed to get bus address: %s", err->message);
1861 g_clear_error(&err);
1866 local_system_gconn = g_dbus_connection_new_for_address_sync(address,
1867 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
1868 G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
1869 NULL, /* GDBusAuthObserver */
1873 if (!local_system_gconn) {
1874 ERR("Unable to connect to dbus: %s", err->message);
1875 g_clear_error(&err);
1878 g_conn = local_system_gconn;
1884 static char* __bt_hal_convert_uuid_to_string(bt_uuid_t *srvc_id)
1889 uuid = btuuid2str(srvc_id->uu);
1890 DBG("Original UUID [%s]", uuid);
1892 if (_bt_hal_uuid_is_standard(srvc_id) == TRUE) {
1893 /* Extract Standard UUID string */
1894 memcpy(uuid_buf, &uuid[4], 4);
1896 DBG("Converted string [%s]", uuid_buf);
1897 return g_strdup(uuid_buf);
1899 return strdup(uuid);
1902 int __bt_hal_add_service_to_dbus(char *app_path, int slot, btgatt_srvc_id_t *srvc_id)
1905 /* For GATT service specific */
1906 GDBusNodeInfo *node_info;
1909 struct gatt_service_info *serv_info = NULL;
1910 GVariantBuilder *builder = NULL;
1911 GVariantBuilder *builder1 = NULL;
1912 GVariantBuilder *inner_builder = NULL;
1913 gboolean svc_primary = TRUE;
1914 GError *error = NULL;
1915 hal_gatt_service_added *user_data = NULL;
1916 DBG("Service add to DBUS slot [%d]", slot);
1918 node_info = __bt_gatt_create_method_node_info(
1919 service_introspection_xml);
1921 if (node_info == NULL)
1922 return BT_STATUS_FAIL;
1924 DBG("Add new GATT Service: Current GATT Service handle [%d]", gatt_service_handle);
1925 path = g_strdup_printf("%s"GATT_SERV_OBJECT_PATH"%d", app_path, ++gatt_service_handle);
1926 DBG("gatt service path is [%s]", path);
1928 object_id = g_dbus_connection_register_object(g_conn, path,
1929 node_info->interfaces[0],
1930 &serv_interface_vtable,
1931 NULL, NULL, &error);
1933 if (object_id == 0) {
1934 ERR("failed to register: %s", error->message);
1935 g_error_free(error);
1937 return BT_STATUS_FAIL;
1939 /* Add object_id/gatt service information; it's required at the time of
1940 * service unregister and Getmanagedobjects
1942 serv_info = g_new0(struct gatt_service_info, 1);
1944 serv_info->serv_path = g_strdup(path);
1945 serv_info->serv_id = object_id;
1946 serv_info->service_uuid = __bt_hal_convert_uuid_to_string(&srvc_id->id.uuid);//g_strdup(btuuid2str(srvc_id->id.uuid.uu));
1947 serv_info->is_svc_registered = FALSE;
1948 serv_info->is_svc_primary = svc_primary;
1949 DBG("Service Handle to be added is [%d]", gatt_service_handle);
1950 serv_info->service_handle = gatt_service_handle;
1952 /* Update service in GATT Server service List */
1953 gatt_services = g_slist_append(gatt_services, serv_info);
1955 /* emit interfacesadded signal here for service path */
1956 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
1957 inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1959 g_variant_builder_add(inner_builder, "{sv}",
1960 "UUID", g_variant_new_string(btuuid2str(srvc_id->id.uuid.uu)));
1962 g_variant_builder_add(inner_builder, "{sv}",
1963 "Primary", g_variant_new_boolean(svc_primary));
1965 builder1 = g_variant_builder_new(G_VARIANT_TYPE("ao"));
1967 g_variant_builder_add(inner_builder, "{sv}", "Characteristics",
1968 g_variant_new("ao", builder1));
1970 g_variant_builder_add(builder, "{sa{sv}}",
1971 GATT_SERV_INTERFACE, inner_builder);
1973 ret = g_dbus_connection_emit_signal(g_conn, NULL, "/",
1974 "org.freedesktop.Dbus.ObjectManager",
1976 g_variant_new("(oa{sa{sv}})",
1981 /* dbus gives error cause */
1982 ERR("d-bus api failure: errcode[%x], message[%s]",
1983 error->code, error->message);
1984 g_clear_error(&error);
1988 /* Send Service handle to application */
1989 user_data = g_malloc0(sizeof(hal_gatt_service_added));
1990 user_data->srvc_hdl = serv_info->service_handle;
1991 user_data->instance_data = slot;
1992 memcpy(user_data->uuid.uu, srvc_id->id.uuid.uu, sizeof(srvc_id->id.uuid.uu));
1993 g_idle_add(__bt_hal_gatt_service_added_cb, (gpointer)user_data);
1995 /* Save newly created service in GATT Server's service list */
1996 _bt_hal_update_gatt_service_in_gatt_server(slot, serv_info);
2000 g_variant_builder_unref(inner_builder);
2001 g_variant_builder_unref(builder);
2002 g_variant_builder_unref(builder1);
2003 return BT_STATUS_SUCCESS;
2006 static void __bt_hal_unregister_application_cb(GObject *object, GAsyncResult *res,
2009 char *app_path = (char*)user_data;
2010 INFO("UnregisterApplication is completed app [%s]", app_path);
2011 GError *error = NULL;
2012 GVariant *result = NULL;
2015 result = g_dbus_proxy_call_finish(manager_gproxy, res, &error);
2017 if (result == NULL) {
2018 /* dBUS-RPC is failed */
2019 ERR("Dbus-RPC is failed\n");
2021 if (error != NULL) {
2022 /* dBUS gives error cause */
2023 ERR("D-Bus API failure: errCode[%x], message[%s]\n",
2024 error->code, error->message);
2025 g_clear_error(&error);
2028 g_variant_unref(result);
2033 static void __bt_hal_gatt_deinit(char *app_path)
2035 GDBusProxy *proxy = NULL;
2039 /* Step1: Remove requested App */
2040 proxy = __bt_gatt_gdbus_get_gatt_manager_proxy("org.bluez",
2041 "/org/bluez/hci0", "org.bluez.GattManager1");
2046 INFO("UnregisterApplication : path [%s]", app_path);
2048 /* Async Call to Unregister Service */
2049 data = g_strdup(app_path);
2050 g_dbus_proxy_call(proxy,
2051 "UnregisterApplication",
2052 g_variant_new("(o)",
2054 G_DBUS_CALL_FLAGS_NONE, -1,
2056 (GAsyncReadyCallback)__bt_hal_unregister_application_cb,
2059 /* If requested app is last GATT Server app, then clean all resources */
2060 if (gatt_server_apps == NULL) {
2061 INFO("All GATT servers are removed, clean all DBUS resources");
2063 /* unregister the exported interface for object manager
2064 g_conn and manager_id are common for all GATT servers */
2065 g_dbus_connection_unregister_object(g_conn,
2068 g_bus_unown_name(owner_id);
2071 g_object_unref(manager_gproxy);
2072 manager_gproxy = NULL;
2074 /* Close the GDBUS connection */
2075 __bt_gatt_close_gdbus_connection();
2081 int __bt_hal_gatt_init(void)
2083 GDBusConnection *conn = NULL;
2085 /* Only once for ALL GATT Servers */
2086 if (owner_id == 0) {
2087 owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
2088 BT_GATT_SERVICE_NAME,
2089 G_BUS_NAME_OWNER_FLAGS_NONE,
2090 NULL, NULL, NULL, NULL, NULL);
2092 INFO("Owner ID [%d]", owner_id);
2094 /* Only once for ALL GATT Servers conn = g_conn(global)*/
2095 conn = __bt_gatt_get_gdbus_connection();
2097 ERR("Unable to get connection");
2098 return BT_STATUS_FAIL;
2101 /* Only once for ALL GATT Servers */
2102 if (manager_node_info == NULL) {
2103 /* Register ObjectManager interface */
2104 manager_node_info = __bt_gatt_create_method_node_info(
2105 manager_introspection_xml);
2107 if (manager_node_info == NULL) {
2108 ERR("failed to get node info");
2109 return BT_STATUS_FAIL;
2114 return BT_STATUS_SUCCESS;
2117 void _bt_hal_is_gatt_server_initialzed(int slot, char **app_path)
2121 for (l = gatt_server_apps; l; l = g_slist_next(l)) {
2122 struct gatt_server_app *app = (struct gatt_server_app *)l->data;
2123 if (app->slot == slot) {
2124 INFO("GATT Server app found app path [%s] instance [%d]",
2125 app->app_path, app->slot);
2126 *app_path = app->app_path;
2130 /* GATT Server not found */
2134 void _bt_hal_update_gatt_server_path(int slot, char *app_path)
2136 if (app_path == NULL)
2139 struct gatt_server_app *app = g_malloc0(sizeof(struct gatt_server_app));
2140 app->app_path = g_strdup(app_path);
2142 gatt_server_apps = g_slist_append(gatt_server_apps, app);
2143 INFO("GATT Server: Path [%s] Slot [%d]-> Updated", app_path, slot);
2148 static bt_status_t gatt_server_add_service(int server_if, btgatt_srvc_id_t *srvc_id,
2151 CHECK_BTGATT_INIT();
2152 char *app_path = NULL;
2153 GError *error = NULL;
2157 int result = BT_STATUS_SUCCESS;
2159 DBG("GATT Server Add service request: Instance ID[%d] Is Primary [%d] UUID [%s]",
2160 server_if, srvc_id->is_primary, btuuid2str(srvc_id->id.uuid.uu));
2162 /* Check if this GATT server Application is already registered with DBUS */
2163 _bt_hal_is_gatt_server_initialzed(server_if, &app_path);
2165 if (app_path != NULL) {
2166 DBG("GATT server path is already defined [%s]", app_path);
2167 return __bt_hal_add_service_to_dbus(app_path, server_if, srvc_id);
2169 DBG("GATT server application path for instance [%d] is not defined yet", server_if);
2170 result = __bt_hal_gatt_init();
2171 if (result != BT_STATUS_SUCCESS)
2174 /* Only once for each GATT Server */
2175 app_path = g_strdup_printf("/com/%d", server_if);
2177 app_id = g_malloc0(sizeof(int));
2178 *app_id = server_if;
2180 manager_id = g_dbus_connection_register_object(g_conn, app_path,
2181 manager_node_info->interfaces[0],
2182 &manager_interface_vtable,
2183 (gpointer)app_id, NULL, &error);
2185 if (manager_id == 0) {
2186 ERR("failed to register: %s", error->message);
2187 g_error_free(error);
2191 /* For current GATT Server, app_path is created, save it in Table */
2192 _bt_hal_update_gatt_server_path(server_if, app_path);
2194 /* Add GATT Service to DBUS */
2195 if (__bt_hal_add_service_to_dbus(app_path, server_if, srvc_id) != BT_STATUS_SUCCESS)
2201 INFO("Successfully added service");
2202 return BT_STATUS_SUCCESS;
2209 INFO("Service addition failed!!");
2210 return BT_STATUS_FAIL;
2213 static bt_status_t gatt_server_add_included_service(int server_if, int service_handle,
2214 int included_handle)
2216 CHECK_BTGATT_INIT();
2217 return BT_STATUS_SUCCESS;
2221 static gboolean __bt_is_service_last_in_server_list(int instance, int service_handle)
2224 GSList *gatt_services = NULL;
2226 struct gatt_service_info *info = NULL;
2228 gatt_services = _bt_get_service_list_from_server(instance);
2230 len = g_slist_length(gatt_services);
2231 l = g_slist_nth(gatt_services, len -1);
2237 if (info->service_handle == service_handle)
2242 static struct gatt_service_info *__bt_gatt_find_gatt_service_info(int instance,
2246 GSList *gatt_services = NULL;
2247 INFO("Find Service: svc handle [%d] from GATT Server slot[%d]", service_handle, instance);
2249 gatt_services = _bt_get_service_list_from_server(instance);
2251 for (l = gatt_services; l != NULL; l = g_slist_next(l)) {
2252 struct gatt_service_info *info = l->data;
2253 INFO("Got one service with handle [%d]", info->service_handle);
2254 if (info->service_handle == service_handle)
2257 ERR("Gatt service with handle [%d] not found", service_handle);
2261 static bt_status_t gatt_server_add_characteristic(int slot, int service_handle,
2262 bt_uuid_t *uuid, int properties,
2266 GError *error = NULL;
2268 GDBusNodeInfo *node_info;
2270 GVariantBuilder *builder = NULL;
2271 GVariantBuilder *inner_builder = NULL;
2272 struct gatt_service_info *serv_info = NULL;
2273 struct gatt_char_info *char_info = NULL;
2274 GVariantBuilder *builder2 = NULL;
2275 GVariantBuilder *builder3 = NULL;
2276 GVariant *flags_val = NULL;
2278 char *char_flags[NUMBER_OF_FLAGS];
2280 hal_gatt_char_added *user_data = NULL;
2283 CHECK_BTGATT_INIT();
2284 DBG("Add new characteristic to GATT Service handle [%d]", service_handle);
2285 DBG("Properties of new charateristic [%d] Permission [%d]", properties, permissions);
2287 serv_info = __bt_gatt_find_gatt_service_info(slot, service_handle);
2288 if (serv_info == NULL)
2289 return BT_STATUS_FAIL;
2291 node_info = __bt_gatt_create_method_node_info(
2292 characteristics_introspection_xml);
2294 if (node_info == NULL)
2295 return BT_STATUS_FAIL;
2297 DBG("Add new GATT characteristic: Current GATT char handle [%d]", gatt_char_handle);
2298 path = g_strdup_printf("%s/characteristic%d", serv_info->serv_path, ++gatt_char_handle);
2299 DBG("gatt characteristic path is [%s]", path);
2301 app_id = g_malloc0(sizeof(int));
2304 object_id = g_dbus_connection_register_object(g_conn, path,
2305 node_info->interfaces[0],
2306 &char_interface_vtable,
2307 (gpointer)app_id, NULL, &error);
2309 if (object_id == 0) {
2310 ERR("failed to register: %s", error->message);
2311 g_error_free(error);
2314 return BT_STATUS_FAIL;
2317 if (permissions & BT_HAL_GATT_PERMISSION_ENCRYPT_READ)
2318 properties |= BT_HAL_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_READ;
2319 if (permissions & BT_HAL_GATT_PERMISSION_ENCRYPT_AUTHENTICATED_READ)
2320 properties |= BT_HAL_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_READ;
2321 if (permissions & BT_HAL_GATT_PERMISSION_ENCRYPT_WRITE)
2322 properties |= BT_HAL_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_WRITE;
2323 if (permissions & BT_HAL_GATT_PERMISSION_ENCRYPT_AUTHENTICATED_WRITE)
2324 properties |= BT_HAL_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_WRITE;
2326 flag_count = bt_hal_gatt_convert_prop2string(properties, char_flags);
2328 char_info = g_new0(struct gatt_char_info, 1);
2330 char_info->char_path = g_strdup(path);
2331 char_info->char_id = object_id;
2332 char_info->char_uuid = __bt_hal_convert_uuid_to_string(uuid);//g_strdup(btuuid2str(uuid->uu));
2333 for (i = 0; i < flag_count; i++)
2334 char_info->char_flags[i] = char_flags[i];
2337 char_info->flags_length = flag_count;
2338 char_info->char_handle = gatt_char_handle;
2341 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
2342 inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
2344 g_variant_builder_add(inner_builder, "{sv}", "UUID",
2345 g_variant_new("s", char_info->char_uuid));
2346 g_variant_builder_add(inner_builder, "{sv}", "Service",
2347 g_variant_new("o", serv_info->serv_path));
2349 builder2 = g_variant_builder_new(G_VARIANT_TYPE("as"));
2351 for (i = 0; i < flag_count; i++)
2352 g_variant_builder_add(builder2, "s", char_flags[i]);
2354 flags_val = g_variant_new("as", builder2);
2355 g_variant_builder_add(inner_builder, "{sv}", "Flags",
2358 builder3 = g_variant_builder_new(G_VARIANT_TYPE("ao"));
2360 g_variant_builder_add(inner_builder, "{sv}", "Descriptors",
2361 g_variant_new("ao", builder3));
2363 g_variant_builder_add(builder, "{sa{sv}}",
2364 GATT_CHAR_INTERFACE,
2367 ret = g_dbus_connection_emit_signal(g_conn, NULL, "/",
2368 "org.freedesktop.Dbus.ObjectManager",
2370 g_variant_new("(oa{sa{sv}})",
2375 /* dbus gives error cause */
2376 ERR("d-bus api failure: errcode[%x], message[%s]",
2377 error->code, error->message);
2378 g_clear_error(&error);
2382 //*char_path = g_strdup(path);
2387 /* Send Service handle to application */
2388 user_data = g_malloc0(sizeof(hal_gatt_char_added));
2389 user_data->srvc_hdl = serv_info->service_handle;
2390 user_data->char_hdl = gatt_char_handle;
2391 user_data->instance_data = slot;
2392 memcpy(user_data->uuid.uu, uuid->uu, sizeof(uuid->uu));
2393 g_idle_add(__bt_hal_gatt_char_added_cb, (gpointer)user_data);
2395 /* Save newly created charatcristic to GATT Server's service's characteristic list */
2396 serv_info->char_data = g_slist_append(serv_info->char_data, char_info);
2401 g_variant_builder_unref(inner_builder);
2402 g_variant_builder_unref(builder);
2403 g_variant_builder_unref(builder2);
2404 g_variant_builder_unref(builder3);
2406 return BT_STATUS_SUCCESS;
2409 static bt_status_t gatt_server_add_descriptor(int slot, int service_handle, bt_uuid_t *uuid,
2412 CHECK_BTGATT_INIT();
2414 // static int desc_id = 1;
2415 GError *error = NULL;
2417 GDBusNodeInfo *node_info;
2419 GVariantBuilder *builder = NULL;
2420 GVariantBuilder *inner_builder = NULL;
2422 struct gatt_char_info *char_info = NULL;
2423 struct gatt_desc_info *desc_info = NULL;
2424 struct gatt_service_info *serv_info = NULL;
2426 gchar **line_argv = NULL;
2428 char *char_path = NULL;
2431 GVariantBuilder *builder2 = NULL;
2432 GVariant *flags_val = NULL;
2434 char *desc_flags[NUMBER_OF_FLAGS];
2438 hal_gatt_desc_added *user_data = NULL;
2445 /* Fetch service data for the GATT server */
2446 serv_info = __bt_gatt_find_gatt_service_info(slot, service_handle);
2447 if (serv_info == NULL)
2448 return BT_STATUS_FAIL;
2450 /* Fetch list of characteristics from the service info */
2451 l = serv_info->char_data;
2453 /* Fetch last char info from the characteristic list */
2454 char_info = g_slist_last(l)->data;
2455 if (char_info == NULL)
2456 return BT_STATUS_FAIL;
2458 /* Fetch characteristic path from char info */
2459 char_path = char_info->char_path;
2461 line_argv = g_strsplit_set(char_path, "/", 0);
2462 serv_path = g_strdup_printf("/%s/%s/%s", line_argv[1], line_argv[2], line_argv[3]);
2465 node_info = __bt_gatt_create_method_node_info(
2466 descriptor_introspection_xml);
2468 if (node_info == NULL) {
2469 g_strfreev(line_argv);
2471 return BT_STATUS_FAIL;
2474 DBG("Add new Descriptor: Current GATT desc handle [%d]", gatt_desc_handle);
2476 path = g_strdup_printf("%s/descriptor%d", char_path, ++gatt_desc_handle);
2477 DBG("gatt descriptor path is [%s]", path);
2479 app_id = g_malloc0(sizeof(int));
2482 object_id = g_dbus_connection_register_object(g_conn, path,
2483 node_info->interfaces[0],
2484 &desc_interface_vtable,
2485 (gpointer)app_id, NULL, &error);
2487 if (object_id == 0) {
2488 ERR("failed to register: %s", error->message);
2489 g_error_free(error);
2491 g_strfreev(line_argv);
2494 return BT_STATUS_FAIL;
2497 flag_count = bt_hal_gatt_convert_perm2string(permissions, desc_flags);
2499 desc_info = g_new0(struct gatt_desc_info, 1);
2501 desc_info->desc_path = g_strdup(path);
2502 desc_info->desc_id = object_id;
2503 desc_info->desc_uuid = __bt_hal_convert_uuid_to_string(uuid);//g_strdup(btuuid2str(uuid->uu));
2505 for (i = 0; i < flag_count; i++)
2506 desc_info->desc_flags[i] = desc_flags[i];
2508 desc_info->flags_length = flag_count;
2509 desc_info->desc_handle = gatt_desc_handle;
2512 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sa{sv}}"));
2513 inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
2515 g_variant_builder_add(inner_builder, "{sv}", "UUID",
2516 g_variant_new("s", btuuid2str(uuid->uu)));
2517 g_variant_builder_add(inner_builder, "{sv}", "Characteristic",
2518 g_variant_new("o", char_path));
2520 builder2 = g_variant_builder_new(G_VARIANT_TYPE("as"));
2522 for (i = 0; i < flag_count; i++)
2523 g_variant_builder_add(builder2, "s", desc_flags[i]);
2525 flags_val = g_variant_new("as", builder2);
2526 g_variant_builder_add(inner_builder, "{sv}", "Flags",
2529 g_variant_builder_add(builder, "{sa{sv}}",
2530 GATT_DESC_INTERFACE,
2533 g_dbus_connection_emit_signal(g_conn, NULL, "/",
2534 "org.freedesktop.Dbus.ObjectManager",
2536 g_variant_new("(oa{sa{sv}})",
2539 if (error != NULL) {
2540 ERR("g_dbus_connection_emit_signal failed: errCode[%x], message[%s]",
2541 error->code, error->message);
2542 g_clear_error(&error);
2545 //*desc_path = g_strdup(path);
2547 /* Save newly created descriptor to GATT server's service's characteristic */
2548 char_info->desc_data = g_slist_append(char_info->desc_data, desc_info);
2550 /* Send descriptor handle to application */
2551 user_data = g_malloc0(sizeof(hal_gatt_desc_added));
2552 user_data->srvc_hdl = serv_info->service_handle;
2553 user_data->desc_hdl = gatt_desc_handle;
2554 user_data->instance_data = slot;
2555 memcpy(user_data->uuid.uu, uuid->uu, sizeof(uuid->uu));
2556 g_idle_add(__bt_hal_gatt_desc_added_cb, (gpointer)user_data);
2561 g_strfreev(line_argv);
2562 g_variant_builder_unref(inner_builder);
2563 g_variant_builder_unref(builder);
2564 return BT_STATUS_SUCCESS;
2567 static void __bt_hal_register_application_cb(GObject *object, GAsyncResult *res, gpointer user_data)
2569 GError *error = NULL;
2571 char *data = (char*) user_data;
2572 INFO("RegisterApplication is completed path [%s]", data);
2574 result = g_dbus_proxy_call_finish(manager_gproxy, res, &error);
2576 if (result == NULL) {
2577 /* dBUS-RPC is failed */
2578 ERR("Dbus-RPC is failed\n");
2580 if (error != NULL) {
2581 /* dBUS gives error cause */
2582 ERR("D-Bus API failure: errCode[%x], message[%s]\n",
2583 error->code, error->message);
2584 g_clear_error(&error);
2587 g_variant_unref(result);
2592 static GDBusProxy *__bt_hal_gatt_gdbus_init_manager_proxy(const gchar *service,
2593 const gchar *path, const gchar *interface)
2599 g_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM,
2604 ERR("Unable to connect to gdbus: %s", err->message);
2605 g_clear_error(&err);
2610 proxy = g_dbus_proxy_new_sync(g_conn,
2611 G_DBUS_PROXY_FLAGS_NONE, NULL,
2613 interface, NULL, &err);
2617 ERR("Unable to create proxy: %s", err->message);
2618 g_clear_error(&err);
2622 manager_gproxy = proxy;
2627 static GDBusProxy *__bt_gatt_gdbus_get_gatt_manager_proxy(const gchar *service,
2628 const gchar *path, const gchar *interface)
2630 return (manager_gproxy) ? manager_gproxy :
2631 __bt_hal_gatt_gdbus_init_manager_proxy(service,
2635 static void __bt_register_application_to_dbus(int slot)
2637 GDBusProxy *proxy = NULL;
2638 char *app_path = NULL;
2640 DBG("RegisterApplication slot [%d]", slot);
2642 /* It is impossible that app path is still not initialized */
2643 _bt_hal_is_gatt_server_initialzed(slot, &app_path);
2645 proxy = __bt_gatt_gdbus_get_gatt_manager_proxy("org.bluez",
2646 "/org/bluez/hci0", "org.bluez.GattManager1");
2648 data = g_strdup(app_path);
2649 g_dbus_proxy_call(proxy,
2650 "RegisterApplication",
2651 g_variant_new("(oa{sv})",
2653 G_DBUS_CALL_FLAGS_NONE, -1,
2655 (GAsyncReadyCallback)__bt_hal_register_application_cb,
2657 INFO("GATT server started");
2660 static bt_status_t gatt_server_start_service(int server_if, int service_handle, int transport)
2662 CHECK_BTGATT_INIT();
2663 struct gatt_service_info *serv_info = NULL;
2664 hal_gatt_service_started *user_data = NULL;
2665 DBG("Start GATT Service svc hdl [%d], slot [%d]", service_handle, server_if);
2667 /* Fetch service data for the GATT server */
2668 serv_info = __bt_gatt_find_gatt_service_info(server_if, service_handle);
2669 if (serv_info == NULL)
2670 return BT_STATUS_FAIL;
2672 if (serv_info->is_svc_registered)
2673 DBG("service already registered \n");
2675 serv_info->is_svc_registered = TRUE;
2677 /* Send Service handle to application */
2678 user_data = g_malloc0(sizeof(hal_gatt_service_started));
2679 user_data->srvc_hdl = serv_info->service_handle;
2680 user_data->instance_data = server_if;
2681 g_idle_add(__bt_hal_gatt_service_started_cb, (gpointer)user_data);
2683 /* If this is nth Service that is started, then register application at this point */
2684 if (__bt_is_service_last_in_server_list(server_if, service_handle)) {
2685 DBG("This is the last service started from the GATT Server's list of services handle [%d]",
2687 __bt_register_application_to_dbus(server_if);
2690 return BT_STATUS_SUCCESS;
2693 static bt_status_t gatt_server_stop_service(int server_if, int service_handle)
2695 CHECK_BTGATT_INIT();
2696 INFO("Stop service successful");
2697 return BT_STATUS_SUCCESS;
2700 static bt_status_t gatt_server_delete_service(int server_if, int service_handle)
2702 CHECK_BTGATT_INIT();
2703 struct gatt_service_info *serv_info = NULL;
2704 hal_gatt_service_deleted *user_data = NULL;
2707 int err = BT_STATUS_SUCCESS;
2708 int ret = BT_STATUS_SUCCESS;
2709 INFO("Slot [%d] service handle [%d]", server_if, service_handle);
2711 /* Fetch service data for the GATT server */
2712 serv_info = __bt_gatt_find_gatt_service_info(server_if, service_handle);
2713 if (serv_info == NULL) {
2714 ERR("Could not find service info svc handle [%d] server slot [%d]",
2715 service_handle, server_if);
2716 return BT_STATUS_FAIL;
2719 if (serv_info->is_svc_registered == FALSE) {
2720 ERR("service Not registered path [%s] handle [%d]",
2721 serv_info->serv_path, service_handle);
2724 for (l = serv_info->char_data; l != NULL; l = g_slist_next(l)) {
2725 struct gatt_char_info *char_info = l->data;
2727 if (char_info == NULL)
2730 for (l1 = char_info->desc_data; l1 != NULL; l1 = g_slist_next(l1)) {
2731 struct gatt_desc_info *desc_info = l1->data;
2733 if (desc_info == NULL)
2736 ret = g_dbus_connection_unregister_object(g_conn,
2737 desc_info->desc_id);
2739 __bt_hal_gatt_emit_interface_removed(
2740 desc_info->desc_path,
2741 GATT_DESC_INTERFACE);
2743 err = BT_STATUS_FAIL;
2746 /* list remove & free */
2747 char_info->desc_data = g_slist_remove(char_info->desc_data, desc_info);
2748 __bt_hal_gatt_free_descriptor_info(desc_info);
2751 g_slist_free(char_info->desc_data);
2752 char_info->desc_data = NULL;
2754 ret = g_dbus_connection_unregister_object(g_conn,
2755 char_info->char_id);
2757 __bt_hal_gatt_emit_interface_removed(char_info->char_path,
2758 GATT_CHAR_INTERFACE);
2761 err = BT_STATUS_FAIL;
2764 /* list remove & free */
2765 serv_info->char_data = g_slist_remove(serv_info->char_data, char_info);
2766 __bt_hal_gatt_free_characteristic_info(char_info);
2769 g_slist_free(serv_info->char_data);
2770 serv_info->char_data = NULL;
2772 ret = g_dbus_connection_unregister_object(g_conn, serv_info->serv_id);
2774 __bt_hal_gatt_emit_interface_removed(serv_info->serv_path,
2775 GATT_SERV_INTERFACE);
2778 err = BT_STATUS_FAIL;
2781 ret = g_dbus_connection_unregister_object(g_conn, serv_info->prop_id);
2783 DBG("Unregistered the service on properties interface");
2785 /* Remove from global list */
2786 gatt_services = g_slist_remove(gatt_services, serv_info);
2787 INFO("After removing from global list total service dount [%d]", g_slist_length(gatt_services));
2789 /* Remove from GATT Server's list of services */
2790 _bt_remote_service_from_gatt_server(server_if, service_handle);
2792 if (gatt_services == NULL)
2793 INFO("All GATT Services of all GATT Servers are unregistered");
2795 if (err == BT_STATUS_SUCCESS) {
2796 INFO("Send GATT Service deleted Event");
2797 /* Send Service handle to application */
2798 user_data = g_malloc0(sizeof(hal_gatt_service_deleted));
2799 user_data->srvc_hdl = serv_info->service_handle;
2800 user_data->instance_data = server_if;
2801 g_idle_add(__bt_hal_gatt_service_deleted_cb, (gpointer)user_data);
2804 /* Free the service */
2805 __bt_hal_gatt_free_service_info(serv_info);
2809 static gboolean __bt_gatt_get_service_state(const char *service_path)
2811 struct gatt_service_info *svc_info = NULL;
2813 for (l = gatt_services; l; l = g_slist_next(l)) {
2815 svc_info = (struct gatt_service_info *)l->data;
2818 if (!g_strcmp0(svc_info->serv_path, service_path)) {
2819 DBG("Return the state of the gatt service %d",
2820 svc_info->is_svc_registered);
2821 return svc_info->is_svc_registered;
2825 DBG("gatt service info is NULL");
2829 static bt_status_t gatt_server_send_indication(int server_if, int attribute_handle, int conn_id,
2830 int len, int confirm, char* p_value)
2832 CHECK_BTGATT_INIT();
2835 GVariantBuilder *outer_builder;
2836 GVariantBuilder *invalidated_builder;
2838 /* For Value update via PropertyChange */
2839 GVariantBuilder *outer_builder1;
2840 GVariantBuilder *inner_builder1;
2841 GVariantBuilder *invalidated_builder1;
2842 GVariant *update_value = NULL;
2844 /* Other variables */
2845 struct gatt_client_info_t *conn_info = NULL;
2846 gchar *serv_path = NULL;
2847 char *char_path = NULL;
2848 gchar **line_argv = NULL;
2849 gboolean notify = TRUE;
2850 gboolean ret = TRUE;
2851 int err = BT_STATUS_SUCCESS;
2853 GError *error = NULL;
2856 memset(addr, 0x00, sizeof(addr));
2858 conn_info = __bt_find_remote_gatt_client_info_from_conn(conn_id);
2859 if (conn_info == NULL) {
2860 ERR("No Connection Inforamtion!!!");
2861 return BT_STATUS_FAIL;
2864 DBG("Send Indication to GATT client addr [%s]", conn_info->addr);
2866 char_path = __bt_gatt_find_char_path_from_handle(attribute_handle);
2867 if (char_path == NULL)
2868 return BT_STATUS_FAIL;
2870 line_argv = g_strsplit_set(char_path, "/", 0);
2871 serv_path = g_strdup_printf("/%s/%s/%s", line_argv[1], line_argv[2], line_argv[3]);
2873 if (!__bt_gatt_get_service_state(serv_path)) {
2874 DBG("service not registered for this characteristic \n");
2876 g_strfreev(line_argv);
2877 return BT_STATUS_FAIL;
2880 outer_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
2881 invalidated_builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
2883 g_variant_builder_add(outer_builder, "{sv}", "Notifying",
2884 g_variant_new("b", notify));
2886 _bt_hal_convert_addr_type_to_string(addr, (unsigned char *)conn_info->addr);
2888 g_variant_builder_add(outer_builder, "{sv}", "Unicast",
2889 g_variant_new("s", addr));
2891 DBG("Set characteristic Notification \n");
2892 ret = g_dbus_connection_emit_signal(g_conn, NULL,
2894 "org.freedesktop.DBus.Properties",
2895 "PropertiesChanged",
2896 g_variant_new("(sa{sv}as)",
2897 "org.bluez.GattCharacteristic1",
2898 outer_builder, invalidated_builder),
2902 if (error != NULL) {
2903 ERR("D-Bus API failure: errCode[%x], \
2905 error->code, error->message);
2906 g_clear_error(&error);
2908 err = BT_STATUS_FAIL;
2911 g_variant_builder_unref(outer_builder);
2912 g_variant_builder_unref(invalidated_builder);
2915 /* Notifying Done, now update Value to Bluez via PropertyChanged */
2916 invalidated_builder1 = g_variant_builder_new(G_VARIANT_TYPE("as"));
2918 inner_builder1 = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
2919 for (i = 0; i < len; i++)
2920 g_variant_builder_add(inner_builder1, "y", p_value[i]);
2922 update_value = g_variant_new("ay", inner_builder1);
2925 outer_builder1 = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
2926 g_variant_builder_add(outer_builder1, "{sv}", "Value",
2929 DBG("Updating characteristic value \n");
2930 ret = g_dbus_connection_emit_signal(g_conn, NULL,
2932 "org.freedesktop.DBus.Properties",
2933 "PropertiesChanged",
2934 g_variant_new("(sa{sv}as)",
2935 "org.bluez.GattCharacteristic1",
2936 outer_builder1, invalidated_builder1),
2940 if (error != NULL) {
2941 ERR("D-Bus API failure: errCode[%x], \
2943 error->code, error->message);
2944 g_clear_error(&error);
2946 err = BT_STATUS_FAIL;
2948 struct gatt_char_info *char_info = NULL;
2950 char_info = __bt_gatt_find_char_info_from_handle(attribute_handle);
2951 if (char_info == NULL) {
2953 g_strfreev(line_argv);
2954 g_variant_builder_unref(inner_builder1);
2955 g_variant_builder_unref(outer_builder1);
2956 g_variant_builder_unref(invalidated_builder1);
2958 return BT_STATUS_FAIL;
2961 char_info->value_length = len;
2963 char_info->char_value = (char *)realloc(char_info->char_value, len);
2964 if (char_info->char_value) {
2965 for (i = 0; i < len; i++)
2966 char_info->char_value[i] = p_value[i];
2971 g_strfreev(line_argv);
2972 g_variant_builder_unref(inner_builder1);
2973 g_variant_builder_unref(outer_builder1);
2974 g_variant_builder_unref(invalidated_builder1);
2979 static bt_status_t gatt_server_send_response(int conn_id, int trans_id,
2980 int status, btgatt_response_t *response)
2982 CHECK_BTGATT_INIT();
2984 struct gatt_req_info *req_info = NULL;
2985 struct gatt_client_info_t *conn_info = NULL;
2988 DBG("GATT Server Send Response Conn ID [%d]", conn_id);
2990 conn_info = __bt_find_remote_gatt_client_info_from_conn(conn_id);
2991 if (conn_info == NULL) {
2992 ERR("No Connection Inforamtion!!!");
2993 return BT_STATUS_FAIL;
2996 req_info = __bt_find_remote_gatt_client_request_info(conn_id, trans_id);
2997 if (req_info == NULL) {
2998 ERR("No Request Inforamtion!!!");
2999 return BT_STATUS_FAIL;
3002 if (status != BT_STATUS_SUCCESS) {
3003 ERR("resp_state is 0x%X", status);
3005 g_dbus_method_invocation_return_dbus_error(req_info->context,
3006 "org.bluez.Error.Failed", "Application Error");
3008 conn_info->gatt_req_info_list = g_slist_remove(conn_info->gatt_req_info_list, req_info);
3010 req_info->context = NULL;
3011 if (req_info->attr_path)
3012 g_free(req_info->attr_path);
3013 if (req_info->svc_path)
3014 g_free(req_info->svc_path);
3017 return BT_STATUS_SUCCESS;
3020 DBG("Reponse Value length [%d]", response->attr_value.len);
3021 DBG("Request type: [%d]", req_info->request_type);
3024 for (i = 0; i < response->attr_value.len; i++)
3025 DBG("Resonse [%d] = [0x%x]", response->attr_value.value[i]);
3027 if (req_info->request_type == BT_HAL_GATT_REQUEST_TYPE_READ) {
3028 GVariantBuilder *inner_builder = NULL;
3029 inner_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
3031 if (response->attr_value.len > 0) {
3032 for (i = 0; i < response->attr_value.len; i++)
3033 g_variant_builder_add(inner_builder, "y", response->attr_value.value[i]);
3035 g_dbus_method_invocation_return_value(req_info->context,
3036 g_variant_new("(ay)", inner_builder));
3038 g_variant_builder_unref(inner_builder);
3040 g_dbus_method_invocation_return_value(req_info->context, NULL);
3042 conn_info->gatt_req_info_list = g_slist_remove(conn_info->gatt_req_info_list, req_info);
3044 req_info->context = NULL;
3045 if (req_info->attr_path)
3046 g_free(req_info->attr_path);
3047 if (req_info->svc_path)
3048 g_free(req_info->svc_path);
3051 return BT_STATUS_SUCCESS;
3055 static bt_status_t gatt_server_send_acquire_response(int conn_id, int trans_id,
3056 int status, int fd, int mtu, void *fdlist)
3058 CHECK_BTGATT_INIT();
3060 struct gatt_req_info *req_info = NULL;
3061 struct gatt_client_info_t *conn_info = NULL;
3064 DBG("GATT Server Send Response Conn ID [%d]", conn_id);
3066 conn_info = __bt_find_remote_gatt_client_info_from_conn(conn_id);
3067 if (conn_info == NULL) {
3068 ERR("No Connection Inforamtion!!!");
3069 return BT_STATUS_FAIL;
3072 req_info = __bt_find_remote_gatt_client_request_info(conn_id, trans_id);
3073 if (req_info == NULL) {
3074 ERR("No Request Inforamtion!!!");
3075 return BT_STATUS_FAIL;
3078 if (status != BT_STATUS_SUCCESS) {
3079 ERR("resp_state is 0x%X", status);
3081 g_dbus_method_invocation_return_dbus_error(req_info->context,
3082 "org.bluez.Error.Failed", "Application Error");
3084 conn_info->gatt_req_info_list = g_slist_remove(conn_info->gatt_req_info_list, req_info);
3086 req_info->context = NULL;
3087 if (req_info->attr_path)
3088 g_free(req_info->attr_path);
3089 if (req_info->svc_path)
3090 g_free(req_info->svc_path);
3093 return BT_STATUS_SUCCESS;
3097 if (req_info->request_type == BT_HAL_GATT_REQUEST_TYPE_ACQUIRE_WRITE) {
3099 INFO("GATT Server Send Response BT_HAL_GATT_REQUEST_TYPE_ACQUIRE_WRITE to bluez %d\n", fd);
3100 g_dbus_method_invocation_return_value_with_unix_fd_list(
3101 req_info->context, g_variant_new("(hq)", 0, mtu), (GUnixFDList*)fdlist);
3103 } else if (req_info->request_type == BT_HAL_GATT_REQUEST_TYPE_ACQUIRE_NOTIFY) {
3105 INFO("GATT Server Send Response BT_HAL_GATT_REQUEST_TYPE_ACQUIRE_NOTIFY to bluez %d\n", fd);
3106 g_dbus_method_invocation_return_value_with_unix_fd_list(
3107 req_info->context, g_variant_new("(hq)", 0, mtu), (GUnixFDList*)fdlist);
3110 g_dbus_method_invocation_return_value(req_info->context, NULL);
3112 conn_info->gatt_req_info_list = g_slist_remove(conn_info->gatt_req_info_list, req_info);
3114 req_info->context = NULL;
3115 if (req_info->attr_path)
3116 g_free(req_info->attr_path);
3117 if (req_info->svc_path)
3118 g_free(req_info->svc_path);
3121 return BT_STATUS_SUCCESS;
3125 static bt_status_t gatt_server_update_att_value(int server_if, int attribute_handle,
3126 int value_length, char* att_value)
3128 CHECK_BTGATT_INIT();
3130 /* Other variables */
3131 char *char_path = NULL;
3132 gboolean ret = TRUE;
3133 GError *error = NULL;
3135 GVariantBuilder *outer_builder;
3136 GVariantBuilder *inner_builder;
3137 GVariantBuilder *invalidated_builder;
3138 GVariant *update_value = NULL;
3139 int err = BT_STATUS_SUCCESS;
3141 gchar **line_argv = NULL;
3142 gchar *serv_path = NULL;
3144 char_path = __bt_gatt_find_char_path_from_handle(attribute_handle);
3145 if (char_path == NULL)
3146 return BT_STATUS_FAIL;
3148 line_argv = g_strsplit_set(char_path, "/", 0);
3149 serv_path = g_strdup_printf("/%s/%s/%s", line_argv[1], line_argv[2], line_argv[3]);
3151 if (!__bt_gatt_get_service_state(serv_path)) {
3152 DBG("service not registered for this characteristic \n");
3154 g_strfreev(line_argv);
3155 return BT_STATUS_FAIL;
3159 line_argv = g_strsplit_set(char_path, "/", 0);
3160 serv_path = g_strdup_printf("/%s/%s/%s", line_argv[1], line_argv[2], line_argv[3]);
3162 if (!__bt_gatt_get_service_state(serv_path)) {
3163 DBG("service not registered for this characteristic \n");
3165 g_strfreev(line_argv);
3166 return BT_STATUS_FAIL;
3169 outer_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
3170 invalidated_builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
3172 inner_builder = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
3173 for (i = 0; i < value_length; i++)
3174 g_variant_builder_add(inner_builder, "y", att_value[i]);
3176 update_value = g_variant_new("ay", inner_builder);
3178 g_variant_builder_add(outer_builder, "{sv}", "Value",
3181 DBG("Updating characteristic value \n");
3182 ret = g_dbus_connection_emit_signal(g_conn, NULL,
3184 "org.freedesktop.DBus.Properties",
3185 "PropertiesChanged",
3186 g_variant_new("(sa{sv}as)",
3187 "org.bluez.GattCharacteristic1",
3188 outer_builder, invalidated_builder),
3192 if (error != NULL) {
3193 ERR("D-Bus API failure: errCode[%x], \
3195 error->code, error->message);
3196 g_clear_error(&error);
3198 err = BT_STATUS_FAIL;
3200 struct gatt_char_info *char_info = NULL;
3202 char_info = __bt_gatt_find_char_info_from_handle(attribute_handle);
3203 if (char_info == NULL) {
3205 g_strfreev(line_argv);
3206 g_variant_builder_unref(inner_builder);
3207 g_variant_builder_unref(outer_builder);
3208 g_variant_builder_unref(invalidated_builder);
3210 return BT_STATUS_FAIL;
3213 char_info->value_length = value_length;
3215 char_info->char_value = (char *)realloc(char_info->char_value, value_length);
3216 if (char_info->char_value) {
3217 for (i = 0; i < value_length; i++)
3218 char_info->char_value[i] = att_value[i];
3224 g_strfreev(line_argv);
3225 g_variant_builder_unref(inner_builder);
3226 g_variant_builder_unref(outer_builder);
3227 g_variant_builder_unref(invalidated_builder);
3232 static bt_status_t gatt_server_listen(int server_if, bool start)
3234 CHECK_BTGATT_INIT();
3235 /* Send Data to LE Module */
3236 return _bt_hal_enable_advertising(server_if, start, FALSE);
3239 static bt_status_t gatt_server_set_adv_data(int server_if, bool set_scan_rsp, bool include_name,
3240 bool include_txpower, int min_interval, int max_interval, int appearance,
3241 uint16_t manufacturer_len, char* manufacturer_data,
3242 uint16_t service_data_len, char* service_data,
3243 uint16_t service_uuid_len, char* service_uuid)
3245 CHECK_BTGATT_INIT();
3246 return BT_STATUS_SUCCESS;
3249 static bt_status_t gatt_server_multi_adv_enable(int server_if)
3251 CHECK_BTGATT_INIT();
3252 /* Send Enable Advertising request to LE Module */
3253 return _bt_hal_enable_advertising(server_if, TRUE, TRUE);
3256 static bt_status_t gatt_server_multi_adv_update(int server_if, int min_interval, int max_interval, int adv_type,
3257 int chnl_map, int tx_power, int timeout_s)
3259 CHECK_BTGATT_INIT();
3261 /* Send Advertising parameters to LE Module */
3262 return _bt_hal_set_advertising_params(server_if, min_interval, max_interval, adv_type,
3263 chnl_map, tx_power, timeout_s);
3266 static bt_status_t gatt_server_multi_adv_set_inst_data(btgatt_adv_param_setup_t adv_param_setup)
3268 CHECK_BTGATT_INIT();
3270 /* Send Data to LE Module */
3271 return _bt_hal_set_advertising_data(adv_param_setup);
3274 static bt_status_t gatt_server_multi_adv_disable(int server_if)
3276 CHECK_BTGATT_INIT();
3277 /* Send Data to LE Module */
3278 return _bt_hal_enable_advertising(server_if, FALSE, TRUE);
3281 static bt_status_t gatt_server_get_mtu_size(int conn_id, int *mtu_size)
3283 CHECK_BTGATT_INIT();
3284 char *object_path = NULL;
3286 GDBusProxy *device_proxy;
3287 GError *error = NULL;
3289 GVariant *tmp_value;
3290 GDBusConnection *conn;
3291 GVariant *result = NULL;
3292 int ret = BT_STATUS_SUCCESS;
3293 struct gatt_client_info_t *conn_info = NULL;
3296 if (mtu_size == NULL)
3297 return BT_STATUS_PARM_INVALID;
3299 /* GDBUS Connection Info validate */
3300 conn = _bt_hal_get_system_gconn();
3302 ERR("Could not get System DBUS Connection");
3303 return BT_STATUS_FAIL;
3306 /* Connection Info validate */
3307 conn_info = __bt_find_remote_gatt_client_info_from_conn(conn_id);
3308 if (conn_info == NULL) {
3309 ERR("No Connection Inforamtion!!!");
3310 return BT_STATUS_FAIL;
3314 object_path = _bt_hal_get_device_object_path(conn_info->addr);
3315 if (object_path == NULL)
3316 return BT_STATUS_FAIL;
3318 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3319 NULL, BT_HAL_BLUEZ_NAME, object_path,
3320 BT_HAL_PROPERTIES_INTERFACE, NULL, NULL);
3322 g_free(object_path);
3323 if (device_proxy == NULL)
3324 return BT_STATUS_FAIL;
3326 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
3327 g_variant_new("(s)", BT_HAL_DEVICE_INTERFACE),
3328 G_DBUS_CALL_FLAGS_NONE,
3332 if (result == NULL) {
3333 if (error != NULL) {
3334 ERR("Error occured in Proxy call [%s]\n", error->message);
3335 g_error_free(error);
3337 g_object_unref(device_proxy);
3338 return BT_STATUS_FAIL;
3341 g_variant_get(result , "(@a{sv})", &value);
3342 g_variant_unref(result);
3344 tmp_value = g_variant_lookup_value(value, "AttMtu", G_VARIANT_TYPE_UINT16);
3345 if (tmp_value == NULL) {
3346 g_object_unref(device_proxy);
3347 g_variant_unref(value);
3348 return BT_STATUS_FAIL;
3351 mtu = g_variant_get_uint16(tmp_value);
3353 DBG("ATT MTU : [%d]", mtu);
3355 g_variant_unref(tmp_value);
3356 g_variant_unref(value);
3357 g_object_unref(device_proxy);
3359 *mtu_size = (int) mtu;
3364 const btgatt_server_interface_t btgatt_server_interface = {
3365 gatt_server_register_app,
3366 gatt_server_unregister_app,
3369 gatt_server_add_service,
3370 gatt_server_add_included_service,
3371 gatt_server_add_characteristic,
3372 gatt_server_add_descriptor,
3373 gatt_server_start_service,
3374 gatt_server_stop_service,
3375 gatt_server_delete_service,
3376 gatt_server_send_indication,
3377 gatt_server_send_response,
3378 gatt_server_update_att_value,
3380 gatt_server_set_adv_data,
3381 gatt_server_multi_adv_enable,
3382 gatt_server_multi_adv_update,
3383 gatt_server_multi_adv_set_inst_data,
3384 gatt_server_multi_adv_disable,
3385 gatt_server_get_mtu_size,
3386 gatt_server_send_acquire_response