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) {
1651 data = g_malloc(3*p_data->value.len+1);
1653 BT_ERR("memory allocation failed");
1657 convert_hex_2_str((unsigned char *)p_data->value.value, p_data->value.len, data);
1658 BT_DBG("Read Data: [%s]", data);
1659 event->data_len = p_data->value.len;
1660 memcpy(&(event->data), &(p_data->value.value), event->data_len);
1664 send_event(OAL_EVENT_GATTC_READ_CHARAC, event, sizeof(*event));
1667 static void cb_gattc_write_characteristic(int conn_id, int status, btgatt_write_params_t *p_data)
1669 char uuid_str1[2*BT_UUID_STRING_MAX];
1670 char uuid_str2[2*BT_UUID_STRING_MAX];
1671 BT_INFO("BTGATT Client Write Charcateristic Callback, conn_id:%d, write_status:%d", conn_id, status);
1673 event_gattc_write_data *event = g_new0(event_gattc_write_data, 1);
1674 event->conn_status.conn_id = conn_id;
1675 event->conn_status.status = convert_to_oal_status(status);
1676 memcpy(&(event->srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1677 memcpy(&(event->char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1679 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1680 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1681 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1,
1682 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1683 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1685 send_event(OAL_EVENT_GATTC_WRITE_CHARAC, event, sizeof(*event));
1688 static void cb_gattc_write_descriptor(int conn_id, int status, btgatt_write_params_t *p_data)
1690 char uuid_str1[2*BT_UUID_STRING_MAX];
1691 char uuid_str2[2*BT_UUID_STRING_MAX];
1692 char uuid_str3[2*BT_UUID_STRING_MAX];
1693 BT_INFO("BTGATT Client Write Descriptor Callback, conn_id:%d, write_status:%d", conn_id, status);
1695 event_gattc_write_data *event = g_new0(event_gattc_write_data, 1);
1696 event->conn_status.conn_id = conn_id;
1697 event->conn_status.status = convert_to_oal_status(status);
1698 memcpy(&(event->srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1699 memcpy(&(event->char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1700 memcpy(&(event->descr_id), &(p_data->descr_id), sizeof(oal_gatt_id_t));
1702 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1703 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1704 uuid_to_stringname((oal_uuid_t *)&(p_data->descr_id.uuid), uuid_str3);
1705 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1,
1706 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1707 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1708 BT_INFO("Desc => [%s], Inst_id [%u]", uuid_str3, p_data->descr_id.inst_id);
1710 send_event(OAL_EVENT_GATTC_WRITE_DESCR, event, sizeof(*event));
1715 static void cb_gattc_read_descriptor(int conn_id, int status, btgatt_read_params_t *p_data)
1717 char uuid_str1[2*BT_UUID_STRING_MAX];
1718 char uuid_str2[2*BT_UUID_STRING_MAX];
1719 char uuid_str3[2*BT_UUID_STRING_MAX];
1720 BT_INFO("BTGATT Client Read Descriptor Callback, conn_id:%d, read_status:%d", conn_id, status);
1722 event_gattc_read_data *event = g_new0(event_gattc_read_data, 1);
1723 event->uuid_status.conn_status.conn_id = conn_id;
1724 event->uuid_status.conn_status.status = convert_to_oal_status(status);
1725 event->value_type = p_data->value_type;
1726 memcpy(&(event->uuid_status.srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1727 memcpy(&(event->uuid_status.char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1728 memcpy(&(event->uuid_status.descr_id), &(p_data->descr_id), sizeof(oal_gatt_id_t));
1731 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1732 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1733 uuid_to_stringname((oal_uuid_t *)&(p_data->descr_id.uuid), uuid_str3);
1734 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1,
1735 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1736 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1737 BT_INFO("Desc => [%s], Inst_id [%u]", uuid_str3, p_data->descr_id.inst_id);
1738 BT_DBG("Len: %u, value_type: %u", p_data->value.len, p_data->value_type);
1739 if (p_data->value.len > 0 && event->value_type == GATTC_READ_VALUE_TYPE_VALUE) {
1741 data = g_malloc(3*p_data->value.len+1);
1743 BT_ERR("memory allocation failed");
1747 convert_hex_2_str((unsigned char *)p_data->value.value, p_data->value.len, data);
1748 BT_DBG("Read Data: [%s]", data);
1749 event->data_len = p_data->value.len;
1750 memcpy(&(event->data), &(p_data->value.value), event->data_len);
1754 send_event(OAL_EVENT_GATTC_READ_DESCR, event, sizeof(*event));
1757 oal_status_t gattc_start_le_discovery(int client_id)
1760 int ret = OAL_STATUS_SUCCESS;
1762 API_TRACE("BTGATT CLIENT SCAN START");
1763 CHECK_OAL_GATT_ENABLED();
1764 CHECK_CLIENT_REGISTRATION(client_id);
1766 ret = gatt_api->client->scan(client_id, 1);
1767 if (ret != BT_STATUS_SUCCESS) {
1768 BT_ERR("Error:Start LE Discovery failed: %s", status2string(ret));
1769 return convert_to_oal_status(ret);
1771 return OAL_STATUS_SUCCESS;
1774 oal_status_t gattc_stop_le_discovery(int client_id)
1777 int ret = OAL_STATUS_SUCCESS;
1779 API_TRACE("Scan is stopped");
1780 CHECK_OAL_GATT_ENABLED();
1781 CHECK_CLIENT_REGISTRATION(client_id);
1782 ret = gatt_api->client->scan(client_id, 0);
1783 if (ret != BT_STATUS_SUCCESS) {
1784 BT_ERR("Error:Stop LE Discovery failed: %s", status2string(ret));
1785 return convert_to_oal_status(ret);
1787 return OAL_STATUS_SUCCESS;
1790 oal_status_t gattc_set_le_scan_param(int scan_type, int itv, int win)
1794 API_TRACE("Scan is stopped");
1795 CHECK_OAL_GATT_ENABLED();
1798 ret = gatt_api->client->set_scan_parameters(scan_type, itv, win);
1799 if (ret != BT_STATUS_SUCCESS) {
1800 BT_ERR("Error:Set scan parameters failed: %s", status2string(ret));
1801 return convert_to_oal_status(ret);
1804 ret = gatt_api->client->set_scan_parameters(itv, win);
1805 if (ret != BT_STATUS_SUCCESS) {
1806 BT_ERR("Error:Set scan parameters failed: %s", status2string(ret));
1807 return convert_to_oal_status(ret);
1811 return OAL_STATUS_SUCCESS;
1814 static void cb_gattc_configure_mtu_cmpl(int conn_id, int status, int mtu)
1816 event_gattc_mtu_configured_t *event;
1817 BT_INFO("BTGATT Client configure mtu complete Callback, conn_id: %d, status: %d, mtu: %d",
1818 conn_id, status, mtu);
1819 event = g_new0(event_gattc_mtu_configured_t, 1);
1820 event->conn_id = conn_id;
1822 event->status = convert_to_oal_status(status);
1823 send_event(OAL_EVENT_GATTC_MTU_EXCHANGE_COMPLETED, event, sizeof(*event));
1826 /************************************ GATT Client ***********************************/
1827 oal_status_t gattc_register(oal_uuid_t* client_uuid)
1830 char str[2*BT_UUID_STRING_MAX];
1831 int ret = OAL_STATUS_SUCCESS;
1833 CHECK_OAL_GATT_ENABLED();
1834 uuid_to_stringname(client_uuid, str);
1835 API_TRACE("uuid: [%s]", str);
1836 /* We will perform actual registration in cb_gattc_register_app callback */
1837 ret = gatt_api->client->register_client((bt_uuid_t *)client_uuid);
1838 if (ret != BT_STATUS_SUCCESS) {
1839 BT_ERR("GATT client register failed: %s", status2string(ret));
1840 return convert_to_oal_status(ret);
1842 return OAL_STATUS_SUCCESS;
1845 oal_status_t gattc_deregister(int client_id)
1847 int ret = OAL_STATUS_SUCCESS;
1849 API_TRACE("GATT client deregister");
1850 CHECK_OAL_GATT_ENABLED();
1851 CHECK_CLIENT_REGISTRATION(client_id);
1853 ret = gatt_api->client->unregister_client(client_id);
1854 if (ret != BT_STATUS_SUCCESS) {
1855 BT_ERR("GATT client deregister failed: %s", status2string(ret));
1856 return convert_to_oal_status(ret);
1859 return OAL_STATUS_SUCCESS;
1862 oal_status_t gattc_connect(int client_id, bt_address_t *device_address, int isDirect)
1864 int ret = OAL_STATUS_SUCCESS;
1867 OAL_CHECK_PARAMETER(device_address, return);
1868 API_TRACE("Client Connect: [%s]", bdt_bd2str(device_address, &bdstr));
1869 CHECK_OAL_GATT_ENABLED();
1870 CHECK_CLIENT_REGISTRATION(client_id);
1872 /* Handle the actual connection in cb_gattc_connection callback */
1873 ret = gatt_api->client->connect(client_id, (bt_bdaddr_t *)device_address, isDirect);
1874 if (ret != BT_STATUS_SUCCESS) {
1875 BT_ERR("GATT client connect failed: %s", status2string(ret));
1876 return convert_to_oal_status(ret);
1878 return OAL_STATUS_SUCCESS;
1881 oal_status_t gattc_disconnect(int client_id, bt_address_t *device_address, int conn_id)
1884 int ret = OAL_STATUS_SUCCESS;
1887 OAL_CHECK_PARAMETER(device_address, return);
1888 API_TRACE("Client Disconnect: [%s]", bdt_bd2str(device_address, &bdstr));
1889 CHECK_OAL_GATT_ENABLED();
1890 CHECK_CLIENT_REGISTRATION(client_id);
1891 CHECK_CLIENT_CONNECTION(conn_id);
1893 /* Handle actual disconnection in callback */
1894 ret = gatt_api->client->disconnect(client_id, (bt_bdaddr_t *) device_address, conn_id);
1895 if (ret != BT_STATUS_SUCCESS) {
1896 BT_ERR("GATT client disconnect failed: %s", status2string(ret));
1897 return convert_to_oal_status(ret);
1899 return OAL_STATUS_SUCCESS;
1903 oal_status_t gattc_search_service(int conn_id, oal_uuid_t *service_uuid)
1905 int ret = OAL_STATUS_SUCCESS;
1906 char uuid_str[2*BT_UUID_STRING_MAX];
1909 uuid_to_stringname(service_uuid, uuid_str);
1910 API_TRACE("Client Service Search UUID: [%s]", uuid_str);
1912 API_TRACE("Client Service Search All");
1914 CHECK_OAL_GATT_ENABLED();
1915 CHECK_CLIENT_CONNECTION(conn_id);
1916 ret = gatt_api->client->search_service(conn_id, (bt_uuid_t *)service_uuid);
1918 if (ret != BT_STATUS_SUCCESS) {
1919 BT_ERR("GATT client service search failed: %s", status2string(ret));
1920 return convert_to_oal_status(ret);
1922 return OAL_STATUS_SUCCESS;
1925 oal_status_t gattc_get_characteristic(int conn_id, oal_gatt_srvc_id_t *srvc_id, oal_gatt_id_t *char_id)
1927 int ret = OAL_STATUS_SUCCESS;
1928 char uuid_str[2*BT_UUID_STRING_MAX];
1930 OAL_CHECK_PARAMETER(srvc_id, return);
1931 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str);
1932 API_TRACE("Client Get Characteristic [%s]", uuid_str);
1933 CHECK_OAL_GATT_ENABLED();
1934 CHECK_CLIENT_CONNECTION(conn_id);
1935 ret = gatt_api->client->get_characteristic(conn_id, (btgatt_srvc_id_t *)srvc_id,
1936 (btgatt_gatt_id_t *)char_id);
1937 if (ret != BT_STATUS_SUCCESS) {
1938 BT_ERR("GATT client get characteristic failed: %s", status2string(ret));
1939 return convert_to_oal_status(ret);
1941 return OAL_STATUS_SUCCESS;
1944 oal_status_t gattc_get_descriptor(int conn_id, oal_gatt_srvc_id_t *srvc_id,
1945 oal_gatt_id_t *char_id, oal_gatt_id_t *desc_id)
1947 int ret = OAL_STATUS_SUCCESS;
1948 char uuid_str1[2*BT_UUID_STRING_MAX];
1949 char uuid_str2[2*BT_UUID_STRING_MAX];
1951 OAL_CHECK_PARAMETER(srvc_id, return);
1952 OAL_CHECK_PARAMETER(char_id, return);
1953 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1954 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1955 API_TRACE("Client Get Descriptor, Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
1956 CHECK_OAL_GATT_ENABLED();
1957 CHECK_CLIENT_CONNECTION(conn_id);
1958 ret = gatt_api->client->get_descriptor(conn_id, (btgatt_srvc_id_t *)srvc_id,
1959 (btgatt_gatt_id_t *)char_id, (btgatt_gatt_id_t *)desc_id);
1960 if (ret != BT_STATUS_SUCCESS) {
1961 BT_ERR("GATT client get descriptor failed: %s", status2string(ret));
1962 return convert_to_oal_status(ret);
1964 return OAL_STATUS_SUCCESS;
1967 oal_status_t gattc_register_for_notification(int conn_id, bt_address_t * address,
1968 oal_gatt_srvc_id_t *srvc_id, oal_gatt_id_t *char_id) {
1969 int ret = OAL_STATUS_SUCCESS;
1970 char uuid_str1[2*BT_UUID_STRING_MAX];
1971 char uuid_str2[2*BT_UUID_STRING_MAX];
1974 OAL_CHECK_PARAMETER(address, return);
1975 OAL_CHECK_PARAMETER(srvc_id, return);
1976 OAL_CHECK_PARAMETER(char_id, return);
1977 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1978 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1979 API_TRACE("Client Register Notification: [%s], Service_uuid: [%s], Char_uuid: [%s]", bdt_bd2str(address, &bdstr), uuid_str1, uuid_str2);
1980 CHECK_OAL_GATT_ENABLED();
1981 CHECK_CLIENT_CONNECTION(conn_id);
1983 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);
1984 if (ret != BT_STATUS_SUCCESS) {
1985 BT_ERR("GATT client register notification failed: %s", status2string(ret));
1986 return convert_to_oal_status(ret);
1989 return OAL_STATUS_SUCCESS;
1992 oal_status_t gattc_deregister_for_notification(int conn_id, bt_address_t * address,
1993 oal_gatt_srvc_id_t *srvc_id, oal_gatt_id_t *char_id) {
1994 int ret = OAL_STATUS_SUCCESS;
1995 char uuid_str1[2*BT_UUID_STRING_MAX];
1996 char uuid_str2[2*BT_UUID_STRING_MAX];
1999 OAL_CHECK_PARAMETER(address, return);
2000 OAL_CHECK_PARAMETER(srvc_id, return);
2001 OAL_CHECK_PARAMETER(char_id, return);
2002 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2003 uuid_to_stringname(&(char_id->uuid), uuid_str2);
2004 API_TRACE("Client Deregister Notification: [%s], Service_uuid: [%s], Char_uuid: [%s]", bdt_bd2str(address, &bdstr), uuid_str1, uuid_str2);
2005 CHECK_OAL_GATT_ENABLED();
2006 CHECK_CLIENT_CONNECTION(conn_id);
2008 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);
2009 if (ret != BT_STATUS_SUCCESS) {
2010 BT_ERR("GATT client deregister notification failed: %s", status2string(ret));
2011 return convert_to_oal_status(ret);
2014 return OAL_STATUS_SUCCESS;
2018 oal_status_t gattc_read_characteristic(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2019 oal_gatt_id_t *char_id, oal_gatt_auth_req_t auth_req)
2021 int ret = OAL_STATUS_SUCCESS;
2022 char uuid_str1[2*BT_UUID_STRING_MAX];
2023 char uuid_str2[2*BT_UUID_STRING_MAX];
2025 OAL_CHECK_PARAMETER(srvc_id, return);
2026 OAL_CHECK_PARAMETER(char_id, return);
2027 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2028 uuid_to_stringname(&(char_id->uuid), uuid_str2);
2029 API_TRACE("Client Read Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
2030 CHECK_OAL_GATT_ENABLED();
2031 CHECK_CLIENT_CONNECTION(conn_id);
2033 ret = gatt_api->client->read_characteristic(conn_id, (btgatt_srvc_id_t *)srvc_id,
2034 (btgatt_gatt_id_t *)char_id, auth_req);
2035 if (ret != BT_STATUS_SUCCESS) {
2036 BT_ERR("GATT client read characteristic failed: %s", status2string(ret));
2037 return convert_to_oal_status(ret);
2040 return OAL_STATUS_SUCCESS;
2043 oal_status_t gattc_read_descriptor(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2044 oal_gatt_id_t *char_id, oal_gatt_id_t *desc_id, oal_gatt_auth_req_t auth_req)
2046 int ret = OAL_STATUS_SUCCESS;
2047 char uuid_str1[2*BT_UUID_STRING_MAX];
2048 char uuid_str2[2*BT_UUID_STRING_MAX];
2049 char uuid_str3[2*BT_UUID_STRING_MAX];
2051 OAL_CHECK_PARAMETER(srvc_id, return);
2052 OAL_CHECK_PARAMETER(char_id, return);
2053 OAL_CHECK_PARAMETER(desc_id, return);
2054 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2055 uuid_to_stringname(&(char_id->uuid), uuid_str2);
2056 uuid_to_stringname(&(desc_id->uuid), uuid_str3);
2057 API_TRACE("Client Read Descriptor: Service_uuid: [%s], Char_uuid: [%s], Desc_uuid: [%s]",
2058 uuid_str1, uuid_str2, uuid_str3);
2059 CHECK_OAL_GATT_ENABLED();
2060 CHECK_CLIENT_CONNECTION(conn_id);
2062 ret = gatt_api->client->read_descriptor(conn_id, (btgatt_srvc_id_t *)srvc_id,
2063 (btgatt_gatt_id_t *)char_id, (btgatt_gatt_id_t *)desc_id, auth_req);
2064 if (ret != BT_STATUS_SUCCESS) {
2065 BT_ERR("GATT client read descriptor failed: %s", status2string(ret));
2066 return convert_to_oal_status(ret);
2069 return OAL_STATUS_SUCCESS;
2072 oal_status_t gattc_acquire_notify(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2073 oal_gatt_id_t *char_id, int *fd, int *mtu)
2075 int ret = OAL_STATUS_SUCCESS;
2076 char uuid_str1[2*BT_UUID_STRING_MAX];
2077 char uuid_str2[2*BT_UUID_STRING_MAX];
2078 OAL_CHECK_PARAMETER(srvc_id, return);
2079 OAL_CHECK_PARAMETER(char_id, return);
2080 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2081 uuid_to_stringname(&(char_id->uuid), uuid_str2);
2082 API_TRACE("Client Write Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
2083 CHECK_OAL_GATT_ENABLED();
2084 CHECK_CLIENT_CONNECTION(conn_id);
2086 ret = gatt_api->client->acquire_notify(conn_id, (btgatt_srvc_id_t *)srvc_id,
2087 (btgatt_gatt_id_t *)char_id, fd, mtu);
2088 if (ret != BT_STATUS_SUCCESS) {
2089 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
2090 return convert_to_oal_status(ret);
2093 return OAL_STATUS_SUCCESS;
2096 oal_status_t gattc_acquire_write(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2097 oal_gatt_id_t *char_id, oal_gatt_auth_req_t auth_req , int *fd, int*mtu)
2099 int ret = OAL_STATUS_SUCCESS;
2100 char uuid_str1[2*BT_UUID_STRING_MAX];
2101 char uuid_str2[2*BT_UUID_STRING_MAX];
2102 OAL_CHECK_PARAMETER(srvc_id, return);
2103 OAL_CHECK_PARAMETER(char_id, return);
2104 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2105 uuid_to_stringname(&(char_id->uuid), uuid_str2);
2106 API_TRACE("Client Write Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
2107 CHECK_OAL_GATT_ENABLED();
2108 CHECK_CLIENT_CONNECTION(conn_id);
2110 ret = gatt_api->client->acquire_write(conn_id, (btgatt_srvc_id_t *)srvc_id,
2111 (btgatt_gatt_id_t *)char_id, auth_req, fd, mtu);
2112 if (ret != BT_STATUS_SUCCESS) {
2113 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
2114 return convert_to_oal_status(ret);
2117 return OAL_STATUS_SUCCESS;
2120 oal_status_t gattc_write_characteristic(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2121 oal_gatt_id_t *char_id, oal_gatt_write_type_t write_type,
2122 int len, oal_gatt_auth_req_t auth_req, char* data)
2124 int ret = OAL_STATUS_SUCCESS;
2125 char uuid_str1[2*BT_UUID_STRING_MAX];
2126 char uuid_str2[2*BT_UUID_STRING_MAX];
2127 OAL_CHECK_PARAMETER(srvc_id, return);
2128 OAL_CHECK_PARAMETER(char_id, return);
2129 OAL_CHECK_PARAMETER(data, return);
2130 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2131 uuid_to_stringname(&(char_id->uuid), uuid_str2);
2132 API_TRACE("Client Write Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
2133 CHECK_OAL_GATT_ENABLED();
2134 CHECK_CLIENT_CONNECTION(conn_id);
2136 ret = gatt_api->client->write_characteristic(conn_id, (btgatt_srvc_id_t *)srvc_id,
2137 (btgatt_gatt_id_t *)char_id, write_type, len, auth_req, data);
2138 if (ret != BT_STATUS_SUCCESS) {
2139 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
2140 return convert_to_oal_status(ret);
2143 return OAL_STATUS_SUCCESS;
2146 oal_status_t gattc_write_descriptor(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2147 oal_gatt_id_t *char_id, oal_gatt_id_t *desc_id,
2148 oal_gatt_write_type_t write_type, int len, oal_gatt_auth_req_t auth_req, char* data)
2150 int ret = OAL_STATUS_SUCCESS;
2151 char uuid_str1[2*BT_UUID_STRING_MAX];
2152 char uuid_str2[2*BT_UUID_STRING_MAX];
2153 char uuid_str3[2*BT_UUID_STRING_MAX];
2154 OAL_CHECK_PARAMETER(srvc_id, return);
2155 OAL_CHECK_PARAMETER(char_id, return);
2156 OAL_CHECK_PARAMETER(desc_id, return);
2157 OAL_CHECK_PARAMETER(data, return);
2158 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2159 uuid_to_stringname(&(char_id->uuid), uuid_str2);
2160 uuid_to_stringname(&(desc_id->uuid), uuid_str3);
2161 API_TRACE("Client Write Descriptor: Service_uuid: [%s], Char_uuid: [%s], Desc_uuid: [%s]",
2162 uuid_str1, uuid_str2, uuid_str3);
2163 CHECK_OAL_GATT_ENABLED();
2164 CHECK_CLIENT_CONNECTION(conn_id);
2166 ret = gatt_api->client->write_descriptor(conn_id, (btgatt_srvc_id_t *)srvc_id,
2167 (btgatt_gatt_id_t *)char_id, (btgatt_gatt_id_t *)desc_id, write_type, len, auth_req, data);
2168 if (ret != BT_STATUS_SUCCESS) {
2169 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
2170 return convert_to_oal_status(ret);
2173 return OAL_STATUS_SUCCESS;
2176 oal_status_t gattc_conn_param_update(bt_address_t * address, int min, int max, int latency, int timeout)
2181 OAL_CHECK_PARAMETER(address, return);
2182 API_TRACE("Conn Param Update: [%s]", bdt_bd2str(address, &bdstr));
2183 CHECK_OAL_GATT_ENABLED();
2185 BT_INFO("[%s] min[%d] max[%d] latency[%d] timeout[%d]", bdt_bd2str(address, &bdstr), min, max, latency, timeout);
2186 ret = gatt_api->client->conn_parameter_update((bt_bdaddr_t *)address, min, max, latency, timeout);
2187 if (ret != BT_STATUS_SUCCESS) {
2188 BT_ERR("error: %s", status2string(ret));
2189 return convert_to_oal_status(ret);
2191 return OAL_STATUS_SUCCESS;
2194 oal_status_t gattc_unregister_scan_filter(int slot_id)
2200 CHECK_OAL_GATT_ENABLED();
2202 BT_INFO("Remove Scan filter Slot ID is: [%d]", slot_id);
2204 ret = gatt_api->client->scan_filter_clear(client_if, slot_id);
2205 if (ret != BT_STATUS_SUCCESS) {
2206 BT_ERR("error: %s", status2string(ret));
2207 return convert_to_oal_status(ret);
2209 return OAL_STATUS_SUCCESS;
2212 oal_status_t gattc_enable_scan_filter(int client_if)
2217 CHECK_OAL_GATT_ENABLED();
2219 BT_INFO("Enable Scan filter. Client If: %d", client_if);
2221 ret = gatt_api->client->scan_filter_enable(client_if, TRUE);
2222 if (ret != BT_STATUS_SUCCESS) {
2223 BT_ERR("error: %s", status2string(ret));
2224 return convert_to_oal_status(ret);
2226 return OAL_STATUS_SUCCESS;
2229 oal_status_t gattc_disable_scan_filter(int client_if)
2234 CHECK_OAL_GATT_ENABLED();
2236 BT_INFO("Disable Scan filter. Client If: %d", client_if);
2238 ret = gatt_api->client->scan_filter_enable(client_if, FALSE);
2239 if (ret != BT_STATUS_SUCCESS) {
2240 BT_ERR("error: %s", status2string(ret));
2241 return convert_to_oal_status(ret);
2243 return OAL_STATUS_SUCCESS;
2246 oal_status_t gattc_register_scan_filter(oal_ble_scan_filter_t* filter_data)
2249 int client_info = 0;
2252 int company_id_mask = 0;
2253 int address_type = 0;
2254 int feature_selection = 0;
2256 oal_ble_scan_filter_param_setup_t scan_filter_setup = {.list_logic_type = 0,
2257 .filt_logic_type = 1,
2258 .rssi_high_thres = -127,
2259 .rssi_low_thres = -127,
2263 .found_timeout_cnt = 0
2266 OAL_CHECK_PARAMETER(filter_data, return);
2268 CHECK_OAL_GATT_ENABLED();
2270 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS){
2272 BT_INFO("OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS is being added");
2273 BT_INFO("BT remote Device address is %s", bdt_bd2str(filter_data->device_address, &bdstr));
2274 ret = gatt_api->client->scan_filter_add_remove(client_info,
2276 OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS,
2277 filter_data->slot_id,
2282 (bt_bdaddr_t*)filter_data->device_address,
2289 if (ret != BT_STATUS_SUCCESS){
2290 BT_ERR("error: %s", status2string(ret));
2291 BT_INFO("unregistering already set filter features.");
2292 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2293 return convert_to_oal_status(ret);
2296 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS;
2298 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_NAME){
2299 ret = gatt_api->client->scan_filter_add_remove(client_info,
2301 OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_NAME,
2302 filter_data->slot_id,
2310 filter_data->device_name, // device_name as p_data in HAL
2314 if (ret != BT_STATUS_SUCCESS){
2315 BT_ERR("error: %s", status2string(ret));
2316 BT_INFO("unregistering already set filter features.");
2317 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2318 return convert_to_oal_status(ret);
2321 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_NAME;
2323 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_UUID){
2324 BT_INFO("OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_UUID is being added");
2325 char uuid_str1[2*BT_UUID_STRING_MAX];
2326 char uuid_str2[2*BT_UUID_STRING_MAX];
2328 uuid_to_stringname((service_uuid_t*)filter_data->service_uuid, uuid_str1);
2329 uuid_to_stringname((service_uuid_t*)filter_data->service_uuid_mask, uuid_str2);
2331 BT_INFO("Service UUID is [%s] and Service UUID mask is [%s]", uuid_str1, uuid_str2);
2332 ret = gatt_api->client->scan_filter_add_remove(client_info,
2334 OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_UUID,
2335 filter_data->slot_id,
2338 (bt_uuid_t*)filter_data->service_uuid,
2339 (bt_uuid_t*)filter_data->service_uuid_mask,
2342 filter_data->service_uuid_len, // service_uuid_len as data_len in HAL
2344 filter_data->service_uuid_mask_len,
2347 if (ret != BT_STATUS_SUCCESS){
2348 BT_ERR("error: %s", status2string(ret));
2349 BT_INFO("unregistering already set filter features.");
2350 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2351 return convert_to_oal_status(ret);
2354 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_UUID;
2356 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID){
2357 BT_INFO("OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID is being added");
2358 char uuid_str1[2*BT_UUID_STRING_MAX];
2359 char uuid_str2[2*BT_UUID_STRING_MAX];
2360 uuid_to_stringname((service_uuid_t*)filter_data->service_solicitation_uuid, uuid_str1);
2361 uuid_to_stringname((service_uuid_t*)filter_data->service_solicitation_uuid_mask, uuid_str2);
2362 BT_INFO("Service Solicitation UUID is [%s] and Service Solicitation UUID mask is [%s]", uuid_str1, uuid_str2);
2363 ret = gatt_api->client->scan_filter_add_remove(client_info,
2365 OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID,
2366 filter_data->slot_id,
2369 (bt_uuid_t*)filter_data->service_solicitation_uuid,
2370 (bt_uuid_t*)filter_data->service_solicitation_uuid_mask,
2373 filter_data->service_solicitation_uuid_len, // service_solicitation_uuid_len as data_len in HAL
2375 filter_data->service_solicitation_uuid_mask_len,
2378 if (ret != BT_STATUS_SUCCESS){
2379 BT_ERR("error: %s", status2string(ret));
2380 BT_INFO("unregistering already set filter features.");
2381 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2382 return convert_to_oal_status(ret);
2385 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID;
2387 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_DATA){
2388 ret = gatt_api->client->scan_filter_add_remove(client_info,
2390 OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_DATA,
2391 filter_data->slot_id,
2398 filter_data->service_data_len, //service_data_len as data_len in HAL
2399 (char*)filter_data->service_data,
2400 filter_data->service_data_mask_len,
2401 (char*)filter_data->service_data_mask
2403 if (ret != BT_STATUS_SUCCESS){
2404 BT_ERR("error: %s", status2string(ret));
2405 BT_INFO("unregistering already set filter features.");
2406 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2407 return convert_to_oal_status(ret);
2410 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_DATA;
2412 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA){
2413 ret = gatt_api->client->scan_filter_add_remove(client_info,
2415 OAL_BLE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA,
2416 filter_data->slot_id,
2417 filter_data->manufacturer_id,
2423 filter_data->manufacturer_data_len, //manufacturer_data_len as data_len in HAL
2424 (char*)filter_data->manufacturer_data,
2425 filter_data->manufacturer_data_mask_len,
2426 (char*)filter_data->manufacturer_data_mask
2428 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA;
2429 if (ret != BT_STATUS_SUCCESS){
2430 BT_ERR("error: %s", status2string(ret));
2431 BT_INFO("unregistering already set filter features.");
2432 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2433 return convert_to_oal_status(ret);
2436 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA;
2439 BT_DBG("Filter selection 0x%.2x", feature_selection);
2441 ret = gatt_api->client->scan_filter_param_setup(client_info, action, filter_data->slot_id, feature_selection,
2442 scan_filter_setup.list_logic_type, scan_filter_setup.filt_logic_type,
2443 scan_filter_setup.rssi_high_thres, scan_filter_setup.rssi_low_thres,
2444 scan_filter_setup.dely_mode, scan_filter_setup.found_timeout,
2445 scan_filter_setup.lost_timeout, scan_filter_setup.found_timeout_cnt);
2446 if (ret != BT_STATUS_SUCCESS){
2447 BT_ERR("error: %s", status2string(ret));
2448 return convert_to_oal_status(ret);
2450 return OAL_STATUS_SUCCESS;
2453 oal_status_t gattc_configure_mtu(int conn_id, int mtu)
2457 API_TRACE("Configure MTU Size: [%d]", mtu);
2458 CHECK_OAL_GATT_ENABLED();
2459 CHECK_CLIENT_CONNECTION(conn_id);
2461 /* To prevent crash in case other libraries not support this api */
2462 if (gatt_api->client->configure_mtu == NULL) {
2463 BT_WARN("configure_mtu is NULL");
2464 return OAL_STATUS_NOT_SUPPORT;
2467 ret = gatt_api->client->configure_mtu(conn_id, mtu);
2468 if (ret != BT_STATUS_SUCCESS) {
2469 BT_ERR("Gatt client configure_mtu error: %s", status2string(ret));
2470 return convert_to_oal_status(ret);
2472 return OAL_STATUS_SUCCESS;
2475 oal_status_t gattc_add_connection_info(bt_address_t *device_address, int conn_id, int server_inst_id)
2477 int ret = OAL_STATUS_SUCCESS;
2480 OAL_CHECK_PARAMETER(device_address, return);
2481 API_TRACE("Add connection info: [%s]", bdt_bd2str(device_address, &bdstr));
2482 CHECK_OAL_GATT_ENABLED();
2483 CHECK_SERVER_INSTANCE(server_inst_id);
2484 CHECK_SERVER_REGISTRATION(server_inst_id);
2486 ret = gatt_api->client->add_connection_info((bt_bdaddr_t *)device_address, conn_id, server_inst_id);
2487 if (ret != BT_STATUS_SUCCESS) {
2488 BT_ERR("GATT connection info add failed: %s", status2string(ret));
2489 return convert_to_oal_status(ret);
2491 return OAL_STATUS_SUCCESS;