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, uint8_t addr_type, int rssi,
201 uint8_t *adv_data, int adv_data_len, uint8_t *scan_rsp_data, int scan_rsp_data_len);
202 static void cb_gattc_connection(int conn_id, int status, int client_if, bt_bdaddr_t* bda);
203 static void cb_gattc_disconnect(int conn_id, int status, int client_if, bt_bdaddr_t* bda);
204 static void cb_gattc_search_result(int conn_id, btgatt_srvc_id_t *srvc_id);
205 static void cb_gattc_search_complete(int conn_id, int status);
206 static void cb_gattc_get_characteristics(int conn_id, int status, btgatt_srvc_id_t *srvc_id,
207 btgatt_gatt_id_t *char_id, int char_prop);
208 static void cb_gattc_get_descriptor(int conn_id, int status, btgatt_srvc_id_t *srvc_id,
209 btgatt_gatt_id_t *char_id, btgatt_gatt_id_t *descr_id);
210 static void cb_gattc_read_characteristic(int conn_id, int status, btgatt_read_params_t *p_data);
211 static void cb_gattc_read_descriptor(int conn_id, int status, btgatt_read_params_t *p_data);
212 static void cb_gattc_write_characteristic(int conn_id, int status, btgatt_write_params_t *p_data);
213 static void cb_gattc_write_descriptor(int conn_id, int status, btgatt_write_params_t *p_data);
214 static void cb_gattc_register_for_notification(int conn_id, int registered, int status,
215 btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id);
216 static void cb_gattc_notify(int conn_id, btgatt_notify_params_t *p_data);
217 static void cb_gattc_service_changed(bt_bdaddr_t *bd_addr, int change_type, uint8_t *uuid, int conn_id, int inst_id);
218 static void cb_gattc_configure_mtu_cmpl(int conn_id, int status, int mtu);
220 /*TODO GATT CLient callbacks will be implemented in subsequent patches */
221 static const btgatt_client_callbacks_t btgatt_client_callbacks = {
222 .register_client_cb = cb_gattc_register_app,
223 .scan_result_cb = cb_gattc_scan_result,
224 .open_cb = cb_gattc_connection,
225 .close_cb = cb_gattc_disconnect,
226 .search_complete_cb = cb_gattc_search_complete,
227 .search_result_cb = cb_gattc_search_result,
228 .get_characteristic_cb = cb_gattc_get_characteristics,
229 .get_descriptor_cb = cb_gattc_get_descriptor,
230 .get_included_service_cb = NULL,
231 .register_for_notification_cb = cb_gattc_register_for_notification,
232 .notify_cb = cb_gattc_notify,
233 .service_changed_cb = cb_gattc_service_changed,
234 .read_characteristic_cb = cb_gattc_read_characteristic,
235 .write_characteristic_cb = cb_gattc_write_characteristic,
236 .read_descriptor_cb = cb_gattc_read_descriptor,
237 .write_descriptor_cb = cb_gattc_write_descriptor,
238 .execute_write_cb = NULL,
239 .read_remote_rssi_cb = NULL,
240 .configure_mtu_cb = cb_gattc_configure_mtu_cmpl,
241 #ifdef PLATFORM_ANDROID_HAL
242 .scan_filter_cfg_cb = NULL,
243 .scan_filter_param_cb = NULL,
244 .scan_filter_status_cb = NULL,
245 .congestion_cb = NULL,
246 .batchscan_cfg_storage_cb = NULL,
247 .batchscan_enb_disable_cb = NULL,
248 .batchscan_reports_cb = NULL,
249 .batchscan_threshold_cb = NULL,
250 .track_adv_event_cb = NULL,
254 static btgatt_callbacks_t btgatt_callbacks = {
255 sizeof(btgatt_callbacks_t),
256 &btgatt_client_callbacks,
257 &btgatt_server_callbacks
260 /*******************************GATT Initialisation - Deinitialisation********************************/
261 oal_status_t gatt_enable(void)
263 const bt_interface_t * blued_api;
267 for (i = 0; i < NUM_SERVER_INST; i++) {
268 gatt_servers[i].server_id = -1;
269 gatt_servers[i].state = GATT_INS_DISABLED;
270 memset(&gatt_servers[i].uuid, 0, sizeof(oal_uuid_t));
271 cur_adv_state[i] = FALSE;
274 /* Get stack interface */
275 blued_api = (const bt_interface_t *) adapter_get_stack_interface();
277 if (blued_api == NULL) {
278 BT_ERR("Stack is not initialized");
279 return OAL_STATUS_NOT_READY;
283 BT_WARN("GATT Interface is already initialized...");
284 return OAL_STATUS_ALREADY_DONE;
287 gatt_api = (const btgatt_interface_t *)blued_api->get_profile_interface(BT_PROFILE_GATT_ID);
288 if (gatt_api == NULL) {
289 BT_ERR("GATT interface failed");
290 return OAL_STATUS_INTERNAL_ERROR;
293 if ((ret = gatt_api->init(&btgatt_callbacks)) != BT_STATUS_SUCCESS) {
294 BT_ERR("Error: Unable to initialise GATT :%s", status2string(ret));
297 return convert_to_oal_status(ret);
300 BT_INFO("GATT successfully initialized");
301 return OAL_STATUS_SUCCESS;
304 oal_status_t gatt_disable(void)
312 for (i = 0; i < NUM_SERVER_INST; i++) {
313 gatt_servers[i].server_id = -1;
314 gatt_servers[i].state = GATT_INS_DISABLED;
315 memset(&gatt_servers[i].uuid, 0, sizeof(oal_uuid_t));
317 return OAL_STATUS_SUCCESS;
320 /************************************GATT Server Functions*************************************/
322 oal_status_t gatts_register(oal_uuid_t* server_uuid)
324 char str[2*BT_UUID_STRING_MAX];
325 int ret = OAL_STATUS_SUCCESS;
328 CHECK_OAL_GATT_ENABLED();
329 uuid_to_stringname((service_uuid_t*)server_uuid->uuid, str);
330 API_TRACE("Register the server instance: UUID: [%s]", str);
332 for (i = 0; i < NUM_SERVER_INST; i++) {
333 if (memcmp(server_uuid->uuid, gatt_servers[i].uuid.uuid, sizeof(oal_uuid_t)) == 0) {
334 if (gatt_servers[i].server_id != -1) {
335 BT_ERR("This is resevered UUID for easy set up application i = %d \
336 gatt_servers[i].server_id = %d", i, gatt_servers[i].server_id);
337 return OAL_STATUS_ALREADY_DONE;
342 ret = gatt_api->server->register_server((bt_uuid_t*)server_uuid);
344 if (ret != BT_STATUS_SUCCESS) {
345 BT_ERR("GATT server registration with UUID:%s failed: %s", str, status2string(ret));
346 return convert_to_oal_status(ret);
348 return OAL_STATUS_SUCCESS;
351 oal_status_t gatts_unregister(int instance_id)
353 int ret = OAL_STATUS_SUCCESS;
354 API_TRACE("Unregister the registered server, instance_id: %d", instance_id);
356 CHECK_OAL_GATT_ENABLED();
357 CHECK_SERVER_INSTANCE(instance_id);
358 CHECK_SERVER_REGISTRATION(instance_id);
360 ret = gatt_api->server->unregister_server(gatt_servers[instance_id - 1].server_id);
362 if (ret != BT_STATUS_SUCCESS) {
363 BT_ERR("GATT server unregistration failed: %d", instance_id);
364 return convert_to_oal_status(ret);
367 gatt_servers[instance_id-1].server_id = -1;
368 gatt_servers[instance_id-1].state = GATT_INS_DISABLED;
370 if (instance_id != 1 && instance_id != 2)
371 memset(&gatt_servers[instance_id-1].uuid, 0, sizeof(oal_uuid_t));
373 return OAL_STATUS_SUCCESS;
376 /* Legacy Advertisement */
377 oal_status_t gatts_start_listen(int instance_id, gboolean enable)
379 int ret = OAL_STATUS_SUCCESS;
380 API_TRACE("server-id:%d enable: %d", gatt_servers[instance_id-1].server_id, enable);
382 CHECK_OAL_GATT_ENABLED();
383 CHECK_SERVER_INSTANCE(instance_id);
384 CHECK_SERVER_REGISTRATION(instance_id);
386 ret = gatt_api->server->listen(gatt_servers[instance_id-1].server_id, enable);
388 if (ret != BT_STATUS_SUCCESS) {
389 BT_ERR("Error:Advertising start/stop on server-id:%d failed With Status: [%s]",
390 gatt_servers[instance_id-1].server_id, status2string(ret));
391 return convert_to_oal_status(ret);
394 return OAL_STATUS_SUCCESS;
397 oal_status_t gatts_stop_advertising(int instance_id)
399 int ret = OAL_STATUS_SUCCESS;
400 API_TRACE("Stop advertising");
402 CHECK_OAL_GATT_ENABLED();
403 CHECK_SERVER_INSTANCE(instance_id);
404 CHECK_SERVER_REGISTRATION(instance_id);
406 ret = gatt_api->server->listen(gatt_servers[instance_id - 1].server_id, FALSE);
408 if (ret != BT_STATUS_SUCCESS) {
409 BT_ERR("Stop Advertising failed for server Instance:%d With Status: %s",
410 instance_id, status2string(ret));
411 return convert_to_oal_status(ret);
413 return OAL_STATUS_SUCCESS;
417 /* Below API's need to be removed as they are hardcoded API's.
418 If at all, product specific API's are required to set specific data, then new naming should
419 be adopted for these API's */
420 oal_status_t gatts_set_advertising_params(int instance_id, float min_intv, float max_intv)
422 int ret = OAL_STATUS_SUCCESS;
424 API_TRACE("Set advertising params min_int:%f, max_int:%f", min_intv, max_intv);
425 CHECK_OAL_GATT_ENABLED();
426 CHECK_SERVER_INSTANCE(instance_id);
427 CHECK_SERVER_REGISTRATION(instance_id);
429 /* Historically used for Legacy Advertising */
430 return OAL_STATUS_NOT_SUPPORT;
433 oal_status_t gatts_set_advertising_data(int instance_id, uint16_t manufac_len, char* manufac_data)
435 int ret = OAL_STATUS_SUCCESS;
437 API_TRACE("Set advertising data");
438 CHECK_OAL_GATT_ENABLED();
439 CHECK_SERVER_INSTANCE(instance_id);
440 CHECK_SERVER_REGISTRATION(instance_id);
442 /* Historically used for Legacy Advertising */
443 return OAL_STATUS_NOT_SUPPORT;
446 oal_status_t gatts_set_scan_response(int instance_id, uint16_t manufac_len, char* manufac_data)
448 int ret = OAL_STATUS_SUCCESS;
450 API_TRACE("Set Scan Response data");
451 CHECK_OAL_GATT_ENABLED();
452 CHECK_SERVER_INSTANCE(instance_id);
453 CHECK_SERVER_REGISTRATION(instance_id);
455 /* Historically used for Legacy Advertising */
456 return OAL_STATUS_NOT_SUPPORT;
460 oal_status_t gatts_multi_adv_enable(int instance_id)
462 int ret = OAL_STATUS_SUCCESS;
463 API_TRACE("Start Multi advertising, srv_if: %d, instance_id: %d",
464 gatt_servers[instance_id - 1].server_id, instance_id);
466 CHECK_OAL_GATT_ENABLED();
467 CHECK_SERVER_INSTANCE(instance_id);
468 CHECK_SERVER_REGISTRATION(instance_id);
470 if (gatt_api->server->multi_adv_enable == NULL)
471 return OAL_STATUS_NOT_SUPPORT;
473 /* Platform Requirement: Advertising to be enabled even when no data is set: i.e Empty Advertisement
474 if (gatt_servers[instance_id - 1].state != GATT_INS_DATA_SET){
475 BT_ERR("Not allowed, state: %d, instance_id: %d",
476 gatt_servers[instance_id - 1].state, instance_id);
477 return OAL_STATUS_BUSY;
480 if (gatt_servers[instance_id - 1].state == GATT_INS_ENABLED) {
481 BT_ERR("Not allowed, state: %d, instance_id: %d",
482 gatt_servers[instance_id - 1].state, instance_id);
483 return OAL_STATUS_ALREADY_DONE;
486 if (gatt_servers[instance_id - 1].state == GATT_INS_ENABLING) {
487 BT_ERR("Not allowed, state: %d, instance_id: %d",
488 gatt_servers[instance_id - 1].state, instance_id);
489 return OAL_STATUS_BUSY;
492 gatt_servers[instance_id - 1].state = GATT_INS_ENABLING;
494 ret = gatt_api->server->multi_adv_enable(gatt_servers[instance_id - 1].server_id);
496 if (ret != BT_STATUS_SUCCESS) {
497 BT_ERR("GATT enable multi advertising failed for server Instance: %d With Status: %s",
498 instance_id, status2string(ret));
499 // gatt_servers[instance_id - 1].state = GATT_INS_DATA_SET;
500 return convert_to_oal_status(ret);
502 return OAL_STATUS_SUCCESS;
505 oal_status_t gatts_multi_adv_disable(int instance_id)
507 int ret = OAL_STATUS_SUCCESS;
508 API_TRACE("Stop Multi advertising, srv_if: %d, instance_id: %d",
509 gatt_servers[instance_id - 1].server_id, instance_id);
511 CHECK_OAL_GATT_ENABLED();
512 CHECK_SERVER_INSTANCE(instance_id);
513 CHECK_SERVER_REGISTRATION(instance_id);
515 if (gatt_api->server->multi_adv_disable == NULL)
516 return OAL_STATUS_NOT_SUPPORT;
518 if ((gatt_servers[instance_id - 1].state != GATT_INS_ENABLED)) {
519 BT_ERR("Not Allowed, state: %d, instance_id: %d",
520 gatt_servers[instance_id - 1].state, instance_id);
521 return OAL_STATUS_BUSY;
524 ret = gatt_api->server->multi_adv_disable(gatt_servers[instance_id - 1].server_id);
526 if (ret != BT_STATUS_SUCCESS) {
527 BT_ERR("GATT disable multi advertising failed for server Instance: %d With Status: %s",
528 instance_id, status2string(ret));
529 return convert_to_oal_status(ret);
531 return OAL_STATUS_SUCCESS;
534 oal_status_t gatts_set_filter_policy(int filter_policy)
536 int ret = OAL_STATUS_SUCCESS;
538 CHECK_OAL_GATT_ENABLED();
540 /* send the filter_policy value to the HAL Layer */
541 ret = gatt_api->server->set_filter_policy(filter_policy);
543 if (ret != BT_STATUS_SUCCESS) {
544 BT_ERR("set_filter_policy failed: %d",ret);
545 return convert_to_oal_status(ret);
548 return OAL_STATUS_SUCCESS;
551 oal_status_t gatts_multi_adv_update(int instance_id,
552 int min_intv, int max_intv,
553 int adv_type, int chnl_map,
554 int tx_power, int timeout_s)
556 int ret = OAL_STATUS_SUCCESS;
557 API_TRACE("Multi advertising Update");
559 CHECK_OAL_GATT_ENABLED();
560 CHECK_SERVER_INSTANCE(instance_id);
561 CHECK_SERVER_REGISTRATION(instance_id);
563 if (gatt_api->server->multi_adv_update == NULL)
564 return OAL_STATUS_NOT_SUPPORT;
566 ret = gatt_api->server->multi_adv_update(gatt_servers[instance_id - 1].server_id,
569 tx_power, timeout_s);
571 if (ret != BT_STATUS_SUCCESS) {
572 BT_ERR("GATT update multi advertising failed for server Instance: %d With Status: %s",
573 instance_id, status2string(ret));
574 return convert_to_oal_status(ret);
576 return OAL_STATUS_SUCCESS;
579 oal_status_t gatts_multi_adv_set_inst_data(int instance_id,
580 oal_ble_multi_adv_param_setup_t *adv_param_setup)
582 int ret = OAL_STATUS_SUCCESS;
583 btgatt_adv_param_setup_t adv_setup;
584 API_TRACE("Set Multi advertising data, srv_if: %d, instance_id: %d",
585 gatt_servers[instance_id - 1].server_id, instance_id);
587 CHECK_OAL_GATT_ENABLED();
588 CHECK_SERVER_INSTANCE(instance_id);
589 CHECK_SERVER_REGISTRATION(instance_id);
591 if (gatt_api->server->multi_adv_set_inst_data == NULL)
592 return OAL_STATUS_NOT_SUPPORT;
594 if ((gatt_servers[instance_id - 1].state >= GATT_INS_DATA_SETTING) && (gatt_servers[instance_id - 1].state != GATT_INS_DATA_SET)) {
595 BT_ERR("Not Allowed, state: %d, instance_id: %d", gatt_servers[instance_id - 1].state, instance_id);
596 return OAL_STATUS_BUSY;
599 gatt_servers[instance_id - 1].state = GATT_INS_DATA_SETTING;
602 adv_setup.set_scan_rsp = adv_param_setup->set_scan_rsp;
603 adv_setup.include_name = adv_param_setup->include_name;
604 adv_setup.include_txpower = adv_param_setup->include_txpower;
605 adv_setup.appearance = adv_param_setup->appearance;
606 adv_setup.manufacturer_data = adv_param_setup->manufacturer_data;
607 adv_setup.manufacturer_data_len = adv_param_setup->manufacturer_data_len;
608 adv_setup.service_data = adv_param_setup->service_data;
609 adv_setup.service_data_len = adv_param_setup->service_data_len;
610 adv_setup.service_uuid = adv_param_setup->service_uuid;
611 adv_setup.service_uuid_len = adv_param_setup->service_uuid_len;
612 /* Solicit UUID handler: Start */
613 adv_setup.solicit_uuid = adv_param_setup->solicit_uuid;
614 adv_setup.solicit_uuid_len = adv_param_setup->solicit_uuid_len;
615 /* Solicit UUID handler: End */
616 adv_setup.min_interval = adv_param_setup->min_interval;
617 adv_setup.max_interval = adv_param_setup->min_interval;
618 adv_setup.adv_type = adv_param_setup->adv_type;
619 adv_setup.chnl_map = adv_param_setup->chnl_map;
620 adv_setup.tx_power = adv_param_setup->tx_power;
621 adv_setup.timeout_s = adv_param_setup->timeout_s;
623 adv_setup.server_if = gatt_servers[instance_id - 1].server_id;
625 ret = gatt_api->server->multi_adv_set_inst_data(adv_setup);
626 if (ret != BT_STATUS_SUCCESS) {
627 BT_ERR("GATT Set Multi advertising data failed: %s", status2string(ret));
628 gatt_servers[instance_id - 1].state = GATT_INS_DISABLED;
629 return convert_to_oal_status(ret);
631 return OAL_STATUS_SUCCESS;
634 oal_status_t gatts_disconnect(int instance_id, bt_address_t *device_address, int conn_id)
636 int ret = OAL_STATUS_SUCCESS;
639 API_TRACE("Server Disonnect: instance_id = %d, conn_id = %d", instance_id, conn_id);
641 CHECK_OAL_GATT_ENABLED();
642 CHECK_SERVER_INSTANCE(instance_id);
643 CHECK_SERVER_REGISTRATION(instance_id);
645 API_TRACE("[%s]", bdt_bd2str(device_address, &bdstr));
647 ret = gatt_api->server->disconnect(gatt_servers[instance_id - 1].server_id, (bt_bdaddr_t *) device_address, conn_id);
648 if (ret != BT_STATUS_SUCCESS) {
649 BT_ERR("GATT Server disconnect failed: %s", status2string(ret));
650 return convert_to_oal_status(ret);
652 return OAL_STATUS_SUCCESS;
656 /************************************GATT Server Functions*************************************/
658 static void cb_gatts_register_app(int status, int server_if, bt_uuid_t *uuid)
661 char str[2*BT_UUID_STRING_MAX];
662 uuid_to_stringname((service_uuid_t*)uuid, str);
664 event_gatts_register_t *event = g_malloc0(sizeof(event_gatts_register_t));
666 BT_INFO("BTGATT SERVER REGISTER APP CB, status:%d, server_if:%d", status, server_if);
667 BT_INFO("UUID: [%s]", str);
669 for (i = 0; i < NUM_SERVER_INST; i++) {
670 if (gatt_servers[i].server_id == -1) {
671 BT_INFO("Server Instance registered with stack [%d]", i+1);
672 gatt_servers[i].server_id = server_if;
673 event->server_inst = i+1;
674 memcpy(gatt_servers[i].uuid.uuid, uuid->uu, sizeof(bt_uuid_t));
675 memcpy(event->server_uuid.uuid, uuid->uu, sizeof(bt_uuid_t));
679 send_event(OAL_EVENT_BLE_SERVER_INSTANCE_INITIALISED, event, sizeof(event_gatts_register_t));
682 static void cb_gatts_listen(int status, int server_if)
684 gboolean prev_state[NUM_SERVER_INST];
686 event_ble_adv_status *event = g_malloc0(sizeof(event_ble_adv_status));
689 for (instance_id = 1 ; instance_id <= NUM_SERVER_INST ; instance_id++) {
690 if (gatt_servers[instance_id - 1].server_id == server_if) {
691 event->server_inst = instance_id;
694 BT_WARN("Server ID:%d recevied server Id:%d\n", gatt_servers[instance_id - 1].server_id, server_if);
697 if ((event->server_inst > 0) && (event->server_inst <= 10)) {
698 prev_state[event->server_inst - 1] = cur_adv_state[event->server_inst - 1];
699 new_state = (status == BT_STATUS_SUCCESS) ? !(prev_state[event->server_inst - 1]) : (prev_state[event->server_inst - 1]);
700 cur_adv_state[event->server_inst - 1] = new_state;
702 BT_ERR("Invalid Callback...");
707 BT_INFO("Adv State of server instance %d :%d", instance_id, new_state);
709 if (TRUE == new_state)
710 send_event(OAL_EVENT_BLE_ADVERTISING_STARTED, event, sizeof(event_ble_adv_status));
712 send_event(OAL_EVENT_BLE_ADVERTISING_STOPPED, event, sizeof(event_ble_adv_status));
715 static void cb_gatts_multi_adv_enable(int server_if, int status)
718 BT_INFO("BTGATT SERVER MULTI ADV ENABLE CB, status:%d, srv_if: %d", status, server_if);
719 event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
722 if (status != BT_STATUS_SUCCESS)
723 BT_ERR("MULTI ADV ENABLE failed, status:%d, srv_if: %d", status, server_if);
725 for (instance_id = 1 ; instance_id <= NUM_SERVER_INST ; instance_id++) {
726 if (gatt_servers[instance_id - 1].server_id == server_if) {
727 event->server_inst = instance_id;
728 gatt_servers[instance_id - 1].state = GATT_INS_ENABLED;
732 BT_ERR("Invalid Interface, srv_if: %d", server_if);
733 event->server_inst = -1;
735 event->status = convert_to_oal_status(status);
736 send_event(OAL_EVENT_BLE_MULTI_ADVERTISING_ENABLE, event, sizeof(event_ble_multiadv_status));
739 static void cb_gatts_multi_adv_disable(int server_if, int status)
741 BT_INFO("BTGATT SERVER MULTI ADV DISABLE CB, status:%d, srv_if: %d", status, server_if);
742 event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
745 if (status != BT_STATUS_SUCCESS)
746 BT_ERR("MULTI ADV DISABLE failed, status:%d, srv_if: %d", status, server_if);
748 for (instance_id = 1 ; instance_id <= NUM_SERVER_INST; instance_id++) {
749 if (gatt_servers[instance_id - 1].server_id == server_if) {
750 event->server_inst = instance_id;
751 gatt_servers[instance_id - 1].state = GATT_INS_DISABLED;
755 BT_ERR("Invalid Interface, srv_if: %d", server_if);
756 event->server_inst = -1;
758 event->status = convert_to_oal_status(status);
759 send_event(OAL_EVENT_BLE_MULTI_ADVERTISING_DISABLE, event, sizeof(event_ble_multiadv_status));
762 static void cb_gatts_multi_adv_update(int server_if, int status)
764 BT_INFO("BTGATT SERVER MULTI ADV UPDATE CB, status:%d, srv_if: %d", status, server_if);
765 event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
768 if (status != BT_STATUS_SUCCESS)
769 BT_ERR("MULTI ADV UPDATE failed, status:%d, srv_if: %d", status, server_if);
771 for (instance_id = 1 ; instance_id <= NUM_SERVER_INST ; instance_id++) {
772 if (gatt_servers[instance_id - 1].server_id == server_if) {
773 event->server_inst = instance_id;
777 BT_ERR("Invalid Interface, srv_if: %d", server_if);
778 event->server_inst = -1;
780 event->status = convert_to_oal_status(status);
781 send_event(OAL_EVENT_BLE_MULTI_ADVERTISING_UPDATE, event, sizeof(event_ble_multiadv_status));
784 static void cb_gatts_multi_adv_set_inst_data(int server_if, int status)
786 BT_INFO("BTGATT SERVER MULTI ADV SET INST DATA CB, status:%d, srv_if: %d", status, server_if);
787 event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
790 if (status != BT_STATUS_SUCCESS)
791 BT_ERR("MULTI ADV SET DATA failed, status:%d, srv_if: %d", status, server_if);
793 for (instance_id = 1 ; instance_id <= NUM_SERVER_INST ; instance_id++) {
794 if (gatt_servers[instance_id - 1].server_id == server_if) {
795 event->server_inst = instance_id;
796 gatt_servers[instance_id - 1].state = GATT_INS_DATA_SET;
800 BT_ERR("Invalid Interface, srv_if: %d", server_if);
801 event->server_inst = -1;
803 event->status = convert_to_oal_status(status);
804 send_event(OAL_EVENT_BLE_MULTI_ADVERTISING_SET_INST_DATA, event, sizeof(event_ble_multiadv_status));
807 oal_status_t gatts_get_att_mtu(int conn_id, int *mtu)
809 int ret = OAL_STATUS_SUCCESS;
810 API_TRACE("Get ATT MTU, conn_id: %d", conn_id);
811 CHECK_OAL_GATT_ENABLED();
812 OAL_CHECK_PARAMETER(mtu, return);
814 /* To prevent crash in case other libraries not support this api */
815 if (gatt_api->server->get_att_mtu == NULL) {
816 BT_WARN("get_att_mtu is NULL");
817 return OAL_STATUS_NOT_SUPPORT;
820 ret = gatt_api->server->get_att_mtu(conn_id, mtu);
821 if (ret != BT_STATUS_SUCCESS) {
822 BT_ERR("GATT MTU Size failed, status: %s", status2string(ret));
823 return convert_to_oal_status(ret);
826 BT_INFO("Current ATT MTU Size: %d", *mtu);
827 return OAL_STATUS_SUCCESS;
829 oal_status_t gatt_send_response_acquire(int conn_id, int trans_id,
830 int status, int fd, int mtu , void * fdlist)
832 int ret = OAL_STATUS_SUCCESS;
834 API_TRACE("Server Send Response : ConnId = %d, TransId = %d, %d", conn_id, trans_id, fd);
835 CHECK_OAL_GATT_ENABLED();
837 ret = gatt_api->server->send_response_acquire(conn_id, trans_id, status, fd, mtu, fdlist);
838 if (ret != BT_STATUS_SUCCESS) {
839 BT_ERR("GATT Server Send Response failed: %s", status2string(ret));
840 return convert_to_oal_status(ret);
842 return OAL_STATUS_SUCCESS;
845 oal_status_t gatts_add_service(int instance_id, oal_gatt_srvc_id_t *gatt_serv_id,
848 int ret = OAL_STATUS_SUCCESS;
849 btgatt_srvc_id_t btgatt_srvc_id;
850 char str[2*BT_UUID_STRING_MAX];
852 API_TRACE("Server Add Service : instance_id = %d, num_handles = %d", instance_id, num_handles);
853 CHECK_OAL_GATT_ENABLED();
855 CHECK_SERVER_INSTANCE(instance_id);
856 CHECK_SERVER_REGISTRATION(instance_id);
858 if (gatt_serv_id != NULL) {
859 uuid_to_stringname(&(gatt_serv_id->id.uuid), str);
860 API_TRACE("Service uuid: [%s]", str);
861 memcpy(btgatt_srvc_id.id.uuid.uu, gatt_serv_id->id.uuid.uuid, sizeof(gatt_serv_id->id.uuid));
862 btgatt_srvc_id.id.inst_id = gatt_serv_id->id.inst_id;
863 btgatt_srvc_id.is_primary = gatt_serv_id->is_prmry;
865 BT_INFO("GATT Server Service Id is NULL");
866 return OAL_STATUS_INVALID_PARAM;
869 ret = gatt_api->server->add_service(gatt_servers[instance_id - 1].server_id, &btgatt_srvc_id, num_handles);
871 if (ret != BT_STATUS_SUCCESS) {
872 BT_ERR("GATT Server add service failed: %s", status2string(ret));
873 return convert_to_oal_status(ret);
875 return OAL_STATUS_SUCCESS;
878 oal_status_t gatts_add_included_services(int instance_id, int serv_handle,
881 int ret = OAL_STATUS_SUCCESS;
882 API_TRACE("Server Add included Service : instance_id = %d, serv_handle = %d, incl_handle = %d",
883 instance_id, serv_handle, incl_handle);
884 CHECK_OAL_GATT_ENABLED();
886 CHECK_SERVER_INSTANCE(instance_id);
888 CHECK_SERVER_REGISTRATION(instance_id);
890 ret = gatt_api->server->add_included_service(gatt_servers[instance_id - 1].server_id, serv_handle, incl_handle);
891 if (ret != BT_STATUS_SUCCESS) {
892 BT_ERR("GATT Server Add included Service failed: %s", status2string(ret));
893 return convert_to_oal_status(ret);
895 return OAL_STATUS_SUCCESS;
898 oal_status_t gatts_add_characteristics(int instance_id, int serv_handle,
899 oal_uuid_t* charc_uuid, int propts, int permsn)
901 int ret = OAL_STATUS_SUCCESS;
902 char str[2*BT_UUID_STRING_MAX];
903 char prop[MAX_PROP_LEN], perm[MAX_PERM_LEN];
904 char_prop_to_string(propts, prop);
905 char_perm_to_string(permsn, perm);
906 API_TRACE("Server Add Characteristic : instance_id = %d, serv_handle = %d, Properties =%s, Permisison = %s",
907 instance_id, serv_handle, prop, perm);
908 CHECK_OAL_GATT_ENABLED();
910 CHECK_SERVER_INSTANCE(instance_id);
912 CHECK_SERVER_REGISTRATION(instance_id);
914 if (charc_uuid != NULL) {
915 uuid_to_stringname(charc_uuid, str);
916 API_TRACE("uuid: [%s]", str);
919 ret = gatt_api->server->add_characteristic(gatt_servers[instance_id - 1].server_id,
920 serv_handle, (bt_uuid_t*)charc_uuid, propts, permsn);
921 if (ret != BT_STATUS_SUCCESS) {
922 BT_ERR("GATT Server Add Characteristic failed: %s", status2string(ret));
923 return convert_to_oal_status(ret);
925 return OAL_STATUS_SUCCESS;
928 oal_status_t gatts_add_descriptor(int instance_id, int serv_handle,
929 oal_uuid_t* desc_uuid, int permsn)
931 int ret = OAL_STATUS_SUCCESS;
932 char str[2*BT_UUID_STRING_MAX];
933 char perm[MAX_PERM_LEN];
934 char_perm_to_string(permsn, perm);
935 API_TRACE("Server Add Descriptor : instance_id = %d, serv_handle = %d, Permisison = %s",
936 instance_id, serv_handle, perm);
937 CHECK_OAL_GATT_ENABLED();
939 CHECK_SERVER_INSTANCE(instance_id);
941 CHECK_SERVER_REGISTRATION(instance_id);
943 if (desc_uuid != NULL) {
944 uuid_to_stringname(desc_uuid, str);
945 API_TRACE("uuid: [%s]", str);
948 ret = gatt_api->server->add_descriptor(gatt_servers[instance_id - 1].server_id,
949 serv_handle, (bt_uuid_t*)desc_uuid, permsn);
951 if (ret != BT_STATUS_SUCCESS) {
952 BT_ERR("GATT Server Add Descriptor failed: %s", status2string(ret));
953 return convert_to_oal_status(ret);
955 return OAL_STATUS_SUCCESS;
958 oal_status_t gatts_start_service(int instance_id, int svc_handle, int transport)
960 int ret = OAL_STATUS_SUCCESS;
962 API_TRACE("Server Start Service : Ins_id = %d, srvHndl = %d, transport = %d",
963 instance_id, svc_handle, transport);
964 CHECK_OAL_GATT_ENABLED();
966 CHECK_SERVER_INSTANCE(instance_id);
968 CHECK_SERVER_REGISTRATION(instance_id);
970 ret = gatt_api->server->start_service(gatt_servers[instance_id - 1].server_id, svc_handle, transport);
972 if (ret != BT_STATUS_SUCCESS) {
973 BT_ERR("GATT Server Start Service failed: %s", status2string(ret));
974 return convert_to_oal_status(ret);
976 return OAL_STATUS_SUCCESS;
979 oal_status_t gatts_stop_service(int ins_id, int srv_hdl)
981 int ret = OAL_STATUS_SUCCESS;
983 API_TRACE("Server Stop Service : Ins_id = %d, srvHndl = %d, ", ins_id, srv_hdl);
984 CHECK_OAL_GATT_ENABLED();
986 CHECK_SERVER_INSTANCE(ins_id);
987 CHECK_SERVER_REGISTRATION(ins_id);
989 ret = gatt_api->server->stop_service(gatt_servers[ins_id - 1].server_id, srv_hdl);
990 if (ret != BT_STATUS_SUCCESS) {
991 BT_ERR("GATT Server Stop Service failed: %s", status2string(ret));
992 return convert_to_oal_status(ret);
994 return OAL_STATUS_SUCCESS;
997 oal_status_t gatts_delete_service(int ins_id, int srv_hdl)
999 int ret = OAL_STATUS_SUCCESS;
1001 API_TRACE("Server Delete Service : Ins_id = %d, srvHndl = %d, ", ins_id, srv_hdl);
1002 CHECK_OAL_GATT_ENABLED();
1004 CHECK_SERVER_INSTANCE(ins_id);
1006 CHECK_SERVER_REGISTRATION(ins_id);
1008 ret = gatt_api->server->delete_service(gatt_servers[ins_id - 1].server_id, srv_hdl);
1009 if (ret != BT_STATUS_SUCCESS) {
1010 BT_ERR("GATT Server Delete Service failed: %s", status2string(ret));
1011 return convert_to_oal_status(ret);
1013 return OAL_STATUS_SUCCESS;
1016 oal_status_t gatts_send_response(int conn_id, int trans_id, int resp_status, oal_gatt_response_t *response)
1018 int ret = OAL_STATUS_SUCCESS;
1020 API_TRACE("Server Send Response : ConnId = %d, TransId = %d, ", conn_id, trans_id);
1021 CHECK_OAL_GATT_ENABLED();
1023 if (response == NULL) {
1024 BT_ERR("GATT Server attribute value is empty");
1025 return OAL_STATUS_INVALID_PARAM;
1028 ret = gatt_api->server->send_response(conn_id, trans_id, resp_status, (btgatt_response_t *)response);
1029 if (ret != BT_STATUS_SUCCESS) {
1030 BT_ERR("GATT Server Send Response failed: %s", status2string(ret));
1031 return convert_to_oal_status(ret);
1033 return OAL_STATUS_SUCCESS;
1036 oal_status_t gatts_send_indication(int ins_id, int attr_hndl, int conn_id, int len, int confirm, char* value)
1038 int ret = OAL_STATUS_SUCCESS;
1040 API_TRACE("Server Send Indication : Ins_id = %d, srvHndl = %d, [%s]",
1041 ins_id, attr_hndl, confirm == TRUE ? "INDICATION" : "NOTIFICATION");
1042 CHECK_OAL_GATT_ENABLED();
1044 CHECK_SERVER_INSTANCE(ins_id);
1046 CHECK_SERVER_REGISTRATION(ins_id);
1048 if (value == NULL || len == 0) {
1049 BT_ERR("GATT Server attribute value is empty");
1050 return OAL_STATUS_INVALID_PARAM;
1053 ret = gatt_api->server->send_indication(gatt_servers[ins_id - 1].server_id,
1054 attr_hndl, conn_id, len, confirm, value);
1056 if (ret != BT_STATUS_SUCCESS) {
1057 BT_ERR("GATT Server Send Indication failed: %s", status2string(ret));
1058 return convert_to_oal_status(ret);
1060 return OAL_STATUS_SUCCESS;
1063 oal_status_t gatts_update_att_value(int ins_id, oal_gatt_value_t *value)
1065 int ret = OAL_STATUS_SUCCESS;
1067 CHECK_OAL_GATT_ENABLED();
1069 CHECK_SERVER_INSTANCE(ins_id);
1071 CHECK_SERVER_REGISTRATION(ins_id);
1073 if (value == NULL || value->len == 0) {
1074 BT_ERR("GATT Server attribute value is empty");
1075 return OAL_STATUS_INVALID_PARAM;
1078 API_TRACE("Server Update Value: Ins_id = [%d], att handle = [%d] data len [%d]",
1079 ins_id, value->handle, value->len);
1081 ret = gatt_api->server->update_att_value(gatt_servers[ins_id - 1].server_id,
1082 (int)value->handle, (int)value->len, (char*)value->value);
1084 if (ret != BT_STATUS_SUCCESS) {
1085 BT_ERR("GATT Server Update Attribute Value failed: [%s]", status2string(ret));
1086 return convert_to_oal_status(ret);
1088 return OAL_STATUS_SUCCESS;
1091 /* GATT Server Callbacks:Start */
1092 static void cb_gatts_service_added(int status, int server_if,
1093 btgatt_srvc_id_t *psrvc_id, int srvc_handle)
1097 BT_INFO("BTGATT SERVER SERVICE ADDED CB, status:%d, handle:%d", status, srvc_handle);
1098 event_gatts_srvc_prm_t* event = g_new0(event_gatts_srvc_prm_t, 1);
1100 CHECK_CORRECT_SERVER_ID(instance_id, server_if, event->gatt_srvc_stat.server_inst);
1102 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1103 event->gatt_srvc_stat.server_inst = -1;
1105 event->gatt_srvc_stat.servic_hndl = srvc_handle;
1106 if (psrvc_id != NULL) {
1107 event->gatt_srvc_id.id.inst_id = psrvc_id->id.inst_id;
1108 //event->gatt_srvc_id.id.uuid = psrvc_id->id.uuid;
1109 memcpy(event->gatt_srvc_id.id.uuid.uuid, psrvc_id->id.uuid.uu, sizeof(psrvc_id->id.uuid));
1110 event->gatt_srvc_id.is_prmry = psrvc_id->is_primary;
1113 event->gatt_srvc_stat.status = convert_to_oal_status(status);
1115 send_event(OAL_EVENT_GATTS_SERVICE_ADDED, event, sizeof(event_gatts_srvc_prm_t));
1118 static void cb_gatts_included_service_added(int status, int server_if,
1120 int incl_srvc_handle)
1123 BT_INFO("BTGATT SERVER INCLUDED SERVICE ADDED CB, status:%d, srvc handle:%d, incl handle:%d",
1124 status, srvc_handle, incl_srvc_handle);
1126 event_gatts_incld_srvc_t* event = g_new0(event_gatts_incld_srvc_t, 1);
1128 CHECK_CORRECT_SERVER_ID(ins_id, server_if, event->gatt_srvc_stat.server_inst);
1130 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1131 event->gatt_srvc_stat.server_inst = -1;
1133 event->gatt_srvc_stat.servic_hndl = srvc_handle;
1134 event->incl_srvc_hndl = incl_srvc_handle;
1135 event->gatt_srvc_stat.status = convert_to_oal_status(status);
1136 send_event(OAL_EVENT_GATTS_INCLUDED_SERVICE_ADDED, event, sizeof(event_gatts_incld_srvc_t));
1139 static void cb_gatts_characteristic_added(int status, int server_if, bt_uuid_t *char_id,
1140 int srvc_handle, int char_handle)
1144 BT_INFO("BTGATT SERVER CHARACTERISTIC ADDED CB, status:%d, srvc handle:%d, char handle:%d",
1145 status, srvc_handle, char_handle);
1147 event_gatts_srvc_charctr_t* event = g_new0(event_gatts_srvc_charctr_t, 1);
1149 CHECK_CORRECT_SERVER_ID(Ins_id, server_if, event->gatt_srvc_stat.server_inst);
1151 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1152 event->gatt_srvc_stat.server_inst = -1;
1154 event->gatt_srvc_stat.servic_hndl = srvc_handle;
1155 event->charctr_hndl = char_handle;
1156 event->gatt_srvc_stat.status = convert_to_oal_status(status);
1157 memcpy(event->charctr_uuid.uuid, char_id->uu, sizeof(bt_uuid_t));
1158 send_event(OAL_EVENT_GATTS_CHARACTERISTIC_ADDED, event, sizeof(event_gatts_srvc_charctr_t));
1161 static void cb_gatts_descriptor_added(int status, int server_if,
1162 bt_uuid_t *descr_id, int srvc_handle,
1166 BT_INFO("BTGATT SERVER DESCRIPTOR ADDED CB, status:%d, srvc handle:%d, desc handle:%d",
1167 status, srvc_handle, descr_handle);
1169 event_gatts_srvc_descr_t* event = g_new0(event_gatts_srvc_descr_t, 1);
1171 CHECK_CORRECT_SERVER_ID(Ins_id, server_if, event->gatt_srvc_stat.server_inst);
1173 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1174 event->gatt_srvc_stat.server_inst = -1;
1176 event->gatt_srvc_stat.servic_hndl = srvc_handle;
1177 event->descrptr_hndl = descr_handle;
1178 event->gatt_srvc_stat.status = convert_to_oal_status(status);
1179 memcpy(event->descrptr_uuid.uuid, descr_id->uu, sizeof(bt_uuid_t));
1180 send_event(OAL_EVENT_GATTS_DESCRIPTOR_ADDED, event, sizeof(event_gatts_srvc_descr_t));
1183 static void cb_gatts_service_started(int status, int server_if, int srvc_handle)
1186 BT_INFO("BTGATT SERVER SERVICE STARTED CB, status:%d, srvc_handle:%d", status, srvc_handle);
1188 event_gatts_srvc_t* event = g_new0(event_gatts_srvc_t, 1);
1190 CHECK_CORRECT_SERVER_ID(Ins_id, server_if, event->server_inst);
1192 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1193 event->server_inst = -1;
1195 event->servic_hndl = srvc_handle;
1196 event->status = convert_to_oal_status(status);
1197 send_event(OAL_EVENT_GATTS_SERVICE_STARTED, event, sizeof(event_gatts_srvc_t));
1200 static void cb_gatts_service_stopped(int status, int server_if, int srvc_handle)
1203 BT_INFO("BTGATT SERVER SERVICE STOPPED CB, status:%d, srvc_handle:%d", status, srvc_handle);
1205 event_gatts_srvc_t* event = g_new0(event_gatts_srvc_t, 1);
1207 CHECK_CORRECT_SERVER_ID(ins_id, server_if, event->server_inst);
1209 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1210 event->server_inst = -1;
1212 event->servic_hndl = srvc_handle;
1213 event->status = convert_to_oal_status(status);
1214 send_event(OAL_EVENT_GATTS_SERVICE_STOPED, event, sizeof(event_gatts_srvc_t));
1217 static void cb_gatts_service_deleted(int status, int server_if, int srvc_handle)
1220 BT_INFO("BTGATT SERVER SERVICE DELETED CB, status:%d, srvc_handle:%d", status, srvc_handle);
1222 event_gatts_srvc_t* event = g_new0(event_gatts_srvc_t, 1);
1224 CHECK_CORRECT_SERVER_ID(ins_id, server_if, event->server_inst);
1226 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1227 event->server_inst = -1;
1229 event->servic_hndl = srvc_handle;
1230 event->status = convert_to_oal_status(status);
1231 send_event(OAL_EVENT_GATTS_SERVICE_DELETED, event, sizeof(event_gatts_srvc_t));
1234 static void cb_gatts_connection(int conn_id, int server_if, int connected, bt_bdaddr_t *bda)
1237 oal_event_t event_type;
1239 BT_INFO("BTGATT SERVER CONNECTION connnected:%d, conn_id:%d server_if:%d", connected, conn_id, server_if);
1240 event_gatts_conn_t *event = g_new0(event_gatts_conn_t, 1);
1242 if (connected == TRUE) {
1243 for (ins_id = 1 ; ins_id <= NUM_SERVER_INST ; ins_id++) {
1244 if ((gatt_servers[ins_id - 1].server_id == server_if)) {
1245 event->server_inst = ins_id;
1248 BT_INFO("Server ID:%d recevied server Id:%d\n", gatt_servers[ins_id - 1].server_id, server_if);
1251 if (ins_id == NUM_SERVER_INST+1) {
1252 BT_WARN("Incorrect Server Interface received: %d\n", server_if);
1257 /* server_inst is not required in disconnected case */
1259 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1260 event->conn_id = conn_id;
1261 event_type = ((connected == TRUE) ? OAL_EVENT_GATTS_CONNECTION_COMPLETED : OAL_EVENT_GATTS_DISCONNECTION_COMPLETED);
1262 event->status = OAL_STATUS_SUCCESS;
1264 send_event(event_type, event, sizeof(event_gatts_conn_t));
1267 static void cb_gatts_request_read(int conn_id, int trans_id, bt_bdaddr_t *bda,
1268 int attr_handle, int offset, bool is_long)
1270 BT_INFO("BTGATT SERVER REQUEST READ CB: conn_id:%d", conn_id);
1272 event_gatts_srvc_read_attr_t* event = g_new0(event_gatts_srvc_read_attr_t, 1);
1274 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1276 event->attr_trans.attr_handle = attr_handle;
1277 event->attr_trans.conn_id = conn_id;
1278 event->attr_trans.trans_id = trans_id;
1279 event->attr_trans.offset = offset;
1280 event->is_long = is_long;
1282 send_event(OAL_EVENT_GATTS_REQUEST_READ, event, sizeof(event_gatts_srvc_read_attr_t));
1285 static void cb_gatts_request_write(int conn_id, int trans_id,
1286 bt_bdaddr_t *bda, int attr_handle,
1287 int offset, int length,
1288 bool need_rsp, bool is_prep, uint8_t* value)
1290 BT_INFO("BTGATT SERVER REQUEST WRITE conn_id:%d", conn_id);
1292 event_gatts_srvc_write_attr_t* event = g_new0(event_gatts_srvc_write_attr_t, 1);
1294 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1296 event->attr_trans.attr_handle = attr_handle;
1297 event->attr_trans.conn_id = conn_id;
1298 event->attr_trans.trans_id = trans_id;
1299 event->attr_trans.offset = offset;
1300 event->need_rsp = need_rsp;
1301 event->is_prep = is_prep;
1303 if (length > 0 && value != NULL) {
1304 if (length > OAL_GATT_MAX_ATTR_LEN)
1305 length = OAL_GATT_MAX_ATTR_LEN;
1306 memcpy(event->value, value, length);
1307 event->length = length;
1309 send_event(OAL_EVENT_GATTS_REQUEST_WRITE, event, sizeof(event_gatts_srvc_write_attr_t));
1312 static void cb_gatts_acquire_write(int mtu, int conn_id, int trans_id, int attr_handle , bt_bdaddr_t *bda)
1315 BT_INFO("BTGATT SERVER REQUEST ACQUIRE WRITE conn_id:%d", conn_id);
1317 event_gatts_srvc_acquire_attr_t* event = g_new0(event_gatts_srvc_acquire_attr_t, 1);
1319 event->attr_trans.attr_handle = attr_handle;
1320 event->attr_trans.conn_id = conn_id;
1321 event->attr_trans.trans_id = trans_id;
1323 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1325 send_event(OAL_EVENT_GATTS_REQUEST_ACQUIRE_WRITE, event, sizeof(event_gatts_srvc_acquire_attr_t));
1328 static void cb_gatts_acquire_notify(int mtu, int conn_id, int trans_id, int attr_handle)
1330 BT_INFO("BTGATT SERVER REQUEST ACQUIRE NOTIFY conn_id:%d", conn_id);
1332 event_gatts_srvc_acquire_attr_t* event = g_new0(event_gatts_srvc_acquire_attr_t, 1);
1334 event->attr_trans.attr_handle = attr_handle;
1335 event->attr_trans.conn_id = conn_id;
1336 event->attr_trans.trans_id = trans_id;
1339 send_event(OAL_EVENT_GATTS_REQUEST_ACQUIRE_NOTIFY, event, sizeof(event_gatts_srvc_acquire_attr_t));
1343 static void cb_gatts_response_confirmation(int status, int handle)
1345 BT_INFO("BTGATT SERVER RESPONSE CONFIRMATION CB, status:%d, handle:%d", status, handle);
1347 event_gatts_respons_t* event = g_new0(event_gatts_respons_t, 1);
1349 event->hndl = handle;
1350 event->status = convert_to_oal_status(status);
1352 send_event(OAL_EVENT_GATTS_RESPONSE_CONFIRMED, event, sizeof(event_gatts_respons_t));
1355 static void cb_indication_confirmation(int conn_id, int trans_id, int attr_handle, bt_bdaddr_t *bda)
1357 BT_INFO("BTGATT SERVER Indication Confirmation CB, conn_id:%d, trans_id:%d attr_handle:%d",
1358 conn_id, trans_id, attr_handle);
1360 event_gatts_ind_cnfrm_t* event = g_new0(event_gatts_ind_cnfrm_t, 1);
1362 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1364 event->attr_handle = attr_handle;
1365 event->conn_id = conn_id;
1366 event->trans_id = trans_id;
1367 send_event(OAL_EVENT_GATTS_IND_CONFIRM, event, sizeof(event_gatts_ind_cnfrm_t));
1370 static void cb_gatts_mtu_changed(int conn_id, int mtu)
1372 BT_INFO("BTGATT SERVER MTU CHANGED CB, conn_id: %d, mtu_size: %d", conn_id, mtu);
1373 event_gatts_mtu_changed_t *event = g_new0(event_gatts_mtu_changed_t, 1);
1374 event->conn_id = conn_id;
1375 event->mtu_size = mtu;
1376 send_event(OAL_EVENT_GATTS_MTU_CHANGED, event, sizeof(event_gatts_mtu_changed_t));
1380 static void cb_notifcation_changed(int conn_id, int trans_id, int attr_handle, bool notify, bt_bdaddr_t *bda)
1382 BT_INFO("BTGATT SERVER Notication CB, conn_id:%d, trans_id:%d attr_handle:%d Notify:%d",
1383 conn_id, trans_id, attr_handle, notify);
1385 event_gatts_notif_t* event = g_new0(event_gatts_notif_t, 1);
1387 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1389 event->attr_handle = attr_handle;
1390 event->conn_id = conn_id;
1391 event->trans_id = trans_id;
1392 event->notify = notify;
1393 send_event(OAL_EVENT_GATTS_NOTIFICATION, event, sizeof(event_gatts_notif_t));
1398 /* GATT Server Callbacks: End */
1399 /************************************ GATT Client ***********************************/
1400 /* Client Callbacks */
1401 static void cb_gattc_register_app(int status, int clientIf, bt_uuid_t *app_uuid)
1403 char uuid_str[BT_UUID_STRING_MAX];
1404 event_gattc_register_t *event;
1406 /* Check if GATT client registered for Default GATT client UUID */
1407 uuid_to_string((service_uuid_t *)app_uuid, uuid_str);
1409 BT_INFO("GATT CLIENT REGISTER APP CB, status:%d, clientIf:%d, %s", status, clientIf, uuid_str);
1410 event = g_new0(event_gattc_register_t, 1);
1411 event->client_if = clientIf;
1412 event->status = (status == 0 ? OAL_STATUS_SUCCESS : OAL_STATUS_INTERNAL_ERROR);
1413 memcpy(event->client_uuid.uuid, app_uuid->uu, sizeof(oal_uuid_t));
1414 send_event(OAL_EVENT_GATTC_REGISTRATION, event, sizeof(event_gattc_register_t));
1417 static void cb_gattc_scan_result(bt_bdaddr_t* bdaddress, uint8_t addr_type, int rssi,
1418 uint8_t *adv_data, int adv_data_len, uint8_t *scan_rsp_data, int scan_rsp_data_len)
1420 event_ble_scan_result_info *event;
1422 event = g_new0(event_ble_scan_result_info, 1);
1423 event->addr_type = addr_type;
1425 memcpy(event->address.addr, bdaddress->address, BT_ADDRESS_BYTES_NUM);
1426 memcpy(event->adv_data, adv_data, BLE_ADV_DATA_LENGTH);
1427 event->adv_data_len = adv_data_len;
1428 memcpy(event->scan_rsp_data, scan_rsp_data, BLE_ADV_DATA_LENGTH);
1429 event->scan_rsp_data_len = scan_rsp_data_len;
1430 send_event_bda_trace(OAL_EVENT_BLE_REMOTE_DEVICE_FOUND, event,
1431 sizeof(event_ble_scan_result_info), (bt_address_t *)bdaddress);
1434 static void cb_gattc_connection(int conn_id, int status, int client_if, bt_bdaddr_t* bda)
1436 event_gattc_conn_t *event;
1437 BT_INFO("BTGATT CLIENT CONNECTED CB, status:[%d] client_if[%d] id[%d]",
1438 status, client_if, conn_id);
1440 event = g_new0(event_gattc_conn_t, 1);
1441 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1442 event->client_if = client_if;
1444 if (BT_STATUS_SUCCESS != status) {
1445 event->status = OAL_STATUS_INTERNAL_ERROR;
1446 BT_ERR("gattc connection Error: %d", status);
1448 event->conn_id = conn_id;
1449 event->status = OAL_STATUS_SUCCESS;
1452 send_event_bda_trace(OAL_EVENT_GATTC_CONNECTION_COMPLETED, event,
1453 sizeof(*event), (bt_address_t *)bda);
1457 static void cb_gattc_disconnect(int conn_id, int status, int client_if, bt_bdaddr_t* bda)
1459 event_gattc_conn_t *event;
1460 BT_INFO("BTGATT Client Disconnect Complete cb, status:[%d], client_if[%d] id[%d]",
1461 status, client_if, conn_id);
1462 event = g_new0(event_gattc_conn_t, 1);
1463 event->status = convert_to_oal_status(status);
1464 event->client_if = client_if;
1465 event->conn_id = conn_id;
1466 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1467 send_event_bda_trace(OAL_EVENT_GATTC_DISCONNECTION_COMPLETED, event,
1468 sizeof(*event), (bt_address_t *)bda);
1471 static void cb_gattc_search_result(int conn_id, btgatt_srvc_id_t *srvc_id)
1473 char uuid_str[2*BT_UUID_STRING_MAX];
1475 uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str);
1476 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]",
1477 uuid_str, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1479 event_gattc_service_result_t *event = g_new0(event_gattc_service_result_t, 1);
1480 event->conn_status.status = OAL_STATUS_SUCCESS;
1481 event->conn_status.conn_id = conn_id;
1482 memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1484 send_event(OAL_EVENT_GATTC_SERVICE_SEARCH_RESULT, event, sizeof(*event));
1487 static void cb_gattc_search_complete(int conn_id, int status)
1489 BT_INFO("BTGATT Client Service Search Complete cb, status:%d, conn_id:%d",
1492 event_gattc_conn_status_t *event = g_new0(event_gattc_conn_status_t, 1);
1493 event->conn_id = conn_id;
1494 event->status = convert_to_oal_status(status);
1496 send_event(OAL_EVENT_GATTC_SERVICE_SEARCH_DONE, event, sizeof(*event));
1500 static void cb_gattc_get_characteristics(int conn_id, int status, btgatt_srvc_id_t *srvc_id,
1501 btgatt_gatt_id_t *char_id, int char_prop)
1503 char uuid_str1[2*BT_UUID_STRING_MAX];
1504 char uuid_str2[2*BT_UUID_STRING_MAX];
1507 BT_DBG("BTGATT Client Get Characteristic Callback, conn_id:%d, status:%d", conn_id, status);
1508 uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str1);
1509 BT_DBG("Service=> [%s], Inst_id [%u], Type [%s]",
1510 uuid_str1, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1512 event_gattc_characteristic_result_t *event = g_new0(event_gattc_characteristic_result_t, 1);
1513 event->conn_status.conn_id = conn_id;
1514 event->conn_status.status = convert_to_oal_status(status);
1515 memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1518 uuid_to_stringname((oal_uuid_t *)&(char_id->uuid), uuid_str2);
1519 BT_INFO("Charac => [%s], Inst_id [%u], Prop [%s]",
1520 uuid_str2, char_id->inst_id, char_prop_to_string(char_prop, str));
1521 event->char_prop = char_prop;
1522 memcpy(&(event->char_id), char_id, sizeof(oal_gatt_id_t));
1524 send_event(OAL_EVENT_GATTC_CHARAC_SERACH_RESULT, event, sizeof(*event));
1529 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)
1531 char uuid_str1[2*BT_UUID_STRING_MAX];
1532 char uuid_str2[2*BT_UUID_STRING_MAX];
1533 char uuid_str3[2*BT_UUID_STRING_MAX];
1534 BT_DBG("BTGATT Client Get Descriptor Callback, conn_id:%d, status:%d", conn_id, status);
1535 uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str1);
1536 uuid_to_stringname((oal_uuid_t *)&(char_id->uuid), uuid_str2);
1537 BT_DBG("Service=> [%s], Inst_id [%u], Type [%s]",
1538 uuid_str1, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1539 BT_DBG("Charac => [%s], Inst_id [%u]", uuid_str2, char_id->inst_id);
1541 event_gattc_descriptor_result_t *event = g_new0(event_gattc_descriptor_result_t, 1);
1542 event->conn_status.conn_id = conn_id;
1543 event->conn_status.status = convert_to_oal_status(status);
1544 memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1545 memcpy(&(event->char_id), char_id, sizeof(oal_gatt_id_t));
1548 uuid_to_stringname((oal_uuid_t *)&(descr_id->uuid), uuid_str3);
1549 BT_INFO("Desc => [%s], Inst_id [%u]", uuid_str3, descr_id->inst_id);
1550 memcpy(&(event->descr_id), descr_id, sizeof(oal_gatt_id_t));
1552 send_event(OAL_EVENT_GATTC_DESC_SERACH_RESULT, event, sizeof(*event));
1555 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)
1557 char uuid_str1[2*BT_UUID_STRING_MAX];
1558 char uuid_str2[2*BT_UUID_STRING_MAX];
1559 BT_INFO("BTGATT Client Register For Notification Callback, conn_id:%d, status:%d, registered: %s",
1560 conn_id, status, registered == 1 ? "REGISTERED" : "DEREGISTERED");
1561 uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str1);
1562 uuid_to_stringname((oal_uuid_t *)&(char_id->uuid), uuid_str2);
1563 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1564 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, char_id->inst_id);
1566 event_gattc_regdereg_notify_t *event = g_new0(event_gattc_regdereg_notify_t, 1);
1567 oal_event_t event_type = (registered == 1 ? OAL_EVENT_GATTC_NOTIFICATION_REGISTERED : OAL_EVENT_GATTC_NOTIFICATION_DEREGISTERED);
1568 event->conn_id = conn_id;
1569 event->status = convert_to_oal_status(status);
1570 memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1571 memcpy(&(event->char_id), char_id, sizeof(oal_gatt_id_t));
1573 send_event(event_type, event, sizeof(*event));
1576 static void cb_gattc_notify(int conn_id, btgatt_notify_params_t *p_data)
1579 char uuid_str1[2*BT_UUID_STRING_MAX];
1580 char uuid_str2[2*BT_UUID_STRING_MAX];
1582 BT_INFO("BTGATT Client Notify Callback, conn_id:%d", conn_id);
1583 BT_INFO("Server Address:[%s], is_notify:[%d], len:[%d]",
1584 bdt_bd2str((bt_address_t *)&(p_data->bda), &bdstr), p_data->is_notify, p_data->len);
1586 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1587 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1589 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]",
1590 uuid_str1, p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1591 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1593 if (p_data->len > 0) {
1595 data = g_malloc(3*p_data->len+1);
1597 BT_ERR("memory allocation failed");
1601 convert_hex_2_str((unsigned char *)p_data->value, p_data->len, data);
1602 BT_INFO("Notified Data: [%s]", data);
1604 event_gattc_notify_data *event = g_new0(event_gattc_notify_data, 1);
1605 memcpy(event->address.addr, p_data->bda.address, BT_ADDRESS_BYTES_NUM);
1606 event->is_notify = p_data->is_notify;
1607 event->data_len = p_data->len;
1608 memcpy(event->data, p_data->value, event->data_len);
1609 memcpy(&(event->char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1610 memcpy(&(event->srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1612 send_event_bda_trace(OAL_EVENT_GATTC_NOTIFY_DATA, event, sizeof(*event), (bt_address_t *)&p_data->bda);
1617 static void cb_gattc_service_changed(bt_bdaddr_t *bd_addr, int change_type, uint8_t *uuid, int conn_id, int inst_id)
1619 event_gattc_service_changed_data *event = g_new0(event_gattc_service_changed_data, 1);
1621 memcpy(event->address.addr, bd_addr->address, 6);
1622 event->change_type = change_type;
1623 memcpy(event->uuid.uuid, uuid, sizeof(event->uuid.uuid));
1624 event->conn_id = conn_id;
1625 event->inst_id = inst_id;
1627 send_event_bda_trace(OAL_EVENT_GATTC_SERVICE_CHANGED_IND, event, sizeof(*event), (bt_address_t *)bd_addr);
1630 static void cb_gattc_read_characteristic(int conn_id, int status, btgatt_read_params_t *p_data)
1632 char uuid_str1[2*BT_UUID_STRING_MAX];
1633 char uuid_str2[2*BT_UUID_STRING_MAX];
1634 BT_INFO("BTGATT Client Read Charcateristic Callback, conn_id:%d, read_status:%d", conn_id, status);
1636 event_gattc_read_data *event = g_new0(event_gattc_read_data, 1);
1637 event->uuid_status.conn_status.conn_id = conn_id;
1638 event->uuid_status.conn_status.status = convert_to_oal_status(status);
1639 event->value_type = p_data->value_type;
1640 memcpy(&(event->uuid_status.srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1641 memcpy(&(event->uuid_status.char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1643 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1644 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
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_DBG("Len: %u, value_type: %u", p_data->value.len, p_data->value_type);
1649 if (p_data->value.len > 0 && event->value_type == GATTC_READ_VALUE_TYPE_VALUE) {
1650 event->data_len = p_data->value.len;
1651 memcpy(&(event->data), &(p_data->value.value), event->data_len);
1654 send_event(OAL_EVENT_GATTC_READ_CHARAC, event, sizeof(*event));
1657 static void cb_gattc_write_characteristic(int conn_id, int status, btgatt_write_params_t *p_data)
1659 char uuid_str1[2*BT_UUID_STRING_MAX];
1660 char uuid_str2[2*BT_UUID_STRING_MAX];
1661 BT_INFO("BTGATT Client Write Charcateristic Callback, conn_id:%d, write_status:%d", conn_id, status);
1663 event_gattc_write_data *event = g_new0(event_gattc_write_data, 1);
1664 event->conn_status.conn_id = conn_id;
1665 event->conn_status.status = convert_to_oal_status(status);
1666 memcpy(&(event->srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1667 memcpy(&(event->char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1669 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1670 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1671 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1,
1672 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1673 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1675 send_event(OAL_EVENT_GATTC_WRITE_CHARAC, event, sizeof(*event));
1678 static void cb_gattc_write_descriptor(int conn_id, int status, btgatt_write_params_t *p_data)
1680 char uuid_str1[2*BT_UUID_STRING_MAX];
1681 char uuid_str2[2*BT_UUID_STRING_MAX];
1682 char uuid_str3[2*BT_UUID_STRING_MAX];
1683 BT_INFO("BTGATT Client Write Descriptor Callback, conn_id:%d, write_status:%d", conn_id, status);
1685 event_gattc_write_data *event = g_new0(event_gattc_write_data, 1);
1686 event->conn_status.conn_id = conn_id;
1687 event->conn_status.status = convert_to_oal_status(status);
1688 memcpy(&(event->srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1689 memcpy(&(event->char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1690 memcpy(&(event->descr_id), &(p_data->descr_id), sizeof(oal_gatt_id_t));
1692 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1693 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1694 uuid_to_stringname((oal_uuid_t *)&(p_data->descr_id.uuid), uuid_str3);
1695 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1,
1696 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1697 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1698 BT_INFO("Desc => [%s], Inst_id [%u]", uuid_str3, p_data->descr_id.inst_id);
1700 send_event(OAL_EVENT_GATTC_WRITE_DESCR, event, sizeof(*event));
1705 static void cb_gattc_read_descriptor(int conn_id, int status, btgatt_read_params_t *p_data)
1707 char uuid_str1[2*BT_UUID_STRING_MAX];
1708 char uuid_str2[2*BT_UUID_STRING_MAX];
1709 char uuid_str3[2*BT_UUID_STRING_MAX];
1710 BT_INFO("BTGATT Client Read Descriptor Callback, conn_id:%d, read_status:%d", conn_id, status);
1712 event_gattc_read_data *event = g_new0(event_gattc_read_data, 1);
1713 event->uuid_status.conn_status.conn_id = conn_id;
1714 event->uuid_status.conn_status.status = convert_to_oal_status(status);
1715 event->value_type = p_data->value_type;
1716 memcpy(&(event->uuid_status.srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1717 memcpy(&(event->uuid_status.char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1718 memcpy(&(event->uuid_status.descr_id), &(p_data->descr_id), sizeof(oal_gatt_id_t));
1721 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1722 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1723 uuid_to_stringname((oal_uuid_t *)&(p_data->descr_id.uuid), uuid_str3);
1724 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1,
1725 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1726 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1727 BT_INFO("Desc => [%s], Inst_id [%u]", uuid_str3, p_data->descr_id.inst_id);
1728 BT_DBG("Len: %u, value_type: %u", p_data->value.len, p_data->value_type);
1729 if (p_data->value.len > 0 && event->value_type == GATTC_READ_VALUE_TYPE_VALUE) {
1731 data = g_malloc(3*p_data->value.len+1);
1733 BT_ERR("memory allocation failed");
1737 convert_hex_2_str((unsigned char *)p_data->value.value, p_data->value.len, data);
1738 BT_DBG("Read Data: [%s]", data);
1739 event->data_len = p_data->value.len;
1740 memcpy(&(event->data), &(p_data->value.value), event->data_len);
1744 send_event(OAL_EVENT_GATTC_READ_DESCR, event, sizeof(*event));
1747 oal_status_t gattc_start_le_discovery(int client_id)
1750 int ret = OAL_STATUS_SUCCESS;
1752 API_TRACE("BTGATT CLIENT SCAN START");
1753 CHECK_OAL_GATT_ENABLED();
1754 CHECK_CLIENT_REGISTRATION(client_id);
1756 ret = gatt_api->client->scan(client_id, 1);
1757 if (ret != BT_STATUS_SUCCESS) {
1758 BT_ERR("Error:Start LE Discovery failed: %s", status2string(ret));
1759 return convert_to_oal_status(ret);
1761 return OAL_STATUS_SUCCESS;
1764 oal_status_t gattc_stop_le_discovery(int client_id)
1767 int ret = OAL_STATUS_SUCCESS;
1769 API_TRACE("Scan is stopped");
1770 CHECK_OAL_GATT_ENABLED();
1771 CHECK_CLIENT_REGISTRATION(client_id);
1772 ret = gatt_api->client->scan(client_id, 0);
1773 if (ret != BT_STATUS_SUCCESS) {
1774 BT_ERR("Error:Stop LE Discovery failed: %s", status2string(ret));
1775 return convert_to_oal_status(ret);
1777 return OAL_STATUS_SUCCESS;
1780 oal_status_t gattc_set_le_scan_param(int scan_type, int itv, int win)
1784 API_TRACE("Scan is stopped");
1785 CHECK_OAL_GATT_ENABLED();
1788 ret = gatt_api->client->set_scan_parameters(scan_type, itv, win);
1789 if (ret != BT_STATUS_SUCCESS) {
1790 BT_ERR("Error:Set scan parameters failed: %s", status2string(ret));
1791 return convert_to_oal_status(ret);
1794 ret = gatt_api->client->set_scan_parameters(itv, win);
1795 if (ret != BT_STATUS_SUCCESS) {
1796 BT_ERR("Error:Set scan parameters failed: %s", status2string(ret));
1797 return convert_to_oal_status(ret);
1801 return OAL_STATUS_SUCCESS;
1804 static void cb_gattc_configure_mtu_cmpl(int conn_id, int status, int mtu)
1806 event_gattc_mtu_configured_t *event;
1807 BT_INFO("BTGATT Client configure mtu complete Callback, conn_id: %d, status: %d, mtu: %d",
1808 conn_id, status, mtu);
1809 event = g_new0(event_gattc_mtu_configured_t, 1);
1810 event->conn_id = conn_id;
1812 event->status = convert_to_oal_status(status);
1813 send_event(OAL_EVENT_GATTC_MTU_EXCHANGE_COMPLETED, event, sizeof(*event));
1816 /************************************ GATT Client ***********************************/
1817 oal_status_t gattc_register(oal_uuid_t* client_uuid)
1820 char str[2*BT_UUID_STRING_MAX];
1821 int ret = OAL_STATUS_SUCCESS;
1823 CHECK_OAL_GATT_ENABLED();
1824 uuid_to_stringname(client_uuid, str);
1825 API_TRACE("uuid: [%s]", str);
1826 /* We will perform actual registration in cb_gattc_register_app callback */
1827 ret = gatt_api->client->register_client((bt_uuid_t *)client_uuid);
1828 if (ret != BT_STATUS_SUCCESS) {
1829 BT_ERR("GATT client register failed: %s", status2string(ret));
1830 return convert_to_oal_status(ret);
1832 return OAL_STATUS_SUCCESS;
1835 oal_status_t gattc_deregister(int client_id)
1837 int ret = OAL_STATUS_SUCCESS;
1839 API_TRACE("GATT client deregister");
1840 CHECK_OAL_GATT_ENABLED();
1841 CHECK_CLIENT_REGISTRATION(client_id);
1843 ret = gatt_api->client->unregister_client(client_id);
1844 if (ret != BT_STATUS_SUCCESS) {
1845 BT_ERR("GATT client deregister failed: %s", status2string(ret));
1846 return convert_to_oal_status(ret);
1849 return OAL_STATUS_SUCCESS;
1852 oal_status_t gattc_connect(int client_id, bt_address_t *device_address, int isDirect)
1854 int ret = OAL_STATUS_SUCCESS;
1857 OAL_CHECK_PARAMETER(device_address, return);
1858 API_TRACE("Client Connect: [%s]", bdt_bd2str(device_address, &bdstr));
1859 CHECK_OAL_GATT_ENABLED();
1860 CHECK_CLIENT_REGISTRATION(client_id);
1862 /* Handle the actual connection in cb_gattc_connection callback */
1863 ret = gatt_api->client->connect(client_id, (bt_bdaddr_t *)device_address, isDirect);
1864 if (ret != BT_STATUS_SUCCESS) {
1865 BT_ERR("GATT client connect failed: %s", status2string(ret));
1866 return convert_to_oal_status(ret);
1868 return OAL_STATUS_SUCCESS;
1871 oal_status_t gattc_disconnect(int client_id, bt_address_t *device_address, int conn_id)
1874 int ret = OAL_STATUS_SUCCESS;
1877 OAL_CHECK_PARAMETER(device_address, return);
1878 API_TRACE("Client Disconnect: [%s]", bdt_bd2str(device_address, &bdstr));
1879 CHECK_OAL_GATT_ENABLED();
1880 CHECK_CLIENT_REGISTRATION(client_id);
1881 CHECK_CLIENT_CONNECTION(conn_id);
1883 /* Handle actual disconnection in callback */
1884 ret = gatt_api->client->disconnect(client_id, (bt_bdaddr_t *) device_address, conn_id);
1885 if (ret != BT_STATUS_SUCCESS) {
1886 BT_ERR("GATT client disconnect failed: %s", status2string(ret));
1887 return convert_to_oal_status(ret);
1889 return OAL_STATUS_SUCCESS;
1893 oal_status_t gattc_search_service(int conn_id, oal_uuid_t *service_uuid)
1895 int ret = OAL_STATUS_SUCCESS;
1896 char uuid_str[2*BT_UUID_STRING_MAX];
1899 uuid_to_stringname(service_uuid, uuid_str);
1900 API_TRACE("Client Service Search UUID: [%s]", uuid_str);
1902 API_TRACE("Client Service Search All");
1904 CHECK_OAL_GATT_ENABLED();
1905 CHECK_CLIENT_CONNECTION(conn_id);
1906 ret = gatt_api->client->search_service(conn_id, (bt_uuid_t *)service_uuid);
1908 if (ret != BT_STATUS_SUCCESS) {
1909 BT_ERR("GATT client service search failed: %s", status2string(ret));
1910 return convert_to_oal_status(ret);
1912 return OAL_STATUS_SUCCESS;
1915 oal_status_t gattc_get_characteristic(int conn_id, oal_gatt_srvc_id_t *srvc_id, oal_gatt_id_t *char_id)
1917 int ret = OAL_STATUS_SUCCESS;
1918 char uuid_str[2*BT_UUID_STRING_MAX];
1920 OAL_CHECK_PARAMETER(srvc_id, return);
1921 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str);
1922 API_TRACE("Client Get Characteristic [%s]", uuid_str);
1923 CHECK_OAL_GATT_ENABLED();
1924 CHECK_CLIENT_CONNECTION(conn_id);
1925 ret = gatt_api->client->get_characteristic(conn_id, (btgatt_srvc_id_t *)srvc_id,
1926 (btgatt_gatt_id_t *)char_id);
1927 if (ret != BT_STATUS_SUCCESS) {
1928 BT_ERR("GATT client get characteristic failed: %s", status2string(ret));
1929 return convert_to_oal_status(ret);
1931 return OAL_STATUS_SUCCESS;
1934 oal_status_t gattc_get_descriptor(int conn_id, oal_gatt_srvc_id_t *srvc_id,
1935 oal_gatt_id_t *char_id, oal_gatt_id_t *desc_id)
1937 int ret = OAL_STATUS_SUCCESS;
1938 char uuid_str1[2*BT_UUID_STRING_MAX];
1939 char uuid_str2[2*BT_UUID_STRING_MAX];
1941 OAL_CHECK_PARAMETER(srvc_id, return);
1942 OAL_CHECK_PARAMETER(char_id, return);
1943 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1944 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1945 API_TRACE("Client Get Descriptor, Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
1946 CHECK_OAL_GATT_ENABLED();
1947 CHECK_CLIENT_CONNECTION(conn_id);
1948 ret = gatt_api->client->get_descriptor(conn_id, (btgatt_srvc_id_t *)srvc_id,
1949 (btgatt_gatt_id_t *)char_id, (btgatt_gatt_id_t *)desc_id);
1950 if (ret != BT_STATUS_SUCCESS) {
1951 BT_ERR("GATT client get descriptor failed: %s", status2string(ret));
1952 return convert_to_oal_status(ret);
1954 return OAL_STATUS_SUCCESS;
1957 oal_status_t gattc_register_for_notification(int conn_id, bt_address_t * address,
1958 oal_gatt_srvc_id_t *srvc_id, oal_gatt_id_t *char_id) {
1959 int ret = OAL_STATUS_SUCCESS;
1960 char uuid_str1[2*BT_UUID_STRING_MAX];
1961 char uuid_str2[2*BT_UUID_STRING_MAX];
1964 OAL_CHECK_PARAMETER(address, return);
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 Register Notification: [%s], Service_uuid: [%s], Char_uuid: [%s]", bdt_bd2str(address, &bdstr), uuid_str1, uuid_str2);
1970 CHECK_OAL_GATT_ENABLED();
1971 CHECK_CLIENT_CONNECTION(conn_id);
1973 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);
1974 if (ret != BT_STATUS_SUCCESS) {
1975 BT_ERR("GATT client register notification failed: %s", status2string(ret));
1976 return convert_to_oal_status(ret);
1979 return OAL_STATUS_SUCCESS;
1982 oal_status_t gattc_deregister_for_notification(int conn_id, bt_address_t * address,
1983 oal_gatt_srvc_id_t *srvc_id, oal_gatt_id_t *char_id) {
1984 int ret = OAL_STATUS_SUCCESS;
1985 char uuid_str1[2*BT_UUID_STRING_MAX];
1986 char uuid_str2[2*BT_UUID_STRING_MAX];
1989 OAL_CHECK_PARAMETER(address, return);
1990 OAL_CHECK_PARAMETER(srvc_id, return);
1991 OAL_CHECK_PARAMETER(char_id, return);
1992 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1993 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1994 API_TRACE("Client Deregister Notification: [%s], Service_uuid: [%s], Char_uuid: [%s]", bdt_bd2str(address, &bdstr), uuid_str1, uuid_str2);
1995 CHECK_OAL_GATT_ENABLED();
1996 CHECK_CLIENT_CONNECTION(conn_id);
1998 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);
1999 if (ret != BT_STATUS_SUCCESS) {
2000 BT_ERR("GATT client deregister notification failed: %s", status2string(ret));
2001 return convert_to_oal_status(ret);
2004 return OAL_STATUS_SUCCESS;
2008 oal_status_t gattc_read_characteristic(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2009 oal_gatt_id_t *char_id, oal_gatt_auth_req_t auth_req)
2011 int ret = OAL_STATUS_SUCCESS;
2012 char uuid_str1[2*BT_UUID_STRING_MAX];
2013 char uuid_str2[2*BT_UUID_STRING_MAX];
2015 OAL_CHECK_PARAMETER(srvc_id, return);
2016 OAL_CHECK_PARAMETER(char_id, return);
2017 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2018 uuid_to_stringname(&(char_id->uuid), uuid_str2);
2019 API_TRACE("Client Read Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
2020 CHECK_OAL_GATT_ENABLED();
2021 CHECK_CLIENT_CONNECTION(conn_id);
2023 ret = gatt_api->client->read_characteristic(conn_id, (btgatt_srvc_id_t *)srvc_id,
2024 (btgatt_gatt_id_t *)char_id, auth_req);
2025 if (ret != BT_STATUS_SUCCESS) {
2026 BT_ERR("GATT client read characteristic failed: %s", status2string(ret));
2027 return convert_to_oal_status(ret);
2030 return OAL_STATUS_SUCCESS;
2033 oal_status_t gattc_read_descriptor(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2034 oal_gatt_id_t *char_id, oal_gatt_id_t *desc_id, oal_gatt_auth_req_t auth_req)
2036 int ret = OAL_STATUS_SUCCESS;
2037 char uuid_str1[2*BT_UUID_STRING_MAX];
2038 char uuid_str2[2*BT_UUID_STRING_MAX];
2039 char uuid_str3[2*BT_UUID_STRING_MAX];
2041 OAL_CHECK_PARAMETER(srvc_id, return);
2042 OAL_CHECK_PARAMETER(char_id, return);
2043 OAL_CHECK_PARAMETER(desc_id, return);
2044 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2045 uuid_to_stringname(&(char_id->uuid), uuid_str2);
2046 uuid_to_stringname(&(desc_id->uuid), uuid_str3);
2047 API_TRACE("Client Read Descriptor: Service_uuid: [%s], Char_uuid: [%s], Desc_uuid: [%s]",
2048 uuid_str1, uuid_str2, uuid_str3);
2049 CHECK_OAL_GATT_ENABLED();
2050 CHECK_CLIENT_CONNECTION(conn_id);
2052 ret = gatt_api->client->read_descriptor(conn_id, (btgatt_srvc_id_t *)srvc_id,
2053 (btgatt_gatt_id_t *)char_id, (btgatt_gatt_id_t *)desc_id, auth_req);
2054 if (ret != BT_STATUS_SUCCESS) {
2055 BT_ERR("GATT client read descriptor failed: %s", status2string(ret));
2056 return convert_to_oal_status(ret);
2059 return OAL_STATUS_SUCCESS;
2062 oal_status_t gattc_acquire_notify(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2063 oal_gatt_id_t *char_id, int *fd, int *mtu)
2065 int ret = OAL_STATUS_SUCCESS;
2066 char uuid_str1[2*BT_UUID_STRING_MAX];
2067 char uuid_str2[2*BT_UUID_STRING_MAX];
2068 OAL_CHECK_PARAMETER(srvc_id, return);
2069 OAL_CHECK_PARAMETER(char_id, 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->acquire_notify(conn_id, (btgatt_srvc_id_t *)srvc_id,
2077 (btgatt_gatt_id_t *)char_id, fd, mtu);
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_acquire_write(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2087 oal_gatt_id_t *char_id, oal_gatt_auth_req_t auth_req , int *fd, int*mtu)
2089 int ret = OAL_STATUS_SUCCESS;
2090 char uuid_str1[2*BT_UUID_STRING_MAX];
2091 char uuid_str2[2*BT_UUID_STRING_MAX];
2092 OAL_CHECK_PARAMETER(srvc_id, return);
2093 OAL_CHECK_PARAMETER(char_id, return);
2094 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2095 uuid_to_stringname(&(char_id->uuid), uuid_str2);
2096 API_TRACE("Client Write Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
2097 CHECK_OAL_GATT_ENABLED();
2098 CHECK_CLIENT_CONNECTION(conn_id);
2100 ret = gatt_api->client->acquire_write(conn_id, (btgatt_srvc_id_t *)srvc_id,
2101 (btgatt_gatt_id_t *)char_id, auth_req, fd, mtu);
2102 if (ret != BT_STATUS_SUCCESS) {
2103 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
2104 return convert_to_oal_status(ret);
2107 return OAL_STATUS_SUCCESS;
2110 oal_status_t gattc_write_characteristic(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2111 oal_gatt_id_t *char_id, oal_gatt_write_type_t write_type,
2112 int len, oal_gatt_auth_req_t auth_req, char* data)
2114 int ret = OAL_STATUS_SUCCESS;
2115 char uuid_str1[2*BT_UUID_STRING_MAX];
2116 char uuid_str2[2*BT_UUID_STRING_MAX];
2117 OAL_CHECK_PARAMETER(srvc_id, return);
2118 OAL_CHECK_PARAMETER(char_id, return);
2119 OAL_CHECK_PARAMETER(data, return);
2120 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2121 uuid_to_stringname(&(char_id->uuid), uuid_str2);
2122 API_TRACE("Client Write Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
2123 CHECK_OAL_GATT_ENABLED();
2124 CHECK_CLIENT_CONNECTION(conn_id);
2126 ret = gatt_api->client->write_characteristic(conn_id, (btgatt_srvc_id_t *)srvc_id,
2127 (btgatt_gatt_id_t *)char_id, write_type, len, auth_req, data);
2128 if (ret != BT_STATUS_SUCCESS) {
2129 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
2130 return convert_to_oal_status(ret);
2133 return OAL_STATUS_SUCCESS;
2136 oal_status_t gattc_write_descriptor(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2137 oal_gatt_id_t *char_id, oal_gatt_id_t *desc_id,
2138 oal_gatt_write_type_t write_type, int len, oal_gatt_auth_req_t auth_req, char* data)
2140 int ret = OAL_STATUS_SUCCESS;
2141 char uuid_str1[2*BT_UUID_STRING_MAX];
2142 char uuid_str2[2*BT_UUID_STRING_MAX];
2143 char uuid_str3[2*BT_UUID_STRING_MAX];
2144 OAL_CHECK_PARAMETER(srvc_id, return);
2145 OAL_CHECK_PARAMETER(char_id, return);
2146 OAL_CHECK_PARAMETER(desc_id, return);
2147 OAL_CHECK_PARAMETER(data, return);
2148 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2149 uuid_to_stringname(&(char_id->uuid), uuid_str2);
2150 uuid_to_stringname(&(desc_id->uuid), uuid_str3);
2151 API_TRACE("Client Write Descriptor: Service_uuid: [%s], Char_uuid: [%s], Desc_uuid: [%s]",
2152 uuid_str1, uuid_str2, uuid_str3);
2153 CHECK_OAL_GATT_ENABLED();
2154 CHECK_CLIENT_CONNECTION(conn_id);
2156 ret = gatt_api->client->write_descriptor(conn_id, (btgatt_srvc_id_t *)srvc_id,
2157 (btgatt_gatt_id_t *)char_id, (btgatt_gatt_id_t *)desc_id, write_type, len, auth_req, data);
2158 if (ret != BT_STATUS_SUCCESS) {
2159 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
2160 return convert_to_oal_status(ret);
2163 return OAL_STATUS_SUCCESS;
2166 oal_status_t gattc_conn_param_update(bt_address_t * address, int min, int max, int latency, int timeout)
2171 OAL_CHECK_PARAMETER(address, return);
2172 API_TRACE("Conn Param Update: [%s]", bdt_bd2str(address, &bdstr));
2173 CHECK_OAL_GATT_ENABLED();
2175 BT_INFO("[%s] min[%d] max[%d] latency[%d] timeout[%d]", bdt_bd2str(address, &bdstr), min, max, latency, timeout);
2176 ret = gatt_api->client->conn_parameter_update((bt_bdaddr_t *)address, min, max, latency, timeout);
2177 if (ret != BT_STATUS_SUCCESS) {
2178 BT_ERR("error: %s", status2string(ret));
2179 return convert_to_oal_status(ret);
2181 return OAL_STATUS_SUCCESS;
2184 oal_status_t gattc_unregister_scan_filter(int slot_id)
2190 CHECK_OAL_GATT_ENABLED();
2192 BT_INFO("Remove Scan filter Slot ID is: [%d]", slot_id);
2194 ret = gatt_api->client->scan_filter_clear(client_if, slot_id);
2195 if (ret != BT_STATUS_SUCCESS) {
2196 BT_ERR("error: %s", status2string(ret));
2197 return convert_to_oal_status(ret);
2199 return OAL_STATUS_SUCCESS;
2202 oal_status_t gattc_enable_scan_filter(int client_if)
2207 CHECK_OAL_GATT_ENABLED();
2209 BT_INFO("Enable Scan filter. Client If: %d", client_if);
2211 ret = gatt_api->client->scan_filter_enable(client_if, TRUE);
2212 if (ret != BT_STATUS_SUCCESS) {
2213 BT_ERR("error: %s", status2string(ret));
2214 return convert_to_oal_status(ret);
2216 return OAL_STATUS_SUCCESS;
2219 oal_status_t gattc_disable_scan_filter(int client_if)
2224 CHECK_OAL_GATT_ENABLED();
2226 BT_INFO("Disable Scan filter. Client If: %d", client_if);
2228 ret = gatt_api->client->scan_filter_enable(client_if, FALSE);
2229 if (ret != BT_STATUS_SUCCESS) {
2230 BT_ERR("error: %s", status2string(ret));
2231 return convert_to_oal_status(ret);
2233 return OAL_STATUS_SUCCESS;
2236 oal_status_t gattc_register_scan_filter(oal_ble_scan_filter_t* filter_data)
2239 int client_info = 0;
2242 int company_id_mask = 0;
2243 int address_type = 0;
2244 int feature_selection = 0;
2246 oal_ble_scan_filter_param_setup_t scan_filter_setup = {.list_logic_type = 0,
2247 .filt_logic_type = 1,
2248 .rssi_high_thres = -127,
2249 .rssi_low_thres = -127,
2253 .found_timeout_cnt = 0
2256 OAL_CHECK_PARAMETER(filter_data, return);
2258 CHECK_OAL_GATT_ENABLED();
2260 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS){
2262 BT_INFO("OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS is being added");
2263 BT_INFO("BT remote Device address is %s", bdt_bd2str(filter_data->device_address, &bdstr));
2264 ret = gatt_api->client->scan_filter_add_remove(client_info,
2266 OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS,
2267 filter_data->slot_id,
2272 (bt_bdaddr_t*)filter_data->device_address,
2279 if (ret != BT_STATUS_SUCCESS){
2280 BT_ERR("error: %s", status2string(ret));
2281 BT_INFO("unregistering already set filter features.");
2282 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2283 return convert_to_oal_status(ret);
2286 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS;
2288 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_NAME){
2289 ret = gatt_api->client->scan_filter_add_remove(client_info,
2291 OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_NAME,
2292 filter_data->slot_id,
2300 filter_data->device_name, // device_name as p_data in HAL
2304 if (ret != BT_STATUS_SUCCESS){
2305 BT_ERR("error: %s", status2string(ret));
2306 BT_INFO("unregistering already set filter features.");
2307 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2308 return convert_to_oal_status(ret);
2311 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_NAME;
2313 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_UUID){
2314 BT_INFO("OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_UUID is being added");
2315 char uuid_str1[2*BT_UUID_STRING_MAX];
2316 char uuid_str2[2*BT_UUID_STRING_MAX];
2318 uuid_to_stringname((service_uuid_t*)filter_data->service_uuid, uuid_str1);
2319 uuid_to_stringname((service_uuid_t*)filter_data->service_uuid_mask, uuid_str2);
2321 BT_INFO("Service UUID is [%s] and Service UUID mask is [%s]", uuid_str1, uuid_str2);
2322 ret = gatt_api->client->scan_filter_add_remove(client_info,
2324 OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_UUID,
2325 filter_data->slot_id,
2328 (bt_uuid_t*)filter_data->service_uuid,
2329 (bt_uuid_t*)filter_data->service_uuid_mask,
2332 filter_data->service_uuid_len, // service_uuid_len as data_len in HAL
2334 filter_data->service_uuid_mask_len,
2337 if (ret != BT_STATUS_SUCCESS){
2338 BT_ERR("error: %s", status2string(ret));
2339 BT_INFO("unregistering already set filter features.");
2340 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2341 return convert_to_oal_status(ret);
2344 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_UUID;
2346 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID){
2347 BT_INFO("OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID is being added");
2348 char uuid_str1[2*BT_UUID_STRING_MAX];
2349 char uuid_str2[2*BT_UUID_STRING_MAX];
2350 uuid_to_stringname((service_uuid_t*)filter_data->service_solicitation_uuid, uuid_str1);
2351 uuid_to_stringname((service_uuid_t*)filter_data->service_solicitation_uuid_mask, uuid_str2);
2352 BT_INFO("Service Solicitation UUID is [%s] and Service Solicitation UUID mask is [%s]", uuid_str1, uuid_str2);
2353 ret = gatt_api->client->scan_filter_add_remove(client_info,
2355 OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID,
2356 filter_data->slot_id,
2359 (bt_uuid_t*)filter_data->service_solicitation_uuid,
2360 (bt_uuid_t*)filter_data->service_solicitation_uuid_mask,
2363 filter_data->service_solicitation_uuid_len, // service_solicitation_uuid_len as data_len in HAL
2365 filter_data->service_solicitation_uuid_mask_len,
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_SERVICE_SOLICITATION_UUID;
2377 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_DATA){
2378 ret = gatt_api->client->scan_filter_add_remove(client_info,
2380 OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_DATA,
2381 filter_data->slot_id,
2388 filter_data->service_data_len, //service_data_len as data_len in HAL
2389 (char*)filter_data->service_data,
2390 filter_data->service_data_mask_len,
2391 (char*)filter_data->service_data_mask
2393 if (ret != BT_STATUS_SUCCESS){
2394 BT_ERR("error: %s", status2string(ret));
2395 BT_INFO("unregistering already set filter features.");
2396 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2397 return convert_to_oal_status(ret);
2400 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_DATA;
2402 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA){
2403 ret = gatt_api->client->scan_filter_add_remove(client_info,
2405 OAL_BLE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA,
2406 filter_data->slot_id,
2407 filter_data->manufacturer_id,
2413 filter_data->manufacturer_data_len, //manufacturer_data_len as data_len in HAL
2414 (char*)filter_data->manufacturer_data,
2415 filter_data->manufacturer_data_mask_len,
2416 (char*)filter_data->manufacturer_data_mask
2418 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA;
2419 if (ret != BT_STATUS_SUCCESS){
2420 BT_ERR("error: %s", status2string(ret));
2421 BT_INFO("unregistering already set filter features.");
2422 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2423 return convert_to_oal_status(ret);
2426 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA;
2429 BT_DBG("Filter selection 0x%.2x", feature_selection);
2431 ret = gatt_api->client->scan_filter_param_setup(client_info, action, filter_data->slot_id, feature_selection,
2432 scan_filter_setup.list_logic_type, scan_filter_setup.filt_logic_type,
2433 scan_filter_setup.rssi_high_thres, scan_filter_setup.rssi_low_thres,
2434 scan_filter_setup.dely_mode, scan_filter_setup.found_timeout,
2435 scan_filter_setup.lost_timeout, scan_filter_setup.found_timeout_cnt);
2436 if (ret != BT_STATUS_SUCCESS){
2437 BT_ERR("error: %s", status2string(ret));
2438 return convert_to_oal_status(ret);
2440 return OAL_STATUS_SUCCESS;
2443 oal_status_t gattc_configure_mtu(int conn_id, int mtu)
2447 API_TRACE("Configure MTU Size: [%d]", mtu);
2448 CHECK_OAL_GATT_ENABLED();
2449 CHECK_CLIENT_CONNECTION(conn_id);
2451 /* To prevent crash in case other libraries not support this api */
2452 if (gatt_api->client->configure_mtu == NULL) {
2453 BT_WARN("configure_mtu is NULL");
2454 return OAL_STATUS_NOT_SUPPORT;
2457 ret = gatt_api->client->configure_mtu(conn_id, mtu);
2458 if (ret != BT_STATUS_SUCCESS) {
2459 BT_ERR("Gatt client configure_mtu error: %s", status2string(ret));
2460 return convert_to_oal_status(ret);
2462 return OAL_STATUS_SUCCESS;
2465 oal_status_t gattc_add_connection_info(bt_address_t *device_address, int conn_id, int server_inst_id)
2467 int ret = OAL_STATUS_SUCCESS;
2470 OAL_CHECK_PARAMETER(device_address, return);
2471 API_TRACE("Add connection info: [%s]", bdt_bd2str(device_address, &bdstr));
2472 CHECK_OAL_GATT_ENABLED();
2473 CHECK_SERVER_INSTANCE(server_inst_id);
2474 CHECK_SERVER_REGISTRATION(server_inst_id);
2476 ret = gatt_api->client->add_connection_info((bt_bdaddr_t *)device_address, conn_id, server_inst_id);
2477 if (ret != BT_STATUS_SUCCESS) {
2478 BT_ERR("GATT connection info add failed: %s", status2string(ret));
2479 return convert_to_oal_status(ret);
2481 return OAL_STATUS_SUCCESS;