2 * Open Adaptation Layer (OAL)
4 * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
20 #include <bluetooth.h>
22 #include "oal-event.h"
23 #include "oal-internal.h"
24 #include "oal-manager.h"
25 #include "oal-adapter-mgr.h"
26 #include "oal-utils.h"
28 #include "oal-common.h"
30 #define DEV_APPEARANCE 0x0000
31 #define DEFAULT_GATT_CLIENT_UUID "0000A00A-1111-1111-0123-456789ABCDEF"
32 #define MAX_PROP_LEN 95 /* SVACE WGID: 324403*/
33 #define MAX_PERM_LEN 80 //Fix for svace wgid:324402
35 #define CHECK_OAL_GATT_ENABLED() \
37 if (gatt_api == NULL) { \
38 BT_ERR("GATT Not Enabled"); \
39 return OAL_STATUS_NOT_READY; \
43 #define CHECK_SERVER_INSTANCE(instance_id) \
45 if (instance_id < 1) { \
46 BT_ERR("Invalid Instance"); \
47 return OAL_STATUS_INVALID_PARAM; \
51 #define CHECK_CORRECT_SERVER_ID(server_if, server_inst) \
53 gatt_server_t *info; \
54 info = __gatts_find_server_instance(server_if); \
56 server_inst = server_if; \
61 #define PRINT_ADV_DATA(length, manuf_data, data_type) \
63 char * buf_str = g_malloc0(3*length + 1);\
64 if (NULL != buf_str) { \
65 convert_hex_2_str(manuf_data, length, buf_str);\
66 if (TRUE == data_type) \
67 BT_INFO("Scan Response Data:%s\n", buf_str);\
69 BT_INFO("Advertising Data:%s\n", buf_str);\
74 #define CHECK_CLIENT_REGISTRATION(client_id) \
76 if (client_id <= 0) { \
77 BT_DBG("No client registered yet...");\
78 return OAL_STATUS_INTERNAL_ERROR;\
82 #define CHECK_CLIENT_CONNECTION(conn_id) \
85 BT_ERR("No active connection");\
86 return OAL_STATUS_INTERNAL_ERROR;\
93 GATT_INS_DATA_SETTING,
103 gboolean cur_adv_state;
106 const char *oal_device_type[] = {
117 } oal_pending_gattc_req_e;
119 static const btgatt_interface_t * gatt_api;
120 static GSList *gatt_servers = NULL; /* List of gatt_server_t */
122 /* Forward declarations of GATT Server callbacks */
123 static void cb_gatts_register_app(int status, int server_if, bt_uuid_t *uuid);
124 static void cb_gatts_multi_adv_enable(int server_if, int status);
125 static void cb_gatts_multi_adv_set_inst_data(int server_if, int status);
126 static void cb_gatts_multi_adv_disable(int server_if, int status);
127 static void cb_gatts_multi_adv_update(int server_if, int status);
129 static void cb_gatts_listen(int status, int server_if);
132 static void cb_gatts_service_added(int status, int server_if, btgatt_srvc_id_t *srvc_id, int srvc_handle);
133 static void cb_gatts_included_service_added(int status, int server_if, int srvc_handle, int incl_srvc_handle);
134 static void cb_gatts_characteristic_added(int status, int server_if, bt_uuid_t *char_id, int srvc_handle, int char_handle);
135 static void cb_gatts_descriptor_added(int status, int server_if, bt_uuid_t *descr_id, int srvc_handle, int descr_handle);
137 static void cb_gatts_service_started(int status, int server_if, int srvc_handle);
138 static void cb_gatts_service_stopped(int status, int server_if, int srvc_handle);
139 static void cb_gatts_service_deleted(int status, int server_if, int srvc_handle);
141 static void cb_gatts_request_read(int conn_id, int trans_id, bt_bdaddr_t *bda, int attr_handle, int offset, bool is_long);
142 static void cb_gatts_request_write(int conn_id, int trans_id, bt_bdaddr_t *bda, int attr_handle, int offset, int length,
143 bool need_rsp, bool is_prep, uint8_t* value);
144 static void cb_gatts_response_confirmation(int status, int handle);
146 static void cb_gatts_acquire_write(int fd, int conn_id, int trans_id, int attr_handle, bt_bdaddr_t*);
147 static void cb_gatts_acquire_notify(int fd, int conn_id, int trans_id, int attr_handle, bt_bdaddr_t*);
149 static void cb_indication_confirmation(int conn_id, int trans_id, int attr_handle, bt_bdaddr_t *bda);
150 static void cb_gatts_connection(int conn_id, int server_if, int connected, bt_bdaddr_t *bda);
152 static void cb_gatts_mtu_changed(int conn_id, int mtu);
155 static void cb_notifcation_changed(int conn_id, int trans_id, int attr_handle, bool notify, bt_bdaddr_t *bda);
158 /************************************HAL Interface *************************************/
160 /*TODO GATT Server callbacks will be implemented in subsequent patches */
161 static const btgatt_server_callbacks_t btgatt_server_callbacks = {
162 .register_server_cb = cb_gatts_register_app,
163 .connection_cb = cb_gatts_connection,
164 .service_added_cb = cb_gatts_service_added,
165 .included_service_added_cb = cb_gatts_included_service_added,
166 .characteristic_added_cb = cb_gatts_characteristic_added,
167 .descriptor_added_cb = cb_gatts_descriptor_added,
168 .service_started_cb = cb_gatts_service_started,
169 .service_stopped_cb = cb_gatts_service_stopped,
170 .service_deleted_cb = cb_gatts_service_deleted,
171 .indication_confirmation_cb = cb_indication_confirmation,
172 .request_read_cb = cb_gatts_request_read,
173 .request_write_cb = cb_gatts_request_write,
174 .request_exec_write_cb = NULL,
175 .response_confirmation_cb = cb_gatts_response_confirmation,
176 .listen_cb = cb_gatts_listen,
177 .multi_adv_enable_cb = cb_gatts_multi_adv_enable,
178 .multi_adv_update_cb = cb_gatts_multi_adv_update,
179 .multi_adv_data_cb = cb_gatts_multi_adv_set_inst_data,
180 .multi_adv_disable_cb = cb_gatts_multi_adv_disable,
181 .mtu_changed_cb = cb_gatts_mtu_changed,
183 .notif_enabled_cb = cb_notifcation_changed,
185 .request_acquire_write_cb = cb_gatts_acquire_write,
186 .request_acquire_notify_cb = cb_gatts_acquire_notify
189 /* Forward declaration for GATT client callbacks */
190 static void cb_gattc_register_app(int status, int clientIf, bt_uuid_t *app_uuid);
191 static void cb_gattc_scan_result(bt_bdaddr_t* bdaddress, uint8_t addr_type, int rssi,
192 uint8_t *adv_data, int adv_data_len, uint8_t *scan_rsp_data, int scan_rsp_data_len);
193 static void cb_gattc_connection(int conn_id, int status, int client_if, bt_bdaddr_t* bda);
194 static void cb_gattc_disconnect(int conn_id, int status, int client_if, bt_bdaddr_t* bda);
195 static void cb_gattc_search_result(int conn_id, btgatt_srvc_id_t *srvc_id);
196 static void cb_gattc_search_complete(int conn_id, int status);
197 static void cb_gattc_get_characteristics(int conn_id, int status, btgatt_srvc_id_t *srvc_id,
198 btgatt_gatt_id_t *char_id, int char_prop);
199 static void cb_gattc_get_descriptor(int conn_id, int status, btgatt_srvc_id_t *srvc_id,
200 btgatt_gatt_id_t *char_id, btgatt_gatt_id_t *descr_id);
201 static void cb_gattc_read_characteristic(int conn_id, int status, btgatt_read_params_t *p_data);
202 static void cb_gattc_read_descriptor(int conn_id, int status, btgatt_read_params_t *p_data);
203 static void cb_gattc_write_characteristic(int conn_id, int status, btgatt_write_params_t *p_data);
204 static void cb_gattc_write_descriptor(int conn_id, int status, btgatt_write_params_t *p_data);
205 static void cb_gattc_register_for_notification(int conn_id, int registered, int status,
206 btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id);
207 static void cb_gattc_notify(int conn_id, btgatt_notify_params_t *p_data);
208 static void cb_gattc_service_changed(bt_bdaddr_t *bd_addr, int change_type, uint8_t *uuid, int conn_id, int inst_id);
209 static void cb_gattc_configure_mtu_cmpl(int conn_id, int status, int mtu);
211 /*TODO GATT CLient callbacks will be implemented in subsequent patches */
212 static const btgatt_client_callbacks_t btgatt_client_callbacks = {
213 .register_client_cb = cb_gattc_register_app,
214 .scan_result_cb = cb_gattc_scan_result,
215 .open_cb = cb_gattc_connection,
216 .close_cb = cb_gattc_disconnect,
217 .search_complete_cb = cb_gattc_search_complete,
218 .search_result_cb = cb_gattc_search_result,
219 .get_characteristic_cb = cb_gattc_get_characteristics,
220 .get_descriptor_cb = cb_gattc_get_descriptor,
221 .get_included_service_cb = NULL,
222 .register_for_notification_cb = cb_gattc_register_for_notification,
223 .notify_cb = cb_gattc_notify,
224 .service_changed_cb = cb_gattc_service_changed,
225 .read_characteristic_cb = cb_gattc_read_characteristic,
226 .write_characteristic_cb = cb_gattc_write_characteristic,
227 .read_descriptor_cb = cb_gattc_read_descriptor,
228 .write_descriptor_cb = cb_gattc_write_descriptor,
229 .execute_write_cb = NULL,
230 .read_remote_rssi_cb = NULL,
231 .configure_mtu_cb = cb_gattc_configure_mtu_cmpl,
232 #ifdef PLATFORM_ANDROID_HAL
233 .scan_filter_cfg_cb = NULL,
234 .scan_filter_param_cb = NULL,
235 .scan_filter_status_cb = NULL,
236 .congestion_cb = NULL,
237 .batchscan_cfg_storage_cb = NULL,
238 .batchscan_enb_disable_cb = NULL,
239 .batchscan_reports_cb = NULL,
240 .batchscan_threshold_cb = NULL,
241 .track_adv_event_cb = NULL,
245 static btgatt_callbacks_t btgatt_callbacks = {
246 sizeof(btgatt_callbacks_t),
247 &btgatt_client_callbacks,
248 &btgatt_server_callbacks
251 /*******************************GATT Initialisation - Deinitialisation********************************/
252 oal_status_t gatt_enable(void)
254 const bt_interface_t * blued_api;
257 /* Get stack interface */
258 blued_api = (const bt_interface_t *) adapter_get_stack_interface();
260 if (blued_api == NULL) {
261 BT_ERR("Stack is not initialized");
262 return OAL_STATUS_NOT_READY;
266 BT_WARN("GATT Interface is already initialized...");
267 return OAL_STATUS_ALREADY_DONE;
270 gatt_api = (const btgatt_interface_t *)blued_api->get_profile_interface(BT_PROFILE_GATT_ID);
271 if (gatt_api == NULL) {
272 BT_ERR("GATT interface failed");
273 return OAL_STATUS_INTERNAL_ERROR;
276 if ((ret = gatt_api->init(&btgatt_callbacks)) != BT_STATUS_SUCCESS) {
277 BT_ERR("Error: Unable to initialise GATT :%s", status2string(ret));
280 return convert_to_oal_status(ret);
283 BT_INFO("GATT successfully initialized");
284 return OAL_STATUS_SUCCESS;
287 oal_status_t gatt_disable(void)
294 g_slist_free_full(gatt_servers, g_free);
296 return OAL_STATUS_SUCCESS;
299 static gatt_server_t *__gatts_find_server_instance(int instance_id)
304 for (l = gatt_servers; l; l = g_slist_next(l)) {
308 if (info->instance_id == instance_id)
315 /************************************GATT Server Functions*************************************/
317 oal_status_t gatts_register(oal_uuid_t* server_uuid)
319 char str[2*BT_UUID_STRING_MAX];
320 int ret = OAL_STATUS_SUCCESS;
323 CHECK_OAL_GATT_ENABLED();
324 uuid_to_stringname((service_uuid_t*)server_uuid->uuid, str);
325 API_TRACE("Register the server instance: UUID: [%s]", str);
327 for (l = gatt_servers; l; l = g_slist_next(l)) {
328 gatt_server_t *info = l->data;
331 if (memcmp(server_uuid->uuid, info->uuid.uuid, sizeof(oal_uuid_t)) == 0) {
332 if (info->instance_id != -1) {
333 BT_ERR("This is resevered UUID for easy set up application \
334 instance_id = %d", info->instance_id);
335 return OAL_STATUS_ALREADY_DONE;
340 ret = gatt_api->server->register_server((bt_uuid_t*)server_uuid);
342 if (ret != BT_STATUS_SUCCESS) {
343 BT_ERR("GATT server registration with UUID:%s failed: %s", str, status2string(ret));
344 return convert_to_oal_status(ret);
346 return OAL_STATUS_SUCCESS;
349 oal_status_t gatts_unregister(int instance_id)
351 int ret = OAL_STATUS_SUCCESS;
353 API_TRACE("Unregister the registered server, instance_id: %d", instance_id);
355 CHECK_OAL_GATT_ENABLED();
356 CHECK_SERVER_INSTANCE(instance_id);
358 info = __gatts_find_server_instance(instance_id);
360 return OAL_STATUS_INTERNAL_ERROR;
362 ret = gatt_api->server->unregister_server(instance_id);
363 if (ret != BT_STATUS_SUCCESS) {
364 BT_ERR("GATT server unregistration failed: %d", instance_id);
365 return convert_to_oal_status(ret);
368 info->instance_id = -1;
369 info->state = GATT_INS_DISABLED;
370 info->cur_adv_state = FALSE;
371 if (instance_id != 1 && instance_id != 2) {
372 gatt_servers = g_slist_remove(gatt_servers, info);
373 BT_DBG("GATT Server Removed. count of gatt_servers: %d",
374 g_slist_length(gatt_servers));
377 return OAL_STATUS_SUCCESS;
380 /* Legacy Advertisement */
381 oal_status_t gatts_start_listen(int instance_id, gboolean enable)
383 int ret = OAL_STATUS_SUCCESS;
385 API_TRACE("instance_id: %d enable: %d", instance_id, enable);
387 CHECK_OAL_GATT_ENABLED();
388 CHECK_SERVER_INSTANCE(instance_id);
389 info = __gatts_find_server_instance(instance_id);
391 return OAL_STATUS_INTERNAL_ERROR;
393 ret = gatt_api->server->listen(instance_id, enable);
394 if (ret != BT_STATUS_SUCCESS) {
395 BT_ERR("Error:Advertising %s on instance_id: %d failed \
396 with status: %s, cur_adv_state: %d",
397 enable ? "Start" : "Stop", instance_id,
398 status2string(ret), info->cur_adv_state);
399 return convert_to_oal_status(ret);
402 return OAL_STATUS_SUCCESS;
405 oal_status_t gatts_stop_advertising(int instance_id)
407 int ret = OAL_STATUS_SUCCESS;
408 API_TRACE("Stop advertising");
410 CHECK_OAL_GATT_ENABLED();
411 CHECK_SERVER_INSTANCE(instance_id);
412 if (__gatts_find_server_instance(instance_id) == NULL)
413 return OAL_STATUS_INTERNAL_ERROR;
415 ret = gatt_api->server->listen(instance_id, FALSE);
416 if (ret != BT_STATUS_SUCCESS) {
417 BT_ERR("Stop Advertising failed for server Instance:%d With Status: %s",
418 instance_id, status2string(ret));
419 return convert_to_oal_status(ret);
421 return OAL_STATUS_SUCCESS;
424 oal_status_t gatts_multi_adv_enable(int instance_id)
426 int ret = OAL_STATUS_SUCCESS;
428 API_TRACE("Start Multi advertising, instance_id: %d", instance_id);
430 CHECK_OAL_GATT_ENABLED();
431 CHECK_SERVER_INSTANCE(instance_id);
432 info = __gatts_find_server_instance(instance_id);
434 return OAL_STATUS_INTERNAL_ERROR;
436 if (gatt_api->server->multi_adv_enable == NULL)
437 return OAL_STATUS_NOT_SUPPORT;
439 /* Platform Requirement: Advertising to be enabled even when no data is set: i.e Empty Advertisement
440 if (gatt_servers[instance_id - 1].state != GATT_INS_DATA_SET){
441 BT_ERR("Not allowed, state: %d, instance_id: %d",
442 gatt_servers[instance_id - 1].state, instance_id);
443 return OAL_STATUS_BUSY;
447 if (info->state == GATT_INS_ENABLED) {
448 BT_ERR("Not allowed, state: %d, instance_id: %d", info->state, instance_id);
449 return OAL_STATUS_ALREADY_DONE;
450 } else if (info->state == GATT_INS_ENABLING) {
451 BT_ERR("Not allowed, state: %d, instance_id: %d", info->state, instance_id);
452 return OAL_STATUS_BUSY;
454 info->state = GATT_INS_ENABLING;
456 ret = gatt_api->server->multi_adv_enable(instance_id);
457 if (ret != BT_STATUS_SUCCESS) {
458 BT_ERR("GATT enable multi advertising failed for server Instance: %d With Status: %s",
459 instance_id, status2string(ret));
460 // gatt_servers[instance_id - 1].state = GATT_INS_DATA_SET;
461 return convert_to_oal_status(ret);
463 return OAL_STATUS_SUCCESS;
466 oal_status_t gatts_multi_adv_disable(int instance_id)
468 int ret = OAL_STATUS_SUCCESS;
470 API_TRACE("Stop Multi advertising, instance_id: %d", instance_id);
472 CHECK_OAL_GATT_ENABLED();
473 CHECK_SERVER_INSTANCE(instance_id);
474 info = __gatts_find_server_instance(instance_id);
476 return OAL_STATUS_INTERNAL_ERROR;
478 if (gatt_api->server->multi_adv_disable == NULL)
479 return OAL_STATUS_NOT_SUPPORT;
481 if (info->state != GATT_INS_ENABLED) {
482 BT_ERR("Not Allowed, state: %d, instance_id: %d", info->state, instance_id);
483 return OAL_STATUS_BUSY;
486 ret = gatt_api->server->multi_adv_disable(instance_id);
487 if (ret != BT_STATUS_SUCCESS) {
488 BT_ERR("GATT disable multi advertising failed for server Instance: %d With Status: %s",
489 instance_id, status2string(ret));
490 return convert_to_oal_status(ret);
492 return OAL_STATUS_SUCCESS;
495 oal_status_t gatts_set_filter_policy(int filter_policy)
497 int ret = OAL_STATUS_SUCCESS;
499 CHECK_OAL_GATT_ENABLED();
501 /* send the filter_policy value to the HAL Layer */
502 ret = gatt_api->server->set_filter_policy(filter_policy);
504 if (ret != BT_STATUS_SUCCESS) {
505 BT_ERR("set_filter_policy failed: %d",ret);
506 return convert_to_oal_status(ret);
509 return OAL_STATUS_SUCCESS;
512 oal_status_t gatts_multi_adv_update(int instance_id,
513 int min_intv, int max_intv,
514 int adv_type, int chnl_map,
515 int tx_power, int timeout_s)
517 int ret = OAL_STATUS_SUCCESS;
518 API_TRACE("Multi advertising Update");
520 CHECK_OAL_GATT_ENABLED();
521 CHECK_SERVER_INSTANCE(instance_id);
522 if (__gatts_find_server_instance(instance_id) == NULL)
523 return OAL_STATUS_INTERNAL_ERROR;
525 if (gatt_api->server->multi_adv_update == NULL)
526 return OAL_STATUS_NOT_SUPPORT;
528 ret = gatt_api->server->multi_adv_update(instance_id,
531 tx_power, timeout_s);
532 if (ret != BT_STATUS_SUCCESS) {
533 BT_ERR("GATT update multi advertising failed for server Instance: %d With Status: %s",
534 instance_id, status2string(ret));
535 return convert_to_oal_status(ret);
537 return OAL_STATUS_SUCCESS;
540 oal_status_t gatts_multi_adv_set_inst_data(int instance_id,
541 oal_ble_multi_adv_param_setup_t *adv_param_setup)
543 int ret = OAL_STATUS_SUCCESS;
544 btgatt_adv_param_setup_t adv_setup;
546 API_TRACE("Set Multi advertising data, instance_id: %d", instance_id);
548 CHECK_OAL_GATT_ENABLED();
549 CHECK_SERVER_INSTANCE(instance_id);
550 info = __gatts_find_server_instance(instance_id);
552 return OAL_STATUS_INTERNAL_ERROR;
554 if (gatt_api->server->multi_adv_set_inst_data == NULL)
555 return OAL_STATUS_NOT_SUPPORT;
557 if (info->state >= GATT_INS_DATA_SETTING && info->state != GATT_INS_DATA_SET) {
558 BT_ERR("Not Allowed, state: %d, instance_id: %d", info->state, instance_id);
559 return OAL_STATUS_BUSY;
561 info->state = GATT_INS_DATA_SETTING;
563 adv_setup.set_scan_rsp = adv_param_setup->set_scan_rsp;
564 adv_setup.include_name = adv_param_setup->include_name;
565 adv_setup.include_txpower = adv_param_setup->include_txpower;
567 adv_setup.include_appearance = adv_param_setup->include_appearance;
569 adv_setup.appearance = adv_param_setup->appearance;
570 adv_setup.manufacturer_data = adv_param_setup->manufacturer_data;
571 adv_setup.manufacturer_data_len = adv_param_setup->manufacturer_data_len;
572 adv_setup.service_data = adv_param_setup->service_data;
573 adv_setup.service_data_len = adv_param_setup->service_data_len;
574 adv_setup.service_uuid = adv_param_setup->service_uuid;
575 adv_setup.service_uuid_len = adv_param_setup->service_uuid_len;
576 /* Solicit UUID handler: Start */
577 adv_setup.solicit_uuid = adv_param_setup->solicit_uuid;
578 adv_setup.solicit_uuid_len = adv_param_setup->solicit_uuid_len;
579 /* Solicit UUID handler: End */
580 adv_setup.min_interval = adv_param_setup->min_interval;
581 adv_setup.max_interval = adv_param_setup->min_interval;
582 adv_setup.adv_type = adv_param_setup->adv_type;
583 adv_setup.chnl_map = adv_param_setup->chnl_map;
584 adv_setup.tx_power = adv_param_setup->tx_power;
585 adv_setup.timeout_s = adv_param_setup->timeout_s;
586 adv_setup.tds_data = adv_param_setup->tds_data;
587 adv_setup.tds_data_len = adv_param_setup->tds_data_len;
589 adv_setup.server_if = instance_id;
591 ret = gatt_api->server->multi_adv_set_inst_data(adv_setup);
592 if (ret != BT_STATUS_SUCCESS) {
593 BT_ERR("GATT Set Multi advertising data failed: %s", status2string(ret));
594 info->state = GATT_INS_DISABLED;
595 return convert_to_oal_status(ret);
597 return OAL_STATUS_SUCCESS;
600 oal_status_t gatts_disconnect(int instance_id, bt_address_t *device_address, int conn_id)
602 int ret = OAL_STATUS_SUCCESS;
605 API_TRACE("Server Disonnect: instance_id = %d, conn_id = %d", instance_id, conn_id);
607 CHECK_OAL_GATT_ENABLED();
608 CHECK_SERVER_INSTANCE(instance_id);
609 if (__gatts_find_server_instance(instance_id) == NULL)
610 return OAL_STATUS_INTERNAL_ERROR;
612 API_TRACE("[%s]", bdt_bd2str(device_address, &bdstr));
614 ret = gatt_api->server->disconnect(instance_id, (bt_bdaddr_t *) device_address, conn_id);
615 if (ret != BT_STATUS_SUCCESS) {
616 BT_ERR("GATT Server disconnect failed: %s", status2string(ret));
617 return convert_to_oal_status(ret);
619 return OAL_STATUS_SUCCESS;
623 /************************************GATT Server Functions*************************************/
625 static void cb_gatts_register_app(int status, int server_if, bt_uuid_t *uuid)
627 char str[2*BT_UUID_STRING_MAX];
628 uuid_to_stringname((service_uuid_t*)uuid, str);
630 event_gatts_register_t *event = g_malloc0(sizeof(event_gatts_register_t));
632 BT_INFO("BTGATT SERVER REGISTER APP CB, UUID:%s, status:%d, server_if:%d",
633 str, status, server_if);
635 gatt_server_t *info = g_malloc0(sizeof(gatt_server_t));
636 info->instance_id = server_if;
637 memcpy(info->uuid.uuid, uuid->uu, sizeof(bt_uuid_t));
638 gatt_servers = g_slist_append(gatt_servers, info);
639 BT_DBG("GATT Server Added. count of gatt_servers: %d", g_slist_length(gatt_servers));
641 event->server_inst = server_if;
642 memcpy(event->server_uuid.uuid, uuid->uu, sizeof(bt_uuid_t));
643 send_event(OAL_EVENT_BLE_SERVER_INSTANCE_INITIALISED, event, sizeof(event_gatts_register_t));
646 static void cb_gatts_listen(int status, int server_if)
649 gboolean new_state = FALSE;
650 event_ble_adv_status *event = g_malloc0(sizeof(event_ble_adv_status));
653 for (l = gatt_servers; l; l = g_slist_next(l)) {
654 gatt_server_t *info = l->data;
657 if (info->instance_id == server_if) {
658 event->server_inst = server_if;
659 prev_state = info->cur_adv_state;
660 new_state = (status == BT_STATUS_SUCCESS) ? !prev_state : prev_state;
661 info->cur_adv_state = new_state;
666 BT_INFO("Adv State of server instance %d, new_state: %d", server_if, new_state);
668 if (TRUE == new_state)
669 send_event(OAL_EVENT_BLE_ADVERTISING_STARTED, event, sizeof(event_ble_adv_status));
671 send_event(OAL_EVENT_BLE_ADVERTISING_STOPPED, event, sizeof(event_ble_adv_status));
674 static void cb_gatts_multi_adv_enable(int server_if, int status)
677 BT_INFO("BTGATT SERVER MULTI ADV ENABLE CB, status:%d, srv_if: %d", status, server_if);
678 event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
681 if (status != BT_STATUS_SUCCESS)
682 BT_ERR("MULTI ADV ENABLE failed, status:%d, srv_if: %d", status, server_if);
684 for (l = gatt_servers; l; l = g_slist_next(l)) {
685 gatt_server_t *info = l->data;
688 if (info->instance_id == server_if) {
689 event->server_inst = server_if;
690 info->state = GATT_INS_ENABLED;
694 event->server_inst = -1;
696 event->status = convert_to_oal_status(status);
697 send_event(OAL_EVENT_BLE_MULTI_ADVERTISING_ENABLE, event, sizeof(event_ble_multiadv_status));
700 static void cb_gatts_multi_adv_disable(int server_if, int status)
702 BT_INFO("BTGATT SERVER MULTI ADV DISABLE CB, status:%d, srv_if: %d", status, server_if);
703 event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
706 if (status != BT_STATUS_SUCCESS)
707 BT_ERR("MULTI ADV DISABLE failed, status:%d, srv_if: %d", status, server_if);
709 for (l = gatt_servers; l; l = g_slist_next(l)) {
710 gatt_server_t *info = l->data;
713 if (info->instance_id == server_if) {
714 event->server_inst = server_if;
715 info->state = GATT_INS_DISABLED;
719 event->server_inst = -1;
721 event->status = convert_to_oal_status(status);
722 send_event(OAL_EVENT_BLE_MULTI_ADVERTISING_DISABLE, event, sizeof(event_ble_multiadv_status));
725 static void cb_gatts_multi_adv_update(int server_if, int status)
727 BT_INFO("BTGATT SERVER MULTI ADV UPDATE CB, status:%d, srv_if: %d", status, server_if);
728 event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
731 if (status != BT_STATUS_SUCCESS)
732 BT_ERR("MULTI ADV UPDATE failed, status:%d, srv_if: %d", status, server_if);
734 for (l = gatt_servers; l; l = g_slist_next(l)) {
735 gatt_server_t *info = l->data;
738 if (info->instance_id == server_if) {
739 event->server_inst = server_if;
743 event->server_inst = -1;
745 event->status = convert_to_oal_status(status);
746 send_event(OAL_EVENT_BLE_MULTI_ADVERTISING_UPDATE, event, sizeof(event_ble_multiadv_status));
749 static void cb_gatts_multi_adv_set_inst_data(int server_if, int status)
751 BT_INFO("BTGATT SERVER MULTI ADV SET INST DATA CB, status:%d, srv_if: %d", status, server_if);
752 event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
755 if (status != BT_STATUS_SUCCESS)
756 BT_ERR("MULTI ADV SET DATA failed, status:%d, srv_if: %d", status, server_if);
758 for (l = gatt_servers; l; l = g_slist_next(l)) {
759 gatt_server_t *info = l->data;
762 if (info->instance_id == server_if) {
763 event->server_inst = server_if;
764 info->state = GATT_INS_DATA_SET;
768 event->server_inst = -1;
770 event->status = convert_to_oal_status(status);
771 send_event(OAL_EVENT_BLE_MULTI_ADVERTISING_SET_INST_DATA, event, sizeof(event_ble_multiadv_status));
774 oal_status_t gatts_get_att_mtu(int conn_id, int *mtu)
776 int ret = OAL_STATUS_SUCCESS;
777 API_TRACE("Get ATT MTU, conn_id: %d", conn_id);
778 CHECK_OAL_GATT_ENABLED();
779 OAL_CHECK_PARAMETER(mtu, return);
781 /* To prevent crash in case other libraries not support this api */
782 if (gatt_api->server->get_att_mtu == NULL) {
783 BT_WARN("get_att_mtu is NULL");
784 return OAL_STATUS_NOT_SUPPORT;
787 ret = gatt_api->server->get_att_mtu(conn_id, mtu);
788 if (ret != BT_STATUS_SUCCESS) {
789 BT_ERR("GATT MTU Size failed, status: %s", status2string(ret));
790 return convert_to_oal_status(ret);
793 BT_INFO("Current ATT MTU Size: %d", *mtu);
794 return OAL_STATUS_SUCCESS;
796 oal_status_t gatt_send_response_acquire(int conn_id, int trans_id,
797 int status, int fd, int mtu , void * fdlist)
799 int ret = OAL_STATUS_SUCCESS;
801 API_TRACE("Server Send Response : ConnId = %d, TransId = %d, %d", conn_id, trans_id, fd);
802 CHECK_OAL_GATT_ENABLED();
804 ret = gatt_api->server->send_response_acquire(conn_id, trans_id, status, fd, mtu, fdlist);
805 if (ret != BT_STATUS_SUCCESS) {
806 BT_ERR("GATT Server Send Response failed: %s", status2string(ret));
807 return convert_to_oal_status(ret);
809 return OAL_STATUS_SUCCESS;
812 oal_status_t gatts_add_service(int instance_id, oal_gatt_srvc_id_t *gatt_serv_id,
815 int ret = OAL_STATUS_SUCCESS;
816 btgatt_srvc_id_t btgatt_srvc_id;
817 char str[2*BT_UUID_STRING_MAX];
819 API_TRACE("Server Add Service : instance_id = %d, num_handles = %d", instance_id, num_handles);
820 CHECK_OAL_GATT_ENABLED();
822 CHECK_SERVER_INSTANCE(instance_id);
823 if (__gatts_find_server_instance(instance_id) == NULL)
824 return OAL_STATUS_INTERNAL_ERROR;
826 if (gatt_serv_id != NULL) {
827 uuid_to_stringname(&(gatt_serv_id->id.uuid), str);
828 API_TRACE("Service uuid: [%s]", str);
829 memcpy(btgatt_srvc_id.id.uuid.uu, gatt_serv_id->id.uuid.uuid, sizeof(gatt_serv_id->id.uuid));
830 btgatt_srvc_id.id.inst_id = gatt_serv_id->id.inst_id;
831 btgatt_srvc_id.is_primary = gatt_serv_id->is_prmry;
833 BT_INFO("GATT Server Service Id is NULL");
834 return OAL_STATUS_INVALID_PARAM;
837 ret = gatt_api->server->add_service(instance_id, &btgatt_srvc_id, num_handles);
838 if (ret != BT_STATUS_SUCCESS) {
839 BT_ERR("GATT Server add service failed: %s", status2string(ret));
840 return convert_to_oal_status(ret);
842 return OAL_STATUS_SUCCESS;
845 oal_status_t gatts_add_included_services(int instance_id, int serv_handle,
848 int ret = OAL_STATUS_SUCCESS;
849 API_TRACE("Server Add included Service : instance_id = %d, serv_handle = %d, incl_handle = %d",
850 instance_id, serv_handle, incl_handle);
851 CHECK_OAL_GATT_ENABLED();
853 CHECK_SERVER_INSTANCE(instance_id);
854 if (__gatts_find_server_instance(instance_id) == NULL)
855 return OAL_STATUS_INTERNAL_ERROR;
857 ret = gatt_api->server->add_included_service(instance_id, serv_handle, incl_handle);
858 if (ret != BT_STATUS_SUCCESS) {
859 BT_ERR("GATT Server Add included Service failed: %s", status2string(ret));
860 return convert_to_oal_status(ret);
862 return OAL_STATUS_SUCCESS;
865 oal_status_t gatts_add_characteristics(int instance_id, int serv_handle,
866 oal_uuid_t* charc_uuid, int propts, int permsn)
868 int ret = OAL_STATUS_SUCCESS;
869 char str[2*BT_UUID_STRING_MAX];
870 char prop[MAX_PROP_LEN], perm[MAX_PERM_LEN];
871 char_prop_to_string(propts, prop);
872 char_perm_to_string(permsn, perm);
873 API_TRACE("Server Add Characteristic : instance_id = %d, serv_handle = %d, Properties =%s, Permisison = %s",
874 instance_id, serv_handle, prop, perm);
875 CHECK_OAL_GATT_ENABLED();
877 CHECK_SERVER_INSTANCE(instance_id);
878 if (__gatts_find_server_instance(instance_id) == NULL)
879 return OAL_STATUS_INTERNAL_ERROR;
881 if (charc_uuid != NULL) {
882 uuid_to_stringname(charc_uuid, str);
883 API_TRACE("uuid: [%s]", str);
886 ret = gatt_api->server->add_characteristic(instance_id,
887 serv_handle, (bt_uuid_t*)charc_uuid, propts, permsn);
888 if (ret != BT_STATUS_SUCCESS) {
889 BT_ERR("GATT Server Add Characteristic failed: %s", status2string(ret));
890 return convert_to_oal_status(ret);
892 return OAL_STATUS_SUCCESS;
895 oal_status_t gatts_add_descriptor(int instance_id, int serv_handle,
896 oal_uuid_t* desc_uuid, int permsn)
898 int ret = OAL_STATUS_SUCCESS;
899 char str[2*BT_UUID_STRING_MAX];
900 char perm[MAX_PERM_LEN];
901 char_perm_to_string(permsn, perm);
902 API_TRACE("Server Add Descriptor : instance_id = %d, serv_handle = %d, Permisison = %s",
903 instance_id, serv_handle, perm);
904 CHECK_OAL_GATT_ENABLED();
906 CHECK_SERVER_INSTANCE(instance_id);
907 if (__gatts_find_server_instance(instance_id) == NULL)
908 return OAL_STATUS_INTERNAL_ERROR;
910 if (desc_uuid != NULL) {
911 uuid_to_stringname(desc_uuid, str);
912 API_TRACE("uuid: [%s]", str);
915 ret = gatt_api->server->add_descriptor(instance_id,
916 serv_handle, (bt_uuid_t*)desc_uuid, permsn);
917 if (ret != BT_STATUS_SUCCESS) {
918 BT_ERR("GATT Server Add Descriptor failed: %s", status2string(ret));
919 return convert_to_oal_status(ret);
921 return OAL_STATUS_SUCCESS;
924 oal_status_t gatts_start_service(int instance_id, int svc_handle, int transport)
926 int ret = OAL_STATUS_SUCCESS;
928 API_TRACE("Server Start Service : Ins_id = %d, srvHndl = %d, transport = %d",
929 instance_id, svc_handle, transport);
930 CHECK_OAL_GATT_ENABLED();
932 CHECK_SERVER_INSTANCE(instance_id);
933 if (__gatts_find_server_instance(instance_id) == NULL)
934 return OAL_STATUS_INTERNAL_ERROR;
936 ret = gatt_api->server->start_service(instance_id, svc_handle, transport);
937 if (ret != BT_STATUS_SUCCESS) {
938 BT_ERR("GATT Server Start Service failed: %s", status2string(ret));
939 return convert_to_oal_status(ret);
941 return OAL_STATUS_SUCCESS;
944 oal_status_t gatts_stop_service(int instance_id, int srv_hdl)
946 int ret = OAL_STATUS_SUCCESS;
948 API_TRACE("Server Stop Service : Ins_id = %d, srvHndl = %d, ", instance_id, srv_hdl);
949 CHECK_OAL_GATT_ENABLED();
951 CHECK_SERVER_INSTANCE(instance_id);
952 if (__gatts_find_server_instance(instance_id) == NULL)
953 return OAL_STATUS_INTERNAL_ERROR;
955 ret = gatt_api->server->stop_service(instance_id, srv_hdl);
956 if (ret != BT_STATUS_SUCCESS) {
957 BT_ERR("GATT Server Stop Service failed: %s", status2string(ret));
958 return convert_to_oal_status(ret);
960 return OAL_STATUS_SUCCESS;
963 oal_status_t gatts_delete_service(int instance_id, int srv_hdl)
965 int ret = OAL_STATUS_SUCCESS;
967 API_TRACE("Server Delete Service : Ins_id = %d, srvHndl = %d, ", instance_id, srv_hdl);
968 CHECK_OAL_GATT_ENABLED();
970 CHECK_SERVER_INSTANCE(instance_id);
971 if (__gatts_find_server_instance(instance_id) == NULL)
972 return OAL_STATUS_INTERNAL_ERROR;
974 ret = gatt_api->server->delete_service(instance_id, srv_hdl);
975 if (ret != BT_STATUS_SUCCESS) {
976 BT_ERR("GATT Server Delete Service failed: %s", status2string(ret));
977 return convert_to_oal_status(ret);
979 return OAL_STATUS_SUCCESS;
982 oal_status_t gatts_send_response(int conn_id, int trans_id, int resp_status, oal_gatt_response_t *response)
984 int ret = OAL_STATUS_SUCCESS;
986 API_TRACE("Server Send Response : ConnId = %d, TransId = %d, ", conn_id, trans_id);
987 CHECK_OAL_GATT_ENABLED();
989 if (response == NULL) {
990 BT_ERR("GATT Server attribute value is empty");
991 return OAL_STATUS_INVALID_PARAM;
994 ret = gatt_api->server->send_response(conn_id, trans_id, resp_status, (btgatt_response_t *)response);
995 if (ret != BT_STATUS_SUCCESS) {
996 BT_ERR("GATT Server Send Response failed: %s", status2string(ret));
997 return convert_to_oal_status(ret);
999 return OAL_STATUS_SUCCESS;
1002 oal_status_t gatts_send_indication(int instance_id, int attr_hndl, int conn_id, int len, int confirm, char* value)
1004 int ret = OAL_STATUS_SUCCESS;
1006 API_TRACE("Server Send Indication : Ins_id = %d, srvHndl = %d, [%s]",
1007 instance_id, attr_hndl, confirm == TRUE ? "INDICATION" : "NOTIFICATION");
1008 CHECK_OAL_GATT_ENABLED();
1010 CHECK_SERVER_INSTANCE(instance_id);
1011 if (__gatts_find_server_instance(instance_id) == NULL)
1012 return OAL_STATUS_INTERNAL_ERROR;
1014 if (value == NULL || len == 0) {
1015 BT_ERR("GATT Server attribute value is empty");
1016 return OAL_STATUS_INVALID_PARAM;
1019 ret = gatt_api->server->send_indication(instance_id,
1020 attr_hndl, conn_id, len, confirm, value);
1021 if (ret != BT_STATUS_SUCCESS) {
1022 BT_ERR("GATT Server Send Indication failed: %s", status2string(ret));
1023 return convert_to_oal_status(ret);
1025 return OAL_STATUS_SUCCESS;
1028 oal_status_t gatts_update_att_value(int instance_id, oal_gatt_value_t *value)
1030 int ret = OAL_STATUS_SUCCESS;
1032 CHECK_OAL_GATT_ENABLED();
1034 CHECK_SERVER_INSTANCE(instance_id);
1035 if (__gatts_find_server_instance(instance_id) == NULL)
1036 return OAL_STATUS_INTERNAL_ERROR;
1038 if (value == NULL || value->len == 0) {
1039 BT_ERR("GATT Server attribute value is empty");
1040 return OAL_STATUS_INVALID_PARAM;
1043 API_TRACE("Server Update Value: Ins_id = [%d], att handle = [%d] data len [%d]",
1044 instance_id, value->handle, value->len);
1046 ret = gatt_api->server->update_att_value(instance_id,
1047 (int)value->handle, (int)value->len, (char*)value->value);
1048 if (ret != BT_STATUS_SUCCESS) {
1049 BT_ERR("GATT Server Update Attribute Value failed: [%s]", status2string(ret));
1050 return convert_to_oal_status(ret);
1052 return OAL_STATUS_SUCCESS;
1055 /* GATT Server Callbacks:Start */
1056 static void cb_gatts_service_added(int status, int server_if,
1057 btgatt_srvc_id_t *psrvc_id, int srvc_handle)
1059 BT_INFO("BTGATT SERVER SERVICE ADDED CB, status:%d, handle:%d", status, srvc_handle);
1060 event_gatts_srvc_prm_t* event = g_new0(event_gatts_srvc_prm_t, 1);
1062 CHECK_CORRECT_SERVER_ID(server_if, event->gatt_srvc_stat.server_inst);
1064 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1065 event->gatt_srvc_stat.server_inst = -1;
1067 event->gatt_srvc_stat.servic_hndl = srvc_handle;
1068 if (psrvc_id != NULL) {
1069 event->gatt_srvc_id.id.inst_id = psrvc_id->id.inst_id;
1070 //event->gatt_srvc_id.id.uuid = psrvc_id->id.uuid;
1071 memcpy(event->gatt_srvc_id.id.uuid.uuid, psrvc_id->id.uuid.uu, sizeof(psrvc_id->id.uuid));
1072 event->gatt_srvc_id.is_prmry = psrvc_id->is_primary;
1075 event->gatt_srvc_stat.status = convert_to_oal_status(status);
1077 send_event(OAL_EVENT_GATTS_SERVICE_ADDED, event, sizeof(event_gatts_srvc_prm_t));
1080 static void cb_gatts_included_service_added(int status, int server_if,
1082 int incl_srvc_handle)
1084 BT_INFO("BTGATT SERVER INCLUDED SERVICE ADDED CB, status:%d, srvc handle:%d, incl handle:%d",
1085 status, srvc_handle, incl_srvc_handle);
1087 event_gatts_incld_srvc_t* event = g_new0(event_gatts_incld_srvc_t, 1);
1089 CHECK_CORRECT_SERVER_ID(server_if, event->gatt_srvc_stat.server_inst);
1091 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1092 event->gatt_srvc_stat.server_inst = -1;
1094 event->gatt_srvc_stat.servic_hndl = srvc_handle;
1095 event->incl_srvc_hndl = incl_srvc_handle;
1096 event->gatt_srvc_stat.status = convert_to_oal_status(status);
1097 send_event(OAL_EVENT_GATTS_INCLUDED_SERVICE_ADDED, event, sizeof(event_gatts_incld_srvc_t));
1100 static void cb_gatts_characteristic_added(int status, int server_if, bt_uuid_t *char_id,
1101 int srvc_handle, int char_handle)
1103 BT_INFO("BTGATT SERVER CHARACTERISTIC ADDED CB, status:%d, srvc handle:%d, char handle:%d",
1104 status, srvc_handle, char_handle);
1106 event_gatts_srvc_charctr_t* event = g_new0(event_gatts_srvc_charctr_t, 1);
1108 CHECK_CORRECT_SERVER_ID(server_if, event->gatt_srvc_stat.server_inst);
1110 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1111 event->gatt_srvc_stat.server_inst = -1;
1113 event->gatt_srvc_stat.servic_hndl = srvc_handle;
1114 event->charctr_hndl = char_handle;
1115 event->gatt_srvc_stat.status = convert_to_oal_status(status);
1116 memcpy(event->charctr_uuid.uuid, char_id->uu, sizeof(bt_uuid_t));
1117 send_event(OAL_EVENT_GATTS_CHARACTERISTIC_ADDED, event, sizeof(event_gatts_srvc_charctr_t));
1120 static void cb_gatts_descriptor_added(int status, int server_if,
1121 bt_uuid_t *descr_id, int srvc_handle,
1124 BT_INFO("BTGATT SERVER DESCRIPTOR ADDED CB, status:%d, srvc handle:%d, desc handle:%d",
1125 status, srvc_handle, descr_handle);
1127 event_gatts_srvc_descr_t* event = g_new0(event_gatts_srvc_descr_t, 1);
1129 CHECK_CORRECT_SERVER_ID(server_if, event->gatt_srvc_stat.server_inst);
1131 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1132 event->gatt_srvc_stat.server_inst = -1;
1134 event->gatt_srvc_stat.servic_hndl = srvc_handle;
1135 event->descrptr_hndl = descr_handle;
1136 event->gatt_srvc_stat.status = convert_to_oal_status(status);
1137 memcpy(event->descrptr_uuid.uuid, descr_id->uu, sizeof(bt_uuid_t));
1138 send_event(OAL_EVENT_GATTS_DESCRIPTOR_ADDED, event, sizeof(event_gatts_srvc_descr_t));
1141 static void cb_gatts_service_started(int status, int server_if, int srvc_handle)
1143 BT_INFO("BTGATT SERVER SERVICE STARTED CB, status:%d, srvc_handle:%d", status, srvc_handle);
1145 event_gatts_srvc_t* event = g_new0(event_gatts_srvc_t, 1);
1147 CHECK_CORRECT_SERVER_ID(server_if, event->server_inst);
1149 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1150 event->server_inst = -1;
1152 event->servic_hndl = srvc_handle;
1153 event->status = convert_to_oal_status(status);
1154 send_event(OAL_EVENT_GATTS_SERVICE_STARTED, event, sizeof(event_gatts_srvc_t));
1157 static void cb_gatts_service_stopped(int status, int server_if, int srvc_handle)
1159 BT_INFO("BTGATT SERVER SERVICE STOPPED CB, status:%d, srvc_handle:%d", status, srvc_handle);
1161 event_gatts_srvc_t* event = g_new0(event_gatts_srvc_t, 1);
1163 CHECK_CORRECT_SERVER_ID(server_if, event->server_inst);
1165 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1166 event->server_inst = -1;
1168 event->servic_hndl = srvc_handle;
1169 event->status = convert_to_oal_status(status);
1170 send_event(OAL_EVENT_GATTS_SERVICE_STOPED, event, sizeof(event_gatts_srvc_t));
1173 static void cb_gatts_service_deleted(int status, int server_if, int srvc_handle)
1175 BT_INFO("BTGATT SERVER SERVICE DELETED CB, status:%d, srvc_handle:%d", status, srvc_handle);
1177 event_gatts_srvc_t* event = g_new0(event_gatts_srvc_t, 1);
1179 CHECK_CORRECT_SERVER_ID(server_if, event->server_inst);
1181 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1182 event->server_inst = -1;
1184 event->servic_hndl = srvc_handle;
1185 event->status = convert_to_oal_status(status);
1186 send_event(OAL_EVENT_GATTS_SERVICE_DELETED, event, sizeof(event_gatts_srvc_t));
1189 static void cb_gatts_connection(int conn_id, int server_if, int connected, bt_bdaddr_t *bda)
1192 oal_event_t event_type;
1194 BT_INFO("BTGATT SERVER CONNECTION connnected:%d, conn_id:%d server_if:%d", connected, conn_id, server_if);
1195 event_gatts_conn_t *event = g_new0(event_gatts_conn_t, 1);
1197 if (connected == TRUE) {
1198 for (l = gatt_servers; l; l = g_slist_next(l)) {
1199 gatt_server_t *info = l->data;
1202 if (info->instance_id == server_if) {
1203 event->server_inst = server_if;
1208 /* server_inst is not required in disconnected case */
1210 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1211 event->conn_id = conn_id;
1212 event_type = ((connected == TRUE) ? OAL_EVENT_GATTS_CONNECTION_COMPLETED : OAL_EVENT_GATTS_DISCONNECTION_COMPLETED);
1213 event->status = OAL_STATUS_SUCCESS;
1215 send_event(event_type, event, sizeof(event_gatts_conn_t));
1218 static void cb_gatts_request_read(int conn_id, int trans_id, bt_bdaddr_t *bda,
1219 int attr_handle, int offset, bool is_long)
1221 BT_DBG("BTGATT SERVER REQUEST READ CB: conn_id:%d", conn_id);
1223 event_gatts_srvc_read_attr_t* event = g_new0(event_gatts_srvc_read_attr_t, 1);
1225 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1227 event->attr_trans.attr_handle = attr_handle;
1228 event->attr_trans.conn_id = conn_id;
1229 event->attr_trans.trans_id = trans_id;
1230 event->attr_trans.offset = offset;
1231 event->is_long = is_long;
1233 send_event(OAL_EVENT_GATTS_REQUEST_READ, event, sizeof(event_gatts_srvc_read_attr_t));
1236 static void cb_gatts_request_write(int conn_id, int trans_id,
1237 bt_bdaddr_t *bda, int attr_handle,
1238 int offset, int length,
1239 bool need_rsp, bool is_prep, uint8_t* value)
1241 BT_INFO("BTGATT SERVER REQUEST WRITE conn_id:%d", conn_id);
1243 event_gatts_srvc_write_attr_t* event = g_new0(event_gatts_srvc_write_attr_t, 1);
1245 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1247 event->attr_trans.attr_handle = attr_handle;
1248 event->attr_trans.conn_id = conn_id;
1249 event->attr_trans.trans_id = trans_id;
1250 event->attr_trans.offset = offset;
1251 event->need_rsp = need_rsp;
1252 event->is_prep = is_prep;
1254 if (length > 0 && value != NULL) {
1255 if (length > OAL_GATT_MAX_ATTR_LEN)
1256 length = OAL_GATT_MAX_ATTR_LEN;
1257 memcpy(event->value, value, length);
1258 event->length = length;
1260 send_event(OAL_EVENT_GATTS_REQUEST_WRITE, event, sizeof(event_gatts_srvc_write_attr_t));
1263 static void cb_gatts_acquire_write(int mtu, int conn_id, int trans_id, int attr_handle , bt_bdaddr_t *bda)
1266 BT_INFO("BTGATT SERVER REQUEST ACQUIRE WRITE conn_id:%d", conn_id);
1268 event_gatts_srvc_acquire_attr_t* event = g_new0(event_gatts_srvc_acquire_attr_t, 1);
1270 event->attr_trans.attr_handle = attr_handle;
1271 event->attr_trans.conn_id = conn_id;
1272 event->attr_trans.trans_id = trans_id;
1274 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1276 send_event(OAL_EVENT_GATTS_REQUEST_ACQUIRE_WRITE, event, sizeof(event_gatts_srvc_acquire_attr_t));
1279 static void cb_gatts_acquire_notify(int mtu, int conn_id, int trans_id, int attr_handle, bt_bdaddr_t *bda)
1281 BT_INFO("BTGATT SERVER REQUEST ACQUIRE NOTIFY conn_id:%d", conn_id);
1283 event_gatts_srvc_acquire_attr_t* event = g_new0(event_gatts_srvc_acquire_attr_t, 1);
1285 event->attr_trans.attr_handle = attr_handle;
1286 event->attr_trans.conn_id = conn_id;
1287 event->attr_trans.trans_id = trans_id;
1289 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1291 send_event(OAL_EVENT_GATTS_REQUEST_ACQUIRE_NOTIFY, event, sizeof(event_gatts_srvc_acquire_attr_t));
1295 static void cb_gatts_response_confirmation(int status, int handle)
1297 BT_INFO("BTGATT SERVER RESPONSE CONFIRMATION CB, status:%d, handle:%d", status, handle);
1299 event_gatts_respons_t* event = g_new0(event_gatts_respons_t, 1);
1301 event->hndl = handle;
1302 event->status = convert_to_oal_status(status);
1304 send_event(OAL_EVENT_GATTS_RESPONSE_CONFIRMED, event, sizeof(event_gatts_respons_t));
1307 static void cb_indication_confirmation(int conn_id, int trans_id, int attr_handle, bt_bdaddr_t *bda)
1309 BT_INFO("BTGATT SERVER Indication Confirmation CB, conn_id:%d, trans_id:%d attr_handle:%d",
1310 conn_id, trans_id, attr_handle);
1312 event_gatts_ind_cnfrm_t* event = g_new0(event_gatts_ind_cnfrm_t, 1);
1314 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1316 event->attr_handle = attr_handle;
1317 event->conn_id = conn_id;
1318 event->trans_id = trans_id;
1319 send_event(OAL_EVENT_GATTS_IND_CONFIRM, event, sizeof(event_gatts_ind_cnfrm_t));
1322 static void cb_gatts_mtu_changed(int conn_id, int mtu)
1324 BT_INFO("BTGATT SERVER MTU CHANGED CB, conn_id: %d, mtu_size: %d", conn_id, mtu);
1325 event_gatts_mtu_changed_t *event = g_new0(event_gatts_mtu_changed_t, 1);
1326 event->conn_id = conn_id;
1327 event->mtu_size = mtu;
1328 send_event(OAL_EVENT_GATTS_MTU_CHANGED, event, sizeof(event_gatts_mtu_changed_t));
1332 static void cb_notifcation_changed(int conn_id, int trans_id, int attr_handle, bool notify, bt_bdaddr_t *bda)
1334 BT_INFO("BTGATT SERVER Notication CB, conn_id:%d, trans_id:%d attr_handle:%d Notify:%d",
1335 conn_id, trans_id, attr_handle, notify);
1337 event_gatts_notif_t* event = g_new0(event_gatts_notif_t, 1);
1339 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1341 event->attr_handle = attr_handle;
1342 event->conn_id = conn_id;
1343 event->trans_id = trans_id;
1344 event->notify = notify;
1345 send_event(OAL_EVENT_GATTS_NOTIFICATION, event, sizeof(event_gatts_notif_t));
1350 /* GATT Server Callbacks: End */
1351 /************************************ GATT Client ***********************************/
1352 /* Client Callbacks */
1353 static void cb_gattc_register_app(int status, int clientIf, bt_uuid_t *app_uuid)
1355 char uuid_str[BT_UUID_STRING_MAX];
1356 event_gattc_register_t *event;
1358 /* Check if GATT client registered for Default GATT client UUID */
1359 uuid_to_string((service_uuid_t *)app_uuid, uuid_str);
1361 BT_INFO("GATT CLIENT REGISTER APP CB, status:%d, clientIf:%d, %s", status, clientIf, uuid_str);
1362 event = g_new0(event_gattc_register_t, 1);
1363 event->client_if = clientIf;
1364 event->status = (status == 0 ? OAL_STATUS_SUCCESS : OAL_STATUS_INTERNAL_ERROR);
1365 memcpy(event->client_uuid.uuid, app_uuid->uu, sizeof(oal_uuid_t));
1366 send_event(OAL_EVENT_GATTC_REGISTRATION, event, sizeof(event_gattc_register_t));
1369 static void cb_gattc_scan_result(bt_bdaddr_t* bdaddress, uint8_t addr_type, int rssi,
1370 uint8_t *adv_data, int adv_data_len, uint8_t *scan_rsp_data, int scan_rsp_data_len)
1372 event_ble_scan_result_info *event;
1374 event = g_new0(event_ble_scan_result_info, 1);
1375 event->addr_type = addr_type;
1377 memcpy(event->address.addr, bdaddress->address, BT_ADDRESS_BYTES_NUM);
1378 memcpy(event->adv_data, adv_data, BLE_ADV_DATA_LENGTH);
1379 event->adv_data_len = adv_data_len;
1380 memcpy(event->scan_rsp_data, scan_rsp_data, BLE_ADV_DATA_LENGTH);
1381 event->scan_rsp_data_len = scan_rsp_data_len;
1382 send_event_bda_trace(OAL_EVENT_BLE_REMOTE_DEVICE_FOUND, event,
1383 sizeof(event_ble_scan_result_info), (bt_address_t *)bdaddress);
1386 static void cb_gattc_connection(int conn_id, int status, int client_if, bt_bdaddr_t* bda)
1388 event_gattc_conn_t *event;
1389 BT_INFO("BTGATT CLIENT CONNECTED CB, status:[%d] client_if[%d] id[%d]",
1390 status, client_if, conn_id);
1392 event = g_new0(event_gattc_conn_t, 1);
1393 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1394 event->client_if = client_if;
1396 if (BT_STATUS_SUCCESS != status) {
1397 event->status = OAL_STATUS_INTERNAL_ERROR;
1398 BT_ERR("gattc connection Error: %d", status);
1400 event->conn_id = conn_id;
1401 event->status = OAL_STATUS_SUCCESS;
1404 send_event_bda_trace(OAL_EVENT_GATTC_CONNECTION_COMPLETED, event,
1405 sizeof(*event), (bt_address_t *)bda);
1409 static void cb_gattc_disconnect(int conn_id, int status, int client_if, bt_bdaddr_t* bda)
1411 event_gattc_conn_t *event;
1412 BT_INFO("BTGATT Client Disconnect Complete cb, status:[%d], client_if[%d] id[%d]",
1413 status, client_if, conn_id);
1414 event = g_new0(event_gattc_conn_t, 1);
1415 event->status = convert_to_oal_status(status);
1416 event->client_if = client_if;
1417 event->conn_id = conn_id;
1418 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1419 send_event_bda_trace(OAL_EVENT_GATTC_DISCONNECTION_COMPLETED, event,
1420 sizeof(*event), (bt_address_t *)bda);
1423 static void cb_gattc_search_result(int conn_id, btgatt_srvc_id_t *srvc_id)
1425 char uuid_str[2*BT_UUID_STRING_MAX];
1427 uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str);
1428 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]",
1429 uuid_str, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1431 event_gattc_service_result_t *event = g_new0(event_gattc_service_result_t, 1);
1432 event->conn_status.status = OAL_STATUS_SUCCESS;
1433 event->conn_status.conn_id = conn_id;
1434 memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1436 send_event(OAL_EVENT_GATTC_SERVICE_SEARCH_RESULT, event, sizeof(*event));
1439 static void cb_gattc_search_complete(int conn_id, int status)
1441 BT_INFO("BTGATT Client Service Search Complete cb, status:%d, conn_id:%d",
1444 event_gattc_conn_status_t *event = g_new0(event_gattc_conn_status_t, 1);
1445 event->conn_id = conn_id;
1446 event->status = convert_to_oal_status(status);
1448 send_event(OAL_EVENT_GATTC_SERVICE_SEARCH_DONE, event, sizeof(*event));
1452 static void cb_gattc_get_characteristics(int conn_id, int status, btgatt_srvc_id_t *srvc_id,
1453 btgatt_gatt_id_t *char_id, int char_prop)
1455 char uuid_str1[2*BT_UUID_STRING_MAX];
1456 char uuid_str2[2*BT_UUID_STRING_MAX];
1459 BT_DBG("BTGATT Client Get Characteristic Callback, conn_id:%d, status:%d", conn_id, status);
1460 uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str1);
1461 BT_DBG("Service=> [%s], Inst_id [%u], Type [%s]",
1462 uuid_str1, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1464 event_gattc_characteristic_result_t *event = g_new0(event_gattc_characteristic_result_t, 1);
1465 event->conn_status.conn_id = conn_id;
1466 event->conn_status.status = convert_to_oal_status(status);
1467 memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1470 uuid_to_stringname((oal_uuid_t *)&(char_id->uuid), uuid_str2);
1471 BT_INFO("Charac => [%s], Inst_id [%u], Prop [%s]",
1472 uuid_str2, char_id->inst_id, char_prop_to_string(char_prop, str));
1473 event->char_prop = char_prop;
1474 memcpy(&(event->char_id), char_id, sizeof(oal_gatt_id_t));
1476 send_event(OAL_EVENT_GATTC_CHARAC_SERACH_RESULT, event, sizeof(*event));
1481 static void cb_gattc_get_descriptor(int conn_id, int status, btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id, btgatt_gatt_id_t *descr_id)
1483 char uuid_str1[2*BT_UUID_STRING_MAX];
1484 char uuid_str2[2*BT_UUID_STRING_MAX];
1485 char uuid_str3[2*BT_UUID_STRING_MAX];
1486 BT_DBG("BTGATT Client Get Descriptor Callback, conn_id:%d, status:%d", conn_id, status);
1487 uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str1);
1488 uuid_to_stringname((oal_uuid_t *)&(char_id->uuid), uuid_str2);
1489 BT_DBG("Service=> [%s], Inst_id [%u], Type [%s]",
1490 uuid_str1, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1491 BT_DBG("Charac => [%s], Inst_id [%u]", uuid_str2, char_id->inst_id);
1493 event_gattc_descriptor_result_t *event = g_new0(event_gattc_descriptor_result_t, 1);
1494 event->conn_status.conn_id = conn_id;
1495 event->conn_status.status = convert_to_oal_status(status);
1496 memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1497 memcpy(&(event->char_id), char_id, sizeof(oal_gatt_id_t));
1500 uuid_to_stringname((oal_uuid_t *)&(descr_id->uuid), uuid_str3);
1501 BT_INFO("Desc => [%s], Inst_id [%u]", uuid_str3, descr_id->inst_id);
1502 memcpy(&(event->descr_id), descr_id, sizeof(oal_gatt_id_t));
1504 send_event(OAL_EVENT_GATTC_DESC_SERACH_RESULT, event, sizeof(*event));
1507 static void cb_gattc_register_for_notification(int conn_id, int registered, int status, btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id)
1509 char uuid_str1[2*BT_UUID_STRING_MAX];
1510 char uuid_str2[2*BT_UUID_STRING_MAX];
1511 BT_INFO("BTGATT Client Register For Notification Callback, conn_id:%d, status:%d, registered: %s",
1512 conn_id, status, registered == 1 ? "REGISTERED" : "DEREGISTERED");
1513 uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str1);
1514 uuid_to_stringname((oal_uuid_t *)&(char_id->uuid), uuid_str2);
1515 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1516 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, char_id->inst_id);
1518 event_gattc_regdereg_notify_t *event = g_new0(event_gattc_regdereg_notify_t, 1);
1519 oal_event_t event_type = (registered == 1 ? OAL_EVENT_GATTC_NOTIFICATION_REGISTERED : OAL_EVENT_GATTC_NOTIFICATION_DEREGISTERED);
1520 event->conn_id = conn_id;
1521 event->status = convert_to_oal_status(status);
1522 memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1523 memcpy(&(event->char_id), char_id, sizeof(oal_gatt_id_t));
1525 send_event(event_type, event, sizeof(*event));
1528 static void cb_gattc_notify(int conn_id, btgatt_notify_params_t *p_data)
1531 char uuid_str1[2*BT_UUID_STRING_MAX];
1532 char uuid_str2[2*BT_UUID_STRING_MAX];
1534 BT_INFO("BTGATT Client Notify Callback, conn_id:%d", conn_id);
1535 BT_INFO("Server Address:[%s], is_notify:[%d], len:[%d]",
1536 bdt_bd2str((bt_address_t *)&(p_data->bda), &bdstr), p_data->is_notify, p_data->len);
1538 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1539 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1541 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]",
1542 uuid_str1, p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1543 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1545 if (p_data->len > 0) {
1547 data = g_malloc(3*p_data->len+1);
1549 BT_ERR("memory allocation failed");
1553 convert_hex_2_str((unsigned char *)p_data->value, p_data->len, data);
1554 BT_INFO("Notified Data: [%s]", data);
1556 event_gattc_notify_data *event = g_new0(event_gattc_notify_data, 1);
1557 memcpy(event->address.addr, p_data->bda.address, BT_ADDRESS_BYTES_NUM);
1558 event->is_notify = p_data->is_notify;
1559 event->data_len = p_data->len;
1560 memcpy(event->data, p_data->value, event->data_len);
1561 memcpy(&(event->char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1562 memcpy(&(event->srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1564 send_event_bda_trace(OAL_EVENT_GATTC_NOTIFY_DATA, event, sizeof(*event), (bt_address_t *)&p_data->bda);
1569 static void cb_gattc_service_changed(bt_bdaddr_t *bd_addr, int change_type, uint8_t *uuid, int conn_id, int inst_id)
1571 event_gattc_service_changed_data *event = g_new0(event_gattc_service_changed_data, 1);
1573 memcpy(event->address.addr, bd_addr->address, 6);
1574 event->change_type = change_type;
1575 memcpy(event->uuid.uuid, uuid, sizeof(event->uuid.uuid));
1576 event->conn_id = conn_id;
1577 event->inst_id = inst_id;
1579 send_event_bda_trace(OAL_EVENT_GATTC_SERVICE_CHANGED_IND, event, sizeof(*event), (bt_address_t *)bd_addr);
1582 static void cb_gattc_read_characteristic(int conn_id, int status, btgatt_read_params_t *p_data)
1584 char uuid_str1[2*BT_UUID_STRING_MAX];
1585 char uuid_str2[2*BT_UUID_STRING_MAX];
1586 BT_DBG("BTGATT Client Read Charcateristic Callback, conn_id:%d, read_status:%d", conn_id, status);
1588 event_gattc_read_data *event = g_new0(event_gattc_read_data, 1);
1589 event->uuid_status.conn_status.conn_id = conn_id;
1590 event->uuid_status.conn_status.status = convert_to_oal_status(status);
1591 event->value_type = p_data->value_type;
1592 memcpy(&(event->uuid_status.srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1593 memcpy(&(event->uuid_status.char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1595 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1596 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1597 BT_DBG("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1,
1598 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1599 BT_DBG("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1600 BT_DBG("Len: %u, value_type: %u", p_data->value.len, p_data->value_type);
1601 if (p_data->value.len > 0 && event->value_type == GATTC_READ_VALUE_TYPE_VALUE) {
1602 event->data_len = p_data->value.len;
1603 memcpy(&(event->data), &(p_data->value.value), event->data_len);
1606 send_event(OAL_EVENT_GATTC_READ_CHARAC, event, sizeof(*event));
1609 static void cb_gattc_write_characteristic(int conn_id, int status, btgatt_write_params_t *p_data)
1611 char uuid_str1[2*BT_UUID_STRING_MAX];
1612 char uuid_str2[2*BT_UUID_STRING_MAX];
1613 BT_INFO("BTGATT Client Write Charcateristic Callback, conn_id:%d, write_status:%d", conn_id, status);
1615 event_gattc_write_data *event = g_new0(event_gattc_write_data, 1);
1616 event->conn_status.conn_id = conn_id;
1617 event->conn_status.status = convert_to_oal_status(status);
1618 memcpy(&(event->srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1619 memcpy(&(event->char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1621 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1622 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1623 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1,
1624 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1625 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1627 send_event(OAL_EVENT_GATTC_WRITE_CHARAC, event, sizeof(*event));
1630 static void cb_gattc_write_descriptor(int conn_id, int status, btgatt_write_params_t *p_data)
1632 char uuid_str1[2*BT_UUID_STRING_MAX];
1633 char uuid_str2[2*BT_UUID_STRING_MAX];
1634 char uuid_str3[2*BT_UUID_STRING_MAX];
1635 BT_DBG("BTGATT Client Write Descriptor Callback, conn_id:%d, write_status:%d", conn_id, status);
1637 event_gattc_write_data *event = g_new0(event_gattc_write_data, 1);
1638 event->conn_status.conn_id = conn_id;
1639 event->conn_status.status = convert_to_oal_status(status);
1640 memcpy(&(event->srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1641 memcpy(&(event->char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1642 memcpy(&(event->descr_id), &(p_data->descr_id), sizeof(oal_gatt_id_t));
1644 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1645 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1646 uuid_to_stringname((oal_uuid_t *)&(p_data->descr_id.uuid), uuid_str3);
1647 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1,
1648 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1649 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1650 BT_INFO("Desc => [%s], Inst_id [%u]", uuid_str3, p_data->descr_id.inst_id);
1652 send_event(OAL_EVENT_GATTC_WRITE_DESCR, event, sizeof(*event));
1657 static void cb_gattc_read_descriptor(int conn_id, int status, btgatt_read_params_t *p_data)
1659 char uuid_str1[2*BT_UUID_STRING_MAX];
1660 char uuid_str2[2*BT_UUID_STRING_MAX];
1661 char uuid_str3[2*BT_UUID_STRING_MAX];
1662 BT_INFO("BTGATT Client Read Descriptor Callback, conn_id:%d, read_status:%d", conn_id, status);
1664 event_gattc_read_data *event = g_new0(event_gattc_read_data, 1);
1665 event->uuid_status.conn_status.conn_id = conn_id;
1666 event->uuid_status.conn_status.status = convert_to_oal_status(status);
1667 event->value_type = p_data->value_type;
1668 memcpy(&(event->uuid_status.srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1669 memcpy(&(event->uuid_status.char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1670 memcpy(&(event->uuid_status.descr_id), &(p_data->descr_id), sizeof(oal_gatt_id_t));
1673 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1674 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1675 uuid_to_stringname((oal_uuid_t *)&(p_data->descr_id.uuid), uuid_str3);
1676 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1,
1677 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1678 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1679 BT_INFO("Desc => [%s], Inst_id [%u]", uuid_str3, p_data->descr_id.inst_id);
1680 BT_DBG("Len: %u, value_type: %u", p_data->value.len, p_data->value_type);
1681 if (p_data->value.len > 0 && event->value_type == GATTC_READ_VALUE_TYPE_VALUE) {
1683 data = g_malloc(3*p_data->value.len+1);
1685 BT_ERR("memory allocation failed");
1689 convert_hex_2_str((unsigned char *)p_data->value.value, p_data->value.len, data);
1690 BT_DBG("Read Data: [%s]", data);
1691 event->data_len = p_data->value.len;
1692 memcpy(&(event->data), &(p_data->value.value), event->data_len);
1696 send_event(OAL_EVENT_GATTC_READ_DESCR, event, sizeof(*event));
1699 oal_status_t gattc_start_le_discovery(int client_id)
1702 int ret = OAL_STATUS_SUCCESS;
1704 API_TRACE("BTGATT CLIENT SCAN START");
1705 CHECK_OAL_GATT_ENABLED();
1706 CHECK_CLIENT_REGISTRATION(client_id);
1708 ret = gatt_api->client->scan(client_id, 1);
1709 if (ret != BT_STATUS_SUCCESS) {
1710 BT_ERR("Error:Start LE Discovery failed: %s", status2string(ret));
1711 return convert_to_oal_status(ret);
1713 return OAL_STATUS_SUCCESS;
1716 oal_status_t gattc_stop_le_discovery(int client_id)
1719 int ret = OAL_STATUS_SUCCESS;
1721 API_TRACE("BTGATT CLIENT SCAN STOP");
1722 CHECK_OAL_GATT_ENABLED();
1723 CHECK_CLIENT_REGISTRATION(client_id);
1724 ret = gatt_api->client->scan(client_id, 0);
1725 if (ret != BT_STATUS_SUCCESS) {
1726 BT_ERR("Error:Stop LE Discovery failed: %s", status2string(ret));
1727 return convert_to_oal_status(ret);
1729 return OAL_STATUS_SUCCESS;
1732 oal_status_t gattc_set_le_scan_param(int scan_type, int itv, int win)
1736 API_TRACE("GATT client set le scan param");
1737 CHECK_OAL_GATT_ENABLED();
1740 ret = gatt_api->client->set_scan_parameters(scan_type, itv, win);
1741 if (ret != BT_STATUS_SUCCESS) {
1742 BT_ERR("Error:Set scan parameters failed: %s", status2string(ret));
1743 return convert_to_oal_status(ret);
1746 ret = gatt_api->client->set_scan_parameters(itv, win);
1747 if (ret != BT_STATUS_SUCCESS) {
1748 BT_ERR("Error:Set scan parameters failed: %s", status2string(ret));
1749 return convert_to_oal_status(ret);
1753 return OAL_STATUS_SUCCESS;
1756 static void cb_gattc_configure_mtu_cmpl(int conn_id, int status, int mtu)
1758 event_gattc_mtu_configured_t *event;
1759 BT_INFO("BTGATT Client configure mtu complete Callback, conn_id: %d, status: %d, mtu: %d",
1760 conn_id, status, mtu);
1761 event = g_new0(event_gattc_mtu_configured_t, 1);
1762 event->conn_id = conn_id;
1764 event->status = convert_to_oal_status(status);
1765 send_event(OAL_EVENT_GATTC_MTU_EXCHANGE_COMPLETED, event, sizeof(*event));
1768 /************************************ GATT Client ***********************************/
1769 oal_status_t gattc_register(oal_uuid_t* client_uuid)
1772 char str[2*BT_UUID_STRING_MAX];
1773 int ret = OAL_STATUS_SUCCESS;
1775 CHECK_OAL_GATT_ENABLED();
1776 uuid_to_stringname(client_uuid, str);
1777 API_TRACE("uuid: [%s]", str);
1778 /* We will perform actual registration in cb_gattc_register_app callback */
1779 ret = gatt_api->client->register_client((bt_uuid_t *)client_uuid);
1780 if (ret != BT_STATUS_SUCCESS) {
1781 BT_ERR("GATT client register failed: %s", status2string(ret));
1782 return convert_to_oal_status(ret);
1784 return OAL_STATUS_SUCCESS;
1787 oal_status_t gattc_deregister(int client_id)
1789 int ret = OAL_STATUS_SUCCESS;
1791 API_TRACE("GATT client deregister");
1792 CHECK_OAL_GATT_ENABLED();
1793 CHECK_CLIENT_REGISTRATION(client_id);
1795 ret = gatt_api->client->unregister_client(client_id);
1796 if (ret != BT_STATUS_SUCCESS) {
1797 BT_ERR("GATT client deregister failed: %s", status2string(ret));
1798 return convert_to_oal_status(ret);
1801 return OAL_STATUS_SUCCESS;
1804 oal_status_t gattc_connect(int client_id, bt_address_t *device_address, int isDirect)
1806 int ret = OAL_STATUS_SUCCESS;
1809 OAL_CHECK_PARAMETER(device_address, return);
1810 API_TRACE("Client Connect: [%s]", bdt_bd2str(device_address, &bdstr) + 12);
1811 CHECK_OAL_GATT_ENABLED();
1812 CHECK_CLIENT_REGISTRATION(client_id);
1814 /* Handle the actual connection in cb_gattc_connection callback */
1815 ret = gatt_api->client->connect(client_id, (bt_bdaddr_t *)device_address, isDirect);
1816 if (ret != BT_STATUS_SUCCESS) {
1817 BT_ERR("GATT client connect failed: %s", status2string(ret));
1818 return convert_to_oal_status(ret);
1820 return OAL_STATUS_SUCCESS;
1823 oal_status_t gattc_disconnect(int client_id, bt_address_t *device_address, int conn_id)
1826 int ret = OAL_STATUS_SUCCESS;
1829 OAL_CHECK_PARAMETER(device_address, return);
1830 API_TRACE("Client Disconnect: [%s]", bdt_bd2str(device_address, &bdstr) + 12);
1831 CHECK_OAL_GATT_ENABLED();
1832 CHECK_CLIENT_REGISTRATION(client_id);
1833 CHECK_CLIENT_CONNECTION(conn_id);
1835 /* Handle actual disconnection in callback */
1836 ret = gatt_api->client->disconnect(client_id, (bt_bdaddr_t *) device_address, conn_id);
1837 if (ret != BT_STATUS_SUCCESS) {
1838 BT_ERR("GATT client disconnect failed: %s", status2string(ret));
1839 return convert_to_oal_status(ret);
1841 return OAL_STATUS_SUCCESS;
1845 oal_status_t gattc_search_service(int conn_id, oal_uuid_t *service_uuid)
1847 int ret = OAL_STATUS_SUCCESS;
1848 char uuid_str[2*BT_UUID_STRING_MAX];
1851 uuid_to_stringname(service_uuid, uuid_str);
1852 API_TRACE("Client Service Search UUID: [%s]", uuid_str);
1854 API_TRACE("Client Service Search All");
1856 CHECK_OAL_GATT_ENABLED();
1857 CHECK_CLIENT_CONNECTION(conn_id);
1858 ret = gatt_api->client->search_service(conn_id, (bt_uuid_t *)service_uuid);
1860 if (ret != BT_STATUS_SUCCESS) {
1861 BT_ERR("GATT client service search failed: %s", status2string(ret));
1862 return convert_to_oal_status(ret);
1864 return OAL_STATUS_SUCCESS;
1867 oal_status_t gattc_get_characteristic(int conn_id, oal_gatt_srvc_id_t *srvc_id, oal_gatt_id_t *char_id)
1869 int ret = OAL_STATUS_SUCCESS;
1870 char uuid_str[2*BT_UUID_STRING_MAX];
1872 OAL_CHECK_PARAMETER(srvc_id, return);
1873 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str);
1874 API_TRACE("Client Get Characteristic [%s]", uuid_str);
1875 CHECK_OAL_GATT_ENABLED();
1876 CHECK_CLIENT_CONNECTION(conn_id);
1877 ret = gatt_api->client->get_characteristic(conn_id, (btgatt_srvc_id_t *)srvc_id,
1878 (btgatt_gatt_id_t *)char_id);
1879 if (ret != BT_STATUS_SUCCESS) {
1880 BT_ERR("GATT client get characteristic failed: %s", status2string(ret));
1881 return convert_to_oal_status(ret);
1883 return OAL_STATUS_SUCCESS;
1886 oal_status_t gattc_get_descriptor(int conn_id, oal_gatt_srvc_id_t *srvc_id,
1887 oal_gatt_id_t *char_id, oal_gatt_id_t *desc_id)
1889 int ret = OAL_STATUS_SUCCESS;
1890 char uuid_str1[2*BT_UUID_STRING_MAX];
1891 char uuid_str2[2*BT_UUID_STRING_MAX];
1893 OAL_CHECK_PARAMETER(srvc_id, return);
1894 OAL_CHECK_PARAMETER(char_id, return);
1895 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1896 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1897 API_TRACE("Client Get Descriptor, Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
1898 CHECK_OAL_GATT_ENABLED();
1899 CHECK_CLIENT_CONNECTION(conn_id);
1900 ret = gatt_api->client->get_descriptor(conn_id, (btgatt_srvc_id_t *)srvc_id,
1901 (btgatt_gatt_id_t *)char_id, (btgatt_gatt_id_t *)desc_id);
1902 if (ret != BT_STATUS_SUCCESS) {
1903 BT_ERR("GATT client get descriptor failed: %s", status2string(ret));
1904 return convert_to_oal_status(ret);
1906 return OAL_STATUS_SUCCESS;
1909 oal_status_t gattc_register_for_notification(int conn_id, bt_address_t * address,
1910 oal_gatt_srvc_id_t *srvc_id, oal_gatt_id_t *char_id) {
1911 int ret = OAL_STATUS_SUCCESS;
1912 char uuid_str1[2*BT_UUID_STRING_MAX];
1913 char uuid_str2[2*BT_UUID_STRING_MAX];
1916 OAL_CHECK_PARAMETER(address, return);
1917 OAL_CHECK_PARAMETER(srvc_id, return);
1918 OAL_CHECK_PARAMETER(char_id, return);
1919 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1920 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1921 API_TRACE("Client Register Notification: [%s], Service_uuid: [%s], Char_uuid: [%s]", bdt_bd2str(address, &bdstr), uuid_str1, uuid_str2);
1922 CHECK_OAL_GATT_ENABLED();
1923 CHECK_CLIENT_CONNECTION(conn_id);
1925 ret = gatt_api->client->register_for_notification(conn_id, (bt_bdaddr_t *)address, (btgatt_srvc_id_t *)srvc_id, (btgatt_gatt_id_t *)char_id);
1926 if (ret != BT_STATUS_SUCCESS) {
1927 BT_ERR("GATT client register notification failed: %s", status2string(ret));
1928 return convert_to_oal_status(ret);
1931 return OAL_STATUS_SUCCESS;
1934 oal_status_t gattc_deregister_for_notification(int conn_id, bt_address_t * address,
1935 oal_gatt_srvc_id_t *srvc_id, oal_gatt_id_t *char_id) {
1936 int ret = OAL_STATUS_SUCCESS;
1937 char uuid_str1[2*BT_UUID_STRING_MAX];
1938 char uuid_str2[2*BT_UUID_STRING_MAX];
1941 OAL_CHECK_PARAMETER(address, return);
1942 OAL_CHECK_PARAMETER(srvc_id, return);
1943 OAL_CHECK_PARAMETER(char_id, return);
1944 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1945 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1946 API_TRACE("Client Deregister Notification: [%s], Service_uuid: [%s], Char_uuid: [%s]", bdt_bd2str(address, &bdstr), uuid_str1, uuid_str2);
1947 CHECK_OAL_GATT_ENABLED();
1948 CHECK_CLIENT_CONNECTION(conn_id);
1950 ret = gatt_api->client->deregister_for_notification(conn_id, (bt_bdaddr_t *)address, (btgatt_srvc_id_t *)srvc_id, (btgatt_gatt_id_t *)char_id);
1951 if (ret != BT_STATUS_SUCCESS) {
1952 BT_ERR("GATT client deregister notification failed: %s", status2string(ret));
1953 return convert_to_oal_status(ret);
1956 return OAL_STATUS_SUCCESS;
1960 oal_status_t gattc_read_characteristic(int conn_id, oal_gatt_srvc_id_t *srvc_id,
1961 oal_gatt_id_t *char_id, oal_gatt_auth_req_t auth_req)
1963 int ret = OAL_STATUS_SUCCESS;
1964 char uuid_str1[2*BT_UUID_STRING_MAX];
1965 char uuid_str2[2*BT_UUID_STRING_MAX];
1967 OAL_CHECK_PARAMETER(srvc_id, return);
1968 OAL_CHECK_PARAMETER(char_id, return);
1969 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1970 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1971 API_TRACE("Client Read Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
1972 CHECK_OAL_GATT_ENABLED();
1973 CHECK_CLIENT_CONNECTION(conn_id);
1975 ret = gatt_api->client->read_characteristic(conn_id, (btgatt_srvc_id_t *)srvc_id,
1976 (btgatt_gatt_id_t *)char_id, auth_req);
1977 if (ret != BT_STATUS_SUCCESS) {
1978 BT_ERR("GATT client read characteristic failed: %s", status2string(ret));
1979 return convert_to_oal_status(ret);
1982 return OAL_STATUS_SUCCESS;
1985 oal_status_t gattc_read_descriptor(int conn_id, oal_gatt_srvc_id_t *srvc_id,
1986 oal_gatt_id_t *char_id, oal_gatt_id_t *desc_id, oal_gatt_auth_req_t auth_req)
1988 int ret = OAL_STATUS_SUCCESS;
1989 char uuid_str1[2*BT_UUID_STRING_MAX];
1990 char uuid_str2[2*BT_UUID_STRING_MAX];
1991 char uuid_str3[2*BT_UUID_STRING_MAX];
1993 OAL_CHECK_PARAMETER(srvc_id, return);
1994 OAL_CHECK_PARAMETER(char_id, return);
1995 OAL_CHECK_PARAMETER(desc_id, return);
1996 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1997 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1998 uuid_to_stringname(&(desc_id->uuid), uuid_str3);
1999 API_TRACE("Client Read Descriptor: Service_uuid: [%s], Char_uuid: [%s], Desc_uuid: [%s]",
2000 uuid_str1, uuid_str2, uuid_str3);
2001 CHECK_OAL_GATT_ENABLED();
2002 CHECK_CLIENT_CONNECTION(conn_id);
2004 ret = gatt_api->client->read_descriptor(conn_id, (btgatt_srvc_id_t *)srvc_id,
2005 (btgatt_gatt_id_t *)char_id, (btgatt_gatt_id_t *)desc_id, auth_req);
2006 if (ret != BT_STATUS_SUCCESS) {
2007 BT_ERR("GATT client read descriptor failed: %s", status2string(ret));
2008 return convert_to_oal_status(ret);
2011 return OAL_STATUS_SUCCESS;
2014 oal_status_t gattc_acquire_notify(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2015 oal_gatt_id_t *char_id, int *fd, int *mtu)
2017 int ret = OAL_STATUS_SUCCESS;
2018 char uuid_str1[2*BT_UUID_STRING_MAX];
2019 char uuid_str2[2*BT_UUID_STRING_MAX];
2020 OAL_CHECK_PARAMETER(srvc_id, return);
2021 OAL_CHECK_PARAMETER(char_id, return);
2022 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2023 uuid_to_stringname(&(char_id->uuid), uuid_str2);
2024 API_TRACE("Client Write Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
2025 CHECK_OAL_GATT_ENABLED();
2026 CHECK_CLIENT_CONNECTION(conn_id);
2028 ret = gatt_api->client->acquire_notify(conn_id, (btgatt_srvc_id_t *)srvc_id,
2029 (btgatt_gatt_id_t *)char_id, fd, mtu);
2030 if (ret != BT_STATUS_SUCCESS) {
2031 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
2032 return convert_to_oal_status(ret);
2035 return OAL_STATUS_SUCCESS;
2038 oal_status_t gattc_acquire_write(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2039 oal_gatt_id_t *char_id, oal_gatt_auth_req_t auth_req , int *fd, int*mtu)
2041 int ret = OAL_STATUS_SUCCESS;
2042 char uuid_str1[2*BT_UUID_STRING_MAX];
2043 char uuid_str2[2*BT_UUID_STRING_MAX];
2044 OAL_CHECK_PARAMETER(srvc_id, return);
2045 OAL_CHECK_PARAMETER(char_id, return);
2046 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2047 uuid_to_stringname(&(char_id->uuid), uuid_str2);
2048 API_TRACE("Client Write Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
2049 CHECK_OAL_GATT_ENABLED();
2050 CHECK_CLIENT_CONNECTION(conn_id);
2052 ret = gatt_api->client->acquire_write(conn_id, (btgatt_srvc_id_t *)srvc_id,
2053 (btgatt_gatt_id_t *)char_id, auth_req, fd, mtu);
2054 if (ret != BT_STATUS_SUCCESS) {
2055 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
2056 return convert_to_oal_status(ret);
2059 return OAL_STATUS_SUCCESS;
2062 oal_status_t gattc_write_characteristic(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2063 oal_gatt_id_t *char_id, oal_gatt_write_type_t write_type,
2064 int len, oal_gatt_auth_req_t auth_req, char* data)
2066 int ret = OAL_STATUS_SUCCESS;
2067 char uuid_str1[2*BT_UUID_STRING_MAX];
2068 char uuid_str2[2*BT_UUID_STRING_MAX];
2069 OAL_CHECK_PARAMETER(srvc_id, return);
2070 OAL_CHECK_PARAMETER(char_id, return);
2071 OAL_CHECK_PARAMETER(data, return);
2072 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2073 uuid_to_stringname(&(char_id->uuid), uuid_str2);
2074 API_TRACE("Client Write Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
2075 CHECK_OAL_GATT_ENABLED();
2076 CHECK_CLIENT_CONNECTION(conn_id);
2078 ret = gatt_api->client->write_characteristic(conn_id, (btgatt_srvc_id_t *)srvc_id,
2079 (btgatt_gatt_id_t *)char_id, write_type, len, auth_req, data);
2080 if (ret != BT_STATUS_SUCCESS) {
2081 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
2082 return convert_to_oal_status(ret);
2085 return OAL_STATUS_SUCCESS;
2088 oal_status_t gattc_write_descriptor(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2089 oal_gatt_id_t *char_id, oal_gatt_id_t *desc_id,
2090 oal_gatt_write_type_t write_type, int len, oal_gatt_auth_req_t auth_req, char* data)
2092 int ret = OAL_STATUS_SUCCESS;
2093 char uuid_str1[2*BT_UUID_STRING_MAX];
2094 char uuid_str2[2*BT_UUID_STRING_MAX];
2095 char uuid_str3[2*BT_UUID_STRING_MAX];
2096 OAL_CHECK_PARAMETER(srvc_id, return);
2097 OAL_CHECK_PARAMETER(char_id, return);
2098 OAL_CHECK_PARAMETER(desc_id, return);
2099 OAL_CHECK_PARAMETER(data, return);
2100 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2101 uuid_to_stringname(&(char_id->uuid), uuid_str2);
2102 uuid_to_stringname(&(desc_id->uuid), uuid_str3);
2103 API_TRACE("Client Write Descriptor: Service_uuid: [%s], Char_uuid: [%s], Desc_uuid: [%s]",
2104 uuid_str1, uuid_str2, uuid_str3);
2105 CHECK_OAL_GATT_ENABLED();
2106 CHECK_CLIENT_CONNECTION(conn_id);
2108 ret = gatt_api->client->write_descriptor(conn_id, (btgatt_srvc_id_t *)srvc_id,
2109 (btgatt_gatt_id_t *)char_id, (btgatt_gatt_id_t *)desc_id, write_type, len, auth_req, data);
2110 if (ret != BT_STATUS_SUCCESS) {
2111 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
2112 return convert_to_oal_status(ret);
2115 return OAL_STATUS_SUCCESS;
2118 oal_status_t gattc_conn_param_update(bt_address_t * address, int min, int max, int latency, int timeout)
2123 OAL_CHECK_PARAMETER(address, return);
2124 CHECK_OAL_GATT_ENABLED();
2126 BT_DBG("[%s] min[%d] max[%d] latency[%d] timeout[%d]", bdt_bd2str(address, &bdstr), min, max, latency, timeout);
2127 ret = gatt_api->client->conn_parameter_update((bt_bdaddr_t *)address, min, max, latency, timeout);
2128 if (ret != BT_STATUS_SUCCESS) {
2129 BT_ERR("error: %s", status2string(ret));
2130 return convert_to_oal_status(ret);
2132 return OAL_STATUS_SUCCESS;
2135 oal_status_t gattc_unregister_scan_filter(int slot_id)
2141 CHECK_OAL_GATT_ENABLED();
2143 BT_INFO("Remove Scan filter Slot ID is: [%d]", slot_id);
2145 ret = gatt_api->client->scan_filter_clear(client_if, slot_id);
2146 if (ret != BT_STATUS_SUCCESS) {
2147 BT_ERR("error: %s", status2string(ret));
2148 return convert_to_oal_status(ret);
2150 return OAL_STATUS_SUCCESS;
2153 oal_status_t gattc_enable_scan_filter(int client_if)
2158 CHECK_OAL_GATT_ENABLED();
2160 BT_INFO("Enable Scan filter. Client If: %d", client_if);
2162 ret = gatt_api->client->scan_filter_enable(client_if, TRUE);
2163 if (ret != BT_STATUS_SUCCESS) {
2164 BT_ERR("error: %s", status2string(ret));
2165 return convert_to_oal_status(ret);
2167 return OAL_STATUS_SUCCESS;
2170 oal_status_t gattc_disable_scan_filter(int client_if)
2175 CHECK_OAL_GATT_ENABLED();
2177 BT_INFO("Disable Scan filter. Client If: %d", client_if);
2179 ret = gatt_api->client->scan_filter_enable(client_if, FALSE);
2180 if (ret != BT_STATUS_SUCCESS) {
2181 BT_ERR("error: %s", status2string(ret));
2182 return convert_to_oal_status(ret);
2184 return OAL_STATUS_SUCCESS;
2187 oal_status_t gattc_register_scan_filter(oal_ble_scan_filter_t* filter_data)
2190 int client_info = 0;
2193 int company_id_mask = 0;
2194 int address_type = 0;
2195 int feature_selection = 0;
2197 oal_ble_scan_filter_param_setup_t scan_filter_setup = {.list_logic_type = 0,
2198 .filt_logic_type = 1,
2199 .rssi_high_thres = -127,
2200 .rssi_low_thres = -127,
2204 .found_timeout_cnt = 0
2207 OAL_CHECK_PARAMETER(filter_data, return);
2209 CHECK_OAL_GATT_ENABLED();
2211 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS){
2213 BT_INFO("OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS is being added");
2214 BT_INFO("BT remote Device address is %s", bdt_bd2str(filter_data->device_address, &bdstr));
2215 ret = gatt_api->client->scan_filter_add_remove(client_info,
2217 OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS,
2218 filter_data->slot_id,
2223 (bt_bdaddr_t*)filter_data->device_address,
2230 if (ret != BT_STATUS_SUCCESS){
2231 BT_ERR("error: %s", status2string(ret));
2232 BT_INFO("unregistering already set filter features.");
2233 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2234 return convert_to_oal_status(ret);
2237 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS;
2239 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_NAME){
2240 ret = gatt_api->client->scan_filter_add_remove(client_info,
2242 OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_NAME,
2243 filter_data->slot_id,
2251 filter_data->device_name, // device_name as p_data in HAL
2255 if (ret != BT_STATUS_SUCCESS){
2256 BT_ERR("error: %s", status2string(ret));
2257 BT_INFO("unregistering already set filter features.");
2258 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2259 return convert_to_oal_status(ret);
2262 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_NAME;
2264 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_UUID){
2265 BT_INFO("OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_UUID is being added");
2266 char uuid_str1[2*BT_UUID_STRING_MAX];
2267 char uuid_str2[2*BT_UUID_STRING_MAX];
2269 uuid_to_stringname((service_uuid_t*)filter_data->service_uuid, uuid_str1);
2270 uuid_to_stringname((service_uuid_t*)filter_data->service_uuid_mask, uuid_str2);
2272 BT_INFO("Service UUID is [%s] and Service UUID mask is [%s]", uuid_str1, uuid_str2);
2273 ret = gatt_api->client->scan_filter_add_remove(client_info,
2275 OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_UUID,
2276 filter_data->slot_id,
2279 (bt_uuid_t*)filter_data->service_uuid,
2280 (bt_uuid_t*)filter_data->service_uuid_mask,
2283 filter_data->service_uuid_len, // service_uuid_len as data_len in HAL
2285 filter_data->service_uuid_mask_len,
2288 if (ret != BT_STATUS_SUCCESS){
2289 BT_ERR("error: %s", status2string(ret));
2290 BT_INFO("unregistering already set filter features.");
2291 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2292 return convert_to_oal_status(ret);
2295 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_UUID;
2297 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID){
2298 BT_INFO("OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID is being added");
2299 char uuid_str1[2*BT_UUID_STRING_MAX];
2300 char uuid_str2[2*BT_UUID_STRING_MAX];
2301 uuid_to_stringname((service_uuid_t*)filter_data->service_solicitation_uuid, uuid_str1);
2302 uuid_to_stringname((service_uuid_t*)filter_data->service_solicitation_uuid_mask, uuid_str2);
2303 BT_INFO("Service Solicitation UUID is [%s] and Service Solicitation UUID mask is [%s]", uuid_str1, uuid_str2);
2304 ret = gatt_api->client->scan_filter_add_remove(client_info,
2306 OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID,
2307 filter_data->slot_id,
2310 (bt_uuid_t*)filter_data->service_solicitation_uuid,
2311 (bt_uuid_t*)filter_data->service_solicitation_uuid_mask,
2314 filter_data->service_solicitation_uuid_len, // service_solicitation_uuid_len as data_len in HAL
2316 filter_data->service_solicitation_uuid_mask_len,
2319 if (ret != BT_STATUS_SUCCESS){
2320 BT_ERR("error: %s", status2string(ret));
2321 BT_INFO("unregistering already set filter features.");
2322 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2323 return convert_to_oal_status(ret);
2326 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID;
2328 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_DATA){
2329 ret = gatt_api->client->scan_filter_add_remove(client_info,
2331 OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_DATA,
2332 filter_data->slot_id,
2339 filter_data->service_data_len, //service_data_len as data_len in HAL
2340 (char*)filter_data->service_data,
2341 filter_data->service_data_mask_len,
2342 (char*)filter_data->service_data_mask
2344 if (ret != BT_STATUS_SUCCESS){
2345 BT_ERR("error: %s", status2string(ret));
2346 BT_INFO("unregistering already set filter features.");
2347 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2348 return convert_to_oal_status(ret);
2351 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_DATA;
2353 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA){
2354 ret = gatt_api->client->scan_filter_add_remove(client_info,
2356 OAL_BLE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA,
2357 filter_data->slot_id,
2358 filter_data->manufacturer_id,
2364 filter_data->manufacturer_data_len, //manufacturer_data_len as data_len in HAL
2365 (char*)filter_data->manufacturer_data,
2366 filter_data->manufacturer_data_mask_len,
2367 (char*)filter_data->manufacturer_data_mask
2369 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA;
2370 if (ret != BT_STATUS_SUCCESS){
2371 BT_ERR("error: %s", status2string(ret));
2372 BT_INFO("unregistering already set filter features.");
2373 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2374 return convert_to_oal_status(ret);
2377 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA;
2379 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_RSSI_HIGH_THRESHOLD) {
2380 scan_filter_setup.rssi_high_thres = filter_data->rssi_high_threshold;
2381 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_RSSI_HIGH_THRESHOLD;
2384 BT_DBG("Filter selection 0x%.2x", feature_selection);
2386 ret = gatt_api->client->scan_filter_param_setup(client_info, action, filter_data->slot_id, feature_selection,
2387 scan_filter_setup.list_logic_type, scan_filter_setup.filt_logic_type,
2388 scan_filter_setup.rssi_high_thres, scan_filter_setup.rssi_low_thres,
2389 scan_filter_setup.dely_mode, scan_filter_setup.found_timeout,
2390 scan_filter_setup.lost_timeout, scan_filter_setup.found_timeout_cnt);
2391 if (ret != BT_STATUS_SUCCESS){
2392 BT_ERR("error: %s", status2string(ret));
2393 return convert_to_oal_status(ret);
2395 return OAL_STATUS_SUCCESS;
2398 oal_status_t gattc_get_att_mtu(int conn_id, int *mtu)
2400 int ret = OAL_STATUS_SUCCESS;
2401 API_TRACE("Get ATT MTU, conn_id: %d", conn_id);
2402 CHECK_OAL_GATT_ENABLED();
2403 CHECK_CLIENT_CONNECTION(conn_id);
2404 OAL_CHECK_PARAMETER(mtu, return);
2406 /* To prevent crash in case other libraries not support this api */
2407 if (gatt_api->client->get_att_mtu == NULL) {
2408 BT_WARN("get_att_mtu is NULL");
2409 return OAL_STATUS_NOT_SUPPORT;
2412 ret = gatt_api->client->get_att_mtu(conn_id, mtu);
2413 if (ret != BT_STATUS_SUCCESS) {
2414 BT_ERR("GATT MTU Size failed, status: %s", status2string(ret));
2415 return convert_to_oal_status(ret);
2418 BT_INFO("Current ATT MTU Size: %d", *mtu);
2419 return OAL_STATUS_SUCCESS;
2422 oal_status_t gattc_configure_mtu(int conn_id, int mtu)
2426 API_TRACE("Configure MTU Size: [%d]", mtu);
2427 CHECK_OAL_GATT_ENABLED();
2428 CHECK_CLIENT_CONNECTION(conn_id);
2430 /* To prevent crash in case other libraries not support this api */
2431 if (gatt_api->client->configure_mtu == NULL) {
2432 BT_WARN("configure_mtu is NULL");
2433 return OAL_STATUS_NOT_SUPPORT;
2436 ret = gatt_api->client->configure_mtu(conn_id, mtu);
2437 if (ret != BT_STATUS_SUCCESS) {
2438 BT_ERR("Gatt client configure_mtu error: %s", status2string(ret));
2439 return convert_to_oal_status(ret);
2442 return OAL_STATUS_SUCCESS;
2445 oal_status_t gatt_get_data_batching_available_packets(
2446 unsigned int *available_packets)
2450 ret = gatt_api->client->get_data_batching_available_packets(available_packets);
2451 if (ret != BT_STATUS_SUCCESS) {
2452 BT_ERR("GATT data batching failed: %s", status2string(ret));
2453 return convert_to_oal_status(ret);
2456 return OAL_STATUS_SUCCESS;
2459 oal_status_t gatt_enable_data_batching(bt_address_t * address,
2460 int packet_threshold, int timeout)
2464 ret = gatt_api->client->enable_data_batching((bt_bdaddr_t *)address, packet_threshold, timeout);
2465 if (ret != BT_STATUS_SUCCESS) {
2466 BT_ERR("GATT data batching failed: %s", status2string(ret));
2467 return convert_to_oal_status(ret);
2470 return OAL_STATUS_SUCCESS;
2473 oal_status_t gatt_disable_data_batching(bt_address_t * address)
2477 ret = gatt_api->client->disable_data_batching((bt_bdaddr_t *)address);
2478 if (ret != BT_STATUS_SUCCESS) {
2479 BT_ERR("GATT data batching failed: %s", status2string(ret));
2480 return convert_to_oal_status(ret);
2483 return OAL_STATUS_SUCCESS;
2486 oal_status_t gattc_add_connection_info(bt_address_t *device_address, int conn_id, int instance_id)
2488 int ret = OAL_STATUS_SUCCESS;
2491 OAL_CHECK_PARAMETER(device_address, return);
2492 API_TRACE("Add connection info: [%s]", bdt_bd2str(device_address, &bdstr) + 12);
2493 CHECK_OAL_GATT_ENABLED();
2495 ret = gatt_api->client->add_connection_info((bt_bdaddr_t *)device_address, conn_id, instance_id);
2496 if (ret != BT_STATUS_SUCCESS) {
2497 BT_ERR("GATT connection info add failed: %s", status2string(ret));
2498 return convert_to_oal_status(ret);
2500 return OAL_STATUS_SUCCESS;