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...");
668 BT_INFO("Adv State of server instance %d :%d", instance_id, new_state);
670 if (TRUE == new_state)
671 send_event(OAL_EVENT_BLE_ADVERTISING_STARTED, event, sizeof(event_ble_adv_status));
673 send_event(OAL_EVENT_BLE_ADVERTISING_STOPPED, event, sizeof(event_ble_adv_status));
676 static void cb_gatts_multi_adv_enable(int server_if, int status)
679 BT_INFO("BTGATT SERVER MULTI ADV ENABLE CB, status:%d, srv_if: %d", status, server_if);
680 event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
683 if (status != BT_STATUS_SUCCESS)
684 BT_ERR("MULTI ADV ENABLE failed, status:%d, srv_if: %d", status, server_if);
686 for (instance_id = 1 ; instance_id <= NUM_SERVER_INST ; instance_id++) {
687 if (gatt_servers[instance_id - 1].server_id == server_if) {
688 event->server_inst = instance_id;
689 gatt_servers[instance_id - 1].state = GATT_INS_ENABLED;
693 BT_ERR("Invalid Interface, srv_if: %d", server_if);
694 event->server_inst = -1;
696 event->status = convert_to_oal_status(status);
697 send_event(OAL_EVENT_BLE_MULTI_ADVERTISING_ENABLE, event, sizeof(event_ble_multiadv_status));
700 static void cb_gatts_multi_adv_disable(int server_if, int status)
702 BT_INFO("BTGATT SERVER MULTI ADV DISABLE CB, status:%d, srv_if: %d", status, server_if);
703 event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
706 if (status != BT_STATUS_SUCCESS)
707 BT_ERR("MULTI ADV DISABLE failed, status:%d, srv_if: %d", status, server_if);
709 for (instance_id = 1 ; instance_id <= NUM_SERVER_INST; instance_id++) {
710 if (gatt_servers[instance_id - 1].server_id == server_if) {
711 event->server_inst = instance_id;
712 gatt_servers[instance_id - 1].state = GATT_INS_DISABLED;
716 BT_ERR("Invalid Interface, srv_if: %d", server_if);
717 event->server_inst = -1;
719 event->status = convert_to_oal_status(status);
720 send_event(OAL_EVENT_BLE_MULTI_ADVERTISING_DISABLE, event, sizeof(event_ble_multiadv_status));
723 static void cb_gatts_multi_adv_update(int server_if, int status)
725 BT_INFO("BTGATT SERVER MULTI ADV UPDATE CB, status:%d, srv_if: %d", status, server_if);
726 event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
729 if (status != BT_STATUS_SUCCESS)
730 BT_ERR("MULTI ADV UPDATE failed, status:%d, srv_if: %d", status, server_if);
732 for (instance_id = 1 ; instance_id <= NUM_SERVER_INST ; instance_id++) {
733 if (gatt_servers[instance_id - 1].server_id == server_if) {
734 event->server_inst = instance_id;
738 BT_ERR("Invalid Interface, srv_if: %d", server_if);
739 event->server_inst = -1;
741 event->status = convert_to_oal_status(status);
742 send_event(OAL_EVENT_BLE_MULTI_ADVERTISING_UPDATE, event, sizeof(event_ble_multiadv_status));
745 static void cb_gatts_multi_adv_set_inst_data(int server_if, int status)
747 BT_INFO("BTGATT SERVER MULTI ADV SET INST DATA CB, status:%d, srv_if: %d", status, server_if);
748 event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
751 if (status != BT_STATUS_SUCCESS)
752 BT_ERR("MULTI ADV SET DATA failed, status:%d, srv_if: %d", status, server_if);
754 for (instance_id = 1 ; instance_id <= NUM_SERVER_INST ; instance_id++) {
755 if (gatt_servers[instance_id - 1].server_id == server_if) {
756 event->server_inst = instance_id;
757 gatt_servers[instance_id - 1].state = GATT_INS_DATA_SET;
761 BT_ERR("Invalid Interface, srv_if: %d", server_if);
762 event->server_inst = -1;
764 event->status = convert_to_oal_status(status);
765 send_event(OAL_EVENT_BLE_MULTI_ADVERTISING_SET_INST_DATA, event, sizeof(event_ble_multiadv_status));
768 oal_status_t gatts_get_att_mtu(int conn_id, int *mtu)
770 int ret = OAL_STATUS_SUCCESS;
771 API_TRACE("Get ATT MTU, conn_id: %d", conn_id);
772 CHECK_OAL_GATT_ENABLED();
773 OAL_CHECK_PARAMETER(mtu, return);
775 /* To prevent crash in case other libraries not support this api */
776 if (gatt_api->server->get_att_mtu == NULL) {
777 BT_WARN("get_att_mtu is NULL");
778 return OAL_STATUS_NOT_SUPPORT;
781 ret = gatt_api->server->get_att_mtu(conn_id, mtu);
782 if (ret != BT_STATUS_SUCCESS) {
783 BT_ERR("GATT MTU Size failed, status: %d", status2string(ret));
784 return convert_to_oal_status(ret);
787 BT_INFO("Current ATT MTU Size: %d", *mtu);
788 return OAL_STATUS_SUCCESS;
791 oal_status_t gatts_add_service(int instance_id, oal_gatt_srvc_id_t *gatt_serv_id,
794 int ret = OAL_STATUS_SUCCESS;
795 btgatt_srvc_id_t btgatt_srvc_id;
796 char str[2*BT_UUID_STRING_MAX];
798 API_TRACE("Server Add Service : instance_id = %d, num_handles = %d", instance_id, num_handles);
799 CHECK_OAL_GATT_ENABLED();
801 CHECK_SERVER_INSTANCE(instance_id);
802 CHECK_SERVER_REGISTRATION(instance_id);
804 if (gatt_serv_id != NULL) {
805 uuid_to_stringname(&(gatt_serv_id->id.uuid), str);
806 API_TRACE("Service uuid: [%s]", str);
807 memcpy(btgatt_srvc_id.id.uuid.uu, gatt_serv_id->id.uuid.uuid, sizeof(gatt_serv_id->id.uuid));
808 btgatt_srvc_id.id.inst_id = gatt_serv_id->id.inst_id;
809 btgatt_srvc_id.is_primary = gatt_serv_id->is_prmry;
811 BT_INFO("GATT Server Service Id is NULL");
812 return OAL_STATUS_INVALID_PARAM;
815 ret = gatt_api->server->add_service(gatt_servers[instance_id - 1].server_id, &btgatt_srvc_id, num_handles);
817 if (ret != BT_STATUS_SUCCESS) {
818 BT_ERR("GATT Server add service failed: %s", status2string(ret));
819 return convert_to_oal_status(ret);
821 return OAL_STATUS_SUCCESS;
824 oal_status_t gatts_add_included_services(int instance_id, int serv_handle,
827 int ret = OAL_STATUS_SUCCESS;
828 API_TRACE("Server Add included Service : instance_id = %d, serv_handle = %d, incl_handle = %d",
829 instance_id, serv_handle, incl_handle);
830 CHECK_OAL_GATT_ENABLED();
832 CHECK_SERVER_INSTANCE(instance_id);
834 CHECK_SERVER_REGISTRATION(instance_id);
836 ret = gatt_api->server->add_included_service(gatt_servers[instance_id - 1].server_id, serv_handle, incl_handle);
837 if (ret != BT_STATUS_SUCCESS) {
838 BT_ERR("GATT Server Add included Service failed: %s", status2string(ret));
839 return convert_to_oal_status(ret);
841 return OAL_STATUS_SUCCESS;
844 oal_status_t gatts_add_characteristics(int instance_id, int serv_handle,
845 oal_uuid_t* charc_uuid, int propts, int permsn)
847 int ret = OAL_STATUS_SUCCESS;
848 char str[2*BT_UUID_STRING_MAX];
849 char prop[MAX_PROP_LEN], perm[MAX_PERM_LEN];
850 char_prop_to_string(propts, prop);
851 char_perm_to_string(permsn, perm);
852 API_TRACE("Server Add Characteristic : instance_id = %d, serv_handle = %d, Properties =%s, Permisison = %s",
853 instance_id, serv_handle, prop, perm);
854 CHECK_OAL_GATT_ENABLED();
856 CHECK_SERVER_INSTANCE(instance_id);
858 CHECK_SERVER_REGISTRATION(instance_id);
860 if (charc_uuid != NULL) {
861 uuid_to_stringname(charc_uuid, str);
862 API_TRACE("uuid: [%s]", str);
865 ret = gatt_api->server->add_characteristic(gatt_servers[instance_id - 1].server_id,
866 serv_handle, (bt_uuid_t*)charc_uuid, propts, permsn);
867 if (ret != BT_STATUS_SUCCESS) {
868 BT_ERR("GATT Server Add Characteristic failed: %s", status2string(ret));
869 return convert_to_oal_status(ret);
871 return OAL_STATUS_SUCCESS;
874 oal_status_t gatts_add_descriptor(int instance_id, int serv_handle,
875 oal_uuid_t* desc_uuid, int permsn)
877 int ret = OAL_STATUS_SUCCESS;
878 char str[2*BT_UUID_STRING_MAX];
879 char perm[MAX_PERM_LEN];
880 char_perm_to_string(permsn, perm);
881 API_TRACE("Server Add Descriptor : instance_id = %d, serv_handle = %d, Permisison = %s",
882 instance_id, serv_handle, perm);
883 CHECK_OAL_GATT_ENABLED();
885 CHECK_SERVER_INSTANCE(instance_id);
887 CHECK_SERVER_REGISTRATION(instance_id);
889 if (desc_uuid != NULL) {
890 uuid_to_stringname(desc_uuid, str);
891 API_TRACE("uuid: [%s]", str);
894 ret = gatt_api->server->add_descriptor(gatt_servers[instance_id - 1].server_id,
895 serv_handle, (bt_uuid_t*)desc_uuid, permsn);
897 if (ret != BT_STATUS_SUCCESS) {
898 BT_ERR("GATT Server Add Descriptor failed: %s", status2string(ret));
899 return convert_to_oal_status(ret);
901 return OAL_STATUS_SUCCESS;
904 oal_status_t gatts_start_service(int instance_id, int svc_handle, int transport)
906 int ret = OAL_STATUS_SUCCESS;
908 API_TRACE("Server Start Service : Ins_id = %d, srvHndl = %d, transport = %d",
909 instance_id, svc_handle, transport);
910 CHECK_OAL_GATT_ENABLED();
912 CHECK_SERVER_INSTANCE(instance_id);
914 CHECK_SERVER_REGISTRATION(instance_id);
916 ret = gatt_api->server->start_service(gatt_servers[instance_id - 1].server_id, svc_handle, transport);
918 if (ret != BT_STATUS_SUCCESS) {
919 BT_ERR("GATT Server Start Service failed: %s", status2string(ret));
920 return convert_to_oal_status(ret);
922 return OAL_STATUS_SUCCESS;
925 oal_status_t gatts_stop_service(int ins_id, int srv_hdl)
927 int ret = OAL_STATUS_SUCCESS;
929 API_TRACE("Server Stop Service : Ins_id = %d, srvHndl = %d, ", ins_id, srv_hdl);
930 CHECK_OAL_GATT_ENABLED();
932 CHECK_SERVER_INSTANCE(ins_id);
933 CHECK_SERVER_REGISTRATION(ins_id);
935 ret = gatt_api->server->stop_service(gatt_servers[ins_id - 1].server_id, srv_hdl);
936 if (ret != BT_STATUS_SUCCESS) {
937 BT_ERR("GATT Server Stop Service failed: %s", status2string(ret));
938 return convert_to_oal_status(ret);
940 return OAL_STATUS_SUCCESS;
943 oal_status_t gatts_delete_service(int ins_id, int srv_hdl)
945 int ret = OAL_STATUS_SUCCESS;
947 API_TRACE("Server Delete Service : Ins_id = %d, srvHndl = %d, ", ins_id, srv_hdl);
948 CHECK_OAL_GATT_ENABLED();
950 CHECK_SERVER_INSTANCE(ins_id);
952 CHECK_SERVER_REGISTRATION(ins_id);
954 ret = gatt_api->server->delete_service(gatt_servers[ins_id - 1].server_id, srv_hdl);
955 if (ret != BT_STATUS_SUCCESS) {
956 BT_ERR("GATT Server Delete Service failed: %s", status2string(ret));
957 return convert_to_oal_status(ret);
959 return OAL_STATUS_SUCCESS;
962 oal_status_t gatts_send_response(int conn_id, int trans_id, int resp_status, oal_gatt_response_t *response)
964 int ret = OAL_STATUS_SUCCESS;
966 API_TRACE("Server Send Response : ConnId = %d, TransId = %d, ", conn_id, trans_id);
967 CHECK_OAL_GATT_ENABLED();
969 if (response == NULL) {
970 BT_ERR("GATT Server attribute value is empty");
971 return OAL_STATUS_INVALID_PARAM;
974 ret = gatt_api->server->send_response(conn_id, trans_id, resp_status, (btgatt_response_t *)response);
975 if (ret != BT_STATUS_SUCCESS) {
976 BT_ERR("GATT Server Send Response failed: %s", status2string(ret));
977 return convert_to_oal_status(ret);
979 return OAL_STATUS_SUCCESS;
982 oal_status_t gatts_send_indication(int ins_id, int attr_hndl, int conn_id, int len, int confirm, char* value)
984 int ret = OAL_STATUS_SUCCESS;
986 API_TRACE("Server Send Indication : Ins_id = %d, srvHndl = %d, [%s]",
987 ins_id, attr_hndl, confirm == TRUE ? "INDICATION" : "NOTIFICATION");
988 CHECK_OAL_GATT_ENABLED();
990 CHECK_SERVER_INSTANCE(ins_id);
992 CHECK_SERVER_REGISTRATION(ins_id);
994 if (value == NULL || len == 0) {
995 BT_ERR("GATT Server attribute value is empty");
996 return OAL_STATUS_INVALID_PARAM;
999 ret = gatt_api->server->send_indication(gatt_servers[ins_id - 1].server_id,
1000 attr_hndl, conn_id, len, confirm, value);
1002 if (ret != BT_STATUS_SUCCESS) {
1003 BT_ERR("GATT Server Send Indication failed: %s", status2string(ret));
1004 return convert_to_oal_status(ret);
1006 return OAL_STATUS_SUCCESS;
1009 oal_status_t gatts_update_att_value(int ins_id, oal_gatt_value_t *value)
1011 int ret = OAL_STATUS_SUCCESS;
1013 CHECK_OAL_GATT_ENABLED();
1015 CHECK_SERVER_INSTANCE(ins_id);
1017 CHECK_SERVER_REGISTRATION(ins_id);
1019 if (value == NULL || value->len == 0) {
1020 BT_ERR("GATT Server attribute value is empty");
1021 return OAL_STATUS_INVALID_PARAM;
1024 API_TRACE("Server Update Value: Ins_id = [%d], att handle = [%d] data len [%d]",
1025 ins_id, value->handle, value->len);
1027 ret = gatt_api->server->update_att_value(gatt_servers[ins_id - 1].server_id,
1028 (int)value->handle, (int)value->len, (char*)value->value);
1030 if (ret != BT_STATUS_SUCCESS) {
1031 BT_ERR("GATT Server Update Attribute Value failed: [%s]", status2string(ret));
1032 return convert_to_oal_status(ret);
1034 return OAL_STATUS_SUCCESS;
1037 /* GATT Server Callbacks:Start */
1038 static void cb_gatts_service_added(int status, int server_if,
1039 btgatt_srvc_id_t *psrvc_id, int srvc_handle)
1043 BT_INFO("BTGATT SERVER SERVICE ADDED CB, status:%d, handle:%d", status, srvc_handle);
1044 event_gatts_srvc_prm_t* event = g_new0(event_gatts_srvc_prm_t, 1);
1046 CHECK_CORRECT_SERVER_ID(instance_id, server_if, event->gatt_srvc_stat.server_inst);
1048 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1049 event->gatt_srvc_stat.server_inst = -1;
1051 event->gatt_srvc_stat.servic_hndl = srvc_handle;
1052 if (psrvc_id != NULL) {
1053 event->gatt_srvc_id.id.inst_id = psrvc_id->id.inst_id;
1054 //event->gatt_srvc_id.id.uuid = psrvc_id->id.uuid;
1055 memcpy(event->gatt_srvc_id.id.uuid.uuid, psrvc_id->id.uuid.uu, sizeof(psrvc_id->id.uuid));
1056 event->gatt_srvc_id.is_prmry = psrvc_id->is_primary;
1059 event->gatt_srvc_stat.status = convert_to_oal_status(status);
1061 send_event(OAL_EVENT_GATTS_SERVICE_ADDED, event, sizeof(event_gatts_srvc_prm_t));
1064 static void cb_gatts_included_service_added(int status, int server_if,
1066 int incl_srvc_handle)
1069 BT_INFO("BTGATT SERVER INCLUDED SERVICE ADDED CB, status:%d, srvc handle:%d, incl handle:%d",
1070 status, srvc_handle, incl_srvc_handle);
1072 event_gatts_incld_srvc_t* event = g_new0(event_gatts_incld_srvc_t, 1);
1074 CHECK_CORRECT_SERVER_ID(ins_id, server_if, event->gatt_srvc_stat.server_inst);
1076 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1077 event->gatt_srvc_stat.server_inst = -1;
1079 event->gatt_srvc_stat.servic_hndl = srvc_handle;
1080 event->incl_srvc_hndl = incl_srvc_handle;
1081 event->gatt_srvc_stat.status = convert_to_oal_status(status);
1082 send_event(OAL_EVENT_GATTS_INCLUDED_SERVICE_ADDED, event, sizeof(event_gatts_incld_srvc_t));
1085 static void cb_gatts_characteristic_added(int status, int server_if, bt_uuid_t *char_id,
1086 int srvc_handle, int char_handle)
1090 BT_INFO("BTGATT SERVER CHARACTERISTIC ADDED CB, status:%d, srvc handle:%d, char handle:%d",
1091 status, srvc_handle, char_handle);
1093 event_gatts_srvc_charctr_t* event = g_new0(event_gatts_srvc_charctr_t, 1);
1095 CHECK_CORRECT_SERVER_ID(Ins_id, server_if, event->gatt_srvc_stat.server_inst);
1097 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1098 event->gatt_srvc_stat.server_inst = -1;
1100 event->gatt_srvc_stat.servic_hndl = srvc_handle;
1101 event->charctr_hndl = char_handle;
1102 event->gatt_srvc_stat.status = convert_to_oal_status(status);
1103 memcpy(event->charctr_uuid.uuid, char_id->uu, sizeof(bt_uuid_t));
1104 send_event(OAL_EVENT_GATTS_CHARACTERISTIC_ADDED, event, sizeof(event_gatts_srvc_charctr_t));
1107 static void cb_gatts_descriptor_added(int status, int server_if,
1108 bt_uuid_t *descr_id, int srvc_handle,
1112 BT_INFO("BTGATT SERVER DESCRIPTOR ADDED CB, status:%d, srvc handle:%d, desc handle:%d",
1113 status, srvc_handle, descr_handle);
1115 event_gatts_srvc_descr_t* event = g_new0(event_gatts_srvc_descr_t, 1);
1117 CHECK_CORRECT_SERVER_ID(Ins_id, server_if, event->gatt_srvc_stat.server_inst);
1119 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1120 event->gatt_srvc_stat.server_inst = -1;
1122 event->gatt_srvc_stat.servic_hndl = srvc_handle;
1123 event->descrptr_hndl = descr_handle;
1124 event->gatt_srvc_stat.status = convert_to_oal_status(status);
1125 memcpy(event->descrptr_uuid.uuid, descr_id->uu, sizeof(bt_uuid_t));
1126 send_event(OAL_EVENT_GATTS_DESCRIPTOR_ADDED, event, sizeof(event_gatts_srvc_descr_t));
1129 static void cb_gatts_service_started(int status, int server_if, int srvc_handle)
1132 BT_INFO("BTGATT SERVER SERVICE STARTED CB, status:%d, srvc_handle:%d", status, srvc_handle);
1134 event_gatts_srvc_t* event = g_new0(event_gatts_srvc_t, 1);
1136 CHECK_CORRECT_SERVER_ID(Ins_id, server_if, event->server_inst);
1138 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1139 event->server_inst = -1;
1141 event->servic_hndl = srvc_handle;
1142 event->status = convert_to_oal_status(status);
1143 send_event(OAL_EVENT_GATTS_SERVICE_STARTED, event, sizeof(event_gatts_srvc_t));
1146 static void cb_gatts_service_stopped(int status, int server_if, int srvc_handle)
1149 BT_INFO("BTGATT SERVER SERVICE STOPPED CB, status:%d, srvc_handle:%d", status, srvc_handle);
1151 event_gatts_srvc_t* event = g_new0(event_gatts_srvc_t, 1);
1153 CHECK_CORRECT_SERVER_ID(ins_id, server_if, event->server_inst);
1155 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1156 event->server_inst = -1;
1158 event->servic_hndl = srvc_handle;
1159 event->status = convert_to_oal_status(status);
1160 send_event(OAL_EVENT_GATTS_SERVICE_STOPED, event, sizeof(event_gatts_srvc_t));
1163 static void cb_gatts_service_deleted(int status, int server_if, int srvc_handle)
1166 BT_INFO("BTGATT SERVER SERVICE DELETED CB, status:%d, srvc_handle:%d", status, srvc_handle);
1168 event_gatts_srvc_t* event = g_new0(event_gatts_srvc_t, 1);
1170 CHECK_CORRECT_SERVER_ID(ins_id, server_if, event->server_inst);
1172 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1173 event->server_inst = -1;
1175 event->servic_hndl = srvc_handle;
1176 event->status = convert_to_oal_status(status);
1177 send_event(OAL_EVENT_GATTS_SERVICE_DELETED, event, sizeof(event_gatts_srvc_t));
1180 static void cb_gatts_connection(int conn_id, int server_if, int connected, bt_bdaddr_t *bda)
1183 oal_event_t event_type;
1185 BT_INFO("BTGATT SERVER CONNECTION connnected:%d, conn_id:%d server_if:%d", connected, conn_id, server_if);
1186 event_gatts_conn_t *event = g_new0(event_gatts_conn_t, 1);
1188 for (ins_id = 1 ; ins_id <= NUM_SERVER_INST ; ins_id++) {
1189 if ((gatt_servers[ins_id - 1].server_id == server_if) &&
1190 (gatt_servers[ins_id - 1].state == GATT_INS_ENABLED)) {
1191 event->server_inst = ins_id;
1194 BT_INFO("Server ID:%d recevied server Id:%d\n", gatt_servers[ins_id - 1].server_id, server_if);
1197 if (ins_id == NUM_SERVER_INST+1) {
1198 BT_WARN("Incorrect Server Interface received: %d\n", server_if);
1202 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1203 event->conn_id = conn_id;
1204 event_type = ((connected == TRUE) ? OAL_EVENT_GATTS_CONNECTION_COMPLETED : OAL_EVENT_GATTS_DISCONNECTION_COMPLETED);
1206 send_event(event_type, event, sizeof(event_gatts_conn_t));
1209 static void cb_gatts_request_read(int conn_id, int trans_id, bt_bdaddr_t *bda,
1210 int attr_handle, int offset, bool is_long)
1212 BT_INFO("BTGATT SERVER REQUEST READ CB: conn_id:%d", conn_id);
1214 event_gatts_srvc_read_attr_t* event = g_new0(event_gatts_srvc_read_attr_t, 1);
1216 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1218 event->attr_trans.attr_handle = attr_handle;
1219 event->attr_trans.conn_id = conn_id;
1220 event->attr_trans.trans_id = trans_id;
1221 event->attr_trans.offset = offset;
1222 event->is_long = is_long;
1224 send_event(OAL_EVENT_GATTS_REQUEST_READ, event, sizeof(event_gatts_srvc_read_attr_t));
1227 static void cb_gatts_request_write(int conn_id, int trans_id,
1228 bt_bdaddr_t *bda, int attr_handle,
1229 int offset, int length,
1230 bool need_rsp, bool is_prep, uint8_t* value)
1232 BT_INFO("BTGATT SERVER REQUEST WRITE conn_id:%d", conn_id);
1234 event_gatts_srvc_write_attr_t* event = g_new0(event_gatts_srvc_write_attr_t, 1);
1236 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1238 event->attr_trans.attr_handle = attr_handle;
1239 event->attr_trans.conn_id = conn_id;
1240 event->attr_trans.trans_id = trans_id;
1241 event->attr_trans.offset = offset;
1242 event->need_rsp = need_rsp;
1243 event->is_prep = is_prep;
1245 if (length > 0 && value != NULL) {
1246 if (length > OAL_GATT_MAX_ATTR_LEN)
1247 length = OAL_GATT_MAX_ATTR_LEN;
1248 memcpy(event->value, value, length);
1249 event->length = length;
1251 send_event(OAL_EVENT_GATTS_REQUEST_WRITE, event, sizeof(event_gatts_srvc_write_attr_t));
1254 static void cb_gatts_response_confirmation(int status, int handle)
1256 BT_INFO("BTGATT SERVER RESPONSE CONFIRMATION CB, status:%d, handle:%d", status, handle);
1258 event_gatts_respons_t* event = g_new0(event_gatts_respons_t, 1);
1260 event->hndl = handle;
1261 event->status = convert_to_oal_status(status);
1263 send_event(OAL_EVENT_GATTS_RESPONSE_CONFIRMED, event, sizeof(event_gatts_respons_t));
1266 static void cb_indication_confirmation(int conn_id, int trans_id, int attr_handle, bt_bdaddr_t *bda)
1268 BT_INFO("BTGATT SERVER Indication Confirmation CB, conn_id:%d, trans_id:%d attr_handle:%d",
1269 conn_id, trans_id, attr_handle);
1271 event_gatts_ind_cnfrm_t* event = g_new0(event_gatts_ind_cnfrm_t, 1);
1273 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1275 event->attr_handle = attr_handle;
1276 event->conn_id = conn_id;
1277 event->trans_id = trans_id;
1278 send_event(OAL_EVENT_GATTS_IND_CONFIRM, event, sizeof(event_gatts_ind_cnfrm_t));
1281 static void cb_gatts_mtu_changed(int conn_id, int mtu)
1283 BT_INFO("BTGATT SERVER MTU CHANGED CB, conn_id: %d, mtu_size: %d", conn_id, mtu);
1284 event_gatts_mtu_changed_t *event = g_new0(event_gatts_mtu_changed_t, 1);
1285 event->conn_id = conn_id;
1286 event->mtu_size = mtu;
1287 send_event(OAL_EVENT_GATTS_MTU_CHANGED, event, sizeof(event_gatts_mtu_changed_t));
1291 static void cb_notifcation_changed(int conn_id, int trans_id, int attr_handle, bool notify, bt_bdaddr_t *bda)
1293 BT_INFO("BTGATT SERVER Notication CB, conn_id:%d, trans_id:%d attr_handle:%d Notify:%d",
1294 conn_id, trans_id, attr_handle, notify);
1296 event_gatts_notif_t* event = g_new0(event_gatts_notif_t, 1);
1298 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1300 event->attr_handle = attr_handle;
1301 event->conn_id = conn_id;
1302 event->trans_id = trans_id;
1303 event->notify = notify;
1304 send_event(OAL_EVENT_GATTS_NOTIFICATION, event, sizeof(event_gatts_notif_t));
1309 /* GATT Server Callbacks: End */
1310 /************************************ GATT Client ***********************************/
1311 /* Client Callbacks */
1312 static void cb_gattc_register_app(int status, int clientIf, bt_uuid_t *app_uuid)
1314 char uuid_str[BT_UUID_STRING_MAX];
1315 event_gattc_register_t *event;
1319 /* Check if GATT client registered for Default GATT client UUID */
1320 uuid_to_string((service_uuid_t *)app_uuid, uuid_str);
1321 BT_INFO("UUID:%s", uuid_str);
1323 BT_INFO("BTGATT CLIENT REGISTER APP CB, status:%d, clientIf:%d", status, clientIf);
1324 event = g_new0(event_gattc_register_t, 1);
1325 event->client_if = clientIf;
1326 event->status = (status == 0 ? OAL_STATUS_SUCCESS : OAL_STATUS_INTERNAL_ERROR);
1327 memcpy(event->client_uuid.uuid, app_uuid->uu, sizeof(oal_uuid_t));
1328 send_event(OAL_EVENT_GATTC_REGISTRATION, event, sizeof(event_gattc_register_t));
1331 static void cb_gattc_scan_result(bt_bdaddr_t* bdaddress, int rssi, uint8_t *adv_data)
1333 event_ble_scan_result_info *event;
1335 event = g_new0(event_ble_scan_result_info, 1);
1337 memcpy(event->address.addr, bdaddress->address, BT_ADDRESS_BYTES_NUM);
1338 memcpy(event->adv_data, adv_data, BLE_ADV_DATA_LENGTH);
1339 send_event_bda_trace(OAL_EVENT_BLE_REMOTE_DEVICE_FOUND, event,
1340 sizeof(event_ble_scan_result_info), (bt_address_t *)bdaddress);
1343 static void cb_gattc_connection(int conn_id, int status, int client_if, bt_bdaddr_t* bda)
1345 event_gattc_conn_t *event;
1346 BT_INFO("BTGATT CLIENT CONNECTED CB, status:[%d] client_if[%d] id[%d]",
1347 status, client_if, conn_id);
1349 event = g_new0(event_gattc_conn_t, 1);
1350 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1351 event->client_if = client_if;
1353 if (BT_STATUS_SUCCESS != status) {
1354 event->status = OAL_STATUS_INTERNAL_ERROR;
1355 BT_ERR("gattc connection Error: %d", status);
1357 event->conn_id = conn_id;
1358 event->status = OAL_STATUS_SUCCESS;
1361 send_event_bda_trace(OAL_EVENT_GATTC_CONNECTION_COMPLETED, event,
1362 sizeof(*event), (bt_address_t *)bda);
1366 static void cb_gattc_disconnect(int conn_id, int status, int client_if, bt_bdaddr_t* bda)
1368 event_gattc_conn_t *event;
1369 BT_INFO("BTGATT Client Disconnect Complete cb, status:[%d], client_if[%d] id[%d]",
1370 status, client_if, conn_id);
1371 event = g_new0(event_gattc_conn_t, 1);
1372 event->status = convert_to_oal_status(status);
1373 event->client_if = client_if;
1374 event->conn_id = conn_id;
1375 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1376 send_event_bda_trace(OAL_EVENT_GATTC_DISCONNECTION_COMPLETED, event,
1377 sizeof(*event), (bt_address_t *)bda);
1380 static void cb_gattc_search_result(int conn_id, btgatt_srvc_id_t *srvc_id)
1382 char uuid_str[2*BT_UUID_STRING_MAX];
1384 BT_INFO("BTGATT Client Service Search Result cb, conn_id:%d", conn_id);
1386 uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str);
1387 BT_INFO("Service=> UUID: [%s], Inst_id: [%u], Type: [%s]",
1388 uuid_str, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1390 event_gattc_service_result_t *event = g_new0(event_gattc_service_result_t, 1);
1391 event->conn_status.status = OAL_STATUS_SUCCESS;
1392 event->conn_status.conn_id = conn_id;
1393 memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1395 send_event(OAL_EVENT_GATTC_SERVICE_SEARCH_RESULT, event, sizeof(*event));
1398 static void cb_gattc_search_complete(int conn_id, int status)
1400 BT_INFO("BTGATT Client Service Search Complete cb, status:%d, conn_id:%d",
1403 event_gattc_conn_status_t *event = g_new0(event_gattc_conn_status_t, 1);
1404 event->conn_id = conn_id;
1405 event->status = convert_to_oal_status(status);
1407 send_event(OAL_EVENT_GATTC_SERVICE_SEARCH_DONE, event, sizeof(*event));
1411 static void cb_gattc_get_characteristics(int conn_id, int status, btgatt_srvc_id_t *srvc_id,
1412 btgatt_gatt_id_t *char_id, int char_prop)
1414 char uuid_str1[2*BT_UUID_STRING_MAX];
1415 char uuid_str2[2*BT_UUID_STRING_MAX];
1417 BT_INFO("BTGATT Client Get Characteristic Callback, conn_id:%d, status:%d", conn_id, status);
1418 uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str1);
1419 BT_INFO("Service=> UUID: [%s], Inst_id: [%u], Type: [%s]",
1420 uuid_str1, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1422 event_gattc_characteristic_result_t *event = g_new0(event_gattc_characteristic_result_t, 1);
1423 event->conn_status.conn_id = conn_id;
1424 event->conn_status.status = convert_to_oal_status(status);
1425 memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1428 uuid_to_stringname((oal_uuid_t *)&(char_id->uuid), uuid_str2);
1429 BT_INFO("Charac=> UUID: [%s], Inst_id: [%u], Prop: [%s]",
1430 uuid_str2, char_id->inst_id, char_prop_to_string(char_prop, str));
1431 event->char_prop = char_prop;
1432 memcpy(&(event->char_id), char_id, sizeof(oal_gatt_id_t));
1434 send_event(OAL_EVENT_GATTC_CHARAC_SERACH_RESULT, event, sizeof(*event));
1439 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)
1441 char uuid_str1[2*BT_UUID_STRING_MAX];
1442 char uuid_str2[2*BT_UUID_STRING_MAX];
1443 char uuid_str3[2*BT_UUID_STRING_MAX];
1444 BT_INFO("BTGATT Client Get Descriptor Callback, conn_id:%d, status:%d", conn_id, status);
1445 uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str1);
1446 uuid_to_stringname((oal_uuid_t *)&(char_id->uuid), uuid_str2);
1447 BT_INFO("Service=> UUID: [%s], Inst_id: [%u], Type: [%s]",
1448 uuid_str1, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1449 BT_INFO("Charac=> UUID: [%s], Inst_id: [%u]", uuid_str2, char_id->inst_id);
1451 event_gattc_descriptor_result_t *event = g_new0(event_gattc_descriptor_result_t, 1);
1452 event->conn_status.conn_id = conn_id;
1453 event->conn_status.status = convert_to_oal_status(status);
1454 memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1455 memcpy(&(event->char_id), char_id, sizeof(oal_gatt_id_t));
1458 uuid_to_stringname((oal_uuid_t *)&(descr_id->uuid), uuid_str3);
1459 BT_INFO("Desc=> UUID: [%s], Inst_id: [%u]", uuid_str3, descr_id->inst_id);
1460 memcpy(&(event->descr_id), descr_id, sizeof(oal_gatt_id_t));
1462 send_event(OAL_EVENT_GATTC_DESC_SERACH_RESULT, event, sizeof(*event));
1465 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)
1467 char uuid_str1[2*BT_UUID_STRING_MAX];
1468 char uuid_str2[2*BT_UUID_STRING_MAX];
1469 BT_INFO("BTGATT Client Register For Notification Callback, client_if:%d, status:%d, registered: %s",
1470 client_if, status, registered == 1 ? "REGISTERED" : "DEREGISTERED");
1471 uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str1);
1472 uuid_to_stringname((oal_uuid_t *)&(char_id->uuid), uuid_str2);
1473 BT_INFO("Service=> UUID: [%s], Inst_id: [%u], Type: [%s]", uuid_str1, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1474 BT_INFO("Charac=> UUID: [%s], Inst_id: [%u]", uuid_str2, char_id->inst_id);
1476 event_gattc_regdereg_notify_t *event = g_new0(event_gattc_regdereg_notify_t, 1);
1477 oal_event_t event_type = (registered == 1 ? OAL_EVENT_GATTC_NOTIFICATION_REGISTERED : OAL_EVENT_GATTC_NOTIFICATION_DEREGISTERED);
1478 event->client_if = client_if;
1479 event->status = convert_to_oal_status(status);
1480 memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1481 memcpy(&(event->char_id), char_id, sizeof(oal_gatt_id_t));
1483 send_event(event_type, event, sizeof(*event));
1487 static void cb_gattc_read_characteristic(int conn_id, int status, btgatt_read_params_t *p_data)
1489 char uuid_str1[2*BT_UUID_STRING_MAX];
1490 char uuid_str2[2*BT_UUID_STRING_MAX];
1491 BT_INFO("BTGATT Client Read Charcateristic Callback, conn_id:%d, read_status:%d", conn_id, status);
1493 event_gattc_read_data *event = g_new0(event_gattc_read_data, 1);
1494 event->uuid_status.conn_status.conn_id = conn_id;
1495 event->uuid_status.conn_status.status = convert_to_oal_status(status);
1496 event->value_type = p_data->value_type;
1497 memcpy(&(event->uuid_status.srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1498 memcpy(&(event->uuid_status.char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1500 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1501 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1502 BT_INFO("Service=> UUID: [%s], Inst_id: [%u], Type: [%s]", uuid_str1,
1503 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1504 BT_INFO("Charac=> UUID: [%s], Inst_id: [%u]", uuid_str2, p_data->char_id.inst_id);
1505 BT_INFO("Len: %u, value_type: %u", p_data->value.len, p_data->value_type);
1506 if (p_data->value.len > 0 && event->value_type == GATTC_READ_VALUE_TYPE_VALUE) {
1508 data = g_malloc(3*p_data->value.len+1);
1510 BT_ERR("memory allocation failed");
1514 convert_hex_2_str((unsigned char *)p_data->value.value, p_data->value.len, data);
1515 BT_INFO("Read Data: [%s]", data);
1516 event->data_len = p_data->value.len;
1517 memcpy(&(event->data), &(p_data->value.value), event->data_len);
1521 send_event(OAL_EVENT_GATTC_READ_CHARAC, event, sizeof(*event));
1524 static void cb_gattc_write_characteristic(int conn_id, int status, btgatt_write_params_t *p_data)
1526 char uuid_str1[2*BT_UUID_STRING_MAX];
1527 char uuid_str2[2*BT_UUID_STRING_MAX];
1528 BT_INFO("BTGATT Client Write Charcateristic Callback, conn_id:%d, write_status:%d", conn_id, status);
1530 event_gattc_write_data *event = g_new0(event_gattc_write_data, 1);
1531 event->conn_status.conn_id = conn_id;
1532 event->conn_status.status = convert_to_oal_status(status);
1533 memcpy(&(event->srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1534 memcpy(&(event->char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1536 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1537 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1538 BT_INFO("Service=> UUID: [%s], Inst_id: [%u], Type: [%s]", uuid_str1,
1539 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1540 BT_INFO("Charac=> UUID: [%s], Inst_id: [%u]", uuid_str2, p_data->char_id.inst_id);
1542 send_event(OAL_EVENT_GATTC_WRITE_CHARAC, event, sizeof(*event));
1545 static void cb_gattc_write_descriptor(int conn_id, int status, btgatt_write_params_t *p_data)
1547 char uuid_str1[2*BT_UUID_STRING_MAX];
1548 char uuid_str2[2*BT_UUID_STRING_MAX];
1549 char uuid_str3[2*BT_UUID_STRING_MAX];
1550 BT_INFO("BTGATT Client Write Descriptor Callback, conn_id:%d, write_status:%d", conn_id, status);
1552 event_gattc_write_data *event = g_new0(event_gattc_write_data, 1);
1553 event->conn_status.conn_id = conn_id;
1554 event->conn_status.status = convert_to_oal_status(status);
1555 memcpy(&(event->srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1556 memcpy(&(event->char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1557 memcpy(&(event->descr_id), &(p_data->descr_id), sizeof(oal_gatt_id_t));
1559 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1560 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1561 uuid_to_stringname((oal_uuid_t *)&(p_data->descr_id.uuid), uuid_str3);
1562 BT_INFO("Service=> UUID: [%s], Inst_id: [%u], Type: [%s]", uuid_str1,
1563 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1564 BT_INFO("Charac=> UUID: [%s], Inst_id: [%u]", uuid_str2, p_data->char_id.inst_id);
1565 BT_INFO("Desc=> UUID: [%s], Inst_id: [%u]", uuid_str3, p_data->descr_id.inst_id);
1567 send_event(OAL_EVENT_GATTC_WRITE_DESCR, event, sizeof(*event));
1572 static void cb_gattc_read_descriptor(int conn_id, int status, btgatt_read_params_t *p_data)
1574 char uuid_str1[2*BT_UUID_STRING_MAX];
1575 char uuid_str2[2*BT_UUID_STRING_MAX];
1576 char uuid_str3[2*BT_UUID_STRING_MAX];
1577 BT_INFO("BTGATT Client Read Descriptor Callback, conn_id:%d, read_status:%d", conn_id, status);
1579 event_gattc_read_data *event = g_new0(event_gattc_read_data, 1);
1580 event->uuid_status.conn_status.conn_id = conn_id;
1581 event->uuid_status.conn_status.status = convert_to_oal_status(status);
1582 event->value_type = p_data->value_type;
1583 memcpy(&(event->uuid_status.srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1584 memcpy(&(event->uuid_status.char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1585 memcpy(&(event->uuid_status.descr_id), &(p_data->descr_id), sizeof(oal_gatt_id_t));
1588 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1589 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1590 uuid_to_stringname((oal_uuid_t *)&(p_data->descr_id.uuid), uuid_str3);
1591 BT_INFO("Service=> UUID: [%s], Inst_id: [%u], Type: [%s]", uuid_str1,
1592 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1593 BT_INFO("Charac=> UUID: [%s], Inst_id: [%u]", uuid_str2, p_data->char_id.inst_id);
1594 BT_INFO("Desc=> UUID: [%s], Inst_id: [%u]", uuid_str3, p_data->descr_id.inst_id);
1595 BT_INFO("Len: %u, value_type: %u", p_data->value.len, p_data->value_type);
1596 if (p_data->value.len > 0 && event->value_type == GATTC_READ_VALUE_TYPE_VALUE) {
1598 data = g_malloc(3*p_data->value.len+1);
1600 BT_ERR("memory allocation failed");
1604 convert_hex_2_str((unsigned char *)p_data->value.value, p_data->value.len, data);
1605 BT_INFO("Read Data: [%s]", data);
1606 event->data_len = p_data->value.len;
1607 memcpy(&(event->data), &(p_data->value.value), event->data_len);
1611 send_event(OAL_EVENT_GATTC_READ_DESCR, event, sizeof(*event));
1614 oal_status_t gattc_start_le_discovery(int client_id)
1617 int ret = OAL_STATUS_SUCCESS;
1619 API_TRACE("BTGATT CLIENT SCAN START");
1620 CHECK_OAL_GATT_ENABLED();
1621 CHECK_CLIENT_REGISTRATION(client_id);
1623 ret = gatt_api->client->scan(client_id, 1);
1624 if (ret != BT_STATUS_SUCCESS) {
1625 BT_ERR("Error:Start LE Discovery failed: %s", status2string(ret));
1626 return convert_to_oal_status(ret);
1628 send_event(OAL_EVENT_BLE_DISCOVERY_STARTED, NULL, 0);
1629 return OAL_STATUS_SUCCESS;
1632 oal_status_t gattc_stop_le_discovery(int client_id)
1635 int ret = OAL_STATUS_SUCCESS;
1637 API_TRACE("Scan is stopped");
1638 CHECK_OAL_GATT_ENABLED();
1639 CHECK_CLIENT_REGISTRATION(client_id);
1640 ret = gatt_api->client->scan(client_id, 0);
1641 if (ret != BT_STATUS_SUCCESS) {
1642 BT_ERR("Error:Stop LE Discovery failed: %s", status2string(ret));
1643 return convert_to_oal_status(ret);
1645 send_event(OAL_EVENT_BLE_DISCOVERY_STOPPED, NULL, 0);
1646 return OAL_STATUS_SUCCESS;
1649 oal_status_t gattc_set_le_scan_param(int scan_type, int itv, int win)
1653 API_TRACE("Scan is stopped");
1654 CHECK_OAL_GATT_ENABLED();
1657 ret = gatt_api->client->set_scan_parameters(scan_type, itv, win);
1658 if (ret != BT_STATUS_SUCCESS) {
1659 BT_ERR("Error:Set scan parameters failed: %s", status2string(ret));
1660 return convert_to_oal_status(ret);
1663 ret = gatt_api->client->set_scan_parameters(itv, win);
1664 if (ret != BT_STATUS_SUCCESS) {
1665 BT_ERR("Error:Set scan parameters failed: %s", status2string(ret));
1666 return convert_to_oal_status(ret);
1670 return OAL_STATUS_SUCCESS;
1672 /************************************ GATT Client ***********************************/
1673 oal_status_t gattc_register(oal_uuid_t* client_uuid)
1676 char str[2*BT_UUID_STRING_MAX];
1677 int ret = OAL_STATUS_SUCCESS;
1679 CHECK_OAL_GATT_ENABLED();
1680 uuid_to_stringname(client_uuid, str);
1681 API_TRACE("uuid: [%s]", str);
1682 /* We will perform actual registration in cb_gattc_register_app callback */
1683 ret = gatt_api->client->register_client((bt_uuid_t *)client_uuid);
1684 if (ret != BT_STATUS_SUCCESS) {
1685 BT_ERR("GATT client register failed: %s", status2string(ret));
1686 return convert_to_oal_status(ret);
1688 return OAL_STATUS_SUCCESS;
1691 oal_status_t gattc_deregister(int client_id)
1693 int ret = OAL_STATUS_SUCCESS;
1695 API_TRACE("GATT client deregister");
1696 CHECK_OAL_GATT_ENABLED();
1697 CHECK_CLIENT_REGISTRATION(client_id);
1699 ret = gatt_api->client->unregister_client(client_id);
1700 if (ret != BT_STATUS_SUCCESS) {
1701 BT_ERR("GATT client deregister failed: %s", status2string(ret));
1702 return convert_to_oal_status(ret);
1705 return OAL_STATUS_SUCCESS;
1708 oal_status_t gattc_connect(int client_id, bt_address_t *device_address, int isDirect)
1710 int ret = OAL_STATUS_SUCCESS;
1713 OAL_CHECK_PARAMETER(device_address, return);
1714 API_TRACE("Client Connect: [%s]", bdt_bd2str(device_address, &bdstr));
1715 CHECK_OAL_GATT_ENABLED();
1716 CHECK_CLIENT_REGISTRATION(client_id);
1718 /* Handle the actual connection in cb_gattc_connection callback */
1719 ret = gatt_api->client->connect(client_id, (bt_bdaddr_t *)device_address, isDirect);
1720 if (ret != BT_STATUS_SUCCESS) {
1721 BT_ERR("GATT client connect failed: %s", status2string(ret));
1722 return convert_to_oal_status(ret);
1724 return OAL_STATUS_SUCCESS;
1727 oal_status_t gattc_disconnect(int client_id, bt_address_t *device_address, int conn_id)
1730 int ret = OAL_STATUS_SUCCESS;
1733 OAL_CHECK_PARAMETER(device_address, return);
1734 API_TRACE("Client Disconnect: [%s]", bdt_bd2str(device_address, &bdstr));
1735 CHECK_OAL_GATT_ENABLED();
1736 CHECK_CLIENT_REGISTRATION(client_id);
1737 CHECK_CLIENT_CONNECTION(conn_id);
1739 /* Handle actual disconnection in callback */
1740 ret = gatt_api->client->disconnect(client_id, (bt_bdaddr_t *) device_address, conn_id);
1741 if (ret != BT_STATUS_SUCCESS) {
1742 BT_ERR("GATT client disconnect failed: %s", status2string(ret));
1743 return convert_to_oal_status(ret);
1745 return OAL_STATUS_SUCCESS;
1749 oal_status_t gattc_search_service(int conn_id, oal_uuid_t *service_uuid)
1751 int ret = OAL_STATUS_SUCCESS;
1752 char uuid_str[2*BT_UUID_STRING_MAX];
1755 uuid_to_stringname(service_uuid, uuid_str);
1756 API_TRACE("Client Service Search UUID: [%s]", uuid_str);
1758 API_TRACE("Client Service Search All");
1760 CHECK_OAL_GATT_ENABLED();
1761 CHECK_CLIENT_CONNECTION(conn_id);
1762 ret = gatt_api->client->search_service(conn_id, (bt_uuid_t *)service_uuid);
1764 if (ret != BT_STATUS_SUCCESS) {
1765 BT_ERR("GATT client service search failed: %s", status2string(ret));
1766 return convert_to_oal_status(ret);
1768 return OAL_STATUS_SUCCESS;
1771 oal_status_t gattc_get_characteristic(int conn_id, oal_gatt_srvc_id_t *srvc_id, oal_gatt_id_t *char_id)
1773 int ret = OAL_STATUS_SUCCESS;
1774 char uuid_str[2*BT_UUID_STRING_MAX];
1776 OAL_CHECK_PARAMETER(srvc_id, return);
1777 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str);
1778 API_TRACE("Client Get Characteristic, Service_uuid: [%s]", uuid_str);
1779 CHECK_OAL_GATT_ENABLED();
1780 CHECK_CLIENT_CONNECTION(conn_id);
1781 ret = gatt_api->client->get_characteristic(conn_id, (btgatt_srvc_id_t *)srvc_id,
1782 (btgatt_gatt_id_t *)char_id);
1783 if (ret != BT_STATUS_SUCCESS) {
1784 BT_ERR("GATT client get characteristic failed: %s", status2string(ret));
1785 return convert_to_oal_status(ret);
1787 return OAL_STATUS_SUCCESS;
1790 oal_status_t gattc_get_descriptor(int conn_id, oal_gatt_srvc_id_t *srvc_id,
1791 oal_gatt_id_t *char_id, oal_gatt_id_t *desc_id)
1793 int ret = OAL_STATUS_SUCCESS;
1794 char uuid_str1[2*BT_UUID_STRING_MAX];
1795 char uuid_str2[2*BT_UUID_STRING_MAX];
1797 OAL_CHECK_PARAMETER(srvc_id, return);
1798 OAL_CHECK_PARAMETER(char_id, return);
1799 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1800 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1801 API_TRACE("Client Get Descriptor, Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
1802 CHECK_OAL_GATT_ENABLED();
1803 CHECK_CLIENT_CONNECTION(conn_id);
1804 ret = gatt_api->client->get_descriptor(conn_id, (btgatt_srvc_id_t *)srvc_id,
1805 (btgatt_gatt_id_t *)char_id, (btgatt_gatt_id_t *)desc_id);
1806 if (ret != BT_STATUS_SUCCESS) {
1807 BT_ERR("GATT client get descriptor failed: %s", status2string(ret));
1808 return convert_to_oal_status(ret);
1810 return OAL_STATUS_SUCCESS;
1813 oal_status_t gattc_register_for_notification(int client_id, bt_address_t * address,
1814 oal_gatt_srvc_id_t *srvc_id, oal_gatt_id_t *char_id) {
1815 int ret = OAL_STATUS_SUCCESS;
1816 char uuid_str1[2*BT_UUID_STRING_MAX];
1817 char uuid_str2[2*BT_UUID_STRING_MAX];
1820 OAL_CHECK_PARAMETER(address, return);
1821 OAL_CHECK_PARAMETER(srvc_id, return);
1822 OAL_CHECK_PARAMETER(char_id, return);
1823 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1824 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1825 API_TRACE("Client Register Notification: [%s], Service_uuid: [%s], Char_uuid: [%s]", bdt_bd2str(address, &bdstr), uuid_str1, uuid_str2);
1826 CHECK_OAL_GATT_ENABLED();
1827 CHECK_CLIENT_REGISTRATION(client_id);
1829 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);
1830 if (ret != BT_STATUS_SUCCESS) {
1831 BT_ERR("GATT client register notification failed: %s", status2string(ret));
1832 return convert_to_oal_status(ret);
1835 return OAL_STATUS_SUCCESS;
1838 oal_status_t gattc_deregister_for_notification(int client_id, bt_address_t * address,
1839 oal_gatt_srvc_id_t *srvc_id, oal_gatt_id_t *char_id) {
1840 int ret = OAL_STATUS_SUCCESS;
1841 char uuid_str1[2*BT_UUID_STRING_MAX];
1842 char uuid_str2[2*BT_UUID_STRING_MAX];
1845 OAL_CHECK_PARAMETER(address, return);
1846 OAL_CHECK_PARAMETER(srvc_id, return);
1847 OAL_CHECK_PARAMETER(char_id, return);
1848 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1849 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1850 API_TRACE("Client Deregister Notification: [%s], Service_uuid: [%s], Char_uuid: [%s]", bdt_bd2str(address, &bdstr), uuid_str1, uuid_str2);
1851 CHECK_OAL_GATT_ENABLED();
1852 CHECK_CLIENT_REGISTRATION(client_id);
1854 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);
1855 if (ret != BT_STATUS_SUCCESS) {
1856 BT_ERR("GATT client deregister notification failed: %s", status2string(ret));
1857 return convert_to_oal_status(ret);
1860 return OAL_STATUS_SUCCESS;
1864 oal_status_t gattc_read_characteristic(int conn_id, oal_gatt_srvc_id_t *srvc_id,
1865 oal_gatt_id_t *char_id, oal_gatt_auth_req_t auth_req)
1867 int ret = OAL_STATUS_SUCCESS;
1868 char uuid_str1[2*BT_UUID_STRING_MAX];
1869 char uuid_str2[2*BT_UUID_STRING_MAX];
1871 OAL_CHECK_PARAMETER(srvc_id, return);
1872 OAL_CHECK_PARAMETER(char_id, return);
1873 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1874 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1875 API_TRACE("Client Read Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
1876 CHECK_OAL_GATT_ENABLED();
1877 CHECK_CLIENT_CONNECTION(conn_id);
1879 ret = gatt_api->client->read_characteristic(conn_id, (btgatt_srvc_id_t *)srvc_id,
1880 (btgatt_gatt_id_t *)char_id, auth_req);
1881 if (ret != BT_STATUS_SUCCESS) {
1882 BT_ERR("GATT client read characteristic failed: %s", status2string(ret));
1883 return convert_to_oal_status(ret);
1886 return OAL_STATUS_SUCCESS;
1889 oal_status_t gattc_read_descriptor(int conn_id, oal_gatt_srvc_id_t *srvc_id,
1890 oal_gatt_id_t *char_id, oal_gatt_id_t *desc_id, oal_gatt_auth_req_t auth_req)
1892 int ret = OAL_STATUS_SUCCESS;
1893 char uuid_str1[2*BT_UUID_STRING_MAX];
1894 char uuid_str2[2*BT_UUID_STRING_MAX];
1895 char uuid_str3[2*BT_UUID_STRING_MAX];
1897 OAL_CHECK_PARAMETER(srvc_id, return);
1898 OAL_CHECK_PARAMETER(char_id, return);
1899 OAL_CHECK_PARAMETER(desc_id, return);
1900 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1901 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1902 uuid_to_stringname(&(desc_id->uuid), uuid_str3);
1903 API_TRACE("Client Read Descriptor: Service_uuid: [%s], Char_uuid: [%s], Desc_uuid: [%s]",
1904 uuid_str1, uuid_str2, uuid_str3);
1905 CHECK_OAL_GATT_ENABLED();
1906 CHECK_CLIENT_CONNECTION(conn_id);
1908 ret = gatt_api->client->read_descriptor(conn_id, (btgatt_srvc_id_t *)srvc_id,
1909 (btgatt_gatt_id_t *)char_id, (btgatt_gatt_id_t *)desc_id, auth_req);
1910 if (ret != BT_STATUS_SUCCESS) {
1911 BT_ERR("GATT client read descriptor failed: %s", status2string(ret));
1912 return convert_to_oal_status(ret);
1915 return OAL_STATUS_SUCCESS;
1918 oal_status_t gattc_write_characteristic(int conn_id, oal_gatt_srvc_id_t *srvc_id,
1919 oal_gatt_id_t *char_id, oal_gatt_write_type_t write_type,
1920 int len, oal_gatt_auth_req_t auth_req, char* data)
1922 int ret = OAL_STATUS_SUCCESS;
1923 char uuid_str1[2*BT_UUID_STRING_MAX];
1924 char uuid_str2[2*BT_UUID_STRING_MAX];
1925 OAL_CHECK_PARAMETER(srvc_id, return);
1926 OAL_CHECK_PARAMETER(char_id, return);
1927 OAL_CHECK_PARAMETER(data, return);
1928 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1929 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1930 API_TRACE("Client Write Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
1931 CHECK_OAL_GATT_ENABLED();
1932 CHECK_CLIENT_CONNECTION(conn_id);
1934 ret = gatt_api->client->write_characteristic(conn_id, (btgatt_srvc_id_t *)srvc_id,
1935 (btgatt_gatt_id_t *)char_id, write_type, len, auth_req, data);
1936 if (ret != BT_STATUS_SUCCESS) {
1937 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
1938 return convert_to_oal_status(ret);
1941 return OAL_STATUS_SUCCESS;
1944 oal_status_t gattc_write_descriptor(int conn_id, oal_gatt_srvc_id_t *srvc_id,
1945 oal_gatt_id_t *char_id, oal_gatt_id_t *desc_id,
1946 oal_gatt_write_type_t write_type, int len, oal_gatt_auth_req_t auth_req, char* data)
1948 int ret = OAL_STATUS_SUCCESS;
1949 char uuid_str1[2*BT_UUID_STRING_MAX];
1950 char uuid_str2[2*BT_UUID_STRING_MAX];
1951 char uuid_str3[2*BT_UUID_STRING_MAX];
1952 OAL_CHECK_PARAMETER(srvc_id, return);
1953 OAL_CHECK_PARAMETER(char_id, return);
1954 OAL_CHECK_PARAMETER(desc_id, return);
1955 OAL_CHECK_PARAMETER(data, return);
1956 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1957 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1958 uuid_to_stringname(&(desc_id->uuid), uuid_str3);
1959 API_TRACE("Client Write Descriptor: Service_uuid: [%s], Char_uuid: [%s], Desc_uuid: [%s]",
1960 uuid_str1, uuid_str2, uuid_str3);
1961 CHECK_OAL_GATT_ENABLED();
1962 CHECK_CLIENT_CONNECTION(conn_id);
1964 ret = gatt_api->client->write_descriptor(conn_id, (btgatt_srvc_id_t *)srvc_id,
1965 (btgatt_gatt_id_t *)char_id, (btgatt_gatt_id_t *)desc_id, write_type, len, auth_req, data);
1966 if (ret != BT_STATUS_SUCCESS) {
1967 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
1968 return convert_to_oal_status(ret);
1971 return OAL_STATUS_SUCCESS;
1974 oal_status_t gattc_conn_param_update(bt_address_t * address, int min, int max, int latency, int timeout)
1979 OAL_CHECK_PARAMETER(address, return);
1980 API_TRACE("Conn Param Update: [%s]", bdt_bd2str(address, &bdstr));
1981 CHECK_OAL_GATT_ENABLED();
1983 BT_INFO("[%s] min[%d] max[%d] latency[%d] timeout[%d]", bdt_bd2str(address, &bdstr), min, max, latency, timeout);
1984 ret = gatt_api->client->conn_parameter_update((bt_bdaddr_t *)address, min, max, latency, timeout);
1985 if (ret != BT_STATUS_SUCCESS) {
1986 BT_ERR("error: %s", status2string(ret));
1987 return convert_to_oal_status(ret);
1989 return OAL_STATUS_SUCCESS;