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_indication_confirmation(int conn_id, int trans_id, int attr_handle, bt_bdaddr_t *bda);
156 static void cb_gatts_connection(int conn_id, int server_if, int connected, bt_bdaddr_t *bda);
158 static void cb_gatts_mtu_changed(int conn_id, int mtu);
161 static void cb_notifcation_changed(int conn_id, int trans_id, int attr_handle, bool notify, bt_bdaddr_t *bda);
164 /************************************HAL Interface *************************************/
166 /*TODO GATT Server callbacks will be implemented in subsequent patches */
167 static const btgatt_server_callbacks_t btgatt_server_callbacks = {
168 cb_gatts_register_app,
170 cb_gatts_service_added,
171 cb_gatts_included_service_added,
172 cb_gatts_characteristic_added,
173 cb_gatts_descriptor_added,
174 cb_gatts_service_started,
175 cb_gatts_service_stopped,
176 cb_gatts_service_deleted,
177 cb_indication_confirmation,
178 cb_gatts_request_read,
179 cb_gatts_request_write,
180 NULL, /*cb_gatts_request_exec_write,*/
181 cb_gatts_response_confirmation,
183 cb_gatts_multi_adv_enable,
184 cb_gatts_multi_adv_update,
185 cb_gatts_multi_adv_set_inst_data,
186 cb_gatts_multi_adv_disable,
187 cb_gatts_mtu_changed,
189 cb_notifcation_changed,
193 /* Forward declaration for GATT client callbacks */
194 static void cb_gattc_register_app(int status, int clientIf, bt_uuid_t *app_uuid);
195 static void cb_gattc_scan_result(bt_bdaddr_t* bdaddress, int rssi, uint8_t *adv_data);
196 static void cb_gattc_connection(int conn_id, int status, int client_if, bt_bdaddr_t* bda);
197 static void cb_gattc_disconnect(int conn_id, int status, int client_if, bt_bdaddr_t* bda);
198 static void cb_gattc_search_result(int conn_id, btgatt_srvc_id_t *srvc_id);
199 static void cb_gattc_search_complete(int conn_id, int status);
200 static void cb_gattc_get_characteristics(int conn_id, int status, btgatt_srvc_id_t *srvc_id,
201 btgatt_gatt_id_t *char_id, int char_prop);
202 static void cb_gattc_get_descriptor(int conn_id, int status, btgatt_srvc_id_t *srvc_id,
203 btgatt_gatt_id_t *char_id, btgatt_gatt_id_t *descr_id);
204 static void cb_gattc_read_characteristic(int conn_id, int status, btgatt_read_params_t *p_data);
205 static void cb_gattc_read_descriptor(int conn_id, int status, btgatt_read_params_t *p_data);
206 static void cb_gattc_write_characteristic(int conn_id, int status, btgatt_write_params_t *p_data);
207 static void cb_gattc_write_descriptor(int conn_id, int status, btgatt_write_params_t *p_data);
208 static void cb_gattc_register_for_notification(int conn_id, int registered, int status,
209 btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id);
211 /*TODO GATT CLient callbacks will be implemented in subsequent patches */
212 static const btgatt_client_callbacks_t btgatt_client_callbacks = {
213 cb_gattc_register_app,
214 cb_gattc_scan_result,
215 cb_gattc_connection, /*cb_gattc_connection*/
216 cb_gattc_disconnect, /*cb_gattc_disconnect*/
217 cb_gattc_search_complete, /*cb_gattc_search_complete*/
218 cb_gattc_search_result, /*cb_gattc_search_result*/
219 cb_gattc_get_characteristics, /*cb_gattc_get_characteristics*/
220 cb_gattc_get_descriptor, /*cb_gattc_get_descriptor*/
221 NULL, /*cb_gattc_get_included_service*/
222 cb_gattc_register_for_notification, /*cb_gattc_register_for_notification*/
223 NULL, /*cb_gattc_notify*/
224 cb_gattc_read_characteristic, /*cb_gattc_read_characteristic*/
225 cb_gattc_write_characteristic, /*cb_gattc_write_characteristic*/
226 cb_gattc_read_descriptor, /*cb_gattc_read_descriptor*/
227 cb_gattc_write_descriptor, /*cb_gattc_write_descriptor*/
228 NULL, /*cb_gattc_execute_write*/
229 NULL, /*cb_gattc_remote_rssi*/
230 NULL, /*cb_gattc_configure_mtu_cmpl*/
231 #ifdef PLATFORM_ANDROID_HAL
232 NULL, /*cb_scan_filter_cfg*/
233 NULL, /*cb_scan_filter_param*/
234 NULL, /*cb_scan_filter_status*/
235 NULL, /*cb_congestion*/
236 NULL, /*cb_batchscan_cfg_storag*/
237 NULL, /*cb_batchscan_enb_disable*/
238 NULL, /*cb_batchscan_reports*/
239 NULL, /*cb_batchscan_threshold*/
240 NULL, /*cb_track_adv_event*/
244 static btgatt_callbacks_t btgatt_callbacks = {
245 sizeof(btgatt_callbacks_t),
246 &btgatt_client_callbacks,
247 &btgatt_server_callbacks
250 /*******************************GATT Initialisation - Deinitialisation********************************/
251 oal_status_t gatt_enable(void)
253 const bt_interface_t * blued_api;
257 for (i = 0; i < NUM_SERVER_INST; i++) {
258 gatt_servers[i].server_id = -1;
259 gatt_servers[i].state = GATT_INS_DISABLED;
260 memset(&gatt_servers[i].uuid, 0, sizeof(oal_uuid_t));
261 cur_adv_state[i] = FALSE;
264 /* Get stack interface */
265 blued_api = (const bt_interface_t *) adapter_get_stack_interface();
267 if (blued_api == NULL) {
268 BT_ERR("Stack is not initialized");
269 return OAL_STATUS_NOT_READY;
273 BT_WARN("GATT Interface is already initialized...");
274 return OAL_STATUS_ALREADY_DONE;
277 gatt_api = (const btgatt_interface_t *)blued_api->get_profile_interface(BT_PROFILE_GATT_ID);
278 if (gatt_api == NULL) {
279 BT_ERR("GATT interface failed");
280 return OAL_STATUS_INTERNAL_ERROR;
283 if ((ret = gatt_api->init(&btgatt_callbacks)) != BT_STATUS_SUCCESS) {
284 BT_ERR("Error: Unable to initialise GATT :%s", status2string(ret));
287 return convert_to_oal_status(ret);
290 BT_INFO("GATT successfully initialized");
291 return OAL_STATUS_SUCCESS;
294 oal_status_t gatt_disable(void)
302 for (i = 0; i < NUM_SERVER_INST; i++) {
303 gatt_servers[i].server_id = -1;
304 gatt_servers[i].state = GATT_INS_DISABLED;
305 memset(&gatt_servers[i].uuid, 0, sizeof(oal_uuid_t));
307 return OAL_STATUS_SUCCESS;
310 /************************************GATT Server Functions*************************************/
312 oal_status_t gatts_register(oal_uuid_t* server_uuid)
314 char str[2*BT_UUID_STRING_MAX];
315 int ret = OAL_STATUS_SUCCESS;
318 CHECK_OAL_GATT_ENABLED();
319 uuid_to_stringname((service_uuid_t*)server_uuid->uuid, str);
320 API_TRACE("Register the server instance: UUID: [%s]", str);
322 for (i = 0; i < NUM_SERVER_INST; i++) {
323 if (memcmp(server_uuid->uuid, gatt_servers[i].uuid.uuid, sizeof(oal_uuid_t)) == 0) {
324 if (gatt_servers[i].server_id != -1) {
325 BT_ERR("This is resevered UUID for easy set up application i = %d \
326 gatt_servers[i].server_id = %d", i, gatt_servers[i].server_id);
327 return OAL_STATUS_ALREADY_DONE;
332 ret = gatt_api->server->register_server((bt_uuid_t*)server_uuid);
334 if (ret != BT_STATUS_SUCCESS) {
335 BT_ERR("GATT server registration with UUID:%s failed: %s", str, status2string(ret));
336 return convert_to_oal_status(ret);
338 return OAL_STATUS_SUCCESS;
341 oal_status_t gatts_unregister(int instance_id)
343 int ret = OAL_STATUS_SUCCESS;
344 API_TRACE("Unregister the registered server, instance_id: %d", instance_id);
346 CHECK_OAL_GATT_ENABLED();
347 CHECK_SERVER_INSTANCE(instance_id);
348 CHECK_SERVER_REGISTRATION(instance_id);
350 ret = gatt_api->server->unregister_server(gatt_servers[instance_id - 1].server_id);
352 if (ret != BT_STATUS_SUCCESS) {
353 BT_ERR("GATT server unregistration failed: %d", instance_id);
354 return convert_to_oal_status(ret);
357 gatt_servers[instance_id-1].server_id = -1;
358 gatt_servers[instance_id-1].state = GATT_INS_DISABLED;
360 if (instance_id != 1 && instance_id != 2)
361 memset(&gatt_servers[instance_id-1].uuid, 0, sizeof(oal_uuid_t));
363 return OAL_STATUS_SUCCESS;
366 /* Legacy Advertisement */
367 oal_status_t gatts_start_listen(int instance_id, gboolean enable)
369 int ret = OAL_STATUS_SUCCESS;
370 API_TRACE("server-id:%d enable: %d", gatt_servers[instance_id-1].server_id, enable);
372 CHECK_OAL_GATT_ENABLED();
373 CHECK_SERVER_INSTANCE(instance_id);
374 CHECK_SERVER_REGISTRATION(instance_id);
376 ret = gatt_api->server->listen(gatt_servers[instance_id-1].server_id, enable);
378 if (ret != BT_STATUS_SUCCESS) {
379 BT_ERR("Error:Advertising start/stop on server-id:%d failed With Status: [%s]",
380 gatt_servers[instance_id-1].server_id, status2string(ret));
381 return convert_to_oal_status(ret);
384 return OAL_STATUS_SUCCESS;
387 oal_status_t gatts_stop_advertising(int instance_id)
389 int ret = OAL_STATUS_SUCCESS;
390 API_TRACE("Stop advertising");
392 CHECK_OAL_GATT_ENABLED();
393 CHECK_SERVER_INSTANCE(instance_id);
394 CHECK_SERVER_REGISTRATION(instance_id);
396 ret = gatt_api->server->listen(gatt_servers[instance_id - 1].server_id, FALSE);
398 if (ret != BT_STATUS_SUCCESS) {
399 BT_ERR("Stop Advertising failed for server Instance:%d With Status: %s",
400 instance_id, status2string(ret));
401 return convert_to_oal_status(ret);
403 return OAL_STATUS_SUCCESS;
407 /* Below API's need to be removed as they are hardcoded API's.
408 If at all, product specific API's are required to set specific data, then new naming should
409 be adopted for these API's */
410 oal_status_t gatts_set_advertising_params(int instance_id, float min_intv, float max_intv)
412 int ret = OAL_STATUS_SUCCESS;
414 API_TRACE("Set advertising params min_int:%f, max_int:%f", min_intv, max_intv);
415 CHECK_OAL_GATT_ENABLED();
416 CHECK_SERVER_INSTANCE(instance_id);
417 CHECK_SERVER_REGISTRATION(instance_id);
419 /* Historically used for Legacy Advertising */
420 return OAL_STATUS_NOT_SUPPORT;
423 oal_status_t gatts_set_advertising_data(int instance_id, uint16_t manufac_len, char* manufac_data)
425 int ret = OAL_STATUS_SUCCESS;
427 API_TRACE("Set advertising data");
428 CHECK_OAL_GATT_ENABLED();
429 CHECK_SERVER_INSTANCE(instance_id);
430 CHECK_SERVER_REGISTRATION(instance_id);
432 /* Historically used for Legacy Advertising */
433 return OAL_STATUS_NOT_SUPPORT;
436 oal_status_t gatts_set_scan_response(int instance_id, uint16_t manufac_len, char* manufac_data)
438 int ret = OAL_STATUS_SUCCESS;
440 API_TRACE("Set Scan Response data");
441 CHECK_OAL_GATT_ENABLED();
442 CHECK_SERVER_INSTANCE(instance_id);
443 CHECK_SERVER_REGISTRATION(instance_id);
445 /* Historically used for Legacy Advertising */
446 return OAL_STATUS_NOT_SUPPORT;
450 oal_status_t gatts_multi_adv_enable(int instance_id)
452 int ret = OAL_STATUS_SUCCESS;
453 API_TRACE("Start Multi advertising, srv_if: %d, instance_id: %d",
454 gatt_servers[instance_id - 1].server_id, instance_id);
456 CHECK_OAL_GATT_ENABLED();
457 CHECK_SERVER_INSTANCE(instance_id);
458 CHECK_SERVER_REGISTRATION(instance_id);
460 if (gatt_api->server->multi_adv_enable == NULL)
461 return OAL_STATUS_NOT_SUPPORT;
463 /* Platform Requirement: Advertising to be enabled even when no data is set: i.e Empty Advertisement
464 if (gatt_servers[instance_id - 1].state != GATT_INS_DATA_SET){
465 BT_ERR("Not allowed, state: %d, instance_id: %d",
466 gatt_servers[instance_id - 1].state, instance_id);
467 return OAL_STATUS_BUSY;
470 if (gatt_servers[instance_id - 1].state == GATT_INS_ENABLED) {
471 BT_ERR("Not allowed, state: %d, instance_id: %d",
472 gatt_servers[instance_id - 1].state, instance_id);
473 return OAL_STATUS_ALREADY_DONE;
476 if (gatt_servers[instance_id - 1].state == GATT_INS_ENABLING) {
477 BT_ERR("Not allowed, state: %d, instance_id: %d",
478 gatt_servers[instance_id - 1].state, instance_id);
479 return OAL_STATUS_BUSY;
482 gatt_servers[instance_id - 1].state = GATT_INS_ENABLING;
484 ret = gatt_api->server->multi_adv_enable(gatt_servers[instance_id - 1].server_id);
486 if (ret != BT_STATUS_SUCCESS) {
487 BT_ERR("GATT enable multi advertising failed for server Instance: %d With Status: %s",
488 instance_id, status2string(ret));
489 // gatt_servers[instance_id - 1].state = GATT_INS_DATA_SET;
490 return convert_to_oal_status(ret);
492 return OAL_STATUS_SUCCESS;
495 oal_status_t gatts_multi_adv_disable(int instance_id)
497 int ret = OAL_STATUS_SUCCESS;
498 API_TRACE("Stop Multi advertising, srv_if: %d, instance_id: %d",
499 gatt_servers[instance_id - 1].server_id, instance_id);
501 CHECK_OAL_GATT_ENABLED();
502 CHECK_SERVER_INSTANCE(instance_id);
503 CHECK_SERVER_REGISTRATION(instance_id);
505 if (gatt_api->server->multi_adv_disable == NULL)
506 return OAL_STATUS_NOT_SUPPORT;
508 if ((gatt_servers[instance_id - 1].state != GATT_INS_ENABLED)) {
509 BT_ERR("Not Allowed, state: %d, instance_id: %d",
510 gatt_servers[instance_id - 1].state, instance_id);
511 return OAL_STATUS_BUSY;
514 ret = gatt_api->server->multi_adv_disable(gatt_servers[instance_id - 1].server_id);
516 if (ret != BT_STATUS_SUCCESS) {
517 BT_ERR("GATT disable multi advertising failed for server Instance: %d With Status: %s",
518 instance_id, status2string(ret));
519 return convert_to_oal_status(ret);
521 return OAL_STATUS_SUCCESS;
524 oal_status_t gatts_multi_adv_update(int instance_id,
525 int min_intv, int max_intv,
526 int adv_type, int chnl_map,
527 int tx_power, int timeout_s)
529 int ret = OAL_STATUS_SUCCESS;
530 API_TRACE("Multi advertising Update");
532 CHECK_OAL_GATT_ENABLED();
533 CHECK_SERVER_INSTANCE(instance_id);
534 CHECK_SERVER_REGISTRATION(instance_id);
536 if (gatt_api->server->multi_adv_update == NULL)
537 return OAL_STATUS_NOT_SUPPORT;
539 ret = gatt_api->server->multi_adv_update(gatt_servers[instance_id - 1].server_id,
542 tx_power, timeout_s);
544 if (ret != BT_STATUS_SUCCESS) {
545 BT_ERR("GATT update multi advertising failed for server Instance: %d With Status: %s",
546 instance_id, status2string(ret));
547 return convert_to_oal_status(ret);
549 return OAL_STATUS_SUCCESS;
552 oal_status_t gatts_multi_adv_set_inst_data(int instance_id,
553 oal_ble_multi_adv_param_setup_t *adv_param_setup)
555 int ret = OAL_STATUS_SUCCESS;
556 btgatt_adv_param_setup_t adv_setup;
557 API_TRACE("Set Multi advertising data, srv_if: %d, instance_id: %d",
558 gatt_servers[instance_id - 1].server_id, instance_id);
560 CHECK_OAL_GATT_ENABLED();
561 CHECK_SERVER_INSTANCE(instance_id);
562 CHECK_SERVER_REGISTRATION(instance_id);
564 if (gatt_api->server->multi_adv_set_inst_data == NULL)
565 return OAL_STATUS_NOT_SUPPORT;
567 if ((gatt_servers[instance_id - 1].state >= GATT_INS_DATA_SETTING) && (gatt_servers[instance_id - 1].state != GATT_INS_DATA_SET)) {
568 BT_ERR("Not Allowed, state: %d, instance_id: %d", gatt_servers[instance_id - 1].state, instance_id);
569 return OAL_STATUS_BUSY;
572 gatt_servers[instance_id - 1].state = GATT_INS_DATA_SETTING;
575 adv_setup.set_scan_rsp = adv_param_setup->set_scan_rsp;
576 adv_setup.include_name = adv_param_setup->include_name;
577 adv_setup.include_txpower = adv_param_setup->include_txpower;
578 adv_setup.appearance = adv_param_setup->appearance;
579 adv_setup.manufacturer_data = adv_param_setup->manufacturer_data;
580 adv_setup.manufacturer_data_len = adv_param_setup->manufacturer_data_len;
581 adv_setup.service_data = adv_param_setup->service_data;
582 adv_setup.service_data_len = adv_param_setup->service_data_len;
583 adv_setup.service_uuid = adv_param_setup->service_uuid;
584 adv_setup.service_uuid_len = adv_param_setup->service_uuid_len;
585 /* Solicit UUID handler: Start */
586 adv_setup.solicit_uuid = adv_param_setup->solicit_uuid;
587 adv_setup.solicit_uuid_len = adv_param_setup->solicit_uuid_len;
588 /* Solicit UUID handler: End */
589 adv_setup.min_interval = adv_param_setup->min_interval;
590 adv_setup.max_interval = adv_param_setup->min_interval;
591 adv_setup.adv_type = adv_param_setup->adv_type;
592 adv_setup.chnl_map = adv_param_setup->chnl_map;
593 adv_setup.tx_power = adv_param_setup->tx_power;
594 adv_setup.timeout_s = adv_param_setup->timeout_s;
596 adv_setup.server_if = gatt_servers[instance_id - 1].server_id;
597 if (adv_setup.manufacturer_data_len > 0 && adv_setup.manufacturer_data != NULL) {
598 BT_INFO("Manufacture data.....\n");
599 PRINT_ADV_DATA(adv_setup.manufacturer_data_len, (unsigned char *)adv_setup.manufacturer_data, adv_setup.set_scan_rsp);
602 if (adv_setup.service_uuid_len > 0 && adv_setup.service_uuid != NULL) {
603 BT_INFO("Service uuid.....\n");
604 PRINT_ADV_DATA(adv_setup.service_uuid_len, (unsigned char*)adv_setup.service_uuid, FALSE);
608 ret = gatt_api->server->multi_adv_set_inst_data(adv_setup);
610 if (ret != BT_STATUS_SUCCESS) {
611 BT_ERR("GATT Set Multi advertising data failed: %s", status2string(ret));
612 gatt_servers[instance_id - 1].state = GATT_INS_DISABLED;
613 return convert_to_oal_status(ret);
615 return OAL_STATUS_SUCCESS;
618 /************************************GATT Server Functions*************************************/
620 static void cb_gatts_register_app(int status, int server_if, bt_uuid_t *uuid)
623 char str[2*BT_UUID_STRING_MAX];
624 uuid_to_stringname((service_uuid_t*)uuid, str);
626 event_gatts_register_t *event = g_malloc0(sizeof(event_gatts_register_t));
628 BT_INFO("BTGATT SERVER REGISTER APP CB, status:%d, server_if:%d", status, server_if);
629 BT_INFO("UUID: [%s]", str);
631 for (i = 0; i < NUM_SERVER_INST; i++) {
632 if (gatt_servers[i].server_id == -1) {
633 BT_INFO("Server Instance registered with stack [%d]", i+1);
634 gatt_servers[i].server_id = server_if;
635 event->server_inst = i+1;
636 memcpy(gatt_servers[i].uuid.uuid, uuid->uu, sizeof(bt_uuid_t));
637 memcpy(event->server_uuid.uuid, uuid->uu, sizeof(bt_uuid_t));
641 send_event(OAL_EVENT_BLE_SERVER_INSTANCE_INITIALISED, event, sizeof(event_gatts_register_t));
644 static void cb_gatts_listen(int status, int server_if)
646 gboolean prev_state[NUM_SERVER_INST];
648 event_ble_adv_status *event = g_malloc0(sizeof(event_ble_adv_status));
651 for (instance_id = 1 ; instance_id <= NUM_SERVER_INST ; instance_id++) {
652 if (gatt_servers[instance_id - 1].server_id == server_if) {
653 event->server_inst = instance_id;
656 BT_WARN("Server ID:%d recevied server Id:%d\n", gatt_servers[instance_id - 1].server_id, server_if);
659 if ((event->server_inst > 0) && (event->server_inst <= 10)) {
660 prev_state[event->server_inst - 1] = cur_adv_state[event->server_inst - 1];
661 new_state = (status == BT_STATUS_SUCCESS) ? !(prev_state[event->server_inst - 1]) : (prev_state[event->server_inst - 1]);
662 cur_adv_state[event->server_inst - 1] = new_state;
664 BT_ERR("Invalid Callback...");
669 BT_INFO("Adv State of server instance %d :%d", instance_id, new_state);
671 if (TRUE == new_state)
672 send_event(OAL_EVENT_BLE_ADVERTISING_STARTED, event, sizeof(event_ble_adv_status));
674 send_event(OAL_EVENT_BLE_ADVERTISING_STOPPED, event, sizeof(event_ble_adv_status));
677 static void cb_gatts_multi_adv_enable(int server_if, int status)
680 BT_INFO("BTGATT SERVER MULTI ADV ENABLE CB, status:%d, srv_if: %d", status, server_if);
681 event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
684 if (status != BT_STATUS_SUCCESS)
685 BT_ERR("MULTI ADV ENABLE failed, status:%d, srv_if: %d", status, server_if);
687 for (instance_id = 1 ; instance_id <= NUM_SERVER_INST ; instance_id++) {
688 if (gatt_servers[instance_id - 1].server_id == server_if) {
689 event->server_inst = instance_id;
690 gatt_servers[instance_id - 1].state = GATT_INS_ENABLED;
694 BT_ERR("Invalid Interface, srv_if: %d", server_if);
695 event->server_inst = -1;
697 event->status = convert_to_oal_status(status);
698 send_event(OAL_EVENT_BLE_MULTI_ADVERTISING_ENABLE, event, sizeof(event_ble_multiadv_status));
701 static void cb_gatts_multi_adv_disable(int server_if, int status)
703 BT_INFO("BTGATT SERVER MULTI ADV DISABLE CB, status:%d, srv_if: %d", status, server_if);
704 event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
707 if (status != BT_STATUS_SUCCESS)
708 BT_ERR("MULTI ADV DISABLE failed, status:%d, srv_if: %d", status, server_if);
710 for (instance_id = 1 ; instance_id <= NUM_SERVER_INST; instance_id++) {
711 if (gatt_servers[instance_id - 1].server_id == server_if) {
712 event->server_inst = instance_id;
713 gatt_servers[instance_id - 1].state = GATT_INS_DISABLED;
717 BT_ERR("Invalid Interface, srv_if: %d", server_if);
718 event->server_inst = -1;
720 event->status = convert_to_oal_status(status);
721 send_event(OAL_EVENT_BLE_MULTI_ADVERTISING_DISABLE, event, sizeof(event_ble_multiadv_status));
724 static void cb_gatts_multi_adv_update(int server_if, int status)
726 BT_INFO("BTGATT SERVER MULTI ADV UPDATE CB, status:%d, srv_if: %d", status, server_if);
727 event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
730 if (status != BT_STATUS_SUCCESS)
731 BT_ERR("MULTI ADV UPDATE failed, status:%d, srv_if: %d", status, server_if);
733 for (instance_id = 1 ; instance_id <= NUM_SERVER_INST ; instance_id++) {
734 if (gatt_servers[instance_id - 1].server_id == server_if) {
735 event->server_inst = instance_id;
739 BT_ERR("Invalid Interface, srv_if: %d", server_if);
740 event->server_inst = -1;
742 event->status = convert_to_oal_status(status);
743 send_event(OAL_EVENT_BLE_MULTI_ADVERTISING_UPDATE, event, sizeof(event_ble_multiadv_status));
746 static void cb_gatts_multi_adv_set_inst_data(int server_if, int status)
748 BT_INFO("BTGATT SERVER MULTI ADV SET INST DATA CB, status:%d, srv_if: %d", status, server_if);
749 event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
752 if (status != BT_STATUS_SUCCESS)
753 BT_ERR("MULTI ADV SET DATA failed, status:%d, srv_if: %d", status, server_if);
755 for (instance_id = 1 ; instance_id <= NUM_SERVER_INST ; instance_id++) {
756 if (gatt_servers[instance_id - 1].server_id == server_if) {
757 event->server_inst = instance_id;
758 gatt_servers[instance_id - 1].state = GATT_INS_DATA_SET;
762 BT_ERR("Invalid Interface, srv_if: %d", server_if);
763 event->server_inst = -1;
765 event->status = convert_to_oal_status(status);
766 send_event(OAL_EVENT_BLE_MULTI_ADVERTISING_SET_INST_DATA, event, sizeof(event_ble_multiadv_status));
769 oal_status_t gatts_get_att_mtu(int conn_id, int *mtu)
771 int ret = OAL_STATUS_SUCCESS;
772 API_TRACE("Get ATT MTU, conn_id: %d", conn_id);
773 CHECK_OAL_GATT_ENABLED();
774 OAL_CHECK_PARAMETER(mtu, return);
776 /* To prevent crash in case other libraries not support this api */
777 if (gatt_api->server->get_att_mtu == NULL) {
778 BT_WARN("get_att_mtu is NULL");
779 return OAL_STATUS_NOT_SUPPORT;
782 ret = gatt_api->server->get_att_mtu(conn_id, mtu);
783 if (ret != BT_STATUS_SUCCESS) {
784 BT_ERR("GATT MTU Size failed, status: %d", status2string(ret));
785 return convert_to_oal_status(ret);
788 BT_INFO("Current ATT MTU Size: %d", *mtu);
789 return OAL_STATUS_SUCCESS;
792 oal_status_t gatts_add_service(int instance_id, oal_gatt_srvc_id_t *gatt_serv_id,
795 int ret = OAL_STATUS_SUCCESS;
796 btgatt_srvc_id_t btgatt_srvc_id;
797 char str[2*BT_UUID_STRING_MAX];
799 API_TRACE("Server Add Service : instance_id = %d, num_handles = %d", instance_id, num_handles);
800 CHECK_OAL_GATT_ENABLED();
802 CHECK_SERVER_INSTANCE(instance_id);
803 CHECK_SERVER_REGISTRATION(instance_id);
805 if (gatt_serv_id != NULL) {
806 uuid_to_stringname(&(gatt_serv_id->id.uuid), str);
807 API_TRACE("Service uuid: [%s]", str);
808 memcpy(btgatt_srvc_id.id.uuid.uu, gatt_serv_id->id.uuid.uuid, sizeof(gatt_serv_id->id.uuid));
809 btgatt_srvc_id.id.inst_id = gatt_serv_id->id.inst_id;
810 btgatt_srvc_id.is_primary = gatt_serv_id->is_prmry;
812 BT_INFO("GATT Server Service Id is NULL");
813 return OAL_STATUS_INVALID_PARAM;
816 ret = gatt_api->server->add_service(gatt_servers[instance_id - 1].server_id, &btgatt_srvc_id, num_handles);
818 if (ret != BT_STATUS_SUCCESS) {
819 BT_ERR("GATT Server add service failed: %s", status2string(ret));
820 return convert_to_oal_status(ret);
822 return OAL_STATUS_SUCCESS;
825 oal_status_t gatts_add_included_services(int instance_id, int serv_handle,
828 int ret = OAL_STATUS_SUCCESS;
829 API_TRACE("Server Add included Service : instance_id = %d, serv_handle = %d, incl_handle = %d",
830 instance_id, serv_handle, incl_handle);
831 CHECK_OAL_GATT_ENABLED();
833 CHECK_SERVER_INSTANCE(instance_id);
835 CHECK_SERVER_REGISTRATION(instance_id);
837 ret = gatt_api->server->add_included_service(gatt_servers[instance_id - 1].server_id, serv_handle, incl_handle);
838 if (ret != BT_STATUS_SUCCESS) {
839 BT_ERR("GATT Server Add included Service failed: %s", status2string(ret));
840 return convert_to_oal_status(ret);
842 return OAL_STATUS_SUCCESS;
845 oal_status_t gatts_add_characteristics(int instance_id, int serv_handle,
846 oal_uuid_t* charc_uuid, int propts, int permsn)
848 int ret = OAL_STATUS_SUCCESS;
849 char str[2*BT_UUID_STRING_MAX];
850 char prop[MAX_PROP_LEN], perm[MAX_PERM_LEN];
851 char_prop_to_string(propts, prop);
852 char_perm_to_string(permsn, perm);
853 API_TRACE("Server Add Characteristic : instance_id = %d, serv_handle = %d, Properties =%s, Permisison = %s",
854 instance_id, serv_handle, prop, perm);
855 CHECK_OAL_GATT_ENABLED();
857 CHECK_SERVER_INSTANCE(instance_id);
859 CHECK_SERVER_REGISTRATION(instance_id);
861 if (charc_uuid != NULL) {
862 uuid_to_stringname(charc_uuid, str);
863 API_TRACE("uuid: [%s]", str);
866 ret = gatt_api->server->add_characteristic(gatt_servers[instance_id - 1].server_id,
867 serv_handle, (bt_uuid_t*)charc_uuid, propts, permsn);
868 if (ret != BT_STATUS_SUCCESS) {
869 BT_ERR("GATT Server Add Characteristic failed: %s", status2string(ret));
870 return convert_to_oal_status(ret);
872 return OAL_STATUS_SUCCESS;
875 oal_status_t gatts_add_descriptor(int instance_id, int serv_handle,
876 oal_uuid_t* desc_uuid, int permsn)
878 int ret = OAL_STATUS_SUCCESS;
879 char str[2*BT_UUID_STRING_MAX];
880 char perm[MAX_PERM_LEN];
881 char_perm_to_string(permsn, perm);
882 API_TRACE("Server Add Descriptor : instance_id = %d, serv_handle = %d, Permisison = %s",
883 instance_id, serv_handle, perm);
884 CHECK_OAL_GATT_ENABLED();
886 CHECK_SERVER_INSTANCE(instance_id);
888 CHECK_SERVER_REGISTRATION(instance_id);
890 if (desc_uuid != NULL) {
891 uuid_to_stringname(desc_uuid, str);
892 API_TRACE("uuid: [%s]", str);
895 ret = gatt_api->server->add_descriptor(gatt_servers[instance_id - 1].server_id,
896 serv_handle, (bt_uuid_t*)desc_uuid, permsn);
898 if (ret != BT_STATUS_SUCCESS) {
899 BT_ERR("GATT Server Add Descriptor failed: %s", status2string(ret));
900 return convert_to_oal_status(ret);
902 return OAL_STATUS_SUCCESS;
905 oal_status_t gatts_start_service(int instance_id, int svc_handle, int transport)
907 int ret = OAL_STATUS_SUCCESS;
909 API_TRACE("Server Start Service : Ins_id = %d, srvHndl = %d, transport = %d",
910 instance_id, svc_handle, transport);
911 CHECK_OAL_GATT_ENABLED();
913 CHECK_SERVER_INSTANCE(instance_id);
915 CHECK_SERVER_REGISTRATION(instance_id);
917 ret = gatt_api->server->start_service(gatt_servers[instance_id - 1].server_id, svc_handle, transport);
919 if (ret != BT_STATUS_SUCCESS) {
920 BT_ERR("GATT Server Start Service failed: %s", status2string(ret));
921 return convert_to_oal_status(ret);
923 return OAL_STATUS_SUCCESS;
926 oal_status_t gatts_stop_service(int ins_id, int srv_hdl)
928 int ret = OAL_STATUS_SUCCESS;
930 API_TRACE("Server Stop Service : Ins_id = %d, srvHndl = %d, ", ins_id, srv_hdl);
931 CHECK_OAL_GATT_ENABLED();
933 CHECK_SERVER_INSTANCE(ins_id);
934 CHECK_SERVER_REGISTRATION(ins_id);
936 ret = gatt_api->server->stop_service(gatt_servers[ins_id - 1].server_id, srv_hdl);
937 if (ret != BT_STATUS_SUCCESS) {
938 BT_ERR("GATT Server Stop Service failed: %s", status2string(ret));
939 return convert_to_oal_status(ret);
941 return OAL_STATUS_SUCCESS;
944 oal_status_t gatts_delete_service(int ins_id, int srv_hdl)
946 int ret = OAL_STATUS_SUCCESS;
948 API_TRACE("Server Delete Service : Ins_id = %d, srvHndl = %d, ", ins_id, srv_hdl);
949 CHECK_OAL_GATT_ENABLED();
951 CHECK_SERVER_INSTANCE(ins_id);
953 CHECK_SERVER_REGISTRATION(ins_id);
955 ret = gatt_api->server->delete_service(gatt_servers[ins_id - 1].server_id, srv_hdl);
956 if (ret != BT_STATUS_SUCCESS) {
957 BT_ERR("GATT Server Delete Service failed: %s", status2string(ret));
958 return convert_to_oal_status(ret);
960 return OAL_STATUS_SUCCESS;
963 oal_status_t gatts_send_response(int conn_id, int trans_id, int resp_status, oal_gatt_response_t *response)
965 int ret = OAL_STATUS_SUCCESS;
967 API_TRACE("Server Send Response : ConnId = %d, TransId = %d, ", conn_id, trans_id);
968 CHECK_OAL_GATT_ENABLED();
970 if (response == NULL) {
971 BT_ERR("GATT Server attribute value is empty");
972 return OAL_STATUS_INVALID_PARAM;
975 ret = gatt_api->server->send_response(conn_id, trans_id, resp_status, (btgatt_response_t *)response);
976 if (ret != BT_STATUS_SUCCESS) {
977 BT_ERR("GATT Server Send Response failed: %s", status2string(ret));
978 return convert_to_oal_status(ret);
980 return OAL_STATUS_SUCCESS;
983 oal_status_t gatts_send_indication(int ins_id, int attr_hndl, int conn_id, int len, int confirm, char* value)
985 int ret = OAL_STATUS_SUCCESS;
987 API_TRACE("Server Send Indication : Ins_id = %d, srvHndl = %d, [%s]",
988 ins_id, attr_hndl, confirm == TRUE ? "INDICATION" : "NOTIFICATION");
989 CHECK_OAL_GATT_ENABLED();
991 CHECK_SERVER_INSTANCE(ins_id);
993 CHECK_SERVER_REGISTRATION(ins_id);
995 if (value == NULL || len == 0) {
996 BT_ERR("GATT Server attribute value is empty");
997 return OAL_STATUS_INVALID_PARAM;
1000 ret = gatt_api->server->send_indication(gatt_servers[ins_id - 1].server_id,
1001 attr_hndl, conn_id, len, confirm, value);
1003 if (ret != BT_STATUS_SUCCESS) {
1004 BT_ERR("GATT Server Send Indication failed: %s", status2string(ret));
1005 return convert_to_oal_status(ret);
1007 return OAL_STATUS_SUCCESS;
1010 oal_status_t gatts_update_att_value(int ins_id, oal_gatt_value_t *value)
1012 int ret = OAL_STATUS_SUCCESS;
1014 CHECK_OAL_GATT_ENABLED();
1016 CHECK_SERVER_INSTANCE(ins_id);
1018 CHECK_SERVER_REGISTRATION(ins_id);
1020 if (value == NULL || value->len == 0) {
1021 BT_ERR("GATT Server attribute value is empty");
1022 return OAL_STATUS_INVALID_PARAM;
1025 API_TRACE("Server Update Value: Ins_id = [%d], att handle = [%d] data len [%d]",
1026 ins_id, value->handle, value->len);
1028 ret = gatt_api->server->update_att_value(gatt_servers[ins_id - 1].server_id,
1029 (int)value->handle, (int)value->len, (char*)value->value);
1031 if (ret != BT_STATUS_SUCCESS) {
1032 BT_ERR("GATT Server Update Attribute Value failed: [%s]", status2string(ret));
1033 return convert_to_oal_status(ret);
1035 return OAL_STATUS_SUCCESS;
1038 /* GATT Server Callbacks:Start */
1039 static void cb_gatts_service_added(int status, int server_if,
1040 btgatt_srvc_id_t *psrvc_id, int srvc_handle)
1044 BT_INFO("BTGATT SERVER SERVICE ADDED CB, status:%d, handle:%d", status, srvc_handle);
1045 event_gatts_srvc_prm_t* event = g_new0(event_gatts_srvc_prm_t, 1);
1047 CHECK_CORRECT_SERVER_ID(instance_id, server_if, event->gatt_srvc_stat.server_inst);
1049 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1050 event->gatt_srvc_stat.server_inst = -1;
1052 event->gatt_srvc_stat.servic_hndl = srvc_handle;
1053 if (psrvc_id != NULL) {
1054 event->gatt_srvc_id.id.inst_id = psrvc_id->id.inst_id;
1055 //event->gatt_srvc_id.id.uuid = psrvc_id->id.uuid;
1056 memcpy(event->gatt_srvc_id.id.uuid.uuid, psrvc_id->id.uuid.uu, sizeof(psrvc_id->id.uuid));
1057 event->gatt_srvc_id.is_prmry = psrvc_id->is_primary;
1060 event->gatt_srvc_stat.status = convert_to_oal_status(status);
1062 send_event(OAL_EVENT_GATTS_SERVICE_ADDED, event, sizeof(event_gatts_srvc_prm_t));
1065 static void cb_gatts_included_service_added(int status, int server_if,
1067 int incl_srvc_handle)
1070 BT_INFO("BTGATT SERVER INCLUDED SERVICE ADDED CB, status:%d, srvc handle:%d, incl handle:%d",
1071 status, srvc_handle, incl_srvc_handle);
1073 event_gatts_incld_srvc_t* event = g_new0(event_gatts_incld_srvc_t, 1);
1075 CHECK_CORRECT_SERVER_ID(ins_id, server_if, event->gatt_srvc_stat.server_inst);
1077 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1078 event->gatt_srvc_stat.server_inst = -1;
1080 event->gatt_srvc_stat.servic_hndl = srvc_handle;
1081 event->incl_srvc_hndl = incl_srvc_handle;
1082 event->gatt_srvc_stat.status = convert_to_oal_status(status);
1083 send_event(OAL_EVENT_GATTS_INCLUDED_SERVICE_ADDED, event, sizeof(event_gatts_incld_srvc_t));
1086 static void cb_gatts_characteristic_added(int status, int server_if, bt_uuid_t *char_id,
1087 int srvc_handle, int char_handle)
1091 BT_INFO("BTGATT SERVER CHARACTERISTIC ADDED CB, status:%d, srvc handle:%d, char handle:%d",
1092 status, srvc_handle, char_handle);
1094 event_gatts_srvc_charctr_t* event = g_new0(event_gatts_srvc_charctr_t, 1);
1096 CHECK_CORRECT_SERVER_ID(Ins_id, server_if, event->gatt_srvc_stat.server_inst);
1098 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1099 event->gatt_srvc_stat.server_inst = -1;
1101 event->gatt_srvc_stat.servic_hndl = srvc_handle;
1102 event->charctr_hndl = char_handle;
1103 event->gatt_srvc_stat.status = convert_to_oal_status(status);
1104 memcpy(event->charctr_uuid.uuid, char_id->uu, sizeof(bt_uuid_t));
1105 send_event(OAL_EVENT_GATTS_CHARACTERISTIC_ADDED, event, sizeof(event_gatts_srvc_charctr_t));
1108 static void cb_gatts_descriptor_added(int status, int server_if,
1109 bt_uuid_t *descr_id, int srvc_handle,
1113 BT_INFO("BTGATT SERVER DESCRIPTOR ADDED CB, status:%d, srvc handle:%d, desc handle:%d",
1114 status, srvc_handle, descr_handle);
1116 event_gatts_srvc_descr_t* event = g_new0(event_gatts_srvc_descr_t, 1);
1118 CHECK_CORRECT_SERVER_ID(Ins_id, server_if, event->gatt_srvc_stat.server_inst);
1120 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1121 event->gatt_srvc_stat.server_inst = -1;
1123 event->gatt_srvc_stat.servic_hndl = srvc_handle;
1124 event->descrptr_hndl = descr_handle;
1125 event->gatt_srvc_stat.status = convert_to_oal_status(status);
1126 memcpy(event->descrptr_uuid.uuid, descr_id->uu, sizeof(bt_uuid_t));
1127 send_event(OAL_EVENT_GATTS_DESCRIPTOR_ADDED, event, sizeof(event_gatts_srvc_descr_t));
1130 static void cb_gatts_service_started(int status, int server_if, int srvc_handle)
1133 BT_INFO("BTGATT SERVER SERVICE STARTED CB, status:%d, srvc_handle:%d", status, srvc_handle);
1135 event_gatts_srvc_t* event = g_new0(event_gatts_srvc_t, 1);
1137 CHECK_CORRECT_SERVER_ID(Ins_id, server_if, event->server_inst);
1139 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1140 event->server_inst = -1;
1142 event->servic_hndl = srvc_handle;
1143 event->status = convert_to_oal_status(status);
1144 send_event(OAL_EVENT_GATTS_SERVICE_STARTED, event, sizeof(event_gatts_srvc_t));
1147 static void cb_gatts_service_stopped(int status, int server_if, int srvc_handle)
1150 BT_INFO("BTGATT SERVER SERVICE STOPPED CB, status:%d, srvc_handle:%d", status, srvc_handle);
1152 event_gatts_srvc_t* event = g_new0(event_gatts_srvc_t, 1);
1154 CHECK_CORRECT_SERVER_ID(ins_id, server_if, event->server_inst);
1156 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1157 event->server_inst = -1;
1159 event->servic_hndl = srvc_handle;
1160 event->status = convert_to_oal_status(status);
1161 send_event(OAL_EVENT_GATTS_SERVICE_STOPED, event, sizeof(event_gatts_srvc_t));
1164 static void cb_gatts_service_deleted(int status, int server_if, int srvc_handle)
1167 BT_INFO("BTGATT SERVER SERVICE DELETED CB, status:%d, srvc_handle:%d", status, srvc_handle);
1169 event_gatts_srvc_t* event = g_new0(event_gatts_srvc_t, 1);
1171 CHECK_CORRECT_SERVER_ID(ins_id, server_if, event->server_inst);
1173 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1174 event->server_inst = -1;
1176 event->servic_hndl = srvc_handle;
1177 event->status = convert_to_oal_status(status);
1178 send_event(OAL_EVENT_GATTS_SERVICE_DELETED, event, sizeof(event_gatts_srvc_t));
1181 static void cb_gatts_connection(int conn_id, int server_if, int connected, bt_bdaddr_t *bda)
1184 oal_event_t event_type;
1186 BT_INFO("BTGATT SERVER CONNECTION connnected:%d, conn_id:%d server_if:%d", connected, conn_id, server_if);
1187 event_gatts_conn_t *event = g_new0(event_gatts_conn_t, 1);
1189 for (ins_id = 1 ; ins_id <= NUM_SERVER_INST ; ins_id++) {
1190 if ((gatt_servers[ins_id - 1].server_id == server_if) &&
1191 (gatt_servers[ins_id - 1].state == GATT_INS_ENABLED)) {
1192 event->server_inst = ins_id;
1195 BT_INFO("Server ID:%d recevied server Id:%d\n", gatt_servers[ins_id - 1].server_id, server_if);
1198 if (ins_id == NUM_SERVER_INST+1) {
1199 BT_WARN("Incorrect Server Interface received: %d\n", server_if);
1203 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1204 event->conn_id = conn_id;
1205 event_type = ((connected == TRUE) ? OAL_EVENT_GATTS_CONNECTION_COMPLETED : OAL_EVENT_GATTS_DISCONNECTION_COMPLETED);
1207 send_event(event_type, event, sizeof(event_gatts_conn_t));
1210 static void cb_gatts_request_read(int conn_id, int trans_id, bt_bdaddr_t *bda,
1211 int attr_handle, int offset, bool is_long)
1213 BT_INFO("BTGATT SERVER REQUEST READ CB: conn_id:%d", conn_id);
1215 event_gatts_srvc_read_attr_t* event = g_new0(event_gatts_srvc_read_attr_t, 1);
1217 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1219 event->attr_trans.attr_handle = attr_handle;
1220 event->attr_trans.conn_id = conn_id;
1221 event->attr_trans.trans_id = trans_id;
1222 event->attr_trans.offset = offset;
1223 event->is_long = is_long;
1225 send_event(OAL_EVENT_GATTS_REQUEST_READ, event, sizeof(event_gatts_srvc_read_attr_t));
1228 static void cb_gatts_request_write(int conn_id, int trans_id,
1229 bt_bdaddr_t *bda, int attr_handle,
1230 int offset, int length,
1231 bool need_rsp, bool is_prep, uint8_t* value)
1233 BT_INFO("BTGATT SERVER REQUEST WRITE conn_id:%d", conn_id);
1235 event_gatts_srvc_write_attr_t* event = g_new0(event_gatts_srvc_write_attr_t, 1);
1237 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1239 event->attr_trans.attr_handle = attr_handle;
1240 event->attr_trans.conn_id = conn_id;
1241 event->attr_trans.trans_id = trans_id;
1242 event->attr_trans.offset = offset;
1243 event->need_rsp = need_rsp;
1244 event->is_prep = is_prep;
1246 if (length > 0 && value != NULL) {
1247 if (length > OAL_GATT_MAX_ATTR_LEN)
1248 length = OAL_GATT_MAX_ATTR_LEN;
1249 memcpy(event->value, value, length);
1250 event->length = length;
1252 send_event(OAL_EVENT_GATTS_REQUEST_WRITE, event, sizeof(event_gatts_srvc_write_attr_t));
1255 static void cb_gatts_response_confirmation(int status, int handle)
1257 BT_INFO("BTGATT SERVER RESPONSE CONFIRMATION CB, status:%d, handle:%d", status, handle);
1259 event_gatts_respons_t* event = g_new0(event_gatts_respons_t, 1);
1261 event->hndl = handle;
1262 event->status = convert_to_oal_status(status);
1264 send_event(OAL_EVENT_GATTS_RESPONSE_CONFIRMED, event, sizeof(event_gatts_respons_t));
1267 static void cb_indication_confirmation(int conn_id, int trans_id, int attr_handle, bt_bdaddr_t *bda)
1269 BT_INFO("BTGATT SERVER Indication Confirmation CB, conn_id:%d, trans_id:%d attr_handle:%d",
1270 conn_id, trans_id, attr_handle);
1272 event_gatts_ind_cnfrm_t* event = g_new0(event_gatts_ind_cnfrm_t, 1);
1274 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1276 event->attr_handle = attr_handle;
1277 event->conn_id = conn_id;
1278 event->trans_id = trans_id;
1279 send_event(OAL_EVENT_GATTS_IND_CONFIRM, event, sizeof(event_gatts_ind_cnfrm_t));
1282 static void cb_gatts_mtu_changed(int conn_id, int mtu)
1284 BT_INFO("BTGATT SERVER MTU CHANGED CB, conn_id: %d, mtu_size: %d", conn_id, mtu);
1285 event_gatts_mtu_changed_t *event = g_new0(event_gatts_mtu_changed_t, 1);
1286 event->conn_id = conn_id;
1287 event->mtu_size = mtu;
1288 send_event(OAL_EVENT_GATTS_MTU_CHANGED, event, sizeof(event_gatts_mtu_changed_t));
1292 static void cb_notifcation_changed(int conn_id, int trans_id, int attr_handle, bool notify, bt_bdaddr_t *bda)
1294 BT_INFO("BTGATT SERVER Notication CB, conn_id:%d, trans_id:%d attr_handle:%d Notify:%d",
1295 conn_id, trans_id, attr_handle, notify);
1297 event_gatts_notif_t* event = g_new0(event_gatts_notif_t, 1);
1299 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1301 event->attr_handle = attr_handle;
1302 event->conn_id = conn_id;
1303 event->trans_id = trans_id;
1304 event->notify = notify;
1305 send_event(OAL_EVENT_GATTS_NOTIFICATION, event, sizeof(event_gatts_notif_t));
1310 /* GATT Server Callbacks: End */
1311 /************************************ GATT Client ***********************************/
1312 /* Client Callbacks */
1313 static void cb_gattc_register_app(int status, int clientIf, bt_uuid_t *app_uuid)
1315 char uuid_str[BT_UUID_STRING_MAX];
1316 event_gattc_register_t *event;
1320 /* Check if GATT client registered for Default GATT client UUID */
1321 uuid_to_string((service_uuid_t *)app_uuid, uuid_str);
1322 BT_INFO("UUID:%s", uuid_str);
1324 BT_INFO("BTGATT CLIENT REGISTER APP CB, status:%d, clientIf:%d", status, clientIf);
1325 event = g_new0(event_gattc_register_t, 1);
1326 event->client_if = clientIf;
1327 event->status = (status == 0 ? OAL_STATUS_SUCCESS : OAL_STATUS_INTERNAL_ERROR);
1328 memcpy(event->client_uuid.uuid, app_uuid->uu, sizeof(oal_uuid_t));
1329 send_event(OAL_EVENT_GATTC_REGISTRATION, event, sizeof(event_gattc_register_t));
1332 static void cb_gattc_scan_result(bt_bdaddr_t* bdaddress, int rssi, uint8_t *adv_data)
1334 event_ble_scan_result_info *event;
1336 event = g_new0(event_ble_scan_result_info, 1);
1338 memcpy(event->address.addr, bdaddress->address, BT_ADDRESS_BYTES_NUM);
1339 memcpy(event->adv_data, adv_data, BLE_ADV_DATA_LENGTH);
1340 send_event_bda_trace(OAL_EVENT_BLE_REMOTE_DEVICE_FOUND, event,
1341 sizeof(event_ble_scan_result_info), (bt_address_t *)bdaddress);
1344 static void cb_gattc_connection(int conn_id, int status, int client_if, bt_bdaddr_t* bda)
1346 event_gattc_conn_t *event;
1347 BT_INFO("BTGATT CLIENT CONNECTED CB, status:[%d] client_if[%d] id[%d]",
1348 status, client_if, conn_id);
1350 event = g_new0(event_gattc_conn_t, 1);
1351 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1352 event->client_if = client_if;
1354 if (BT_STATUS_SUCCESS != status) {
1355 event->status = OAL_STATUS_INTERNAL_ERROR;
1356 BT_ERR("gattc connection Error: %d", status);
1358 event->conn_id = conn_id;
1359 event->status = OAL_STATUS_SUCCESS;
1362 send_event_bda_trace(OAL_EVENT_GATTC_CONNECTION_COMPLETED, event,
1363 sizeof(*event), (bt_address_t *)bda);
1367 static void cb_gattc_disconnect(int conn_id, int status, int client_if, bt_bdaddr_t* bda)
1369 event_gattc_conn_t *event;
1370 BT_INFO("BTGATT Client Disconnect Complete cb, status:[%d], client_if[%d] id[%d]",
1371 status, client_if, conn_id);
1372 event = g_new0(event_gattc_conn_t, 1);
1373 event->status = convert_to_oal_status(status);
1374 event->client_if = client_if;
1375 event->conn_id = conn_id;
1376 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1377 send_event_bda_trace(OAL_EVENT_GATTC_DISCONNECTION_COMPLETED, event,
1378 sizeof(*event), (bt_address_t *)bda);
1381 static void cb_gattc_search_result(int conn_id, btgatt_srvc_id_t *srvc_id)
1383 char uuid_str[2*BT_UUID_STRING_MAX];
1385 BT_INFO("BTGATT Client Service Search Result cb, conn_id:%d", conn_id);
1387 uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str);
1388 BT_INFO("Service=> UUID: [%s], Inst_id: [%u], Type: [%s]",
1389 uuid_str, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1391 event_gattc_service_result_t *event = g_new0(event_gattc_service_result_t, 1);
1392 event->conn_status.status = OAL_STATUS_SUCCESS;
1393 event->conn_status.conn_id = conn_id;
1394 memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1396 send_event(OAL_EVENT_GATTC_SERVICE_SEARCH_RESULT, event, sizeof(*event));
1399 static void cb_gattc_search_complete(int conn_id, int status)
1401 BT_INFO("BTGATT Client Service Search Complete cb, status:%d, conn_id:%d",
1404 event_gattc_conn_status_t *event = g_new0(event_gattc_conn_status_t, 1);
1405 event->conn_id = conn_id;
1406 event->status = convert_to_oal_status(status);
1408 send_event(OAL_EVENT_GATTC_SERVICE_SEARCH_DONE, event, sizeof(*event));
1412 static void cb_gattc_get_characteristics(int conn_id, int status, btgatt_srvc_id_t *srvc_id,
1413 btgatt_gatt_id_t *char_id, int char_prop)
1415 char uuid_str1[2*BT_UUID_STRING_MAX];
1416 char uuid_str2[2*BT_UUID_STRING_MAX];
1418 BT_INFO("BTGATT Client Get Characteristic Callback, conn_id:%d, status:%d", conn_id, status);
1419 uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str1);
1420 BT_INFO("Service=> UUID: [%s], Inst_id: [%u], Type: [%s]",
1421 uuid_str1, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1423 event_gattc_characteristic_result_t *event = g_new0(event_gattc_characteristic_result_t, 1);
1424 event->conn_status.conn_id = conn_id;
1425 event->conn_status.status = convert_to_oal_status(status);
1426 memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1429 uuid_to_stringname((oal_uuid_t *)&(char_id->uuid), uuid_str2);
1430 BT_INFO("Charac=> UUID: [%s], Inst_id: [%u], Prop: [%s]",
1431 uuid_str2, char_id->inst_id, char_prop_to_string(char_prop, str));
1432 event->char_prop = char_prop;
1433 memcpy(&(event->char_id), char_id, sizeof(oal_gatt_id_t));
1435 send_event(OAL_EVENT_GATTC_CHARAC_SERACH_RESULT, event, sizeof(*event));
1440 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)
1442 char uuid_str1[2*BT_UUID_STRING_MAX];
1443 char uuid_str2[2*BT_UUID_STRING_MAX];
1444 char uuid_str3[2*BT_UUID_STRING_MAX];
1445 BT_INFO("BTGATT Client Get Descriptor Callback, conn_id:%d, status:%d", conn_id, status);
1446 uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str1);
1447 uuid_to_stringname((oal_uuid_t *)&(char_id->uuid), uuid_str2);
1448 BT_INFO("Service=> UUID: [%s], Inst_id: [%u], Type: [%s]",
1449 uuid_str1, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1450 BT_INFO("Charac=> UUID: [%s], Inst_id: [%u]", uuid_str2, char_id->inst_id);
1452 event_gattc_descriptor_result_t *event = g_new0(event_gattc_descriptor_result_t, 1);
1453 event->conn_status.conn_id = conn_id;
1454 event->conn_status.status = convert_to_oal_status(status);
1455 memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1456 memcpy(&(event->char_id), char_id, sizeof(oal_gatt_id_t));
1459 uuid_to_stringname((oal_uuid_t *)&(descr_id->uuid), uuid_str3);
1460 BT_INFO("Desc=> UUID: [%s], Inst_id: [%u]", uuid_str3, descr_id->inst_id);
1461 memcpy(&(event->descr_id), descr_id, sizeof(oal_gatt_id_t));
1463 send_event(OAL_EVENT_GATTC_DESC_SERACH_RESULT, event, sizeof(*event));
1466 static void cb_gattc_register_for_notification(int client_if, int registered, int status, btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id)
1468 char uuid_str1[2*BT_UUID_STRING_MAX];
1469 char uuid_str2[2*BT_UUID_STRING_MAX];
1470 BT_INFO("BTGATT Client Register For Notification Callback, client_if:%d, status:%d, registered: %s",
1471 client_if, status, registered == 1 ? "REGISTERED" : "DEREGISTERED");
1472 uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str1);
1473 uuid_to_stringname((oal_uuid_t *)&(char_id->uuid), uuid_str2);
1474 BT_INFO("Service=> UUID: [%s], Inst_id: [%u], Type: [%s]", uuid_str1, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1475 BT_INFO("Charac=> UUID: [%s], Inst_id: [%u]", uuid_str2, char_id->inst_id);
1477 event_gattc_regdereg_notify_t *event = g_new0(event_gattc_regdereg_notify_t, 1);
1478 oal_event_t event_type = (registered == 1 ? OAL_EVENT_GATTC_NOTIFICATION_REGISTERED : OAL_EVENT_GATTC_NOTIFICATION_DEREGISTERED);
1479 event->client_if = client_if;
1480 event->status = convert_to_oal_status(status);
1481 memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1482 memcpy(&(event->char_id), char_id, sizeof(oal_gatt_id_t));
1484 send_event(event_type, event, sizeof(*event));
1488 static void cb_gattc_read_characteristic(int conn_id, int status, btgatt_read_params_t *p_data)
1490 char uuid_str1[2*BT_UUID_STRING_MAX];
1491 char uuid_str2[2*BT_UUID_STRING_MAX];
1492 BT_INFO("BTGATT Client Read Charcateristic Callback, conn_id:%d, read_status:%d", conn_id, status);
1494 event_gattc_read_data *event = g_new0(event_gattc_read_data, 1);
1495 event->uuid_status.conn_status.conn_id = conn_id;
1496 event->uuid_status.conn_status.status = convert_to_oal_status(status);
1497 event->value_type = p_data->value_type;
1498 memcpy(&(event->uuid_status.srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1499 memcpy(&(event->uuid_status.char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1501 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1502 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1503 BT_INFO("Service=> UUID: [%s], Inst_id: [%u], Type: [%s]", uuid_str1,
1504 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1505 BT_INFO("Charac=> UUID: [%s], Inst_id: [%u]", uuid_str2, p_data->char_id.inst_id);
1506 BT_INFO("Len: %u, value_type: %u", p_data->value.len, p_data->value_type);
1507 if (p_data->value.len > 0 && event->value_type == GATTC_READ_VALUE_TYPE_VALUE) {
1509 data = g_malloc(3*p_data->value.len+1);
1511 BT_ERR("memory allocation failed");
1515 convert_hex_2_str((unsigned char *)p_data->value.value, p_data->value.len, data);
1516 BT_INFO("Read Data: [%s]", data);
1517 event->data_len = p_data->value.len;
1518 memcpy(&(event->data), &(p_data->value.value), event->data_len);
1522 send_event(OAL_EVENT_GATTC_READ_CHARAC, event, sizeof(*event));
1525 static void cb_gattc_write_characteristic(int conn_id, int status, btgatt_write_params_t *p_data)
1527 char uuid_str1[2*BT_UUID_STRING_MAX];
1528 char uuid_str2[2*BT_UUID_STRING_MAX];
1529 BT_INFO("BTGATT Client Write Charcateristic Callback, conn_id:%d, write_status:%d", conn_id, status);
1531 event_gattc_write_data *event = g_new0(event_gattc_write_data, 1);
1532 event->conn_status.conn_id = conn_id;
1533 event->conn_status.status = convert_to_oal_status(status);
1534 memcpy(&(event->srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1535 memcpy(&(event->char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1537 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1538 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1539 BT_INFO("Service=> UUID: [%s], Inst_id: [%u], Type: [%s]", uuid_str1,
1540 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1541 BT_INFO("Charac=> UUID: [%s], Inst_id: [%u]", uuid_str2, p_data->char_id.inst_id);
1543 send_event(OAL_EVENT_GATTC_WRITE_CHARAC, event, sizeof(*event));
1546 static void cb_gattc_write_descriptor(int conn_id, int status, btgatt_write_params_t *p_data)
1548 char uuid_str1[2*BT_UUID_STRING_MAX];
1549 char uuid_str2[2*BT_UUID_STRING_MAX];
1550 char uuid_str3[2*BT_UUID_STRING_MAX];
1551 BT_INFO("BTGATT Client Write Descriptor Callback, conn_id:%d, write_status:%d", conn_id, status);
1553 event_gattc_write_data *event = g_new0(event_gattc_write_data, 1);
1554 event->conn_status.conn_id = conn_id;
1555 event->conn_status.status = convert_to_oal_status(status);
1556 memcpy(&(event->srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1557 memcpy(&(event->char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1558 memcpy(&(event->descr_id), &(p_data->descr_id), sizeof(oal_gatt_id_t));
1560 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1561 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1562 uuid_to_stringname((oal_uuid_t *)&(p_data->descr_id.uuid), uuid_str3);
1563 BT_INFO("Service=> UUID: [%s], Inst_id: [%u], Type: [%s]", uuid_str1,
1564 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1565 BT_INFO("Charac=> UUID: [%s], Inst_id: [%u]", uuid_str2, p_data->char_id.inst_id);
1566 BT_INFO("Desc=> UUID: [%s], Inst_id: [%u]", uuid_str3, p_data->descr_id.inst_id);
1568 send_event(OAL_EVENT_GATTC_WRITE_DESCR, event, sizeof(*event));
1573 static void cb_gattc_read_descriptor(int conn_id, int status, btgatt_read_params_t *p_data)
1575 char uuid_str1[2*BT_UUID_STRING_MAX];
1576 char uuid_str2[2*BT_UUID_STRING_MAX];
1577 char uuid_str3[2*BT_UUID_STRING_MAX];
1578 BT_INFO("BTGATT Client Read Descriptor Callback, conn_id:%d, read_status:%d", conn_id, status);
1580 event_gattc_read_data *event = g_new0(event_gattc_read_data, 1);
1581 event->uuid_status.conn_status.conn_id = conn_id;
1582 event->uuid_status.conn_status.status = convert_to_oal_status(status);
1583 event->value_type = p_data->value_type;
1584 memcpy(&(event->uuid_status.srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1585 memcpy(&(event->uuid_status.char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1586 memcpy(&(event->uuid_status.descr_id), &(p_data->descr_id), sizeof(oal_gatt_id_t));
1589 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1590 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1591 uuid_to_stringname((oal_uuid_t *)&(p_data->descr_id.uuid), uuid_str3);
1592 BT_INFO("Service=> UUID: [%s], Inst_id: [%u], Type: [%s]", uuid_str1,
1593 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1594 BT_INFO("Charac=> UUID: [%s], Inst_id: [%u]", uuid_str2, p_data->char_id.inst_id);
1595 BT_INFO("Desc=> UUID: [%s], Inst_id: [%u]", uuid_str3, p_data->descr_id.inst_id);
1596 BT_INFO("Len: %u, value_type: %u", p_data->value.len, p_data->value_type);
1597 if (p_data->value.len > 0 && event->value_type == GATTC_READ_VALUE_TYPE_VALUE) {
1599 data = g_malloc(3*p_data->value.len+1);
1601 BT_ERR("memory allocation failed");
1605 convert_hex_2_str((unsigned char *)p_data->value.value, p_data->value.len, data);
1606 BT_INFO("Read Data: [%s]", data);
1607 event->data_len = p_data->value.len;
1608 memcpy(&(event->data), &(p_data->value.value), event->data_len);
1612 send_event(OAL_EVENT_GATTC_READ_DESCR, event, sizeof(*event));
1615 oal_status_t gattc_start_le_discovery(int client_id)
1618 int ret = OAL_STATUS_SUCCESS;
1620 API_TRACE("BTGATT CLIENT SCAN START");
1621 CHECK_OAL_GATT_ENABLED();
1622 CHECK_CLIENT_REGISTRATION(client_id);
1624 ret = gatt_api->client->scan(client_id, 1);
1625 if (ret != BT_STATUS_SUCCESS) {
1626 BT_ERR("Error:Start LE Discovery failed: %s", status2string(ret));
1627 return convert_to_oal_status(ret);
1629 send_event(OAL_EVENT_BLE_DISCOVERY_STARTED, NULL, 0);
1630 return OAL_STATUS_SUCCESS;
1633 oal_status_t gattc_stop_le_discovery(int client_id)
1636 int ret = OAL_STATUS_SUCCESS;
1638 API_TRACE("Scan is stopped");
1639 CHECK_OAL_GATT_ENABLED();
1640 CHECK_CLIENT_REGISTRATION(client_id);
1641 ret = gatt_api->client->scan(client_id, 0);
1642 if (ret != BT_STATUS_SUCCESS) {
1643 BT_ERR("Error:Stop LE Discovery failed: %s", status2string(ret));
1644 return convert_to_oal_status(ret);
1646 send_event(OAL_EVENT_BLE_DISCOVERY_STOPPED, NULL, 0);
1647 return OAL_STATUS_SUCCESS;
1650 oal_status_t gattc_set_le_scan_param(int scan_type, int itv, int win)
1654 API_TRACE("Scan is stopped");
1655 CHECK_OAL_GATT_ENABLED();
1658 ret = gatt_api->client->set_scan_parameters(scan_type, itv, win);
1659 if (ret != BT_STATUS_SUCCESS) {
1660 BT_ERR("Error:Set scan parameters failed: %s", status2string(ret));
1661 return convert_to_oal_status(ret);
1664 ret = gatt_api->client->set_scan_parameters(itv, win);
1665 if (ret != BT_STATUS_SUCCESS) {
1666 BT_ERR("Error:Set scan parameters failed: %s", status2string(ret));
1667 return convert_to_oal_status(ret);
1671 return OAL_STATUS_SUCCESS;
1673 /************************************ GATT Client ***********************************/
1674 oal_status_t gattc_register(oal_uuid_t* client_uuid)
1677 char str[2*BT_UUID_STRING_MAX];
1678 int ret = OAL_STATUS_SUCCESS;
1680 CHECK_OAL_GATT_ENABLED();
1681 uuid_to_stringname(client_uuid, str);
1682 API_TRACE("uuid: [%s]", str);
1683 /* We will perform actual registration in cb_gattc_register_app callback */
1684 ret = gatt_api->client->register_client((bt_uuid_t *)client_uuid);
1685 if (ret != BT_STATUS_SUCCESS) {
1686 BT_ERR("GATT client register failed: %s", status2string(ret));
1687 return convert_to_oal_status(ret);
1689 return OAL_STATUS_SUCCESS;
1692 oal_status_t gattc_deregister(int client_id)
1694 int ret = OAL_STATUS_SUCCESS;
1696 API_TRACE("GATT client deregister");
1697 CHECK_OAL_GATT_ENABLED();
1698 CHECK_CLIENT_REGISTRATION(client_id);
1700 ret = gatt_api->client->unregister_client(client_id);
1701 if (ret != BT_STATUS_SUCCESS) {
1702 BT_ERR("GATT client deregister failed: %s", status2string(ret));
1703 return convert_to_oal_status(ret);
1706 return OAL_STATUS_SUCCESS;
1709 oal_status_t gattc_connect(int client_id, bt_address_t *device_address, int isDirect)
1711 int ret = OAL_STATUS_SUCCESS;
1714 OAL_CHECK_PARAMETER(device_address, return);
1715 API_TRACE("Client Connect: [%s]", bdt_bd2str(device_address, &bdstr));
1716 CHECK_OAL_GATT_ENABLED();
1717 CHECK_CLIENT_REGISTRATION(client_id);
1719 /* Handle the actual connection in cb_gattc_connection callback */
1720 ret = gatt_api->client->connect(client_id, (bt_bdaddr_t *)device_address, isDirect);
1721 if (ret != BT_STATUS_SUCCESS) {
1722 BT_ERR("GATT client connect failed: %s", status2string(ret));
1723 return convert_to_oal_status(ret);
1725 return OAL_STATUS_SUCCESS;
1728 oal_status_t gattc_disconnect(int client_id, bt_address_t *device_address, int conn_id)
1731 int ret = OAL_STATUS_SUCCESS;
1734 OAL_CHECK_PARAMETER(device_address, return);
1735 API_TRACE("Client Disconnect: [%s]", bdt_bd2str(device_address, &bdstr));
1736 CHECK_OAL_GATT_ENABLED();
1737 CHECK_CLIENT_REGISTRATION(client_id);
1738 CHECK_CLIENT_CONNECTION(conn_id);
1740 /* Handle actual disconnection in callback */
1741 ret = gatt_api->client->disconnect(client_id, (bt_bdaddr_t *) device_address, conn_id);
1742 if (ret != BT_STATUS_SUCCESS) {
1743 BT_ERR("GATT client disconnect failed: %s", status2string(ret));
1744 return convert_to_oal_status(ret);
1746 return OAL_STATUS_SUCCESS;
1750 oal_status_t gattc_search_service(int conn_id, oal_uuid_t *service_uuid)
1752 int ret = OAL_STATUS_SUCCESS;
1753 char uuid_str[2*BT_UUID_STRING_MAX];
1756 uuid_to_stringname(service_uuid, uuid_str);
1757 API_TRACE("Client Service Search UUID: [%s]", uuid_str);
1759 API_TRACE("Client Service Search All");
1761 CHECK_OAL_GATT_ENABLED();
1762 CHECK_CLIENT_CONNECTION(conn_id);
1763 ret = gatt_api->client->search_service(conn_id, (bt_uuid_t *)service_uuid);
1765 if (ret != BT_STATUS_SUCCESS) {
1766 BT_ERR("GATT client service search failed: %s", status2string(ret));
1767 return convert_to_oal_status(ret);
1769 return OAL_STATUS_SUCCESS;
1772 oal_status_t gattc_get_characteristic(int conn_id, oal_gatt_srvc_id_t *srvc_id, oal_gatt_id_t *char_id)
1774 int ret = OAL_STATUS_SUCCESS;
1775 char uuid_str[2*BT_UUID_STRING_MAX];
1777 OAL_CHECK_PARAMETER(srvc_id, return);
1778 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str);
1779 API_TRACE("Client Get Characteristic, Service_uuid: [%s]", uuid_str);
1780 CHECK_OAL_GATT_ENABLED();
1781 CHECK_CLIENT_CONNECTION(conn_id);
1782 ret = gatt_api->client->get_characteristic(conn_id, (btgatt_srvc_id_t *)srvc_id,
1783 (btgatt_gatt_id_t *)char_id);
1784 if (ret != BT_STATUS_SUCCESS) {
1785 BT_ERR("GATT client get characteristic failed: %s", status2string(ret));
1786 return convert_to_oal_status(ret);
1788 return OAL_STATUS_SUCCESS;
1791 oal_status_t gattc_get_descriptor(int conn_id, oal_gatt_srvc_id_t *srvc_id,
1792 oal_gatt_id_t *char_id, oal_gatt_id_t *desc_id)
1794 int ret = OAL_STATUS_SUCCESS;
1795 char uuid_str1[2*BT_UUID_STRING_MAX];
1796 char uuid_str2[2*BT_UUID_STRING_MAX];
1798 OAL_CHECK_PARAMETER(srvc_id, return);
1799 OAL_CHECK_PARAMETER(char_id, return);
1800 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1801 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1802 API_TRACE("Client Get Descriptor, Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
1803 CHECK_OAL_GATT_ENABLED();
1804 CHECK_CLIENT_CONNECTION(conn_id);
1805 ret = gatt_api->client->get_descriptor(conn_id, (btgatt_srvc_id_t *)srvc_id,
1806 (btgatt_gatt_id_t *)char_id, (btgatt_gatt_id_t *)desc_id);
1807 if (ret != BT_STATUS_SUCCESS) {
1808 BT_ERR("GATT client get descriptor failed: %s", status2string(ret));
1809 return convert_to_oal_status(ret);
1811 return OAL_STATUS_SUCCESS;
1814 oal_status_t gattc_register_for_notification(int client_id, bt_address_t * address,
1815 oal_gatt_srvc_id_t *srvc_id, oal_gatt_id_t *char_id) {
1816 int ret = OAL_STATUS_SUCCESS;
1817 char uuid_str1[2*BT_UUID_STRING_MAX];
1818 char uuid_str2[2*BT_UUID_STRING_MAX];
1821 OAL_CHECK_PARAMETER(address, return);
1822 OAL_CHECK_PARAMETER(srvc_id, return);
1823 OAL_CHECK_PARAMETER(char_id, return);
1824 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1825 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1826 API_TRACE("Client Register Notification: [%s], Service_uuid: [%s], Char_uuid: [%s]", bdt_bd2str(address, &bdstr), uuid_str1, uuid_str2);
1827 CHECK_OAL_GATT_ENABLED();
1828 CHECK_CLIENT_REGISTRATION(client_id);
1830 ret = gatt_api->client->register_for_notification(client_id, (bt_bdaddr_t *)address, (btgatt_srvc_id_t *)srvc_id, (btgatt_gatt_id_t *)char_id);
1831 if (ret != BT_STATUS_SUCCESS) {
1832 BT_ERR("GATT client register notification failed: %s", status2string(ret));
1833 return convert_to_oal_status(ret);
1836 return OAL_STATUS_SUCCESS;
1839 oal_status_t gattc_deregister_for_notification(int client_id, bt_address_t * address,
1840 oal_gatt_srvc_id_t *srvc_id, oal_gatt_id_t *char_id) {
1841 int ret = OAL_STATUS_SUCCESS;
1842 char uuid_str1[2*BT_UUID_STRING_MAX];
1843 char uuid_str2[2*BT_UUID_STRING_MAX];
1846 OAL_CHECK_PARAMETER(address, return);
1847 OAL_CHECK_PARAMETER(srvc_id, return);
1848 OAL_CHECK_PARAMETER(char_id, return);
1849 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1850 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1851 API_TRACE("Client Deregister Notification: [%s], Service_uuid: [%s], Char_uuid: [%s]", bdt_bd2str(address, &bdstr), uuid_str1, uuid_str2);
1852 CHECK_OAL_GATT_ENABLED();
1853 CHECK_CLIENT_REGISTRATION(client_id);
1855 ret = gatt_api->client->deregister_for_notification(client_id, (bt_bdaddr_t *)address, (btgatt_srvc_id_t *)srvc_id, (btgatt_gatt_id_t *)char_id);
1856 if (ret != BT_STATUS_SUCCESS) {
1857 BT_ERR("GATT client deregister notification failed: %s", status2string(ret));
1858 return convert_to_oal_status(ret);
1861 return OAL_STATUS_SUCCESS;
1865 oal_status_t gattc_read_characteristic(int conn_id, oal_gatt_srvc_id_t *srvc_id,
1866 oal_gatt_id_t *char_id, oal_gatt_auth_req_t auth_req)
1868 int ret = OAL_STATUS_SUCCESS;
1869 char uuid_str1[2*BT_UUID_STRING_MAX];
1870 char uuid_str2[2*BT_UUID_STRING_MAX];
1872 OAL_CHECK_PARAMETER(srvc_id, return);
1873 OAL_CHECK_PARAMETER(char_id, return);
1874 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1875 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1876 API_TRACE("Client Read Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
1877 CHECK_OAL_GATT_ENABLED();
1878 CHECK_CLIENT_CONNECTION(conn_id);
1880 ret = gatt_api->client->read_characteristic(conn_id, (btgatt_srvc_id_t *)srvc_id,
1881 (btgatt_gatt_id_t *)char_id, auth_req);
1882 if (ret != BT_STATUS_SUCCESS) {
1883 BT_ERR("GATT client read characteristic failed: %s", status2string(ret));
1884 return convert_to_oal_status(ret);
1887 return OAL_STATUS_SUCCESS;
1890 oal_status_t gattc_read_descriptor(int conn_id, oal_gatt_srvc_id_t *srvc_id,
1891 oal_gatt_id_t *char_id, oal_gatt_id_t *desc_id, oal_gatt_auth_req_t auth_req)
1893 int ret = OAL_STATUS_SUCCESS;
1894 char uuid_str1[2*BT_UUID_STRING_MAX];
1895 char uuid_str2[2*BT_UUID_STRING_MAX];
1896 char uuid_str3[2*BT_UUID_STRING_MAX];
1898 OAL_CHECK_PARAMETER(srvc_id, return);
1899 OAL_CHECK_PARAMETER(char_id, return);
1900 OAL_CHECK_PARAMETER(desc_id, return);
1901 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1902 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1903 uuid_to_stringname(&(desc_id->uuid), uuid_str3);
1904 API_TRACE("Client Read Descriptor: Service_uuid: [%s], Char_uuid: [%s], Desc_uuid: [%s]",
1905 uuid_str1, uuid_str2, uuid_str3);
1906 CHECK_OAL_GATT_ENABLED();
1907 CHECK_CLIENT_CONNECTION(conn_id);
1909 ret = gatt_api->client->read_descriptor(conn_id, (btgatt_srvc_id_t *)srvc_id,
1910 (btgatt_gatt_id_t *)char_id, (btgatt_gatt_id_t *)desc_id, auth_req);
1911 if (ret != BT_STATUS_SUCCESS) {
1912 BT_ERR("GATT client read descriptor failed: %s", status2string(ret));
1913 return convert_to_oal_status(ret);
1916 return OAL_STATUS_SUCCESS;
1919 oal_status_t gattc_write_characteristic(int conn_id, oal_gatt_srvc_id_t *srvc_id,
1920 oal_gatt_id_t *char_id, oal_gatt_write_type_t write_type,
1921 int len, oal_gatt_auth_req_t auth_req, char* data)
1923 int ret = OAL_STATUS_SUCCESS;
1924 char uuid_str1[2*BT_UUID_STRING_MAX];
1925 char uuid_str2[2*BT_UUID_STRING_MAX];
1926 OAL_CHECK_PARAMETER(srvc_id, return);
1927 OAL_CHECK_PARAMETER(char_id, return);
1928 OAL_CHECK_PARAMETER(data, return);
1929 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1930 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1931 API_TRACE("Client Write Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
1932 CHECK_OAL_GATT_ENABLED();
1933 CHECK_CLIENT_CONNECTION(conn_id);
1935 ret = gatt_api->client->write_characteristic(conn_id, (btgatt_srvc_id_t *)srvc_id,
1936 (btgatt_gatt_id_t *)char_id, write_type, len, auth_req, data);
1937 if (ret != BT_STATUS_SUCCESS) {
1938 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
1939 return convert_to_oal_status(ret);
1942 return OAL_STATUS_SUCCESS;
1945 oal_status_t gattc_write_descriptor(int conn_id, oal_gatt_srvc_id_t *srvc_id,
1946 oal_gatt_id_t *char_id, oal_gatt_id_t *desc_id,
1947 oal_gatt_write_type_t write_type, int len, oal_gatt_auth_req_t auth_req, char* data)
1949 int ret = OAL_STATUS_SUCCESS;
1950 char uuid_str1[2*BT_UUID_STRING_MAX];
1951 char uuid_str2[2*BT_UUID_STRING_MAX];
1952 char uuid_str3[2*BT_UUID_STRING_MAX];
1953 OAL_CHECK_PARAMETER(srvc_id, return);
1954 OAL_CHECK_PARAMETER(char_id, return);
1955 OAL_CHECK_PARAMETER(desc_id, return);
1956 OAL_CHECK_PARAMETER(data, return);
1957 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1958 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1959 uuid_to_stringname(&(desc_id->uuid), uuid_str3);
1960 API_TRACE("Client Write Descriptor: Service_uuid: [%s], Char_uuid: [%s], Desc_uuid: [%s]",
1961 uuid_str1, uuid_str2, uuid_str3);
1962 CHECK_OAL_GATT_ENABLED();
1963 CHECK_CLIENT_CONNECTION(conn_id);
1965 ret = gatt_api->client->write_descriptor(conn_id, (btgatt_srvc_id_t *)srvc_id,
1966 (btgatt_gatt_id_t *)char_id, (btgatt_gatt_id_t *)desc_id, write_type, len, auth_req, data);
1967 if (ret != BT_STATUS_SUCCESS) {
1968 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
1969 return convert_to_oal_status(ret);
1972 return OAL_STATUS_SUCCESS;
1975 oal_status_t gattc_conn_param_update(bt_address_t * address, int min, int max, int latency, int timeout)
1980 OAL_CHECK_PARAMETER(address, return);
1981 API_TRACE("Conn Param Update: [%s]", bdt_bd2str(address, &bdstr));
1982 CHECK_OAL_GATT_ENABLED();
1984 BT_INFO("[%s] min[%d] max[%d] latency[%d] timeout[%d]", bdt_bd2str(address, &bdstr), min, max, latency, timeout);
1985 ret = gatt_api->client->conn_parameter_update((bt_bdaddr_t *)address, min, max, latency, timeout);
1986 if (ret != BT_STATUS_SUCCESS) {
1987 BT_ERR("error: %s", status2string(ret));
1988 return convert_to_oal_status(ret);
1990 return OAL_STATUS_SUCCESS;