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;
566 adv_setup.appearance = adv_param_setup->appearance;
567 adv_setup.manufacturer_data = adv_param_setup->manufacturer_data;
568 adv_setup.manufacturer_data_len = adv_param_setup->manufacturer_data_len;
569 adv_setup.service_data = adv_param_setup->service_data;
570 adv_setup.service_data_len = adv_param_setup->service_data_len;
571 adv_setup.service_uuid = adv_param_setup->service_uuid;
572 adv_setup.service_uuid_len = adv_param_setup->service_uuid_len;
573 /* Solicit UUID handler: Start */
574 adv_setup.solicit_uuid = adv_param_setup->solicit_uuid;
575 adv_setup.solicit_uuid_len = adv_param_setup->solicit_uuid_len;
576 /* Solicit UUID handler: End */
577 adv_setup.min_interval = adv_param_setup->min_interval;
578 adv_setup.max_interval = adv_param_setup->min_interval;
579 adv_setup.adv_type = adv_param_setup->adv_type;
580 adv_setup.chnl_map = adv_param_setup->chnl_map;
581 adv_setup.tx_power = adv_param_setup->tx_power;
582 adv_setup.timeout_s = adv_param_setup->timeout_s;
584 adv_setup.server_if = instance_id;
586 ret = gatt_api->server->multi_adv_set_inst_data(adv_setup);
587 if (ret != BT_STATUS_SUCCESS) {
588 BT_ERR("GATT Set Multi advertising data failed: %s", status2string(ret));
589 info->state = GATT_INS_DISABLED;
590 return convert_to_oal_status(ret);
592 return OAL_STATUS_SUCCESS;
595 oal_status_t gatts_disconnect(int instance_id, bt_address_t *device_address, int conn_id)
597 int ret = OAL_STATUS_SUCCESS;
600 API_TRACE("Server Disonnect: instance_id = %d, conn_id = %d", instance_id, conn_id);
602 CHECK_OAL_GATT_ENABLED();
603 CHECK_SERVER_INSTANCE(instance_id);
604 if (__gatts_find_server_instance(instance_id) == NULL)
605 return OAL_STATUS_INTERNAL_ERROR;
607 API_TRACE("[%s]", bdt_bd2str(device_address, &bdstr));
609 ret = gatt_api->server->disconnect(instance_id, (bt_bdaddr_t *) device_address, conn_id);
610 if (ret != BT_STATUS_SUCCESS) {
611 BT_ERR("GATT Server disconnect failed: %s", status2string(ret));
612 return convert_to_oal_status(ret);
614 return OAL_STATUS_SUCCESS;
618 /************************************GATT Server Functions*************************************/
620 static void cb_gatts_register_app(int status, int server_if, bt_uuid_t *uuid)
622 char str[2*BT_UUID_STRING_MAX];
623 uuid_to_stringname((service_uuid_t*)uuid, str);
625 event_gatts_register_t *event = g_malloc0(sizeof(event_gatts_register_t));
627 BT_INFO("BTGATT SERVER REGISTER APP CB, UUID:%s, status:%d, server_if:%d",
628 str, status, server_if);
630 gatt_server_t *info = g_malloc0(sizeof(gatt_server_t));
631 info->instance_id = server_if;
632 memcpy(info->uuid.uuid, uuid->uu, sizeof(bt_uuid_t));
633 gatt_servers = g_slist_append(gatt_servers, info);
634 BT_DBG("GATT Server Added. count of gatt_servers: %d", g_slist_length(gatt_servers));
636 event->server_inst = server_if;
637 memcpy(event->server_uuid.uuid, uuid->uu, sizeof(bt_uuid_t));
638 send_event(OAL_EVENT_BLE_SERVER_INSTANCE_INITIALISED, event, sizeof(event_gatts_register_t));
641 static void cb_gatts_listen(int status, int server_if)
644 gboolean new_state = FALSE;
645 event_ble_adv_status *event = g_malloc0(sizeof(event_ble_adv_status));
648 for (l = gatt_servers; l; l = g_slist_next(l)) {
649 gatt_server_t *info = l->data;
652 if (info->instance_id == server_if) {
653 event->server_inst = server_if;
654 prev_state = info->cur_adv_state;
655 new_state = (status == BT_STATUS_SUCCESS) ? !prev_state : prev_state;
656 info->cur_adv_state = new_state;
661 BT_INFO("Adv State of server instance %d, new_state: %d", server_if, new_state);
663 if (TRUE == new_state)
664 send_event(OAL_EVENT_BLE_ADVERTISING_STARTED, event, sizeof(event_ble_adv_status));
666 send_event(OAL_EVENT_BLE_ADVERTISING_STOPPED, event, sizeof(event_ble_adv_status));
669 static void cb_gatts_multi_adv_enable(int server_if, int status)
672 BT_INFO("BTGATT SERVER MULTI ADV ENABLE CB, status:%d, srv_if: %d", status, server_if);
673 event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
676 if (status != BT_STATUS_SUCCESS)
677 BT_ERR("MULTI ADV ENABLE failed, status:%d, srv_if: %d", status, server_if);
679 for (l = gatt_servers; l; l = g_slist_next(l)) {
680 gatt_server_t *info = l->data;
683 if (info->instance_id == server_if) {
684 event->server_inst = server_if;
685 info->state = GATT_INS_ENABLED;
689 event->server_inst = -1;
691 event->status = convert_to_oal_status(status);
692 send_event(OAL_EVENT_BLE_MULTI_ADVERTISING_ENABLE, event, sizeof(event_ble_multiadv_status));
695 static void cb_gatts_multi_adv_disable(int server_if, int status)
697 BT_INFO("BTGATT SERVER MULTI ADV DISABLE CB, status:%d, srv_if: %d", status, server_if);
698 event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
701 if (status != BT_STATUS_SUCCESS)
702 BT_ERR("MULTI ADV DISABLE failed, status:%d, srv_if: %d", status, server_if);
704 for (l = gatt_servers; l; l = g_slist_next(l)) {
705 gatt_server_t *info = l->data;
708 if (info->instance_id == server_if) {
709 event->server_inst = server_if;
710 info->state = GATT_INS_DISABLED;
714 event->server_inst = -1;
716 event->status = convert_to_oal_status(status);
717 send_event(OAL_EVENT_BLE_MULTI_ADVERTISING_DISABLE, event, sizeof(event_ble_multiadv_status));
720 static void cb_gatts_multi_adv_update(int server_if, int status)
722 BT_INFO("BTGATT SERVER MULTI ADV UPDATE CB, status:%d, srv_if: %d", status, server_if);
723 event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
726 if (status != BT_STATUS_SUCCESS)
727 BT_ERR("MULTI ADV UPDATE failed, status:%d, srv_if: %d", status, server_if);
729 for (l = gatt_servers; l; l = g_slist_next(l)) {
730 gatt_server_t *info = l->data;
733 if (info->instance_id == server_if) {
734 event->server_inst = server_if;
738 event->server_inst = -1;
740 event->status = convert_to_oal_status(status);
741 send_event(OAL_EVENT_BLE_MULTI_ADVERTISING_UPDATE, event, sizeof(event_ble_multiadv_status));
744 static void cb_gatts_multi_adv_set_inst_data(int server_if, int status)
746 BT_INFO("BTGATT SERVER MULTI ADV SET INST DATA CB, status:%d, srv_if: %d", status, server_if);
747 event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
750 if (status != BT_STATUS_SUCCESS)
751 BT_ERR("MULTI ADV SET DATA failed, status:%d, srv_if: %d", status, server_if);
753 for (l = gatt_servers; l; l = g_slist_next(l)) {
754 gatt_server_t *info = l->data;
757 if (info->instance_id == server_if) {
758 event->server_inst = server_if;
759 info->state = GATT_INS_DATA_SET;
763 event->server_inst = -1;
765 event->status = convert_to_oal_status(status);
766 send_event(OAL_EVENT_BLE_MULTI_ADVERTISING_SET_INST_DATA, event, sizeof(event_ble_multiadv_status));
769 oal_status_t gatts_get_att_mtu(int conn_id, int *mtu)
771 int ret = OAL_STATUS_SUCCESS;
772 API_TRACE("Get ATT MTU, conn_id: %d", conn_id);
773 CHECK_OAL_GATT_ENABLED();
774 OAL_CHECK_PARAMETER(mtu, return);
776 /* To prevent crash in case other libraries not support this api */
777 if (gatt_api->server->get_att_mtu == NULL) {
778 BT_WARN("get_att_mtu is NULL");
779 return OAL_STATUS_NOT_SUPPORT;
782 ret = gatt_api->server->get_att_mtu(conn_id, mtu);
783 if (ret != BT_STATUS_SUCCESS) {
784 BT_ERR("GATT MTU Size failed, status: %s", status2string(ret));
785 return convert_to_oal_status(ret);
788 BT_INFO("Current ATT MTU Size: %d", *mtu);
789 return OAL_STATUS_SUCCESS;
791 oal_status_t gatt_send_response_acquire(int conn_id, int trans_id,
792 int status, int fd, int mtu , void * fdlist)
794 int ret = OAL_STATUS_SUCCESS;
796 API_TRACE("Server Send Response : ConnId = %d, TransId = %d, %d", conn_id, trans_id, fd);
797 CHECK_OAL_GATT_ENABLED();
799 ret = gatt_api->server->send_response_acquire(conn_id, trans_id, status, fd, mtu, fdlist);
800 if (ret != BT_STATUS_SUCCESS) {
801 BT_ERR("GATT Server Send Response failed: %s", status2string(ret));
802 return convert_to_oal_status(ret);
804 return OAL_STATUS_SUCCESS;
807 oal_status_t gatts_add_service(int instance_id, oal_gatt_srvc_id_t *gatt_serv_id,
810 int ret = OAL_STATUS_SUCCESS;
811 btgatt_srvc_id_t btgatt_srvc_id;
812 char str[2*BT_UUID_STRING_MAX];
814 API_TRACE("Server Add Service : instance_id = %d, num_handles = %d", instance_id, num_handles);
815 CHECK_OAL_GATT_ENABLED();
817 CHECK_SERVER_INSTANCE(instance_id);
818 if (__gatts_find_server_instance(instance_id) == NULL)
819 return OAL_STATUS_INTERNAL_ERROR;
821 if (gatt_serv_id != NULL) {
822 uuid_to_stringname(&(gatt_serv_id->id.uuid), str);
823 API_TRACE("Service uuid: [%s]", str);
824 memcpy(btgatt_srvc_id.id.uuid.uu, gatt_serv_id->id.uuid.uuid, sizeof(gatt_serv_id->id.uuid));
825 btgatt_srvc_id.id.inst_id = gatt_serv_id->id.inst_id;
826 btgatt_srvc_id.is_primary = gatt_serv_id->is_prmry;
828 BT_INFO("GATT Server Service Id is NULL");
829 return OAL_STATUS_INVALID_PARAM;
832 ret = gatt_api->server->add_service(instance_id, &btgatt_srvc_id, num_handles);
833 if (ret != BT_STATUS_SUCCESS) {
834 BT_ERR("GATT Server add service failed: %s", status2string(ret));
835 return convert_to_oal_status(ret);
837 return OAL_STATUS_SUCCESS;
840 oal_status_t gatts_add_included_services(int instance_id, int serv_handle,
843 int ret = OAL_STATUS_SUCCESS;
844 API_TRACE("Server Add included Service : instance_id = %d, serv_handle = %d, incl_handle = %d",
845 instance_id, serv_handle, incl_handle);
846 CHECK_OAL_GATT_ENABLED();
848 CHECK_SERVER_INSTANCE(instance_id);
849 if (__gatts_find_server_instance(instance_id) == NULL)
850 return OAL_STATUS_INTERNAL_ERROR;
852 ret = gatt_api->server->add_included_service(instance_id, serv_handle, incl_handle);
853 if (ret != BT_STATUS_SUCCESS) {
854 BT_ERR("GATT Server Add included Service failed: %s", status2string(ret));
855 return convert_to_oal_status(ret);
857 return OAL_STATUS_SUCCESS;
860 oal_status_t gatts_add_characteristics(int instance_id, int serv_handle,
861 oal_uuid_t* charc_uuid, int propts, int permsn)
863 int ret = OAL_STATUS_SUCCESS;
864 char str[2*BT_UUID_STRING_MAX];
865 char prop[MAX_PROP_LEN], perm[MAX_PERM_LEN];
866 char_prop_to_string(propts, prop);
867 char_perm_to_string(permsn, perm);
868 API_TRACE("Server Add Characteristic : instance_id = %d, serv_handle = %d, Properties =%s, Permisison = %s",
869 instance_id, serv_handle, prop, perm);
870 CHECK_OAL_GATT_ENABLED();
872 CHECK_SERVER_INSTANCE(instance_id);
873 if (__gatts_find_server_instance(instance_id) == NULL)
874 return OAL_STATUS_INTERNAL_ERROR;
876 if (charc_uuid != NULL) {
877 uuid_to_stringname(charc_uuid, str);
878 API_TRACE("uuid: [%s]", str);
881 ret = gatt_api->server->add_characteristic(instance_id,
882 serv_handle, (bt_uuid_t*)charc_uuid, propts, permsn);
883 if (ret != BT_STATUS_SUCCESS) {
884 BT_ERR("GATT Server Add Characteristic failed: %s", status2string(ret));
885 return convert_to_oal_status(ret);
887 return OAL_STATUS_SUCCESS;
890 oal_status_t gatts_add_descriptor(int instance_id, int serv_handle,
891 oal_uuid_t* desc_uuid, int permsn)
893 int ret = OAL_STATUS_SUCCESS;
894 char str[2*BT_UUID_STRING_MAX];
895 char perm[MAX_PERM_LEN];
896 char_perm_to_string(permsn, perm);
897 API_TRACE("Server Add Descriptor : instance_id = %d, serv_handle = %d, Permisison = %s",
898 instance_id, serv_handle, perm);
899 CHECK_OAL_GATT_ENABLED();
901 CHECK_SERVER_INSTANCE(instance_id);
902 if (__gatts_find_server_instance(instance_id) == NULL)
903 return OAL_STATUS_INTERNAL_ERROR;
905 if (desc_uuid != NULL) {
906 uuid_to_stringname(desc_uuid, str);
907 API_TRACE("uuid: [%s]", str);
910 ret = gatt_api->server->add_descriptor(instance_id,
911 serv_handle, (bt_uuid_t*)desc_uuid, permsn);
912 if (ret != BT_STATUS_SUCCESS) {
913 BT_ERR("GATT Server Add Descriptor failed: %s", status2string(ret));
914 return convert_to_oal_status(ret);
916 return OAL_STATUS_SUCCESS;
919 oal_status_t gatts_start_service(int instance_id, int svc_handle, int transport)
921 int ret = OAL_STATUS_SUCCESS;
923 API_TRACE("Server Start Service : Ins_id = %d, srvHndl = %d, transport = %d",
924 instance_id, svc_handle, transport);
925 CHECK_OAL_GATT_ENABLED();
927 CHECK_SERVER_INSTANCE(instance_id);
928 if (__gatts_find_server_instance(instance_id) == NULL)
929 return OAL_STATUS_INTERNAL_ERROR;
931 ret = gatt_api->server->start_service(instance_id, svc_handle, transport);
932 if (ret != BT_STATUS_SUCCESS) {
933 BT_ERR("GATT Server Start Service failed: %s", status2string(ret));
934 return convert_to_oal_status(ret);
936 return OAL_STATUS_SUCCESS;
939 oal_status_t gatts_stop_service(int instance_id, int srv_hdl)
941 int ret = OAL_STATUS_SUCCESS;
943 API_TRACE("Server Stop Service : Ins_id = %d, srvHndl = %d, ", instance_id, srv_hdl);
944 CHECK_OAL_GATT_ENABLED();
946 CHECK_SERVER_INSTANCE(instance_id);
947 if (__gatts_find_server_instance(instance_id) == NULL)
948 return OAL_STATUS_INTERNAL_ERROR;
950 ret = gatt_api->server->stop_service(instance_id, srv_hdl);
951 if (ret != BT_STATUS_SUCCESS) {
952 BT_ERR("GATT Server Stop Service failed: %s", status2string(ret));
953 return convert_to_oal_status(ret);
955 return OAL_STATUS_SUCCESS;
958 oal_status_t gatts_delete_service(int instance_id, int srv_hdl)
960 int ret = OAL_STATUS_SUCCESS;
962 API_TRACE("Server Delete Service : Ins_id = %d, srvHndl = %d, ", instance_id, srv_hdl);
963 CHECK_OAL_GATT_ENABLED();
965 CHECK_SERVER_INSTANCE(instance_id);
966 if (__gatts_find_server_instance(instance_id) == NULL)
967 return OAL_STATUS_INTERNAL_ERROR;
969 ret = gatt_api->server->delete_service(instance_id, srv_hdl);
970 if (ret != BT_STATUS_SUCCESS) {
971 BT_ERR("GATT Server Delete Service failed: %s", status2string(ret));
972 return convert_to_oal_status(ret);
974 return OAL_STATUS_SUCCESS;
977 oal_status_t gatts_send_response(int conn_id, int trans_id, int resp_status, oal_gatt_response_t *response)
979 int ret = OAL_STATUS_SUCCESS;
981 API_TRACE("Server Send Response : ConnId = %d, TransId = %d, ", conn_id, trans_id);
982 CHECK_OAL_GATT_ENABLED();
984 if (response == NULL) {
985 BT_ERR("GATT Server attribute value is empty");
986 return OAL_STATUS_INVALID_PARAM;
989 ret = gatt_api->server->send_response(conn_id, trans_id, resp_status, (btgatt_response_t *)response);
990 if (ret != BT_STATUS_SUCCESS) {
991 BT_ERR("GATT Server Send Response failed: %s", status2string(ret));
992 return convert_to_oal_status(ret);
994 return OAL_STATUS_SUCCESS;
997 oal_status_t gatts_send_indication(int instance_id, int attr_hndl, int conn_id, int len, int confirm, char* value)
999 int ret = OAL_STATUS_SUCCESS;
1001 API_TRACE("Server Send Indication : Ins_id = %d, srvHndl = %d, [%s]",
1002 instance_id, attr_hndl, confirm == TRUE ? "INDICATION" : "NOTIFICATION");
1003 CHECK_OAL_GATT_ENABLED();
1005 CHECK_SERVER_INSTANCE(instance_id);
1006 if (__gatts_find_server_instance(instance_id) == NULL)
1007 return OAL_STATUS_INTERNAL_ERROR;
1009 if (value == NULL || len == 0) {
1010 BT_ERR("GATT Server attribute value is empty");
1011 return OAL_STATUS_INVALID_PARAM;
1014 ret = gatt_api->server->send_indication(instance_id,
1015 attr_hndl, conn_id, len, confirm, value);
1016 if (ret != BT_STATUS_SUCCESS) {
1017 BT_ERR("GATT Server Send Indication failed: %s", status2string(ret));
1018 return convert_to_oal_status(ret);
1020 return OAL_STATUS_SUCCESS;
1023 oal_status_t gatts_update_att_value(int instance_id, oal_gatt_value_t *value)
1025 int ret = OAL_STATUS_SUCCESS;
1027 CHECK_OAL_GATT_ENABLED();
1029 CHECK_SERVER_INSTANCE(instance_id);
1030 if (__gatts_find_server_instance(instance_id) == NULL)
1031 return OAL_STATUS_INTERNAL_ERROR;
1033 if (value == NULL || value->len == 0) {
1034 BT_ERR("GATT Server attribute value is empty");
1035 return OAL_STATUS_INVALID_PARAM;
1038 API_TRACE("Server Update Value: Ins_id = [%d], att handle = [%d] data len [%d]",
1039 instance_id, value->handle, value->len);
1041 ret = gatt_api->server->update_att_value(instance_id,
1042 (int)value->handle, (int)value->len, (char*)value->value);
1043 if (ret != BT_STATUS_SUCCESS) {
1044 BT_ERR("GATT Server Update Attribute Value failed: [%s]", status2string(ret));
1045 return convert_to_oal_status(ret);
1047 return OAL_STATUS_SUCCESS;
1050 /* GATT Server Callbacks:Start */
1051 static void cb_gatts_service_added(int status, int server_if,
1052 btgatt_srvc_id_t *psrvc_id, int srvc_handle)
1054 BT_INFO("BTGATT SERVER SERVICE ADDED CB, status:%d, handle:%d", status, srvc_handle);
1055 event_gatts_srvc_prm_t* event = g_new0(event_gatts_srvc_prm_t, 1);
1057 CHECK_CORRECT_SERVER_ID(server_if, event->gatt_srvc_stat.server_inst);
1059 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1060 event->gatt_srvc_stat.server_inst = -1;
1062 event->gatt_srvc_stat.servic_hndl = srvc_handle;
1063 if (psrvc_id != NULL) {
1064 event->gatt_srvc_id.id.inst_id = psrvc_id->id.inst_id;
1065 //event->gatt_srvc_id.id.uuid = psrvc_id->id.uuid;
1066 memcpy(event->gatt_srvc_id.id.uuid.uuid, psrvc_id->id.uuid.uu, sizeof(psrvc_id->id.uuid));
1067 event->gatt_srvc_id.is_prmry = psrvc_id->is_primary;
1070 event->gatt_srvc_stat.status = convert_to_oal_status(status);
1072 send_event(OAL_EVENT_GATTS_SERVICE_ADDED, event, sizeof(event_gatts_srvc_prm_t));
1075 static void cb_gatts_included_service_added(int status, int server_if,
1077 int incl_srvc_handle)
1079 BT_INFO("BTGATT SERVER INCLUDED SERVICE ADDED CB, status:%d, srvc handle:%d, incl handle:%d",
1080 status, srvc_handle, incl_srvc_handle);
1082 event_gatts_incld_srvc_t* event = g_new0(event_gatts_incld_srvc_t, 1);
1084 CHECK_CORRECT_SERVER_ID(server_if, event->gatt_srvc_stat.server_inst);
1086 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1087 event->gatt_srvc_stat.server_inst = -1;
1089 event->gatt_srvc_stat.servic_hndl = srvc_handle;
1090 event->incl_srvc_hndl = incl_srvc_handle;
1091 event->gatt_srvc_stat.status = convert_to_oal_status(status);
1092 send_event(OAL_EVENT_GATTS_INCLUDED_SERVICE_ADDED, event, sizeof(event_gatts_incld_srvc_t));
1095 static void cb_gatts_characteristic_added(int status, int server_if, bt_uuid_t *char_id,
1096 int srvc_handle, int char_handle)
1098 BT_INFO("BTGATT SERVER CHARACTERISTIC ADDED CB, status:%d, srvc handle:%d, char handle:%d",
1099 status, srvc_handle, char_handle);
1101 event_gatts_srvc_charctr_t* event = g_new0(event_gatts_srvc_charctr_t, 1);
1103 CHECK_CORRECT_SERVER_ID(server_if, event->gatt_srvc_stat.server_inst);
1105 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1106 event->gatt_srvc_stat.server_inst = -1;
1108 event->gatt_srvc_stat.servic_hndl = srvc_handle;
1109 event->charctr_hndl = char_handle;
1110 event->gatt_srvc_stat.status = convert_to_oal_status(status);
1111 memcpy(event->charctr_uuid.uuid, char_id->uu, sizeof(bt_uuid_t));
1112 send_event(OAL_EVENT_GATTS_CHARACTERISTIC_ADDED, event, sizeof(event_gatts_srvc_charctr_t));
1115 static void cb_gatts_descriptor_added(int status, int server_if,
1116 bt_uuid_t *descr_id, int srvc_handle,
1119 BT_INFO("BTGATT SERVER DESCRIPTOR ADDED CB, status:%d, srvc handle:%d, desc handle:%d",
1120 status, srvc_handle, descr_handle);
1122 event_gatts_srvc_descr_t* event = g_new0(event_gatts_srvc_descr_t, 1);
1124 CHECK_CORRECT_SERVER_ID(server_if, event->gatt_srvc_stat.server_inst);
1126 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1127 event->gatt_srvc_stat.server_inst = -1;
1129 event->gatt_srvc_stat.servic_hndl = srvc_handle;
1130 event->descrptr_hndl = descr_handle;
1131 event->gatt_srvc_stat.status = convert_to_oal_status(status);
1132 memcpy(event->descrptr_uuid.uuid, descr_id->uu, sizeof(bt_uuid_t));
1133 send_event(OAL_EVENT_GATTS_DESCRIPTOR_ADDED, event, sizeof(event_gatts_srvc_descr_t));
1136 static void cb_gatts_service_started(int status, int server_if, int srvc_handle)
1138 BT_INFO("BTGATT SERVER SERVICE STARTED CB, status:%d, srvc_handle:%d", status, srvc_handle);
1140 event_gatts_srvc_t* event = g_new0(event_gatts_srvc_t, 1);
1142 CHECK_CORRECT_SERVER_ID(server_if, event->server_inst);
1144 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1145 event->server_inst = -1;
1147 event->servic_hndl = srvc_handle;
1148 event->status = convert_to_oal_status(status);
1149 send_event(OAL_EVENT_GATTS_SERVICE_STARTED, event, sizeof(event_gatts_srvc_t));
1152 static void cb_gatts_service_stopped(int status, int server_if, int srvc_handle)
1154 BT_INFO("BTGATT SERVER SERVICE STOPPED CB, status:%d, srvc_handle:%d", status, srvc_handle);
1156 event_gatts_srvc_t* event = g_new0(event_gatts_srvc_t, 1);
1158 CHECK_CORRECT_SERVER_ID(server_if, event->server_inst);
1160 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1161 event->server_inst = -1;
1163 event->servic_hndl = srvc_handle;
1164 event->status = convert_to_oal_status(status);
1165 send_event(OAL_EVENT_GATTS_SERVICE_STOPED, event, sizeof(event_gatts_srvc_t));
1168 static void cb_gatts_service_deleted(int status, int server_if, int srvc_handle)
1170 BT_INFO("BTGATT SERVER SERVICE DELETED CB, status:%d, srvc_handle:%d", status, srvc_handle);
1172 event_gatts_srvc_t* event = g_new0(event_gatts_srvc_t, 1);
1174 CHECK_CORRECT_SERVER_ID(server_if, event->server_inst);
1176 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1177 event->server_inst = -1;
1179 event->servic_hndl = srvc_handle;
1180 event->status = convert_to_oal_status(status);
1181 send_event(OAL_EVENT_GATTS_SERVICE_DELETED, event, sizeof(event_gatts_srvc_t));
1184 static void cb_gatts_connection(int conn_id, int server_if, int connected, bt_bdaddr_t *bda)
1187 oal_event_t event_type;
1189 BT_INFO("BTGATT SERVER CONNECTION connnected:%d, conn_id:%d server_if:%d", connected, conn_id, server_if);
1190 event_gatts_conn_t *event = g_new0(event_gatts_conn_t, 1);
1192 if (connected == TRUE) {
1193 for (l = gatt_servers; l; l = g_slist_next(l)) {
1194 gatt_server_t *info = l->data;
1197 if (info->instance_id == server_if) {
1198 event->server_inst = server_if;
1203 /* server_inst is not required in disconnected case */
1205 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1206 event->conn_id = conn_id;
1207 event_type = ((connected == TRUE) ? OAL_EVENT_GATTS_CONNECTION_COMPLETED : OAL_EVENT_GATTS_DISCONNECTION_COMPLETED);
1208 event->status = OAL_STATUS_SUCCESS;
1210 send_event(event_type, event, sizeof(event_gatts_conn_t));
1213 static void cb_gatts_request_read(int conn_id, int trans_id, bt_bdaddr_t *bda,
1214 int attr_handle, int offset, bool is_long)
1216 BT_DBG("BTGATT SERVER REQUEST READ CB: conn_id:%d", conn_id);
1218 event_gatts_srvc_read_attr_t* event = g_new0(event_gatts_srvc_read_attr_t, 1);
1220 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1222 event->attr_trans.attr_handle = attr_handle;
1223 event->attr_trans.conn_id = conn_id;
1224 event->attr_trans.trans_id = trans_id;
1225 event->attr_trans.offset = offset;
1226 event->is_long = is_long;
1228 send_event(OAL_EVENT_GATTS_REQUEST_READ, event, sizeof(event_gatts_srvc_read_attr_t));
1231 static void cb_gatts_request_write(int conn_id, int trans_id,
1232 bt_bdaddr_t *bda, int attr_handle,
1233 int offset, int length,
1234 bool need_rsp, bool is_prep, uint8_t* value)
1236 BT_INFO("BTGATT SERVER REQUEST WRITE conn_id:%d", conn_id);
1238 event_gatts_srvc_write_attr_t* event = g_new0(event_gatts_srvc_write_attr_t, 1);
1240 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1242 event->attr_trans.attr_handle = attr_handle;
1243 event->attr_trans.conn_id = conn_id;
1244 event->attr_trans.trans_id = trans_id;
1245 event->attr_trans.offset = offset;
1246 event->need_rsp = need_rsp;
1247 event->is_prep = is_prep;
1249 if (length > 0 && value != NULL) {
1250 if (length > OAL_GATT_MAX_ATTR_LEN)
1251 length = OAL_GATT_MAX_ATTR_LEN;
1252 memcpy(event->value, value, length);
1253 event->length = length;
1255 send_event(OAL_EVENT_GATTS_REQUEST_WRITE, event, sizeof(event_gatts_srvc_write_attr_t));
1258 static void cb_gatts_acquire_write(int mtu, int conn_id, int trans_id, int attr_handle , bt_bdaddr_t *bda)
1261 BT_INFO("BTGATT SERVER REQUEST ACQUIRE WRITE conn_id:%d", conn_id);
1263 event_gatts_srvc_acquire_attr_t* event = g_new0(event_gatts_srvc_acquire_attr_t, 1);
1265 event->attr_trans.attr_handle = attr_handle;
1266 event->attr_trans.conn_id = conn_id;
1267 event->attr_trans.trans_id = trans_id;
1269 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1271 send_event(OAL_EVENT_GATTS_REQUEST_ACQUIRE_WRITE, event, sizeof(event_gatts_srvc_acquire_attr_t));
1274 static void cb_gatts_acquire_notify(int mtu, int conn_id, int trans_id, int attr_handle)
1276 BT_INFO("BTGATT SERVER REQUEST ACQUIRE NOTIFY conn_id:%d", conn_id);
1278 event_gatts_srvc_acquire_attr_t* event = g_new0(event_gatts_srvc_acquire_attr_t, 1);
1280 event->attr_trans.attr_handle = attr_handle;
1281 event->attr_trans.conn_id = conn_id;
1282 event->attr_trans.trans_id = trans_id;
1285 send_event(OAL_EVENT_GATTS_REQUEST_ACQUIRE_NOTIFY, event, sizeof(event_gatts_srvc_acquire_attr_t));
1289 static void cb_gatts_response_confirmation(int status, int handle)
1291 BT_INFO("BTGATT SERVER RESPONSE CONFIRMATION CB, status:%d, handle:%d", status, handle);
1293 event_gatts_respons_t* event = g_new0(event_gatts_respons_t, 1);
1295 event->hndl = handle;
1296 event->status = convert_to_oal_status(status);
1298 send_event(OAL_EVENT_GATTS_RESPONSE_CONFIRMED, event, sizeof(event_gatts_respons_t));
1301 static void cb_indication_confirmation(int conn_id, int trans_id, int attr_handle, bt_bdaddr_t *bda)
1303 BT_INFO("BTGATT SERVER Indication Confirmation CB, conn_id:%d, trans_id:%d attr_handle:%d",
1304 conn_id, trans_id, attr_handle);
1306 event_gatts_ind_cnfrm_t* event = g_new0(event_gatts_ind_cnfrm_t, 1);
1308 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1310 event->attr_handle = attr_handle;
1311 event->conn_id = conn_id;
1312 event->trans_id = trans_id;
1313 send_event(OAL_EVENT_GATTS_IND_CONFIRM, event, sizeof(event_gatts_ind_cnfrm_t));
1316 static void cb_gatts_mtu_changed(int conn_id, int mtu)
1318 BT_INFO("BTGATT SERVER MTU CHANGED CB, conn_id: %d, mtu_size: %d", conn_id, mtu);
1319 event_gatts_mtu_changed_t *event = g_new0(event_gatts_mtu_changed_t, 1);
1320 event->conn_id = conn_id;
1321 event->mtu_size = mtu;
1322 send_event(OAL_EVENT_GATTS_MTU_CHANGED, event, sizeof(event_gatts_mtu_changed_t));
1326 static void cb_notifcation_changed(int conn_id, int trans_id, int attr_handle, bool notify, bt_bdaddr_t *bda)
1328 BT_INFO("BTGATT SERVER Notication CB, conn_id:%d, trans_id:%d attr_handle:%d Notify:%d",
1329 conn_id, trans_id, attr_handle, notify);
1331 event_gatts_notif_t* event = g_new0(event_gatts_notif_t, 1);
1333 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1335 event->attr_handle = attr_handle;
1336 event->conn_id = conn_id;
1337 event->trans_id = trans_id;
1338 event->notify = notify;
1339 send_event(OAL_EVENT_GATTS_NOTIFICATION, event, sizeof(event_gatts_notif_t));
1344 /* GATT Server Callbacks: End */
1345 /************************************ GATT Client ***********************************/
1346 /* Client Callbacks */
1347 static void cb_gattc_register_app(int status, int clientIf, bt_uuid_t *app_uuid)
1349 char uuid_str[BT_UUID_STRING_MAX];
1350 event_gattc_register_t *event;
1352 /* Check if GATT client registered for Default GATT client UUID */
1353 uuid_to_string((service_uuid_t *)app_uuid, uuid_str);
1355 BT_INFO("GATT CLIENT REGISTER APP CB, status:%d, clientIf:%d, %s", status, clientIf, uuid_str);
1356 event = g_new0(event_gattc_register_t, 1);
1357 event->client_if = clientIf;
1358 event->status = (status == 0 ? OAL_STATUS_SUCCESS : OAL_STATUS_INTERNAL_ERROR);
1359 memcpy(event->client_uuid.uuid, app_uuid->uu, sizeof(oal_uuid_t));
1360 send_event(OAL_EVENT_GATTC_REGISTRATION, event, sizeof(event_gattc_register_t));
1363 static void cb_gattc_scan_result(bt_bdaddr_t* bdaddress, uint8_t addr_type, int rssi,
1364 uint8_t *adv_data, int adv_data_len, uint8_t *scan_rsp_data, int scan_rsp_data_len)
1366 event_ble_scan_result_info *event;
1368 event = g_new0(event_ble_scan_result_info, 1);
1369 event->addr_type = addr_type;
1371 memcpy(event->address.addr, bdaddress->address, BT_ADDRESS_BYTES_NUM);
1372 memcpy(event->adv_data, adv_data, BLE_ADV_DATA_LENGTH);
1373 event->adv_data_len = adv_data_len;
1374 memcpy(event->scan_rsp_data, scan_rsp_data, BLE_ADV_DATA_LENGTH);
1375 event->scan_rsp_data_len = scan_rsp_data_len;
1376 send_event_bda_trace(OAL_EVENT_BLE_REMOTE_DEVICE_FOUND, event,
1377 sizeof(event_ble_scan_result_info), (bt_address_t *)bdaddress);
1380 static void cb_gattc_connection(int conn_id, int status, int client_if, bt_bdaddr_t* bda)
1382 event_gattc_conn_t *event;
1383 BT_INFO("BTGATT CLIENT CONNECTED CB, status:[%d] client_if[%d] id[%d]",
1384 status, client_if, conn_id);
1386 event = g_new0(event_gattc_conn_t, 1);
1387 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1388 event->client_if = client_if;
1390 if (BT_STATUS_SUCCESS != status) {
1391 event->status = OAL_STATUS_INTERNAL_ERROR;
1392 BT_ERR("gattc connection Error: %d", status);
1394 event->conn_id = conn_id;
1395 event->status = OAL_STATUS_SUCCESS;
1398 send_event_bda_trace(OAL_EVENT_GATTC_CONNECTION_COMPLETED, event,
1399 sizeof(*event), (bt_address_t *)bda);
1403 static void cb_gattc_disconnect(int conn_id, int status, int client_if, bt_bdaddr_t* bda)
1405 event_gattc_conn_t *event;
1406 BT_INFO("BTGATT Client Disconnect Complete cb, status:[%d], client_if[%d] id[%d]",
1407 status, client_if, conn_id);
1408 event = g_new0(event_gattc_conn_t, 1);
1409 event->status = convert_to_oal_status(status);
1410 event->client_if = client_if;
1411 event->conn_id = conn_id;
1412 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1413 send_event_bda_trace(OAL_EVENT_GATTC_DISCONNECTION_COMPLETED, event,
1414 sizeof(*event), (bt_address_t *)bda);
1417 static void cb_gattc_search_result(int conn_id, btgatt_srvc_id_t *srvc_id)
1419 char uuid_str[2*BT_UUID_STRING_MAX];
1421 uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str);
1422 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]",
1423 uuid_str, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1425 event_gattc_service_result_t *event = g_new0(event_gattc_service_result_t, 1);
1426 event->conn_status.status = OAL_STATUS_SUCCESS;
1427 event->conn_status.conn_id = conn_id;
1428 memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1430 send_event(OAL_EVENT_GATTC_SERVICE_SEARCH_RESULT, event, sizeof(*event));
1433 static void cb_gattc_search_complete(int conn_id, int status)
1435 BT_INFO("BTGATT Client Service Search Complete cb, status:%d, conn_id:%d",
1438 event_gattc_conn_status_t *event = g_new0(event_gattc_conn_status_t, 1);
1439 event->conn_id = conn_id;
1440 event->status = convert_to_oal_status(status);
1442 send_event(OAL_EVENT_GATTC_SERVICE_SEARCH_DONE, event, sizeof(*event));
1446 static void cb_gattc_get_characteristics(int conn_id, int status, btgatt_srvc_id_t *srvc_id,
1447 btgatt_gatt_id_t *char_id, int char_prop)
1449 char uuid_str1[2*BT_UUID_STRING_MAX];
1450 char uuid_str2[2*BT_UUID_STRING_MAX];
1453 BT_DBG("BTGATT Client Get Characteristic Callback, conn_id:%d, status:%d", conn_id, status);
1454 uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str1);
1455 BT_DBG("Service=> [%s], Inst_id [%u], Type [%s]",
1456 uuid_str1, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1458 event_gattc_characteristic_result_t *event = g_new0(event_gattc_characteristic_result_t, 1);
1459 event->conn_status.conn_id = conn_id;
1460 event->conn_status.status = convert_to_oal_status(status);
1461 memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1464 uuid_to_stringname((oal_uuid_t *)&(char_id->uuid), uuid_str2);
1465 BT_INFO("Charac => [%s], Inst_id [%u], Prop [%s]",
1466 uuid_str2, char_id->inst_id, char_prop_to_string(char_prop, str));
1467 event->char_prop = char_prop;
1468 memcpy(&(event->char_id), char_id, sizeof(oal_gatt_id_t));
1470 send_event(OAL_EVENT_GATTC_CHARAC_SERACH_RESULT, event, sizeof(*event));
1475 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)
1477 char uuid_str1[2*BT_UUID_STRING_MAX];
1478 char uuid_str2[2*BT_UUID_STRING_MAX];
1479 char uuid_str3[2*BT_UUID_STRING_MAX];
1480 BT_DBG("BTGATT Client Get Descriptor Callback, conn_id:%d, status:%d", conn_id, status);
1481 uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str1);
1482 uuid_to_stringname((oal_uuid_t *)&(char_id->uuid), uuid_str2);
1483 BT_DBG("Service=> [%s], Inst_id [%u], Type [%s]",
1484 uuid_str1, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1485 BT_DBG("Charac => [%s], Inst_id [%u]", uuid_str2, char_id->inst_id);
1487 event_gattc_descriptor_result_t *event = g_new0(event_gattc_descriptor_result_t, 1);
1488 event->conn_status.conn_id = conn_id;
1489 event->conn_status.status = convert_to_oal_status(status);
1490 memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1491 memcpy(&(event->char_id), char_id, sizeof(oal_gatt_id_t));
1494 uuid_to_stringname((oal_uuid_t *)&(descr_id->uuid), uuid_str3);
1495 BT_INFO("Desc => [%s], Inst_id [%u]", uuid_str3, descr_id->inst_id);
1496 memcpy(&(event->descr_id), descr_id, sizeof(oal_gatt_id_t));
1498 send_event(OAL_EVENT_GATTC_DESC_SERACH_RESULT, event, sizeof(*event));
1501 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)
1503 char uuid_str1[2*BT_UUID_STRING_MAX];
1504 char uuid_str2[2*BT_UUID_STRING_MAX];
1505 BT_INFO("BTGATT Client Register For Notification Callback, conn_id:%d, status:%d, registered: %s",
1506 conn_id, status, registered == 1 ? "REGISTERED" : "DEREGISTERED");
1507 uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str1);
1508 uuid_to_stringname((oal_uuid_t *)&(char_id->uuid), uuid_str2);
1509 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1510 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, char_id->inst_id);
1512 event_gattc_regdereg_notify_t *event = g_new0(event_gattc_regdereg_notify_t, 1);
1513 oal_event_t event_type = (registered == 1 ? OAL_EVENT_GATTC_NOTIFICATION_REGISTERED : OAL_EVENT_GATTC_NOTIFICATION_DEREGISTERED);
1514 event->conn_id = conn_id;
1515 event->status = convert_to_oal_status(status);
1516 memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1517 memcpy(&(event->char_id), char_id, sizeof(oal_gatt_id_t));
1519 send_event(event_type, event, sizeof(*event));
1522 static void cb_gattc_notify(int conn_id, btgatt_notify_params_t *p_data)
1525 char uuid_str1[2*BT_UUID_STRING_MAX];
1526 char uuid_str2[2*BT_UUID_STRING_MAX];
1528 BT_INFO("BTGATT Client Notify Callback, conn_id:%d", conn_id);
1529 BT_INFO("Server Address:[%s], is_notify:[%d], len:[%d]",
1530 bdt_bd2str((bt_address_t *)&(p_data->bda), &bdstr), p_data->is_notify, p_data->len);
1532 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1533 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1535 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]",
1536 uuid_str1, p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1537 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1539 if (p_data->len > 0) {
1541 data = g_malloc(3*p_data->len+1);
1543 BT_ERR("memory allocation failed");
1547 convert_hex_2_str((unsigned char *)p_data->value, p_data->len, data);
1548 BT_INFO("Notified Data: [%s]", data);
1550 event_gattc_notify_data *event = g_new0(event_gattc_notify_data, 1);
1551 memcpy(event->address.addr, p_data->bda.address, BT_ADDRESS_BYTES_NUM);
1552 event->is_notify = p_data->is_notify;
1553 event->data_len = p_data->len;
1554 memcpy(event->data, p_data->value, event->data_len);
1555 memcpy(&(event->char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1556 memcpy(&(event->srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1558 send_event_bda_trace(OAL_EVENT_GATTC_NOTIFY_DATA, event, sizeof(*event), (bt_address_t *)&p_data->bda);
1563 static void cb_gattc_service_changed(bt_bdaddr_t *bd_addr, int change_type, uint8_t *uuid, int conn_id, int inst_id)
1565 event_gattc_service_changed_data *event = g_new0(event_gattc_service_changed_data, 1);
1567 memcpy(event->address.addr, bd_addr->address, 6);
1568 event->change_type = change_type;
1569 memcpy(event->uuid.uuid, uuid, sizeof(event->uuid.uuid));
1570 event->conn_id = conn_id;
1571 event->inst_id = inst_id;
1573 send_event_bda_trace(OAL_EVENT_GATTC_SERVICE_CHANGED_IND, event, sizeof(*event), (bt_address_t *)bd_addr);
1576 static void cb_gattc_read_characteristic(int conn_id, int status, btgatt_read_params_t *p_data)
1578 char uuid_str1[2*BT_UUID_STRING_MAX];
1579 char uuid_str2[2*BT_UUID_STRING_MAX];
1580 BT_DBG("BTGATT Client Read Charcateristic Callback, conn_id:%d, read_status:%d", conn_id, status);
1582 event_gattc_read_data *event = g_new0(event_gattc_read_data, 1);
1583 event->uuid_status.conn_status.conn_id = conn_id;
1584 event->uuid_status.conn_status.status = convert_to_oal_status(status);
1585 event->value_type = p_data->value_type;
1586 memcpy(&(event->uuid_status.srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1587 memcpy(&(event->uuid_status.char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1589 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1590 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1591 BT_DBG("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1,
1592 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1593 BT_DBG("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1594 BT_DBG("Len: %u, value_type: %u", p_data->value.len, p_data->value_type);
1595 if (p_data->value.len > 0 && event->value_type == GATTC_READ_VALUE_TYPE_VALUE) {
1596 event->data_len = p_data->value.len;
1597 memcpy(&(event->data), &(p_data->value.value), event->data_len);
1600 send_event(OAL_EVENT_GATTC_READ_CHARAC, event, sizeof(*event));
1603 static void cb_gattc_write_characteristic(int conn_id, int status, btgatt_write_params_t *p_data)
1605 char uuid_str1[2*BT_UUID_STRING_MAX];
1606 char uuid_str2[2*BT_UUID_STRING_MAX];
1607 BT_INFO("BTGATT Client Write Charcateristic Callback, conn_id:%d, write_status:%d", conn_id, status);
1609 event_gattc_write_data *event = g_new0(event_gattc_write_data, 1);
1610 event->conn_status.conn_id = conn_id;
1611 event->conn_status.status = convert_to_oal_status(status);
1612 memcpy(&(event->srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1613 memcpy(&(event->char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1615 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1616 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1617 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1,
1618 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1619 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1621 send_event(OAL_EVENT_GATTC_WRITE_CHARAC, event, sizeof(*event));
1624 static void cb_gattc_write_descriptor(int conn_id, int status, btgatt_write_params_t *p_data)
1626 char uuid_str1[2*BT_UUID_STRING_MAX];
1627 char uuid_str2[2*BT_UUID_STRING_MAX];
1628 char uuid_str3[2*BT_UUID_STRING_MAX];
1629 BT_DBG("BTGATT Client Write Descriptor Callback, conn_id:%d, write_status:%d", conn_id, status);
1631 event_gattc_write_data *event = g_new0(event_gattc_write_data, 1);
1632 event->conn_status.conn_id = conn_id;
1633 event->conn_status.status = convert_to_oal_status(status);
1634 memcpy(&(event->srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1635 memcpy(&(event->char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1636 memcpy(&(event->descr_id), &(p_data->descr_id), sizeof(oal_gatt_id_t));
1638 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1639 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1640 uuid_to_stringname((oal_uuid_t *)&(p_data->descr_id.uuid), uuid_str3);
1641 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1,
1642 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1643 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1644 BT_INFO("Desc => [%s], Inst_id [%u]", uuid_str3, p_data->descr_id.inst_id);
1646 send_event(OAL_EVENT_GATTC_WRITE_DESCR, event, sizeof(*event));
1651 static void cb_gattc_read_descriptor(int conn_id, int status, btgatt_read_params_t *p_data)
1653 char uuid_str1[2*BT_UUID_STRING_MAX];
1654 char uuid_str2[2*BT_UUID_STRING_MAX];
1655 char uuid_str3[2*BT_UUID_STRING_MAX];
1656 BT_INFO("BTGATT Client Read Descriptor Callback, conn_id:%d, read_status:%d", conn_id, status);
1658 event_gattc_read_data *event = g_new0(event_gattc_read_data, 1);
1659 event->uuid_status.conn_status.conn_id = conn_id;
1660 event->uuid_status.conn_status.status = convert_to_oal_status(status);
1661 event->value_type = p_data->value_type;
1662 memcpy(&(event->uuid_status.srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1663 memcpy(&(event->uuid_status.char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1664 memcpy(&(event->uuid_status.descr_id), &(p_data->descr_id), sizeof(oal_gatt_id_t));
1667 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1668 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1669 uuid_to_stringname((oal_uuid_t *)&(p_data->descr_id.uuid), uuid_str3);
1670 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1,
1671 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1672 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1673 BT_INFO("Desc => [%s], Inst_id [%u]", uuid_str3, p_data->descr_id.inst_id);
1674 BT_DBG("Len: %u, value_type: %u", p_data->value.len, p_data->value_type);
1675 if (p_data->value.len > 0 && event->value_type == GATTC_READ_VALUE_TYPE_VALUE) {
1677 data = g_malloc(3*p_data->value.len+1);
1679 BT_ERR("memory allocation failed");
1683 convert_hex_2_str((unsigned char *)p_data->value.value, p_data->value.len, data);
1684 BT_DBG("Read Data: [%s]", data);
1685 event->data_len = p_data->value.len;
1686 memcpy(&(event->data), &(p_data->value.value), event->data_len);
1690 send_event(OAL_EVENT_GATTC_READ_DESCR, event, sizeof(*event));
1693 oal_status_t gattc_start_le_discovery(int client_id)
1696 int ret = OAL_STATUS_SUCCESS;
1698 API_TRACE("BTGATT CLIENT SCAN START");
1699 CHECK_OAL_GATT_ENABLED();
1700 CHECK_CLIENT_REGISTRATION(client_id);
1702 ret = gatt_api->client->scan(client_id, 1);
1703 if (ret != BT_STATUS_SUCCESS) {
1704 BT_ERR("Error:Start LE Discovery failed: %s", status2string(ret));
1705 return convert_to_oal_status(ret);
1707 return OAL_STATUS_SUCCESS;
1710 oal_status_t gattc_stop_le_discovery(int client_id)
1713 int ret = OAL_STATUS_SUCCESS;
1715 API_TRACE("Scan is stopped");
1716 CHECK_OAL_GATT_ENABLED();
1717 CHECK_CLIENT_REGISTRATION(client_id);
1718 ret = gatt_api->client->scan(client_id, 0);
1719 if (ret != BT_STATUS_SUCCESS) {
1720 BT_ERR("Error:Stop LE Discovery failed: %s", status2string(ret));
1721 return convert_to_oal_status(ret);
1723 return OAL_STATUS_SUCCESS;
1726 oal_status_t gattc_set_le_scan_param(int scan_type, int itv, int win)
1730 API_TRACE("Scan is stopped");
1731 CHECK_OAL_GATT_ENABLED();
1734 ret = gatt_api->client->set_scan_parameters(scan_type, itv, win);
1735 if (ret != BT_STATUS_SUCCESS) {
1736 BT_ERR("Error:Set scan parameters failed: %s", status2string(ret));
1737 return convert_to_oal_status(ret);
1740 ret = gatt_api->client->set_scan_parameters(itv, win);
1741 if (ret != BT_STATUS_SUCCESS) {
1742 BT_ERR("Error:Set scan parameters failed: %s", status2string(ret));
1743 return convert_to_oal_status(ret);
1747 return OAL_STATUS_SUCCESS;
1750 static void cb_gattc_configure_mtu_cmpl(int conn_id, int status, int mtu)
1752 event_gattc_mtu_configured_t *event;
1753 BT_INFO("BTGATT Client configure mtu complete Callback, conn_id: %d, status: %d, mtu: %d",
1754 conn_id, status, mtu);
1755 event = g_new0(event_gattc_mtu_configured_t, 1);
1756 event->conn_id = conn_id;
1758 event->status = convert_to_oal_status(status);
1759 send_event(OAL_EVENT_GATTC_MTU_EXCHANGE_COMPLETED, event, sizeof(*event));
1762 /************************************ GATT Client ***********************************/
1763 oal_status_t gattc_register(oal_uuid_t* client_uuid)
1766 char str[2*BT_UUID_STRING_MAX];
1767 int ret = OAL_STATUS_SUCCESS;
1769 CHECK_OAL_GATT_ENABLED();
1770 uuid_to_stringname(client_uuid, str);
1771 API_TRACE("uuid: [%s]", str);
1772 /* We will perform actual registration in cb_gattc_register_app callback */
1773 ret = gatt_api->client->register_client((bt_uuid_t *)client_uuid);
1774 if (ret != BT_STATUS_SUCCESS) {
1775 BT_ERR("GATT client register failed: %s", status2string(ret));
1776 return convert_to_oal_status(ret);
1778 return OAL_STATUS_SUCCESS;
1781 oal_status_t gattc_deregister(int client_id)
1783 int ret = OAL_STATUS_SUCCESS;
1785 API_TRACE("GATT client deregister");
1786 CHECK_OAL_GATT_ENABLED();
1787 CHECK_CLIENT_REGISTRATION(client_id);
1789 ret = gatt_api->client->unregister_client(client_id);
1790 if (ret != BT_STATUS_SUCCESS) {
1791 BT_ERR("GATT client deregister failed: %s", status2string(ret));
1792 return convert_to_oal_status(ret);
1795 return OAL_STATUS_SUCCESS;
1798 oal_status_t gattc_connect(int client_id, bt_address_t *device_address, int isDirect)
1800 int ret = OAL_STATUS_SUCCESS;
1803 OAL_CHECK_PARAMETER(device_address, return);
1804 API_TRACE("Client Connect: [%s]", bdt_bd2str(device_address, &bdstr) + 12);
1805 CHECK_OAL_GATT_ENABLED();
1806 CHECK_CLIENT_REGISTRATION(client_id);
1808 /* Handle the actual connection in cb_gattc_connection callback */
1809 ret = gatt_api->client->connect(client_id, (bt_bdaddr_t *)device_address, isDirect);
1810 if (ret != BT_STATUS_SUCCESS) {
1811 BT_ERR("GATT client connect failed: %s", status2string(ret));
1812 return convert_to_oal_status(ret);
1814 return OAL_STATUS_SUCCESS;
1817 oal_status_t gattc_disconnect(int client_id, bt_address_t *device_address, int conn_id)
1820 int ret = OAL_STATUS_SUCCESS;
1823 OAL_CHECK_PARAMETER(device_address, return);
1824 API_TRACE("Client Disconnect: [%s]", bdt_bd2str(device_address, &bdstr) + 12);
1825 CHECK_OAL_GATT_ENABLED();
1826 CHECK_CLIENT_REGISTRATION(client_id);
1827 CHECK_CLIENT_CONNECTION(conn_id);
1829 /* Handle actual disconnection in callback */
1830 ret = gatt_api->client->disconnect(client_id, (bt_bdaddr_t *) device_address, conn_id);
1831 if (ret != BT_STATUS_SUCCESS) {
1832 BT_ERR("GATT client disconnect failed: %s", status2string(ret));
1833 return convert_to_oal_status(ret);
1835 return OAL_STATUS_SUCCESS;
1839 oal_status_t gattc_search_service(int conn_id, oal_uuid_t *service_uuid)
1841 int ret = OAL_STATUS_SUCCESS;
1842 char uuid_str[2*BT_UUID_STRING_MAX];
1845 uuid_to_stringname(service_uuid, uuid_str);
1846 API_TRACE("Client Service Search UUID: [%s]", uuid_str);
1848 API_TRACE("Client Service Search All");
1850 CHECK_OAL_GATT_ENABLED();
1851 CHECK_CLIENT_CONNECTION(conn_id);
1852 ret = gatt_api->client->search_service(conn_id, (bt_uuid_t *)service_uuid);
1854 if (ret != BT_STATUS_SUCCESS) {
1855 BT_ERR("GATT client service search failed: %s", status2string(ret));
1856 return convert_to_oal_status(ret);
1858 return OAL_STATUS_SUCCESS;
1861 oal_status_t gattc_get_characteristic(int conn_id, oal_gatt_srvc_id_t *srvc_id, oal_gatt_id_t *char_id)
1863 int ret = OAL_STATUS_SUCCESS;
1864 char uuid_str[2*BT_UUID_STRING_MAX];
1866 OAL_CHECK_PARAMETER(srvc_id, return);
1867 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str);
1868 API_TRACE("Client Get Characteristic [%s]", uuid_str);
1869 CHECK_OAL_GATT_ENABLED();
1870 CHECK_CLIENT_CONNECTION(conn_id);
1871 ret = gatt_api->client->get_characteristic(conn_id, (btgatt_srvc_id_t *)srvc_id,
1872 (btgatt_gatt_id_t *)char_id);
1873 if (ret != BT_STATUS_SUCCESS) {
1874 BT_ERR("GATT client get characteristic failed: %s", status2string(ret));
1875 return convert_to_oal_status(ret);
1877 return OAL_STATUS_SUCCESS;
1880 oal_status_t gattc_get_descriptor(int conn_id, oal_gatt_srvc_id_t *srvc_id,
1881 oal_gatt_id_t *char_id, oal_gatt_id_t *desc_id)
1883 int ret = OAL_STATUS_SUCCESS;
1884 char uuid_str1[2*BT_UUID_STRING_MAX];
1885 char uuid_str2[2*BT_UUID_STRING_MAX];
1887 OAL_CHECK_PARAMETER(srvc_id, return);
1888 OAL_CHECK_PARAMETER(char_id, return);
1889 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1890 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1891 API_TRACE("Client Get Descriptor, Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
1892 CHECK_OAL_GATT_ENABLED();
1893 CHECK_CLIENT_CONNECTION(conn_id);
1894 ret = gatt_api->client->get_descriptor(conn_id, (btgatt_srvc_id_t *)srvc_id,
1895 (btgatt_gatt_id_t *)char_id, (btgatt_gatt_id_t *)desc_id);
1896 if (ret != BT_STATUS_SUCCESS) {
1897 BT_ERR("GATT client get descriptor failed: %s", status2string(ret));
1898 return convert_to_oal_status(ret);
1900 return OAL_STATUS_SUCCESS;
1903 oal_status_t gattc_register_for_notification(int conn_id, bt_address_t * address,
1904 oal_gatt_srvc_id_t *srvc_id, oal_gatt_id_t *char_id) {
1905 int ret = OAL_STATUS_SUCCESS;
1906 char uuid_str1[2*BT_UUID_STRING_MAX];
1907 char uuid_str2[2*BT_UUID_STRING_MAX];
1910 OAL_CHECK_PARAMETER(address, return);
1911 OAL_CHECK_PARAMETER(srvc_id, return);
1912 OAL_CHECK_PARAMETER(char_id, return);
1913 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1914 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1915 API_TRACE("Client Register Notification: [%s], Service_uuid: [%s], Char_uuid: [%s]", bdt_bd2str(address, &bdstr), uuid_str1, uuid_str2);
1916 CHECK_OAL_GATT_ENABLED();
1917 CHECK_CLIENT_CONNECTION(conn_id);
1919 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);
1920 if (ret != BT_STATUS_SUCCESS) {
1921 BT_ERR("GATT client register notification failed: %s", status2string(ret));
1922 return convert_to_oal_status(ret);
1925 return OAL_STATUS_SUCCESS;
1928 oal_status_t gattc_deregister_for_notification(int conn_id, bt_address_t * address,
1929 oal_gatt_srvc_id_t *srvc_id, oal_gatt_id_t *char_id) {
1930 int ret = OAL_STATUS_SUCCESS;
1931 char uuid_str1[2*BT_UUID_STRING_MAX];
1932 char uuid_str2[2*BT_UUID_STRING_MAX];
1935 OAL_CHECK_PARAMETER(address, return);
1936 OAL_CHECK_PARAMETER(srvc_id, return);
1937 OAL_CHECK_PARAMETER(char_id, return);
1938 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1939 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1940 API_TRACE("Client Deregister Notification: [%s], Service_uuid: [%s], Char_uuid: [%s]", bdt_bd2str(address, &bdstr), uuid_str1, uuid_str2);
1941 CHECK_OAL_GATT_ENABLED();
1942 CHECK_CLIENT_CONNECTION(conn_id);
1944 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);
1945 if (ret != BT_STATUS_SUCCESS) {
1946 BT_ERR("GATT client deregister notification failed: %s", status2string(ret));
1947 return convert_to_oal_status(ret);
1950 return OAL_STATUS_SUCCESS;
1954 oal_status_t gattc_read_characteristic(int conn_id, oal_gatt_srvc_id_t *srvc_id,
1955 oal_gatt_id_t *char_id, oal_gatt_auth_req_t auth_req)
1957 int ret = OAL_STATUS_SUCCESS;
1958 char uuid_str1[2*BT_UUID_STRING_MAX];
1959 char uuid_str2[2*BT_UUID_STRING_MAX];
1961 OAL_CHECK_PARAMETER(srvc_id, return);
1962 OAL_CHECK_PARAMETER(char_id, return);
1963 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1964 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1965 API_TRACE("Client Read Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
1966 CHECK_OAL_GATT_ENABLED();
1967 CHECK_CLIENT_CONNECTION(conn_id);
1969 ret = gatt_api->client->read_characteristic(conn_id, (btgatt_srvc_id_t *)srvc_id,
1970 (btgatt_gatt_id_t *)char_id, auth_req);
1971 if (ret != BT_STATUS_SUCCESS) {
1972 BT_ERR("GATT client read characteristic failed: %s", status2string(ret));
1973 return convert_to_oal_status(ret);
1976 return OAL_STATUS_SUCCESS;
1979 oal_status_t gattc_read_descriptor(int conn_id, oal_gatt_srvc_id_t *srvc_id,
1980 oal_gatt_id_t *char_id, oal_gatt_id_t *desc_id, oal_gatt_auth_req_t auth_req)
1982 int ret = OAL_STATUS_SUCCESS;
1983 char uuid_str1[2*BT_UUID_STRING_MAX];
1984 char uuid_str2[2*BT_UUID_STRING_MAX];
1985 char uuid_str3[2*BT_UUID_STRING_MAX];
1987 OAL_CHECK_PARAMETER(srvc_id, return);
1988 OAL_CHECK_PARAMETER(char_id, return);
1989 OAL_CHECK_PARAMETER(desc_id, return);
1990 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1991 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1992 uuid_to_stringname(&(desc_id->uuid), uuid_str3);
1993 API_TRACE("Client Read Descriptor: Service_uuid: [%s], Char_uuid: [%s], Desc_uuid: [%s]",
1994 uuid_str1, uuid_str2, uuid_str3);
1995 CHECK_OAL_GATT_ENABLED();
1996 CHECK_CLIENT_CONNECTION(conn_id);
1998 ret = gatt_api->client->read_descriptor(conn_id, (btgatt_srvc_id_t *)srvc_id,
1999 (btgatt_gatt_id_t *)char_id, (btgatt_gatt_id_t *)desc_id, auth_req);
2000 if (ret != BT_STATUS_SUCCESS) {
2001 BT_ERR("GATT client read descriptor failed: %s", status2string(ret));
2002 return convert_to_oal_status(ret);
2005 return OAL_STATUS_SUCCESS;
2008 oal_status_t gattc_acquire_notify(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2009 oal_gatt_id_t *char_id, int *fd, int *mtu)
2011 int ret = OAL_STATUS_SUCCESS;
2012 char uuid_str1[2*BT_UUID_STRING_MAX];
2013 char uuid_str2[2*BT_UUID_STRING_MAX];
2014 OAL_CHECK_PARAMETER(srvc_id, return);
2015 OAL_CHECK_PARAMETER(char_id, return);
2016 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2017 uuid_to_stringname(&(char_id->uuid), uuid_str2);
2018 API_TRACE("Client Write Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
2019 CHECK_OAL_GATT_ENABLED();
2020 CHECK_CLIENT_CONNECTION(conn_id);
2022 ret = gatt_api->client->acquire_notify(conn_id, (btgatt_srvc_id_t *)srvc_id,
2023 (btgatt_gatt_id_t *)char_id, fd, mtu);
2024 if (ret != BT_STATUS_SUCCESS) {
2025 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
2026 return convert_to_oal_status(ret);
2029 return OAL_STATUS_SUCCESS;
2032 oal_status_t gattc_acquire_write(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2033 oal_gatt_id_t *char_id, oal_gatt_auth_req_t auth_req , int *fd, int*mtu)
2035 int ret = OAL_STATUS_SUCCESS;
2036 char uuid_str1[2*BT_UUID_STRING_MAX];
2037 char uuid_str2[2*BT_UUID_STRING_MAX];
2038 OAL_CHECK_PARAMETER(srvc_id, return);
2039 OAL_CHECK_PARAMETER(char_id, return);
2040 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2041 uuid_to_stringname(&(char_id->uuid), uuid_str2);
2042 API_TRACE("Client Write Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
2043 CHECK_OAL_GATT_ENABLED();
2044 CHECK_CLIENT_CONNECTION(conn_id);
2046 ret = gatt_api->client->acquire_write(conn_id, (btgatt_srvc_id_t *)srvc_id,
2047 (btgatt_gatt_id_t *)char_id, auth_req, fd, mtu);
2048 if (ret != BT_STATUS_SUCCESS) {
2049 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
2050 return convert_to_oal_status(ret);
2053 return OAL_STATUS_SUCCESS;
2056 oal_status_t gattc_write_characteristic(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2057 oal_gatt_id_t *char_id, oal_gatt_write_type_t write_type,
2058 int len, oal_gatt_auth_req_t auth_req, char* data)
2060 int ret = OAL_STATUS_SUCCESS;
2061 char uuid_str1[2*BT_UUID_STRING_MAX];
2062 char uuid_str2[2*BT_UUID_STRING_MAX];
2063 OAL_CHECK_PARAMETER(srvc_id, return);
2064 OAL_CHECK_PARAMETER(char_id, return);
2065 OAL_CHECK_PARAMETER(data, return);
2066 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2067 uuid_to_stringname(&(char_id->uuid), uuid_str2);
2068 API_TRACE("Client Write Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
2069 CHECK_OAL_GATT_ENABLED();
2070 CHECK_CLIENT_CONNECTION(conn_id);
2072 ret = gatt_api->client->write_characteristic(conn_id, (btgatt_srvc_id_t *)srvc_id,
2073 (btgatt_gatt_id_t *)char_id, write_type, len, auth_req, data);
2074 if (ret != BT_STATUS_SUCCESS) {
2075 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
2076 return convert_to_oal_status(ret);
2079 return OAL_STATUS_SUCCESS;
2082 oal_status_t gattc_write_descriptor(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2083 oal_gatt_id_t *char_id, oal_gatt_id_t *desc_id,
2084 oal_gatt_write_type_t write_type, int len, oal_gatt_auth_req_t auth_req, char* data)
2086 int ret = OAL_STATUS_SUCCESS;
2087 char uuid_str1[2*BT_UUID_STRING_MAX];
2088 char uuid_str2[2*BT_UUID_STRING_MAX];
2089 char uuid_str3[2*BT_UUID_STRING_MAX];
2090 OAL_CHECK_PARAMETER(srvc_id, return);
2091 OAL_CHECK_PARAMETER(char_id, return);
2092 OAL_CHECK_PARAMETER(desc_id, return);
2093 OAL_CHECK_PARAMETER(data, return);
2094 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2095 uuid_to_stringname(&(char_id->uuid), uuid_str2);
2096 uuid_to_stringname(&(desc_id->uuid), uuid_str3);
2097 API_TRACE("Client Write Descriptor: Service_uuid: [%s], Char_uuid: [%s], Desc_uuid: [%s]",
2098 uuid_str1, uuid_str2, uuid_str3);
2099 CHECK_OAL_GATT_ENABLED();
2100 CHECK_CLIENT_CONNECTION(conn_id);
2102 ret = gatt_api->client->write_descriptor(conn_id, (btgatt_srvc_id_t *)srvc_id,
2103 (btgatt_gatt_id_t *)char_id, (btgatt_gatt_id_t *)desc_id, write_type, len, auth_req, data);
2104 if (ret != BT_STATUS_SUCCESS) {
2105 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
2106 return convert_to_oal_status(ret);
2109 return OAL_STATUS_SUCCESS;
2112 oal_status_t gattc_conn_param_update(bt_address_t * address, int min, int max, int latency, int timeout)
2117 OAL_CHECK_PARAMETER(address, return);
2118 CHECK_OAL_GATT_ENABLED();
2120 BT_DBG("[%s] min[%d] max[%d] latency[%d] timeout[%d]", bdt_bd2str(address, &bdstr), min, max, latency, timeout);
2121 ret = gatt_api->client->conn_parameter_update((bt_bdaddr_t *)address, min, max, latency, timeout);
2122 if (ret != BT_STATUS_SUCCESS) {
2123 BT_ERR("error: %s", status2string(ret));
2124 return convert_to_oal_status(ret);
2126 return OAL_STATUS_SUCCESS;
2129 oal_status_t gattc_unregister_scan_filter(int slot_id)
2135 CHECK_OAL_GATT_ENABLED();
2137 BT_INFO("Remove Scan filter Slot ID is: [%d]", slot_id);
2139 ret = gatt_api->client->scan_filter_clear(client_if, slot_id);
2140 if (ret != BT_STATUS_SUCCESS) {
2141 BT_ERR("error: %s", status2string(ret));
2142 return convert_to_oal_status(ret);
2144 return OAL_STATUS_SUCCESS;
2147 oal_status_t gattc_enable_scan_filter(int client_if)
2152 CHECK_OAL_GATT_ENABLED();
2154 BT_INFO("Enable Scan filter. Client If: %d", client_if);
2156 ret = gatt_api->client->scan_filter_enable(client_if, TRUE);
2157 if (ret != BT_STATUS_SUCCESS) {
2158 BT_ERR("error: %s", status2string(ret));
2159 return convert_to_oal_status(ret);
2161 return OAL_STATUS_SUCCESS;
2164 oal_status_t gattc_disable_scan_filter(int client_if)
2169 CHECK_OAL_GATT_ENABLED();
2171 BT_INFO("Disable Scan filter. Client If: %d", client_if);
2173 ret = gatt_api->client->scan_filter_enable(client_if, FALSE);
2174 if (ret != BT_STATUS_SUCCESS) {
2175 BT_ERR("error: %s", status2string(ret));
2176 return convert_to_oal_status(ret);
2178 return OAL_STATUS_SUCCESS;
2181 oal_status_t gattc_register_scan_filter(oal_ble_scan_filter_t* filter_data)
2184 int client_info = 0;
2187 int company_id_mask = 0;
2188 int address_type = 0;
2189 int feature_selection = 0;
2191 oal_ble_scan_filter_param_setup_t scan_filter_setup = {.list_logic_type = 0,
2192 .filt_logic_type = 1,
2193 .rssi_high_thres = -127,
2194 .rssi_low_thres = -127,
2198 .found_timeout_cnt = 0
2201 OAL_CHECK_PARAMETER(filter_data, return);
2203 CHECK_OAL_GATT_ENABLED();
2205 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS){
2207 BT_INFO("OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS is being added");
2208 BT_INFO("BT remote Device address is %s", bdt_bd2str(filter_data->device_address, &bdstr));
2209 ret = gatt_api->client->scan_filter_add_remove(client_info,
2211 OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS,
2212 filter_data->slot_id,
2217 (bt_bdaddr_t*)filter_data->device_address,
2224 if (ret != BT_STATUS_SUCCESS){
2225 BT_ERR("error: %s", status2string(ret));
2226 BT_INFO("unregistering already set filter features.");
2227 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2228 return convert_to_oal_status(ret);
2231 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS;
2233 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_NAME){
2234 ret = gatt_api->client->scan_filter_add_remove(client_info,
2236 OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_NAME,
2237 filter_data->slot_id,
2245 filter_data->device_name, // device_name as p_data in HAL
2249 if (ret != BT_STATUS_SUCCESS){
2250 BT_ERR("error: %s", status2string(ret));
2251 BT_INFO("unregistering already set filter features.");
2252 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2253 return convert_to_oal_status(ret);
2256 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_NAME;
2258 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_UUID){
2259 BT_INFO("OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_UUID is being added");
2260 char uuid_str1[2*BT_UUID_STRING_MAX];
2261 char uuid_str2[2*BT_UUID_STRING_MAX];
2263 uuid_to_stringname((service_uuid_t*)filter_data->service_uuid, uuid_str1);
2264 uuid_to_stringname((service_uuid_t*)filter_data->service_uuid_mask, uuid_str2);
2266 BT_INFO("Service UUID is [%s] and Service UUID mask is [%s]", uuid_str1, uuid_str2);
2267 ret = gatt_api->client->scan_filter_add_remove(client_info,
2269 OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_UUID,
2270 filter_data->slot_id,
2273 (bt_uuid_t*)filter_data->service_uuid,
2274 (bt_uuid_t*)filter_data->service_uuid_mask,
2277 filter_data->service_uuid_len, // service_uuid_len as data_len in HAL
2279 filter_data->service_uuid_mask_len,
2282 if (ret != BT_STATUS_SUCCESS){
2283 BT_ERR("error: %s", status2string(ret));
2284 BT_INFO("unregistering already set filter features.");
2285 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2286 return convert_to_oal_status(ret);
2289 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_UUID;
2291 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID){
2292 BT_INFO("OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID is being added");
2293 char uuid_str1[2*BT_UUID_STRING_MAX];
2294 char uuid_str2[2*BT_UUID_STRING_MAX];
2295 uuid_to_stringname((service_uuid_t*)filter_data->service_solicitation_uuid, uuid_str1);
2296 uuid_to_stringname((service_uuid_t*)filter_data->service_solicitation_uuid_mask, uuid_str2);
2297 BT_INFO("Service Solicitation UUID is [%s] and Service Solicitation UUID mask is [%s]", uuid_str1, uuid_str2);
2298 ret = gatt_api->client->scan_filter_add_remove(client_info,
2300 OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID,
2301 filter_data->slot_id,
2304 (bt_uuid_t*)filter_data->service_solicitation_uuid,
2305 (bt_uuid_t*)filter_data->service_solicitation_uuid_mask,
2308 filter_data->service_solicitation_uuid_len, // service_solicitation_uuid_len as data_len in HAL
2310 filter_data->service_solicitation_uuid_mask_len,
2313 if (ret != BT_STATUS_SUCCESS){
2314 BT_ERR("error: %s", status2string(ret));
2315 BT_INFO("unregistering already set filter features.");
2316 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2317 return convert_to_oal_status(ret);
2320 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID;
2322 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_DATA){
2323 ret = gatt_api->client->scan_filter_add_remove(client_info,
2325 OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_DATA,
2326 filter_data->slot_id,
2333 filter_data->service_data_len, //service_data_len as data_len in HAL
2334 (char*)filter_data->service_data,
2335 filter_data->service_data_mask_len,
2336 (char*)filter_data->service_data_mask
2338 if (ret != BT_STATUS_SUCCESS){
2339 BT_ERR("error: %s", status2string(ret));
2340 BT_INFO("unregistering already set filter features.");
2341 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2342 return convert_to_oal_status(ret);
2345 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_DATA;
2347 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA){
2348 ret = gatt_api->client->scan_filter_add_remove(client_info,
2350 OAL_BLE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA,
2351 filter_data->slot_id,
2352 filter_data->manufacturer_id,
2358 filter_data->manufacturer_data_len, //manufacturer_data_len as data_len in HAL
2359 (char*)filter_data->manufacturer_data,
2360 filter_data->manufacturer_data_mask_len,
2361 (char*)filter_data->manufacturer_data_mask
2363 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA;
2364 if (ret != BT_STATUS_SUCCESS){
2365 BT_ERR("error: %s", status2string(ret));
2366 BT_INFO("unregistering already set filter features.");
2367 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2368 return convert_to_oal_status(ret);
2371 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA;
2374 BT_DBG("Filter selection 0x%.2x", feature_selection);
2376 ret = gatt_api->client->scan_filter_param_setup(client_info, action, filter_data->slot_id, feature_selection,
2377 scan_filter_setup.list_logic_type, scan_filter_setup.filt_logic_type,
2378 scan_filter_setup.rssi_high_thres, scan_filter_setup.rssi_low_thres,
2379 scan_filter_setup.dely_mode, scan_filter_setup.found_timeout,
2380 scan_filter_setup.lost_timeout, scan_filter_setup.found_timeout_cnt);
2381 if (ret != BT_STATUS_SUCCESS){
2382 BT_ERR("error: %s", status2string(ret));
2383 return convert_to_oal_status(ret);
2385 return OAL_STATUS_SUCCESS;
2388 oal_status_t gattc_get_att_mtu(int conn_id, int *mtu)
2390 int ret = OAL_STATUS_SUCCESS;
2391 API_TRACE("Get ATT MTU, conn_id: %d", conn_id);
2392 CHECK_OAL_GATT_ENABLED();
2393 CHECK_CLIENT_CONNECTION(conn_id);
2394 OAL_CHECK_PARAMETER(mtu, return);
2396 /* To prevent crash in case other libraries not support this api */
2397 if (gatt_api->client->get_att_mtu == NULL) {
2398 BT_WARN("get_att_mtu is NULL");
2399 return OAL_STATUS_NOT_SUPPORT;
2402 ret = gatt_api->client->get_att_mtu(conn_id, mtu);
2403 if (ret != BT_STATUS_SUCCESS) {
2404 BT_ERR("GATT MTU Size failed, status: %s", status2string(ret));
2405 return convert_to_oal_status(ret);
2408 BT_INFO("Current ATT MTU Size: %d", *mtu);
2409 return OAL_STATUS_SUCCESS;
2412 oal_status_t gattc_configure_mtu(int conn_id, int mtu)
2416 API_TRACE("Configure MTU Size: [%d]", mtu);
2417 CHECK_OAL_GATT_ENABLED();
2418 CHECK_CLIENT_CONNECTION(conn_id);
2420 /* To prevent crash in case other libraries not support this api */
2421 if (gatt_api->client->configure_mtu == NULL) {
2422 BT_WARN("configure_mtu is NULL");
2423 return OAL_STATUS_NOT_SUPPORT;
2426 ret = gatt_api->client->configure_mtu(conn_id, mtu);
2427 if (ret != BT_STATUS_SUCCESS) {
2428 BT_ERR("Gatt client configure_mtu error: %s", status2string(ret));
2429 return convert_to_oal_status(ret);
2432 return OAL_STATUS_SUCCESS;
2435 oal_status_t gatt_get_data_batching_available_packets(
2436 unsigned int *available_packets)
2440 ret = gatt_api->client->get_data_batching_available_packets(available_packets);
2441 if (ret != BT_STATUS_SUCCESS) {
2442 BT_ERR("GATT data batching failed: %s", status2string(ret));
2443 return convert_to_oal_status(ret);
2446 return OAL_STATUS_SUCCESS;
2449 oal_status_t gatt_enable_data_batching(bt_address_t * address,
2450 int packet_threshold, int timeout)
2454 ret = gatt_api->client->enable_data_batching((bt_bdaddr_t *)address, packet_threshold, timeout);
2455 if (ret != BT_STATUS_SUCCESS) {
2456 BT_ERR("GATT data batching failed: %s", status2string(ret));
2457 return convert_to_oal_status(ret);
2460 return OAL_STATUS_SUCCESS;
2463 oal_status_t gatt_disable_data_batching(bt_address_t * address)
2467 ret = gatt_api->client->disable_data_batching((bt_bdaddr_t *)address);
2468 if (ret != BT_STATUS_SUCCESS) {
2469 BT_ERR("GATT data batching failed: %s", status2string(ret));
2470 return convert_to_oal_status(ret);
2473 return OAL_STATUS_SUCCESS;
2476 oal_status_t gattc_add_connection_info(bt_address_t *device_address, int conn_id, int instance_id)
2478 int ret = OAL_STATUS_SUCCESS;
2481 OAL_CHECK_PARAMETER(device_address, return);
2482 API_TRACE("Add connection info: [%s]", bdt_bd2str(device_address, &bdstr) + 12);
2483 CHECK_OAL_GATT_ENABLED();
2485 ret = gatt_api->client->add_connection_info((bt_bdaddr_t *)device_address, conn_id, instance_id);
2486 if (ret != BT_STATUS_SUCCESS) {
2487 BT_ERR("GATT connection info add failed: %s", status2string(ret));
2488 return convert_to_oal_status(ret);
2490 return OAL_STATUS_SUCCESS;