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 NUM_SERVER_INST 10 // Maximum slots supported by stack is set to 10
36 #define CHECK_OAL_GATT_ENABLED() \
38 if (gatt_api == NULL) { \
39 BT_ERR("GATT Not Enabled"); \
40 return OAL_STATUS_NOT_READY; \
44 #define CHECK_SERVER_INSTANCE(instance_id) \
46 if (instance_id < 1 || instance_id > NUM_SERVER_INST) { \
47 BT_ERR("Invalid Instance"); \
48 return OAL_STATUS_INVALID_PARAM; \
52 #define CHECK_SERVER_REGISTRATION(instance_id) \
54 if (gatt_servers[instance_id - 1].server_id < 0) {\
55 BT_INFO("GATT server registeration is not done for server Instance : %d", instance_id);\
56 return OAL_STATUS_INTERNAL_ERROR;\
60 #define CHECK_CORRECT_SERVER_ID(instance_id, server_if, server_inst)\
62 for (instance_id = 1 ; instance_id <= NUM_SERVER_INST; instance_id++) { \
63 if (gatt_servers[instance_id - 1].server_id == server_if) { \
64 server_inst = instance_id; \
70 #define PRINT_ADV_DATA(length, manuf_data, data_type) \
72 char * buf_str = g_malloc0(3*length + 1);\
73 if (NULL != buf_str) { \
74 convert_hex_2_str(manuf_data, length, buf_str);\
75 if (TRUE == data_type) \
76 BT_INFO("Scan Response Data:%s\n", buf_str);\
78 BT_INFO("Advertising Data:%s\n", buf_str);\
83 #define CHECK_CLIENT_REGISTRATION(client_id) \
85 if (client_id <= 0) { \
86 BT_DBG("No client registered yet...");\
87 return OAL_STATUS_INTERNAL_ERROR;\
91 #define CHECK_CLIENT_CONNECTION(conn_id) \
94 BT_ERR("No active connection");\
95 return OAL_STATUS_INTERNAL_ERROR;\
102 GATT_INS_DATA_SETTING,
114 const char *oal_device_type[] = {
125 } oal_pending_gattc_req_e;
127 static const btgatt_interface_t * gatt_api;
128 static gboolean cur_adv_state[NUM_SERVER_INST];
129 static gatt_server_t gatt_servers[NUM_SERVER_INST];
131 /* Forward declarations of GATT Server callbacks */
132 static void cb_gatts_register_app(int status, int server_if, bt_uuid_t *uuid);
133 static void cb_gatts_multi_adv_enable(int server_if, int status);
134 static void cb_gatts_multi_adv_set_inst_data(int server_if, int status);
135 static void cb_gatts_multi_adv_disable(int server_if, int status);
136 static void cb_gatts_multi_adv_update(int server_if, int status);
138 static void cb_gatts_listen(int status, int server_if);
141 static void cb_gatts_service_added(int status, int server_if, btgatt_srvc_id_t *srvc_id, int srvc_handle);
142 static void cb_gatts_included_service_added(int status, int server_if, int srvc_handle, int incl_srvc_handle);
143 static void cb_gatts_characteristic_added(int status, int server_if, bt_uuid_t *char_id, int srvc_handle, int char_handle);
144 static void cb_gatts_descriptor_added(int status, int server_if, bt_uuid_t *descr_id, int srvc_handle, int descr_handle);
146 static void cb_gatts_service_started(int status, int server_if, int srvc_handle);
147 static void cb_gatts_service_stopped(int status, int server_if, int srvc_handle);
148 static void cb_gatts_service_deleted(int status, int server_if, int srvc_handle);
150 static void cb_gatts_request_read(int conn_id, int trans_id, bt_bdaddr_t *bda, int attr_handle, int offset, bool is_long);
151 static void cb_gatts_request_write(int conn_id, int trans_id, bt_bdaddr_t *bda, int attr_handle, int offset, int length,
152 bool need_rsp, bool is_prep, uint8_t* value);
153 static void cb_gatts_response_confirmation(int status, int handle);
155 static void cb_gatts_acquire_write(int fd, int conn_id, int trans_id, int attr_handle, bt_bdaddr_t*);
156 static void cb_gatts_acquire_notify(int fd, int conn_id, int trans_id, int attr_handle);
158 static void cb_indication_confirmation(int conn_id, int trans_id, int attr_handle, bt_bdaddr_t *bda);
159 static void cb_gatts_connection(int conn_id, int server_if, int connected, bt_bdaddr_t *bda);
161 static void cb_gatts_mtu_changed(int conn_id, int mtu);
164 static void cb_notifcation_changed(int conn_id, int trans_id, int attr_handle, bool notify, bt_bdaddr_t *bda);
167 /************************************HAL Interface *************************************/
169 /*TODO GATT Server callbacks will be implemented in subsequent patches */
170 static const btgatt_server_callbacks_t btgatt_server_callbacks = {
171 .register_server_cb = cb_gatts_register_app,
172 .connection_cb = cb_gatts_connection,
173 .service_added_cb = cb_gatts_service_added,
174 .included_service_added_cb = cb_gatts_included_service_added,
175 .characteristic_added_cb = cb_gatts_characteristic_added,
176 .descriptor_added_cb = cb_gatts_descriptor_added,
177 .service_started_cb = cb_gatts_service_started,
178 .service_stopped_cb = cb_gatts_service_stopped,
179 .service_deleted_cb = cb_gatts_service_deleted,
180 .indication_confirmation_cb = cb_indication_confirmation,
181 .request_read_cb = cb_gatts_request_read,
182 .request_write_cb = cb_gatts_request_write,
183 .request_exec_write_cb = NULL,
184 .response_confirmation_cb = cb_gatts_response_confirmation,
185 .listen_cb = cb_gatts_listen,
186 .multi_adv_enable_cb = cb_gatts_multi_adv_enable,
187 .multi_adv_update_cb = cb_gatts_multi_adv_update,
188 .multi_adv_data_cb = cb_gatts_multi_adv_set_inst_data,
189 .multi_adv_disable_cb = cb_gatts_multi_adv_disable,
190 .mtu_changed_cb = cb_gatts_mtu_changed,
192 .notif_enabled_cb = cb_notifcation_changed,
194 .request_acquire_write_cb = cb_gatts_acquire_write,
195 .request_acquire_notify_cb = cb_gatts_acquire_notify
198 /* Forward declaration for GATT client callbacks */
199 static void cb_gattc_register_app(int status, int clientIf, bt_uuid_t *app_uuid);
200 static void cb_gattc_scan_result(bt_bdaddr_t* bdaddress, int rssi, uint8_t *adv_data);
201 static void cb_gattc_connection(int conn_id, int status, int client_if, bt_bdaddr_t* bda);
202 static void cb_gattc_disconnect(int conn_id, int status, int client_if, bt_bdaddr_t* bda);
203 static void cb_gattc_search_result(int conn_id, btgatt_srvc_id_t *srvc_id);
204 static void cb_gattc_search_complete(int conn_id, int status);
205 static void cb_gattc_get_characteristics(int conn_id, int status, btgatt_srvc_id_t *srvc_id,
206 btgatt_gatt_id_t *char_id, int char_prop);
207 static void cb_gattc_get_descriptor(int conn_id, int status, btgatt_srvc_id_t *srvc_id,
208 btgatt_gatt_id_t *char_id, btgatt_gatt_id_t *descr_id);
209 static void cb_gattc_read_characteristic(int conn_id, int status, btgatt_read_params_t *p_data);
210 static void cb_gattc_read_descriptor(int conn_id, int status, btgatt_read_params_t *p_data);
211 static void cb_gattc_write_characteristic(int conn_id, int status, btgatt_write_params_t *p_data);
212 static void cb_gattc_write_descriptor(int conn_id, int status, btgatt_write_params_t *p_data);
213 static void cb_gattc_register_for_notification(int conn_id, int registered, int status,
214 btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id);
215 static void cb_gattc_notify(int conn_id, btgatt_notify_params_t *p_data);
216 static void cb_gattc_service_changed(bt_bdaddr_t *bd_addr, int change_type, uint8_t *uuid);
217 static void cb_gattc_configure_mtu_cmpl(int conn_id, int status, int mtu);
219 /*TODO GATT CLient callbacks will be implemented in subsequent patches */
220 static const btgatt_client_callbacks_t btgatt_client_callbacks = {
221 .register_client_cb = cb_gattc_register_app,
222 .scan_result_cb = cb_gattc_scan_result,
223 .open_cb = cb_gattc_connection,
224 .close_cb = cb_gattc_disconnect,
225 .search_complete_cb = cb_gattc_search_complete,
226 .search_result_cb = cb_gattc_search_result,
227 .get_characteristic_cb = cb_gattc_get_characteristics,
228 .get_descriptor_cb = cb_gattc_get_descriptor,
229 .get_included_service_cb = NULL,
230 .register_for_notification_cb = cb_gattc_register_for_notification,
231 .notify_cb = cb_gattc_notify,
232 .service_changed_cb = cb_gattc_service_changed,
233 .read_characteristic_cb = cb_gattc_read_characteristic,
234 .write_characteristic_cb = cb_gattc_write_characteristic,
235 .read_descriptor_cb = cb_gattc_read_descriptor,
236 .write_descriptor_cb = cb_gattc_write_descriptor,
237 .execute_write_cb = NULL,
238 .read_remote_rssi_cb = NULL,
239 .configure_mtu_cb = cb_gattc_configure_mtu_cmpl,
240 #ifdef PLATFORM_ANDROID_HAL
241 .scan_filter_cfg_cb = NULL,
242 .scan_filter_param_cb = NULL,
243 .scan_filter_status_cb = NULL,
244 .congestion_cb = NULL,
245 .batchscan_cfg_storage_cb = NULL,
246 .batchscan_enb_disable_cb = NULL,
247 .batchscan_reports_cb = NULL,
248 .batchscan_threshold_cb = NULL,
249 .track_adv_event_cb = NULL,
253 static btgatt_callbacks_t btgatt_callbacks = {
254 sizeof(btgatt_callbacks_t),
255 &btgatt_client_callbacks,
256 &btgatt_server_callbacks
259 /*******************************GATT Initialisation - Deinitialisation********************************/
260 oal_status_t gatt_enable(void)
262 const bt_interface_t * blued_api;
266 for (i = 0; i < NUM_SERVER_INST; i++) {
267 gatt_servers[i].server_id = -1;
268 gatt_servers[i].state = GATT_INS_DISABLED;
269 memset(&gatt_servers[i].uuid, 0, sizeof(oal_uuid_t));
270 cur_adv_state[i] = FALSE;
273 /* Get stack interface */
274 blued_api = (const bt_interface_t *) adapter_get_stack_interface();
276 if (blued_api == NULL) {
277 BT_ERR("Stack is not initialized");
278 return OAL_STATUS_NOT_READY;
282 BT_WARN("GATT Interface is already initialized...");
283 return OAL_STATUS_ALREADY_DONE;
286 gatt_api = (const btgatt_interface_t *)blued_api->get_profile_interface(BT_PROFILE_GATT_ID);
287 if (gatt_api == NULL) {
288 BT_ERR("GATT interface failed");
289 return OAL_STATUS_INTERNAL_ERROR;
292 if ((ret = gatt_api->init(&btgatt_callbacks)) != BT_STATUS_SUCCESS) {
293 BT_ERR("Error: Unable to initialise GATT :%s", status2string(ret));
296 return convert_to_oal_status(ret);
299 BT_INFO("GATT successfully initialized");
300 return OAL_STATUS_SUCCESS;
303 oal_status_t gatt_disable(void)
311 for (i = 0; i < NUM_SERVER_INST; i++) {
312 gatt_servers[i].server_id = -1;
313 gatt_servers[i].state = GATT_INS_DISABLED;
314 memset(&gatt_servers[i].uuid, 0, sizeof(oal_uuid_t));
316 return OAL_STATUS_SUCCESS;
319 /************************************GATT Server Functions*************************************/
321 oal_status_t gatts_register(oal_uuid_t* server_uuid)
323 char str[2*BT_UUID_STRING_MAX];
324 int ret = OAL_STATUS_SUCCESS;
327 CHECK_OAL_GATT_ENABLED();
328 uuid_to_stringname((service_uuid_t*)server_uuid->uuid, str);
329 API_TRACE("Register the server instance: UUID: [%s]", str);
331 for (i = 0; i < NUM_SERVER_INST; i++) {
332 if (memcmp(server_uuid->uuid, gatt_servers[i].uuid.uuid, sizeof(oal_uuid_t)) == 0) {
333 if (gatt_servers[i].server_id != -1) {
334 BT_ERR("This is resevered UUID for easy set up application i = %d \
335 gatt_servers[i].server_id = %d", i, gatt_servers[i].server_id);
336 return OAL_STATUS_ALREADY_DONE;
341 ret = gatt_api->server->register_server((bt_uuid_t*)server_uuid);
343 if (ret != BT_STATUS_SUCCESS) {
344 BT_ERR("GATT server registration with UUID:%s failed: %s", str, status2string(ret));
345 return convert_to_oal_status(ret);
347 return OAL_STATUS_SUCCESS;
350 oal_status_t gatts_unregister(int instance_id)
352 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);
357 CHECK_SERVER_REGISTRATION(instance_id);
359 ret = gatt_api->server->unregister_server(gatt_servers[instance_id - 1].server_id);
361 if (ret != BT_STATUS_SUCCESS) {
362 BT_ERR("GATT server unregistration failed: %d", instance_id);
363 return convert_to_oal_status(ret);
366 gatt_servers[instance_id-1].server_id = -1;
367 gatt_servers[instance_id-1].state = GATT_INS_DISABLED;
369 if (instance_id != 1 && instance_id != 2)
370 memset(&gatt_servers[instance_id-1].uuid, 0, sizeof(oal_uuid_t));
372 return OAL_STATUS_SUCCESS;
375 /* Legacy Advertisement */
376 oal_status_t gatts_start_listen(int instance_id, gboolean enable)
378 int ret = OAL_STATUS_SUCCESS;
379 API_TRACE("server-id:%d enable: %d", gatt_servers[instance_id-1].server_id, enable);
381 CHECK_OAL_GATT_ENABLED();
382 CHECK_SERVER_INSTANCE(instance_id);
383 CHECK_SERVER_REGISTRATION(instance_id);
385 ret = gatt_api->server->listen(gatt_servers[instance_id-1].server_id, enable);
387 if (ret != BT_STATUS_SUCCESS) {
388 BT_ERR("Error:Advertising start/stop on server-id:%d failed With Status: [%s]",
389 gatt_servers[instance_id-1].server_id, status2string(ret));
390 return convert_to_oal_status(ret);
393 return OAL_STATUS_SUCCESS;
396 oal_status_t gatts_stop_advertising(int instance_id)
398 int ret = OAL_STATUS_SUCCESS;
399 API_TRACE("Stop advertising");
401 CHECK_OAL_GATT_ENABLED();
402 CHECK_SERVER_INSTANCE(instance_id);
403 CHECK_SERVER_REGISTRATION(instance_id);
405 ret = gatt_api->server->listen(gatt_servers[instance_id - 1].server_id, FALSE);
407 if (ret != BT_STATUS_SUCCESS) {
408 BT_ERR("Stop Advertising failed for server Instance:%d With Status: %s",
409 instance_id, status2string(ret));
410 return convert_to_oal_status(ret);
412 return OAL_STATUS_SUCCESS;
416 /* Below API's need to be removed as they are hardcoded API's.
417 If at all, product specific API's are required to set specific data, then new naming should
418 be adopted for these API's */
419 oal_status_t gatts_set_advertising_params(int instance_id, float min_intv, float max_intv)
421 int ret = OAL_STATUS_SUCCESS;
423 API_TRACE("Set advertising params min_int:%f, max_int:%f", min_intv, max_intv);
424 CHECK_OAL_GATT_ENABLED();
425 CHECK_SERVER_INSTANCE(instance_id);
426 CHECK_SERVER_REGISTRATION(instance_id);
428 /* Historically used for Legacy Advertising */
429 return OAL_STATUS_NOT_SUPPORT;
432 oal_status_t gatts_set_advertising_data(int instance_id, uint16_t manufac_len, char* manufac_data)
434 int ret = OAL_STATUS_SUCCESS;
436 API_TRACE("Set advertising data");
437 CHECK_OAL_GATT_ENABLED();
438 CHECK_SERVER_INSTANCE(instance_id);
439 CHECK_SERVER_REGISTRATION(instance_id);
441 /* Historically used for Legacy Advertising */
442 return OAL_STATUS_NOT_SUPPORT;
445 oal_status_t gatts_set_scan_response(int instance_id, uint16_t manufac_len, char* manufac_data)
447 int ret = OAL_STATUS_SUCCESS;
449 API_TRACE("Set Scan Response data");
450 CHECK_OAL_GATT_ENABLED();
451 CHECK_SERVER_INSTANCE(instance_id);
452 CHECK_SERVER_REGISTRATION(instance_id);
454 /* Historically used for Legacy Advertising */
455 return OAL_STATUS_NOT_SUPPORT;
459 oal_status_t gatts_multi_adv_enable(int instance_id)
461 int ret = OAL_STATUS_SUCCESS;
462 API_TRACE("Start Multi advertising, srv_if: %d, instance_id: %d",
463 gatt_servers[instance_id - 1].server_id, instance_id);
465 CHECK_OAL_GATT_ENABLED();
466 CHECK_SERVER_INSTANCE(instance_id);
467 CHECK_SERVER_REGISTRATION(instance_id);
469 if (gatt_api->server->multi_adv_enable == NULL)
470 return OAL_STATUS_NOT_SUPPORT;
472 /* Platform Requirement: Advertising to be enabled even when no data is set: i.e Empty Advertisement
473 if (gatt_servers[instance_id - 1].state != GATT_INS_DATA_SET){
474 BT_ERR("Not allowed, state: %d, instance_id: %d",
475 gatt_servers[instance_id - 1].state, instance_id);
476 return OAL_STATUS_BUSY;
479 if (gatt_servers[instance_id - 1].state == GATT_INS_ENABLED) {
480 BT_ERR("Not allowed, state: %d, instance_id: %d",
481 gatt_servers[instance_id - 1].state, instance_id);
482 return OAL_STATUS_ALREADY_DONE;
485 if (gatt_servers[instance_id - 1].state == GATT_INS_ENABLING) {
486 BT_ERR("Not allowed, state: %d, instance_id: %d",
487 gatt_servers[instance_id - 1].state, instance_id);
488 return OAL_STATUS_BUSY;
491 gatt_servers[instance_id - 1].state = GATT_INS_ENABLING;
493 ret = gatt_api->server->multi_adv_enable(gatt_servers[instance_id - 1].server_id);
495 if (ret != BT_STATUS_SUCCESS) {
496 BT_ERR("GATT enable multi advertising failed for server Instance: %d With Status: %s",
497 instance_id, status2string(ret));
498 // gatt_servers[instance_id - 1].state = GATT_INS_DATA_SET;
499 return convert_to_oal_status(ret);
501 return OAL_STATUS_SUCCESS;
504 oal_status_t gatts_multi_adv_disable(int instance_id)
506 int ret = OAL_STATUS_SUCCESS;
507 API_TRACE("Stop Multi advertising, srv_if: %d, instance_id: %d",
508 gatt_servers[instance_id - 1].server_id, instance_id);
510 CHECK_OAL_GATT_ENABLED();
511 CHECK_SERVER_INSTANCE(instance_id);
512 CHECK_SERVER_REGISTRATION(instance_id);
514 if (gatt_api->server->multi_adv_disable == NULL)
515 return OAL_STATUS_NOT_SUPPORT;
517 if ((gatt_servers[instance_id - 1].state != GATT_INS_ENABLED)) {
518 BT_ERR("Not Allowed, state: %d, instance_id: %d",
519 gatt_servers[instance_id - 1].state, instance_id);
520 return OAL_STATUS_BUSY;
523 ret = gatt_api->server->multi_adv_disable(gatt_servers[instance_id - 1].server_id);
525 if (ret != BT_STATUS_SUCCESS) {
526 BT_ERR("GATT disable multi advertising failed for server Instance: %d With Status: %s",
527 instance_id, status2string(ret));
528 return convert_to_oal_status(ret);
530 return OAL_STATUS_SUCCESS;
533 oal_status_t gatts_set_filter_policy(int filter_policy)
535 int ret = OAL_STATUS_SUCCESS;
537 CHECK_OAL_GATT_ENABLED();
539 /* send the filter_policy value to the HAL Layer */
540 ret = gatt_api->server->set_filter_policy(filter_policy);
542 if (ret != BT_STATUS_SUCCESS) {
543 BT_ERR("set_filter_policy failed: %d",ret);
544 return convert_to_oal_status(ret);
547 return OAL_STATUS_SUCCESS;
550 oal_status_t gatts_multi_adv_update(int instance_id,
551 int min_intv, int max_intv,
552 int adv_type, int chnl_map,
553 int tx_power, int timeout_s)
555 int ret = OAL_STATUS_SUCCESS;
556 API_TRACE("Multi advertising Update");
558 CHECK_OAL_GATT_ENABLED();
559 CHECK_SERVER_INSTANCE(instance_id);
560 CHECK_SERVER_REGISTRATION(instance_id);
562 if (gatt_api->server->multi_adv_update == NULL)
563 return OAL_STATUS_NOT_SUPPORT;
565 ret = gatt_api->server->multi_adv_update(gatt_servers[instance_id - 1].server_id,
568 tx_power, timeout_s);
570 if (ret != BT_STATUS_SUCCESS) {
571 BT_ERR("GATT update multi advertising failed for server Instance: %d With Status: %s",
572 instance_id, status2string(ret));
573 return convert_to_oal_status(ret);
575 return OAL_STATUS_SUCCESS;
578 oal_status_t gatts_multi_adv_set_inst_data(int instance_id,
579 oal_ble_multi_adv_param_setup_t *adv_param_setup)
581 int ret = OAL_STATUS_SUCCESS;
582 btgatt_adv_param_setup_t adv_setup;
583 API_TRACE("Set Multi advertising data, srv_if: %d, instance_id: %d",
584 gatt_servers[instance_id - 1].server_id, instance_id);
586 CHECK_OAL_GATT_ENABLED();
587 CHECK_SERVER_INSTANCE(instance_id);
588 CHECK_SERVER_REGISTRATION(instance_id);
590 if (gatt_api->server->multi_adv_set_inst_data == NULL)
591 return OAL_STATUS_NOT_SUPPORT;
593 if ((gatt_servers[instance_id - 1].state >= GATT_INS_DATA_SETTING) && (gatt_servers[instance_id - 1].state != GATT_INS_DATA_SET)) {
594 BT_ERR("Not Allowed, state: %d, instance_id: %d", gatt_servers[instance_id - 1].state, instance_id);
595 return OAL_STATUS_BUSY;
598 gatt_servers[instance_id - 1].state = GATT_INS_DATA_SETTING;
601 adv_setup.set_scan_rsp = adv_param_setup->set_scan_rsp;
602 adv_setup.include_name = adv_param_setup->include_name;
603 adv_setup.include_txpower = adv_param_setup->include_txpower;
604 adv_setup.appearance = adv_param_setup->appearance;
605 adv_setup.manufacturer_data = adv_param_setup->manufacturer_data;
606 adv_setup.manufacturer_data_len = adv_param_setup->manufacturer_data_len;
607 adv_setup.service_data = adv_param_setup->service_data;
608 adv_setup.service_data_len = adv_param_setup->service_data_len;
609 adv_setup.service_uuid = adv_param_setup->service_uuid;
610 adv_setup.service_uuid_len = adv_param_setup->service_uuid_len;
611 /* Solicit UUID handler: Start */
612 adv_setup.solicit_uuid = adv_param_setup->solicit_uuid;
613 adv_setup.solicit_uuid_len = adv_param_setup->solicit_uuid_len;
614 /* Solicit UUID handler: End */
615 adv_setup.min_interval = adv_param_setup->min_interval;
616 adv_setup.max_interval = adv_param_setup->min_interval;
617 adv_setup.adv_type = adv_param_setup->adv_type;
618 adv_setup.chnl_map = adv_param_setup->chnl_map;
619 adv_setup.tx_power = adv_param_setup->tx_power;
620 adv_setup.timeout_s = adv_param_setup->timeout_s;
622 adv_setup.server_if = gatt_servers[instance_id - 1].server_id;
623 if (adv_setup.manufacturer_data_len > 0 && adv_setup.manufacturer_data != NULL) {
624 BT_INFO("Manufacture data.....\n");
625 PRINT_ADV_DATA(adv_setup.manufacturer_data_len, (unsigned char *)adv_setup.manufacturer_data, adv_setup.set_scan_rsp);
628 if (adv_setup.service_uuid_len > 0 && adv_setup.service_uuid != NULL) {
629 BT_INFO("Service uuid.....\n");
630 PRINT_ADV_DATA(adv_setup.service_uuid_len, (unsigned char*)adv_setup.service_uuid, FALSE);
634 ret = gatt_api->server->multi_adv_set_inst_data(adv_setup);
636 if (ret != BT_STATUS_SUCCESS) {
637 BT_ERR("GATT Set Multi advertising data failed: %s", status2string(ret));
638 gatt_servers[instance_id - 1].state = GATT_INS_DISABLED;
639 return convert_to_oal_status(ret);
641 return OAL_STATUS_SUCCESS;
644 oal_status_t gatts_disconnect(int instance_id, bt_address_t *device_address, int conn_id)
646 int ret = OAL_STATUS_SUCCESS;
649 API_TRACE("Server Disonnect: instance_id = %d, conn_id = %d", instance_id, conn_id);
651 CHECK_OAL_GATT_ENABLED();
652 CHECK_SERVER_INSTANCE(instance_id);
653 CHECK_SERVER_REGISTRATION(instance_id);
655 API_TRACE("[%s]", bdt_bd2str(device_address, &bdstr));
657 ret = gatt_api->server->disconnect(gatt_servers[instance_id - 1].server_id, (bt_bdaddr_t *) device_address, conn_id);
658 if (ret != BT_STATUS_SUCCESS) {
659 BT_ERR("GATT Server disconnect failed: %s", status2string(ret));
660 return convert_to_oal_status(ret);
662 return OAL_STATUS_SUCCESS;
666 /************************************GATT Server Functions*************************************/
668 static void cb_gatts_register_app(int status, int server_if, bt_uuid_t *uuid)
671 char str[2*BT_UUID_STRING_MAX];
672 uuid_to_stringname((service_uuid_t*)uuid, str);
674 event_gatts_register_t *event = g_malloc0(sizeof(event_gatts_register_t));
676 BT_INFO("BTGATT SERVER REGISTER APP CB, status:%d, server_if:%d", status, server_if);
677 BT_INFO("UUID: [%s]", str);
679 for (i = 0; i < NUM_SERVER_INST; i++) {
680 if (gatt_servers[i].server_id == -1) {
681 BT_INFO("Server Instance registered with stack [%d]", i+1);
682 gatt_servers[i].server_id = server_if;
683 event->server_inst = i+1;
684 memcpy(gatt_servers[i].uuid.uuid, uuid->uu, sizeof(bt_uuid_t));
685 memcpy(event->server_uuid.uuid, uuid->uu, sizeof(bt_uuid_t));
689 send_event(OAL_EVENT_BLE_SERVER_INSTANCE_INITIALISED, event, sizeof(event_gatts_register_t));
692 static void cb_gatts_listen(int status, int server_if)
694 gboolean prev_state[NUM_SERVER_INST];
696 event_ble_adv_status *event = g_malloc0(sizeof(event_ble_adv_status));
699 for (instance_id = 1 ; instance_id <= NUM_SERVER_INST ; instance_id++) {
700 if (gatt_servers[instance_id - 1].server_id == server_if) {
701 event->server_inst = instance_id;
704 BT_WARN("Server ID:%d recevied server Id:%d\n", gatt_servers[instance_id - 1].server_id, server_if);
707 if ((event->server_inst > 0) && (event->server_inst <= 10)) {
708 prev_state[event->server_inst - 1] = cur_adv_state[event->server_inst - 1];
709 new_state = (status == BT_STATUS_SUCCESS) ? !(prev_state[event->server_inst - 1]) : (prev_state[event->server_inst - 1]);
710 cur_adv_state[event->server_inst - 1] = new_state;
712 BT_ERR("Invalid Callback...");
717 BT_INFO("Adv State of server instance %d :%d", instance_id, new_state);
719 if (TRUE == new_state)
720 send_event(OAL_EVENT_BLE_ADVERTISING_STARTED, event, sizeof(event_ble_adv_status));
722 send_event(OAL_EVENT_BLE_ADVERTISING_STOPPED, event, sizeof(event_ble_adv_status));
725 static void cb_gatts_multi_adv_enable(int server_if, int status)
728 BT_INFO("BTGATT SERVER MULTI ADV ENABLE CB, status:%d, srv_if: %d", status, server_if);
729 event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
732 if (status != BT_STATUS_SUCCESS)
733 BT_ERR("MULTI ADV ENABLE failed, status:%d, srv_if: %d", status, server_if);
735 for (instance_id = 1 ; instance_id <= NUM_SERVER_INST ; instance_id++) {
736 if (gatt_servers[instance_id - 1].server_id == server_if) {
737 event->server_inst = instance_id;
738 gatt_servers[instance_id - 1].state = GATT_INS_ENABLED;
742 BT_ERR("Invalid Interface, srv_if: %d", server_if);
743 event->server_inst = -1;
745 event->status = convert_to_oal_status(status);
746 send_event(OAL_EVENT_BLE_MULTI_ADVERTISING_ENABLE, event, sizeof(event_ble_multiadv_status));
749 static void cb_gatts_multi_adv_disable(int server_if, int status)
751 BT_INFO("BTGATT SERVER MULTI ADV DISABLE CB, status:%d, srv_if: %d", status, server_if);
752 event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
755 if (status != BT_STATUS_SUCCESS)
756 BT_ERR("MULTI ADV DISABLE failed, status:%d, srv_if: %d", status, server_if);
758 for (instance_id = 1 ; instance_id <= NUM_SERVER_INST; instance_id++) {
759 if (gatt_servers[instance_id - 1].server_id == server_if) {
760 event->server_inst = instance_id;
761 gatt_servers[instance_id - 1].state = GATT_INS_DISABLED;
765 BT_ERR("Invalid Interface, srv_if: %d", server_if);
766 event->server_inst = -1;
768 event->status = convert_to_oal_status(status);
769 send_event(OAL_EVENT_BLE_MULTI_ADVERTISING_DISABLE, event, sizeof(event_ble_multiadv_status));
772 static void cb_gatts_multi_adv_update(int server_if, int status)
774 BT_INFO("BTGATT SERVER MULTI ADV UPDATE CB, status:%d, srv_if: %d", status, server_if);
775 event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
778 if (status != BT_STATUS_SUCCESS)
779 BT_ERR("MULTI ADV UPDATE failed, status:%d, srv_if: %d", status, server_if);
781 for (instance_id = 1 ; instance_id <= NUM_SERVER_INST ; instance_id++) {
782 if (gatt_servers[instance_id - 1].server_id == server_if) {
783 event->server_inst = instance_id;
787 BT_ERR("Invalid Interface, srv_if: %d", server_if);
788 event->server_inst = -1;
790 event->status = convert_to_oal_status(status);
791 send_event(OAL_EVENT_BLE_MULTI_ADVERTISING_UPDATE, event, sizeof(event_ble_multiadv_status));
794 static void cb_gatts_multi_adv_set_inst_data(int server_if, int status)
796 BT_INFO("BTGATT SERVER MULTI ADV SET INST DATA CB, status:%d, srv_if: %d", status, server_if);
797 event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
800 if (status != BT_STATUS_SUCCESS)
801 BT_ERR("MULTI ADV SET DATA failed, status:%d, srv_if: %d", status, server_if);
803 for (instance_id = 1 ; instance_id <= NUM_SERVER_INST ; instance_id++) {
804 if (gatt_servers[instance_id - 1].server_id == server_if) {
805 event->server_inst = instance_id;
806 gatt_servers[instance_id - 1].state = GATT_INS_DATA_SET;
810 BT_ERR("Invalid Interface, srv_if: %d", server_if);
811 event->server_inst = -1;
813 event->status = convert_to_oal_status(status);
814 send_event(OAL_EVENT_BLE_MULTI_ADVERTISING_SET_INST_DATA, event, sizeof(event_ble_multiadv_status));
817 oal_status_t gatts_get_att_mtu(int conn_id, int *mtu)
819 int ret = OAL_STATUS_SUCCESS;
820 API_TRACE("Get ATT MTU, conn_id: %d", conn_id);
821 CHECK_OAL_GATT_ENABLED();
822 OAL_CHECK_PARAMETER(mtu, return);
824 /* To prevent crash in case other libraries not support this api */
825 if (gatt_api->server->get_att_mtu == NULL) {
826 BT_WARN("get_att_mtu is NULL");
827 return OAL_STATUS_NOT_SUPPORT;
830 ret = gatt_api->server->get_att_mtu(conn_id, mtu);
831 if (ret != BT_STATUS_SUCCESS) {
832 BT_ERR("GATT MTU Size failed, status: %s", status2string(ret));
833 return convert_to_oal_status(ret);
836 BT_INFO("Current ATT MTU Size: %d", *mtu);
837 return OAL_STATUS_SUCCESS;
839 oal_status_t gatt_send_response_acquire(int conn_id, int trans_id,
840 int status, int fd, int mtu , void * fdlist)
842 int ret = OAL_STATUS_SUCCESS;
844 API_TRACE("Server Send Response : ConnId = %d, TransId = %d, %d", conn_id, trans_id, fd);
845 CHECK_OAL_GATT_ENABLED();
847 ret = gatt_api->server->send_response_acquire(conn_id, trans_id, status, fd, mtu, fdlist);
848 if (ret != BT_STATUS_SUCCESS) {
849 BT_ERR("GATT Server Send Response failed: %s", status2string(ret));
850 return convert_to_oal_status(ret);
852 return OAL_STATUS_SUCCESS;
855 oal_status_t gatts_add_service(int instance_id, oal_gatt_srvc_id_t *gatt_serv_id,
858 int ret = OAL_STATUS_SUCCESS;
859 btgatt_srvc_id_t btgatt_srvc_id;
860 char str[2*BT_UUID_STRING_MAX];
862 API_TRACE("Server Add Service : instance_id = %d, num_handles = %d", instance_id, num_handles);
863 CHECK_OAL_GATT_ENABLED();
865 CHECK_SERVER_INSTANCE(instance_id);
866 CHECK_SERVER_REGISTRATION(instance_id);
868 if (gatt_serv_id != NULL) {
869 uuid_to_stringname(&(gatt_serv_id->id.uuid), str);
870 API_TRACE("Service uuid: [%s]", str);
871 memcpy(btgatt_srvc_id.id.uuid.uu, gatt_serv_id->id.uuid.uuid, sizeof(gatt_serv_id->id.uuid));
872 btgatt_srvc_id.id.inst_id = gatt_serv_id->id.inst_id;
873 btgatt_srvc_id.is_primary = gatt_serv_id->is_prmry;
875 BT_INFO("GATT Server Service Id is NULL");
876 return OAL_STATUS_INVALID_PARAM;
879 ret = gatt_api->server->add_service(gatt_servers[instance_id - 1].server_id, &btgatt_srvc_id, num_handles);
881 if (ret != BT_STATUS_SUCCESS) {
882 BT_ERR("GATT Server add service failed: %s", status2string(ret));
883 return convert_to_oal_status(ret);
885 return OAL_STATUS_SUCCESS;
888 oal_status_t gatts_add_included_services(int instance_id, int serv_handle,
891 int ret = OAL_STATUS_SUCCESS;
892 API_TRACE("Server Add included Service : instance_id = %d, serv_handle = %d, incl_handle = %d",
893 instance_id, serv_handle, incl_handle);
894 CHECK_OAL_GATT_ENABLED();
896 CHECK_SERVER_INSTANCE(instance_id);
898 CHECK_SERVER_REGISTRATION(instance_id);
900 ret = gatt_api->server->add_included_service(gatt_servers[instance_id - 1].server_id, serv_handle, incl_handle);
901 if (ret != BT_STATUS_SUCCESS) {
902 BT_ERR("GATT Server Add included Service failed: %s", status2string(ret));
903 return convert_to_oal_status(ret);
905 return OAL_STATUS_SUCCESS;
908 oal_status_t gatts_add_characteristics(int instance_id, int serv_handle,
909 oal_uuid_t* charc_uuid, int propts, int permsn)
911 int ret = OAL_STATUS_SUCCESS;
912 char str[2*BT_UUID_STRING_MAX];
913 char prop[MAX_PROP_LEN], perm[MAX_PERM_LEN];
914 char_prop_to_string(propts, prop);
915 char_perm_to_string(permsn, perm);
916 API_TRACE("Server Add Characteristic : instance_id = %d, serv_handle = %d, Properties =%s, Permisison = %s",
917 instance_id, serv_handle, prop, perm);
918 CHECK_OAL_GATT_ENABLED();
920 CHECK_SERVER_INSTANCE(instance_id);
922 CHECK_SERVER_REGISTRATION(instance_id);
924 if (charc_uuid != NULL) {
925 uuid_to_stringname(charc_uuid, str);
926 API_TRACE("uuid: [%s]", str);
929 ret = gatt_api->server->add_characteristic(gatt_servers[instance_id - 1].server_id,
930 serv_handle, (bt_uuid_t*)charc_uuid, propts, permsn);
931 if (ret != BT_STATUS_SUCCESS) {
932 BT_ERR("GATT Server Add Characteristic failed: %s", status2string(ret));
933 return convert_to_oal_status(ret);
935 return OAL_STATUS_SUCCESS;
938 oal_status_t gatts_add_descriptor(int instance_id, int serv_handle,
939 oal_uuid_t* desc_uuid, int permsn)
941 int ret = OAL_STATUS_SUCCESS;
942 char str[2*BT_UUID_STRING_MAX];
943 char perm[MAX_PERM_LEN];
944 char_perm_to_string(permsn, perm);
945 API_TRACE("Server Add Descriptor : instance_id = %d, serv_handle = %d, Permisison = %s",
946 instance_id, serv_handle, perm);
947 CHECK_OAL_GATT_ENABLED();
949 CHECK_SERVER_INSTANCE(instance_id);
951 CHECK_SERVER_REGISTRATION(instance_id);
953 if (desc_uuid != NULL) {
954 uuid_to_stringname(desc_uuid, str);
955 API_TRACE("uuid: [%s]", str);
958 ret = gatt_api->server->add_descriptor(gatt_servers[instance_id - 1].server_id,
959 serv_handle, (bt_uuid_t*)desc_uuid, permsn);
961 if (ret != BT_STATUS_SUCCESS) {
962 BT_ERR("GATT Server Add Descriptor failed: %s", status2string(ret));
963 return convert_to_oal_status(ret);
965 return OAL_STATUS_SUCCESS;
968 oal_status_t gatts_start_service(int instance_id, int svc_handle, int transport)
970 int ret = OAL_STATUS_SUCCESS;
972 API_TRACE("Server Start Service : Ins_id = %d, srvHndl = %d, transport = %d",
973 instance_id, svc_handle, transport);
974 CHECK_OAL_GATT_ENABLED();
976 CHECK_SERVER_INSTANCE(instance_id);
978 CHECK_SERVER_REGISTRATION(instance_id);
980 ret = gatt_api->server->start_service(gatt_servers[instance_id - 1].server_id, svc_handle, transport);
982 if (ret != BT_STATUS_SUCCESS) {
983 BT_ERR("GATT Server Start Service failed: %s", status2string(ret));
984 return convert_to_oal_status(ret);
986 return OAL_STATUS_SUCCESS;
989 oal_status_t gatts_stop_service(int ins_id, int srv_hdl)
991 int ret = OAL_STATUS_SUCCESS;
993 API_TRACE("Server Stop Service : Ins_id = %d, srvHndl = %d, ", ins_id, srv_hdl);
994 CHECK_OAL_GATT_ENABLED();
996 CHECK_SERVER_INSTANCE(ins_id);
997 CHECK_SERVER_REGISTRATION(ins_id);
999 ret = gatt_api->server->stop_service(gatt_servers[ins_id - 1].server_id, srv_hdl);
1000 if (ret != BT_STATUS_SUCCESS) {
1001 BT_ERR("GATT Server Stop Service failed: %s", status2string(ret));
1002 return convert_to_oal_status(ret);
1004 return OAL_STATUS_SUCCESS;
1007 oal_status_t gatts_delete_service(int ins_id, int srv_hdl)
1009 int ret = OAL_STATUS_SUCCESS;
1011 API_TRACE("Server Delete Service : Ins_id = %d, srvHndl = %d, ", ins_id, srv_hdl);
1012 CHECK_OAL_GATT_ENABLED();
1014 CHECK_SERVER_INSTANCE(ins_id);
1016 CHECK_SERVER_REGISTRATION(ins_id);
1018 ret = gatt_api->server->delete_service(gatt_servers[ins_id - 1].server_id, srv_hdl);
1019 if (ret != BT_STATUS_SUCCESS) {
1020 BT_ERR("GATT Server Delete Service failed: %s", status2string(ret));
1021 return convert_to_oal_status(ret);
1023 return OAL_STATUS_SUCCESS;
1026 oal_status_t gatts_send_response(int conn_id, int trans_id, int resp_status, oal_gatt_response_t *response)
1028 int ret = OAL_STATUS_SUCCESS;
1030 API_TRACE("Server Send Response : ConnId = %d, TransId = %d, ", conn_id, trans_id);
1031 CHECK_OAL_GATT_ENABLED();
1033 if (response == NULL) {
1034 BT_ERR("GATT Server attribute value is empty");
1035 return OAL_STATUS_INVALID_PARAM;
1038 ret = gatt_api->server->send_response(conn_id, trans_id, resp_status, (btgatt_response_t *)response);
1039 if (ret != BT_STATUS_SUCCESS) {
1040 BT_ERR("GATT Server Send Response failed: %s", status2string(ret));
1041 return convert_to_oal_status(ret);
1043 return OAL_STATUS_SUCCESS;
1046 oal_status_t gatts_send_indication(int ins_id, int attr_hndl, int conn_id, int len, int confirm, char* value)
1048 int ret = OAL_STATUS_SUCCESS;
1050 API_TRACE("Server Send Indication : Ins_id = %d, srvHndl = %d, [%s]",
1051 ins_id, attr_hndl, confirm == TRUE ? "INDICATION" : "NOTIFICATION");
1052 CHECK_OAL_GATT_ENABLED();
1054 CHECK_SERVER_INSTANCE(ins_id);
1056 CHECK_SERVER_REGISTRATION(ins_id);
1058 if (value == NULL || len == 0) {
1059 BT_ERR("GATT Server attribute value is empty");
1060 return OAL_STATUS_INVALID_PARAM;
1063 ret = gatt_api->server->send_indication(gatt_servers[ins_id - 1].server_id,
1064 attr_hndl, conn_id, len, confirm, value);
1066 if (ret != BT_STATUS_SUCCESS) {
1067 BT_ERR("GATT Server Send Indication failed: %s", status2string(ret));
1068 return convert_to_oal_status(ret);
1070 return OAL_STATUS_SUCCESS;
1073 oal_status_t gatts_update_att_value(int ins_id, oal_gatt_value_t *value)
1075 int ret = OAL_STATUS_SUCCESS;
1077 CHECK_OAL_GATT_ENABLED();
1079 CHECK_SERVER_INSTANCE(ins_id);
1081 CHECK_SERVER_REGISTRATION(ins_id);
1083 if (value == NULL || value->len == 0) {
1084 BT_ERR("GATT Server attribute value is empty");
1085 return OAL_STATUS_INVALID_PARAM;
1088 API_TRACE("Server Update Value: Ins_id = [%d], att handle = [%d] data len [%d]",
1089 ins_id, value->handle, value->len);
1091 ret = gatt_api->server->update_att_value(gatt_servers[ins_id - 1].server_id,
1092 (int)value->handle, (int)value->len, (char*)value->value);
1094 if (ret != BT_STATUS_SUCCESS) {
1095 BT_ERR("GATT Server Update Attribute Value failed: [%s]", status2string(ret));
1096 return convert_to_oal_status(ret);
1098 return OAL_STATUS_SUCCESS;
1101 /* GATT Server Callbacks:Start */
1102 static void cb_gatts_service_added(int status, int server_if,
1103 btgatt_srvc_id_t *psrvc_id, int srvc_handle)
1107 BT_INFO("BTGATT SERVER SERVICE ADDED CB, status:%d, handle:%d", status, srvc_handle);
1108 event_gatts_srvc_prm_t* event = g_new0(event_gatts_srvc_prm_t, 1);
1110 CHECK_CORRECT_SERVER_ID(instance_id, server_if, event->gatt_srvc_stat.server_inst);
1112 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1113 event->gatt_srvc_stat.server_inst = -1;
1115 event->gatt_srvc_stat.servic_hndl = srvc_handle;
1116 if (psrvc_id != NULL) {
1117 event->gatt_srvc_id.id.inst_id = psrvc_id->id.inst_id;
1118 //event->gatt_srvc_id.id.uuid = psrvc_id->id.uuid;
1119 memcpy(event->gatt_srvc_id.id.uuid.uuid, psrvc_id->id.uuid.uu, sizeof(psrvc_id->id.uuid));
1120 event->gatt_srvc_id.is_prmry = psrvc_id->is_primary;
1123 event->gatt_srvc_stat.status = convert_to_oal_status(status);
1125 send_event(OAL_EVENT_GATTS_SERVICE_ADDED, event, sizeof(event_gatts_srvc_prm_t));
1128 static void cb_gatts_included_service_added(int status, int server_if,
1130 int incl_srvc_handle)
1133 BT_INFO("BTGATT SERVER INCLUDED SERVICE ADDED CB, status:%d, srvc handle:%d, incl handle:%d",
1134 status, srvc_handle, incl_srvc_handle);
1136 event_gatts_incld_srvc_t* event = g_new0(event_gatts_incld_srvc_t, 1);
1138 CHECK_CORRECT_SERVER_ID(ins_id, server_if, event->gatt_srvc_stat.server_inst);
1140 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1141 event->gatt_srvc_stat.server_inst = -1;
1143 event->gatt_srvc_stat.servic_hndl = srvc_handle;
1144 event->incl_srvc_hndl = incl_srvc_handle;
1145 event->gatt_srvc_stat.status = convert_to_oal_status(status);
1146 send_event(OAL_EVENT_GATTS_INCLUDED_SERVICE_ADDED, event, sizeof(event_gatts_incld_srvc_t));
1149 static void cb_gatts_characteristic_added(int status, int server_if, bt_uuid_t *char_id,
1150 int srvc_handle, int char_handle)
1154 BT_INFO("BTGATT SERVER CHARACTERISTIC ADDED CB, status:%d, srvc handle:%d, char handle:%d",
1155 status, srvc_handle, char_handle);
1157 event_gatts_srvc_charctr_t* event = g_new0(event_gatts_srvc_charctr_t, 1);
1159 CHECK_CORRECT_SERVER_ID(Ins_id, server_if, event->gatt_srvc_stat.server_inst);
1161 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1162 event->gatt_srvc_stat.server_inst = -1;
1164 event->gatt_srvc_stat.servic_hndl = srvc_handle;
1165 event->charctr_hndl = char_handle;
1166 event->gatt_srvc_stat.status = convert_to_oal_status(status);
1167 memcpy(event->charctr_uuid.uuid, char_id->uu, sizeof(bt_uuid_t));
1168 send_event(OAL_EVENT_GATTS_CHARACTERISTIC_ADDED, event, sizeof(event_gatts_srvc_charctr_t));
1171 static void cb_gatts_descriptor_added(int status, int server_if,
1172 bt_uuid_t *descr_id, int srvc_handle,
1176 BT_INFO("BTGATT SERVER DESCRIPTOR ADDED CB, status:%d, srvc handle:%d, desc handle:%d",
1177 status, srvc_handle, descr_handle);
1179 event_gatts_srvc_descr_t* event = g_new0(event_gatts_srvc_descr_t, 1);
1181 CHECK_CORRECT_SERVER_ID(Ins_id, server_if, event->gatt_srvc_stat.server_inst);
1183 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1184 event->gatt_srvc_stat.server_inst = -1;
1186 event->gatt_srvc_stat.servic_hndl = srvc_handle;
1187 event->descrptr_hndl = descr_handle;
1188 event->gatt_srvc_stat.status = convert_to_oal_status(status);
1189 memcpy(event->descrptr_uuid.uuid, descr_id->uu, sizeof(bt_uuid_t));
1190 send_event(OAL_EVENT_GATTS_DESCRIPTOR_ADDED, event, sizeof(event_gatts_srvc_descr_t));
1193 static void cb_gatts_service_started(int status, int server_if, int srvc_handle)
1196 BT_INFO("BTGATT SERVER SERVICE STARTED CB, status:%d, srvc_handle:%d", status, srvc_handle);
1198 event_gatts_srvc_t* event = g_new0(event_gatts_srvc_t, 1);
1200 CHECK_CORRECT_SERVER_ID(Ins_id, server_if, event->server_inst);
1202 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1203 event->server_inst = -1;
1205 event->servic_hndl = srvc_handle;
1206 event->status = convert_to_oal_status(status);
1207 send_event(OAL_EVENT_GATTS_SERVICE_STARTED, event, sizeof(event_gatts_srvc_t));
1210 static void cb_gatts_service_stopped(int status, int server_if, int srvc_handle)
1213 BT_INFO("BTGATT SERVER SERVICE STOPPED CB, status:%d, srvc_handle:%d", status, srvc_handle);
1215 event_gatts_srvc_t* event = g_new0(event_gatts_srvc_t, 1);
1217 CHECK_CORRECT_SERVER_ID(ins_id, server_if, event->server_inst);
1219 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1220 event->server_inst = -1;
1222 event->servic_hndl = srvc_handle;
1223 event->status = convert_to_oal_status(status);
1224 send_event(OAL_EVENT_GATTS_SERVICE_STOPED, event, sizeof(event_gatts_srvc_t));
1227 static void cb_gatts_service_deleted(int status, int server_if, int srvc_handle)
1230 BT_INFO("BTGATT SERVER SERVICE DELETED CB, status:%d, srvc_handle:%d", status, srvc_handle);
1232 event_gatts_srvc_t* event = g_new0(event_gatts_srvc_t, 1);
1234 CHECK_CORRECT_SERVER_ID(ins_id, server_if, event->server_inst);
1236 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1237 event->server_inst = -1;
1239 event->servic_hndl = srvc_handle;
1240 event->status = convert_to_oal_status(status);
1241 send_event(OAL_EVENT_GATTS_SERVICE_DELETED, event, sizeof(event_gatts_srvc_t));
1244 static void cb_gatts_connection(int conn_id, int server_if, int connected, bt_bdaddr_t *bda)
1247 oal_event_t event_type;
1249 BT_INFO("BTGATT SERVER CONNECTION connnected:%d, conn_id:%d server_if:%d", connected, conn_id, server_if);
1250 event_gatts_conn_t *event = g_new0(event_gatts_conn_t, 1);
1252 for (ins_id = 1 ; ins_id <= NUM_SERVER_INST ; ins_id++) {
1253 if ((gatt_servers[ins_id - 1].server_id == server_if)) {
1254 event->server_inst = ins_id;
1257 BT_INFO("Server ID:%d recevied server Id:%d\n", gatt_servers[ins_id - 1].server_id, server_if);
1260 if (ins_id == NUM_SERVER_INST+1) {
1261 BT_WARN("Incorrect Server Interface received: %d\n", server_if);
1265 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1266 event->conn_id = conn_id;
1267 event_type = ((connected == TRUE) ? OAL_EVENT_GATTS_CONNECTION_COMPLETED : OAL_EVENT_GATTS_DISCONNECTION_COMPLETED);
1268 event->status = OAL_STATUS_SUCCESS;
1270 send_event(event_type, event, sizeof(event_gatts_conn_t));
1273 static void cb_gatts_request_read(int conn_id, int trans_id, bt_bdaddr_t *bda,
1274 int attr_handle, int offset, bool is_long)
1276 BT_INFO("BTGATT SERVER REQUEST READ CB: conn_id:%d", conn_id);
1278 event_gatts_srvc_read_attr_t* event = g_new0(event_gatts_srvc_read_attr_t, 1);
1280 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1282 event->attr_trans.attr_handle = attr_handle;
1283 event->attr_trans.conn_id = conn_id;
1284 event->attr_trans.trans_id = trans_id;
1285 event->attr_trans.offset = offset;
1286 event->is_long = is_long;
1288 send_event(OAL_EVENT_GATTS_REQUEST_READ, event, sizeof(event_gatts_srvc_read_attr_t));
1291 static void cb_gatts_request_write(int conn_id, int trans_id,
1292 bt_bdaddr_t *bda, int attr_handle,
1293 int offset, int length,
1294 bool need_rsp, bool is_prep, uint8_t* value)
1296 BT_INFO("BTGATT SERVER REQUEST WRITE conn_id:%d", conn_id);
1298 event_gatts_srvc_write_attr_t* event = g_new0(event_gatts_srvc_write_attr_t, 1);
1300 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1302 event->attr_trans.attr_handle = attr_handle;
1303 event->attr_trans.conn_id = conn_id;
1304 event->attr_trans.trans_id = trans_id;
1305 event->attr_trans.offset = offset;
1306 event->need_rsp = need_rsp;
1307 event->is_prep = is_prep;
1309 if (length > 0 && value != NULL) {
1310 if (length > OAL_GATT_MAX_ATTR_LEN)
1311 length = OAL_GATT_MAX_ATTR_LEN;
1312 memcpy(event->value, value, length);
1313 event->length = length;
1315 send_event(OAL_EVENT_GATTS_REQUEST_WRITE, event, sizeof(event_gatts_srvc_write_attr_t));
1318 static void cb_gatts_acquire_write(int mtu, int conn_id, int trans_id, int attr_handle , bt_bdaddr_t *bda)
1321 BT_INFO("BTGATT SERVER REQUEST ACQUIRE WRITE conn_id:%d", conn_id);
1323 event_gatts_srvc_acquire_attr_t* event = g_new0(event_gatts_srvc_acquire_attr_t, 1);
1325 event->attr_trans.attr_handle = attr_handle;
1326 event->attr_trans.conn_id = conn_id;
1327 event->attr_trans.trans_id = trans_id;
1329 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1331 send_event(OAL_EVENT_GATTS_REQUEST_ACQUIRE_WRITE, event, sizeof(event_gatts_srvc_acquire_attr_t));
1334 static void cb_gatts_acquire_notify(int mtu, int conn_id, int trans_id, int attr_handle)
1336 BT_INFO("BTGATT SERVER REQUEST ACQUIRE NOTIFY conn_id:%d", conn_id);
1338 event_gatts_srvc_acquire_attr_t* event = g_new0(event_gatts_srvc_acquire_attr_t, 1);
1340 event->attr_trans.attr_handle = attr_handle;
1341 event->attr_trans.conn_id = conn_id;
1342 event->attr_trans.trans_id = trans_id;
1345 send_event(OAL_EVENT_GATTS_REQUEST_ACQUIRE_NOTIFY, event, sizeof(event_gatts_srvc_acquire_attr_t));
1349 static void cb_gatts_response_confirmation(int status, int handle)
1351 BT_INFO("BTGATT SERVER RESPONSE CONFIRMATION CB, status:%d, handle:%d", status, handle);
1353 event_gatts_respons_t* event = g_new0(event_gatts_respons_t, 1);
1355 event->hndl = handle;
1356 event->status = convert_to_oal_status(status);
1358 send_event(OAL_EVENT_GATTS_RESPONSE_CONFIRMED, event, sizeof(event_gatts_respons_t));
1361 static void cb_indication_confirmation(int conn_id, int trans_id, int attr_handle, bt_bdaddr_t *bda)
1363 BT_INFO("BTGATT SERVER Indication Confirmation CB, conn_id:%d, trans_id:%d attr_handle:%d",
1364 conn_id, trans_id, attr_handle);
1366 event_gatts_ind_cnfrm_t* event = g_new0(event_gatts_ind_cnfrm_t, 1);
1368 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1370 event->attr_handle = attr_handle;
1371 event->conn_id = conn_id;
1372 event->trans_id = trans_id;
1373 send_event(OAL_EVENT_GATTS_IND_CONFIRM, event, sizeof(event_gatts_ind_cnfrm_t));
1376 static void cb_gatts_mtu_changed(int conn_id, int mtu)
1378 BT_INFO("BTGATT SERVER MTU CHANGED CB, conn_id: %d, mtu_size: %d", conn_id, mtu);
1379 event_gatts_mtu_changed_t *event = g_new0(event_gatts_mtu_changed_t, 1);
1380 event->conn_id = conn_id;
1381 event->mtu_size = mtu;
1382 send_event(OAL_EVENT_GATTS_MTU_CHANGED, event, sizeof(event_gatts_mtu_changed_t));
1386 static void cb_notifcation_changed(int conn_id, int trans_id, int attr_handle, bool notify, bt_bdaddr_t *bda)
1388 BT_INFO("BTGATT SERVER Notication CB, conn_id:%d, trans_id:%d attr_handle:%d Notify:%d",
1389 conn_id, trans_id, attr_handle, notify);
1391 event_gatts_notif_t* event = g_new0(event_gatts_notif_t, 1);
1393 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1395 event->attr_handle = attr_handle;
1396 event->conn_id = conn_id;
1397 event->trans_id = trans_id;
1398 event->notify = notify;
1399 send_event(OAL_EVENT_GATTS_NOTIFICATION, event, sizeof(event_gatts_notif_t));
1404 /* GATT Server Callbacks: End */
1405 /************************************ GATT Client ***********************************/
1406 /* Client Callbacks */
1407 static void cb_gattc_register_app(int status, int clientIf, bt_uuid_t *app_uuid)
1409 char uuid_str[BT_UUID_STRING_MAX];
1410 event_gattc_register_t *event;
1412 /* Check if GATT client registered for Default GATT client UUID */
1413 uuid_to_string((service_uuid_t *)app_uuid, uuid_str);
1415 BT_INFO("GATT CLIENT REGISTER APP CB, status:%d, clientIf:%d, %s", status, clientIf, uuid_str);
1416 event = g_new0(event_gattc_register_t, 1);
1417 event->client_if = clientIf;
1418 event->status = (status == 0 ? OAL_STATUS_SUCCESS : OAL_STATUS_INTERNAL_ERROR);
1419 memcpy(event->client_uuid.uuid, app_uuid->uu, sizeof(oal_uuid_t));
1420 send_event(OAL_EVENT_GATTC_REGISTRATION, event, sizeof(event_gattc_register_t));
1423 static void cb_gattc_scan_result(bt_bdaddr_t* bdaddress, int rssi, uint8_t *adv_data)
1425 event_ble_scan_result_info *event;
1427 event = g_new0(event_ble_scan_result_info, 1);
1429 memcpy(event->address.addr, bdaddress->address, BT_ADDRESS_BYTES_NUM);
1430 memcpy(event->adv_data, adv_data, BLE_ADV_DATA_LENGTH);
1431 send_event_bda_trace(OAL_EVENT_BLE_REMOTE_DEVICE_FOUND, event,
1432 sizeof(event_ble_scan_result_info), (bt_address_t *)bdaddress);
1435 static void cb_gattc_connection(int conn_id, int status, int client_if, bt_bdaddr_t* bda)
1437 event_gattc_conn_t *event;
1438 BT_INFO("BTGATT CLIENT CONNECTED CB, status:[%d] client_if[%d] id[%d]",
1439 status, client_if, conn_id);
1441 event = g_new0(event_gattc_conn_t, 1);
1442 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1443 event->client_if = client_if;
1445 if (BT_STATUS_SUCCESS != status) {
1446 event->status = OAL_STATUS_INTERNAL_ERROR;
1447 BT_ERR("gattc connection Error: %d", status);
1449 event->conn_id = conn_id;
1450 event->status = OAL_STATUS_SUCCESS;
1453 send_event_bda_trace(OAL_EVENT_GATTC_CONNECTION_COMPLETED, event,
1454 sizeof(*event), (bt_address_t *)bda);
1458 static void cb_gattc_disconnect(int conn_id, int status, int client_if, bt_bdaddr_t* bda)
1460 event_gattc_conn_t *event;
1461 BT_INFO("BTGATT Client Disconnect Complete cb, status:[%d], client_if[%d] id[%d]",
1462 status, client_if, conn_id);
1463 event = g_new0(event_gattc_conn_t, 1);
1464 event->status = convert_to_oal_status(status);
1465 event->client_if = client_if;
1466 event->conn_id = conn_id;
1467 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1468 send_event_bda_trace(OAL_EVENT_GATTC_DISCONNECTION_COMPLETED, event,
1469 sizeof(*event), (bt_address_t *)bda);
1472 static void cb_gattc_search_result(int conn_id, btgatt_srvc_id_t *srvc_id)
1474 char uuid_str[2*BT_UUID_STRING_MAX];
1476 BT_INFO("BTGATT Client Service Search Result cb, conn_id:%d", conn_id);
1478 uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str);
1479 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]",
1480 uuid_str, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1482 event_gattc_service_result_t *event = g_new0(event_gattc_service_result_t, 1);
1483 event->conn_status.status = OAL_STATUS_SUCCESS;
1484 event->conn_status.conn_id = conn_id;
1485 memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1487 send_event(OAL_EVENT_GATTC_SERVICE_SEARCH_RESULT, event, sizeof(*event));
1490 static void cb_gattc_search_complete(int conn_id, int status)
1492 BT_INFO("BTGATT Client Service Search Complete cb, status:%d, conn_id:%d",
1495 event_gattc_conn_status_t *event = g_new0(event_gattc_conn_status_t, 1);
1496 event->conn_id = conn_id;
1497 event->status = convert_to_oal_status(status);
1499 send_event(OAL_EVENT_GATTC_SERVICE_SEARCH_DONE, event, sizeof(*event));
1503 static void cb_gattc_get_characteristics(int conn_id, int status, btgatt_srvc_id_t *srvc_id,
1504 btgatt_gatt_id_t *char_id, int char_prop)
1506 char uuid_str1[2*BT_UUID_STRING_MAX];
1507 char uuid_str2[2*BT_UUID_STRING_MAX];
1510 BT_INFO("BTGATT Client Get Characteristic Callback, conn_id:%d, status:%d", conn_id, status);
1511 uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str1);
1512 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]",
1513 uuid_str1, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1515 event_gattc_characteristic_result_t *event = g_new0(event_gattc_characteristic_result_t, 1);
1516 event->conn_status.conn_id = conn_id;
1517 event->conn_status.status = convert_to_oal_status(status);
1518 memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1521 uuid_to_stringname((oal_uuid_t *)&(char_id->uuid), uuid_str2);
1522 BT_INFO("Charac => [%s], Inst_id [%u], Prop [%s]",
1523 uuid_str2, char_id->inst_id, char_prop_to_string(char_prop, str));
1524 event->char_prop = char_prop;
1525 memcpy(&(event->char_id), char_id, sizeof(oal_gatt_id_t));
1527 send_event(OAL_EVENT_GATTC_CHARAC_SERACH_RESULT, event, sizeof(*event));
1532 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)
1534 char uuid_str1[2*BT_UUID_STRING_MAX];
1535 char uuid_str2[2*BT_UUID_STRING_MAX];
1536 char uuid_str3[2*BT_UUID_STRING_MAX];
1537 BT_INFO("BTGATT Client Get Descriptor Callback, conn_id:%d, status:%d", conn_id, status);
1538 uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str1);
1539 uuid_to_stringname((oal_uuid_t *)&(char_id->uuid), uuid_str2);
1540 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]",
1541 uuid_str1, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1542 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, char_id->inst_id);
1544 event_gattc_descriptor_result_t *event = g_new0(event_gattc_descriptor_result_t, 1);
1545 event->conn_status.conn_id = conn_id;
1546 event->conn_status.status = convert_to_oal_status(status);
1547 memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1548 memcpy(&(event->char_id), char_id, sizeof(oal_gatt_id_t));
1551 uuid_to_stringname((oal_uuid_t *)&(descr_id->uuid), uuid_str3);
1552 BT_INFO("Desc => [%s], Inst_id [%u]", uuid_str3, descr_id->inst_id);
1553 memcpy(&(event->descr_id), descr_id, sizeof(oal_gatt_id_t));
1555 send_event(OAL_EVENT_GATTC_DESC_SERACH_RESULT, event, sizeof(*event));
1558 static void cb_gattc_register_for_notification(int client_if, int registered, int status, btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id)
1560 char uuid_str1[2*BT_UUID_STRING_MAX];
1561 char uuid_str2[2*BT_UUID_STRING_MAX];
1562 BT_INFO("BTGATT Client Register For Notification Callback, client_if:%d, status:%d, registered: %s",
1563 client_if, status, registered == 1 ? "REGISTERED" : "DEREGISTERED");
1564 uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str1);
1565 uuid_to_stringname((oal_uuid_t *)&(char_id->uuid), uuid_str2);
1566 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1567 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, char_id->inst_id);
1569 event_gattc_regdereg_notify_t *event = g_new0(event_gattc_regdereg_notify_t, 1);
1570 oal_event_t event_type = (registered == 1 ? OAL_EVENT_GATTC_NOTIFICATION_REGISTERED : OAL_EVENT_GATTC_NOTIFICATION_DEREGISTERED);
1571 event->client_if = client_if;
1572 event->status = convert_to_oal_status(status);
1573 memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1574 memcpy(&(event->char_id), char_id, sizeof(oal_gatt_id_t));
1576 send_event(event_type, event, sizeof(*event));
1579 static void cb_gattc_notify(int conn_id, btgatt_notify_params_t *p_data)
1582 char uuid_str1[2*BT_UUID_STRING_MAX];
1583 char uuid_str2[2*BT_UUID_STRING_MAX];
1585 BT_INFO("BTGATT Client Notify Callback, conn_id:%d", conn_id);
1586 BT_INFO("Server Address:[%s], is_notify:[%d], len:[%d]",
1587 bdt_bd2str((bt_address_t *)&(p_data->bda), &bdstr), p_data->is_notify, p_data->len);
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);
1592 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]",
1593 uuid_str1, p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1594 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1596 if (p_data->len > 0) {
1598 data = g_malloc(3*p_data->len+1);
1600 BT_ERR("memory allocation failed");
1604 convert_hex_2_str((unsigned char *)p_data->value, p_data->len, data);
1605 BT_INFO("Notified Data: [%s]", data);
1607 event_gattc_notify_data *event = g_new0(event_gattc_notify_data, 1);
1608 memcpy(event->address.addr, p_data->bda.address, BT_ADDRESS_BYTES_NUM);
1609 event->is_notify = p_data->is_notify;
1610 event->data_len = p_data->len;
1611 memcpy(event->data, p_data->value, event->data_len);
1612 memcpy(&(event->char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1613 memcpy(&(event->srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1615 send_event_bda_trace(OAL_EVENT_GATTC_NOTIFY_DATA, event, sizeof(*event), (bt_address_t *)&p_data->bda);
1620 static void cb_gattc_service_changed(bt_bdaddr_t *bd_addr, int change_type, uint8_t *uuid)
1622 event_gattc_service_changed_data *event = g_new0(event_gattc_service_changed_data, 1);
1624 memcpy(event->address.addr, bd_addr->address, 6);
1625 event->change_type = change_type;
1626 memcpy(event->uuid.uuid, uuid, sizeof(event->uuid.uuid));
1628 send_event_bda_trace(OAL_EVENT_GATTC_SERVICE_CHANGED_IND, event, sizeof(*event), (bt_address_t *)bd_addr);
1631 static void cb_gattc_read_characteristic(int conn_id, int status, btgatt_read_params_t *p_data)
1633 char uuid_str1[2*BT_UUID_STRING_MAX];
1634 char uuid_str2[2*BT_UUID_STRING_MAX];
1635 BT_INFO("BTGATT Client Read Charcateristic Callback, conn_id:%d, read_status:%d", conn_id, status);
1637 event_gattc_read_data *event = g_new0(event_gattc_read_data, 1);
1638 event->uuid_status.conn_status.conn_id = conn_id;
1639 event->uuid_status.conn_status.status = convert_to_oal_status(status);
1640 event->value_type = p_data->value_type;
1641 memcpy(&(event->uuid_status.srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1642 memcpy(&(event->uuid_status.char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1644 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1645 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1646 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1,
1647 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1648 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1649 BT_DBG("Len: %u, value_type: %u", p_data->value.len, p_data->value_type);
1650 if (p_data->value.len > 0 && event->value_type == GATTC_READ_VALUE_TYPE_VALUE) {
1652 data = g_malloc(3*p_data->value.len+1);
1654 BT_ERR("memory allocation failed");
1658 convert_hex_2_str((unsigned char *)p_data->value.value, p_data->value.len, data);
1659 BT_DBG("Read Data: [%s]", data);
1660 event->data_len = p_data->value.len;
1661 memcpy(&(event->data), &(p_data->value.value), event->data_len);
1665 send_event(OAL_EVENT_GATTC_READ_CHARAC, event, sizeof(*event));
1668 static void cb_gattc_write_characteristic(int conn_id, int status, btgatt_write_params_t *p_data)
1670 char uuid_str1[2*BT_UUID_STRING_MAX];
1671 char uuid_str2[2*BT_UUID_STRING_MAX];
1672 BT_INFO("BTGATT Client Write Charcateristic Callback, conn_id:%d, write_status:%d", conn_id, status);
1674 event_gattc_write_data *event = g_new0(event_gattc_write_data, 1);
1675 event->conn_status.conn_id = conn_id;
1676 event->conn_status.status = convert_to_oal_status(status);
1677 memcpy(&(event->srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1678 memcpy(&(event->char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1680 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1681 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1682 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1,
1683 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1684 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1686 send_event(OAL_EVENT_GATTC_WRITE_CHARAC, event, sizeof(*event));
1689 static void cb_gattc_write_descriptor(int conn_id, int status, btgatt_write_params_t *p_data)
1691 char uuid_str1[2*BT_UUID_STRING_MAX];
1692 char uuid_str2[2*BT_UUID_STRING_MAX];
1693 char uuid_str3[2*BT_UUID_STRING_MAX];
1694 BT_INFO("BTGATT Client Write Descriptor Callback, conn_id:%d, write_status:%d", conn_id, status);
1696 event_gattc_write_data *event = g_new0(event_gattc_write_data, 1);
1697 event->conn_status.conn_id = conn_id;
1698 event->conn_status.status = convert_to_oal_status(status);
1699 memcpy(&(event->srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1700 memcpy(&(event->char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1701 memcpy(&(event->descr_id), &(p_data->descr_id), sizeof(oal_gatt_id_t));
1703 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1704 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1705 uuid_to_stringname((oal_uuid_t *)&(p_data->descr_id.uuid), uuid_str3);
1706 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1,
1707 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1708 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1709 BT_INFO("Desc => [%s], Inst_id [%u]", uuid_str3, p_data->descr_id.inst_id);
1711 send_event(OAL_EVENT_GATTC_WRITE_DESCR, event, sizeof(*event));
1716 static void cb_gattc_read_descriptor(int conn_id, int status, btgatt_read_params_t *p_data)
1718 char uuid_str1[2*BT_UUID_STRING_MAX];
1719 char uuid_str2[2*BT_UUID_STRING_MAX];
1720 char uuid_str3[2*BT_UUID_STRING_MAX];
1721 BT_INFO("BTGATT Client Read Descriptor Callback, conn_id:%d, read_status:%d", conn_id, status);
1723 event_gattc_read_data *event = g_new0(event_gattc_read_data, 1);
1724 event->uuid_status.conn_status.conn_id = conn_id;
1725 event->uuid_status.conn_status.status = convert_to_oal_status(status);
1726 event->value_type = p_data->value_type;
1727 memcpy(&(event->uuid_status.srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1728 memcpy(&(event->uuid_status.char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1729 memcpy(&(event->uuid_status.descr_id), &(p_data->descr_id), sizeof(oal_gatt_id_t));
1732 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1733 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1734 uuid_to_stringname((oal_uuid_t *)&(p_data->descr_id.uuid), uuid_str3);
1735 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1,
1736 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1737 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1738 BT_INFO("Desc => [%s], Inst_id [%u]", uuid_str3, p_data->descr_id.inst_id);
1739 BT_DBG("Len: %u, value_type: %u", p_data->value.len, p_data->value_type);
1740 if (p_data->value.len > 0 && event->value_type == GATTC_READ_VALUE_TYPE_VALUE) {
1742 data = g_malloc(3*p_data->value.len+1);
1744 BT_ERR("memory allocation failed");
1748 convert_hex_2_str((unsigned char *)p_data->value.value, p_data->value.len, data);
1749 BT_DBG("Read Data: [%s]", data);
1750 event->data_len = p_data->value.len;
1751 memcpy(&(event->data), &(p_data->value.value), event->data_len);
1755 send_event(OAL_EVENT_GATTC_READ_DESCR, event, sizeof(*event));
1758 oal_status_t gattc_start_le_discovery(int client_id)
1761 int ret = OAL_STATUS_SUCCESS;
1763 API_TRACE("BTGATT CLIENT SCAN START");
1764 CHECK_OAL_GATT_ENABLED();
1765 CHECK_CLIENT_REGISTRATION(client_id);
1767 ret = gatt_api->client->scan(client_id, 1);
1768 if (ret != BT_STATUS_SUCCESS) {
1769 BT_ERR("Error:Start LE Discovery failed: %s", status2string(ret));
1770 return convert_to_oal_status(ret);
1772 return OAL_STATUS_SUCCESS;
1775 oal_status_t gattc_stop_le_discovery(int client_id)
1778 int ret = OAL_STATUS_SUCCESS;
1780 API_TRACE("Scan is stopped");
1781 CHECK_OAL_GATT_ENABLED();
1782 CHECK_CLIENT_REGISTRATION(client_id);
1783 ret = gatt_api->client->scan(client_id, 0);
1784 if (ret != BT_STATUS_SUCCESS) {
1785 BT_ERR("Error:Stop LE Discovery failed: %s", status2string(ret));
1786 return convert_to_oal_status(ret);
1788 return OAL_STATUS_SUCCESS;
1791 oal_status_t gattc_set_le_scan_param(int scan_type, int itv, int win)
1795 API_TRACE("Scan is stopped");
1796 CHECK_OAL_GATT_ENABLED();
1799 ret = gatt_api->client->set_scan_parameters(scan_type, itv, win);
1800 if (ret != BT_STATUS_SUCCESS) {
1801 BT_ERR("Error:Set scan parameters failed: %s", status2string(ret));
1802 return convert_to_oal_status(ret);
1805 ret = gatt_api->client->set_scan_parameters(itv, win);
1806 if (ret != BT_STATUS_SUCCESS) {
1807 BT_ERR("Error:Set scan parameters failed: %s", status2string(ret));
1808 return convert_to_oal_status(ret);
1812 return OAL_STATUS_SUCCESS;
1815 static void cb_gattc_configure_mtu_cmpl(int conn_id, int status, int mtu)
1817 event_gattc_mtu_configured_t *event;
1818 BT_INFO("BTGATT Client configure mtu complete Callback, conn_id: %d, status: %d, mtu: %d",
1819 conn_id, status, mtu);
1820 event = g_new0(event_gattc_mtu_configured_t, 1);
1821 event->conn_id = conn_id;
1823 event->status = convert_to_oal_status(status);
1824 send_event(OAL_EVENT_GATTC_MTU_EXCHANGE_COMPLETED, event, sizeof(*event));
1827 /************************************ GATT Client ***********************************/
1828 oal_status_t gattc_register(oal_uuid_t* client_uuid)
1831 char str[2*BT_UUID_STRING_MAX];
1832 int ret = OAL_STATUS_SUCCESS;
1834 CHECK_OAL_GATT_ENABLED();
1835 uuid_to_stringname(client_uuid, str);
1836 API_TRACE("uuid: [%s]", str);
1837 /* We will perform actual registration in cb_gattc_register_app callback */
1838 ret = gatt_api->client->register_client((bt_uuid_t *)client_uuid);
1839 if (ret != BT_STATUS_SUCCESS) {
1840 BT_ERR("GATT client register failed: %s", status2string(ret));
1841 return convert_to_oal_status(ret);
1843 return OAL_STATUS_SUCCESS;
1846 oal_status_t gattc_deregister(int client_id)
1848 int ret = OAL_STATUS_SUCCESS;
1850 API_TRACE("GATT client deregister");
1851 CHECK_OAL_GATT_ENABLED();
1852 CHECK_CLIENT_REGISTRATION(client_id);
1854 ret = gatt_api->client->unregister_client(client_id);
1855 if (ret != BT_STATUS_SUCCESS) {
1856 BT_ERR("GATT client deregister failed: %s", status2string(ret));
1857 return convert_to_oal_status(ret);
1860 return OAL_STATUS_SUCCESS;
1863 oal_status_t gattc_connect(int client_id, bt_address_t *device_address, int isDirect)
1865 int ret = OAL_STATUS_SUCCESS;
1868 OAL_CHECK_PARAMETER(device_address, return);
1869 API_TRACE("Client Connect: [%s]", bdt_bd2str(device_address, &bdstr));
1870 CHECK_OAL_GATT_ENABLED();
1871 CHECK_CLIENT_REGISTRATION(client_id);
1873 /* Handle the actual connection in cb_gattc_connection callback */
1874 ret = gatt_api->client->connect(client_id, (bt_bdaddr_t *)device_address, isDirect);
1875 if (ret != BT_STATUS_SUCCESS) {
1876 BT_ERR("GATT client connect failed: %s", status2string(ret));
1877 return convert_to_oal_status(ret);
1879 return OAL_STATUS_SUCCESS;
1882 oal_status_t gattc_disconnect(int client_id, bt_address_t *device_address, int conn_id)
1885 int ret = OAL_STATUS_SUCCESS;
1888 OAL_CHECK_PARAMETER(device_address, return);
1889 API_TRACE("Client Disconnect: [%s]", bdt_bd2str(device_address, &bdstr));
1890 CHECK_OAL_GATT_ENABLED();
1891 CHECK_CLIENT_REGISTRATION(client_id);
1892 CHECK_CLIENT_CONNECTION(conn_id);
1894 /* Handle actual disconnection in callback */
1895 ret = gatt_api->client->disconnect(client_id, (bt_bdaddr_t *) device_address, conn_id);
1896 if (ret != BT_STATUS_SUCCESS) {
1897 BT_ERR("GATT client disconnect failed: %s", status2string(ret));
1898 return convert_to_oal_status(ret);
1900 return OAL_STATUS_SUCCESS;
1904 oal_status_t gattc_search_service(int conn_id, oal_uuid_t *service_uuid)
1906 int ret = OAL_STATUS_SUCCESS;
1907 char uuid_str[2*BT_UUID_STRING_MAX];
1910 uuid_to_stringname(service_uuid, uuid_str);
1911 API_TRACE("Client Service Search UUID: [%s]", uuid_str);
1913 API_TRACE("Client Service Search All");
1915 CHECK_OAL_GATT_ENABLED();
1916 CHECK_CLIENT_CONNECTION(conn_id);
1917 ret = gatt_api->client->search_service(conn_id, (bt_uuid_t *)service_uuid);
1919 if (ret != BT_STATUS_SUCCESS) {
1920 BT_ERR("GATT client service search failed: %s", status2string(ret));
1921 return convert_to_oal_status(ret);
1923 return OAL_STATUS_SUCCESS;
1926 oal_status_t gattc_get_characteristic(int conn_id, oal_gatt_srvc_id_t *srvc_id, oal_gatt_id_t *char_id)
1928 int ret = OAL_STATUS_SUCCESS;
1929 char uuid_str[2*BT_UUID_STRING_MAX];
1931 OAL_CHECK_PARAMETER(srvc_id, return);
1932 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str);
1933 API_TRACE("Client Get Characteristic, Service_uuid: [%s]", uuid_str);
1934 CHECK_OAL_GATT_ENABLED();
1935 CHECK_CLIENT_CONNECTION(conn_id);
1936 ret = gatt_api->client->get_characteristic(conn_id, (btgatt_srvc_id_t *)srvc_id,
1937 (btgatt_gatt_id_t *)char_id);
1938 if (ret != BT_STATUS_SUCCESS) {
1939 BT_ERR("GATT client get characteristic failed: %s", status2string(ret));
1940 return convert_to_oal_status(ret);
1942 return OAL_STATUS_SUCCESS;
1945 oal_status_t gattc_get_descriptor(int conn_id, oal_gatt_srvc_id_t *srvc_id,
1946 oal_gatt_id_t *char_id, oal_gatt_id_t *desc_id)
1948 int ret = OAL_STATUS_SUCCESS;
1949 char uuid_str1[2*BT_UUID_STRING_MAX];
1950 char uuid_str2[2*BT_UUID_STRING_MAX];
1952 OAL_CHECK_PARAMETER(srvc_id, return);
1953 OAL_CHECK_PARAMETER(char_id, return);
1954 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1955 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1956 API_TRACE("Client Get Descriptor, Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
1957 CHECK_OAL_GATT_ENABLED();
1958 CHECK_CLIENT_CONNECTION(conn_id);
1959 ret = gatt_api->client->get_descriptor(conn_id, (btgatt_srvc_id_t *)srvc_id,
1960 (btgatt_gatt_id_t *)char_id, (btgatt_gatt_id_t *)desc_id);
1961 if (ret != BT_STATUS_SUCCESS) {
1962 BT_ERR("GATT client get descriptor failed: %s", status2string(ret));
1963 return convert_to_oal_status(ret);
1965 return OAL_STATUS_SUCCESS;
1968 oal_status_t gattc_register_for_notification(int client_id, bt_address_t * address,
1969 oal_gatt_srvc_id_t *srvc_id, oal_gatt_id_t *char_id) {
1970 int ret = OAL_STATUS_SUCCESS;
1971 char uuid_str1[2*BT_UUID_STRING_MAX];
1972 char uuid_str2[2*BT_UUID_STRING_MAX];
1975 OAL_CHECK_PARAMETER(address, return);
1976 OAL_CHECK_PARAMETER(srvc_id, return);
1977 OAL_CHECK_PARAMETER(char_id, return);
1978 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1979 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1980 API_TRACE("Client Register Notification: [%s], Service_uuid: [%s], Char_uuid: [%s]", bdt_bd2str(address, &bdstr), uuid_str1, uuid_str2);
1981 CHECK_OAL_GATT_ENABLED();
1982 CHECK_CLIENT_REGISTRATION(client_id);
1984 ret = gatt_api->client->register_for_notification(client_id, (bt_bdaddr_t *)address, (btgatt_srvc_id_t *)srvc_id, (btgatt_gatt_id_t *)char_id);
1985 if (ret != BT_STATUS_SUCCESS) {
1986 BT_ERR("GATT client register notification failed: %s", status2string(ret));
1987 return convert_to_oal_status(ret);
1990 return OAL_STATUS_SUCCESS;
1993 oal_status_t gattc_deregister_for_notification(int client_id, bt_address_t * address,
1994 oal_gatt_srvc_id_t *srvc_id, oal_gatt_id_t *char_id) {
1995 int ret = OAL_STATUS_SUCCESS;
1996 char uuid_str1[2*BT_UUID_STRING_MAX];
1997 char uuid_str2[2*BT_UUID_STRING_MAX];
2000 OAL_CHECK_PARAMETER(address, return);
2001 OAL_CHECK_PARAMETER(srvc_id, return);
2002 OAL_CHECK_PARAMETER(char_id, return);
2003 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2004 uuid_to_stringname(&(char_id->uuid), uuid_str2);
2005 API_TRACE("Client Deregister Notification: [%s], Service_uuid: [%s], Char_uuid: [%s]", bdt_bd2str(address, &bdstr), uuid_str1, uuid_str2);
2006 CHECK_OAL_GATT_ENABLED();
2007 CHECK_CLIENT_REGISTRATION(client_id);
2009 ret = gatt_api->client->deregister_for_notification(client_id, (bt_bdaddr_t *)address, (btgatt_srvc_id_t *)srvc_id, (btgatt_gatt_id_t *)char_id);
2010 if (ret != BT_STATUS_SUCCESS) {
2011 BT_ERR("GATT client deregister notification failed: %s", status2string(ret));
2012 return convert_to_oal_status(ret);
2015 return OAL_STATUS_SUCCESS;
2019 oal_status_t gattc_read_characteristic(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2020 oal_gatt_id_t *char_id, oal_gatt_auth_req_t auth_req)
2022 int ret = OAL_STATUS_SUCCESS;
2023 char uuid_str1[2*BT_UUID_STRING_MAX];
2024 char uuid_str2[2*BT_UUID_STRING_MAX];
2026 OAL_CHECK_PARAMETER(srvc_id, return);
2027 OAL_CHECK_PARAMETER(char_id, return);
2028 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2029 uuid_to_stringname(&(char_id->uuid), uuid_str2);
2030 API_TRACE("Client Read Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
2031 CHECK_OAL_GATT_ENABLED();
2032 CHECK_CLIENT_CONNECTION(conn_id);
2034 ret = gatt_api->client->read_characteristic(conn_id, (btgatt_srvc_id_t *)srvc_id,
2035 (btgatt_gatt_id_t *)char_id, auth_req);
2036 if (ret != BT_STATUS_SUCCESS) {
2037 BT_ERR("GATT client read characteristic failed: %s", status2string(ret));
2038 return convert_to_oal_status(ret);
2041 return OAL_STATUS_SUCCESS;
2044 oal_status_t gattc_read_descriptor(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2045 oal_gatt_id_t *char_id, oal_gatt_id_t *desc_id, oal_gatt_auth_req_t auth_req)
2047 int ret = OAL_STATUS_SUCCESS;
2048 char uuid_str1[2*BT_UUID_STRING_MAX];
2049 char uuid_str2[2*BT_UUID_STRING_MAX];
2050 char uuid_str3[2*BT_UUID_STRING_MAX];
2052 OAL_CHECK_PARAMETER(srvc_id, return);
2053 OAL_CHECK_PARAMETER(char_id, return);
2054 OAL_CHECK_PARAMETER(desc_id, return);
2055 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2056 uuid_to_stringname(&(char_id->uuid), uuid_str2);
2057 uuid_to_stringname(&(desc_id->uuid), uuid_str3);
2058 API_TRACE("Client Read Descriptor: Service_uuid: [%s], Char_uuid: [%s], Desc_uuid: [%s]",
2059 uuid_str1, uuid_str2, uuid_str3);
2060 CHECK_OAL_GATT_ENABLED();
2061 CHECK_CLIENT_CONNECTION(conn_id);
2063 ret = gatt_api->client->read_descriptor(conn_id, (btgatt_srvc_id_t *)srvc_id,
2064 (btgatt_gatt_id_t *)char_id, (btgatt_gatt_id_t *)desc_id, auth_req);
2065 if (ret != BT_STATUS_SUCCESS) {
2066 BT_ERR("GATT client read descriptor failed: %s", status2string(ret));
2067 return convert_to_oal_status(ret);
2070 return OAL_STATUS_SUCCESS;
2073 oal_status_t gattc_acquire_notify(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2074 oal_gatt_id_t *char_id, int *fd, int *mtu)
2076 int ret = OAL_STATUS_SUCCESS;
2077 char uuid_str1[2*BT_UUID_STRING_MAX];
2078 char uuid_str2[2*BT_UUID_STRING_MAX];
2079 OAL_CHECK_PARAMETER(srvc_id, return);
2080 OAL_CHECK_PARAMETER(char_id, return);
2081 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2082 uuid_to_stringname(&(char_id->uuid), uuid_str2);
2083 API_TRACE("Client Write Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
2084 CHECK_OAL_GATT_ENABLED();
2085 CHECK_CLIENT_CONNECTION(conn_id);
2087 ret = gatt_api->client->acquire_notify(conn_id, (btgatt_srvc_id_t *)srvc_id,
2088 (btgatt_gatt_id_t *)char_id, fd, mtu);
2089 if (ret != BT_STATUS_SUCCESS) {
2090 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
2091 return convert_to_oal_status(ret);
2094 return OAL_STATUS_SUCCESS;
2097 oal_status_t gattc_acquire_write(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2098 oal_gatt_id_t *char_id, oal_gatt_auth_req_t auth_req , int *fd, int*mtu)
2100 int ret = OAL_STATUS_SUCCESS;
2101 char uuid_str1[2*BT_UUID_STRING_MAX];
2102 char uuid_str2[2*BT_UUID_STRING_MAX];
2103 OAL_CHECK_PARAMETER(srvc_id, return);
2104 OAL_CHECK_PARAMETER(char_id, return);
2105 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2106 uuid_to_stringname(&(char_id->uuid), uuid_str2);
2107 API_TRACE("Client Write Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
2108 CHECK_OAL_GATT_ENABLED();
2109 CHECK_CLIENT_CONNECTION(conn_id);
2111 ret = gatt_api->client->acquire_write(conn_id, (btgatt_srvc_id_t *)srvc_id,
2112 (btgatt_gatt_id_t *)char_id, auth_req, fd, mtu);
2113 if (ret != BT_STATUS_SUCCESS) {
2114 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
2115 return convert_to_oal_status(ret);
2118 return OAL_STATUS_SUCCESS;
2121 oal_status_t gattc_write_characteristic(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2122 oal_gatt_id_t *char_id, oal_gatt_write_type_t write_type,
2123 int len, oal_gatt_auth_req_t auth_req, char* data)
2125 int ret = OAL_STATUS_SUCCESS;
2126 char uuid_str1[2*BT_UUID_STRING_MAX];
2127 char uuid_str2[2*BT_UUID_STRING_MAX];
2128 OAL_CHECK_PARAMETER(srvc_id, return);
2129 OAL_CHECK_PARAMETER(char_id, return);
2130 OAL_CHECK_PARAMETER(data, return);
2131 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2132 uuid_to_stringname(&(char_id->uuid), uuid_str2);
2133 API_TRACE("Client Write Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
2134 CHECK_OAL_GATT_ENABLED();
2135 CHECK_CLIENT_CONNECTION(conn_id);
2137 ret = gatt_api->client->write_characteristic(conn_id, (btgatt_srvc_id_t *)srvc_id,
2138 (btgatt_gatt_id_t *)char_id, write_type, len, auth_req, data);
2139 if (ret != BT_STATUS_SUCCESS) {
2140 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
2141 return convert_to_oal_status(ret);
2144 return OAL_STATUS_SUCCESS;
2147 oal_status_t gattc_write_descriptor(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2148 oal_gatt_id_t *char_id, oal_gatt_id_t *desc_id,
2149 oal_gatt_write_type_t write_type, int len, oal_gatt_auth_req_t auth_req, char* data)
2151 int ret = OAL_STATUS_SUCCESS;
2152 char uuid_str1[2*BT_UUID_STRING_MAX];
2153 char uuid_str2[2*BT_UUID_STRING_MAX];
2154 char uuid_str3[2*BT_UUID_STRING_MAX];
2155 OAL_CHECK_PARAMETER(srvc_id, return);
2156 OAL_CHECK_PARAMETER(char_id, return);
2157 OAL_CHECK_PARAMETER(desc_id, return);
2158 OAL_CHECK_PARAMETER(data, return);
2159 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2160 uuid_to_stringname(&(char_id->uuid), uuid_str2);
2161 uuid_to_stringname(&(desc_id->uuid), uuid_str3);
2162 API_TRACE("Client Write Descriptor: Service_uuid: [%s], Char_uuid: [%s], Desc_uuid: [%s]",
2163 uuid_str1, uuid_str2, uuid_str3);
2164 CHECK_OAL_GATT_ENABLED();
2165 CHECK_CLIENT_CONNECTION(conn_id);
2167 ret = gatt_api->client->write_descriptor(conn_id, (btgatt_srvc_id_t *)srvc_id,
2168 (btgatt_gatt_id_t *)char_id, (btgatt_gatt_id_t *)desc_id, write_type, len, auth_req, data);
2169 if (ret != BT_STATUS_SUCCESS) {
2170 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
2171 return convert_to_oal_status(ret);
2174 return OAL_STATUS_SUCCESS;
2177 oal_status_t gattc_conn_param_update(bt_address_t * address, int min, int max, int latency, int timeout)
2182 OAL_CHECK_PARAMETER(address, return);
2183 API_TRACE("Conn Param Update: [%s]", bdt_bd2str(address, &bdstr));
2184 CHECK_OAL_GATT_ENABLED();
2186 BT_INFO("[%s] min[%d] max[%d] latency[%d] timeout[%d]", bdt_bd2str(address, &bdstr), min, max, latency, timeout);
2187 ret = gatt_api->client->conn_parameter_update((bt_bdaddr_t *)address, min, max, latency, timeout);
2188 if (ret != BT_STATUS_SUCCESS) {
2189 BT_ERR("error: %s", status2string(ret));
2190 return convert_to_oal_status(ret);
2192 return OAL_STATUS_SUCCESS;
2195 oal_status_t gattc_unregister_scan_filter(int slot_id)
2201 CHECK_OAL_GATT_ENABLED();
2203 BT_INFO("Remove Scan filter Slot ID is: [%d]", slot_id);
2205 ret = gatt_api->client->scan_filter_clear(client_if, slot_id);
2206 if (ret != BT_STATUS_SUCCESS) {
2207 BT_ERR("error: %s", status2string(ret));
2208 return convert_to_oal_status(ret);
2210 return OAL_STATUS_SUCCESS;
2213 oal_status_t gattc_enable_scan_filter(int client_if)
2218 CHECK_OAL_GATT_ENABLED();
2220 BT_INFO("Enable Scan filter. Client If: %d", client_if);
2222 ret = gatt_api->client->scan_filter_enable(client_if, TRUE);
2223 if (ret != BT_STATUS_SUCCESS) {
2224 BT_ERR("error: %s", status2string(ret));
2225 return convert_to_oal_status(ret);
2227 return OAL_STATUS_SUCCESS;
2230 oal_status_t gattc_disable_scan_filter(int client_if)
2235 CHECK_OAL_GATT_ENABLED();
2237 BT_INFO("Disable Scan filter. Client If: %d", client_if);
2239 ret = gatt_api->client->scan_filter_enable(client_if, FALSE);
2240 if (ret != BT_STATUS_SUCCESS) {
2241 BT_ERR("error: %s", status2string(ret));
2242 return convert_to_oal_status(ret);
2244 return OAL_STATUS_SUCCESS;
2247 oal_status_t gattc_register_scan_filter(oal_ble_scan_filter_t* filter_data)
2250 int client_info = 0;
2253 int company_id_mask = 0;
2254 int address_type = 0;
2255 int feature_selection = 0;
2257 oal_ble_scan_filter_param_setup_t scan_filter_setup = {.list_logic_type = 0,
2258 .filt_logic_type = 1,
2259 .rssi_high_thres = -127,
2260 .rssi_low_thres = -127,
2264 .found_timeout_cnt = 0
2267 OAL_CHECK_PARAMETER(filter_data, return);
2269 CHECK_OAL_GATT_ENABLED();
2271 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS){
2273 BT_INFO("OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS is being added");
2274 BT_INFO("BT remote Device address is %s", bdt_bd2str(filter_data->device_address, &bdstr));
2275 ret = gatt_api->client->scan_filter_add_remove(client_info,
2277 OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS,
2278 filter_data->slot_id,
2283 (bt_bdaddr_t*)filter_data->device_address,
2290 if (ret != BT_STATUS_SUCCESS){
2291 BT_ERR("error: %s", status2string(ret));
2292 BT_INFO("unregistering already set filter features.");
2293 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2294 return convert_to_oal_status(ret);
2297 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS;
2299 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_NAME){
2300 ret = gatt_api->client->scan_filter_add_remove(client_info,
2302 OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_NAME,
2303 filter_data->slot_id,
2311 filter_data->device_name, // device_name as p_data in HAL
2315 if (ret != BT_STATUS_SUCCESS){
2316 BT_ERR("error: %s", status2string(ret));
2317 BT_INFO("unregistering already set filter features.");
2318 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2319 return convert_to_oal_status(ret);
2322 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_NAME;
2324 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_UUID){
2325 BT_INFO("OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_UUID is being added");
2326 char uuid_str1[2*BT_UUID_STRING_MAX];
2327 char uuid_str2[2*BT_UUID_STRING_MAX];
2329 uuid_to_stringname((service_uuid_t*)filter_data->service_uuid, uuid_str1);
2330 uuid_to_stringname((service_uuid_t*)filter_data->service_uuid_mask, uuid_str2);
2332 BT_INFO("Service UUID is [%s] and Service UUID mask is [%s]", uuid_str1, uuid_str2);
2333 ret = gatt_api->client->scan_filter_add_remove(client_info,
2335 OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_UUID,
2336 filter_data->slot_id,
2339 (bt_uuid_t*)filter_data->service_uuid,
2340 (bt_uuid_t*)filter_data->service_uuid_mask,
2343 filter_data->service_uuid_len, // service_uuid_len as data_len in HAL
2345 filter_data->service_uuid_mask_len,
2348 if (ret != BT_STATUS_SUCCESS){
2349 BT_ERR("error: %s", status2string(ret));
2350 BT_INFO("unregistering already set filter features.");
2351 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2352 return convert_to_oal_status(ret);
2355 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_UUID;
2357 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID){
2358 BT_INFO("OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID is being added");
2359 char uuid_str1[2*BT_UUID_STRING_MAX];
2360 char uuid_str2[2*BT_UUID_STRING_MAX];
2361 uuid_to_stringname((service_uuid_t*)filter_data->service_solicitation_uuid, uuid_str1);
2362 uuid_to_stringname((service_uuid_t*)filter_data->service_solicitation_uuid_mask, uuid_str2);
2363 BT_INFO("Service Solicitation UUID is [%s] and Service Solicitation UUID mask is [%s]", uuid_str1, uuid_str2);
2364 ret = gatt_api->client->scan_filter_add_remove(client_info,
2366 OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID,
2367 filter_data->slot_id,
2370 (bt_uuid_t*)filter_data->service_solicitation_uuid,
2371 (bt_uuid_t*)filter_data->service_solicitation_uuid_mask,
2374 filter_data->service_solicitation_uuid_len, // service_solicitation_uuid_len as data_len in HAL
2376 filter_data->service_solicitation_uuid_mask_len,
2379 if (ret != BT_STATUS_SUCCESS){
2380 BT_ERR("error: %s", status2string(ret));
2381 BT_INFO("unregistering already set filter features.");
2382 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2383 return convert_to_oal_status(ret);
2386 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID;
2388 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_DATA){
2389 ret = gatt_api->client->scan_filter_add_remove(client_info,
2391 OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_DATA,
2392 filter_data->slot_id,
2399 filter_data->service_data_len, //service_data_len as data_len in HAL
2400 (char*)filter_data->service_data,
2401 filter_data->service_data_mask_len,
2402 (char*)filter_data->service_data_mask
2404 if (ret != BT_STATUS_SUCCESS){
2405 BT_ERR("error: %s", status2string(ret));
2406 BT_INFO("unregistering already set filter features.");
2407 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2408 return convert_to_oal_status(ret);
2411 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_DATA;
2413 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA){
2414 ret = gatt_api->client->scan_filter_add_remove(client_info,
2416 OAL_BLE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA,
2417 filter_data->slot_id,
2418 filter_data->manufacturer_id,
2424 filter_data->manufacturer_data_len, //manufacturer_data_len as data_len in HAL
2425 (char*)filter_data->manufacturer_data,
2426 filter_data->manufacturer_data_mask_len,
2427 (char*)filter_data->manufacturer_data_mask
2429 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA;
2430 if (ret != BT_STATUS_SUCCESS){
2431 BT_ERR("error: %s", status2string(ret));
2432 BT_INFO("unregistering already set filter features.");
2433 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2434 return convert_to_oal_status(ret);
2437 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA;
2440 BT_DBG("Filter selection 0x%.2x", feature_selection);
2442 ret = gatt_api->client->scan_filter_param_setup(client_info, action, filter_data->slot_id, feature_selection,
2443 scan_filter_setup.list_logic_type, scan_filter_setup.filt_logic_type,
2444 scan_filter_setup.rssi_high_thres, scan_filter_setup.rssi_low_thres,
2445 scan_filter_setup.dely_mode, scan_filter_setup.found_timeout,
2446 scan_filter_setup.lost_timeout, scan_filter_setup.found_timeout_cnt);
2447 if (ret != BT_STATUS_SUCCESS){
2448 BT_ERR("error: %s", status2string(ret));
2449 return convert_to_oal_status(ret);
2451 return OAL_STATUS_SUCCESS;
2454 oal_status_t gattc_configure_mtu(int conn_id, int mtu)
2458 API_TRACE("Configure MTU Size: [%d]", mtu);
2459 CHECK_OAL_GATT_ENABLED();
2460 CHECK_CLIENT_CONNECTION(conn_id);
2462 /* To prevent crash in case other libraries not support this api */
2463 if (gatt_api->client->configure_mtu == NULL) {
2464 BT_WARN("configure_mtu is NULL");
2465 return OAL_STATUS_NOT_SUPPORT;
2468 ret = gatt_api->client->configure_mtu(conn_id, mtu);
2469 if (ret != BT_STATUS_SUCCESS) {
2470 BT_ERR("Gatt client configure_mtu error: %s", status2string(ret));
2471 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 server_inst_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));
2483 CHECK_OAL_GATT_ENABLED();
2484 CHECK_SERVER_INSTANCE(server_inst_id);
2485 CHECK_SERVER_REGISTRATION(server_inst_id);
2487 ret = gatt_api->client->add_connection_info((bt_bdaddr_t *)device_address, conn_id, server_inst_id);
2488 if (ret != BT_STATUS_SUCCESS) {
2489 BT_ERR("GATT connection info add failed: %s", status2string(ret));
2490 return convert_to_oal_status(ret);
2492 return OAL_STATUS_SUCCESS;