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 #ifdef TIZEN_GATT_CLIENT
41 #include "bluetooth-gatt-client-api.h"
44 #include <oal-hardware.h>
45 #include <oal-manager.h>
46 #include <oal-event.h>
47 #include <oal-adapter-mgr.h>
48 #include <oal-device-mgr.h>
51 #define BT_GATT_TRANSPORT_LE 0
52 #define BT_GATT_TRANSPORT_BR_EDR 1
53 #define BT_GATT_TRANSPORT_LE_BR_EDR 2
54 #define BT_UUID_STRING_MAX 64
55 #define BT_SENDER_MAX_LENGTH 50
56 #define MAX_APPS_SUPPORTED 11 /* Slot 0 is not used */
57 #define BT_DEFAULT_ATT_MTU 23
59 #ifdef TIZEN_GATT_CLIENT
66 #define UUID_MAX_LEN 50
69 #define BDADDR_ANY (&(bluetooth_device_address_t) {{0, 0, 0, 0, 0, 0} })
71 #ifdef TIZEN_GATT_CLIENT
72 char uuid_list[NUM_UUID][BT_UUID_STRING_MAX] = {"0000b00b-0000-0000-f065-080080fa49b5", /* Used by BLEAPP */
73 "0000b00b-1111-1111-0123-456789ab0cd2", /* Used by BLEAPP */
74 "0000b00b-2222-1111-0123-456789ab0cd2",
75 "0000b00b-3333-1111-0123-456789ab0cd2",
76 "0000b00b-4444-1111-0123-456789ab0cd2",
77 "0000b00b-5555-1111-0123-456789ab0cd2",
78 "0000b00b-6666-1111-0123-456789ab0cd2",
79 "0000b00b-7777-1111-0123-456789ab0cd2",
80 "0000b00b-8888-1111-0123-456789ab0cd2",
81 "0000b00b-9999-1111-0123-456789ab0cd2",
82 "0000b00b-aaaa-1111-0123-456789ab0cd2",
83 "0000b00b-bbbb-1111-0123-456789ab0cd2",
84 "0000b00b-cccc-1111-0123-456789ab0cd2",
85 "0000b00b-dddd-1111-0123-456789ab0cd2",
86 "0000b00b-eeee-1111-0123-456789ab0cd2",
87 "0000b00b-ffff-1111-0123-456789ab0cd2",
88 "0000b00c-0000-1111-0123-456789ab0cd2",
89 "0000b00c-1111-1111-0123-456789ab0cd2",
90 "0000b00c-2222-1111-0123-456789ab0cd2",
91 "0000b00c-3333-1111-0123-456789ab0cd2"};
93 char uuid_list[NUM_UUID][BT_UUID_STRING_MAX] = {"0000b00b-0000-0000-f065-080080fa49b5", /* Used by BLEAPP */
94 "0000b00b-1111-1111-0123-456789ab0cd2", /* Used by BLEAPP */
95 "0000b00b-2222-1111-0123-456789ab0cd2",
96 "0000b00b-3333-1111-0123-456789ab0cd2",
97 "0000b00b-4444-1111-0123-456789ab0cd2",
98 "0000b00b-5555-1111-0123-456789ab0cd2",
99 "0000b00b-6666-1111-0123-456789ab0cd2",
100 "0000b00b-7777-1111-0123-456789ab0cd2",
101 "0000b00b-8888-1111-0123-456789ab0cd2",
102 "0000b00b-9999-1111-0123-456789ab0cd2"};
105 #ifdef TIZEN_GATT_CLIENT
106 /* Reserved GATT client Instance UUID. This is used only internally by bt-service */
107 #define DEFAULT_GATT_CLIENT_UUID "0000a00a-1111-1111-0123-456789abcdef"
109 static int gatt_default_client = -1;
112 gboolean is_registered;
113 bluetooth_device_address_t addr;
114 unsigned char svc_uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
116 unsigned char char_uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
118 } bt_gatt_notif_reg_info_t;
120 struct gatt_out_conn_info_t {
121 int client_id; /* This value unique identifies a GATT Client instance */
122 char *addr; /* Remote GATT Server address */
125 /* Linked List of outgoing gatt connection list
126 Note: This is valid only for local GATT client */
127 static GSList *outgoing_gatt_conn_list = NULL;
129 /* GATT Server Info(Local Client) List Structure */
130 struct gatt_server_info_t {
131 int connection_id; /* This value will uniquely identify a GATT client-server connection */
132 int client_id; /* This value unique identifies a GATT Client instance */
133 char *addr; /* Remote GATT Server address */
136 /* Linked List of connected Remote GATT Servers */
137 static GSList *gatt_server_info_list = NULL;
141 unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN]; /* If any service added */
142 } bt_gatt_svc_changed_info_t;
147 // bluetooth_device_address_t address; /* Remote BLE Device Address */
148 GSList *services; /* List of all services of above remote device */
149 int count; /* Number of services browsed from remote device */
150 bt_gatt_svc_changed_info_t info;
151 } bt_gatt_service_info_list_t;
154 unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
158 GSList *included_svcs;
159 gboolean is_removed; /* 0 => Service is added, 1=> removed */
160 } bt_gatt_service_info_t;
163 unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
167 unsigned char val[BLUETOOTH_GATT_ATT_DATA_LENGTH_MAX];
169 } bt_gatt_char_info_t;
172 unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
175 unsigned char val[BLUETOOTH_GATT_ATT_DATA_LENGTH_MAX];
176 } bt_gatt_descriptor_info_t;
179 unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
181 } bt_gatt_included_service_info_t;
183 GSList *list_gatt_info;
188 /* App Information structure */
191 char sender[BT_SENDER_MAX_LENGTH];
192 char uuid[BT_UUID_STRING_MAX];
195 bluetooth_advertising_data_t adv_data; /* Will store adv data for specific slot */
197 bluetooth_scan_resp_data_t scan_rsp; /* Will store scan rsp data for specific slot */
199 gboolean is_initialized;
200 GSList *service_handles;
201 #ifdef TIZEN_GATT_CLIENT
202 int client_id; /* GATT Client instance ID */
203 bluetooth_device_address_t address; /* Remote BLE Device Address */
204 gboolean is_watcher_enabled;
206 } bt_service_app_info_t;
208 /* GATT Server Request Info Structure */
209 struct gatt_server_req_info {
210 int connection_id; /* This value will uniquely identify a GATT client-server connection */
211 int request_id; /* This is an unique transaction ID assigned against each request by stack */
212 int attribute_handle; /* GATT server attribute handle */
213 int offset; /* GATT server attribute offset on which request is invoked by GATT client */
214 bluetooth_gatt_att_request_type_e request_type; /* Read or Write request */
215 char *addr; /* Remote GATT client address */
218 /* GATT Client Info List Structure */
219 struct gatt_client_info_t {
220 int connection_id; /* This value will uniquely identify a GATT client-server connection */
221 int instance_id; /* This value unique identifies a GATT server instance */
222 char *addr; /* Remote GATT client address */
225 /* GATT Indicate confirm result */
226 struct gatt_indicate_cfm_result_info_t {
227 int result; /* Result of event */
228 char *addr; /* Remote GATT client address */
229 int att_hdl; /* Characteristic Attribute handle */
230 int completed; /* 1 if last event, otheriwse 0 */
233 /* Request Search Utility method */
234 static struct gatt_server_req_info *__bt_gatt_server_find_request_info(int request_id,
235 bluetooth_gatt_att_request_type_e req_type);
237 static int __bt_gatt_send_indication_to_all_connected_clients(bluetooth_gatt_att_data_t *data,
238 bluetooth_gatt_server_indication_params_t *param);
240 static void __bt_remove_all_service_handles(int instance_id);
241 static void __bt_free_service_info(bt_gatt_service_info_t *service_info);
243 #ifdef TIZEN_GATT_CLIENT
244 static void __bt_handle_client_instance_registered(event_gattc_register_t *data);
245 static void __bt_handle_client_connected(event_gattc_conn_t *event_data);
246 static void __bt_handle_client_disconnected(event_gattc_conn_t *event_data);
247 static void __bt_handle_client_service_search_completed(event_gattc_conn_status_t *event_data);
248 static void __bt_handle_client_service_search_result(event_gattc_service_result_t *event_data);
249 static void __bt_handle_client_characteristic_search_result(
250 event_gattc_characteristic_result_t *event_data);
251 static void __bt_handle_client_descriptor_search_result(event_gattc_descriptor_result_t *event_data);
252 static void __bt_handle_client_characteristic_read_data(event_gattc_read_data *event_data);
253 static void __bt_handle_client_descriptor_read_data(event_gattc_read_data *event_data);
254 static void __bt_handle_client_characteristic_write_data(event_gattc_write_data *event_data);
255 static void __bt_handle_client_descriptor_write_data(event_gattc_write_data *event_data);
256 static void __bt_hanlde_le_device_disconnection(event_dev_conn_status_t *event_data);
257 static void __bt_handle_client_notification_registered(event_gattc_regdereg_notify_t *event_data,
258 gboolean is_registered);
259 static void __bt_handle_client_notification_data(event_gattc_notify_data *event_data);
260 static void __bt_handle_client_service_changed_ind(event_gattc_service_changed_data *event_data);
261 static void __bt_handle_client_mtu_exchange_completed(event_gattc_mtu_configured_t *event_data);
264 static int __bt_unregister_gatt_server_instance(int server_instance);
267 struct gatt_mtu_info_t {
268 char *addr; /* Remote GATT Server address */
272 static GSList *gatt_mtu_info_list = NULL;
274 static struct gatt_mtu_info_t *__bt_find_mtu_gatt_device(char *address);
275 static void __bt_remove_mtu_gatt_device(char *address);
276 static void __bt_add_mtu_gatt_device(char *address);
277 static void __bt_update_mtu_gatt_device(char *address, int mtu);
279 /* Linked List of GATT requests from Remote GATT Clients */
280 static GSList *gatt_server_requests = NULL;
282 /* Linked List of connected Remote GATT clients */
283 static GSList *gatt_client_info_list = NULL;
285 /* Number of clients to be notified to */
286 static int num_indicate_clients;
288 /* List of applications */
289 static bt_service_app_info_t numapps[MAX_APPS_SUPPORTED];
291 static void __bt_gatt_handle_pending_request_info(int result,
292 int service_function, void *data, unsigned int size);
294 static void __bt_handle_server_instance_registered(event_gatts_register_t *data);
296 static void __bt_gatt_event_handler(int event_type, gpointer event_data);
298 void _bt_check_adv_app_termination(const char *name)
300 bt_service_app_info_t *app = NULL;
302 int apps[MAX_APPS_SUPPORTED];
304 ret_if(NULL == name);
306 memset(&apps, -1, sizeof(apps));
308 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
311 /* Search for a app which has same sender and stop adv is running */
312 if (!strncasecmp(app->sender, name, strlen(name)) && app->is_initialized == TRUE) {
313 BT_DBG("Match found, name: %s", name);
315 /* Unregister all service handles with stack */
316 __bt_remove_all_service_handles(app->instance_id);
318 /* If Advertising is enabled, stop it */
319 if (app->adv_handle > 0) {
320 BT_INFO("Stop advertising on instance ID [%d]", app->instance_id);
321 /* Disable adv if running */
322 BT_INFO("Disable Advertising Adv Handle [%d] sender [%s]",
323 app->adv_handle, name);
324 _bt_set_advertising(app->sender, app->adv_handle, FALSE, FALSE);
327 /* Save instances of all apps that need to be unregistered */
328 apps[app->instance_id] = 1;
332 /* Unregister all apps one by one */
333 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
335 BT_INFO("Unregister app[%d]", k);
336 /* Unregister server instance */
337 __bt_unregister_gatt_server_instance(k);
342 static int __bt_gatt_send_indication_to_all_connected_clients(bluetooth_gatt_att_data_t *data,
343 bluetooth_gatt_server_indication_params_t *param)
346 int ret = OAL_STATUS_SUCCESS;
347 int result = OAL_STATUS_INTERNAL_ERROR;
349 BT_INFO("Current total number of connected clients [%d]", g_slist_length(gatt_client_info_list));
350 for (l = gatt_client_info_list; l != NULL; l = l->next) {
351 struct gatt_client_info_t *info = l->data;
354 BT_INFO("GATT Remote client address [%s] connection Id [%d]", info->addr, info->connection_id);
356 ret = gatts_send_indication(param->instance_id, param->atrribute_handle,
357 info->connection_id, data->length,
358 param->need_confirmation, (char *)(&data->data[0]));
360 BT_INFO("Send Indication to GATT client [%s] result: [%d]", info->addr, ret);
361 if (ret == OAL_STATUS_SUCCESS) {
362 BT_INFO("Send Indication sent successfully to GATT client [%s]", info->addr);
364 num_indicate_clients++;
368 BT_INFO("Indication sending done for total number of clients [%d]", num_indicate_clients);
372 static struct gatt_server_req_info *__bt_gatt_server_find_request_info(int request_id,
373 bluetooth_gatt_att_request_type_e req_type)
377 for (l = gatt_server_requests; l != NULL; l = l->next) {
378 struct gatt_server_req_info *req_info = l->data;
380 if (req_info && req_info->request_id == request_id && req_info->request_type == req_type) {
381 BT_INFO("GATT Server request info found Req ID [%d] handle [%d] conn ID [%d]",
382 req_info->request_id, req_info->attribute_handle, req_info->connection_id);
386 BT_ERR("Gatt Request not found");
390 void _bt_get_adv_handle_from_instance(int server_inst, int *adv_handle)
394 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
395 if (numapps[k].is_initialized == 1 && numapps[k].instance_id == server_inst) {
396 *adv_handle = numapps[k].adv_handle;
402 #ifdef TIZEN_GATT_CLIENT
403 char * _bt_gatt_get_default_gatt_client_uuid(void)
405 return g_strdup(DEFAULT_GATT_CLIENT_UUID);
409 static void __bt_register_default_gatt_client()
414 uuid_str = _bt_gatt_get_default_gatt_client_uuid();
415 _bt_string_to_uuid(uuid_str, (service_uuid_t*)&uuid);
417 BT_INFO("Register Default GATT client uuid [%s]", uuid_str);
419 if (OAL_STATUS_SUCCESS != gattc_register(&uuid)) /* for only Smart Control */
420 BT_ERR("gattc register failed");
426 int _bt_gatt_init(void)
430 if (OAL_STATUS_SUCCESS != gatt_enable()) {
431 BT_ERR("gatt Init failed");
432 return BLUETOOTH_ERROR_INTERNAL;
435 /* Register gatt event handler */
436 _bt_service_register_event_handler_callback(BT_GATT_MODULE, __bt_gatt_event_handler);
437 #ifdef TIZEN_GATT_CLIENT
438 /*In bluedroid product, defacult gatt client is handled differently*/
439 __bt_register_default_gatt_client();
442 return BLUETOOTH_ERROR_NONE;
445 static void __bt_service_reset_gatt_data()
449 BT_INFO("Rest numapp");
452 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
453 numapps[k].is_initialized = 0;
454 numapps[k].instance_id = -1;
455 numapps[k].adv_handle = 0;
456 numapps[k].adv_instance = -1;
457 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
458 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
459 memset(numapps[k].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
460 memset(numapps[k].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
461 numapps[k].adv_data_len = 0;
462 numapps[k].scan_rsp_len = 0;
466 void _bt_gatt_deinit(void)
468 BT_INFO("GATT deinit");
470 if (OAL_STATUS_SUCCESS != gatt_disable())
471 BT_ERR("gatt deinit failed");
473 /* Un-register gatt event handler */
474 _bt_service_unregister_event_handler_callback(BT_GATT_MODULE);
476 __bt_service_reset_gatt_data();
479 void _bt_update_adv_handle(const char *sender, int adv_handle)
482 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
483 bt_service_app_info_t *info = NULL;
485 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
487 /* Search for a app which has same sender and adv handle as 0 */
488 if (!g_strcmp0(info->sender, sender) && info->adv_handle == 0)
489 info->adv_handle = adv_handle;
493 int _bt_is_sender_gatt_server_with_no_adv(const char *sender, int adv_handle)
496 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
497 bt_service_app_info_t *info = NULL;
499 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
501 /* Search for a app which has same sender and adv handle as 0
502 It is possible that same sender but different adv handle */
503 if (!g_strcmp0(info->sender, sender) && info->adv_handle == 0) {
504 //info->adv_handle = adv_handle;
505 return info->instance_id;
511 int _bt_get_allocated_server_instance(const char *sender, int adv_handle, gboolean use_reserved_slot)
514 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
515 bt_service_app_info_t *info = NULL;
517 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
520 /* Exact matching of Adv handle + sender combination */
521 if (!g_strcmp0(info->sender, sender) && info->adv_handle == adv_handle)
522 return info->instance_id;
524 if (!g_strcmp0(info->sender, sender) && info->adv_handle == -1)
525 return info->instance_id;
531 char * _bt_get_sender_and_handle(int server_instance, int *adv_handle)
534 bt_service_app_info_t *info = NULL;
536 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
539 if (info->instance_id == server_instance && info->adv_handle != -1) {
540 *adv_handle = info->adv_handle;
541 BT_DBG("Server instance [%d] Adv handle [%d] Sender [%s]", server_instance, *adv_handle, info->sender);
542 return g_strdup(info->sender);
548 void _bt_set_new_adv_data(bluetooth_advertising_data_t *adv, int len, int instance)
552 bt_service_app_info_t *info = NULL;
553 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
556 if (info->instance_id == instance) {
557 memcpy(info->adv_data.data, &adv->data, len);
563 void _bt_set_new_scan_rsp_data(bluetooth_scan_resp_data_t *scan, int len, int instance)
567 bt_service_app_info_t *info = NULL;
568 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
571 if (info->instance_id == instance) {
572 memcpy(info->scan_rsp.data, &scan->data, len);
578 void _bt_get_previous_adv_data(bluetooth_advertising_data_t *adv, int *len, int instance)
581 bt_service_app_info_t *info = NULL;
583 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
586 if (info->instance_id == instance) {
587 memcpy(&adv->data, info->adv_data.data, info->adv_data_len);
588 *len = info->adv_data_len;
594 void _bt_get_previous_scan_rsp_data(bluetooth_scan_resp_data_t *scan, int *len, int instance)
598 bt_service_app_info_t *info = NULL;
600 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
603 if (info->instance_id == instance) {
604 memcpy(&scan->data, info->scan_rsp.data, info->scan_rsp_len);
605 *len = info->scan_rsp_len;
611 static int __bt_unregister_gatt_server_instance(int server_instance)
613 int ret = OAL_STATUS_SUCCESS;
616 /* Unregister the server instance */
617 ret = gatts_unregister(server_instance);
618 if (ret != OAL_STATUS_SUCCESS) {
619 BT_ERR("DeAllocate server instance with stack Fail ret: %d", ret);
620 return BLUETOOTH_ERROR_INTERNAL;
622 BT_INFO("DeAllocated server instance with stack successful..");
625 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
626 if (numapps[k].instance_id == server_instance) {
627 numapps[k].is_initialized = 0;
628 numapps[k].instance_id = -1;
629 numapps[k].adv_handle = 0;
630 numapps[k].adv_instance = -1;
631 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
632 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
633 memset(numapps[k].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
634 memset(numapps[k].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
635 numapps[k].adv_data_len = 0;
636 numapps[k].scan_rsp_len = 0;
640 BT_DBG("Going8 to reset numapp block num [%d]", k);
641 return BLUETOOTH_ERROR_NONE;
644 static void __bt_remove_all_service_handles(int instance_id)
648 int ret = OAL_STATUS_SUCCESS;
651 count = g_slist_length(numapps[instance_id].service_handles);
652 BT_INFO("Before handle removal: current count [%d]", count);
654 for (l = numapps[instance_id].service_handles; l != NULL;) {
659 BT_INFO("Server Handle to be Removed [%d] Instance ID [%d]", *handle, instance_id);
660 if (_bt_gatt_server_stop_service(numapps[instance_id].sender, *handle, instance_id) != BLUETOOTH_ERROR_NONE)
663 ret = gatts_delete_service(instance_id, *handle);
664 if (ret != OAL_STATUS_SUCCESS) {
665 BT_ERR("ret: %d", ret);
668 numapps[instance_id].service_handles = g_slist_remove(numapps[instance_id].service_handles, handle);
671 count = g_slist_length(numapps[instance_id].service_handles);
672 BT_INFO("After deleting current count [%d]", count);
678 int _bt_unregister_server_instance(const char *sender, int adv_handle)
680 BT_INFO("Unregister Allocated server instance request Sender [%s] Adv handle [%d]", sender, adv_handle);
681 int result = BLUETOOTH_ERROR_NONE;
682 int apps[MAX_APPS_SUPPORTED];
686 memset(&apps, -1, sizeof(apps));
688 if (adv_handle == 0) {
689 BT_DBG("Its a direct GATT Server app request to unregister");
690 /* Unregister server instance for each app with same sender (case: GATT Server with multiple adv handle) */
691 bt_service_app_info_t *info = NULL;
693 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
696 /* Exact matching of sender */
697 if (!g_strcmp0(info->sender, sender)) {
698 BT_INFO("Unregister GATT server instance [%d]", info->instance_id);
699 /* Unregister all service handles with stack */
700 __bt_remove_all_service_handles(info->instance_id);
702 /* Disable adv if running */
703 BT_INFO("Disable Advertising Adv Handle [%d] sender [%s]",
704 info->adv_handle, sender);
705 _bt_set_advertising(sender, info->adv_handle, FALSE, FALSE);
707 /* Save all instances which need to be unregistered */
708 apps[info->instance_id] = 1;
712 BT_DBG("Its an Internal unregister request by adv application");
713 server_instance = _bt_get_allocated_server_instance(sender, adv_handle, FALSE);
714 BT_DBG("Its an Internal unregister request by adv application: Adv disabled srv instance [%d]", server_instance);
715 if (server_instance == -1) {
716 BT_ERR("No allocated server instance to be removed");
717 return BLUETOOTH_ERROR_INVALID_PARAM;
719 if (!numapps[server_instance].service_handles) {
720 BT_INFO("There are no Service handles with this app, safe to unregister");
721 /* Unregister server instance only if this sender does not have any gatt services in it */
722 result = __bt_unregister_gatt_server_instance(server_instance);
724 numapps[server_instance].adv_handle = 0;
725 memset(numapps[server_instance].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
726 numapps[server_instance].adv_data_len = 0;
727 memset(numapps[server_instance].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
728 numapps[server_instance].scan_rsp_len = 0;
733 /* Unregister all apps one by one */
734 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
736 BT_INFO("Unregister app[%d]", k);
737 /* Unregister server instance */
738 __bt_unregister_gatt_server_instance(k);
745 int _bt_register_server_instance(const char *sender, int adv_handle)
747 int ret = OAL_STATUS_SUCCESS;
748 char *uuid_string = NULL;
753 BT_INFO("###Check on which instance Server instance can be initialized....");
754 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
755 if (numapps[k].is_initialized == 1) {
756 BT_INFO("Instance ID [%d] is already in use..Check next slot", numapps[k].instance_id);
759 BT_INFO("Time to register GATT Server..UUID to be used is [%s] slot [%d]", uuid_list[slot-1], slot);
765 BT_ERR("No Slot if free for GATT Server registration..");
766 return BLUETOOTH_ERROR_REGISTRATION_FAILED;
769 uuid_string = g_malloc0(BT_UUID_STRING_MAX);
770 _bt_string_to_uuid(uuid_list[slot-1], (service_uuid_t*)&uuid);
771 g_strlcpy(uuid_string, uuid_list[slot-1], BT_UUID_STRING_MAX);
772 BT_INFO("Copied UUID string [%s]", uuid_string);
774 /* Register GATT Server */
775 ret = gatts_register(&uuid);
776 if (ret != OAL_STATUS_SUCCESS) {
777 BT_ERR("ret: %d", ret);
779 return BLUETOOTH_ERROR_INTERNAL;
781 BT_INFO("GATT Server registration call successfully accepted by OAL..wait for Instance Initialized event from OAL..");
782 /* Return & wait for GATT Server Instance Initialization event */
783 memset(numapps[slot].sender, 0x00, sizeof(numapps[slot].sender));
784 memset(numapps[slot].uuid, 0x00, sizeof(numapps[slot].uuid));
786 g_strlcpy(numapps[slot].sender, sender, sizeof(numapps[slot].sender));
787 g_strlcpy(numapps[slot].uuid, uuid_string, sizeof(numapps[slot].uuid));
789 numapps[slot].is_initialized = 0; /* Set initialization from app registered callback */
790 numapps[slot].adv_handle = adv_handle;
793 return BLUETOOTH_ERROR_NONE;
797 static void __bt_gatt_handle_pending_request_info(int result,
798 int service_function, void *data, unsigned int size)
802 invocation_info_t *req_info = NULL;
803 ret_if(data == NULL);
805 for (l = _bt_get_invocation_list(); l != NULL; ) {
808 if (req_info == NULL || req_info->service_function != service_function)
811 switch (service_function) {
813 case BT_GATT_SERVER_REGISTER: {
814 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
815 BT_INFO("Sender [%s] Request Sender [%s]", param->sender, req_info->sender);
816 if (!g_strcmp0(req_info->sender, param->sender)) {
817 BT_DBG("GATT Server app found [%s]", req_info->sender);
819 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
820 g_array_append_vals(out_param, ¶m->instance_id, sizeof(int));
821 _bt_service_method_return(req_info->context, out_param, result);
823 g_free(req_info->user_data);
824 _bt_free_info_from_invocation_list(req_info);
825 g_array_free(out_param, TRUE);
829 case BT_GATT_SERVER_START_SERVICE:
830 case BT_GATT_SERVER_DELETE_SERVICE: {
831 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
832 BT_INFO("Sender [%s] Request Sender [%s]", param->sender, req_info->sender);
834 int *saved_instance_id = (int*)req_info->user_data;
835 if (!g_strcmp0(req_info->sender, param->sender) && param->instance_id == *saved_instance_id) {
836 BT_DBG("GATT Server app found [%s] Instance ID [%d] Reply DBUS",
837 req_info->sender, *saved_instance_id);
839 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
840 g_array_append_vals(out_param, &saved_instance_id, sizeof(int));
841 _bt_service_method_return(req_info->context, out_param, result);
843 g_free(req_info->user_data);
844 _bt_free_info_from_invocation_list(req_info);
845 g_array_free(out_param, TRUE);
849 case BT_GATT_SERVER_ADD_SERVICE:
850 case BT_GATT_SERVER_ADD_DESCRIPTOR:
851 case BT_GATT_SERVER_ADD_CHARACTERISTIC: {
852 int *handle = (int*)data;
853 BT_INFO("Characteristic added: Handle [%d]", *handle);
854 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
856 g_array_append_vals(out_param, handle, sizeof(int));
857 _bt_service_method_return(req_info->context, out_param, result);
859 g_free(req_info->user_data);
860 _bt_free_info_from_invocation_list(req_info);
861 g_array_free(out_param, TRUE);
866 case BT_DISCONNECT_LE: {
867 #ifdef TIZEN_GATT_CLIENT
868 char *addr = (char*)req_info->user_data;
869 bluetooth_device_address_t address;
871 if (!g_strcmp0(addr, (char*)data)) {
872 BT_INFO("GATT Client connect-disconnect call pending for app [%s] addr [%s]",
873 req_info->sender, addr);
874 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
875 _bt_convert_addr_string_to_type(address.addr, addr);
877 g_array_append_vals(out_param, (bluetooth_device_address_t*)&address,
878 sizeof(bluetooth_device_address_t));
879 _bt_service_method_return(req_info->context, out_param, result);
881 g_free(req_info->user_data);
882 _bt_free_info_from_invocation_list(req_info);
883 g_array_free(out_param, TRUE);
886 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
888 g_array_append_vals(out_param, (bluetooth_device_address_t*)data,
889 sizeof(bluetooth_device_address_t));
890 _bt_service_method_return(req_info->context, out_param, result);
892 g_free(req_info->user_data);
893 _bt_free_info_from_invocation_list(req_info);
895 g_array_free(out_param, TRUE);
899 #ifdef TIZEN_GATT_CLIENT
900 case BT_GATT_CLIENT_REGISTER: {
901 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
902 BT_INFO("Sender [%s] Request Sender [%s]", param->sender, req_info->sender);
903 if (!g_strcmp0(req_info->sender, param->sender)) {
904 BT_DBG("GATT Client app found [%s] created client ID [%d]",
905 req_info->sender, param->client_id);
907 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
908 g_array_append_vals(out_param, ¶m->client_id, sizeof(int));
909 _bt_service_method_return(req_info->context, out_param, result);
911 g_free(req_info->user_data);
912 _bt_free_info_from_invocation_list(req_info);
913 g_array_free(out_param, TRUE);
917 case BT_GATT_GET_PRIMARY_SERVICES: {
918 bt_services_browse_info_t *param = (bt_services_browse_info_t*)data;
919 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
920 _bt_convert_addr_type_to_string(address,
921 (unsigned char *)(¶m->device_addr.addr));
923 BT_INFO("Request Sender [%s] address [%s]", req_info->sender, address);
925 /* Match address to determine same request */
926 if (!g_strcmp0((char*)req_info->user_data, address)) {
927 BT_DBG("GATT Client app found [%s] Remote address [%s]",
928 req_info->sender, address);
930 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
931 g_array_append_vals(out_param, param,
932 sizeof(bt_services_browse_info_t));
934 //g_array_append_vals(out_param, ¶m->client_id, sizeof(int));
935 _bt_service_method_return(req_info->context, out_param, result);
937 g_free(req_info->user_data);
938 _bt_free_info_from_invocation_list(req_info);
939 g_array_free(out_param, TRUE);
944 case BT_GATT_GET_SERVICE_PROPERTIES: {
945 bt_char_browse_info_t param;
946 memcpy((void*)¶m, data, sizeof(bt_char_browse_info_t));
947 //bt_char_browse_info_t *param = (bt_char_browse_info_t*)data;
948 BT_INFO("Request Sender [%s]", req_info->sender);
949 bluetooth_gatt_client_svc_prop_info_t *prop = (bluetooth_gatt_client_svc_prop_info_t*)req_info->user_data;
951 /* Match both address and service properties to determine same request */
952 if (!memcmp(param.device_addr.addr,
953 prop->device_address.addr,
954 sizeof(bluetooth_device_address_t)) &&
955 !memcmp(param.svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
956 param.svc_inst_id == prop->svc.instance_id) {
957 BT_INFO("Returning Service properties");
959 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
960 g_array_append_vals(out_param, ¶m, sizeof(bt_char_browse_info_t));
961 _bt_service_method_return(req_info->context, out_param, result);
963 g_free(req_info->user_data);
964 _bt_free_info_from_invocation_list(req_info);
965 g_array_free(out_param, TRUE);
969 case BT_GATT_GET_CHARACTERISTIC_PROPERTIES: {
970 bt_descriptor_browse_info_t *param = (bt_descriptor_browse_info_t*)data;
972 BT_INFO("Request Sender [%s]", req_info->sender);
973 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
975 /* Match both address, service properties &char properties to determine same request */
976 if (!memcmp(param->device_addr.addr,
977 prop->device_address.addr,
978 sizeof(bluetooth_device_address_t)) &&
979 !memcmp(param->svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
980 param->svc_inst_id == prop->svc.instance_id &&
981 !memcmp(param->char_uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
982 param->char_inst_id == prop->characteristic.instance_id) {
983 BT_INFO("Returning Characteristic properties");
984 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
985 g_array_append_vals(out_param, param, sizeof(bt_descriptor_browse_info_t));
986 _bt_service_method_return(req_info->context, out_param, result);
988 g_free(req_info->user_data);
989 _bt_free_info_from_invocation_list(req_info);
990 g_array_free(out_param, TRUE);
994 case BT_GATT_WATCH_CHARACTERISTIC: {
995 bt_gatt_notif_reg_info_t *param = (bt_gatt_notif_reg_info_t*)data;
996 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
997 BT_INFO("Request Sender [%s]", req_info->sender);
998 /* Match both address, service properties &char properties to determine same request */
999 if (!memcmp(param->addr.addr,
1000 prop->device_address.addr,
1001 sizeof(bluetooth_device_address_t)) &&
1002 !memcmp(param->svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1003 param->svc_inst == prop->svc.instance_id &&
1004 !memcmp(param->char_uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1005 param->char_inst == prop->characteristic.instance_id) {
1006 BT_INFO("Characteristic Watch Successful: Is registered [%d]",
1007 param->is_registered);
1008 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1009 g_array_append_vals(out_param, param, sizeof(bt_gatt_notif_reg_info_t));
1010 _bt_service_method_return(req_info->context, out_param, result);
1011 g_free(req_info->user_data);
1012 _bt_free_info_from_invocation_list(req_info);
1013 g_array_free(out_param, TRUE);
1017 case BT_GATT_READ_CHARACTERISTIC:
1018 case BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE: {
1019 bluetooth_gatt_client_char_prop_info_t *param = (bluetooth_gatt_client_char_prop_info_t*)data;
1020 BT_INFO("Request Sender [%s]", req_info->sender);
1022 char *addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
1023 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
1024 _bt_convert_addr_type_to_string(addr, prop->device_address.addr);
1026 if (!memcmp(param, prop, sizeof(bluetooth_gatt_client_char_prop_info_t))) {
1027 BT_INFO("Gatt Char read or write request matched for address [%s]", addr);
1028 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1029 g_array_append_vals(out_param, param, sizeof(bluetooth_gatt_client_char_prop_info_t));
1030 _bt_service_method_return(req_info->context, out_param, result);
1032 g_free(req_info->user_data);
1033 _bt_free_info_from_invocation_list(req_info);
1034 g_array_free(out_param, TRUE);
1039 case BT_GATT_READ_DESCRIPTOR_VALUE:
1040 case BT_GATT_WRITE_DESCRIPTOR_VALUE: {
1041 bluetooth_gatt_client_desc_prop_info_t *param = (bluetooth_gatt_client_desc_prop_info_t*)data;
1042 BT_INFO("Request Sender [%s]", req_info->sender);
1044 char *addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
1045 bluetooth_gatt_client_desc_prop_info_t *prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
1046 _bt_convert_addr_type_to_string(addr, prop->device_address.addr);
1048 if (!memcmp(param, prop, sizeof(bluetooth_gatt_client_desc_prop_info_t))) {
1049 BT_INFO("Descriptor read or write request matched for address [%s]", addr);
1050 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1051 g_array_append_vals(out_param, param, sizeof(bluetooth_gatt_client_desc_prop_info_t));
1052 _bt_service_method_return(req_info->context, out_param, result);
1054 g_free(req_info->user_data);
1055 _bt_free_info_from_invocation_list(req_info);
1056 g_array_free(out_param, TRUE);
1061 case BT_REQ_ATT_MTU: {
1062 char *addr = (char*)req_info->user_data;
1063 bluetooth_device_address_t address;
1065 if (!g_strcmp0(addr, (char*)data)) {
1066 BT_INFO("GATT Client BT_REQ_ATT_MTU call pending for app [%s] addr [%s]",
1067 req_info->sender, addr);
1068 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1069 _bt_convert_addr_string_to_type(address.addr, addr);
1071 g_array_append_vals(out_param, (bluetooth_device_address_t*)&address,
1072 sizeof(bluetooth_device_address_t));
1073 _bt_service_method_return(req_info->context, out_param, result);
1075 g_free(req_info->user_data);
1076 _bt_free_info_from_invocation_list(req_info);
1077 g_array_free(out_param, TRUE);
1088 static void __bt_handle_server_instance_registered(event_gatts_register_t *data)
1090 bt_service_app_info_t *info = NULL;
1092 char *uuid_string = g_malloc0(BT_UUID_STRING_MAX);
1094 _bt_uuid_to_string(&(data->server_uuid), uuid_string);
1095 BT_INFO("Instance ID is Intialized [%d] UUID initialized [%s]", data->server_inst, uuid_string);
1097 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1098 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1101 if (g_strcmp0(info->uuid, uuid_string) == 0) {
1102 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1103 info->is_initialized = TRUE;
1104 info->instance_id = data->server_inst;
1105 info->adv_instance = data->server_inst;
1106 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE, BT_GATT_SERVER_REGISTER,
1107 (void*)info, sizeof(bt_service_app_info_t));
1111 g_free(uuid_string);
1114 static void __bt_handle_gatt_server_service_added(event_gatts_srvc_prm_t *event)
1116 int result = BLUETOOTH_ERROR_NONE;
1120 bt_service_app_info_t *info = NULL;
1123 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1124 _bt_uuid_to_string(&(event->gatt_srvc_id.id.uuid), uuid_str);
1125 BT_INFO("GATT Added Service UUID: [%s] Server Ins [%d] Service hdl [%d] Is Primary [%d]\n",
1126 uuid_str, event->gatt_srvc_stat.server_inst, svc_handle, event->gatt_srvc_id.is_prmry);
1128 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1129 result = BLUETOOTH_ERROR_INTERNAL;
1130 svc_handle = 0; /* Service handle set to 0 indicates.
1131 0 is reserved by BT SIG, cant be used by app*/
1133 svc_handle = event->gatt_srvc_stat.servic_hndl;
1135 BT_INFO("GATT Added Service Status [%d] Server Ins [%d] Service hdl [%d] Is Primary [%d]\n",
1136 event->gatt_srvc_stat.status, event->gatt_srvc_stat.server_inst, svc_handle, event->gatt_srvc_id.is_prmry);
1138 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1139 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1142 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1143 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1144 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_SERVICE,
1145 (int*)&svc_handle, sizeof(int));
1147 /* Add Service Handle */
1148 if (svc_handle > 0) {
1149 handle = g_malloc0(sizeof(int));
1150 *handle = svc_handle;
1151 numapps[event->gatt_srvc_stat.server_inst].service_handles = g_slist_append(numapps[event->gatt_srvc_stat.server_inst].service_handles, handle);
1152 count = g_slist_length(numapps[event->gatt_srvc_stat.server_inst].service_handles);
1153 BT_INFO("Added Service handle [%d] to list..current count [%d]", svc_handle, count);
1162 static void __bt_handle_gatt_server_characteristic_added(event_gatts_srvc_charctr_t *event)
1164 int result = BLUETOOTH_ERROR_NONE;
1166 bt_service_app_info_t *info = NULL;
1168 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1170 BT_INFO("GATT Server Char added status [%d]", event->gatt_srvc_stat.status);
1171 BT_INFO("GATT Server_inst: [%d]", event->gatt_srvc_stat.server_inst);
1172 BT_INFO("GATT Service Handle: [%d]", event->gatt_srvc_stat.servic_hndl);
1173 BT_INFO("GATT Add characteristic Status: [%d]", event->gatt_srvc_stat.status);
1174 BT_INFO("GATT Service characteristic Handle: [%d]", event->charctr_hndl);
1176 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1177 result = BLUETOOTH_ERROR_INTERNAL;
1178 char_handle = 0; /* characteristic handle set to 0 indicates.
1179 0 is reserved by BT SIG, cant be used by app*/
1181 char_handle = event->charctr_hndl;
1184 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1185 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1188 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1189 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1190 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_CHARACTERISTIC,
1191 (int*)&char_handle, sizeof(int));
1197 _bt_uuid_to_string(&(event->charctr_uuid), uuid_str);
1198 BT_INFO("GATT Added Characteristic: UUID: [%s]", uuid_str);
1203 static void __bt_handle_gatt_server_descriptor_added(event_gatts_srvc_descr_t* event)
1205 int result = BLUETOOTH_ERROR_NONE;
1207 bt_service_app_info_t *info = NULL;
1209 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1211 BT_INFO("GATT Server Descriptor added status [%d]", event->gatt_srvc_stat.status);
1212 BT_INFO("GATT Server_inst: [%d]", event->gatt_srvc_stat.server_inst);
1213 BT_INFO("GATT Service Handle: [%d]", event->gatt_srvc_stat.servic_hndl);
1214 BT_INFO("GATT Add Descriptor Status: [%d]", event->gatt_srvc_stat.status);
1215 BT_INFO("GATT Service Descriptor Handle: [%d]", event->descrptr_hndl);
1217 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1218 result = BLUETOOTH_ERROR_INTERNAL;
1219 desc_handle = 0; /* Service handle set to 0 indicates.
1220 0 is reserved by BT SIG, cant be used by app*/
1222 desc_handle = event->descrptr_hndl;
1224 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1225 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1228 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1229 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1230 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_DESCRIPTOR,
1231 (int*)&desc_handle, sizeof(int));
1236 _bt_uuid_to_string(&(event->descrptr_uuid), uuid_str);
1237 BT_INFO("GATT Added Descriptor: UUID: [%s]", uuid_str);
1242 static void __bt_handle_gatt_server_service_started(event_gatts_srvc_t *event)
1244 bt_service_app_info_t *info = NULL;
1245 int result = BLUETOOTH_ERROR_NONE;
1247 BT_INFO("GATT Server Service Started..");
1250 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1251 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1252 BT_INFO("GATT Service Started Status: [%d]", event->status);
1254 if (event->status != OAL_STATUS_SUCCESS) {
1255 BT_ERR("GATT Server Service Start Failed Err: [%d]", event->status);
1256 result = BLUETOOTH_ERROR_INTERNAL;
1259 /* Check if the just registered Instance ID belongs to requester */
1260 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1263 if (info->instance_id == event->server_inst) {
1264 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1265 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_START_SERVICE,
1266 (void*)info, sizeof(bt_service_app_info_t));
1272 static void __bt_handle_gatt_server_service_stopped(event_gatts_srvc_t *event)
1274 int result = BLUETOOTH_ERROR_NONE;
1275 bt_service_app_info_t *info = NULL;
1277 BT_INFO("GATT Server Service Stopped..");
1279 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1280 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1281 BT_INFO("GATT Service Stopped Status: [%d]", event->status);
1283 if (event->status != OAL_STATUS_SUCCESS) {
1284 BT_ERR("GATT Server Service Stop Failed Err: [%d]", event->status);
1285 result = BLUETOOTH_ERROR_INTERNAL;
1288 /* Check if the just registered Instance ID belongs to requester */
1289 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1292 if (info->instance_id == event->server_inst) {
1293 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1294 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_STOP_SERVICE,
1295 (void*)info, sizeof(bt_service_app_info_t));
1301 static void __bt_handle_gatt_server_service_deleted(event_gatts_srvc_t *event)
1303 int result = BLUETOOTH_ERROR_NONE;
1304 bt_service_app_info_t *info = NULL;
1306 BT_INFO("GATT Server Service Deleted..");
1308 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1309 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1310 BT_INFO("GATT Service Deleted Status: [%d]", event->status);
1312 if (event->status != OAL_STATUS_SUCCESS) {
1313 BT_ERR("GATT Server Service Delete Failed Err: [%d]", event->status);
1314 result = BLUETOOTH_ERROR_INTERNAL;
1317 /* Check if the just registered Instance ID belongs to requester */
1318 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1321 if (info->instance_id == event->server_inst) {
1322 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1323 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_DELETE_SERVICE,
1324 (void*)info, sizeof(bt_service_app_info_t));
1330 struct gatt_client_info_t *_bt_find_remote_gatt_client_info(char *address)
1333 struct gatt_client_info_t *info = NULL;
1334 for (l = gatt_client_info_list; l != NULL; l = g_slist_next(l)) {
1335 info = (struct gatt_client_info_t*)l->data;
1339 if (!g_strcmp0(info->addr, address)) {
1340 BT_DBG("Remote GATT client found addr[%s]", info->addr);
1347 static struct gatt_client_info_t *__bt_find_remote_gatt_client_info_from_conn_id(int conn_id)
1350 struct gatt_client_info_t *info = NULL;
1352 for (l = gatt_client_info_list; l != NULL; l = g_slist_next(l)) {
1353 info = (struct gatt_client_info_t*)l->data;
1357 if (info->connection_id == conn_id) {
1358 BT_INFO("Remote GATT client found addr[%s]", info->addr);
1365 #ifdef TIZEN_GATT_CLIENT
1366 struct gatt_server_info_t *_bt_find_remote_gatt_server_info(char *address)
1369 struct gatt_server_info_t *info = NULL;
1370 for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
1371 info = (struct gatt_server_info_t*)l->data;
1375 if (!g_strcmp0(info->addr, address)) {
1376 BT_DBG("Remote GATT Server found addr[%s]", info->addr);
1383 static struct gatt_out_conn_info_t* __bt_find_gatt_outgoing_conn_info(char *address)
1386 struct gatt_out_conn_info_t *info = NULL;
1387 for (l = outgoing_gatt_conn_list; l != NULL; l = g_slist_next(l)) {
1388 info = (struct gatt_out_conn_info_t*)l->data;
1392 if (!g_strcmp0(info->addr, address)) {
1393 BT_INFO("Outgoing connection info found addr[%s]", info->addr);
1400 static struct gatt_server_info_t *__bt_find_remote_gatt_server_info_from_client_if(int client_if)
1403 struct gatt_server_info_t *info = NULL;
1405 for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
1406 info = (struct gatt_server_info_t*)l->data;
1410 if (info->client_id == client_if) {
1411 BT_INFO("Remote GATT server found addr[%s]", info->addr);
1422 #ifdef TIZEN_GATT_CLIENT
1423 static void __bt_handle_gatt_server_connection_state(event_gatts_conn_t *event)
1425 int result = BLUETOOTH_ERROR_NONE;
1426 struct gatt_client_info_t *client_info = NULL;
1427 struct gatt_server_info_t *server_info = NULL;
1428 bluetooth_device_address_t dev_addr;
1429 GVariant *param = NULL;
1432 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1434 memcpy(dev_addr.addr, event->address.addr, 6);
1436 /* REPLY dbus Context to application which called BT_CONNECT_LE. There is status
1438 _bt_convert_addr_type_to_string(address,
1439 (unsigned char *)dev_addr.addr);
1441 if (event->status != OAL_STATUS_SUCCESS)
1442 result = BLUETOOTH_ERROR_INTERNAL;
1444 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1445 address, BT_ADDRESS_STRING_SIZE);
1447 BT_INFO("GATT Server Connedted: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1448 address, event->server_inst, event->conn_id);
1451 /* Check if device is already in connected list */
1452 client_info = _bt_find_remote_gatt_client_info(address);
1455 BT_INFO("Conn Info absent: But no need to Send Local GATT Server Connected event to apps");
1457 param = g_variant_new("(is)", result, address);
1459 /* Send event to application */
1460 _bt_send_event(BT_DEVICE_EVENT,
1461 BLUETOOTH_EVENT_GATT_SERVER_CONNECTED, /* Local device is GATT server */
1464 /* Save client connection info */
1465 client_info = g_new0(struct gatt_client_info_t, 1);
1466 client_info->addr = g_strdup(address);
1467 BT_INFO("Added GATT client addr[%s]", client_info->addr);
1468 client_info->connection_id = event->conn_id;
1469 client_info->instance_id = event->server_inst;
1470 gatt_client_info_list = g_slist_append(gatt_client_info_list, client_info);
1471 BT_INFO("Total num of connected Remote GATT Clients [%d]", g_slist_length(gatt_client_info_list));
1473 /* Save server connection info */
1474 server_info = g_new0(struct gatt_server_info_t, 1);
1475 server_info->addr = g_strdup(address);
1476 server_info->client_id = -1;
1477 BT_INFO("Added GATT server addr[%s]", server_info->addr);
1478 server_info->connection_id = event->conn_id;
1479 gatt_server_info_list = g_slist_append(gatt_server_info_list, server_info);
1480 BT_INFO("Total num of connected Remote GATT Servers [%d]", g_slist_length(gatt_server_info_list));
1482 ret = gattc_add_connection_info((bt_address_t *)&dev_addr, event->conn_id, event->server_inst);
1483 if (ret != OAL_STATUS_SUCCESS) {
1484 BT_ERR("gattc register server instance failed");
1488 __bt_add_mtu_gatt_device(address);
1493 /* GATT Server Dis connected */
1494 static void __bt_handle_gatt_server_disconnection_state(event_gatts_conn_t *event)
1496 int result = BLUETOOTH_ERROR_NONE;
1497 struct gatt_client_info_t *client_info = NULL;
1498 struct gatt_server_info_t *server_info = NULL;
1499 bluetooth_device_address_t dev_addr;
1500 GVariant *param = NULL;
1501 char address[BT_ADDRESS_STRING_SIZE];
1503 memcpy(dev_addr.addr, event->address.addr, 6);
1505 /* REPLY dbus Context to application which called BT_DISCONNECT_LE. There is status
1507 _bt_convert_addr_type_to_string(address,
1508 (unsigned char *)dev_addr.addr);
1510 if (event->status != OAL_STATUS_SUCCESS)
1511 result = BLUETOOTH_ERROR_INTERNAL;
1513 if (NULL == _bt_get_request_info_data(BT_DISCONNECT_LE, address)) {
1514 if (NULL != _bt_get_request_info_data(BT_CONNECT_LE, address)) {
1515 result = BLUETOOTH_ERROR_INTERNAL;
1517 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1518 address, BT_ADDRESS_STRING_SIZE);
1520 BT_ERR("Failed to connect Local GATT Server Remote Client addr[%s]", address);
1524 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE,
1525 address, BT_ADDRESS_STRING_SIZE);
1528 BT_INFO("Local GATT Server DisConnected: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1529 address, event->server_inst, event->conn_id);
1530 /* Remove Connection info */
1531 client_info = _bt_find_remote_gatt_client_info(address);
1533 BT_INFO("No need to Send Local GATT Server Disconnected event to apps, just remove remote client info");
1535 param = g_variant_new("(is)", result, address);
1536 /* Send event to application */
1537 _bt_send_event(BT_DEVICE_EVENT,
1538 BLUETOOTH_EVENT_GATT_SERVER_DISCONNECTED, /* Local device is GATT server */
1541 /* Remove server info from list */
1542 server_info = _bt_find_remote_gatt_server_info(address);
1544 gatt_server_info_list = g_slist_remove(gatt_server_info_list, server_info);
1546 BT_INFO("Can not find conn info, already removed!");
1548 /* Remove client info from List */
1549 gatt_client_info_list = g_slist_remove(gatt_client_info_list, client_info);
1550 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1551 g_free(client_info->addr);
1552 g_free(client_info);
1555 __bt_remove_mtu_gatt_device(address);
1559 static void __bt_handle_gatt_server_connection_state(event_gatts_conn_t *event)
1561 int result = BLUETOOTH_ERROR_NONE;
1562 struct gatt_client_info_t *conn_info = NULL;
1563 bt_service_app_info_t *info = NULL;
1564 bluetooth_device_address_t dev_addr;
1565 GVariant *param = NULL;
1568 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1570 memcpy(dev_addr.addr, event->address.addr, 6);
1573 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1574 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1577 if (info->instance_id == event->server_inst) {
1578 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1579 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1580 (bluetooth_device_address_t*)&dev_addr, sizeof(bluetooth_device_address_t));
1586 /* Send event to BT-API */
1587 _bt_convert_addr_type_to_string(address,
1588 (unsigned char *)dev_addr.addr);
1590 BT_INFO("GATT Server Connedted: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1591 address, event->server_inst, event->conn_id);
1594 /* Check if device is already in connected list */
1595 conn_info = _bt_find_remote_gatt_client_info(address);
1598 param = g_variant_new("(is)", result, address);
1599 /* Send event to application */
1600 _bt_send_event(BT_GATT_SERVER_EVENT,
1601 BLUETOOTH_EVENT_GATT_CONNECTED,
1604 /* Save Connection info */
1605 conn_info = g_new0(struct gatt_client_info_t, 1);
1606 conn_info->addr = g_strdup(address);
1607 BT_INFO("Added GATT client addr[%s]", conn_info->addr);
1608 conn_info->connection_id = event->conn_id;
1609 conn_info->instance_id = event->server_inst;
1610 gatt_client_info_list = g_slist_append(gatt_client_info_list, conn_info);
1611 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1617 /* GATT Server Dis connected */
1618 static void __bt_handle_gatt_server_disconnection_state(event_gatts_conn_t *event)
1620 int result = BLUETOOTH_ERROR_NONE;
1621 struct gatt_client_info_t *conn_info = NULL;
1622 bluetooth_device_address_t dev_addr;
1623 bt_service_app_info_t *info = NULL;
1624 GVariant *param = NULL;
1627 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1629 memcpy(dev_addr.addr, event->address.addr, 6);
1631 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1632 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1635 if (info->instance_id == event->server_inst) {
1636 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1637 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE,
1638 (bluetooth_device_address_t*)&dev_addr, sizeof(bluetooth_device_address_t));
1644 /* Send event to BT-API */
1645 _bt_convert_addr_type_to_string(address,
1646 (unsigned char *)dev_addr.addr);
1648 BT_INFO("GATT Server DisConnected: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1649 address, event->server_inst, event->conn_id);
1651 param = g_variant_new("(is)", result, address);
1652 /* Send event to application */
1653 _bt_send_event(BT_GATT_SERVER_EVENT,
1654 BLUETOOTH_EVENT_GATT_DISCONNECTED,
1657 /* Remove Connection info */
1658 conn_info = _bt_find_remote_gatt_client_info(address);
1660 BT_INFO("Remove GATT client info from List..");
1661 /* Remove info from List */
1662 gatt_client_info_list = g_slist_remove(gatt_client_info_list, conn_info);
1663 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1664 g_free(conn_info->addr);
1673 static void __bt_handle_gatt_server_acquire_write_requested(event_gatts_srvc_acquire_attr_t *event)
1675 GVariant *param = NULL;
1676 int result = BLUETOOTH_ERROR_NONE;
1677 struct gatt_server_req_info *req_info = NULL;
1678 bluetooth_device_address_t dev_addr;
1679 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1681 BT_INFO("GATT Server ACQUIRE Write Req Connection ID: [%d]", event->attr_trans.conn_id);
1682 BT_INFO("GATT Server ACQUIRE Write Req Transaction ID:[%d]", event->attr_trans.trans_id);
1683 BT_INFO("GATT Server ACQUIRE Write Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
1685 //address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1686 memcpy(dev_addr.addr, event->address.addr, 6);
1688 req_info = g_new0(struct gatt_server_req_info, 1);
1689 req_info->request_id = event->attr_trans.trans_id;
1690 req_info->attribute_handle = event->attr_trans.attr_handle;
1691 req_info->connection_id = event->attr_trans.conn_id;
1692 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_ACQUIRE_WRITE;
1693 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1695 _bt_convert_addr_type_to_string(address,
1696 (unsigned char *)dev_addr.addr);
1698 param = g_variant_new("(iiiiiis)", result,
1699 event->attr_trans.conn_id,
1700 event->attr_trans.trans_id,
1701 event->attr_trans.attr_handle,
1702 event->mtu, event->attr_trans.offset, address);
1703 BT_INFO("GATT Server ACQUIRE Write Req Attribute remote address : [%s]", address);
1705 _bt_send_event(BT_GATT_SERVER_EVENT,
1706 BLUETOOTH_EVENT_GATT_SERVER_ACQUIRE_WRITE,
1711 static void __bt_handle_gatt_server_acquire_notify_requested(event_gatts_srvc_acquire_attr_t *event)
1713 GVariant *param = NULL;
1714 int result = BLUETOOTH_ERROR_NONE;
1715 struct gatt_server_req_info *req_info = NULL;
1716 bluetooth_device_address_t dev_addr;
1717 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1719 BT_INFO("GATT Server ACQUIRE Notify Req Connection ID: [%d]", event->attr_trans.conn_id);
1720 BT_INFO("GATT Server ACQUIRE Notify Req Transaction ID:[%d]", event->attr_trans.trans_id);
1721 BT_INFO("GATT Server ACQUIRE Notify Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
1722 BT_INFO("GATT Server ACQUIRE notify Req address : [%s]", address);
1724 memcpy(dev_addr.addr, event->address.addr, 6);
1725 _bt_convert_addr_type_to_string(address,
1726 (unsigned char *)dev_addr.addr);
1727 BT_INFO("GATT Server ACQUIRE notify Req remote address : [%s]", address);
1729 req_info = g_new0(struct gatt_server_req_info, 1);
1730 req_info->request_id = event->attr_trans.trans_id;
1731 req_info->attribute_handle = event->attr_trans.attr_handle;
1732 req_info->connection_id = event->attr_trans.conn_id;
1733 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_ACQUIRE_NOTIFY;
1734 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1736 param = g_variant_new("(iiiiiis)", result,
1737 event->attr_trans.conn_id,
1738 event->attr_trans.trans_id,
1739 event->attr_trans.attr_handle,
1740 event->mtu, event->attr_trans.offset,
1743 BT_INFO("GATT Server ACQUIRE Notify Req Attribute : ");
1745 _bt_send_event(BT_GATT_SERVER_EVENT,
1746 BLUETOOTH_EVENT_GATT_SERVER_ACQUIRE_NOTIFY,
1750 static void __bt_handle_gatt_server_write_requested(event_gatts_srvc_write_attr_t *event)
1753 bluetooth_device_address_t dev_addr;
1754 GVariant *param = NULL;
1755 int result = BLUETOOTH_ERROR_NONE;
1757 gboolean is_prepare_write;
1759 GVariant *data = NULL;
1761 struct gatt_server_req_info *req_info = NULL;
1762 BT_INFO("GATT Server Write Requested");
1764 memcpy(dev_addr.addr, event->address.addr, 6);
1766 BT_INFO("GATT Server Write Req Connection ID: [%d]", event->attr_trans.conn_id);
1767 BT_INFO("GATT Server Write Req Transaction ID:[%d]", event->attr_trans.trans_id);
1768 BT_INFO("GATT Server Write Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
1769 BT_INFO("GATT Server Write Req Attribute Offset: [%d]", event->attr_trans.offset);
1770 BT_INFO("GATT Server Write Req value length [%d]", event->length);
1771 BT_INFO("GATT Server Write Req needs response: [%d]", event->need_rsp);
1772 BT_INFO("GATT Server Write Req Is Prep: [%d]", event->is_prep);
1774 if (event->length <= 0) {
1775 BT_INFO("GATT Server write requested, but length of attr value is 0");
1779 need_resp = event->need_rsp;
1780 is_prepare_write = event->is_prep;
1782 write_val = g_memdup(&event->value[0], event->length);
1784 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1785 _bt_convert_addr_type_to_string(address,
1786 (unsigned char *)dev_addr.addr);
1788 BT_INFO("GATT Server Write Request from remote client [%s]", address);
1790 if (event->length > 0) {
1791 for (i = 0; i < event->length; i++)
1792 BT_DBG("Data[%d] = [0x%x]", i, event->value[i]);
1794 /* Save Write Request Info */
1795 req_info = g_new0(struct gatt_server_req_info, 1);
1796 req_info->request_id = event->attr_trans.trans_id;
1797 req_info->attribute_handle = event->attr_trans.attr_handle;
1798 req_info->connection_id = event->attr_trans.conn_id;
1799 req_info->addr = address;
1800 req_info->offset = event->attr_trans.offset;
1801 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_WRITE;
1802 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1804 data = g_variant_new_from_data(
1805 G_VARIANT_TYPE_BYTESTRING,
1810 param = g_variant_new("(iiiiiibbsn@ay)", result,
1811 event->attr_trans.conn_id,
1812 event->attr_trans.trans_id,
1813 event->attr_trans.attr_handle,
1814 event->attr_trans.offset,
1822 _bt_send_event(BT_GATT_SERVER_EVENT,
1823 BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED,
1829 static void __bt_handle_gatt_server_read_requested(event_gatts_srvc_read_attr_t *event)
1831 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1832 bluetooth_device_address_t dev_addr;
1833 int result = BLUETOOTH_ERROR_NONE;
1834 struct gatt_server_req_info *req_info = NULL;
1835 GVariant *param = NULL;
1837 BT_INFO("GATT Server Read Requested");
1839 memcpy(dev_addr.addr, event->address.addr, 6);
1841 BT_INFO("GATT Server-Client Connection ID: [%d]", event->attr_trans.conn_id);
1842 BT_INFO("GATT Server-Client Transaction ID: [%d]", event->attr_trans.trans_id);
1843 BT_INFO("GATT Server Attribute Handle: [%d]", event->attr_trans.attr_handle);
1844 BT_INFO("GATT Server Attribute Offset for read: [%d]", event->attr_trans.offset);
1845 BT_INFO("GATT Server Attribute is long: [%d]", event->is_long);
1847 is_long = event->is_long;
1849 /* Save Read Request Info */
1850 req_info = g_new0(struct gatt_server_req_info, 1);
1851 req_info->request_id = event->attr_trans.trans_id;
1852 req_info->attribute_handle = event->attr_trans.attr_handle;
1853 req_info->connection_id = event->attr_trans.conn_id;
1854 req_info->addr = address;
1855 req_info->offset = event->attr_trans.offset;
1856 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_READ;
1857 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1859 /* Send event to BT-API */
1860 _bt_convert_addr_type_to_string(address,
1861 (unsigned char *)dev_addr.addr);
1863 BT_INFO("GATT Server Read Request from remote client [%s]", address);
1865 param = g_variant_new("(iiiiibs)", result,
1866 event->attr_trans.conn_id,
1867 event->attr_trans.trans_id,
1868 event->attr_trans.attr_handle,
1869 event->attr_trans.offset,
1873 _bt_send_event(BT_GATT_SERVER_EVENT,
1874 BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED,
1878 static void __bt_handle_gatt_server_indicate_confirmed(event_gatts_ind_cnfrm_t *event)
1880 bluetooth_device_address_t dev_addr;
1881 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1882 int cur_connected_clients;
1883 static int recvd = 0;
1884 gboolean completed = 0;
1885 GVariant *param = NULL;
1887 /* OAL event does provide error, so MW assumes event will never contain wrong data,
1888 incase of any issues, check with OAL */
1889 int result = BLUETOOTH_ERROR_NONE;
1891 memcpy(dev_addr.addr, event->address.addr, 6);
1892 _bt_convert_addr_type_to_string(address,
1893 (unsigned char *)dev_addr.addr);
1895 BT_INFO("Indication sent to GATT client [%s] conn_ ID [%d] transaction ID [%d] Att handle [%d]",
1896 address, event->conn_id, event->trans_id, event->attr_handle);
1899 cur_connected_clients = g_slist_length(gatt_client_info_list);
1900 BT_INFO("Number of connected clients during sending Indication [%d] & current connected count [%d]",
1901 num_indicate_clients, cur_connected_clients);
1904 if (recvd == num_indicate_clients) {
1905 BT_INFO("Gatt indication confirm event for last GATT client.. [%s]", address);
1906 completed = 1; /* Last event */
1907 recvd = 0; /* Reset */
1908 num_indicate_clients = 0;
1911 param = g_variant_new("(isib)",
1917 /* Send event to BT-API */
1918 _bt_send_event(BT_GATT_SERVER_EVENT,
1919 BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_COMPLETED,
1922 BT_INFO("Received Indication confirm for client number [%d]", recvd);
1926 /* Tizen Platform Specific */
1927 static void __bt_handle_gatt_server_notification_changed(event_gatts_notif_t *event)
1929 bluetooth_device_address_t dev_addr;
1930 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1931 GVariant *param = NULL;
1934 /* OAL event does provide error, so MW assumes event will never contain wrong data,
1935 incase of any issues, check with OAL */
1936 int result = BLUETOOTH_ERROR_NONE;
1938 memcpy(dev_addr.addr, event->address.addr, 6);
1939 _bt_convert_addr_type_to_string(address,
1940 (unsigned char *)dev_addr.addr);
1942 BT_INFO("notification_changed [%s] conn_ ID [%d] transaction ID [%d] Att handle [%d] Notify[%d]",
1943 address, event->conn_id, event->trans_id, event->attr_handle, event->notify);
1945 /* Set Notifcation status */
1946 notify = event->notify;
1948 param = g_variant_new("(isib)",
1954 /* Send event to BT-API */
1955 _bt_send_event(BT_GATT_SERVER_EVENT,
1956 BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_STATE_CHANGED,
1962 static void __bt_handle_gatt_mtu_changed_event(event_gatts_mtu_changed_t *event)
1964 int result = BLUETOOTH_ERROR_NONE;
1965 struct gatt_client_info_t *conn_info = NULL;
1966 GVariant *param = NULL;
1968 BT_INFO("GATT Server MTU changed event");
1970 conn_info = __bt_find_remote_gatt_client_info_from_conn_id(event->conn_id);
1971 if (conn_info == NULL) {
1972 BT_ERR("Cant find connection Information");
1975 BT_INFO("Got connection Info GATT client [%s] MTU Size [%d]",
1976 conn_info->addr, event->mtu_size);
1978 __bt_update_mtu_gatt_device(conn_info->addr, event->mtu_size);
1980 param = g_variant_new("(isqy)",
1986 /* Send event to BT-API */
1987 _bt_send_event(BT_GATT_SERVER_EVENT,
1988 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
1992 static void __bt_gatt_event_handler(int event_type, gpointer event_data)
1994 BT_INFO("OAL event = 0x%x, \n", event_type);
1995 switch (event_type) {
1996 case OAL_EVENT_BLE_SERVER_INSTANCE_INITIALISED: {
1997 BT_INFO("OAL Event: Server Instance Registered");
1998 /* GATT Server Registered event is handled in MAIN thread context */
1999 __bt_handle_server_instance_registered((event_gatts_register_t *)event_data);
2002 case OAL_EVENT_GATTS_SERVICE_ADDED: {
2003 BT_INFO("OAL Event: GATT Service added");
2004 __bt_handle_gatt_server_service_added((event_gatts_srvc_prm_t *)event_data);
2007 case OAL_EVENT_GATTS_CHARACTERISTIC_ADDED: {
2008 BT_INFO("OAL Event: GATT characteristic added");
2009 __bt_handle_gatt_server_characteristic_added((event_gatts_srvc_charctr_t *)event_data);
2012 case OAL_EVENT_GATTS_DESCRIPTOR_ADDED: {
2013 BT_INFO("OAL Event: GATT descriptor added");
2014 __bt_handle_gatt_server_descriptor_added((event_gatts_srvc_descr_t *)event_data);
2017 case OAL_EVENT_GATTS_SERVICE_STARTED: {
2018 BT_INFO("OAL Event: GATT Service started");
2019 __bt_handle_gatt_server_service_started((event_gatts_srvc_t *)event_data);
2022 case OAL_EVENT_GATTS_SERVICE_STOPED: {
2023 BT_INFO("OAL Event: GATT Service stopped");
2024 __bt_handle_gatt_server_service_stopped((event_gatts_srvc_t *)event_data);
2027 case OAL_EVENT_GATTS_SERVICE_DELETED: {
2028 BT_INFO("OAL Event: GATT Service deleted");
2029 __bt_handle_gatt_server_service_deleted((event_gatts_srvc_t *) event_data);
2032 case OAL_EVENT_GATTS_CONNECTION_COMPLETED: {
2033 BT_INFO("OAL Event: GATT Server Connected");
2034 __bt_handle_gatt_server_connection_state((event_gatts_conn_t *)event_data);
2037 case OAL_EVENT_GATTS_DISCONNECTION_COMPLETED: {
2038 BT_INFO("OAL Event: GATT Server Disconnected");
2039 __bt_handle_gatt_server_disconnection_state((event_gatts_conn_t *)event_data);
2042 case OAL_EVENT_GATTS_REQUEST_READ: {
2043 BT_INFO("OAL Event: GATT Server Read Request");
2044 __bt_handle_gatt_server_read_requested((event_gatts_srvc_read_attr_t *)event_data);
2047 case OAL_EVENT_GATTS_REQUEST_WRITE: {
2048 BT_INFO("OAL Event: GATT Server Write Request");
2049 __bt_handle_gatt_server_write_requested((event_gatts_srvc_write_attr_t *)event_data);
2052 case OAL_EVENT_GATTS_REQUEST_ACQUIRE_WRITE: {
2053 BT_INFO("OAL Event: GATT Server Acquire Write Request");
2054 __bt_handle_gatt_server_acquire_write_requested((event_gatts_srvc_acquire_attr_t*)event_data);
2057 case OAL_EVENT_GATTS_REQUEST_ACQUIRE_NOTIFY: {
2058 BT_INFO("OAL Event: GATT ServerAcquire Notify Request");
2059 __bt_handle_gatt_server_acquire_notify_requested((event_gatts_srvc_acquire_attr_t*)event_data);
2062 case OAL_EVENT_GATTS_IND_CONFIRM: {
2063 BT_INFO("OAL Event: GATT Server Indication confirmed");
2064 __bt_handle_gatt_server_indicate_confirmed((event_gatts_ind_cnfrm_t *)event_data);
2067 case OAL_EVENT_GATTS_NOTIFICATION: { /* Tizen Platform Specific */
2068 BT_INFO("OAL Event: GATT Server DisConnected");
2069 __bt_handle_gatt_server_notification_changed((event_gatts_notif_t *)event_data);
2072 case OAL_EVENT_GATTS_MTU_CHANGED: {
2073 BT_INFO("OAL Event: GATT Server MTU changed event callback");
2074 __bt_handle_gatt_mtu_changed_event((event_gatts_mtu_changed_t *)event_data);
2077 #ifdef TIZEN_GATT_CLIENT
2078 case OAL_EVENT_GATTC_REGISTRATION: {
2079 BT_INFO("OAL Event: GATT Client instance Registered");
2080 __bt_handle_client_instance_registered((event_gattc_register_t *) event_data);
2083 case OAL_EVENT_GATTC_CONNECTION_COMPLETED: {
2084 BT_INFO("OAL Event: GATT Client Connected");
2085 __bt_handle_client_connected((event_gattc_conn_t *) event_data);
2088 case OAL_EVENT_GATTC_DISCONNECTION_COMPLETED: {
2089 BT_INFO("OAL Event: GATT Client DisConnected");
2090 __bt_handle_client_disconnected((event_gattc_conn_t *) event_data);
2093 case OAL_EVENT_GATTC_SERVICE_SEARCH_RESULT: {
2094 BT_INFO("OAL Event: GATT Client Service Search Result");
2095 __bt_handle_client_service_search_result((event_gattc_service_result_t *) event_data);
2098 case OAL_EVENT_GATTC_SERVICE_SEARCH_DONE: {
2099 BT_INFO("OAL Event: GATT Client Service Completed");
2100 __bt_handle_client_service_search_completed((event_gattc_conn_status_t *) event_data);
2103 case OAL_EVENT_GATTC_CHARAC_SERACH_RESULT: {
2104 BT_INFO("OAL Event: GATT Client Characteristic Search Result");
2105 __bt_handle_client_characteristic_search_result((event_gattc_characteristic_result_t *) event_data);
2108 case OAL_EVENT_GATTC_DESC_SERACH_RESULT: {
2109 BT_INFO("OAL Event: GATT Client Descriptor Search Result");
2110 __bt_handle_client_descriptor_search_result((event_gattc_descriptor_result_t *) event_data);
2113 case OAL_EVENT_GATTC_READ_CHARAC: {
2114 BT_INFO("OAL Event: GATT Client Characteristic Read Data");
2115 __bt_handle_client_characteristic_read_data((event_gattc_read_data *) event_data);
2118 case OAL_EVENT_GATTC_READ_DESCR: {
2119 BT_INFO("OAL Event: GATT Client Descriptor Read Data");
2120 __bt_handle_client_descriptor_read_data((event_gattc_read_data *) event_data);
2123 case OAL_EVENT_GATTC_WRITE_CHARAC: {
2124 BT_INFO("OAL Event: GATT Client Characteristic Write Data");
2125 __bt_handle_client_characteristic_write_data((event_gattc_write_data *) event_data);
2128 case OAL_EVENT_GATTC_WRITE_DESCR: {
2129 BT_INFO("OAL Event: GATT Client Descriptor Write Data");
2130 __bt_handle_client_descriptor_write_data((event_gattc_write_data *) event_data);
2133 case OAL_EVENT_DEVICE_LE_DISCONNECTED: {
2134 BT_INFO("OAL Event: LE device disconnected");
2135 __bt_hanlde_le_device_disconnection((event_dev_conn_status_t *)event_data);
2138 case OAL_EVENT_GATTC_NOTIFICATION_REGISTERED: {
2139 BT_INFO("OAL Event: GATT Client Notification Registered");
2140 __bt_handle_client_notification_registered((event_gattc_regdereg_notify_t *) event_data, TRUE);
2143 case OAL_EVENT_GATTC_NOTIFICATION_DEREGISTERED: {
2144 BT_INFO("OAL Event: GATT Client Notification Registered");
2145 __bt_handle_client_notification_registered((event_gattc_regdereg_notify_t *) event_data, FALSE);
2148 case OAL_EVENT_GATTC_NOTIFY_DATA: {
2149 BT_INFO("OAL Event: GATT Client Notification Data");
2150 __bt_handle_client_notification_data((event_gattc_notify_data *) event_data);
2153 case OAL_EVENT_GATTC_SERVICE_CHANGED_IND: {
2154 BT_INFO("OAL Event: GATT Client service changed indication");
2155 __bt_handle_client_service_changed_ind((event_gattc_service_changed_data *)event_data);
2158 case OAL_EVENT_GATTC_MTU_EXCHANGE_COMPLETED: {
2159 BT_INFO("OAL Event: GATT Client MTU Exchange Complete");
2160 __bt_handle_client_mtu_exchange_completed((event_gattc_mtu_configured_t *) event_data);
2169 int _bt_gatt_server_add_service(char *sender, int service_type,
2170 int num_handles, char *svc_uuid, int instance_id)
2172 BT_CHECK_PARAMETER(svc_uuid, return);
2173 BT_CHECK_PARAMETER(sender, return);
2174 int ret = OAL_STATUS_SUCCESS;
2176 oal_gatt_srvc_id_t svc_data;
2178 svc_data.is_prmry = service_type;
2179 svc_data.id.inst_id = instance_id;
2181 BT_INFO("Service UUID [%s] Num handles [%d] Instance ID [%d]", svc_uuid, num_handles, instance_id);
2182 _bt_string_to_uuid(svc_uuid, (service_uuid_t*)&svc_data.id.uuid);
2184 ret = gatts_add_service(instance_id, &svc_data, num_handles);
2185 if (ret != OAL_STATUS_SUCCESS) {
2186 BT_ERR("ret: %d", ret);
2187 return BLUETOOTH_ERROR_INTERNAL;
2190 return BLUETOOTH_ERROR_NONE;
2194 int _bt_gatt_server_add_included_service(char *sender, int instance_id,
2195 int service_handle, int included_svc_handle)
2197 BT_CHECK_PARAMETER(sender, return);
2198 int ret = OAL_STATUS_SUCCESS;
2200 ret = gatts_add_included_services(instance_id, service_handle, included_svc_handle);
2201 if (ret != OAL_STATUS_SUCCESS) {
2202 BT_ERR("ret: %d", ret);
2203 return BLUETOOTH_ERROR_INTERNAL;
2205 return BLUETOOTH_ERROR_NONE;
2208 int _bt_gatt_server_add_characteristic(char *sender, char *char_uuid,
2209 bluetooth_gatt_server_attribute_params_t *param)
2211 BT_CHECK_PARAMETER(char_uuid, return);
2212 BT_CHECK_PARAMETER(sender, return);
2213 BT_CHECK_PARAMETER(param, return);
2214 int ret = OAL_STATUS_SUCCESS;
2216 oal_uuid_t uuid = {{0} };
2218 BT_INFO("Char UUID [%s] Instance ID [%d]", char_uuid, param->instance_id);
2219 _bt_string_to_uuid(char_uuid, (service_uuid_t*)&uuid);
2221 BT_INFO("Char permission From API [0x%x]", param->permissions);
2223 ret = gatts_add_characteristics(param->instance_id, param->service_handle, &uuid,
2224 param->properties, (int)param->permissions);
2225 if (ret != OAL_STATUS_SUCCESS) {
2226 BT_ERR("ret: %d", ret);
2227 return BLUETOOTH_ERROR_INTERNAL;
2229 return BLUETOOTH_ERROR_NONE;
2232 int _bt_gatt_server_add_descriptor(char *sender, char *desc_uuid,
2233 bt_gatt_permission_t *param, int service_handle, int instance_id)
2235 BT_CHECK_PARAMETER(desc_uuid, return);
2236 BT_CHECK_PARAMETER(sender, return);
2237 BT_CHECK_PARAMETER(param, return);
2238 int ret = OAL_STATUS_SUCCESS;
2240 oal_uuid_t uuid = {{0} };
2242 BT_INFO("Descriptor UUID [%s] Instance ID [%d] Service handle [%d]",
2243 desc_uuid, service_handle, instance_id);
2245 _bt_string_to_uuid(desc_uuid, (service_uuid_t*)&uuid);
2247 BT_INFO("Descriptor permission From API [0x%x]", *param);
2248 ret = gatts_add_descriptor(instance_id, service_handle, &uuid, (int)*param);
2250 if (ret != OAL_STATUS_SUCCESS) {
2251 BT_ERR("ret: %d", ret);
2252 return BLUETOOTH_ERROR_INTERNAL;
2254 return BLUETOOTH_ERROR_NONE;
2257 int _bt_gatt_server_start_service(char *sender, int service_handle, int instance_id)
2259 BT_CHECK_PARAMETER(sender, return);
2260 int ret = OAL_STATUS_SUCCESS;
2262 ret = gatts_start_service(instance_id, service_handle, BT_GATT_TRANSPORT_LE);
2263 if (ret != OAL_STATUS_SUCCESS) {
2264 BT_ERR("ret: %d", ret);
2265 return BLUETOOTH_ERROR_INTERNAL;
2267 return BLUETOOTH_ERROR_NONE;
2270 int _bt_gatt_server_stop_service(char *sender, int service_handle, int instance_id)
2272 BT_CHECK_PARAMETER(sender, return);
2273 int ret = OAL_STATUS_SUCCESS;
2275 ret = gatts_stop_service(instance_id, service_handle);
2276 if (ret != OAL_STATUS_SUCCESS) {
2277 BT_ERR("ret: %d", ret);
2278 return BLUETOOTH_ERROR_INTERNAL;
2280 return BLUETOOTH_ERROR_NONE;
2283 int _bt_gatt_server_delete_service(char *sender, int service_handle, int instance_id)
2285 BT_CHECK_PARAMETER(sender, return);
2286 int ret = OAL_STATUS_SUCCESS;
2290 ret = gatts_delete_service(instance_id, service_handle);
2291 if (ret != OAL_STATUS_SUCCESS) {
2292 BT_ERR("ret: %d", ret);
2293 return BLUETOOTH_ERROR_INTERNAL;
2296 /* Remove the Service Handle */
2297 for (l = numapps[instance_id].service_handles; l != NULL;) {
2299 l = g_slist_next(l);
2300 if (handle && *handle == service_handle) {
2301 BT_INFO("Remove Service handle [%d]", *handle);
2302 numapps[instance_id].service_handles = g_slist_remove(numapps[instance_id].service_handles, handle);
2308 return BLUETOOTH_ERROR_NONE;
2311 int _bt_gatt_server_send_response(char *sender, bluetooth_gatt_att_data_t *data,
2312 bluetooth_gatt_server_response_params_t *param)
2314 BT_CHECK_PARAMETER(sender, return);
2315 BT_CHECK_PARAMETER(data, return);
2316 BT_CHECK_PARAMETER(param, return);
2317 struct gatt_server_req_info *req_info = NULL;
2318 int ret = OAL_STATUS_SUCCESS;
2320 oal_gatt_response_t response;
2322 BT_INFO("GATT Server Response: Req Type [%d] req_id [%d] status [%d] auth_req [%d] offset[%d] data len[%d]",
2323 param->req_type, param->request_id,
2324 param->response_status, param->auth_req,
2325 data->offset, data->length);
2327 /* Search for matching Request in List */
2328 req_info = __bt_gatt_server_find_request_info(param->request_id, param->req_type);
2330 BT_ERR("GATT Server Req Info not found for current response..return Error");
2331 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2334 memset(&response, 0x00, sizeof(oal_gatt_response_t));
2336 response.handle = req_info->attribute_handle;
2337 response.attr_value.auth_req = param->auth_req;
2338 response.attr_value.handle = req_info->attribute_handle;
2339 response.attr_value.offset = data->offset;
2340 response.attr_value.len = data->length;
2341 memcpy(&response.attr_value.value, &data->data, data->length);
2344 ret = gatts_send_response(req_info->connection_id, param->request_id,
2345 param->response_status, &response);
2347 if (ret != OAL_STATUS_SUCCESS) {
2348 BT_ERR("ret: %d", ret);
2349 return BLUETOOTH_ERROR_INTERNAL;
2352 BT_INFO("GATT Server Response successfully sent");
2353 /* Remove GATT server request from list */
2354 gatt_server_requests = g_slist_remove(gatt_server_requests, req_info);
2355 g_free(req_info->addr);
2357 return BLUETOOTH_ERROR_NONE;
2360 int _bt_gatt_server_acquire_send_response(char *sender, bluetooth_gatt_server_acquire_response_params_t *param , void *fd_list)
2362 BT_CHECK_PARAMETER(sender, return);
2363 BT_CHECK_PARAMETER(param, return);
2364 struct gatt_server_req_info *req_info = NULL;
2365 int ret = OAL_STATUS_SUCCESS;
2368 BT_INFO("GATT acquire Server Response: Req Type [%d] req_id [%d] fd [%d] mtu[%d]",
2369 param->req_type, param->request_id,
2373 /* Search for matching Request in List */
2374 req_info = __bt_gatt_server_find_request_info(param->request_id, param->req_type);
2376 BT_ERR("GATT acquire Server Req Info not found for current response..return Error");
2377 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2380 ret = gatt_send_response_acquire(req_info->connection_id, param->request_id, 0, param->fd, param->mtu, fd_list);
2382 if (ret != OAL_STATUS_SUCCESS) {
2383 BT_ERR("ret: %d", ret);
2384 return BLUETOOTH_ERROR_INTERNAL;
2387 BT_INFO("GATT acquire Server Response successfully sent");
2388 /* Remove GATT server request from list */
2389 gatt_server_requests = g_slist_remove(gatt_server_requests, req_info);
2390 g_free(req_info->addr);
2392 return BLUETOOTH_ERROR_NONE;
2397 int _bt_gatt_server_send_indication(char *sender, bluetooth_device_address_t *dev_addr,
2398 bluetooth_gatt_att_data_t *data,
2399 bluetooth_gatt_server_indication_params_t *param)
2401 BT_CHECK_PARAMETER(sender, return);
2402 BT_CHECK_PARAMETER(data, return);
2403 BT_CHECK_PARAMETER(param, return);
2405 gboolean all_send = FALSE;
2406 int ret = OAL_STATUS_SUCCESS;
2407 struct gatt_client_info_t *conn;
2409 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2410 _bt_convert_addr_type_to_string(address, dev_addr->addr);
2412 if (memcmp(dev_addr->addr, BDADDR_ANY, 6) == 0) {
2413 BT_INFO("GATT Server: Send Indication to all connected GATT clients..");
2416 BT_INFO("GATT Server: Send Indication to connected GATT client addr [%s]", address);
2419 /* Attempt to send Notification/Indication to all Connected GATT clients */
2421 ret = __bt_gatt_send_indication_to_all_connected_clients(data, param);
2422 if (ret != OAL_STATUS_SUCCESS) {
2423 BT_ERR("ret: %d", ret);
2425 return BLUETOOTH_ERROR_INTERNAL;
2429 conn = _bt_find_remote_gatt_client_info(address);
2431 ret = gatts_send_indication(param->instance_id, param->atrribute_handle,
2432 conn->connection_id, data->length,
2433 param->need_confirmation, (char *)(&data->data[0]));
2435 if (ret != OAL_STATUS_SUCCESS) {
2436 BT_ERR("ret: %d", ret);
2437 BT_INFO("Indication failed to send to Remote GATT Client [%s]", address);
2439 return BLUETOOTH_ERROR_INTERNAL;
2441 BT_INFO("Indication sent to Remote GATT Client [%s] wait for Notification completed event from OAL", address);
2443 num_indicate_clients = 1;
2444 return BLUETOOTH_ERROR_NONE;
2446 BT_ERR("Remote GATT client [%s] is not connected..Cant send Indication!!", address);
2448 return BLUETOOTH_ERROR_NOT_CONNECTED;
2451 return BLUETOOTH_ERROR_NONE;
2454 int _bt_gatt_server_update_attribute_value(char *sender, int instance_id,
2455 bluetooth_gatt_server_update_value_t *param)
2457 BT_CHECK_PARAMETER(sender, return);
2458 BT_CHECK_PARAMETER(param, return);
2459 int ret = OAL_STATUS_SUCCESS;
2461 oal_gatt_value_t value;
2462 BT_INFO("GATT Server Update value: Instance ID [%d] attr handle [%d] Value len [%d]",
2463 instance_id, param->attribute_handle, param->length);
2466 memset(&value, 0x00, sizeof(oal_gatt_value_t));
2468 value.handle = param->attribute_handle;
2469 value.len = param->length;
2470 memcpy(&value.value, ¶m->data.data, param->length);
2472 ret = gatts_update_att_value(instance_id, &value);
2474 if (ret != OAL_STATUS_SUCCESS) {
2475 BT_ERR("ret: %d", ret);
2476 return BLUETOOTH_ERROR_INTERNAL;
2479 BT_INFO("GATT Server Update Attribute Value successfully");
2480 return BLUETOOTH_ERROR_NONE;
2483 int _bt_request_att_mtu(bluetooth_device_address_t *device_address,
2486 struct gatt_server_info_t *conn_info = NULL;
2488 int ret = OAL_STATUS_SUCCESS;
2490 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
2491 _bt_convert_addr_type_to_string(addr, device_address->addr);
2493 /* Check if remote GATT Server is connected or not */
2494 conn_info = _bt_find_remote_gatt_server_info(addr);
2496 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
2497 conn_info->addr, conn_info->connection_id);
2499 BT_ERR("GATT Server is not yet connected..");
2501 return BLUETOOTH_ERROR_NOT_CONNECTED;
2504 ret = gattc_configure_mtu(conn_info->connection_id, mtu);
2505 if (ret != OAL_STATUS_SUCCESS) {
2506 BT_ERR("ret: %d", ret);
2508 return BLUETOOTH_ERROR_INTERNAL;
2512 return BLUETOOTH_ERROR_NONE;
2515 int _bt_get_att_mtu(bluetooth_device_address_t *address,
2518 BT_CHECK_PARAMETER(address, return);
2519 BT_CHECK_PARAMETER(mtu, return);
2520 struct gatt_client_info_t *conn_info = NULL;
2521 char addr[BT_ADDRESS_STRING_SIZE] = { 0 };
2522 int ret = OAL_STATUS_SUCCESS;
2525 _bt_convert_addr_type_to_string(addr, address->addr);
2527 BT_INFO("Get current MTU size for the remote client:DevAddress:[%s]", addr);
2529 conn_info = _bt_find_remote_gatt_client_info(addr);
2531 BT_INFO("GATT Client [%s] is connected, conn Id [%d] Instance ID [%d]",
2532 conn_info->addr, conn_info->connection_id, conn_info->instance_id);
2534 BT_ERR("GATT Client [%s] is not yet connected..", addr);
2535 return BLUETOOTH_ERROR_NOT_CONNECTED;
2538 ret = gatts_get_att_mtu(conn_info->connection_id, &stack_mtu);
2539 if (ret != OAL_STATUS_SUCCESS) {
2540 BT_ERR("ret: %d", ret);
2541 return BLUETOOTH_ERROR_INTERNAL;
2544 BT_INFO("ATT MTU received from OAL [%d]", stack_mtu);
2545 *mtu = (unsigned int)stack_mtu;
2548 BT_ERR("MTU value is zero, GATT Client [%s] is not yet connected..", addr);
2549 return BLUETOOTH_ERROR_NOT_CONNECTED;
2552 return BLUETOOTH_ERROR_NONE;
2555 #ifdef TIZEN_GATT_CLIENT
2556 /* GATT Client utility static functions */
2557 static bt_gatt_service_info_list_t *__bt_get_service_info_list(int conn_id)
2560 bt_gatt_service_info_list_t *info = NULL;
2562 for (l = list_gatt_info; l != NULL; l = g_slist_next(l)) {
2563 info = (bt_gatt_service_info_list_t *)l->data;
2567 if (info->conn_id == conn_id)
2574 static bt_gatt_service_info_t *__bt_find_matching_service(
2575 bt_gatt_service_info_list_t *svc_list, oal_gatt_srvc_id_t *svc)
2578 bt_gatt_service_info_t *info = NULL;
2580 for (l = svc_list->services; l != NULL; l = g_slist_next(l)) {
2581 info = (bt_gatt_service_info_t *)l->data;
2585 /* Match UUID and instance ID */
2586 if (!memcmp(&svc->id.uuid.uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2587 && (svc->id.inst_id == info->inst_id)) {
2594 static bt_gatt_char_info_t *__bt_find_matching_charc(
2595 bt_gatt_service_info_t *svc_info, oal_gatt_id_t *charc)
2598 bt_gatt_char_info_t *info = NULL;
2600 for (l = svc_info->chars; l != NULL; l = g_slist_next(l)) {
2601 info = (bt_gatt_char_info_t *)l->data;
2605 /* Match UUID and instance ID */
2606 if (!memcmp(&charc->uuid.uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2607 && (charc->inst_id == info->inst_id)) {
2614 static bt_gatt_descriptor_info_t *__bt_find_matching_desc(
2615 bt_gatt_char_info_t *char_info, oal_gatt_id_t *desc)
2618 bt_gatt_descriptor_info_t *info = NULL;
2620 for (l = char_info->descs; l != NULL; l = g_slist_next(l)) {
2621 info = (bt_gatt_descriptor_info_t *)l->data;
2625 /* Match UUID and instance ID */
2626 if (!memcmp(&desc->uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2627 && (desc->inst_id == info->inst_id)) {
2635 static struct gatt_server_info_t *__bt_find_remote_gatt_server_info_from_conn_id(int conn_id)
2638 struct gatt_server_info_t *info = NULL;
2640 for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
2641 info = (struct gatt_server_info_t*)l->data;
2645 if (info->connection_id == conn_id) {
2646 BT_INFO("Remote GATT server found addr[%s]", info->addr);
2653 static bt_gatt_service_info_t* __bt_find_removed_service(bt_gatt_service_info_list_t *svc_list)
2656 bt_gatt_service_info_t *info = NULL;
2658 for (l = svc_list->services; l != NULL; l = g_slist_next(l)) {
2659 info = (bt_gatt_service_info_t*)l->data;
2663 /* Service is marked a removed */
2664 if (info->is_removed == 1)
2670 static void __bt_remove_service_info_from_list(bt_gatt_service_info_t *svc_info)
2676 bt_gatt_char_info_t *charc = NULL;
2677 bt_gatt_included_service_info_t *incl = NULL;
2678 bt_gatt_descriptor_info_t *desc = NULL;
2680 /* Remove all Characteristic and Descriptors within characteristic */
2681 for (l = svc_info->chars; l != NULL;) {
2682 charc = (bt_gatt_char_info_t*)l->data;
2683 l = g_slist_next(l); /* Incase if l is removed, saving next to l */
2688 /* Inside Characteristic */
2689 for (l1 = charc->descs; l1 != NULL;) {
2691 desc = (bt_gatt_descriptor_info_t*)l1->data;
2692 l1 = g_slist_next(l1);
2697 /* Remove Descriptor */
2698 charc->descs = g_slist_remove(charc->descs, desc);
2701 /* Remove Characteristic */
2702 svc_info->chars = g_slist_remove(svc_info->chars, charc);
2706 /* Remove all Included Services */
2707 for (l2 = svc_info->included_svcs; l2 != NULL;) {
2708 incl = (bt_gatt_included_service_info_t*)l2->data;
2709 l2 = g_slist_next(l2); /* Incase if l is removed, saving next to l */
2714 /* Remove included service */
2715 svc_info->included_svcs = g_slist_remove(svc_info->included_svcs, incl);
2722 static void __bt_build_service_browse_info(int conn_id,
2723 bt_services_browse_info_t* info)
2726 bt_gatt_service_info_list_t *svc_info_list;
2727 bt_gatt_service_info_t *svc_info;
2729 service_uuid_t uuid;
2730 struct gatt_server_info_t *conn_info = NULL;
2732 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2734 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2739 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2741 svc_info_list = __bt_get_service_info_list(conn_id);
2745 info->count = g_slist_length(svc_info_list->services);
2746 BT_DBG("Total services present in the svc info list for this conn id [%d] is [%d]",
2747 conn_id, info->count);
2749 for (l = svc_info_list->services; l != NULL; l = g_slist_next(l)) {
2750 svc_info = (bt_gatt_service_info_t*)l->data;
2751 if (svc_info == NULL)
2754 memcpy(&uuid.uuid, &svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2755 _bt_uuid_to_string(&uuid, uuid_string);
2757 BT_INFO("[%d] %s [%s]", count, uuid_string, _bt_convert_uuid_to_string(uuid_string));
2759 /* Fill UUID of service */
2760 g_strlcpy(info->uuids[count], uuid_string,
2761 BLUETOOTH_UUID_STRING_MAX);
2763 /* Fill instance ID of service */
2764 info->inst_id[count] = svc_info->inst_id;
2766 /* Fill primary service or not info */
2767 info->primary[count] = svc_info->is_primary;
2769 /* Increment count of services browsed */
2774 static void __bt_build_char_browse_info(int conn_id,
2775 bt_gatt_service_info_t *svc_info,
2776 bt_char_browse_info_t* info)
2779 bt_gatt_char_info_t *char_info;
2780 service_uuid_t uuid;
2782 struct gatt_server_info_t *conn_info = NULL;
2784 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2786 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2790 /* Fill default data, this will be required even in case of failure */
2791 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2792 memcpy(&info->svc_uuid, svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2793 info->svc_inst_id = svc_info->inst_id;
2795 if (!svc_info->chars) {
2796 BT_ERR("No Chars browsed for address [%s]", conn_info->addr);
2800 info->count = g_slist_length(svc_info->chars);
2802 for (l = svc_info->chars; l != NULL; l = g_slist_next(l)) {
2803 char_info = (bt_gatt_char_info_t*)l->data;
2804 if (char_info == NULL)
2807 memcpy(&uuid.uuid, &char_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2808 _bt_uuid_to_string(&uuid, uuid_string);
2810 /* Fill UUID of characteristic */
2811 g_strlcpy(info->uuids[count], uuid_string,
2812 BLUETOOTH_UUID_STRING_MAX);
2814 /* Fill instance ID of characteristic */
2815 info->inst_id[count] = char_info->inst_id;
2817 /* Fill property of characteristic */
2818 info->props[count] = char_info->props;
2820 /* Increment count of services browsed */
2823 BT_DBG("Total characteristics browsed [%d]", count);
2826 static void __bt_build_descriptor_browse_info(int conn_id,
2827 bt_gatt_service_info_t *svc_info,
2828 bt_gatt_char_info_t *char_info,
2829 bt_descriptor_browse_info_t* info)
2832 bt_gatt_descriptor_info_t *desc_info;
2834 service_uuid_t uuid;
2835 struct gatt_server_info_t *conn_info = NULL;
2837 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2839 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2841 /* Fill default data, this will be required even in case of failure */
2842 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2843 memcpy(&info->svc_uuid, svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2844 info->svc_inst_id = svc_info->inst_id;
2845 memcpy(&info->char_uuid, char_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2846 info->char_inst_id = char_info->inst_id;
2848 /* Fill property of the parent characteristic of this descriptor */
2849 info->char_props_map = char_info->props;
2851 info->count = g_slist_length(char_info->descs);
2853 if (!char_info->descs) {
2854 BT_ERR("No Descriptors browsed for address [%s]", conn_info->addr);
2858 for (l = char_info->descs; l != NULL; l = g_slist_next(l)) {
2859 desc_info = (bt_gatt_descriptor_info_t*)l->data;
2860 if (desc_info == NULL)
2863 memcpy(&uuid.uuid, &desc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2864 _bt_uuid_to_string(&uuid, uuid_string);
2866 /* Fill UUID of Descriptor */
2867 g_strlcpy(info->uuids[count], uuid_string,
2868 BLUETOOTH_UUID_STRING_MAX);
2870 /* Fill instance ID of Descriptor */
2871 info->inst_id[count] = desc_info->inst_id;
2874 /* Increment count of Descriptor browsed */
2878 BT_INFO("Total descriptors browsed [%d]", count);
2881 static void __bt_free_service_info(bt_gatt_service_info_t *svc)
2883 GSList *ll, *lll, *llll;
2884 bt_gatt_char_info_t *chr = NULL;
2885 bt_gatt_descriptor_info_t *desc = NULL;
2886 bt_gatt_included_service_info_t *incl_svc = NULL;
2888 BT_DBG("Service info Is Prim[%d] Inst ID [%d]", svc->is_primary, svc->inst_id);
2889 /* Delete all chars and its descriptors */
2890 for (ll = svc->chars; ll != NULL; ) {
2891 chr = (bt_gatt_char_info_t *)ll->data;
2892 ll = g_slist_next(ll);
2896 for (lll = chr->descs; lll != NULL; ) {
2897 desc = (bt_gatt_descriptor_info_t *)lll->data;
2898 lll = g_slist_next(lll);
2901 chr->descs = g_slist_remove(chr->descs, desc);
2904 svc->chars = g_slist_remove(svc->chars, chr);
2908 /* Delete all included services */
2909 for (llll = svc->included_svcs; llll != NULL; ) {
2910 incl_svc = (bt_gatt_included_service_info_t *)llll->data;
2911 llll = g_slist_next(llll);
2912 if (incl_svc == NULL)
2914 svc->included_svcs = g_slist_remove(svc->included_svcs, incl_svc);
2919 static void __bt_cleanup_remote_services(struct gatt_server_info_t *conn_info)
2921 bt_gatt_service_info_list_t *svc_info_list = NULL;
2922 bt_gatt_service_info_t *svc = NULL;
2925 svc_info_list = __bt_get_service_info_list(conn_info->connection_id);
2926 if (!svc_info_list) {
2927 BT_INFO("Could not find Svc Info list for the connection ID [%d]",
2928 conn_info->connection_id);
2932 BT_INFO("Start Cleanup of all services. Num Services [%d]", g_slist_length(svc_info_list->services));
2933 for (l = svc_info_list->services; l != NULL; ) {
2934 svc = (bt_gatt_service_info_t *)l->data;
2935 l = g_slist_next(l);
2939 __bt_free_service_info(svc);
2940 svc_info_list->services = g_slist_remove(svc_info_list->services, svc);
2944 list_gatt_info = g_slist_remove(list_gatt_info, svc_info_list);
2945 g_free(svc_info_list);
2948 int _bt_register_gatt_client_instance(const char *sender,
2949 bluetooth_device_address_t *address)
2951 int ret = OAL_STATUS_SUCCESS;
2952 char *uuid_string = NULL;
2957 /* App should ensure that it should not send */
2958 BT_INFO("### Check on which instance GATT Client instance can be initialized....");
2959 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
2960 if (numapps[k].is_initialized == 1) {
2961 BT_INFO("Instance ID [%d] is already in use..Check next slot",
2962 numapps[k].instance_id);
2965 BT_INFO("Time to register GATT client instancer..UUID to be used is [%s] slot [%d]",
2966 uuid_list[slot-1], slot);
2972 BT_ERR("No Slot if free for GATT Client registration..");
2973 return BLUETOOTH_ERROR_REGISTRATION_FAILED;
2976 uuid_string = g_malloc0(BT_UUID_STRING_MAX);
2977 _bt_string_to_uuid(uuid_list[slot-1], (service_uuid_t*)&uuid);
2978 g_strlcpy(uuid_string, uuid_list[slot-1], BT_UUID_STRING_MAX);
2979 BT_INFO("Copied UUID string [%s]", uuid_string);
2981 /* Register GATT Client */
2982 ret = gattc_register(&uuid);
2983 if (ret != OAL_STATUS_SUCCESS) {
2984 BT_ERR("ret: %d", ret);
2985 g_free(uuid_string);
2986 return BLUETOOTH_ERROR_INTERNAL;
2989 BT_INFO("GATT Client registration call successfully accepted by OAL..wait for Instance Initialized event from OAL..");
2991 /* Return & wait for GATT Client Instance Initialization event */
2992 memset(numapps[slot].sender, 0x00, sizeof(numapps[slot].sender));
2993 memset(numapps[slot].uuid, 0x00, sizeof(numapps[slot].uuid));
2995 g_strlcpy(numapps[slot].sender, sender, sizeof(numapps[slot].sender));
2996 g_strlcpy(numapps[slot].uuid, uuid_string, sizeof(numapps[slot].uuid));
2998 /* Address is saved here. When event comes, sender + address are matched for replying pending
2999 request. It is impossible for same sender to have requests with two same addresses */
3000 memcpy(&numapps[slot].address.addr, address->addr, sizeof(bluetooth_device_address_t));
3002 BT_INFO("Slot [%d] occupied", slot);
3003 numapps[slot].is_initialized = TRUE; /* Set initialization to true here itself */
3005 g_free(uuid_string);
3006 return BLUETOOTH_ERROR_NONE;
3012 /* GATT client events */
3013 static void __bt_handle_client_instance_registered(event_gattc_register_t *data)
3015 bt_service_app_info_t *info = NULL;
3017 char *uuid_string = g_malloc0(BT_UUID_STRING_MAX);
3019 _bt_uuid_to_string(&(data->client_uuid), uuid_string);
3020 BT_INFO("CLient ID is Initialized [%d] UUID initialized [%s]", data->client_if, uuid_string);
3022 /* Platform GATT client framwork does not use Default GATT client instance
3023 This GATT client instance is never deregistred in the lifetime of bt-service */
3024 if (g_strcmp0(uuid_string, DEFAULT_GATT_CLIENT_UUID) == 0) {
3025 BT_INFO("Default client Instance Registered");
3026 gatt_default_client = data->client_if;
3027 g_free(uuid_string);
3031 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
3034 if (g_strcmp0(info->uuid, uuid_string) == 0) {
3035 BT_INFO("Found GATT client.. sender [%s] Slot [%d] occupied", info->sender, k);
3036 info->is_initialized = TRUE;
3037 info->client_id = data->client_if;
3038 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE, BT_GATT_CLIENT_REGISTER,
3039 (void*)info, sizeof(bt_service_app_info_t));
3043 g_free(uuid_string);
3046 static void __bt_handle_client_connected(event_gattc_conn_t *event_data)
3048 int result = BLUETOOTH_ERROR_NONE;
3049 struct gatt_server_info_t *conn_info = NULL;
3050 struct gatt_out_conn_info_t *out_conn_info = NULL;
3052 GVariant *param = NULL;
3054 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
3055 _bt_convert_addr_type_to_string(address,
3056 (unsigned char *)event_data->address.addr);
3058 if (event_data->status != OAL_STATUS_SUCCESS)
3059 result = BLUETOOTH_ERROR_INTERNAL;
3061 /* DBUS Return fo BT_CONNECT_LE for all the apps */
3062 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE, address,
3063 BT_ADDRESS_STRING_SIZE);
3065 BT_INFO("Local GATT Client Connected: Remote addr[%s] Client Interface [%d] Connection ID [%d] status[%d]",
3066 address, event_data->client_if, event_data->conn_id, event_data->status);
3068 if (result == BLUETOOTH_ERROR_NONE) {
3069 /* Check if device is already in connected list */
3070 conn_info = _bt_find_remote_gatt_server_info(address);
3073 /* Send event to BT-API */
3074 param = g_variant_new("(is)", result, address);
3075 _bt_send_event(BT_DEVICE_EVENT,
3076 BLUETOOTH_EVENT_GATT_CLIENT_CONNECTED, /* Local device is GATT client */
3079 /* Save Connection info */
3080 conn_info = g_new0(struct gatt_server_info_t, 1);
3081 conn_info->addr = g_strdup(address);
3082 conn_info->client_id = event_data->client_if;
3083 BT_INFO("Added GATT server addr[%s]", conn_info->addr);
3084 conn_info->connection_id = event_data->conn_id;
3085 gatt_server_info_list = g_slist_append(gatt_server_info_list, conn_info);
3086 BT_INFO("Total num of connected Remote GATT server devices [%d]",
3087 g_slist_length(gatt_server_info_list));
3090 BT_INFO("Do a Internal refresh");
3091 if (OAL_STATUS_SUCCESS != gattc_refresh(conn_info->client_id, &event_data->address))
3092 BT_ERR("GATT database refresh failed!!");
3094 BT_INFO("GATT database refresh Success!!");
3097 BT_ERR("Local GATT Client connected event for addr[%s], but device is in connected list already", address);
3099 __bt_add_mtu_gatt_device(address);
3101 BT_ERR("GATT Client Connection failed!!");
3103 /* If outgoing connection Info is present, then remove it */
3104 out_conn_info = __bt_find_gatt_outgoing_conn_info(address);
3105 if (out_conn_info) {
3106 BT_ERR("Outgoing Client connect request was sent");
3107 outgoing_gatt_conn_list = g_slist_remove(outgoing_gatt_conn_list, out_conn_info);
3108 g_free(out_conn_info->addr);
3109 g_free(out_conn_info);
3114 static void __bt_handle_client_disconnected(event_gattc_conn_t *event_data)
3116 int result = BLUETOOTH_ERROR_NONE;
3118 struct gatt_server_info_t *conn_info = NULL;
3119 struct gatt_out_conn_info_t *out_conn_info = NULL;
3121 GVariant *param = NULL;
3123 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
3124 _bt_convert_addr_type_to_string(address,
3125 (unsigned char *)event_data->address.addr);
3127 if (event_data->status != OAL_STATUS_SUCCESS)
3128 result = BLUETOOTH_ERROR_INTERNAL;
3130 if (NULL == _bt_get_request_info_data(BT_DISCONNECT_LE, address)) {
3131 if (NULL != _bt_get_request_info_data(BT_CONNECT_LE, address)) {
3132 result = BLUETOOTH_ERROR_INTERNAL;
3133 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
3134 address, BT_ADDRESS_STRING_SIZE);
3135 BT_ERR("Failed to connect Local GATT Remote addr[%s]", address);
3140 /* DBUS Return for BT_DISCONNECT_LE for all the apps */
3141 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE, address,
3142 BT_ADDRESS_STRING_SIZE);
3144 BT_INFO("Local GATT Client DisConnected: Remote addr[%s] Client Interface [%d] Connection ID [%d] status [%d]",
3145 address, event_data->client_if, event_data->conn_id, event_data->status);
3147 /* Remove Connection info */
3148 conn_info = _bt_find_remote_gatt_server_info(address);
3151 param = g_variant_new("(is)", result, address);
3152 /* Send event to application */
3153 _bt_send_event(BT_DEVICE_EVENT,
3154 BLUETOOTH_EVENT_GATT_CLIENT_DISCONNECTED,
3157 BT_INFO("Remove GATT server info from List..");
3158 /* Remove all services from info list_gatt_info */
3159 __bt_cleanup_remote_services(conn_info);
3161 /* Remove info from List */
3162 gatt_server_info_list = g_slist_remove(gatt_server_info_list, conn_info);
3163 BT_INFO("Total num of connected GATT servers [%d]", g_slist_length(gatt_server_info_list));
3164 g_free(conn_info->addr);
3167 BT_INFO("Can not find conn info, already removed!");
3169 __bt_remove_mtu_gatt_device(address);
3171 /* If outgoing connection Info is present, then remove it */
3172 out_conn_info = __bt_find_gatt_outgoing_conn_info(address);
3173 if (out_conn_info) {
3174 BT_ERR("Client Disconnected event, but outgoing connect request was sent");
3175 outgoing_gatt_conn_list = g_slist_remove(outgoing_gatt_conn_list, out_conn_info);
3176 g_free(out_conn_info->addr);
3177 g_free(out_conn_info);
3183 static void __bt_handle_client_service_search_result(
3184 event_gattc_service_result_t *event_data)
3186 /* Pre: status is never fail from OAL */
3188 /* Find service list from address */
3189 bt_gatt_service_info_list_t *svc_info_list;
3190 bt_gatt_service_info_t *svc_info;
3192 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3193 if (!svc_info_list) {
3194 BT_DBG("Service info list not present for connection ID %d, means first time browse", event_data->conn_status.conn_id);
3195 /* Means for this conn_id, no services are ever browsed, first time,
3196 create service info list for this conn_id */
3197 svc_info_list = g_malloc0(sizeof(bt_gatt_service_info_list_t));
3198 svc_info_list->conn_id = event_data->conn_status.conn_id;
3199 list_gatt_info = g_slist_append(list_gatt_info, svc_info_list);
3202 /* send list and current service's uuid and instance id to find it */
3203 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3206 /* If not found, check if service changed, if yes, means this is a new service added
3207 in remote GATT device, update uuid info in svc info list structure, to be used when
3208 search is completed */
3210 if (svc_info_list->info.is_changed) {
3211 BT_DBG("Service Changed indication already found for connection ID %d", event_data->conn_status.conn_id);
3212 memcpy(svc_info_list->info.uuid, event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3214 /* Create and add new service in service list */
3215 svc_info = g_malloc0(sizeof(bt_gatt_service_info_t));
3216 memcpy(svc_info->uuid, event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3217 svc_info->inst_id = event_data->srvc_id.id.inst_id;
3218 svc_info->is_primary = event_data->srvc_id.is_prmry;
3219 svc_info_list->services = g_slist_append(svc_info_list->services, svc_info);
3220 BT_DBG("Service created and added in Svc info list or connection ID %d", event_data->conn_status.conn_id);
3222 /* If returned matching service info, then just update service_rmeoved value inside it to 0 */
3223 svc_info->is_removed = 0;
3227 static void __bt_handle_client_service_search_completed(
3228 event_gattc_conn_status_t *event_data)
3230 struct gatt_server_info_t *conn_info = NULL;
3231 bt_gatt_service_info_list_t *svc_info_list;
3232 bt_gatt_service_info_t *svc_info;
3233 bt_services_browse_info_t browse_info;
3234 unsigned char uuid_empty[BLUETOOTH_UUID_HEX_MAX_LEN];
3236 memset(&uuid_empty, 0x00, BLUETOOTH_UUID_HEX_MAX_LEN);
3237 memset(&browse_info, 0x00, sizeof(bt_services_browse_info_t));
3238 BT_INFO("Primary Services browsing completed status[%d] conn ID [%d]",
3239 event_data->status, event_data->conn_id);
3241 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(event_data->conn_id);
3243 svc_info_list = __bt_get_service_info_list(event_data->conn_id);
3244 if (!svc_info_list) {
3245 BT_ERR("No services browsed ever for addr [%s]", conn_info->addr);
3247 /* Just build response and return ERROR */
3248 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3250 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_INTERNAL,
3251 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3252 sizeof(bt_services_browse_info_t));
3256 /* If fail, then send event with error */
3257 if (event_data->status != OAL_STATUS_SUCCESS) {
3258 /* Just build response and return ERROR */
3259 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3261 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_INTERNAL,
3262 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3263 sizeof(bt_services_browse_info_t));
3267 /* If success, then find service info list from address */
3269 /* If svc_changed == 1 and uuid valid, means a new service is added*/
3270 if (svc_info_list->info.is_changed && !memcmp(uuid_empty, svc_info_list->info.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
3271 /* TODO: Send event -Service added with instance ID and UUID of newly added service */
3272 BT_INFO("new service added");
3274 BT_INFO("TODO new service added");
3277 /* If svc_changed == 1 and uuid invalid, then a service is removed */
3278 if (svc_info_list->info.is_changed && memcmp(uuid_empty, svc_info_list->info.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
3279 /* Scan through the service info list to find service with is_removed = 1*/
3280 svc_info = __bt_find_removed_service(svc_info_list);
3282 /* TODO Send event - Service removed with instance ID and UUID of just rmeoved service */
3284 /* Remove that service info from service info list */
3285 svc_info_list->services = g_slist_remove(svc_info_list->services, svc_info);
3287 /* Delete that service completely from svc_info list*/
3288 __bt_remove_service_info_from_list(svc_info);
3292 /* Reset svc_changed = 0, and reset UUID = all 0's */
3293 svc_info_list->info.is_changed = 0;
3294 memset(&svc_info_list->info.uuid, 0x00, BLUETOOTH_UUID_HEX_MAX_LEN);
3296 /* Build Reply and send to service browse primary services request of pending apps */
3297 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3299 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3300 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3301 sizeof(bt_services_browse_info_t));
3305 static void __bt_handle_client_characteristic_search_result(
3306 event_gattc_characteristic_result_t *event_data)
3308 bt_gatt_service_info_list_t *svc_info_list;
3309 bt_gatt_service_info_t *svc_info;
3310 bt_gatt_char_info_t *char_info;
3311 bt_char_browse_info_t browse_info;
3313 memset(&browse_info, 0x00, sizeof(bt_char_browse_info_t));
3316 if (event_data->conn_status.status == OAL_STATUS_SUCCESS) {
3317 /* Find service info list from address */
3318 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3319 if (svc_info_list == NULL) {
3320 BT_ERR("svc_info_list is NULL");
3325 /* Find matching service info from svc info list */
3326 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3327 if (svc_info == NULL) {
3328 BT_ERR("svc_info is NULL");
3332 /* Find Matching char from service info in event */
3333 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3335 /* If not found, then add new characteristic and return */
3337 BT_DBG("Add new characteristic");
3338 char_info = g_malloc0(sizeof(bt_gatt_char_info_t));
3339 memcpy(char_info->uuid, event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3341 char_info->inst_id = event_data->char_id.inst_id;
3342 char_info->props = event_data->char_prop;
3343 svc_info->chars = g_slist_append(svc_info->chars, char_info);
3345 /* If found, then return */
3346 BT_DBG("update char property as Characteristic browsed is already present");
3347 char_info->props |= event_data->char_prop;
3350 /* If Not success: Means Charc browse is completed */
3351 /* Find char list from service in event */
3352 /* Find service list from address */
3353 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3354 if (svc_info_list == NULL) {
3355 BT_ERR("svc_info_list is NULL");
3359 /* Find service info from service in event */
3360 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3361 if (svc_info == NULL) {
3362 BT_ERR("svc_info is NULL");
3366 /* Build char list from service in event */
3367 __bt_build_char_browse_info(event_data->conn_status.conn_id,
3368 svc_info, &browse_info);
3370 /* Create response and return by sending event*/
3371 /* Build Reply and send to service browse All Included services request of pending apps */
3372 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3373 BT_GATT_GET_SERVICE_PROPERTIES,
3375 sizeof(bt_char_browse_info_t));
3379 static void __bt_handle_client_descriptor_search_result(
3380 event_gattc_descriptor_result_t *event_data)
3382 bt_gatt_service_info_list_t *svc_info_list;
3383 bt_gatt_service_info_t *svc_info;
3384 bt_gatt_char_info_t *char_info;
3385 bt_gatt_descriptor_info_t *desc_info;
3386 bt_descriptor_browse_info_t browse_info;
3388 BT_INFO("descriptor search result status [%d]", event_data->conn_status.status);
3390 memset(&browse_info, 0x00, sizeof(bt_descriptor_browse_info_t));
3393 if (event_data->conn_status.status == OAL_STATUS_SUCCESS) {
3394 /* Find service list from address */
3395 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3396 if (svc_info_list == NULL) {
3397 BT_ERR("svc_info_list is NULL");
3401 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3402 if (svc_info == NULL) {
3403 BT_ERR("svc_info is NULL");
3407 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3408 if (char_info == NULL) {
3409 BT_ERR("char_info is NULL");
3413 desc_info = __bt_find_matching_desc(char_info, &event_data->descr_id);
3414 /* If not found, add new descriptor and return */
3416 desc_info = g_malloc0(sizeof(bt_gatt_descriptor_info_t));
3417 memcpy(desc_info->uuid, event_data->descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3419 desc_info->inst_id = event_data->descr_id.inst_id;
3420 char_info->descs = g_slist_append(char_info->descs, desc_info);
3423 /* If found, then return */
3424 BT_INFO("Characteristic browsed is already presesnt");
3427 /* If Not success */
3428 /* Find service list from address */
3429 /* Find included service list from service in event */
3430 /* Create response and return by sending event*/
3431 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3432 if (svc_info_list == NULL) {
3433 BT_ERR("svc_info_list is NULL");
3437 /* Find service info from service in event */
3438 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3439 if (svc_info == NULL) {
3440 BT_ERR("svc_info is NULL");
3444 /* Find char info from char in event */
3445 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3446 if (char_info == NULL) {
3447 BT_ERR("char_info is NULL");
3451 /* Build descriptor list from char in event */
3452 __bt_build_descriptor_browse_info(event_data->conn_status.conn_id,
3453 svc_info, char_info, &browse_info);
3456 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3457 BT_GATT_GET_CHARACTERISTIC_PROPERTIES,
3459 sizeof(bt_descriptor_browse_info_t));
3463 static void __bt_handle_client_characteristic_read_data(
3464 event_gattc_read_data *event_data)
3466 int result = BLUETOOTH_ERROR_NONE;
3467 struct gatt_server_info_t *conn_info = NULL;
3468 bluetooth_gatt_client_char_prop_info_t read_info;
3470 /* Read Information data structures */
3471 GVariant *param = NULL;
3472 GVariant *data = NULL;
3473 GVariant *data_svc_uuid = NULL;
3474 GVariant *data_char_uuid = NULL;
3475 char *read_val = NULL;
3476 char *svc_uuid = NULL;
3477 char *char_uuid = NULL;
3480 //memset(&read_info, 0x00, sizeof(bt_gatt_handle_property_t));
3481 memset(&read_info, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
3483 /* Extract Address from conn_id of event data */
3484 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3485 event_data->uuid_status.conn_status.conn_id);
3487 BT_INFO("Characteristic Read result from addr [%s] status [%d]",
3488 conn_info->addr, event_data->uuid_status.conn_status.status);
3490 /* Fill char in buffer */
3491 memcpy(&read_info.characteristic.uuid,
3492 event_data->uuid_status.char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3493 read_info.characteristic.instance_id = event_data->uuid_status.char_id.inst_id;
3495 /* Fill Service in buffer */
3496 memcpy(&read_info.svc.uuid,
3497 event_data->uuid_status.srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3498 read_info.svc.instance_id = event_data->uuid_status.srvc_id.id.inst_id;
3500 /* Fill remote device address */
3501 _bt_convert_addr_string_to_type(read_info.device_address.addr, conn_info->addr);
3503 /* Fill data and reply to all apps waiting for Read result on the same characteristic
3504 Note: Even in case of failure, address, handles and result code should be returned */
3505 if (event_data->uuid_status.conn_status.status != OAL_STATUS_SUCCESS)
3506 result = BLUETOOTH_ERROR_INTERNAL;
3508 if (event_data->data_len > 0) {
3510 // for (i = 0; i < event_data->data_len; i++)
3511 // BT_INFO("Data[%d] = [0x%x]", i, event_data->data[i]);
3514 read_val = g_memdup(&event_data->data[0], event_data->data_len);
3516 data = g_variant_new_from_data(
3517 G_VARIANT_TYPE_BYTESTRING,
3519 event_data->data_len,
3522 BT_ERR("Characteristic Read success, but no data!!!");
3524 data = g_variant_new_from_data(
3525 G_VARIANT_TYPE_BYTESTRING,
3532 svc_uuid = g_memdup(&event_data->uuid_status.srvc_id.id.uuid.uuid[0], uuid_len);
3534 data_svc_uuid = g_variant_new_from_data(
3535 G_VARIANT_TYPE_BYTESTRING,
3541 char_uuid = g_memdup(&event_data->uuid_status.char_id.uuid.uuid[0], uuid_len);
3543 data_char_uuid = g_variant_new_from_data(
3544 G_VARIANT_TYPE_BYTESTRING,
3549 param = g_variant_new("(isn@ayin@ayin@ay)", result,
3553 event_data->uuid_status.srvc_id.id.inst_id,
3556 event_data->uuid_status.char_id.inst_id,
3557 event_data->data_len,
3561 _bt_send_event(BT_GATT_CLIENT_EVENT,
3562 BLUETOOTH_EVENT_GATT_READ_CHAR,
3565 /* Send DBUS return */
3566 __bt_gatt_handle_pending_request_info(result,
3567 BT_GATT_READ_CHARACTERISTIC,
3569 sizeof(bluetooth_gatt_client_char_prop_info_t));
3580 static void __bt_handle_client_descriptor_read_data(
3581 event_gattc_read_data *event_data)
3583 int result = BLUETOOTH_ERROR_NONE;
3584 struct gatt_server_info_t *conn_info = NULL;
3585 bluetooth_gatt_client_desc_prop_info_t read_info;
3587 /* Read Information data structures */
3588 GVariant *param = NULL;
3589 GVariant *data = NULL;
3590 GVariant *data_svc_uuid = NULL;
3591 GVariant *data_char_uuid = NULL;
3592 GVariant *data_desc_uuid = NULL;
3593 char *read_val = NULL;
3594 char *svc_uuid = NULL;
3595 char *char_uuid = NULL;
3596 char *desc_uuid = NULL;
3601 memset(&read_info, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
3603 /* Extract Address from conn_id of event data */
3604 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3605 event_data->uuid_status.conn_status.conn_id);
3607 BT_INFO("Descriptor Read result from addr [%s] status [%d]",
3608 conn_info->addr, event_data->uuid_status.conn_status.status);
3610 /* Fill descriptor informations in buffer */
3611 memcpy(&read_info.descriptor.uuid,
3612 event_data->uuid_status.descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3613 read_info.descriptor.instance_id = event_data->uuid_status.descr_id.inst_id;
3615 /* Fill Characteristic informations in buffer */
3616 memcpy(&read_info.characteristic.uuid,
3617 event_data->uuid_status.char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3618 read_info.characteristic.instance_id = event_data->uuid_status.char_id.inst_id;
3620 /* Fill Service informations in buffer */
3621 memcpy(&read_info.svc.uuid,
3622 event_data->uuid_status.srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3623 read_info.svc.instance_id = event_data->uuid_status.srvc_id.id.inst_id;
3625 /* Fill remote device address */
3626 _bt_convert_addr_string_to_type(read_info.device_address.addr, conn_info->addr);
3628 /* Fill data and reply to all apps waiting for Read result on the same characteristic */
3629 if (event_data->uuid_status.conn_status.status != OAL_STATUS_SUCCESS)
3630 result = BLUETOOTH_ERROR_INTERNAL;
3632 BT_INFO("desc data len:%d", event_data->data_len);
3633 if (event_data->data_len > 0) {
3635 for (i = 0; i < event_data->data_len; i++)
3636 BT_DBG("Data[%d] = [0x%x]", i, event_data->data[i]);
3639 read_val = g_memdup(&event_data->data[0], event_data->data_len);
3641 data = g_variant_new_from_data(
3642 G_VARIANT_TYPE_BYTESTRING,
3644 event_data->data_len,
3647 BT_INFO("Descriptor Read success, but no data!!!");
3649 data = g_variant_new_from_data(
3650 G_VARIANT_TYPE_BYTESTRING,
3656 svc_uuid = g_memdup(&event_data->uuid_status.srvc_id.id.uuid.uuid[0], uuid_len);
3658 data_svc_uuid = g_variant_new_from_data(
3659 G_VARIANT_TYPE_BYTESTRING,
3665 char_uuid = g_memdup(&event_data->uuid_status.char_id.uuid.uuid[0], uuid_len);
3667 data_char_uuid = g_variant_new_from_data(
3668 G_VARIANT_TYPE_BYTESTRING,
3674 desc_uuid = g_memdup(&event_data->uuid_status.descr_id.uuid.uuid[0], uuid_len);
3676 data_desc_uuid = g_variant_new_from_data(
3677 G_VARIANT_TYPE_BYTESTRING,
3682 param = g_variant_new("(isn@ayin@ayin@ayin@ay)", result,
3686 event_data->uuid_status.srvc_id.id.inst_id,
3689 event_data->uuid_status.char_id.inst_id,
3692 event_data->uuid_status.descr_id.inst_id,
3693 event_data->data_len,
3697 _bt_send_event(BT_GATT_CLIENT_EVENT,
3698 BLUETOOTH_EVENT_GATT_READ_DESC,
3701 BT_INFO("Send DBUS rpely for GATT Read Descriptor");
3702 /* Send DBUS return */
3703 __bt_gatt_handle_pending_request_info(result,
3704 BT_GATT_READ_DESCRIPTOR_VALUE,
3706 sizeof(bluetooth_gatt_client_desc_prop_info_t));
3717 static void __bt_handle_client_characteristic_write_data(
3718 event_gattc_write_data *event_data)
3720 int result = BLUETOOTH_ERROR_NONE;
3721 struct gatt_server_info_t *conn_info = NULL;
3722 bluetooth_gatt_client_char_prop_info_t write_info;
3724 /* Read Information data structures */
3725 GVariant *param = NULL;
3726 GVariant *data_svc_uuid = NULL;
3727 GVariant *data_char_uuid = NULL;
3728 char *svc_uuid = NULL;
3729 char *char_uuid = NULL;
3733 memset(&write_info, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
3735 /* Extract Address from conn_id of event data */
3736 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3737 event_data->conn_status.conn_id);
3739 BT_INFO("Characteristic Write callback from addr [%s] status [%d]",
3740 conn_info->addr, event_data->conn_status.status);
3742 /* Fill char in buffer */
3743 memcpy(&write_info.characteristic.uuid,
3744 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3745 write_info.characteristic.instance_id = event_data->char_id.inst_id;
3747 /* Fill Service in buffer */
3748 memcpy(&write_info.svc.uuid,
3749 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3750 write_info.svc.instance_id = event_data->srvc_id.id.inst_id;
3752 /* Fill remote device address */
3753 _bt_convert_addr_string_to_type(write_info.device_address.addr, conn_info->addr);
3755 if (event_data->conn_status.status != OAL_STATUS_SUCCESS) {
3756 result = BLUETOOTH_ERROR_INTERNAL;
3762 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
3764 data_svc_uuid = g_variant_new_from_data(
3765 G_VARIANT_TYPE_BYTESTRING,
3771 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
3773 data_char_uuid = g_variant_new_from_data(
3774 G_VARIANT_TYPE_BYTESTRING,
3779 param = g_variant_new("(isn@ayin@ayi)", result,
3783 event_data->srvc_id.id.inst_id,
3786 event_data->char_id.inst_id);
3789 _bt_send_event(BT_GATT_CLIENT_EVENT,
3790 BLUETOOTH_EVENT_GATT_WRITE_CHAR,
3799 /* Send DBUS return */
3800 __bt_gatt_handle_pending_request_info(result,
3801 BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE,
3803 sizeof(bluetooth_gatt_client_char_prop_info_t));
3807 static void __bt_handle_client_descriptor_write_data(
3808 event_gattc_write_data *event_data)
3810 int result = BLUETOOTH_ERROR_NONE;
3811 struct gatt_server_info_t *conn_info = NULL;
3812 bluetooth_gatt_client_desc_prop_info_t write_info;
3814 /* Write Information data structures */
3815 GVariant *param = NULL;
3816 GVariant *data_svc_uuid = NULL;
3817 GVariant *data_char_uuid = NULL;
3818 GVariant *data_desc_uuid = NULL;
3819 char *svc_uuid = NULL;
3820 char *char_uuid = NULL;
3821 char *desc_uuid = NULL;
3825 memset(&write_info, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
3827 /* Extract Address from conn_id of event data */
3828 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3829 event_data->conn_status.conn_id);
3831 if (NULL == conn_info) {
3833 BT_INFO("Failed to get the conn info for conn_id [%d]", event_data->conn_status.conn_id);
3837 BT_INFO("Descriptor Write callback from addr [%s] status [%d]",
3838 conn_info->addr, event_data->conn_status.status);
3840 /* Fill descriptor informations in buffer */
3841 memcpy(&write_info.descriptor.uuid,
3842 event_data->descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3843 write_info.descriptor.instance_id = event_data->descr_id.inst_id;
3845 /* Fill Characteristic informations in buffer */
3846 memcpy(&write_info.characteristic.uuid,
3847 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3848 write_info.characteristic.instance_id = event_data->char_id.inst_id;
3850 /* Fill Service informations in buffer */
3851 memcpy(&write_info.svc.uuid,
3852 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3853 write_info.svc.instance_id = event_data->srvc_id.id.inst_id;
3855 /* Fill remote device address */
3856 _bt_convert_addr_string_to_type(write_info.device_address.addr, conn_info->addr);
3858 if (event_data->conn_status.status != OAL_STATUS_SUCCESS) {
3859 result = BLUETOOTH_ERROR_INTERNAL;
3865 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
3867 data_svc_uuid = g_variant_new_from_data(
3868 G_VARIANT_TYPE_BYTESTRING,
3874 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
3876 data_char_uuid = g_variant_new_from_data(
3877 G_VARIANT_TYPE_BYTESTRING,
3883 desc_uuid = g_memdup(&event_data->descr_id.uuid.uuid[0], uuid_len);
3885 data_desc_uuid = g_variant_new_from_data(
3886 G_VARIANT_TYPE_BYTESTRING,
3891 param = g_variant_new("(isn@ayin@ayin@ayi)", result,
3895 event_data->srvc_id.id.inst_id,
3898 event_data->char_id.inst_id,
3901 event_data->descr_id.inst_id);
3904 _bt_send_event(BT_GATT_CLIENT_EVENT,
3905 BLUETOOTH_EVENT_GATT_WRITE_DESC,
3916 /* Send DBUS return */
3917 __bt_gatt_handle_pending_request_info(result,
3918 BT_GATT_WRITE_DESCRIPTOR_VALUE,
3920 sizeof(bluetooth_gatt_client_desc_prop_info_t));
3923 static void __bt_hanlde_le_device_disconnection(event_dev_conn_status_t *event_data)
3925 int result = BLUETOOTH_ERROR_INTERNAL;
3926 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
3928 _bt_convert_addr_type_to_string(address, (unsigned char *)event_data->address.addr);
3930 /* DBUS Return with fail of pending BT_CONNECT_LE for all the apps */
3931 BT_INFO("Local GATT Client disconnected: Remote addr[%s] ", address);
3933 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE, address,
3934 BT_ADDRESS_STRING_SIZE);
3938 static void __bt_handle_client_notification_registered(
3939 event_gattc_regdereg_notify_t *event_data,
3940 gboolean is_registered)
3942 int result = BLUETOOTH_ERROR_NONE;
3943 struct gatt_server_info_t *conn_info = NULL;
3944 bt_gatt_notif_reg_info_t notif_info;
3947 memset(¬if_info, 0x00, sizeof(bt_gatt_notif_reg_info_t));
3949 BT_INFO("Client Interface [%d] status [%d]",
3950 event_data->client_if,
3951 event_data->status);
3953 /* Extract Address from conn_id of event data */
3954 conn_info = __bt_find_remote_gatt_server_info_from_client_if(
3955 event_data->client_if);
3958 BT_INFO("Connection Info is not present, return");
3961 BT_INFO("Notification Registered for addr [%s]",
3965 /* Fill svc informations in buffer */
3966 memcpy(¬if_info.svc_uuid,
3967 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3968 notif_info.svc_inst = event_data->srvc_id.id.inst_id;
3970 /* Fill char in buffer */
3971 memcpy(¬if_info.char_uuid,
3972 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3973 notif_info.char_inst = event_data->char_id.inst_id;
3975 /* Fill remote device address */
3976 _bt_convert_addr_string_to_type(notif_info.addr.addr, conn_info->addr);
3978 notif_info.is_registered = is_registered;
3980 if (event_data->status != OAL_STATUS_SUCCESS)
3981 result = BLUETOOTH_ERROR_INTERNAL;
3983 /* Send DBUS Return for BT_GATT_WATCH_CHARACTERISTIC */
3984 __bt_gatt_handle_pending_request_info(result,
3985 BT_GATT_WATCH_CHARACTERISTIC,
3987 sizeof(bt_gatt_notif_reg_info_t));
3990 static void __bt_handle_client_notification_data(event_gattc_notify_data *event_data)
3992 /* No status in this event from OAL */
3993 int result = BLUETOOTH_ERROR_NONE;
3995 /* Read Information data structures */
3996 GVariant *param = NULL;
3997 GVariant *data = NULL;
3998 GVariant *data_svc_uuid = NULL;
3999 GVariant *data_char_uuid = NULL;
4000 char *read_val = NULL;
4001 char *svc_uuid = NULL;
4002 char *char_uuid = NULL;
4008 BT_INFO("Notifcation of charc data changed");
4010 if (event_data->data_len > 0) {
4012 for (i = 0; i < event_data->data_len; i++)
4013 BT_INFO("Data[%d] = [0x%x]", i, event_data->data[i]);
4016 addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
4017 _bt_convert_addr_type_to_string(addr,
4018 (unsigned char *)&(event_data->address.addr));
4021 read_val = g_memdup(&event_data->data[0], event_data->data_len);
4023 data = g_variant_new_from_data(
4024 G_VARIANT_TYPE_BYTESTRING,
4026 event_data->data_len,
4029 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
4031 data_svc_uuid = g_variant_new_from_data(
4032 G_VARIANT_TYPE_BYTESTRING,
4038 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
4040 data_char_uuid = g_variant_new_from_data(
4041 G_VARIANT_TYPE_BYTESTRING,
4047 param = g_variant_new("(isn@ayin@ayin@ay)", result,
4051 event_data->srvc_id.id.inst_id,
4054 event_data->char_id.inst_id,
4055 event_data->data_len,
4059 _bt_send_event(BT_GATT_CLIENT_EVENT,
4060 BLUETOOTH_EVENT_GATT_CHAR_VAL_CHANGED,
4063 BT_ERR("No Data!!");
4076 static void __bt_handle_client_service_changed_ind(event_gattc_service_changed_data *event_data)
4078 bt_gatt_service_info_list_t *svc_info_list;
4080 bt_gatt_service_info_t *svc_info;
4081 GVariant *param = NULL;
4082 char *address_str = NULL;
4083 char *uuid_str = NULL;
4085 svc_info_list = __bt_get_service_info_list(event_data->conn_id);
4086 if (svc_info_list == NULL) {
4087 BT_ERR("svc_info_list is NULL");
4091 if (event_data->change_type) {
4092 /* Add service UUID in list */
4093 svc_info = g_malloc0(sizeof(bt_gatt_service_info_t));
4094 memcpy(svc_info->uuid, event_data->uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4095 svc_info->inst_id = event_data->inst_id;
4096 svc_info->is_primary = 1; // TODO: Need to check is_primary is required or not
4097 svc_info_list->services = g_slist_append(svc_info_list->services, svc_info);
4098 BT_DBG("Service created and added in Svc info list or connection ID %d", event_data->conn_id);
4100 /* Remove service UUID in list */
4101 for (l = svc_info_list->services; l != NULL; l = g_slist_next(l)) {
4102 svc_info = (bt_gatt_service_info_t *)l->data;
4103 if (svc_info == NULL)
4106 if (!memcmp(svc_info->uuid, event_data->uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
4107 svc_info_list->services = g_slist_remove(svc_info_list->services, svc_info);
4108 __bt_free_service_info(svc_info);
4114 address_str = g_malloc0(BT_ADDRESS_STRING_SIZE);
4115 uuid_str = g_malloc0(BT_UUID_STRING_MAX);
4116 _bt_convert_addr_type_to_string(address_str, event_data->address.addr);
4117 _bt_uuid_to_string(&event_data->uuid, uuid_str);
4119 param = g_variant_new("(iiss)", event_data->inst_id, event_data->change_type, address_str, uuid_str);
4121 _bt_send_event(BT_GATT_CLIENT_EVENT,
4122 BLUETOOTH_EVENT_GATT_CLIENT_SERVICE_CHANGED,
4124 g_free(address_str);
4128 gboolean _bt_is_remote_gatt_device_connected(bluetooth_device_address_t *address)
4131 struct gatt_server_info_t *conn_info = NULL;
4132 gboolean connected = FALSE;
4134 addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
4135 _bt_convert_addr_type_to_string(addr,
4136 (unsigned char *)&(address->addr));
4138 BT_DBG("Check GATT connection status of [%s]", addr);
4139 /* Check if device is already in connected list */
4140 conn_info = _bt_find_remote_gatt_server_info(addr);
4143 BT_DBG("Remote GATT Server device [%s] is Connected", conn_info->addr);
4146 struct gatt_client_info_t *client_info = NULL;
4148 BT_INFO("Remote GATT Server Device [%s] is not Connected", addr);
4150 /* Check if device is already in connected list */
4151 client_info = _bt_find_remote_gatt_client_info(addr);
4154 BT_DBG("Remote Client device [%s] is Connected", client_info->addr);
4157 BT_INFO("Remote GATT Client Device [%s] is not Connected", addr);
4166 int _bt_connect_le_device(bluetooth_device_address_t *address,
4167 int auto_connect, int client_id)
4169 struct gatt_server_info_t *conn_info = NULL;
4170 struct gatt_out_conn_info_t *out_conn_info = NULL;
4172 invocation_info_t *req_info = NULL;
4173 int ret = OAL_STATUS_SUCCESS;
4175 char *remote_address = NULL;
4177 BT_CHECK_PARAMETER(address, return);
4179 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4180 _bt_convert_addr_type_to_string(addr, address->addr);
4181 BT_INFO("GATT Client connect request for address [%s] client instance [%d]",
4185 /* Check if Remote Device is already under connection progress */
4186 req_info = _bt_get_request_info_data_from_function_name(BT_CONNECT_LE);
4188 remote_address = (char*)req_info->user_data;
4189 if (remote_address && !strcasecmp(remote_address, addr)) {/* Address matched */
4190 BT_INFO("Already Connection ongoing for same remote GATT Server address [%s]", remote_address);
4191 /* Return and wait for events to be sent to all apps */
4193 return BLUETOOTH_ERROR_IN_PROGRESS;
4197 /* Check if remote GATT Server is connected or not */
4198 conn_info = _bt_find_remote_gatt_server_info(addr);
4200 BT_ERR("GATT Server is already connected..");
4202 return BLUETOOTH_ERROR_ALREADY_CONNECT;
4205 /* TODO Check Requirement of holding Advertisement before initiating LE connect */
4207 /* Check if app sent 0 client id for connection, in such case, use default gatt client ID */
4208 if (client_id == 0) {
4209 BT_INFO("GATT CLient connect request sent by an app without any client instance [%d]",
4211 BT_INFO("Assign default GATT client id [%d]", gatt_default_client);
4212 client_id = gatt_default_client;
4215 BT_INFO("Connect using CLient ID [%d]", client_id);
4216 ret = gattc_connect(client_id, (bt_address_t*)(address), auto_connect);
4218 if (ret != OAL_STATUS_SUCCESS) {
4219 BT_ERR("ret: %d", ret);
4221 return BLUETOOTH_ERROR_INTERNAL;
4224 /* Mark this as outgoing connection */
4225 out_conn_info = g_new0(struct gatt_out_conn_info_t, 1);
4226 out_conn_info->addr = g_strdup(addr);
4227 out_conn_info->client_id = client_id;
4228 BT_INFO("Added outgoing connection info addr[%s]", out_conn_info->addr);
4229 outgoing_gatt_conn_list = g_slist_append(outgoing_gatt_conn_list, out_conn_info);
4232 return BLUETOOTH_ERROR_NONE;
4235 int _bt_gatt_get_primary_services(char *address)
4237 BT_CHECK_PARAMETER(address, return);
4238 struct gatt_server_info_t *conn_info = NULL;
4239 invocation_info_t *req_info = NULL;
4240 int ret = OAL_STATUS_SUCCESS;
4243 /* Check if any app is already browsing primary services on the same remote GATT Server */
4244 req_info = _bt_get_request_info_data(BT_GATT_GET_PRIMARY_SERVICES, address);
4246 BT_INFO("Already Primary Service Browsing ongoing for same rmeote GATT Server");
4247 /* Return and wait for events to be sent to all apps */
4248 return BLUETOOTH_ERROR_NONE;
4251 /* Check if remote GATT Server is connected or not */
4252 conn_info = _bt_find_remote_gatt_server_info(address);
4254 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4255 conn_info->addr, conn_info->connection_id);
4257 BT_ERR("GATT Server is not yet connected..");
4258 return BLUETOOTH_ERROR_NOT_CONNECTED;
4261 BT_INFO("Remove GATT server info from List..");
4262 /* Remove all services from info list_gatt_info */
4263 __bt_cleanup_remote_services(conn_info);
4265 /* Send Primary Service Browsing request to stack */
4266 ret = gattc_search_service(conn_info->connection_id, NULL);
4267 if (ret != OAL_STATUS_SUCCESS) {
4268 BT_ERR("ret: %d", ret);
4269 return BLUETOOTH_ERROR_INTERNAL;
4271 return BLUETOOTH_ERROR_NONE;
4274 int _bt_gatt_get_all_characteristic(bluetooth_gatt_client_svc_prop_info_t *svc)
4276 BT_CHECK_PARAMETER(svc, return);
4277 struct gatt_server_info_t *conn_info = NULL;
4278 invocation_info_t *req_info = NULL;
4279 bluetooth_gatt_client_svc_prop_info_t *prop;
4280 oal_gatt_srvc_id_t srvc_id;
4281 int ret = OAL_STATUS_SUCCESS;
4284 /* Check if any app is already browsing characteristics of the same service on the same remote GATT Server */
4285 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_GET_SERVICE_PROPERTIES);
4287 prop = (bluetooth_gatt_client_svc_prop_info_t*)req_info->user_data;
4288 if (prop && !memcmp(svc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t))
4289 && memcmp(prop->svc.uuid, svc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
4290 && prop->svc.instance_id == svc->svc.instance_id) {
4291 BT_INFO("Already Properties browsing for Primary Service ongoing for same remote GATT Server");
4292 /* Return and wait for events to be sent to all apps */
4293 return BLUETOOTH_ERROR_NONE;
4297 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4298 _bt_convert_addr_type_to_string(addr, svc->device_address.addr);
4300 /* Check if remote GATT Server is connected or not */
4301 conn_info = _bt_find_remote_gatt_server_info(addr);
4303 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4304 conn_info->addr, conn_info->connection_id);
4306 BT_ERR("GATT Server is not yet connected..");
4308 return BLUETOOTH_ERROR_NOT_CONNECTED;
4311 srvc_id.is_prmry = TRUE;
4312 srvc_id.id.inst_id = svc->svc.instance_id;
4313 memcpy(srvc_id.id.uuid.uuid, svc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4315 /* Search All Characteristic */
4316 ret = gattc_get_characteristic(conn_info->connection_id, &srvc_id, NULL);
4317 if (ret != OAL_STATUS_SUCCESS) {
4318 BT_ERR("ret: %d", ret);
4320 return BLUETOOTH_ERROR_INTERNAL;
4323 return BLUETOOTH_ERROR_NONE;
4326 int _bt_gatt_get_all_characteristic_properties(
4327 bluetooth_gatt_client_char_prop_info_t *chr)
4329 struct gatt_server_info_t *conn_info = NULL;
4330 invocation_info_t *req_info = NULL;
4331 bluetooth_gatt_client_char_prop_info_t *prop;
4332 oal_gatt_srvc_id_t srvc_id;
4333 oal_gatt_id_t char_id;
4334 int ret = OAL_STATUS_SUCCESS;
4337 BT_CHECK_PARAMETER(chr, return);
4339 /* Check if any app is already browsing descriptors of the same char of
4340 particular service on the same remote GATT Server */
4341 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_GET_CHARACTERISTIC_PROPERTIES);
4343 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4344 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4345 && memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4346 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4347 && memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4348 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4349 BT_INFO("Already Properties browsing for Characteristic ongoing for same remote GATT Server");
4350 /* Return and wait for events to be sent to all apps */
4351 return BLUETOOTH_ERROR_NONE;
4355 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4356 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4358 /* Check if remote GATT Server is connected or not */
4359 conn_info = _bt_find_remote_gatt_server_info(addr);
4361 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4362 conn_info->addr, conn_info->connection_id);
4364 BT_ERR("GATT Server is not yet connected..");
4366 return BLUETOOTH_ERROR_NOT_CONNECTED;
4369 srvc_id.is_prmry = TRUE;
4370 srvc_id.id.inst_id = chr->svc.instance_id;
4371 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4373 char_id.inst_id = chr->characteristic.instance_id;
4374 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4376 /* Search All Descriptors */
4377 ret = gattc_get_descriptor(conn_info->connection_id, &srvc_id, &char_id, NULL);
4378 if (ret != OAL_STATUS_SUCCESS) {
4379 BT_ERR("ret: %d", ret);
4381 return BLUETOOTH_ERROR_INTERNAL;
4384 return BLUETOOTH_ERROR_NONE;
4387 int _bt_gatt_read_characteristic_value(
4388 bluetooth_gatt_client_char_prop_info_t *chr)
4390 struct gatt_server_info_t *conn_info = NULL;
4391 invocation_info_t *req_info = NULL;
4392 bluetooth_gatt_client_char_prop_info_t *prop;
4393 oal_gatt_srvc_id_t srvc_id;
4394 oal_gatt_id_t char_id;
4395 int ret = OAL_STATUS_SUCCESS;
4398 BT_CHECK_PARAMETER(chr, return);
4400 /* Check if any app is already Reading characteristic of the same char of
4401 particular service on the same remote GATT Server */
4402 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_READ_CHARACTERISTIC);
4404 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4405 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4406 && memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4407 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4408 && memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4409 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4410 BT_INFO("Already Characteristic value Read operation in progress for same remote GATT Server");
4411 /* Return and wait for events to be sent to all apps */
4412 return BLUETOOTH_ERROR_NONE;
4416 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4417 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4419 /* Check if remote GATT Server is connected or not */
4420 conn_info = _bt_find_remote_gatt_server_info(addr);
4422 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4423 conn_info->addr, conn_info->connection_id);
4425 BT_ERR("GATT Server is not yet connected..");
4427 return BLUETOOTH_ERROR_NOT_CONNECTED;
4430 srvc_id.is_prmry = TRUE;
4431 srvc_id.id.inst_id = chr->svc.instance_id;
4432 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4434 char_id.inst_id = chr->characteristic.instance_id;
4435 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4437 /* Search All Descriptors */
4438 ret = gattc_read_characteristic(conn_info->connection_id, &srvc_id, &char_id, OAL_GATT_AUTH_REQ_NONE);
4439 if (ret != OAL_STATUS_SUCCESS) {
4440 BT_ERR("ret: %d", ret);
4442 return BLUETOOTH_ERROR_INTERNAL;
4445 return BLUETOOTH_ERROR_NONE;
4448 int _bt_gatt_read_descriptor_value(
4449 bluetooth_gatt_client_desc_prop_info_t *desc)
4451 struct gatt_server_info_t *conn_info = NULL;
4452 invocation_info_t *req_info = NULL;
4453 bluetooth_gatt_client_desc_prop_info_t *prop;
4454 oal_gatt_srvc_id_t srvc_id;
4455 oal_gatt_id_t char_id;
4456 oal_gatt_id_t desc_id;
4457 int ret = OAL_STATUS_SUCCESS;
4460 BT_CHECK_PARAMETER(desc, return);
4462 /* Check if any app is already Reading descriptors of the same char of
4463 particular service on the same remote GATT Server */
4464 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_READ_DESCRIPTOR_VALUE);
4466 prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
4467 if (prop && !memcmp(desc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4468 && memcmp(desc->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4469 && desc->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4470 && memcmp(desc->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4471 && desc->characteristic.instance_id == prop->characteristic.instance_id /* Characteristic Instance ID matched */
4472 && memcmp(desc->descriptor.uuid, prop->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Descriptor UUID matched */
4473 && desc->descriptor.instance_id == prop->descriptor.instance_id) { /* Descriptor Instance ID matched */
4474 BT_INFO("Already Descriptor value Read operation in progress for same remote GATT Server");
4475 /* Return and wait for events to be sent to all apps */
4476 return BLUETOOTH_ERROR_NONE;
4480 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4481 _bt_convert_addr_type_to_string(addr, desc->device_address.addr);
4483 /* Check if remote GATT Server is connected or not */
4484 conn_info = _bt_find_remote_gatt_server_info(addr);
4486 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4487 conn_info->addr, conn_info->connection_id);
4489 BT_ERR("GATT Server is not yet connected..");
4491 return BLUETOOTH_ERROR_NOT_CONNECTED;
4494 srvc_id.is_prmry = TRUE;
4495 srvc_id.id.inst_id = desc->svc.instance_id;
4496 memcpy(srvc_id.id.uuid.uuid, desc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4498 char_id.inst_id = desc->characteristic.instance_id;
4499 memcpy(char_id.uuid.uuid, desc->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4501 desc_id.inst_id = desc->descriptor.instance_id;
4502 memcpy(desc_id.uuid.uuid, desc->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4504 /* Search All Descriptors */
4505 ret = gattc_read_descriptor(conn_info->connection_id, &srvc_id, &char_id,
4506 &desc_id, OAL_GATT_AUTH_REQ_NONE);
4507 if (ret != OAL_STATUS_SUCCESS) {
4508 BT_ERR("ret: %d", ret);
4510 return BLUETOOTH_ERROR_INTERNAL;
4513 return BLUETOOTH_ERROR_NONE;
4517 int _bt_gatt_acquire_notify(bluetooth_gatt_client_char_prop_info_t *chr, int *fd, int *mtu)
4519 struct gatt_server_info_t *conn_info = NULL;
4520 oal_gatt_srvc_id_t srvc_id;
4521 oal_gatt_id_t char_id;
4522 int ret = OAL_STATUS_SUCCESS;
4525 BT_CHECK_PARAMETER(chr, return);
4527 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4528 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4530 /* Check if remote GATT Server is connected or not */
4531 conn_info = _bt_find_remote_gatt_server_info(addr);
4533 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4534 conn_info->addr, conn_info->connection_id);
4536 BT_ERR("GATT Server is not yet connected..");
4538 return BLUETOOTH_ERROR_NOT_CONNECTED;
4541 srvc_id.is_prmry = TRUE;
4542 srvc_id.id.inst_id = chr->svc.instance_id;
4543 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4545 char_id.inst_id = chr->characteristic.instance_id;
4546 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4548 ret = gattc_acquire_notify(conn_info->connection_id, &srvc_id, &char_id, fd, mtu);
4550 if (ret != OAL_STATUS_SUCCESS) {
4551 BT_ERR("ret: %d", ret);
4553 return BLUETOOTH_ERROR_INTERNAL;
4555 BT_INFO("GATT characterstics FD [%d] mtu[%d]", *fd, *mtu);
4557 return BLUETOOTH_ERROR_NONE;
4561 int _bt_gatt_acquire_write(bluetooth_gatt_client_char_prop_info_t *chr, int *fd, int *mtu)
4564 struct gatt_server_info_t *conn_info = NULL;
4565 oal_gatt_srvc_id_t srvc_id;
4566 oal_gatt_id_t char_id;
4567 int ret = OAL_STATUS_SUCCESS;
4570 BT_CHECK_PARAMETER(chr, return);
4572 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4573 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4575 /* Check if remote GATT Server is connected or not */
4576 conn_info = _bt_find_remote_gatt_server_info(addr);
4578 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4579 conn_info->addr, conn_info->connection_id);
4581 BT_ERR("GATT Server is not yet connected..");
4583 return BLUETOOTH_ERROR_NOT_CONNECTED;
4586 srvc_id.is_prmry = TRUE;
4587 srvc_id.id.inst_id = chr->svc.instance_id;
4588 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4590 char_id.inst_id = chr->characteristic.instance_id;
4591 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4593 ret = gattc_acquire_write(conn_info->connection_id, &srvc_id, &char_id,
4594 OAL_GATT_AUTH_REQ_NONE, fd, mtu);
4595 if (ret != OAL_STATUS_SUCCESS) {
4596 BT_ERR("ret: %d", ret);
4598 return BLUETOOTH_ERROR_INTERNAL;
4600 BT_INFO("GATT characterstics FD [%d] mtu [%d]", *fd, *mtu);
4602 return BLUETOOTH_ERROR_NONE;
4607 /* Write Characteristic */
4608 int _bt_gatt_write_characteristic_value_by_type(
4609 bluetooth_gatt_client_char_prop_info_t *chr,
4610 bluetooth_gatt_att_data_t *data,
4611 bluetooth_gatt_write_type_e write_type)
4613 struct gatt_server_info_t *conn_info = NULL;
4614 invocation_info_t *req_info = NULL;
4615 bluetooth_gatt_client_char_prop_info_t *prop;
4616 oal_gatt_srvc_id_t srvc_id;
4617 oal_gatt_id_t char_id;
4618 int ret = OAL_STATUS_SUCCESS;
4622 BT_CHECK_PARAMETER(chr, return);
4623 BT_CHECK_PARAMETER(data, return);
4627 /* Check if any app is already writing same char of
4628 particular service on the same remote GATT Server */
4629 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE);
4631 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4632 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4633 && memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4634 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4635 && memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4636 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4637 BT_INFO("Already Characteristic Write Value operation in progress for same remote GATT Server");
4638 /* Return and wait for events to be sent to all apps */
4639 return BLUETOOTH_ERROR_NONE;
4643 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4644 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4646 /* Check if remote GATT Server is connected or not */
4647 conn_info = _bt_find_remote_gatt_server_info(addr);
4649 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4650 conn_info->addr, conn_info->connection_id);
4652 BT_ERR("GATT Server is not yet connected..");
4654 return BLUETOOTH_ERROR_NOT_CONNECTED;
4657 srvc_id.is_prmry = TRUE;
4658 srvc_id.id.inst_id = chr->svc.instance_id;
4659 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4661 char_id.inst_id = chr->characteristic.instance_id;
4662 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4664 /* Write CHar value */
4666 BT_INFO("Connection ID [%d] write type [%d] data length [%d]", conn_info->connection_id, write_type, data->length);
4667 for (k = 0; k < data->length; k++)
4668 BT_INFO("Data[%d] [0x%x]", k, data->data[k]);
4670 ret = gattc_write_characteristic(conn_info->connection_id,
4672 (oal_gatt_write_type_t)write_type, data->length,
4673 OAL_GATT_AUTH_REQ_NONE, (char *)(&data->data[0]));
4674 if (ret != OAL_STATUS_SUCCESS) {
4675 BT_ERR("ret: %d", ret);
4677 return BLUETOOTH_ERROR_INTERNAL;
4680 return BLUETOOTH_ERROR_NONE;
4683 /* Write Descriptor */
4684 int _bt_gatt_write_descriptor_value_by_type(
4685 bluetooth_gatt_client_desc_prop_info_t *desc,
4686 bluetooth_gatt_att_data_t *data,
4687 bluetooth_gatt_write_type_e write_type)
4689 struct gatt_server_info_t *conn_info = NULL;
4690 invocation_info_t *req_info = NULL;
4691 bluetooth_gatt_client_desc_prop_info_t *prop;
4692 oal_gatt_srvc_id_t srvc_id;
4693 oal_gatt_id_t char_id;
4694 oal_gatt_id_t desc_id;
4695 int ret = OAL_STATUS_SUCCESS;
4699 BT_CHECK_PARAMETER(desc, return);
4700 BT_CHECK_PARAMETER(data, return);
4704 /* Check if any app is already writing on same Descriptor of the same char of
4705 particular service on the same remote GATT Server */
4706 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_WRITE_DESCRIPTOR_VALUE);
4708 prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
4709 if (prop && !memcmp(desc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4710 && memcmp(desc->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4711 && desc->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4712 && memcmp(desc->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4713 && desc->characteristic.instance_id == prop->characteristic.instance_id /* Characteristic Instance ID matched */
4714 && memcmp(desc->descriptor.uuid, prop->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Descriptor UUID matched */
4715 && desc->descriptor.instance_id == prop->descriptor.instance_id) { /* Descriptor Instance ID matched */
4716 BT_INFO("Already Descriptor value Write operation in progress for same remote GATT Server");
4717 /* Return and wait for events to be sent to all apps */
4718 return BLUETOOTH_ERROR_NONE;
4722 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4723 _bt_convert_addr_type_to_string(addr, desc->device_address.addr);
4725 /* Check if remote GATT Server is connected or not */
4726 conn_info = _bt_find_remote_gatt_server_info(addr);
4728 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4729 conn_info->addr, conn_info->connection_id);
4731 BT_ERR("GATT Server is not yet connected..");
4733 return BLUETOOTH_ERROR_NOT_CONNECTED;
4736 srvc_id.is_prmry = TRUE;
4737 srvc_id.id.inst_id = desc->svc.instance_id;
4738 memcpy(srvc_id.id.uuid.uuid, desc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4740 char_id.inst_id = desc->characteristic.instance_id;
4741 memcpy(char_id.uuid.uuid, desc->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4743 desc_id.inst_id = desc->descriptor.instance_id;
4744 memcpy(desc_id.uuid.uuid, desc->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4747 BT_INFO("Connection ID [%d] write type [%d] data length [%d]", conn_info->connection_id, write_type, data->length);
4748 for (k = 0; k < data->length; k++)
4749 BT_INFO("Data[%d] [0x%x]", k, data->data[k]);
4751 ret = gattc_write_descriptor(conn_info->connection_id,
4752 &srvc_id, &char_id, &desc_id,
4753 (oal_gatt_write_type_t)write_type, data->length,
4754 OAL_GATT_AUTH_REQ_NONE, (char *)(&data->data[0]));
4755 if (ret != OAL_STATUS_SUCCESS) {
4756 BT_ERR("ret: %d", ret);
4758 return BLUETOOTH_ERROR_INTERNAL;
4761 return BLUETOOTH_ERROR_NONE;
4764 int _bt_gatt_watch_characteristic(
4765 bluetooth_gatt_client_char_prop_info_t *chr,
4769 struct gatt_server_info_t *conn_info = NULL;
4770 oal_gatt_srvc_id_t srvc_id;
4771 oal_gatt_id_t char_id;
4772 int ret = OAL_STATUS_SUCCESS;
4775 BT_CHECK_PARAMETER(chr, return);
4777 BT_INFO("Client ID [%d] Is Notify [%d]", client_id, is_notify);
4779 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4780 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4782 /* Check if remote GATT Server is connected or not */
4783 conn_info = _bt_find_remote_gatt_server_info(addr);
4785 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4786 conn_info->addr, conn_info->connection_id);
4788 BT_ERR("GATT Server is not yet connected..");
4790 return BLUETOOTH_ERROR_NOT_CONNECTED;
4792 srvc_id.is_prmry = TRUE;
4793 srvc_id.id.inst_id = chr->svc.instance_id;
4794 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4796 char_id.inst_id = chr->characteristic.instance_id;
4797 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4799 /* Register or unregister Notification characteristic */
4801 ret = gattc_register_for_notification(conn_info->client_id,
4802 (bt_address_t*)&(chr->device_address),
4803 &srvc_id, &char_id);
4805 ret = gattc_deregister_for_notification(conn_info->client_id,
4806 (bt_address_t*)&(chr->device_address),
4807 &srvc_id, &char_id);
4809 BT_INFO("Result[%d]", ret);
4810 if (ret != OAL_STATUS_SUCCESS) {
4811 BT_ERR("ret: %d", ret);
4813 return BLUETOOTH_ERROR_INTERNAL;
4816 return BLUETOOTH_ERROR_NONE;
4820 int _bt_disconnect_le_device(bluetooth_device_address_t *address,
4823 struct gatt_server_info_t *conn_info = NULL;
4824 struct gatt_client_info_t *rem_client_conn_info = NULL;
4825 invocation_info_t *req_info = NULL;
4826 int ret = OAL_STATUS_SUCCESS;
4828 char *remote_address = NULL;
4830 BT_CHECK_PARAMETER(address, return);
4834 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4835 _bt_convert_addr_type_to_string(addr, address->addr);
4836 BT_INFO("GATT Client Disconnect request for address [%s]", addr);
4838 /* Check if Remote Device is already under connection progress */
4839 req_info = _bt_get_request_info_data_from_function_name(BT_DISCONNECT_LE);
4841 remote_address = (char*)req_info->user_data;
4842 if (remote_address && !strcasecmp(remote_address, addr)) {/* Address matched */
4843 BT_INFO("Already DisConnection ongoing for same remote GATT Server address [%s]", remote_address);
4844 /* Return success and wait for events to be sent to all apps */
4846 return BLUETOOTH_ERROR_IN_PROGRESS;
4849 /* Check if remote GATT Server is connected or not */
4850 conn_info = _bt_find_remote_gatt_server_info(addr);
4852 /* Check if app sent 0 client id for Disconnection, in such case, use default gatt client ID */
4853 if (client_id == 0) {
4854 BT_INFO("GATT CLient Disconnect request sent by an app without any client instance [%d]",
4856 BT_INFO("Assign default GATT client id [%d]", gatt_default_client);
4857 client_id = gatt_default_client;
4860 BT_INFO("Disconnect remote gatt server using CLient ID [%d] Connection ID [%d]", client_id, conn_info->connection_id);
4861 ret = gattc_disconnect(client_id, (bt_address_t*)(address),
4862 conn_info->connection_id);
4864 /* check if remote client is connected */
4865 rem_client_conn_info = _bt_find_remote_gatt_client_info(addr);
4867 if (!rem_client_conn_info || client_id != 0) {
4868 BT_ERR("GATT device is not connected..");
4870 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
4873 BT_INFO("Disconnect remote gatt client ");
4875 ret = gatts_disconnect(rem_client_conn_info->instance_id,
4876 (bt_address_t*)(address), rem_client_conn_info->connection_id);
4879 if (ret != OAL_STATUS_SUCCESS) {
4880 BT_ERR("ret: %d", ret);
4882 return BLUETOOTH_ERROR_INTERNAL;
4885 return BLUETOOTH_ERROR_NONE;
4888 int _bt_gatt_watch_service_changed_indication(const char *sender,
4889 bluetooth_device_address_t *address,
4890 gboolean is_enabled)
4893 bt_service_app_info_t *info = NULL;
4895 BT_INFO("%s Servic changed Indication watcher for app [%s]",
4896 is_enabled ? "Enable":"Disable", sender);
4898 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
4901 if (g_strcmp0(sender, info->sender) == 0 &&
4902 memcmp(info->address.addr, address->addr,
4903 sizeof(bluetooth_device_address_t)) == 0) {
4904 BT_DBG("Found GATT client App.. [%s], sender [%s]", info->uuid, info->sender);
4905 info->is_watcher_enabled = is_enabled;
4909 return BLUETOOTH_ERROR_NONE;
4912 int _bt_unregister_gatt_client_instance(const char *sender, int client_id)
4914 bt_service_app_info_t *info = NULL;
4917 BT_INFO("Unregister Allocated GATT Client instance [%s] Client ID [%d]", sender, client_id);
4919 /* Unregister CLient instance associated with address X. It is possible that another app still
4920 has client_id valid for same remote address */
4921 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
4924 /* Exact matching of sender */
4925 if (!g_strcmp0(info->sender, sender) && info->client_id == client_id) { /* Check for only valid GATT client Instance */
4926 BT_INFO("Unregister GATT client instance [%d]", info->client_id);
4927 numapps[k].client_id = -1;
4928 numapps[k].is_initialized = FALSE;
4929 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
4930 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
4931 memset(&numapps[k].address.addr, 0x00, sizeof(bluetooth_device_address_t));
4933 /* Its a GATT Client Instance */
4934 ret = gattc_deregister(client_id);
4935 if (ret != OAL_STATUS_SUCCESS) {
4936 BT_ERR("DeAllocate GATT Client instance with stack Fail ret: %d", ret);
4937 return BLUETOOTH_ERROR_INTERNAL;
4939 return BLUETOOTH_ERROR_NONE;
4944 return BLUETOOTH_ERROR_NOT_FOUND;
4947 static void __bt_handle_client_mtu_exchange_completed(event_gattc_mtu_configured_t *event_data)
4949 int result = BLUETOOTH_ERROR_NONE;
4950 struct gatt_server_info_t *conn_info = NULL;
4951 GVariant *param = NULL;
4955 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(event_data->conn_id);
4956 if (conn_info == NULL) {
4957 BT_ERR("Cant find connection Information");
4961 BT_INFO("GATT Client: MTU Configured from addr [%s] status [%d] MTU size [%d]",
4962 conn_info->addr, event_data->status, event_data->mtu);
4964 if (event_data->status != OAL_STATUS_SUCCESS)
4965 result = BLUETOOTH_ERROR_INTERNAL;
4967 BT_INFO("reply BT_REQ_ATT_MTU ");
4969 /* DBUS Return fo BT_REQ_ATT_MTU for all the apps */
4970 __bt_gatt_handle_pending_request_info(result, BT_REQ_ATT_MTU, conn_info->addr,
4971 BT_ADDRESS_STRING_SIZE);
4973 if (result == BLUETOOTH_ERROR_NONE) {
4974 BT_INFO("sending mtu event");
4976 mtu = event_data->mtu;
4977 param = g_variant_new("(isqy)",
4983 /* Send event to BT-API */
4984 _bt_send_event(BT_DEVICE_EVENT,
4985 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
4988 /* Update the MTU for current connection */
4989 BT_INFO("Updating the MTU for current connection");
4990 __bt_update_mtu_gatt_device(conn_info->addr, event_data->mtu);
4994 static struct gatt_mtu_info_t *__bt_find_mtu_gatt_device(char *address)
4997 struct gatt_mtu_info_t *info = NULL;
5001 for (l = gatt_mtu_info_list; l != NULL; l = g_slist_next(l)) {
5002 info = (struct gatt_mtu_info_t*)l->data;
5006 if (!g_strcmp0(info->addr, address)) {
5007 BT_INFO("Remote GATT device found addr[%s]", info->addr);
5012 BT_INFO("Not found Remote GATT device addr[%s]", address);
5016 static void __bt_remove_mtu_gatt_device(char *address)
5018 struct gatt_mtu_info_t *dev_info = NULL;
5022 dev_info = __bt_find_mtu_gatt_device(address);
5025 BT_INFO("removing the gatt device from mtu list");
5026 gatt_mtu_info_list = g_slist_remove(gatt_mtu_info_list, dev_info);
5027 g_free(dev_info->addr);
5032 static void __bt_add_mtu_gatt_device(char *address)
5034 struct gatt_mtu_info_t *dev_info = NULL;
5038 dev_info = __bt_find_mtu_gatt_device(address);
5041 BT_INFO("adding the gatt device in mtu list");
5042 dev_info = g_new0(struct gatt_mtu_info_t, 1);
5043 dev_info->addr = g_strdup(address);
5044 dev_info->att_mtu = BT_DEFAULT_ATT_MTU;
5045 gatt_mtu_info_list = g_slist_append(gatt_mtu_info_list, dev_info);
5049 static void __bt_update_mtu_gatt_device(char *address, int mtu)
5051 struct gatt_mtu_info_t *dev_info = NULL;
5055 dev_info = __bt_find_mtu_gatt_device(address);
5058 BT_INFO("Udating the mtu");
5059 dev_info->att_mtu = mtu;