4 * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd.
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.
23 #include "bt-service-common.h"
24 #include "bt-service-core-adapter.h"
25 #include "bt-service-event-receiver.h"
26 #include "bt-request-handler.h"
27 #include "bluetooth-api.h"
29 #include "bluetooth-api.h"
30 #include "bt-internal-types.h"
31 #include "bt-service-util.h"
32 #include "bt-service-common.h"
33 #include "bt-service-event.h"
35 #include "bt-internal-types.h"
36 #include "bt-service-gatt.h"
37 #include "bt-service-gatt.h"
38 #include "bt-service-core-device.h"
39 #include "bt-service-core-adapter-le.h"
40 #include "bluetooth-gatt-client-api.h"
42 #include <oal-hardware.h>
43 #include <oal-manager.h>
44 #include <oal-event.h>
45 #include <oal-adapter-mgr.h>
46 #include <oal-device-mgr.h>
49 #define BT_GATT_TRANSPORT_LE 0
50 #define BT_GATT_TRANSPORT_BR_EDR 1
51 #define BT_GATT_TRANSPORT_LE_BR_EDR 2
52 #define BT_UUID_STRING_MAX 64
53 #define BT_SENDER_MAX_LENGTH 50
54 #define MAX_APPS_SUPPORTED 11 /* Slot 0 is not used */
55 #define BT_DEFAULT_ATT_MTU 23
57 #define UUID_MAX_LEN 50
59 #ifdef TIZEN_BLUEDROID_PORTING
68 bluetooth_gatt_att_request_type_e request_type;
69 int prep_request_count;
70 } bt_gatt_prep_write_data_t;
72 static GSList *g_pending_write_list = NULL;
75 #define BDADDR_ANY (&(bluetooth_device_address_t) {{0, 0, 0, 0, 0, 0} })
77 static char uuid_list[NUM_UUID][BT_UUID_STRING_MAX] = {"0000b00b-0000-0000-f065-080080fa49b5", /* Used by BLEAPP */
78 "0000b00b-1111-1111-0123-456789ab0cd2", /* Used by BLEAPP */
79 "0000b00b-2222-1111-0123-456789ab0cd2",
80 "0000b00b-3333-1111-0123-456789ab0cd2",
81 "0000b00b-4444-1111-0123-456789ab0cd2",
82 "0000b00b-5555-1111-0123-456789ab0cd2",
83 "0000b00b-6666-1111-0123-456789ab0cd2",
84 "0000b00b-7777-1111-0123-456789ab0cd2",
85 "0000b00b-8888-1111-0123-456789ab0cd2",
86 "0000b00b-9999-1111-0123-456789ab0cd2",
87 "0000b00b-aaaa-1111-0123-456789ab0cd2",
88 "0000b00b-bbbb-1111-0123-456789ab0cd2",
89 "0000b00b-cccc-1111-0123-456789ab0cd2",
90 "0000b00b-dddd-1111-0123-456789ab0cd2",
91 "0000b00b-eeee-1111-0123-456789ab0cd2",
92 "0000b00b-ffff-1111-0123-456789ab0cd2",
93 "0000b00c-0000-1111-0123-456789ab0cd2",
94 "0000b00c-1111-1111-0123-456789ab0cd2",
95 "0000b00c-2222-1111-0123-456789ab0cd2",
96 "0000b00c-3333-1111-0123-456789ab0cd2"};
98 /* Reserved GATT client Instance UUID. This is used only internally by bt-service */
99 #define DEFAULT_GATT_CLIENT_UUID "0000a00a-1111-1111-0123-456789abcdef"
101 static int gatt_default_client = -1;
104 gboolean is_registered;
105 bluetooth_device_address_t addr;
106 unsigned char svc_uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
108 unsigned char char_uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
110 } bt_gatt_notif_reg_info_t;
112 struct gatt_out_conn_info_t {
113 int client_id; /* This value unique identifies a GATT Client instance */
114 char *addr; /* Remote GATT Server address */
117 /* Linked List of outgoing gatt connection list
118 Note: This is valid only for local GATT client */
119 static GSList *outgoing_gatt_conn_list = NULL;
121 /* GATT Server Info(Local Client) List Structure */
122 struct gatt_server_info_t {
123 int connection_id; /* This value will uniquely identify a GATT client-server connection */
124 int client_id; /* This value unique identifies a GATT Client instance */
125 char *addr; /* Remote GATT Server address */
128 struct gatt_client_info_t {
129 int connection_id; /* This value will uniquely identify a GATT client-server connection */
130 int instance_id; /* This value unique identifies a GATT server instance */
131 char *addr; /* Remote GATT client address */
135 * Remove this feature if code is verified
136 * Remove gatt_client/server_info_t and use gatt_conn_info_t
137 * Remove gatt_client/server_info_list and use gatt_conn_info_list
139 //#define __INTEGRATE_GATT_INFO__ // TODO: this feature can be used if easy setup scenario is fully supported and the name need to be changed to avoid confusion with existing GATT flags
140 #ifndef __INTEGRATE_GATT_INFO__
141 /* Linked List of connected Remote GATT Servers */
142 static GSList *gatt_server_info_list = NULL;
143 /* Linked List of connected Remote GATT clients */
144 static GSList *gatt_client_info_list = NULL;
146 /* GATT Connection Info List Structure */
147 struct gatt_conn_info_t {
148 char *addr; /* Remote GATT address */
149 int connection_id; /* This value will uniquely identify a GATT client-server connection */
150 int client_id; /* This value unique identifies a GATT Client instance */
151 int instance_id; /* This value unique identifies a GATT Server instance */
154 /* Linked List of connected Remote GATT info */
155 static GSList *gatt_conn_info_list = NULL;
156 #define gatt_server_info_t gatt_conn_info_t
157 #define gatt_client_info_t gatt_conn_info_t
158 #define gatt_server_info_list gatt_conn_info_list
159 #define gatt_client_info_list gatt_conn_info_list
164 unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN]; /* If any service added */
165 } bt_gatt_svc_changed_info_t;
170 // bluetooth_device_address_t address; /* Remote BLE Device Address */
171 GSList *services; /* List of all services of above remote device */
172 int count; /* Number of services browsed from remote device */
173 bt_gatt_svc_changed_info_t info;
174 } bt_gatt_service_info_list_t;
177 unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
181 GSList *included_svcs;
182 gboolean is_removed; /* 0 => Service is added, 1=> removed */
183 } bt_gatt_service_info_t;
186 unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
190 unsigned char val[BLUETOOTH_GATT_ATT_DATA_LENGTH_MAX];
192 } bt_gatt_char_info_t;
195 unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
198 unsigned char val[BLUETOOTH_GATT_ATT_DATA_LENGTH_MAX];
199 } bt_gatt_descriptor_info_t;
202 unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
204 } bt_gatt_included_service_info_t;
206 static GSList *list_gatt_info = NULL;
211 } bt_service_handle_uuid_info_t;
213 /* App Information structure */
216 char sender[BT_SENDER_MAX_LENGTH];
217 char uuid[BT_UUID_STRING_MAX];
220 bluetooth_advertising_data_t adv_data; /* Will store adv data for specific slot */
222 bluetooth_scan_resp_data_t scan_rsp; /* Will store scan rsp data for specific slot */
224 gboolean is_initialized;
225 GSList *service_handles;
226 GSList *service_handle_uuids; /* List of bt_service_handle_uuid_info_t */
227 int client_id; /* GATT Client instance ID */
228 bluetooth_device_address_t address; /* Remote BLE Device Address */
229 gboolean is_watcher_enabled;
230 } bt_service_app_info_t;
232 /* GATT Server Request Info Structure */
233 struct gatt_server_req_info {
234 int connection_id; /* This value will uniquely identify a GATT client-server connection */
235 int request_id; /* This is an unique transaction ID assigned against each request by stack */
236 int attribute_handle; /* GATT server attribute handle */
237 int offset; /* GATT server attribute offset on which request is invoked by GATT client */
238 bluetooth_gatt_att_request_type_e request_type; /* Read or Write request */
239 char *addr; /* Remote GATT client address */
242 /* GATT Indicate confirm result */
243 struct gatt_indicate_cfm_result_info_t {
244 int result; /* Result of event */
245 char *addr; /* Remote GATT client address */
246 int att_hdl; /* Characteristic Attribute handle */
247 int completed; /* 1 if last event, otheriwse 0 */
250 /* Request Search Utility method */
251 static struct gatt_server_req_info *__bt_gatt_server_find_request_info(int request_id,
252 bluetooth_gatt_att_request_type_e req_type);
254 static int __bt_gatt_send_indication_to_all_connected_clients(bluetooth_gatt_att_data_t *data,
255 bluetooth_gatt_server_indication_params_t *param);
257 static void __bt_remove_all_service_handles(bt_service_app_info_t *app_info);
259 static void __bt_free_service_info(bt_gatt_service_info_t *service_info);
261 static int __bt_unregister_gatt_client_instance(int client_if);
263 static void __bt_service_reset_gatt_data(void);
265 static void __bt_handle_client_instance_registered(event_gattc_register_t *data);
266 static void __bt_handle_client_connected(event_gattc_conn_t *event_data);
267 static void __bt_handle_client_disconnected(event_gattc_conn_t *event_data);
268 static void __bt_handle_client_service_search_completed(event_gattc_conn_status_t *event_data);
269 static void __bt_handle_client_service_search_result(event_gattc_service_result_t *event_data);
270 static void __bt_handle_client_characteristic_search_result(
271 event_gattc_characteristic_result_t *event_data);
272 static void __bt_handle_client_descriptor_search_result(event_gattc_descriptor_result_t *event_data);
273 static void __bt_handle_client_characteristic_read_data(event_gattc_read_data *event_data);
274 static void __bt_handle_client_descriptor_read_data(event_gattc_read_data *event_data);
275 static void __bt_handle_client_characteristic_write_data(event_gattc_write_data *event_data);
276 static void __bt_handle_client_descriptor_write_data(event_gattc_write_data *event_data);
277 static void __bt_hanlde_le_device_disconnection(event_dev_conn_status_t *event_data);
278 static void __bt_handle_client_notification_registered(event_gattc_regdereg_notify_t *event_data,
279 gboolean is_registered);
280 static void __bt_handle_client_notification_data(event_gattc_notify_data *event_data);
281 static void __bt_handle_client_service_changed_ind(event_gattc_service_changed_data *event_data);
282 static void __bt_handle_client_mtu_exchange_completed(event_gattc_mtu_configured_t *event_data);
284 static int __bt_unregister_gatt_server_instance(int server_instance);
285 static void __bt_cleanup_remote_services(struct gatt_server_info_t *conn_info);
286 #ifdef TIZEN_BLUEDROID_PORTING
287 static void __bt_remove_all_prep_write_req(int conn_id);
291 struct gatt_mtu_info_t {
292 char *addr; /* Remote GATT Server address */
296 static GSList *gatt_mtu_info_list = NULL;
298 static struct gatt_mtu_info_t *__bt_find_mtu_gatt_device(char *address);
299 static void __bt_remove_mtu_gatt_device(char *address);
300 static void __bt_add_mtu_gatt_device(char *address);
301 static void __bt_update_mtu_gatt_device(char *address, int mtu);
303 /* Linked List of GATT requests from Remote GATT Clients */
304 static GSList *gatt_server_requests = NULL;
306 /* Number of clients to be notified to */
307 static int num_indicate_clients;
309 /* List of applications */
310 static bt_service_app_info_t numapps[MAX_APPS_SUPPORTED];
312 static void __bt_gatt_handle_pending_request_info(int result,
313 int service_function, void *data, unsigned int size);
315 static void __bt_handle_server_instance_registered(event_gatts_register_t *data);
317 static void __bt_gatt_event_handler(int event_type, gpointer event_data);
324 void _bt_check_adv_app_termination(const char *name)
326 bt_service_app_info_t *app = NULL;
328 int apps[MAX_APPS_SUPPORTED] = { 0, };
330 ret_if(NULL == name);
332 memset(&apps, -1, sizeof(apps));
334 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
337 /* Search for a app which has same sender and stop adv is running */
338 if (strcasecmp(app->sender, name) == 0 && app->is_initialized == TRUE) {
339 BT_DBG("numapps[%d] Match found, name: %s", k, name);
341 /* TODO 2: Need to manage app info as list, not array.
342 This loop always run for MAX count if any apps are terminated.
345 /* Save instances of all apps that need to be unregistered */
346 if (app->instance_id != -1) {
348 /* Unregister all service handles with stack */
349 __bt_remove_all_service_handles(app);
351 /* If Advertising is enabled, stop it */
352 if (app->adv_handle != 0) {
353 BT_INFO("Stop advertising on instance ID [%d]", app->instance_id);
354 /* Disable adv if running */
355 BT_INFO("Disable Advertising Adv Handle [%d] sender [%s]",
356 app->adv_handle, name);
357 _bt_set_advertising(app->sender, app->adv_handle, FALSE, FALSE);
360 apps[app->instance_id] = BT_GATT_SERVER; /* App holds a GATT server Instance */
361 } else if (app->client_id != -1) {
363 apps[app->client_id] = BT_GATT_CLIENT; /* App holds a GATT client Instance */
368 /* Unregister all apps one by one */
369 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
370 if (apps[k] == BT_GATT_SERVER) {
371 BT_INFO("Unregister server app[%d]", k);
372 /* Unregister server instance */
373 __bt_unregister_gatt_server_instance(k);
374 } else if (apps[k] == BT_GATT_CLIENT) {
375 BT_INFO("Unregister client app[%d]", k);
376 /* Unregister client instance */
377 __bt_unregister_gatt_client_instance(k);
382 static int __bt_gatt_send_indication_to_all_connected_clients(bluetooth_gatt_att_data_t *data,
383 bluetooth_gatt_server_indication_params_t *param)
386 int ret = OAL_STATUS_SUCCESS;
387 int result = OAL_STATUS_INTERNAL_ERROR;
389 BT_INFO("Current total number of connected clients [%d]", g_slist_length(gatt_client_info_list));
390 for (l = gatt_client_info_list; l != NULL; l = l->next) {
391 struct gatt_client_info_t *info = l->data;
394 BT_INFO("GATT Remote client address [%s] connection Id [%d]", info->addr, info->connection_id);
396 ret = gatts_send_indication(param->instance_id, param->atrribute_handle,
397 info->connection_id, data->length,
398 param->need_confirmation, (char *)(&data->data[0]));
400 BT_INFO("Send Indication to GATT client [%s] result: [%d]", info->addr, ret);
401 if (ret == OAL_STATUS_SUCCESS) {
402 BT_INFO("Send Indication sent successfully to GATT client [%s]", info->addr);
404 num_indicate_clients++;
408 BT_INFO("Indication sending done for total number of clients [%d]", num_indicate_clients);
412 static struct gatt_server_req_info *__bt_gatt_server_find_request_info(int request_id,
413 bluetooth_gatt_att_request_type_e req_type)
417 for (l = gatt_server_requests; l != NULL; l = l->next) {
418 struct gatt_server_req_info *req_info = l->data;
420 if (req_info && req_info->request_id == request_id && req_info->request_type == req_type) {
421 BT_DBG("GATT Server request info found Req ID [%d] handle [%d] conn ID [%d]",
422 req_info->request_id, req_info->attribute_handle, req_info->connection_id);
426 BT_ERR("Gatt Request not found");
430 static void __bt_gatt_server_release_request_info(const char *address)
433 struct gatt_server_req_info *req_info = NULL;
435 for (l = gatt_server_requests; l != NULL; l = g_slist_next(l)) {
437 if (req_info == NULL)
440 if (g_strcmp0(req_info->addr, address) == 0) {
441 BT_DBG("Remove unhandled req_info %s", address);
442 g_free(req_info->addr);
443 gatt_server_requests = g_slist_remove(gatt_server_requests, req_info);
449 void _bt_get_adv_handle_from_instance(int server_inst, int *adv_handle)
453 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
454 if (numapps[k].is_initialized == 1 && numapps[k].instance_id == server_inst) {
455 *adv_handle = numapps[k].adv_handle;
461 char * _bt_gatt_get_default_gatt_client_uuid(void)
463 return g_strdup(DEFAULT_GATT_CLIENT_UUID);
467 static void __bt_register_default_gatt_client()
472 uuid_str = _bt_gatt_get_default_gatt_client_uuid();
473 _bt_string_to_uuid(uuid_str, (service_uuid_t*)&uuid);
475 BT_INFO("Register Default GATT client uuid [%s]", uuid_str);
477 if (OAL_STATUS_SUCCESS != gattc_register(&uuid)) /* for only Smart Control */
478 BT_ERR("gattc register failed");
483 int _bt_gatt_init(void)
485 const char *stack_name = NULL;
490 result = gatt_enable();
491 if (result != OAL_STATUS_SUCCESS) {
492 BT_ERR("gatt Init failed");
493 return _bt_convert_oal_status_to_bt_error(result);
496 /* Register gatt event handler */
497 _bt_service_register_event_handler_callback(BT_GATT_MODULE, __bt_gatt_event_handler);
499 __bt_service_reset_gatt_data();
501 stack_name = oal_get_stack_name();
503 if (stack_name && g_strcmp0(stack_name, "bluez") == 0) {
504 /*In the platform, defacult gatt client should be registered */
505 __bt_register_default_gatt_client();
509 return BLUETOOTH_ERROR_NONE;
512 static void __bt_service_reset_gatt_data(void)
516 BT_INFO("Rest numapp");
519 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
520 numapps[k].is_initialized = 0;
521 numapps[k].instance_id = -1;
522 numapps[k].adv_handle = 0;
523 numapps[k].adv_instance = -1;
524 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
525 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
526 memset(numapps[k].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
527 memset(numapps[k].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
528 numapps[k].adv_data_len = 0;
529 numapps[k].scan_rsp_len = 0;
532 numapps[k].client_id = -1;
533 memset(numapps[k].address.addr, 0x00, BLUETOOTH_ADDRESS_LENGTH);
534 numapps[k].is_watcher_enabled = FALSE;
538 void _bt_gatt_deinit(void)
540 BT_INFO("GATT deinit");
542 /* Un-register the default gatt client before */
543 __bt_unregister_gatt_client_instance(gatt_default_client);
545 if (OAL_STATUS_SUCCESS != gatt_disable())
546 BT_ERR("gatt deinit failed");
548 /* Un-register gatt event handler */
549 _bt_service_unregister_event_handler_callback(BT_GATT_MODULE);
551 __bt_service_reset_gatt_data();
554 void _bt_update_adv_handle(const char *sender, int adv_handle)
557 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
558 bt_service_app_info_t *info = NULL;
560 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
562 /* Do not update client instance */
563 if (info->instance_id == -1)
565 /* Search for a app which has same sender and adv handle as 0 */
566 if (!g_strcmp0(info->sender, sender) && info->adv_handle == 0)
567 info->adv_handle = adv_handle;
571 int _bt_is_sender_gatt_server_with_no_adv(const char *sender, int adv_handle)
574 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
575 bt_service_app_info_t *info = NULL;
577 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
579 /* Search for a app which has same sender and adv handle as 0
580 It is possible that same sender but different adv handle */
581 if (!g_strcmp0(info->sender, sender) && info->adv_handle == 0) {
582 //info->adv_handle = adv_handle;
583 return info->instance_id;
589 int _bt_get_allocated_server_instance(const char *sender, int adv_handle, gboolean use_reserved_slot)
592 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
593 bt_service_app_info_t *info = NULL;
595 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
598 /* Exact matching of Adv handle + sender combination */
599 if (!g_strcmp0(info->sender, sender) && info->adv_handle == adv_handle)
600 return info->instance_id;
602 if (!g_strcmp0(info->sender, sender) && info->adv_handle == -1)
603 return info->instance_id;
609 char * _bt_get_sender_and_handle(int server_instance, int *adv_handle)
612 bt_service_app_info_t *info = NULL;
614 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
617 if (info->instance_id == server_instance && info->adv_handle != 0) {
618 *adv_handle = info->adv_handle;
619 BT_DBG("Server instance [%d] Adv handle [%d] Sender [%s]", server_instance, *adv_handle, info->sender);
620 return g_strdup(info->sender);
626 void _bt_set_new_adv_data(bluetooth_advertising_data_t *adv, int len, int instance)
630 bt_service_app_info_t *info = NULL;
631 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
634 if (info->instance_id == instance) {
635 memcpy(info->adv_data.data, &adv->data, len);
641 void _bt_set_new_scan_rsp_data(bluetooth_scan_resp_data_t *scan, int len, int instance)
645 bt_service_app_info_t *info = NULL;
646 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
649 if (info->instance_id == instance) {
650 memcpy(info->scan_rsp.data, &scan->data, len);
656 void _bt_get_previous_adv_data(bluetooth_advertising_data_t *adv, int *len, int instance)
659 bt_service_app_info_t *info = NULL;
661 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
664 if (info->instance_id == instance) {
665 memcpy(&adv->data, info->adv_data.data, info->adv_data_len);
666 *len = info->adv_data_len;
672 void _bt_get_previous_scan_rsp_data(bluetooth_scan_resp_data_t *scan, int *len, int instance)
676 bt_service_app_info_t *info = NULL;
678 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
681 if (info->instance_id == instance) {
682 memcpy(&scan->data, info->scan_rsp.data, info->scan_rsp_len);
683 *len = info->scan_rsp_len;
689 static gboolean __bt_check_service_uuid_registered(const char *service_uuid)
693 bt_service_app_info_t *info = NULL;
695 bt_service_handle_uuid_info_t *uuid_info = NULL;
697 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
700 for (l = info->service_handle_uuids; l != NULL; l = g_slist_next(l)) {
704 if (!g_strcmp0(uuid_info->service_uuid, service_uuid)) {
705 BT_INFO("Same Service UUID is registered");
714 static void __bt_remove_service_uuid(int service_handle)
718 bt_service_app_info_t *info = NULL;
720 bt_service_handle_uuid_info_t *uuid_info = NULL;
721 bool removed = false;
723 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
726 for (l = info->service_handle_uuids; l != NULL;) {
731 if (uuid_info->service_handle == service_handle) {
732 BT_INFO("Removed Service handle [%d] Service UUID [%s]",
733 uuid_info->service_handle, uuid_info->service_uuid);
734 info->service_handle_uuids = g_slist_remove(info->service_handle_uuids, uuid_info);
735 g_free(uuid_info->service_uuid);
747 static int __bt_unregister_gatt_client_instance(int client_if)
749 int ret = OAL_STATUS_SUCCESS;
752 BT_INFO("DeAllocate client instance ID [%d]", client_if);
754 /* Reset data: instance_id parameter could be either for GATT Server or for GATT client */
755 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
756 if (numapps[k].client_id == client_if) {
757 BT_INFO("This is a GATT client app, unregister: Slot [%d] vacant", k);
758 numapps[k].client_id = -1;
759 numapps[k].is_initialized = FALSE;
760 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
761 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
762 memset(&numapps[k].address.addr, 0x00, sizeof(bluetooth_device_address_t));
764 /* Its a GATT Client Instance */
765 ret = gattc_deregister(client_if);
766 if (ret != OAL_STATUS_SUCCESS) {
767 BT_ERR("DeAllocate GATT Client instance with stack Fail ret: %d", ret);
768 return _bt_convert_oal_status_to_bt_error(ret);
773 return BLUETOOTH_ERROR_NONE;
776 static int __bt_unregister_gatt_server_instance(int server_instance)
778 int ret = OAL_STATUS_SUCCESS;
781 /* Unregister the server instance */
782 ret = gatts_unregister(server_instance);
783 if (ret != OAL_STATUS_SUCCESS) {
784 BT_ERR("DeAllocate server instance with stack Fail ret: %d", ret);
785 return _bt_convert_oal_status_to_bt_error(ret);
787 BT_INFO("DeAllocated server instance with stack successful..");
790 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
791 if (numapps[k].instance_id == server_instance) {
792 numapps[k].is_initialized = 0;
793 numapps[k].instance_id = -1;
794 numapps[k].adv_handle = 0;
795 numapps[k].adv_instance = -1;
796 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
797 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
798 memset(numapps[k].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
799 memset(numapps[k].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
800 numapps[k].adv_data_len = 0;
801 numapps[k].scan_rsp_len = 0;
805 BT_DBG("Going8 to reset numapp block num [%d]", k);
806 return BLUETOOTH_ERROR_NONE;
809 static void __bt_remove_all_service_handles(bt_service_app_info_t *app_info)
813 int ret = OAL_STATUS_SUCCESS;
816 if (app_info == NULL)
819 count = g_slist_length(app_info->service_handles);
820 BT_INFO("Before handle removal: current count [%d]", count);
822 for (l = app_info->service_handles; l != NULL;) {
827 BT_INFO("Server Handle to be Removed [%d] Instance ID [%d]", *handle, app_info->instance_id);
828 if (_bt_gatt_server_stop_service(app_info->sender, *handle, app_info->instance_id) != BLUETOOTH_ERROR_NONE)
831 ret = gatts_delete_service(app_info->instance_id, *handle);
832 if (ret != OAL_STATUS_SUCCESS) {
833 BT_ERR("ret: %d", ret);
836 app_info->service_handles = g_slist_remove(app_info->service_handles, handle);
837 /* Remove Service UUID from the list */
838 __bt_remove_service_uuid(*handle);
841 count = g_slist_length(app_info->service_handles);
842 BT_INFO("After deleting current count [%d]", count);
848 int _bt_unregister_server_instance(const char *sender, int adv_handle)
850 BT_INFO("Unregister Allocated server instance request Sender [%s] Adv handle [%d]", sender, adv_handle);
851 int result = BLUETOOTH_ERROR_NONE;
852 int apps[MAX_APPS_SUPPORTED];
855 bt_service_app_info_t *info = NULL;
857 memset(&apps, -1, sizeof(apps));
859 if (adv_handle == 0) {
860 BT_DBG("Its a direct GATT Server app request to unregister");
861 /* Unregister server instance for each app with same sender (case: GATT Server with multiple adv handle) */
863 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
866 /* Exact matching of sender */
867 if (!g_strcmp0(info->sender, sender)) {
868 BT_INFO("Unregister GATT server instance [%d]", info->instance_id);
869 /* Unregister all service handles with stack */
870 __bt_remove_all_service_handles(info);
872 /* Disable adv if running */
873 BT_INFO("Disable Advertising Adv Handle [%d] sender [%s]",
874 info->adv_handle, sender);
875 _bt_set_advertising(sender, info->adv_handle, FALSE, FALSE);
877 /* Save all instances which need to be unregistered */
878 apps[info->instance_id] = 1;
882 BT_DBG("Its an Internal unregister request by adv application");
883 server_instance = _bt_get_allocated_server_instance(sender, adv_handle, FALSE);
884 BT_DBG("Its an Internal unregister request by adv application: Adv disabled srv instance [%d]", server_instance);
885 if (server_instance == -1) {
886 BT_ERR("No allocated server instance to be removed");
887 return BLUETOOTH_ERROR_INVALID_PARAM;
890 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
892 if (info->instance_id == server_instance) {
893 if (info->service_handles == NULL) {
894 BT_INFO("There are no Service handles with this app, safe to unregister");
895 /* Unregister server instance only if this sender does not have any gatt services in it */
896 result = __bt_unregister_gatt_server_instance(server_instance);
898 info->adv_handle = 0;
899 memset(info->adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
900 info->adv_data_len = 0;
901 memset(info->scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
902 info->scan_rsp_len = 0;
909 /* Unregister all apps one by one */
910 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
912 BT_INFO("Unregister app[%d]", k);
913 /* Unregister server instance */
914 __bt_unregister_gatt_server_instance(k);
921 int _bt_register_server_instance(const char *sender, int adv_handle)
923 int ret = OAL_STATUS_SUCCESS;
924 char *uuid_string = NULL;
929 BT_INFO("Check on which instance Server instance can be initialized....");
930 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
931 if (numapps[k].is_initialized == 1 || strlen(numapps[k].uuid) > 0) {
932 BT_DBG("Instance ID [%d] is already in use..Check next slot", numapps[k].instance_id);
935 BT_DBG("Time to register GATT Server..UUID to be used is [%s] slot [%d]", uuid_list[slot-1], slot);
941 BT_ERR("No Slot if free for GATT Server registration..");
942 return BLUETOOTH_ERROR_REGISTRATION_FAILED;
945 uuid_string = g_malloc0(BT_UUID_STRING_MAX);
946 _bt_string_to_uuid(uuid_list[slot-1], (service_uuid_t*)&uuid);
947 g_strlcpy(uuid_string, uuid_list[slot-1], BT_UUID_STRING_MAX);
948 BT_INFO("Copied UUID string [%s] slot [%d]", uuid_string, slot);
950 /* Register GATT Server */
951 ret = gatts_register(&uuid);
952 if (ret != OAL_STATUS_SUCCESS) {
953 BT_ERR("ret: %d", ret);
955 return _bt_convert_oal_status_to_bt_error(ret);
957 BT_DBG("GATT Server registration call successfully accepted by OAL..wait for Instance Initialized event from OAL..");
958 /* Return & wait for GATT Server Instance Initialization event */
959 memset(numapps[slot].sender, 0x00, sizeof(numapps[slot].sender));
960 memset(numapps[slot].uuid, 0x00, sizeof(numapps[slot].uuid));
962 g_strlcpy(numapps[slot].sender, sender, sizeof(numapps[slot].sender));
963 g_strlcpy(numapps[slot].uuid, uuid_string, sizeof(numapps[slot].uuid));
965 numapps[slot].is_initialized = 0; /* Set initialization from app registered callback */
966 numapps[slot].adv_handle = adv_handle;
969 return BLUETOOTH_ERROR_NONE;
973 static void __bt_gatt_get_pending_request_info(int service_function,
977 invocation_info_t *req_info = NULL;
979 for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
981 if (req_info == NULL)
984 if (req_info->service_function == service_function) {
985 *sender = req_info->sender;
991 static void __bt_gatt_cleanup_invocation_on_gatt_disconnection(int result,
992 void *data, unsigned int size)
997 invocation_info_t *req_info = NULL;
998 ret_if(data == NULL);
999 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
1001 for (l = _bt_get_invocation_list(); l != NULL; ) {
1003 l = g_slist_next(l);
1005 if (req_info == NULL)
1008 switch (req_info->service_function) {
1009 case BT_GATT_GET_PRIMARY_SERVICES: {
1010 if(!g_strcmp0((char*)req_info->user_data, (char*)data)){
1011 bt_services_browse_info_t param;
1012 memset(¶m, 0, sizeof(bt_services_browse_info_t));
1013 _bt_convert_addr_string_to_type(param.device_addr.addr, (char*)data);
1014 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1015 g_array_append_vals(out_param, ¶m, sizeof(bt_services_browse_info_t));
1016 _bt_service_method_return(req_info->context, out_param, result);
1017 BT_DBG("BT_GATT_GET_PRIMARY_SERVICES request cleared for address [%s]", (char*)data);
1018 _bt_free_info_from_invocation_list(req_info);
1019 g_array_free(out_param, TRUE);
1023 case BT_GATT_GET_SERVICE_PROPERTIES: {
1024 bluetooth_gatt_client_svc_prop_info_t *prop = (bluetooth_gatt_client_svc_prop_info_t*)req_info->user_data;
1025 _bt_convert_addr_type_to_string(addr, prop->device_address.addr);
1026 if (!g_strcmp0(addr, (char*)data)){
1027 bt_char_browse_info_t param;
1028 memset(¶m, 0, sizeof(bt_char_browse_info_t));
1029 _bt_convert_addr_string_to_type(param.device_addr.addr,addr);
1030 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1031 g_array_append_vals(out_param, ¶m, sizeof(bt_char_browse_info_t));
1032 _bt_service_method_return(req_info->context, out_param, result);
1033 BT_DBG("BT_GATT_GET_SERVICE_PROPERTIES request cleared for address [%s]", addr);
1034 _bt_free_info_from_invocation_list(req_info);
1035 g_array_free(out_param, TRUE);
1039 case BT_GATT_GET_CHARACTERISTIC_PROPERTIES: {
1040 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
1041 _bt_convert_addr_type_to_string(addr, prop->device_address.addr);
1042 if (!g_strcmp0(addr, (char*)data)){
1043 bt_descriptor_browse_info_t param;
1044 memset(¶m, 0, sizeof(bt_descriptor_browse_info_t));
1045 _bt_convert_addr_string_to_type(param.device_addr.addr, addr);
1046 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1047 g_array_append_vals(out_param, ¶m, sizeof(bt_descriptor_browse_info_t));
1048 _bt_service_method_return(req_info->context, out_param, result);
1049 BT_DBG("BT_GATT_GET_CHARACTERISTIC_PROPERTIES request cleared for address [%s]", addr);
1050 _bt_free_info_from_invocation_list(req_info);
1051 g_array_free(out_param, TRUE);
1055 case BT_GATT_WATCH_CHARACTERISTIC: {
1056 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
1057 _bt_convert_addr_type_to_string(addr, prop->device_address.addr);
1058 if (!g_strcmp0(addr, (char*)data)){
1059 bt_gatt_notif_reg_info_t param;
1060 memset(¶m, 0, sizeof(bt_gatt_notif_reg_info_t));
1061 _bt_convert_addr_string_to_type(param.addr.addr, addr);
1062 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1063 g_array_append_vals(out_param, ¶m, sizeof(bt_gatt_notif_reg_info_t));
1064 _bt_service_method_return(req_info->context, out_param, result);
1065 _bt_free_info_from_invocation_list(req_info);
1066 g_array_free(out_param, TRUE);
1070 case BT_GATT_READ_CHARACTERISTIC:
1071 case BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE: {
1072 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
1073 _bt_convert_addr_type_to_string(addr, prop->device_address.addr);
1074 if (!g_strcmp0(addr, (char*)data)){
1075 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1076 g_array_append_vals(out_param, prop, sizeof(bluetooth_gatt_client_char_prop_info_t));
1077 _bt_service_method_return(req_info->context, out_param, result);
1078 BT_DBG("BT_GATT_READ/WRITE_CHARACTERISTIC request cleared for address [%s]", addr);
1079 _bt_free_info_from_invocation_list(req_info);
1080 g_array_free(out_param, TRUE);
1084 case BT_GATT_READ_DESCRIPTOR_VALUE:
1085 case BT_GATT_WRITE_DESCRIPTOR_VALUE: {
1086 bluetooth_gatt_client_desc_prop_info_t *prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
1087 _bt_convert_addr_type_to_string(addr, prop->device_address.addr);
1088 if (!g_strcmp0(addr, (char*)data)){
1089 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1090 g_array_append_vals(out_param, prop, sizeof(bluetooth_gatt_client_desc_prop_info_t));
1091 _bt_service_method_return(req_info->context, out_param, result);
1092 BT_DBG("BT_GATT_READ/WRITE_DESCRIPTOR_VALUE request cleared for address [%s]", addr);
1093 _bt_free_info_from_invocation_list(req_info);
1094 g_array_free(out_param, TRUE);
1098 case BT_REQ_ATT_MTU: {
1099 char *temp_addr = (char*)req_info->user_data;
1100 bluetooth_device_address_t address;
1101 if (!g_strcmp0(temp_addr, (char*)data)) {
1102 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1103 _bt_convert_addr_string_to_type(address.addr, temp_addr);
1104 g_array_append_vals(out_param, (bluetooth_device_address_t*)&address,
1105 sizeof(bluetooth_device_address_t));
1106 _bt_service_method_return(req_info->context, out_param, result);
1107 _bt_free_info_from_invocation_list(req_info);
1108 g_array_free(out_param, TRUE);
1119 static void __bt_gatt_handle_pending_request_info(int result,
1120 int service_function, void *data, unsigned int size)
1124 invocation_info_t *req_info = NULL;
1125 ret_if(data == NULL);
1127 for (l = _bt_get_invocation_list(); l != NULL; ) {
1129 l = g_slist_next(l);
1130 if (req_info == NULL || req_info->service_function != service_function)
1133 switch (service_function) {
1135 case BT_GATT_SERVER_REGISTER: {
1136 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
1138 if (!g_strcmp0(req_info->sender, param->sender)) {
1139 BT_DBG("GATT Server app found [%s]", req_info->sender);
1141 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1142 g_array_append_vals(out_param, ¶m->instance_id, sizeof(int));
1143 _bt_service_method_return(req_info->context, out_param, result);
1145 _bt_free_info_from_invocation_list(req_info);
1146 g_array_free(out_param, TRUE);
1150 case BT_GATT_SERVER_START_SERVICE:
1151 case BT_GATT_SERVER_DELETE_SERVICE: {
1152 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
1154 int *saved_instance_id = (int*)req_info->user_data;
1155 if (!g_strcmp0(req_info->sender, param->sender) && param->instance_id == *saved_instance_id) {
1156 BT_DBG("GATT Server app found [%s] Instance ID [%d] Reply DBUS",
1157 req_info->sender, *saved_instance_id);
1159 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1160 g_array_append_vals(out_param, &saved_instance_id, sizeof(int));
1161 _bt_service_method_return(req_info->context, out_param, result);
1163 _bt_free_info_from_invocation_list(req_info);
1164 g_array_free(out_param, TRUE);
1168 case BT_GATT_SERVER_ADD_SERVICE:
1169 case BT_GATT_SERVER_ADD_DESCRIPTOR:
1170 case BT_GATT_SERVER_ADD_CHARACTERISTIC: {
1171 int *handle = (int*)data;
1172 BT_DBG("Characteristic added: Handle [%d]", *handle);
1173 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1175 g_array_append_vals(out_param, handle, sizeof(int));
1176 _bt_service_method_return(req_info->context, out_param, result);
1178 _bt_free_info_from_invocation_list(req_info);
1179 g_array_free(out_param, TRUE);
1184 case BT_DISCONNECT_LE: {
1185 char *addr = (char*)req_info->user_data;
1186 bluetooth_device_address_t address;
1188 if (!g_strcmp0(addr, (char*)data)) {
1189 BT_INFO("GATT Client connect-disconnect call pending for app [%s] addr [%s]",
1190 req_info->sender, addr + 12);
1191 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1192 _bt_convert_addr_string_to_type(address.addr, addr);
1194 g_array_append_vals(out_param, (bluetooth_device_address_t*)&address,
1195 sizeof(bluetooth_device_address_t));
1196 _bt_service_method_return(req_info->context, out_param, result);
1198 _bt_free_info_from_invocation_list(req_info);
1199 g_array_free(out_param, TRUE);
1203 case BT_GATT_CLIENT_REGISTER: {
1204 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
1206 if (!g_strcmp0(req_info->sender, param->sender)) {
1207 BT_DBG("GATT Client app found [%s] created client ID [%d]",
1208 req_info->sender, param->client_id);
1210 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1211 g_array_append_vals(out_param, ¶m->client_id, sizeof(int));
1212 _bt_service_method_return(req_info->context, out_param, result);
1214 _bt_free_info_from_invocation_list(req_info);
1215 g_array_free(out_param, TRUE);
1219 case BT_GATT_GET_PRIMARY_SERVICES: {
1220 bt_services_browse_info_t *param = (bt_services_browse_info_t*)data;
1221 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1222 _bt_convert_addr_type_to_string(address,
1223 (unsigned char *)(¶m->device_addr.addr));
1225 /* Match address to determine same request */
1226 if (!g_strcmp0((char*)req_info->user_data, address)) {
1227 BT_DBG("GATT Client app found [%s] Remote address [%s]",
1228 req_info->sender, address);
1230 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1231 g_array_append_vals(out_param, param,
1232 sizeof(bt_services_browse_info_t));
1234 //g_array_append_vals(out_param, ¶m->client_id, sizeof(int));
1235 _bt_service_method_return(req_info->context, out_param, result);
1237 _bt_free_info_from_invocation_list(req_info);
1238 g_array_free(out_param, TRUE);
1243 case BT_GATT_GET_SERVICE_PROPERTIES: {
1244 bt_char_browse_info_t param;
1245 memcpy((void*)¶m, data, sizeof(bt_char_browse_info_t));
1246 //bt_char_browse_info_t *param = (bt_char_browse_info_t*)data;
1248 bluetooth_gatt_client_svc_prop_info_t *prop = (bluetooth_gatt_client_svc_prop_info_t*)req_info->user_data;
1250 /* Match both address and service properties to determine same request */
1251 if (!memcmp(param.device_addr.addr,
1252 prop->device_address.addr,
1253 sizeof(bluetooth_device_address_t)) &&
1254 !memcmp(param.svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1255 param.svc_inst_id == prop->svc.instance_id) {
1256 BT_DBG("Returning Service properties");
1258 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1259 g_array_append_vals(out_param, ¶m, sizeof(bt_char_browse_info_t));
1260 _bt_service_method_return(req_info->context, out_param, result);
1262 _bt_free_info_from_invocation_list(req_info);
1263 g_array_free(out_param, TRUE);
1267 case BT_GATT_GET_CHARACTERISTIC_PROPERTIES: {
1268 bt_descriptor_browse_info_t *param = (bt_descriptor_browse_info_t*)data;
1270 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
1272 /* Match both address, service properties &char properties to determine same request */
1273 if (!memcmp(param->device_addr.addr,
1274 prop->device_address.addr,
1275 sizeof(bluetooth_device_address_t)) &&
1276 !memcmp(param->svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1277 param->svc_inst_id == prop->svc.instance_id &&
1278 !memcmp(param->char_uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1279 param->char_inst_id == prop->characteristic.instance_id) {
1280 BT_DBG("Returning Characteristic properties");
1281 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1282 g_array_append_vals(out_param, param, sizeof(bt_descriptor_browse_info_t));
1283 _bt_service_method_return(req_info->context, out_param, result);
1285 _bt_free_info_from_invocation_list(req_info);
1286 g_array_free(out_param, TRUE);
1290 case BT_GATT_WATCH_CHARACTERISTIC: {
1291 bt_gatt_notif_reg_info_t *param = (bt_gatt_notif_reg_info_t*)data;
1292 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
1294 /* Match both address, service properties &char properties to determine same request */
1295 if (!memcmp(param->addr.addr,
1296 prop->device_address.addr,
1297 sizeof(bluetooth_device_address_t)) &&
1298 !memcmp(param->svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1299 param->svc_inst == prop->svc.instance_id &&
1300 !memcmp(param->char_uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1301 param->char_inst == prop->characteristic.instance_id) {
1302 BT_INFO("Characteristic Watch Successful: Is registered [%d]",
1303 param->is_registered);
1304 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1305 g_array_append_vals(out_param, param, sizeof(bt_gatt_notif_reg_info_t));
1306 _bt_service_method_return(req_info->context, out_param, result);
1307 _bt_free_info_from_invocation_list(req_info);
1308 g_array_free(out_param, TRUE);
1312 case BT_GATT_READ_CHARACTERISTIC:
1313 case BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE: {
1314 bluetooth_gatt_client_char_prop_info_t *param = (bluetooth_gatt_client_char_prop_info_t*)data;
1316 char *addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
1317 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
1318 _bt_convert_addr_type_to_string(addr, prop->device_address.addr);
1320 if (!memcmp(param, prop, sizeof(bluetooth_gatt_client_char_prop_info_t))) {
1321 BT_DBG("Gatt Char read or write request matched for address [%s]", addr);
1322 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1323 g_array_append_vals(out_param, param, sizeof(bluetooth_gatt_client_char_prop_info_t));
1324 _bt_service_method_return(req_info->context, out_param, result);
1326 _bt_free_info_from_invocation_list(req_info);
1327 g_array_free(out_param, TRUE);
1332 case BT_GATT_READ_DESCRIPTOR_VALUE:
1333 case BT_GATT_WRITE_DESCRIPTOR_VALUE: {
1334 bluetooth_gatt_client_desc_prop_info_t *param = (bluetooth_gatt_client_desc_prop_info_t*)data;
1336 char *addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
1337 bluetooth_gatt_client_desc_prop_info_t *prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
1338 _bt_convert_addr_type_to_string(addr, prop->device_address.addr);
1340 if (!memcmp(param, prop, sizeof(bluetooth_gatt_client_desc_prop_info_t))) {
1341 BT_DBG("Descriptor read or write request matched for address [%s]", addr);
1342 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1343 g_array_append_vals(out_param, param, sizeof(bluetooth_gatt_client_desc_prop_info_t));
1344 _bt_service_method_return(req_info->context, out_param, result);
1346 _bt_free_info_from_invocation_list(req_info);
1347 g_array_free(out_param, TRUE);
1352 case BT_REQ_ATT_MTU: {
1353 char *addr = (char*)req_info->user_data;
1354 bluetooth_device_address_t address;
1356 if (!g_strcmp0(addr, (char*)data)) {
1357 BT_DBG("GATT Client BT_REQ_ATT_MTU call pending for app [%s] addr [%s]",
1358 req_info->sender, addr);
1359 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1360 _bt_convert_addr_string_to_type(address.addr, addr);
1362 g_array_append_vals(out_param, (bluetooth_device_address_t*)&address,
1363 sizeof(bluetooth_device_address_t));
1364 _bt_service_method_return(req_info->context, out_param, result);
1366 _bt_free_info_from_invocation_list(req_info);
1367 g_array_free(out_param, TRUE);
1376 static void __bt_handle_server_instance_registered(event_gatts_register_t *data)
1378 bt_service_app_info_t *info = NULL;
1380 char *uuid_string = g_malloc0(BT_UUID_STRING_MAX);
1382 _bt_uuid_to_string(&(data->server_uuid), uuid_string);
1383 BT_INFO("Instance ID is Intialized [%d] UUID initialized [%s]", data->server_inst, uuid_string);
1385 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1386 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1389 if (g_strcmp0(info->uuid, uuid_string) == 0) {
1390 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1391 info->is_initialized = TRUE;
1392 info->instance_id = data->server_inst;
1393 info->adv_instance = data->server_inst;
1394 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE, BT_GATT_SERVER_REGISTER,
1395 (void*)info, sizeof(bt_service_app_info_t));
1399 g_free(uuid_string);
1402 static void __bt_handle_gatt_server_service_added(event_gatts_srvc_prm_t *event)
1404 int result = BLUETOOTH_ERROR_NONE;
1408 bt_service_app_info_t *info = NULL;
1411 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1412 _bt_uuid_to_string(&(event->gatt_srvc_id.id.uuid), uuid_str);
1413 BT_INFO("GATT Added Service UUID: [%s] Server Ins [%d] Service hdl [%d] Is Primary [%d]\n",
1414 uuid_str, event->gatt_srvc_stat.server_inst, svc_handle, event->gatt_srvc_id.is_prmry);
1416 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1417 result = BLUETOOTH_ERROR_INTERNAL;
1418 svc_handle = 0; /* Service handle set to 0 indicates.
1419 0 is reserved by BT SIG, cant be used by app*/
1421 svc_handle = event->gatt_srvc_stat.servic_hndl;
1423 BT_INFO("GATT Added Service Status [%d] Server Ins [%d] Service hdl [%d] Is Primary [%d]\n",
1424 event->gatt_srvc_stat.status, event->gatt_srvc_stat.server_inst, svc_handle, event->gatt_srvc_id.is_prmry);
1426 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1427 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1430 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1431 BT_INFO("numapps[%d] Found GATT Server.. UUID [%s], sender [%s]", k, info->uuid, info->sender);
1432 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_SERVICE,
1433 (int*)&svc_handle, sizeof(int));
1435 /* Add Service Handle */
1436 if (svc_handle > 0) {
1437 bt_service_handle_uuid_info_t *uuid_info;
1438 handle = g_malloc0(sizeof(int));
1439 *handle = svc_handle;
1440 numapps[k].service_handles = g_slist_append(numapps[k].service_handles, handle);
1441 count = g_slist_length(numapps[k].service_handles);
1442 BT_INFO("Added Service handle [%d] to list..current count [%d]", svc_handle, count);
1443 /* Add Service UUID to the list */
1444 uuid_info = g_malloc0(sizeof(bt_service_handle_uuid_info_t));
1445 uuid_info->service_handle = svc_handle;
1446 uuid_info->service_uuid = g_strdup(uuid_str);
1447 numapps[k].service_handle_uuids = g_slist_append(numapps[k].service_handle_uuids, uuid_info);
1456 static void __bt_handle_gatt_server_characteristic_added(event_gatts_srvc_charctr_t *event)
1458 int result = BLUETOOTH_ERROR_NONE;
1460 bt_service_app_info_t *info = NULL;
1462 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1464 BT_INFO("GATT Server Char added status [%d]", event->gatt_srvc_stat.status);
1465 BT_INFO("GATT Server_inst: [%d]", event->gatt_srvc_stat.server_inst);
1466 BT_INFO("GATT Service Handle: [%d]", event->gatt_srvc_stat.servic_hndl);
1467 BT_INFO("GATT Add characteristic Status: [%d]", event->gatt_srvc_stat.status);
1468 BT_INFO("GATT Service characteristic Handle: [%d]", event->charctr_hndl);
1470 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1471 result = BLUETOOTH_ERROR_INTERNAL;
1472 char_handle = 0; /* characteristic handle set to 0 indicates.
1473 0 is reserved by BT SIG, cant be used by app*/
1475 char_handle = event->charctr_hndl;
1478 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1479 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1482 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1483 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1484 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_CHARACTERISTIC,
1485 (int*)&char_handle, sizeof(int));
1491 _bt_uuid_to_string(&(event->charctr_uuid), uuid_str);
1492 BT_INFO("GATT Added Characteristic: UUID: [%s]", uuid_str);
1497 static void __bt_handle_gatt_server_descriptor_added(event_gatts_srvc_descr_t* event)
1499 int result = BLUETOOTH_ERROR_NONE;
1501 bt_service_app_info_t *info = NULL;
1503 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1505 BT_INFO("GATT Server Descriptor added status [%d]", event->gatt_srvc_stat.status);
1506 BT_INFO("GATT Server_inst: [%d]", event->gatt_srvc_stat.server_inst);
1507 BT_INFO("GATT Service Handle: [%d]", event->gatt_srvc_stat.servic_hndl);
1508 BT_INFO("GATT Add Descriptor Status: [%d]", event->gatt_srvc_stat.status);
1509 BT_INFO("GATT Service Descriptor Handle: [%d]", event->descrptr_hndl);
1511 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1512 result = BLUETOOTH_ERROR_INTERNAL;
1513 desc_handle = 0; /* Service handle set to 0 indicates.
1514 0 is reserved by BT SIG, cant be used by app*/
1516 desc_handle = event->descrptr_hndl;
1518 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1519 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1522 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1523 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1524 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_DESCRIPTOR,
1525 (int*)&desc_handle, sizeof(int));
1530 _bt_uuid_to_string(&(event->descrptr_uuid), uuid_str);
1531 BT_INFO("GATT Added Descriptor: UUID: [%s]", uuid_str);
1536 static void __bt_handle_gatt_server_service_started(event_gatts_srvc_t *event)
1538 bt_service_app_info_t *info = NULL;
1539 int result = BLUETOOTH_ERROR_NONE;
1541 BT_INFO("GATT Server Service Started..");
1544 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1545 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1546 BT_INFO("GATT Service Started Status: [%d]", event->status);
1548 if (event->status != OAL_STATUS_SUCCESS) {
1549 BT_ERR("GATT Server Service Start Failed Err: [%d]", event->status);
1550 result = BLUETOOTH_ERROR_INTERNAL;
1553 /* Check if the just registered Instance ID belongs to requester */
1554 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1557 if (info->instance_id == event->server_inst) {
1558 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1559 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_START_SERVICE,
1560 (void*)info, sizeof(bt_service_app_info_t));
1566 static void __bt_handle_gatt_server_service_stopped(event_gatts_srvc_t *event)
1568 int result = BLUETOOTH_ERROR_NONE;
1569 bt_service_app_info_t *info = NULL;
1571 BT_INFO("GATT Server Service Stopped..");
1573 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1574 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1575 BT_INFO("GATT Service Stopped Status: [%d]", event->status);
1577 if (event->status != OAL_STATUS_SUCCESS) {
1578 BT_ERR("GATT Server Service Stop Failed Err: [%d]", event->status);
1579 result = BLUETOOTH_ERROR_INTERNAL;
1582 /* Check if the just registered Instance ID belongs to requester */
1583 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1586 if (info->instance_id == event->server_inst) {
1587 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1588 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_STOP_SERVICE,
1589 (void*)info, sizeof(bt_service_app_info_t));
1595 static void __bt_handle_gatt_server_service_deleted(event_gatts_srvc_t *event)
1597 int result = BLUETOOTH_ERROR_NONE;
1598 bt_service_app_info_t *info = NULL;
1600 BT_INFO("GATT Server Service Deleted..");
1602 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1603 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1604 BT_INFO("GATT Service Deleted Status: [%d]", event->status);
1606 if (event->status != OAL_STATUS_SUCCESS) {
1607 BT_ERR("GATT Server Service Delete Failed Err: [%d]", event->status);
1608 result = BLUETOOTH_ERROR_INTERNAL;
1611 /* Check if the just registered Instance ID belongs to requester */
1612 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1615 if (info->instance_id == event->server_inst) {
1616 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1617 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_DELETE_SERVICE,
1618 (void*)info, sizeof(bt_service_app_info_t));
1624 struct gatt_client_info_t *_bt_find_remote_gatt_client_info(char *address)
1627 struct gatt_client_info_t *info = NULL;
1628 for (l = gatt_client_info_list; l != NULL; l = g_slist_next(l)) {
1629 info = (struct gatt_client_info_t*)l->data;
1633 if (!g_strcmp0(info->addr, address)) {
1634 BT_DBG("Remote GATT client found addr[%s]", info->addr);
1641 static struct gatt_client_info_t *__bt_find_remote_gatt_client_info_from_conn_id(int conn_id)
1644 struct gatt_client_info_t *info = NULL;
1646 for (l = gatt_client_info_list; l != NULL; l = g_slist_next(l)) {
1647 info = (struct gatt_client_info_t*)l->data;
1651 if (info->connection_id == conn_id) {
1652 BT_INFO("Remote GATT client found addr[%s]", info->addr);
1659 struct gatt_server_info_t *_bt_find_remote_gatt_server_info(char *address)
1662 struct gatt_server_info_t *info = NULL;
1663 for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
1664 info = (struct gatt_server_info_t*)l->data;
1668 if (!g_strcmp0(info->addr, address)) {
1669 BT_DBG("Remote GATT Server found addr[%s] connection_id %d", info->addr, info->connection_id);
1676 static struct gatt_out_conn_info_t* __bt_find_gatt_outgoing_conn_info(char *address)
1679 struct gatt_out_conn_info_t *info = NULL;
1680 for (l = outgoing_gatt_conn_list; l != NULL; l = g_slist_next(l)) {
1681 info = (struct gatt_out_conn_info_t*)l->data;
1685 if (!g_strcmp0(info->addr, address)) {
1686 BT_INFO("Outgoing connection info found addr[%s]", info->addr + 12);
1693 static void __bt_handle_gatt_server_connection_state(event_gatts_conn_t *event)
1695 int result = BLUETOOTH_ERROR_NONE;
1696 struct gatt_client_info_t *client_info = NULL;
1697 #ifndef __INTEGRATE_GATT_INFO__
1698 struct gatt_server_info_t *server_info = NULL;
1700 bluetooth_device_address_t dev_addr;
1701 GVariant *param = NULL;
1704 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1706 memcpy(dev_addr.addr, event->address.addr, 6);
1708 /* REPLY dbus Context to application which called BT_CONNECT_LE. There is status
1710 _bt_convert_addr_type_to_string(address,
1711 (unsigned char *)dev_addr.addr);
1713 if (event->status != OAL_STATUS_SUCCESS)
1714 result = BLUETOOTH_ERROR_INTERNAL;
1716 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1717 address, BT_ADDRESS_STRING_SIZE);
1719 BT_INFO("GATT Server Connedted: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1720 address + 12, event->server_inst, event->conn_id);
1723 /* Check if device is already in connected list */
1724 client_info = _bt_find_remote_gatt_client_info(address);
1727 BT_DBG("Conn Info absent: But no need to Send Local GATT Server Connected event to apps");
1729 param = g_variant_new("(is)", result, address);
1731 /* Send event to application */
1732 _bt_send_event(BT_DEVICE_EVENT,
1733 BLUETOOTH_EVENT_GATT_SERVER_CONNECTED, /* Local device is GATT server */
1736 /* Save client connection info */
1737 client_info = g_new0(struct gatt_client_info_t, 1);
1738 client_info->addr = g_strdup(address);
1739 BT_INFO("Added GATT client addr[%s]", client_info->addr + 12);
1740 client_info->connection_id = event->conn_id;
1741 #ifdef __INTEGRATE_GATT_INFO__
1742 client_info->client_id = -1;
1744 client_info->instance_id = event->server_inst;
1745 gatt_client_info_list = g_slist_append(gatt_client_info_list, client_info);
1746 BT_INFO("Total num of connected Remote GATT Clients [%d]", g_slist_length(gatt_client_info_list));
1748 #ifndef __INTEGRATE_GATT_INFO__
1749 /* Save server connection info */
1750 server_info = g_new0(struct gatt_server_info_t, 1);
1751 server_info->addr = g_strdup(address);
1752 server_info->client_id = -1;
1753 BT_INFO("Added GATT server addr[%s]", server_info->addr + 12);
1754 server_info->connection_id = event->conn_id;
1755 gatt_server_info_list = g_slist_append(gatt_server_info_list, server_info);
1756 BT_INFO("Total num of connected Remote GATT Servers [%d]", g_slist_length(gatt_server_info_list));
1759 ret = gattc_add_connection_info((bt_address_t *)&dev_addr, event->conn_id, event->server_inst);
1760 if (ret != OAL_STATUS_SUCCESS) {
1761 BT_ERR("gattc register server instance failed");
1765 __bt_add_mtu_gatt_device(address);
1770 /* GATT Server Dis connected */
1771 static void __bt_handle_gatt_server_disconnection_state(event_gatts_conn_t *event)
1773 int result = BLUETOOTH_ERROR_NONE;
1774 struct gatt_client_info_t *client_info = NULL;
1775 #ifndef __INTEGRATE_GATT_INFO__
1776 struct gatt_server_info_t *server_info = NULL;
1778 bluetooth_device_address_t dev_addr;
1779 GVariant *param = NULL;
1780 char address[BT_ADDRESS_STRING_SIZE];
1782 memcpy(dev_addr.addr, event->address.addr, 6);
1784 /* REPLY dbus Context to application which called BT_DISCONNECT_LE. There is status
1786 _bt_convert_addr_type_to_string(address,
1787 (unsigned char *)dev_addr.addr);
1789 if (event->status != OAL_STATUS_SUCCESS)
1790 result = BLUETOOTH_ERROR_INTERNAL;
1792 if (NULL == _bt_get_request_info_data(BT_DISCONNECT_LE, address)) {
1793 if (NULL != _bt_get_request_info_data(BT_CONNECT_LE, address)) {
1794 result = BLUETOOTH_ERROR_INTERNAL;
1796 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1797 address, BT_ADDRESS_STRING_SIZE);
1799 BT_ERR("Failed to connect Local GATT Server Remote Client addr[%s]", address + 12);
1803 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE,
1804 address, BT_ADDRESS_STRING_SIZE);
1807 /* Remove previous invalid request info */
1808 __bt_gatt_server_release_request_info(address);
1810 BT_INFO("Local GATT Server DisConnected: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1811 address + 12, event->server_inst, event->conn_id);
1813 #ifdef TIZEN_BLUEDROID_PORTING
1814 __bt_remove_all_prep_write_req(event->conn_id);
1817 /* Remove Connection info */
1818 client_info = _bt_find_remote_gatt_client_info(address);
1820 BT_DBG("No need to Send Local GATT Server Disconnected event to apps, just remove remote client info");
1822 param = g_variant_new("(is)", result, address);
1823 /* Send event to application */
1824 _bt_send_event(BT_DEVICE_EVENT,
1825 BLUETOOTH_EVENT_GATT_SERVER_DISCONNECTED, /* Local device is GATT server */
1828 #ifndef __INTEGRATE_GATT_INFO__
1829 /* Remove server info from list */
1830 server_info = _bt_find_remote_gatt_server_info(address);
1832 gatt_server_info_list = g_slist_remove(gatt_server_info_list, server_info);
1834 /* Remove all services from info list_gatt_info */
1835 __bt_cleanup_remote_services(server_info);
1837 BT_INFO("Can not find conn info, already removed!");
1840 /* Remove all services from info list_gatt_info */
1841 __bt_cleanup_remote_services(client_info);
1844 /* Remove client info from List */
1845 gatt_client_info_list = g_slist_remove(gatt_client_info_list, client_info);
1846 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1847 g_free(client_info->addr);
1848 g_free(client_info);
1851 __bt_remove_mtu_gatt_device(address);
1854 static void __bt_handle_gatt_server_acquire_write_requested(event_gatts_srvc_acquire_attr_t *event)
1856 GVariant *param = NULL;
1857 int result = BLUETOOTH_ERROR_NONE;
1858 struct gatt_server_req_info *req_info = NULL;
1859 bluetooth_device_address_t dev_addr;
1860 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1862 BT_INFO("GATT Server ACQUIRE Write Req");
1863 BT_DBG(" conn id %d, trans id %d, attr andle %d", event->attr_trans.conn_id,
1864 event->attr_trans.trans_id, event->attr_trans.attr_handle);
1866 //address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1867 memcpy(dev_addr.addr, event->address.addr, 6);
1869 req_info = g_new0(struct gatt_server_req_info, 1);
1870 req_info->request_id = event->attr_trans.trans_id;
1871 req_info->attribute_handle = event->attr_trans.attr_handle;
1872 req_info->connection_id = event->attr_trans.conn_id;
1873 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_ACQUIRE_WRITE;
1874 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1876 _bt_convert_addr_type_to_string(address,
1877 (unsigned char *)dev_addr.addr);
1879 param = g_variant_new("(iiiiiis)", result,
1880 event->attr_trans.conn_id,
1881 event->attr_trans.trans_id,
1882 event->attr_trans.attr_handle,
1883 event->mtu, event->attr_trans.offset, address);
1884 BT_DBG("remote address : [%s]", address);
1886 _bt_send_event(BT_GATT_SERVER_EVENT,
1887 BLUETOOTH_EVENT_GATT_SERVER_ACQUIRE_WRITE,
1892 static void __bt_handle_gatt_server_acquire_notify_requested(event_gatts_srvc_acquire_attr_t *event)
1894 GVariant *param = NULL;
1895 int result = BLUETOOTH_ERROR_NONE;
1896 struct gatt_server_req_info *req_info = NULL;
1897 bluetooth_device_address_t dev_addr;
1898 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1900 BT_INFO("GATT Server ACQUIRE Notify Req");
1901 BT_DBG("conn id %d, trans id %d, attr handle %d, req address %s",
1902 event->attr_trans.conn_id, event->attr_trans.trans_id,
1903 event->attr_trans.attr_handle, address);
1905 memcpy(dev_addr.addr, event->address.addr, 6);
1906 _bt_convert_addr_type_to_string(address,
1907 (unsigned char *)dev_addr.addr);
1908 BT_INFO("Remote address : [%s]", address);
1910 req_info = g_new0(struct gatt_server_req_info, 1);
1911 req_info->request_id = event->attr_trans.trans_id;
1912 req_info->attribute_handle = event->attr_trans.attr_handle;
1913 req_info->connection_id = event->attr_trans.conn_id;
1914 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_ACQUIRE_NOTIFY;
1915 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1917 param = g_variant_new("(iiiiiis)", result,
1918 event->attr_trans.conn_id,
1919 event->attr_trans.trans_id,
1920 event->attr_trans.attr_handle,
1921 event->mtu, event->attr_trans.offset,
1924 _bt_send_event(BT_GATT_SERVER_EVENT,
1925 BLUETOOTH_EVENT_GATT_SERVER_ACQUIRE_NOTIFY,
1929 #ifdef TIZEN_BLUEDROID_PORTING
1930 static bt_gatt_prep_write_data_t* __bt_create_prep_write_data(event_gatts_srvc_write_attr_t *event)
1932 bluetooth_device_address_t dev_addr;
1934 bt_gatt_prep_write_data_t *prep_data = NULL;
1936 prep_data = g_malloc0(sizeof(bt_gatt_prep_write_data_t));
1937 prep_data->connection_id = event->attr_trans.conn_id;
1938 prep_data->request_id = event->attr_trans.trans_id;
1939 prep_data->handle = event->attr_trans.attr_handle;
1940 prep_data->request_type = BLUETOOTH_GATT_REQUEST_TYPE_WRITE;
1941 memcpy(dev_addr.addr, event->address.addr, 6);
1942 addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1943 _bt_convert_addr_type_to_string(addr,
1944 (unsigned char *)dev_addr.addr);
1945 prep_data->device_address = addr;
1946 prep_data->offset = event->attr_trans.offset;
1947 prep_data->length = event->length;
1948 prep_data->value = g_memdup(&event->value[0], event->length);
1953 static int __bt_gatt_server_send_long_write_response(bt_gatt_prep_write_data_t *prep_data, int resp_status, int auth_req)
1955 int ret = OAL_STATUS_SUCCESS;
1956 oal_gatt_response_t response;
1958 memset(&response, 0x00, sizeof(oal_gatt_response_t));
1960 BT_INFO("GATT Server Write Res Connection ID: [%d]", prep_data->connection_id);
1961 BT_INFO("GATT Server Write Res Transaction ID:[%d]", prep_data->request_id);
1962 BT_INFO("GATT Server Write Res Attribute Handle: [%d]", prep_data->handle);
1963 BT_INFO("GATT Server Write Res Attribute Offset: [%d]", prep_data->offset);
1964 BT_INFO("GATT Server Write Res value length [%d]", prep_data->length);
1966 response.handle = prep_data->handle;
1967 response.attr_value.auth_req = auth_req;
1968 response.attr_value.handle = prep_data->handle;
1969 response.attr_value.offset = prep_data->offset;
1970 response.attr_value.len = prep_data->length;
1971 memcpy(&response.attr_value.value, &prep_data->value[0], prep_data->length);
1973 ret = gatts_send_response(prep_data->connection_id, prep_data->request_id,
1974 resp_status, &response);
1978 static bt_gatt_prep_write_data_t* __bt_find_prep_write_data_from_request_id(int request_id)
1981 bt_gatt_prep_write_data_t *prep_data = NULL;
1983 for (l = g_pending_write_list; l != NULL; l = g_slist_next(l)) {
1984 prep_data = (bt_gatt_prep_write_data_t*)l->data;
1985 if (prep_data && (prep_data->request_id == request_id) &&
1986 (prep_data->request_type == BLUETOOTH_GATT_REQUEST_TYPE_WRITE)) {
1987 BT_INFO("prep_data found for request id [%d]", request_id);
1991 BT_INFO("prep_data not found for request [%d]", request_id);
1995 static bt_gatt_prep_write_data_t* __bt_find_exec_write_req(int conn_id)
1998 bt_gatt_prep_write_data_t *prep_data = NULL;
2000 for (l = g_pending_write_list; l != NULL; l = g_slist_next(l)) {
2001 prep_data = (bt_gatt_prep_write_data_t*)l->data;
2002 if (prep_data && (prep_data->request_type == BLUETOOTH_GATT_REQUEST_TYPE_EXEC_WRITE)
2003 && (prep_data->connection_id == conn_id)) {
2004 BT_INFO("Exec request found");
2008 BT_INFO("Exec request not found");
2012 static int __bt_get_prep_request_count(int conn_id)
2016 bt_gatt_prep_write_data_t *prep_data = NULL;
2018 for (l = g_pending_write_list; l != NULL; l = g_slist_next(l)) {
2019 prep_data = (bt_gatt_prep_write_data_t*)l->data;
2020 if (prep_data && (prep_data->connection_id == conn_id) &&
2021 (prep_data->request_type == BLUETOOTH_GATT_REQUEST_TYPE_WRITE))
2027 static bt_gatt_prep_write_data_t* __bt_find_prep_write_data_from_handle(int conn_id, int handle)
2030 bt_gatt_prep_write_data_t *prep_data = NULL;
2031 bt_gatt_prep_write_data_t *last_prep_data = NULL;
2033 for (l = g_pending_write_list; l != NULL; l = g_slist_next(l)) {
2034 prep_data = (bt_gatt_prep_write_data_t*)l->data;
2035 if (prep_data && (prep_data->connection_id == conn_id) && (prep_data->handle == handle)) {
2036 BT_INFO("prep_data entry found for handle [%d]", handle);
2037 last_prep_data = prep_data;
2041 if (!last_prep_data)
2042 BT_INFO("prep_data entry not found for handle [%d]", handle);
2044 return last_prep_data;
2047 static void __bt_gatt_server_send_prep_write_req(int conn_id)
2049 int result = BLUETOOTH_ERROR_NONE;
2051 bt_gatt_prep_write_data_t *prep_data = NULL;
2053 for (l = g_pending_write_list; l != NULL; l = g_slist_next(l)) {
2054 prep_data = (bt_gatt_prep_write_data_t*)l->data;
2055 if (prep_data && (prep_data->connection_id == conn_id) &&
2056 (prep_data->request_type == BLUETOOTH_GATT_REQUEST_TYPE_WRITE)) {
2057 BT_INFO("sending prep_req, req_id=%d", prep_data->request_id);
2058 GVariant *data = NULL;
2059 GVariant *param = NULL;
2060 data = g_variant_new_from_data(
2061 G_VARIANT_TYPE_BYTESTRING,
2066 param = g_variant_new("(iiiiiibbsn@ay)", result,
2067 prep_data->connection_id,
2068 prep_data->request_id,
2074 prep_data->device_address,
2078 _bt_send_event(BT_GATT_SERVER_EVENT,
2079 BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED,
2085 static void __bt_remove_all_prep_write_req(int conn_id)
2088 bt_gatt_prep_write_data_t *prep_data = NULL;
2090 BT_INFO("Removing all req for conn_id %d", conn_id);
2091 for (l = g_pending_write_list; l != NULL;) {
2092 prep_data = (bt_gatt_prep_write_data_t*)l->data;
2093 l = g_slist_next(l);
2094 if (prep_data && (prep_data->connection_id == conn_id)) {
2095 BT_INFO("Removing req for req_id %d", prep_data->request_id);
2096 g_pending_write_list = g_slist_remove(g_pending_write_list, prep_data);
2097 g_free(prep_data->value);
2098 g_free(prep_data->device_address);
2105 static bool __bt_update_prep_write_data(bt_gatt_prep_write_data_t *prep_data, int offset,
2106 int length, char *value)
2114 len = prep_data->length + length;
2115 val = g_realloc(prep_data->value, len);
2119 memcpy(val + prep_data->length, value, length);
2120 prep_data->value = val;
2121 prep_data->length = len;
2123 BT_INFO("updated prep_data->length %d, prep_data->req_id %d", prep_data->length, prep_data->request_id);
2127 static bool __bt_handle_gatt_server_prepare_write_response(int *res,
2128 bluetooth_gatt_server_response_params_t *param)
2130 bt_gatt_prep_write_data_t *prep_data = NULL;
2131 bt_gatt_prep_write_data_t *exec_data = NULL;
2133 int ret = OAL_STATUS_SUCCESS;
2135 /* Search for matching Request in prepare write List */
2136 prep_data = __bt_find_prep_write_data_from_request_id(param->request_id);
2141 conn_id = prep_data->connection_id;
2142 exec_data = __bt_find_exec_write_req(conn_id);
2145 BT_ERR("Oops, Something weird has happened!!!");
2146 *res = BLUETOOTH_ERROR_INTERNAL;
2147 __bt_remove_all_prep_write_req(conn_id);
2149 // remove pending write request from the list
2150 BT_INFO("Removing prending write request, request id = %d", prep_data->request_id);
2151 g_pending_write_list = g_slist_remove(g_pending_write_list, prep_data);
2152 g_free(prep_data->value);
2153 g_free(prep_data->device_address);
2156 exec_data->prep_request_count--;
2157 if (param->response_status || !exec_data->prep_request_count) {
2158 BT_INFO("Sending exec response with status = %d", param->response_status);
2159 ret = __bt_gatt_server_send_long_write_response(exec_data, param->response_status, param->auth_req);
2160 if (ret != OAL_STATUS_SUCCESS) {
2161 BT_ERR("ret: %d", ret);
2162 *res = BLUETOOTH_ERROR_INTERNAL;
2164 __bt_remove_all_prep_write_req(conn_id);
2170 static void __bt_handle_gatt_server_prepare_write_requested(event_gatts_srvc_write_attr_t *event)
2172 bt_gatt_prep_write_data_t *pdata = NULL;
2173 bt_gatt_prep_write_data_t *prep_data = NULL;
2175 int resp_status = BLUETOOTH_ATT_ERROR_NONE;
2177 prep_data = __bt_create_prep_write_data(event);
2179 /* Find if the req node for that attribute already exists */
2180 pdata = __bt_find_prep_write_data_from_handle(prep_data->connection_id, prep_data->handle);
2182 if (!pdata || (prep_data->offset != (pdata->length + pdata->offset))) {
2183 BT_INFO("prep_write_req node doestn't exist or data is not in continuation, offset=%d", prep_data->offset);
2185 g_pending_write_list = g_slist_append(g_pending_write_list, (gpointer)pdata);
2186 BT_INFO("Send prep_write_response");
2187 ret = __bt_gatt_server_send_long_write_response(prep_data, resp_status, 0);
2189 /* Update the data and offset in attribute node */
2190 if (!(__bt_update_prep_write_data(pdata, prep_data->offset, prep_data->length, prep_data->value))) {
2191 BT_ERR("prep_data couldnot be updated");
2192 resp_status = BLUETOOTH_ATT_ERROR_INSUFFICIENT_RESOURCES;
2194 BT_INFO("Send prep_write_response");
2195 ret = __bt_gatt_server_send_long_write_response(prep_data, resp_status, 0);
2196 g_free(prep_data->device_address);
2197 g_free(prep_data->value);
2201 if (ret != OAL_STATUS_SUCCESS)
2202 BT_ERR("ret: %d", ret);
2206 static void __bt_handle_gatt_server_write_requested(event_gatts_srvc_write_attr_t *event)
2209 bluetooth_device_address_t dev_addr;
2210 GVariant *param = NULL;
2211 int result = BLUETOOTH_ERROR_NONE;
2213 gboolean is_prepare_write;
2214 char *write_val = NULL;
2215 GVariant *data = NULL;
2217 struct gatt_server_req_info *req_info = NULL;
2218 BT_INFO("GATT Server Write Requested");
2220 memcpy(dev_addr.addr, event->address.addr, 6);
2222 BT_INFO("GATT Server Write Req Connection ID: [%d]", event->attr_trans.conn_id);
2223 BT_INFO("GATT Server Write Req Transaction ID:[%d]", event->attr_trans.trans_id);
2224 BT_INFO("GATT Server Write Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
2225 BT_INFO("GATT Server Write Req Attribute Offset: [%d]", event->attr_trans.offset);
2226 BT_INFO("GATT Server Write Req value length [%d]", event->length);
2227 BT_INFO("GATT Server Write Req needs response: [%d]", event->need_rsp);
2228 BT_INFO("GATT Server Write Req Is Prep: [%d]", event->is_prep);
2230 #ifdef TIZEN_BLUEDROID_PORTING
2231 if (event->is_prep) {
2232 BT_INFO("receive prepare_write request");
2233 return __bt_handle_gatt_server_prepare_write_requested(event);
2237 need_resp = event->need_rsp;
2238 is_prepare_write = event->is_prep;
2240 if (event->length > 0)
2241 write_val = g_memdup(&event->value[0], event->length);
2243 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2244 _bt_convert_addr_type_to_string(address,
2245 (unsigned char *)dev_addr.addr);
2247 BT_INFO("GATT Server Write Request from remote client [%s]", address);
2249 if (event->length > 0) {
2250 for (i = 0; i < event->length; i++)
2251 BT_DBG("Data[%d] = [0x%x]", i, event->value[i]);
2253 /* Save Write Request Info */
2254 req_info = g_new0(struct gatt_server_req_info, 1);
2255 req_info->request_id = event->attr_trans.trans_id;
2256 req_info->attribute_handle = event->attr_trans.attr_handle;
2257 req_info->connection_id = event->attr_trans.conn_id;
2258 req_info->addr = address;
2259 req_info->offset = event->attr_trans.offset;
2260 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_WRITE;
2261 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
2263 data = g_variant_new_from_data(
2264 G_VARIANT_TYPE_BYTESTRING,
2269 param = g_variant_new("(iiiiiibbsn@ay)", result,
2270 event->attr_trans.conn_id,
2271 event->attr_trans.trans_id,
2272 event->attr_trans.attr_handle,
2273 event->attr_trans.offset,
2281 _bt_send_event(BT_GATT_SERVER_EVENT,
2282 BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED,
2288 #ifdef TIZEN_BLUEDROID_PORTING
2289 static void __bt_handle_gatt_server_exec_write_requested(event_gatts_srvc_exec_write_attr_t *event)
2292 bluetooth_device_address_t dev_addr;
2294 bt_gatt_prep_write_data_t *exec_data = NULL;
2295 int resp_status = BLUETOOTH_ATT_ERROR_NONE;
2296 BT_INFO("GATT Server Execute Write Requested");
2298 memcpy(dev_addr.addr, event->address.addr, 6);
2299 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2300 _bt_convert_addr_type_to_string(address,
2301 (unsigned char *)dev_addr.addr);
2303 BT_INFO("GATT Server Exec Write Req Connection ID: [%d]", event->conn_id);
2304 BT_INFO("GATT Server Exec Write Req Transaction ID:[%d]", event->trans_id);
2305 BT_INFO("GATT Server Exec Write Req Exec Write: [%d]", event->exec_write);
2307 // prepare exec response data
2308 exec_data = g_malloc0(sizeof(bt_gatt_prep_write_data_t));
2309 exec_data->connection_id = event->conn_id;
2310 exec_data->request_id = event->trans_id;
2311 exec_data->device_address = address;
2312 exec_data->request_type = BLUETOOTH_GATT_REQUEST_TYPE_EXEC_WRITE;
2313 exec_data->prep_request_count = __bt_get_prep_request_count(exec_data->connection_id);
2315 if ((exec_data->prep_request_count != 1) || !event->exec_write) {
2316 if (!event->exec_write) {
2317 BT_INFO("Cancelling all prepared writes, removing all pending entries");
2318 __bt_remove_all_prep_write_req(event->conn_id);
2319 } else if (exec_data->prep_request_count > 1) {
2320 /* TODO: Handle reliable-write session */
2321 BT_INFO("This may be reliable write session. Not yet supported!!!, prep_request_count =%d",
2322 exec_data->prep_request_count);
2323 resp_status = BLUETOOTH_ATT_ERROR_REQUEST_NOT_SUPPORTED;
2324 __bt_remove_all_prep_write_req(event->conn_id);
2327 BT_INFO("Send exec response");
2328 // Made response and send it.
2329 ret = __bt_gatt_server_send_long_write_response(exec_data, resp_status, 0);
2330 if (ret != OAL_STATUS_SUCCESS)
2331 BT_ERR("ret: %d", ret);
2333 g_free(exec_data->device_address);
2338 BT_INFO("Write all pending prepared values");
2339 __bt_gatt_server_send_prep_write_req(exec_data->connection_id);
2341 // Add exec request in the queue.
2342 g_pending_write_list = g_slist_append(g_pending_write_list, (gpointer)exec_data);
2346 static void __bt_handle_gatt_server_read_requested(event_gatts_srvc_read_attr_t *event)
2348 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2349 bluetooth_device_address_t dev_addr;
2350 int result = BLUETOOTH_ERROR_NONE;
2351 struct gatt_server_req_info *req_info = NULL;
2352 GVariant *param = NULL;
2355 memcpy(dev_addr.addr, event->address.addr, 6);
2356 _bt_convert_addr_type_to_string(address,
2357 (unsigned char *)dev_addr.addr);
2359 BT_DBG("conn_id %d, trans id %d, attr handle %d, offset %d, is_long %d, addr %s",
2360 event->attr_trans.conn_id, event->attr_trans.trans_id,
2361 event->attr_trans.attr_handle, event->attr_trans.offset,
2362 event->is_long, address);
2364 is_long = event->is_long;
2366 /* Save Read Request Info */
2367 req_info = g_new0(struct gatt_server_req_info, 1);
2368 req_info->request_id = event->attr_trans.trans_id;
2369 req_info->attribute_handle = event->attr_trans.attr_handle;
2370 req_info->connection_id = event->attr_trans.conn_id;
2371 req_info->addr = address;
2372 req_info->offset = event->attr_trans.offset;
2373 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_READ;
2374 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
2376 /* Send event to BT-API */
2377 param = g_variant_new("(iiiiibs)", result,
2378 event->attr_trans.conn_id,
2379 event->attr_trans.trans_id,
2380 event->attr_trans.attr_handle,
2381 event->attr_trans.offset,
2385 _bt_send_event(BT_GATT_SERVER_EVENT,
2386 BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED,
2390 static void __bt_handle_gatt_server_indicate_confirmed(event_gatts_ind_cnfrm_t *event)
2392 bluetooth_device_address_t dev_addr;
2393 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2394 int cur_connected_clients;
2395 static int recvd = 0;
2396 gboolean completed = 0;
2397 GVariant *param = NULL;
2399 /* OAL event does provide error, so MW assumes event will never contain wrong data,
2400 incase of any issues, check with OAL */
2401 int result = BLUETOOTH_ERROR_NONE;
2403 memcpy(dev_addr.addr, event->address.addr, 6);
2404 _bt_convert_addr_type_to_string(address,
2405 (unsigned char *)dev_addr.addr);
2407 BT_INFO("Indication sent to GATT client [%s] conn_ ID [%d] transaction ID [%d] Att handle [%d]",
2408 address, event->conn_id, event->trans_id, event->attr_handle);
2411 cur_connected_clients = g_slist_length(gatt_client_info_list);
2412 BT_INFO("Number of connected clients during sending Indication [%d] & current connected count [%d]",
2413 num_indicate_clients, cur_connected_clients);
2416 if (recvd == num_indicate_clients) {
2417 BT_INFO("Gatt indication confirm event for last GATT client.. [%s]", address);
2418 completed = 1; /* Last event */
2419 recvd = 0; /* Reset */
2420 num_indicate_clients = 0;
2423 param = g_variant_new("(isib)",
2429 /* Send event to BT-API */
2430 _bt_send_event(BT_GATT_SERVER_EVENT,
2431 BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_COMPLETED,
2434 BT_INFO("Received Indication confirm for client number [%d]", recvd);
2438 /* Tizen Platform Specific */
2439 static void __bt_handle_gatt_server_notification_changed(event_gatts_notif_t *event)
2441 bluetooth_device_address_t dev_addr;
2442 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2443 GVariant *param = NULL;
2446 /* OAL event does provide error, so MW assumes event will never contain wrong data,
2447 incase of any issues, check with OAL */
2448 int result = BLUETOOTH_ERROR_NONE;
2450 memcpy(dev_addr.addr, event->address.addr, 6);
2451 _bt_convert_addr_type_to_string(address,
2452 (unsigned char *)dev_addr.addr);
2454 BT_INFO("notification_changed [%s] conn_ ID [%d] transaction ID [%d] Att handle [%d] Notify[%d]",
2455 address, event->conn_id, event->trans_id, event->attr_handle, event->notify);
2457 /* Set Notifcation status */
2458 notify = event->notify;
2460 param = g_variant_new("(isib)",
2466 /* Send event to BT-API */
2467 _bt_send_event(BT_GATT_SERVER_EVENT,
2468 BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_STATE_CHANGED,
2474 static void __bt_handle_gatt_mtu_changed_event(event_gatts_mtu_changed_t *event)
2476 int result = BLUETOOTH_ERROR_NONE;
2477 struct gatt_client_info_t *conn_info = NULL;
2478 GVariant *param = NULL;
2481 conn_info = __bt_find_remote_gatt_client_info_from_conn_id(event->conn_id);
2482 if (conn_info == NULL) {
2483 BT_ERR("Cant find connection Information");
2486 BT_INFO("Got connection Info GATT client [%s] MTU Size [%d]",
2487 conn_info->addr, event->mtu_size);
2489 __bt_update_mtu_gatt_device(conn_info->addr, event->mtu_size);
2491 param = g_variant_new("(isqy)",
2497 /* Send event to BT-API */
2498 _bt_send_event(BT_GATT_SERVER_EVENT,
2499 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
2503 static void __bt_gatt_event_handler(int event_type, gpointer event_data)
2505 switch (event_type) {
2506 case OAL_EVENT_BLE_SERVER_INSTANCE_INITIALISED: {
2507 BT_INFO("OAL Event: Server Instance Registered");
2508 /* GATT Server Registered event is handled in MAIN thread context */
2509 __bt_handle_server_instance_registered((event_gatts_register_t *)event_data);
2512 case OAL_EVENT_GATTS_SERVICE_ADDED: {
2513 BT_INFO("OAL Event: GATT Service added");
2514 __bt_handle_gatt_server_service_added((event_gatts_srvc_prm_t *)event_data);
2517 case OAL_EVENT_GATTS_CHARACTERISTIC_ADDED: {
2518 BT_INFO("OAL Event: GATT characteristic added");
2519 __bt_handle_gatt_server_characteristic_added((event_gatts_srvc_charctr_t *)event_data);
2522 case OAL_EVENT_GATTS_DESCRIPTOR_ADDED: {
2523 BT_INFO("OAL Event: GATT descriptor added");
2524 __bt_handle_gatt_server_descriptor_added((event_gatts_srvc_descr_t *)event_data);
2527 case OAL_EVENT_GATTS_SERVICE_STARTED: {
2528 BT_INFO("OAL Event: GATT Service started");
2529 __bt_handle_gatt_server_service_started((event_gatts_srvc_t *)event_data);
2532 case OAL_EVENT_GATTS_SERVICE_STOPED: {
2533 BT_INFO("OAL Event: GATT Service stopped");
2534 __bt_handle_gatt_server_service_stopped((event_gatts_srvc_t *)event_data);
2537 case OAL_EVENT_GATTS_SERVICE_DELETED: {
2538 BT_INFO("OAL Event: GATT Service deleted");
2539 __bt_handle_gatt_server_service_deleted((event_gatts_srvc_t *) event_data);
2542 case OAL_EVENT_GATTS_CONNECTION_COMPLETED: {
2543 BT_INFO("OAL Event: GATT Server Connected");
2544 __bt_handle_gatt_server_connection_state((event_gatts_conn_t *)event_data);
2547 case OAL_EVENT_GATTS_DISCONNECTION_COMPLETED: {
2548 BT_INFO("OAL Event: GATT Server Disconnected");
2549 __bt_handle_gatt_server_disconnection_state((event_gatts_conn_t *)event_data);
2552 case OAL_EVENT_GATTS_REQUEST_READ: {
2553 BT_DBG("OAL Event: GATT Server Read Request");
2554 __bt_handle_gatt_server_read_requested((event_gatts_srvc_read_attr_t *)event_data);
2557 case OAL_EVENT_GATTS_REQUEST_WRITE: {
2558 BT_DBG("OAL Event: GATT Server Write Request");
2559 __bt_handle_gatt_server_write_requested((event_gatts_srvc_write_attr_t *)event_data);
2562 #ifdef TIZEN_BLUEDROID_PORTING
2563 case OAL_EVENT_GATTS_EXEC_REQUEST_WRITE: {
2564 BT_INFO("OAL Event: GATT Server Exec Write Request");
2565 __bt_handle_gatt_server_exec_write_requested((event_gatts_srvc_exec_write_attr_t *)event_data);
2569 case OAL_EVENT_GATTS_REQUEST_ACQUIRE_WRITE: {
2570 BT_INFO("OAL Event: GATT Server Acquire Write Request");
2571 __bt_handle_gatt_server_acquire_write_requested((event_gatts_srvc_acquire_attr_t*)event_data);
2574 case OAL_EVENT_GATTS_REQUEST_ACQUIRE_NOTIFY: {
2575 BT_INFO("OAL Event: GATT ServerAcquire Notify Request");
2576 __bt_handle_gatt_server_acquire_notify_requested((event_gatts_srvc_acquire_attr_t*)event_data);
2579 case OAL_EVENT_GATTS_IND_CONFIRM: {
2580 BT_INFO("OAL Event: GATT Server Indication confirmed");
2581 __bt_handle_gatt_server_indicate_confirmed((event_gatts_ind_cnfrm_t *)event_data);
2584 case OAL_EVENT_GATTS_NOTIFICATION: { /* Tizen Platform Specific */
2585 BT_INFO("OAL Event: GATT Server DisConnected");
2586 __bt_handle_gatt_server_notification_changed((event_gatts_notif_t *)event_data);
2589 case OAL_EVENT_GATTS_MTU_CHANGED: {
2590 BT_INFO("OAL Event: GATT Server MTU changed event callback");
2591 __bt_handle_gatt_mtu_changed_event((event_gatts_mtu_changed_t *)event_data);
2594 case OAL_EVENT_GATTC_REGISTRATION: {
2595 BT_INFO("OAL Event: GATT Client instance Registered");
2596 __bt_handle_client_instance_registered((event_gattc_register_t *) event_data);
2599 case OAL_EVENT_GATTC_CONNECTION_COMPLETED: {
2600 BT_INFO("OAL Event: GATT Client Connected");
2601 __bt_handle_client_connected((event_gattc_conn_t *) event_data);
2604 case OAL_EVENT_GATTC_DISCONNECTION_COMPLETED: {
2605 BT_INFO("OAL Event: GATT Client DisConnected");
2606 __bt_handle_client_disconnected((event_gattc_conn_t *) event_data);
2609 case OAL_EVENT_GATTC_SERVICE_SEARCH_RESULT: {
2610 BT_DBG("OAL Event: GATT Client Service Search Result");
2611 __bt_handle_client_service_search_result((event_gattc_service_result_t *) event_data);
2614 case OAL_EVENT_GATTC_SERVICE_SEARCH_DONE: {
2615 BT_INFO("OAL Event: GATT Client Service Completed");
2616 __bt_handle_client_service_search_completed((event_gattc_conn_status_t *) event_data);
2619 case OAL_EVENT_GATTC_CHARAC_SERACH_RESULT: {
2620 BT_DBG("OAL Event: GATT Client Characteristic Search Result");
2621 __bt_handle_client_characteristic_search_result((event_gattc_characteristic_result_t *) event_data);
2624 case OAL_EVENT_GATTC_DESC_SERACH_RESULT: {
2625 BT_DBG("OAL Event: GATT Client Descriptor Search Result");
2626 __bt_handle_client_descriptor_search_result((event_gattc_descriptor_result_t *) event_data);
2629 case OAL_EVENT_GATTC_READ_CHARAC: {
2630 BT_DBG("OAL Event: GATT Client Characteristic Read Data");
2631 __bt_handle_client_characteristic_read_data((event_gattc_read_data *) event_data);
2634 case OAL_EVENT_GATTC_READ_DESCR: {
2635 BT_DBG("OAL Event: GATT Client Descriptor Read Data");
2636 __bt_handle_client_descriptor_read_data((event_gattc_read_data *) event_data);
2639 case OAL_EVENT_GATTC_WRITE_CHARAC: {
2640 BT_DBG("OAL Event: GATT Client Characteristic Write Data");
2641 __bt_handle_client_characteristic_write_data((event_gattc_write_data *) event_data);
2644 case OAL_EVENT_GATTC_WRITE_DESCR: {
2645 BT_DBG("OAL Event: GATT Client Descriptor Write Data");
2646 __bt_handle_client_descriptor_write_data((event_gattc_write_data *) event_data);
2649 case OAL_EVENT_DEVICE_LE_DISCONNECTED: {
2650 BT_INFO("OAL Event: LE device disconnected");
2651 __bt_hanlde_le_device_disconnection((event_dev_conn_status_t *)event_data);
2654 case OAL_EVENT_GATTC_NOTIFICATION_REGISTERED: {
2655 BT_INFO("OAL Event: GATT Client Notification Registered");
2656 __bt_handle_client_notification_registered((event_gattc_regdereg_notify_t *) event_data, TRUE);
2659 case OAL_EVENT_GATTC_NOTIFICATION_DEREGISTERED: {
2660 BT_INFO("OAL Event: GATT Client Notification Registered");
2661 __bt_handle_client_notification_registered((event_gattc_regdereg_notify_t *) event_data, FALSE);
2664 case OAL_EVENT_GATTC_NOTIFY_DATA: {
2665 BT_DBG("OAL Event: GATT Client Notification Data");
2666 __bt_handle_client_notification_data((event_gattc_notify_data *) event_data);
2669 case OAL_EVENT_GATTC_SERVICE_CHANGED_IND: {
2670 BT_INFO("OAL Event: GATT Client service changed indication");
2671 __bt_handle_client_service_changed_ind((event_gattc_service_changed_data *)event_data);
2674 case OAL_EVENT_GATTC_MTU_EXCHANGE_COMPLETED: {
2675 BT_INFO("OAL Event: GATT Client MTU Exchange Complete");
2676 __bt_handle_client_mtu_exchange_completed((event_gattc_mtu_configured_t *) event_data);
2680 BT_DBG("Unhandled OAL event = 0x%x", event_type);
2685 int _bt_gatt_server_add_service(char *sender, int service_type,
2686 int num_handles, char *svc_uuid, int instance_id)
2688 BT_CHECK_PARAMETER(svc_uuid, return);
2689 BT_CHECK_PARAMETER(sender, return);
2690 int ret = OAL_STATUS_SUCCESS;
2692 oal_gatt_srvc_id_t svc_data;
2694 svc_data.is_prmry = service_type;
2695 svc_data.id.inst_id = instance_id;
2697 /* Check Service UUID duplication */
2698 if (__bt_check_service_uuid_registered(svc_uuid))
2699 return BLUETOOTH_ERROR_INTERNAL;
2701 BT_INFO("Service UUID [%s] Num handles [%d] Instance ID [%d]", svc_uuid, num_handles, instance_id);
2702 _bt_string_to_uuid(svc_uuid, (service_uuid_t*)&svc_data.id.uuid);
2704 ret = gatts_add_service(instance_id, &svc_data, num_handles);
2705 if (ret != OAL_STATUS_SUCCESS) {
2706 BT_ERR("ret: %d", ret);
2707 return _bt_convert_oal_status_to_bt_error(ret);
2710 return BLUETOOTH_ERROR_NONE;
2714 int _bt_gatt_server_add_included_service(char *sender, int instance_id,
2715 int service_handle, int included_svc_handle)
2717 BT_CHECK_PARAMETER(sender, return);
2718 int ret = OAL_STATUS_SUCCESS;
2720 ret = gatts_add_included_services(instance_id, service_handle, included_svc_handle);
2721 if (ret != OAL_STATUS_SUCCESS) {
2722 BT_ERR("ret: %d", ret);
2723 return _bt_convert_oal_status_to_bt_error(ret);
2725 return BLUETOOTH_ERROR_NONE;
2728 int _bt_gatt_server_add_characteristic(char *sender, char *char_uuid,
2729 bluetooth_gatt_server_attribute_params_t *param)
2731 BT_CHECK_PARAMETER(char_uuid, return);
2732 BT_CHECK_PARAMETER(sender, return);
2733 BT_CHECK_PARAMETER(param, return);
2734 int ret = OAL_STATUS_SUCCESS;
2736 oal_uuid_t uuid = {{0} };
2738 BT_INFO("Char UUID [%s] Instance ID [%d]", char_uuid, param->instance_id);
2739 _bt_string_to_uuid(char_uuid, (service_uuid_t*)&uuid);
2741 BT_INFO("Char permission From API [0x%x]", param->permissions);
2743 ret = gatts_add_characteristics(param->instance_id, param->service_handle, &uuid,
2744 param->properties, (int)param->permissions);
2745 if (ret != OAL_STATUS_SUCCESS) {
2746 BT_ERR("ret: %d", ret);
2747 return _bt_convert_oal_status_to_bt_error(ret);
2749 return BLUETOOTH_ERROR_NONE;
2752 int _bt_gatt_server_add_descriptor(char *sender, char *desc_uuid,
2753 bt_gatt_permission_t *param, int service_handle, int instance_id)
2755 BT_CHECK_PARAMETER(desc_uuid, return);
2756 BT_CHECK_PARAMETER(sender, return);
2757 BT_CHECK_PARAMETER(param, return);
2758 int ret = OAL_STATUS_SUCCESS;
2760 oal_uuid_t uuid = {{0} };
2762 BT_INFO("Descriptor UUID [%s] Instance ID [%d] Service handle [%d]",
2763 desc_uuid, service_handle, instance_id);
2765 _bt_string_to_uuid(desc_uuid, (service_uuid_t*)&uuid);
2767 BT_INFO("Descriptor permission From API [0x%x]", *param);
2768 ret = gatts_add_descriptor(instance_id, service_handle, &uuid, (int)*param);
2770 if (ret != OAL_STATUS_SUCCESS) {
2771 BT_ERR("ret: %d", ret);
2772 return _bt_convert_oal_status_to_bt_error(ret);
2774 return BLUETOOTH_ERROR_NONE;
2777 int _bt_gatt_server_start_service(char *sender, int service_handle, int instance_id)
2779 BT_CHECK_PARAMETER(sender, return);
2780 int ret = OAL_STATUS_SUCCESS;
2782 ret = gatts_start_service(instance_id, service_handle, BT_GATT_TRANSPORT_LE);
2783 if (ret != OAL_STATUS_SUCCESS) {
2784 BT_ERR("ret: %d", ret);
2785 return _bt_convert_oal_status_to_bt_error(ret);
2787 return BLUETOOTH_ERROR_NONE;
2790 int _bt_gatt_server_stop_service(char *sender, int service_handle, int instance_id)
2792 BT_CHECK_PARAMETER(sender, return);
2793 int ret = OAL_STATUS_SUCCESS;
2795 ret = gatts_stop_service(instance_id, service_handle);
2796 if (ret != OAL_STATUS_SUCCESS) {
2797 BT_ERR("ret: %d", ret);
2798 return _bt_convert_oal_status_to_bt_error(ret);
2800 return BLUETOOTH_ERROR_NONE;
2803 int _bt_gatt_server_delete_service(char *sender, int service_handle, int instance_id)
2805 BT_CHECK_PARAMETER(sender, return);
2806 int ret = OAL_STATUS_SUCCESS;
2810 bt_service_app_info_t *info = NULL;
2812 ret = gatts_delete_service(instance_id, service_handle);
2813 if (ret != OAL_STATUS_SUCCESS) {
2814 BT_ERR("ret: %d", ret);
2815 return _bt_convert_oal_status_to_bt_error(ret);
2818 /* Remove the Service Handle */
2819 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
2821 if (info->instance_id == instance_id) {
2822 for (l = info->service_handles; l != NULL; ) {
2824 l = g_slist_next(l);
2825 if (handle && *handle == service_handle) {
2826 BT_INFO("Remove Service handle [%d]", *handle);
2827 info->service_handles = g_slist_remove(info->service_handles, handle);
2828 /* Remove Service UUID from the list */
2829 __bt_remove_service_uuid(*handle);
2838 return BLUETOOTH_ERROR_NONE;
2841 int _bt_gatt_server_send_response(char *sender, bluetooth_gatt_att_data_t *data,
2842 bluetooth_gatt_server_response_params_t *param)
2844 BT_CHECK_PARAMETER(sender, return);
2845 BT_CHECK_PARAMETER(data, return);
2846 BT_CHECK_PARAMETER(param, return);
2847 struct gatt_server_req_info *req_info = NULL;
2848 int ret = OAL_STATUS_SUCCESS;
2849 #ifdef TIZEN_BLUEDROID_PORTING
2850 int res = BLUETOOTH_ERROR_NONE;
2852 oal_gatt_response_t response;
2854 BT_INFO("GATT Server Response: Req Type [%d] req_id [%d] status [%d] auth_req [%d] offset[%d] data len[%d]",
2855 param->req_type, param->request_id,
2856 param->response_status, param->auth_req,
2857 data->offset, data->length);
2859 #ifdef TIZEN_BLUEDROID_PORTING
2860 if (__bt_handle_gatt_server_prepare_write_response(&res, param))
2864 /* Search for matching Request in List */
2865 req_info = __bt_gatt_server_find_request_info(param->request_id, param->req_type);
2867 BT_ERR("GATT Server Req Info not found for current response..return Error");
2868 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2871 memset(&response, 0x00, sizeof(oal_gatt_response_t));
2873 response.handle = req_info->attribute_handle;
2874 response.attr_value.auth_req = param->auth_req;
2875 response.attr_value.handle = req_info->attribute_handle;
2876 response.attr_value.offset = data->offset;
2877 response.attr_value.len = data->length;
2878 memcpy(&response.attr_value.value, &data->data, data->length);
2881 ret = gatts_send_response(req_info->connection_id, param->request_id,
2882 param->response_status, &response);
2884 if (ret != OAL_STATUS_SUCCESS) {
2885 BT_ERR("ret: %d", ret);
2886 return _bt_convert_oal_status_to_bt_error(ret);
2889 /* Remove GATT server request from list */
2890 gatt_server_requests = g_slist_remove(gatt_server_requests, req_info);
2891 g_free(req_info->addr);
2893 return BLUETOOTH_ERROR_NONE;
2896 int _bt_gatt_server_acquire_send_response(char *sender, bluetooth_gatt_server_acquire_response_params_t *param , void *fd_list)
2898 BT_CHECK_PARAMETER(sender, return);
2899 BT_CHECK_PARAMETER(param, return);
2900 struct gatt_server_req_info *req_info = NULL;
2901 int ret = OAL_STATUS_SUCCESS;
2904 BT_INFO("GATT acquire Server Response: Req Type [%d] req_id [%d] fd [%d] mtu[%d]",
2905 param->req_type, param->request_id,
2909 /* Search for matching Request in List */
2910 req_info = __bt_gatt_server_find_request_info(param->request_id, param->req_type);
2912 BT_ERR("GATT acquire Server Req Info not found for current response..return Error");
2913 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2916 ret = gatt_send_response_acquire(req_info->connection_id, param->request_id, 0, param->fd, param->mtu, fd_list);
2918 if (ret != OAL_STATUS_SUCCESS) {
2919 BT_ERR("ret: %d", ret);
2920 return _bt_convert_oal_status_to_bt_error(ret);
2923 /* Remove GATT server request from list */
2924 gatt_server_requests = g_slist_remove(gatt_server_requests, req_info);
2925 g_free(req_info->addr);
2927 return BLUETOOTH_ERROR_NONE;
2932 int _bt_gatt_server_send_indication(char *sender, bluetooth_device_address_t *dev_addr,
2933 bluetooth_gatt_att_data_t *data,
2934 bluetooth_gatt_server_indication_params_t *param)
2936 BT_CHECK_PARAMETER(sender, return);
2937 BT_CHECK_PARAMETER(data, return);
2938 BT_CHECK_PARAMETER(param, return);
2940 gboolean all_send = FALSE;
2941 int ret = OAL_STATUS_SUCCESS;
2942 struct gatt_client_info_t *conn;
2944 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2945 _bt_convert_addr_type_to_string(address, dev_addr->addr);
2947 if (memcmp(dev_addr->addr, BDADDR_ANY, 6) == 0) {
2948 BT_INFO("GATT Server: Send Indication to all connected GATT clients..");
2951 BT_INFO("GATT Server: Send Indication to connected GATT client addr [%s]", address);
2954 /* Attempt to send Notification/Indication to all Connected GATT clients */
2956 ret = __bt_gatt_send_indication_to_all_connected_clients(data, param);
2957 if (ret != OAL_STATUS_SUCCESS) {
2958 BT_ERR("ret: %d", ret);
2960 return _bt_convert_oal_status_to_bt_error(ret);
2964 conn = _bt_find_remote_gatt_client_info(address);
2966 ret = gatts_send_indication(param->instance_id, param->atrribute_handle,
2967 conn->connection_id, data->length,
2968 param->need_confirmation, (char *)(&data->data[0]));
2970 if (ret != OAL_STATUS_SUCCESS) {
2971 BT_ERR("ret: %d", ret);
2972 BT_INFO("Indication failed to send to Remote GATT Client [%s]", address);
2974 return _bt_convert_oal_status_to_bt_error(ret);
2976 BT_INFO("Indication sent to Remote GATT Client [%s] wait for Notification completed event from OAL", address);
2978 num_indicate_clients = 1;
2979 return BLUETOOTH_ERROR_NONE;
2981 BT_ERR("Remote GATT client [%s] is not connected..Cant send Indication!!", address);
2983 return BLUETOOTH_ERROR_NOT_CONNECTED;
2986 return BLUETOOTH_ERROR_NONE;
2989 int _bt_gatt_server_update_attribute_value(char *sender, int instance_id,
2990 bluetooth_gatt_server_update_value_t *param)
2992 BT_CHECK_PARAMETER(sender, return);
2993 BT_CHECK_PARAMETER(param, return);
2994 int ret = OAL_STATUS_SUCCESS;
2996 oal_gatt_value_t value;
2997 BT_DBG("GATT Server Update value: Instance ID [%d] attr handle [%d] Value len [%d]",
2998 instance_id, param->attribute_handle, param->length);
3000 memset(&value, 0x00, sizeof(oal_gatt_value_t));
3002 value.handle = param->attribute_handle;
3003 value.len = param->length;
3004 memcpy(&value.value, ¶m->data.data, param->length);
3006 ret = gatts_update_att_value(instance_id, &value);
3008 if (ret != OAL_STATUS_SUCCESS) {
3009 BT_ERR("ret: %d", ret);
3010 return _bt_convert_oal_status_to_bt_error(ret);
3013 return BLUETOOTH_ERROR_NONE;
3016 int _bt_request_att_mtu(bluetooth_device_address_t *device_address,
3019 struct gatt_server_info_t *conn_info = NULL;
3021 int ret = OAL_STATUS_SUCCESS;
3023 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
3024 _bt_convert_addr_type_to_string(addr, device_address->addr);
3026 /* Check if remote GATT Server is connected or not */
3027 conn_info = _bt_find_remote_gatt_server_info(addr);
3028 if (conn_info == NULL) {
3029 BT_ERR("GATT Server is not yet connected..");
3031 return BLUETOOTH_ERROR_NOT_CONNECTED;
3034 ret = gattc_configure_mtu(conn_info->connection_id, mtu);
3035 if (ret != OAL_STATUS_SUCCESS) {
3036 BT_ERR("ret: %d", ret);
3038 return _bt_convert_oal_status_to_bt_error(ret);
3042 return BLUETOOTH_ERROR_NONE;
3045 int _bt_get_att_mtu(bluetooth_device_address_t *address,
3048 BT_CHECK_PARAMETER(address, return);
3049 BT_CHECK_PARAMETER(mtu, return);
3050 struct gatt_client_info_t *client_info = NULL;
3051 char addr[BT_ADDRESS_STRING_SIZE] = { 0 };
3052 int ret = OAL_STATUS_SUCCESS;
3055 _bt_convert_addr_type_to_string(addr, address->addr);
3057 BT_INFO("Get current MTU size for the remote client:DevAddress:[%s]", addr);
3059 client_info = _bt_find_remote_gatt_client_info(addr);
3061 BT_INFO("GATT Client [%s] is connected, conn Id [%d] Instance ID [%d]",
3062 client_info->addr, client_info->connection_id, client_info->instance_id);
3064 ret = gatts_get_att_mtu(client_info->connection_id, &stack_mtu);
3065 if (ret != OAL_STATUS_SUCCESS) {
3066 BT_ERR("ret: %d", ret);
3067 return _bt_convert_oal_status_to_bt_error(ret);
3070 struct gatt_server_info_t *server_info = NULL;
3071 BT_ERR("GATT Client [%s] is not yet connected..", addr);
3072 server_info = _bt_find_remote_gatt_server_info(addr);
3074 BT_INFO("GATT Server [%s] is connected, conn Id [%d] Client ID [%d]",
3075 server_info->addr, server_info->connection_id, server_info->client_id);
3077 ret = gattc_get_att_mtu(server_info->connection_id, &stack_mtu);
3078 if (ret != OAL_STATUS_SUCCESS) {
3079 BT_ERR("ret: %d", ret);
3080 return _bt_convert_oal_status_to_bt_error(ret);
3083 BT_ERR("GATT Server [%s] is not yet connected..", addr);
3084 return BLUETOOTH_ERROR_NOT_CONNECTED;
3088 BT_INFO("ATT MTU received from OAL [%d]", stack_mtu);
3089 *mtu = (unsigned int)stack_mtu;
3092 BT_ERR("MTU value is zero, GATT Client [%s] is not yet connected..", addr);
3093 return BLUETOOTH_ERROR_NOT_CONNECTED;
3096 return BLUETOOTH_ERROR_NONE;
3099 /* GATT Client utility static functions */
3100 static bt_gatt_service_info_list_t *__bt_get_service_info_list(int conn_id)
3103 bt_gatt_service_info_list_t *info = NULL;
3105 for (l = list_gatt_info; l != NULL; l = g_slist_next(l)) {
3106 info = (bt_gatt_service_info_list_t *)l->data;
3110 if (info->conn_id == conn_id)
3117 static bt_gatt_service_info_t *__bt_find_matching_service(
3118 bt_gatt_service_info_list_t *svc_list, oal_gatt_srvc_id_t *svc)
3121 bt_gatt_service_info_t *info = NULL;
3123 for (l = svc_list->services; l != NULL; l = g_slist_next(l)) {
3124 info = (bt_gatt_service_info_t *)l->data;
3128 /* Match UUID and instance ID */
3129 if (!memcmp(&svc->id.uuid.uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
3130 && (svc->id.inst_id == info->inst_id)) {
3137 static bt_gatt_char_info_t *__bt_find_matching_charc(
3138 bt_gatt_service_info_t *svc_info, oal_gatt_id_t *charc)
3141 bt_gatt_char_info_t *info = NULL;
3143 for (l = svc_info->chars; l != NULL; l = g_slist_next(l)) {
3144 info = (bt_gatt_char_info_t *)l->data;
3148 /* Match UUID and instance ID */
3149 if (!memcmp(&charc->uuid.uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
3150 && (charc->inst_id == info->inst_id)) {
3157 static bt_gatt_descriptor_info_t *__bt_find_matching_desc(
3158 bt_gatt_char_info_t *char_info, oal_gatt_id_t *desc)
3161 bt_gatt_descriptor_info_t *info = NULL;
3163 for (l = char_info->descs; l != NULL; l = g_slist_next(l)) {
3164 info = (bt_gatt_descriptor_info_t *)l->data;
3168 /* Match UUID and instance ID */
3169 if (!memcmp(&desc->uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
3170 && (desc->inst_id == info->inst_id)) {
3178 static struct gatt_server_info_t *__bt_find_remote_gatt_server_info_from_conn_id(int conn_id)
3181 struct gatt_server_info_t *info = NULL;
3183 for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
3184 info = (struct gatt_server_info_t*)l->data;
3188 if (info->connection_id == conn_id)
3194 static bt_gatt_service_info_t* __bt_find_removed_service(bt_gatt_service_info_list_t *svc_list)
3197 bt_gatt_service_info_t *info = NULL;
3199 for (l = svc_list->services; l != NULL; l = g_slist_next(l)) {
3200 info = (bt_gatt_service_info_t*)l->data;
3204 /* Service is marked a removed */
3205 if (info->is_removed == 1)
3211 static void __bt_remove_service_info_from_list(bt_gatt_service_info_t *svc_info)
3217 bt_gatt_char_info_t *charc = NULL;
3218 bt_gatt_included_service_info_t *incl = NULL;
3219 bt_gatt_descriptor_info_t *desc = NULL;
3221 /* Remove all Characteristic and Descriptors within characteristic */
3222 for (l = svc_info->chars; l != NULL;) {
3223 charc = (bt_gatt_char_info_t*)l->data;
3224 l = g_slist_next(l); /* Incase if l is removed, saving next to l */
3229 /* Inside Characteristic */
3230 for (l1 = charc->descs; l1 != NULL;) {
3232 desc = (bt_gatt_descriptor_info_t*)l1->data;
3233 l1 = g_slist_next(l1);
3238 /* Remove Descriptor */
3239 charc->descs = g_slist_remove(charc->descs, desc);
3242 /* Remove Characteristic */
3243 svc_info->chars = g_slist_remove(svc_info->chars, charc);
3247 /* Remove all Included Services */
3248 for (l2 = svc_info->included_svcs; l2 != NULL;) {
3249 incl = (bt_gatt_included_service_info_t*)l2->data;
3250 l2 = g_slist_next(l2); /* Incase if l is removed, saving next to l */
3255 /* Remove included service */
3256 svc_info->included_svcs = g_slist_remove(svc_info->included_svcs, incl);
3263 static void __bt_build_service_browse_info(int conn_id,
3264 bt_services_browse_info_t* info)
3267 bt_gatt_service_info_list_t *svc_info_list;
3268 bt_gatt_service_info_t *svc_info;
3270 service_uuid_t uuid;
3271 struct gatt_server_info_t *conn_info = NULL;
3273 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
3275 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
3276 if (conn_info == NULL) {
3277 BT_ERR("Cant find connection Information");
3281 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
3283 svc_info_list = __bt_get_service_info_list(conn_id);
3287 info->count = g_slist_length(svc_info_list->services);
3288 BT_DBG("Total services present in the svc info list for this conn id [%d] is [%d]",
3289 conn_id, info->count);
3291 for (l = svc_info_list->services; l != NULL; l = g_slist_next(l)) {
3292 svc_info = (bt_gatt_service_info_t*)l->data;
3293 if (svc_info == NULL)
3296 memcpy(&uuid.uuid, &svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3297 _bt_uuid_to_string(&uuid, uuid_string);
3299 BT_INFO("[%d] %s [%s]", count, uuid_string, _bt_convert_uuid_to_string(uuid_string));
3301 /* Fill UUID of service */
3302 g_strlcpy(info->uuids[count], uuid_string,
3303 BLUETOOTH_UUID_STRING_MAX);
3305 /* Fill instance ID of service */
3306 info->inst_id[count] = svc_info->inst_id;
3308 /* Fill primary service or not info */
3309 info->primary[count] = svc_info->is_primary;
3311 /* Increment count of services browsed */
3316 static void __bt_build_char_browse_info(int conn_id,
3317 bt_gatt_service_info_t *svc_info,
3318 bt_char_browse_info_t* info)
3321 bt_gatt_char_info_t *char_info;
3322 service_uuid_t uuid;
3324 struct gatt_server_info_t *conn_info = NULL;
3326 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
3328 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
3329 if (conn_info == NULL) {
3330 BT_ERR("Cant find connection Information");
3334 /* Fill default data, this will be required even in case of failure */
3335 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
3336 memcpy(&info->svc_uuid, svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3337 info->svc_inst_id = svc_info->inst_id;
3339 if (!svc_info->chars) {
3340 BT_ERR("No Chars browsed for address [%s]", conn_info->addr);
3344 info->count = g_slist_length(svc_info->chars);
3346 for (l = svc_info->chars; l != NULL; l = g_slist_next(l)) {
3347 char_info = (bt_gatt_char_info_t*)l->data;
3348 if (char_info == NULL)
3351 memcpy(&uuid.uuid, &char_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3352 _bt_uuid_to_string(&uuid, uuid_string);
3354 /* Fill UUID of characteristic */
3355 g_strlcpy(info->uuids[count], uuid_string,
3356 BLUETOOTH_UUID_STRING_MAX);
3358 /* Fill instance ID of characteristic */
3359 info->inst_id[count] = char_info->inst_id;
3361 /* Fill property of characteristic */
3362 info->props[count] = char_info->props;
3364 /* Increment count of services browsed */
3367 BT_DBG("Total characteristics browsed [%d]", count);
3370 static void __bt_build_descriptor_browse_info(int conn_id,
3371 bt_gatt_service_info_t *svc_info,
3372 bt_gatt_char_info_t *char_info,
3373 bt_descriptor_browse_info_t* info)
3376 bt_gatt_descriptor_info_t *desc_info;
3378 service_uuid_t uuid;
3379 struct gatt_server_info_t *conn_info = NULL;
3381 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
3383 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
3385 /* Fill default data, this will be required even in case of failure */
3386 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
3387 memcpy(&info->svc_uuid, svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3388 info->svc_inst_id = svc_info->inst_id;
3389 memcpy(&info->char_uuid, char_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3390 info->char_inst_id = char_info->inst_id;
3392 /* Fill property of the parent characteristic of this descriptor */
3393 info->char_props_map = char_info->props;
3395 info->count = g_slist_length(char_info->descs);
3397 if (!char_info->descs) {
3398 BT_ERR("No Descriptors browsed for address [%s]", conn_info->addr + 12);
3402 for (l = char_info->descs; l != NULL; l = g_slist_next(l)) {
3403 desc_info = (bt_gatt_descriptor_info_t*)l->data;
3404 if (desc_info == NULL)
3407 memcpy(&uuid.uuid, &desc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3408 _bt_uuid_to_string(&uuid, uuid_string);
3410 /* Fill UUID of Descriptor */
3411 g_strlcpy(info->uuids[count], uuid_string,
3412 BLUETOOTH_UUID_STRING_MAX);
3414 /* Fill instance ID of Descriptor */
3415 info->inst_id[count] = desc_info->inst_id;
3418 /* Increment count of Descriptor browsed */
3422 BT_INFO("Total descriptors browsed [%d]", count);
3425 static void __bt_free_service_info(bt_gatt_service_info_t *svc)
3427 GSList *ll, *lll, *llll;
3428 bt_gatt_char_info_t *chr = NULL;
3429 bt_gatt_descriptor_info_t *desc = NULL;
3430 bt_gatt_included_service_info_t *incl_svc = NULL;
3432 BT_DBG("Service info Is Prim[%d] Inst ID [%d]", svc->is_primary, svc->inst_id);
3433 /* Delete all chars and its descriptors */
3434 for (ll = svc->chars; ll != NULL; ) {
3435 chr = (bt_gatt_char_info_t *)ll->data;
3436 ll = g_slist_next(ll);
3440 for (lll = chr->descs; lll != NULL; ) {
3441 desc = (bt_gatt_descriptor_info_t *)lll->data;
3442 lll = g_slist_next(lll);
3445 chr->descs = g_slist_remove(chr->descs, desc);
3448 svc->chars = g_slist_remove(svc->chars, chr);
3452 /* Delete all included services */
3453 for (llll = svc->included_svcs; llll != NULL; ) {
3454 incl_svc = (bt_gatt_included_service_info_t *)llll->data;
3455 llll = g_slist_next(llll);
3456 if (incl_svc == NULL)
3458 svc->included_svcs = g_slist_remove(svc->included_svcs, incl_svc);
3463 static void __bt_cleanup_remote_services(struct gatt_server_info_t *conn_info)
3465 bt_gatt_service_info_list_t *svc_info_list = NULL;
3466 bt_gatt_service_info_t *svc = NULL;
3470 BT_ERR("conn_info is NULL");
3474 svc_info_list = __bt_get_service_info_list(conn_info->connection_id);
3475 if (!svc_info_list) {
3476 BT_INFO("Could not find Svc Info list for the connection ID [%d]",
3477 conn_info->connection_id);
3481 BT_INFO("Start Cleanup of all services. Num Services [%d]", g_slist_length(svc_info_list->services));
3482 for (l = svc_info_list->services; l != NULL; ) {
3483 svc = (bt_gatt_service_info_t *)l->data;
3484 l = g_slist_next(l);
3488 __bt_free_service_info(svc);
3489 svc_info_list->services = g_slist_remove(svc_info_list->services, svc);
3493 list_gatt_info = g_slist_remove(list_gatt_info, svc_info_list);
3494 g_free(svc_info_list);
3497 int _bt_register_gatt_client_instance(const char *sender,
3498 bluetooth_device_address_t *address)
3500 int ret = OAL_STATUS_SUCCESS;
3501 char *uuid_string = NULL;
3506 /* App should ensure that it should not send */
3507 BT_INFO("Check on which instance GATT Client instance can be initialized....");
3508 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
3509 if (numapps[k].is_initialized == 1) {
3510 BT_DBG("Instance ID [%d] is already in use..Check next slot",
3511 numapps[k].instance_id);
3514 BT_DBG("Time to register GATT client instancer..UUID to be used is [%s] slot [%d]",
3515 uuid_list[slot-1], slot);
3521 BT_ERR("No Slot if free for GATT Client registration..");
3522 return BLUETOOTH_ERROR_REGISTRATION_FAILED;
3525 uuid_string = g_malloc0(BT_UUID_STRING_MAX);
3526 _bt_string_to_uuid(uuid_list[slot-1], (service_uuid_t*)&uuid);
3527 g_strlcpy(uuid_string, uuid_list[slot-1], BT_UUID_STRING_MAX);
3528 BT_INFO("Copied UUID string [%s] slot [%d]", uuid_string, slot);
3530 /* Register GATT Client */
3531 ret = gattc_register(&uuid);
3532 if (ret != OAL_STATUS_SUCCESS) {
3533 BT_ERR("ret: %d", ret);
3534 g_free(uuid_string);
3535 return _bt_convert_oal_status_to_bt_error(ret);
3538 BT_DBG("GATT Client registration call successfully accepted by OAL..wait for Instance Initialized event from OAL..");
3540 /* Return & wait for GATT Client Instance Initialization event */
3541 memset(numapps[slot].sender, 0x00, sizeof(numapps[slot].sender));
3542 memset(numapps[slot].uuid, 0x00, sizeof(numapps[slot].uuid));
3544 g_strlcpy(numapps[slot].sender, sender, sizeof(numapps[slot].sender));
3545 g_strlcpy(numapps[slot].uuid, uuid_string, sizeof(numapps[slot].uuid));
3547 /* Address is saved here. When event comes, sender + address are matched for replying pending
3548 request. It is impossible for same sender to have requests with two same addresses */
3549 memcpy(&numapps[slot].address.addr, address->addr, sizeof(bluetooth_device_address_t));
3551 numapps[slot].is_initialized = TRUE; /* Set initialization to true here itself */
3553 g_free(uuid_string);
3554 return BLUETOOTH_ERROR_NONE;
3560 /* GATT client events */
3561 static void __bt_handle_client_instance_registered(event_gattc_register_t *data)
3563 bt_service_app_info_t *info = NULL;
3565 char *uuid_string = g_malloc0(BT_UUID_STRING_MAX);
3567 _bt_uuid_to_string(&(data->client_uuid), uuid_string);
3568 BT_INFO("Client ID is Initialized [%d] UUID initialized [%s]", data->client_if, uuid_string);
3570 /* Platform GATT client framwork does not use Default GATT client instance
3571 This GATT client instance is never deregistred in the lifetime of bt-service */
3572 if (g_strcmp0(uuid_string, DEFAULT_GATT_CLIENT_UUID) == 0) {
3573 BT_INFO("Default client Instance Registered");
3574 gatt_default_client = data->client_if;
3575 g_free(uuid_string);
3579 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
3582 if (g_strcmp0(info->uuid, uuid_string) == 0) {
3583 BT_INFO("Found GATT client.. sender [%s] Slot [%d] occupied", info->sender, k);
3584 info->is_initialized = TRUE;
3585 info->client_id = data->client_if;
3586 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE, BT_GATT_CLIENT_REGISTER,
3587 (void*)info, sizeof(bt_service_app_info_t));
3591 g_free(uuid_string);
3594 static void __bt_handle_client_connected(event_gattc_conn_t *event_data)
3596 int result = BLUETOOTH_ERROR_NONE;
3597 struct gatt_server_info_t *conn_info = NULL;
3598 struct gatt_out_conn_info_t *out_conn_info = NULL;
3600 GVariant *param = NULL;
3602 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
3603 _bt_convert_addr_type_to_string(address,
3604 (unsigned char *)event_data->address.addr);
3606 if (event_data->status != OAL_STATUS_SUCCESS)
3607 result = BLUETOOTH_ERROR_INTERNAL;
3609 /* DBUS Return fo BT_CONNECT_LE for all the apps */
3610 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE, address,
3611 BT_ADDRESS_STRING_SIZE);
3613 BT_INFO("Local GATT Client Connected: Remote addr[%s] Client Interface [%d] Connection ID [%d] status[%d]",
3614 address, event_data->client_if, event_data->conn_id, event_data->status);
3616 if (result == BLUETOOTH_ERROR_NONE) {
3617 /* Check if device is already in connected list */
3618 conn_info = _bt_find_remote_gatt_server_info(address);
3621 /* Send event to BT-API */
3622 param = g_variant_new("(is)", result, address);
3623 _bt_send_event(BT_DEVICE_EVENT,
3624 BLUETOOTH_EVENT_GATT_CLIENT_CONNECTED, /* Local device is GATT client */
3627 /* Save Connection info */
3628 conn_info = g_new0(struct gatt_server_info_t, 1);
3629 conn_info->addr = g_strdup(address);
3630 conn_info->client_id = event_data->client_if;
3631 #ifdef __INTEGRATE_GATT_INFO__
3632 conn_info->instance_id = -1;
3634 conn_info->connection_id = event_data->conn_id;
3635 gatt_server_info_list = g_slist_append(gatt_server_info_list, conn_info);
3636 BT_DBG("Total num of connected Remote GATT server devices [%d]",
3637 g_slist_length(gatt_server_info_list));
3640 BT_INFO("Do a Internal refresh");
3641 if (OAL_STATUS_SUCCESS != gattc_refresh(conn_info->client_id, &event_data->address))
3642 BT_ERR("GATT database refresh failed!!");
3644 BT_INFO("GATT database refresh Success!!");
3647 BT_ERR("Local GATT Client connected event for addr[%s], but device is in connected list already", address);
3649 __bt_add_mtu_gatt_device(address);
3651 _bt_le_set_default_connection_param(address, 30, 35, 0, 6000);
3653 BT_ERR("GATT Client Connection failed!!");
3655 /* If outgoing connection Info is present, then remove it */
3656 out_conn_info = __bt_find_gatt_outgoing_conn_info(address);
3657 if (out_conn_info) {
3658 BT_ERR("Outgoing Client connect request was sent");
3659 outgoing_gatt_conn_list = g_slist_remove(outgoing_gatt_conn_list, out_conn_info);
3660 g_free(out_conn_info->addr);
3661 g_free(out_conn_info);
3663 _bt_restart_le_scan();
3668 static void __bt_handle_client_disconnected(event_gattc_conn_t *event_data)
3670 int result = BLUETOOTH_ERROR_NONE;
3671 struct gatt_server_info_t *conn_info = NULL;
3672 #ifndef __INTEGRATE_GATT_INFO__
3673 struct gatt_client_info_t *client_info = NULL;
3675 struct gatt_out_conn_info_t *out_conn_info = NULL;
3676 GVariant *param = NULL;
3678 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
3679 _bt_convert_addr_type_to_string(address,
3680 (unsigned char *)event_data->address.addr);
3682 if (event_data->status != OAL_STATUS_SUCCESS)
3683 result = BLUETOOTH_ERROR_INTERNAL;
3685 if (NULL == _bt_get_request_info_data(BT_DISCONNECT_LE, address)) {
3686 if (NULL != _bt_get_request_info_data(BT_CONNECT_LE, address)) {
3687 result = BLUETOOTH_ERROR_INTERNAL;
3688 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
3689 address, BT_ADDRESS_STRING_SIZE);
3690 BT_ERR("Failed to connect Local GATT Remote addr[%s]", address);
3695 /* DBUS Return for BT_DISCONNECT_LE for all the apps */
3696 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE, address,
3697 BT_ADDRESS_STRING_SIZE);
3699 BT_INFO("Local GATT Client DisConnected: Remote addr[%s] Client Interface [%d] Connection ID [%d] status [%d]",
3700 address + 12, event_data->client_if, event_data->conn_id, event_data->status);
3702 /* Remove Connection info */
3703 conn_info = _bt_find_remote_gatt_server_info(address);
3706 param = g_variant_new("(is)", result, address);
3707 /* Send event to application */
3708 _bt_send_event(BT_DEVICE_EVENT,
3709 BLUETOOTH_EVENT_GATT_CLIENT_DISCONNECTED,
3712 BT_INFO("Remove GATT server info from List..");
3713 /* Remove all services from info list_gatt_info */
3714 __bt_cleanup_remote_services(conn_info);
3716 /* Remove info from List */
3717 gatt_server_info_list = g_slist_remove(gatt_server_info_list, conn_info);
3719 /* Remove all pending invocations from invocatin_list */
3720 BT_INFO("Clear all pending invocations");
3721 __bt_gatt_cleanup_invocation_on_gatt_disconnection(BLUETOOTH_ERROR_INTERNAL,
3722 address, BT_ADDRESS_STRING_SIZE);
3724 BT_INFO("Total num of connected GATT servers [%d]", g_slist_length(gatt_server_info_list));
3725 g_free(conn_info->addr);
3728 BT_INFO("Can not find conn info, already removed!");
3730 #ifndef __INTEGRATE_GATT_INFO__
3731 /* Remove client info */
3732 client_info = _bt_find_remote_gatt_client_info(address);
3734 BT_DBG("Remove GATT client info from list");
3735 gatt_client_info_list = g_slist_remove(gatt_client_info_list, client_info);
3736 g_free(client_info->addr);
3737 g_free(client_info);
3741 __bt_remove_mtu_gatt_device(address);
3743 /* If outgoing connection Info is present, then remove it */
3744 out_conn_info = __bt_find_gatt_outgoing_conn_info(address);
3745 if (out_conn_info) {
3746 BT_ERR("Client Disconnected event, but outgoing connect request was sent");
3747 outgoing_gatt_conn_list = g_slist_remove(outgoing_gatt_conn_list, out_conn_info);
3748 g_free(out_conn_info->addr);
3749 g_free(out_conn_info);
3755 static void __bt_handle_client_service_search_result(
3756 event_gattc_service_result_t *event_data)
3758 /* Pre: status is never fail from OAL */
3760 /* Find service list from address */
3761 bt_gatt_service_info_list_t *svc_info_list;
3762 bt_gatt_service_info_t *svc_info;
3764 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3765 if (!svc_info_list) {
3766 BT_DBG("Service info list not present for connection ID %d, means first time browse", event_data->conn_status.conn_id);
3767 /* Means for this conn_id, no services are ever browsed, first time,
3768 create service info list for this conn_id */
3769 svc_info_list = g_malloc0(sizeof(bt_gatt_service_info_list_t));
3770 svc_info_list->conn_id = event_data->conn_status.conn_id;
3771 list_gatt_info = g_slist_append(list_gatt_info, svc_info_list);
3774 /* send list and current service's uuid and instance id to find it */
3775 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3776 /* If not found, check if service changed, if yes, means this is a new service added
3777 in remote GATT device, update uuid info in svc info list structure, to be used when
3778 search is completed */
3780 if (svc_info_list->info.is_changed) {
3781 BT_DBG("Service Changed indication already found for connection ID %d", event_data->conn_status.conn_id);
3782 memcpy(svc_info_list->info.uuid, event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3784 /* Create and add new service in service list */
3785 svc_info = g_malloc0(sizeof(bt_gatt_service_info_t));
3786 memcpy(svc_info->uuid, event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3787 svc_info->inst_id = event_data->srvc_id.id.inst_id;
3788 svc_info->is_primary = event_data->srvc_id.is_prmry;
3789 svc_info_list->services = g_slist_append(svc_info_list->services, svc_info);
3790 BT_DBG("Service created and added in Svc info list or connection ID %d", event_data->conn_status.conn_id);
3792 /* If returned matching service info, then just update service_rmeoved value inside it to 0 */
3793 svc_info->is_removed = 0;
3797 static void __bt_handle_client_service_search_completed(
3798 event_gattc_conn_status_t *event_data)
3800 struct gatt_server_info_t *conn_info = NULL;
3801 bt_gatt_service_info_list_t *svc_info_list;
3802 bt_gatt_service_info_t *svc_info;
3803 bt_services_browse_info_t browse_info;
3804 unsigned char uuid_empty[BLUETOOTH_UUID_HEX_MAX_LEN];
3806 memset(&uuid_empty, 0x00, BLUETOOTH_UUID_HEX_MAX_LEN);
3807 memset(&browse_info, 0x00, sizeof(bt_services_browse_info_t));
3808 BT_INFO("Primary Services browsing completed status[%d] conn ID [%d]",
3809 event_data->status, event_data->conn_id);
3811 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(event_data->conn_id);
3813 svc_info_list = __bt_get_service_info_list(event_data->conn_id);
3814 if (!svc_info_list) {
3815 BT_ERR("No services browsed ever for addr [%s]", conn_info->addr);
3817 /* Just build response and return ERROR */
3818 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3820 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_INTERNAL,
3821 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3822 sizeof(bt_services_browse_info_t));
3826 /* If fail, then send event with error */
3827 if (event_data->status != OAL_STATUS_SUCCESS) {
3828 /* Just build response and return ERROR */
3829 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3831 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_INTERNAL,
3832 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3833 sizeof(bt_services_browse_info_t));
3837 /* If success, then find service info list from address */
3839 /* If svc_changed == 1 and uuid valid, means a new service is added*/
3840 if (svc_info_list->info.is_changed && !memcmp(uuid_empty, svc_info_list->info.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
3841 /* TODO: Send event -Service added with instance ID and UUID of newly added service */
3842 BT_INFO("new service added");
3844 BT_INFO("TODO new service added");
3847 /* If svc_changed == 1 and uuid invalid, then a service is removed */
3848 if (svc_info_list->info.is_changed && memcmp(uuid_empty, svc_info_list->info.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
3849 /* Scan through the service info list to find service with is_removed = 1*/
3850 svc_info = __bt_find_removed_service(svc_info_list);
3852 /* TODO Send event - Service removed with instance ID and UUID of just rmeoved service */
3854 /* Remove that service info from service info list */
3855 svc_info_list->services = g_slist_remove(svc_info_list->services, svc_info);
3857 /* Delete that service completely from svc_info list*/
3858 __bt_remove_service_info_from_list(svc_info);
3862 /* Reset svc_changed = 0, and reset UUID = all 0's */
3863 svc_info_list->info.is_changed = 0;
3864 memset(&svc_info_list->info.uuid, 0x00, BLUETOOTH_UUID_HEX_MAX_LEN);
3866 /* Build Reply and send to service browse primary services request of pending apps */
3867 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3869 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3870 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3871 sizeof(bt_services_browse_info_t));
3875 static void __bt_handle_client_characteristic_search_result(
3876 event_gattc_characteristic_result_t *event_data)
3878 bt_gatt_service_info_list_t *svc_info_list;
3879 bt_gatt_service_info_t *svc_info;
3880 bt_gatt_char_info_t *char_info;
3881 bt_char_browse_info_t browse_info;
3883 memset(&browse_info, 0x00, sizeof(bt_char_browse_info_t));
3886 if (event_data->conn_status.status == OAL_STATUS_SUCCESS) {
3887 /* Find service info list from address */
3888 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3889 if (svc_info_list == NULL) {
3890 BT_ERR("svc_info_list is NULL");
3894 /* Find matching service info from svc info list */
3895 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3896 if (svc_info == NULL) {
3897 BT_ERR("svc_info is NULL");
3901 /* Find Matching char from service info in event */
3902 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3903 /* If not found, then add new characteristic and return */
3905 BT_DBG("Add new characteristic");
3906 char_info = g_malloc0(sizeof(bt_gatt_char_info_t));
3907 memcpy(char_info->uuid, event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3909 char_info->inst_id = event_data->char_id.inst_id;
3910 char_info->props = event_data->char_prop;
3911 svc_info->chars = g_slist_append(svc_info->chars, char_info);
3913 /* If found, then return */
3914 BT_DBG("update char property as Characteristic browsed is already present");
3915 char_info->props |= event_data->char_prop;
3918 /* If Not success: Means Charc browse is completed */
3919 /* Find char list from service in event */
3920 /* Find service list from address */
3921 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3922 if (svc_info_list == NULL) {
3923 BT_ERR("svc_info_list is NULL");
3927 /* Find service info from service in event */
3928 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3929 if (svc_info == NULL) {
3930 BT_ERR("svc_info is NULL");
3934 /* Build char list from service in event */
3935 __bt_build_char_browse_info(event_data->conn_status.conn_id,
3936 svc_info, &browse_info);
3938 /* Create response and return by sending event*/
3939 /* Build Reply and send to service browse All Included services request of pending apps */
3940 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3941 BT_GATT_GET_SERVICE_PROPERTIES,
3943 sizeof(bt_char_browse_info_t));
3947 static void __bt_handle_client_descriptor_search_result(
3948 event_gattc_descriptor_result_t *event_data)
3950 bt_gatt_service_info_list_t *svc_info_list;
3951 bt_gatt_service_info_t *svc_info;
3952 bt_gatt_char_info_t *char_info;
3953 bt_gatt_descriptor_info_t *desc_info;
3954 bt_descriptor_browse_info_t browse_info;
3956 BT_DBG("descriptor search result status [%d]", event_data->conn_status.status);
3958 memset(&browse_info, 0x00, sizeof(bt_descriptor_browse_info_t));
3961 if (event_data->conn_status.status == OAL_STATUS_SUCCESS) {
3962 /* Find service list from address */
3963 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3964 if (svc_info_list == NULL) {
3965 BT_ERR("svc_info_list is NULL");
3969 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3970 if (svc_info == NULL) {
3971 BT_ERR("svc_info is NULL");
3975 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3976 if (char_info == NULL) {
3977 BT_ERR("char_info is NULL");
3981 desc_info = __bt_find_matching_desc(char_info, &event_data->descr_id);
3982 /* If not found, add new descriptor and return */
3984 desc_info = g_malloc0(sizeof(bt_gatt_descriptor_info_t));
3985 memcpy(desc_info->uuid, event_data->descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3987 desc_info->inst_id = event_data->descr_id.inst_id;
3988 char_info->descs = g_slist_append(char_info->descs, desc_info);
3991 /* If found, then return */
3992 BT_DBG("Descriptor browsed is already presesnt");
3995 /* If Not success */
3996 /* Find service list from address */
3997 /* Find included service list from service in event */
3998 /* Create response and return by sending event*/
3999 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
4000 if (svc_info_list == NULL) {
4001 BT_ERR("svc_info_list is NULL");
4005 /* Find service info from service in event */
4006 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
4007 if (svc_info == NULL) {
4008 BT_ERR("svc_info is NULL");
4012 /* Find char info from char in event */
4013 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
4014 if (char_info == NULL) {
4015 BT_ERR("char_info is NULL");
4019 /* Build descriptor list from char in event */
4020 __bt_build_descriptor_browse_info(event_data->conn_status.conn_id,
4021 svc_info, char_info, &browse_info);
4024 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
4025 BT_GATT_GET_CHARACTERISTIC_PROPERTIES,
4027 sizeof(bt_descriptor_browse_info_t));
4031 static void __bt_handle_client_characteristic_read_data(
4032 event_gattc_read_data *event_data)
4034 int result = BLUETOOTH_ERROR_NONE;
4035 struct gatt_server_info_t *conn_info = NULL;
4036 bluetooth_gatt_client_char_prop_info_t read_info;
4038 /* Read Information data structures */
4039 GVariant *param = NULL;
4040 GVariant *data = NULL;
4041 GVariant *data_svc_uuid = NULL;
4042 GVariant *data_char_uuid = NULL;
4043 char *read_val = NULL;
4044 char *svc_uuid = NULL;
4045 char *char_uuid = NULL;
4048 //memset(&read_info, 0x00, sizeof(bt_gatt_handle_property_t));
4049 memset(&read_info, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
4051 /* Extract Address from conn_id of event data */
4052 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
4053 event_data->uuid_status.conn_status.conn_id);
4055 BT_INFO("Characteristic Read result from addr [%s] status [%d]",
4056 conn_info->addr, event_data->uuid_status.conn_status.status);
4058 /* Fill char in buffer */
4059 memcpy(&read_info.characteristic.uuid,
4060 event_data->uuid_status.char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4061 read_info.characteristic.instance_id = event_data->uuid_status.char_id.inst_id;
4063 /* Fill Service in buffer */
4064 memcpy(&read_info.svc.uuid,
4065 event_data->uuid_status.srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4066 read_info.svc.instance_id = event_data->uuid_status.srvc_id.id.inst_id;
4068 /* Fill remote device address */
4069 _bt_convert_addr_string_to_type(read_info.device_address.addr, conn_info->addr);
4071 /* Fill data and reply to all apps waiting for Read result on the same characteristic
4072 Note: Even in case of failure, address, handles and result code should be returned */
4073 if (event_data->uuid_status.conn_status.status != OAL_STATUS_SUCCESS)
4074 result = BLUETOOTH_ERROR_INTERNAL;
4076 if (event_data->data_len > 0) {
4078 // for (i = 0; i < event_data->data_len; i++)
4079 // BT_INFO("Data[%d] = [0x%x]", i, event_data->data[i]);
4082 read_val = g_memdup(&event_data->data[0], event_data->data_len);
4084 data = g_variant_new_from_data(
4085 G_VARIANT_TYPE_BYTESTRING,
4087 event_data->data_len,
4090 BT_ERR("Characteristic Read success, but no data!!!");
4092 data = g_variant_new_from_data(
4093 G_VARIANT_TYPE_BYTESTRING,
4100 svc_uuid = g_memdup(&event_data->uuid_status.srvc_id.id.uuid.uuid[0], uuid_len);
4102 data_svc_uuid = g_variant_new_from_data(
4103 G_VARIANT_TYPE_BYTESTRING,
4109 char_uuid = g_memdup(&event_data->uuid_status.char_id.uuid.uuid[0], uuid_len);
4111 data_char_uuid = g_variant_new_from_data(
4112 G_VARIANT_TYPE_BYTESTRING,
4117 param = g_variant_new("(isn@ayin@ayin@ay)", result,
4121 event_data->uuid_status.srvc_id.id.inst_id,
4124 event_data->uuid_status.char_id.inst_id,
4125 event_data->data_len,
4129 char *sender = NULL;
4130 __bt_gatt_get_pending_request_info(BT_GATT_READ_CHARACTERISTIC, &sender);
4131 _bt_send_event_to_dest(sender, BT_GATT_CLIENT_EVENT,
4132 BLUETOOTH_EVENT_GATT_READ_CHAR,
4135 /* Send DBUS return */
4136 __bt_gatt_handle_pending_request_info(result,
4137 BT_GATT_READ_CHARACTERISTIC,
4139 sizeof(bluetooth_gatt_client_char_prop_info_t));
4150 static void __bt_handle_client_descriptor_read_data(
4151 event_gattc_read_data *event_data)
4153 int result = BLUETOOTH_ERROR_NONE;
4154 struct gatt_server_info_t *conn_info = NULL;
4155 bluetooth_gatt_client_desc_prop_info_t read_info;
4157 /* Read Information data structures */
4158 GVariant *param = NULL;
4159 GVariant *data = NULL;
4160 GVariant *data_svc_uuid = NULL;
4161 GVariant *data_char_uuid = NULL;
4162 GVariant *data_desc_uuid = NULL;
4163 char *read_val = NULL;
4164 char *svc_uuid = NULL;
4165 char *char_uuid = NULL;
4166 char *desc_uuid = NULL;
4170 memset(&read_info, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
4172 /* Extract Address from conn_id of event data */
4173 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
4174 event_data->uuid_status.conn_status.conn_id);
4176 BT_DBG("Descriptor Read result from addr [%s] status [%d]",
4177 conn_info->addr, event_data->uuid_status.conn_status.status);
4179 /* Fill descriptor informations in buffer */
4180 memcpy(&read_info.descriptor.uuid,
4181 event_data->uuid_status.descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4182 read_info.descriptor.instance_id = event_data->uuid_status.descr_id.inst_id;
4184 /* Fill Characteristic informations in buffer */
4185 memcpy(&read_info.characteristic.uuid,
4186 event_data->uuid_status.char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4187 read_info.characteristic.instance_id = event_data->uuid_status.char_id.inst_id;
4189 /* Fill Service informations in buffer */
4190 memcpy(&read_info.svc.uuid,
4191 event_data->uuid_status.srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4192 read_info.svc.instance_id = event_data->uuid_status.srvc_id.id.inst_id;
4194 /* Fill remote device address */
4195 _bt_convert_addr_string_to_type(read_info.device_address.addr, conn_info->addr);
4197 /* Fill data and reply to all apps waiting for Read result on the same characteristic */
4198 if (event_data->uuid_status.conn_status.status != OAL_STATUS_SUCCESS)
4199 result = BLUETOOTH_ERROR_INTERNAL;
4201 if (event_data->data_len > 0) {
4203 for (i = 0; i < event_data->data_len; i++)
4204 BT_DBG("Data[%d] = [0x%x]", i, event_data->data[i]);
4207 read_val = g_memdup(&event_data->data[0], event_data->data_len);
4209 data = g_variant_new_from_data(
4210 G_VARIANT_TYPE_BYTESTRING,
4212 event_data->data_len,
4215 BT_INFO("Descriptor Read success, but no data!!!");
4217 data = g_variant_new_from_data(
4218 G_VARIANT_TYPE_BYTESTRING,
4224 svc_uuid = g_memdup(&event_data->uuid_status.srvc_id.id.uuid.uuid[0], uuid_len);
4226 data_svc_uuid = g_variant_new_from_data(
4227 G_VARIANT_TYPE_BYTESTRING,
4233 char_uuid = g_memdup(&event_data->uuid_status.char_id.uuid.uuid[0], uuid_len);
4235 data_char_uuid = g_variant_new_from_data(
4236 G_VARIANT_TYPE_BYTESTRING,
4242 desc_uuid = g_memdup(&event_data->uuid_status.descr_id.uuid.uuid[0], uuid_len);
4244 data_desc_uuid = g_variant_new_from_data(
4245 G_VARIANT_TYPE_BYTESTRING,
4250 param = g_variant_new("(isn@ayin@ayin@ayin@ay)", result,
4254 event_data->uuid_status.srvc_id.id.inst_id,
4257 event_data->uuid_status.char_id.inst_id,
4260 event_data->uuid_status.descr_id.inst_id,
4261 event_data->data_len,
4265 char *sender = NULL;
4266 __bt_gatt_get_pending_request_info(BT_GATT_READ_DESCRIPTOR_VALUE, &sender);
4267 _bt_send_event_to_dest(sender, BT_GATT_CLIENT_EVENT,
4268 BLUETOOTH_EVENT_GATT_READ_DESC,
4272 /* Send DBUS return */
4273 __bt_gatt_handle_pending_request_info(result,
4274 BT_GATT_READ_DESCRIPTOR_VALUE,
4276 sizeof(bluetooth_gatt_client_desc_prop_info_t));
4287 static void __bt_handle_client_characteristic_write_data(
4288 event_gattc_write_data *event_data)
4290 int result = BLUETOOTH_ERROR_NONE;
4291 struct gatt_server_info_t *conn_info = NULL;
4292 bluetooth_gatt_client_char_prop_info_t write_info;
4294 /* Read Information data structures */
4295 GVariant *param = NULL;
4296 GVariant *data_svc_uuid = NULL;
4297 GVariant *data_char_uuid = NULL;
4298 char *svc_uuid = NULL;
4299 char *char_uuid = NULL;
4302 memset(&write_info, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
4304 /* Extract Address from conn_id of event data */
4305 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
4306 event_data->conn_status.conn_id);
4308 BT_DBG("Characteristic Write callback from addr [%s] status [%d]",
4309 conn_info->addr, event_data->conn_status.status);
4311 /* Fill char in buffer */
4312 memcpy(&write_info.characteristic.uuid,
4313 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4314 write_info.characteristic.instance_id = event_data->char_id.inst_id;
4316 /* Fill Service in buffer */
4317 memcpy(&write_info.svc.uuid,
4318 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4319 write_info.svc.instance_id = event_data->srvc_id.id.inst_id;
4321 /* Fill remote device address */
4322 _bt_convert_addr_string_to_type(write_info.device_address.addr, conn_info->addr);
4324 if (event_data->conn_status.status != OAL_STATUS_SUCCESS) {
4325 result = BLUETOOTH_ERROR_INTERNAL;
4331 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
4333 data_svc_uuid = g_variant_new_from_data(
4334 G_VARIANT_TYPE_BYTESTRING,
4340 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
4342 data_char_uuid = g_variant_new_from_data(
4343 G_VARIANT_TYPE_BYTESTRING,
4348 param = g_variant_new("(isn@ayin@ayi)", result,
4352 event_data->srvc_id.id.inst_id,
4355 event_data->char_id.inst_id);
4358 char *sender = NULL;
4359 __bt_gatt_get_pending_request_info(BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE, &sender);
4360 _bt_send_event_to_dest(sender, BT_GATT_CLIENT_EVENT,
4361 BLUETOOTH_EVENT_GATT_WRITE_CHAR,
4371 /* Send DBUS return */
4372 __bt_gatt_handle_pending_request_info(result,
4373 BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE,
4375 sizeof(bluetooth_gatt_client_char_prop_info_t));
4379 static void __bt_handle_client_descriptor_write_data(
4380 event_gattc_write_data *event_data)
4382 int result = BLUETOOTH_ERROR_NONE;
4383 struct gatt_server_info_t *conn_info = NULL;
4384 bluetooth_gatt_client_desc_prop_info_t write_info;
4386 /* Write Information data structures */
4387 GVariant *param = NULL;
4388 GVariant *data_svc_uuid = NULL;
4389 GVariant *data_char_uuid = NULL;
4390 GVariant *data_desc_uuid = NULL;
4391 char *svc_uuid = NULL;
4392 char *char_uuid = NULL;
4393 char *desc_uuid = NULL;
4396 memset(&write_info, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
4398 /* Extract Address from conn_id of event data */
4399 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
4400 event_data->conn_status.conn_id);
4402 if (NULL == conn_info) {
4403 BT_ERR("Failed to get the conn info for conn_id [%d]", event_data->conn_status.conn_id);
4407 BT_DBG("Descriptor Write callback from addr [%s] status [%d]",
4408 conn_info->addr, event_data->conn_status.status);
4410 /* Fill descriptor informations in buffer */
4411 memcpy(&write_info.descriptor.uuid,
4412 event_data->descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4413 write_info.descriptor.instance_id = event_data->descr_id.inst_id;
4415 /* Fill Characteristic informations in buffer */
4416 memcpy(&write_info.characteristic.uuid,
4417 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4418 write_info.characteristic.instance_id = event_data->char_id.inst_id;
4420 /* Fill Service informations in buffer */
4421 memcpy(&write_info.svc.uuid,
4422 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4423 write_info.svc.instance_id = event_data->srvc_id.id.inst_id;
4425 /* Fill remote device address */
4426 _bt_convert_addr_string_to_type(write_info.device_address.addr, conn_info->addr);
4428 if (event_data->conn_status.status != OAL_STATUS_SUCCESS) {
4429 result = BLUETOOTH_ERROR_INTERNAL;
4435 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
4437 data_svc_uuid = g_variant_new_from_data(
4438 G_VARIANT_TYPE_BYTESTRING,
4444 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
4446 data_char_uuid = g_variant_new_from_data(
4447 G_VARIANT_TYPE_BYTESTRING,
4453 desc_uuid = g_memdup(&event_data->descr_id.uuid.uuid[0], uuid_len);
4455 data_desc_uuid = g_variant_new_from_data(
4456 G_VARIANT_TYPE_BYTESTRING,
4461 param = g_variant_new("(isn@ayin@ayin@ayi)", result,
4465 event_data->srvc_id.id.inst_id,
4468 event_data->char_id.inst_id,
4471 event_data->descr_id.inst_id);
4474 char *sender = NULL;
4475 __bt_gatt_get_pending_request_info(BT_GATT_WRITE_DESCRIPTOR_VALUE, &sender);
4476 _bt_send_event_to_dest(sender, BT_GATT_CLIENT_EVENT,
4477 BLUETOOTH_EVENT_GATT_WRITE_DESC,
4488 /* Send DBUS return */
4489 __bt_gatt_handle_pending_request_info(result,
4490 BT_GATT_WRITE_DESCRIPTOR_VALUE,
4492 sizeof(bluetooth_gatt_client_desc_prop_info_t));
4495 static void __bt_hanlde_le_device_disconnection(event_dev_conn_status_t *event_data)
4497 int result = BLUETOOTH_ERROR_INTERNAL;
4498 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
4500 _bt_convert_addr_type_to_string(address, (unsigned char *)event_data->address.addr);
4502 /* DBUS Return with fail of pending BT_CONNECT_LE for all the apps */
4503 BT_INFO("Local GATT Client disconnected: Remote addr[%s] ", address + 12);
4505 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE, address,
4506 BT_ADDRESS_STRING_SIZE);
4510 static void __bt_handle_client_notification_registered(
4511 event_gattc_regdereg_notify_t *event_data,
4512 gboolean is_registered)
4514 int result = BLUETOOTH_ERROR_NONE;
4515 struct gatt_server_info_t *conn_info = NULL;
4516 bt_gatt_notif_reg_info_t notif_info;
4519 memset(¬if_info, 0x00, sizeof(bt_gatt_notif_reg_info_t));
4521 BT_INFO("Client Interface [%d] status [%d]",
4522 event_data->conn_id,
4523 event_data->status);
4525 /* Extract Address from conn_id of event data */
4526 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(event_data->conn_id);
4528 BT_INFO("Connection Info is not present, return");
4531 BT_INFO("Notification Registered for addr [%s]", conn_info->addr);
4533 /* Fill svc informations in buffer */
4534 memcpy(¬if_info.svc_uuid,
4535 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4536 notif_info.svc_inst = event_data->srvc_id.id.inst_id;
4538 /* Fill char in buffer */
4539 memcpy(¬if_info.char_uuid,
4540 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4541 notif_info.char_inst = event_data->char_id.inst_id;
4543 /* Fill remote device address */
4544 _bt_convert_addr_string_to_type(notif_info.addr.addr, conn_info->addr);
4546 notif_info.is_registered = is_registered;
4548 if (event_data->status != OAL_STATUS_SUCCESS)
4549 result = BLUETOOTH_ERROR_INTERNAL;
4551 /* Send DBUS Return for BT_GATT_WATCH_CHARACTERISTIC */
4552 __bt_gatt_handle_pending_request_info(result,
4553 BT_GATT_WATCH_CHARACTERISTIC,
4555 sizeof(bt_gatt_notif_reg_info_t));
4558 static void __bt_handle_client_notification_data(event_gattc_notify_data *event_data)
4560 /* No status in this event from OAL */
4561 int result = BLUETOOTH_ERROR_NONE;
4563 /* Read Information data structures */
4564 GVariant *param = NULL;
4565 GVariant *data = NULL;
4566 GVariant *data_svc_uuid = NULL;
4567 GVariant *data_char_uuid = NULL;
4568 char *read_val = NULL;
4569 char *svc_uuid = NULL;
4570 char *char_uuid = NULL;
4576 BT_INFO("Notifcation of charc data changed");
4578 if (event_data->data_len > 0) {
4580 for (i = 0; i < event_data->data_len; i++)
4581 BT_INFO("Data[%d] = [0x%x]", i, event_data->data[i]);
4584 addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
4585 _bt_convert_addr_type_to_string(addr,
4586 (unsigned char *)&(event_data->address.addr));
4589 read_val = g_memdup(&event_data->data[0], event_data->data_len);
4591 data = g_variant_new_from_data(
4592 G_VARIANT_TYPE_BYTESTRING,
4594 event_data->data_len,
4597 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
4599 data_svc_uuid = g_variant_new_from_data(
4600 G_VARIANT_TYPE_BYTESTRING,
4606 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
4608 data_char_uuid = g_variant_new_from_data(
4609 G_VARIANT_TYPE_BYTESTRING,
4615 param = g_variant_new("(isn@ayin@ayin@ay)", result,
4619 event_data->srvc_id.id.inst_id,
4622 event_data->char_id.inst_id,
4623 event_data->data_len,
4627 _bt_send_event(BT_GATT_CLIENT_EVENT,
4628 BLUETOOTH_EVENT_GATT_CHAR_VAL_CHANGED,
4631 BT_ERR("No Data!!");
4644 static void __bt_handle_client_service_changed_ind(event_gattc_service_changed_data *event_data)
4646 bt_gatt_service_info_list_t *svc_info_list;
4648 bt_gatt_service_info_t *svc_info;
4649 GVariant *param = NULL;
4650 char *address_str = NULL;
4651 char *uuid_str = NULL;
4653 svc_info_list = __bt_get_service_info_list(event_data->conn_id);
4654 if (svc_info_list == NULL) {
4655 BT_ERR("svc_info_list is NULL");
4659 if (event_data->change_type) {
4660 /* Add service UUID in list */
4661 svc_info = g_malloc0(sizeof(bt_gatt_service_info_t));
4662 memcpy(svc_info->uuid, event_data->uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4663 svc_info->inst_id = event_data->inst_id;
4664 svc_info->is_primary = 1; // TODO: Need to check is_primary is required or not
4665 svc_info_list->services = g_slist_append(svc_info_list->services, svc_info);
4666 BT_DBG("Service created and added in Svc info list or connection ID %d", event_data->conn_id);
4668 /* Remove service UUID in list */
4669 for (l = svc_info_list->services; l != NULL; l = g_slist_next(l)) {
4670 svc_info = (bt_gatt_service_info_t *)l->data;
4671 if (svc_info == NULL)
4674 if (!memcmp(svc_info->uuid, event_data->uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
4675 svc_info_list->services = g_slist_remove(svc_info_list->services, svc_info);
4676 __bt_free_service_info(svc_info);
4682 address_str = g_malloc0(BT_ADDRESS_STRING_SIZE);
4683 uuid_str = g_malloc0(BT_UUID_STRING_MAX);
4684 _bt_convert_addr_type_to_string(address_str, event_data->address.addr);
4685 _bt_uuid_to_string(&event_data->uuid, uuid_str);
4687 param = g_variant_new("(iiss)", event_data->inst_id, event_data->change_type, address_str, uuid_str);
4689 _bt_send_event(BT_GATT_CLIENT_EVENT,
4690 BLUETOOTH_EVENT_GATT_CLIENT_SERVICE_CHANGED,
4692 g_free(address_str);
4696 gboolean _bt_is_remote_gatt_device_connected(bluetooth_device_address_t *address)
4699 struct gatt_server_info_t *conn_info = NULL;
4700 gboolean connected = FALSE;
4702 addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
4703 _bt_convert_addr_type_to_string(addr,
4704 (unsigned char *)&(address->addr));
4706 BT_DBG("Check GATT connection status of [%s]", addr);
4707 /* Check if device is already in connected list */
4708 conn_info = _bt_find_remote_gatt_server_info(addr);
4710 BT_DBG("Remote GATT Server device [%s] is Connected", conn_info->addr);
4713 struct gatt_client_info_t *client_info = NULL;
4715 BT_DBG("Remote GATT Server Device [%s] is not Connected", addr);
4717 /* Check if device is already in connected list */
4718 client_info = _bt_find_remote_gatt_client_info(addr);
4720 BT_DBG("Remote Client device [%s] is Connected", client_info->addr);
4723 BT_DBG("Remote GATT Client Device [%s] is not Connected", addr);
4731 void _bt_handle_invocation_context(int function_name, void *data)
4733 switch (function_name) {
4735 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE, BT_CONNECT_LE,
4736 (char *)data, BT_ADDRESS_STRING_SIZE);
4743 int _bt_connect_le_device(bluetooth_device_address_t *address,
4744 int auto_connect, int client_id)
4746 struct gatt_server_info_t *conn_info = NULL;
4747 struct gatt_out_conn_info_t *out_conn_info = NULL;
4749 invocation_info_t *req_info = NULL;
4750 int ret = OAL_STATUS_SUCCESS;
4752 char *remote_address = NULL;
4754 BT_CHECK_PARAMETER(address, return);
4756 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4757 _bt_convert_addr_type_to_string(addr, address->addr);
4758 BT_DBG("GATT Client connect request for address [%s] client instance [%d]",
4762 /* Check if Remote Device is already under connection progress */
4763 req_info = _bt_get_request_info_data_from_function_name(BT_CONNECT_LE);
4765 remote_address = (char*)req_info->user_data;
4766 if (remote_address && !strcasecmp(remote_address, addr)) {/* Address matched */
4767 BT_DBG("Already Connection ongoing for same remote GATT Server address [%s]", remote_address);
4768 /* Return and wait for events to be sent to all apps */
4770 return BLUETOOTH_ERROR_IN_PROGRESS;
4774 /* Check if remote GATT Server is connected or not */
4775 conn_info = _bt_find_remote_gatt_server_info(addr);
4777 BT_ERR("GATT Server is already connected..");
4779 return BLUETOOTH_ERROR_ALREADY_CONNECT;
4782 /* TODO Check Requirement of holding Advertisement before initiating LE connect */
4785 /* Check if app sent 0 client id for connection, in such case, use default gatt client ID */
4786 if (client_id == 0) {
4787 /* GATT CLient connect request sent by an app without any client instance [0] */
4788 BT_DBG("Assign default GATT client id [%d]", gatt_default_client);
4789 client_id = gatt_default_client;
4792 BT_INFO("Connect using CLient ID [%d]", client_id);
4793 ret = gattc_connect(client_id, (bt_address_t*)(address), auto_connect);
4795 if (ret != OAL_STATUS_SUCCESS) {
4796 BT_ERR("gattc_connect is failed. ret: %d", ret);
4798 _bt_restart_le_scan();
4799 return _bt_convert_oal_status_to_bt_error(ret);
4802 /* Mark this as outgoing connection */
4803 out_conn_info = g_new0(struct gatt_out_conn_info_t, 1);
4804 out_conn_info->addr = g_strdup(addr);
4805 out_conn_info->client_id = client_id;
4806 BT_INFO("Added outgoing connection info addr[%s]", out_conn_info->addr + 12);
4807 outgoing_gatt_conn_list = g_slist_append(outgoing_gatt_conn_list, out_conn_info);
4810 return BLUETOOTH_ERROR_NONE;
4813 int _bt_gatt_get_primary_services(char *address)
4815 BT_CHECK_PARAMETER(address, return);
4816 struct gatt_server_info_t *conn_info = NULL;
4817 invocation_info_t *req_info = NULL;
4818 int ret = OAL_STATUS_SUCCESS;
4820 /* Check if any app is already browsing primary services on the same remote GATT Server */
4821 req_info = _bt_get_request_info_data(BT_GATT_GET_PRIMARY_SERVICES, address);
4823 BT_INFO("Already Primary Service Browsing ongoing for same rmeote GATT Server");
4824 /* Return and wait for events to be sent to all apps */
4825 return BLUETOOTH_ERROR_NONE;
4828 /* Check if remote GATT Server is connected or not */
4829 conn_info = _bt_find_remote_gatt_server_info(address);
4831 BT_ERR("GATT Server is not yet connected..");
4832 return BLUETOOTH_ERROR_NOT_CONNECTED;
4835 BT_INFO("Get all services. GATT Server [%s] is connected, conn Id [%d]",
4836 conn_info->addr + 12, conn_info->connection_id);
4838 /* Send Primary Service Browsing request to stack */
4839 ret = gattc_search_service(conn_info->connection_id, NULL);
4840 if (ret != OAL_STATUS_SUCCESS) {
4841 BT_ERR("ret: %d", ret);
4842 return _bt_convert_oal_status_to_bt_error(ret);
4844 return BLUETOOTH_ERROR_NONE;
4847 int _bt_gatt_get_all_characteristic(bluetooth_gatt_client_svc_prop_info_t *svc)
4849 BT_CHECK_PARAMETER(svc, return);
4850 struct gatt_server_info_t *conn_info = NULL;
4851 invocation_info_t *req_info = NULL;
4852 bluetooth_gatt_client_svc_prop_info_t *prop;
4853 oal_gatt_srvc_id_t srvc_id;
4854 int ret = OAL_STATUS_SUCCESS;
4857 /* Check if any app is already browsing characteristics of the same service on the same remote GATT Server */
4858 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_GET_SERVICE_PROPERTIES);
4860 prop = (bluetooth_gatt_client_svc_prop_info_t*)req_info->user_data;
4861 if (prop && !memcmp(svc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t))
4862 && !memcmp(prop->svc.uuid, svc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
4863 && prop->svc.instance_id == svc->svc.instance_id) {
4864 BT_INFO("Already Properties browsing for Primary Service ongoing for same remote GATT Server");
4865 /* Return and wait for events to be sent to all apps */
4866 return BLUETOOTH_ERROR_NONE;
4870 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4871 _bt_convert_addr_type_to_string(addr, svc->device_address.addr);
4873 /* Check if remote GATT Server is connected or not */
4874 conn_info = _bt_find_remote_gatt_server_info(addr);
4876 BT_ERR("GATT Server is not yet connected..");
4878 return BLUETOOTH_ERROR_NOT_CONNECTED;
4881 BT_DBG("Get all charc. GATT Server [%s] is connected, conn Id [%d]",
4882 conn_info->addr, conn_info->connection_id);
4884 srvc_id.is_prmry = TRUE;
4885 srvc_id.id.inst_id = svc->svc.instance_id;
4886 memcpy(srvc_id.id.uuid.uuid, svc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4888 /* Search All Characteristic */
4889 ret = gattc_get_characteristic(conn_info->connection_id, &srvc_id, NULL);
4890 if (ret != OAL_STATUS_SUCCESS) {
4891 BT_ERR("ret: %d", ret);
4893 return _bt_convert_oal_status_to_bt_error(ret);
4896 return BLUETOOTH_ERROR_NONE;
4899 int _bt_gatt_get_all_characteristic_properties(
4900 bluetooth_gatt_client_char_prop_info_t *chr)
4902 struct gatt_server_info_t *conn_info = NULL;
4903 invocation_info_t *req_info = NULL;
4904 bluetooth_gatt_client_char_prop_info_t *prop;
4905 oal_gatt_srvc_id_t srvc_id;
4906 oal_gatt_id_t char_id;
4907 int ret = OAL_STATUS_SUCCESS;
4910 BT_CHECK_PARAMETER(chr, return);
4912 /* Check if any app is already browsing descriptors of the same char of
4913 particular service on the same remote GATT Server */
4914 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_GET_CHARACTERISTIC_PROPERTIES);
4916 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4917 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4918 && !memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4919 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4920 && !memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4921 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4922 BT_INFO("Already Properties browsing for Characteristic ongoing for same remote GATT Server");
4923 /* Return and wait for events to be sent to all apps */
4924 return BLUETOOTH_ERROR_NONE;
4928 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4929 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4931 /* Check if remote GATT Server is connected or not */
4932 conn_info = _bt_find_remote_gatt_server_info(addr);
4934 BT_ERR("GATT Server is not yet connected..");
4936 return BLUETOOTH_ERROR_NOT_CONNECTED;
4939 BT_DBG("Get all desc. GATT Server [%s] is connected, conn Id [%d]",
4940 conn_info->addr, conn_info->connection_id);
4942 srvc_id.is_prmry = TRUE;
4943 srvc_id.id.inst_id = chr->svc.instance_id;
4944 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4946 char_id.inst_id = chr->characteristic.instance_id;
4947 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4949 /* Search All Descriptors */
4950 ret = gattc_get_descriptor(conn_info->connection_id, &srvc_id, &char_id, NULL);
4951 if (ret != OAL_STATUS_SUCCESS) {
4952 BT_ERR("ret: %d", ret);
4954 return _bt_convert_oal_status_to_bt_error(ret);
4957 return BLUETOOTH_ERROR_NONE;
4960 int _bt_gatt_read_characteristic_value(
4961 bluetooth_gatt_client_char_prop_info_t *chr)
4963 struct gatt_server_info_t *conn_info = NULL;
4964 invocation_info_t *req_info = NULL;
4965 bluetooth_gatt_client_char_prop_info_t *prop;
4966 oal_gatt_srvc_id_t srvc_id;
4967 oal_gatt_id_t char_id;
4968 int ret = OAL_STATUS_SUCCESS;
4971 BT_CHECK_PARAMETER(chr, return);
4973 /* Check if any app is already Reading characteristic of the same char of
4974 particular service on the same remote GATT Server */
4975 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_READ_CHARACTERISTIC);
4977 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4978 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4979 && !memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4980 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4981 && !memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4982 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4983 BT_INFO("Already Characteristic value Read operation in progress for same remote GATT Server");
4984 /* Return and wait for events to be sent to all apps */
4985 return BLUETOOTH_ERROR_NONE;
4989 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4990 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4992 /* Check if remote GATT Server is connected or not */
4993 conn_info = _bt_find_remote_gatt_server_info(addr);
4995 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4996 conn_info->addr, conn_info->connection_id);
4998 BT_ERR("GATT Server is not yet connected..");
5000 return BLUETOOTH_ERROR_NOT_CONNECTED;
5003 srvc_id.is_prmry = TRUE;
5004 srvc_id.id.inst_id = chr->svc.instance_id;
5005 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
5007 char_id.inst_id = chr->characteristic.instance_id;
5008 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
5010 /* Search All Descriptors */
5011 ret = gattc_read_characteristic(conn_info->connection_id, &srvc_id, &char_id, OAL_GATT_AUTH_REQ_NONE);
5012 if (ret != OAL_STATUS_SUCCESS) {
5013 BT_ERR("ret: %d", ret);
5015 return _bt_convert_oal_status_to_bt_error(ret);
5018 return BLUETOOTH_ERROR_NONE;
5021 int _bt_gatt_read_descriptor_value(
5022 bluetooth_gatt_client_desc_prop_info_t *desc)
5024 struct gatt_server_info_t *conn_info = NULL;
5025 invocation_info_t *req_info = NULL;
5026 bluetooth_gatt_client_desc_prop_info_t *prop;
5027 oal_gatt_srvc_id_t srvc_id;
5028 oal_gatt_id_t char_id;
5029 oal_gatt_id_t desc_id;
5030 int ret = OAL_STATUS_SUCCESS;
5033 BT_CHECK_PARAMETER(desc, return);
5035 /* Check if any app is already Reading descriptors of the same char of
5036 particular service on the same remote GATT Server */
5037 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_READ_DESCRIPTOR_VALUE);
5039 prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
5040 if (prop && !memcmp(desc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
5041 && !memcmp(desc->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
5042 && desc->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
5043 && !memcmp(desc->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
5044 && desc->characteristic.instance_id == prop->characteristic.instance_id /* Characteristic Instance ID matched */
5045 && !memcmp(desc->descriptor.uuid, prop->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Descriptor UUID matched */
5046 && desc->descriptor.instance_id == prop->descriptor.instance_id) { /* Descriptor Instance ID matched */
5047 BT_INFO("Already Descriptor value Read operation in progress for same remote GATT Server");
5048 /* Return and wait for events to be sent to all apps */
5049 return BLUETOOTH_ERROR_NONE;
5053 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
5054 _bt_convert_addr_type_to_string(addr, desc->device_address.addr);
5056 /* Check if remote GATT Server is connected or not */
5057 conn_info = _bt_find_remote_gatt_server_info(addr);
5059 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
5060 conn_info->addr, conn_info->connection_id);
5062 BT_ERR("GATT Server is not yet connected..");
5064 return BLUETOOTH_ERROR_NOT_CONNECTED;
5067 srvc_id.is_prmry = TRUE;
5068 srvc_id.id.inst_id = desc->svc.instance_id;
5069 memcpy(srvc_id.id.uuid.uuid, desc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
5071 char_id.inst_id = desc->characteristic.instance_id;
5072 memcpy(char_id.uuid.uuid, desc->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
5074 desc_id.inst_id = desc->descriptor.instance_id;
5075 memcpy(desc_id.uuid.uuid, desc->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
5077 /* Search All Descriptors */
5078 ret = gattc_read_descriptor(conn_info->connection_id, &srvc_id, &char_id,
5079 &desc_id, OAL_GATT_AUTH_REQ_NONE);
5080 if (ret != OAL_STATUS_SUCCESS) {
5081 BT_ERR("ret: %d", ret);
5083 return _bt_convert_oal_status_to_bt_error(ret);
5086 return BLUETOOTH_ERROR_NONE;
5090 int _bt_gatt_acquire_notify(bluetooth_gatt_client_char_prop_info_t *chr, int *fd, int *mtu)
5092 struct gatt_server_info_t *conn_info = NULL;
5093 oal_gatt_srvc_id_t srvc_id;
5094 oal_gatt_id_t char_id;
5095 int ret = OAL_STATUS_SUCCESS;
5098 BT_CHECK_PARAMETER(chr, return);
5100 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
5101 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
5103 /* Check if remote GATT Server is connected or not */
5104 conn_info = _bt_find_remote_gatt_server_info(addr);
5106 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
5107 conn_info->addr, conn_info->connection_id);
5109 BT_ERR("GATT Server is not yet connected..");
5111 return BLUETOOTH_ERROR_NOT_CONNECTED;
5114 srvc_id.is_prmry = TRUE;
5115 srvc_id.id.inst_id = chr->svc.instance_id;
5116 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
5118 char_id.inst_id = chr->characteristic.instance_id;
5119 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
5121 ret = gattc_acquire_notify(conn_info->connection_id, &srvc_id, &char_id, fd, mtu);
5123 if (ret != OAL_STATUS_SUCCESS) {
5124 BT_ERR("ret: %d", ret);
5126 return _bt_convert_oal_status_to_bt_error(ret);
5128 BT_INFO("GATT characterstics FD [%d] mtu[%d]", *fd, *mtu);
5130 return BLUETOOTH_ERROR_NONE;
5134 int _bt_gatt_acquire_write(bluetooth_gatt_client_char_prop_info_t *chr, int *fd, int *mtu)
5137 struct gatt_server_info_t *conn_info = NULL;
5138 oal_gatt_srvc_id_t srvc_id;
5139 oal_gatt_id_t char_id;
5140 int ret = OAL_STATUS_SUCCESS;
5143 BT_CHECK_PARAMETER(chr, return);
5145 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
5146 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
5148 /* Check if remote GATT Server is connected or not */
5149 conn_info = _bt_find_remote_gatt_server_info(addr);
5151 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
5152 conn_info->addr, conn_info->connection_id);
5154 BT_ERR("GATT Server is not yet connected..");
5156 return BLUETOOTH_ERROR_NOT_CONNECTED;
5159 srvc_id.is_prmry = TRUE;
5160 srvc_id.id.inst_id = chr->svc.instance_id;
5161 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
5163 char_id.inst_id = chr->characteristic.instance_id;
5164 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
5166 ret = gattc_acquire_write(conn_info->connection_id, &srvc_id, &char_id,
5167 OAL_GATT_AUTH_REQ_NONE, fd, mtu);
5168 if (ret != OAL_STATUS_SUCCESS) {
5169 BT_ERR("ret: %d", ret);
5171 return _bt_convert_oal_status_to_bt_error(ret);
5173 BT_INFO("GATT characterstics FD [%d] mtu [%d]", *fd, *mtu);
5175 return BLUETOOTH_ERROR_NONE;
5180 /* Write Characteristic */
5181 int _bt_gatt_write_characteristic_value_by_type(
5182 bluetooth_gatt_client_char_prop_info_t *chr,
5183 bluetooth_gatt_att_data_t *data,
5184 bluetooth_gatt_write_type_e write_type)
5186 struct gatt_server_info_t *conn_info = NULL;
5187 invocation_info_t *req_info = NULL;
5188 bluetooth_gatt_client_char_prop_info_t *prop;
5189 oal_gatt_srvc_id_t srvc_id;
5190 oal_gatt_id_t char_id;
5191 int ret = OAL_STATUS_SUCCESS;
5194 BT_CHECK_PARAMETER(chr, return);
5195 BT_CHECK_PARAMETER(data, return);
5197 /* Check if any app is already writing same char of
5198 particular service on the same remote GATT Server */
5199 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE);
5201 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
5202 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
5203 && !memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
5204 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
5205 && !memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
5206 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
5207 BT_INFO("Already Characteristic Write Value operation in progress for same remote GATT Server");
5208 /* Return and wait for events to be sent to all apps */
5209 return BLUETOOTH_ERROR_NONE;
5213 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
5214 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
5216 /* Check if remote GATT Server is connected or not */
5217 conn_info = _bt_find_remote_gatt_server_info(addr);
5219 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
5220 conn_info->addr, conn_info->connection_id);
5222 BT_ERR("GATT Server is not yet connected..");
5224 return BLUETOOTH_ERROR_NOT_CONNECTED;
5227 srvc_id.is_prmry = TRUE;
5228 srvc_id.id.inst_id = chr->svc.instance_id;
5229 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
5231 char_id.inst_id = chr->characteristic.instance_id;
5232 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
5234 /* Write CHar value */
5235 ret = gattc_write_characteristic(conn_info->connection_id,
5237 (oal_gatt_write_type_t)write_type, data->length,
5238 OAL_GATT_AUTH_REQ_NONE, (char *)(&data->data[0]));
5239 if (ret != OAL_STATUS_SUCCESS) {
5240 BT_ERR("ret: %d", ret);
5242 return _bt_convert_oal_status_to_bt_error(ret);
5245 return BLUETOOTH_ERROR_NONE;
5248 /* Write Descriptor */
5249 int _bt_gatt_write_descriptor_value_by_type(
5250 bluetooth_gatt_client_desc_prop_info_t *desc,
5251 bluetooth_gatt_att_data_t *data,
5252 bluetooth_gatt_write_type_e write_type)
5254 struct gatt_server_info_t *conn_info = NULL;
5255 invocation_info_t *req_info = NULL;
5256 bluetooth_gatt_client_desc_prop_info_t *prop;
5257 oal_gatt_srvc_id_t srvc_id;
5258 oal_gatt_id_t char_id;
5259 oal_gatt_id_t desc_id;
5260 int ret = OAL_STATUS_SUCCESS;
5264 BT_CHECK_PARAMETER(desc, return);
5265 BT_CHECK_PARAMETER(data, return);
5269 /* Check if any app is already writing on same Descriptor of the same char of
5270 particular service on the same remote GATT Server */
5271 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_WRITE_DESCRIPTOR_VALUE);
5273 prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
5274 if (prop && !memcmp(desc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
5275 && !memcmp(desc->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
5276 && desc->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
5277 && !memcmp(desc->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
5278 && desc->characteristic.instance_id == prop->characteristic.instance_id /* Characteristic Instance ID matched */
5279 && !memcmp(desc->descriptor.uuid, prop->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Descriptor UUID matched */
5280 && desc->descriptor.instance_id == prop->descriptor.instance_id) { /* Descriptor Instance ID matched */
5281 BT_INFO("Already Descriptor value Write operation in progress for same remote GATT Server");
5282 /* Return and wait for events to be sent to all apps */
5283 return BLUETOOTH_ERROR_NONE;
5287 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
5288 _bt_convert_addr_type_to_string(addr, desc->device_address.addr);
5290 /* Check if remote GATT Server is connected or not */
5291 conn_info = _bt_find_remote_gatt_server_info(addr);
5293 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
5294 conn_info->addr, conn_info->connection_id);
5296 BT_ERR("GATT Server is not yet connected..");
5298 return BLUETOOTH_ERROR_NOT_CONNECTED;
5301 srvc_id.is_prmry = TRUE;
5302 srvc_id.id.inst_id = desc->svc.instance_id;
5303 memcpy(srvc_id.id.uuid.uuid, desc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
5305 char_id.inst_id = desc->characteristic.instance_id;
5306 memcpy(char_id.uuid.uuid, desc->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
5308 desc_id.inst_id = desc->descriptor.instance_id;
5309 memcpy(desc_id.uuid.uuid, desc->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
5312 BT_INFO("Connection ID [%d] write type [%d] data length [%d]", conn_info->connection_id, write_type, data->length);
5313 for (k = 0; k < data->length; k++)
5314 BT_INFO("Data[%d] [0x%x]", k, data->data[k]);
5316 ret = gattc_write_descriptor(conn_info->connection_id,
5317 &srvc_id, &char_id, &desc_id,
5318 (oal_gatt_write_type_t)write_type, data->length,
5319 OAL_GATT_AUTH_REQ_NONE, (char *)(&data->data[0]));
5320 if (ret != OAL_STATUS_SUCCESS) {
5321 BT_ERR("ret: %d", ret);
5323 return _bt_convert_oal_status_to_bt_error(ret);
5326 return BLUETOOTH_ERROR_NONE;
5329 int _bt_gatt_watch_characteristic(
5330 bluetooth_gatt_client_char_prop_info_t *chr,
5334 struct gatt_server_info_t *conn_info = NULL;
5335 oal_gatt_srvc_id_t srvc_id;
5336 oal_gatt_id_t char_id;
5337 int ret = OAL_STATUS_SUCCESS;
5340 BT_CHECK_PARAMETER(chr, return);
5342 BT_INFO("Client ID [%d] Is Notify [%d]", client_id, is_notify);
5344 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
5345 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
5347 /* Check if remote GATT Server is connected or not */
5348 conn_info = _bt_find_remote_gatt_server_info(addr);
5350 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
5351 conn_info->addr, conn_info->connection_id);
5353 BT_ERR("GATT Server is not yet connected..");
5355 return BLUETOOTH_ERROR_NOT_CONNECTED;
5357 srvc_id.is_prmry = TRUE;
5358 srvc_id.id.inst_id = chr->svc.instance_id;
5359 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
5361 char_id.inst_id = chr->characteristic.instance_id;
5362 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
5364 /* Register or unregister Notification characteristic */
5366 ret = gattc_register_for_notification(conn_info->connection_id,
5367 (bt_address_t*)&(chr->device_address),
5368 &srvc_id, &char_id);
5370 ret = gattc_deregister_for_notification(conn_info->connection_id,
5371 (bt_address_t*)&(chr->device_address),
5372 &srvc_id, &char_id);
5374 BT_INFO("Result[%d]", ret);
5375 if (ret != OAL_STATUS_SUCCESS) {
5376 BT_ERR("ret: %d", ret);
5378 return _bt_convert_oal_status_to_bt_error(ret);
5381 return BLUETOOTH_ERROR_NONE;
5385 int _bt_disconnect_le_device(bluetooth_device_address_t *address,
5388 struct gatt_server_info_t *conn_info = NULL;
5389 struct gatt_client_info_t *rem_client_conn_info = NULL;
5390 invocation_info_t *req_info = NULL;
5391 int ret = OAL_STATUS_SUCCESS;
5393 char *remote_address = NULL;
5395 BT_CHECK_PARAMETER(address, return);
5397 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
5398 _bt_convert_addr_type_to_string(addr, address->addr);
5399 BT_INFO("GATT Client Disconnect request for address [%s]", addr + 12);
5401 /* Check if Remote Device is already under connection progress */
5402 req_info = _bt_get_request_info_data_from_function_name(BT_DISCONNECT_LE);
5404 remote_address = (char*)req_info->user_data;
5405 if (remote_address && !strcasecmp(remote_address, addr)) {/* Address matched */
5406 BT_DBG("Already DisConnection ongoing for same remote GATT Server address [%s]", remote_address);
5407 /* Return success and wait for events to be sent to all apps */
5409 return BLUETOOTH_ERROR_IN_PROGRESS;
5412 /* Check if remote GATT Server is connected or not */
5413 conn_info = _bt_find_remote_gatt_server_info(addr);
5415 /* Check if app sent 0 client id for Disconnection, in such case, use default gatt client ID */
5416 if (client_id == 0) {
5417 BT_INFO("GATT CLient Disconnect request sent by an app without any client instance [%d]",
5419 BT_INFO("Assign default GATT client id [%d]", gatt_default_client);
5420 client_id = gatt_default_client;
5423 BT_INFO("Disconnect remote gatt server using CLient ID [%d] Connection ID [%d]", client_id, conn_info->connection_id);
5424 ret = gattc_disconnect(client_id, (bt_address_t*)(address),
5425 conn_info->connection_id);
5427 /* check if remote client is connected */
5428 rem_client_conn_info = _bt_find_remote_gatt_client_info(addr);
5430 if (!rem_client_conn_info || client_id != 0) {
5431 BT_ERR("GATT device is not connected..");
5433 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
5436 BT_INFO("Disconnect remote gatt client ");
5438 ret = gatts_disconnect(rem_client_conn_info->instance_id,
5439 (bt_address_t*)(address), rem_client_conn_info->connection_id);
5442 if (ret != OAL_STATUS_SUCCESS) {
5443 BT_ERR("ret: %d", ret);
5445 return _bt_convert_oal_status_to_bt_error(ret);
5448 return BLUETOOTH_ERROR_NONE;
5451 int _bt_gatt_watch_service_changed_indication(const char *sender,
5452 bluetooth_device_address_t *address,
5453 gboolean is_enabled)
5456 bt_service_app_info_t *info = NULL;
5458 BT_INFO("%s Servic changed Indication watcher for app [%s]",
5459 is_enabled ? "Enable":"Disable", sender);
5461 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
5464 if (g_strcmp0(sender, info->sender) == 0 &&
5465 memcmp(info->address.addr, address->addr,
5466 sizeof(bluetooth_device_address_t)) == 0) {
5467 BT_DBG("Found GATT client App.. [%s], sender [%s]", info->uuid, info->sender);
5468 info->is_watcher_enabled = is_enabled;
5472 return BLUETOOTH_ERROR_NONE;
5475 int _bt_unregister_gatt_client_instance(const char *sender, int client_id)
5477 bt_service_app_info_t *info = NULL;
5480 BT_DBG("Unregister Allocated GATT Client instance [%s] Client ID [%d]", sender, client_id);
5482 /* Unregister CLient instance associated with address X. It is possible that another app still
5483 has client_id valid for same remote address */
5484 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
5487 /* Exact matching of sender */
5488 if (!g_strcmp0(info->sender, sender) && info->client_id == client_id) { /* Check for only valid GATT client Instance */
5489 numapps[k].client_id = -1;
5490 numapps[k].is_initialized = FALSE;
5491 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
5492 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
5493 memset(&numapps[k].address.addr, 0x00, sizeof(bluetooth_device_address_t));
5495 /* Its a GATT Client Instance */
5496 ret = gattc_deregister(client_id);
5497 if (ret != OAL_STATUS_SUCCESS) {
5498 BT_ERR("DeAllocate GATT Client instance with stack Fail ret: %d", ret);
5499 return _bt_convert_oal_status_to_bt_error(ret);
5501 return BLUETOOTH_ERROR_NONE;
5506 return BLUETOOTH_ERROR_NOT_FOUND;
5509 static void __bt_handle_client_mtu_exchange_completed(event_gattc_mtu_configured_t *event_data)
5511 int result = BLUETOOTH_ERROR_NONE;
5512 struct gatt_server_info_t *conn_info = NULL;
5513 GVariant *param = NULL;
5517 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(event_data->conn_id);
5518 if (conn_info == NULL) {
5519 BT_ERR("Cant find connection Information");
5523 BT_DBG("GATT Client: MTU Configured from addr [%s] status [%d] MTU size [%d]",
5524 conn_info->addr, event_data->status, event_data->mtu);
5526 if (event_data->status != OAL_STATUS_SUCCESS)
5527 result = BLUETOOTH_ERROR_INTERNAL;
5529 /* DBUS Return fo BT_REQ_ATT_MTU for all the apps */
5530 __bt_gatt_handle_pending_request_info(result, BT_REQ_ATT_MTU, conn_info->addr,
5531 BT_ADDRESS_STRING_SIZE);
5533 if (result == BLUETOOTH_ERROR_NONE) {
5534 mtu = event_data->mtu;
5535 param = g_variant_new("(isqy)",
5541 /* Send event to BT-API */
5542 _bt_send_event(BT_DEVICE_EVENT,
5543 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
5546 /* Update the MTU for current connection */
5547 __bt_update_mtu_gatt_device(conn_info->addr, event_data->mtu);
5551 static struct gatt_mtu_info_t *__bt_find_mtu_gatt_device(char *address)
5554 struct gatt_mtu_info_t *info = NULL;
5556 for (l = gatt_mtu_info_list; l != NULL; l = g_slist_next(l)) {
5557 info = (struct gatt_mtu_info_t*)l->data;
5561 if (!g_strcmp0(info->addr, address)) {
5562 BT_DBG("Remote GATT device found addr[%s]", info->addr);
5567 BT_DBG("Not found Remote GATT device addr[%s]", address);
5571 static void __bt_remove_mtu_gatt_device(char *address)
5573 struct gatt_mtu_info_t *dev_info = NULL;
5575 dev_info = __bt_find_mtu_gatt_device(address);
5578 BT_DBG("removing the gatt device from mtu list");
5579 gatt_mtu_info_list = g_slist_remove(gatt_mtu_info_list, dev_info);
5580 g_free(dev_info->addr);
5585 static void __bt_add_mtu_gatt_device(char *address)
5587 struct gatt_mtu_info_t *dev_info = NULL;
5589 dev_info = __bt_find_mtu_gatt_device(address);
5592 BT_DBG("adding the gatt device in mtu list");
5593 dev_info = g_new0(struct gatt_mtu_info_t, 1);
5594 dev_info->addr = g_strdup(address);
5595 dev_info->att_mtu = BT_DEFAULT_ATT_MTU;
5596 gatt_mtu_info_list = g_slist_append(gatt_mtu_info_list, dev_info);
5600 static void __bt_update_mtu_gatt_device(char *address, int mtu)
5602 struct gatt_mtu_info_t *dev_info = NULL;
5604 dev_info = __bt_find_mtu_gatt_device(address);
5607 dev_info->att_mtu = mtu;
5611 int _bt_gatt_get_data_batching_available_packets(
5612 guint *available_packets)
5614 int ret = OAL_STATUS_SUCCESS;
5616 BT_CHECK_PARAMETER(available_packets, return);
5618 ret = gatt_get_data_batching_available_packets(available_packets);
5619 if (ret != OAL_STATUS_SUCCESS) {
5620 BT_ERR("ret: %d", ret);
5621 return _bt_convert_oal_status_to_bt_error(ret);
5624 return BLUETOOTH_ERROR_NONE;
5627 int _bt_gatt_enable_data_batching(bluetooth_device_address_t *address,
5628 int packet_threshold, int timeout)
5630 int ret = OAL_STATUS_SUCCESS;
5631 char remote_address[BT_ADDRESS_STRING_SIZE] = { 0 };
5633 BT_CHECK_PARAMETER(address, return);
5635 _bt_convert_addr_type_to_string(remote_address, address->addr);
5636 BT_INFO("Enable GATT data batching. address[%s] packet_threshold[%d] timeout[%d]",
5637 remote_address, packet_threshold, timeout);
5639 ret = gatt_enable_data_batching((bt_address_t*)(address), packet_threshold, timeout);
5641 if (ret != OAL_STATUS_SUCCESS) {
5642 BT_ERR("ret: %d", ret);
5643 return _bt_convert_oal_status_to_bt_error(ret);
5646 return BLUETOOTH_ERROR_NONE;
5649 int _bt_gatt_disable_data_batching(bluetooth_device_address_t *address)
5651 int ret = OAL_STATUS_SUCCESS;
5652 char remote_address[BT_ADDRESS_STRING_SIZE] = { 0 };
5654 BT_CHECK_PARAMETER(address, return);
5656 _bt_convert_addr_type_to_string(remote_address, address->addr);
5657 BT_INFO("Disable GATT data batching. address[%s]", remote_address);
5659 ret = gatt_disable_data_batching((bt_address_t*)(address));
5661 if (ret != OAL_STATUS_SUCCESS) {
5662 BT_ERR("ret: %d", ret);
5663 return _bt_convert_oal_status_to_bt_error(ret);
5666 return BLUETOOTH_ERROR_NONE;