2 * Open Adaptation Layer (OAL)
4 * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
20 #include <bluetooth.h>
22 #include "oal-event.h"
23 #include "oal-internal.h"
24 #include "oal-manager.h"
25 #include "oal-adapter-mgr.h"
26 #include "oal-utils.h"
28 #include "oal-common.h"
30 #define DEV_APPEARANCE 0x0000
31 #define DEFAULT_GATT_CLIENT_UUID "0000A00A-1111-1111-0123-456789ABCDEF"
32 #define MAX_PROP_LEN 95 /* SVACE WGID: 324403*/
33 #define MAX_PERM_LEN 80 //Fix for svace wgid:324402
35 #define CHECK_OAL_GATT_ENABLED() \
37 if (gatt_api == NULL) { \
38 BT_ERR("GATT Not Enabled"); \
39 return OAL_STATUS_NOT_READY; \
43 #define CHECK_SERVER_INSTANCE(instance_id) \
45 if (instance_id < 1) { \
46 BT_ERR("Invalid Instance"); \
47 return OAL_STATUS_INVALID_PARAM; \
51 #define CHECK_CORRECT_SERVER_ID(server_if, server_inst) \
53 gatt_server_t *info; \
54 info = __gatts_find_server_instance(server_if); \
56 server_inst = server_if; \
61 #define PRINT_ADV_DATA(length, manuf_data, data_type) \
63 char * buf_str = g_malloc0(3*length + 1);\
64 if (NULL != buf_str) { \
65 convert_hex_2_str(manuf_data, length, buf_str);\
66 if (TRUE == data_type) \
67 BT_INFO("Scan Response Data:%s\n", buf_str);\
69 BT_INFO("Advertising Data:%s\n", buf_str);\
74 #define CHECK_CLIENT_REGISTRATION(client_id) \
76 if (client_id <= 0) { \
77 BT_DBG("No client registered yet...");\
78 return OAL_STATUS_INTERNAL_ERROR;\
82 #define CHECK_CLIENT_CONNECTION(conn_id) \
85 BT_ERR("No active connection");\
86 return OAL_STATUS_INTERNAL_ERROR;\
93 GATT_INS_DATA_SETTING,
103 gboolean cur_adv_state;
106 const char *oal_device_type[] = {
117 } oal_pending_gattc_req_e;
119 static const btgatt_interface_t * gatt_api;
120 static GSList *gatt_servers = NULL; /* List of gatt_server_t */
122 /* Forward declarations of GATT Server callbacks */
123 static void cb_gatts_register_app(int status, int server_if, bt_uuid_t *uuid);
124 static void cb_gatts_multi_adv_enable(int server_if, int status);
125 static void cb_gatts_multi_adv_set_inst_data(int server_if, int status);
126 static void cb_gatts_multi_adv_disable(int server_if, int status);
127 static void cb_gatts_multi_adv_update(int server_if, int status);
129 static void cb_gatts_listen(int status, int server_if);
132 static void cb_gatts_service_added(int status, int server_if, btgatt_srvc_id_t *srvc_id, int srvc_handle);
133 static void cb_gatts_included_service_added(int status, int server_if, int srvc_handle, int incl_srvc_handle);
134 static void cb_gatts_characteristic_added(int status, int server_if, bt_uuid_t *char_id, int srvc_handle, int char_handle);
135 static void cb_gatts_descriptor_added(int status, int server_if, bt_uuid_t *descr_id, int srvc_handle, int descr_handle);
137 static void cb_gatts_service_started(int status, int server_if, int srvc_handle);
138 static void cb_gatts_service_stopped(int status, int server_if, int srvc_handle);
139 static void cb_gatts_service_deleted(int status, int server_if, int srvc_handle);
141 static void cb_gatts_request_read(int conn_id, int trans_id, bt_bdaddr_t *bda, int attr_handle, int offset, bool is_long);
142 static void cb_gatts_request_write(int conn_id, int trans_id, bt_bdaddr_t *bda, int attr_handle, int offset, int length,
143 bool need_rsp, bool is_prep, uint8_t* value);
144 static void cb_gatts_response_confirmation(int status, int handle);
146 static void cb_gatts_acquire_write(int fd, int conn_id, int trans_id, int attr_handle, bt_bdaddr_t*);
147 static void cb_gatts_acquire_notify(int fd, int conn_id, int trans_id, int attr_handle);
149 static void cb_indication_confirmation(int conn_id, int trans_id, int attr_handle, bt_bdaddr_t *bda);
150 static void cb_gatts_connection(int conn_id, int server_if, int connected, bt_bdaddr_t *bda);
152 static void cb_gatts_mtu_changed(int conn_id, int mtu);
155 static void cb_notifcation_changed(int conn_id, int trans_id, int attr_handle, bool notify, bt_bdaddr_t *bda);
158 /************************************HAL Interface *************************************/
160 /*TODO GATT Server callbacks will be implemented in subsequent patches */
161 static const btgatt_server_callbacks_t btgatt_server_callbacks = {
162 .register_server_cb = cb_gatts_register_app,
163 .connection_cb = cb_gatts_connection,
164 .service_added_cb = cb_gatts_service_added,
165 .included_service_added_cb = cb_gatts_included_service_added,
166 .characteristic_added_cb = cb_gatts_characteristic_added,
167 .descriptor_added_cb = cb_gatts_descriptor_added,
168 .service_started_cb = cb_gatts_service_started,
169 .service_stopped_cb = cb_gatts_service_stopped,
170 .service_deleted_cb = cb_gatts_service_deleted,
171 .indication_confirmation_cb = cb_indication_confirmation,
172 .request_read_cb = cb_gatts_request_read,
173 .request_write_cb = cb_gatts_request_write,
174 .request_exec_write_cb = NULL,
175 .response_confirmation_cb = cb_gatts_response_confirmation,
176 .listen_cb = cb_gatts_listen,
177 .multi_adv_enable_cb = cb_gatts_multi_adv_enable,
178 .multi_adv_update_cb = cb_gatts_multi_adv_update,
179 .multi_adv_data_cb = cb_gatts_multi_adv_set_inst_data,
180 .multi_adv_disable_cb = cb_gatts_multi_adv_disable,
181 .mtu_changed_cb = cb_gatts_mtu_changed,
183 .notif_enabled_cb = cb_notifcation_changed,
185 .request_acquire_write_cb = cb_gatts_acquire_write,
186 .request_acquire_notify_cb = cb_gatts_acquire_notify
189 /* Forward declaration for GATT client callbacks */
190 static void cb_gattc_register_app(int status, int clientIf, bt_uuid_t *app_uuid);
191 static void cb_gattc_scan_result(bt_bdaddr_t* bdaddress, uint8_t addr_type, int rssi,
192 uint8_t *adv_data, int adv_data_len, uint8_t *scan_rsp_data, int scan_rsp_data_len);
193 static void cb_gattc_connection(int conn_id, int status, int client_if, bt_bdaddr_t* bda);
194 static void cb_gattc_disconnect(int conn_id, int status, int client_if, bt_bdaddr_t* bda);
195 static void cb_gattc_search_result(int conn_id, btgatt_srvc_id_t *srvc_id);
196 static void cb_gattc_search_complete(int conn_id, int status);
197 static void cb_gattc_get_characteristics(int conn_id, int status, btgatt_srvc_id_t *srvc_id,
198 btgatt_gatt_id_t *char_id, int char_prop);
199 static void cb_gattc_get_descriptor(int conn_id, int status, btgatt_srvc_id_t *srvc_id,
200 btgatt_gatt_id_t *char_id, btgatt_gatt_id_t *descr_id);
201 static void cb_gattc_read_characteristic(int conn_id, int status, btgatt_read_params_t *p_data);
202 static void cb_gattc_read_descriptor(int conn_id, int status, btgatt_read_params_t *p_data);
203 static void cb_gattc_write_characteristic(int conn_id, int status, btgatt_write_params_t *p_data);
204 static void cb_gattc_write_descriptor(int conn_id, int status, btgatt_write_params_t *p_data);
205 static void cb_gattc_register_for_notification(int conn_id, int registered, int status,
206 btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id);
207 static void cb_gattc_notify(int conn_id, btgatt_notify_params_t *p_data);
208 static void cb_gattc_service_changed(bt_bdaddr_t *bd_addr, int change_type, uint8_t *uuid, int conn_id, int inst_id);
209 static void cb_gattc_configure_mtu_cmpl(int conn_id, int status, int mtu);
211 /*TODO GATT CLient callbacks will be implemented in subsequent patches */
212 static const btgatt_client_callbacks_t btgatt_client_callbacks = {
213 .register_client_cb = cb_gattc_register_app,
214 .scan_result_cb = cb_gattc_scan_result,
215 .open_cb = cb_gattc_connection,
216 .close_cb = cb_gattc_disconnect,
217 .search_complete_cb = cb_gattc_search_complete,
218 .search_result_cb = cb_gattc_search_result,
219 .get_characteristic_cb = cb_gattc_get_characteristics,
220 .get_descriptor_cb = cb_gattc_get_descriptor,
221 .get_included_service_cb = NULL,
222 .register_for_notification_cb = cb_gattc_register_for_notification,
223 .notify_cb = cb_gattc_notify,
224 .service_changed_cb = cb_gattc_service_changed,
225 .read_characteristic_cb = cb_gattc_read_characteristic,
226 .write_characteristic_cb = cb_gattc_write_characteristic,
227 .read_descriptor_cb = cb_gattc_read_descriptor,
228 .write_descriptor_cb = cb_gattc_write_descriptor,
229 .execute_write_cb = NULL,
230 .read_remote_rssi_cb = NULL,
231 .configure_mtu_cb = cb_gattc_configure_mtu_cmpl,
232 #ifdef PLATFORM_ANDROID_HAL
233 .scan_filter_cfg_cb = NULL,
234 .scan_filter_param_cb = NULL,
235 .scan_filter_status_cb = NULL,
236 .congestion_cb = NULL,
237 .batchscan_cfg_storage_cb = NULL,
238 .batchscan_enb_disable_cb = NULL,
239 .batchscan_reports_cb = NULL,
240 .batchscan_threshold_cb = NULL,
241 .track_adv_event_cb = NULL,
245 static btgatt_callbacks_t btgatt_callbacks = {
246 sizeof(btgatt_callbacks_t),
247 &btgatt_client_callbacks,
248 &btgatt_server_callbacks
251 /*******************************GATT Initialisation - Deinitialisation********************************/
252 oal_status_t gatt_enable(void)
254 const bt_interface_t * blued_api;
257 /* Get stack interface */
258 blued_api = (const bt_interface_t *) adapter_get_stack_interface();
260 if (blued_api == NULL) {
261 BT_ERR("Stack is not initialized");
262 return OAL_STATUS_NOT_READY;
266 BT_WARN("GATT Interface is already initialized...");
267 return OAL_STATUS_ALREADY_DONE;
270 gatt_api = (const btgatt_interface_t *)blued_api->get_profile_interface(BT_PROFILE_GATT_ID);
271 if (gatt_api == NULL) {
272 BT_ERR("GATT interface failed");
273 return OAL_STATUS_INTERNAL_ERROR;
276 if ((ret = gatt_api->init(&btgatt_callbacks)) != BT_STATUS_SUCCESS) {
277 BT_ERR("Error: Unable to initialise GATT :%s", status2string(ret));
280 return convert_to_oal_status(ret);
283 BT_INFO("GATT successfully initialized");
284 return OAL_STATUS_SUCCESS;
287 oal_status_t gatt_disable(void)
294 g_slist_free_full(gatt_servers, g_free);
295 return OAL_STATUS_SUCCESS;
298 static gatt_server_t *__gatts_find_server_instance(int instance_id)
303 for (l = gatt_servers; l; l = g_slist_next(l)) {
307 if (info->instance_id == instance_id)
314 /************************************GATT Server Functions*************************************/
316 oal_status_t gatts_register(oal_uuid_t* server_uuid)
318 char str[2*BT_UUID_STRING_MAX];
319 int ret = OAL_STATUS_SUCCESS;
322 CHECK_OAL_GATT_ENABLED();
323 uuid_to_stringname((service_uuid_t*)server_uuid->uuid, str);
324 API_TRACE("Register the server instance: UUID: [%s]", str);
326 for (l = gatt_servers; l; l = g_slist_next(l)) {
327 gatt_server_t *info = l->data;
330 if (memcmp(server_uuid->uuid, info->uuid.uuid, sizeof(oal_uuid_t)) == 0) {
331 if (info->instance_id != -1) {
332 BT_ERR("This is resevered UUID for easy set up application \
333 instance_id = %d", info->instance_id);
334 return OAL_STATUS_ALREADY_DONE;
339 ret = gatt_api->server->register_server((bt_uuid_t*)server_uuid);
341 if (ret != BT_STATUS_SUCCESS) {
342 BT_ERR("GATT server registration with UUID:%s failed: %s", str, status2string(ret));
343 return convert_to_oal_status(ret);
345 return OAL_STATUS_SUCCESS;
348 oal_status_t gatts_unregister(int instance_id)
350 int ret = OAL_STATUS_SUCCESS;
352 API_TRACE("Unregister the registered server, instance_id: %d", instance_id);
354 CHECK_OAL_GATT_ENABLED();
355 CHECK_SERVER_INSTANCE(instance_id);
357 info = __gatts_find_server_instance(instance_id);
359 return OAL_STATUS_INTERNAL_ERROR;
361 ret = gatt_api->server->unregister_server(instance_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 info->instance_id = -1;
368 info->state = GATT_INS_DISABLED;
369 info->cur_adv_state = FALSE;
370 if (instance_id != 1 && instance_id != 2) {
371 gatt_servers = g_slist_remove(gatt_servers, info);
372 BT_DBG("GATT Server Removed. count of gatt_servers: %d",
373 g_slist_length(gatt_servers));
376 return OAL_STATUS_SUCCESS;
379 /* Legacy Advertisement */
380 oal_status_t gatts_start_listen(int instance_id, gboolean enable)
382 int ret = OAL_STATUS_SUCCESS;
384 API_TRACE("instance_id: %d enable: %d", instance_id, enable);
386 CHECK_OAL_GATT_ENABLED();
387 CHECK_SERVER_INSTANCE(instance_id);
388 info = __gatts_find_server_instance(instance_id);
390 return OAL_STATUS_INTERNAL_ERROR;
392 ret = gatt_api->server->listen(instance_id, enable);
393 if (ret != BT_STATUS_SUCCESS) {
394 BT_ERR("Error:Advertising %s on instance_id: %d failed \
395 with status: %s, cur_adv_state: %d",
396 enable ? "Start" : "Stop", instance_id,
397 status2string(ret), info->cur_adv_state);
398 return convert_to_oal_status(ret);
401 return OAL_STATUS_SUCCESS;
404 oal_status_t gatts_stop_advertising(int instance_id)
406 int ret = OAL_STATUS_SUCCESS;
407 API_TRACE("Stop advertising");
409 CHECK_OAL_GATT_ENABLED();
410 CHECK_SERVER_INSTANCE(instance_id);
411 if (__gatts_find_server_instance(instance_id) == NULL)
412 return OAL_STATUS_INTERNAL_ERROR;
414 ret = gatt_api->server->listen(instance_id, FALSE);
415 if (ret != BT_STATUS_SUCCESS) {
416 BT_ERR("Stop Advertising failed for server Instance:%d With Status: %s",
417 instance_id, status2string(ret));
418 return convert_to_oal_status(ret);
420 return OAL_STATUS_SUCCESS;
424 /* Below API's need to be removed as they are hardcoded API's.
425 If at all, product specific API's are required to set specific data, then new naming should
426 be adopted for these API's */
427 oal_status_t gatts_set_advertising_params(int instance_id, float min_intv, float max_intv)
429 int ret = OAL_STATUS_SUCCESS;
431 API_TRACE("Set advertising params min_int:%f, max_int:%f", min_intv, max_intv);
432 CHECK_OAL_GATT_ENABLED();
433 CHECK_SERVER_INSTANCE(instance_id);
434 CHECK_SERVER_REGISTRATION(instance_id);
436 /* Historically used for Legacy Advertising */
437 return OAL_STATUS_NOT_SUPPORT;
440 oal_status_t gatts_set_advertising_data(int instance_id, uint16_t manufac_len, char* manufac_data)
442 int ret = OAL_STATUS_SUCCESS;
444 API_TRACE("Set advertising data");
445 CHECK_OAL_GATT_ENABLED();
446 CHECK_SERVER_INSTANCE(instance_id);
447 CHECK_SERVER_REGISTRATION(instance_id);
449 /* Historically used for Legacy Advertising */
450 return OAL_STATUS_NOT_SUPPORT;
453 oal_status_t gatts_set_scan_response(int instance_id, uint16_t manufac_len, char* manufac_data)
455 int ret = OAL_STATUS_SUCCESS;
457 API_TRACE("Set Scan Response data");
458 CHECK_OAL_GATT_ENABLED();
459 CHECK_SERVER_INSTANCE(instance_id);
460 CHECK_SERVER_REGISTRATION(instance_id);
462 /* Historically used for Legacy Advertising */
463 return OAL_STATUS_NOT_SUPPORT;
467 oal_status_t gatts_multi_adv_enable(int instance_id)
469 int ret = OAL_STATUS_SUCCESS;
471 API_TRACE("Start Multi advertising, instance_id: %d", instance_id);
473 CHECK_OAL_GATT_ENABLED();
474 CHECK_SERVER_INSTANCE(instance_id);
475 info = __gatts_find_server_instance(instance_id);
477 return OAL_STATUS_INTERNAL_ERROR;
479 if (gatt_api->server->multi_adv_enable == NULL)
480 return OAL_STATUS_NOT_SUPPORT;
482 /* Platform Requirement: Advertising to be enabled even when no data is set: i.e Empty Advertisement
483 if (gatt_servers[instance_id - 1].state != GATT_INS_DATA_SET){
484 BT_ERR("Not allowed, state: %d, instance_id: %d",
485 gatt_servers[instance_id - 1].state, instance_id);
486 return OAL_STATUS_BUSY;
490 if (info->state == GATT_INS_ENABLED) {
491 BT_ERR("Not allowed, state: %d, instance_id: %d", info->state, instance_id);
492 return OAL_STATUS_ALREADY_DONE;
493 } else if (info->state == GATT_INS_ENABLING) {
494 BT_ERR("Not allowed, state: %d, instance_id: %d", info->state, instance_id);
495 return OAL_STATUS_BUSY;
497 info->state = GATT_INS_ENABLING;
499 ret = gatt_api->server->multi_adv_enable(instance_id);
500 if (ret != BT_STATUS_SUCCESS) {
501 BT_ERR("GATT enable multi advertising failed for server Instance: %d With Status: %s",
502 instance_id, status2string(ret));
503 // gatt_servers[instance_id - 1].state = GATT_INS_DATA_SET;
504 return convert_to_oal_status(ret);
506 return OAL_STATUS_SUCCESS;
509 oal_status_t gatts_multi_adv_disable(int instance_id)
511 int ret = OAL_STATUS_SUCCESS;
513 API_TRACE("Stop Multi advertising, instance_id: %d", instance_id);
515 CHECK_OAL_GATT_ENABLED();
516 CHECK_SERVER_INSTANCE(instance_id);
517 info = __gatts_find_server_instance(instance_id);
519 return OAL_STATUS_INTERNAL_ERROR;
521 if (gatt_api->server->multi_adv_disable == NULL)
522 return OAL_STATUS_NOT_SUPPORT;
524 if (info->state != GATT_INS_ENABLED) {
525 BT_ERR("Not Allowed, state: %d, instance_id: %d", info->state, instance_id);
526 return OAL_STATUS_BUSY;
529 ret = gatt_api->server->multi_adv_disable(instance_id);
530 if (ret != BT_STATUS_SUCCESS) {
531 BT_ERR("GATT disable multi advertising failed for server Instance: %d With Status: %s",
532 instance_id, status2string(ret));
533 return convert_to_oal_status(ret);
535 return OAL_STATUS_SUCCESS;
538 oal_status_t gatts_set_filter_policy(int filter_policy)
540 int ret = OAL_STATUS_SUCCESS;
542 CHECK_OAL_GATT_ENABLED();
544 /* send the filter_policy value to the HAL Layer */
545 ret = gatt_api->server->set_filter_policy(filter_policy);
547 if (ret != BT_STATUS_SUCCESS) {
548 BT_ERR("set_filter_policy failed: %d",ret);
549 return convert_to_oal_status(ret);
552 return OAL_STATUS_SUCCESS;
555 oal_status_t gatts_multi_adv_update(int instance_id,
556 int min_intv, int max_intv,
557 int adv_type, int chnl_map,
558 int tx_power, int timeout_s)
560 int ret = OAL_STATUS_SUCCESS;
561 API_TRACE("Multi advertising Update");
563 CHECK_OAL_GATT_ENABLED();
564 CHECK_SERVER_INSTANCE(instance_id);
565 if (__gatts_find_server_instance(instance_id) == NULL)
566 return OAL_STATUS_INTERNAL_ERROR;
568 if (gatt_api->server->multi_adv_update == NULL)
569 return OAL_STATUS_NOT_SUPPORT;
571 ret = gatt_api->server->multi_adv_update(instance_id,
574 tx_power, timeout_s);
575 if (ret != BT_STATUS_SUCCESS) {
576 BT_ERR("GATT update multi advertising failed for server Instance: %d With Status: %s",
577 instance_id, status2string(ret));
578 return convert_to_oal_status(ret);
580 return OAL_STATUS_SUCCESS;
583 oal_status_t gatts_multi_adv_set_inst_data(int instance_id,
584 oal_ble_multi_adv_param_setup_t *adv_param_setup)
586 int ret = OAL_STATUS_SUCCESS;
587 btgatt_adv_param_setup_t adv_setup;
589 API_TRACE("Set Multi advertising data, instance_id: %d", instance_id);
591 CHECK_OAL_GATT_ENABLED();
592 CHECK_SERVER_INSTANCE(instance_id);
593 info = __gatts_find_server_instance(instance_id);
595 return OAL_STATUS_INTERNAL_ERROR;
597 if (gatt_api->server->multi_adv_set_inst_data == NULL)
598 return OAL_STATUS_NOT_SUPPORT;
600 if (info->state >= GATT_INS_DATA_SETTING && info->state != GATT_INS_DATA_SET) {
601 BT_ERR("Not Allowed, state: %d, instance_id: %d", info->state, instance_id);
602 return OAL_STATUS_BUSY;
604 info->state = GATT_INS_DATA_SETTING;
606 adv_setup.set_scan_rsp = adv_param_setup->set_scan_rsp;
607 adv_setup.include_name = adv_param_setup->include_name;
608 adv_setup.include_txpower = adv_param_setup->include_txpower;
609 adv_setup.appearance = adv_param_setup->appearance;
610 adv_setup.manufacturer_data = adv_param_setup->manufacturer_data;
611 adv_setup.manufacturer_data_len = adv_param_setup->manufacturer_data_len;
612 adv_setup.service_data = adv_param_setup->service_data;
613 adv_setup.service_data_len = adv_param_setup->service_data_len;
614 adv_setup.service_uuid = adv_param_setup->service_uuid;
615 adv_setup.service_uuid_len = adv_param_setup->service_uuid_len;
616 /* Solicit UUID handler: Start */
617 adv_setup.solicit_uuid = adv_param_setup->solicit_uuid;
618 adv_setup.solicit_uuid_len = adv_param_setup->solicit_uuid_len;
619 /* Solicit UUID handler: End */
620 adv_setup.min_interval = adv_param_setup->min_interval;
621 adv_setup.max_interval = adv_param_setup->min_interval;
622 adv_setup.adv_type = adv_param_setup->adv_type;
623 adv_setup.chnl_map = adv_param_setup->chnl_map;
624 adv_setup.tx_power = adv_param_setup->tx_power;
625 adv_setup.timeout_s = adv_param_setup->timeout_s;
627 adv_setup.server_if = instance_id;
629 ret = gatt_api->server->multi_adv_set_inst_data(adv_setup);
630 if (ret != BT_STATUS_SUCCESS) {
631 BT_ERR("GATT Set Multi advertising data failed: %s", status2string(ret));
632 info->state = GATT_INS_DISABLED;
633 return convert_to_oal_status(ret);
635 return OAL_STATUS_SUCCESS;
638 oal_status_t gatts_disconnect(int instance_id, bt_address_t *device_address, int conn_id)
640 int ret = OAL_STATUS_SUCCESS;
643 API_TRACE("Server Disonnect: instance_id = %d, conn_id = %d", instance_id, conn_id);
645 CHECK_OAL_GATT_ENABLED();
646 CHECK_SERVER_INSTANCE(instance_id);
647 if (__gatts_find_server_instance(instance_id) == NULL)
648 return OAL_STATUS_INTERNAL_ERROR;
650 API_TRACE("[%s]", bdt_bd2str(device_address, &bdstr));
652 ret = gatt_api->server->disconnect(instance_id, (bt_bdaddr_t *) device_address, conn_id);
653 if (ret != BT_STATUS_SUCCESS) {
654 BT_ERR("GATT Server disconnect failed: %s", status2string(ret));
655 return convert_to_oal_status(ret);
657 return OAL_STATUS_SUCCESS;
661 /************************************GATT Server Functions*************************************/
663 static void cb_gatts_register_app(int status, int server_if, bt_uuid_t *uuid)
665 char str[2*BT_UUID_STRING_MAX];
666 uuid_to_stringname((service_uuid_t*)uuid, str);
668 event_gatts_register_t *event = g_malloc0(sizeof(event_gatts_register_t));
670 BT_INFO("BTGATT SERVER REGISTER APP CB, UUID:%s, status:%d, server_if:%d",
671 str, status, server_if);
673 gatt_server_t *info = g_malloc0(sizeof(gatt_server_t));
674 info->instance_id = server_if;
675 memcpy(info->uuid.uuid, uuid->uu, sizeof(bt_uuid_t));
676 gatt_servers = g_slist_append(gatt_servers, info);
677 BT_DBG("GATT Server Added. count of gatt_servers: %d", g_slist_length(gatt_servers));
679 event->server_inst = server_if;
680 memcpy(event->server_uuid.uuid, uuid->uu, sizeof(bt_uuid_t));
681 send_event(OAL_EVENT_BLE_SERVER_INSTANCE_INITIALISED, event, sizeof(event_gatts_register_t));
684 static void cb_gatts_listen(int status, int server_if)
687 gboolean new_state = FALSE;
688 event_ble_adv_status *event = g_malloc0(sizeof(event_ble_adv_status));
691 for (l = gatt_servers; l; l = g_slist_next(l)) {
692 gatt_server_t *info = l->data;
695 if (info->instance_id == server_if) {
696 event->server_inst = server_if;
697 prev_state = info->cur_adv_state;
698 new_state = (status == BT_STATUS_SUCCESS) ? !prev_state : prev_state;
699 info->cur_adv_state = new_state;
704 BT_INFO("Adv State of server instance %d, new_state: %d", server_if, new_state);
706 if (TRUE == new_state)
707 send_event(OAL_EVENT_BLE_ADVERTISING_STARTED, event, sizeof(event_ble_adv_status));
709 send_event(OAL_EVENT_BLE_ADVERTISING_STOPPED, event, sizeof(event_ble_adv_status));
712 static void cb_gatts_multi_adv_enable(int server_if, int status)
715 BT_INFO("BTGATT SERVER MULTI ADV ENABLE CB, status:%d, srv_if: %d", status, server_if);
716 event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
719 if (status != BT_STATUS_SUCCESS)
720 BT_ERR("MULTI ADV ENABLE failed, status:%d, srv_if: %d", status, server_if);
722 for (l = gatt_servers; l; l = g_slist_next(l)) {
723 gatt_server_t *info = l->data;
726 if (info->instance_id == server_if) {
727 event->server_inst = server_if;
728 info->state = GATT_INS_ENABLED;
732 event->server_inst = -1;
734 event->status = convert_to_oal_status(status);
735 send_event(OAL_EVENT_BLE_MULTI_ADVERTISING_ENABLE, event, sizeof(event_ble_multiadv_status));
738 static void cb_gatts_multi_adv_disable(int server_if, int status)
740 BT_INFO("BTGATT SERVER MULTI ADV DISABLE CB, status:%d, srv_if: %d", status, server_if);
741 event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
744 if (status != BT_STATUS_SUCCESS)
745 BT_ERR("MULTI ADV DISABLE failed, status:%d, srv_if: %d", status, server_if);
747 for (l = gatt_servers; l; l = g_slist_next(l)) {
748 gatt_server_t *info = l->data;
751 if (info->instance_id == server_if) {
752 event->server_inst = server_if;
753 info->state = GATT_INS_DISABLED;
757 event->server_inst = -1;
759 event->status = convert_to_oal_status(status);
760 send_event(OAL_EVENT_BLE_MULTI_ADVERTISING_DISABLE, event, sizeof(event_ble_multiadv_status));
763 static void cb_gatts_multi_adv_update(int server_if, int status)
765 BT_INFO("BTGATT SERVER MULTI ADV UPDATE CB, status:%d, srv_if: %d", status, server_if);
766 event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
769 if (status != BT_STATUS_SUCCESS)
770 BT_ERR("MULTI ADV UPDATE failed, status:%d, srv_if: %d", status, server_if);
772 for (l = gatt_servers; l; l = g_slist_next(l)) {
773 gatt_server_t *info = l->data;
776 if (info->instance_id == server_if) {
777 event->server_inst = server_if;
781 event->server_inst = -1;
783 event->status = convert_to_oal_status(status);
784 send_event(OAL_EVENT_BLE_MULTI_ADVERTISING_UPDATE, event, sizeof(event_ble_multiadv_status));
787 static void cb_gatts_multi_adv_set_inst_data(int server_if, int status)
789 BT_INFO("BTGATT SERVER MULTI ADV SET INST DATA CB, status:%d, srv_if: %d", status, server_if);
790 event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
793 if (status != BT_STATUS_SUCCESS)
794 BT_ERR("MULTI ADV SET DATA failed, status:%d, srv_if: %d", status, server_if);
796 for (l = gatt_servers; l; l = g_slist_next(l)) {
797 gatt_server_t *info = l->data;
800 if (info->instance_id == server_if) {
801 event->server_inst = server_if;
802 info->state = GATT_INS_DATA_SET;
806 event->server_inst = -1;
808 event->status = convert_to_oal_status(status);
809 send_event(OAL_EVENT_BLE_MULTI_ADVERTISING_SET_INST_DATA, event, sizeof(event_ble_multiadv_status));
812 oal_status_t gatts_get_att_mtu(int conn_id, int *mtu)
814 int ret = OAL_STATUS_SUCCESS;
815 API_TRACE("Get ATT MTU, conn_id: %d", conn_id);
816 CHECK_OAL_GATT_ENABLED();
817 OAL_CHECK_PARAMETER(mtu, return);
819 /* To prevent crash in case other libraries not support this api */
820 if (gatt_api->server->get_att_mtu == NULL) {
821 BT_WARN("get_att_mtu is NULL");
822 return OAL_STATUS_NOT_SUPPORT;
825 ret = gatt_api->server->get_att_mtu(conn_id, mtu);
826 if (ret != BT_STATUS_SUCCESS) {
827 BT_ERR("GATT MTU Size failed, status: %s", status2string(ret));
828 return convert_to_oal_status(ret);
831 BT_INFO("Current ATT MTU Size: %d", *mtu);
832 return OAL_STATUS_SUCCESS;
834 oal_status_t gatt_send_response_acquire(int conn_id, int trans_id,
835 int status, int fd, int mtu , void * fdlist)
837 int ret = OAL_STATUS_SUCCESS;
839 API_TRACE("Server Send Response : ConnId = %d, TransId = %d, %d", conn_id, trans_id, fd);
840 CHECK_OAL_GATT_ENABLED();
842 ret = gatt_api->server->send_response_acquire(conn_id, trans_id, status, fd, mtu, fdlist);
843 if (ret != BT_STATUS_SUCCESS) {
844 BT_ERR("GATT Server Send Response failed: %s", status2string(ret));
845 return convert_to_oal_status(ret);
847 return OAL_STATUS_SUCCESS;
850 oal_status_t gatts_add_service(int instance_id, oal_gatt_srvc_id_t *gatt_serv_id,
853 int ret = OAL_STATUS_SUCCESS;
854 btgatt_srvc_id_t btgatt_srvc_id;
855 char str[2*BT_UUID_STRING_MAX];
857 API_TRACE("Server Add Service : instance_id = %d, num_handles = %d", instance_id, num_handles);
858 CHECK_OAL_GATT_ENABLED();
860 CHECK_SERVER_INSTANCE(instance_id);
861 if (__gatts_find_server_instance(instance_id) == NULL)
862 return OAL_STATUS_INTERNAL_ERROR;
864 if (gatt_serv_id != NULL) {
865 uuid_to_stringname(&(gatt_serv_id->id.uuid), str);
866 API_TRACE("Service uuid: [%s]", str);
867 memcpy(btgatt_srvc_id.id.uuid.uu, gatt_serv_id->id.uuid.uuid, sizeof(gatt_serv_id->id.uuid));
868 btgatt_srvc_id.id.inst_id = gatt_serv_id->id.inst_id;
869 btgatt_srvc_id.is_primary = gatt_serv_id->is_prmry;
871 BT_INFO("GATT Server Service Id is NULL");
872 return OAL_STATUS_INVALID_PARAM;
875 ret = gatt_api->server->add_service(instance_id, &btgatt_srvc_id, num_handles);
876 if (ret != BT_STATUS_SUCCESS) {
877 BT_ERR("GATT Server add service failed: %s", status2string(ret));
878 return convert_to_oal_status(ret);
880 return OAL_STATUS_SUCCESS;
883 oal_status_t gatts_add_included_services(int instance_id, int serv_handle,
886 int ret = OAL_STATUS_SUCCESS;
887 API_TRACE("Server Add included Service : instance_id = %d, serv_handle = %d, incl_handle = %d",
888 instance_id, serv_handle, incl_handle);
889 CHECK_OAL_GATT_ENABLED();
891 CHECK_SERVER_INSTANCE(instance_id);
892 if (__gatts_find_server_instance(instance_id) == NULL)
893 return OAL_STATUS_INTERNAL_ERROR;
895 ret = gatt_api->server->add_included_service(instance_id, serv_handle, incl_handle);
896 if (ret != BT_STATUS_SUCCESS) {
897 BT_ERR("GATT Server Add included Service failed: %s", status2string(ret));
898 return convert_to_oal_status(ret);
900 return OAL_STATUS_SUCCESS;
903 oal_status_t gatts_add_characteristics(int instance_id, int serv_handle,
904 oal_uuid_t* charc_uuid, int propts, int permsn)
906 int ret = OAL_STATUS_SUCCESS;
907 char str[2*BT_UUID_STRING_MAX];
908 char prop[MAX_PROP_LEN], perm[MAX_PERM_LEN];
909 char_prop_to_string(propts, prop);
910 char_perm_to_string(permsn, perm);
911 API_TRACE("Server Add Characteristic : instance_id = %d, serv_handle = %d, Properties =%s, Permisison = %s",
912 instance_id, serv_handle, prop, perm);
913 CHECK_OAL_GATT_ENABLED();
915 CHECK_SERVER_INSTANCE(instance_id);
916 if (__gatts_find_server_instance(instance_id) == NULL)
917 return OAL_STATUS_INTERNAL_ERROR;
919 if (charc_uuid != NULL) {
920 uuid_to_stringname(charc_uuid, str);
921 API_TRACE("uuid: [%s]", str);
924 ret = gatt_api->server->add_characteristic(instance_id,
925 serv_handle, (bt_uuid_t*)charc_uuid, propts, permsn);
926 if (ret != BT_STATUS_SUCCESS) {
927 BT_ERR("GATT Server Add Characteristic failed: %s", status2string(ret));
928 return convert_to_oal_status(ret);
930 return OAL_STATUS_SUCCESS;
933 oal_status_t gatts_add_descriptor(int instance_id, int serv_handle,
934 oal_uuid_t* desc_uuid, int permsn)
936 int ret = OAL_STATUS_SUCCESS;
937 char str[2*BT_UUID_STRING_MAX];
938 char perm[MAX_PERM_LEN];
939 char_perm_to_string(permsn, perm);
940 API_TRACE("Server Add Descriptor : instance_id = %d, serv_handle = %d, Permisison = %s",
941 instance_id, serv_handle, perm);
942 CHECK_OAL_GATT_ENABLED();
944 CHECK_SERVER_INSTANCE(instance_id);
945 if (__gatts_find_server_instance(instance_id) == NULL)
946 return OAL_STATUS_INTERNAL_ERROR;
948 if (desc_uuid != NULL) {
949 uuid_to_stringname(desc_uuid, str);
950 API_TRACE("uuid: [%s]", str);
953 ret = gatt_api->server->add_descriptor(instance_id,
954 serv_handle, (bt_uuid_t*)desc_uuid, permsn);
955 if (ret != BT_STATUS_SUCCESS) {
956 BT_ERR("GATT Server Add Descriptor failed: %s", status2string(ret));
957 return convert_to_oal_status(ret);
959 return OAL_STATUS_SUCCESS;
962 oal_status_t gatts_start_service(int instance_id, int svc_handle, int transport)
964 int ret = OAL_STATUS_SUCCESS;
966 API_TRACE("Server Start Service : Ins_id = %d, srvHndl = %d, transport = %d",
967 instance_id, svc_handle, transport);
968 CHECK_OAL_GATT_ENABLED();
970 CHECK_SERVER_INSTANCE(instance_id);
971 if (__gatts_find_server_instance(instance_id) == NULL)
972 return OAL_STATUS_INTERNAL_ERROR;
974 ret = gatt_api->server->start_service(instance_id, svc_handle, transport);
975 if (ret != BT_STATUS_SUCCESS) {
976 BT_ERR("GATT Server Start Service failed: %s", status2string(ret));
977 return convert_to_oal_status(ret);
979 return OAL_STATUS_SUCCESS;
982 oal_status_t gatts_stop_service(int instance_id, int srv_hdl)
984 int ret = OAL_STATUS_SUCCESS;
986 API_TRACE("Server Stop Service : Ins_id = %d, srvHndl = %d, ", instance_id, srv_hdl);
987 CHECK_OAL_GATT_ENABLED();
989 CHECK_SERVER_INSTANCE(instance_id);
990 if (__gatts_find_server_instance(instance_id) == NULL)
991 return OAL_STATUS_INTERNAL_ERROR;
993 ret = gatt_api->server->stop_service(instance_id, srv_hdl);
994 if (ret != BT_STATUS_SUCCESS) {
995 BT_ERR("GATT Server Stop Service failed: %s", status2string(ret));
996 return convert_to_oal_status(ret);
998 return OAL_STATUS_SUCCESS;
1001 oal_status_t gatts_delete_service(int instance_id, int srv_hdl)
1003 int ret = OAL_STATUS_SUCCESS;
1005 API_TRACE("Server Delete Service : Ins_id = %d, srvHndl = %d, ", instance_id, srv_hdl);
1006 CHECK_OAL_GATT_ENABLED();
1008 CHECK_SERVER_INSTANCE(instance_id);
1009 if (__gatts_find_server_instance(instance_id) == NULL)
1010 return OAL_STATUS_INTERNAL_ERROR;
1012 ret = gatt_api->server->delete_service(instance_id, srv_hdl);
1013 if (ret != BT_STATUS_SUCCESS) {
1014 BT_ERR("GATT Server Delete Service failed: %s", status2string(ret));
1015 return convert_to_oal_status(ret);
1017 return OAL_STATUS_SUCCESS;
1020 oal_status_t gatts_send_response(int conn_id, int trans_id, int resp_status, oal_gatt_response_t *response)
1022 int ret = OAL_STATUS_SUCCESS;
1024 API_TRACE("Server Send Response : ConnId = %d, TransId = %d, ", conn_id, trans_id);
1025 CHECK_OAL_GATT_ENABLED();
1027 if (response == NULL) {
1028 BT_ERR("GATT Server attribute value is empty");
1029 return OAL_STATUS_INVALID_PARAM;
1032 ret = gatt_api->server->send_response(conn_id, trans_id, resp_status, (btgatt_response_t *)response);
1033 if (ret != BT_STATUS_SUCCESS) {
1034 BT_ERR("GATT Server Send Response failed: %s", status2string(ret));
1035 return convert_to_oal_status(ret);
1037 return OAL_STATUS_SUCCESS;
1040 oal_status_t gatts_send_indication(int instance_id, int attr_hndl, int conn_id, int len, int confirm, char* value)
1042 int ret = OAL_STATUS_SUCCESS;
1044 API_TRACE("Server Send Indication : Ins_id = %d, srvHndl = %d, [%s]",
1045 instance_id, attr_hndl, confirm == TRUE ? "INDICATION" : "NOTIFICATION");
1046 CHECK_OAL_GATT_ENABLED();
1048 CHECK_SERVER_INSTANCE(instance_id);
1049 if (__gatts_find_server_instance(instance_id) == NULL)
1050 return OAL_STATUS_INTERNAL_ERROR;
1052 if (value == NULL || len == 0) {
1053 BT_ERR("GATT Server attribute value is empty");
1054 return OAL_STATUS_INVALID_PARAM;
1057 ret = gatt_api->server->send_indication(instance_id,
1058 attr_hndl, conn_id, len, confirm, value);
1059 if (ret != BT_STATUS_SUCCESS) {
1060 BT_ERR("GATT Server Send Indication failed: %s", status2string(ret));
1061 return convert_to_oal_status(ret);
1063 return OAL_STATUS_SUCCESS;
1066 oal_status_t gatts_update_att_value(int instance_id, oal_gatt_value_t *value)
1068 int ret = OAL_STATUS_SUCCESS;
1070 CHECK_OAL_GATT_ENABLED();
1072 CHECK_SERVER_INSTANCE(instance_id);
1073 if (__gatts_find_server_instance(instance_id) == NULL)
1074 return OAL_STATUS_INTERNAL_ERROR;
1076 if (value == NULL || value->len == 0) {
1077 BT_ERR("GATT Server attribute value is empty");
1078 return OAL_STATUS_INVALID_PARAM;
1081 API_TRACE("Server Update Value: Ins_id = [%d], att handle = [%d] data len [%d]",
1082 instance_id, value->handle, value->len);
1084 ret = gatt_api->server->update_att_value(instance_id,
1085 (int)value->handle, (int)value->len, (char*)value->value);
1086 if (ret != BT_STATUS_SUCCESS) {
1087 BT_ERR("GATT Server Update Attribute Value failed: [%s]", status2string(ret));
1088 return convert_to_oal_status(ret);
1090 return OAL_STATUS_SUCCESS;
1093 /* GATT Server Callbacks:Start */
1094 static void cb_gatts_service_added(int status, int server_if,
1095 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(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)
1122 BT_INFO("BTGATT SERVER INCLUDED SERVICE ADDED CB, status:%d, srvc handle:%d, incl handle:%d",
1123 status, srvc_handle, incl_srvc_handle);
1125 event_gatts_incld_srvc_t* event = g_new0(event_gatts_incld_srvc_t, 1);
1127 CHECK_CORRECT_SERVER_ID(server_if, event->gatt_srvc_stat.server_inst);
1129 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1130 event->gatt_srvc_stat.server_inst = -1;
1132 event->gatt_srvc_stat.servic_hndl = srvc_handle;
1133 event->incl_srvc_hndl = incl_srvc_handle;
1134 event->gatt_srvc_stat.status = convert_to_oal_status(status);
1135 send_event(OAL_EVENT_GATTS_INCLUDED_SERVICE_ADDED, event, sizeof(event_gatts_incld_srvc_t));
1138 static void cb_gatts_characteristic_added(int status, int server_if, bt_uuid_t *char_id,
1139 int srvc_handle, int char_handle)
1141 BT_INFO("BTGATT SERVER CHARACTERISTIC ADDED CB, status:%d, srvc handle:%d, char handle:%d",
1142 status, srvc_handle, char_handle);
1144 event_gatts_srvc_charctr_t* event = g_new0(event_gatts_srvc_charctr_t, 1);
1146 CHECK_CORRECT_SERVER_ID(server_if, event->gatt_srvc_stat.server_inst);
1148 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1149 event->gatt_srvc_stat.server_inst = -1;
1151 event->gatt_srvc_stat.servic_hndl = srvc_handle;
1152 event->charctr_hndl = char_handle;
1153 event->gatt_srvc_stat.status = convert_to_oal_status(status);
1154 memcpy(event->charctr_uuid.uuid, char_id->uu, sizeof(bt_uuid_t));
1155 send_event(OAL_EVENT_GATTS_CHARACTERISTIC_ADDED, event, sizeof(event_gatts_srvc_charctr_t));
1158 static void cb_gatts_descriptor_added(int status, int server_if,
1159 bt_uuid_t *descr_id, int srvc_handle,
1162 BT_INFO("BTGATT SERVER DESCRIPTOR ADDED CB, status:%d, srvc handle:%d, desc handle:%d",
1163 status, srvc_handle, descr_handle);
1165 event_gatts_srvc_descr_t* event = g_new0(event_gatts_srvc_descr_t, 1);
1167 CHECK_CORRECT_SERVER_ID(server_if, event->gatt_srvc_stat.server_inst);
1169 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1170 event->gatt_srvc_stat.server_inst = -1;
1172 event->gatt_srvc_stat.servic_hndl = srvc_handle;
1173 event->descrptr_hndl = descr_handle;
1174 event->gatt_srvc_stat.status = convert_to_oal_status(status);
1175 memcpy(event->descrptr_uuid.uuid, descr_id->uu, sizeof(bt_uuid_t));
1176 send_event(OAL_EVENT_GATTS_DESCRIPTOR_ADDED, event, sizeof(event_gatts_srvc_descr_t));
1179 static void cb_gatts_service_started(int status, int server_if, int srvc_handle)
1181 BT_INFO("BTGATT SERVER SERVICE STARTED CB, status:%d, srvc_handle:%d", status, srvc_handle);
1183 event_gatts_srvc_t* event = g_new0(event_gatts_srvc_t, 1);
1185 CHECK_CORRECT_SERVER_ID(server_if, event->server_inst);
1187 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1188 event->server_inst = -1;
1190 event->servic_hndl = srvc_handle;
1191 event->status = convert_to_oal_status(status);
1192 send_event(OAL_EVENT_GATTS_SERVICE_STARTED, event, sizeof(event_gatts_srvc_t));
1195 static void cb_gatts_service_stopped(int status, int server_if, int srvc_handle)
1197 BT_INFO("BTGATT SERVER SERVICE STOPPED CB, status:%d, srvc_handle:%d", status, srvc_handle);
1199 event_gatts_srvc_t* event = g_new0(event_gatts_srvc_t, 1);
1201 CHECK_CORRECT_SERVER_ID(server_if, event->server_inst);
1203 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1204 event->server_inst = -1;
1206 event->servic_hndl = srvc_handle;
1207 event->status = convert_to_oal_status(status);
1208 send_event(OAL_EVENT_GATTS_SERVICE_STOPED, event, sizeof(event_gatts_srvc_t));
1211 static void cb_gatts_service_deleted(int status, int server_if, int srvc_handle)
1213 BT_INFO("BTGATT SERVER SERVICE DELETED CB, status:%d, srvc_handle:%d", status, srvc_handle);
1215 event_gatts_srvc_t* event = g_new0(event_gatts_srvc_t, 1);
1217 CHECK_CORRECT_SERVER_ID(server_if, event->server_inst);
1219 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1220 event->server_inst = -1;
1222 event->servic_hndl = srvc_handle;
1223 event->status = convert_to_oal_status(status);
1224 send_event(OAL_EVENT_GATTS_SERVICE_DELETED, event, sizeof(event_gatts_srvc_t));
1227 static void cb_gatts_connection(int conn_id, int server_if, int connected, bt_bdaddr_t *bda)
1230 oal_event_t event_type;
1232 BT_INFO("BTGATT SERVER CONNECTION connnected:%d, conn_id:%d server_if:%d", connected, conn_id, server_if);
1233 event_gatts_conn_t *event = g_new0(event_gatts_conn_t, 1);
1235 if (connected == TRUE) {
1236 for (l = gatt_servers; l; l = g_slist_next(l)) {
1237 gatt_server_t *info = l->data;
1240 if (info->instance_id == server_if) {
1241 event->server_inst = server_if;
1246 /* server_inst is not required in disconnected case */
1248 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1249 event->conn_id = conn_id;
1250 event_type = ((connected == TRUE) ? OAL_EVENT_GATTS_CONNECTION_COMPLETED : OAL_EVENT_GATTS_DISCONNECTION_COMPLETED);
1251 event->status = OAL_STATUS_SUCCESS;
1253 send_event(event_type, event, sizeof(event_gatts_conn_t));
1256 static void cb_gatts_request_read(int conn_id, int trans_id, bt_bdaddr_t *bda,
1257 int attr_handle, int offset, bool is_long)
1259 BT_INFO("BTGATT SERVER REQUEST READ CB: conn_id:%d", conn_id);
1261 event_gatts_srvc_read_attr_t* event = g_new0(event_gatts_srvc_read_attr_t, 1);
1263 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1265 event->attr_trans.attr_handle = attr_handle;
1266 event->attr_trans.conn_id = conn_id;
1267 event->attr_trans.trans_id = trans_id;
1268 event->attr_trans.offset = offset;
1269 event->is_long = is_long;
1271 send_event(OAL_EVENT_GATTS_REQUEST_READ, event, sizeof(event_gatts_srvc_read_attr_t));
1274 static void cb_gatts_request_write(int conn_id, int trans_id,
1275 bt_bdaddr_t *bda, int attr_handle,
1276 int offset, int length,
1277 bool need_rsp, bool is_prep, uint8_t* value)
1279 BT_INFO("BTGATT SERVER REQUEST WRITE conn_id:%d", conn_id);
1281 event_gatts_srvc_write_attr_t* event = g_new0(event_gatts_srvc_write_attr_t, 1);
1283 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1285 event->attr_trans.attr_handle = attr_handle;
1286 event->attr_trans.conn_id = conn_id;
1287 event->attr_trans.trans_id = trans_id;
1288 event->attr_trans.offset = offset;
1289 event->need_rsp = need_rsp;
1290 event->is_prep = is_prep;
1292 if (length > 0 && value != NULL) {
1293 if (length > OAL_GATT_MAX_ATTR_LEN)
1294 length = OAL_GATT_MAX_ATTR_LEN;
1295 memcpy(event->value, value, length);
1296 event->length = length;
1298 send_event(OAL_EVENT_GATTS_REQUEST_WRITE, event, sizeof(event_gatts_srvc_write_attr_t));
1301 static void cb_gatts_acquire_write(int mtu, int conn_id, int trans_id, int attr_handle , bt_bdaddr_t *bda)
1304 BT_INFO("BTGATT SERVER REQUEST ACQUIRE WRITE conn_id:%d", conn_id);
1306 event_gatts_srvc_acquire_attr_t* event = g_new0(event_gatts_srvc_acquire_attr_t, 1);
1308 event->attr_trans.attr_handle = attr_handle;
1309 event->attr_trans.conn_id = conn_id;
1310 event->attr_trans.trans_id = trans_id;
1312 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1314 send_event(OAL_EVENT_GATTS_REQUEST_ACQUIRE_WRITE, event, sizeof(event_gatts_srvc_acquire_attr_t));
1317 static void cb_gatts_acquire_notify(int mtu, int conn_id, int trans_id, int attr_handle)
1319 BT_INFO("BTGATT SERVER REQUEST ACQUIRE NOTIFY conn_id:%d", conn_id);
1321 event_gatts_srvc_acquire_attr_t* event = g_new0(event_gatts_srvc_acquire_attr_t, 1);
1323 event->attr_trans.attr_handle = attr_handle;
1324 event->attr_trans.conn_id = conn_id;
1325 event->attr_trans.trans_id = trans_id;
1328 send_event(OAL_EVENT_GATTS_REQUEST_ACQUIRE_NOTIFY, event, sizeof(event_gatts_srvc_acquire_attr_t));
1332 static void cb_gatts_response_confirmation(int status, int handle)
1334 BT_INFO("BTGATT SERVER RESPONSE CONFIRMATION CB, status:%d, handle:%d", status, handle);
1336 event_gatts_respons_t* event = g_new0(event_gatts_respons_t, 1);
1338 event->hndl = handle;
1339 event->status = convert_to_oal_status(status);
1341 send_event(OAL_EVENT_GATTS_RESPONSE_CONFIRMED, event, sizeof(event_gatts_respons_t));
1344 static void cb_indication_confirmation(int conn_id, int trans_id, int attr_handle, bt_bdaddr_t *bda)
1346 BT_INFO("BTGATT SERVER Indication Confirmation CB, conn_id:%d, trans_id:%d attr_handle:%d",
1347 conn_id, trans_id, attr_handle);
1349 event_gatts_ind_cnfrm_t* event = g_new0(event_gatts_ind_cnfrm_t, 1);
1351 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1353 event->attr_handle = attr_handle;
1354 event->conn_id = conn_id;
1355 event->trans_id = trans_id;
1356 send_event(OAL_EVENT_GATTS_IND_CONFIRM, event, sizeof(event_gatts_ind_cnfrm_t));
1359 static void cb_gatts_mtu_changed(int conn_id, int mtu)
1361 BT_INFO("BTGATT SERVER MTU CHANGED CB, conn_id: %d, mtu_size: %d", conn_id, mtu);
1362 event_gatts_mtu_changed_t *event = g_new0(event_gatts_mtu_changed_t, 1);
1363 event->conn_id = conn_id;
1364 event->mtu_size = mtu;
1365 send_event(OAL_EVENT_GATTS_MTU_CHANGED, event, sizeof(event_gatts_mtu_changed_t));
1369 static void cb_notifcation_changed(int conn_id, int trans_id, int attr_handle, bool notify, bt_bdaddr_t *bda)
1371 BT_INFO("BTGATT SERVER Notication CB, conn_id:%d, trans_id:%d attr_handle:%d Notify:%d",
1372 conn_id, trans_id, attr_handle, notify);
1374 event_gatts_notif_t* event = g_new0(event_gatts_notif_t, 1);
1376 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1378 event->attr_handle = attr_handle;
1379 event->conn_id = conn_id;
1380 event->trans_id = trans_id;
1381 event->notify = notify;
1382 send_event(OAL_EVENT_GATTS_NOTIFICATION, event, sizeof(event_gatts_notif_t));
1387 /* GATT Server Callbacks: End */
1388 /************************************ GATT Client ***********************************/
1389 /* Client Callbacks */
1390 static void cb_gattc_register_app(int status, int clientIf, bt_uuid_t *app_uuid)
1392 char uuid_str[BT_UUID_STRING_MAX];
1393 event_gattc_register_t *event;
1395 /* Check if GATT client registered for Default GATT client UUID */
1396 uuid_to_string((service_uuid_t *)app_uuid, uuid_str);
1398 BT_INFO("GATT CLIENT REGISTER APP CB, status:%d, clientIf:%d, %s", status, clientIf, uuid_str);
1399 event = g_new0(event_gattc_register_t, 1);
1400 event->client_if = clientIf;
1401 event->status = (status == 0 ? OAL_STATUS_SUCCESS : OAL_STATUS_INTERNAL_ERROR);
1402 memcpy(event->client_uuid.uuid, app_uuid->uu, sizeof(oal_uuid_t));
1403 send_event(OAL_EVENT_GATTC_REGISTRATION, event, sizeof(event_gattc_register_t));
1406 static void cb_gattc_scan_result(bt_bdaddr_t* bdaddress, uint8_t addr_type, int rssi,
1407 uint8_t *adv_data, int adv_data_len, uint8_t *scan_rsp_data, int scan_rsp_data_len)
1409 event_ble_scan_result_info *event;
1411 event = g_new0(event_ble_scan_result_info, 1);
1412 event->addr_type = addr_type;
1414 memcpy(event->address.addr, bdaddress->address, BT_ADDRESS_BYTES_NUM);
1415 memcpy(event->adv_data, adv_data, BLE_ADV_DATA_LENGTH);
1416 event->adv_data_len = adv_data_len;
1417 memcpy(event->scan_rsp_data, scan_rsp_data, BLE_ADV_DATA_LENGTH);
1418 event->scan_rsp_data_len = scan_rsp_data_len;
1419 send_event_bda_trace(OAL_EVENT_BLE_REMOTE_DEVICE_FOUND, event,
1420 sizeof(event_ble_scan_result_info), (bt_address_t *)bdaddress);
1423 static void cb_gattc_connection(int conn_id, int status, int client_if, bt_bdaddr_t* bda)
1425 event_gattc_conn_t *event;
1426 BT_INFO("BTGATT CLIENT CONNECTED CB, status:[%d] client_if[%d] id[%d]",
1427 status, client_if, conn_id);
1429 event = g_new0(event_gattc_conn_t, 1);
1430 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1431 event->client_if = client_if;
1433 if (BT_STATUS_SUCCESS != status) {
1434 event->status = OAL_STATUS_INTERNAL_ERROR;
1435 BT_ERR("gattc connection Error: %d", status);
1437 event->conn_id = conn_id;
1438 event->status = OAL_STATUS_SUCCESS;
1441 send_event_bda_trace(OAL_EVENT_GATTC_CONNECTION_COMPLETED, event,
1442 sizeof(*event), (bt_address_t *)bda);
1446 static void cb_gattc_disconnect(int conn_id, int status, int client_if, bt_bdaddr_t* bda)
1448 event_gattc_conn_t *event;
1449 BT_INFO("BTGATT Client Disconnect Complete cb, status:[%d], client_if[%d] id[%d]",
1450 status, client_if, conn_id);
1451 event = g_new0(event_gattc_conn_t, 1);
1452 event->status = convert_to_oal_status(status);
1453 event->client_if = client_if;
1454 event->conn_id = conn_id;
1455 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1456 send_event_bda_trace(OAL_EVENT_GATTC_DISCONNECTION_COMPLETED, event,
1457 sizeof(*event), (bt_address_t *)bda);
1460 static void cb_gattc_search_result(int conn_id, btgatt_srvc_id_t *srvc_id)
1462 char uuid_str[2*BT_UUID_STRING_MAX];
1464 uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str);
1465 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]",
1466 uuid_str, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1468 event_gattc_service_result_t *event = g_new0(event_gattc_service_result_t, 1);
1469 event->conn_status.status = OAL_STATUS_SUCCESS;
1470 event->conn_status.conn_id = conn_id;
1471 memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1473 send_event(OAL_EVENT_GATTC_SERVICE_SEARCH_RESULT, event, sizeof(*event));
1476 static void cb_gattc_search_complete(int conn_id, int status)
1478 BT_INFO("BTGATT Client Service Search Complete cb, status:%d, conn_id:%d",
1481 event_gattc_conn_status_t *event = g_new0(event_gattc_conn_status_t, 1);
1482 event->conn_id = conn_id;
1483 event->status = convert_to_oal_status(status);
1485 send_event(OAL_EVENT_GATTC_SERVICE_SEARCH_DONE, event, sizeof(*event));
1489 static void cb_gattc_get_characteristics(int conn_id, int status, btgatt_srvc_id_t *srvc_id,
1490 btgatt_gatt_id_t *char_id, int char_prop)
1492 char uuid_str1[2*BT_UUID_STRING_MAX];
1493 char uuid_str2[2*BT_UUID_STRING_MAX];
1496 BT_DBG("BTGATT Client Get Characteristic Callback, conn_id:%d, status:%d", conn_id, status);
1497 uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str1);
1498 BT_DBG("Service=> [%s], Inst_id [%u], Type [%s]",
1499 uuid_str1, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1501 event_gattc_characteristic_result_t *event = g_new0(event_gattc_characteristic_result_t, 1);
1502 event->conn_status.conn_id = conn_id;
1503 event->conn_status.status = convert_to_oal_status(status);
1504 memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1507 uuid_to_stringname((oal_uuid_t *)&(char_id->uuid), uuid_str2);
1508 BT_INFO("Charac => [%s], Inst_id [%u], Prop [%s]",
1509 uuid_str2, char_id->inst_id, char_prop_to_string(char_prop, str));
1510 event->char_prop = char_prop;
1511 memcpy(&(event->char_id), char_id, sizeof(oal_gatt_id_t));
1513 send_event(OAL_EVENT_GATTC_CHARAC_SERACH_RESULT, event, sizeof(*event));
1518 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)
1520 char uuid_str1[2*BT_UUID_STRING_MAX];
1521 char uuid_str2[2*BT_UUID_STRING_MAX];
1522 char uuid_str3[2*BT_UUID_STRING_MAX];
1523 BT_DBG("BTGATT Client Get Descriptor Callback, conn_id:%d, status:%d", conn_id, status);
1524 uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str1);
1525 uuid_to_stringname((oal_uuid_t *)&(char_id->uuid), uuid_str2);
1526 BT_DBG("Service=> [%s], Inst_id [%u], Type [%s]",
1527 uuid_str1, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1528 BT_DBG("Charac => [%s], Inst_id [%u]", uuid_str2, char_id->inst_id);
1530 event_gattc_descriptor_result_t *event = g_new0(event_gattc_descriptor_result_t, 1);
1531 event->conn_status.conn_id = conn_id;
1532 event->conn_status.status = convert_to_oal_status(status);
1533 memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1534 memcpy(&(event->char_id), char_id, sizeof(oal_gatt_id_t));
1537 uuid_to_stringname((oal_uuid_t *)&(descr_id->uuid), uuid_str3);
1538 BT_INFO("Desc => [%s], Inst_id [%u]", uuid_str3, descr_id->inst_id);
1539 memcpy(&(event->descr_id), descr_id, sizeof(oal_gatt_id_t));
1541 send_event(OAL_EVENT_GATTC_DESC_SERACH_RESULT, event, sizeof(*event));
1544 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)
1546 char uuid_str1[2*BT_UUID_STRING_MAX];
1547 char uuid_str2[2*BT_UUID_STRING_MAX];
1548 BT_INFO("BTGATT Client Register For Notification Callback, conn_id:%d, status:%d, registered: %s",
1549 conn_id, status, registered == 1 ? "REGISTERED" : "DEREGISTERED");
1550 uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str1);
1551 uuid_to_stringname((oal_uuid_t *)&(char_id->uuid), uuid_str2);
1552 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1553 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, char_id->inst_id);
1555 event_gattc_regdereg_notify_t *event = g_new0(event_gattc_regdereg_notify_t, 1);
1556 oal_event_t event_type = (registered == 1 ? OAL_EVENT_GATTC_NOTIFICATION_REGISTERED : OAL_EVENT_GATTC_NOTIFICATION_DEREGISTERED);
1557 event->conn_id = conn_id;
1558 event->status = convert_to_oal_status(status);
1559 memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1560 memcpy(&(event->char_id), char_id, sizeof(oal_gatt_id_t));
1562 send_event(event_type, event, sizeof(*event));
1565 static void cb_gattc_notify(int conn_id, btgatt_notify_params_t *p_data)
1568 char uuid_str1[2*BT_UUID_STRING_MAX];
1569 char uuid_str2[2*BT_UUID_STRING_MAX];
1571 BT_INFO("BTGATT Client Notify Callback, conn_id:%d", conn_id);
1572 BT_INFO("Server Address:[%s], is_notify:[%d], len:[%d]",
1573 bdt_bd2str((bt_address_t *)&(p_data->bda), &bdstr), p_data->is_notify, p_data->len);
1575 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1576 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1578 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]",
1579 uuid_str1, p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1580 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1582 if (p_data->len > 0) {
1584 data = g_malloc(3*p_data->len+1);
1586 BT_ERR("memory allocation failed");
1590 convert_hex_2_str((unsigned char *)p_data->value, p_data->len, data);
1591 BT_INFO("Notified Data: [%s]", data);
1593 event_gattc_notify_data *event = g_new0(event_gattc_notify_data, 1);
1594 memcpy(event->address.addr, p_data->bda.address, BT_ADDRESS_BYTES_NUM);
1595 event->is_notify = p_data->is_notify;
1596 event->data_len = p_data->len;
1597 memcpy(event->data, p_data->value, event->data_len);
1598 memcpy(&(event->char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1599 memcpy(&(event->srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1601 send_event_bda_trace(OAL_EVENT_GATTC_NOTIFY_DATA, event, sizeof(*event), (bt_address_t *)&p_data->bda);
1606 static void cb_gattc_service_changed(bt_bdaddr_t *bd_addr, int change_type, uint8_t *uuid, int conn_id, int inst_id)
1608 event_gattc_service_changed_data *event = g_new0(event_gattc_service_changed_data, 1);
1610 memcpy(event->address.addr, bd_addr->address, 6);
1611 event->change_type = change_type;
1612 memcpy(event->uuid.uuid, uuid, sizeof(event->uuid.uuid));
1613 event->conn_id = conn_id;
1614 event->inst_id = inst_id;
1616 send_event_bda_trace(OAL_EVENT_GATTC_SERVICE_CHANGED_IND, event, sizeof(*event), (bt_address_t *)bd_addr);
1619 static void cb_gattc_read_characteristic(int conn_id, int status, btgatt_read_params_t *p_data)
1621 char uuid_str1[2*BT_UUID_STRING_MAX];
1622 char uuid_str2[2*BT_UUID_STRING_MAX];
1623 BT_INFO("BTGATT Client Read Charcateristic Callback, conn_id:%d, read_status:%d", conn_id, status);
1625 event_gattc_read_data *event = g_new0(event_gattc_read_data, 1);
1626 event->uuid_status.conn_status.conn_id = conn_id;
1627 event->uuid_status.conn_status.status = convert_to_oal_status(status);
1628 event->value_type = p_data->value_type;
1629 memcpy(&(event->uuid_status.srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1630 memcpy(&(event->uuid_status.char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1632 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1633 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1634 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1,
1635 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1636 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1637 BT_DBG("Len: %u, value_type: %u", p_data->value.len, p_data->value_type);
1638 if (p_data->value.len > 0 && event->value_type == GATTC_READ_VALUE_TYPE_VALUE) {
1639 event->data_len = p_data->value.len;
1640 memcpy(&(event->data), &(p_data->value.value), event->data_len);
1643 send_event(OAL_EVENT_GATTC_READ_CHARAC, event, sizeof(*event));
1646 static void cb_gattc_write_characteristic(int conn_id, int status, btgatt_write_params_t *p_data)
1648 char uuid_str1[2*BT_UUID_STRING_MAX];
1649 char uuid_str2[2*BT_UUID_STRING_MAX];
1650 BT_INFO("BTGATT Client Write Charcateristic Callback, conn_id:%d, write_status:%d", conn_id, status);
1652 event_gattc_write_data *event = g_new0(event_gattc_write_data, 1);
1653 event->conn_status.conn_id = conn_id;
1654 event->conn_status.status = convert_to_oal_status(status);
1655 memcpy(&(event->srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1656 memcpy(&(event->char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1658 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1659 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1660 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1,
1661 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1662 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1664 send_event(OAL_EVENT_GATTC_WRITE_CHARAC, event, sizeof(*event));
1667 static void cb_gattc_write_descriptor(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 char uuid_str3[2*BT_UUID_STRING_MAX];
1672 BT_INFO("BTGATT Client Write Descriptor Callback, conn_id:%d, write_status:%d", conn_id, status);
1674 event_gattc_write_data *event = g_new0(event_gattc_write_data, 1);
1675 event->conn_status.conn_id = conn_id;
1676 event->conn_status.status = convert_to_oal_status(status);
1677 memcpy(&(event->srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1678 memcpy(&(event->char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1679 memcpy(&(event->descr_id), &(p_data->descr_id), sizeof(oal_gatt_id_t));
1681 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1682 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1683 uuid_to_stringname((oal_uuid_t *)&(p_data->descr_id.uuid), uuid_str3);
1684 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1,
1685 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1686 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1687 BT_INFO("Desc => [%s], Inst_id [%u]", uuid_str3, p_data->descr_id.inst_id);
1689 send_event(OAL_EVENT_GATTC_WRITE_DESCR, event, sizeof(*event));
1694 static void cb_gattc_read_descriptor(int conn_id, int status, btgatt_read_params_t *p_data)
1696 char uuid_str1[2*BT_UUID_STRING_MAX];
1697 char uuid_str2[2*BT_UUID_STRING_MAX];
1698 char uuid_str3[2*BT_UUID_STRING_MAX];
1699 BT_INFO("BTGATT Client Read Descriptor Callback, conn_id:%d, read_status:%d", conn_id, status);
1701 event_gattc_read_data *event = g_new0(event_gattc_read_data, 1);
1702 event->uuid_status.conn_status.conn_id = conn_id;
1703 event->uuid_status.conn_status.status = convert_to_oal_status(status);
1704 event->value_type = p_data->value_type;
1705 memcpy(&(event->uuid_status.srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1706 memcpy(&(event->uuid_status.char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1707 memcpy(&(event->uuid_status.descr_id), &(p_data->descr_id), sizeof(oal_gatt_id_t));
1710 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1711 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1712 uuid_to_stringname((oal_uuid_t *)&(p_data->descr_id.uuid), uuid_str3);
1713 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1,
1714 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1715 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1716 BT_INFO("Desc => [%s], Inst_id [%u]", uuid_str3, p_data->descr_id.inst_id);
1717 BT_DBG("Len: %u, value_type: %u", p_data->value.len, p_data->value_type);
1718 if (p_data->value.len > 0 && event->value_type == GATTC_READ_VALUE_TYPE_VALUE) {
1720 data = g_malloc(3*p_data->value.len+1);
1722 BT_ERR("memory allocation failed");
1726 convert_hex_2_str((unsigned char *)p_data->value.value, p_data->value.len, data);
1727 BT_DBG("Read Data: [%s]", data);
1728 event->data_len = p_data->value.len;
1729 memcpy(&(event->data), &(p_data->value.value), event->data_len);
1733 send_event(OAL_EVENT_GATTC_READ_DESCR, event, sizeof(*event));
1736 oal_status_t gattc_start_le_discovery(int client_id)
1739 int ret = OAL_STATUS_SUCCESS;
1741 API_TRACE("BTGATT CLIENT SCAN START");
1742 CHECK_OAL_GATT_ENABLED();
1743 CHECK_CLIENT_REGISTRATION(client_id);
1745 ret = gatt_api->client->scan(client_id, 1);
1746 if (ret != BT_STATUS_SUCCESS) {
1747 BT_ERR("Error:Start LE Discovery failed: %s", status2string(ret));
1748 return convert_to_oal_status(ret);
1750 return OAL_STATUS_SUCCESS;
1753 oal_status_t gattc_stop_le_discovery(int client_id)
1756 int ret = OAL_STATUS_SUCCESS;
1758 API_TRACE("Scan is stopped");
1759 CHECK_OAL_GATT_ENABLED();
1760 CHECK_CLIENT_REGISTRATION(client_id);
1761 ret = gatt_api->client->scan(client_id, 0);
1762 if (ret != BT_STATUS_SUCCESS) {
1763 BT_ERR("Error:Stop LE Discovery failed: %s", status2string(ret));
1764 return convert_to_oal_status(ret);
1766 return OAL_STATUS_SUCCESS;
1769 oal_status_t gattc_set_le_scan_param(int scan_type, int itv, int win)
1773 API_TRACE("Scan is stopped");
1774 CHECK_OAL_GATT_ENABLED();
1777 ret = gatt_api->client->set_scan_parameters(scan_type, itv, win);
1778 if (ret != BT_STATUS_SUCCESS) {
1779 BT_ERR("Error:Set scan parameters failed: %s", status2string(ret));
1780 return convert_to_oal_status(ret);
1783 ret = gatt_api->client->set_scan_parameters(itv, win);
1784 if (ret != BT_STATUS_SUCCESS) {
1785 BT_ERR("Error:Set scan parameters failed: %s", status2string(ret));
1786 return convert_to_oal_status(ret);
1790 return OAL_STATUS_SUCCESS;
1793 static void cb_gattc_configure_mtu_cmpl(int conn_id, int status, int mtu)
1795 event_gattc_mtu_configured_t *event;
1796 BT_INFO("BTGATT Client configure mtu complete Callback, conn_id: %d, status: %d, mtu: %d",
1797 conn_id, status, mtu);
1798 event = g_new0(event_gattc_mtu_configured_t, 1);
1799 event->conn_id = conn_id;
1801 event->status = convert_to_oal_status(status);
1802 send_event(OAL_EVENT_GATTC_MTU_EXCHANGE_COMPLETED, event, sizeof(*event));
1805 /************************************ GATT Client ***********************************/
1806 oal_status_t gattc_register(oal_uuid_t* client_uuid)
1809 char str[2*BT_UUID_STRING_MAX];
1810 int ret = OAL_STATUS_SUCCESS;
1812 CHECK_OAL_GATT_ENABLED();
1813 uuid_to_stringname(client_uuid, str);
1814 API_TRACE("uuid: [%s]", str);
1815 /* We will perform actual registration in cb_gattc_register_app callback */
1816 ret = gatt_api->client->register_client((bt_uuid_t *)client_uuid);
1817 if (ret != BT_STATUS_SUCCESS) {
1818 BT_ERR("GATT client register failed: %s", status2string(ret));
1819 return convert_to_oal_status(ret);
1821 return OAL_STATUS_SUCCESS;
1824 oal_status_t gattc_deregister(int client_id)
1826 int ret = OAL_STATUS_SUCCESS;
1828 API_TRACE("GATT client deregister");
1829 CHECK_OAL_GATT_ENABLED();
1830 CHECK_CLIENT_REGISTRATION(client_id);
1832 ret = gatt_api->client->unregister_client(client_id);
1833 if (ret != BT_STATUS_SUCCESS) {
1834 BT_ERR("GATT client deregister failed: %s", status2string(ret));
1835 return convert_to_oal_status(ret);
1838 return OAL_STATUS_SUCCESS;
1841 oal_status_t gattc_connect(int client_id, bt_address_t *device_address, int isDirect)
1843 int ret = OAL_STATUS_SUCCESS;
1846 OAL_CHECK_PARAMETER(device_address, return);
1847 API_TRACE("Client Connect: [%s]", bdt_bd2str(device_address, &bdstr));
1848 CHECK_OAL_GATT_ENABLED();
1849 CHECK_CLIENT_REGISTRATION(client_id);
1851 /* Handle the actual connection in cb_gattc_connection callback */
1852 ret = gatt_api->client->connect(client_id, (bt_bdaddr_t *)device_address, isDirect);
1853 if (ret != BT_STATUS_SUCCESS) {
1854 BT_ERR("GATT client connect failed: %s", status2string(ret));
1855 return convert_to_oal_status(ret);
1857 return OAL_STATUS_SUCCESS;
1860 oal_status_t gattc_disconnect(int client_id, bt_address_t *device_address, int conn_id)
1863 int ret = OAL_STATUS_SUCCESS;
1866 OAL_CHECK_PARAMETER(device_address, return);
1867 API_TRACE("Client Disconnect: [%s]", bdt_bd2str(device_address, &bdstr));
1868 CHECK_OAL_GATT_ENABLED();
1869 CHECK_CLIENT_REGISTRATION(client_id);
1870 CHECK_CLIENT_CONNECTION(conn_id);
1872 /* Handle actual disconnection in callback */
1873 ret = gatt_api->client->disconnect(client_id, (bt_bdaddr_t *) device_address, conn_id);
1874 if (ret != BT_STATUS_SUCCESS) {
1875 BT_ERR("GATT client disconnect failed: %s", status2string(ret));
1876 return convert_to_oal_status(ret);
1878 return OAL_STATUS_SUCCESS;
1882 oal_status_t gattc_search_service(int conn_id, oal_uuid_t *service_uuid)
1884 int ret = OAL_STATUS_SUCCESS;
1885 char uuid_str[2*BT_UUID_STRING_MAX];
1888 uuid_to_stringname(service_uuid, uuid_str);
1889 API_TRACE("Client Service Search UUID: [%s]", uuid_str);
1891 API_TRACE("Client Service Search All");
1893 CHECK_OAL_GATT_ENABLED();
1894 CHECK_CLIENT_CONNECTION(conn_id);
1895 ret = gatt_api->client->search_service(conn_id, (bt_uuid_t *)service_uuid);
1897 if (ret != BT_STATUS_SUCCESS) {
1898 BT_ERR("GATT client service search failed: %s", status2string(ret));
1899 return convert_to_oal_status(ret);
1901 return OAL_STATUS_SUCCESS;
1904 oal_status_t gattc_get_characteristic(int conn_id, oal_gatt_srvc_id_t *srvc_id, oal_gatt_id_t *char_id)
1906 int ret = OAL_STATUS_SUCCESS;
1907 char uuid_str[2*BT_UUID_STRING_MAX];
1909 OAL_CHECK_PARAMETER(srvc_id, return);
1910 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str);
1911 API_TRACE("Client Get Characteristic [%s]", uuid_str);
1912 CHECK_OAL_GATT_ENABLED();
1913 CHECK_CLIENT_CONNECTION(conn_id);
1914 ret = gatt_api->client->get_characteristic(conn_id, (btgatt_srvc_id_t *)srvc_id,
1915 (btgatt_gatt_id_t *)char_id);
1916 if (ret != BT_STATUS_SUCCESS) {
1917 BT_ERR("GATT client get characteristic failed: %s", status2string(ret));
1918 return convert_to_oal_status(ret);
1920 return OAL_STATUS_SUCCESS;
1923 oal_status_t gattc_get_descriptor(int conn_id, oal_gatt_srvc_id_t *srvc_id,
1924 oal_gatt_id_t *char_id, oal_gatt_id_t *desc_id)
1926 int ret = OAL_STATUS_SUCCESS;
1927 char uuid_str1[2*BT_UUID_STRING_MAX];
1928 char uuid_str2[2*BT_UUID_STRING_MAX];
1930 OAL_CHECK_PARAMETER(srvc_id, return);
1931 OAL_CHECK_PARAMETER(char_id, return);
1932 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1933 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1934 API_TRACE("Client Get Descriptor, Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
1935 CHECK_OAL_GATT_ENABLED();
1936 CHECK_CLIENT_CONNECTION(conn_id);
1937 ret = gatt_api->client->get_descriptor(conn_id, (btgatt_srvc_id_t *)srvc_id,
1938 (btgatt_gatt_id_t *)char_id, (btgatt_gatt_id_t *)desc_id);
1939 if (ret != BT_STATUS_SUCCESS) {
1940 BT_ERR("GATT client get descriptor failed: %s", status2string(ret));
1941 return convert_to_oal_status(ret);
1943 return OAL_STATUS_SUCCESS;
1946 oal_status_t gattc_register_for_notification(int conn_id, bt_address_t * address,
1947 oal_gatt_srvc_id_t *srvc_id, oal_gatt_id_t *char_id) {
1948 int ret = OAL_STATUS_SUCCESS;
1949 char uuid_str1[2*BT_UUID_STRING_MAX];
1950 char uuid_str2[2*BT_UUID_STRING_MAX];
1953 OAL_CHECK_PARAMETER(address, return);
1954 OAL_CHECK_PARAMETER(srvc_id, return);
1955 OAL_CHECK_PARAMETER(char_id, return);
1956 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1957 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1958 API_TRACE("Client Register Notification: [%s], Service_uuid: [%s], Char_uuid: [%s]", bdt_bd2str(address, &bdstr), uuid_str1, uuid_str2);
1959 CHECK_OAL_GATT_ENABLED();
1960 CHECK_CLIENT_CONNECTION(conn_id);
1962 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);
1963 if (ret != BT_STATUS_SUCCESS) {
1964 BT_ERR("GATT client register notification failed: %s", status2string(ret));
1965 return convert_to_oal_status(ret);
1968 return OAL_STATUS_SUCCESS;
1971 oal_status_t gattc_deregister_for_notification(int conn_id, bt_address_t * address,
1972 oal_gatt_srvc_id_t *srvc_id, oal_gatt_id_t *char_id) {
1973 int ret = OAL_STATUS_SUCCESS;
1974 char uuid_str1[2*BT_UUID_STRING_MAX];
1975 char uuid_str2[2*BT_UUID_STRING_MAX];
1978 OAL_CHECK_PARAMETER(address, return);
1979 OAL_CHECK_PARAMETER(srvc_id, return);
1980 OAL_CHECK_PARAMETER(char_id, return);
1981 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1982 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1983 API_TRACE("Client Deregister Notification: [%s], Service_uuid: [%s], Char_uuid: [%s]", bdt_bd2str(address, &bdstr), uuid_str1, uuid_str2);
1984 CHECK_OAL_GATT_ENABLED();
1985 CHECK_CLIENT_CONNECTION(conn_id);
1987 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);
1988 if (ret != BT_STATUS_SUCCESS) {
1989 BT_ERR("GATT client deregister notification failed: %s", status2string(ret));
1990 return convert_to_oal_status(ret);
1993 return OAL_STATUS_SUCCESS;
1997 oal_status_t gattc_read_characteristic(int conn_id, oal_gatt_srvc_id_t *srvc_id,
1998 oal_gatt_id_t *char_id, oal_gatt_auth_req_t auth_req)
2000 int ret = OAL_STATUS_SUCCESS;
2001 char uuid_str1[2*BT_UUID_STRING_MAX];
2002 char uuid_str2[2*BT_UUID_STRING_MAX];
2004 OAL_CHECK_PARAMETER(srvc_id, return);
2005 OAL_CHECK_PARAMETER(char_id, return);
2006 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2007 uuid_to_stringname(&(char_id->uuid), uuid_str2);
2008 API_TRACE("Client Read Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
2009 CHECK_OAL_GATT_ENABLED();
2010 CHECK_CLIENT_CONNECTION(conn_id);
2012 ret = gatt_api->client->read_characteristic(conn_id, (btgatt_srvc_id_t *)srvc_id,
2013 (btgatt_gatt_id_t *)char_id, auth_req);
2014 if (ret != BT_STATUS_SUCCESS) {
2015 BT_ERR("GATT client read characteristic failed: %s", status2string(ret));
2016 return convert_to_oal_status(ret);
2019 return OAL_STATUS_SUCCESS;
2022 oal_status_t gattc_read_descriptor(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2023 oal_gatt_id_t *char_id, oal_gatt_id_t *desc_id, oal_gatt_auth_req_t auth_req)
2025 int ret = OAL_STATUS_SUCCESS;
2026 char uuid_str1[2*BT_UUID_STRING_MAX];
2027 char uuid_str2[2*BT_UUID_STRING_MAX];
2028 char uuid_str3[2*BT_UUID_STRING_MAX];
2030 OAL_CHECK_PARAMETER(srvc_id, return);
2031 OAL_CHECK_PARAMETER(char_id, return);
2032 OAL_CHECK_PARAMETER(desc_id, return);
2033 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2034 uuid_to_stringname(&(char_id->uuid), uuid_str2);
2035 uuid_to_stringname(&(desc_id->uuid), uuid_str3);
2036 API_TRACE("Client Read Descriptor: Service_uuid: [%s], Char_uuid: [%s], Desc_uuid: [%s]",
2037 uuid_str1, uuid_str2, uuid_str3);
2038 CHECK_OAL_GATT_ENABLED();
2039 CHECK_CLIENT_CONNECTION(conn_id);
2041 ret = gatt_api->client->read_descriptor(conn_id, (btgatt_srvc_id_t *)srvc_id,
2042 (btgatt_gatt_id_t *)char_id, (btgatt_gatt_id_t *)desc_id, auth_req);
2043 if (ret != BT_STATUS_SUCCESS) {
2044 BT_ERR("GATT client read descriptor failed: %s", status2string(ret));
2045 return convert_to_oal_status(ret);
2048 return OAL_STATUS_SUCCESS;
2051 oal_status_t gattc_acquire_notify(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2052 oal_gatt_id_t *char_id, int *fd, int *mtu)
2054 int ret = OAL_STATUS_SUCCESS;
2055 char uuid_str1[2*BT_UUID_STRING_MAX];
2056 char uuid_str2[2*BT_UUID_STRING_MAX];
2057 OAL_CHECK_PARAMETER(srvc_id, return);
2058 OAL_CHECK_PARAMETER(char_id, return);
2059 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2060 uuid_to_stringname(&(char_id->uuid), uuid_str2);
2061 API_TRACE("Client Write Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
2062 CHECK_OAL_GATT_ENABLED();
2063 CHECK_CLIENT_CONNECTION(conn_id);
2065 ret = gatt_api->client->acquire_notify(conn_id, (btgatt_srvc_id_t *)srvc_id,
2066 (btgatt_gatt_id_t *)char_id, fd, mtu);
2067 if (ret != BT_STATUS_SUCCESS) {
2068 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
2069 return convert_to_oal_status(ret);
2072 return OAL_STATUS_SUCCESS;
2075 oal_status_t gattc_acquire_write(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2076 oal_gatt_id_t *char_id, oal_gatt_auth_req_t auth_req , int *fd, int*mtu)
2078 int ret = OAL_STATUS_SUCCESS;
2079 char uuid_str1[2*BT_UUID_STRING_MAX];
2080 char uuid_str2[2*BT_UUID_STRING_MAX];
2081 OAL_CHECK_PARAMETER(srvc_id, return);
2082 OAL_CHECK_PARAMETER(char_id, return);
2083 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2084 uuid_to_stringname(&(char_id->uuid), uuid_str2);
2085 API_TRACE("Client Write Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
2086 CHECK_OAL_GATT_ENABLED();
2087 CHECK_CLIENT_CONNECTION(conn_id);
2089 ret = gatt_api->client->acquire_write(conn_id, (btgatt_srvc_id_t *)srvc_id,
2090 (btgatt_gatt_id_t *)char_id, auth_req, fd, mtu);
2091 if (ret != BT_STATUS_SUCCESS) {
2092 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
2093 return convert_to_oal_status(ret);
2096 return OAL_STATUS_SUCCESS;
2099 oal_status_t gattc_write_characteristic(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2100 oal_gatt_id_t *char_id, oal_gatt_write_type_t write_type,
2101 int len, oal_gatt_auth_req_t auth_req, char* data)
2103 int ret = OAL_STATUS_SUCCESS;
2104 char uuid_str1[2*BT_UUID_STRING_MAX];
2105 char uuid_str2[2*BT_UUID_STRING_MAX];
2106 OAL_CHECK_PARAMETER(srvc_id, return);
2107 OAL_CHECK_PARAMETER(char_id, return);
2108 OAL_CHECK_PARAMETER(data, return);
2109 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2110 uuid_to_stringname(&(char_id->uuid), uuid_str2);
2111 API_TRACE("Client Write Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
2112 CHECK_OAL_GATT_ENABLED();
2113 CHECK_CLIENT_CONNECTION(conn_id);
2115 ret = gatt_api->client->write_characteristic(conn_id, (btgatt_srvc_id_t *)srvc_id,
2116 (btgatt_gatt_id_t *)char_id, write_type, len, auth_req, data);
2117 if (ret != BT_STATUS_SUCCESS) {
2118 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
2119 return convert_to_oal_status(ret);
2122 return OAL_STATUS_SUCCESS;
2125 oal_status_t gattc_write_descriptor(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2126 oal_gatt_id_t *char_id, oal_gatt_id_t *desc_id,
2127 oal_gatt_write_type_t write_type, int len, oal_gatt_auth_req_t auth_req, char* data)
2129 int ret = OAL_STATUS_SUCCESS;
2130 char uuid_str1[2*BT_UUID_STRING_MAX];
2131 char uuid_str2[2*BT_UUID_STRING_MAX];
2132 char uuid_str3[2*BT_UUID_STRING_MAX];
2133 OAL_CHECK_PARAMETER(srvc_id, return);
2134 OAL_CHECK_PARAMETER(char_id, return);
2135 OAL_CHECK_PARAMETER(desc_id, return);
2136 OAL_CHECK_PARAMETER(data, return);
2137 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2138 uuid_to_stringname(&(char_id->uuid), uuid_str2);
2139 uuid_to_stringname(&(desc_id->uuid), uuid_str3);
2140 API_TRACE("Client Write Descriptor: Service_uuid: [%s], Char_uuid: [%s], Desc_uuid: [%s]",
2141 uuid_str1, uuid_str2, uuid_str3);
2142 CHECK_OAL_GATT_ENABLED();
2143 CHECK_CLIENT_CONNECTION(conn_id);
2145 ret = gatt_api->client->write_descriptor(conn_id, (btgatt_srvc_id_t *)srvc_id,
2146 (btgatt_gatt_id_t *)char_id, (btgatt_gatt_id_t *)desc_id, write_type, len, auth_req, data);
2147 if (ret != BT_STATUS_SUCCESS) {
2148 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
2149 return convert_to_oal_status(ret);
2152 return OAL_STATUS_SUCCESS;
2155 oal_status_t gattc_conn_param_update(bt_address_t * address, int min, int max, int latency, int timeout)
2160 OAL_CHECK_PARAMETER(address, return);
2161 API_TRACE("Conn Param Update: [%s]", bdt_bd2str(address, &bdstr));
2162 CHECK_OAL_GATT_ENABLED();
2164 BT_INFO("[%s] min[%d] max[%d] latency[%d] timeout[%d]", bdt_bd2str(address, &bdstr), min, max, latency, timeout);
2165 ret = gatt_api->client->conn_parameter_update((bt_bdaddr_t *)address, min, max, latency, timeout);
2166 if (ret != BT_STATUS_SUCCESS) {
2167 BT_ERR("error: %s", status2string(ret));
2168 return convert_to_oal_status(ret);
2170 return OAL_STATUS_SUCCESS;
2173 oal_status_t gattc_unregister_scan_filter(int slot_id)
2179 CHECK_OAL_GATT_ENABLED();
2181 BT_INFO("Remove Scan filter Slot ID is: [%d]", slot_id);
2183 ret = gatt_api->client->scan_filter_clear(client_if, slot_id);
2184 if (ret != BT_STATUS_SUCCESS) {
2185 BT_ERR("error: %s", status2string(ret));
2186 return convert_to_oal_status(ret);
2188 return OAL_STATUS_SUCCESS;
2191 oal_status_t gattc_enable_scan_filter(int client_if)
2196 CHECK_OAL_GATT_ENABLED();
2198 BT_INFO("Enable Scan filter. Client If: %d", client_if);
2200 ret = gatt_api->client->scan_filter_enable(client_if, TRUE);
2201 if (ret != BT_STATUS_SUCCESS) {
2202 BT_ERR("error: %s", status2string(ret));
2203 return convert_to_oal_status(ret);
2205 return OAL_STATUS_SUCCESS;
2208 oal_status_t gattc_disable_scan_filter(int client_if)
2213 CHECK_OAL_GATT_ENABLED();
2215 BT_INFO("Disable Scan filter. Client If: %d", client_if);
2217 ret = gatt_api->client->scan_filter_enable(client_if, FALSE);
2218 if (ret != BT_STATUS_SUCCESS) {
2219 BT_ERR("error: %s", status2string(ret));
2220 return convert_to_oal_status(ret);
2222 return OAL_STATUS_SUCCESS;
2225 oal_status_t gattc_register_scan_filter(oal_ble_scan_filter_t* filter_data)
2228 int client_info = 0;
2231 int company_id_mask = 0;
2232 int address_type = 0;
2233 int feature_selection = 0;
2235 oal_ble_scan_filter_param_setup_t scan_filter_setup = {.list_logic_type = 0,
2236 .filt_logic_type = 1,
2237 .rssi_high_thres = -127,
2238 .rssi_low_thres = -127,
2242 .found_timeout_cnt = 0
2245 OAL_CHECK_PARAMETER(filter_data, return);
2247 CHECK_OAL_GATT_ENABLED();
2249 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS){
2251 BT_INFO("OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS is being added");
2252 BT_INFO("BT remote Device address is %s", bdt_bd2str(filter_data->device_address, &bdstr));
2253 ret = gatt_api->client->scan_filter_add_remove(client_info,
2255 OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS,
2256 filter_data->slot_id,
2261 (bt_bdaddr_t*)filter_data->device_address,
2268 if (ret != BT_STATUS_SUCCESS){
2269 BT_ERR("error: %s", status2string(ret));
2270 BT_INFO("unregistering already set filter features.");
2271 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2272 return convert_to_oal_status(ret);
2275 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS;
2277 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_NAME){
2278 ret = gatt_api->client->scan_filter_add_remove(client_info,
2280 OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_NAME,
2281 filter_data->slot_id,
2289 filter_data->device_name, // device_name as p_data in HAL
2293 if (ret != BT_STATUS_SUCCESS){
2294 BT_ERR("error: %s", status2string(ret));
2295 BT_INFO("unregistering already set filter features.");
2296 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2297 return convert_to_oal_status(ret);
2300 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_NAME;
2302 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_UUID){
2303 BT_INFO("OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_UUID is being added");
2304 char uuid_str1[2*BT_UUID_STRING_MAX];
2305 char uuid_str2[2*BT_UUID_STRING_MAX];
2307 uuid_to_stringname((service_uuid_t*)filter_data->service_uuid, uuid_str1);
2308 uuid_to_stringname((service_uuid_t*)filter_data->service_uuid_mask, uuid_str2);
2310 BT_INFO("Service UUID is [%s] and Service UUID mask is [%s]", uuid_str1, uuid_str2);
2311 ret = gatt_api->client->scan_filter_add_remove(client_info,
2313 OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_UUID,
2314 filter_data->slot_id,
2317 (bt_uuid_t*)filter_data->service_uuid,
2318 (bt_uuid_t*)filter_data->service_uuid_mask,
2321 filter_data->service_uuid_len, // service_uuid_len as data_len in HAL
2323 filter_data->service_uuid_mask_len,
2326 if (ret != BT_STATUS_SUCCESS){
2327 BT_ERR("error: %s", status2string(ret));
2328 BT_INFO("unregistering already set filter features.");
2329 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2330 return convert_to_oal_status(ret);
2333 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_UUID;
2335 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID){
2336 BT_INFO("OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID is being added");
2337 char uuid_str1[2*BT_UUID_STRING_MAX];
2338 char uuid_str2[2*BT_UUID_STRING_MAX];
2339 uuid_to_stringname((service_uuid_t*)filter_data->service_solicitation_uuid, uuid_str1);
2340 uuid_to_stringname((service_uuid_t*)filter_data->service_solicitation_uuid_mask, uuid_str2);
2341 BT_INFO("Service Solicitation UUID is [%s] and Service Solicitation UUID mask is [%s]", uuid_str1, uuid_str2);
2342 ret = gatt_api->client->scan_filter_add_remove(client_info,
2344 OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID,
2345 filter_data->slot_id,
2348 (bt_uuid_t*)filter_data->service_solicitation_uuid,
2349 (bt_uuid_t*)filter_data->service_solicitation_uuid_mask,
2352 filter_data->service_solicitation_uuid_len, // service_solicitation_uuid_len as data_len in HAL
2354 filter_data->service_solicitation_uuid_mask_len,
2357 if (ret != BT_STATUS_SUCCESS){
2358 BT_ERR("error: %s", status2string(ret));
2359 BT_INFO("unregistering already set filter features.");
2360 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2361 return convert_to_oal_status(ret);
2364 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID;
2366 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_DATA){
2367 ret = gatt_api->client->scan_filter_add_remove(client_info,
2369 OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_DATA,
2370 filter_data->slot_id,
2377 filter_data->service_data_len, //service_data_len as data_len in HAL
2378 (char*)filter_data->service_data,
2379 filter_data->service_data_mask_len,
2380 (char*)filter_data->service_data_mask
2382 if (ret != BT_STATUS_SUCCESS){
2383 BT_ERR("error: %s", status2string(ret));
2384 BT_INFO("unregistering already set filter features.");
2385 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2386 return convert_to_oal_status(ret);
2389 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_DATA;
2391 if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA){
2392 ret = gatt_api->client->scan_filter_add_remove(client_info,
2394 OAL_BLE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA,
2395 filter_data->slot_id,
2396 filter_data->manufacturer_id,
2402 filter_data->manufacturer_data_len, //manufacturer_data_len as data_len in HAL
2403 (char*)filter_data->manufacturer_data,
2404 filter_data->manufacturer_data_mask_len,
2405 (char*)filter_data->manufacturer_data_mask
2407 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA;
2408 if (ret != BT_STATUS_SUCCESS){
2409 BT_ERR("error: %s", status2string(ret));
2410 BT_INFO("unregistering already set filter features.");
2411 gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2412 return convert_to_oal_status(ret);
2415 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA;
2418 BT_DBG("Filter selection 0x%.2x", feature_selection);
2420 ret = gatt_api->client->scan_filter_param_setup(client_info, action, filter_data->slot_id, feature_selection,
2421 scan_filter_setup.list_logic_type, scan_filter_setup.filt_logic_type,
2422 scan_filter_setup.rssi_high_thres, scan_filter_setup.rssi_low_thres,
2423 scan_filter_setup.dely_mode, scan_filter_setup.found_timeout,
2424 scan_filter_setup.lost_timeout, scan_filter_setup.found_timeout_cnt);
2425 if (ret != BT_STATUS_SUCCESS){
2426 BT_ERR("error: %s", status2string(ret));
2427 return convert_to_oal_status(ret);
2429 return OAL_STATUS_SUCCESS;
2432 oal_status_t gattc_configure_mtu(int conn_id, int mtu)
2436 API_TRACE("Configure MTU Size: [%d]", mtu);
2437 CHECK_OAL_GATT_ENABLED();
2438 CHECK_CLIENT_CONNECTION(conn_id);
2440 /* To prevent crash in case other libraries not support this api */
2441 if (gatt_api->client->configure_mtu == NULL) {
2442 BT_WARN("configure_mtu is NULL");
2443 return OAL_STATUS_NOT_SUPPORT;
2446 ret = gatt_api->client->configure_mtu(conn_id, mtu);
2447 if (ret != BT_STATUS_SUCCESS) {
2448 BT_ERR("Gatt client configure_mtu error: %s", status2string(ret));
2449 return convert_to_oal_status(ret);
2451 return OAL_STATUS_SUCCESS;
2454 oal_status_t gattc_add_connection_info(bt_address_t *device_address, int conn_id, int instance_id)
2456 int ret = OAL_STATUS_SUCCESS;
2459 OAL_CHECK_PARAMETER(device_address, return);
2460 API_TRACE("Add connection info: [%s]", bdt_bd2str(device_address, &bdstr));
2461 CHECK_OAL_GATT_ENABLED();
2462 CHECK_SERVER_INSTANCE(instance_id);
2463 if (__gatts_find_server_instance(instance_id) == NULL)
2464 return OAL_STATUS_INTERNAL_ERROR;
2466 ret = gatt_api->client->add_connection_info((bt_bdaddr_t *)device_address, conn_id, instance_id);
2467 if (ret != BT_STATUS_SUCCESS) {
2468 BT_ERR("GATT connection info add failed: %s", status2string(ret));
2469 return convert_to_oal_status(ret);
2471 return OAL_STATUS_SUCCESS;