2 * Open Adaptation Layer (OAL)
4 * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
20 #include <bluetooth.h>
22 #include "oal-event.h"
23 #include "oal-internal.h"
24 #include "oal-manager.h"
25 #include "oal-adapter-mgr.h"
26 #include "oal-utils.h"
28 #include "oal-common.h"
30 #define DEV_APPEARANCE 0x0000
31 #define DEFAULT_GATT_CLIENT_UUID "0000A00A-1111-1111-0123-456789ABCDEF"
32 #define MAX_PROP_LEN 95 /* SVACE WGID: 324403*/
33 #define MAX_PERM_LEN 80 //Fix for svace wgid:324402
35 #define CHECK_OAL_GATT_ENABLED() \
37 if (gatt_api == NULL) { \
38 BT_ERR("GATT Not Enabled"); \
39 return OAL_STATUS_NOT_READY; \
43 #define CHECK_SERVER_INSTANCE(instance_id) \
45 if (instance_id < 1) { \
46 BT_ERR("Invalid Instance"); \
47 return OAL_STATUS_INVALID_PARAM; \
51 #define CHECK_CORRECT_SERVER_ID(server_if, server_inst) \
53 gatt_server_t *info; \
54 info = __gatts_find_server_instance(server_if); \
56 server_inst = server_if; \
61 #define PRINT_ADV_DATA(length, manuf_data, data_type) \
63 char * buf_str = g_malloc0(3*length + 1);\
64 if (NULL != buf_str) { \
65 convert_hex_2_str(manuf_data, length, buf_str);\
66 if (TRUE == data_type) \
67 BT_INFO("Scan Response Data:%s\n", buf_str);\
69 BT_INFO("Advertising Data:%s\n", buf_str);\
74 #define CHECK_CLIENT_REGISTRATION(client_id) \
76 if (client_id <= 0) { \
77 BT_DBG("No client registered yet...");\
78 return OAL_STATUS_INTERNAL_ERROR;\
82 #define CHECK_CLIENT_CONNECTION(conn_id) \
85 BT_ERR("No active connection");\
86 return OAL_STATUS_INTERNAL_ERROR;\
93 GATT_INS_DATA_SETTING,
103 gboolean cur_adv_state;
106 const char *oal_device_type[] = {
117 } oal_pending_gattc_req_e;
119 static const btgatt_interface_t * gatt_api;
120 static GSList *gatt_servers = NULL; /* List of gatt_server_t */
122 /* Forward declarations of GATT Server callbacks */
123 static void cb_gatts_register_app(int status, int server_if, bt_uuid_t *uuid);
124 static void cb_gatts_multi_adv_enable(int server_if, int status);
125 static void cb_gatts_multi_adv_set_inst_data(int server_if, int status);
126 static void cb_gatts_multi_adv_disable(int server_if, int status);
127 static void cb_gatts_multi_adv_update(int server_if, int status);
129 static void cb_gatts_listen(int status, int server_if);
132 static void cb_gatts_service_added(int status, int server_if, btgatt_srvc_id_t *srvc_id, int srvc_handle);
133 static void cb_gatts_included_service_added(int status, int server_if, int srvc_handle, int incl_srvc_handle);
134 static void cb_gatts_characteristic_added(int status, int server_if, bt_uuid_t *char_id, int srvc_handle, int char_handle);
135 static void cb_gatts_descriptor_added(int status, int server_if, bt_uuid_t *descr_id, int srvc_handle, int descr_handle);
137 static void cb_gatts_service_started(int status, int server_if, int srvc_handle);
138 static void cb_gatts_service_stopped(int status, int server_if, int srvc_handle);
139 static void cb_gatts_service_deleted(int status, int server_if, int srvc_handle);
141 static void cb_gatts_request_read(int conn_id, int trans_id, bt_bdaddr_t *bda, int attr_handle, int offset, bool is_long);
142 static void cb_gatts_request_write(int conn_id, int trans_id, bt_bdaddr_t *bda, int attr_handle, int offset, int length,
143 bool need_rsp, bool is_prep, uint8_t* value);
144 static void cb_gatts_response_confirmation(int status, int handle);
146 static void cb_gatts_acquire_write(int fd, int conn_id, int trans_id, int attr_handle, bt_bdaddr_t*);
147 static void cb_gatts_acquire_notify(int fd, int conn_id, int trans_id, int attr_handle, bt_bdaddr_t*);
149 static void cb_indication_confirmation(int conn_id, int trans_id, int attr_handle, bt_bdaddr_t *bda);
150 static void cb_gatts_connection(int conn_id, int server_if, int connected, bt_bdaddr_t *bda);
152 static void cb_gatts_mtu_changed(int conn_id, int mtu);
155 static void cb_notifcation_changed(int conn_id, int trans_id, int attr_handle, bool notify, bt_bdaddr_t *bda);
158 /************************************HAL Interface *************************************/
160 /*TODO GATT Server callbacks will be implemented in subsequent patches */
161 static const btgatt_server_callbacks_t btgatt_server_callbacks = {
162 .register_server_cb = cb_gatts_register_app,
163 .connection_cb = cb_gatts_connection,
164 .service_added_cb = cb_gatts_service_added,
165 .included_service_added_cb = cb_gatts_included_service_added,
166 .characteristic_added_cb = cb_gatts_characteristic_added,
167 .descriptor_added_cb = cb_gatts_descriptor_added,
168 .service_started_cb = cb_gatts_service_started,
169 .service_stopped_cb = cb_gatts_service_stopped,
170 .service_deleted_cb = cb_gatts_service_deleted,
171 .indication_confirmation_cb = cb_indication_confirmation,
172 .request_read_cb = cb_gatts_request_read,
173 .request_write_cb = cb_gatts_request_write,
174 .request_exec_write_cb = NULL,
175 .response_confirmation_cb = cb_gatts_response_confirmation,
176 .listen_cb = cb_gatts_listen,
177 .multi_adv_enable_cb = cb_gatts_multi_adv_enable,
178 .multi_adv_update_cb = cb_gatts_multi_adv_update,
179 .multi_adv_data_cb = cb_gatts_multi_adv_set_inst_data,
180 .multi_adv_disable_cb = cb_gatts_multi_adv_disable,
181 .mtu_changed_cb = cb_gatts_mtu_changed,
183 .notif_enabled_cb = cb_notifcation_changed,
185 .request_acquire_write_cb = cb_gatts_acquire_write,
186 .request_acquire_notify_cb = cb_gatts_acquire_notify
189 /* Forward declaration for GATT client callbacks */
190 static void cb_gattc_register_app(int status, int clientIf, bt_uuid_t *app_uuid);
191 static void cb_gattc_scan_result(bt_bdaddr_t* bdaddress, uint8_t addr_type, int rssi,
192 uint8_t *adv_data, int adv_data_len, uint8_t *scan_rsp_data, int scan_rsp_data_len);
193 static void cb_gattc_connection(int conn_id, int status, int client_if, bt_bdaddr_t* bda);
194 static void cb_gattc_disconnect(int conn_id, int status, int client_if, bt_bdaddr_t* bda);
195 static void cb_gattc_search_result(int conn_id, btgatt_srvc_id_t *srvc_id);
196 static void cb_gattc_search_complete(int conn_id, int status);
197 static void cb_gattc_get_characteristics(int conn_id, int status, btgatt_srvc_id_t *srvc_id,
198 btgatt_gatt_id_t *char_id, int char_prop);
199 static void cb_gattc_get_descriptor(int conn_id, int status, btgatt_srvc_id_t *srvc_id,
200 btgatt_gatt_id_t *char_id, btgatt_gatt_id_t *descr_id);
201 static void cb_gattc_read_characteristic(int conn_id, int status, btgatt_read_params_t *p_data);
202 static void cb_gattc_read_descriptor(int conn_id, int status, btgatt_read_params_t *p_data);
203 static void cb_gattc_write_characteristic(int conn_id, int status, btgatt_write_params_t *p_data);
204 static void cb_gattc_write_descriptor(int conn_id, int status, btgatt_write_params_t *p_data);
205 static void cb_gattc_register_for_notification(int conn_id, int registered, int status,
206 btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id);
207 static void cb_gattc_notify(int conn_id, btgatt_notify_params_t *p_data);
208 static void cb_gattc_service_changed(bt_bdaddr_t *bd_addr, int change_type, uint8_t *uuid, int conn_id, int inst_id);
209 static void cb_gattc_configure_mtu_cmpl(int conn_id, int status, int mtu);
211 /*TODO GATT CLient callbacks will be implemented in subsequent patches */
212 static const btgatt_client_callbacks_t btgatt_client_callbacks = {
213 .register_client_cb = cb_gattc_register_app,
214 .scan_result_cb = cb_gattc_scan_result,
215 .open_cb = cb_gattc_connection,
216 .close_cb = cb_gattc_disconnect,
217 .search_complete_cb = cb_gattc_search_complete,
218 .search_result_cb = cb_gattc_search_result,
219 .get_characteristic_cb = cb_gattc_get_characteristics,
220 .get_descriptor_cb = cb_gattc_get_descriptor,
221 .get_included_service_cb = NULL,
222 .register_for_notification_cb = cb_gattc_register_for_notification,
223 .notify_cb = cb_gattc_notify,
224 .service_changed_cb = cb_gattc_service_changed,
225 .read_characteristic_cb = cb_gattc_read_characteristic,
226 .write_characteristic_cb = cb_gattc_write_characteristic,
227 .read_descriptor_cb = cb_gattc_read_descriptor,
228 .write_descriptor_cb = cb_gattc_write_descriptor,
229 .execute_write_cb = NULL,
230 .read_remote_rssi_cb = NULL,
231 .configure_mtu_cb = cb_gattc_configure_mtu_cmpl,
232 #ifdef PLATFORM_ANDROID_HAL
233 .scan_filter_cfg_cb = NULL,
234 .scan_filter_param_cb = NULL,
235 .scan_filter_status_cb = NULL,
236 .congestion_cb = NULL,
237 .batchscan_cfg_storage_cb = NULL,
238 .batchscan_enb_disable_cb = NULL,
239 .batchscan_reports_cb = NULL,
240 .batchscan_threshold_cb = NULL,
241 .track_adv_event_cb = NULL,
245 static btgatt_callbacks_t btgatt_callbacks = {
246 sizeof(btgatt_callbacks_t),
247 &btgatt_client_callbacks,
248 &btgatt_server_callbacks
251 /*******************************GATT Initialisation - Deinitialisation********************************/
252 oal_status_t gatt_enable(void)
254 const bt_interface_t * blued_api;
257 /* Get stack interface */
258 blued_api = (const bt_interface_t *) adapter_get_stack_interface();
260 if (blued_api == NULL) {
261 BT_ERR("Stack is not initialized");
262 return OAL_STATUS_NOT_READY;
266 BT_WARN("GATT Interface is already initialized...");
267 return OAL_STATUS_ALREADY_DONE;
270 gatt_api = (const btgatt_interface_t *)blued_api->get_profile_interface(BT_PROFILE_GATT_ID);
271 if (gatt_api == NULL) {
272 BT_ERR("GATT interface failed");
273 return OAL_STATUS_INTERNAL_ERROR;
276 if ((ret = gatt_api->init(&btgatt_callbacks)) != BT_STATUS_SUCCESS) {
277 BT_ERR("Error: Unable to initialise GATT :%s", status2string(ret));
280 return convert_to_oal_status(ret);
283 BT_INFO("GATT successfully initialized");
284 return OAL_STATUS_SUCCESS;
287 oal_status_t gatt_disable(void)
294 g_slist_free_full(gatt_servers, g_free);
296 return OAL_STATUS_SUCCESS;
299 static gatt_server_t *__gatts_find_server_instance(int instance_id)
304 for (l = gatt_servers; l; l = g_slist_next(l)) {
308 if (info->instance_id == instance_id)
315 /************************************GATT Server Functions*************************************/
317 oal_status_t gatts_register(oal_uuid_t* server_uuid)
319 char str[2*BT_UUID_STRING_MAX];
320 int ret = OAL_STATUS_SUCCESS;
323 CHECK_OAL_GATT_ENABLED();
324 uuid_to_stringname((service_uuid_t*)server_uuid->uuid, str);
325 API_TRACE("Register the server instance: UUID: [%s]", str);
327 for (l = gatt_servers; l; l = g_slist_next(l)) {
328 gatt_server_t *info = l->data;
331 if (memcmp(server_uuid->uuid, info->uuid.uuid, sizeof(oal_uuid_t)) == 0) {
332 if (info->instance_id != -1) {
333 BT_ERR("This is resevered UUID for easy set up application \
334 instance_id = %d", info->instance_id);
335 return OAL_STATUS_ALREADY_DONE;
340 ret = gatt_api->server->register_server((bt_uuid_t*)server_uuid);
342 if (ret != BT_STATUS_SUCCESS) {
343 BT_ERR("GATT server registration with UUID:%s failed: %s", str, status2string(ret));
344 return convert_to_oal_status(ret);
346 return OAL_STATUS_SUCCESS;
349 oal_status_t gatts_unregister(int instance_id)
351 int ret = OAL_STATUS_SUCCESS;
353 API_TRACE("Unregister the registered server, instance_id: %d", instance_id);
355 CHECK_OAL_GATT_ENABLED();
356 CHECK_SERVER_INSTANCE(instance_id);
358 info = __gatts_find_server_instance(instance_id);
360 return OAL_STATUS_INTERNAL_ERROR;
362 ret = gatt_api->server->unregister_server(instance_id);
363 if (ret != BT_STATUS_SUCCESS) {
364 BT_ERR("GATT server unregistration failed: %d", instance_id);
365 return convert_to_oal_status(ret);
368 info->instance_id = -1;
369 info->state = GATT_INS_DISABLED;
370 info->cur_adv_state = FALSE;
371 if (instance_id != 1 && instance_id != 2) {
372 gatt_servers = g_slist_remove(gatt_servers, info);
373 BT_DBG("GATT Server Removed. count of gatt_servers: %d",
374 g_slist_length(gatt_servers));
377 return OAL_STATUS_SUCCESS;
380 /* Legacy Advertisement */
381 oal_status_t gatts_start_listen(int instance_id, gboolean enable)
383 int ret = OAL_STATUS_SUCCESS;
385 API_TRACE("instance_id: %d enable: %d", instance_id, enable);
387 CHECK_OAL_GATT_ENABLED();
388 CHECK_SERVER_INSTANCE(instance_id);
389 info = __gatts_find_server_instance(instance_id);
391 return OAL_STATUS_INTERNAL_ERROR;
393 ret = gatt_api->server->listen(instance_id, enable);
394 if (ret != BT_STATUS_SUCCESS) {
395 BT_ERR("Error:Advertising %s on instance_id: %d failed \
396 with status: %s, cur_adv_state: %d",
397 enable ? "Start" : "Stop", instance_id,
398 status2string(ret), info->cur_adv_state);
399 return convert_to_oal_status(ret);
402 return OAL_STATUS_SUCCESS;
405 oal_status_t gatts_stop_advertising(int instance_id)
407 int ret = OAL_STATUS_SUCCESS;
408 API_TRACE("Stop advertising");
410 CHECK_OAL_GATT_ENABLED();
411 CHECK_SERVER_INSTANCE(instance_id);
412 if (__gatts_find_server_instance(instance_id) == NULL)
413 return OAL_STATUS_INTERNAL_ERROR;
415 ret = gatt_api->server->listen(instance_id, FALSE);
416 if (ret != BT_STATUS_SUCCESS) {
417 BT_ERR("Stop Advertising failed for server Instance:%d With Status: %s",
418 instance_id, status2string(ret));
419 return convert_to_oal_status(ret);
421 return OAL_STATUS_SUCCESS;
424 oal_status_t gatts_multi_adv_enable(int instance_id)
426 int ret = OAL_STATUS_SUCCESS;
428 API_TRACE("Start Multi advertising, instance_id: %d", instance_id);
430 CHECK_OAL_GATT_ENABLED();
431 CHECK_SERVER_INSTANCE(instance_id);
432 info = __gatts_find_server_instance(instance_id);
434 return OAL_STATUS_INTERNAL_ERROR;
436 if (gatt_api->server->multi_adv_enable == NULL)
437 return OAL_STATUS_NOT_SUPPORT;
439 /* Platform Requirement: Advertising to be enabled even when no data is set: i.e Empty Advertisement
440 if (gatt_servers[instance_id - 1].state != GATT_INS_DATA_SET){
441 BT_ERR("Not allowed, state: %d, instance_id: %d",
442 gatt_servers[instance_id - 1].state, instance_id);
443 return OAL_STATUS_BUSY;
447 if (info->state == GATT_INS_ENABLED) {
448 BT_ERR("Not allowed, state: %d, instance_id: %d", info->state, instance_id);
449 return OAL_STATUS_ALREADY_DONE;
450 } else if (info->state == GATT_INS_ENABLING) {
451 BT_ERR("Not allowed, state: %d, instance_id: %d", info->state, instance_id);
452 return OAL_STATUS_BUSY;
454 info->state = GATT_INS_ENABLING;
456 ret = gatt_api->server->multi_adv_enable(instance_id);
457 if (ret != BT_STATUS_SUCCESS) {
458 BT_ERR("GATT enable multi advertising failed for server Instance: %d With Status: %s",
459 instance_id, status2string(ret));
460 // gatt_servers[instance_id - 1].state = GATT_INS_DATA_SET;
461 return convert_to_oal_status(ret);
463 return OAL_STATUS_SUCCESS;
466 oal_status_t gatts_multi_adv_disable(int instance_id)
468 int ret = OAL_STATUS_SUCCESS;
470 API_TRACE("Stop Multi advertising, instance_id: %d", instance_id);
472 CHECK_OAL_GATT_ENABLED();
473 CHECK_SERVER_INSTANCE(instance_id);
474 info = __gatts_find_server_instance(instance_id);
476 return OAL_STATUS_INTERNAL_ERROR;
478 if (gatt_api->server->multi_adv_disable == NULL)
479 return OAL_STATUS_NOT_SUPPORT;
481 if (info->state != GATT_INS_ENABLED) {
482 BT_ERR("Not Allowed, state: %d, instance_id: %d", info->state, instance_id);
483 return OAL_STATUS_BUSY;
486 ret = gatt_api->server->multi_adv_disable(instance_id);
487 if (ret != BT_STATUS_SUCCESS) {
488 BT_ERR("GATT disable multi advertising failed for server Instance: %d With Status: %s",
489 instance_id, status2string(ret));
490 return convert_to_oal_status(ret);
492 return OAL_STATUS_SUCCESS;
495 oal_status_t gatts_set_filter_policy(int filter_policy)
497 int ret = OAL_STATUS_SUCCESS;
499 CHECK_OAL_GATT_ENABLED();
501 /* send the filter_policy value to the HAL Layer */
502 ret = gatt_api->server->set_filter_policy(filter_policy);
504 if (ret != BT_STATUS_SUCCESS) {
505 BT_ERR("set_filter_policy failed: %d",ret);
506 return convert_to_oal_status(ret);
509 return OAL_STATUS_SUCCESS;
512 oal_status_t gatts_multi_adv_update(int instance_id,
513 int min_intv, int max_intv,
514 int adv_type, int chnl_map,
515 int tx_power, int timeout_s)
517 int ret = OAL_STATUS_SUCCESS;
518 API_TRACE("Multi advertising Update");
520 CHECK_OAL_GATT_ENABLED();
521 CHECK_SERVER_INSTANCE(instance_id);
522 if (__gatts_find_server_instance(instance_id) == NULL)
523 return OAL_STATUS_INTERNAL_ERROR;
525 if (gatt_api->server->multi_adv_update == NULL)
526 return OAL_STATUS_NOT_SUPPORT;
528 ret = gatt_api->server->multi_adv_update(instance_id,
531 tx_power, timeout_s);
532 if (ret != BT_STATUS_SUCCESS) {
533 BT_ERR("GATT update multi advertising failed for server Instance: %d With Status: %s",
534 instance_id, status2string(ret));
535 return convert_to_oal_status(ret);
537 return OAL_STATUS_SUCCESS;
540 oal_status_t gatts_multi_adv_set_inst_data(int instance_id,
541 oal_ble_multi_adv_param_setup_t *adv_param_setup)
543 int ret = OAL_STATUS_SUCCESS;
544 btgatt_adv_param_setup_t adv_setup;
546 API_TRACE("Set Multi advertising data, instance_id: %d", instance_id);
548 CHECK_OAL_GATT_ENABLED();
549 CHECK_SERVER_INSTANCE(instance_id);
550 info = __gatts_find_server_instance(instance_id);
552 return OAL_STATUS_INTERNAL_ERROR;
554 if (gatt_api->server->multi_adv_set_inst_data == NULL)
555 return OAL_STATUS_NOT_SUPPORT;
557 if (info->state >= GATT_INS_DATA_SETTING && info->state != GATT_INS_DATA_SET) {
558 BT_ERR("Not Allowed, state: %d, instance_id: %d", info->state, instance_id);
559 return OAL_STATUS_BUSY;
561 info->state = GATT_INS_DATA_SETTING;
563 adv_setup.set_scan_rsp = adv_param_setup->set_scan_rsp;
564 adv_setup.include_name = adv_param_setup->include_name;
565 adv_setup.include_txpower = adv_param_setup->include_txpower;
567 adv_setup.include_appearance = adv_param_setup->include_appearance;
569 adv_setup.appearance = adv_param_setup->appearance;
570 adv_setup.manufacturer_data = adv_param_setup->manufacturer_data;
571 adv_setup.manufacturer_data_len = adv_param_setup->manufacturer_data_len;
572 adv_setup.service_data = adv_param_setup->service_data;
573 adv_setup.service_data_len = adv_param_setup->service_data_len;
574 adv_setup.service_uuid = adv_param_setup->service_uuid;
575 adv_setup.service_uuid_len = adv_param_setup->service_uuid_len;
576 /* Solicit UUID handler: Start */
577 adv_setup.solicit_uuid = adv_param_setup->solicit_uuid;
578 adv_setup.solicit_uuid_len = adv_param_setup->solicit_uuid_len;
579 /* Solicit UUID handler: End */
580 adv_setup.min_interval = adv_param_setup->min_interval;
581 adv_setup.max_interval = adv_param_setup->min_interval;
582 adv_setup.adv_type = adv_param_setup->adv_type;
583 adv_setup.chnl_map = adv_param_setup->chnl_map;
584 adv_setup.tx_power = adv_param_setup->tx_power;
585 adv_setup.timeout_s = adv_param_setup->timeout_s;
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, bt_bdaddr_t *bda)
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;
1287 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1289 send_event(OAL_EVENT_GATTS_REQUEST_ACQUIRE_NOTIFY, event, sizeof(event_gatts_srvc_acquire_attr_t));
1293 static void cb_gatts_response_confirmation(int status, int handle)
1295 BT_INFO("BTGATT SERVER RESPONSE CONFIRMATION CB, status:%d, handle:%d", status, handle);
1297 event_gatts_respons_t* event = g_new0(event_gatts_respons_t, 1);
1299 event->hndl = handle;
1300 event->status = convert_to_oal_status(status);
1302 send_event(OAL_EVENT_GATTS_RESPONSE_CONFIRMED, event, sizeof(event_gatts_respons_t));
1305 static void cb_indication_confirmation(int conn_id, int trans_id, int attr_handle, bt_bdaddr_t *bda)
1307 BT_INFO("BTGATT SERVER Indication Confirmation CB, conn_id:%d, trans_id:%d attr_handle:%d",
1308 conn_id, trans_id, attr_handle);
1310 event_gatts_ind_cnfrm_t* event = g_new0(event_gatts_ind_cnfrm_t, 1);
1312 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1314 event->attr_handle = attr_handle;
1315 event->conn_id = conn_id;
1316 event->trans_id = trans_id;
1317 send_event(OAL_EVENT_GATTS_IND_CONFIRM, event, sizeof(event_gatts_ind_cnfrm_t));
1320 static void cb_gatts_mtu_changed(int conn_id, int mtu)
1322 BT_INFO("BTGATT SERVER MTU CHANGED CB, conn_id: %d, mtu_size: %d", conn_id, mtu);
1323 event_gatts_mtu_changed_t *event = g_new0(event_gatts_mtu_changed_t, 1);
1324 event->conn_id = conn_id;
1325 event->mtu_size = mtu;
1326 send_event(OAL_EVENT_GATTS_MTU_CHANGED, event, sizeof(event_gatts_mtu_changed_t));
1330 static void cb_notifcation_changed(int conn_id, int trans_id, int attr_handle, bool notify, bt_bdaddr_t *bda)
1332 BT_INFO("BTGATT SERVER Notication CB, conn_id:%d, trans_id:%d attr_handle:%d Notify:%d",
1333 conn_id, trans_id, attr_handle, notify);
1335 event_gatts_notif_t* event = g_new0(event_gatts_notif_t, 1);
1337 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1339 event->attr_handle = attr_handle;
1340 event->conn_id = conn_id;
1341 event->trans_id = trans_id;
1342 event->notify = notify;
1343 send_event(OAL_EVENT_GATTS_NOTIFICATION, event, sizeof(event_gatts_notif_t));
1348 /* GATT Server Callbacks: End */
1349 /************************************ GATT Client ***********************************/
1350 /* Client Callbacks */
1351 static void cb_gattc_register_app(int status, int clientIf, bt_uuid_t *app_uuid)
1353 char uuid_str[BT_UUID_STRING_MAX];
1354 event_gattc_register_t *event;
1356 /* Check if GATT client registered for Default GATT client UUID */
1357 uuid_to_string((service_uuid_t *)app_uuid, uuid_str);
1359 BT_INFO("GATT CLIENT REGISTER APP CB, status:%d, clientIf:%d, %s", status, clientIf, uuid_str);
1360 event = g_new0(event_gattc_register_t, 1);
1361 event->client_if = clientIf;
1362 event->status = (status == 0 ? OAL_STATUS_SUCCESS : OAL_STATUS_INTERNAL_ERROR);
1363 memcpy(event->client_uuid.uuid, app_uuid->uu, sizeof(oal_uuid_t));
1364 send_event(OAL_EVENT_GATTC_REGISTRATION, event, sizeof(event_gattc_register_t));
1367 static void cb_gattc_scan_result(bt_bdaddr_t* bdaddress, uint8_t addr_type, int rssi,
1368 uint8_t *adv_data, int adv_data_len, uint8_t *scan_rsp_data, int scan_rsp_data_len)
1370 event_ble_scan_result_info *event;
1372 event = g_new0(event_ble_scan_result_info, 1);
1373 event->addr_type = addr_type;
1375 memcpy(event->address.addr, bdaddress->address, BT_ADDRESS_BYTES_NUM);
1376 memcpy(event->adv_data, adv_data, BLE_ADV_DATA_LENGTH);
1377 event->adv_data_len = adv_data_len;
1378 memcpy(event->scan_rsp_data, scan_rsp_data, BLE_ADV_DATA_LENGTH);
1379 event->scan_rsp_data_len = scan_rsp_data_len;
1380 send_event_bda_trace(OAL_EVENT_BLE_REMOTE_DEVICE_FOUND, event,
1381 sizeof(event_ble_scan_result_info), (bt_address_t *)bdaddress);
1384 static void cb_gattc_connection(int conn_id, int status, int client_if, bt_bdaddr_t* bda)
1386 event_gattc_conn_t *event;
1387 BT_INFO("BTGATT CLIENT CONNECTED CB, status:[%d] client_if[%d] id[%d]",
1388 status, client_if, conn_id);
1390 event = g_new0(event_gattc_conn_t, 1);
1391 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1392 event->client_if = client_if;
1394 if (BT_STATUS_SUCCESS != status) {
1395 event->status = OAL_STATUS_INTERNAL_ERROR;
1396 BT_ERR("gattc connection Error: %d", status);
1398 event->conn_id = conn_id;
1399 event->status = OAL_STATUS_SUCCESS;
1402 send_event_bda_trace(OAL_EVENT_GATTC_CONNECTION_COMPLETED, event,
1403 sizeof(*event), (bt_address_t *)bda);
1407 static void cb_gattc_disconnect(int conn_id, int status, int client_if, bt_bdaddr_t* bda)
1409 event_gattc_conn_t *event;
1410 BT_INFO("BTGATT Client Disconnect Complete cb, status:[%d], client_if[%d] id[%d]",
1411 status, client_if, conn_id);
1412 event = g_new0(event_gattc_conn_t, 1);
1413 event->status = convert_to_oal_status(status);
1414 event->client_if = client_if;
1415 event->conn_id = conn_id;
1416 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1417 send_event_bda_trace(OAL_EVENT_GATTC_DISCONNECTION_COMPLETED, event,
1418 sizeof(*event), (bt_address_t *)bda);
1421 static void cb_gattc_search_result(int conn_id, btgatt_srvc_id_t *srvc_id)
1423 char uuid_str[2*BT_UUID_STRING_MAX];
1425 uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str);
1426 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]",
1427 uuid_str, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1429 event_gattc_service_result_t *event = g_new0(event_gattc_service_result_t, 1);
1430 event->conn_status.status = OAL_STATUS_SUCCESS;
1431 event->conn_status.conn_id = conn_id;
1432 memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1434 send_event(OAL_EVENT_GATTC_SERVICE_SEARCH_RESULT, event, sizeof(*event));
1437 static void cb_gattc_search_complete(int conn_id, int status)
1439 BT_INFO("BTGATT Client Service Search Complete cb, status:%d, conn_id:%d",
1442 event_gattc_conn_status_t *event = g_new0(event_gattc_conn_status_t, 1);
1443 event->conn_id = conn_id;
1444 event->status = convert_to_oal_status(status);
1446 send_event(OAL_EVENT_GATTC_SERVICE_SEARCH_DONE, event, sizeof(*event));
1450 static void cb_gattc_get_characteristics(int conn_id, int status, btgatt_srvc_id_t *srvc_id,
1451 btgatt_gatt_id_t *char_id, int char_prop)
1453 char uuid_str1[2*BT_UUID_STRING_MAX];
1454 char uuid_str2[2*BT_UUID_STRING_MAX];
1457 BT_DBG("BTGATT Client Get Characteristic Callback, conn_id:%d, status:%d", conn_id, status);
1458 uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str1);
1459 BT_DBG("Service=> [%s], Inst_id [%u], Type [%s]",
1460 uuid_str1, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1462 event_gattc_characteristic_result_t *event = g_new0(event_gattc_characteristic_result_t, 1);
1463 event->conn_status.conn_id = conn_id;
1464 event->conn_status.status = convert_to_oal_status(status);
1465 memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1468 uuid_to_stringname((oal_uuid_t *)&(char_id->uuid), uuid_str2);
1469 BT_INFO("Charac => [%s], Inst_id [%u], Prop [%s]",
1470 uuid_str2, char_id->inst_id, char_prop_to_string(char_prop, str));
1471 event->char_prop = char_prop;
1472 memcpy(&(event->char_id), char_id, sizeof(oal_gatt_id_t));
1474 send_event(OAL_EVENT_GATTC_CHARAC_SERACH_RESULT, event, sizeof(*event));
1479 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)
1481 char uuid_str1[2*BT_UUID_STRING_MAX];
1482 char uuid_str2[2*BT_UUID_STRING_MAX];
1483 char uuid_str3[2*BT_UUID_STRING_MAX];
1484 BT_DBG("BTGATT Client Get Descriptor Callback, conn_id:%d, status:%d", conn_id, status);
1485 uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str1);
1486 uuid_to_stringname((oal_uuid_t *)&(char_id->uuid), uuid_str2);
1487 BT_DBG("Service=> [%s], Inst_id [%u], Type [%s]",
1488 uuid_str1, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1489 BT_DBG("Charac => [%s], Inst_id [%u]", uuid_str2, char_id->inst_id);
1491 event_gattc_descriptor_result_t *event = g_new0(event_gattc_descriptor_result_t, 1);
1492 event->conn_status.conn_id = conn_id;
1493 event->conn_status.status = convert_to_oal_status(status);
1494 memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1495 memcpy(&(event->char_id), char_id, sizeof(oal_gatt_id_t));
1498 uuid_to_stringname((oal_uuid_t *)&(descr_id->uuid), uuid_str3);
1499 BT_INFO("Desc => [%s], Inst_id [%u]", uuid_str3, descr_id->inst_id);
1500 memcpy(&(event->descr_id), descr_id, sizeof(oal_gatt_id_t));
1502 send_event(OAL_EVENT_GATTC_DESC_SERACH_RESULT, event, sizeof(*event));
1505 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)
1507 char uuid_str1[2*BT_UUID_STRING_MAX];
1508 char uuid_str2[2*BT_UUID_STRING_MAX];
1509 BT_INFO("BTGATT Client Register For Notification Callback, conn_id:%d, status:%d, registered: %s",
1510 conn_id, status, registered == 1 ? "REGISTERED" : "DEREGISTERED");
1511 uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str1);
1512 uuid_to_stringname((oal_uuid_t *)&(char_id->uuid), uuid_str2);
1513 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1514 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, char_id->inst_id);
1516 event_gattc_regdereg_notify_t *event = g_new0(event_gattc_regdereg_notify_t, 1);
1517 oal_event_t event_type = (registered == 1 ? OAL_EVENT_GATTC_NOTIFICATION_REGISTERED : OAL_EVENT_GATTC_NOTIFICATION_DEREGISTERED);
1518 event->conn_id = conn_id;
1519 event->status = convert_to_oal_status(status);
1520 memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1521 memcpy(&(event->char_id), char_id, sizeof(oal_gatt_id_t));
1523 send_event(event_type, event, sizeof(*event));
1526 static void cb_gattc_notify(int conn_id, btgatt_notify_params_t *p_data)
1529 char uuid_str1[2*BT_UUID_STRING_MAX];
1530 char uuid_str2[2*BT_UUID_STRING_MAX];
1532 BT_INFO("BTGATT Client Notify Callback, conn_id:%d", conn_id);
1533 BT_INFO("Server Address:[%s], is_notify:[%d], len:[%d]",
1534 bdt_bd2str((bt_address_t *)&(p_data->bda), &bdstr), p_data->is_notify, p_data->len);
1536 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1537 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1539 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]",
1540 uuid_str1, p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1541 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1543 if (p_data->len > 0) {
1545 data = g_malloc(3*p_data->len+1);
1547 BT_ERR("memory allocation failed");
1551 convert_hex_2_str((unsigned char *)p_data->value, p_data->len, data);
1552 BT_INFO("Notified Data: [%s]", data);
1554 event_gattc_notify_data *event = g_new0(event_gattc_notify_data, 1);
1555 memcpy(event->address.addr, p_data->bda.address, BT_ADDRESS_BYTES_NUM);
1556 event->is_notify = p_data->is_notify;
1557 event->data_len = p_data->len;
1558 memcpy(event->data, p_data->value, event->data_len);
1559 memcpy(&(event->char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1560 memcpy(&(event->srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1562 send_event_bda_trace(OAL_EVENT_GATTC_NOTIFY_DATA, event, sizeof(*event), (bt_address_t *)&p_data->bda);
1567 static void cb_gattc_service_changed(bt_bdaddr_t *bd_addr, int change_type, uint8_t *uuid, int conn_id, int inst_id)
1569 event_gattc_service_changed_data *event = g_new0(event_gattc_service_changed_data, 1);
1571 memcpy(event->address.addr, bd_addr->address, 6);
1572 event->change_type = change_type;
1573 memcpy(event->uuid.uuid, uuid, sizeof(event->uuid.uuid));
1574 event->conn_id = conn_id;
1575 event->inst_id = inst_id;
1577 send_event_bda_trace(OAL_EVENT_GATTC_SERVICE_CHANGED_IND, event, sizeof(*event), (bt_address_t *)bd_addr);
1580 static void cb_gattc_read_characteristic(int conn_id, int status, btgatt_read_params_t *p_data)
1582 char uuid_str1[2*BT_UUID_STRING_MAX];
1583 char uuid_str2[2*BT_UUID_STRING_MAX];
1584 BT_DBG("BTGATT Client Read Charcateristic Callback, conn_id:%d, read_status:%d", conn_id, status);
1586 event_gattc_read_data *event = g_new0(event_gattc_read_data, 1);
1587 event->uuid_status.conn_status.conn_id = conn_id;
1588 event->uuid_status.conn_status.status = convert_to_oal_status(status);
1589 event->value_type = p_data->value_type;
1590 memcpy(&(event->uuid_status.srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1591 memcpy(&(event->uuid_status.char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1593 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1594 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1595 BT_DBG("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1,
1596 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1597 BT_DBG("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1598 BT_DBG("Len: %u, value_type: %u", p_data->value.len, p_data->value_type);
1599 if (p_data->value.len > 0 && event->value_type == GATTC_READ_VALUE_TYPE_VALUE) {
1600 event->data_len = p_data->value.len;
1601 memcpy(&(event->data), &(p_data->value.value), event->data_len);
1604 send_event(OAL_EVENT_GATTC_READ_CHARAC, event, sizeof(*event));
1607 static void cb_gattc_write_characteristic(int conn_id, int status, btgatt_write_params_t *p_data)
1609 char uuid_str1[2*BT_UUID_STRING_MAX];
1610 char uuid_str2[2*BT_UUID_STRING_MAX];
1611 BT_INFO("BTGATT Client Write Charcateristic Callback, conn_id:%d, write_status:%d", conn_id, status);
1613 event_gattc_write_data *event = g_new0(event_gattc_write_data, 1);
1614 event->conn_status.conn_id = conn_id;
1615 event->conn_status.status = convert_to_oal_status(status);
1616 memcpy(&(event->srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1617 memcpy(&(event->char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1619 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1620 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1621 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1,
1622 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1623 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1625 send_event(OAL_EVENT_GATTC_WRITE_CHARAC, event, sizeof(*event));
1628 static void cb_gattc_write_descriptor(int conn_id, int status, btgatt_write_params_t *p_data)
1630 char uuid_str1[2*BT_UUID_STRING_MAX];
1631 char uuid_str2[2*BT_UUID_STRING_MAX];
1632 char uuid_str3[2*BT_UUID_STRING_MAX];
1633 BT_DBG("BTGATT Client Write Descriptor Callback, conn_id:%d, write_status:%d", conn_id, status);
1635 event_gattc_write_data *event = g_new0(event_gattc_write_data, 1);
1636 event->conn_status.conn_id = conn_id;
1637 event->conn_status.status = convert_to_oal_status(status);
1638 memcpy(&(event->srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1639 memcpy(&(event->char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1640 memcpy(&(event->descr_id), &(p_data->descr_id), sizeof(oal_gatt_id_t));
1642 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1643 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1644 uuid_to_stringname((oal_uuid_t *)&(p_data->descr_id.uuid), uuid_str3);
1645 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1,
1646 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1647 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1648 BT_INFO("Desc => [%s], Inst_id [%u]", uuid_str3, p_data->descr_id.inst_id);
1650 send_event(OAL_EVENT_GATTC_WRITE_DESCR, event, sizeof(*event));
1655 static void cb_gattc_read_descriptor(int conn_id, int status, btgatt_read_params_t *p_data)
1657 char uuid_str1[2*BT_UUID_STRING_MAX];
1658 char uuid_str2[2*BT_UUID_STRING_MAX];
1659 char uuid_str3[2*BT_UUID_STRING_MAX];
1660 BT_INFO("BTGATT Client Read Descriptor Callback, conn_id:%d, read_status:%d", conn_id, status);
1662 event_gattc_read_data *event = g_new0(event_gattc_read_data, 1);
1663 event->uuid_status.conn_status.conn_id = conn_id;
1664 event->uuid_status.conn_status.status = convert_to_oal_status(status);
1665 event->value_type = p_data->value_type;
1666 memcpy(&(event->uuid_status.srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1667 memcpy(&(event->uuid_status.char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1668 memcpy(&(event->uuid_status.descr_id), &(p_data->descr_id), sizeof(oal_gatt_id_t));
1671 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1672 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1673 uuid_to_stringname((oal_uuid_t *)&(p_data->descr_id.uuid), uuid_str3);
1674 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1,
1675 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1676 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1677 BT_INFO("Desc => [%s], Inst_id [%u]", uuid_str3, p_data->descr_id.inst_id);
1678 BT_DBG("Len: %u, value_type: %u", p_data->value.len, p_data->value_type);
1679 if (p_data->value.len > 0 && event->value_type == GATTC_READ_VALUE_TYPE_VALUE) {
1681 data = g_malloc(3*p_data->value.len+1);
1683 BT_ERR("memory allocation failed");
1687 convert_hex_2_str((unsigned char *)p_data->value.value, p_data->value.len, data);
1688 BT_DBG("Read Data: [%s]", data);
1689 event->data_len = p_data->value.len;
1690 memcpy(&(event->data), &(p_data->value.value), event->data_len);
1694 send_event(OAL_EVENT_GATTC_READ_DESCR, event, sizeof(*event));
1697 oal_status_t gattc_start_le_discovery(int client_id)
1700 int ret = OAL_STATUS_SUCCESS;
1702 API_TRACE("BTGATT CLIENT SCAN START");
1703 CHECK_OAL_GATT_ENABLED();
1704 CHECK_CLIENT_REGISTRATION(client_id);
1706 ret = gatt_api->client->scan(client_id, 1);
1707 if (ret != BT_STATUS_SUCCESS) {
1708 BT_ERR("Error:Start LE Discovery failed: %s", status2string(ret));
1709 return convert_to_oal_status(ret);
1711 return OAL_STATUS_SUCCESS;
1714 oal_status_t gattc_stop_le_discovery(int client_id)
1717 int ret = OAL_STATUS_SUCCESS;
1719 API_TRACE("BTGATT CLIENT SCAN STOP");
1720 CHECK_OAL_GATT_ENABLED();
1721 CHECK_CLIENT_REGISTRATION(client_id);
1722 ret = gatt_api->client->scan(client_id, 0);
1723 if (ret != BT_STATUS_SUCCESS) {
1724 BT_ERR("Error:Stop LE Discovery failed: %s", status2string(ret));
1725 return convert_to_oal_status(ret);
1727 return OAL_STATUS_SUCCESS;
1730 oal_status_t gattc_set_le_scan_param(int scan_type, int itv, int win)
1734 API_TRACE("GATT client set le scan param");
1735 CHECK_OAL_GATT_ENABLED();
1738 ret = gatt_api->client->set_scan_parameters(scan_type, itv, win);
1739 if (ret != BT_STATUS_SUCCESS) {
1740 BT_ERR("Error:Set scan parameters failed: %s", status2string(ret));
1741 return convert_to_oal_status(ret);
1744 ret = gatt_api->client->set_scan_parameters(itv, win);
1745 if (ret != BT_STATUS_SUCCESS) {
1746 BT_ERR("Error:Set scan parameters failed: %s", status2string(ret));
1747 return convert_to_oal_status(ret);
1751 return OAL_STATUS_SUCCESS;
1754 static void cb_gattc_configure_mtu_cmpl(int conn_id, int status, int mtu)
1756 event_gattc_mtu_configured_t *event;
1757 BT_INFO("BTGATT Client configure mtu complete Callback, conn_id: %d, status: %d, mtu: %d",
1758 conn_id, status, mtu);
1759 event = g_new0(event_gattc_mtu_configured_t, 1);
1760 event->conn_id = conn_id;
1762 event->status = convert_to_oal_status(status);
1763 send_event(OAL_EVENT_GATTC_MTU_EXCHANGE_COMPLETED, event, sizeof(*event));
1766 /************************************ GATT Client ***********************************/
1767 oal_status_t gattc_register(oal_uuid_t* client_uuid)
1770 char str[2*BT_UUID_STRING_MAX];
1771 int ret = OAL_STATUS_SUCCESS;
1773 CHECK_OAL_GATT_ENABLED();
1774 uuid_to_stringname(client_uuid, str);
1775 API_TRACE("uuid: [%s]", str);
1776 /* We will perform actual registration in cb_gattc_register_app callback */
1777 ret = gatt_api->client->register_client((bt_uuid_t *)client_uuid);
1778 if (ret != BT_STATUS_SUCCESS) {
1779 BT_ERR("GATT client register failed: %s", status2string(ret));
1780 return convert_to_oal_status(ret);
1782 return OAL_STATUS_SUCCESS;
1785 oal_status_t gattc_deregister(int client_id)
1787 int ret = OAL_STATUS_SUCCESS;
1789 API_TRACE("GATT client deregister");
1790 CHECK_OAL_GATT_ENABLED();
1791 CHECK_CLIENT_REGISTRATION(client_id);
1793 ret = gatt_api->client->unregister_client(client_id);
1794 if (ret != BT_STATUS_SUCCESS) {
1795 BT_ERR("GATT client deregister failed: %s", status2string(ret));
1796 return convert_to_oal_status(ret);
1799 return OAL_STATUS_SUCCESS;
1802 oal_status_t gattc_connect(int client_id, bt_address_t *device_address, int isDirect)
1804 int ret = OAL_STATUS_SUCCESS;
1807 OAL_CHECK_PARAMETER(device_address, return);
1808 API_TRACE("Client Connect: [%s]", bdt_bd2str(device_address, &bdstr) + 12);
1809 CHECK_OAL_GATT_ENABLED();
1810 CHECK_CLIENT_REGISTRATION(client_id);
1812 /* Handle the actual connection in cb_gattc_connection callback */
1813 ret = gatt_api->client->connect(client_id, (bt_bdaddr_t *)device_address, isDirect);
1814 if (ret != BT_STATUS_SUCCESS) {
1815 BT_ERR("GATT client connect failed: %s", status2string(ret));
1816 return convert_to_oal_status(ret);
1818 return OAL_STATUS_SUCCESS;
1821 oal_status_t gattc_disconnect(int client_id, bt_address_t *device_address, int conn_id)
1824 int ret = OAL_STATUS_SUCCESS;
1827 OAL_CHECK_PARAMETER(device_address, return);
1828 API_TRACE("Client Disconnect: [%s]", bdt_bd2str(device_address, &bdstr) + 12);
1829 CHECK_OAL_GATT_ENABLED();
1830 CHECK_CLIENT_REGISTRATION(client_id);
1831 CHECK_CLIENT_CONNECTION(conn_id);
1833 /* Handle actual disconnection in callback */
1834 ret = gatt_api->client->disconnect(client_id, (bt_bdaddr_t *) device_address, conn_id);
1835 if (ret != BT_STATUS_SUCCESS) {
1836 BT_ERR("GATT client disconnect failed: %s", status2string(ret));
1837 return convert_to_oal_status(ret);
1839 return OAL_STATUS_SUCCESS;
1843 oal_status_t gattc_search_service(int conn_id, oal_uuid_t *service_uuid)
1845 int ret = OAL_STATUS_SUCCESS;
1846 char uuid_str[2*BT_UUID_STRING_MAX];
1849 uuid_to_stringname(service_uuid, uuid_str);
1850 API_TRACE("Client Service Search UUID: [%s]", uuid_str);
1852 API_TRACE("Client Service Search All");
1854 CHECK_OAL_GATT_ENABLED();
1855 CHECK_CLIENT_CONNECTION(conn_id);
1856 ret = gatt_api->client->search_service(conn_id, (bt_uuid_t *)service_uuid);
1858 if (ret != BT_STATUS_SUCCESS) {
1859 BT_ERR("GATT client service search failed: %s", status2string(ret));
1860 return convert_to_oal_status(ret);
1862 return OAL_STATUS_SUCCESS;
1865 oal_status_t gattc_get_characteristic(int conn_id, oal_gatt_srvc_id_t *srvc_id, oal_gatt_id_t *char_id)
1867 int ret = OAL_STATUS_SUCCESS;
1868 char uuid_str[2*BT_UUID_STRING_MAX];
1870 OAL_CHECK_PARAMETER(srvc_id, return);
1871 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str);
1872 API_TRACE("Client Get Characteristic [%s]", uuid_str);
1873 CHECK_OAL_GATT_ENABLED();
1874 CHECK_CLIENT_CONNECTION(conn_id);
1875 ret = gatt_api->client->get_characteristic(conn_id, (btgatt_srvc_id_t *)srvc_id,
1876 (btgatt_gatt_id_t *)char_id);
1877 if (ret != BT_STATUS_SUCCESS) {
1878 BT_ERR("GATT client get characteristic failed: %s", status2string(ret));
1879 return convert_to_oal_status(ret);
1881 return OAL_STATUS_SUCCESS;
1884 oal_status_t gattc_get_descriptor(int conn_id, oal_gatt_srvc_id_t *srvc_id,
1885 oal_gatt_id_t *char_id, oal_gatt_id_t *desc_id)
1887 int ret = OAL_STATUS_SUCCESS;
1888 char uuid_str1[2*BT_UUID_STRING_MAX];
1889 char uuid_str2[2*BT_UUID_STRING_MAX];
1891 OAL_CHECK_PARAMETER(srvc_id, return);
1892 OAL_CHECK_PARAMETER(char_id, return);
1893 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1894 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1895 API_TRACE("Client Get Descriptor, Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
1896 CHECK_OAL_GATT_ENABLED();
1897 CHECK_CLIENT_CONNECTION(conn_id);
1898 ret = gatt_api->client->get_descriptor(conn_id, (btgatt_srvc_id_t *)srvc_id,
1899 (btgatt_gatt_id_t *)char_id, (btgatt_gatt_id_t *)desc_id);
1900 if (ret != BT_STATUS_SUCCESS) {
1901 BT_ERR("GATT client get descriptor failed: %s", status2string(ret));
1902 return convert_to_oal_status(ret);
1904 return OAL_STATUS_SUCCESS;
1907 oal_status_t gattc_register_for_notification(int conn_id, bt_address_t * address,
1908 oal_gatt_srvc_id_t *srvc_id, oal_gatt_id_t *char_id) {
1909 int ret = OAL_STATUS_SUCCESS;
1910 char uuid_str1[2*BT_UUID_STRING_MAX];
1911 char uuid_str2[2*BT_UUID_STRING_MAX];
1914 OAL_CHECK_PARAMETER(address, return);
1915 OAL_CHECK_PARAMETER(srvc_id, return);
1916 OAL_CHECK_PARAMETER(char_id, return);
1917 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1918 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1919 API_TRACE("Client Register Notification: [%s], Service_uuid: [%s], Char_uuid: [%s]", bdt_bd2str(address, &bdstr), uuid_str1, uuid_str2);
1920 CHECK_OAL_GATT_ENABLED();
1921 CHECK_CLIENT_CONNECTION(conn_id);
1923 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);
1924 if (ret != BT_STATUS_SUCCESS) {
1925 BT_ERR("GATT client register notification failed: %s", status2string(ret));
1926 return convert_to_oal_status(ret);
1929 return OAL_STATUS_SUCCESS;
1932 oal_status_t gattc_deregister_for_notification(int conn_id, bt_address_t * address,
1933 oal_gatt_srvc_id_t *srvc_id, oal_gatt_id_t *char_id) {
1934 int ret = OAL_STATUS_SUCCESS;
1935 char uuid_str1[2*BT_UUID_STRING_MAX];
1936 char uuid_str2[2*BT_UUID_STRING_MAX];
1939 OAL_CHECK_PARAMETER(address, return);
1940 OAL_CHECK_PARAMETER(srvc_id, return);
1941 OAL_CHECK_PARAMETER(char_id, return);
1942 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1943 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1944 API_TRACE("Client Deregister Notification: [%s], Service_uuid: [%s], Char_uuid: [%s]", bdt_bd2str(address, &bdstr), uuid_str1, uuid_str2);
1945 CHECK_OAL_GATT_ENABLED();
1946 CHECK_CLIENT_CONNECTION(conn_id);
1948 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);
1949 if (ret != BT_STATUS_SUCCESS) {
1950 BT_ERR("GATT client deregister notification failed: %s", status2string(ret));
1951 return convert_to_oal_status(ret);
1954 return OAL_STATUS_SUCCESS;
1958 oal_status_t gattc_read_characteristic(int conn_id, oal_gatt_srvc_id_t *srvc_id,
1959 oal_gatt_id_t *char_id, oal_gatt_auth_req_t auth_req)
1961 int ret = OAL_STATUS_SUCCESS;
1962 char uuid_str1[2*BT_UUID_STRING_MAX];
1963 char uuid_str2[2*BT_UUID_STRING_MAX];
1965 OAL_CHECK_PARAMETER(srvc_id, return);
1966 OAL_CHECK_PARAMETER(char_id, return);
1967 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1968 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1969 API_TRACE("Client Read Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
1970 CHECK_OAL_GATT_ENABLED();
1971 CHECK_CLIENT_CONNECTION(conn_id);
1973 ret = gatt_api->client->read_characteristic(conn_id, (btgatt_srvc_id_t *)srvc_id,
1974 (btgatt_gatt_id_t *)char_id, auth_req);
1975 if (ret != BT_STATUS_SUCCESS) {
1976 BT_ERR("GATT client read characteristic failed: %s", status2string(ret));
1977 return convert_to_oal_status(ret);
1980 return OAL_STATUS_SUCCESS;
1983 oal_status_t gattc_read_descriptor(int conn_id, oal_gatt_srvc_id_t *srvc_id,
1984 oal_gatt_id_t *char_id, oal_gatt_id_t *desc_id, oal_gatt_auth_req_t auth_req)
1986 int ret = OAL_STATUS_SUCCESS;
1987 char uuid_str1[2*BT_UUID_STRING_MAX];
1988 char uuid_str2[2*BT_UUID_STRING_MAX];
1989 char uuid_str3[2*BT_UUID_STRING_MAX];
1991 OAL_CHECK_PARAMETER(srvc_id, return);
1992 OAL_CHECK_PARAMETER(char_id, return);
1993 OAL_CHECK_PARAMETER(desc_id, return);
1994 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1995 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1996 uuid_to_stringname(&(desc_id->uuid), uuid_str3);
1997 API_TRACE("Client Read Descriptor: Service_uuid: [%s], Char_uuid: [%s], Desc_uuid: [%s]",
1998 uuid_str1, uuid_str2, uuid_str3);
1999 CHECK_OAL_GATT_ENABLED();
2000 CHECK_CLIENT_CONNECTION(conn_id);
2002 ret = gatt_api->client->read_descriptor(conn_id, (btgatt_srvc_id_t *)srvc_id,
2003 (btgatt_gatt_id_t *)char_id, (btgatt_gatt_id_t *)desc_id, auth_req);
2004 if (ret != BT_STATUS_SUCCESS) {
2005 BT_ERR("GATT client read descriptor failed: %s", status2string(ret));
2006 return convert_to_oal_status(ret);
2009 return OAL_STATUS_SUCCESS;
2012 oal_status_t gattc_acquire_notify(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2013 oal_gatt_id_t *char_id, int *fd, int *mtu)
2015 int ret = OAL_STATUS_SUCCESS;
2016 char uuid_str1[2*BT_UUID_STRING_MAX];
2017 char uuid_str2[2*BT_UUID_STRING_MAX];
2018 OAL_CHECK_PARAMETER(srvc_id, return);
2019 OAL_CHECK_PARAMETER(char_id, return);
2020 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2021 uuid_to_stringname(&(char_id->uuid), uuid_str2);
2022 API_TRACE("Client Write Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
2023 CHECK_OAL_GATT_ENABLED();
2024 CHECK_CLIENT_CONNECTION(conn_id);
2026 ret = gatt_api->client->acquire_notify(conn_id, (btgatt_srvc_id_t *)srvc_id,
2027 (btgatt_gatt_id_t *)char_id, fd, mtu);
2028 if (ret != BT_STATUS_SUCCESS) {
2029 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
2030 return convert_to_oal_status(ret);
2033 return OAL_STATUS_SUCCESS;
2036 oal_status_t gattc_acquire_write(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2037 oal_gatt_id_t *char_id, oal_gatt_auth_req_t auth_req , int *fd, int*mtu)
2039 int ret = OAL_STATUS_SUCCESS;
2040 char uuid_str1[2*BT_UUID_STRING_MAX];
2041 char uuid_str2[2*BT_UUID_STRING_MAX];
2042 OAL_CHECK_PARAMETER(srvc_id, return);
2043 OAL_CHECK_PARAMETER(char_id, return);
2044 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2045 uuid_to_stringname(&(char_id->uuid), uuid_str2);
2046 API_TRACE("Client Write Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
2047 CHECK_OAL_GATT_ENABLED();
2048 CHECK_CLIENT_CONNECTION(conn_id);
2050 ret = gatt_api->client->acquire_write(conn_id, (btgatt_srvc_id_t *)srvc_id,
2051 (btgatt_gatt_id_t *)char_id, auth_req, fd, mtu);
2052 if (ret != BT_STATUS_SUCCESS) {
2053 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
2054 return convert_to_oal_status(ret);
2057 return OAL_STATUS_SUCCESS;
2060 oal_status_t gattc_write_characteristic(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2061 oal_gatt_id_t *char_id, oal_gatt_write_type_t write_type,
2062 int len, oal_gatt_auth_req_t auth_req, char* data)
2064 int ret = OAL_STATUS_SUCCESS;
2065 char uuid_str1[2*BT_UUID_STRING_MAX];
2066 char uuid_str2[2*BT_UUID_STRING_MAX];
2067 OAL_CHECK_PARAMETER(srvc_id, return);
2068 OAL_CHECK_PARAMETER(char_id, return);
2069 OAL_CHECK_PARAMETER(data, return);
2070 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2071 uuid_to_stringname(&(char_id->uuid), uuid_str2);
2072 API_TRACE("Client Write Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
2073 CHECK_OAL_GATT_ENABLED();
2074 CHECK_CLIENT_CONNECTION(conn_id);
2076 ret = gatt_api->client->write_characteristic(conn_id, (btgatt_srvc_id_t *)srvc_id,
2077 (btgatt_gatt_id_t *)char_id, write_type, len, auth_req, data);
2078 if (ret != BT_STATUS_SUCCESS) {
2079 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
2080 return convert_to_oal_status(ret);
2083 return OAL_STATUS_SUCCESS;
2086 oal_status_t gattc_write_descriptor(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2087 oal_gatt_id_t *char_id, oal_gatt_id_t *desc_id,
2088 oal_gatt_write_type_t write_type, int len, oal_gatt_auth_req_t auth_req, char* data)
2090 int ret = OAL_STATUS_SUCCESS;
2091 char uuid_str1[2*BT_UUID_STRING_MAX];
2092 char uuid_str2[2*BT_UUID_STRING_MAX];
2093 char uuid_str3[2*BT_UUID_STRING_MAX];
2094 OAL_CHECK_PARAMETER(srvc_id, return);
2095 OAL_CHECK_PARAMETER(char_id, return);
2096 OAL_CHECK_PARAMETER(desc_id, return);
2097 OAL_CHECK_PARAMETER(data, return);
2098 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2099 uuid_to_stringname(&(char_id->uuid), uuid_str2);
2100 uuid_to_stringname(&(desc_id->uuid), uuid_str3);
2101 API_TRACE("Client Write Descriptor: Service_uuid: [%s], Char_uuid: [%s], Desc_uuid: [%s]",
2102 uuid_str1, uuid_str2, uuid_str3);
2103 CHECK_OAL_GATT_ENABLED();
2104 CHECK_CLIENT_CONNECTION(conn_id);
2106 ret = gatt_api->client->write_descriptor(conn_id, (btgatt_srvc_id_t *)srvc_id,
2107 (btgatt_gatt_id_t *)char_id, (btgatt_gatt_id_t *)desc_id, write_type, len, auth_req, data);
2108 if (ret != BT_STATUS_SUCCESS) {
2109 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
2110 return convert_to_oal_status(ret);
2113 return OAL_STATUS_SUCCESS;
2116 oal_status_t gattc_conn_param_update(bt_address_t * address, int min, int max, int latency, int timeout)
2121 OAL_CHECK_PARAMETER(address, return);
2122 CHECK_OAL_GATT_ENABLED();
2124 BT_DBG("[%s] min[%d] max[%d] latency[%d] timeout[%d]", bdt_bd2str(address, &bdstr), min, max, latency, timeout);
2125 ret = gatt_api->client->conn_parameter_update((bt_bdaddr_t *)address, min, max, latency, timeout);
2126 if (ret != BT_STATUS_SUCCESS) {
2127 BT_ERR("error: %s", status2string(ret));
2128 return convert_to_oal_status(ret);
2130 return OAL_STATUS_SUCCESS;
2133 oal_status_t gattc_unregister_scan_filter(int slot_id)
2139 CHECK_OAL_GATT_ENABLED();
2141 BT_INFO("Remove Scan filter Slot ID is: [%d]", slot_id);
2143 ret = gatt_api->client->scan_filter_clear(client_if, slot_id);
2144 if (ret != BT_STATUS_SUCCESS) {
2145 BT_ERR("error: %s", status2string(ret));
2146 return convert_to_oal_status(ret);
2148 return OAL_STATUS_SUCCESS;
2151 oal_status_t gattc_enable_scan_filter(int client_if)
2156 CHECK_OAL_GATT_ENABLED();
2158 BT_INFO("Enable Scan filter. Client If: %d", client_if);
2160 ret = gatt_api->client->scan_filter_enable(client_if, TRUE);
2161 if (ret != BT_STATUS_SUCCESS) {
2162 BT_ERR("error: %s", status2string(ret));
2163 return convert_to_oal_status(ret);
2165 return OAL_STATUS_SUCCESS;
2168 oal_status_t gattc_disable_scan_filter(int client_if)
2173 CHECK_OAL_GATT_ENABLED();
2175 BT_INFO("Disable Scan filter. Client If: %d", client_if);
2177 ret = gatt_api->client->scan_filter_enable(client_if, FALSE);
2178 if (ret != BT_STATUS_SUCCESS) {
2179 BT_ERR("error: %s", status2string(ret));
2180 return convert_to_oal_status(ret);
2182 return OAL_STATUS_SUCCESS;
2185 oal_status_t gattc_register_scan_filter(oal_ble_scan_filter_t* filter_data)
2188 int client_info = 0;
2191 int company_id_mask = 0;
2192 int address_type = 0;
2193 int feature_selection = 0;
2195 oal_ble_scan_filter_param_setup_t scan_filter_setup = {.list_logic_type = 0,
2196 .filt_logic_type = 1,
2197 .rssi_high_thres = -127,
2198 .rssi_low_thres = -127,
2202 .found_timeout_cnt = 0
2205 OAL_CHECK_PARAMETER(filter_data, return);
2207 CHECK_OAL_GATT_ENABLED();
2209 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS){
2211 BT_INFO("OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS is being added");
2212 BT_INFO("BT remote Device address is %s", bdt_bd2str(filter_data->device_address, &bdstr));
2213 ret = gatt_api->client->scan_filter_add_remove(client_info,
2215 OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS,
2216 filter_data->slot_id,
2221 (bt_bdaddr_t*)filter_data->device_address,
2228 if (ret != BT_STATUS_SUCCESS){
2229 BT_ERR("error: %s", status2string(ret));
2230 BT_INFO("unregistering already set filter features.");
2231 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2232 return convert_to_oal_status(ret);
2235 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS;
2237 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_NAME){
2238 ret = gatt_api->client->scan_filter_add_remove(client_info,
2240 OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_NAME,
2241 filter_data->slot_id,
2249 filter_data->device_name, // device_name as p_data in HAL
2253 if (ret != BT_STATUS_SUCCESS){
2254 BT_ERR("error: %s", status2string(ret));
2255 BT_INFO("unregistering already set filter features.");
2256 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2257 return convert_to_oal_status(ret);
2260 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_NAME;
2262 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_UUID){
2263 BT_INFO("OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_UUID is being added");
2264 char uuid_str1[2*BT_UUID_STRING_MAX];
2265 char uuid_str2[2*BT_UUID_STRING_MAX];
2267 uuid_to_stringname((service_uuid_t*)filter_data->service_uuid, uuid_str1);
2268 uuid_to_stringname((service_uuid_t*)filter_data->service_uuid_mask, uuid_str2);
2270 BT_INFO("Service UUID is [%s] and Service UUID mask is [%s]", uuid_str1, uuid_str2);
2271 ret = gatt_api->client->scan_filter_add_remove(client_info,
2273 OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_UUID,
2274 filter_data->slot_id,
2277 (bt_uuid_t*)filter_data->service_uuid,
2278 (bt_uuid_t*)filter_data->service_uuid_mask,
2281 filter_data->service_uuid_len, // service_uuid_len as data_len in HAL
2283 filter_data->service_uuid_mask_len,
2286 if (ret != BT_STATUS_SUCCESS){
2287 BT_ERR("error: %s", status2string(ret));
2288 BT_INFO("unregistering already set filter features.");
2289 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2290 return convert_to_oal_status(ret);
2293 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_UUID;
2295 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID){
2296 BT_INFO("OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID is being added");
2297 char uuid_str1[2*BT_UUID_STRING_MAX];
2298 char uuid_str2[2*BT_UUID_STRING_MAX];
2299 uuid_to_stringname((service_uuid_t*)filter_data->service_solicitation_uuid, uuid_str1);
2300 uuid_to_stringname((service_uuid_t*)filter_data->service_solicitation_uuid_mask, uuid_str2);
2301 BT_INFO("Service Solicitation UUID is [%s] and Service Solicitation UUID mask is [%s]", uuid_str1, uuid_str2);
2302 ret = gatt_api->client->scan_filter_add_remove(client_info,
2304 OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID,
2305 filter_data->slot_id,
2308 (bt_uuid_t*)filter_data->service_solicitation_uuid,
2309 (bt_uuid_t*)filter_data->service_solicitation_uuid_mask,
2312 filter_data->service_solicitation_uuid_len, // service_solicitation_uuid_len as data_len in HAL
2314 filter_data->service_solicitation_uuid_mask_len,
2317 if (ret != BT_STATUS_SUCCESS){
2318 BT_ERR("error: %s", status2string(ret));
2319 BT_INFO("unregistering already set filter features.");
2320 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2321 return convert_to_oal_status(ret);
2324 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID;
2326 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_DATA){
2327 ret = gatt_api->client->scan_filter_add_remove(client_info,
2329 OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_DATA,
2330 filter_data->slot_id,
2337 filter_data->service_data_len, //service_data_len as data_len in HAL
2338 (char*)filter_data->service_data,
2339 filter_data->service_data_mask_len,
2340 (char*)filter_data->service_data_mask
2342 if (ret != BT_STATUS_SUCCESS){
2343 BT_ERR("error: %s", status2string(ret));
2344 BT_INFO("unregistering already set filter features.");
2345 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2346 return convert_to_oal_status(ret);
2349 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_DATA;
2351 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA){
2352 ret = gatt_api->client->scan_filter_add_remove(client_info,
2354 OAL_BLE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA,
2355 filter_data->slot_id,
2356 filter_data->manufacturer_id,
2362 filter_data->manufacturer_data_len, //manufacturer_data_len as data_len in HAL
2363 (char*)filter_data->manufacturer_data,
2364 filter_data->manufacturer_data_mask_len,
2365 (char*)filter_data->manufacturer_data_mask
2367 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA;
2368 if (ret != BT_STATUS_SUCCESS){
2369 BT_ERR("error: %s", status2string(ret));
2370 BT_INFO("unregistering already set filter features.");
2371 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2372 return convert_to_oal_status(ret);
2375 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA;
2377 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_RSSI_HIGH_THRESHOLD) {
2378 scan_filter_setup.rssi_high_thres = filter_data->rssi_high_threshold;
2379 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_RSSI_HIGH_THRESHOLD;
2382 BT_DBG("Filter selection 0x%.2x", feature_selection);
2384 ret = gatt_api->client->scan_filter_param_setup(client_info, action, filter_data->slot_id, feature_selection,
2385 scan_filter_setup.list_logic_type, scan_filter_setup.filt_logic_type,
2386 scan_filter_setup.rssi_high_thres, scan_filter_setup.rssi_low_thres,
2387 scan_filter_setup.dely_mode, scan_filter_setup.found_timeout,
2388 scan_filter_setup.lost_timeout, scan_filter_setup.found_timeout_cnt);
2389 if (ret != BT_STATUS_SUCCESS){
2390 BT_ERR("error: %s", status2string(ret));
2391 return convert_to_oal_status(ret);
2393 return OAL_STATUS_SUCCESS;
2396 oal_status_t gattc_get_att_mtu(int conn_id, int *mtu)
2398 int ret = OAL_STATUS_SUCCESS;
2399 API_TRACE("Get ATT MTU, conn_id: %d", conn_id);
2400 CHECK_OAL_GATT_ENABLED();
2401 CHECK_CLIENT_CONNECTION(conn_id);
2402 OAL_CHECK_PARAMETER(mtu, return);
2404 /* To prevent crash in case other libraries not support this api */
2405 if (gatt_api->client->get_att_mtu == NULL) {
2406 BT_WARN("get_att_mtu is NULL");
2407 return OAL_STATUS_NOT_SUPPORT;
2410 ret = gatt_api->client->get_att_mtu(conn_id, mtu);
2411 if (ret != BT_STATUS_SUCCESS) {
2412 BT_ERR("GATT MTU Size failed, status: %s", status2string(ret));
2413 return convert_to_oal_status(ret);
2416 BT_INFO("Current ATT MTU Size: %d", *mtu);
2417 return OAL_STATUS_SUCCESS;
2420 oal_status_t gattc_configure_mtu(int conn_id, int mtu)
2424 API_TRACE("Configure MTU Size: [%d]", mtu);
2425 CHECK_OAL_GATT_ENABLED();
2426 CHECK_CLIENT_CONNECTION(conn_id);
2428 /* To prevent crash in case other libraries not support this api */
2429 if (gatt_api->client->configure_mtu == NULL) {
2430 BT_WARN("configure_mtu is NULL");
2431 return OAL_STATUS_NOT_SUPPORT;
2434 ret = gatt_api->client->configure_mtu(conn_id, mtu);
2435 if (ret != BT_STATUS_SUCCESS) {
2436 BT_ERR("Gatt client configure_mtu error: %s", status2string(ret));
2437 return convert_to_oal_status(ret);
2440 return OAL_STATUS_SUCCESS;
2443 oal_status_t gatt_get_data_batching_available_packets(
2444 unsigned int *available_packets)
2448 ret = gatt_api->client->get_data_batching_available_packets(available_packets);
2449 if (ret != BT_STATUS_SUCCESS) {
2450 BT_ERR("GATT data batching failed: %s", status2string(ret));
2451 return convert_to_oal_status(ret);
2454 return OAL_STATUS_SUCCESS;
2457 oal_status_t gatt_enable_data_batching(bt_address_t * address,
2458 int packet_threshold, int timeout)
2462 ret = gatt_api->client->enable_data_batching((bt_bdaddr_t *)address, packet_threshold, timeout);
2463 if (ret != BT_STATUS_SUCCESS) {
2464 BT_ERR("GATT data batching failed: %s", status2string(ret));
2465 return convert_to_oal_status(ret);
2468 return OAL_STATUS_SUCCESS;
2471 oal_status_t gatt_disable_data_batching(bt_address_t * address)
2475 ret = gatt_api->client->disable_data_batching((bt_bdaddr_t *)address);
2476 if (ret != BT_STATUS_SUCCESS) {
2477 BT_ERR("GATT data batching failed: %s", status2string(ret));
2478 return convert_to_oal_status(ret);
2481 return OAL_STATUS_SUCCESS;
2484 oal_status_t gattc_add_connection_info(bt_address_t *device_address, int conn_id, int instance_id)
2486 int ret = OAL_STATUS_SUCCESS;
2489 OAL_CHECK_PARAMETER(device_address, return);
2490 API_TRACE("Add connection info: [%s]", bdt_bd2str(device_address, &bdstr) + 12);
2491 CHECK_OAL_GATT_ENABLED();
2493 ret = gatt_api->client->add_connection_info((bt_bdaddr_t *)device_address, conn_id, instance_id);
2494 if (ret != BT_STATUS_SUCCESS) {
2495 BT_ERR("GATT connection info add failed: %s", status2string(ret));
2496 return convert_to_oal_status(ret);
2498 return OAL_STATUS_SUCCESS;