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);
242 #ifdef TIZEN_GATT_CLIENT
243 static void __bt_handle_client_instance_registered(event_gattc_register_t *data);
244 static void __bt_handle_client_connected(event_gattc_conn_t *event_data);
245 static void __bt_handle_client_disconnected(event_gattc_conn_t *event_data);
246 static void __bt_handle_client_service_search_completed(event_gattc_conn_status_t *event_data);
247 static void __bt_handle_client_service_search_result(event_gattc_service_result_t *event_data);
248 static void __bt_handle_client_characteristic_search_result(
249 event_gattc_characteristic_result_t *event_data);
250 static void __bt_handle_client_descriptor_search_result(event_gattc_descriptor_result_t *event_data);
251 static void __bt_handle_client_characteristic_read_data(event_gattc_read_data *event_data);
252 static void __bt_handle_client_descriptor_read_data(event_gattc_read_data *event_data);
253 static void __bt_handle_client_characteristic_write_data(event_gattc_write_data *event_data);
254 static void __bt_handle_client_descriptor_write_data(event_gattc_write_data *event_data);
255 static void __bt_hanlde_le_device_disconnection(event_dev_conn_status_t *event_data);
256 static void __bt_handle_client_notification_registered(event_gattc_regdereg_notify_t *event_data,
257 gboolean is_registered);
258 static void __bt_handle_client_notification_data(event_gattc_notify_data *event_data);
259 static void __bt_handle_client_service_changed_ind(event_gattc_service_changed_data *event_data);
260 static void __bt_handle_client_mtu_exchange_completed(event_gattc_mtu_configured_t *event_data);
263 static int __bt_unregister_gatt_server_instance(int server_instance);
266 struct gatt_mtu_info_t {
267 char *addr; /* Remote GATT Server address */
271 static GSList *gatt_mtu_info_list = NULL;
273 static struct gatt_mtu_info_t *__bt_find_mtu_gatt_device(char *address);
274 static void __bt_remove_mtu_gatt_device(char *address);
275 static void __bt_add_mtu_gatt_device(char *address);
276 static void __bt_update_mtu_gatt_device(char *address, int mtu);
278 /* Linked List of GATT requests from Remote GATT Clients */
279 static GSList *gatt_server_requests = NULL;
281 /* Linked List of connected Remote GATT clients */
282 static GSList *gatt_client_info_list = NULL;
284 /* Number of clients to be notified to */
285 static int num_indicate_clients;
287 /* List of applications */
288 static bt_service_app_info_t numapps[MAX_APPS_SUPPORTED];
290 static void __bt_gatt_handle_pending_request_info(int result,
291 int service_function, void *data, unsigned int size);
293 static void __bt_handle_server_instance_registered(event_gatts_register_t *data);
295 static void __bt_gatt_event_handler(int event_type, gpointer event_data);
297 void _bt_check_adv_app_termination(const char *name)
299 bt_service_app_info_t *app = NULL;
301 int apps[MAX_APPS_SUPPORTED];
303 ret_if(NULL == name);
305 memset(&apps, -1, sizeof(apps));
307 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
310 /* Search for a app which has same sender and stop adv is running */
311 if (!strncasecmp(app->sender, name, strlen(name)) && app->is_initialized == TRUE) {
312 BT_DBG("Match found, name: %s", name);
314 /* Unregister all service handles with stack */
315 __bt_remove_all_service_handles(app->instance_id);
317 /* If Advertising is enabled, stop it */
318 if (app->adv_handle > 0) {
319 BT_INFO("Stop advertising on instance ID [%d]", app->instance_id);
320 /* Disable adv if running */
321 BT_INFO("Disable Advertising Adv Handle [%d] sender [%s]",
322 app->adv_handle, name);
323 _bt_set_advertising(app->sender, app->adv_handle, FALSE, FALSE);
326 /* Save instances of all apps that need to be unregistered */
327 apps[app->instance_id] = 1;
331 /* Unregister all apps one by one */
332 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
334 BT_INFO("Unregister app[%d]", k);
335 /* Unregister server instance */
336 __bt_unregister_gatt_server_instance(k);
341 static int __bt_gatt_send_indication_to_all_connected_clients(bluetooth_gatt_att_data_t *data,
342 bluetooth_gatt_server_indication_params_t *param)
345 int ret = OAL_STATUS_SUCCESS;
346 int result = OAL_STATUS_INTERNAL_ERROR;
348 BT_INFO("Current total number of connected clients [%d]", g_slist_length(gatt_client_info_list));
349 for (l = gatt_client_info_list; l != NULL; l = l->next) {
350 struct gatt_client_info_t *info = l->data;
353 BT_INFO("GATT Remote client address [%s] connection Id [%d]", info->addr, info->connection_id);
355 ret = gatts_send_indication(param->instance_id, param->atrribute_handle,
356 info->connection_id, data->length,
357 param->need_confirmation, (char *)(&data->data[0]));
359 BT_INFO("Send Indication to GATT client [%s] result: [%d]", info->addr, ret);
360 if (ret == OAL_STATUS_SUCCESS) {
361 BT_INFO("Send Indication sent successfully to GATT client [%s]", info->addr);
363 num_indicate_clients++;
367 BT_INFO("Indication sending done for total number of clients [%d]", num_indicate_clients);
371 static struct gatt_server_req_info *__bt_gatt_server_find_request_info(int request_id,
372 bluetooth_gatt_att_request_type_e req_type)
376 for (l = gatt_server_requests; l != NULL; l = l->next) {
377 struct gatt_server_req_info *req_info = l->data;
379 if (req_info && req_info->request_id == request_id && req_info->request_type == req_type) {
380 BT_INFO("GATT Server request info found Req ID [%d] handle [%d] conn ID [%d]",
381 req_info->request_id, req_info->attribute_handle, req_info->connection_id);
385 BT_ERR("Gatt Request not found");
389 void _bt_get_adv_handle_from_instance(int server_inst, int *adv_handle)
393 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
394 if (numapps[k].is_initialized == 1 && numapps[k].instance_id == server_inst) {
395 *adv_handle = numapps[k].adv_handle;
401 #ifdef TIZEN_GATT_CLIENT
402 char * _bt_gatt_get_default_gatt_client_uuid(void)
404 return g_strdup(DEFAULT_GATT_CLIENT_UUID);
408 static void __bt_register_default_gatt_client()
413 uuid_str = _bt_gatt_get_default_gatt_client_uuid();
414 _bt_string_to_uuid(uuid_str, (service_uuid_t*)&uuid);
416 BT_INFO("Register Default GATT client uuid [%s]", uuid_str);
418 if (OAL_STATUS_SUCCESS != gattc_register(&uuid)) /* for only Smart Control */
419 BT_ERR("gattc register failed");
425 int _bt_gatt_init(void)
429 if (OAL_STATUS_SUCCESS != gatt_enable()) {
430 BT_ERR("gatt Init failed");
431 return BLUETOOTH_ERROR_INTERNAL;
434 /* Register gatt event handler */
435 _bt_service_register_event_handler_callback(BT_GATT_MODULE, __bt_gatt_event_handler);
436 #ifdef TIZEN_GATT_CLIENT
437 /*In bluedroid product, defacult gatt client is handled differently*/
438 __bt_register_default_gatt_client();
441 return BLUETOOTH_ERROR_NONE;
444 static void __bt_service_reset_gatt_data()
448 BT_INFO("Rest numapp");
451 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
452 numapps[k].is_initialized = 0;
453 numapps[k].instance_id = -1;
454 numapps[k].adv_handle = 0;
455 numapps[k].adv_instance = -1;
456 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
457 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
458 memset(numapps[k].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
459 memset(numapps[k].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
460 numapps[k].adv_data_len = 0;
461 numapps[k].scan_rsp_len = 0;
465 void _bt_gatt_deinit(void)
467 BT_INFO("GATT deinit");
469 if (OAL_STATUS_SUCCESS != gatt_disable())
470 BT_ERR("gatt deinit failed");
472 /* Un-register gatt event handler */
473 _bt_service_unregister_event_handler_callback(BT_GATT_MODULE);
475 __bt_service_reset_gatt_data();
478 void _bt_update_adv_handle(const char *sender, int adv_handle)
481 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
482 bt_service_app_info_t *info = NULL;
484 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
486 /* Search for a app which has same sender and adv handle as 0 */
487 if (!g_strcmp0(info->sender, sender) && info->adv_handle == 0)
488 info->adv_handle = adv_handle;
492 int _bt_is_sender_gatt_server_with_no_adv(const char *sender, int adv_handle)
495 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
496 bt_service_app_info_t *info = NULL;
498 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
500 /* Search for a app which has same sender and adv handle as 0
501 It is possible that same sender but different adv handle */
502 if (!g_strcmp0(info->sender, sender) && info->adv_handle == 0) {
503 //info->adv_handle = adv_handle;
504 return info->instance_id;
510 int _bt_get_allocated_server_instance(const char *sender, int adv_handle, gboolean use_reserved_slot)
513 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
514 bt_service_app_info_t *info = NULL;
516 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
519 /* Exact matching of Adv handle + sender combination */
520 if (!g_strcmp0(info->sender, sender) && info->adv_handle == adv_handle)
521 return info->instance_id;
523 if (!g_strcmp0(info->sender, sender) && info->adv_handle == -1)
524 return info->instance_id;
530 char * _bt_get_sender_and_handle(int server_instance, int *adv_handle)
533 bt_service_app_info_t *info = NULL;
535 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
538 if (info->instance_id == server_instance && info->adv_handle != -1) {
539 *adv_handle = info->adv_handle;
540 BT_DBG("Server instance [%d] Adv handle [%d] Sender [%s]", server_instance, *adv_handle, info->sender);
541 return g_strdup(info->sender);
547 void _bt_set_new_adv_data(bluetooth_advertising_data_t *adv, int len, int instance)
551 bt_service_app_info_t *info = NULL;
552 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
555 if (info->instance_id == instance) {
556 memcpy(info->adv_data.data, &adv->data, len);
562 void _bt_set_new_scan_rsp_data(bluetooth_scan_resp_data_t *scan, int len, int instance)
566 bt_service_app_info_t *info = NULL;
567 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
570 if (info->instance_id == instance) {
571 memcpy(info->scan_rsp.data, &scan->data, len);
577 void _bt_get_previous_adv_data(bluetooth_advertising_data_t *adv, int *len, int instance)
580 bt_service_app_info_t *info = NULL;
582 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
585 if (info->instance_id == instance) {
586 memcpy(&adv->data, info->adv_data.data, info->adv_data_len);
587 *len = info->adv_data_len;
593 void _bt_get_previous_scan_rsp_data(bluetooth_scan_resp_data_t *scan, int *len, int instance)
597 bt_service_app_info_t *info = NULL;
599 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
602 if (info->instance_id == instance) {
603 memcpy(&scan->data, info->scan_rsp.data, info->scan_rsp_len);
604 *len = info->scan_rsp_len;
610 static int __bt_unregister_gatt_server_instance(int server_instance)
612 int ret = OAL_STATUS_SUCCESS;
615 /* Unregister the server instance */
616 ret = gatts_unregister(server_instance);
617 if (ret != OAL_STATUS_SUCCESS) {
618 BT_ERR("DeAllocate server instance with stack Fail ret: %d", ret);
619 return BLUETOOTH_ERROR_INTERNAL;
621 BT_INFO("DeAllocated server instance with stack successful..");
624 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
625 if (numapps[k].instance_id == server_instance) {
626 numapps[k].is_initialized = 0;
627 numapps[k].instance_id = -1;
628 numapps[k].adv_handle = 0;
629 numapps[k].adv_instance = -1;
630 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
631 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
632 memset(numapps[k].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
633 memset(numapps[k].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
634 numapps[k].adv_data_len = 0;
635 numapps[k].scan_rsp_len = 0;
639 BT_DBG("Going8 to reset numapp block num [%d]", k);
640 return BLUETOOTH_ERROR_NONE;
643 static void __bt_remove_all_service_handles(int instance_id)
647 int ret = OAL_STATUS_SUCCESS;
650 count = g_slist_length(numapps[instance_id].service_handles);
651 BT_INFO("Before handle removal: current count [%d]", count);
653 for (l = numapps[instance_id].service_handles; l != NULL;) {
658 BT_INFO("Server Handle to be Removed [%d] Instance ID [%d]", *handle, instance_id);
659 if (_bt_gatt_server_stop_service(numapps[instance_id].sender, *handle, instance_id) != BLUETOOTH_ERROR_NONE)
662 ret = gatts_delete_service(instance_id, *handle);
663 if (ret != OAL_STATUS_SUCCESS) {
664 BT_ERR("ret: %d", ret);
667 numapps[instance_id].service_handles = g_slist_remove(numapps[instance_id].service_handles, handle);
670 count = g_slist_length(numapps[instance_id].service_handles);
671 BT_INFO("After deleting current count [%d]", count);
677 int _bt_unregister_server_instance(const char *sender, int adv_handle)
679 BT_INFO("Unregister Allocated server instance request Sender [%s] Adv handle [%d]", sender, adv_handle);
680 int result = BLUETOOTH_ERROR_NONE;
681 int apps[MAX_APPS_SUPPORTED];
685 memset(&apps, -1, sizeof(apps));
687 if (adv_handle == 0) {
688 BT_DBG("Its a direct GATT Server app request to unregister");
689 /* Unregister server instance for each app with same sender (case: GATT Server with multiple adv handle) */
690 bt_service_app_info_t *info = NULL;
692 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
695 /* Exact matching of sender */
696 if (!g_strcmp0(info->sender, sender)) {
697 BT_INFO("Unregister GATT server instance [%d]", info->instance_id);
698 /* Unregister all service handles with stack */
699 __bt_remove_all_service_handles(info->instance_id);
701 /* Disable adv if running */
702 BT_INFO("Disable Advertising Adv Handle [%d] sender [%s]",
703 info->adv_handle, sender);
704 _bt_set_advertising(sender, info->adv_handle, FALSE, FALSE);
706 /* Save all instances which need to be unregistered */
707 apps[info->instance_id] = 1;
711 BT_DBG("Its an Internal unregister request by adv application");
712 server_instance = _bt_get_allocated_server_instance(sender, adv_handle, FALSE);
713 BT_DBG("Its an Internal unregister request by adv application: Adv disabled srv instance [%d]", server_instance);
714 if (server_instance == -1) {
715 BT_ERR("No allocated server instance to be removed");
716 return BLUETOOTH_ERROR_INVALID_PARAM;
718 if (!numapps[server_instance].service_handles) {
719 BT_INFO("There are no Service handles with this app, safe to unregister");
720 /* Unregister server instance only if this sender does not have any gatt services in it */
721 result = __bt_unregister_gatt_server_instance(server_instance);
723 numapps[server_instance].adv_handle = 0;
724 memset(numapps[server_instance].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
725 numapps[server_instance].adv_data_len = 0;
726 memset(numapps[server_instance].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
727 numapps[server_instance].scan_rsp_len = 0;
732 /* Unregister all apps one by one */
733 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
735 BT_INFO("Unregister app[%d]", k);
736 /* Unregister server instance */
737 __bt_unregister_gatt_server_instance(k);
744 int _bt_register_server_instance(const char *sender, int adv_handle)
746 int ret = OAL_STATUS_SUCCESS;
747 char *uuid_string = NULL;
752 BT_INFO("###Check on which instance Server instance can be initialized....");
753 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
754 if (numapps[k].is_initialized == 1) {
755 BT_INFO("Instance ID [%d] is already in use..Check next slot", numapps[k].instance_id);
758 BT_INFO("Time to register GATT Server..UUID to be used is [%s] slot [%d]", uuid_list[slot-1], slot);
764 BT_ERR("No Slot if free for GATT Server registration..");
765 return BLUETOOTH_ERROR_REGISTRATION_FAILED;
768 uuid_string = g_malloc0(BT_UUID_STRING_MAX);
769 _bt_string_to_uuid(uuid_list[slot-1], (service_uuid_t*)&uuid);
770 g_strlcpy(uuid_string, uuid_list[slot-1], BT_UUID_STRING_MAX);
771 BT_INFO("Copied UUID string [%s]", uuid_string);
773 /* Register GATT Server */
774 ret = gatts_register(&uuid);
775 if (ret != OAL_STATUS_SUCCESS) {
776 BT_ERR("ret: %d", ret);
778 return BLUETOOTH_ERROR_INTERNAL;
780 BT_INFO("GATT Server registration call successfully accepted by OAL..wait for Instance Initialized event from OAL..");
781 /* Return & wait for GATT Server Instance Initialization event */
782 memset(numapps[slot].sender, 0x00, sizeof(numapps[slot].sender));
783 memset(numapps[slot].uuid, 0x00, sizeof(numapps[slot].uuid));
785 g_strlcpy(numapps[slot].sender, sender, sizeof(numapps[slot].sender));
786 g_strlcpy(numapps[slot].uuid, uuid_string, sizeof(numapps[slot].uuid));
788 numapps[slot].is_initialized = 0; /* Set initialization from app registered callback */
789 numapps[slot].adv_handle = adv_handle;
792 return BLUETOOTH_ERROR_NONE;
796 static void __bt_gatt_handle_pending_request_info(int result,
797 int service_function, void *data, unsigned int size)
801 invocation_info_t *req_info = NULL;
802 ret_if(data == NULL);
804 for (l = _bt_get_invocation_list(); l != NULL; ) {
807 if (req_info == NULL || req_info->service_function != service_function)
810 switch (service_function) {
812 case BT_GATT_SERVER_REGISTER: {
813 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
814 BT_INFO("Sender [%s] Request Sender [%s]", param->sender, req_info->sender);
815 if (!g_strcmp0(req_info->sender, param->sender)) {
816 BT_DBG("GATT Server app found [%s]", req_info->sender);
818 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
819 g_array_append_vals(out_param, ¶m->instance_id, sizeof(int));
820 _bt_service_method_return(req_info->context, out_param, result);
822 g_free(req_info->user_data);
823 _bt_free_info_from_invocation_list(req_info);
824 g_array_free(out_param, TRUE);
828 case BT_GATT_SERVER_START_SERVICE:
829 case BT_GATT_SERVER_DELETE_SERVICE: {
830 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
831 BT_INFO("Sender [%s] Request Sender [%s]", param->sender, req_info->sender);
833 int *saved_instance_id = (int*)req_info->user_data;
834 if (!g_strcmp0(req_info->sender, param->sender) && param->instance_id == *saved_instance_id) {
835 BT_DBG("GATT Server app found [%s] Instance ID [%d] Reply DBUS",
836 req_info->sender, *saved_instance_id);
838 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
839 g_array_append_vals(out_param, &saved_instance_id, sizeof(int));
840 _bt_service_method_return(req_info->context, out_param, result);
842 g_free(req_info->user_data);
843 _bt_free_info_from_invocation_list(req_info);
844 g_array_free(out_param, TRUE);
848 case BT_GATT_SERVER_ADD_SERVICE:
849 case BT_GATT_SERVER_ADD_DESCRIPTOR:
850 case BT_GATT_SERVER_ADD_CHARACTERISTIC: {
851 int *handle = (int*)data;
852 BT_INFO("Characteristic added: Handle [%d]", *handle);
853 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
855 g_array_append_vals(out_param, handle, sizeof(int));
856 _bt_service_method_return(req_info->context, out_param, result);
858 g_free(req_info->user_data);
859 _bt_free_info_from_invocation_list(req_info);
860 g_array_free(out_param, TRUE);
865 case BT_DISCONNECT_LE: {
866 #ifdef TIZEN_GATT_CLIENT
867 char *addr = (char*)req_info->user_data;
868 bluetooth_device_address_t address;
870 if (!g_strcmp0(addr, (char*)data)) {
871 BT_INFO("GATT Client connect-disconnect call pending for app [%s] addr [%s]",
872 req_info->sender, addr);
873 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
874 _bt_convert_addr_string_to_type(address.addr, addr);
876 g_array_append_vals(out_param, (bluetooth_device_address_t*)&address,
877 sizeof(bluetooth_device_address_t));
878 _bt_service_method_return(req_info->context, out_param, result);
880 g_free(req_info->user_data);
881 _bt_free_info_from_invocation_list(req_info);
882 g_array_free(out_param, TRUE);
885 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
887 g_array_append_vals(out_param, (bluetooth_device_address_t*)data,
888 sizeof(bluetooth_device_address_t));
889 _bt_service_method_return(req_info->context, out_param, result);
891 g_free(req_info->user_data);
892 _bt_free_info_from_invocation_list(req_info);
894 g_array_free(out_param, TRUE);
898 #ifdef TIZEN_GATT_CLIENT
899 case BT_GATT_CLIENT_REGISTER: {
900 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
901 BT_INFO("Sender [%s] Request Sender [%s]", param->sender, req_info->sender);
902 if (!g_strcmp0(req_info->sender, param->sender)) {
903 BT_DBG("GATT Client app found [%s] created client ID [%d]",
904 req_info->sender, param->client_id);
906 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
907 g_array_append_vals(out_param, ¶m->client_id, sizeof(int));
908 _bt_service_method_return(req_info->context, out_param, result);
910 g_free(req_info->user_data);
911 _bt_free_info_from_invocation_list(req_info);
912 g_array_free(out_param, TRUE);
916 case BT_GATT_GET_PRIMARY_SERVICES: {
917 bt_services_browse_info_t *param = (bt_services_browse_info_t*)data;
918 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
919 _bt_convert_addr_type_to_string(address,
920 (unsigned char *)(¶m->device_addr.addr));
922 BT_INFO("Request Sender [%s] address [%s]", req_info->sender, address);
924 /* Match address to determine same request */
925 if (!g_strcmp0((char*)req_info->user_data, address)) {
926 BT_DBG("GATT Client app found [%s] Remote address [%s]",
927 req_info->sender, address);
929 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
930 g_array_append_vals(out_param, param,
931 sizeof(bt_services_browse_info_t));
933 //g_array_append_vals(out_param, ¶m->client_id, sizeof(int));
934 _bt_service_method_return(req_info->context, out_param, result);
936 g_free(req_info->user_data);
937 _bt_free_info_from_invocation_list(req_info);
938 g_array_free(out_param, TRUE);
943 case BT_GATT_GET_SERVICE_PROPERTIES: {
944 bt_char_browse_info_t param;
945 memcpy((void*)¶m, data, sizeof(bt_char_browse_info_t));
946 //bt_char_browse_info_t *param = (bt_char_browse_info_t*)data;
947 BT_INFO("Request Sender [%s]", req_info->sender);
948 bluetooth_gatt_client_svc_prop_info_t *prop = (bluetooth_gatt_client_svc_prop_info_t*)req_info->user_data;
950 /* Match both address and service properties to determine same request */
951 if (!memcmp(param.device_addr.addr,
952 prop->device_address.addr,
953 sizeof(bluetooth_device_address_t)) &&
954 !memcmp(param.svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
955 param.svc_inst_id == prop->svc.instance_id) {
956 BT_INFO("Returning Service properties");
958 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
959 g_array_append_vals(out_param, ¶m, sizeof(bt_char_browse_info_t));
960 _bt_service_method_return(req_info->context, out_param, result);
962 g_free(req_info->user_data);
963 _bt_free_info_from_invocation_list(req_info);
964 g_array_free(out_param, TRUE);
968 case BT_GATT_GET_CHARACTERISTIC_PROPERTIES: {
969 bt_descriptor_browse_info_t *param = (bt_descriptor_browse_info_t*)data;
971 BT_INFO("Request Sender [%s]", req_info->sender);
972 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
974 /* Match both address, service properties &char properties to determine same request */
975 if (!memcmp(param->device_addr.addr,
976 prop->device_address.addr,
977 sizeof(bluetooth_device_address_t)) &&
978 !memcmp(param->svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
979 param->svc_inst_id == prop->svc.instance_id &&
980 !memcmp(param->char_uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
981 param->char_inst_id == prop->characteristic.instance_id) {
982 BT_INFO("Returning Characteristic properties");
983 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
984 g_array_append_vals(out_param, param, sizeof(bt_descriptor_browse_info_t));
985 _bt_service_method_return(req_info->context, out_param, result);
987 g_free(req_info->user_data);
988 _bt_free_info_from_invocation_list(req_info);
989 g_array_free(out_param, TRUE);
993 case BT_GATT_WATCH_CHARACTERISTIC: {
994 bt_gatt_notif_reg_info_t *param = (bt_gatt_notif_reg_info_t*)data;
995 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
996 BT_INFO("Request Sender [%s]", req_info->sender);
997 /* Match both address, service properties &char properties to determine same request */
998 if (!memcmp(param->addr.addr,
999 prop->device_address.addr,
1000 sizeof(bluetooth_device_address_t)) &&
1001 !memcmp(param->svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1002 param->svc_inst == prop->svc.instance_id &&
1003 !memcmp(param->char_uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1004 param->char_inst == prop->characteristic.instance_id) {
1005 BT_INFO("Characteristic Watch Successful: Is registered [%d]",
1006 param->is_registered);
1007 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1008 g_array_append_vals(out_param, param, sizeof(bt_gatt_notif_reg_info_t));
1009 _bt_service_method_return(req_info->context, out_param, result);
1010 g_free(req_info->user_data);
1011 _bt_free_info_from_invocation_list(req_info);
1012 g_array_free(out_param, TRUE);
1016 case BT_GATT_READ_CHARACTERISTIC:
1017 case BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE: {
1018 bluetooth_gatt_client_char_prop_info_t *param = (bluetooth_gatt_client_char_prop_info_t*)data;
1019 BT_INFO("Request Sender [%s]", req_info->sender);
1021 char *addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
1022 bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
1023 _bt_convert_addr_type_to_string(addr, prop->device_address.addr);
1025 if (!memcmp(param, prop, sizeof(bluetooth_gatt_client_char_prop_info_t))) {
1026 BT_INFO("Gatt Char read or write request matched for address [%s]", addr);
1027 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1028 g_array_append_vals(out_param, param, sizeof(bluetooth_gatt_client_char_prop_info_t));
1029 _bt_service_method_return(req_info->context, out_param, result);
1031 g_free(req_info->user_data);
1032 _bt_free_info_from_invocation_list(req_info);
1033 g_array_free(out_param, TRUE);
1038 case BT_GATT_READ_DESCRIPTOR_VALUE:
1039 case BT_GATT_WRITE_DESCRIPTOR_VALUE: {
1040 bluetooth_gatt_client_desc_prop_info_t *param = (bluetooth_gatt_client_desc_prop_info_t*)data;
1041 BT_INFO("Request Sender [%s]", req_info->sender);
1043 char *addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
1044 bluetooth_gatt_client_desc_prop_info_t *prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
1045 _bt_convert_addr_type_to_string(addr, prop->device_address.addr);
1047 if (!memcmp(param, prop, sizeof(bluetooth_gatt_client_desc_prop_info_t))) {
1048 BT_INFO("Descriptor read or write request matched for address [%s]", addr);
1049 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1050 g_array_append_vals(out_param, param, sizeof(bluetooth_gatt_client_desc_prop_info_t));
1051 _bt_service_method_return(req_info->context, out_param, result);
1053 g_free(req_info->user_data);
1054 _bt_free_info_from_invocation_list(req_info);
1055 g_array_free(out_param, TRUE);
1060 case BT_REQ_ATT_MTU: {
1061 char *addr = (char*)req_info->user_data;
1062 bluetooth_device_address_t address;
1064 if (!g_strcmp0(addr, (char*)data)) {
1065 BT_INFO("GATT Client BT_REQ_ATT_MTU call pending for app [%s] addr [%s]",
1066 req_info->sender, addr);
1067 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1068 _bt_convert_addr_string_to_type(address.addr, addr);
1070 g_array_append_vals(out_param, (bluetooth_device_address_t*)&address,
1071 sizeof(bluetooth_device_address_t));
1072 _bt_service_method_return(req_info->context, out_param, result);
1074 g_free(req_info->user_data);
1075 _bt_free_info_from_invocation_list(req_info);
1076 g_array_free(out_param, TRUE);
1087 static void __bt_handle_server_instance_registered(event_gatts_register_t *data)
1089 bt_service_app_info_t *info = NULL;
1091 char *uuid_string = g_malloc0(BT_UUID_STRING_MAX);
1093 _bt_uuid_to_string(&(data->server_uuid), uuid_string);
1094 BT_INFO("Instance ID is Intialized [%d] UUID initialized [%s]", data->server_inst, uuid_string);
1096 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1097 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1100 if (g_strcmp0(info->uuid, uuid_string) == 0) {
1101 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1102 info->is_initialized = TRUE;
1103 info->instance_id = data->server_inst;
1104 info->adv_instance = data->server_inst;
1105 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE, BT_GATT_SERVER_REGISTER,
1106 (void*)info, sizeof(bt_service_app_info_t));
1110 g_free(uuid_string);
1113 static void __bt_handle_gatt_server_service_added(event_gatts_srvc_prm_t *event)
1115 int result = BLUETOOTH_ERROR_NONE;
1119 bt_service_app_info_t *info = NULL;
1122 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1123 _bt_uuid_to_string(&(event->gatt_srvc_id.id.uuid), uuid_str);
1124 BT_INFO("GATT Added Service UUID: [%s] Server Ins [%d] Service hdl [%d] Is Primary [%d]\n",
1125 uuid_str, event->gatt_srvc_stat.server_inst, svc_handle, event->gatt_srvc_id.is_prmry);
1127 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1128 result = BLUETOOTH_ERROR_INTERNAL;
1129 svc_handle = 0; /* Service handle set to 0 indicates.
1130 0 is reserved by BT SIG, cant be used by app*/
1132 svc_handle = event->gatt_srvc_stat.servic_hndl;
1134 BT_INFO("GATT Added Service Status [%d] Server Ins [%d] Service hdl [%d] Is Primary [%d]\n",
1135 event->gatt_srvc_stat.status, event->gatt_srvc_stat.server_inst, svc_handle, event->gatt_srvc_id.is_prmry);
1137 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1138 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1141 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1142 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1143 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_SERVICE,
1144 (int*)&svc_handle, sizeof(int));
1146 /* Add Service Handle */
1147 if (svc_handle > 0) {
1148 handle = g_malloc0(sizeof(int));
1149 *handle = svc_handle;
1150 numapps[event->gatt_srvc_stat.server_inst].service_handles = g_slist_append(numapps[event->gatt_srvc_stat.server_inst].service_handles, handle);
1151 count = g_slist_length(numapps[event->gatt_srvc_stat.server_inst].service_handles);
1152 BT_INFO("Added Service handle [%d] to list..current count [%d]", svc_handle, count);
1161 static void __bt_handle_gatt_server_characteristic_added(event_gatts_srvc_charctr_t *event)
1163 int result = BLUETOOTH_ERROR_NONE;
1165 bt_service_app_info_t *info = NULL;
1167 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1169 BT_INFO("GATT Server Char added status [%d]", event->gatt_srvc_stat.status);
1170 BT_INFO("GATT Server_inst: [%d]", event->gatt_srvc_stat.server_inst);
1171 BT_INFO("GATT Service Handle: [%d]", event->gatt_srvc_stat.servic_hndl);
1172 BT_INFO("GATT Add characteristic Status: [%d]", event->gatt_srvc_stat.status);
1173 BT_INFO("GATT Service characteristic Handle: [%d]", event->charctr_hndl);
1175 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1176 result = BLUETOOTH_ERROR_INTERNAL;
1177 char_handle = 0; /* characteristic handle set to 0 indicates.
1178 0 is reserved by BT SIG, cant be used by app*/
1180 char_handle = event->charctr_hndl;
1183 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1184 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1187 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1188 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1189 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_CHARACTERISTIC,
1190 (int*)&char_handle, sizeof(int));
1196 _bt_uuid_to_string(&(event->charctr_uuid), uuid_str);
1197 BT_INFO("GATT Added Characteristic: UUID: [%s]", uuid_str);
1202 static void __bt_handle_gatt_server_descriptor_added(event_gatts_srvc_descr_t* event)
1204 int result = BLUETOOTH_ERROR_NONE;
1206 bt_service_app_info_t *info = NULL;
1208 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1210 BT_INFO("GATT Server Descriptor added status [%d]", event->gatt_srvc_stat.status);
1211 BT_INFO("GATT Server_inst: [%d]", event->gatt_srvc_stat.server_inst);
1212 BT_INFO("GATT Service Handle: [%d]", event->gatt_srvc_stat.servic_hndl);
1213 BT_INFO("GATT Add Descriptor Status: [%d]", event->gatt_srvc_stat.status);
1214 BT_INFO("GATT Service Descriptor Handle: [%d]", event->descrptr_hndl);
1216 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1217 result = BLUETOOTH_ERROR_INTERNAL;
1218 desc_handle = 0; /* Service handle set to 0 indicates.
1219 0 is reserved by BT SIG, cant be used by app*/
1221 desc_handle = event->descrptr_hndl;
1223 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1224 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1227 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1228 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1229 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_DESCRIPTOR,
1230 (int*)&desc_handle, sizeof(int));
1235 _bt_uuid_to_string(&(event->descrptr_uuid), uuid_str);
1236 BT_INFO("GATT Added Descriptor: UUID: [%s]", uuid_str);
1241 static void __bt_handle_gatt_server_service_started(event_gatts_srvc_t *event)
1243 bt_service_app_info_t *info = NULL;
1244 int result = BLUETOOTH_ERROR_NONE;
1246 BT_INFO("GATT Server Service Started..");
1249 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1250 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1251 BT_INFO("GATT Service Started Status: [%d]", event->status);
1253 if (event->status != OAL_STATUS_SUCCESS) {
1254 BT_ERR("GATT Server Service Start Failed Err: [%d]", event->status);
1255 result = BLUETOOTH_ERROR_INTERNAL;
1258 /* Check if the just registered Instance ID belongs to requester */
1259 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1262 if (info->instance_id == event->server_inst) {
1263 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1264 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_START_SERVICE,
1265 (void*)info, sizeof(bt_service_app_info_t));
1271 static void __bt_handle_gatt_server_service_stopped(event_gatts_srvc_t *event)
1273 int result = BLUETOOTH_ERROR_NONE;
1274 bt_service_app_info_t *info = NULL;
1276 BT_INFO("GATT Server Service Stopped..");
1278 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1279 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1280 BT_INFO("GATT Service Stopped Status: [%d]", event->status);
1282 if (event->status != OAL_STATUS_SUCCESS) {
1283 BT_ERR("GATT Server Service Stop Failed Err: [%d]", event->status);
1284 result = BLUETOOTH_ERROR_INTERNAL;
1287 /* Check if the just registered Instance ID belongs to requester */
1288 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1291 if (info->instance_id == event->server_inst) {
1292 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1293 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_STOP_SERVICE,
1294 (void*)info, sizeof(bt_service_app_info_t));
1300 static void __bt_handle_gatt_server_service_deleted(event_gatts_srvc_t *event)
1302 int result = BLUETOOTH_ERROR_NONE;
1303 bt_service_app_info_t *info = NULL;
1305 BT_INFO("GATT Server Service Deleted..");
1307 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1308 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1309 BT_INFO("GATT Service Deleted Status: [%d]", event->status);
1311 if (event->status != OAL_STATUS_SUCCESS) {
1312 BT_ERR("GATT Server Service Delete Failed Err: [%d]", event->status);
1313 result = BLUETOOTH_ERROR_INTERNAL;
1316 /* Check if the just registered Instance ID belongs to requester */
1317 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1320 if (info->instance_id == event->server_inst) {
1321 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1322 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_DELETE_SERVICE,
1323 (void*)info, sizeof(bt_service_app_info_t));
1329 struct gatt_client_info_t *_bt_find_remote_gatt_client_info(char *address)
1332 struct gatt_client_info_t *info = NULL;
1333 for (l = gatt_client_info_list; l != NULL; l = g_slist_next(l)) {
1334 info = (struct gatt_client_info_t*)l->data;
1338 if (!g_strcmp0(info->addr, address)) {
1339 BT_DBG("Remote GATT client found addr[%s]", info->addr);
1346 static struct gatt_client_info_t *__bt_find_remote_gatt_client_info_from_conn_id(int conn_id)
1349 struct gatt_client_info_t *info = NULL;
1351 for (l = gatt_client_info_list; l != NULL; l = g_slist_next(l)) {
1352 info = (struct gatt_client_info_t*)l->data;
1356 if (info->connection_id == conn_id) {
1357 BT_INFO("Remote GATT client found addr[%s]", info->addr);
1364 #ifdef TIZEN_GATT_CLIENT
1365 struct gatt_server_info_t *_bt_find_remote_gatt_server_info(char *address)
1368 struct gatt_server_info_t *info = NULL;
1369 for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
1370 info = (struct gatt_server_info_t*)l->data;
1374 if (!g_strcmp0(info->addr, address)) {
1375 BT_DBG("Remote GATT Server found addr[%s]", info->addr);
1382 static struct gatt_out_conn_info_t* __bt_find_gatt_outgoing_conn_info(char *address)
1385 struct gatt_out_conn_info_t *info = NULL;
1386 for (l = outgoing_gatt_conn_list; l != NULL; l = g_slist_next(l)) {
1387 info = (struct gatt_out_conn_info_t*)l->data;
1391 if (!g_strcmp0(info->addr, address)) {
1392 BT_INFO("Outgoing connection info found addr[%s]", info->addr);
1399 static struct gatt_server_info_t *__bt_find_remote_gatt_server_info_from_client_if(int client_if)
1402 struct gatt_server_info_t *info = NULL;
1404 for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
1405 info = (struct gatt_server_info_t*)l->data;
1409 if (info->client_id == client_if) {
1410 BT_INFO("Remote GATT server found addr[%s]", info->addr);
1421 #ifdef TIZEN_GATT_CLIENT
1422 static void __bt_handle_gatt_server_connection_state(event_gatts_conn_t *event)
1424 int result = BLUETOOTH_ERROR_NONE;
1425 struct gatt_client_info_t *client_info = NULL;
1426 struct gatt_server_info_t *server_info = NULL;
1427 bluetooth_device_address_t dev_addr;
1428 GVariant *param = NULL;
1431 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1433 memcpy(dev_addr.addr, event->address.addr, 6);
1435 /* REPLY dbus Context to application which called BT_CONNECT_LE. There is status
1437 _bt_convert_addr_type_to_string(address,
1438 (unsigned char *)dev_addr.addr);
1440 if (event->status != OAL_STATUS_SUCCESS)
1441 result = BLUETOOTH_ERROR_INTERNAL;
1443 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1444 address, BT_ADDRESS_STRING_SIZE);
1446 BT_INFO("GATT Server Connedted: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1447 address, event->server_inst, event->conn_id);
1450 /* Check if device is already in connected list */
1451 client_info = _bt_find_remote_gatt_client_info(address);
1454 BT_INFO("Conn Info absent: But no need to Send Local GATT Server Connected event to apps");
1456 param = g_variant_new("(is)", result, address);
1458 /* Send event to application */
1459 _bt_send_event(BT_DEVICE_EVENT,
1460 BLUETOOTH_EVENT_GATT_SERVER_CONNECTED, /* Local device is GATT server */
1463 /* Save client connection info */
1464 client_info = g_new0(struct gatt_client_info_t, 1);
1465 client_info->addr = g_strdup(address);
1466 BT_INFO("Added GATT client addr[%s]", client_info->addr);
1467 client_info->connection_id = event->conn_id;
1468 client_info->instance_id = event->server_inst;
1469 gatt_client_info_list = g_slist_append(gatt_client_info_list, client_info);
1470 BT_INFO("Total num of connected Remote GATT Clients [%d]", g_slist_length(gatt_client_info_list));
1472 /* Save server connection info */
1473 server_info = g_new0(struct gatt_server_info_t, 1);
1474 server_info->addr = g_strdup(address);
1475 server_info->client_id = -1;
1476 BT_INFO("Added GATT server addr[%s]", server_info->addr);
1477 server_info->connection_id = event->conn_id;
1478 gatt_server_info_list = g_slist_append(gatt_server_info_list, server_info);
1479 BT_INFO("Total num of connected Remote GATT Servers [%d]", g_slist_length(gatt_server_info_list));
1481 ret = gattc_add_connection_info((bt_address_t *)&dev_addr, event->conn_id, event->server_inst);
1482 if (ret != OAL_STATUS_SUCCESS) {
1483 BT_ERR("gattc register server instance failed");
1487 __bt_add_mtu_gatt_device(address);
1492 /* GATT Server Dis connected */
1493 static void __bt_handle_gatt_server_disconnection_state(event_gatts_conn_t *event)
1495 int result = BLUETOOTH_ERROR_NONE;
1496 struct gatt_client_info_t *client_info = NULL;
1497 struct gatt_server_info_t *server_info = NULL;
1498 bluetooth_device_address_t dev_addr;
1499 GVariant *param = NULL;
1500 char address[BT_ADDRESS_STRING_SIZE];
1502 memcpy(dev_addr.addr, event->address.addr, 6);
1504 /* REPLY dbus Context to application which called BT_DISCONNECT_LE. There is status
1506 _bt_convert_addr_type_to_string(address,
1507 (unsigned char *)dev_addr.addr);
1509 if (event->status != OAL_STATUS_SUCCESS)
1510 result = BLUETOOTH_ERROR_INTERNAL;
1512 if (NULL == _bt_get_request_info_data(BT_DISCONNECT_LE, address)) {
1513 if (NULL != _bt_get_request_info_data(BT_CONNECT_LE, address)) {
1514 result = BLUETOOTH_ERROR_INTERNAL;
1516 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1517 address, BT_ADDRESS_STRING_SIZE);
1519 BT_ERR("Failed to connect Local GATT Server Remote Client addr[%s]", address);
1523 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE,
1524 address, BT_ADDRESS_STRING_SIZE);
1527 BT_INFO("Local GATT Server DisConnected: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1528 address, event->server_inst, event->conn_id);
1529 /* Remove Connection info */
1530 client_info = _bt_find_remote_gatt_client_info(address);
1532 BT_INFO("No need to Send Local GATT Server Disconnected event to apps, just remove remote client info");
1534 param = g_variant_new("(is)", result, address);
1535 /* Send event to application */
1536 _bt_send_event(BT_DEVICE_EVENT,
1537 BLUETOOTH_EVENT_GATT_SERVER_DISCONNECTED, /* Local device is GATT server */
1540 /* Remove server info from list */
1541 server_info = _bt_find_remote_gatt_server_info(address);
1543 gatt_server_info_list = g_slist_remove(gatt_server_info_list, server_info);
1545 BT_INFO("Can not find conn info, already removed!");
1547 /* Remove client info from List */
1548 gatt_client_info_list = g_slist_remove(gatt_client_info_list, client_info);
1549 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1550 g_free(client_info->addr);
1551 g_free(client_info);
1554 __bt_remove_mtu_gatt_device(address);
1558 static void __bt_handle_gatt_server_connection_state(event_gatts_conn_t *event)
1560 int result = BLUETOOTH_ERROR_NONE;
1561 struct gatt_client_info_t *conn_info = NULL;
1562 bt_service_app_info_t *info = NULL;
1563 bluetooth_device_address_t dev_addr;
1564 GVariant *param = NULL;
1567 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1569 memcpy(dev_addr.addr, event->address.addr, 6);
1572 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1573 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1576 if (info->instance_id == event->server_inst) {
1577 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1578 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1579 (bluetooth_device_address_t*)&dev_addr, sizeof(bluetooth_device_address_t));
1585 /* Send event to BT-API */
1586 _bt_convert_addr_type_to_string(address,
1587 (unsigned char *)dev_addr.addr);
1589 BT_INFO("GATT Server Connedted: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1590 address, event->server_inst, event->conn_id);
1593 /* Check if device is already in connected list */
1594 conn_info = _bt_find_remote_gatt_client_info(address);
1597 param = g_variant_new("(is)", result, address);
1598 /* Send event to application */
1599 _bt_send_event(BT_GATT_SERVER_EVENT,
1600 BLUETOOTH_EVENT_GATT_CONNECTED,
1603 /* Save Connection info */
1604 conn_info = g_new0(struct gatt_client_info_t, 1);
1605 conn_info->addr = g_strdup(address);
1606 BT_INFO("Added GATT client addr[%s]", conn_info->addr);
1607 conn_info->connection_id = event->conn_id;
1608 conn_info->instance_id = event->server_inst;
1609 gatt_client_info_list = g_slist_append(gatt_client_info_list, conn_info);
1610 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1616 /* GATT Server Dis connected */
1617 static void __bt_handle_gatt_server_disconnection_state(event_gatts_conn_t *event)
1619 int result = BLUETOOTH_ERROR_NONE;
1620 struct gatt_client_info_t *conn_info = NULL;
1621 bluetooth_device_address_t dev_addr;
1622 bt_service_app_info_t *info = NULL;
1623 GVariant *param = NULL;
1626 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1628 memcpy(dev_addr.addr, event->address.addr, 6);
1630 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1631 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1634 if (info->instance_id == event->server_inst) {
1635 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1636 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE,
1637 (bluetooth_device_address_t*)&dev_addr, sizeof(bluetooth_device_address_t));
1643 /* Send event to BT-API */
1644 _bt_convert_addr_type_to_string(address,
1645 (unsigned char *)dev_addr.addr);
1647 BT_INFO("GATT Server DisConnected: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1648 address, event->server_inst, event->conn_id);
1650 param = g_variant_new("(is)", result, address);
1651 /* Send event to application */
1652 _bt_send_event(BT_GATT_SERVER_EVENT,
1653 BLUETOOTH_EVENT_GATT_DISCONNECTED,
1656 /* Remove Connection info */
1657 conn_info = _bt_find_remote_gatt_client_info(address);
1659 BT_INFO("Remove GATT client info from List..");
1660 /* Remove info from List */
1661 gatt_client_info_list = g_slist_remove(gatt_client_info_list, conn_info);
1662 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1663 g_free(conn_info->addr);
1672 static void __bt_handle_gatt_server_acquire_write_requested(event_gatts_srvc_acquire_attr_t *event)
1674 GVariant *param = NULL;
1675 int result = BLUETOOTH_ERROR_NONE;
1676 struct gatt_server_req_info *req_info = NULL;
1677 bluetooth_device_address_t dev_addr;
1678 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1680 BT_INFO("GATT Server ACQUIRE Write Req Connection ID: [%d]", event->attr_trans.conn_id);
1681 BT_INFO("GATT Server ACQUIRE Write Req Transaction ID:[%d]", event->attr_trans.trans_id);
1682 BT_INFO("GATT Server ACQUIRE Write Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
1684 //address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1685 memcpy(dev_addr.addr, event->address.addr, 6);
1687 req_info = g_new0(struct gatt_server_req_info, 1);
1688 req_info->request_id = event->attr_trans.trans_id;
1689 req_info->attribute_handle = event->attr_trans.attr_handle;
1690 req_info->connection_id = event->attr_trans.conn_id;
1691 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_ACQUIRE_WRITE;
1692 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1694 _bt_convert_addr_type_to_string(address,
1695 (unsigned char *)dev_addr.addr);
1697 param = g_variant_new("(iiiiiis)", result,
1698 event->attr_trans.conn_id,
1699 event->attr_trans.trans_id,
1700 event->attr_trans.attr_handle,
1701 event->mtu, event->attr_trans.offset, address);
1702 BT_INFO("GATT Server ACQUIRE Write Req Attribute remote address : [%s]", address);
1704 _bt_send_event(BT_GATT_SERVER_EVENT,
1705 BLUETOOTH_EVENT_GATT_SERVER_ACQUIRE_WRITE,
1710 static void __bt_handle_gatt_server_acquire_notify_requested(event_gatts_srvc_acquire_attr_t *event)
1712 GVariant *param = NULL;
1713 int result = BLUETOOTH_ERROR_NONE;
1714 struct gatt_server_req_info *req_info = NULL;
1715 bluetooth_device_address_t dev_addr;
1716 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1718 BT_INFO("GATT Server ACQUIRE Notify Req Connection ID: [%d]", event->attr_trans.conn_id);
1719 BT_INFO("GATT Server ACQUIRE Notify Req Transaction ID:[%d]", event->attr_trans.trans_id);
1720 BT_INFO("GATT Server ACQUIRE Notify Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
1721 BT_INFO("GATT Server ACQUIRE notify Req address : [%s]", address);
1723 memcpy(dev_addr.addr, event->address.addr, 6);
1724 _bt_convert_addr_type_to_string(address,
1725 (unsigned char *)dev_addr.addr);
1726 BT_INFO("GATT Server ACQUIRE notify Req remote address : [%s]", address);
1728 req_info = g_new0(struct gatt_server_req_info, 1);
1729 req_info->request_id = event->attr_trans.trans_id;
1730 req_info->attribute_handle = event->attr_trans.attr_handle;
1731 req_info->connection_id = event->attr_trans.conn_id;
1732 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_ACQUIRE_NOTIFY;
1733 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1735 param = g_variant_new("(iiiiiis)", result,
1736 event->attr_trans.conn_id,
1737 event->attr_trans.trans_id,
1738 event->attr_trans.attr_handle,
1739 event->mtu, event->attr_trans.offset,
1742 BT_INFO("GATT Server ACQUIRE Notify Req Attribute : ");
1744 _bt_send_event(BT_GATT_SERVER_EVENT,
1745 BLUETOOTH_EVENT_GATT_SERVER_ACQUIRE_NOTIFY,
1749 static void __bt_handle_gatt_server_write_requested(event_gatts_srvc_write_attr_t *event)
1752 bluetooth_device_address_t dev_addr;
1753 GVariant *param = NULL;
1754 int result = BLUETOOTH_ERROR_NONE;
1756 gboolean is_prepare_write;
1758 GVariant *data = NULL;
1760 struct gatt_server_req_info *req_info = NULL;
1761 BT_INFO("GATT Server Write Requested");
1763 memcpy(dev_addr.addr, event->address.addr, 6);
1765 BT_INFO("GATT Server Write Req Connection ID: [%d]", event->attr_trans.conn_id);
1766 BT_INFO("GATT Server Write Req Transaction ID:[%d]", event->attr_trans.trans_id);
1767 BT_INFO("GATT Server Write Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
1768 BT_INFO("GATT Server Write Req Attribute Offset: [%d]", event->attr_trans.offset);
1769 BT_INFO("GATT Server Write Req value length [%d]", event->length);
1770 BT_INFO("GATT Server Write Req needs response: [%d]", event->need_rsp);
1771 BT_INFO("GATT Server Write Req Is Prep: [%d]", event->is_prep);
1773 if (event->length <= 0) {
1774 BT_INFO("GATT Server write requested, but length of attr value is 0");
1778 need_resp = event->need_rsp;
1779 is_prepare_write = event->is_prep;
1781 write_val = g_memdup(&event->value[0], event->length);
1783 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1784 _bt_convert_addr_type_to_string(address,
1785 (unsigned char *)dev_addr.addr);
1787 BT_INFO("GATT Server Write Request from remote client [%s]", address);
1789 if (event->length > 0) {
1790 for (i = 0; i < event->length; i++)
1791 BT_DBG("Data[%d] = [0x%x]", i, event->value[i]);
1793 /* Save Write Request Info */
1794 req_info = g_new0(struct gatt_server_req_info, 1);
1795 req_info->request_id = event->attr_trans.trans_id;
1796 req_info->attribute_handle = event->attr_trans.attr_handle;
1797 req_info->connection_id = event->attr_trans.conn_id;
1798 req_info->addr = address;
1799 req_info->offset = event->attr_trans.offset;
1800 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_WRITE;
1801 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1803 data = g_variant_new_from_data(
1804 G_VARIANT_TYPE_BYTESTRING,
1809 param = g_variant_new("(iiiiiibbsn@ay)", result,
1810 event->attr_trans.conn_id,
1811 event->attr_trans.trans_id,
1812 event->attr_trans.attr_handle,
1813 event->attr_trans.offset,
1821 _bt_send_event(BT_GATT_SERVER_EVENT,
1822 BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED,
1828 static void __bt_handle_gatt_server_read_requested(event_gatts_srvc_read_attr_t *event)
1830 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1831 bluetooth_device_address_t dev_addr;
1832 int result = BLUETOOTH_ERROR_NONE;
1833 struct gatt_server_req_info *req_info = NULL;
1834 GVariant *param = NULL;
1836 BT_INFO("GATT Server Read Requested");
1838 memcpy(dev_addr.addr, event->address.addr, 6);
1840 BT_INFO("GATT Server-Client Connection ID: [%d]", event->attr_trans.conn_id);
1841 BT_INFO("GATT Server-Client Transaction ID: [%d]", event->attr_trans.trans_id);
1842 BT_INFO("GATT Server Attribute Handle: [%d]", event->attr_trans.attr_handle);
1843 BT_INFO("GATT Server Attribute Offset for read: [%d]", event->attr_trans.offset);
1844 BT_INFO("GATT Server Attribute is long: [%d]", event->is_long);
1846 is_long = event->is_long;
1848 /* Save Read Request Info */
1849 req_info = g_new0(struct gatt_server_req_info, 1);
1850 req_info->request_id = event->attr_trans.trans_id;
1851 req_info->attribute_handle = event->attr_trans.attr_handle;
1852 req_info->connection_id = event->attr_trans.conn_id;
1853 req_info->addr = address;
1854 req_info->offset = event->attr_trans.offset;
1855 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_READ;
1856 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1858 /* Send event to BT-API */
1859 _bt_convert_addr_type_to_string(address,
1860 (unsigned char *)dev_addr.addr);
1862 BT_INFO("GATT Server Read Request from remote client [%s]", address);
1864 param = g_variant_new("(iiiiibs)", result,
1865 event->attr_trans.conn_id,
1866 event->attr_trans.trans_id,
1867 event->attr_trans.attr_handle,
1868 event->attr_trans.offset,
1872 _bt_send_event(BT_GATT_SERVER_EVENT,
1873 BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED,
1877 static void __bt_handle_gatt_server_indicate_confirmed(event_gatts_ind_cnfrm_t *event)
1879 bluetooth_device_address_t dev_addr;
1880 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1881 int cur_connected_clients;
1882 static int recvd = 0;
1883 gboolean completed = 0;
1884 GVariant *param = NULL;
1886 /* OAL event does provide error, so MW assumes event will never contain wrong data,
1887 incase of any issues, check with OAL */
1888 int result = BLUETOOTH_ERROR_NONE;
1890 memcpy(dev_addr.addr, event->address.addr, 6);
1891 _bt_convert_addr_type_to_string(address,
1892 (unsigned char *)dev_addr.addr);
1894 BT_INFO("Indication sent to GATT client [%s] conn_ ID [%d] transaction ID [%d] Att handle [%d]",
1895 address, event->conn_id, event->trans_id, event->attr_handle);
1898 cur_connected_clients = g_slist_length(gatt_client_info_list);
1899 BT_INFO("Number of connected clients during sending Indication [%d] & current connected count [%d]",
1900 num_indicate_clients, cur_connected_clients);
1903 if (recvd == num_indicate_clients) {
1904 BT_INFO("Gatt indication confirm event for last GATT client.. [%s]", address);
1905 completed = 1; /* Last event */
1906 recvd = 0; /* Reset */
1907 num_indicate_clients = 0;
1910 param = g_variant_new("(isib)",
1916 /* Send event to BT-API */
1917 _bt_send_event(BT_GATT_SERVER_EVENT,
1918 BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_COMPLETED,
1921 BT_INFO("Received Indication confirm for client number [%d]", recvd);
1925 /* Tizen Platform Specific */
1926 static void __bt_handle_gatt_server_notification_changed(event_gatts_notif_t *event)
1928 bluetooth_device_address_t dev_addr;
1929 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1930 GVariant *param = NULL;
1933 /* OAL event does provide error, so MW assumes event will never contain wrong data,
1934 incase of any issues, check with OAL */
1935 int result = BLUETOOTH_ERROR_NONE;
1937 memcpy(dev_addr.addr, event->address.addr, 6);
1938 _bt_convert_addr_type_to_string(address,
1939 (unsigned char *)dev_addr.addr);
1941 BT_INFO("notification_changed [%s] conn_ ID [%d] transaction ID [%d] Att handle [%d] Notify[%d]",
1942 address, event->conn_id, event->trans_id, event->attr_handle, event->notify);
1944 /* Set Notifcation status */
1945 notify = event->notify;
1947 param = g_variant_new("(isib)",
1953 /* Send event to BT-API */
1954 _bt_send_event(BT_GATT_SERVER_EVENT,
1955 BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_STATE_CHANGED,
1961 static void __bt_handle_gatt_mtu_changed_event(event_gatts_mtu_changed_t *event)
1963 int result = BLUETOOTH_ERROR_NONE;
1964 struct gatt_client_info_t *conn_info = NULL;
1965 GVariant *param = NULL;
1967 BT_INFO("GATT Server MTU changed event");
1969 conn_info = __bt_find_remote_gatt_client_info_from_conn_id(event->conn_id);
1970 if (conn_info == NULL) {
1971 BT_ERR("Cant find connection Information");
1974 BT_INFO("Got connection Info GATT client [%s] MTU Size [%d]",
1975 conn_info->addr, event->mtu_size);
1977 __bt_update_mtu_gatt_device(conn_info->addr, event->mtu_size);
1979 param = g_variant_new("(isqy)",
1985 /* Send event to BT-API */
1986 _bt_send_event(BT_GATT_SERVER_EVENT,
1987 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
1991 static void __bt_gatt_event_handler(int event_type, gpointer event_data)
1993 BT_INFO("OAL event = 0x%x, \n", event_type);
1994 switch (event_type) {
1995 case OAL_EVENT_BLE_SERVER_INSTANCE_INITIALISED: {
1996 BT_INFO("OAL Event: Server Instance Registered");
1997 /* GATT Server Registered event is handled in MAIN thread context */
1998 __bt_handle_server_instance_registered((event_gatts_register_t *)event_data);
2001 case OAL_EVENT_GATTS_SERVICE_ADDED: {
2002 BT_INFO("OAL Event: GATT Service added");
2003 __bt_handle_gatt_server_service_added((event_gatts_srvc_prm_t *)event_data);
2006 case OAL_EVENT_GATTS_CHARACTERISTIC_ADDED: {
2007 BT_INFO("OAL Event: GATT characteristic added");
2008 __bt_handle_gatt_server_characteristic_added((event_gatts_srvc_charctr_t *)event_data);
2011 case OAL_EVENT_GATTS_DESCRIPTOR_ADDED: {
2012 BT_INFO("OAL Event: GATT descriptor added");
2013 __bt_handle_gatt_server_descriptor_added((event_gatts_srvc_descr_t *)event_data);
2016 case OAL_EVENT_GATTS_SERVICE_STARTED: {
2017 BT_INFO("OAL Event: GATT Service started");
2018 __bt_handle_gatt_server_service_started((event_gatts_srvc_t *)event_data);
2021 case OAL_EVENT_GATTS_SERVICE_STOPED: {
2022 BT_INFO("OAL Event: GATT Service stopped");
2023 __bt_handle_gatt_server_service_stopped((event_gatts_srvc_t *)event_data);
2026 case OAL_EVENT_GATTS_SERVICE_DELETED: {
2027 BT_INFO("OAL Event: GATT Service deleted");
2028 __bt_handle_gatt_server_service_deleted((event_gatts_srvc_t *) event_data);
2031 case OAL_EVENT_GATTS_CONNECTION_COMPLETED: {
2032 BT_INFO("OAL Event: GATT Server Connected");
2033 __bt_handle_gatt_server_connection_state((event_gatts_conn_t *)event_data);
2036 case OAL_EVENT_GATTS_DISCONNECTION_COMPLETED: {
2037 BT_INFO("OAL Event: GATT Server Disconnected");
2038 __bt_handle_gatt_server_disconnection_state((event_gatts_conn_t *)event_data);
2041 case OAL_EVENT_GATTS_REQUEST_READ: {
2042 BT_INFO("OAL Event: GATT Server Read Request");
2043 __bt_handle_gatt_server_read_requested((event_gatts_srvc_read_attr_t *)event_data);
2046 case OAL_EVENT_GATTS_REQUEST_WRITE: {
2047 BT_INFO("OAL Event: GATT Server Write Request");
2048 __bt_handle_gatt_server_write_requested((event_gatts_srvc_write_attr_t *)event_data);
2051 case OAL_EVENT_GATTS_REQUEST_ACQUIRE_WRITE: {
2052 BT_INFO("OAL Event: GATT Server Acquire Write Request");
2053 __bt_handle_gatt_server_acquire_write_requested((event_gatts_srvc_acquire_attr_t*)event_data);
2056 case OAL_EVENT_GATTS_REQUEST_ACQUIRE_NOTIFY: {
2057 BT_INFO("OAL Event: GATT ServerAcquire Notify Request");
2058 __bt_handle_gatt_server_acquire_notify_requested((event_gatts_srvc_acquire_attr_t*)event_data);
2061 case OAL_EVENT_GATTS_IND_CONFIRM: {
2062 BT_INFO("OAL Event: GATT Server Indication confirmed");
2063 __bt_handle_gatt_server_indicate_confirmed((event_gatts_ind_cnfrm_t *)event_data);
2066 case OAL_EVENT_GATTS_NOTIFICATION: { /* Tizen Platform Specific */
2067 BT_INFO("OAL Event: GATT Server DisConnected");
2068 __bt_handle_gatt_server_notification_changed((event_gatts_notif_t *)event_data);
2071 case OAL_EVENT_GATTS_MTU_CHANGED: {
2072 BT_INFO("OAL Event: GATT Server MTU changed event callback");
2073 __bt_handle_gatt_mtu_changed_event((event_gatts_mtu_changed_t *)event_data);
2076 #ifdef TIZEN_GATT_CLIENT
2077 case OAL_EVENT_GATTC_REGISTRATION: {
2078 BT_INFO("OAL Event: GATT Client instance Registered");
2079 __bt_handle_client_instance_registered((event_gattc_register_t *) event_data);
2082 case OAL_EVENT_GATTC_CONNECTION_COMPLETED: {
2083 BT_INFO("OAL Event: GATT Client Connected");
2084 __bt_handle_client_connected((event_gattc_conn_t *) event_data);
2087 case OAL_EVENT_GATTC_DISCONNECTION_COMPLETED: {
2088 BT_INFO("OAL Event: GATT Client DisConnected");
2089 __bt_handle_client_disconnected((event_gattc_conn_t *) event_data);
2092 case OAL_EVENT_GATTC_SERVICE_SEARCH_RESULT: {
2093 BT_INFO("OAL Event: GATT Client Service Search Result");
2094 __bt_handle_client_service_search_result((event_gattc_service_result_t *) event_data);
2097 case OAL_EVENT_GATTC_SERVICE_SEARCH_DONE: {
2098 BT_INFO("OAL Event: GATT Client Service Completed");
2099 __bt_handle_client_service_search_completed((event_gattc_conn_status_t *) event_data);
2102 case OAL_EVENT_GATTC_CHARAC_SERACH_RESULT: {
2103 BT_INFO("OAL Event: GATT Client Characteristic Search Result");
2104 __bt_handle_client_characteristic_search_result((event_gattc_characteristic_result_t *) event_data);
2107 case OAL_EVENT_GATTC_DESC_SERACH_RESULT: {
2108 BT_INFO("OAL Event: GATT Client Descriptor Search Result");
2109 __bt_handle_client_descriptor_search_result((event_gattc_descriptor_result_t *) event_data);
2112 case OAL_EVENT_GATTC_READ_CHARAC: {
2113 BT_INFO("OAL Event: GATT Client Characteristic Read Data");
2114 __bt_handle_client_characteristic_read_data((event_gattc_read_data *) event_data);
2117 case OAL_EVENT_GATTC_READ_DESCR: {
2118 BT_INFO("OAL Event: GATT Client Descriptor Read Data");
2119 __bt_handle_client_descriptor_read_data((event_gattc_read_data *) event_data);
2122 case OAL_EVENT_GATTC_WRITE_CHARAC: {
2123 BT_INFO("OAL Event: GATT Client Characteristic Write Data");
2124 __bt_handle_client_characteristic_write_data((event_gattc_write_data *) event_data);
2127 case OAL_EVENT_GATTC_WRITE_DESCR: {
2128 BT_INFO("OAL Event: GATT Client Descriptor Write Data");
2129 __bt_handle_client_descriptor_write_data((event_gattc_write_data *) event_data);
2132 case OAL_EVENT_DEVICE_LE_DISCONNECTED: {
2133 BT_INFO("OAL Event: LE device disconnected");
2134 __bt_hanlde_le_device_disconnection((event_dev_conn_status_t *)event_data);
2137 case OAL_EVENT_GATTC_NOTIFICATION_REGISTERED: {
2138 BT_INFO("OAL Event: GATT Client Notification Registered");
2139 __bt_handle_client_notification_registered((event_gattc_regdereg_notify_t *) event_data, TRUE);
2142 case OAL_EVENT_GATTC_NOTIFICATION_DEREGISTERED: {
2143 BT_INFO("OAL Event: GATT Client Notification Registered");
2144 __bt_handle_client_notification_registered((event_gattc_regdereg_notify_t *) event_data, FALSE);
2147 case OAL_EVENT_GATTC_NOTIFY_DATA: {
2148 BT_INFO("OAL Event: GATT Client Notification Data");
2149 __bt_handle_client_notification_data((event_gattc_notify_data *) event_data);
2152 case OAL_EVENT_GATTC_SERVICE_CHANGED_IND: {
2153 BT_INFO("OAL Event: GATT Client service changed indication");
2154 __bt_handle_client_service_changed_ind((event_gattc_service_changed_data *)event_data);
2157 case OAL_EVENT_GATTC_MTU_EXCHANGE_COMPLETED: {
2158 BT_INFO("OAL Event: GATT Client MTU Exchange Complete");
2159 __bt_handle_client_mtu_exchange_completed((event_gattc_mtu_configured_t *) event_data);
2168 int _bt_gatt_server_add_service(char *sender, int service_type,
2169 int num_handles, char *svc_uuid, int instance_id)
2171 BT_CHECK_PARAMETER(svc_uuid, return);
2172 BT_CHECK_PARAMETER(sender, return);
2173 int ret = OAL_STATUS_SUCCESS;
2175 oal_gatt_srvc_id_t svc_data;
2177 svc_data.is_prmry = service_type;
2178 svc_data.id.inst_id = instance_id;
2180 BT_INFO("Service UUID [%s] Num handles [%d] Instance ID [%d]", svc_uuid, num_handles, instance_id);
2181 _bt_string_to_uuid(svc_uuid, (service_uuid_t*)&svc_data.id.uuid);
2183 ret = gatts_add_service(instance_id, &svc_data, num_handles);
2184 if (ret != OAL_STATUS_SUCCESS) {
2185 BT_ERR("ret: %d", ret);
2186 return BLUETOOTH_ERROR_INTERNAL;
2189 return BLUETOOTH_ERROR_NONE;
2193 int _bt_gatt_server_add_included_service(char *sender, int instance_id,
2194 int service_handle, int included_svc_handle)
2196 BT_CHECK_PARAMETER(sender, return);
2197 int ret = OAL_STATUS_SUCCESS;
2199 ret = gatts_add_included_services(instance_id, service_handle, included_svc_handle);
2200 if (ret != OAL_STATUS_SUCCESS) {
2201 BT_ERR("ret: %d", ret);
2202 return BLUETOOTH_ERROR_INTERNAL;
2204 return BLUETOOTH_ERROR_NONE;
2207 int _bt_gatt_server_add_characteristic(char *sender, char *char_uuid,
2208 bluetooth_gatt_server_attribute_params_t *param)
2210 BT_CHECK_PARAMETER(char_uuid, return);
2211 BT_CHECK_PARAMETER(sender, return);
2212 BT_CHECK_PARAMETER(param, return);
2213 int ret = OAL_STATUS_SUCCESS;
2215 oal_uuid_t uuid = {{0} };
2217 BT_INFO("Char UUID [%s] Instance ID [%d]", char_uuid, param->instance_id);
2218 _bt_string_to_uuid(char_uuid, (service_uuid_t*)&uuid);
2220 BT_INFO("Char permission From API [0x%x]", param->permissions);
2222 ret = gatts_add_characteristics(param->instance_id, param->service_handle, &uuid,
2223 param->properties, (int)param->permissions);
2224 if (ret != OAL_STATUS_SUCCESS) {
2225 BT_ERR("ret: %d", ret);
2226 return BLUETOOTH_ERROR_INTERNAL;
2228 return BLUETOOTH_ERROR_NONE;
2231 int _bt_gatt_server_add_descriptor(char *sender, char *desc_uuid,
2232 bt_gatt_permission_t *param, int service_handle, int instance_id)
2234 BT_CHECK_PARAMETER(desc_uuid, return);
2235 BT_CHECK_PARAMETER(sender, return);
2236 BT_CHECK_PARAMETER(param, return);
2237 int ret = OAL_STATUS_SUCCESS;
2239 oal_uuid_t uuid = {{0} };
2241 BT_INFO("Descriptor UUID [%s] Instance ID [%d] Service handle [%d]",
2242 desc_uuid, service_handle, instance_id);
2244 _bt_string_to_uuid(desc_uuid, (service_uuid_t*)&uuid);
2246 BT_INFO("Descriptor permission From API [0x%x]", *param);
2247 ret = gatts_add_descriptor(instance_id, service_handle, &uuid, (int)*param);
2249 if (ret != OAL_STATUS_SUCCESS) {
2250 BT_ERR("ret: %d", ret);
2251 return BLUETOOTH_ERROR_INTERNAL;
2253 return BLUETOOTH_ERROR_NONE;
2256 int _bt_gatt_server_start_service(char *sender, int service_handle, int instance_id)
2258 BT_CHECK_PARAMETER(sender, return);
2259 int ret = OAL_STATUS_SUCCESS;
2261 ret = gatts_start_service(instance_id, service_handle, BT_GATT_TRANSPORT_LE);
2262 if (ret != OAL_STATUS_SUCCESS) {
2263 BT_ERR("ret: %d", ret);
2264 return BLUETOOTH_ERROR_INTERNAL;
2266 return BLUETOOTH_ERROR_NONE;
2269 int _bt_gatt_server_stop_service(char *sender, int service_handle, int instance_id)
2271 BT_CHECK_PARAMETER(sender, return);
2272 int ret = OAL_STATUS_SUCCESS;
2274 ret = gatts_stop_service(instance_id, service_handle);
2275 if (ret != OAL_STATUS_SUCCESS) {
2276 BT_ERR("ret: %d", ret);
2277 return BLUETOOTH_ERROR_INTERNAL;
2279 return BLUETOOTH_ERROR_NONE;
2282 int _bt_gatt_server_delete_service(char *sender, int service_handle, int instance_id)
2284 BT_CHECK_PARAMETER(sender, return);
2285 int ret = OAL_STATUS_SUCCESS;
2289 ret = gatts_delete_service(instance_id, service_handle);
2290 if (ret != OAL_STATUS_SUCCESS) {
2291 BT_ERR("ret: %d", ret);
2292 return BLUETOOTH_ERROR_INTERNAL;
2295 /* Remove the Service Handle */
2296 for (l = numapps[instance_id].service_handles; l != NULL;) {
2298 l = g_slist_next(l);
2299 if (handle && *handle == service_handle) {
2300 BT_INFO("Remove Service handle [%d]", *handle);
2301 numapps[instance_id].service_handles = g_slist_remove(numapps[instance_id].service_handles, handle);
2307 return BLUETOOTH_ERROR_NONE;
2310 int _bt_gatt_server_send_response(char *sender, bluetooth_gatt_att_data_t *data,
2311 bluetooth_gatt_server_response_params_t *param)
2313 BT_CHECK_PARAMETER(sender, return);
2314 BT_CHECK_PARAMETER(data, return);
2315 BT_CHECK_PARAMETER(param, return);
2316 struct gatt_server_req_info *req_info = NULL;
2317 int ret = OAL_STATUS_SUCCESS;
2319 oal_gatt_response_t response;
2321 BT_INFO("GATT Server Response: Req Type [%d] req_id [%d] status [%d] auth_req [%d] offset[%d] data len[%d]",
2322 param->req_type, param->request_id,
2323 param->response_status, param->auth_req,
2324 data->offset, data->length);
2326 /* Search for matching Request in List */
2327 req_info = __bt_gatt_server_find_request_info(param->request_id, param->req_type);
2329 BT_ERR("GATT Server Req Info not found for current response..return Error");
2330 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2333 memset(&response, 0x00, sizeof(oal_gatt_response_t));
2335 response.handle = req_info->attribute_handle;
2336 response.attr_value.auth_req = param->auth_req;
2337 response.attr_value.handle = req_info->attribute_handle;
2338 response.attr_value.offset = data->offset;
2339 response.attr_value.len = data->length;
2340 memcpy(&response.attr_value.value, &data->data, data->length);
2343 ret = gatts_send_response(req_info->connection_id, param->request_id,
2344 param->response_status, &response);
2346 if (ret != OAL_STATUS_SUCCESS) {
2347 BT_ERR("ret: %d", ret);
2348 return BLUETOOTH_ERROR_INTERNAL;
2351 BT_INFO("GATT Server Response successfully sent");
2352 /* Remove GATT server request from list */
2353 gatt_server_requests = g_slist_remove(gatt_server_requests, req_info);
2354 g_free(req_info->addr);
2356 return BLUETOOTH_ERROR_NONE;
2359 int _bt_gatt_server_acquire_send_response(char *sender, bluetooth_gatt_server_acquire_response_params_t *param , void *fd_list)
2361 BT_CHECK_PARAMETER(sender, return);
2362 BT_CHECK_PARAMETER(param, return);
2363 struct gatt_server_req_info *req_info = NULL;
2364 int ret = OAL_STATUS_SUCCESS;
2367 BT_INFO("GATT acquire Server Response: Req Type [%d] req_id [%d] fd [%d] mtu[%d]",
2368 param->req_type, param->request_id,
2372 /* Search for matching Request in List */
2373 req_info = __bt_gatt_server_find_request_info(param->request_id, param->req_type);
2375 BT_ERR("GATT acquire Server Req Info not found for current response..return Error");
2376 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2379 ret = gatt_send_response_acquire(req_info->connection_id, param->request_id, 0, param->fd, param->mtu, fd_list);
2381 if (ret != OAL_STATUS_SUCCESS) {
2382 BT_ERR("ret: %d", ret);
2383 return BLUETOOTH_ERROR_INTERNAL;
2386 BT_INFO("GATT acquire Server Response successfully sent");
2387 /* Remove GATT server request from list */
2388 gatt_server_requests = g_slist_remove(gatt_server_requests, req_info);
2389 g_free(req_info->addr);
2391 return BLUETOOTH_ERROR_NONE;
2396 int _bt_gatt_server_send_indication(char *sender, bluetooth_device_address_t *dev_addr,
2397 bluetooth_gatt_att_data_t *data,
2398 bluetooth_gatt_server_indication_params_t *param)
2400 BT_CHECK_PARAMETER(sender, return);
2401 BT_CHECK_PARAMETER(data, return);
2402 BT_CHECK_PARAMETER(param, return);
2404 gboolean all_send = FALSE;
2405 int ret = OAL_STATUS_SUCCESS;
2406 struct gatt_client_info_t *conn;
2408 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2409 _bt_convert_addr_type_to_string(address, dev_addr->addr);
2411 if (memcmp(dev_addr->addr, BDADDR_ANY, 6) == 0) {
2412 BT_INFO("GATT Server: Send Indication to all connected GATT clients..");
2415 BT_INFO("GATT Server: Send Indication to connected GATT client addr [%s]", address);
2418 /* Attempt to send Notification/Indication to all Connected GATT clients */
2420 ret = __bt_gatt_send_indication_to_all_connected_clients(data, param);
2421 if (ret != OAL_STATUS_SUCCESS) {
2422 BT_ERR("ret: %d", ret);
2424 return BLUETOOTH_ERROR_INTERNAL;
2428 conn = _bt_find_remote_gatt_client_info(address);
2430 ret = gatts_send_indication(param->instance_id, param->atrribute_handle,
2431 conn->connection_id, data->length,
2432 param->need_confirmation, (char *)(&data->data[0]));
2434 if (ret != OAL_STATUS_SUCCESS) {
2435 BT_ERR("ret: %d", ret);
2436 BT_INFO("Indication failed to send to Remote GATT Client [%s]", address);
2438 return BLUETOOTH_ERROR_INTERNAL;
2440 BT_INFO("Indication sent to Remote GATT Client [%s] wait for Notification completed event from OAL", address);
2442 num_indicate_clients = 1;
2443 return BLUETOOTH_ERROR_NONE;
2445 BT_ERR("Remote GATT client [%s] is not connected..Cant send Indication!!", address);
2447 return BLUETOOTH_ERROR_NOT_CONNECTED;
2450 return BLUETOOTH_ERROR_NONE;
2453 int _bt_gatt_server_update_attribute_value(char *sender, int instance_id,
2454 bluetooth_gatt_server_update_value_t *param)
2456 BT_CHECK_PARAMETER(sender, return);
2457 BT_CHECK_PARAMETER(param, return);
2458 int ret = OAL_STATUS_SUCCESS;
2460 oal_gatt_value_t value;
2461 BT_INFO("GATT Server Update value: Instance ID [%d] attr handle [%d] Value len [%d]",
2462 instance_id, param->attribute_handle, param->length);
2465 memset(&value, 0x00, sizeof(oal_gatt_value_t));
2467 value.handle = param->attribute_handle;
2468 value.len = param->length;
2469 memcpy(&value.value, ¶m->data.data, param->length);
2471 ret = gatts_update_att_value(instance_id, &value);
2473 if (ret != OAL_STATUS_SUCCESS) {
2474 BT_ERR("ret: %d", ret);
2475 return BLUETOOTH_ERROR_INTERNAL;
2478 BT_INFO("GATT Server Update Attribute Value successfully");
2479 return BLUETOOTH_ERROR_NONE;
2482 int _bt_request_att_mtu(bluetooth_device_address_t *device_address,
2485 struct gatt_server_info_t *conn_info = NULL;
2487 int ret = OAL_STATUS_SUCCESS;
2489 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
2490 _bt_convert_addr_type_to_string(addr, device_address->addr);
2492 /* Check if remote GATT Server is connected or not */
2493 conn_info = _bt_find_remote_gatt_server_info(addr);
2495 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
2496 conn_info->addr, conn_info->connection_id);
2498 BT_ERR("GATT Server is not yet connected..");
2500 return BLUETOOTH_ERROR_NOT_CONNECTED;
2503 ret = gattc_configure_mtu(conn_info->connection_id, mtu);
2504 if (ret != OAL_STATUS_SUCCESS) {
2505 BT_ERR("ret: %d", ret);
2507 return BLUETOOTH_ERROR_INTERNAL;
2511 return BLUETOOTH_ERROR_NONE;
2514 int _bt_get_att_mtu(bluetooth_device_address_t *address,
2517 BT_CHECK_PARAMETER(address, return);
2518 BT_CHECK_PARAMETER(mtu, return);
2519 struct gatt_client_info_t *conn_info = NULL;
2520 char addr[BT_ADDRESS_STRING_SIZE] = { 0 };
2521 int ret = OAL_STATUS_SUCCESS;
2524 _bt_convert_addr_type_to_string(addr, address->addr);
2526 BT_INFO("Get current MTU size for the remote client:DevAddress:[%s]", addr);
2528 conn_info = _bt_find_remote_gatt_client_info(addr);
2530 BT_INFO("GATT Client [%s] is connected, conn Id [%d] Instance ID [%d]",
2531 conn_info->addr, conn_info->connection_id, conn_info->instance_id);
2533 BT_ERR("GATT Client [%s] is not yet connected..", addr);
2534 return BLUETOOTH_ERROR_NOT_CONNECTED;
2537 ret = gatts_get_att_mtu(conn_info->connection_id, &stack_mtu);
2538 if (ret != OAL_STATUS_SUCCESS) {
2539 BT_ERR("ret: %d", ret);
2540 return BLUETOOTH_ERROR_INTERNAL;
2543 BT_INFO("ATT MTU received from OAL [%d]", stack_mtu);
2544 *mtu = (unsigned int)stack_mtu;
2547 BT_ERR("MTU value is zero, GATT Client [%s] is not yet connected..", addr);
2548 return BLUETOOTH_ERROR_NOT_CONNECTED;
2551 return BLUETOOTH_ERROR_NONE;
2554 #ifdef TIZEN_GATT_CLIENT
2555 /* GATT Client utility static functions */
2556 static bt_gatt_service_info_list_t *__bt_get_service_info_list(int conn_id)
2559 bt_gatt_service_info_list_t *info = NULL;
2561 for (l = list_gatt_info; l != NULL; l = g_slist_next(l)) {
2562 info = (bt_gatt_service_info_list_t *)l->data;
2566 if (info->conn_id == conn_id)
2573 static bt_gatt_service_info_t *__bt_find_matching_service(
2574 bt_gatt_service_info_list_t *svc_list, oal_gatt_srvc_id_t *svc)
2577 bt_gatt_service_info_t *info = NULL;
2579 for (l = svc_list->services; l != NULL; l = g_slist_next(l)) {
2580 info = (bt_gatt_service_info_t *)l->data;
2584 /* Match UUID and instance ID */
2585 if (!memcmp(&svc->id.uuid.uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2586 && (svc->id.inst_id == info->inst_id)) {
2593 static bt_gatt_char_info_t *__bt_find_matching_charc(
2594 bt_gatt_service_info_t *svc_info, oal_gatt_id_t *charc)
2597 bt_gatt_char_info_t *info = NULL;
2599 for (l = svc_info->chars; l != NULL; l = g_slist_next(l)) {
2600 info = (bt_gatt_char_info_t *)l->data;
2604 /* Match UUID and instance ID */
2605 if (!memcmp(&charc->uuid.uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2606 && (charc->inst_id == info->inst_id)) {
2613 static bt_gatt_descriptor_info_t *__bt_find_matching_desc(
2614 bt_gatt_char_info_t *char_info, oal_gatt_id_t *desc)
2617 bt_gatt_descriptor_info_t *info = NULL;
2619 for (l = char_info->descs; l != NULL; l = g_slist_next(l)) {
2620 info = (bt_gatt_descriptor_info_t *)l->data;
2624 /* Match UUID and instance ID */
2625 if (!memcmp(&desc->uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2626 && (desc->inst_id == info->inst_id)) {
2634 static struct gatt_server_info_t *__bt_find_remote_gatt_server_info_from_conn_id(int conn_id)
2637 struct gatt_server_info_t *info = NULL;
2639 for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
2640 info = (struct gatt_server_info_t*)l->data;
2644 if (info->connection_id == conn_id) {
2645 BT_INFO("Remote GATT server found addr[%s]", info->addr);
2652 static bt_gatt_service_info_t* __bt_find_removed_service(bt_gatt_service_info_list_t *svc_list)
2655 bt_gatt_service_info_t *info = NULL;
2657 for (l = svc_list->services; l != NULL; l = g_slist_next(l)) {
2658 info = (bt_gatt_service_info_t*)l->data;
2662 /* Service is marked a removed */
2663 if (info->is_removed == 1)
2669 static void __bt_remove_service_info_from_list(bt_gatt_service_info_t *svc_info)
2675 bt_gatt_char_info_t *charc = NULL;
2676 bt_gatt_included_service_info_t *incl = NULL;
2677 bt_gatt_descriptor_info_t *desc = NULL;
2679 /* Remove all Characteristic and Descriptors within characteristic */
2680 for (l = svc_info->chars; l != NULL;) {
2681 charc = (bt_gatt_char_info_t*)l->data;
2682 l = g_slist_next(l); /* Incase if l is removed, saving next to l */
2687 /* Inside Characteristic */
2688 for (l1 = charc->descs; l1 != NULL;) {
2690 desc = (bt_gatt_descriptor_info_t*)l1->data;
2691 l1 = g_slist_next(l1);
2696 /* Remove Descriptor */
2697 charc->descs = g_slist_remove(charc->descs, desc);
2700 /* Remove Characteristic */
2701 svc_info->chars = g_slist_remove(svc_info->chars, charc);
2705 /* Remove all Included Services */
2706 for (l2 = svc_info->included_svcs; l2 != NULL;) {
2707 incl = (bt_gatt_included_service_info_t*)l2->data;
2708 l2 = g_slist_next(l2); /* Incase if l is removed, saving next to l */
2713 /* Remove included service */
2714 svc_info->included_svcs = g_slist_remove(svc_info->included_svcs, incl);
2721 static void __bt_build_service_browse_info(int conn_id,
2722 bt_services_browse_info_t* info)
2725 bt_gatt_service_info_list_t *svc_info_list;
2726 bt_gatt_service_info_t *svc_info;
2728 service_uuid_t uuid;
2729 struct gatt_server_info_t *conn_info = NULL;
2731 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2733 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2738 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2740 svc_info_list = __bt_get_service_info_list(conn_id);
2744 info->count = g_slist_length(svc_info_list->services);
2745 BT_DBG("Total services present in the svc info list for this conn id [%d] is [%d]",
2746 conn_id, info->count);
2748 for (l = svc_info_list->services; l != NULL; l = g_slist_next(l)) {
2749 svc_info = (bt_gatt_service_info_t*)l->data;
2750 if (svc_info == NULL)
2753 memcpy(&uuid.uuid, &svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2754 _bt_uuid_to_string(&uuid, uuid_string);
2756 BT_INFO("[%d] %s [%s]", count, uuid_string, _bt_convert_uuid_to_string(uuid_string));
2758 /* Fill UUID of service */
2759 g_strlcpy(info->uuids[count], uuid_string,
2760 BLUETOOTH_UUID_STRING_MAX);
2762 /* Fill instance ID of service */
2763 info->inst_id[count] = svc_info->inst_id;
2765 /* Fill primary service or not info */
2766 info->primary[count] = svc_info->is_primary;
2768 /* Increment count of services browsed */
2773 static void __bt_build_char_browse_info(int conn_id,
2774 bt_gatt_service_info_t *svc_info,
2775 bt_char_browse_info_t* info)
2778 bt_gatt_char_info_t *char_info;
2779 service_uuid_t uuid;
2781 struct gatt_server_info_t *conn_info = NULL;
2783 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2785 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2789 /* Fill default data, this will be required even in case of failure */
2790 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2791 memcpy(&info->svc_uuid, svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2792 info->svc_inst_id = svc_info->inst_id;
2794 if (!svc_info->chars) {
2795 BT_ERR("No Chars browsed for address [%s]", conn_info->addr);
2799 info->count = g_slist_length(svc_info->chars);
2801 for (l = svc_info->chars; l != NULL; l = g_slist_next(l)) {
2802 char_info = (bt_gatt_char_info_t*)l->data;
2803 if (char_info == NULL)
2806 memcpy(&uuid.uuid, &char_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2807 _bt_uuid_to_string(&uuid, uuid_string);
2809 /* Fill UUID of characteristic */
2810 g_strlcpy(info->uuids[count], uuid_string,
2811 BLUETOOTH_UUID_STRING_MAX);
2813 /* Fill instance ID of characteristic */
2814 info->inst_id[count] = char_info->inst_id;
2816 /* Fill property of characteristic */
2817 info->props[count] = char_info->props;
2819 /* Increment count of services browsed */
2822 BT_DBG("Total characteristics browsed [%d]", count);
2825 static void __bt_build_descriptor_browse_info(int conn_id,
2826 bt_gatt_service_info_t *svc_info,
2827 bt_gatt_char_info_t *char_info,
2828 bt_descriptor_browse_info_t* info)
2831 bt_gatt_descriptor_info_t *desc_info;
2833 service_uuid_t uuid;
2834 struct gatt_server_info_t *conn_info = NULL;
2836 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2838 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2840 /* Fill default data, this will be required even in case of failure */
2841 _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2842 memcpy(&info->svc_uuid, svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2843 info->svc_inst_id = svc_info->inst_id;
2844 memcpy(&info->char_uuid, char_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2845 info->char_inst_id = char_info->inst_id;
2847 /* Fill property of the parent characteristic of this descriptor */
2848 info->char_props_map = char_info->props;
2850 info->count = g_slist_length(char_info->descs);
2852 if (!char_info->descs) {
2853 BT_ERR("No Descriptors browsed for address [%s]", conn_info->addr);
2857 for (l = char_info->descs; l != NULL; l = g_slist_next(l)) {
2858 desc_info = (bt_gatt_descriptor_info_t*)l->data;
2859 if (desc_info == NULL)
2862 memcpy(&uuid.uuid, &desc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2863 _bt_uuid_to_string(&uuid, uuid_string);
2865 /* Fill UUID of Descriptor */
2866 g_strlcpy(info->uuids[count], uuid_string,
2867 BLUETOOTH_UUID_STRING_MAX);
2869 /* Fill instance ID of Descriptor */
2870 info->inst_id[count] = desc_info->inst_id;
2873 /* Increment count of Descriptor browsed */
2877 BT_INFO("Total descriptors browsed [%d]", count);
2881 static void __bt_cleanup_remote_services(struct gatt_server_info_t *conn_info)
2888 bt_gatt_service_info_list_t * svc_info_list = NULL;
2889 bt_gatt_service_info_t *svc = NULL;
2890 bt_gatt_char_info_t *chr = NULL;
2891 bt_gatt_descriptor_info_t *desc = NULL;
2892 bt_gatt_included_service_info_t *incl_svc = NULL;
2894 svc_info_list = __bt_get_service_info_list(conn_info->connection_id);
2895 if (!svc_info_list) {
2896 BT_INFO("Could not find Svc Info list for the connection ID [%d]",
2897 conn_info->connection_id);
2901 BT_INFO("Start Cleanup of all services. Num Services [%d]", g_slist_length(svc_info_list->services));
2902 for (l = svc_info_list->services; l;) {
2903 svc = (bt_gatt_service_info_t*)l->data;
2904 l = g_slist_next(l);
2908 BT_DBG("Service info Is Prim[%d] Inst ID [%d]",
2909 svc->is_primary, svc->inst_id);
2910 /* Delete all chars and its descriptors */
2911 for (ll = svc->chars; ll;) {
2912 chr = (bt_gatt_char_info_t*)ll->data;
2913 ll = g_slist_next(ll);
2917 for (lll = chr->descs; lll;) {
2918 desc = (bt_gatt_descriptor_info_t *)lll->data;
2919 lll = g_slist_next(lll);
2922 chr->descs = g_slist_remove(chr->descs, desc);
2925 svc->chars = g_slist_remove(svc->chars, chr);
2929 /* Delete all included services */
2930 for (llll = svc->included_svcs; llll;) {
2931 incl_svc = (bt_gatt_included_service_info_t*)llll->data;
2932 llll = g_slist_next(llll);
2933 if (incl_svc == NULL)
2936 svc->included_svcs = g_slist_remove(svc->included_svcs, incl_svc);
2939 svc_info_list->services = g_slist_remove(svc_info_list->services, svc);
2943 list_gatt_info = g_slist_remove(list_gatt_info, svc_info_list);
2944 g_free(svc_info_list);
2947 int _bt_register_gatt_client_instance(const char *sender,
2948 bluetooth_device_address_t *address)
2950 int ret = OAL_STATUS_SUCCESS;
2951 char *uuid_string = NULL;
2956 /* App should ensure that it should not send */
2957 BT_INFO("### Check on which instance GATT Client instance can be initialized....");
2958 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
2959 if (numapps[k].is_initialized == 1) {
2960 BT_INFO("Instance ID [%d] is already in use..Check next slot",
2961 numapps[k].instance_id);
2964 BT_INFO("Time to register GATT client instancer..UUID to be used is [%s] slot [%d]",
2965 uuid_list[slot-1], slot);
2971 BT_ERR("No Slot if free for GATT Client registration..");
2972 return BLUETOOTH_ERROR_REGISTRATION_FAILED;
2975 uuid_string = g_malloc0(BT_UUID_STRING_MAX);
2976 _bt_string_to_uuid(uuid_list[slot-1], (service_uuid_t*)&uuid);
2977 g_strlcpy(uuid_string, uuid_list[slot-1], BT_UUID_STRING_MAX);
2978 BT_INFO("Copied UUID string [%s]", uuid_string);
2980 /* Register GATT Client */
2981 ret = gattc_register(&uuid);
2982 if (ret != OAL_STATUS_SUCCESS) {
2983 BT_ERR("ret: %d", ret);
2984 g_free(uuid_string);
2985 return BLUETOOTH_ERROR_INTERNAL;
2988 BT_INFO("GATT Client registration call successfully accepted by OAL..wait for Instance Initialized event from OAL..");
2990 /* Return & wait for GATT Client Instance Initialization event */
2991 memset(numapps[slot].sender, 0x00, sizeof(numapps[slot].sender));
2992 memset(numapps[slot].uuid, 0x00, sizeof(numapps[slot].uuid));
2994 g_strlcpy(numapps[slot].sender, sender, sizeof(numapps[slot].sender));
2995 g_strlcpy(numapps[slot].uuid, uuid_string, sizeof(numapps[slot].uuid));
2997 /* Address is saved here. When event comes, sender + address are matched for replying pending
2998 request. It is impossible for same sender to have requests with two same addresses */
2999 memcpy(&numapps[slot].address.addr, address->addr, sizeof(bluetooth_device_address_t));
3001 BT_INFO("Slot [%d] occupied", slot);
3002 numapps[slot].is_initialized = TRUE; /* Set initialization to true here itself */
3004 g_free(uuid_string);
3005 return BLUETOOTH_ERROR_NONE;
3011 /* GATT client events */
3012 static void __bt_handle_client_instance_registered(event_gattc_register_t *data)
3014 bt_service_app_info_t *info = NULL;
3016 char *uuid_string = g_malloc0(BT_UUID_STRING_MAX);
3018 _bt_uuid_to_string(&(data->client_uuid), uuid_string);
3019 BT_INFO("CLient ID is Initialized [%d] UUID initialized [%s]", data->client_if, uuid_string);
3021 /* Platform GATT client framwork does not use Default GATT client instance
3022 This GATT client instance is never deregistred in the lifetime of bt-service */
3023 if (g_strcmp0(uuid_string, DEFAULT_GATT_CLIENT_UUID) == 0) {
3024 BT_INFO("Default client Instance Registered");
3025 gatt_default_client = data->client_if;
3026 g_free(uuid_string);
3030 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
3033 if (g_strcmp0(info->uuid, uuid_string) == 0) {
3034 BT_INFO("Found GATT client.. sender [%s] Slot [%d] occupied", info->sender, k);
3035 info->is_initialized = TRUE;
3036 info->client_id = data->client_if;
3037 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE, BT_GATT_CLIENT_REGISTER,
3038 (void*)info, sizeof(bt_service_app_info_t));
3042 g_free(uuid_string);
3045 static void __bt_handle_client_connected(event_gattc_conn_t *event_data)
3047 int result = BLUETOOTH_ERROR_NONE;
3048 struct gatt_server_info_t *conn_info = NULL;
3049 struct gatt_out_conn_info_t *out_conn_info = NULL;
3051 GVariant *param = NULL;
3053 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
3054 _bt_convert_addr_type_to_string(address,
3055 (unsigned char *)event_data->address.addr);
3057 if (event_data->status != OAL_STATUS_SUCCESS)
3058 result = BLUETOOTH_ERROR_INTERNAL;
3060 /* DBUS Return fo BT_CONNECT_LE for all the apps */
3061 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE, address,
3062 BT_ADDRESS_STRING_SIZE);
3064 BT_INFO("Local GATT Client Connected: Remote addr[%s] Client Interface [%d] Connection ID [%d] status[%d]",
3065 address, event_data->client_if, event_data->conn_id, event_data->status);
3067 if (result == BLUETOOTH_ERROR_NONE) {
3068 /* Check if device is already in connected list */
3069 conn_info = _bt_find_remote_gatt_server_info(address);
3072 /* Send event to BT-API */
3073 param = g_variant_new("(is)", result, address);
3074 _bt_send_event(BT_DEVICE_EVENT,
3075 BLUETOOTH_EVENT_GATT_CLIENT_CONNECTED, /* Local device is GATT client */
3078 /* Save Connection info */
3079 conn_info = g_new0(struct gatt_server_info_t, 1);
3080 conn_info->addr = g_strdup(address);
3081 conn_info->client_id = event_data->client_if;
3082 BT_INFO("Added GATT server addr[%s]", conn_info->addr);
3083 conn_info->connection_id = event_data->conn_id;
3084 gatt_server_info_list = g_slist_append(gatt_server_info_list, conn_info);
3085 BT_INFO("Total num of connected Remote GATT server devices [%d]",
3086 g_slist_length(gatt_server_info_list));
3089 BT_INFO("Do a Internal refresh");
3090 if (OAL_STATUS_SUCCESS != gattc_refresh(conn_info->client_id, &event_data->address))
3091 BT_ERR("GATT database refresh failed!!");
3093 BT_INFO("GATT database refresh Success!!");
3096 BT_ERR("Local GATT Client connected event for addr[%s], but device is in connected list already", address);
3098 __bt_add_mtu_gatt_device(address);
3100 BT_ERR("GATT Client Connection failed!!");
3102 /* If outgoing connection Info is present, then remove it */
3103 out_conn_info = __bt_find_gatt_outgoing_conn_info(address);
3104 if (out_conn_info) {
3105 BT_ERR("Outgoing Client connect request was sent");
3106 outgoing_gatt_conn_list = g_slist_remove(outgoing_gatt_conn_list, out_conn_info);
3107 g_free(out_conn_info->addr);
3108 g_free(out_conn_info);
3113 static void __bt_handle_client_disconnected(event_gattc_conn_t *event_data)
3115 int result = BLUETOOTH_ERROR_NONE;
3117 struct gatt_server_info_t *conn_info = NULL;
3118 struct gatt_out_conn_info_t *out_conn_info = NULL;
3120 GVariant *param = NULL;
3122 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
3123 _bt_convert_addr_type_to_string(address,
3124 (unsigned char *)event_data->address.addr);
3126 if (event_data->status != OAL_STATUS_SUCCESS)
3127 result = BLUETOOTH_ERROR_INTERNAL;
3129 if (NULL == _bt_get_request_info_data(BT_DISCONNECT_LE, address)) {
3130 if (NULL != _bt_get_request_info_data(BT_CONNECT_LE, address)) {
3131 result = BLUETOOTH_ERROR_INTERNAL;
3132 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
3133 address, BT_ADDRESS_STRING_SIZE);
3134 BT_ERR("Failed to connect Local GATT Remote addr[%s]", address);
3139 /* DBUS Return for BT_DISCONNECT_LE for all the apps */
3140 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE, address,
3141 BT_ADDRESS_STRING_SIZE);
3143 BT_INFO("Local GATT Client DisConnected: Remote addr[%s] Client Interface [%d] Connection ID [%d] status [%d]",
3144 address, event_data->client_if, event_data->conn_id, event_data->status);
3146 /* Remove Connection info */
3147 conn_info = _bt_find_remote_gatt_server_info(address);
3150 param = g_variant_new("(is)", result, address);
3151 /* Send event to application */
3152 _bt_send_event(BT_DEVICE_EVENT,
3153 BLUETOOTH_EVENT_GATT_CLIENT_DISCONNECTED,
3156 BT_INFO("Remove GATT server info from List..");
3157 /* Remove all services from info list_gatt_info */
3158 __bt_cleanup_remote_services(conn_info);
3160 /* Remove info from List */
3161 gatt_server_info_list = g_slist_remove(gatt_server_info_list, conn_info);
3162 BT_INFO("Total num of connected GATT servers [%d]", g_slist_length(gatt_server_info_list));
3163 g_free(conn_info->addr);
3166 BT_INFO("Can not find conn info, already removed!");
3168 __bt_remove_mtu_gatt_device(address);
3170 /* If outgoing connection Info is present, then remove it */
3171 out_conn_info = __bt_find_gatt_outgoing_conn_info(address);
3172 if (out_conn_info) {
3173 BT_ERR("Client Disconnected event, but outgoing connect request was sent");
3174 outgoing_gatt_conn_list = g_slist_remove(outgoing_gatt_conn_list, out_conn_info);
3175 g_free(out_conn_info->addr);
3176 g_free(out_conn_info);
3182 static void __bt_handle_client_service_search_result(
3183 event_gattc_service_result_t *event_data)
3185 /* Pre: status is never fail from OAL */
3187 /* Find service list from address */
3188 bt_gatt_service_info_list_t *svc_info_list;
3189 bt_gatt_service_info_t *svc_info;
3191 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3192 if (!svc_info_list) {
3193 BT_DBG("Service info list not present for connection ID %d, means first time browse", event_data->conn_status.conn_id);
3194 /* Means for this conn_id, no services are ever browsed, first time,
3195 create service info list for this conn_id */
3196 svc_info_list = g_malloc0(sizeof(bt_gatt_service_info_list_t));
3197 svc_info_list->conn_id = event_data->conn_status.conn_id;
3198 list_gatt_info = g_slist_append(list_gatt_info, svc_info_list);
3201 /* send list and current service's uuid and instance id to find it */
3202 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3205 /* If not found, check if service changed, if yes, means this is a new service added
3206 in remote GATT device, update uuid info in svc info list structure, to be used when
3207 search is completed */
3209 if (svc_info_list->info.is_changed) {
3210 BT_DBG("Service Changed indication already found for connection ID %d", event_data->conn_status.conn_id);
3211 memcpy(svc_info_list->info.uuid, event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3213 /* Create and add new service in service list */
3214 svc_info = g_malloc0(sizeof(bt_gatt_service_info_t));
3215 memcpy(svc_info->uuid, event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3216 svc_info->inst_id = event_data->srvc_id.id.inst_id;
3217 svc_info->is_primary = event_data->srvc_id.is_prmry;
3218 svc_info_list->services = g_slist_append(svc_info_list->services, svc_info);
3219 BT_DBG("Service created and added in Svc info list or connection ID %d", event_data->conn_status.conn_id);
3221 /* If returned matching service info, then just update service_rmeoved value inside it to 0 */
3222 svc_info->is_removed = 0;
3226 static void __bt_handle_client_service_search_completed(
3227 event_gattc_conn_status_t *event_data)
3229 struct gatt_server_info_t *conn_info = NULL;
3230 bt_gatt_service_info_list_t *svc_info_list;
3231 bt_gatt_service_info_t *svc_info;
3232 bt_services_browse_info_t browse_info;
3233 unsigned char uuid_empty[BLUETOOTH_UUID_HEX_MAX_LEN];
3235 memset(&uuid_empty, 0x00, BLUETOOTH_UUID_HEX_MAX_LEN);
3236 memset(&browse_info, 0x00, sizeof(bt_services_browse_info_t));
3237 BT_INFO("Primary Services browsing completed status[%d] conn ID [%d]",
3238 event_data->status, event_data->conn_id);
3240 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(event_data->conn_id);
3242 svc_info_list = __bt_get_service_info_list(event_data->conn_id);
3243 if (!svc_info_list) {
3244 BT_ERR("No services browsed ever for addr [%s]", conn_info->addr);
3246 /* Just build response and return ERROR */
3247 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3249 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_INTERNAL,
3250 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3251 sizeof(bt_services_browse_info_t));
3255 /* If fail, then send event with error */
3256 if (event_data->status != OAL_STATUS_SUCCESS) {
3257 /* Just build response and return ERROR */
3258 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3260 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_INTERNAL,
3261 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3262 sizeof(bt_services_browse_info_t));
3266 /* If success, then find service info list from address */
3268 /* If svc_changed == 1 and uuid valid, means a new service is added*/
3269 if (svc_info_list->info.is_changed && !memcmp(uuid_empty, svc_info_list->info.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
3270 /* TODO: Send event -Service added with instance ID and UUID of newly added service */
3271 BT_INFO("new service added");
3273 BT_INFO("TODO new service added");
3276 /* If svc_changed == 1 and uuid invalid, then a service is removed */
3277 if (svc_info_list->info.is_changed && memcmp(uuid_empty, svc_info_list->info.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
3278 /* Scan through the service info list to find service with is_removed = 1*/
3279 svc_info = __bt_find_removed_service(svc_info_list);
3281 /* TODO Send event - Service removed with instance ID and UUID of just rmeoved service */
3283 /* Remove that service info from service info list */
3284 svc_info_list->services = g_slist_remove(svc_info_list->services, svc_info);
3286 /* Delete that service completely from svc_info list*/
3287 __bt_remove_service_info_from_list(svc_info);
3291 /* Reset svc_changed = 0, and reset UUID = all 0's */
3292 svc_info_list->info.is_changed = 0;
3293 memset(&svc_info_list->info.uuid, 0x00, BLUETOOTH_UUID_HEX_MAX_LEN);
3295 /* Build Reply and send to service browse primary services request of pending apps */
3296 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3298 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3299 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3300 sizeof(bt_services_browse_info_t));
3304 static void __bt_handle_client_characteristic_search_result(
3305 event_gattc_characteristic_result_t *event_data)
3307 bt_gatt_service_info_list_t *svc_info_list;
3308 bt_gatt_service_info_t *svc_info;
3309 bt_gatt_char_info_t *char_info;
3310 bt_char_browse_info_t browse_info;
3312 memset(&browse_info, 0x00, sizeof(bt_char_browse_info_t));
3315 if (event_data->conn_status.status == OAL_STATUS_SUCCESS) {
3316 /* Find service info list from address */
3317 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3318 if (svc_info_list == NULL) {
3319 BT_ERR("svc_info_list is NULL");
3324 /* Find matching service info from svc info list */
3325 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3326 if (svc_info == NULL) {
3327 BT_ERR("svc_info is NULL");
3331 /* Find Matching char from service info in event */
3332 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3334 /* If not found, then add new characteristic and return */
3336 BT_DBG("Add new characteristic");
3337 char_info = g_malloc0(sizeof(bt_gatt_char_info_t));
3338 memcpy(char_info->uuid, event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3340 char_info->inst_id = event_data->char_id.inst_id;
3341 char_info->props = event_data->char_prop;
3342 svc_info->chars = g_slist_append(svc_info->chars, char_info);
3344 /* If found, then return */
3345 BT_DBG("update char property as Characteristic browsed is already present");
3346 char_info->props |= event_data->char_prop;
3349 /* If Not success: Means Charc browse is completed */
3350 /* Find char list from service in event */
3351 /* Find service list from address */
3352 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3353 if (svc_info_list == NULL) {
3354 BT_ERR("svc_info_list is NULL");
3358 /* Find service info from service in event */
3359 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3360 if (svc_info == NULL) {
3361 BT_ERR("svc_info is NULL");
3365 /* Build char list from service in event */
3366 __bt_build_char_browse_info(event_data->conn_status.conn_id,
3367 svc_info, &browse_info);
3369 /* Create response and return by sending event*/
3370 /* Build Reply and send to service browse All Included services request of pending apps */
3371 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3372 BT_GATT_GET_SERVICE_PROPERTIES,
3374 sizeof(bt_char_browse_info_t));
3378 static void __bt_handle_client_descriptor_search_result(
3379 event_gattc_descriptor_result_t *event_data)
3381 bt_gatt_service_info_list_t *svc_info_list;
3382 bt_gatt_service_info_t *svc_info;
3383 bt_gatt_char_info_t *char_info;
3384 bt_gatt_descriptor_info_t *desc_info;
3385 bt_descriptor_browse_info_t browse_info;
3387 BT_INFO("descriptor search result status [%d]", event_data->conn_status.status);
3389 memset(&browse_info, 0x00, sizeof(bt_descriptor_browse_info_t));
3392 if (event_data->conn_status.status == OAL_STATUS_SUCCESS) {
3393 /* Find service list from address */
3394 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3395 if (svc_info_list == NULL) {
3396 BT_ERR("svc_info_list is NULL");
3400 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3401 if (svc_info == NULL) {
3402 BT_ERR("svc_info is NULL");
3406 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3407 if (char_info == NULL) {
3408 BT_ERR("char_info is NULL");
3412 desc_info = __bt_find_matching_desc(char_info, &event_data->descr_id);
3413 /* If not found, add new descriptor and return */
3415 desc_info = g_malloc0(sizeof(bt_gatt_descriptor_info_t));
3416 memcpy(desc_info->uuid, event_data->descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3418 desc_info->inst_id = event_data->descr_id.inst_id;
3419 char_info->descs = g_slist_append(char_info->descs, desc_info);
3422 /* If found, then return */
3423 BT_INFO("Characteristic browsed is already presesnt");
3426 /* If Not success */
3427 /* Find service list from address */
3428 /* Find included service list from service in event */
3429 /* Create response and return by sending event*/
3430 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3431 if (svc_info_list == NULL) {
3432 BT_ERR("svc_info_list is NULL");
3436 /* Find service info from service in event */
3437 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3438 if (svc_info == NULL) {
3439 BT_ERR("svc_info is NULL");
3443 /* Find char info from char in event */
3444 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3445 if (char_info == NULL) {
3446 BT_ERR("char_info is NULL");
3450 /* Build descriptor list from char in event */
3451 __bt_build_descriptor_browse_info(event_data->conn_status.conn_id,
3452 svc_info, char_info, &browse_info);
3455 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3456 BT_GATT_GET_CHARACTERISTIC_PROPERTIES,
3458 sizeof(bt_descriptor_browse_info_t));
3462 static void __bt_handle_client_characteristic_read_data(
3463 event_gattc_read_data *event_data)
3465 int result = BLUETOOTH_ERROR_NONE;
3466 struct gatt_server_info_t *conn_info = NULL;
3467 bluetooth_gatt_client_char_prop_info_t read_info;
3469 /* Read Information data structures */
3470 GVariant *param = NULL;
3471 GVariant *data = NULL;
3472 GVariant *data_svc_uuid = NULL;
3473 GVariant *data_char_uuid = NULL;
3474 char *read_val = NULL;
3475 char *svc_uuid = NULL;
3476 char *char_uuid = NULL;
3479 //memset(&read_info, 0x00, sizeof(bt_gatt_handle_property_t));
3480 memset(&read_info, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
3482 /* Extract Address from conn_id of event data */
3483 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3484 event_data->uuid_status.conn_status.conn_id);
3486 BT_INFO("Characteristic Read result from addr [%s] status [%d]",
3487 conn_info->addr, event_data->uuid_status.conn_status.status);
3489 /* Fill char in buffer */
3490 memcpy(&read_info.characteristic.uuid,
3491 event_data->uuid_status.char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3492 read_info.characteristic.instance_id = event_data->uuid_status.char_id.inst_id;
3494 /* Fill Service in buffer */
3495 memcpy(&read_info.svc.uuid,
3496 event_data->uuid_status.srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3497 read_info.svc.instance_id = event_data->uuid_status.srvc_id.id.inst_id;
3499 /* Fill remote device address */
3500 _bt_convert_addr_string_to_type(read_info.device_address.addr, conn_info->addr);
3502 /* Fill data and reply to all apps waiting for Read result on the same characteristic
3503 Note: Even in case of failure, address, handles and result code should be returned */
3504 if (event_data->uuid_status.conn_status.status != OAL_STATUS_SUCCESS)
3505 result = BLUETOOTH_ERROR_INTERNAL;
3507 if (event_data->data_len > 0) {
3509 // for (i = 0; i < event_data->data_len; i++)
3510 // BT_INFO("Data[%d] = [0x%x]", i, event_data->data[i]);
3513 read_val = g_memdup(&event_data->data[0], event_data->data_len);
3515 data = g_variant_new_from_data(
3516 G_VARIANT_TYPE_BYTESTRING,
3518 event_data->data_len,
3521 BT_ERR("Characteristic Read success, but no data!!!");
3523 data = g_variant_new_from_data(
3524 G_VARIANT_TYPE_BYTESTRING,
3531 svc_uuid = g_memdup(&event_data->uuid_status.srvc_id.id.uuid.uuid[0], uuid_len);
3533 data_svc_uuid = g_variant_new_from_data(
3534 G_VARIANT_TYPE_BYTESTRING,
3540 char_uuid = g_memdup(&event_data->uuid_status.char_id.uuid.uuid[0], uuid_len);
3542 data_char_uuid = g_variant_new_from_data(
3543 G_VARIANT_TYPE_BYTESTRING,
3548 param = g_variant_new("(isn@ayin@ayin@ay)", result,
3552 event_data->uuid_status.srvc_id.id.inst_id,
3555 event_data->uuid_status.char_id.inst_id,
3556 event_data->data_len,
3560 _bt_send_event(BT_GATT_CLIENT_EVENT,
3561 BLUETOOTH_EVENT_GATT_READ_CHAR,
3564 /* Send DBUS return */
3565 __bt_gatt_handle_pending_request_info(result,
3566 BT_GATT_READ_CHARACTERISTIC,
3568 sizeof(bluetooth_gatt_client_char_prop_info_t));
3579 static void __bt_handle_client_descriptor_read_data(
3580 event_gattc_read_data *event_data)
3582 int result = BLUETOOTH_ERROR_NONE;
3583 struct gatt_server_info_t *conn_info = NULL;
3584 bluetooth_gatt_client_desc_prop_info_t read_info;
3586 /* Read Information data structures */
3587 GVariant *param = NULL;
3588 GVariant *data = NULL;
3589 GVariant *data_svc_uuid = NULL;
3590 GVariant *data_char_uuid = NULL;
3591 GVariant *data_desc_uuid = NULL;
3592 char *read_val = NULL;
3593 char *svc_uuid = NULL;
3594 char *char_uuid = NULL;
3595 char *desc_uuid = NULL;
3600 memset(&read_info, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
3602 /* Extract Address from conn_id of event data */
3603 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3604 event_data->uuid_status.conn_status.conn_id);
3606 BT_INFO("Descriptor Read result from addr [%s] status [%d]",
3607 conn_info->addr, event_data->uuid_status.conn_status.status);
3609 /* Fill descriptor informations in buffer */
3610 memcpy(&read_info.descriptor.uuid,
3611 event_data->uuid_status.descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3612 read_info.descriptor.instance_id = event_data->uuid_status.descr_id.inst_id;
3614 /* Fill Characteristic informations in buffer */
3615 memcpy(&read_info.characteristic.uuid,
3616 event_data->uuid_status.char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3617 read_info.characteristic.instance_id = event_data->uuid_status.char_id.inst_id;
3619 /* Fill Service informations in buffer */
3620 memcpy(&read_info.svc.uuid,
3621 event_data->uuid_status.srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3622 read_info.svc.instance_id = event_data->uuid_status.srvc_id.id.inst_id;
3624 /* Fill remote device address */
3625 _bt_convert_addr_string_to_type(read_info.device_address.addr, conn_info->addr);
3627 /* Fill data and reply to all apps waiting for Read result on the same characteristic */
3628 if (event_data->uuid_status.conn_status.status != OAL_STATUS_SUCCESS)
3629 result = BLUETOOTH_ERROR_INTERNAL;
3631 BT_INFO("desc data len:%d", event_data->data_len);
3632 if (event_data->data_len > 0) {
3634 for (i = 0; i < event_data->data_len; i++)
3635 BT_DBG("Data[%d] = [0x%x]", i, event_data->data[i]);
3638 read_val = g_memdup(&event_data->data[0], event_data->data_len);
3640 data = g_variant_new_from_data(
3641 G_VARIANT_TYPE_BYTESTRING,
3643 event_data->data_len,
3646 BT_INFO("Descriptor Read success, but no data!!!");
3648 data = g_variant_new_from_data(
3649 G_VARIANT_TYPE_BYTESTRING,
3655 svc_uuid = g_memdup(&event_data->uuid_status.srvc_id.id.uuid.uuid[0], uuid_len);
3657 data_svc_uuid = g_variant_new_from_data(
3658 G_VARIANT_TYPE_BYTESTRING,
3664 char_uuid = g_memdup(&event_data->uuid_status.char_id.uuid.uuid[0], uuid_len);
3666 data_char_uuid = g_variant_new_from_data(
3667 G_VARIANT_TYPE_BYTESTRING,
3673 desc_uuid = g_memdup(&event_data->uuid_status.descr_id.uuid.uuid[0], uuid_len);
3675 data_desc_uuid = g_variant_new_from_data(
3676 G_VARIANT_TYPE_BYTESTRING,
3681 param = g_variant_new("(isn@ayin@ayin@ayin@ay)", result,
3685 event_data->uuid_status.srvc_id.id.inst_id,
3688 event_data->uuid_status.char_id.inst_id,
3691 event_data->uuid_status.descr_id.inst_id,
3692 event_data->data_len,
3696 _bt_send_event(BT_GATT_CLIENT_EVENT,
3697 BLUETOOTH_EVENT_GATT_READ_DESC,
3700 BT_INFO("Send DBUS rpely for GATT Read Descriptor");
3701 /* Send DBUS return */
3702 __bt_gatt_handle_pending_request_info(result,
3703 BT_GATT_READ_DESCRIPTOR_VALUE,
3705 sizeof(bluetooth_gatt_client_desc_prop_info_t));
3716 static void __bt_handle_client_characteristic_write_data(
3717 event_gattc_write_data *event_data)
3719 int result = BLUETOOTH_ERROR_NONE;
3720 struct gatt_server_info_t *conn_info = NULL;
3721 bluetooth_gatt_client_char_prop_info_t write_info;
3723 /* Read Information data structures */
3724 GVariant *param = NULL;
3725 GVariant *data_svc_uuid = NULL;
3726 GVariant *data_char_uuid = NULL;
3727 char *svc_uuid = NULL;
3728 char *char_uuid = NULL;
3732 memset(&write_info, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
3734 /* Extract Address from conn_id of event data */
3735 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3736 event_data->conn_status.conn_id);
3738 BT_INFO("Characteristic Write callback from addr [%s] status [%d]",
3739 conn_info->addr, event_data->conn_status.status);
3741 /* Fill char in buffer */
3742 memcpy(&write_info.characteristic.uuid,
3743 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3744 write_info.characteristic.instance_id = event_data->char_id.inst_id;
3746 /* Fill Service in buffer */
3747 memcpy(&write_info.svc.uuid,
3748 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3749 write_info.svc.instance_id = event_data->srvc_id.id.inst_id;
3751 /* Fill remote device address */
3752 _bt_convert_addr_string_to_type(write_info.device_address.addr, conn_info->addr);
3754 if (event_data->conn_status.status != OAL_STATUS_SUCCESS) {
3755 result = BLUETOOTH_ERROR_INTERNAL;
3761 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
3763 data_svc_uuid = g_variant_new_from_data(
3764 G_VARIANT_TYPE_BYTESTRING,
3770 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
3772 data_char_uuid = g_variant_new_from_data(
3773 G_VARIANT_TYPE_BYTESTRING,
3778 param = g_variant_new("(isn@ayin@ayi)", result,
3782 event_data->srvc_id.id.inst_id,
3785 event_data->char_id.inst_id);
3788 _bt_send_event(BT_GATT_CLIENT_EVENT,
3789 BLUETOOTH_EVENT_GATT_WRITE_CHAR,
3798 /* Send DBUS return */
3799 __bt_gatt_handle_pending_request_info(result,
3800 BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE,
3802 sizeof(bluetooth_gatt_client_char_prop_info_t));
3806 static void __bt_handle_client_descriptor_write_data(
3807 event_gattc_write_data *event_data)
3809 int result = BLUETOOTH_ERROR_NONE;
3810 struct gatt_server_info_t *conn_info = NULL;
3811 bluetooth_gatt_client_desc_prop_info_t write_info;
3813 /* Write Information data structures */
3814 GVariant *param = NULL;
3815 GVariant *data_svc_uuid = NULL;
3816 GVariant *data_char_uuid = NULL;
3817 GVariant *data_desc_uuid = NULL;
3818 char *svc_uuid = NULL;
3819 char *char_uuid = NULL;
3820 char *desc_uuid = NULL;
3824 memset(&write_info, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
3826 /* Extract Address from conn_id of event data */
3827 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3828 event_data->conn_status.conn_id);
3830 if (NULL == conn_info) {
3832 BT_INFO("Failed to get the conn info for conn_id [%d]", event_data->conn_status.conn_id);
3836 BT_INFO("Descriptor Write callback from addr [%s] status [%d]",
3837 conn_info->addr, event_data->conn_status.status);
3839 /* Fill descriptor informations in buffer */
3840 memcpy(&write_info.descriptor.uuid,
3841 event_data->descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3842 write_info.descriptor.instance_id = event_data->descr_id.inst_id;
3844 /* Fill Characteristic informations in buffer */
3845 memcpy(&write_info.characteristic.uuid,
3846 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3847 write_info.characteristic.instance_id = event_data->char_id.inst_id;
3849 /* Fill Service informations in buffer */
3850 memcpy(&write_info.svc.uuid,
3851 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3852 write_info.svc.instance_id = event_data->srvc_id.id.inst_id;
3854 /* Fill remote device address */
3855 _bt_convert_addr_string_to_type(write_info.device_address.addr, conn_info->addr);
3857 if (event_data->conn_status.status != OAL_STATUS_SUCCESS) {
3858 result = BLUETOOTH_ERROR_INTERNAL;
3864 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
3866 data_svc_uuid = g_variant_new_from_data(
3867 G_VARIANT_TYPE_BYTESTRING,
3873 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
3875 data_char_uuid = g_variant_new_from_data(
3876 G_VARIANT_TYPE_BYTESTRING,
3882 desc_uuid = g_memdup(&event_data->descr_id.uuid.uuid[0], uuid_len);
3884 data_desc_uuid = g_variant_new_from_data(
3885 G_VARIANT_TYPE_BYTESTRING,
3890 param = g_variant_new("(isn@ayin@ayin@ayi)", result,
3894 event_data->srvc_id.id.inst_id,
3897 event_data->char_id.inst_id,
3900 event_data->descr_id.inst_id);
3903 _bt_send_event(BT_GATT_CLIENT_EVENT,
3904 BLUETOOTH_EVENT_GATT_WRITE_DESC,
3915 /* Send DBUS return */
3916 __bt_gatt_handle_pending_request_info(result,
3917 BT_GATT_WRITE_DESCRIPTOR_VALUE,
3919 sizeof(bluetooth_gatt_client_desc_prop_info_t));
3922 static void __bt_hanlde_le_device_disconnection(event_dev_conn_status_t *event_data)
3924 int result = BLUETOOTH_ERROR_INTERNAL;
3925 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
3927 _bt_convert_addr_type_to_string(address, (unsigned char *)event_data->address.addr);
3929 /* DBUS Return with fail of pending BT_CONNECT_LE for all the apps */
3930 BT_INFO("Local GATT Client disconnected: Remote addr[%s] ", address);
3932 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE, address,
3933 BT_ADDRESS_STRING_SIZE);
3937 static void __bt_handle_client_notification_registered(
3938 event_gattc_regdereg_notify_t *event_data,
3939 gboolean is_registered)
3941 int result = BLUETOOTH_ERROR_NONE;
3942 struct gatt_server_info_t *conn_info = NULL;
3943 bt_gatt_notif_reg_info_t notif_info;
3946 memset(¬if_info, 0x00, sizeof(bt_gatt_notif_reg_info_t));
3948 BT_INFO("Client Interface [%d] status [%d]",
3949 event_data->client_if,
3950 event_data->status);
3952 /* Extract Address from conn_id of event data */
3953 conn_info = __bt_find_remote_gatt_server_info_from_client_if(
3954 event_data->client_if);
3957 BT_INFO("Connection Info is not present, return");
3960 BT_INFO("Notification Registered for addr [%s]",
3964 /* Fill svc informations in buffer */
3965 memcpy(¬if_info.svc_uuid,
3966 event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3967 notif_info.svc_inst = event_data->srvc_id.id.inst_id;
3969 /* Fill char in buffer */
3970 memcpy(¬if_info.char_uuid,
3971 event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3972 notif_info.char_inst = event_data->char_id.inst_id;
3974 /* Fill remote device address */
3975 _bt_convert_addr_string_to_type(notif_info.addr.addr, conn_info->addr);
3977 notif_info.is_registered = is_registered;
3979 if (event_data->status != OAL_STATUS_SUCCESS)
3980 result = BLUETOOTH_ERROR_INTERNAL;
3982 /* Send DBUS Return for BT_GATT_WATCH_CHARACTERISTIC */
3983 __bt_gatt_handle_pending_request_info(result,
3984 BT_GATT_WATCH_CHARACTERISTIC,
3986 sizeof(bt_gatt_notif_reg_info_t));
3989 static void __bt_handle_client_notification_data(event_gattc_notify_data *event_data)
3991 /* No status in this event from OAL */
3992 int result = BLUETOOTH_ERROR_NONE;
3994 /* Read Information data structures */
3995 GVariant *param = NULL;
3996 GVariant *data = NULL;
3997 GVariant *data_svc_uuid = NULL;
3998 GVariant *data_char_uuid = NULL;
3999 char *read_val = NULL;
4000 char *svc_uuid = NULL;
4001 char *char_uuid = NULL;
4007 BT_INFO("Notifcation of charc data changed");
4009 if (event_data->data_len > 0) {
4011 for (i = 0; i < event_data->data_len; i++)
4012 BT_INFO("Data[%d] = [0x%x]", i, event_data->data[i]);
4015 addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
4016 _bt_convert_addr_type_to_string(addr,
4017 (unsigned char *)&(event_data->address.addr));
4020 read_val = g_memdup(&event_data->data[0], event_data->data_len);
4022 data = g_variant_new_from_data(
4023 G_VARIANT_TYPE_BYTESTRING,
4025 event_data->data_len,
4028 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
4030 data_svc_uuid = g_variant_new_from_data(
4031 G_VARIANT_TYPE_BYTESTRING,
4037 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
4039 data_char_uuid = g_variant_new_from_data(
4040 G_VARIANT_TYPE_BYTESTRING,
4046 param = g_variant_new("(isn@ayin@ayin@ay)", result,
4050 event_data->srvc_id.id.inst_id,
4053 event_data->char_id.inst_id,
4054 event_data->data_len,
4058 _bt_send_event(BT_GATT_CLIENT_EVENT,
4059 BLUETOOTH_EVENT_GATT_CHAR_VAL_CHANGED,
4062 BT_ERR("No Data!!");
4075 static void __bt_handle_client_service_changed_ind(event_gattc_service_changed_data *event_data)
4077 GVariant *param = NULL;
4078 char *address_str = NULL;
4079 char *uuid_str = NULL;
4081 address_str = g_malloc0(BT_ADDRESS_STRING_SIZE);
4082 uuid_str = g_malloc0(BT_UUID_STRING_MAX);
4083 _bt_convert_addr_type_to_string(address_str, event_data->address.addr);
4084 _bt_uuid_to_string(&event_data->uuid, uuid_str);
4086 param = g_variant_new("(iss)", event_data->change_type, address_str, uuid_str);
4088 _bt_send_event(BT_GATT_CLIENT_EVENT,
4089 BLUETOOTH_EVENT_GATT_CLIENT_SERVICE_CHANGED,
4093 gboolean _bt_is_remote_gatt_device_connected(bluetooth_device_address_t *address)
4096 struct gatt_server_info_t *conn_info = NULL;
4097 gboolean connected = FALSE;
4099 addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
4100 _bt_convert_addr_type_to_string(addr,
4101 (unsigned char *)&(address->addr));
4103 BT_DBG("Check GATT connection status of [%s]", addr);
4104 /* Check if device is already in connected list */
4105 conn_info = _bt_find_remote_gatt_server_info(addr);
4108 BT_DBG("Remote GATT Server device [%s] is Connected", conn_info->addr);
4111 struct gatt_client_info_t *client_info = NULL;
4113 BT_INFO("Remote GATT Server Device [%s] is not Connected", addr);
4115 /* Check if device is already in connected list */
4116 client_info = _bt_find_remote_gatt_client_info(addr);
4119 BT_DBG("Remote Client device [%s] is Connected", client_info->addr);
4122 BT_INFO("Remote GATT Client Device [%s] is not Connected", addr);
4131 int _bt_connect_le_device(bluetooth_device_address_t *address,
4132 int auto_connect, int client_id)
4134 struct gatt_server_info_t *conn_info = NULL;
4135 struct gatt_out_conn_info_t *out_conn_info = NULL;
4137 invocation_info_t *req_info = NULL;
4138 int ret = OAL_STATUS_SUCCESS;
4140 char *remote_address = NULL;
4142 BT_CHECK_PARAMETER(address, return);
4144 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4145 _bt_convert_addr_type_to_string(addr, address->addr);
4146 BT_INFO("GATT Client connect request for address [%s] client instance [%d]",
4150 /* Check if Remote Device is already under connection progress */
4151 req_info = _bt_get_request_info_data_from_function_name(BT_CONNECT_LE);
4153 remote_address = (char*)req_info->user_data;
4154 if (remote_address && !strcasecmp(remote_address, addr)) {/* Address matched */
4155 BT_INFO("Already Connection ongoing for same remote GATT Server address [%s]", remote_address);
4156 /* Return and wait for events to be sent to all apps */
4158 return BLUETOOTH_ERROR_IN_PROGRESS;
4162 /* Check if remote GATT Server is connected or not */
4163 conn_info = _bt_find_remote_gatt_server_info(addr);
4165 BT_ERR("GATT Server is already connected..");
4167 return BLUETOOTH_ERROR_ALREADY_CONNECT;
4170 /* TODO Check Requirement of holding Advertisement before initiating LE connect */
4172 /* Check if app sent 0 client id for connection, in such case, use default gatt client ID */
4173 if (client_id == 0) {
4174 BT_INFO("GATT CLient connect request sent by an app without any client instance [%d]",
4176 BT_INFO("Assign default GATT client id [%d]", gatt_default_client);
4177 client_id = gatt_default_client;
4180 BT_INFO("Connect using CLient ID [%d]", client_id);
4181 ret = gattc_connect(client_id, (bt_address_t*)(address), auto_connect);
4183 if (ret != OAL_STATUS_SUCCESS) {
4184 BT_ERR("ret: %d", ret);
4186 return BLUETOOTH_ERROR_INTERNAL;
4189 /* Mark this as outgoing connection */
4190 out_conn_info = g_new0(struct gatt_out_conn_info_t, 1);
4191 out_conn_info->addr = g_strdup(addr);
4192 out_conn_info->client_id = client_id;
4193 BT_INFO("Added outgoing connection info addr[%s]", out_conn_info->addr);
4194 outgoing_gatt_conn_list = g_slist_append(outgoing_gatt_conn_list, out_conn_info);
4197 return BLUETOOTH_ERROR_NONE;
4200 int _bt_gatt_get_primary_services(char *address)
4202 BT_CHECK_PARAMETER(address, return);
4203 struct gatt_server_info_t *conn_info = NULL;
4204 invocation_info_t *req_info = NULL;
4205 int ret = OAL_STATUS_SUCCESS;
4208 /* Check if any app is already browsing primary services on the same remote GATT Server */
4209 req_info = _bt_get_request_info_data(BT_GATT_GET_PRIMARY_SERVICES, address);
4211 BT_INFO("Already Primary Service Browsing ongoing for same rmeote GATT Server");
4212 /* Return and wait for events to be sent to all apps */
4213 return BLUETOOTH_ERROR_NONE;
4216 /* Check if remote GATT Server is connected or not */
4217 conn_info = _bt_find_remote_gatt_server_info(address);
4219 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4220 conn_info->addr, conn_info->connection_id);
4222 BT_ERR("GATT Server is not yet connected..");
4223 return BLUETOOTH_ERROR_NOT_CONNECTED;
4226 BT_INFO("Remove GATT server info from List..");
4227 /* Remove all services from info list_gatt_info */
4228 __bt_cleanup_remote_services(conn_info);
4230 /* Send Primary Service Browsing request to stack */
4231 ret = gattc_search_service(conn_info->connection_id, NULL);
4232 if (ret != OAL_STATUS_SUCCESS) {
4233 BT_ERR("ret: %d", ret);
4234 return BLUETOOTH_ERROR_INTERNAL;
4236 return BLUETOOTH_ERROR_NONE;
4239 int _bt_gatt_get_all_characteristic(bluetooth_gatt_client_svc_prop_info_t *svc)
4241 BT_CHECK_PARAMETER(svc, return);
4242 struct gatt_server_info_t *conn_info = NULL;
4243 invocation_info_t *req_info = NULL;
4244 bluetooth_gatt_client_svc_prop_info_t *prop;
4245 oal_gatt_srvc_id_t srvc_id;
4246 int ret = OAL_STATUS_SUCCESS;
4249 /* Check if any app is already browsing characteristics of the same service on the same remote GATT Server */
4250 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_GET_SERVICE_PROPERTIES);
4252 prop = (bluetooth_gatt_client_svc_prop_info_t*)req_info->user_data;
4253 if (prop && !memcmp(svc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t))
4254 && memcmp(prop->svc.uuid, svc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
4255 && prop->svc.instance_id == svc->svc.instance_id) {
4256 BT_INFO("Already Properties browsing for Primary Service ongoing for same remote GATT Server");
4257 /* Return and wait for events to be sent to all apps */
4258 return BLUETOOTH_ERROR_NONE;
4262 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4263 _bt_convert_addr_type_to_string(addr, svc->device_address.addr);
4265 /* Check if remote GATT Server is connected or not */
4266 conn_info = _bt_find_remote_gatt_server_info(addr);
4268 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4269 conn_info->addr, conn_info->connection_id);
4271 BT_ERR("GATT Server is not yet connected..");
4273 return BLUETOOTH_ERROR_NOT_CONNECTED;
4276 srvc_id.is_prmry = TRUE;
4277 srvc_id.id.inst_id = svc->svc.instance_id;
4278 memcpy(srvc_id.id.uuid.uuid, svc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4280 /* Search All Characteristic */
4281 ret = gattc_get_characteristic(conn_info->connection_id, &srvc_id, NULL);
4282 if (ret != OAL_STATUS_SUCCESS) {
4283 BT_ERR("ret: %d", ret);
4285 return BLUETOOTH_ERROR_INTERNAL;
4288 return BLUETOOTH_ERROR_NONE;
4291 int _bt_gatt_get_all_characteristic_properties(
4292 bluetooth_gatt_client_char_prop_info_t *chr)
4294 struct gatt_server_info_t *conn_info = NULL;
4295 invocation_info_t *req_info = NULL;
4296 bluetooth_gatt_client_char_prop_info_t *prop;
4297 oal_gatt_srvc_id_t srvc_id;
4298 oal_gatt_id_t char_id;
4299 int ret = OAL_STATUS_SUCCESS;
4302 BT_CHECK_PARAMETER(chr, return);
4304 /* Check if any app is already browsing descriptors of the same char of
4305 particular service on the same remote GATT Server */
4306 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_GET_CHARACTERISTIC_PROPERTIES);
4308 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4309 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4310 && memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4311 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4312 && memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4313 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4314 BT_INFO("Already Properties browsing for Characteristic ongoing for same remote GATT Server");
4315 /* Return and wait for events to be sent to all apps */
4316 return BLUETOOTH_ERROR_NONE;
4320 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4321 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4323 /* Check if remote GATT Server is connected or not */
4324 conn_info = _bt_find_remote_gatt_server_info(addr);
4326 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4327 conn_info->addr, conn_info->connection_id);
4329 BT_ERR("GATT Server is not yet connected..");
4331 return BLUETOOTH_ERROR_NOT_CONNECTED;
4334 srvc_id.is_prmry = TRUE;
4335 srvc_id.id.inst_id = chr->svc.instance_id;
4336 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4338 char_id.inst_id = chr->characteristic.instance_id;
4339 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4341 /* Search All Descriptors */
4342 ret = gattc_get_descriptor(conn_info->connection_id, &srvc_id, &char_id, NULL);
4343 if (ret != OAL_STATUS_SUCCESS) {
4344 BT_ERR("ret: %d", ret);
4346 return BLUETOOTH_ERROR_INTERNAL;
4349 return BLUETOOTH_ERROR_NONE;
4352 int _bt_gatt_read_characteristic_value(
4353 bluetooth_gatt_client_char_prop_info_t *chr)
4355 struct gatt_server_info_t *conn_info = NULL;
4356 invocation_info_t *req_info = NULL;
4357 bluetooth_gatt_client_char_prop_info_t *prop;
4358 oal_gatt_srvc_id_t srvc_id;
4359 oal_gatt_id_t char_id;
4360 int ret = OAL_STATUS_SUCCESS;
4363 BT_CHECK_PARAMETER(chr, return);
4365 /* Check if any app is already Reading characteristic of the same char of
4366 particular service on the same remote GATT Server */
4367 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_READ_CHARACTERISTIC);
4369 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4370 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4371 && memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4372 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4373 && memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4374 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4375 BT_INFO("Already Characteristic value Read operation in progress for same remote GATT Server");
4376 /* Return and wait for events to be sent to all apps */
4377 return BLUETOOTH_ERROR_NONE;
4381 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4382 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4384 /* Check if remote GATT Server is connected or not */
4385 conn_info = _bt_find_remote_gatt_server_info(addr);
4387 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4388 conn_info->addr, conn_info->connection_id);
4390 BT_ERR("GATT Server is not yet connected..");
4392 return BLUETOOTH_ERROR_NOT_CONNECTED;
4395 srvc_id.is_prmry = TRUE;
4396 srvc_id.id.inst_id = chr->svc.instance_id;
4397 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4399 char_id.inst_id = chr->characteristic.instance_id;
4400 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4402 /* Search All Descriptors */
4403 ret = gattc_read_characteristic(conn_info->connection_id, &srvc_id, &char_id, OAL_GATT_AUTH_REQ_NONE);
4404 if (ret != OAL_STATUS_SUCCESS) {
4405 BT_ERR("ret: %d", ret);
4407 return BLUETOOTH_ERROR_INTERNAL;
4410 return BLUETOOTH_ERROR_NONE;
4413 int _bt_gatt_read_descriptor_value(
4414 bluetooth_gatt_client_desc_prop_info_t *desc)
4416 struct gatt_server_info_t *conn_info = NULL;
4417 invocation_info_t *req_info = NULL;
4418 bluetooth_gatt_client_desc_prop_info_t *prop;
4419 oal_gatt_srvc_id_t srvc_id;
4420 oal_gatt_id_t char_id;
4421 oal_gatt_id_t desc_id;
4422 int ret = OAL_STATUS_SUCCESS;
4425 BT_CHECK_PARAMETER(desc, return);
4427 /* Check if any app is already Reading descriptors of the same char of
4428 particular service on the same remote GATT Server */
4429 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_READ_DESCRIPTOR_VALUE);
4431 prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
4432 if (prop && !memcmp(desc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4433 && memcmp(desc->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4434 && desc->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4435 && memcmp(desc->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4436 && desc->characteristic.instance_id == prop->characteristic.instance_id /* Characteristic Instance ID matched */
4437 && memcmp(desc->descriptor.uuid, prop->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Descriptor UUID matched */
4438 && desc->descriptor.instance_id == prop->descriptor.instance_id) { /* Descriptor Instance ID matched */
4439 BT_INFO("Already Descriptor value Read operation in progress for same remote GATT Server");
4440 /* Return and wait for events to be sent to all apps */
4441 return BLUETOOTH_ERROR_NONE;
4445 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4446 _bt_convert_addr_type_to_string(addr, desc->device_address.addr);
4448 /* Check if remote GATT Server is connected or not */
4449 conn_info = _bt_find_remote_gatt_server_info(addr);
4451 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4452 conn_info->addr, conn_info->connection_id);
4454 BT_ERR("GATT Server is not yet connected..");
4456 return BLUETOOTH_ERROR_NOT_CONNECTED;
4459 srvc_id.is_prmry = TRUE;
4460 srvc_id.id.inst_id = desc->svc.instance_id;
4461 memcpy(srvc_id.id.uuid.uuid, desc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4463 char_id.inst_id = desc->characteristic.instance_id;
4464 memcpy(char_id.uuid.uuid, desc->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4466 desc_id.inst_id = desc->descriptor.instance_id;
4467 memcpy(desc_id.uuid.uuid, desc->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4469 /* Search All Descriptors */
4470 ret = gattc_read_descriptor(conn_info->connection_id, &srvc_id, &char_id,
4471 &desc_id, OAL_GATT_AUTH_REQ_NONE);
4472 if (ret != OAL_STATUS_SUCCESS) {
4473 BT_ERR("ret: %d", ret);
4475 return BLUETOOTH_ERROR_INTERNAL;
4478 return BLUETOOTH_ERROR_NONE;
4482 int _bt_gatt_acquire_notify(bluetooth_gatt_client_char_prop_info_t *chr, int *fd, int *mtu)
4484 struct gatt_server_info_t *conn_info = NULL;
4485 oal_gatt_srvc_id_t srvc_id;
4486 oal_gatt_id_t char_id;
4487 int ret = OAL_STATUS_SUCCESS;
4490 BT_CHECK_PARAMETER(chr, return);
4492 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4493 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4495 /* Check if remote GATT Server is connected or not */
4496 conn_info = _bt_find_remote_gatt_server_info(addr);
4498 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4499 conn_info->addr, conn_info->connection_id);
4501 BT_ERR("GATT Server is not yet connected..");
4503 return BLUETOOTH_ERROR_NOT_CONNECTED;
4506 srvc_id.is_prmry = TRUE;
4507 srvc_id.id.inst_id = chr->svc.instance_id;
4508 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4510 char_id.inst_id = chr->characteristic.instance_id;
4511 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4513 ret = gattc_acquire_notify(conn_info->connection_id, &srvc_id, &char_id, fd, mtu);
4515 if (ret != OAL_STATUS_SUCCESS) {
4516 BT_ERR("ret: %d", ret);
4518 return BLUETOOTH_ERROR_INTERNAL;
4520 BT_INFO("GATT characterstics FD [%d] mtu[%d]", *fd, *mtu);
4522 return BLUETOOTH_ERROR_NONE;
4526 int _bt_gatt_acquire_write(bluetooth_gatt_client_char_prop_info_t *chr, int *fd, int *mtu)
4529 struct gatt_server_info_t *conn_info = NULL;
4530 oal_gatt_srvc_id_t srvc_id;
4531 oal_gatt_id_t char_id;
4532 int ret = OAL_STATUS_SUCCESS;
4535 BT_CHECK_PARAMETER(chr, return);
4537 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4538 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4540 /* Check if remote GATT Server is connected or not */
4541 conn_info = _bt_find_remote_gatt_server_info(addr);
4543 BT_DBG("GATT Server [%s] is connected, conn Id [%d]",
4544 conn_info->addr, conn_info->connection_id);
4546 BT_ERR("GATT Server is not yet connected..");
4548 return BLUETOOTH_ERROR_NOT_CONNECTED;
4551 srvc_id.is_prmry = TRUE;
4552 srvc_id.id.inst_id = chr->svc.instance_id;
4553 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4555 char_id.inst_id = chr->characteristic.instance_id;
4556 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4558 ret = gattc_acquire_write(conn_info->connection_id, &srvc_id, &char_id,
4559 OAL_GATT_AUTH_REQ_NONE, fd, mtu);
4560 if (ret != OAL_STATUS_SUCCESS) {
4561 BT_ERR("ret: %d", ret);
4563 return BLUETOOTH_ERROR_INTERNAL;
4565 BT_INFO("GATT characterstics FD [%d] mtu [%d]", *fd, *mtu);
4567 return BLUETOOTH_ERROR_NONE;
4572 /* Write Characteristic */
4573 int _bt_gatt_write_characteristic_value_by_type(
4574 bluetooth_gatt_client_char_prop_info_t *chr,
4575 bluetooth_gatt_att_data_t *data,
4576 bluetooth_gatt_write_type_e write_type)
4578 struct gatt_server_info_t *conn_info = NULL;
4579 invocation_info_t *req_info = NULL;
4580 bluetooth_gatt_client_char_prop_info_t *prop;
4581 oal_gatt_srvc_id_t srvc_id;
4582 oal_gatt_id_t char_id;
4583 int ret = OAL_STATUS_SUCCESS;
4587 BT_CHECK_PARAMETER(chr, return);
4588 BT_CHECK_PARAMETER(data, return);
4592 /* Check if any app is already writing same char of
4593 particular service on the same remote GATT Server */
4594 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE);
4596 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4597 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4598 && memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4599 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4600 && memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4601 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4602 BT_INFO("Already Characteristic Write Value operation in progress for same remote GATT Server");
4603 /* Return and wait for events to be sent to all apps */
4604 return BLUETOOTH_ERROR_NONE;
4608 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4609 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4611 /* Check if remote GATT Server is connected or not */
4612 conn_info = _bt_find_remote_gatt_server_info(addr);
4614 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4615 conn_info->addr, conn_info->connection_id);
4617 BT_ERR("GATT Server is not yet connected..");
4619 return BLUETOOTH_ERROR_NOT_CONNECTED;
4622 srvc_id.is_prmry = TRUE;
4623 srvc_id.id.inst_id = chr->svc.instance_id;
4624 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4626 char_id.inst_id = chr->characteristic.instance_id;
4627 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4629 /* Write CHar value */
4631 BT_INFO("Connection ID [%d] write type [%d] data length [%d]", conn_info->connection_id, write_type, data->length);
4632 for (k = 0; k < data->length; k++)
4633 BT_INFO("Data[%d] [0x%x]", k, data->data[k]);
4635 ret = gattc_write_characteristic(conn_info->connection_id,
4637 (oal_gatt_write_type_t)write_type, data->length,
4638 OAL_GATT_AUTH_REQ_NONE, (char *)(&data->data[0]));
4639 if (ret != OAL_STATUS_SUCCESS) {
4640 BT_ERR("ret: %d", ret);
4642 return BLUETOOTH_ERROR_INTERNAL;
4645 return BLUETOOTH_ERROR_NONE;
4648 /* Write Descriptor */
4649 int _bt_gatt_write_descriptor_value_by_type(
4650 bluetooth_gatt_client_desc_prop_info_t *desc,
4651 bluetooth_gatt_att_data_t *data,
4652 bluetooth_gatt_write_type_e write_type)
4654 struct gatt_server_info_t *conn_info = NULL;
4655 invocation_info_t *req_info = NULL;
4656 bluetooth_gatt_client_desc_prop_info_t *prop;
4657 oal_gatt_srvc_id_t srvc_id;
4658 oal_gatt_id_t char_id;
4659 oal_gatt_id_t desc_id;
4660 int ret = OAL_STATUS_SUCCESS;
4664 BT_CHECK_PARAMETER(desc, return);
4665 BT_CHECK_PARAMETER(data, return);
4669 /* Check if any app is already writing on same Descriptor of the same char of
4670 particular service on the same remote GATT Server */
4671 req_info = _bt_get_request_info_data_from_function_name(BT_GATT_WRITE_DESCRIPTOR_VALUE);
4673 prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
4674 if (prop && !memcmp(desc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4675 && memcmp(desc->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4676 && desc->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4677 && memcmp(desc->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4678 && desc->characteristic.instance_id == prop->characteristic.instance_id /* Characteristic Instance ID matched */
4679 && memcmp(desc->descriptor.uuid, prop->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Descriptor UUID matched */
4680 && desc->descriptor.instance_id == prop->descriptor.instance_id) { /* Descriptor Instance ID matched */
4681 BT_INFO("Already Descriptor value Write operation in progress for same remote GATT Server");
4682 /* Return and wait for events to be sent to all apps */
4683 return BLUETOOTH_ERROR_NONE;
4687 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4688 _bt_convert_addr_type_to_string(addr, desc->device_address.addr);
4690 /* Check if remote GATT Server is connected or not */
4691 conn_info = _bt_find_remote_gatt_server_info(addr);
4693 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4694 conn_info->addr, conn_info->connection_id);
4696 BT_ERR("GATT Server is not yet connected..");
4698 return BLUETOOTH_ERROR_NOT_CONNECTED;
4701 srvc_id.is_prmry = TRUE;
4702 srvc_id.id.inst_id = desc->svc.instance_id;
4703 memcpy(srvc_id.id.uuid.uuid, desc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4705 char_id.inst_id = desc->characteristic.instance_id;
4706 memcpy(char_id.uuid.uuid, desc->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4708 desc_id.inst_id = desc->descriptor.instance_id;
4709 memcpy(desc_id.uuid.uuid, desc->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4712 BT_INFO("Connection ID [%d] write type [%d] data length [%d]", conn_info->connection_id, write_type, data->length);
4713 for (k = 0; k < data->length; k++)
4714 BT_INFO("Data[%d] [0x%x]", k, data->data[k]);
4716 ret = gattc_write_descriptor(conn_info->connection_id,
4717 &srvc_id, &char_id, &desc_id,
4718 (oal_gatt_write_type_t)write_type, data->length,
4719 OAL_GATT_AUTH_REQ_NONE, (char *)(&data->data[0]));
4720 if (ret != OAL_STATUS_SUCCESS) {
4721 BT_ERR("ret: %d", ret);
4723 return BLUETOOTH_ERROR_INTERNAL;
4726 return BLUETOOTH_ERROR_NONE;
4729 int _bt_gatt_watch_characteristic(
4730 bluetooth_gatt_client_char_prop_info_t *chr,
4734 struct gatt_server_info_t *conn_info = NULL;
4735 oal_gatt_srvc_id_t srvc_id;
4736 oal_gatt_id_t char_id;
4737 int ret = OAL_STATUS_SUCCESS;
4740 BT_CHECK_PARAMETER(chr, return);
4742 BT_INFO("Client ID [%d] Is Notify [%d]", client_id, is_notify);
4744 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4745 _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4747 /* Check if remote GATT Server is connected or not */
4748 conn_info = _bt_find_remote_gatt_server_info(addr);
4750 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4751 conn_info->addr, conn_info->connection_id);
4753 BT_ERR("GATT Server is not yet connected..");
4755 return BLUETOOTH_ERROR_NOT_CONNECTED;
4757 srvc_id.is_prmry = TRUE;
4758 srvc_id.id.inst_id = chr->svc.instance_id;
4759 memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4761 char_id.inst_id = chr->characteristic.instance_id;
4762 memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4764 /* Register or unregister Notification characteristic */
4766 ret = gattc_register_for_notification(conn_info->client_id,
4767 (bt_address_t*)&(chr->device_address),
4768 &srvc_id, &char_id);
4770 ret = gattc_deregister_for_notification(conn_info->client_id,
4771 (bt_address_t*)&(chr->device_address),
4772 &srvc_id, &char_id);
4774 BT_INFO("Result[%d]", ret);
4775 if (ret != OAL_STATUS_SUCCESS) {
4776 BT_ERR("ret: %d", ret);
4778 return BLUETOOTH_ERROR_INTERNAL;
4781 return BLUETOOTH_ERROR_NONE;
4785 int _bt_disconnect_le_device(bluetooth_device_address_t *address,
4788 struct gatt_server_info_t *conn_info = NULL;
4789 struct gatt_client_info_t *rem_client_conn_info = NULL;
4790 invocation_info_t *req_info = NULL;
4791 int ret = OAL_STATUS_SUCCESS;
4793 char *remote_address = NULL;
4795 BT_CHECK_PARAMETER(address, return);
4799 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4800 _bt_convert_addr_type_to_string(addr, address->addr);
4801 BT_INFO("GATT Client Disconnect request for address [%s]", addr);
4803 /* Check if Remote Device is already under connection progress */
4804 req_info = _bt_get_request_info_data_from_function_name(BT_DISCONNECT_LE);
4806 remote_address = (char*)req_info->user_data;
4807 if (remote_address && !strcasecmp(remote_address, addr)) {/* Address matched */
4808 BT_INFO("Already DisConnection ongoing for same remote GATT Server address [%s]", remote_address);
4809 /* Return success and wait for events to be sent to all apps */
4811 return BLUETOOTH_ERROR_IN_PROGRESS;
4814 /* Check if remote GATT Server is connected or not */
4815 conn_info = _bt_find_remote_gatt_server_info(addr);
4817 /* Check if app sent 0 client id for Disconnection, in such case, use default gatt client ID */
4818 if (client_id == 0) {
4819 BT_INFO("GATT CLient Disconnect request sent by an app without any client instance [%d]",
4821 BT_INFO("Assign default GATT client id [%d]", gatt_default_client);
4822 client_id = gatt_default_client;
4825 BT_INFO("Disconnect remote gatt server using CLient ID [%d] Connection ID [%d]", client_id, conn_info->connection_id);
4826 ret = gattc_disconnect(client_id, (bt_address_t*)(address),
4827 conn_info->connection_id);
4829 /* check if remote client is connected */
4830 rem_client_conn_info = _bt_find_remote_gatt_client_info(addr);
4832 if (!rem_client_conn_info || client_id != 0) {
4833 BT_ERR("GATT device is not connected..");
4835 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
4838 BT_INFO("Disconnect remote gatt client ");
4840 ret = gatts_disconnect(rem_client_conn_info->instance_id,
4841 (bt_address_t*)(address), rem_client_conn_info->connection_id);
4844 if (ret != OAL_STATUS_SUCCESS) {
4845 BT_ERR("ret: %d", ret);
4847 return BLUETOOTH_ERROR_INTERNAL;
4850 return BLUETOOTH_ERROR_NONE;
4853 int _bt_gatt_watch_service_changed_indication(const char *sender,
4854 bluetooth_device_address_t *address,
4855 gboolean is_enabled)
4858 bt_service_app_info_t *info = NULL;
4860 BT_INFO("%s Servic changed Indication watcher for app [%s]",
4861 is_enabled ? "Enable":"Disable", sender);
4863 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
4866 if (g_strcmp0(sender, info->sender) == 0 &&
4867 memcmp(info->address.addr, address->addr,
4868 sizeof(bluetooth_device_address_t)) == 0) {
4869 BT_DBG("Found GATT client App.. [%s], sender [%s]", info->uuid, info->sender);
4870 info->is_watcher_enabled = is_enabled;
4874 return BLUETOOTH_ERROR_NONE;
4877 int _bt_unregister_gatt_client_instance(const char *sender, int client_id)
4879 bt_service_app_info_t *info = NULL;
4882 BT_INFO("Unregister Allocated GATT Client instance [%s] Client ID [%d]", sender, client_id);
4884 /* Unregister CLient instance associated with address X. It is possible that another app still
4885 has client_id valid for same remote address */
4886 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
4889 /* Exact matching of sender */
4890 if (!g_strcmp0(info->sender, sender) && info->client_id == client_id) { /* Check for only valid GATT client Instance */
4891 BT_INFO("Unregister GATT client instance [%d]", info->client_id);
4892 numapps[k].client_id = -1;
4893 numapps[k].is_initialized = FALSE;
4894 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
4895 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
4896 memset(&numapps[k].address.addr, 0x00, sizeof(bluetooth_device_address_t));
4898 /* Its a GATT Client Instance */
4899 ret = gattc_deregister(client_id);
4900 if (ret != OAL_STATUS_SUCCESS) {
4901 BT_ERR("DeAllocate GATT Client instance with stack Fail ret: %d", ret);
4902 return BLUETOOTH_ERROR_INTERNAL;
4904 return BLUETOOTH_ERROR_NONE;
4909 return BLUETOOTH_ERROR_NOT_FOUND;
4912 static void __bt_handle_client_mtu_exchange_completed(event_gattc_mtu_configured_t *event_data)
4914 int result = BLUETOOTH_ERROR_NONE;
4915 struct gatt_server_info_t *conn_info = NULL;
4916 GVariant *param = NULL;
4920 conn_info = __bt_find_remote_gatt_server_info_from_conn_id(event_data->conn_id);
4921 if (conn_info == NULL) {
4922 BT_ERR("Cant find connection Information");
4926 BT_INFO("GATT Client: MTU Configured from addr [%s] status [%d] MTU size [%d]",
4927 conn_info->addr, event_data->status, event_data->mtu);
4929 if (event_data->status != OAL_STATUS_SUCCESS)
4930 result = BLUETOOTH_ERROR_INTERNAL;
4932 BT_INFO("reply BT_REQ_ATT_MTU ");
4934 /* DBUS Return fo BT_REQ_ATT_MTU for all the apps */
4935 __bt_gatt_handle_pending_request_info(result, BT_REQ_ATT_MTU, conn_info->addr,
4936 BT_ADDRESS_STRING_SIZE);
4938 if (result == BLUETOOTH_ERROR_NONE) {
4939 BT_INFO("sending mtu event");
4941 mtu = event_data->mtu;
4942 param = g_variant_new("(isqy)",
4948 /* Send event to BT-API */
4949 _bt_send_event(BT_DEVICE_EVENT,
4950 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
4953 /* Update the MTU for current connection */
4954 BT_INFO("Updating the MTU for current connection");
4955 __bt_update_mtu_gatt_device(conn_info->addr, event_data->mtu);
4959 static struct gatt_mtu_info_t *__bt_find_mtu_gatt_device(char *address)
4962 struct gatt_mtu_info_t *info = NULL;
4966 for (l = gatt_mtu_info_list; l != NULL; l = g_slist_next(l)) {
4967 info = (struct gatt_mtu_info_t*)l->data;
4971 if (!g_strcmp0(info->addr, address)) {
4972 BT_INFO("Remote GATT device found addr[%s]", info->addr);
4977 BT_INFO("Not found Remote GATT device addr[%s]", address);
4981 static void __bt_remove_mtu_gatt_device(char *address)
4983 struct gatt_mtu_info_t *dev_info = NULL;
4987 dev_info = __bt_find_mtu_gatt_device(address);
4990 BT_INFO("removing the gatt device from mtu list");
4991 gatt_mtu_info_list = g_slist_remove(gatt_mtu_info_list, dev_info);
4992 g_free(dev_info->addr);
4997 static void __bt_add_mtu_gatt_device(char *address)
4999 struct gatt_mtu_info_t *dev_info = NULL;
5003 dev_info = __bt_find_mtu_gatt_device(address);
5006 BT_INFO("adding the gatt device in mtu list");
5007 dev_info = g_new0(struct gatt_mtu_info_t, 1);
5008 dev_info->addr = g_strdup(address);
5009 dev_info->att_mtu = BT_DEFAULT_ATT_MTU;
5010 gatt_mtu_info_list = g_slist_append(gatt_mtu_info_list, dev_info);
5014 static void __bt_update_mtu_gatt_device(char *address, int mtu)
5016 struct gatt_mtu_info_t *dev_info = NULL;
5020 dev_info = __bt_find_mtu_gatt_device(address);
5023 BT_INFO("Udating the mtu");
5024 dev_info->att_mtu = mtu;