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);
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;
425 /* Below API's need to be removed as they are hardcoded API's.
426 If at all, product specific API's are required to set specific data, then new naming should
427 be adopted for these API's */
428 oal_status_t gatts_set_advertising_params(int instance_id, float min_intv, float max_intv)
430 int ret = OAL_STATUS_SUCCESS;
432 API_TRACE("Set advertising params min_int:%f, max_int:%f", min_intv, max_intv);
433 CHECK_OAL_GATT_ENABLED();
434 CHECK_SERVER_INSTANCE(instance_id);
435 CHECK_SERVER_REGISTRATION(instance_id);
437 /* Historically used for Legacy Advertising */
438 return OAL_STATUS_NOT_SUPPORT;
441 oal_status_t gatts_set_advertising_data(int instance_id, uint16_t manufac_len, char* manufac_data)
443 int ret = OAL_STATUS_SUCCESS;
445 API_TRACE("Set advertising data");
446 CHECK_OAL_GATT_ENABLED();
447 CHECK_SERVER_INSTANCE(instance_id);
448 CHECK_SERVER_REGISTRATION(instance_id);
450 /* Historically used for Legacy Advertising */
451 return OAL_STATUS_NOT_SUPPORT;
454 oal_status_t gatts_set_scan_response(int instance_id, uint16_t manufac_len, char* manufac_data)
456 int ret = OAL_STATUS_SUCCESS;
458 API_TRACE("Set Scan Response data");
459 CHECK_OAL_GATT_ENABLED();
460 CHECK_SERVER_INSTANCE(instance_id);
461 CHECK_SERVER_REGISTRATION(instance_id);
463 /* Historically used for Legacy Advertising */
464 return OAL_STATUS_NOT_SUPPORT;
468 oal_status_t gatts_multi_adv_enable(int instance_id)
470 int ret = OAL_STATUS_SUCCESS;
472 API_TRACE("Start Multi advertising, instance_id: %d", instance_id);
474 CHECK_OAL_GATT_ENABLED();
475 CHECK_SERVER_INSTANCE(instance_id);
476 info = __gatts_find_server_instance(instance_id);
478 return OAL_STATUS_INTERNAL_ERROR;
480 if (gatt_api->server->multi_adv_enable == NULL)
481 return OAL_STATUS_NOT_SUPPORT;
483 /* Platform Requirement: Advertising to be enabled even when no data is set: i.e Empty Advertisement
484 if (gatt_servers[instance_id - 1].state != GATT_INS_DATA_SET){
485 BT_ERR("Not allowed, state: %d, instance_id: %d",
486 gatt_servers[instance_id - 1].state, instance_id);
487 return OAL_STATUS_BUSY;
491 if (info->state == GATT_INS_ENABLED) {
492 BT_ERR("Not allowed, state: %d, instance_id: %d", info->state, instance_id);
493 return OAL_STATUS_ALREADY_DONE;
494 } else if (info->state == GATT_INS_ENABLING) {
495 BT_ERR("Not allowed, state: %d, instance_id: %d", info->state, instance_id);
496 return OAL_STATUS_BUSY;
498 info->state = GATT_INS_ENABLING;
500 ret = gatt_api->server->multi_adv_enable(instance_id);
501 if (ret != BT_STATUS_SUCCESS) {
502 BT_ERR("GATT enable multi advertising failed for server Instance: %d With Status: %s",
503 instance_id, status2string(ret));
504 // gatt_servers[instance_id - 1].state = GATT_INS_DATA_SET;
505 return convert_to_oal_status(ret);
507 return OAL_STATUS_SUCCESS;
510 oal_status_t gatts_multi_adv_disable(int instance_id)
512 int ret = OAL_STATUS_SUCCESS;
514 API_TRACE("Stop Multi advertising, instance_id: %d", instance_id);
516 CHECK_OAL_GATT_ENABLED();
517 CHECK_SERVER_INSTANCE(instance_id);
518 info = __gatts_find_server_instance(instance_id);
520 return OAL_STATUS_INTERNAL_ERROR;
522 if (gatt_api->server->multi_adv_disable == NULL)
523 return OAL_STATUS_NOT_SUPPORT;
525 if (info->state != GATT_INS_ENABLED) {
526 BT_ERR("Not Allowed, state: %d, instance_id: %d", info->state, instance_id);
527 return OAL_STATUS_BUSY;
530 ret = gatt_api->server->multi_adv_disable(instance_id);
531 if (ret != BT_STATUS_SUCCESS) {
532 BT_ERR("GATT disable multi advertising failed for server Instance: %d With Status: %s",
533 instance_id, status2string(ret));
534 return convert_to_oal_status(ret);
536 return OAL_STATUS_SUCCESS;
539 oal_status_t gatts_set_filter_policy(int filter_policy)
541 int ret = OAL_STATUS_SUCCESS;
543 CHECK_OAL_GATT_ENABLED();
545 /* send the filter_policy value to the HAL Layer */
546 ret = gatt_api->server->set_filter_policy(filter_policy);
548 if (ret != BT_STATUS_SUCCESS) {
549 BT_ERR("set_filter_policy failed: %d",ret);
550 return convert_to_oal_status(ret);
553 return OAL_STATUS_SUCCESS;
556 oal_status_t gatts_multi_adv_update(int instance_id,
557 int min_intv, int max_intv,
558 int adv_type, int chnl_map,
559 int tx_power, int timeout_s)
561 int ret = OAL_STATUS_SUCCESS;
562 API_TRACE("Multi advertising Update");
564 CHECK_OAL_GATT_ENABLED();
565 CHECK_SERVER_INSTANCE(instance_id);
566 if (__gatts_find_server_instance(instance_id) == NULL)
567 return OAL_STATUS_INTERNAL_ERROR;
569 if (gatt_api->server->multi_adv_update == NULL)
570 return OAL_STATUS_NOT_SUPPORT;
572 ret = gatt_api->server->multi_adv_update(instance_id,
575 tx_power, timeout_s);
576 if (ret != BT_STATUS_SUCCESS) {
577 BT_ERR("GATT update multi advertising failed for server Instance: %d With Status: %s",
578 instance_id, status2string(ret));
579 return convert_to_oal_status(ret);
581 return OAL_STATUS_SUCCESS;
584 oal_status_t gatts_multi_adv_set_inst_data(int instance_id,
585 oal_ble_multi_adv_param_setup_t *adv_param_setup)
587 int ret = OAL_STATUS_SUCCESS;
588 btgatt_adv_param_setup_t adv_setup;
590 API_TRACE("Set Multi advertising data, instance_id: %d", instance_id);
592 CHECK_OAL_GATT_ENABLED();
593 CHECK_SERVER_INSTANCE(instance_id);
594 info = __gatts_find_server_instance(instance_id);
596 return OAL_STATUS_INTERNAL_ERROR;
598 if (gatt_api->server->multi_adv_set_inst_data == NULL)
599 return OAL_STATUS_NOT_SUPPORT;
601 if (info->state >= GATT_INS_DATA_SETTING && info->state != GATT_INS_DATA_SET) {
602 BT_ERR("Not Allowed, state: %d, instance_id: %d", info->state, instance_id);
603 return OAL_STATUS_BUSY;
605 info->state = GATT_INS_DATA_SETTING;
607 adv_setup.set_scan_rsp = adv_param_setup->set_scan_rsp;
608 adv_setup.include_name = adv_param_setup->include_name;
609 adv_setup.include_txpower = adv_param_setup->include_txpower;
610 adv_setup.appearance = adv_param_setup->appearance;
611 adv_setup.manufacturer_data = adv_param_setup->manufacturer_data;
612 adv_setup.manufacturer_data_len = adv_param_setup->manufacturer_data_len;
613 adv_setup.service_data = adv_param_setup->service_data;
614 adv_setup.service_data_len = adv_param_setup->service_data_len;
615 adv_setup.service_uuid = adv_param_setup->service_uuid;
616 adv_setup.service_uuid_len = adv_param_setup->service_uuid_len;
617 /* Solicit UUID handler: Start */
618 adv_setup.solicit_uuid = adv_param_setup->solicit_uuid;
619 adv_setup.solicit_uuid_len = adv_param_setup->solicit_uuid_len;
620 /* Solicit UUID handler: End */
621 adv_setup.min_interval = adv_param_setup->min_interval;
622 adv_setup.max_interval = adv_param_setup->min_interval;
623 adv_setup.adv_type = adv_param_setup->adv_type;
624 adv_setup.chnl_map = adv_param_setup->chnl_map;
625 adv_setup.tx_power = adv_param_setup->tx_power;
626 adv_setup.timeout_s = adv_param_setup->timeout_s;
628 adv_setup.server_if = instance_id;
630 ret = gatt_api->server->multi_adv_set_inst_data(adv_setup);
631 if (ret != BT_STATUS_SUCCESS) {
632 BT_ERR("GATT Set Multi advertising data failed: %s", status2string(ret));
633 info->state = GATT_INS_DISABLED;
634 return convert_to_oal_status(ret);
636 return OAL_STATUS_SUCCESS;
639 oal_status_t gatts_disconnect(int instance_id, bt_address_t *device_address, int conn_id)
641 int ret = OAL_STATUS_SUCCESS;
644 API_TRACE("Server Disonnect: instance_id = %d, conn_id = %d", instance_id, conn_id);
646 CHECK_OAL_GATT_ENABLED();
647 CHECK_SERVER_INSTANCE(instance_id);
648 if (__gatts_find_server_instance(instance_id) == NULL)
649 return OAL_STATUS_INTERNAL_ERROR;
651 API_TRACE("[%s]", bdt_bd2str(device_address, &bdstr));
653 ret = gatt_api->server->disconnect(instance_id, (bt_bdaddr_t *) device_address, conn_id);
654 if (ret != BT_STATUS_SUCCESS) {
655 BT_ERR("GATT Server disconnect failed: %s", status2string(ret));
656 return convert_to_oal_status(ret);
658 return OAL_STATUS_SUCCESS;
662 /************************************GATT Server Functions*************************************/
664 static void cb_gatts_register_app(int status, int server_if, bt_uuid_t *uuid)
666 char str[2*BT_UUID_STRING_MAX];
667 uuid_to_stringname((service_uuid_t*)uuid, str);
669 event_gatts_register_t *event = g_malloc0(sizeof(event_gatts_register_t));
671 BT_INFO("BTGATT SERVER REGISTER APP CB, UUID:%s, status:%d, server_if:%d",
672 str, status, server_if);
674 gatt_server_t *info = g_malloc0(sizeof(gatt_server_t));
675 info->instance_id = server_if;
676 memcpy(info->uuid.uuid, uuid->uu, sizeof(bt_uuid_t));
677 gatt_servers = g_slist_append(gatt_servers, info);
678 BT_DBG("GATT Server Added. count of gatt_servers: %d", g_slist_length(gatt_servers));
680 event->server_inst = server_if;
681 memcpy(event->server_uuid.uuid, uuid->uu, sizeof(bt_uuid_t));
682 send_event(OAL_EVENT_BLE_SERVER_INSTANCE_INITIALISED, event, sizeof(event_gatts_register_t));
685 static void cb_gatts_listen(int status, int server_if)
688 gboolean new_state = FALSE;
689 event_ble_adv_status *event = g_malloc0(sizeof(event_ble_adv_status));
692 for (l = gatt_servers; l; l = g_slist_next(l)) {
693 gatt_server_t *info = l->data;
696 if (info->instance_id == server_if) {
697 event->server_inst = server_if;
698 prev_state = info->cur_adv_state;
699 new_state = (status == BT_STATUS_SUCCESS) ? !prev_state : prev_state;
700 info->cur_adv_state = new_state;
705 BT_INFO("Adv State of server instance %d, new_state: %d", server_if, new_state);
707 if (TRUE == new_state)
708 send_event(OAL_EVENT_BLE_ADVERTISING_STARTED, event, sizeof(event_ble_adv_status));
710 send_event(OAL_EVENT_BLE_ADVERTISING_STOPPED, event, sizeof(event_ble_adv_status));
713 static void cb_gatts_multi_adv_enable(int server_if, int status)
716 BT_INFO("BTGATT SERVER MULTI ADV ENABLE CB, status:%d, srv_if: %d", status, server_if);
717 event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
720 if (status != BT_STATUS_SUCCESS)
721 BT_ERR("MULTI ADV ENABLE failed, status:%d, srv_if: %d", status, server_if);
723 for (l = gatt_servers; l; l = g_slist_next(l)) {
724 gatt_server_t *info = l->data;
727 if (info->instance_id == server_if) {
728 event->server_inst = server_if;
729 info->state = GATT_INS_ENABLED;
733 event->server_inst = -1;
735 event->status = convert_to_oal_status(status);
736 send_event(OAL_EVENT_BLE_MULTI_ADVERTISING_ENABLE, event, sizeof(event_ble_multiadv_status));
739 static void cb_gatts_multi_adv_disable(int server_if, int status)
741 BT_INFO("BTGATT SERVER MULTI ADV DISABLE CB, status:%d, srv_if: %d", status, server_if);
742 event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
745 if (status != BT_STATUS_SUCCESS)
746 BT_ERR("MULTI ADV DISABLE failed, status:%d, srv_if: %d", status, server_if);
748 for (l = gatt_servers; l; l = g_slist_next(l)) {
749 gatt_server_t *info = l->data;
752 if (info->instance_id == server_if) {
753 event->server_inst = server_if;
754 info->state = GATT_INS_DISABLED;
758 event->server_inst = -1;
760 event->status = convert_to_oal_status(status);
761 send_event(OAL_EVENT_BLE_MULTI_ADVERTISING_DISABLE, event, sizeof(event_ble_multiadv_status));
764 static void cb_gatts_multi_adv_update(int server_if, int status)
766 BT_INFO("BTGATT SERVER MULTI ADV UPDATE CB, status:%d, srv_if: %d", status, server_if);
767 event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
770 if (status != BT_STATUS_SUCCESS)
771 BT_ERR("MULTI ADV UPDATE failed, status:%d, srv_if: %d", status, server_if);
773 for (l = gatt_servers; l; l = g_slist_next(l)) {
774 gatt_server_t *info = l->data;
777 if (info->instance_id == server_if) {
778 event->server_inst = server_if;
782 event->server_inst = -1;
784 event->status = convert_to_oal_status(status);
785 send_event(OAL_EVENT_BLE_MULTI_ADVERTISING_UPDATE, event, sizeof(event_ble_multiadv_status));
788 static void cb_gatts_multi_adv_set_inst_data(int server_if, int status)
790 BT_INFO("BTGATT SERVER MULTI ADV SET INST DATA CB, status:%d, srv_if: %d", status, server_if);
791 event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
794 if (status != BT_STATUS_SUCCESS)
795 BT_ERR("MULTI ADV SET DATA failed, status:%d, srv_if: %d", status, server_if);
797 for (l = gatt_servers; l; l = g_slist_next(l)) {
798 gatt_server_t *info = l->data;
801 if (info->instance_id == server_if) {
802 event->server_inst = server_if;
803 info->state = GATT_INS_DATA_SET;
807 event->server_inst = -1;
809 event->status = convert_to_oal_status(status);
810 send_event(OAL_EVENT_BLE_MULTI_ADVERTISING_SET_INST_DATA, event, sizeof(event_ble_multiadv_status));
813 oal_status_t gatts_get_att_mtu(int conn_id, int *mtu)
815 int ret = OAL_STATUS_SUCCESS;
816 API_TRACE("Get ATT MTU, conn_id: %d", conn_id);
817 CHECK_OAL_GATT_ENABLED();
818 OAL_CHECK_PARAMETER(mtu, return);
820 /* To prevent crash in case other libraries not support this api */
821 if (gatt_api->server->get_att_mtu == NULL) {
822 BT_WARN("get_att_mtu is NULL");
823 return OAL_STATUS_NOT_SUPPORT;
826 ret = gatt_api->server->get_att_mtu(conn_id, mtu);
827 if (ret != BT_STATUS_SUCCESS) {
828 BT_ERR("GATT MTU Size failed, status: %s", status2string(ret));
829 return convert_to_oal_status(ret);
832 BT_INFO("Current ATT MTU Size: %d", *mtu);
833 return OAL_STATUS_SUCCESS;
835 oal_status_t gatt_send_response_acquire(int conn_id, int trans_id,
836 int status, int fd, int mtu , void * fdlist)
838 int ret = OAL_STATUS_SUCCESS;
840 API_TRACE("Server Send Response : ConnId = %d, TransId = %d, %d", conn_id, trans_id, fd);
841 CHECK_OAL_GATT_ENABLED();
843 ret = gatt_api->server->send_response_acquire(conn_id, trans_id, status, fd, mtu, fdlist);
844 if (ret != BT_STATUS_SUCCESS) {
845 BT_ERR("GATT Server Send Response failed: %s", status2string(ret));
846 return convert_to_oal_status(ret);
848 return OAL_STATUS_SUCCESS;
851 oal_status_t gatts_add_service(int instance_id, oal_gatt_srvc_id_t *gatt_serv_id,
854 int ret = OAL_STATUS_SUCCESS;
855 btgatt_srvc_id_t btgatt_srvc_id;
856 char str[2*BT_UUID_STRING_MAX];
858 API_TRACE("Server Add Service : instance_id = %d, num_handles = %d", instance_id, num_handles);
859 CHECK_OAL_GATT_ENABLED();
861 CHECK_SERVER_INSTANCE(instance_id);
862 if (__gatts_find_server_instance(instance_id) == NULL)
863 return OAL_STATUS_INTERNAL_ERROR;
865 if (gatt_serv_id != NULL) {
866 uuid_to_stringname(&(gatt_serv_id->id.uuid), str);
867 API_TRACE("Service uuid: [%s]", str);
868 memcpy(btgatt_srvc_id.id.uuid.uu, gatt_serv_id->id.uuid.uuid, sizeof(gatt_serv_id->id.uuid));
869 btgatt_srvc_id.id.inst_id = gatt_serv_id->id.inst_id;
870 btgatt_srvc_id.is_primary = gatt_serv_id->is_prmry;
872 BT_INFO("GATT Server Service Id is NULL");
873 return OAL_STATUS_INVALID_PARAM;
876 ret = gatt_api->server->add_service(instance_id, &btgatt_srvc_id, num_handles);
877 if (ret != BT_STATUS_SUCCESS) {
878 BT_ERR("GATT Server add service failed: %s", status2string(ret));
879 return convert_to_oal_status(ret);
881 return OAL_STATUS_SUCCESS;
884 oal_status_t gatts_add_included_services(int instance_id, int serv_handle,
887 int ret = OAL_STATUS_SUCCESS;
888 API_TRACE("Server Add included Service : instance_id = %d, serv_handle = %d, incl_handle = %d",
889 instance_id, serv_handle, incl_handle);
890 CHECK_OAL_GATT_ENABLED();
892 CHECK_SERVER_INSTANCE(instance_id);
893 if (__gatts_find_server_instance(instance_id) == NULL)
894 return OAL_STATUS_INTERNAL_ERROR;
896 ret = gatt_api->server->add_included_service(instance_id, serv_handle, incl_handle);
897 if (ret != BT_STATUS_SUCCESS) {
898 BT_ERR("GATT Server Add included Service failed: %s", status2string(ret));
899 return convert_to_oal_status(ret);
901 return OAL_STATUS_SUCCESS;
904 oal_status_t gatts_add_characteristics(int instance_id, int serv_handle,
905 oal_uuid_t* charc_uuid, int propts, int permsn)
907 int ret = OAL_STATUS_SUCCESS;
908 char str[2*BT_UUID_STRING_MAX];
909 char prop[MAX_PROP_LEN], perm[MAX_PERM_LEN];
910 char_prop_to_string(propts, prop);
911 char_perm_to_string(permsn, perm);
912 API_TRACE("Server Add Characteristic : instance_id = %d, serv_handle = %d, Properties =%s, Permisison = %s",
913 instance_id, serv_handle, prop, perm);
914 CHECK_OAL_GATT_ENABLED();
916 CHECK_SERVER_INSTANCE(instance_id);
917 if (__gatts_find_server_instance(instance_id) == NULL)
918 return OAL_STATUS_INTERNAL_ERROR;
920 if (charc_uuid != NULL) {
921 uuid_to_stringname(charc_uuid, str);
922 API_TRACE("uuid: [%s]", str);
925 ret = gatt_api->server->add_characteristic(instance_id,
926 serv_handle, (bt_uuid_t*)charc_uuid, propts, permsn);
927 if (ret != BT_STATUS_SUCCESS) {
928 BT_ERR("GATT Server Add Characteristic failed: %s", status2string(ret));
929 return convert_to_oal_status(ret);
931 return OAL_STATUS_SUCCESS;
934 oal_status_t gatts_add_descriptor(int instance_id, int serv_handle,
935 oal_uuid_t* desc_uuid, int permsn)
937 int ret = OAL_STATUS_SUCCESS;
938 char str[2*BT_UUID_STRING_MAX];
939 char perm[MAX_PERM_LEN];
940 char_perm_to_string(permsn, perm);
941 API_TRACE("Server Add Descriptor : instance_id = %d, serv_handle = %d, Permisison = %s",
942 instance_id, serv_handle, perm);
943 CHECK_OAL_GATT_ENABLED();
945 CHECK_SERVER_INSTANCE(instance_id);
946 if (__gatts_find_server_instance(instance_id) == NULL)
947 return OAL_STATUS_INTERNAL_ERROR;
949 if (desc_uuid != NULL) {
950 uuid_to_stringname(desc_uuid, str);
951 API_TRACE("uuid: [%s]", str);
954 ret = gatt_api->server->add_descriptor(instance_id,
955 serv_handle, (bt_uuid_t*)desc_uuid, permsn);
956 if (ret != BT_STATUS_SUCCESS) {
957 BT_ERR("GATT Server Add Descriptor failed: %s", status2string(ret));
958 return convert_to_oal_status(ret);
960 return OAL_STATUS_SUCCESS;
963 oal_status_t gatts_start_service(int instance_id, int svc_handle, int transport)
965 int ret = OAL_STATUS_SUCCESS;
967 API_TRACE("Server Start Service : Ins_id = %d, srvHndl = %d, transport = %d",
968 instance_id, svc_handle, transport);
969 CHECK_OAL_GATT_ENABLED();
971 CHECK_SERVER_INSTANCE(instance_id);
972 if (__gatts_find_server_instance(instance_id) == NULL)
973 return OAL_STATUS_INTERNAL_ERROR;
975 ret = gatt_api->server->start_service(instance_id, svc_handle, transport);
976 if (ret != BT_STATUS_SUCCESS) {
977 BT_ERR("GATT Server Start Service failed: %s", status2string(ret));
978 return convert_to_oal_status(ret);
980 return OAL_STATUS_SUCCESS;
983 oal_status_t gatts_stop_service(int instance_id, int srv_hdl)
985 int ret = OAL_STATUS_SUCCESS;
987 API_TRACE("Server Stop Service : Ins_id = %d, srvHndl = %d, ", instance_id, srv_hdl);
988 CHECK_OAL_GATT_ENABLED();
990 CHECK_SERVER_INSTANCE(instance_id);
991 if (__gatts_find_server_instance(instance_id) == NULL)
992 return OAL_STATUS_INTERNAL_ERROR;
994 ret = gatt_api->server->stop_service(instance_id, srv_hdl);
995 if (ret != BT_STATUS_SUCCESS) {
996 BT_ERR("GATT Server Stop Service failed: %s", status2string(ret));
997 return convert_to_oal_status(ret);
999 return OAL_STATUS_SUCCESS;
1002 oal_status_t gatts_delete_service(int instance_id, int srv_hdl)
1004 int ret = OAL_STATUS_SUCCESS;
1006 API_TRACE("Server Delete Service : Ins_id = %d, srvHndl = %d, ", instance_id, srv_hdl);
1007 CHECK_OAL_GATT_ENABLED();
1009 CHECK_SERVER_INSTANCE(instance_id);
1010 if (__gatts_find_server_instance(instance_id) == NULL)
1011 return OAL_STATUS_INTERNAL_ERROR;
1013 ret = gatt_api->server->delete_service(instance_id, srv_hdl);
1014 if (ret != BT_STATUS_SUCCESS) {
1015 BT_ERR("GATT Server Delete Service failed: %s", status2string(ret));
1016 return convert_to_oal_status(ret);
1018 return OAL_STATUS_SUCCESS;
1021 oal_status_t gatts_send_response(int conn_id, int trans_id, int resp_status, oal_gatt_response_t *response)
1023 int ret = OAL_STATUS_SUCCESS;
1025 API_TRACE("Server Send Response : ConnId = %d, TransId = %d, ", conn_id, trans_id);
1026 CHECK_OAL_GATT_ENABLED();
1028 if (response == NULL) {
1029 BT_ERR("GATT Server attribute value is empty");
1030 return OAL_STATUS_INVALID_PARAM;
1033 ret = gatt_api->server->send_response(conn_id, trans_id, resp_status, (btgatt_response_t *)response);
1034 if (ret != BT_STATUS_SUCCESS) {
1035 BT_ERR("GATT Server Send Response failed: %s", status2string(ret));
1036 return convert_to_oal_status(ret);
1038 return OAL_STATUS_SUCCESS;
1041 oal_status_t gatts_send_indication(int instance_id, int attr_hndl, int conn_id, int len, int confirm, char* value)
1043 int ret = OAL_STATUS_SUCCESS;
1045 API_TRACE("Server Send Indication : Ins_id = %d, srvHndl = %d, [%s]",
1046 instance_id, attr_hndl, confirm == TRUE ? "INDICATION" : "NOTIFICATION");
1047 CHECK_OAL_GATT_ENABLED();
1049 CHECK_SERVER_INSTANCE(instance_id);
1050 if (__gatts_find_server_instance(instance_id) == NULL)
1051 return OAL_STATUS_INTERNAL_ERROR;
1053 if (value == NULL || len == 0) {
1054 BT_ERR("GATT Server attribute value is empty");
1055 return OAL_STATUS_INVALID_PARAM;
1058 ret = gatt_api->server->send_indication(instance_id,
1059 attr_hndl, conn_id, len, confirm, value);
1060 if (ret != BT_STATUS_SUCCESS) {
1061 BT_ERR("GATT Server Send Indication failed: %s", status2string(ret));
1062 return convert_to_oal_status(ret);
1064 return OAL_STATUS_SUCCESS;
1067 oal_status_t gatts_update_att_value(int instance_id, oal_gatt_value_t *value)
1069 int ret = OAL_STATUS_SUCCESS;
1071 CHECK_OAL_GATT_ENABLED();
1073 CHECK_SERVER_INSTANCE(instance_id);
1074 if (__gatts_find_server_instance(instance_id) == NULL)
1075 return OAL_STATUS_INTERNAL_ERROR;
1077 if (value == NULL || value->len == 0) {
1078 BT_ERR("GATT Server attribute value is empty");
1079 return OAL_STATUS_INVALID_PARAM;
1082 API_TRACE("Server Update Value: Ins_id = [%d], att handle = [%d] data len [%d]",
1083 instance_id, value->handle, value->len);
1085 ret = gatt_api->server->update_att_value(instance_id,
1086 (int)value->handle, (int)value->len, (char*)value->value);
1087 if (ret != BT_STATUS_SUCCESS) {
1088 BT_ERR("GATT Server Update Attribute Value failed: [%s]", status2string(ret));
1089 return convert_to_oal_status(ret);
1091 return OAL_STATUS_SUCCESS;
1094 /* GATT Server Callbacks:Start */
1095 static void cb_gatts_service_added(int status, int server_if,
1096 btgatt_srvc_id_t *psrvc_id, int srvc_handle)
1098 BT_INFO("BTGATT SERVER SERVICE ADDED CB, status:%d, handle:%d", status, srvc_handle);
1099 event_gatts_srvc_prm_t* event = g_new0(event_gatts_srvc_prm_t, 1);
1101 CHECK_CORRECT_SERVER_ID(server_if, event->gatt_srvc_stat.server_inst);
1103 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1104 event->gatt_srvc_stat.server_inst = -1;
1106 event->gatt_srvc_stat.servic_hndl = srvc_handle;
1107 if (psrvc_id != NULL) {
1108 event->gatt_srvc_id.id.inst_id = psrvc_id->id.inst_id;
1109 //event->gatt_srvc_id.id.uuid = psrvc_id->id.uuid;
1110 memcpy(event->gatt_srvc_id.id.uuid.uuid, psrvc_id->id.uuid.uu, sizeof(psrvc_id->id.uuid));
1111 event->gatt_srvc_id.is_prmry = psrvc_id->is_primary;
1114 event->gatt_srvc_stat.status = convert_to_oal_status(status);
1116 send_event(OAL_EVENT_GATTS_SERVICE_ADDED, event, sizeof(event_gatts_srvc_prm_t));
1119 static void cb_gatts_included_service_added(int status, int server_if,
1121 int incl_srvc_handle)
1123 BT_INFO("BTGATT SERVER INCLUDED SERVICE ADDED CB, status:%d, srvc handle:%d, incl handle:%d",
1124 status, srvc_handle, incl_srvc_handle);
1126 event_gatts_incld_srvc_t* event = g_new0(event_gatts_incld_srvc_t, 1);
1128 CHECK_CORRECT_SERVER_ID(server_if, event->gatt_srvc_stat.server_inst);
1130 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1131 event->gatt_srvc_stat.server_inst = -1;
1133 event->gatt_srvc_stat.servic_hndl = srvc_handle;
1134 event->incl_srvc_hndl = incl_srvc_handle;
1135 event->gatt_srvc_stat.status = convert_to_oal_status(status);
1136 send_event(OAL_EVENT_GATTS_INCLUDED_SERVICE_ADDED, event, sizeof(event_gatts_incld_srvc_t));
1139 static void cb_gatts_characteristic_added(int status, int server_if, bt_uuid_t *char_id,
1140 int srvc_handle, int char_handle)
1142 BT_INFO("BTGATT SERVER CHARACTERISTIC ADDED CB, status:%d, srvc handle:%d, char handle:%d",
1143 status, srvc_handle, char_handle);
1145 event_gatts_srvc_charctr_t* event = g_new0(event_gatts_srvc_charctr_t, 1);
1147 CHECK_CORRECT_SERVER_ID(server_if, event->gatt_srvc_stat.server_inst);
1149 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1150 event->gatt_srvc_stat.server_inst = -1;
1152 event->gatt_srvc_stat.servic_hndl = srvc_handle;
1153 event->charctr_hndl = char_handle;
1154 event->gatt_srvc_stat.status = convert_to_oal_status(status);
1155 memcpy(event->charctr_uuid.uuid, char_id->uu, sizeof(bt_uuid_t));
1156 send_event(OAL_EVENT_GATTS_CHARACTERISTIC_ADDED, event, sizeof(event_gatts_srvc_charctr_t));
1159 static void cb_gatts_descriptor_added(int status, int server_if,
1160 bt_uuid_t *descr_id, int srvc_handle,
1163 BT_INFO("BTGATT SERVER DESCRIPTOR ADDED CB, status:%d, srvc handle:%d, desc handle:%d",
1164 status, srvc_handle, descr_handle);
1166 event_gatts_srvc_descr_t* event = g_new0(event_gatts_srvc_descr_t, 1);
1168 CHECK_CORRECT_SERVER_ID(server_if, event->gatt_srvc_stat.server_inst);
1170 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1171 event->gatt_srvc_stat.server_inst = -1;
1173 event->gatt_srvc_stat.servic_hndl = srvc_handle;
1174 event->descrptr_hndl = descr_handle;
1175 event->gatt_srvc_stat.status = convert_to_oal_status(status);
1176 memcpy(event->descrptr_uuid.uuid, descr_id->uu, sizeof(bt_uuid_t));
1177 send_event(OAL_EVENT_GATTS_DESCRIPTOR_ADDED, event, sizeof(event_gatts_srvc_descr_t));
1180 static void cb_gatts_service_started(int status, int server_if, int srvc_handle)
1182 BT_INFO("BTGATT SERVER SERVICE STARTED CB, status:%d, srvc_handle:%d", status, srvc_handle);
1184 event_gatts_srvc_t* event = g_new0(event_gatts_srvc_t, 1);
1186 CHECK_CORRECT_SERVER_ID(server_if, event->server_inst);
1188 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1189 event->server_inst = -1;
1191 event->servic_hndl = srvc_handle;
1192 event->status = convert_to_oal_status(status);
1193 send_event(OAL_EVENT_GATTS_SERVICE_STARTED, event, sizeof(event_gatts_srvc_t));
1196 static void cb_gatts_service_stopped(int status, int server_if, int srvc_handle)
1198 BT_INFO("BTGATT SERVER SERVICE STOPPED CB, status:%d, srvc_handle:%d", status, srvc_handle);
1200 event_gatts_srvc_t* event = g_new0(event_gatts_srvc_t, 1);
1202 CHECK_CORRECT_SERVER_ID(server_if, event->server_inst);
1204 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1205 event->server_inst = -1;
1207 event->servic_hndl = srvc_handle;
1208 event->status = convert_to_oal_status(status);
1209 send_event(OAL_EVENT_GATTS_SERVICE_STOPED, event, sizeof(event_gatts_srvc_t));
1212 static void cb_gatts_service_deleted(int status, int server_if, int srvc_handle)
1214 BT_INFO("BTGATT SERVER SERVICE DELETED CB, status:%d, srvc_handle:%d", status, srvc_handle);
1216 event_gatts_srvc_t* event = g_new0(event_gatts_srvc_t, 1);
1218 CHECK_CORRECT_SERVER_ID(server_if, event->server_inst);
1220 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1221 event->server_inst = -1;
1223 event->servic_hndl = srvc_handle;
1224 event->status = convert_to_oal_status(status);
1225 send_event(OAL_EVENT_GATTS_SERVICE_DELETED, event, sizeof(event_gatts_srvc_t));
1228 static void cb_gatts_connection(int conn_id, int server_if, int connected, bt_bdaddr_t *bda)
1231 oal_event_t event_type;
1233 BT_INFO("BTGATT SERVER CONNECTION connnected:%d, conn_id:%d server_if:%d", connected, conn_id, server_if);
1234 event_gatts_conn_t *event = g_new0(event_gatts_conn_t, 1);
1236 if (connected == TRUE) {
1237 for (l = gatt_servers; l; l = g_slist_next(l)) {
1238 gatt_server_t *info = l->data;
1241 if (info->instance_id == server_if) {
1242 event->server_inst = server_if;
1247 /* server_inst is not required in disconnected case */
1249 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1250 event->conn_id = conn_id;
1251 event_type = ((connected == TRUE) ? OAL_EVENT_GATTS_CONNECTION_COMPLETED : OAL_EVENT_GATTS_DISCONNECTION_COMPLETED);
1252 event->status = OAL_STATUS_SUCCESS;
1254 send_event(event_type, event, sizeof(event_gatts_conn_t));
1257 static void cb_gatts_request_read(int conn_id, int trans_id, bt_bdaddr_t *bda,
1258 int attr_handle, int offset, bool is_long)
1260 BT_DBG("BTGATT SERVER REQUEST READ CB: conn_id:%d", conn_id);
1262 event_gatts_srvc_read_attr_t* event = g_new0(event_gatts_srvc_read_attr_t, 1);
1264 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1266 event->attr_trans.attr_handle = attr_handle;
1267 event->attr_trans.conn_id = conn_id;
1268 event->attr_trans.trans_id = trans_id;
1269 event->attr_trans.offset = offset;
1270 event->is_long = is_long;
1272 send_event(OAL_EVENT_GATTS_REQUEST_READ, event, sizeof(event_gatts_srvc_read_attr_t));
1275 static void cb_gatts_request_write(int conn_id, int trans_id,
1276 bt_bdaddr_t *bda, int attr_handle,
1277 int offset, int length,
1278 bool need_rsp, bool is_prep, uint8_t* value)
1280 BT_INFO("BTGATT SERVER REQUEST WRITE conn_id:%d", conn_id);
1282 event_gatts_srvc_write_attr_t* event = g_new0(event_gatts_srvc_write_attr_t, 1);
1284 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1286 event->attr_trans.attr_handle = attr_handle;
1287 event->attr_trans.conn_id = conn_id;
1288 event->attr_trans.trans_id = trans_id;
1289 event->attr_trans.offset = offset;
1290 event->need_rsp = need_rsp;
1291 event->is_prep = is_prep;
1293 if (length > 0 && value != NULL) {
1294 if (length > OAL_GATT_MAX_ATTR_LEN)
1295 length = OAL_GATT_MAX_ATTR_LEN;
1296 memcpy(event->value, value, length);
1297 event->length = length;
1299 send_event(OAL_EVENT_GATTS_REQUEST_WRITE, event, sizeof(event_gatts_srvc_write_attr_t));
1302 static void cb_gatts_acquire_write(int mtu, int conn_id, int trans_id, int attr_handle , bt_bdaddr_t *bda)
1305 BT_INFO("BTGATT SERVER REQUEST ACQUIRE WRITE conn_id:%d", conn_id);
1307 event_gatts_srvc_acquire_attr_t* event = g_new0(event_gatts_srvc_acquire_attr_t, 1);
1309 event->attr_trans.attr_handle = attr_handle;
1310 event->attr_trans.conn_id = conn_id;
1311 event->attr_trans.trans_id = trans_id;
1313 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1315 send_event(OAL_EVENT_GATTS_REQUEST_ACQUIRE_WRITE, event, sizeof(event_gatts_srvc_acquire_attr_t));
1318 static void cb_gatts_acquire_notify(int mtu, int conn_id, int trans_id, int attr_handle)
1320 BT_INFO("BTGATT SERVER REQUEST ACQUIRE NOTIFY conn_id:%d", conn_id);
1322 event_gatts_srvc_acquire_attr_t* event = g_new0(event_gatts_srvc_acquire_attr_t, 1);
1324 event->attr_trans.attr_handle = attr_handle;
1325 event->attr_trans.conn_id = conn_id;
1326 event->attr_trans.trans_id = trans_id;
1329 send_event(OAL_EVENT_GATTS_REQUEST_ACQUIRE_NOTIFY, event, sizeof(event_gatts_srvc_acquire_attr_t));
1333 static void cb_gatts_response_confirmation(int status, int handle)
1335 BT_INFO("BTGATT SERVER RESPONSE CONFIRMATION CB, status:%d, handle:%d", status, handle);
1337 event_gatts_respons_t* event = g_new0(event_gatts_respons_t, 1);
1339 event->hndl = handle;
1340 event->status = convert_to_oal_status(status);
1342 send_event(OAL_EVENT_GATTS_RESPONSE_CONFIRMED, event, sizeof(event_gatts_respons_t));
1345 static void cb_indication_confirmation(int conn_id, int trans_id, int attr_handle, bt_bdaddr_t *bda)
1347 BT_INFO("BTGATT SERVER Indication Confirmation CB, conn_id:%d, trans_id:%d attr_handle:%d",
1348 conn_id, trans_id, attr_handle);
1350 event_gatts_ind_cnfrm_t* event = g_new0(event_gatts_ind_cnfrm_t, 1);
1352 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1354 event->attr_handle = attr_handle;
1355 event->conn_id = conn_id;
1356 event->trans_id = trans_id;
1357 send_event(OAL_EVENT_GATTS_IND_CONFIRM, event, sizeof(event_gatts_ind_cnfrm_t));
1360 static void cb_gatts_mtu_changed(int conn_id, int mtu)
1362 BT_INFO("BTGATT SERVER MTU CHANGED CB, conn_id: %d, mtu_size: %d", conn_id, mtu);
1363 event_gatts_mtu_changed_t *event = g_new0(event_gatts_mtu_changed_t, 1);
1364 event->conn_id = conn_id;
1365 event->mtu_size = mtu;
1366 send_event(OAL_EVENT_GATTS_MTU_CHANGED, event, sizeof(event_gatts_mtu_changed_t));
1370 static void cb_notifcation_changed(int conn_id, int trans_id, int attr_handle, bool notify, bt_bdaddr_t *bda)
1372 BT_INFO("BTGATT SERVER Notication CB, conn_id:%d, trans_id:%d attr_handle:%d Notify:%d",
1373 conn_id, trans_id, attr_handle, notify);
1375 event_gatts_notif_t* event = g_new0(event_gatts_notif_t, 1);
1377 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1379 event->attr_handle = attr_handle;
1380 event->conn_id = conn_id;
1381 event->trans_id = trans_id;
1382 event->notify = notify;
1383 send_event(OAL_EVENT_GATTS_NOTIFICATION, event, sizeof(event_gatts_notif_t));
1388 /* GATT Server Callbacks: End */
1389 /************************************ GATT Client ***********************************/
1390 /* Client Callbacks */
1391 static void cb_gattc_register_app(int status, int clientIf, bt_uuid_t *app_uuid)
1393 char uuid_str[BT_UUID_STRING_MAX];
1394 event_gattc_register_t *event;
1396 /* Check if GATT client registered for Default GATT client UUID */
1397 uuid_to_string((service_uuid_t *)app_uuid, uuid_str);
1399 BT_INFO("GATT CLIENT REGISTER APP CB, status:%d, clientIf:%d, %s", status, clientIf, uuid_str);
1400 event = g_new0(event_gattc_register_t, 1);
1401 event->client_if = clientIf;
1402 event->status = (status == 0 ? OAL_STATUS_SUCCESS : OAL_STATUS_INTERNAL_ERROR);
1403 memcpy(event->client_uuid.uuid, app_uuid->uu, sizeof(oal_uuid_t));
1404 send_event(OAL_EVENT_GATTC_REGISTRATION, event, sizeof(event_gattc_register_t));
1407 static void cb_gattc_scan_result(bt_bdaddr_t* bdaddress, uint8_t addr_type, int rssi,
1408 uint8_t *adv_data, int adv_data_len, uint8_t *scan_rsp_data, int scan_rsp_data_len)
1410 event_ble_scan_result_info *event;
1412 event = g_new0(event_ble_scan_result_info, 1);
1413 event->addr_type = addr_type;
1415 memcpy(event->address.addr, bdaddress->address, BT_ADDRESS_BYTES_NUM);
1416 memcpy(event->adv_data, adv_data, BLE_ADV_DATA_LENGTH);
1417 event->adv_data_len = adv_data_len;
1418 memcpy(event->scan_rsp_data, scan_rsp_data, BLE_ADV_DATA_LENGTH);
1419 event->scan_rsp_data_len = scan_rsp_data_len;
1420 send_event_bda_trace(OAL_EVENT_BLE_REMOTE_DEVICE_FOUND, event,
1421 sizeof(event_ble_scan_result_info), (bt_address_t *)bdaddress);
1424 static void cb_gattc_connection(int conn_id, int status, int client_if, bt_bdaddr_t* bda)
1426 event_gattc_conn_t *event;
1427 BT_INFO("BTGATT CLIENT CONNECTED CB, status:[%d] client_if[%d] id[%d]",
1428 status, client_if, conn_id);
1430 event = g_new0(event_gattc_conn_t, 1);
1431 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1432 event->client_if = client_if;
1434 if (BT_STATUS_SUCCESS != status) {
1435 event->status = OAL_STATUS_INTERNAL_ERROR;
1436 BT_ERR("gattc connection Error: %d", status);
1438 event->conn_id = conn_id;
1439 event->status = OAL_STATUS_SUCCESS;
1442 send_event_bda_trace(OAL_EVENT_GATTC_CONNECTION_COMPLETED, event,
1443 sizeof(*event), (bt_address_t *)bda);
1447 static void cb_gattc_disconnect(int conn_id, int status, int client_if, bt_bdaddr_t* bda)
1449 event_gattc_conn_t *event;
1450 BT_INFO("BTGATT Client Disconnect Complete cb, status:[%d], client_if[%d] id[%d]",
1451 status, client_if, conn_id);
1452 event = g_new0(event_gattc_conn_t, 1);
1453 event->status = convert_to_oal_status(status);
1454 event->client_if = client_if;
1455 event->conn_id = conn_id;
1456 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1457 send_event_bda_trace(OAL_EVENT_GATTC_DISCONNECTION_COMPLETED, event,
1458 sizeof(*event), (bt_address_t *)bda);
1461 static void cb_gattc_search_result(int conn_id, btgatt_srvc_id_t *srvc_id)
1463 char uuid_str[2*BT_UUID_STRING_MAX];
1465 uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str);
1466 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]",
1467 uuid_str, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1469 event_gattc_service_result_t *event = g_new0(event_gattc_service_result_t, 1);
1470 event->conn_status.status = OAL_STATUS_SUCCESS;
1471 event->conn_status.conn_id = conn_id;
1472 memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1474 send_event(OAL_EVENT_GATTC_SERVICE_SEARCH_RESULT, event, sizeof(*event));
1477 static void cb_gattc_search_complete(int conn_id, int status)
1479 BT_INFO("BTGATT Client Service Search Complete cb, status:%d, conn_id:%d",
1482 event_gattc_conn_status_t *event = g_new0(event_gattc_conn_status_t, 1);
1483 event->conn_id = conn_id;
1484 event->status = convert_to_oal_status(status);
1486 send_event(OAL_EVENT_GATTC_SERVICE_SEARCH_DONE, event, sizeof(*event));
1490 static void cb_gattc_get_characteristics(int conn_id, int status, btgatt_srvc_id_t *srvc_id,
1491 btgatt_gatt_id_t *char_id, int char_prop)
1493 char uuid_str1[2*BT_UUID_STRING_MAX];
1494 char uuid_str2[2*BT_UUID_STRING_MAX];
1497 BT_DBG("BTGATT Client Get Characteristic Callback, conn_id:%d, status:%d", conn_id, status);
1498 uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str1);
1499 BT_DBG("Service=> [%s], Inst_id [%u], Type [%s]",
1500 uuid_str1, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1502 event_gattc_characteristic_result_t *event = g_new0(event_gattc_characteristic_result_t, 1);
1503 event->conn_status.conn_id = conn_id;
1504 event->conn_status.status = convert_to_oal_status(status);
1505 memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1508 uuid_to_stringname((oal_uuid_t *)&(char_id->uuid), uuid_str2);
1509 BT_INFO("Charac => [%s], Inst_id [%u], Prop [%s]",
1510 uuid_str2, char_id->inst_id, char_prop_to_string(char_prop, str));
1511 event->char_prop = char_prop;
1512 memcpy(&(event->char_id), char_id, sizeof(oal_gatt_id_t));
1514 send_event(OAL_EVENT_GATTC_CHARAC_SERACH_RESULT, event, sizeof(*event));
1519 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)
1521 char uuid_str1[2*BT_UUID_STRING_MAX];
1522 char uuid_str2[2*BT_UUID_STRING_MAX];
1523 char uuid_str3[2*BT_UUID_STRING_MAX];
1524 BT_DBG("BTGATT Client Get Descriptor Callback, conn_id:%d, status:%d", conn_id, status);
1525 uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str1);
1526 uuid_to_stringname((oal_uuid_t *)&(char_id->uuid), uuid_str2);
1527 BT_DBG("Service=> [%s], Inst_id [%u], Type [%s]",
1528 uuid_str1, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1529 BT_DBG("Charac => [%s], Inst_id [%u]", uuid_str2, char_id->inst_id);
1531 event_gattc_descriptor_result_t *event = g_new0(event_gattc_descriptor_result_t, 1);
1532 event->conn_status.conn_id = conn_id;
1533 event->conn_status.status = convert_to_oal_status(status);
1534 memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1535 memcpy(&(event->char_id), char_id, sizeof(oal_gatt_id_t));
1538 uuid_to_stringname((oal_uuid_t *)&(descr_id->uuid), uuid_str3);
1539 BT_INFO("Desc => [%s], Inst_id [%u]", uuid_str3, descr_id->inst_id);
1540 memcpy(&(event->descr_id), descr_id, sizeof(oal_gatt_id_t));
1542 send_event(OAL_EVENT_GATTC_DESC_SERACH_RESULT, event, sizeof(*event));
1545 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)
1547 char uuid_str1[2*BT_UUID_STRING_MAX];
1548 char uuid_str2[2*BT_UUID_STRING_MAX];
1549 BT_INFO("BTGATT Client Register For Notification Callback, conn_id:%d, status:%d, registered: %s",
1550 conn_id, status, registered == 1 ? "REGISTERED" : "DEREGISTERED");
1551 uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str1);
1552 uuid_to_stringname((oal_uuid_t *)&(char_id->uuid), uuid_str2);
1553 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1554 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, char_id->inst_id);
1556 event_gattc_regdereg_notify_t *event = g_new0(event_gattc_regdereg_notify_t, 1);
1557 oal_event_t event_type = (registered == 1 ? OAL_EVENT_GATTC_NOTIFICATION_REGISTERED : OAL_EVENT_GATTC_NOTIFICATION_DEREGISTERED);
1558 event->conn_id = conn_id;
1559 event->status = convert_to_oal_status(status);
1560 memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1561 memcpy(&(event->char_id), char_id, sizeof(oal_gatt_id_t));
1563 send_event(event_type, event, sizeof(*event));
1566 static void cb_gattc_notify(int conn_id, btgatt_notify_params_t *p_data)
1569 char uuid_str1[2*BT_UUID_STRING_MAX];
1570 char uuid_str2[2*BT_UUID_STRING_MAX];
1572 BT_INFO("BTGATT Client Notify Callback, conn_id:%d", conn_id);
1573 BT_INFO("Server Address:[%s], is_notify:[%d], len:[%d]",
1574 bdt_bd2str((bt_address_t *)&(p_data->bda), &bdstr), p_data->is_notify, p_data->len);
1576 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1577 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1579 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]",
1580 uuid_str1, p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1581 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1583 if (p_data->len > 0) {
1585 data = g_malloc(3*p_data->len+1);
1587 BT_ERR("memory allocation failed");
1591 convert_hex_2_str((unsigned char *)p_data->value, p_data->len, data);
1592 BT_INFO("Notified Data: [%s]", data);
1594 event_gattc_notify_data *event = g_new0(event_gattc_notify_data, 1);
1595 memcpy(event->address.addr, p_data->bda.address, BT_ADDRESS_BYTES_NUM);
1596 event->is_notify = p_data->is_notify;
1597 event->data_len = p_data->len;
1598 memcpy(event->data, p_data->value, event->data_len);
1599 memcpy(&(event->char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1600 memcpy(&(event->srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1602 send_event_bda_trace(OAL_EVENT_GATTC_NOTIFY_DATA, event, sizeof(*event), (bt_address_t *)&p_data->bda);
1607 static void cb_gattc_service_changed(bt_bdaddr_t *bd_addr, int change_type, uint8_t *uuid, int conn_id, int inst_id)
1609 event_gattc_service_changed_data *event = g_new0(event_gattc_service_changed_data, 1);
1611 memcpy(event->address.addr, bd_addr->address, 6);
1612 event->change_type = change_type;
1613 memcpy(event->uuid.uuid, uuid, sizeof(event->uuid.uuid));
1614 event->conn_id = conn_id;
1615 event->inst_id = inst_id;
1617 send_event_bda_trace(OAL_EVENT_GATTC_SERVICE_CHANGED_IND, event, sizeof(*event), (bt_address_t *)bd_addr);
1620 static void cb_gattc_read_characteristic(int conn_id, int status, btgatt_read_params_t *p_data)
1622 char uuid_str1[2*BT_UUID_STRING_MAX];
1623 char uuid_str2[2*BT_UUID_STRING_MAX];
1624 BT_DBG("BTGATT Client Read Charcateristic Callback, conn_id:%d, read_status:%d", conn_id, status);
1626 event_gattc_read_data *event = g_new0(event_gattc_read_data, 1);
1627 event->uuid_status.conn_status.conn_id = conn_id;
1628 event->uuid_status.conn_status.status = convert_to_oal_status(status);
1629 event->value_type = p_data->value_type;
1630 memcpy(&(event->uuid_status.srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1631 memcpy(&(event->uuid_status.char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1633 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1634 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1635 BT_DBG("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1,
1636 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1637 BT_DBG("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1638 BT_DBG("Len: %u, value_type: %u", p_data->value.len, p_data->value_type);
1639 if (p_data->value.len > 0 && event->value_type == GATTC_READ_VALUE_TYPE_VALUE) {
1640 event->data_len = p_data->value.len;
1641 memcpy(&(event->data), &(p_data->value.value), event->data_len);
1644 send_event(OAL_EVENT_GATTC_READ_CHARAC, event, sizeof(*event));
1647 static void cb_gattc_write_characteristic(int conn_id, int status, btgatt_write_params_t *p_data)
1649 char uuid_str1[2*BT_UUID_STRING_MAX];
1650 char uuid_str2[2*BT_UUID_STRING_MAX];
1651 BT_INFO("BTGATT Client Write Charcateristic Callback, conn_id:%d, write_status:%d", conn_id, status);
1653 event_gattc_write_data *event = g_new0(event_gattc_write_data, 1);
1654 event->conn_status.conn_id = conn_id;
1655 event->conn_status.status = convert_to_oal_status(status);
1656 memcpy(&(event->srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1657 memcpy(&(event->char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1659 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1660 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1661 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1,
1662 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1663 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1665 send_event(OAL_EVENT_GATTC_WRITE_CHARAC, event, sizeof(*event));
1668 static void cb_gattc_write_descriptor(int conn_id, int status, btgatt_write_params_t *p_data)
1670 char uuid_str1[2*BT_UUID_STRING_MAX];
1671 char uuid_str2[2*BT_UUID_STRING_MAX];
1672 char uuid_str3[2*BT_UUID_STRING_MAX];
1673 BT_DBG("BTGATT Client Write Descriptor Callback, conn_id:%d, write_status:%d", conn_id, status);
1675 event_gattc_write_data *event = g_new0(event_gattc_write_data, 1);
1676 event->conn_status.conn_id = conn_id;
1677 event->conn_status.status = convert_to_oal_status(status);
1678 memcpy(&(event->srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1679 memcpy(&(event->char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1680 memcpy(&(event->descr_id), &(p_data->descr_id), sizeof(oal_gatt_id_t));
1682 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1683 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1684 uuid_to_stringname((oal_uuid_t *)&(p_data->descr_id.uuid), uuid_str3);
1685 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1,
1686 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1687 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1688 BT_INFO("Desc => [%s], Inst_id [%u]", uuid_str3, p_data->descr_id.inst_id);
1690 send_event(OAL_EVENT_GATTC_WRITE_DESCR, event, sizeof(*event));
1695 static void cb_gattc_read_descriptor(int conn_id, int status, btgatt_read_params_t *p_data)
1697 char uuid_str1[2*BT_UUID_STRING_MAX];
1698 char uuid_str2[2*BT_UUID_STRING_MAX];
1699 char uuid_str3[2*BT_UUID_STRING_MAX];
1700 BT_INFO("BTGATT Client Read Descriptor Callback, conn_id:%d, read_status:%d", conn_id, status);
1702 event_gattc_read_data *event = g_new0(event_gattc_read_data, 1);
1703 event->uuid_status.conn_status.conn_id = conn_id;
1704 event->uuid_status.conn_status.status = convert_to_oal_status(status);
1705 event->value_type = p_data->value_type;
1706 memcpy(&(event->uuid_status.srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1707 memcpy(&(event->uuid_status.char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1708 memcpy(&(event->uuid_status.descr_id), &(p_data->descr_id), sizeof(oal_gatt_id_t));
1711 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1712 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1713 uuid_to_stringname((oal_uuid_t *)&(p_data->descr_id.uuid), uuid_str3);
1714 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1,
1715 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1716 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1717 BT_INFO("Desc => [%s], Inst_id [%u]", uuid_str3, p_data->descr_id.inst_id);
1718 BT_DBG("Len: %u, value_type: %u", p_data->value.len, p_data->value_type);
1719 if (p_data->value.len > 0 && event->value_type == GATTC_READ_VALUE_TYPE_VALUE) {
1721 data = g_malloc(3*p_data->value.len+1);
1723 BT_ERR("memory allocation failed");
1727 convert_hex_2_str((unsigned char *)p_data->value.value, p_data->value.len, data);
1728 BT_DBG("Read Data: [%s]", data);
1729 event->data_len = p_data->value.len;
1730 memcpy(&(event->data), &(p_data->value.value), event->data_len);
1734 send_event(OAL_EVENT_GATTC_READ_DESCR, event, sizeof(*event));
1737 oal_status_t gattc_start_le_discovery(int client_id)
1740 int ret = OAL_STATUS_SUCCESS;
1742 API_TRACE("BTGATT CLIENT SCAN START");
1743 CHECK_OAL_GATT_ENABLED();
1744 CHECK_CLIENT_REGISTRATION(client_id);
1746 ret = gatt_api->client->scan(client_id, 1);
1747 if (ret != BT_STATUS_SUCCESS) {
1748 BT_ERR("Error:Start LE Discovery failed: %s", status2string(ret));
1749 return convert_to_oal_status(ret);
1751 return OAL_STATUS_SUCCESS;
1754 oal_status_t gattc_stop_le_discovery(int client_id)
1757 int ret = OAL_STATUS_SUCCESS;
1759 API_TRACE("Scan is stopped");
1760 CHECK_OAL_GATT_ENABLED();
1761 CHECK_CLIENT_REGISTRATION(client_id);
1762 ret = gatt_api->client->scan(client_id, 0);
1763 if (ret != BT_STATUS_SUCCESS) {
1764 BT_ERR("Error:Stop LE Discovery failed: %s", status2string(ret));
1765 return convert_to_oal_status(ret);
1767 return OAL_STATUS_SUCCESS;
1770 oal_status_t gattc_set_le_scan_param(int scan_type, int itv, int win)
1774 API_TRACE("Scan is stopped");
1775 CHECK_OAL_GATT_ENABLED();
1778 ret = gatt_api->client->set_scan_parameters(scan_type, itv, win);
1779 if (ret != BT_STATUS_SUCCESS) {
1780 BT_ERR("Error:Set scan parameters failed: %s", status2string(ret));
1781 return convert_to_oal_status(ret);
1784 ret = gatt_api->client->set_scan_parameters(itv, win);
1785 if (ret != BT_STATUS_SUCCESS) {
1786 BT_ERR("Error:Set scan parameters failed: %s", status2string(ret));
1787 return convert_to_oal_status(ret);
1791 return OAL_STATUS_SUCCESS;
1794 static void cb_gattc_configure_mtu_cmpl(int conn_id, int status, int mtu)
1796 event_gattc_mtu_configured_t *event;
1797 BT_INFO("BTGATT Client configure mtu complete Callback, conn_id: %d, status: %d, mtu: %d",
1798 conn_id, status, mtu);
1799 event = g_new0(event_gattc_mtu_configured_t, 1);
1800 event->conn_id = conn_id;
1802 event->status = convert_to_oal_status(status);
1803 send_event(OAL_EVENT_GATTC_MTU_EXCHANGE_COMPLETED, event, sizeof(*event));
1806 /************************************ GATT Client ***********************************/
1807 oal_status_t gattc_register(oal_uuid_t* client_uuid)
1810 char str[2*BT_UUID_STRING_MAX];
1811 int ret = OAL_STATUS_SUCCESS;
1813 CHECK_OAL_GATT_ENABLED();
1814 uuid_to_stringname(client_uuid, str);
1815 API_TRACE("uuid: [%s]", str);
1816 /* We will perform actual registration in cb_gattc_register_app callback */
1817 ret = gatt_api->client->register_client((bt_uuid_t *)client_uuid);
1818 if (ret != BT_STATUS_SUCCESS) {
1819 BT_ERR("GATT client register failed: %s", status2string(ret));
1820 return convert_to_oal_status(ret);
1822 return OAL_STATUS_SUCCESS;
1825 oal_status_t gattc_deregister(int client_id)
1827 int ret = OAL_STATUS_SUCCESS;
1829 API_TRACE("GATT client deregister");
1830 CHECK_OAL_GATT_ENABLED();
1831 CHECK_CLIENT_REGISTRATION(client_id);
1833 ret = gatt_api->client->unregister_client(client_id);
1834 if (ret != BT_STATUS_SUCCESS) {
1835 BT_ERR("GATT client deregister failed: %s", status2string(ret));
1836 return convert_to_oal_status(ret);
1839 return OAL_STATUS_SUCCESS;
1842 oal_status_t gattc_connect(int client_id, bt_address_t *device_address, int isDirect)
1844 int ret = OAL_STATUS_SUCCESS;
1847 OAL_CHECK_PARAMETER(device_address, return);
1848 API_TRACE("Client Connect: [%s]", bdt_bd2str(device_address, &bdstr) + 12);
1849 CHECK_OAL_GATT_ENABLED();
1850 CHECK_CLIENT_REGISTRATION(client_id);
1852 /* Handle the actual connection in cb_gattc_connection callback */
1853 ret = gatt_api->client->connect(client_id, (bt_bdaddr_t *)device_address, isDirect);
1854 if (ret != BT_STATUS_SUCCESS) {
1855 BT_ERR("GATT client connect failed: %s", status2string(ret));
1856 return convert_to_oal_status(ret);
1858 return OAL_STATUS_SUCCESS;
1861 oal_status_t gattc_disconnect(int client_id, bt_address_t *device_address, int conn_id)
1864 int ret = OAL_STATUS_SUCCESS;
1867 OAL_CHECK_PARAMETER(device_address, return);
1868 API_TRACE("Client Disconnect: [%s]", bdt_bd2str(device_address, &bdstr) + 12);
1869 CHECK_OAL_GATT_ENABLED();
1870 CHECK_CLIENT_REGISTRATION(client_id);
1871 CHECK_CLIENT_CONNECTION(conn_id);
1873 /* Handle actual disconnection in callback */
1874 ret = gatt_api->client->disconnect(client_id, (bt_bdaddr_t *) device_address, conn_id);
1875 if (ret != BT_STATUS_SUCCESS) {
1876 BT_ERR("GATT client disconnect failed: %s", status2string(ret));
1877 return convert_to_oal_status(ret);
1879 return OAL_STATUS_SUCCESS;
1883 oal_status_t gattc_search_service(int conn_id, oal_uuid_t *service_uuid)
1885 int ret = OAL_STATUS_SUCCESS;
1886 char uuid_str[2*BT_UUID_STRING_MAX];
1889 uuid_to_stringname(service_uuid, uuid_str);
1890 API_TRACE("Client Service Search UUID: [%s]", uuid_str);
1892 API_TRACE("Client Service Search All");
1894 CHECK_OAL_GATT_ENABLED();
1895 CHECK_CLIENT_CONNECTION(conn_id);
1896 ret = gatt_api->client->search_service(conn_id, (bt_uuid_t *)service_uuid);
1898 if (ret != BT_STATUS_SUCCESS) {
1899 BT_ERR("GATT client service search failed: %s", status2string(ret));
1900 return convert_to_oal_status(ret);
1902 return OAL_STATUS_SUCCESS;
1905 oal_status_t gattc_get_characteristic(int conn_id, oal_gatt_srvc_id_t *srvc_id, oal_gatt_id_t *char_id)
1907 int ret = OAL_STATUS_SUCCESS;
1908 char uuid_str[2*BT_UUID_STRING_MAX];
1910 OAL_CHECK_PARAMETER(srvc_id, return);
1911 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str);
1912 API_TRACE("Client Get Characteristic [%s]", uuid_str);
1913 CHECK_OAL_GATT_ENABLED();
1914 CHECK_CLIENT_CONNECTION(conn_id);
1915 ret = gatt_api->client->get_characteristic(conn_id, (btgatt_srvc_id_t *)srvc_id,
1916 (btgatt_gatt_id_t *)char_id);
1917 if (ret != BT_STATUS_SUCCESS) {
1918 BT_ERR("GATT client get characteristic failed: %s", status2string(ret));
1919 return convert_to_oal_status(ret);
1921 return OAL_STATUS_SUCCESS;
1924 oal_status_t gattc_get_descriptor(int conn_id, oal_gatt_srvc_id_t *srvc_id,
1925 oal_gatt_id_t *char_id, oal_gatt_id_t *desc_id)
1927 int ret = OAL_STATUS_SUCCESS;
1928 char uuid_str1[2*BT_UUID_STRING_MAX];
1929 char uuid_str2[2*BT_UUID_STRING_MAX];
1931 OAL_CHECK_PARAMETER(srvc_id, return);
1932 OAL_CHECK_PARAMETER(char_id, return);
1933 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1934 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1935 API_TRACE("Client Get Descriptor, Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
1936 CHECK_OAL_GATT_ENABLED();
1937 CHECK_CLIENT_CONNECTION(conn_id);
1938 ret = gatt_api->client->get_descriptor(conn_id, (btgatt_srvc_id_t *)srvc_id,
1939 (btgatt_gatt_id_t *)char_id, (btgatt_gatt_id_t *)desc_id);
1940 if (ret != BT_STATUS_SUCCESS) {
1941 BT_ERR("GATT client get descriptor failed: %s", status2string(ret));
1942 return convert_to_oal_status(ret);
1944 return OAL_STATUS_SUCCESS;
1947 oal_status_t gattc_register_for_notification(int conn_id, bt_address_t * address,
1948 oal_gatt_srvc_id_t *srvc_id, oal_gatt_id_t *char_id) {
1949 int ret = OAL_STATUS_SUCCESS;
1950 char uuid_str1[2*BT_UUID_STRING_MAX];
1951 char uuid_str2[2*BT_UUID_STRING_MAX];
1954 OAL_CHECK_PARAMETER(address, return);
1955 OAL_CHECK_PARAMETER(srvc_id, return);
1956 OAL_CHECK_PARAMETER(char_id, return);
1957 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1958 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1959 API_TRACE("Client Register Notification: [%s], Service_uuid: [%s], Char_uuid: [%s]", bdt_bd2str(address, &bdstr), uuid_str1, uuid_str2);
1960 CHECK_OAL_GATT_ENABLED();
1961 CHECK_CLIENT_CONNECTION(conn_id);
1963 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);
1964 if (ret != BT_STATUS_SUCCESS) {
1965 BT_ERR("GATT client register notification failed: %s", status2string(ret));
1966 return convert_to_oal_status(ret);
1969 return OAL_STATUS_SUCCESS;
1972 oal_status_t gattc_deregister_for_notification(int conn_id, bt_address_t * address,
1973 oal_gatt_srvc_id_t *srvc_id, oal_gatt_id_t *char_id) {
1974 int ret = OAL_STATUS_SUCCESS;
1975 char uuid_str1[2*BT_UUID_STRING_MAX];
1976 char uuid_str2[2*BT_UUID_STRING_MAX];
1979 OAL_CHECK_PARAMETER(address, return);
1980 OAL_CHECK_PARAMETER(srvc_id, return);
1981 OAL_CHECK_PARAMETER(char_id, return);
1982 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1983 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1984 API_TRACE("Client Deregister Notification: [%s], Service_uuid: [%s], Char_uuid: [%s]", bdt_bd2str(address, &bdstr), uuid_str1, uuid_str2);
1985 CHECK_OAL_GATT_ENABLED();
1986 CHECK_CLIENT_CONNECTION(conn_id);
1988 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);
1989 if (ret != BT_STATUS_SUCCESS) {
1990 BT_ERR("GATT client deregister notification failed: %s", status2string(ret));
1991 return convert_to_oal_status(ret);
1994 return OAL_STATUS_SUCCESS;
1998 oal_status_t gattc_read_characteristic(int conn_id, oal_gatt_srvc_id_t *srvc_id,
1999 oal_gatt_id_t *char_id, oal_gatt_auth_req_t auth_req)
2001 int ret = OAL_STATUS_SUCCESS;
2002 char uuid_str1[2*BT_UUID_STRING_MAX];
2003 char uuid_str2[2*BT_UUID_STRING_MAX];
2005 OAL_CHECK_PARAMETER(srvc_id, return);
2006 OAL_CHECK_PARAMETER(char_id, return);
2007 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2008 uuid_to_stringname(&(char_id->uuid), uuid_str2);
2009 API_TRACE("Client Read Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
2010 CHECK_OAL_GATT_ENABLED();
2011 CHECK_CLIENT_CONNECTION(conn_id);
2013 ret = gatt_api->client->read_characteristic(conn_id, (btgatt_srvc_id_t *)srvc_id,
2014 (btgatt_gatt_id_t *)char_id, auth_req);
2015 if (ret != BT_STATUS_SUCCESS) {
2016 BT_ERR("GATT client read characteristic failed: %s", status2string(ret));
2017 return convert_to_oal_status(ret);
2020 return OAL_STATUS_SUCCESS;
2023 oal_status_t gattc_read_descriptor(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2024 oal_gatt_id_t *char_id, oal_gatt_id_t *desc_id, oal_gatt_auth_req_t auth_req)
2026 int ret = OAL_STATUS_SUCCESS;
2027 char uuid_str1[2*BT_UUID_STRING_MAX];
2028 char uuid_str2[2*BT_UUID_STRING_MAX];
2029 char uuid_str3[2*BT_UUID_STRING_MAX];
2031 OAL_CHECK_PARAMETER(srvc_id, return);
2032 OAL_CHECK_PARAMETER(char_id, return);
2033 OAL_CHECK_PARAMETER(desc_id, return);
2034 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2035 uuid_to_stringname(&(char_id->uuid), uuid_str2);
2036 uuid_to_stringname(&(desc_id->uuid), uuid_str3);
2037 API_TRACE("Client Read Descriptor: Service_uuid: [%s], Char_uuid: [%s], Desc_uuid: [%s]",
2038 uuid_str1, uuid_str2, uuid_str3);
2039 CHECK_OAL_GATT_ENABLED();
2040 CHECK_CLIENT_CONNECTION(conn_id);
2042 ret = gatt_api->client->read_descriptor(conn_id, (btgatt_srvc_id_t *)srvc_id,
2043 (btgatt_gatt_id_t *)char_id, (btgatt_gatt_id_t *)desc_id, auth_req);
2044 if (ret != BT_STATUS_SUCCESS) {
2045 BT_ERR("GATT client read descriptor failed: %s", status2string(ret));
2046 return convert_to_oal_status(ret);
2049 return OAL_STATUS_SUCCESS;
2052 oal_status_t gattc_acquire_notify(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2053 oal_gatt_id_t *char_id, int *fd, int *mtu)
2055 int ret = OAL_STATUS_SUCCESS;
2056 char uuid_str1[2*BT_UUID_STRING_MAX];
2057 char uuid_str2[2*BT_UUID_STRING_MAX];
2058 OAL_CHECK_PARAMETER(srvc_id, return);
2059 OAL_CHECK_PARAMETER(char_id, return);
2060 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2061 uuid_to_stringname(&(char_id->uuid), uuid_str2);
2062 API_TRACE("Client Write Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
2063 CHECK_OAL_GATT_ENABLED();
2064 CHECK_CLIENT_CONNECTION(conn_id);
2066 ret = gatt_api->client->acquire_notify(conn_id, (btgatt_srvc_id_t *)srvc_id,
2067 (btgatt_gatt_id_t *)char_id, fd, mtu);
2068 if (ret != BT_STATUS_SUCCESS) {
2069 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
2070 return convert_to_oal_status(ret);
2073 return OAL_STATUS_SUCCESS;
2076 oal_status_t gattc_acquire_write(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2077 oal_gatt_id_t *char_id, oal_gatt_auth_req_t auth_req , int *fd, int*mtu)
2079 int ret = OAL_STATUS_SUCCESS;
2080 char uuid_str1[2*BT_UUID_STRING_MAX];
2081 char uuid_str2[2*BT_UUID_STRING_MAX];
2082 OAL_CHECK_PARAMETER(srvc_id, return);
2083 OAL_CHECK_PARAMETER(char_id, return);
2084 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2085 uuid_to_stringname(&(char_id->uuid), uuid_str2);
2086 API_TRACE("Client Write Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
2087 CHECK_OAL_GATT_ENABLED();
2088 CHECK_CLIENT_CONNECTION(conn_id);
2090 ret = gatt_api->client->acquire_write(conn_id, (btgatt_srvc_id_t *)srvc_id,
2091 (btgatt_gatt_id_t *)char_id, auth_req, fd, mtu);
2092 if (ret != BT_STATUS_SUCCESS) {
2093 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
2094 return convert_to_oal_status(ret);
2097 return OAL_STATUS_SUCCESS;
2100 oal_status_t gattc_write_characteristic(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2101 oal_gatt_id_t *char_id, oal_gatt_write_type_t write_type,
2102 int len, oal_gatt_auth_req_t auth_req, char* data)
2104 int ret = OAL_STATUS_SUCCESS;
2105 char uuid_str1[2*BT_UUID_STRING_MAX];
2106 char uuid_str2[2*BT_UUID_STRING_MAX];
2107 OAL_CHECK_PARAMETER(srvc_id, return);
2108 OAL_CHECK_PARAMETER(char_id, return);
2109 OAL_CHECK_PARAMETER(data, return);
2110 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2111 uuid_to_stringname(&(char_id->uuid), uuid_str2);
2112 API_TRACE("Client Write Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
2113 CHECK_OAL_GATT_ENABLED();
2114 CHECK_CLIENT_CONNECTION(conn_id);
2116 ret = gatt_api->client->write_characteristic(conn_id, (btgatt_srvc_id_t *)srvc_id,
2117 (btgatt_gatt_id_t *)char_id, write_type, len, auth_req, data);
2118 if (ret != BT_STATUS_SUCCESS) {
2119 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
2120 return convert_to_oal_status(ret);
2123 return OAL_STATUS_SUCCESS;
2126 oal_status_t gattc_write_descriptor(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2127 oal_gatt_id_t *char_id, oal_gatt_id_t *desc_id,
2128 oal_gatt_write_type_t write_type, int len, oal_gatt_auth_req_t auth_req, char* data)
2130 int ret = OAL_STATUS_SUCCESS;
2131 char uuid_str1[2*BT_UUID_STRING_MAX];
2132 char uuid_str2[2*BT_UUID_STRING_MAX];
2133 char uuid_str3[2*BT_UUID_STRING_MAX];
2134 OAL_CHECK_PARAMETER(srvc_id, return);
2135 OAL_CHECK_PARAMETER(char_id, return);
2136 OAL_CHECK_PARAMETER(desc_id, return);
2137 OAL_CHECK_PARAMETER(data, return);
2138 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2139 uuid_to_stringname(&(char_id->uuid), uuid_str2);
2140 uuid_to_stringname(&(desc_id->uuid), uuid_str3);
2141 API_TRACE("Client Write Descriptor: Service_uuid: [%s], Char_uuid: [%s], Desc_uuid: [%s]",
2142 uuid_str1, uuid_str2, uuid_str3);
2143 CHECK_OAL_GATT_ENABLED();
2144 CHECK_CLIENT_CONNECTION(conn_id);
2146 ret = gatt_api->client->write_descriptor(conn_id, (btgatt_srvc_id_t *)srvc_id,
2147 (btgatt_gatt_id_t *)char_id, (btgatt_gatt_id_t *)desc_id, write_type, len, auth_req, data);
2148 if (ret != BT_STATUS_SUCCESS) {
2149 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
2150 return convert_to_oal_status(ret);
2153 return OAL_STATUS_SUCCESS;
2156 oal_status_t gattc_conn_param_update(bt_address_t * address, int min, int max, int latency, int timeout)
2161 OAL_CHECK_PARAMETER(address, return);
2162 CHECK_OAL_GATT_ENABLED();
2164 BT_DBG("[%s] min[%d] max[%d] latency[%d] timeout[%d]", bdt_bd2str(address, &bdstr), min, max, latency, timeout);
2165 ret = gatt_api->client->conn_parameter_update((bt_bdaddr_t *)address, min, max, latency, timeout);
2166 if (ret != BT_STATUS_SUCCESS) {
2167 BT_ERR("error: %s", status2string(ret));
2168 return convert_to_oal_status(ret);
2170 return OAL_STATUS_SUCCESS;
2173 oal_status_t gattc_unregister_scan_filter(int slot_id)
2179 CHECK_OAL_GATT_ENABLED();
2181 BT_INFO("Remove Scan filter Slot ID is: [%d]", slot_id);
2183 ret = gatt_api->client->scan_filter_clear(client_if, slot_id);
2184 if (ret != BT_STATUS_SUCCESS) {
2185 BT_ERR("error: %s", status2string(ret));
2186 return convert_to_oal_status(ret);
2188 return OAL_STATUS_SUCCESS;
2191 oal_status_t gattc_enable_scan_filter(int client_if)
2196 CHECK_OAL_GATT_ENABLED();
2198 BT_INFO("Enable Scan filter. Client If: %d", client_if);
2200 ret = gatt_api->client->scan_filter_enable(client_if, TRUE);
2201 if (ret != BT_STATUS_SUCCESS) {
2202 BT_ERR("error: %s", status2string(ret));
2203 return convert_to_oal_status(ret);
2205 return OAL_STATUS_SUCCESS;
2208 oal_status_t gattc_disable_scan_filter(int client_if)
2213 CHECK_OAL_GATT_ENABLED();
2215 BT_INFO("Disable Scan filter. Client If: %d", client_if);
2217 ret = gatt_api->client->scan_filter_enable(client_if, FALSE);
2218 if (ret != BT_STATUS_SUCCESS) {
2219 BT_ERR("error: %s", status2string(ret));
2220 return convert_to_oal_status(ret);
2222 return OAL_STATUS_SUCCESS;
2225 oal_status_t gattc_register_scan_filter(oal_ble_scan_filter_t* filter_data)
2228 int client_info = 0;
2231 int company_id_mask = 0;
2232 int address_type = 0;
2233 int feature_selection = 0;
2235 oal_ble_scan_filter_param_setup_t scan_filter_setup = {.list_logic_type = 0,
2236 .filt_logic_type = 1,
2237 .rssi_high_thres = -127,
2238 .rssi_low_thres = -127,
2242 .found_timeout_cnt = 0
2245 OAL_CHECK_PARAMETER(filter_data, return);
2247 CHECK_OAL_GATT_ENABLED();
2249 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS){
2251 BT_INFO("OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS is being added");
2252 BT_INFO("BT remote Device address is %s", bdt_bd2str(filter_data->device_address, &bdstr));
2253 ret = gatt_api->client->scan_filter_add_remove(client_info,
2255 OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS,
2256 filter_data->slot_id,
2261 (bt_bdaddr_t*)filter_data->device_address,
2268 if (ret != BT_STATUS_SUCCESS){
2269 BT_ERR("error: %s", status2string(ret));
2270 BT_INFO("unregistering already set filter features.");
2271 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2272 return convert_to_oal_status(ret);
2275 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS;
2277 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_NAME){
2278 ret = gatt_api->client->scan_filter_add_remove(client_info,
2280 OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_NAME,
2281 filter_data->slot_id,
2289 filter_data->device_name, // device_name as p_data in HAL
2293 if (ret != BT_STATUS_SUCCESS){
2294 BT_ERR("error: %s", status2string(ret));
2295 BT_INFO("unregistering already set filter features.");
2296 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2297 return convert_to_oal_status(ret);
2300 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_NAME;
2302 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_UUID){
2303 BT_INFO("OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_UUID is being added");
2304 char uuid_str1[2*BT_UUID_STRING_MAX];
2305 char uuid_str2[2*BT_UUID_STRING_MAX];
2307 uuid_to_stringname((service_uuid_t*)filter_data->service_uuid, uuid_str1);
2308 uuid_to_stringname((service_uuid_t*)filter_data->service_uuid_mask, uuid_str2);
2310 BT_INFO("Service UUID is [%s] and Service UUID mask is [%s]", uuid_str1, uuid_str2);
2311 ret = gatt_api->client->scan_filter_add_remove(client_info,
2313 OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_UUID,
2314 filter_data->slot_id,
2317 (bt_uuid_t*)filter_data->service_uuid,
2318 (bt_uuid_t*)filter_data->service_uuid_mask,
2321 filter_data->service_uuid_len, // service_uuid_len as data_len in HAL
2323 filter_data->service_uuid_mask_len,
2326 if (ret != BT_STATUS_SUCCESS){
2327 BT_ERR("error: %s", status2string(ret));
2328 BT_INFO("unregistering already set filter features.");
2329 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2330 return convert_to_oal_status(ret);
2333 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_UUID;
2335 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID){
2336 BT_INFO("OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID is being added");
2337 char uuid_str1[2*BT_UUID_STRING_MAX];
2338 char uuid_str2[2*BT_UUID_STRING_MAX];
2339 uuid_to_stringname((service_uuid_t*)filter_data->service_solicitation_uuid, uuid_str1);
2340 uuid_to_stringname((service_uuid_t*)filter_data->service_solicitation_uuid_mask, uuid_str2);
2341 BT_INFO("Service Solicitation UUID is [%s] and Service Solicitation UUID mask is [%s]", uuid_str1, uuid_str2);
2342 ret = gatt_api->client->scan_filter_add_remove(client_info,
2344 OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID,
2345 filter_data->slot_id,
2348 (bt_uuid_t*)filter_data->service_solicitation_uuid,
2349 (bt_uuid_t*)filter_data->service_solicitation_uuid_mask,
2352 filter_data->service_solicitation_uuid_len, // service_solicitation_uuid_len as data_len in HAL
2354 filter_data->service_solicitation_uuid_mask_len,
2357 if (ret != BT_STATUS_SUCCESS){
2358 BT_ERR("error: %s", status2string(ret));
2359 BT_INFO("unregistering already set filter features.");
2360 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2361 return convert_to_oal_status(ret);
2364 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID;
2366 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_DATA){
2367 ret = gatt_api->client->scan_filter_add_remove(client_info,
2369 OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_DATA,
2370 filter_data->slot_id,
2377 filter_data->service_data_len, //service_data_len as data_len in HAL
2378 (char*)filter_data->service_data,
2379 filter_data->service_data_mask_len,
2380 (char*)filter_data->service_data_mask
2382 if (ret != BT_STATUS_SUCCESS){
2383 BT_ERR("error: %s", status2string(ret));
2384 BT_INFO("unregistering already set filter features.");
2385 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2386 return convert_to_oal_status(ret);
2389 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_DATA;
2391 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA){
2392 ret = gatt_api->client->scan_filter_add_remove(client_info,
2394 OAL_BLE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA,
2395 filter_data->slot_id,
2396 filter_data->manufacturer_id,
2402 filter_data->manufacturer_data_len, //manufacturer_data_len as data_len in HAL
2403 (char*)filter_data->manufacturer_data,
2404 filter_data->manufacturer_data_mask_len,
2405 (char*)filter_data->manufacturer_data_mask
2407 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA;
2408 if (ret != BT_STATUS_SUCCESS){
2409 BT_ERR("error: %s", status2string(ret));
2410 BT_INFO("unregistering already set filter features.");
2411 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2412 return convert_to_oal_status(ret);
2415 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA;
2418 BT_DBG("Filter selection 0x%.2x", feature_selection);
2420 ret = gatt_api->client->scan_filter_param_setup(client_info, action, filter_data->slot_id, feature_selection,
2421 scan_filter_setup.list_logic_type, scan_filter_setup.filt_logic_type,
2422 scan_filter_setup.rssi_high_thres, scan_filter_setup.rssi_low_thres,
2423 scan_filter_setup.dely_mode, scan_filter_setup.found_timeout,
2424 scan_filter_setup.lost_timeout, scan_filter_setup.found_timeout_cnt);
2425 if (ret != BT_STATUS_SUCCESS){
2426 BT_ERR("error: %s", status2string(ret));
2427 return convert_to_oal_status(ret);
2429 return OAL_STATUS_SUCCESS;
2432 oal_status_t gattc_get_att_mtu(int conn_id, int *mtu)
2434 int ret = OAL_STATUS_SUCCESS;
2435 API_TRACE("Get ATT MTU, conn_id: %d", conn_id);
2436 CHECK_OAL_GATT_ENABLED();
2437 CHECK_CLIENT_CONNECTION(conn_id);
2438 OAL_CHECK_PARAMETER(mtu, return);
2440 /* To prevent crash in case other libraries not support this api */
2441 if (gatt_api->client->get_att_mtu == NULL) {
2442 BT_WARN("get_att_mtu is NULL");
2443 return OAL_STATUS_NOT_SUPPORT;
2446 ret = gatt_api->client->get_att_mtu(conn_id, mtu);
2447 if (ret != BT_STATUS_SUCCESS) {
2448 BT_ERR("GATT MTU Size failed, status: %s", status2string(ret));
2449 return convert_to_oal_status(ret);
2452 BT_INFO("Current ATT MTU Size: %d", *mtu);
2453 return OAL_STATUS_SUCCESS;
2456 oal_status_t gattc_configure_mtu(int conn_id, int mtu)
2460 API_TRACE("Configure MTU Size: [%d]", mtu);
2461 CHECK_OAL_GATT_ENABLED();
2462 CHECK_CLIENT_CONNECTION(conn_id);
2464 /* To prevent crash in case other libraries not support this api */
2465 if (gatt_api->client->configure_mtu == NULL) {
2466 BT_WARN("configure_mtu is NULL");
2467 return OAL_STATUS_NOT_SUPPORT;
2470 ret = gatt_api->client->configure_mtu(conn_id, mtu);
2471 if (ret != BT_STATUS_SUCCESS) {
2472 BT_ERR("Gatt client configure_mtu error: %s", status2string(ret));
2473 return convert_to_oal_status(ret);
2476 return OAL_STATUS_SUCCESS;
2479 oal_status_t gatt_get_data_batching_available_packets(
2480 unsigned int *available_packets)
2484 ret = gatt_api->client->get_data_batching_available_packets(available_packets);
2485 if (ret != BT_STATUS_SUCCESS) {
2486 BT_ERR("GATT data batching failed: %s", status2string(ret));
2487 return convert_to_oal_status(ret);
2490 return OAL_STATUS_SUCCESS;
2493 oal_status_t gatt_enable_data_batching(bt_address_t * address,
2494 int packet_threshold, int timeout)
2498 ret = gatt_api->client->enable_data_batching((bt_bdaddr_t *)address, packet_threshold, timeout);
2499 if (ret != BT_STATUS_SUCCESS) {
2500 BT_ERR("GATT data batching failed: %s", status2string(ret));
2501 return convert_to_oal_status(ret);
2504 return OAL_STATUS_SUCCESS;
2507 oal_status_t gatt_disable_data_batching(bt_address_t * address)
2511 ret = gatt_api->client->disable_data_batching((bt_bdaddr_t *)address);
2512 if (ret != BT_STATUS_SUCCESS) {
2513 BT_ERR("GATT data batching failed: %s", status2string(ret));
2514 return convert_to_oal_status(ret);
2517 return OAL_STATUS_SUCCESS;
2520 oal_status_t gattc_add_connection_info(bt_address_t *device_address, int conn_id, int instance_id)
2522 int ret = OAL_STATUS_SUCCESS;
2525 OAL_CHECK_PARAMETER(device_address, return);
2526 API_TRACE("Add connection info: [%s]", bdt_bd2str(device_address, &bdstr) + 12);
2527 CHECK_OAL_GATT_ENABLED();
2529 ret = gatt_api->client->add_connection_info((bt_bdaddr_t *)device_address, conn_id, instance_id);
2530 if (ret != BT_STATUS_SUCCESS) {
2531 BT_ERR("GATT connection info add failed: %s", status2string(ret));
2532 return convert_to_oal_status(ret);
2534 return OAL_STATUS_SUCCESS;