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;
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;
587 adv_setup.server_if = instance_id;
589 ret = gatt_api->server->multi_adv_set_inst_data(adv_setup);
590 if (ret != BT_STATUS_SUCCESS) {
591 BT_ERR("GATT Set Multi advertising data failed: %s", status2string(ret));
592 info->state = GATT_INS_DISABLED;
593 return convert_to_oal_status(ret);
595 return OAL_STATUS_SUCCESS;
598 oal_status_t gatts_disconnect(int instance_id, bt_address_t *device_address, int conn_id)
600 int ret = OAL_STATUS_SUCCESS;
603 API_TRACE("Server Disonnect: instance_id = %d, conn_id = %d", instance_id, conn_id);
605 CHECK_OAL_GATT_ENABLED();
606 CHECK_SERVER_INSTANCE(instance_id);
607 if (__gatts_find_server_instance(instance_id) == NULL)
608 return OAL_STATUS_INTERNAL_ERROR;
610 API_TRACE("[%s]", bdt_bd2str(device_address, &bdstr));
612 ret = gatt_api->server->disconnect(instance_id, (bt_bdaddr_t *) device_address, conn_id);
613 if (ret != BT_STATUS_SUCCESS) {
614 BT_ERR("GATT Server disconnect failed: %s", status2string(ret));
615 return convert_to_oal_status(ret);
617 return OAL_STATUS_SUCCESS;
621 /************************************GATT Server Functions*************************************/
623 static void cb_gatts_register_app(int status, int server_if, bt_uuid_t *uuid)
625 char str[2*BT_UUID_STRING_MAX];
626 uuid_to_stringname((service_uuid_t*)uuid, str);
628 event_gatts_register_t *event = g_malloc0(sizeof(event_gatts_register_t));
630 BT_INFO("BTGATT SERVER REGISTER APP CB, UUID:%s, status:%d, server_if:%d",
631 str, status, server_if);
633 gatt_server_t *info = g_malloc0(sizeof(gatt_server_t));
634 info->instance_id = server_if;
635 memcpy(info->uuid.uuid, uuid->uu, sizeof(bt_uuid_t));
636 gatt_servers = g_slist_append(gatt_servers, info);
637 BT_DBG("GATT Server Added. count of gatt_servers: %d", g_slist_length(gatt_servers));
639 event->server_inst = server_if;
640 memcpy(event->server_uuid.uuid, uuid->uu, sizeof(bt_uuid_t));
641 send_event(OAL_EVENT_BLE_SERVER_INSTANCE_INITIALISED, event, sizeof(event_gatts_register_t));
644 static void cb_gatts_listen(int status, int server_if)
647 gboolean new_state = FALSE;
648 event_ble_adv_status *event = g_malloc0(sizeof(event_ble_adv_status));
651 for (l = gatt_servers; l; l = g_slist_next(l)) {
652 gatt_server_t *info = l->data;
655 if (info->instance_id == server_if) {
656 event->server_inst = server_if;
657 prev_state = info->cur_adv_state;
658 new_state = (status == BT_STATUS_SUCCESS) ? !prev_state : prev_state;
659 info->cur_adv_state = new_state;
664 BT_INFO("Adv State of server instance %d, new_state: %d", server_if, new_state);
666 if (TRUE == new_state)
667 send_event(OAL_EVENT_BLE_ADVERTISING_STARTED, event, sizeof(event_ble_adv_status));
669 send_event(OAL_EVENT_BLE_ADVERTISING_STOPPED, event, sizeof(event_ble_adv_status));
672 static void cb_gatts_multi_adv_enable(int server_if, int status)
675 BT_INFO("BTGATT SERVER MULTI ADV ENABLE CB, status:%d, srv_if: %d", status, server_if);
676 event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
679 if (status != BT_STATUS_SUCCESS)
680 BT_ERR("MULTI ADV ENABLE failed, status:%d, srv_if: %d", status, server_if);
682 for (l = gatt_servers; l; l = g_slist_next(l)) {
683 gatt_server_t *info = l->data;
686 if (info->instance_id == server_if) {
687 event->server_inst = server_if;
688 info->state = GATT_INS_ENABLED;
692 event->server_inst = -1;
694 event->status = convert_to_oal_status(status);
695 send_event(OAL_EVENT_BLE_MULTI_ADVERTISING_ENABLE, event, sizeof(event_ble_multiadv_status));
698 static void cb_gatts_multi_adv_disable(int server_if, int status)
700 BT_INFO("BTGATT SERVER MULTI ADV DISABLE CB, status:%d, srv_if: %d", status, server_if);
701 event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
704 if (status != BT_STATUS_SUCCESS)
705 BT_ERR("MULTI ADV DISABLE failed, status:%d, srv_if: %d", status, server_if);
707 for (l = gatt_servers; l; l = g_slist_next(l)) {
708 gatt_server_t *info = l->data;
711 if (info->instance_id == server_if) {
712 event->server_inst = server_if;
713 info->state = GATT_INS_DISABLED;
717 event->server_inst = -1;
719 event->status = convert_to_oal_status(status);
720 send_event(OAL_EVENT_BLE_MULTI_ADVERTISING_DISABLE, event, sizeof(event_ble_multiadv_status));
723 static void cb_gatts_multi_adv_update(int server_if, int status)
725 BT_INFO("BTGATT SERVER MULTI ADV UPDATE CB, status:%d, srv_if: %d", status, server_if);
726 event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
729 if (status != BT_STATUS_SUCCESS)
730 BT_ERR("MULTI ADV UPDATE failed, status:%d, srv_if: %d", status, server_if);
732 for (l = gatt_servers; l; l = g_slist_next(l)) {
733 gatt_server_t *info = l->data;
736 if (info->instance_id == server_if) {
737 event->server_inst = server_if;
741 event->server_inst = -1;
743 event->status = convert_to_oal_status(status);
744 send_event(OAL_EVENT_BLE_MULTI_ADVERTISING_UPDATE, event, sizeof(event_ble_multiadv_status));
747 static void cb_gatts_multi_adv_set_inst_data(int server_if, int status)
749 BT_INFO("BTGATT SERVER MULTI ADV SET INST DATA CB, status:%d, srv_if: %d", status, server_if);
750 event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
753 if (status != BT_STATUS_SUCCESS)
754 BT_ERR("MULTI ADV SET DATA failed, status:%d, srv_if: %d", status, server_if);
756 for (l = gatt_servers; l; l = g_slist_next(l)) {
757 gatt_server_t *info = l->data;
760 if (info->instance_id == server_if) {
761 event->server_inst = server_if;
762 info->state = GATT_INS_DATA_SET;
766 event->server_inst = -1;
768 event->status = convert_to_oal_status(status);
769 send_event(OAL_EVENT_BLE_MULTI_ADVERTISING_SET_INST_DATA, event, sizeof(event_ble_multiadv_status));
772 oal_status_t gatts_get_att_mtu(int conn_id, int *mtu)
774 int ret = OAL_STATUS_SUCCESS;
775 API_TRACE("Get ATT MTU, conn_id: %d", conn_id);
776 CHECK_OAL_GATT_ENABLED();
777 OAL_CHECK_PARAMETER(mtu, return);
779 /* To prevent crash in case other libraries not support this api */
780 if (gatt_api->server->get_att_mtu == NULL) {
781 BT_WARN("get_att_mtu is NULL");
782 return OAL_STATUS_NOT_SUPPORT;
785 ret = gatt_api->server->get_att_mtu(conn_id, mtu);
786 if (ret != BT_STATUS_SUCCESS) {
787 BT_ERR("GATT MTU Size failed, status: %s", status2string(ret));
788 return convert_to_oal_status(ret);
791 BT_INFO("Current ATT MTU Size: %d", *mtu);
792 return OAL_STATUS_SUCCESS;
794 oal_status_t gatt_send_response_acquire(int conn_id, int trans_id,
795 int status, int fd, int mtu , void * fdlist)
797 int ret = OAL_STATUS_SUCCESS;
799 API_TRACE("Server Send Response : ConnId = %d, TransId = %d, %d", conn_id, trans_id, fd);
800 CHECK_OAL_GATT_ENABLED();
802 ret = gatt_api->server->send_response_acquire(conn_id, trans_id, status, fd, mtu, fdlist);
803 if (ret != BT_STATUS_SUCCESS) {
804 BT_ERR("GATT Server Send Response failed: %s", status2string(ret));
805 return convert_to_oal_status(ret);
807 return OAL_STATUS_SUCCESS;
810 oal_status_t gatts_add_service(int instance_id, oal_gatt_srvc_id_t *gatt_serv_id,
813 int ret = OAL_STATUS_SUCCESS;
814 btgatt_srvc_id_t btgatt_srvc_id;
815 char str[2*BT_UUID_STRING_MAX];
817 API_TRACE("Server Add Service : instance_id = %d, num_handles = %d", instance_id, num_handles);
818 CHECK_OAL_GATT_ENABLED();
820 CHECK_SERVER_INSTANCE(instance_id);
821 if (__gatts_find_server_instance(instance_id) == NULL)
822 return OAL_STATUS_INTERNAL_ERROR;
824 if (gatt_serv_id != NULL) {
825 uuid_to_stringname(&(gatt_serv_id->id.uuid), str);
826 API_TRACE("Service uuid: [%s]", str);
827 memcpy(btgatt_srvc_id.id.uuid.uu, gatt_serv_id->id.uuid.uuid, sizeof(gatt_serv_id->id.uuid));
828 btgatt_srvc_id.id.inst_id = gatt_serv_id->id.inst_id;
829 btgatt_srvc_id.is_primary = gatt_serv_id->is_prmry;
831 BT_INFO("GATT Server Service Id is NULL");
832 return OAL_STATUS_INVALID_PARAM;
835 ret = gatt_api->server->add_service(instance_id, &btgatt_srvc_id, num_handles);
836 if (ret != BT_STATUS_SUCCESS) {
837 BT_ERR("GATT Server add service failed: %s", status2string(ret));
838 return convert_to_oal_status(ret);
840 return OAL_STATUS_SUCCESS;
843 oal_status_t gatts_add_included_services(int instance_id, int serv_handle,
846 int ret = OAL_STATUS_SUCCESS;
847 API_TRACE("Server Add included Service : instance_id = %d, serv_handle = %d, incl_handle = %d",
848 instance_id, serv_handle, incl_handle);
849 CHECK_OAL_GATT_ENABLED();
851 CHECK_SERVER_INSTANCE(instance_id);
852 if (__gatts_find_server_instance(instance_id) == NULL)
853 return OAL_STATUS_INTERNAL_ERROR;
855 ret = gatt_api->server->add_included_service(instance_id, serv_handle, incl_handle);
856 if (ret != BT_STATUS_SUCCESS) {
857 BT_ERR("GATT Server Add included Service failed: %s", status2string(ret));
858 return convert_to_oal_status(ret);
860 return OAL_STATUS_SUCCESS;
863 oal_status_t gatts_add_characteristics(int instance_id, int serv_handle,
864 oal_uuid_t* charc_uuid, int propts, int permsn)
866 int ret = OAL_STATUS_SUCCESS;
867 char str[2*BT_UUID_STRING_MAX];
868 char prop[MAX_PROP_LEN], perm[MAX_PERM_LEN];
869 char_prop_to_string(propts, prop);
870 char_perm_to_string(permsn, perm);
871 API_TRACE("Server Add Characteristic : instance_id = %d, serv_handle = %d, Properties =%s, Permisison = %s",
872 instance_id, serv_handle, prop, perm);
873 CHECK_OAL_GATT_ENABLED();
875 CHECK_SERVER_INSTANCE(instance_id);
876 if (__gatts_find_server_instance(instance_id) == NULL)
877 return OAL_STATUS_INTERNAL_ERROR;
879 if (charc_uuid != NULL) {
880 uuid_to_stringname(charc_uuid, str);
881 API_TRACE("uuid: [%s]", str);
884 ret = gatt_api->server->add_characteristic(instance_id,
885 serv_handle, (bt_uuid_t*)charc_uuid, propts, permsn);
886 if (ret != BT_STATUS_SUCCESS) {
887 BT_ERR("GATT Server Add Characteristic failed: %s", status2string(ret));
888 return convert_to_oal_status(ret);
890 return OAL_STATUS_SUCCESS;
893 oal_status_t gatts_add_descriptor(int instance_id, int serv_handle,
894 oal_uuid_t* desc_uuid, int permsn)
896 int ret = OAL_STATUS_SUCCESS;
897 char str[2*BT_UUID_STRING_MAX];
898 char perm[MAX_PERM_LEN];
899 char_perm_to_string(permsn, perm);
900 API_TRACE("Server Add Descriptor : instance_id = %d, serv_handle = %d, Permisison = %s",
901 instance_id, serv_handle, perm);
902 CHECK_OAL_GATT_ENABLED();
904 CHECK_SERVER_INSTANCE(instance_id);
905 if (__gatts_find_server_instance(instance_id) == NULL)
906 return OAL_STATUS_INTERNAL_ERROR;
908 if (desc_uuid != NULL) {
909 uuid_to_stringname(desc_uuid, str);
910 API_TRACE("uuid: [%s]", str);
913 ret = gatt_api->server->add_descriptor(instance_id,
914 serv_handle, (bt_uuid_t*)desc_uuid, permsn);
915 if (ret != BT_STATUS_SUCCESS) {
916 BT_ERR("GATT Server Add Descriptor failed: %s", status2string(ret));
917 return convert_to_oal_status(ret);
919 return OAL_STATUS_SUCCESS;
922 oal_status_t gatts_start_service(int instance_id, int svc_handle, int transport)
924 int ret = OAL_STATUS_SUCCESS;
926 API_TRACE("Server Start Service : Ins_id = %d, srvHndl = %d, transport = %d",
927 instance_id, svc_handle, transport);
928 CHECK_OAL_GATT_ENABLED();
930 CHECK_SERVER_INSTANCE(instance_id);
931 if (__gatts_find_server_instance(instance_id) == NULL)
932 return OAL_STATUS_INTERNAL_ERROR;
934 ret = gatt_api->server->start_service(instance_id, svc_handle, transport);
935 if (ret != BT_STATUS_SUCCESS) {
936 BT_ERR("GATT Server Start Service failed: %s", status2string(ret));
937 return convert_to_oal_status(ret);
939 return OAL_STATUS_SUCCESS;
942 oal_status_t gatts_stop_service(int instance_id, int srv_hdl)
944 int ret = OAL_STATUS_SUCCESS;
946 API_TRACE("Server Stop Service : Ins_id = %d, srvHndl = %d, ", instance_id, srv_hdl);
947 CHECK_OAL_GATT_ENABLED();
949 CHECK_SERVER_INSTANCE(instance_id);
950 if (__gatts_find_server_instance(instance_id) == NULL)
951 return OAL_STATUS_INTERNAL_ERROR;
953 ret = gatt_api->server->stop_service(instance_id, srv_hdl);
954 if (ret != BT_STATUS_SUCCESS) {
955 BT_ERR("GATT Server Stop Service failed: %s", status2string(ret));
956 return convert_to_oal_status(ret);
958 return OAL_STATUS_SUCCESS;
961 oal_status_t gatts_delete_service(int instance_id, int srv_hdl)
963 int ret = OAL_STATUS_SUCCESS;
965 API_TRACE("Server Delete Service : Ins_id = %d, srvHndl = %d, ", instance_id, srv_hdl);
966 CHECK_OAL_GATT_ENABLED();
968 CHECK_SERVER_INSTANCE(instance_id);
969 if (__gatts_find_server_instance(instance_id) == NULL)
970 return OAL_STATUS_INTERNAL_ERROR;
972 ret = gatt_api->server->delete_service(instance_id, srv_hdl);
973 if (ret != BT_STATUS_SUCCESS) {
974 BT_ERR("GATT Server Delete Service failed: %s", status2string(ret));
975 return convert_to_oal_status(ret);
977 return OAL_STATUS_SUCCESS;
980 oal_status_t gatts_send_response(int conn_id, int trans_id, int resp_status, oal_gatt_response_t *response)
982 int ret = OAL_STATUS_SUCCESS;
984 API_TRACE("Server Send Response : ConnId = %d, TransId = %d, ", conn_id, trans_id);
985 CHECK_OAL_GATT_ENABLED();
987 if (response == NULL) {
988 BT_ERR("GATT Server attribute value is empty");
989 return OAL_STATUS_INVALID_PARAM;
992 ret = gatt_api->server->send_response(conn_id, trans_id, resp_status, (btgatt_response_t *)response);
993 if (ret != BT_STATUS_SUCCESS) {
994 BT_ERR("GATT Server Send Response failed: %s", status2string(ret));
995 return convert_to_oal_status(ret);
997 return OAL_STATUS_SUCCESS;
1000 oal_status_t gatts_send_indication(int instance_id, int attr_hndl, int conn_id, int len, int confirm, char* value)
1002 int ret = OAL_STATUS_SUCCESS;
1004 API_TRACE("Server Send Indication : Ins_id = %d, srvHndl = %d, [%s]",
1005 instance_id, attr_hndl, confirm == TRUE ? "INDICATION" : "NOTIFICATION");
1006 CHECK_OAL_GATT_ENABLED();
1008 CHECK_SERVER_INSTANCE(instance_id);
1009 if (__gatts_find_server_instance(instance_id) == NULL)
1010 return OAL_STATUS_INTERNAL_ERROR;
1012 if (value == NULL || len == 0) {
1013 BT_ERR("GATT Server attribute value is empty");
1014 return OAL_STATUS_INVALID_PARAM;
1017 ret = gatt_api->server->send_indication(instance_id,
1018 attr_hndl, conn_id, len, confirm, value);
1019 if (ret != BT_STATUS_SUCCESS) {
1020 BT_ERR("GATT Server Send Indication failed: %s", status2string(ret));
1021 return convert_to_oal_status(ret);
1023 return OAL_STATUS_SUCCESS;
1026 oal_status_t gatts_update_att_value(int instance_id, oal_gatt_value_t *value)
1028 int ret = OAL_STATUS_SUCCESS;
1030 CHECK_OAL_GATT_ENABLED();
1032 CHECK_SERVER_INSTANCE(instance_id);
1033 if (__gatts_find_server_instance(instance_id) == NULL)
1034 return OAL_STATUS_INTERNAL_ERROR;
1036 if (value == NULL || value->len == 0) {
1037 BT_ERR("GATT Server attribute value is empty");
1038 return OAL_STATUS_INVALID_PARAM;
1041 API_TRACE("Server Update Value: Ins_id = [%d], att handle = [%d] data len [%d]",
1042 instance_id, value->handle, value->len);
1044 ret = gatt_api->server->update_att_value(instance_id,
1045 (int)value->handle, (int)value->len, (char*)value->value);
1046 if (ret != BT_STATUS_SUCCESS) {
1047 BT_ERR("GATT Server Update Attribute Value failed: [%s]", status2string(ret));
1048 return convert_to_oal_status(ret);
1050 return OAL_STATUS_SUCCESS;
1053 /* GATT Server Callbacks:Start */
1054 static void cb_gatts_service_added(int status, int server_if,
1055 btgatt_srvc_id_t *psrvc_id, int srvc_handle)
1057 BT_INFO("BTGATT SERVER SERVICE ADDED CB, status:%d, handle:%d", status, srvc_handle);
1058 event_gatts_srvc_prm_t* event = g_new0(event_gatts_srvc_prm_t, 1);
1060 CHECK_CORRECT_SERVER_ID(server_if, event->gatt_srvc_stat.server_inst);
1062 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1063 event->gatt_srvc_stat.server_inst = -1;
1065 event->gatt_srvc_stat.servic_hndl = srvc_handle;
1066 if (psrvc_id != NULL) {
1067 event->gatt_srvc_id.id.inst_id = psrvc_id->id.inst_id;
1068 //event->gatt_srvc_id.id.uuid = psrvc_id->id.uuid;
1069 memcpy(event->gatt_srvc_id.id.uuid.uuid, psrvc_id->id.uuid.uu, sizeof(psrvc_id->id.uuid));
1070 event->gatt_srvc_id.is_prmry = psrvc_id->is_primary;
1073 event->gatt_srvc_stat.status = convert_to_oal_status(status);
1075 send_event(OAL_EVENT_GATTS_SERVICE_ADDED, event, sizeof(event_gatts_srvc_prm_t));
1078 static void cb_gatts_included_service_added(int status, int server_if,
1080 int incl_srvc_handle)
1082 BT_INFO("BTGATT SERVER INCLUDED SERVICE ADDED CB, status:%d, srvc handle:%d, incl handle:%d",
1083 status, srvc_handle, incl_srvc_handle);
1085 event_gatts_incld_srvc_t* event = g_new0(event_gatts_incld_srvc_t, 1);
1087 CHECK_CORRECT_SERVER_ID(server_if, event->gatt_srvc_stat.server_inst);
1089 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1090 event->gatt_srvc_stat.server_inst = -1;
1092 event->gatt_srvc_stat.servic_hndl = srvc_handle;
1093 event->incl_srvc_hndl = incl_srvc_handle;
1094 event->gatt_srvc_stat.status = convert_to_oal_status(status);
1095 send_event(OAL_EVENT_GATTS_INCLUDED_SERVICE_ADDED, event, sizeof(event_gatts_incld_srvc_t));
1098 static void cb_gatts_characteristic_added(int status, int server_if, bt_uuid_t *char_id,
1099 int srvc_handle, int char_handle)
1101 BT_INFO("BTGATT SERVER CHARACTERISTIC ADDED CB, status:%d, srvc handle:%d, char handle:%d",
1102 status, srvc_handle, char_handle);
1104 event_gatts_srvc_charctr_t* event = g_new0(event_gatts_srvc_charctr_t, 1);
1106 CHECK_CORRECT_SERVER_ID(server_if, event->gatt_srvc_stat.server_inst);
1108 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1109 event->gatt_srvc_stat.server_inst = -1;
1111 event->gatt_srvc_stat.servic_hndl = srvc_handle;
1112 event->charctr_hndl = char_handle;
1113 event->gatt_srvc_stat.status = convert_to_oal_status(status);
1114 memcpy(event->charctr_uuid.uuid, char_id->uu, sizeof(bt_uuid_t));
1115 send_event(OAL_EVENT_GATTS_CHARACTERISTIC_ADDED, event, sizeof(event_gatts_srvc_charctr_t));
1118 static void cb_gatts_descriptor_added(int status, int server_if,
1119 bt_uuid_t *descr_id, int srvc_handle,
1122 BT_INFO("BTGATT SERVER DESCRIPTOR ADDED CB, status:%d, srvc handle:%d, desc handle:%d",
1123 status, srvc_handle, descr_handle);
1125 event_gatts_srvc_descr_t* event = g_new0(event_gatts_srvc_descr_t, 1);
1127 CHECK_CORRECT_SERVER_ID(server_if, event->gatt_srvc_stat.server_inst);
1129 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1130 event->gatt_srvc_stat.server_inst = -1;
1132 event->gatt_srvc_stat.servic_hndl = srvc_handle;
1133 event->descrptr_hndl = descr_handle;
1134 event->gatt_srvc_stat.status = convert_to_oal_status(status);
1135 memcpy(event->descrptr_uuid.uuid, descr_id->uu, sizeof(bt_uuid_t));
1136 send_event(OAL_EVENT_GATTS_DESCRIPTOR_ADDED, event, sizeof(event_gatts_srvc_descr_t));
1139 static void cb_gatts_service_started(int status, int server_if, int srvc_handle)
1141 BT_INFO("BTGATT SERVER SERVICE STARTED CB, status:%d, srvc_handle:%d", status, srvc_handle);
1143 event_gatts_srvc_t* event = g_new0(event_gatts_srvc_t, 1);
1145 CHECK_CORRECT_SERVER_ID(server_if, event->server_inst);
1147 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1148 event->server_inst = -1;
1150 event->servic_hndl = srvc_handle;
1151 event->status = convert_to_oal_status(status);
1152 send_event(OAL_EVENT_GATTS_SERVICE_STARTED, event, sizeof(event_gatts_srvc_t));
1155 static void cb_gatts_service_stopped(int status, int server_if, int srvc_handle)
1157 BT_INFO("BTGATT SERVER SERVICE STOPPED CB, status:%d, srvc_handle:%d", status, srvc_handle);
1159 event_gatts_srvc_t* event = g_new0(event_gatts_srvc_t, 1);
1161 CHECK_CORRECT_SERVER_ID(server_if, event->server_inst);
1163 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1164 event->server_inst = -1;
1166 event->servic_hndl = srvc_handle;
1167 event->status = convert_to_oal_status(status);
1168 send_event(OAL_EVENT_GATTS_SERVICE_STOPED, event, sizeof(event_gatts_srvc_t));
1171 static void cb_gatts_service_deleted(int status, int server_if, int srvc_handle)
1173 BT_INFO("BTGATT SERVER SERVICE DELETED CB, status:%d, srvc_handle:%d", status, srvc_handle);
1175 event_gatts_srvc_t* event = g_new0(event_gatts_srvc_t, 1);
1177 CHECK_CORRECT_SERVER_ID(server_if, event->server_inst);
1179 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1180 event->server_inst = -1;
1182 event->servic_hndl = srvc_handle;
1183 event->status = convert_to_oal_status(status);
1184 send_event(OAL_EVENT_GATTS_SERVICE_DELETED, event, sizeof(event_gatts_srvc_t));
1187 static void cb_gatts_connection(int conn_id, int server_if, int connected, bt_bdaddr_t *bda)
1190 oal_event_t event_type;
1192 BT_INFO("BTGATT SERVER CONNECTION connnected:%d, conn_id:%d server_if:%d", connected, conn_id, server_if);
1193 event_gatts_conn_t *event = g_new0(event_gatts_conn_t, 1);
1195 if (connected == TRUE) {
1196 for (l = gatt_servers; l; l = g_slist_next(l)) {
1197 gatt_server_t *info = l->data;
1200 if (info->instance_id == server_if) {
1201 event->server_inst = server_if;
1206 /* server_inst is not required in disconnected case */
1208 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1209 event->conn_id = conn_id;
1210 event_type = ((connected == TRUE) ? OAL_EVENT_GATTS_CONNECTION_COMPLETED : OAL_EVENT_GATTS_DISCONNECTION_COMPLETED);
1211 event->status = OAL_STATUS_SUCCESS;
1213 send_event(event_type, event, sizeof(event_gatts_conn_t));
1216 static void cb_gatts_request_read(int conn_id, int trans_id, bt_bdaddr_t *bda,
1217 int attr_handle, int offset, bool is_long)
1219 BT_DBG("BTGATT SERVER REQUEST READ CB: conn_id:%d", conn_id);
1221 event_gatts_srvc_read_attr_t* event = g_new0(event_gatts_srvc_read_attr_t, 1);
1223 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1225 event->attr_trans.attr_handle = attr_handle;
1226 event->attr_trans.conn_id = conn_id;
1227 event->attr_trans.trans_id = trans_id;
1228 event->attr_trans.offset = offset;
1229 event->is_long = is_long;
1231 send_event(OAL_EVENT_GATTS_REQUEST_READ, event, sizeof(event_gatts_srvc_read_attr_t));
1234 static void cb_gatts_request_write(int conn_id, int trans_id,
1235 bt_bdaddr_t *bda, int attr_handle,
1236 int offset, int length,
1237 bool need_rsp, bool is_prep, uint8_t* value)
1239 BT_INFO("BTGATT SERVER REQUEST WRITE conn_id:%d", conn_id);
1241 event_gatts_srvc_write_attr_t* event = g_new0(event_gatts_srvc_write_attr_t, 1);
1243 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1245 event->attr_trans.attr_handle = attr_handle;
1246 event->attr_trans.conn_id = conn_id;
1247 event->attr_trans.trans_id = trans_id;
1248 event->attr_trans.offset = offset;
1249 event->need_rsp = need_rsp;
1250 event->is_prep = is_prep;
1252 if (length > 0 && value != NULL) {
1253 if (length > OAL_GATT_MAX_ATTR_LEN)
1254 length = OAL_GATT_MAX_ATTR_LEN;
1255 memcpy(event->value, value, length);
1256 event->length = length;
1258 send_event(OAL_EVENT_GATTS_REQUEST_WRITE, event, sizeof(event_gatts_srvc_write_attr_t));
1261 static void cb_gatts_acquire_write(int mtu, int conn_id, int trans_id, int attr_handle , bt_bdaddr_t *bda)
1264 BT_INFO("BTGATT SERVER REQUEST ACQUIRE WRITE conn_id:%d", conn_id);
1266 event_gatts_srvc_acquire_attr_t* event = g_new0(event_gatts_srvc_acquire_attr_t, 1);
1268 event->attr_trans.attr_handle = attr_handle;
1269 event->attr_trans.conn_id = conn_id;
1270 event->attr_trans.trans_id = trans_id;
1272 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1274 send_event(OAL_EVENT_GATTS_REQUEST_ACQUIRE_WRITE, event, sizeof(event_gatts_srvc_acquire_attr_t));
1277 static void cb_gatts_acquire_notify(int mtu, int conn_id, int trans_id, int attr_handle)
1279 BT_INFO("BTGATT SERVER REQUEST ACQUIRE NOTIFY conn_id:%d", conn_id);
1281 event_gatts_srvc_acquire_attr_t* event = g_new0(event_gatts_srvc_acquire_attr_t, 1);
1283 event->attr_trans.attr_handle = attr_handle;
1284 event->attr_trans.conn_id = conn_id;
1285 event->attr_trans.trans_id = trans_id;
1288 send_event(OAL_EVENT_GATTS_REQUEST_ACQUIRE_NOTIFY, event, sizeof(event_gatts_srvc_acquire_attr_t));
1292 static void cb_gatts_response_confirmation(int status, int handle)
1294 BT_INFO("BTGATT SERVER RESPONSE CONFIRMATION CB, status:%d, handle:%d", status, handle);
1296 event_gatts_respons_t* event = g_new0(event_gatts_respons_t, 1);
1298 event->hndl = handle;
1299 event->status = convert_to_oal_status(status);
1301 send_event(OAL_EVENT_GATTS_RESPONSE_CONFIRMED, event, sizeof(event_gatts_respons_t));
1304 static void cb_indication_confirmation(int conn_id, int trans_id, int attr_handle, bt_bdaddr_t *bda)
1306 BT_INFO("BTGATT SERVER Indication Confirmation CB, conn_id:%d, trans_id:%d attr_handle:%d",
1307 conn_id, trans_id, attr_handle);
1309 event_gatts_ind_cnfrm_t* event = g_new0(event_gatts_ind_cnfrm_t, 1);
1311 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1313 event->attr_handle = attr_handle;
1314 event->conn_id = conn_id;
1315 event->trans_id = trans_id;
1316 send_event(OAL_EVENT_GATTS_IND_CONFIRM, event, sizeof(event_gatts_ind_cnfrm_t));
1319 static void cb_gatts_mtu_changed(int conn_id, int mtu)
1321 BT_INFO("BTGATT SERVER MTU CHANGED CB, conn_id: %d, mtu_size: %d", conn_id, mtu);
1322 event_gatts_mtu_changed_t *event = g_new0(event_gatts_mtu_changed_t, 1);
1323 event->conn_id = conn_id;
1324 event->mtu_size = mtu;
1325 send_event(OAL_EVENT_GATTS_MTU_CHANGED, event, sizeof(event_gatts_mtu_changed_t));
1329 static void cb_notifcation_changed(int conn_id, int trans_id, int attr_handle, bool notify, bt_bdaddr_t *bda)
1331 BT_INFO("BTGATT SERVER Notication CB, conn_id:%d, trans_id:%d attr_handle:%d Notify:%d",
1332 conn_id, trans_id, attr_handle, notify);
1334 event_gatts_notif_t* event = g_new0(event_gatts_notif_t, 1);
1336 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1338 event->attr_handle = attr_handle;
1339 event->conn_id = conn_id;
1340 event->trans_id = trans_id;
1341 event->notify = notify;
1342 send_event(OAL_EVENT_GATTS_NOTIFICATION, event, sizeof(event_gatts_notif_t));
1347 /* GATT Server Callbacks: End */
1348 /************************************ GATT Client ***********************************/
1349 /* Client Callbacks */
1350 static void cb_gattc_register_app(int status, int clientIf, bt_uuid_t *app_uuid)
1352 char uuid_str[BT_UUID_STRING_MAX];
1353 event_gattc_register_t *event;
1355 /* Check if GATT client registered for Default GATT client UUID */
1356 uuid_to_string((service_uuid_t *)app_uuid, uuid_str);
1358 BT_INFO("GATT CLIENT REGISTER APP CB, status:%d, clientIf:%d, %s", status, clientIf, uuid_str);
1359 event = g_new0(event_gattc_register_t, 1);
1360 event->client_if = clientIf;
1361 event->status = (status == 0 ? OAL_STATUS_SUCCESS : OAL_STATUS_INTERNAL_ERROR);
1362 memcpy(event->client_uuid.uuid, app_uuid->uu, sizeof(oal_uuid_t));
1363 send_event(OAL_EVENT_GATTC_REGISTRATION, event, sizeof(event_gattc_register_t));
1366 static void cb_gattc_scan_result(bt_bdaddr_t* bdaddress, uint8_t addr_type, int rssi,
1367 uint8_t *adv_data, int adv_data_len, uint8_t *scan_rsp_data, int scan_rsp_data_len)
1369 event_ble_scan_result_info *event;
1371 event = g_new0(event_ble_scan_result_info, 1);
1372 event->addr_type = addr_type;
1374 memcpy(event->address.addr, bdaddress->address, BT_ADDRESS_BYTES_NUM);
1375 memcpy(event->adv_data, adv_data, BLE_ADV_DATA_LENGTH);
1376 event->adv_data_len = adv_data_len;
1377 memcpy(event->scan_rsp_data, scan_rsp_data, BLE_ADV_DATA_LENGTH);
1378 event->scan_rsp_data_len = scan_rsp_data_len;
1379 send_event_bda_trace(OAL_EVENT_BLE_REMOTE_DEVICE_FOUND, event,
1380 sizeof(event_ble_scan_result_info), (bt_address_t *)bdaddress);
1383 static void cb_gattc_connection(int conn_id, int status, int client_if, bt_bdaddr_t* bda)
1385 event_gattc_conn_t *event;
1386 BT_INFO("BTGATT CLIENT CONNECTED CB, status:[%d] client_if[%d] id[%d]",
1387 status, client_if, conn_id);
1389 event = g_new0(event_gattc_conn_t, 1);
1390 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1391 event->client_if = client_if;
1393 if (BT_STATUS_SUCCESS != status) {
1394 event->status = OAL_STATUS_INTERNAL_ERROR;
1395 BT_ERR("gattc connection Error: %d", status);
1397 event->conn_id = conn_id;
1398 event->status = OAL_STATUS_SUCCESS;
1401 send_event_bda_trace(OAL_EVENT_GATTC_CONNECTION_COMPLETED, event,
1402 sizeof(*event), (bt_address_t *)bda);
1406 static void cb_gattc_disconnect(int conn_id, int status, int client_if, bt_bdaddr_t* bda)
1408 event_gattc_conn_t *event;
1409 BT_INFO("BTGATT Client Disconnect Complete cb, status:[%d], client_if[%d] id[%d]",
1410 status, client_if, conn_id);
1411 event = g_new0(event_gattc_conn_t, 1);
1412 event->status = convert_to_oal_status(status);
1413 event->client_if = client_if;
1414 event->conn_id = conn_id;
1415 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1416 send_event_bda_trace(OAL_EVENT_GATTC_DISCONNECTION_COMPLETED, event,
1417 sizeof(*event), (bt_address_t *)bda);
1420 static void cb_gattc_search_result(int conn_id, btgatt_srvc_id_t *srvc_id)
1422 char uuid_str[2*BT_UUID_STRING_MAX];
1424 uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str);
1425 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]",
1426 uuid_str, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1428 event_gattc_service_result_t *event = g_new0(event_gattc_service_result_t, 1);
1429 event->conn_status.status = OAL_STATUS_SUCCESS;
1430 event->conn_status.conn_id = conn_id;
1431 memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1433 send_event(OAL_EVENT_GATTC_SERVICE_SEARCH_RESULT, event, sizeof(*event));
1436 static void cb_gattc_search_complete(int conn_id, int status)
1438 BT_INFO("BTGATT Client Service Search Complete cb, status:%d, conn_id:%d",
1441 event_gattc_conn_status_t *event = g_new0(event_gattc_conn_status_t, 1);
1442 event->conn_id = conn_id;
1443 event->status = convert_to_oal_status(status);
1445 send_event(OAL_EVENT_GATTC_SERVICE_SEARCH_DONE, event, sizeof(*event));
1449 static void cb_gattc_get_characteristics(int conn_id, int status, btgatt_srvc_id_t *srvc_id,
1450 btgatt_gatt_id_t *char_id, int char_prop)
1452 char uuid_str1[2*BT_UUID_STRING_MAX];
1453 char uuid_str2[2*BT_UUID_STRING_MAX];
1456 BT_DBG("BTGATT Client Get Characteristic Callback, conn_id:%d, status:%d", conn_id, status);
1457 uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str1);
1458 BT_DBG("Service=> [%s], Inst_id [%u], Type [%s]",
1459 uuid_str1, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1461 event_gattc_characteristic_result_t *event = g_new0(event_gattc_characteristic_result_t, 1);
1462 event->conn_status.conn_id = conn_id;
1463 event->conn_status.status = convert_to_oal_status(status);
1464 memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1467 uuid_to_stringname((oal_uuid_t *)&(char_id->uuid), uuid_str2);
1468 BT_INFO("Charac => [%s], Inst_id [%u], Prop [%s]",
1469 uuid_str2, char_id->inst_id, char_prop_to_string(char_prop, str));
1470 event->char_prop = char_prop;
1471 memcpy(&(event->char_id), char_id, sizeof(oal_gatt_id_t));
1473 send_event(OAL_EVENT_GATTC_CHARAC_SERACH_RESULT, event, sizeof(*event));
1478 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)
1480 char uuid_str1[2*BT_UUID_STRING_MAX];
1481 char uuid_str2[2*BT_UUID_STRING_MAX];
1482 char uuid_str3[2*BT_UUID_STRING_MAX];
1483 BT_DBG("BTGATT Client Get Descriptor Callback, conn_id:%d, status:%d", conn_id, status);
1484 uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str1);
1485 uuid_to_stringname((oal_uuid_t *)&(char_id->uuid), uuid_str2);
1486 BT_DBG("Service=> [%s], Inst_id [%u], Type [%s]",
1487 uuid_str1, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1488 BT_DBG("Charac => [%s], Inst_id [%u]", uuid_str2, char_id->inst_id);
1490 event_gattc_descriptor_result_t *event = g_new0(event_gattc_descriptor_result_t, 1);
1491 event->conn_status.conn_id = conn_id;
1492 event->conn_status.status = convert_to_oal_status(status);
1493 memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1494 memcpy(&(event->char_id), char_id, sizeof(oal_gatt_id_t));
1497 uuid_to_stringname((oal_uuid_t *)&(descr_id->uuid), uuid_str3);
1498 BT_INFO("Desc => [%s], Inst_id [%u]", uuid_str3, descr_id->inst_id);
1499 memcpy(&(event->descr_id), descr_id, sizeof(oal_gatt_id_t));
1501 send_event(OAL_EVENT_GATTC_DESC_SERACH_RESULT, event, sizeof(*event));
1504 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)
1506 char uuid_str1[2*BT_UUID_STRING_MAX];
1507 char uuid_str2[2*BT_UUID_STRING_MAX];
1508 BT_INFO("BTGATT Client Register For Notification Callback, conn_id:%d, status:%d, registered: %s",
1509 conn_id, status, registered == 1 ? "REGISTERED" : "DEREGISTERED");
1510 uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str1);
1511 uuid_to_stringname((oal_uuid_t *)&(char_id->uuid), uuid_str2);
1512 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1513 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, char_id->inst_id);
1515 event_gattc_regdereg_notify_t *event = g_new0(event_gattc_regdereg_notify_t, 1);
1516 oal_event_t event_type = (registered == 1 ? OAL_EVENT_GATTC_NOTIFICATION_REGISTERED : OAL_EVENT_GATTC_NOTIFICATION_DEREGISTERED);
1517 event->conn_id = conn_id;
1518 event->status = convert_to_oal_status(status);
1519 memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1520 memcpy(&(event->char_id), char_id, sizeof(oal_gatt_id_t));
1522 send_event(event_type, event, sizeof(*event));
1525 static void cb_gattc_notify(int conn_id, btgatt_notify_params_t *p_data)
1528 char uuid_str1[2*BT_UUID_STRING_MAX];
1529 char uuid_str2[2*BT_UUID_STRING_MAX];
1531 BT_INFO("BTGATT Client Notify Callback, conn_id:%d", conn_id);
1532 BT_INFO("Server Address:[%s], is_notify:[%d], len:[%d]",
1533 bdt_bd2str((bt_address_t *)&(p_data->bda), &bdstr), p_data->is_notify, p_data->len);
1535 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1536 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1538 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]",
1539 uuid_str1, p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1540 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1542 if (p_data->len > 0) {
1544 data = g_malloc(3*p_data->len+1);
1546 BT_ERR("memory allocation failed");
1550 convert_hex_2_str((unsigned char *)p_data->value, p_data->len, data);
1551 BT_INFO("Notified Data: [%s]", data);
1553 event_gattc_notify_data *event = g_new0(event_gattc_notify_data, 1);
1554 memcpy(event->address.addr, p_data->bda.address, BT_ADDRESS_BYTES_NUM);
1555 event->is_notify = p_data->is_notify;
1556 event->data_len = p_data->len;
1557 memcpy(event->data, p_data->value, event->data_len);
1558 memcpy(&(event->char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1559 memcpy(&(event->srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1561 send_event_bda_trace(OAL_EVENT_GATTC_NOTIFY_DATA, event, sizeof(*event), (bt_address_t *)&p_data->bda);
1566 static void cb_gattc_service_changed(bt_bdaddr_t *bd_addr, int change_type, uint8_t *uuid, int conn_id, int inst_id)
1568 event_gattc_service_changed_data *event = g_new0(event_gattc_service_changed_data, 1);
1570 memcpy(event->address.addr, bd_addr->address, 6);
1571 event->change_type = change_type;
1572 memcpy(event->uuid.uuid, uuid, sizeof(event->uuid.uuid));
1573 event->conn_id = conn_id;
1574 event->inst_id = inst_id;
1576 send_event_bda_trace(OAL_EVENT_GATTC_SERVICE_CHANGED_IND, event, sizeof(*event), (bt_address_t *)bd_addr);
1579 static void cb_gattc_read_characteristic(int conn_id, int status, btgatt_read_params_t *p_data)
1581 char uuid_str1[2*BT_UUID_STRING_MAX];
1582 char uuid_str2[2*BT_UUID_STRING_MAX];
1583 BT_DBG("BTGATT Client Read Charcateristic Callback, conn_id:%d, read_status:%d", conn_id, status);
1585 event_gattc_read_data *event = g_new0(event_gattc_read_data, 1);
1586 event->uuid_status.conn_status.conn_id = conn_id;
1587 event->uuid_status.conn_status.status = convert_to_oal_status(status);
1588 event->value_type = p_data->value_type;
1589 memcpy(&(event->uuid_status.srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1590 memcpy(&(event->uuid_status.char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1592 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1593 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1594 BT_DBG("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1,
1595 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1596 BT_DBG("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1597 BT_DBG("Len: %u, value_type: %u", p_data->value.len, p_data->value_type);
1598 if (p_data->value.len > 0 && event->value_type == GATTC_READ_VALUE_TYPE_VALUE) {
1599 event->data_len = p_data->value.len;
1600 memcpy(&(event->data), &(p_data->value.value), event->data_len);
1603 send_event(OAL_EVENT_GATTC_READ_CHARAC, event, sizeof(*event));
1606 static void cb_gattc_write_characteristic(int conn_id, int status, btgatt_write_params_t *p_data)
1608 char uuid_str1[2*BT_UUID_STRING_MAX];
1609 char uuid_str2[2*BT_UUID_STRING_MAX];
1610 BT_INFO("BTGATT Client Write Charcateristic Callback, conn_id:%d, write_status:%d", conn_id, status);
1612 event_gattc_write_data *event = g_new0(event_gattc_write_data, 1);
1613 event->conn_status.conn_id = conn_id;
1614 event->conn_status.status = convert_to_oal_status(status);
1615 memcpy(&(event->srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1616 memcpy(&(event->char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1618 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1619 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1620 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1,
1621 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1622 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1624 send_event(OAL_EVENT_GATTC_WRITE_CHARAC, event, sizeof(*event));
1627 static void cb_gattc_write_descriptor(int conn_id, int status, btgatt_write_params_t *p_data)
1629 char uuid_str1[2*BT_UUID_STRING_MAX];
1630 char uuid_str2[2*BT_UUID_STRING_MAX];
1631 char uuid_str3[2*BT_UUID_STRING_MAX];
1632 BT_DBG("BTGATT Client Write Descriptor Callback, conn_id:%d, write_status:%d", conn_id, status);
1634 event_gattc_write_data *event = g_new0(event_gattc_write_data, 1);
1635 event->conn_status.conn_id = conn_id;
1636 event->conn_status.status = convert_to_oal_status(status);
1637 memcpy(&(event->srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1638 memcpy(&(event->char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1639 memcpy(&(event->descr_id), &(p_data->descr_id), sizeof(oal_gatt_id_t));
1641 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1642 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1643 uuid_to_stringname((oal_uuid_t *)&(p_data->descr_id.uuid), uuid_str3);
1644 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1,
1645 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1646 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1647 BT_INFO("Desc => [%s], Inst_id [%u]", uuid_str3, p_data->descr_id.inst_id);
1649 send_event(OAL_EVENT_GATTC_WRITE_DESCR, event, sizeof(*event));
1654 static void cb_gattc_read_descriptor(int conn_id, int status, btgatt_read_params_t *p_data)
1656 char uuid_str1[2*BT_UUID_STRING_MAX];
1657 char uuid_str2[2*BT_UUID_STRING_MAX];
1658 char uuid_str3[2*BT_UUID_STRING_MAX];
1659 BT_INFO("BTGATT Client Read Descriptor Callback, conn_id:%d, read_status:%d", conn_id, status);
1661 event_gattc_read_data *event = g_new0(event_gattc_read_data, 1);
1662 event->uuid_status.conn_status.conn_id = conn_id;
1663 event->uuid_status.conn_status.status = convert_to_oal_status(status);
1664 event->value_type = p_data->value_type;
1665 memcpy(&(event->uuid_status.srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1666 memcpy(&(event->uuid_status.char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1667 memcpy(&(event->uuid_status.descr_id), &(p_data->descr_id), sizeof(oal_gatt_id_t));
1670 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1671 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1672 uuid_to_stringname((oal_uuid_t *)&(p_data->descr_id.uuid), uuid_str3);
1673 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1,
1674 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1675 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1676 BT_INFO("Desc => [%s], Inst_id [%u]", uuid_str3, p_data->descr_id.inst_id);
1677 BT_DBG("Len: %u, value_type: %u", p_data->value.len, p_data->value_type);
1678 if (p_data->value.len > 0 && event->value_type == GATTC_READ_VALUE_TYPE_VALUE) {
1680 data = g_malloc(3*p_data->value.len+1);
1682 BT_ERR("memory allocation failed");
1686 convert_hex_2_str((unsigned char *)p_data->value.value, p_data->value.len, data);
1687 BT_DBG("Read Data: [%s]", data);
1688 event->data_len = p_data->value.len;
1689 memcpy(&(event->data), &(p_data->value.value), event->data_len);
1693 send_event(OAL_EVENT_GATTC_READ_DESCR, event, sizeof(*event));
1696 oal_status_t gattc_start_le_discovery(int client_id)
1699 int ret = OAL_STATUS_SUCCESS;
1701 API_TRACE("BTGATT CLIENT SCAN START");
1702 CHECK_OAL_GATT_ENABLED();
1703 CHECK_CLIENT_REGISTRATION(client_id);
1705 ret = gatt_api->client->scan(client_id, 1);
1706 if (ret != BT_STATUS_SUCCESS) {
1707 BT_ERR("Error:Start LE Discovery failed: %s", status2string(ret));
1708 return convert_to_oal_status(ret);
1710 return OAL_STATUS_SUCCESS;
1713 oal_status_t gattc_stop_le_discovery(int client_id)
1716 int ret = OAL_STATUS_SUCCESS;
1718 API_TRACE("BTGATT CLIENT SCAN STOP");
1719 CHECK_OAL_GATT_ENABLED();
1720 CHECK_CLIENT_REGISTRATION(client_id);
1721 ret = gatt_api->client->scan(client_id, 0);
1722 if (ret != BT_STATUS_SUCCESS) {
1723 BT_ERR("Error:Stop LE Discovery failed: %s", status2string(ret));
1724 return convert_to_oal_status(ret);
1726 return OAL_STATUS_SUCCESS;
1729 oal_status_t gattc_set_le_scan_param(int scan_type, int itv, int win)
1733 API_TRACE("GATT client set le scan param");
1734 CHECK_OAL_GATT_ENABLED();
1737 ret = gatt_api->client->set_scan_parameters(scan_type, itv, win);
1738 if (ret != BT_STATUS_SUCCESS) {
1739 BT_ERR("Error:Set scan parameters failed: %s", status2string(ret));
1740 return convert_to_oal_status(ret);
1743 ret = gatt_api->client->set_scan_parameters(itv, win);
1744 if (ret != BT_STATUS_SUCCESS) {
1745 BT_ERR("Error:Set scan parameters failed: %s", status2string(ret));
1746 return convert_to_oal_status(ret);
1750 return OAL_STATUS_SUCCESS;
1753 static void cb_gattc_configure_mtu_cmpl(int conn_id, int status, int mtu)
1755 event_gattc_mtu_configured_t *event;
1756 BT_INFO("BTGATT Client configure mtu complete Callback, conn_id: %d, status: %d, mtu: %d",
1757 conn_id, status, mtu);
1758 event = g_new0(event_gattc_mtu_configured_t, 1);
1759 event->conn_id = conn_id;
1761 event->status = convert_to_oal_status(status);
1762 send_event(OAL_EVENT_GATTC_MTU_EXCHANGE_COMPLETED, event, sizeof(*event));
1765 /************************************ GATT Client ***********************************/
1766 oal_status_t gattc_register(oal_uuid_t* client_uuid)
1769 char str[2*BT_UUID_STRING_MAX];
1770 int ret = OAL_STATUS_SUCCESS;
1772 CHECK_OAL_GATT_ENABLED();
1773 uuid_to_stringname(client_uuid, str);
1774 API_TRACE("uuid: [%s]", str);
1775 /* We will perform actual registration in cb_gattc_register_app callback */
1776 ret = gatt_api->client->register_client((bt_uuid_t *)client_uuid);
1777 if (ret != BT_STATUS_SUCCESS) {
1778 BT_ERR("GATT client register failed: %s", status2string(ret));
1779 return convert_to_oal_status(ret);
1781 return OAL_STATUS_SUCCESS;
1784 oal_status_t gattc_deregister(int client_id)
1786 int ret = OAL_STATUS_SUCCESS;
1788 API_TRACE("GATT client deregister");
1789 CHECK_OAL_GATT_ENABLED();
1790 CHECK_CLIENT_REGISTRATION(client_id);
1792 ret = gatt_api->client->unregister_client(client_id);
1793 if (ret != BT_STATUS_SUCCESS) {
1794 BT_ERR("GATT client deregister failed: %s", status2string(ret));
1795 return convert_to_oal_status(ret);
1798 return OAL_STATUS_SUCCESS;
1801 oal_status_t gattc_connect(int client_id, bt_address_t *device_address, int isDirect)
1803 int ret = OAL_STATUS_SUCCESS;
1806 OAL_CHECK_PARAMETER(device_address, return);
1807 API_TRACE("Client Connect: [%s]", bdt_bd2str(device_address, &bdstr) + 12);
1808 CHECK_OAL_GATT_ENABLED();
1809 CHECK_CLIENT_REGISTRATION(client_id);
1811 /* Handle the actual connection in cb_gattc_connection callback */
1812 ret = gatt_api->client->connect(client_id, (bt_bdaddr_t *)device_address, isDirect);
1813 if (ret != BT_STATUS_SUCCESS) {
1814 BT_ERR("GATT client connect failed: %s", status2string(ret));
1815 return convert_to_oal_status(ret);
1817 return OAL_STATUS_SUCCESS;
1820 oal_status_t gattc_disconnect(int client_id, bt_address_t *device_address, int conn_id)
1823 int ret = OAL_STATUS_SUCCESS;
1826 OAL_CHECK_PARAMETER(device_address, return);
1827 API_TRACE("Client Disconnect: [%s]", bdt_bd2str(device_address, &bdstr) + 12);
1828 CHECK_OAL_GATT_ENABLED();
1829 CHECK_CLIENT_REGISTRATION(client_id);
1830 CHECK_CLIENT_CONNECTION(conn_id);
1832 /* Handle actual disconnection in callback */
1833 ret = gatt_api->client->disconnect(client_id, (bt_bdaddr_t *) device_address, conn_id);
1834 if (ret != BT_STATUS_SUCCESS) {
1835 BT_ERR("GATT client disconnect failed: %s", status2string(ret));
1836 return convert_to_oal_status(ret);
1838 return OAL_STATUS_SUCCESS;
1842 oal_status_t gattc_search_service(int conn_id, oal_uuid_t *service_uuid)
1844 int ret = OAL_STATUS_SUCCESS;
1845 char uuid_str[2*BT_UUID_STRING_MAX];
1848 uuid_to_stringname(service_uuid, uuid_str);
1849 API_TRACE("Client Service Search UUID: [%s]", uuid_str);
1851 API_TRACE("Client Service Search All");
1853 CHECK_OAL_GATT_ENABLED();
1854 CHECK_CLIENT_CONNECTION(conn_id);
1855 ret = gatt_api->client->search_service(conn_id, (bt_uuid_t *)service_uuid);
1857 if (ret != BT_STATUS_SUCCESS) {
1858 BT_ERR("GATT client service search failed: %s", status2string(ret));
1859 return convert_to_oal_status(ret);
1861 return OAL_STATUS_SUCCESS;
1864 oal_status_t gattc_get_characteristic(int conn_id, oal_gatt_srvc_id_t *srvc_id, oal_gatt_id_t *char_id)
1866 int ret = OAL_STATUS_SUCCESS;
1867 char uuid_str[2*BT_UUID_STRING_MAX];
1869 OAL_CHECK_PARAMETER(srvc_id, return);
1870 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str);
1871 API_TRACE("Client Get Characteristic [%s]", uuid_str);
1872 CHECK_OAL_GATT_ENABLED();
1873 CHECK_CLIENT_CONNECTION(conn_id);
1874 ret = gatt_api->client->get_characteristic(conn_id, (btgatt_srvc_id_t *)srvc_id,
1875 (btgatt_gatt_id_t *)char_id);
1876 if (ret != BT_STATUS_SUCCESS) {
1877 BT_ERR("GATT client get characteristic failed: %s", status2string(ret));
1878 return convert_to_oal_status(ret);
1880 return OAL_STATUS_SUCCESS;
1883 oal_status_t gattc_get_descriptor(int conn_id, oal_gatt_srvc_id_t *srvc_id,
1884 oal_gatt_id_t *char_id, oal_gatt_id_t *desc_id)
1886 int ret = OAL_STATUS_SUCCESS;
1887 char uuid_str1[2*BT_UUID_STRING_MAX];
1888 char uuid_str2[2*BT_UUID_STRING_MAX];
1890 OAL_CHECK_PARAMETER(srvc_id, return);
1891 OAL_CHECK_PARAMETER(char_id, return);
1892 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1893 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1894 API_TRACE("Client Get Descriptor, Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
1895 CHECK_OAL_GATT_ENABLED();
1896 CHECK_CLIENT_CONNECTION(conn_id);
1897 ret = gatt_api->client->get_descriptor(conn_id, (btgatt_srvc_id_t *)srvc_id,
1898 (btgatt_gatt_id_t *)char_id, (btgatt_gatt_id_t *)desc_id);
1899 if (ret != BT_STATUS_SUCCESS) {
1900 BT_ERR("GATT client get descriptor failed: %s", status2string(ret));
1901 return convert_to_oal_status(ret);
1903 return OAL_STATUS_SUCCESS;
1906 oal_status_t gattc_register_for_notification(int conn_id, bt_address_t * address,
1907 oal_gatt_srvc_id_t *srvc_id, oal_gatt_id_t *char_id) {
1908 int ret = OAL_STATUS_SUCCESS;
1909 char uuid_str1[2*BT_UUID_STRING_MAX];
1910 char uuid_str2[2*BT_UUID_STRING_MAX];
1913 OAL_CHECK_PARAMETER(address, return);
1914 OAL_CHECK_PARAMETER(srvc_id, return);
1915 OAL_CHECK_PARAMETER(char_id, return);
1916 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1917 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1918 API_TRACE("Client Register Notification: [%s], Service_uuid: [%s], Char_uuid: [%s]", bdt_bd2str(address, &bdstr), uuid_str1, uuid_str2);
1919 CHECK_OAL_GATT_ENABLED();
1920 CHECK_CLIENT_CONNECTION(conn_id);
1922 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);
1923 if (ret != BT_STATUS_SUCCESS) {
1924 BT_ERR("GATT client register notification failed: %s", status2string(ret));
1925 return convert_to_oal_status(ret);
1928 return OAL_STATUS_SUCCESS;
1931 oal_status_t gattc_deregister_for_notification(int conn_id, bt_address_t * address,
1932 oal_gatt_srvc_id_t *srvc_id, oal_gatt_id_t *char_id) {
1933 int ret = OAL_STATUS_SUCCESS;
1934 char uuid_str1[2*BT_UUID_STRING_MAX];
1935 char uuid_str2[2*BT_UUID_STRING_MAX];
1938 OAL_CHECK_PARAMETER(address, return);
1939 OAL_CHECK_PARAMETER(srvc_id, return);
1940 OAL_CHECK_PARAMETER(char_id, return);
1941 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1942 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1943 API_TRACE("Client Deregister Notification: [%s], Service_uuid: [%s], Char_uuid: [%s]", bdt_bd2str(address, &bdstr), uuid_str1, uuid_str2);
1944 CHECK_OAL_GATT_ENABLED();
1945 CHECK_CLIENT_CONNECTION(conn_id);
1947 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);
1948 if (ret != BT_STATUS_SUCCESS) {
1949 BT_ERR("GATT client deregister notification failed: %s", status2string(ret));
1950 return convert_to_oal_status(ret);
1953 return OAL_STATUS_SUCCESS;
1957 oal_status_t gattc_read_characteristic(int conn_id, oal_gatt_srvc_id_t *srvc_id,
1958 oal_gatt_id_t *char_id, oal_gatt_auth_req_t auth_req)
1960 int ret = OAL_STATUS_SUCCESS;
1961 char uuid_str1[2*BT_UUID_STRING_MAX];
1962 char uuid_str2[2*BT_UUID_STRING_MAX];
1964 OAL_CHECK_PARAMETER(srvc_id, return);
1965 OAL_CHECK_PARAMETER(char_id, return);
1966 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1967 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1968 API_TRACE("Client Read Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
1969 CHECK_OAL_GATT_ENABLED();
1970 CHECK_CLIENT_CONNECTION(conn_id);
1972 ret = gatt_api->client->read_characteristic(conn_id, (btgatt_srvc_id_t *)srvc_id,
1973 (btgatt_gatt_id_t *)char_id, auth_req);
1974 if (ret != BT_STATUS_SUCCESS) {
1975 BT_ERR("GATT client read characteristic failed: %s", status2string(ret));
1976 return convert_to_oal_status(ret);
1979 return OAL_STATUS_SUCCESS;
1982 oal_status_t gattc_read_descriptor(int conn_id, oal_gatt_srvc_id_t *srvc_id,
1983 oal_gatt_id_t *char_id, oal_gatt_id_t *desc_id, oal_gatt_auth_req_t auth_req)
1985 int ret = OAL_STATUS_SUCCESS;
1986 char uuid_str1[2*BT_UUID_STRING_MAX];
1987 char uuid_str2[2*BT_UUID_STRING_MAX];
1988 char uuid_str3[2*BT_UUID_STRING_MAX];
1990 OAL_CHECK_PARAMETER(srvc_id, return);
1991 OAL_CHECK_PARAMETER(char_id, return);
1992 OAL_CHECK_PARAMETER(desc_id, return);
1993 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1994 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1995 uuid_to_stringname(&(desc_id->uuid), uuid_str3);
1996 API_TRACE("Client Read Descriptor: Service_uuid: [%s], Char_uuid: [%s], Desc_uuid: [%s]",
1997 uuid_str1, uuid_str2, uuid_str3);
1998 CHECK_OAL_GATT_ENABLED();
1999 CHECK_CLIENT_CONNECTION(conn_id);
2001 ret = gatt_api->client->read_descriptor(conn_id, (btgatt_srvc_id_t *)srvc_id,
2002 (btgatt_gatt_id_t *)char_id, (btgatt_gatt_id_t *)desc_id, auth_req);
2003 if (ret != BT_STATUS_SUCCESS) {
2004 BT_ERR("GATT client read descriptor failed: %s", status2string(ret));
2005 return convert_to_oal_status(ret);
2008 return OAL_STATUS_SUCCESS;
2011 oal_status_t gattc_acquire_notify(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2012 oal_gatt_id_t *char_id, int *fd, int *mtu)
2014 int ret = OAL_STATUS_SUCCESS;
2015 char uuid_str1[2*BT_UUID_STRING_MAX];
2016 char uuid_str2[2*BT_UUID_STRING_MAX];
2017 OAL_CHECK_PARAMETER(srvc_id, return);
2018 OAL_CHECK_PARAMETER(char_id, return);
2019 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2020 uuid_to_stringname(&(char_id->uuid), uuid_str2);
2021 API_TRACE("Client Write Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
2022 CHECK_OAL_GATT_ENABLED();
2023 CHECK_CLIENT_CONNECTION(conn_id);
2025 ret = gatt_api->client->acquire_notify(conn_id, (btgatt_srvc_id_t *)srvc_id,
2026 (btgatt_gatt_id_t *)char_id, fd, mtu);
2027 if (ret != BT_STATUS_SUCCESS) {
2028 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
2029 return convert_to_oal_status(ret);
2032 return OAL_STATUS_SUCCESS;
2035 oal_status_t gattc_acquire_write(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2036 oal_gatt_id_t *char_id, oal_gatt_auth_req_t auth_req , int *fd, int*mtu)
2038 int ret = OAL_STATUS_SUCCESS;
2039 char uuid_str1[2*BT_UUID_STRING_MAX];
2040 char uuid_str2[2*BT_UUID_STRING_MAX];
2041 OAL_CHECK_PARAMETER(srvc_id, return);
2042 OAL_CHECK_PARAMETER(char_id, return);
2043 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2044 uuid_to_stringname(&(char_id->uuid), uuid_str2);
2045 API_TRACE("Client Write Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
2046 CHECK_OAL_GATT_ENABLED();
2047 CHECK_CLIENT_CONNECTION(conn_id);
2049 ret = gatt_api->client->acquire_write(conn_id, (btgatt_srvc_id_t *)srvc_id,
2050 (btgatt_gatt_id_t *)char_id, auth_req, fd, mtu);
2051 if (ret != BT_STATUS_SUCCESS) {
2052 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
2053 return convert_to_oal_status(ret);
2056 return OAL_STATUS_SUCCESS;
2059 oal_status_t gattc_write_characteristic(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2060 oal_gatt_id_t *char_id, oal_gatt_write_type_t write_type,
2061 int len, oal_gatt_auth_req_t auth_req, char* data)
2063 int ret = OAL_STATUS_SUCCESS;
2064 char uuid_str1[2*BT_UUID_STRING_MAX];
2065 char uuid_str2[2*BT_UUID_STRING_MAX];
2066 OAL_CHECK_PARAMETER(srvc_id, return);
2067 OAL_CHECK_PARAMETER(char_id, return);
2068 OAL_CHECK_PARAMETER(data, return);
2069 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2070 uuid_to_stringname(&(char_id->uuid), uuid_str2);
2071 API_TRACE("Client Write Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
2072 CHECK_OAL_GATT_ENABLED();
2073 CHECK_CLIENT_CONNECTION(conn_id);
2075 ret = gatt_api->client->write_characteristic(conn_id, (btgatt_srvc_id_t *)srvc_id,
2076 (btgatt_gatt_id_t *)char_id, write_type, len, auth_req, data);
2077 if (ret != BT_STATUS_SUCCESS) {
2078 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
2079 return convert_to_oal_status(ret);
2082 return OAL_STATUS_SUCCESS;
2085 oal_status_t gattc_write_descriptor(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2086 oal_gatt_id_t *char_id, oal_gatt_id_t *desc_id,
2087 oal_gatt_write_type_t write_type, int len, oal_gatt_auth_req_t auth_req, char* data)
2089 int ret = OAL_STATUS_SUCCESS;
2090 char uuid_str1[2*BT_UUID_STRING_MAX];
2091 char uuid_str2[2*BT_UUID_STRING_MAX];
2092 char uuid_str3[2*BT_UUID_STRING_MAX];
2093 OAL_CHECK_PARAMETER(srvc_id, return);
2094 OAL_CHECK_PARAMETER(char_id, return);
2095 OAL_CHECK_PARAMETER(desc_id, return);
2096 OAL_CHECK_PARAMETER(data, return);
2097 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2098 uuid_to_stringname(&(char_id->uuid), uuid_str2);
2099 uuid_to_stringname(&(desc_id->uuid), uuid_str3);
2100 API_TRACE("Client Write Descriptor: Service_uuid: [%s], Char_uuid: [%s], Desc_uuid: [%s]",
2101 uuid_str1, uuid_str2, uuid_str3);
2102 CHECK_OAL_GATT_ENABLED();
2103 CHECK_CLIENT_CONNECTION(conn_id);
2105 ret = gatt_api->client->write_descriptor(conn_id, (btgatt_srvc_id_t *)srvc_id,
2106 (btgatt_gatt_id_t *)char_id, (btgatt_gatt_id_t *)desc_id, write_type, len, auth_req, data);
2107 if (ret != BT_STATUS_SUCCESS) {
2108 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
2109 return convert_to_oal_status(ret);
2112 return OAL_STATUS_SUCCESS;
2115 oal_status_t gattc_conn_param_update(bt_address_t * address, int min, int max, int latency, int timeout)
2120 OAL_CHECK_PARAMETER(address, return);
2121 CHECK_OAL_GATT_ENABLED();
2123 BT_DBG("[%s] min[%d] max[%d] latency[%d] timeout[%d]", bdt_bd2str(address, &bdstr), min, max, latency, timeout);
2124 ret = gatt_api->client->conn_parameter_update((bt_bdaddr_t *)address, min, max, latency, timeout);
2125 if (ret != BT_STATUS_SUCCESS) {
2126 BT_ERR("error: %s", status2string(ret));
2127 return convert_to_oal_status(ret);
2129 return OAL_STATUS_SUCCESS;
2132 oal_status_t gattc_unregister_scan_filter(int slot_id)
2138 CHECK_OAL_GATT_ENABLED();
2140 BT_INFO("Remove Scan filter Slot ID is: [%d]", slot_id);
2142 ret = gatt_api->client->scan_filter_clear(client_if, slot_id);
2143 if (ret != BT_STATUS_SUCCESS) {
2144 BT_ERR("error: %s", status2string(ret));
2145 return convert_to_oal_status(ret);
2147 return OAL_STATUS_SUCCESS;
2150 oal_status_t gattc_enable_scan_filter(int client_if)
2155 CHECK_OAL_GATT_ENABLED();
2157 BT_INFO("Enable Scan filter. Client If: %d", client_if);
2159 ret = gatt_api->client->scan_filter_enable(client_if, TRUE);
2160 if (ret != BT_STATUS_SUCCESS) {
2161 BT_ERR("error: %s", status2string(ret));
2162 return convert_to_oal_status(ret);
2164 return OAL_STATUS_SUCCESS;
2167 oal_status_t gattc_disable_scan_filter(int client_if)
2172 CHECK_OAL_GATT_ENABLED();
2174 BT_INFO("Disable Scan filter. Client If: %d", client_if);
2176 ret = gatt_api->client->scan_filter_enable(client_if, FALSE);
2177 if (ret != BT_STATUS_SUCCESS) {
2178 BT_ERR("error: %s", status2string(ret));
2179 return convert_to_oal_status(ret);
2181 return OAL_STATUS_SUCCESS;
2184 oal_status_t gattc_register_scan_filter(oal_ble_scan_filter_t* filter_data)
2187 int client_info = 0;
2190 int company_id_mask = 0;
2191 int address_type = 0;
2192 int feature_selection = 0;
2194 oal_ble_scan_filter_param_setup_t scan_filter_setup = {.list_logic_type = 0,
2195 .filt_logic_type = 1,
2196 .rssi_high_thres = -127,
2197 .rssi_low_thres = -127,
2201 .found_timeout_cnt = 0
2204 OAL_CHECK_PARAMETER(filter_data, return);
2206 CHECK_OAL_GATT_ENABLED();
2208 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS){
2210 BT_INFO("OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS is being added");
2211 BT_INFO("BT remote Device address is %s", bdt_bd2str(filter_data->device_address, &bdstr));
2212 ret = gatt_api->client->scan_filter_add_remove(client_info,
2214 OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS,
2215 filter_data->slot_id,
2220 (bt_bdaddr_t*)filter_data->device_address,
2227 if (ret != BT_STATUS_SUCCESS){
2228 BT_ERR("error: %s", status2string(ret));
2229 BT_INFO("unregistering already set filter features.");
2230 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2231 return convert_to_oal_status(ret);
2234 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS;
2236 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_NAME){
2237 ret = gatt_api->client->scan_filter_add_remove(client_info,
2239 OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_NAME,
2240 filter_data->slot_id,
2248 filter_data->device_name, // device_name as p_data in HAL
2252 if (ret != BT_STATUS_SUCCESS){
2253 BT_ERR("error: %s", status2string(ret));
2254 BT_INFO("unregistering already set filter features.");
2255 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2256 return convert_to_oal_status(ret);
2259 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_NAME;
2261 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_UUID){
2262 BT_INFO("OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_UUID is being added");
2263 char uuid_str1[2*BT_UUID_STRING_MAX];
2264 char uuid_str2[2*BT_UUID_STRING_MAX];
2266 uuid_to_stringname((service_uuid_t*)filter_data->service_uuid, uuid_str1);
2267 uuid_to_stringname((service_uuid_t*)filter_data->service_uuid_mask, uuid_str2);
2269 BT_INFO("Service UUID is [%s] and Service UUID mask is [%s]", uuid_str1, uuid_str2);
2270 ret = gatt_api->client->scan_filter_add_remove(client_info,
2272 OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_UUID,
2273 filter_data->slot_id,
2276 (bt_uuid_t*)filter_data->service_uuid,
2277 (bt_uuid_t*)filter_data->service_uuid_mask,
2280 filter_data->service_uuid_len, // service_uuid_len as data_len in HAL
2282 filter_data->service_uuid_mask_len,
2285 if (ret != BT_STATUS_SUCCESS){
2286 BT_ERR("error: %s", status2string(ret));
2287 BT_INFO("unregistering already set filter features.");
2288 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2289 return convert_to_oal_status(ret);
2292 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_UUID;
2294 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID){
2295 BT_INFO("OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID is being added");
2296 char uuid_str1[2*BT_UUID_STRING_MAX];
2297 char uuid_str2[2*BT_UUID_STRING_MAX];
2298 uuid_to_stringname((service_uuid_t*)filter_data->service_solicitation_uuid, uuid_str1);
2299 uuid_to_stringname((service_uuid_t*)filter_data->service_solicitation_uuid_mask, uuid_str2);
2300 BT_INFO("Service Solicitation UUID is [%s] and Service Solicitation UUID mask is [%s]", uuid_str1, uuid_str2);
2301 ret = gatt_api->client->scan_filter_add_remove(client_info,
2303 OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID,
2304 filter_data->slot_id,
2307 (bt_uuid_t*)filter_data->service_solicitation_uuid,
2308 (bt_uuid_t*)filter_data->service_solicitation_uuid_mask,
2311 filter_data->service_solicitation_uuid_len, // service_solicitation_uuid_len as data_len in HAL
2313 filter_data->service_solicitation_uuid_mask_len,
2316 if (ret != BT_STATUS_SUCCESS){
2317 BT_ERR("error: %s", status2string(ret));
2318 BT_INFO("unregistering already set filter features.");
2319 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2320 return convert_to_oal_status(ret);
2323 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID;
2325 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_DATA){
2326 ret = gatt_api->client->scan_filter_add_remove(client_info,
2328 OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_DATA,
2329 filter_data->slot_id,
2336 filter_data->service_data_len, //service_data_len as data_len in HAL
2337 (char*)filter_data->service_data,
2338 filter_data->service_data_mask_len,
2339 (char*)filter_data->service_data_mask
2341 if (ret != BT_STATUS_SUCCESS){
2342 BT_ERR("error: %s", status2string(ret));
2343 BT_INFO("unregistering already set filter features.");
2344 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2345 return convert_to_oal_status(ret);
2348 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_DATA;
2350 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA){
2351 ret = gatt_api->client->scan_filter_add_remove(client_info,
2353 OAL_BLE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA,
2354 filter_data->slot_id,
2355 filter_data->manufacturer_id,
2361 filter_data->manufacturer_data_len, //manufacturer_data_len as data_len in HAL
2362 (char*)filter_data->manufacturer_data,
2363 filter_data->manufacturer_data_mask_len,
2364 (char*)filter_data->manufacturer_data_mask
2366 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA;
2367 if (ret != BT_STATUS_SUCCESS){
2368 BT_ERR("error: %s", status2string(ret));
2369 BT_INFO("unregistering already set filter features.");
2370 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2371 return convert_to_oal_status(ret);
2374 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA;
2377 BT_DBG("Filter selection 0x%.2x", feature_selection);
2379 ret = gatt_api->client->scan_filter_param_setup(client_info, action, filter_data->slot_id, feature_selection,
2380 scan_filter_setup.list_logic_type, scan_filter_setup.filt_logic_type,
2381 scan_filter_setup.rssi_high_thres, scan_filter_setup.rssi_low_thres,
2382 scan_filter_setup.dely_mode, scan_filter_setup.found_timeout,
2383 scan_filter_setup.lost_timeout, scan_filter_setup.found_timeout_cnt);
2384 if (ret != BT_STATUS_SUCCESS){
2385 BT_ERR("error: %s", status2string(ret));
2386 return convert_to_oal_status(ret);
2388 return OAL_STATUS_SUCCESS;
2391 oal_status_t gattc_get_att_mtu(int conn_id, int *mtu)
2393 int ret = OAL_STATUS_SUCCESS;
2394 API_TRACE("Get ATT MTU, conn_id: %d", conn_id);
2395 CHECK_OAL_GATT_ENABLED();
2396 CHECK_CLIENT_CONNECTION(conn_id);
2397 OAL_CHECK_PARAMETER(mtu, return);
2399 /* To prevent crash in case other libraries not support this api */
2400 if (gatt_api->client->get_att_mtu == NULL) {
2401 BT_WARN("get_att_mtu is NULL");
2402 return OAL_STATUS_NOT_SUPPORT;
2405 ret = gatt_api->client->get_att_mtu(conn_id, mtu);
2406 if (ret != BT_STATUS_SUCCESS) {
2407 BT_ERR("GATT MTU Size failed, status: %s", status2string(ret));
2408 return convert_to_oal_status(ret);
2411 BT_INFO("Current ATT MTU Size: %d", *mtu);
2412 return OAL_STATUS_SUCCESS;
2415 oal_status_t gattc_configure_mtu(int conn_id, int mtu)
2419 API_TRACE("Configure MTU Size: [%d]", mtu);
2420 CHECK_OAL_GATT_ENABLED();
2421 CHECK_CLIENT_CONNECTION(conn_id);
2423 /* To prevent crash in case other libraries not support this api */
2424 if (gatt_api->client->configure_mtu == NULL) {
2425 BT_WARN("configure_mtu is NULL");
2426 return OAL_STATUS_NOT_SUPPORT;
2429 ret = gatt_api->client->configure_mtu(conn_id, mtu);
2430 if (ret != BT_STATUS_SUCCESS) {
2431 BT_ERR("Gatt client configure_mtu error: %s", status2string(ret));
2432 return convert_to_oal_status(ret);
2435 return OAL_STATUS_SUCCESS;
2438 oal_status_t gatt_get_data_batching_available_packets(
2439 unsigned int *available_packets)
2443 ret = gatt_api->client->get_data_batching_available_packets(available_packets);
2444 if (ret != BT_STATUS_SUCCESS) {
2445 BT_ERR("GATT data batching failed: %s", status2string(ret));
2446 return convert_to_oal_status(ret);
2449 return OAL_STATUS_SUCCESS;
2452 oal_status_t gatt_enable_data_batching(bt_address_t * address,
2453 int packet_threshold, int timeout)
2457 ret = gatt_api->client->enable_data_batching((bt_bdaddr_t *)address, packet_threshold, timeout);
2458 if (ret != BT_STATUS_SUCCESS) {
2459 BT_ERR("GATT data batching failed: %s", status2string(ret));
2460 return convert_to_oal_status(ret);
2463 return OAL_STATUS_SUCCESS;
2466 oal_status_t gatt_disable_data_batching(bt_address_t * address)
2470 ret = gatt_api->client->disable_data_batching((bt_bdaddr_t *)address);
2471 if (ret != BT_STATUS_SUCCESS) {
2472 BT_ERR("GATT data batching failed: %s", status2string(ret));
2473 return convert_to_oal_status(ret);
2476 return OAL_STATUS_SUCCESS;
2479 oal_status_t gattc_add_connection_info(bt_address_t *device_address, int conn_id, int instance_id)
2481 int ret = OAL_STATUS_SUCCESS;
2484 OAL_CHECK_PARAMETER(device_address, return);
2485 API_TRACE("Add connection info: [%s]", bdt_bd2str(device_address, &bdstr) + 12);
2486 CHECK_OAL_GATT_ENABLED();
2488 ret = gatt_api->client->add_connection_info((bt_bdaddr_t *)device_address, conn_id, instance_id);
2489 if (ret != BT_STATUS_SUCCESS) {
2490 BT_ERR("GATT connection info add failed: %s", status2string(ret));
2491 return convert_to_oal_status(ret);
2493 return OAL_STATUS_SUCCESS;