2 * Open Adaptation Layer (OAL)
4 * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
20 #include <bluetooth.h>
22 #include "oal-event.h"
23 #include "oal-internal.h"
24 #include "oal-manager.h"
25 #include "oal-adapter-mgr.h"
26 #include "oal-utils.h"
28 #include "oal-common.h"
30 #define DEV_APPEARANCE 0x0000
31 #define DEFAULT_GATT_CLIENT_UUID "0000A00A-1111-1111-0123-456789ABCDEF"
32 #define MAX_PROP_LEN 95 /* SVACE WGID: 324403*/
33 #define MAX_PERM_LEN 80 //Fix for svace wgid:324402
35 #define NUM_SERVER_INST 10 // Maximum slots supported by stack is set to 10
36 #define CHECK_OAL_GATT_ENABLED() \
38 if (gatt_api == NULL) { \
39 BT_ERR("GATT Not Enabled"); \
40 return OAL_STATUS_NOT_READY; \
44 #define CHECK_SERVER_INSTANCE(instance_id) \
46 if (instance_id < 1 || instance_id > NUM_SERVER_INST) { \
47 BT_ERR("Invalid Instance"); \
48 return OAL_STATUS_INVALID_PARAM; \
52 #define CHECK_SERVER_REGISTRATION(instance_id) \
54 if (gatt_servers[instance_id - 1].server_id < 0) {\
55 BT_INFO("GATT server registeration is not done for server Instance : %d", instance_id);\
56 return OAL_STATUS_INTERNAL_ERROR;\
60 #define CHECK_CORRECT_SERVER_ID(instance_id, server_if, server_inst)\
62 for (instance_id = 1 ; instance_id <= NUM_SERVER_INST; instance_id++) { \
63 if (gatt_servers[instance_id - 1].server_id == server_if) { \
64 server_inst = instance_id; \
70 #define PRINT_ADV_DATA(length, manuf_data, data_type) \
72 char * buf_str = g_malloc0(3*length + 1);\
73 if (NULL != buf_str) { \
74 convert_hex_2_str(manuf_data, length, buf_str);\
75 if (TRUE == data_type) \
76 BT_INFO("Scan Response Data:%s\n", buf_str);\
78 BT_INFO("Advertising Data:%s\n", buf_str);\
83 #define CHECK_CLIENT_REGISTRATION(client_id) \
85 if (client_id <= 0) { \
86 BT_DBG("No client registered yet...");\
87 return OAL_STATUS_INTERNAL_ERROR;\
91 #define CHECK_CLIENT_CONNECTION(conn_id) \
94 BT_ERR("No active connection");\
95 return OAL_STATUS_INTERNAL_ERROR;\
102 GATT_INS_DATA_SETTING,
114 const char *oal_device_type[] = {
125 } oal_pending_gattc_req_e;
127 static const btgatt_interface_t * gatt_api;
128 static gboolean cur_adv_state[NUM_SERVER_INST];
129 static gatt_server_t gatt_servers[NUM_SERVER_INST];
131 /* Forward declarations of GATT Server callbacks */
132 static void cb_gatts_register_app(int status, int server_if, bt_uuid_t *uuid);
133 static void cb_gatts_multi_adv_enable(int server_if, int status);
134 static void cb_gatts_multi_adv_set_inst_data(int server_if, int status);
135 static void cb_gatts_multi_adv_disable(int server_if, int status);
136 static void cb_gatts_multi_adv_update(int server_if, int status);
138 static void cb_gatts_listen(int status, int server_if);
141 static void cb_gatts_service_added(int status, int server_if, btgatt_srvc_id_t *srvc_id, int srvc_handle);
142 static void cb_gatts_included_service_added(int status, int server_if, int srvc_handle, int incl_srvc_handle);
143 static void cb_gatts_characteristic_added(int status, int server_if, bt_uuid_t *char_id, int srvc_handle, int char_handle);
144 static void cb_gatts_descriptor_added(int status, int server_if, bt_uuid_t *descr_id, int srvc_handle, int descr_handle);
146 static void cb_gatts_service_started(int status, int server_if, int srvc_handle);
147 static void cb_gatts_service_stopped(int status, int server_if, int srvc_handle);
148 static void cb_gatts_service_deleted(int status, int server_if, int srvc_handle);
150 static void cb_gatts_request_read(int conn_id, int trans_id, bt_bdaddr_t *bda, int attr_handle, int offset, bool is_long);
151 static void cb_gatts_request_write(int conn_id, int trans_id, bt_bdaddr_t *bda, int attr_handle, int offset, int length,
152 bool need_rsp, bool is_prep, uint8_t* value);
153 static void cb_gatts_response_confirmation(int status, int handle);
155 static void cb_gatts_acquire_write(int fd, int conn_id, int trans_id, int attr_handle, bt_bdaddr_t*);
156 static void cb_gatts_acquire_notify(int fd, int conn_id, int trans_id, int attr_handle);
158 static void cb_indication_confirmation(int conn_id, int trans_id, int attr_handle, bt_bdaddr_t *bda);
159 static void cb_gatts_connection(int conn_id, int server_if, int connected, bt_bdaddr_t *bda);
161 static void cb_gatts_mtu_changed(int conn_id, int mtu);
164 static void cb_notifcation_changed(int conn_id, int trans_id, int attr_handle, bool notify, bt_bdaddr_t *bda);
167 /************************************HAL Interface *************************************/
169 /*TODO GATT Server callbacks will be implemented in subsequent patches */
170 static const btgatt_server_callbacks_t btgatt_server_callbacks = {
171 cb_gatts_register_app,
173 cb_gatts_service_added,
174 cb_gatts_included_service_added,
175 cb_gatts_characteristic_added,
176 cb_gatts_descriptor_added,
177 cb_gatts_service_started,
178 cb_gatts_service_stopped,
179 cb_gatts_service_deleted,
180 cb_indication_confirmation,
181 cb_gatts_request_read,
182 cb_gatts_request_write,
183 NULL, /*cb_gatts_request_exec_write,*/
184 cb_gatts_response_confirmation,
186 cb_gatts_multi_adv_enable,
187 cb_gatts_multi_adv_update,
188 cb_gatts_multi_adv_set_inst_data,
189 cb_gatts_multi_adv_disable,
190 cb_gatts_mtu_changed,
192 cb_notifcation_changed,
194 cb_gatts_acquire_write,
195 cb_gatts_acquire_notify
198 /* Forward declaration for GATT client callbacks */
199 static void cb_gattc_register_app(int status, int clientIf, bt_uuid_t *app_uuid);
200 static void cb_gattc_scan_result(bt_bdaddr_t* bdaddress, int rssi, uint8_t *adv_data);
201 static void cb_gattc_connection(int conn_id, int status, int client_if, bt_bdaddr_t* bda);
202 static void cb_gattc_disconnect(int conn_id, int status, int client_if, bt_bdaddr_t* bda);
203 static void cb_gattc_search_result(int conn_id, btgatt_srvc_id_t *srvc_id);
204 static void cb_gattc_search_complete(int conn_id, int status);
205 static void cb_gattc_get_characteristics(int conn_id, int status, btgatt_srvc_id_t *srvc_id,
206 btgatt_gatt_id_t *char_id, int char_prop);
207 static void cb_gattc_get_descriptor(int conn_id, int status, btgatt_srvc_id_t *srvc_id,
208 btgatt_gatt_id_t *char_id, btgatt_gatt_id_t *descr_id);
209 static void cb_gattc_read_characteristic(int conn_id, int status, btgatt_read_params_t *p_data);
210 static void cb_gattc_read_descriptor(int conn_id, int status, btgatt_read_params_t *p_data);
211 static void cb_gattc_write_characteristic(int conn_id, int status, btgatt_write_params_t *p_data);
212 static void cb_gattc_write_descriptor(int conn_id, int status, btgatt_write_params_t *p_data);
213 static void cb_gattc_register_for_notification(int conn_id, int registered, int status,
214 btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id);
216 /*TODO GATT CLient callbacks will be implemented in subsequent patches */
217 static const btgatt_client_callbacks_t btgatt_client_callbacks = {
218 cb_gattc_register_app,
219 cb_gattc_scan_result,
220 cb_gattc_connection, /*cb_gattc_connection*/
221 cb_gattc_disconnect, /*cb_gattc_disconnect*/
222 cb_gattc_search_complete, /*cb_gattc_search_complete*/
223 cb_gattc_search_result, /*cb_gattc_search_result*/
224 cb_gattc_get_characteristics, /*cb_gattc_get_characteristics*/
225 cb_gattc_get_descriptor, /*cb_gattc_get_descriptor*/
226 NULL, /*cb_gattc_get_included_service*/
227 cb_gattc_register_for_notification, /*cb_gattc_register_for_notification*/
228 NULL, /*cb_gattc_notify*/
229 cb_gattc_read_characteristic, /*cb_gattc_read_characteristic*/
230 cb_gattc_write_characteristic, /*cb_gattc_write_characteristic*/
231 cb_gattc_read_descriptor, /*cb_gattc_read_descriptor*/
232 cb_gattc_write_descriptor, /*cb_gattc_write_descriptor*/
233 NULL, /*cb_gattc_execute_write*/
234 NULL, /*cb_gattc_remote_rssi*/
235 NULL, /*cb_gattc_configure_mtu_cmpl*/
236 #ifdef PLATFORM_ANDROID_HAL
237 NULL, /*cb_scan_filter_cfg*/
238 NULL, /*cb_scan_filter_param*/
239 NULL, /*cb_scan_filter_status*/
240 NULL, /*cb_congestion*/
241 NULL, /*cb_batchscan_cfg_storag*/
242 NULL, /*cb_batchscan_enb_disable*/
243 NULL, /*cb_batchscan_reports*/
244 NULL, /*cb_batchscan_threshold*/
245 NULL, /*cb_track_adv_event*/
249 static btgatt_callbacks_t btgatt_callbacks = {
250 sizeof(btgatt_callbacks_t),
251 &btgatt_client_callbacks,
252 &btgatt_server_callbacks
255 /*******************************GATT Initialisation - Deinitialisation********************************/
256 oal_status_t gatt_enable(void)
258 const bt_interface_t * blued_api;
262 for (i = 0; i < NUM_SERVER_INST; i++) {
263 gatt_servers[i].server_id = -1;
264 gatt_servers[i].state = GATT_INS_DISABLED;
265 memset(&gatt_servers[i].uuid, 0, sizeof(oal_uuid_t));
266 cur_adv_state[i] = FALSE;
269 /* Get stack interface */
270 blued_api = (const bt_interface_t *) adapter_get_stack_interface();
272 if (blued_api == NULL) {
273 BT_ERR("Stack is not initialized");
274 return OAL_STATUS_NOT_READY;
278 BT_WARN("GATT Interface is already initialized...");
279 return OAL_STATUS_ALREADY_DONE;
282 gatt_api = (const btgatt_interface_t *)blued_api->get_profile_interface(BT_PROFILE_GATT_ID);
283 if (gatt_api == NULL) {
284 BT_ERR("GATT interface failed");
285 return OAL_STATUS_INTERNAL_ERROR;
288 if ((ret = gatt_api->init(&btgatt_callbacks)) != BT_STATUS_SUCCESS) {
289 BT_ERR("Error: Unable to initialise GATT :%s", status2string(ret));
292 return convert_to_oal_status(ret);
295 BT_INFO("GATT successfully initialized");
296 return OAL_STATUS_SUCCESS;
299 oal_status_t gatt_disable(void)
307 for (i = 0; i < NUM_SERVER_INST; i++) {
308 gatt_servers[i].server_id = -1;
309 gatt_servers[i].state = GATT_INS_DISABLED;
310 memset(&gatt_servers[i].uuid, 0, sizeof(oal_uuid_t));
312 return OAL_STATUS_SUCCESS;
315 /************************************GATT Server Functions*************************************/
317 oal_status_t gatts_register(oal_uuid_t* server_uuid)
319 char str[2*BT_UUID_STRING_MAX];
320 int ret = OAL_STATUS_SUCCESS;
323 CHECK_OAL_GATT_ENABLED();
324 uuid_to_stringname((service_uuid_t*)server_uuid->uuid, str);
325 API_TRACE("Register the server instance: UUID: [%s]", str);
327 for (i = 0; i < NUM_SERVER_INST; i++) {
328 if (memcmp(server_uuid->uuid, gatt_servers[i].uuid.uuid, sizeof(oal_uuid_t)) == 0) {
329 if (gatt_servers[i].server_id != -1) {
330 BT_ERR("This is resevered UUID for easy set up application i = %d \
331 gatt_servers[i].server_id = %d", i, gatt_servers[i].server_id);
332 return OAL_STATUS_ALREADY_DONE;
337 ret = gatt_api->server->register_server((bt_uuid_t*)server_uuid);
339 if (ret != BT_STATUS_SUCCESS) {
340 BT_ERR("GATT server registration with UUID:%s failed: %s", str, status2string(ret));
341 return convert_to_oal_status(ret);
343 return OAL_STATUS_SUCCESS;
346 oal_status_t gatts_unregister(int instance_id)
348 int ret = OAL_STATUS_SUCCESS;
349 API_TRACE("Unregister the registered server, instance_id: %d", instance_id);
351 CHECK_OAL_GATT_ENABLED();
352 CHECK_SERVER_INSTANCE(instance_id);
353 CHECK_SERVER_REGISTRATION(instance_id);
355 ret = gatt_api->server->unregister_server(gatt_servers[instance_id - 1].server_id);
357 if (ret != BT_STATUS_SUCCESS) {
358 BT_ERR("GATT server unregistration failed: %d", instance_id);
359 return convert_to_oal_status(ret);
362 gatt_servers[instance_id-1].server_id = -1;
363 gatt_servers[instance_id-1].state = GATT_INS_DISABLED;
365 if (instance_id != 1 && instance_id != 2)
366 memset(&gatt_servers[instance_id-1].uuid, 0, sizeof(oal_uuid_t));
368 return OAL_STATUS_SUCCESS;
371 /* Legacy Advertisement */
372 oal_status_t gatts_start_listen(int instance_id, gboolean enable)
374 int ret = OAL_STATUS_SUCCESS;
375 API_TRACE("server-id:%d enable: %d", gatt_servers[instance_id-1].server_id, enable);
377 CHECK_OAL_GATT_ENABLED();
378 CHECK_SERVER_INSTANCE(instance_id);
379 CHECK_SERVER_REGISTRATION(instance_id);
381 ret = gatt_api->server->listen(gatt_servers[instance_id-1].server_id, enable);
383 if (ret != BT_STATUS_SUCCESS) {
384 BT_ERR("Error:Advertising start/stop on server-id:%d failed With Status: [%s]",
385 gatt_servers[instance_id-1].server_id, status2string(ret));
386 return convert_to_oal_status(ret);
389 return OAL_STATUS_SUCCESS;
392 oal_status_t gatts_stop_advertising(int instance_id)
394 int ret = OAL_STATUS_SUCCESS;
395 API_TRACE("Stop advertising");
397 CHECK_OAL_GATT_ENABLED();
398 CHECK_SERVER_INSTANCE(instance_id);
399 CHECK_SERVER_REGISTRATION(instance_id);
401 ret = gatt_api->server->listen(gatt_servers[instance_id - 1].server_id, FALSE);
403 if (ret != BT_STATUS_SUCCESS) {
404 BT_ERR("Stop Advertising failed for server Instance:%d With Status: %s",
405 instance_id, status2string(ret));
406 return convert_to_oal_status(ret);
408 return OAL_STATUS_SUCCESS;
412 /* Below API's need to be removed as they are hardcoded API's.
413 If at all, product specific API's are required to set specific data, then new naming should
414 be adopted for these API's */
415 oal_status_t gatts_set_advertising_params(int instance_id, float min_intv, float max_intv)
417 int ret = OAL_STATUS_SUCCESS;
419 API_TRACE("Set advertising params min_int:%f, max_int:%f", min_intv, max_intv);
420 CHECK_OAL_GATT_ENABLED();
421 CHECK_SERVER_INSTANCE(instance_id);
422 CHECK_SERVER_REGISTRATION(instance_id);
424 /* Historically used for Legacy Advertising */
425 return OAL_STATUS_NOT_SUPPORT;
428 oal_status_t gatts_set_advertising_data(int instance_id, uint16_t manufac_len, char* manufac_data)
430 int ret = OAL_STATUS_SUCCESS;
432 API_TRACE("Set advertising data");
433 CHECK_OAL_GATT_ENABLED();
434 CHECK_SERVER_INSTANCE(instance_id);
435 CHECK_SERVER_REGISTRATION(instance_id);
437 /* Historically used for Legacy Advertising */
438 return OAL_STATUS_NOT_SUPPORT;
441 oal_status_t gatts_set_scan_response(int instance_id, uint16_t manufac_len, char* manufac_data)
443 int ret = OAL_STATUS_SUCCESS;
445 API_TRACE("Set Scan Response data");
446 CHECK_OAL_GATT_ENABLED();
447 CHECK_SERVER_INSTANCE(instance_id);
448 CHECK_SERVER_REGISTRATION(instance_id);
450 /* Historically used for Legacy Advertising */
451 return OAL_STATUS_NOT_SUPPORT;
455 oal_status_t gatts_multi_adv_enable(int instance_id)
457 int ret = OAL_STATUS_SUCCESS;
458 API_TRACE("Start Multi advertising, srv_if: %d, instance_id: %d",
459 gatt_servers[instance_id - 1].server_id, instance_id);
461 CHECK_OAL_GATT_ENABLED();
462 CHECK_SERVER_INSTANCE(instance_id);
463 CHECK_SERVER_REGISTRATION(instance_id);
465 if (gatt_api->server->multi_adv_enable == NULL)
466 return OAL_STATUS_NOT_SUPPORT;
468 /* Platform Requirement: Advertising to be enabled even when no data is set: i.e Empty Advertisement
469 if (gatt_servers[instance_id - 1].state != GATT_INS_DATA_SET){
470 BT_ERR("Not allowed, state: %d, instance_id: %d",
471 gatt_servers[instance_id - 1].state, instance_id);
472 return OAL_STATUS_BUSY;
475 if (gatt_servers[instance_id - 1].state == GATT_INS_ENABLED) {
476 BT_ERR("Not allowed, state: %d, instance_id: %d",
477 gatt_servers[instance_id - 1].state, instance_id);
478 return OAL_STATUS_ALREADY_DONE;
481 if (gatt_servers[instance_id - 1].state == GATT_INS_ENABLING) {
482 BT_ERR("Not allowed, state: %d, instance_id: %d",
483 gatt_servers[instance_id - 1].state, instance_id);
484 return OAL_STATUS_BUSY;
487 gatt_servers[instance_id - 1].state = GATT_INS_ENABLING;
489 ret = gatt_api->server->multi_adv_enable(gatt_servers[instance_id - 1].server_id);
491 if (ret != BT_STATUS_SUCCESS) {
492 BT_ERR("GATT enable multi advertising failed for server Instance: %d With Status: %s",
493 instance_id, status2string(ret));
494 // gatt_servers[instance_id - 1].state = GATT_INS_DATA_SET;
495 return convert_to_oal_status(ret);
497 return OAL_STATUS_SUCCESS;
500 oal_status_t gatts_multi_adv_disable(int instance_id)
502 int ret = OAL_STATUS_SUCCESS;
503 API_TRACE("Stop Multi advertising, srv_if: %d, instance_id: %d",
504 gatt_servers[instance_id - 1].server_id, instance_id);
506 CHECK_OAL_GATT_ENABLED();
507 CHECK_SERVER_INSTANCE(instance_id);
508 CHECK_SERVER_REGISTRATION(instance_id);
510 if (gatt_api->server->multi_adv_disable == NULL)
511 return OAL_STATUS_NOT_SUPPORT;
513 if ((gatt_servers[instance_id - 1].state != GATT_INS_ENABLED)) {
514 BT_ERR("Not Allowed, state: %d, instance_id: %d",
515 gatt_servers[instance_id - 1].state, instance_id);
516 return OAL_STATUS_BUSY;
519 ret = gatt_api->server->multi_adv_disable(gatt_servers[instance_id - 1].server_id);
521 if (ret != BT_STATUS_SUCCESS) {
522 BT_ERR("GATT disable multi advertising failed for server Instance: %d With Status: %s",
523 instance_id, status2string(ret));
524 return convert_to_oal_status(ret);
526 return OAL_STATUS_SUCCESS;
529 oal_status_t gatts_multi_adv_update(int instance_id,
530 int min_intv, int max_intv,
531 int adv_type, int chnl_map,
532 int tx_power, int timeout_s)
534 int ret = OAL_STATUS_SUCCESS;
535 API_TRACE("Multi advertising Update");
537 CHECK_OAL_GATT_ENABLED();
538 CHECK_SERVER_INSTANCE(instance_id);
539 CHECK_SERVER_REGISTRATION(instance_id);
541 if (gatt_api->server->multi_adv_update == NULL)
542 return OAL_STATUS_NOT_SUPPORT;
544 ret = gatt_api->server->multi_adv_update(gatt_servers[instance_id - 1].server_id,
547 tx_power, timeout_s);
549 if (ret != BT_STATUS_SUCCESS) {
550 BT_ERR("GATT update multi advertising failed for server Instance: %d With Status: %s",
551 instance_id, status2string(ret));
552 return convert_to_oal_status(ret);
554 return OAL_STATUS_SUCCESS;
557 oal_status_t gatts_multi_adv_set_inst_data(int instance_id,
558 oal_ble_multi_adv_param_setup_t *adv_param_setup)
560 int ret = OAL_STATUS_SUCCESS;
561 btgatt_adv_param_setup_t adv_setup;
562 API_TRACE("Set Multi advertising data, srv_if: %d, instance_id: %d",
563 gatt_servers[instance_id - 1].server_id, instance_id);
565 CHECK_OAL_GATT_ENABLED();
566 CHECK_SERVER_INSTANCE(instance_id);
567 CHECK_SERVER_REGISTRATION(instance_id);
569 if (gatt_api->server->multi_adv_set_inst_data == NULL)
570 return OAL_STATUS_NOT_SUPPORT;
572 if ((gatt_servers[instance_id - 1].state >= GATT_INS_DATA_SETTING) && (gatt_servers[instance_id - 1].state != GATT_INS_DATA_SET)) {
573 BT_ERR("Not Allowed, state: %d, instance_id: %d", gatt_servers[instance_id - 1].state, instance_id);
574 return OAL_STATUS_BUSY;
577 gatt_servers[instance_id - 1].state = GATT_INS_DATA_SETTING;
580 adv_setup.set_scan_rsp = adv_param_setup->set_scan_rsp;
581 adv_setup.include_name = adv_param_setup->include_name;
582 adv_setup.include_txpower = adv_param_setup->include_txpower;
583 adv_setup.appearance = adv_param_setup->appearance;
584 adv_setup.manufacturer_data = adv_param_setup->manufacturer_data;
585 adv_setup.manufacturer_data_len = adv_param_setup->manufacturer_data_len;
586 adv_setup.service_data = adv_param_setup->service_data;
587 adv_setup.service_data_len = adv_param_setup->service_data_len;
588 adv_setup.service_uuid = adv_param_setup->service_uuid;
589 adv_setup.service_uuid_len = adv_param_setup->service_uuid_len;
590 /* Solicit UUID handler: Start */
591 adv_setup.solicit_uuid = adv_param_setup->solicit_uuid;
592 adv_setup.solicit_uuid_len = adv_param_setup->solicit_uuid_len;
593 /* Solicit UUID handler: End */
594 adv_setup.min_interval = adv_param_setup->min_interval;
595 adv_setup.max_interval = adv_param_setup->min_interval;
596 adv_setup.adv_type = adv_param_setup->adv_type;
597 adv_setup.chnl_map = adv_param_setup->chnl_map;
598 adv_setup.tx_power = adv_param_setup->tx_power;
599 adv_setup.timeout_s = adv_param_setup->timeout_s;
601 adv_setup.server_if = gatt_servers[instance_id - 1].server_id;
602 if (adv_setup.manufacturer_data_len > 0 && adv_setup.manufacturer_data != NULL) {
603 BT_INFO("Manufacture data.....\n");
604 PRINT_ADV_DATA(adv_setup.manufacturer_data_len, (unsigned char *)adv_setup.manufacturer_data, adv_setup.set_scan_rsp);
607 if (adv_setup.service_uuid_len > 0 && adv_setup.service_uuid != NULL) {
608 BT_INFO("Service uuid.....\n");
609 PRINT_ADV_DATA(adv_setup.service_uuid_len, (unsigned char*)adv_setup.service_uuid, FALSE);
613 ret = gatt_api->server->multi_adv_set_inst_data(adv_setup);
615 if (ret != BT_STATUS_SUCCESS) {
616 BT_ERR("GATT Set Multi advertising data failed: %s", status2string(ret));
617 gatt_servers[instance_id - 1].state = GATT_INS_DISABLED;
618 return convert_to_oal_status(ret);
620 return OAL_STATUS_SUCCESS;
623 /************************************GATT Server Functions*************************************/
625 static void cb_gatts_register_app(int status, int server_if, bt_uuid_t *uuid)
628 char str[2*BT_UUID_STRING_MAX];
629 uuid_to_stringname((service_uuid_t*)uuid, str);
631 event_gatts_register_t *event = g_malloc0(sizeof(event_gatts_register_t));
633 BT_INFO("BTGATT SERVER REGISTER APP CB, status:%d, server_if:%d", status, server_if);
634 BT_INFO("UUID: [%s]", str);
636 for (i = 0; i < NUM_SERVER_INST; i++) {
637 if (gatt_servers[i].server_id == -1) {
638 BT_INFO("Server Instance registered with stack [%d]", i+1);
639 gatt_servers[i].server_id = server_if;
640 event->server_inst = i+1;
641 memcpy(gatt_servers[i].uuid.uuid, uuid->uu, sizeof(bt_uuid_t));
642 memcpy(event->server_uuid.uuid, uuid->uu, sizeof(bt_uuid_t));
646 send_event(OAL_EVENT_BLE_SERVER_INSTANCE_INITIALISED, event, sizeof(event_gatts_register_t));
649 static void cb_gatts_listen(int status, int server_if)
651 gboolean prev_state[NUM_SERVER_INST];
653 event_ble_adv_status *event = g_malloc0(sizeof(event_ble_adv_status));
656 for (instance_id = 1 ; instance_id <= NUM_SERVER_INST ; instance_id++) {
657 if (gatt_servers[instance_id - 1].server_id == server_if) {
658 event->server_inst = instance_id;
661 BT_WARN("Server ID:%d recevied server Id:%d\n", gatt_servers[instance_id - 1].server_id, server_if);
664 if ((event->server_inst > 0) && (event->server_inst <= 10)) {
665 prev_state[event->server_inst - 1] = cur_adv_state[event->server_inst - 1];
666 new_state = (status == BT_STATUS_SUCCESS) ? !(prev_state[event->server_inst - 1]) : (prev_state[event->server_inst - 1]);
667 cur_adv_state[event->server_inst - 1] = new_state;
669 BT_ERR("Invalid Callback...");
674 BT_INFO("Adv State of server instance %d :%d", instance_id, new_state);
676 if (TRUE == new_state)
677 send_event(OAL_EVENT_BLE_ADVERTISING_STARTED, event, sizeof(event_ble_adv_status));
679 send_event(OAL_EVENT_BLE_ADVERTISING_STOPPED, event, sizeof(event_ble_adv_status));
682 static void cb_gatts_multi_adv_enable(int server_if, int status)
685 BT_INFO("BTGATT SERVER MULTI ADV ENABLE CB, status:%d, srv_if: %d", status, server_if);
686 event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
689 if (status != BT_STATUS_SUCCESS)
690 BT_ERR("MULTI ADV ENABLE failed, status:%d, srv_if: %d", status, server_if);
692 for (instance_id = 1 ; instance_id <= NUM_SERVER_INST ; instance_id++) {
693 if (gatt_servers[instance_id - 1].server_id == server_if) {
694 event->server_inst = instance_id;
695 gatt_servers[instance_id - 1].state = GATT_INS_ENABLED;
699 BT_ERR("Invalid Interface, srv_if: %d", server_if);
700 event->server_inst = -1;
702 event->status = convert_to_oal_status(status);
703 send_event(OAL_EVENT_BLE_MULTI_ADVERTISING_ENABLE, event, sizeof(event_ble_multiadv_status));
706 static void cb_gatts_multi_adv_disable(int server_if, int status)
708 BT_INFO("BTGATT SERVER MULTI ADV DISABLE CB, status:%d, srv_if: %d", status, server_if);
709 event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
712 if (status != BT_STATUS_SUCCESS)
713 BT_ERR("MULTI ADV DISABLE failed, status:%d, srv_if: %d", status, server_if);
715 for (instance_id = 1 ; instance_id <= NUM_SERVER_INST; instance_id++) {
716 if (gatt_servers[instance_id - 1].server_id == server_if) {
717 event->server_inst = instance_id;
718 gatt_servers[instance_id - 1].state = GATT_INS_DISABLED;
722 BT_ERR("Invalid Interface, srv_if: %d", server_if);
723 event->server_inst = -1;
725 event->status = convert_to_oal_status(status);
726 send_event(OAL_EVENT_BLE_MULTI_ADVERTISING_DISABLE, event, sizeof(event_ble_multiadv_status));
729 static void cb_gatts_multi_adv_update(int server_if, int status)
731 BT_INFO("BTGATT SERVER MULTI ADV UPDATE CB, status:%d, srv_if: %d", status, server_if);
732 event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
735 if (status != BT_STATUS_SUCCESS)
736 BT_ERR("MULTI ADV UPDATE failed, status:%d, srv_if: %d", status, server_if);
738 for (instance_id = 1 ; instance_id <= NUM_SERVER_INST ; instance_id++) {
739 if (gatt_servers[instance_id - 1].server_id == server_if) {
740 event->server_inst = instance_id;
744 BT_ERR("Invalid Interface, srv_if: %d", server_if);
745 event->server_inst = -1;
747 event->status = convert_to_oal_status(status);
748 send_event(OAL_EVENT_BLE_MULTI_ADVERTISING_UPDATE, event, sizeof(event_ble_multiadv_status));
751 static void cb_gatts_multi_adv_set_inst_data(int server_if, int status)
753 BT_INFO("BTGATT SERVER MULTI ADV SET INST DATA CB, status:%d, srv_if: %d", status, server_if);
754 event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
757 if (status != BT_STATUS_SUCCESS)
758 BT_ERR("MULTI ADV SET DATA failed, status:%d, srv_if: %d", status, server_if);
760 for (instance_id = 1 ; instance_id <= NUM_SERVER_INST ; instance_id++) {
761 if (gatt_servers[instance_id - 1].server_id == server_if) {
762 event->server_inst = instance_id;
763 gatt_servers[instance_id - 1].state = GATT_INS_DATA_SET;
767 BT_ERR("Invalid Interface, srv_if: %d", server_if);
768 event->server_inst = -1;
770 event->status = convert_to_oal_status(status);
771 send_event(OAL_EVENT_BLE_MULTI_ADVERTISING_SET_INST_DATA, event, sizeof(event_ble_multiadv_status));
774 oal_status_t gatts_get_att_mtu(int conn_id, int *mtu)
776 int ret = OAL_STATUS_SUCCESS;
777 API_TRACE("Get ATT MTU, conn_id: %d", conn_id);
778 CHECK_OAL_GATT_ENABLED();
779 OAL_CHECK_PARAMETER(mtu, return);
781 /* To prevent crash in case other libraries not support this api */
782 if (gatt_api->server->get_att_mtu == NULL) {
783 BT_WARN("get_att_mtu is NULL");
784 return OAL_STATUS_NOT_SUPPORT;
787 ret = gatt_api->server->get_att_mtu(conn_id, mtu);
788 if (ret != BT_STATUS_SUCCESS) {
789 BT_ERR("GATT MTU Size failed, status: %d", status2string(ret));
790 return convert_to_oal_status(ret);
793 BT_INFO("Current ATT MTU Size: %d", *mtu);
794 return OAL_STATUS_SUCCESS;
796 oal_status_t gatt_send_response_acquire(int conn_id, int trans_id,
797 int status, int fd, int mtu , void * fdlist)
799 int ret = OAL_STATUS_SUCCESS;
801 API_TRACE("Server Send Response : ConnId = %d, TransId = %d, %d", conn_id, trans_id, fd);
802 CHECK_OAL_GATT_ENABLED();
804 ret = gatt_api->server->send_response_acquire(conn_id, trans_id, status, fd, mtu, fdlist);
805 if (ret != BT_STATUS_SUCCESS) {
806 BT_ERR("GATT Server Send Response failed: %s", status2string(ret));
807 return convert_to_oal_status(ret);
809 return OAL_STATUS_SUCCESS;
812 oal_status_t gatts_add_service(int instance_id, oal_gatt_srvc_id_t *gatt_serv_id,
815 int ret = OAL_STATUS_SUCCESS;
816 btgatt_srvc_id_t btgatt_srvc_id;
817 char str[2*BT_UUID_STRING_MAX];
819 API_TRACE("Server Add Service : instance_id = %d, num_handles = %d", instance_id, num_handles);
820 CHECK_OAL_GATT_ENABLED();
822 CHECK_SERVER_INSTANCE(instance_id);
823 CHECK_SERVER_REGISTRATION(instance_id);
825 if (gatt_serv_id != NULL) {
826 uuid_to_stringname(&(gatt_serv_id->id.uuid), str);
827 API_TRACE("Service uuid: [%s]", str);
828 memcpy(btgatt_srvc_id.id.uuid.uu, gatt_serv_id->id.uuid.uuid, sizeof(gatt_serv_id->id.uuid));
829 btgatt_srvc_id.id.inst_id = gatt_serv_id->id.inst_id;
830 btgatt_srvc_id.is_primary = gatt_serv_id->is_prmry;
832 BT_INFO("GATT Server Service Id is NULL");
833 return OAL_STATUS_INVALID_PARAM;
836 ret = gatt_api->server->add_service(gatt_servers[instance_id - 1].server_id, &btgatt_srvc_id, num_handles);
838 if (ret != BT_STATUS_SUCCESS) {
839 BT_ERR("GATT Server add service failed: %s", status2string(ret));
840 return convert_to_oal_status(ret);
842 return OAL_STATUS_SUCCESS;
845 oal_status_t gatts_add_included_services(int instance_id, int serv_handle,
848 int ret = OAL_STATUS_SUCCESS;
849 API_TRACE("Server Add included Service : instance_id = %d, serv_handle = %d, incl_handle = %d",
850 instance_id, serv_handle, incl_handle);
851 CHECK_OAL_GATT_ENABLED();
853 CHECK_SERVER_INSTANCE(instance_id);
855 CHECK_SERVER_REGISTRATION(instance_id);
857 ret = gatt_api->server->add_included_service(gatt_servers[instance_id - 1].server_id, serv_handle, incl_handle);
858 if (ret != BT_STATUS_SUCCESS) {
859 BT_ERR("GATT Server Add included Service failed: %s", status2string(ret));
860 return convert_to_oal_status(ret);
862 return OAL_STATUS_SUCCESS;
865 oal_status_t gatts_add_characteristics(int instance_id, int serv_handle,
866 oal_uuid_t* charc_uuid, int propts, int permsn)
868 int ret = OAL_STATUS_SUCCESS;
869 char str[2*BT_UUID_STRING_MAX];
870 char prop[MAX_PROP_LEN], perm[MAX_PERM_LEN];
871 char_prop_to_string(propts, prop);
872 char_perm_to_string(permsn, perm);
873 API_TRACE("Server Add Characteristic : instance_id = %d, serv_handle = %d, Properties =%s, Permisison = %s",
874 instance_id, serv_handle, prop, perm);
875 CHECK_OAL_GATT_ENABLED();
877 CHECK_SERVER_INSTANCE(instance_id);
879 CHECK_SERVER_REGISTRATION(instance_id);
881 if (charc_uuid != NULL) {
882 uuid_to_stringname(charc_uuid, str);
883 API_TRACE("uuid: [%s]", str);
886 ret = gatt_api->server->add_characteristic(gatt_servers[instance_id - 1].server_id,
887 serv_handle, (bt_uuid_t*)charc_uuid, propts, permsn);
888 if (ret != BT_STATUS_SUCCESS) {
889 BT_ERR("GATT Server Add Characteristic failed: %s", status2string(ret));
890 return convert_to_oal_status(ret);
892 return OAL_STATUS_SUCCESS;
895 oal_status_t gatts_add_descriptor(int instance_id, int serv_handle,
896 oal_uuid_t* desc_uuid, int permsn)
898 int ret = OAL_STATUS_SUCCESS;
899 char str[2*BT_UUID_STRING_MAX];
900 char perm[MAX_PERM_LEN];
901 char_perm_to_string(permsn, perm);
902 API_TRACE("Server Add Descriptor : instance_id = %d, serv_handle = %d, Permisison = %s",
903 instance_id, serv_handle, perm);
904 CHECK_OAL_GATT_ENABLED();
906 CHECK_SERVER_INSTANCE(instance_id);
908 CHECK_SERVER_REGISTRATION(instance_id);
910 if (desc_uuid != NULL) {
911 uuid_to_stringname(desc_uuid, str);
912 API_TRACE("uuid: [%s]", str);
915 ret = gatt_api->server->add_descriptor(gatt_servers[instance_id - 1].server_id,
916 serv_handle, (bt_uuid_t*)desc_uuid, permsn);
918 if (ret != BT_STATUS_SUCCESS) {
919 BT_ERR("GATT Server Add Descriptor failed: %s", status2string(ret));
920 return convert_to_oal_status(ret);
922 return OAL_STATUS_SUCCESS;
925 oal_status_t gatts_start_service(int instance_id, int svc_handle, int transport)
927 int ret = OAL_STATUS_SUCCESS;
929 API_TRACE("Server Start Service : Ins_id = %d, srvHndl = %d, transport = %d",
930 instance_id, svc_handle, transport);
931 CHECK_OAL_GATT_ENABLED();
933 CHECK_SERVER_INSTANCE(instance_id);
935 CHECK_SERVER_REGISTRATION(instance_id);
937 ret = gatt_api->server->start_service(gatt_servers[instance_id - 1].server_id, svc_handle, transport);
939 if (ret != BT_STATUS_SUCCESS) {
940 BT_ERR("GATT Server Start Service failed: %s", status2string(ret));
941 return convert_to_oal_status(ret);
943 return OAL_STATUS_SUCCESS;
946 oal_status_t gatts_stop_service(int ins_id, int srv_hdl)
948 int ret = OAL_STATUS_SUCCESS;
950 API_TRACE("Server Stop Service : Ins_id = %d, srvHndl = %d, ", ins_id, srv_hdl);
951 CHECK_OAL_GATT_ENABLED();
953 CHECK_SERVER_INSTANCE(ins_id);
954 CHECK_SERVER_REGISTRATION(ins_id);
956 ret = gatt_api->server->stop_service(gatt_servers[ins_id - 1].server_id, srv_hdl);
957 if (ret != BT_STATUS_SUCCESS) {
958 BT_ERR("GATT Server Stop Service failed: %s", status2string(ret));
959 return convert_to_oal_status(ret);
961 return OAL_STATUS_SUCCESS;
964 oal_status_t gatts_delete_service(int ins_id, int srv_hdl)
966 int ret = OAL_STATUS_SUCCESS;
968 API_TRACE("Server Delete Service : Ins_id = %d, srvHndl = %d, ", ins_id, srv_hdl);
969 CHECK_OAL_GATT_ENABLED();
971 CHECK_SERVER_INSTANCE(ins_id);
973 CHECK_SERVER_REGISTRATION(ins_id);
975 ret = gatt_api->server->delete_service(gatt_servers[ins_id - 1].server_id, srv_hdl);
976 if (ret != BT_STATUS_SUCCESS) {
977 BT_ERR("GATT Server Delete Service failed: %s", status2string(ret));
978 return convert_to_oal_status(ret);
980 return OAL_STATUS_SUCCESS;
983 oal_status_t gatts_send_response(int conn_id, int trans_id, int resp_status, oal_gatt_response_t *response)
985 int ret = OAL_STATUS_SUCCESS;
987 API_TRACE("Server Send Response : ConnId = %d, TransId = %d, ", conn_id, trans_id);
988 CHECK_OAL_GATT_ENABLED();
990 if (response == NULL) {
991 BT_ERR("GATT Server attribute value is empty");
992 return OAL_STATUS_INVALID_PARAM;
995 ret = gatt_api->server->send_response(conn_id, trans_id, resp_status, (btgatt_response_t *)response);
996 if (ret != BT_STATUS_SUCCESS) {
997 BT_ERR("GATT Server Send Response failed: %s", status2string(ret));
998 return convert_to_oal_status(ret);
1000 return OAL_STATUS_SUCCESS;
1003 oal_status_t gatts_send_indication(int ins_id, int attr_hndl, int conn_id, int len, int confirm, char* value)
1005 int ret = OAL_STATUS_SUCCESS;
1007 API_TRACE("Server Send Indication : Ins_id = %d, srvHndl = %d, [%s]",
1008 ins_id, attr_hndl, confirm == TRUE ? "INDICATION" : "NOTIFICATION");
1009 CHECK_OAL_GATT_ENABLED();
1011 CHECK_SERVER_INSTANCE(ins_id);
1013 CHECK_SERVER_REGISTRATION(ins_id);
1015 if (value == NULL || len == 0) {
1016 BT_ERR("GATT Server attribute value is empty");
1017 return OAL_STATUS_INVALID_PARAM;
1020 ret = gatt_api->server->send_indication(gatt_servers[ins_id - 1].server_id,
1021 attr_hndl, conn_id, len, confirm, value);
1023 if (ret != BT_STATUS_SUCCESS) {
1024 BT_ERR("GATT Server Send Indication failed: %s", status2string(ret));
1025 return convert_to_oal_status(ret);
1027 return OAL_STATUS_SUCCESS;
1030 oal_status_t gatts_update_att_value(int ins_id, oal_gatt_value_t *value)
1032 int ret = OAL_STATUS_SUCCESS;
1034 CHECK_OAL_GATT_ENABLED();
1036 CHECK_SERVER_INSTANCE(ins_id);
1038 CHECK_SERVER_REGISTRATION(ins_id);
1040 if (value == NULL || value->len == 0) {
1041 BT_ERR("GATT Server attribute value is empty");
1042 return OAL_STATUS_INVALID_PARAM;
1045 API_TRACE("Server Update Value: Ins_id = [%d], att handle = [%d] data len [%d]",
1046 ins_id, value->handle, value->len);
1048 ret = gatt_api->server->update_att_value(gatt_servers[ins_id - 1].server_id,
1049 (int)value->handle, (int)value->len, (char*)value->value);
1051 if (ret != BT_STATUS_SUCCESS) {
1052 BT_ERR("GATT Server Update Attribute Value failed: [%s]", status2string(ret));
1053 return convert_to_oal_status(ret);
1055 return OAL_STATUS_SUCCESS;
1058 /* GATT Server Callbacks:Start */
1059 static void cb_gatts_service_added(int status, int server_if,
1060 btgatt_srvc_id_t *psrvc_id, int srvc_handle)
1064 BT_INFO("BTGATT SERVER SERVICE ADDED CB, status:%d, handle:%d", status, srvc_handle);
1065 event_gatts_srvc_prm_t* event = g_new0(event_gatts_srvc_prm_t, 1);
1067 CHECK_CORRECT_SERVER_ID(instance_id, server_if, event->gatt_srvc_stat.server_inst);
1069 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1070 event->gatt_srvc_stat.server_inst = -1;
1072 event->gatt_srvc_stat.servic_hndl = srvc_handle;
1073 if (psrvc_id != NULL) {
1074 event->gatt_srvc_id.id.inst_id = psrvc_id->id.inst_id;
1075 //event->gatt_srvc_id.id.uuid = psrvc_id->id.uuid;
1076 memcpy(event->gatt_srvc_id.id.uuid.uuid, psrvc_id->id.uuid.uu, sizeof(psrvc_id->id.uuid));
1077 event->gatt_srvc_id.is_prmry = psrvc_id->is_primary;
1080 event->gatt_srvc_stat.status = convert_to_oal_status(status);
1082 send_event(OAL_EVENT_GATTS_SERVICE_ADDED, event, sizeof(event_gatts_srvc_prm_t));
1085 static void cb_gatts_included_service_added(int status, int server_if,
1087 int incl_srvc_handle)
1090 BT_INFO("BTGATT SERVER INCLUDED SERVICE ADDED CB, status:%d, srvc handle:%d, incl handle:%d",
1091 status, srvc_handle, incl_srvc_handle);
1093 event_gatts_incld_srvc_t* event = g_new0(event_gatts_incld_srvc_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->incl_srvc_hndl = incl_srvc_handle;
1102 event->gatt_srvc_stat.status = convert_to_oal_status(status);
1103 send_event(OAL_EVENT_GATTS_INCLUDED_SERVICE_ADDED, event, sizeof(event_gatts_incld_srvc_t));
1106 static void cb_gatts_characteristic_added(int status, int server_if, bt_uuid_t *char_id,
1107 int srvc_handle, int char_handle)
1111 BT_INFO("BTGATT SERVER CHARACTERISTIC ADDED CB, status:%d, srvc handle:%d, char handle:%d",
1112 status, srvc_handle, char_handle);
1114 event_gatts_srvc_charctr_t* event = g_new0(event_gatts_srvc_charctr_t, 1);
1116 CHECK_CORRECT_SERVER_ID(Ins_id, server_if, event->gatt_srvc_stat.server_inst);
1118 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1119 event->gatt_srvc_stat.server_inst = -1;
1121 event->gatt_srvc_stat.servic_hndl = srvc_handle;
1122 event->charctr_hndl = char_handle;
1123 event->gatt_srvc_stat.status = convert_to_oal_status(status);
1124 memcpy(event->charctr_uuid.uuid, char_id->uu, sizeof(bt_uuid_t));
1125 send_event(OAL_EVENT_GATTS_CHARACTERISTIC_ADDED, event, sizeof(event_gatts_srvc_charctr_t));
1128 static void cb_gatts_descriptor_added(int status, int server_if,
1129 bt_uuid_t *descr_id, int srvc_handle,
1133 BT_INFO("BTGATT SERVER DESCRIPTOR ADDED CB, status:%d, srvc handle:%d, desc handle:%d",
1134 status, srvc_handle, descr_handle);
1136 event_gatts_srvc_descr_t* event = g_new0(event_gatts_srvc_descr_t, 1);
1138 CHECK_CORRECT_SERVER_ID(Ins_id, server_if, event->gatt_srvc_stat.server_inst);
1140 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1141 event->gatt_srvc_stat.server_inst = -1;
1143 event->gatt_srvc_stat.servic_hndl = srvc_handle;
1144 event->descrptr_hndl = descr_handle;
1145 event->gatt_srvc_stat.status = convert_to_oal_status(status);
1146 memcpy(event->descrptr_uuid.uuid, descr_id->uu, sizeof(bt_uuid_t));
1147 send_event(OAL_EVENT_GATTS_DESCRIPTOR_ADDED, event, sizeof(event_gatts_srvc_descr_t));
1150 static void cb_gatts_service_started(int status, int server_if, int srvc_handle)
1153 BT_INFO("BTGATT SERVER SERVICE STARTED CB, status:%d, srvc_handle:%d", status, srvc_handle);
1155 event_gatts_srvc_t* event = g_new0(event_gatts_srvc_t, 1);
1157 CHECK_CORRECT_SERVER_ID(Ins_id, server_if, event->server_inst);
1159 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1160 event->server_inst = -1;
1162 event->servic_hndl = srvc_handle;
1163 event->status = convert_to_oal_status(status);
1164 send_event(OAL_EVENT_GATTS_SERVICE_STARTED, event, sizeof(event_gatts_srvc_t));
1167 static void cb_gatts_service_stopped(int status, int server_if, int srvc_handle)
1170 BT_INFO("BTGATT SERVER SERVICE STOPPED CB, status:%d, srvc_handle:%d", status, srvc_handle);
1172 event_gatts_srvc_t* event = g_new0(event_gatts_srvc_t, 1);
1174 CHECK_CORRECT_SERVER_ID(ins_id, server_if, event->server_inst);
1176 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1177 event->server_inst = -1;
1179 event->servic_hndl = srvc_handle;
1180 event->status = convert_to_oal_status(status);
1181 send_event(OAL_EVENT_GATTS_SERVICE_STOPED, event, sizeof(event_gatts_srvc_t));
1184 static void cb_gatts_service_deleted(int status, int server_if, int srvc_handle)
1187 BT_INFO("BTGATT SERVER SERVICE DELETED CB, status:%d, srvc_handle:%d", status, srvc_handle);
1189 event_gatts_srvc_t* event = g_new0(event_gatts_srvc_t, 1);
1191 CHECK_CORRECT_SERVER_ID(ins_id, server_if, event->server_inst);
1193 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1194 event->server_inst = -1;
1196 event->servic_hndl = srvc_handle;
1197 event->status = convert_to_oal_status(status);
1198 send_event(OAL_EVENT_GATTS_SERVICE_DELETED, event, sizeof(event_gatts_srvc_t));
1201 static void cb_gatts_connection(int conn_id, int server_if, int connected, bt_bdaddr_t *bda)
1204 oal_event_t event_type;
1206 BT_INFO("BTGATT SERVER CONNECTION connnected:%d, conn_id:%d server_if:%d", connected, conn_id, server_if);
1207 event_gatts_conn_t *event = g_new0(event_gatts_conn_t, 1);
1209 for (ins_id = 1 ; ins_id <= NUM_SERVER_INST ; ins_id++) {
1210 if ((gatt_servers[ins_id - 1].server_id == server_if) &&
1211 (gatt_servers[ins_id - 1].state == GATT_INS_ENABLED)) {
1212 event->server_inst = ins_id;
1215 BT_INFO("Server ID:%d recevied server Id:%d\n", gatt_servers[ins_id - 1].server_id, server_if);
1218 if (ins_id == NUM_SERVER_INST+1) {
1219 BT_WARN("Incorrect Server Interface received: %d\n", server_if);
1223 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1224 event->conn_id = conn_id;
1225 event_type = ((connected == TRUE) ? OAL_EVENT_GATTS_CONNECTION_COMPLETED : OAL_EVENT_GATTS_DISCONNECTION_COMPLETED);
1227 send_event(event_type, event, sizeof(event_gatts_conn_t));
1230 static void cb_gatts_request_read(int conn_id, int trans_id, bt_bdaddr_t *bda,
1231 int attr_handle, int offset, bool is_long)
1233 BT_INFO("BTGATT SERVER REQUEST READ CB: conn_id:%d", conn_id);
1235 event_gatts_srvc_read_attr_t* event = g_new0(event_gatts_srvc_read_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->is_long = is_long;
1245 send_event(OAL_EVENT_GATTS_REQUEST_READ, event, sizeof(event_gatts_srvc_read_attr_t));
1248 static void cb_gatts_request_write(int conn_id, int trans_id,
1249 bt_bdaddr_t *bda, int attr_handle,
1250 int offset, int length,
1251 bool need_rsp, bool is_prep, uint8_t* value)
1253 BT_INFO("BTGATT SERVER REQUEST WRITE conn_id:%d", conn_id);
1255 event_gatts_srvc_write_attr_t* event = g_new0(event_gatts_srvc_write_attr_t, 1);
1257 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1259 event->attr_trans.attr_handle = attr_handle;
1260 event->attr_trans.conn_id = conn_id;
1261 event->attr_trans.trans_id = trans_id;
1262 event->attr_trans.offset = offset;
1263 event->need_rsp = need_rsp;
1264 event->is_prep = is_prep;
1266 if (length > 0 && value != NULL) {
1267 if (length > OAL_GATT_MAX_ATTR_LEN)
1268 length = OAL_GATT_MAX_ATTR_LEN;
1269 memcpy(event->value, value, length);
1270 event->length = length;
1272 send_event(OAL_EVENT_GATTS_REQUEST_WRITE, event, sizeof(event_gatts_srvc_write_attr_t));
1275 static void cb_gatts_acquire_write(int mtu, int conn_id, int trans_id, int attr_handle , bt_bdaddr_t *bda)
1278 BT_INFO("BTGATT SERVER REQUEST ACQUIRE WRITE conn_id:%d", conn_id);
1280 event_gatts_srvc_acquire_attr_t* event = g_new0(event_gatts_srvc_acquire_attr_t, 1);
1282 event->attr_trans.attr_handle = attr_handle;
1283 event->attr_trans.conn_id = conn_id;
1284 event->attr_trans.trans_id = trans_id;
1286 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1288 send_event(OAL_EVENT_GATTS_REQUEST_ACQUIRE_WRITE, event, sizeof(event_gatts_srvc_acquire_attr_t));
1291 static void cb_gatts_acquire_notify(int mtu, int conn_id, int trans_id, int attr_handle)
1293 BT_INFO("BTGATT SERVER REQUEST ACQUIRE NOTIFY conn_id:%d", conn_id);
1295 event_gatts_srvc_acquire_attr_t* event = g_new0(event_gatts_srvc_acquire_attr_t, 1);
1297 event->attr_trans.attr_handle = attr_handle;
1298 event->attr_trans.conn_id = conn_id;
1299 event->attr_trans.trans_id = trans_id;
1302 send_event(OAL_EVENT_GATTS_REQUEST_ACQUIRE_NOTIFY, event, sizeof(event_gatts_srvc_acquire_attr_t));
1306 static void cb_gatts_response_confirmation(int status, int handle)
1308 BT_INFO("BTGATT SERVER RESPONSE CONFIRMATION CB, status:%d, handle:%d", status, handle);
1310 event_gatts_respons_t* event = g_new0(event_gatts_respons_t, 1);
1312 event->hndl = handle;
1313 event->status = convert_to_oal_status(status);
1315 send_event(OAL_EVENT_GATTS_RESPONSE_CONFIRMED, event, sizeof(event_gatts_respons_t));
1318 static void cb_indication_confirmation(int conn_id, int trans_id, int attr_handle, bt_bdaddr_t *bda)
1320 BT_INFO("BTGATT SERVER Indication Confirmation CB, conn_id:%d, trans_id:%d attr_handle:%d",
1321 conn_id, trans_id, attr_handle);
1323 event_gatts_ind_cnfrm_t* event = g_new0(event_gatts_ind_cnfrm_t, 1);
1325 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1327 event->attr_handle = attr_handle;
1328 event->conn_id = conn_id;
1329 event->trans_id = trans_id;
1330 send_event(OAL_EVENT_GATTS_IND_CONFIRM, event, sizeof(event_gatts_ind_cnfrm_t));
1333 static void cb_gatts_mtu_changed(int conn_id, int mtu)
1335 BT_INFO("BTGATT SERVER MTU CHANGED CB, conn_id: %d, mtu_size: %d", conn_id, mtu);
1336 event_gatts_mtu_changed_t *event = g_new0(event_gatts_mtu_changed_t, 1);
1337 event->conn_id = conn_id;
1338 event->mtu_size = mtu;
1339 send_event(OAL_EVENT_GATTS_MTU_CHANGED, event, sizeof(event_gatts_mtu_changed_t));
1343 static void cb_notifcation_changed(int conn_id, int trans_id, int attr_handle, bool notify, bt_bdaddr_t *bda)
1345 BT_INFO("BTGATT SERVER Notication CB, conn_id:%d, trans_id:%d attr_handle:%d Notify:%d",
1346 conn_id, trans_id, attr_handle, notify);
1348 event_gatts_notif_t* event = g_new0(event_gatts_notif_t, 1);
1350 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1352 event->attr_handle = attr_handle;
1353 event->conn_id = conn_id;
1354 event->trans_id = trans_id;
1355 event->notify = notify;
1356 send_event(OAL_EVENT_GATTS_NOTIFICATION, event, sizeof(event_gatts_notif_t));
1361 /* GATT Server Callbacks: End */
1362 /************************************ GATT Client ***********************************/
1363 /* Client Callbacks */
1364 static void cb_gattc_register_app(int status, int clientIf, bt_uuid_t *app_uuid)
1366 char uuid_str[BT_UUID_STRING_MAX];
1367 event_gattc_register_t *event;
1371 /* Check if GATT client registered for Default GATT client UUID */
1372 uuid_to_string((service_uuid_t *)app_uuid, uuid_str);
1373 BT_INFO("UUID:%s", uuid_str);
1375 BT_INFO("BTGATT CLIENT REGISTER APP CB, status:%d, clientIf:%d", status, clientIf);
1376 event = g_new0(event_gattc_register_t, 1);
1377 event->client_if = clientIf;
1378 event->status = (status == 0 ? OAL_STATUS_SUCCESS : OAL_STATUS_INTERNAL_ERROR);
1379 memcpy(event->client_uuid.uuid, app_uuid->uu, sizeof(oal_uuid_t));
1380 send_event(OAL_EVENT_GATTC_REGISTRATION, event, sizeof(event_gattc_register_t));
1383 static void cb_gattc_scan_result(bt_bdaddr_t* bdaddress, int rssi, uint8_t *adv_data)
1385 event_ble_scan_result_info *event;
1387 event = g_new0(event_ble_scan_result_info, 1);
1389 memcpy(event->address.addr, bdaddress->address, BT_ADDRESS_BYTES_NUM);
1390 memcpy(event->adv_data, adv_data, BLE_ADV_DATA_LENGTH);
1391 send_event_bda_trace(OAL_EVENT_BLE_REMOTE_DEVICE_FOUND, event,
1392 sizeof(event_ble_scan_result_info), (bt_address_t *)bdaddress);
1395 static void cb_gattc_connection(int conn_id, int status, int client_if, bt_bdaddr_t* bda)
1397 event_gattc_conn_t *event;
1398 BT_INFO("BTGATT CLIENT CONNECTED CB, status:[%d] client_if[%d] id[%d]",
1399 status, client_if, conn_id);
1401 event = g_new0(event_gattc_conn_t, 1);
1402 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1403 event->client_if = client_if;
1405 if (BT_STATUS_SUCCESS != status) {
1406 event->status = OAL_STATUS_INTERNAL_ERROR;
1407 BT_ERR("gattc connection Error: %d", status);
1409 event->conn_id = conn_id;
1410 event->status = OAL_STATUS_SUCCESS;
1413 send_event_bda_trace(OAL_EVENT_GATTC_CONNECTION_COMPLETED, event,
1414 sizeof(*event), (bt_address_t *)bda);
1418 static void cb_gattc_disconnect(int conn_id, int status, int client_if, bt_bdaddr_t* bda)
1420 event_gattc_conn_t *event;
1421 BT_INFO("BTGATT Client Disconnect Complete cb, status:[%d], client_if[%d] id[%d]",
1422 status, client_if, conn_id);
1423 event = g_new0(event_gattc_conn_t, 1);
1424 event->status = convert_to_oal_status(status);
1425 event->client_if = client_if;
1426 event->conn_id = conn_id;
1427 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1428 send_event_bda_trace(OAL_EVENT_GATTC_DISCONNECTION_COMPLETED, event,
1429 sizeof(*event), (bt_address_t *)bda);
1432 static void cb_gattc_search_result(int conn_id, btgatt_srvc_id_t *srvc_id)
1434 char uuid_str[2*BT_UUID_STRING_MAX];
1436 BT_INFO("BTGATT Client Service Search Result cb, conn_id:%d", conn_id);
1438 uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str);
1439 BT_INFO("Service=> UUID: [%s], Inst_id: [%u], Type: [%s]",
1440 uuid_str, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1442 event_gattc_service_result_t *event = g_new0(event_gattc_service_result_t, 1);
1443 event->conn_status.status = OAL_STATUS_SUCCESS;
1444 event->conn_status.conn_id = conn_id;
1445 memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1447 send_event(OAL_EVENT_GATTC_SERVICE_SEARCH_RESULT, event, sizeof(*event));
1450 static void cb_gattc_search_complete(int conn_id, int status)
1452 BT_INFO("BTGATT Client Service Search Complete cb, status:%d, conn_id:%d",
1455 event_gattc_conn_status_t *event = g_new0(event_gattc_conn_status_t, 1);
1456 event->conn_id = conn_id;
1457 event->status = convert_to_oal_status(status);
1459 send_event(OAL_EVENT_GATTC_SERVICE_SEARCH_DONE, event, sizeof(*event));
1463 static void cb_gattc_get_characteristics(int conn_id, int status, btgatt_srvc_id_t *srvc_id,
1464 btgatt_gatt_id_t *char_id, int char_prop)
1466 char uuid_str1[2*BT_UUID_STRING_MAX];
1467 char uuid_str2[2*BT_UUID_STRING_MAX];
1469 BT_INFO("BTGATT Client Get Characteristic Callback, conn_id:%d, status:%d", conn_id, status);
1470 uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str1);
1471 BT_INFO("Service=> UUID: [%s], Inst_id: [%u], Type: [%s]",
1472 uuid_str1, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1474 event_gattc_characteristic_result_t *event = g_new0(event_gattc_characteristic_result_t, 1);
1475 event->conn_status.conn_id = conn_id;
1476 event->conn_status.status = convert_to_oal_status(status);
1477 memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1480 uuid_to_stringname((oal_uuid_t *)&(char_id->uuid), uuid_str2);
1481 BT_INFO("Charac=> UUID: [%s], Inst_id: [%u], Prop: [%s]",
1482 uuid_str2, char_id->inst_id, char_prop_to_string(char_prop, str));
1483 event->char_prop = char_prop;
1484 memcpy(&(event->char_id), char_id, sizeof(oal_gatt_id_t));
1486 send_event(OAL_EVENT_GATTC_CHARAC_SERACH_RESULT, event, sizeof(*event));
1491 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)
1493 char uuid_str1[2*BT_UUID_STRING_MAX];
1494 char uuid_str2[2*BT_UUID_STRING_MAX];
1495 char uuid_str3[2*BT_UUID_STRING_MAX];
1496 BT_INFO("BTGATT Client Get Descriptor Callback, conn_id:%d, status:%d", conn_id, status);
1497 uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str1);
1498 uuid_to_stringname((oal_uuid_t *)&(char_id->uuid), uuid_str2);
1499 BT_INFO("Service=> UUID: [%s], Inst_id: [%u], Type: [%s]",
1500 uuid_str1, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1501 BT_INFO("Charac=> UUID: [%s], Inst_id: [%u]", uuid_str2, char_id->inst_id);
1503 event_gattc_descriptor_result_t *event = g_new0(event_gattc_descriptor_result_t, 1);
1504 event->conn_status.conn_id = conn_id;
1505 event->conn_status.status = convert_to_oal_status(status);
1506 memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1507 memcpy(&(event->char_id), char_id, sizeof(oal_gatt_id_t));
1510 uuid_to_stringname((oal_uuid_t *)&(descr_id->uuid), uuid_str3);
1511 BT_INFO("Desc=> UUID: [%s], Inst_id: [%u]", uuid_str3, descr_id->inst_id);
1512 memcpy(&(event->descr_id), descr_id, sizeof(oal_gatt_id_t));
1514 send_event(OAL_EVENT_GATTC_DESC_SERACH_RESULT, event, sizeof(*event));
1517 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)
1519 char uuid_str1[2*BT_UUID_STRING_MAX];
1520 char uuid_str2[2*BT_UUID_STRING_MAX];
1521 BT_INFO("BTGATT Client Register For Notification Callback, client_if:%d, status:%d, registered: %s",
1522 client_if, status, registered == 1 ? "REGISTERED" : "DEREGISTERED");
1523 uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str1);
1524 uuid_to_stringname((oal_uuid_t *)&(char_id->uuid), uuid_str2);
1525 BT_INFO("Service=> UUID: [%s], Inst_id: [%u], Type: [%s]", uuid_str1, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1526 BT_INFO("Charac=> UUID: [%s], Inst_id: [%u]", uuid_str2, char_id->inst_id);
1528 event_gattc_regdereg_notify_t *event = g_new0(event_gattc_regdereg_notify_t, 1);
1529 oal_event_t event_type = (registered == 1 ? OAL_EVENT_GATTC_NOTIFICATION_REGISTERED : OAL_EVENT_GATTC_NOTIFICATION_DEREGISTERED);
1530 event->client_if = client_if;
1531 event->status = convert_to_oal_status(status);
1532 memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1533 memcpy(&(event->char_id), char_id, sizeof(oal_gatt_id_t));
1535 send_event(event_type, event, sizeof(*event));
1539 static void cb_gattc_read_characteristic(int conn_id, int status, btgatt_read_params_t *p_data)
1541 char uuid_str1[2*BT_UUID_STRING_MAX];
1542 char uuid_str2[2*BT_UUID_STRING_MAX];
1543 BT_INFO("BTGATT Client Read Charcateristic Callback, conn_id:%d, read_status:%d", conn_id, status);
1545 event_gattc_read_data *event = g_new0(event_gattc_read_data, 1);
1546 event->uuid_status.conn_status.conn_id = conn_id;
1547 event->uuid_status.conn_status.status = convert_to_oal_status(status);
1548 event->value_type = p_data->value_type;
1549 memcpy(&(event->uuid_status.srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1550 memcpy(&(event->uuid_status.char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1552 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1553 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1554 BT_INFO("Service=> UUID: [%s], Inst_id: [%u], Type: [%s]", uuid_str1,
1555 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1556 BT_INFO("Charac=> UUID: [%s], Inst_id: [%u]", uuid_str2, p_data->char_id.inst_id);
1557 BT_INFO("Len: %u, value_type: %u", p_data->value.len, p_data->value_type);
1558 if (p_data->value.len > 0 && event->value_type == GATTC_READ_VALUE_TYPE_VALUE) {
1560 data = g_malloc(3*p_data->value.len+1);
1562 BT_ERR("memory allocation failed");
1566 convert_hex_2_str((unsigned char *)p_data->value.value, p_data->value.len, data);
1567 BT_INFO("Read Data: [%s]", data);
1568 event->data_len = p_data->value.len;
1569 memcpy(&(event->data), &(p_data->value.value), event->data_len);
1573 send_event(OAL_EVENT_GATTC_READ_CHARAC, event, sizeof(*event));
1576 static void cb_gattc_write_characteristic(int conn_id, int status, btgatt_write_params_t *p_data)
1578 char uuid_str1[2*BT_UUID_STRING_MAX];
1579 char uuid_str2[2*BT_UUID_STRING_MAX];
1580 BT_INFO("BTGATT Client Write Charcateristic Callback, conn_id:%d, write_status:%d", conn_id, status);
1582 event_gattc_write_data *event = g_new0(event_gattc_write_data, 1);
1583 event->conn_status.conn_id = conn_id;
1584 event->conn_status.status = convert_to_oal_status(status);
1585 memcpy(&(event->srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1586 memcpy(&(event->char_id), &(p_data->char_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 BT_INFO("Service=> UUID: [%s], Inst_id: [%u], Type: [%s]", uuid_str1,
1591 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1592 BT_INFO("Charac=> UUID: [%s], Inst_id: [%u]", uuid_str2, p_data->char_id.inst_id);
1594 send_event(OAL_EVENT_GATTC_WRITE_CHARAC, event, sizeof(*event));
1597 static void cb_gattc_write_descriptor(int conn_id, int status, btgatt_write_params_t *p_data)
1599 char uuid_str1[2*BT_UUID_STRING_MAX];
1600 char uuid_str2[2*BT_UUID_STRING_MAX];
1601 char uuid_str3[2*BT_UUID_STRING_MAX];
1602 BT_INFO("BTGATT Client Write Descriptor Callback, conn_id:%d, write_status:%d", conn_id, status);
1604 event_gattc_write_data *event = g_new0(event_gattc_write_data, 1);
1605 event->conn_status.conn_id = conn_id;
1606 event->conn_status.status = convert_to_oal_status(status);
1607 memcpy(&(event->srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1608 memcpy(&(event->char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1609 memcpy(&(event->descr_id), &(p_data->descr_id), sizeof(oal_gatt_id_t));
1611 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1612 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1613 uuid_to_stringname((oal_uuid_t *)&(p_data->descr_id.uuid), uuid_str3);
1614 BT_INFO("Service=> UUID: [%s], Inst_id: [%u], Type: [%s]", uuid_str1,
1615 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1616 BT_INFO("Charac=> UUID: [%s], Inst_id: [%u]", uuid_str2, p_data->char_id.inst_id);
1617 BT_INFO("Desc=> UUID: [%s], Inst_id: [%u]", uuid_str3, p_data->descr_id.inst_id);
1619 send_event(OAL_EVENT_GATTC_WRITE_DESCR, event, sizeof(*event));
1624 static void cb_gattc_read_descriptor(int conn_id, int status, btgatt_read_params_t *p_data)
1626 char uuid_str1[2*BT_UUID_STRING_MAX];
1627 char uuid_str2[2*BT_UUID_STRING_MAX];
1628 char uuid_str3[2*BT_UUID_STRING_MAX];
1629 BT_INFO("BTGATT Client Read Descriptor Callback, conn_id:%d, read_status:%d", conn_id, status);
1631 event_gattc_read_data *event = g_new0(event_gattc_read_data, 1);
1632 event->uuid_status.conn_status.conn_id = conn_id;
1633 event->uuid_status.conn_status.status = convert_to_oal_status(status);
1634 event->value_type = p_data->value_type;
1635 memcpy(&(event->uuid_status.srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1636 memcpy(&(event->uuid_status.char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1637 memcpy(&(event->uuid_status.descr_id), &(p_data->descr_id), sizeof(oal_gatt_id_t));
1640 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1641 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1642 uuid_to_stringname((oal_uuid_t *)&(p_data->descr_id.uuid), uuid_str3);
1643 BT_INFO("Service=> UUID: [%s], Inst_id: [%u], Type: [%s]", uuid_str1,
1644 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1645 BT_INFO("Charac=> UUID: [%s], Inst_id: [%u]", uuid_str2, p_data->char_id.inst_id);
1646 BT_INFO("Desc=> UUID: [%s], Inst_id: [%u]", uuid_str3, p_data->descr_id.inst_id);
1647 BT_INFO("Len: %u, value_type: %u", p_data->value.len, p_data->value_type);
1648 if (p_data->value.len > 0 && event->value_type == GATTC_READ_VALUE_TYPE_VALUE) {
1650 data = g_malloc(3*p_data->value.len+1);
1652 BT_ERR("memory allocation failed");
1656 convert_hex_2_str((unsigned char *)p_data->value.value, p_data->value.len, data);
1657 BT_INFO("Read Data: [%s]", data);
1658 event->data_len = p_data->value.len;
1659 memcpy(&(event->data), &(p_data->value.value), event->data_len);
1663 send_event(OAL_EVENT_GATTC_READ_DESCR, event, sizeof(*event));
1666 oal_status_t gattc_start_le_discovery(int client_id)
1669 int ret = OAL_STATUS_SUCCESS;
1671 API_TRACE("BTGATT CLIENT SCAN START");
1672 CHECK_OAL_GATT_ENABLED();
1673 CHECK_CLIENT_REGISTRATION(client_id);
1675 ret = gatt_api->client->scan(client_id, 1);
1676 if (ret != BT_STATUS_SUCCESS) {
1677 BT_ERR("Error:Start LE Discovery failed: %s", status2string(ret));
1678 return convert_to_oal_status(ret);
1680 send_event(OAL_EVENT_BLE_DISCOVERY_STARTED, NULL, 0);
1681 return OAL_STATUS_SUCCESS;
1684 oal_status_t gattc_stop_le_discovery(int client_id)
1687 int ret = OAL_STATUS_SUCCESS;
1689 API_TRACE("Scan is stopped");
1690 CHECK_OAL_GATT_ENABLED();
1691 CHECK_CLIENT_REGISTRATION(client_id);
1692 ret = gatt_api->client->scan(client_id, 0);
1693 if (ret != BT_STATUS_SUCCESS) {
1694 BT_ERR("Error:Stop LE Discovery failed: %s", status2string(ret));
1695 return convert_to_oal_status(ret);
1697 send_event(OAL_EVENT_BLE_DISCOVERY_STOPPED, NULL, 0);
1698 return OAL_STATUS_SUCCESS;
1701 oal_status_t gattc_set_le_scan_param(int scan_type, int itv, int win)
1705 API_TRACE("Scan is stopped");
1706 CHECK_OAL_GATT_ENABLED();
1709 ret = gatt_api->client->set_scan_parameters(scan_type, itv, win);
1710 if (ret != BT_STATUS_SUCCESS) {
1711 BT_ERR("Error:Set scan parameters failed: %s", status2string(ret));
1712 return convert_to_oal_status(ret);
1715 ret = gatt_api->client->set_scan_parameters(itv, win);
1716 if (ret != BT_STATUS_SUCCESS) {
1717 BT_ERR("Error:Set scan parameters failed: %s", status2string(ret));
1718 return convert_to_oal_status(ret);
1722 return OAL_STATUS_SUCCESS;
1724 /************************************ GATT Client ***********************************/
1725 oal_status_t gattc_register(oal_uuid_t* client_uuid)
1728 char str[2*BT_UUID_STRING_MAX];
1729 int ret = OAL_STATUS_SUCCESS;
1731 CHECK_OAL_GATT_ENABLED();
1732 uuid_to_stringname(client_uuid, str);
1733 API_TRACE("uuid: [%s]", str);
1734 /* We will perform actual registration in cb_gattc_register_app callback */
1735 ret = gatt_api->client->register_client((bt_uuid_t *)client_uuid);
1736 if (ret != BT_STATUS_SUCCESS) {
1737 BT_ERR("GATT client register failed: %s", status2string(ret));
1738 return convert_to_oal_status(ret);
1740 return OAL_STATUS_SUCCESS;
1743 oal_status_t gattc_deregister(int client_id)
1745 int ret = OAL_STATUS_SUCCESS;
1747 API_TRACE("GATT client deregister");
1748 CHECK_OAL_GATT_ENABLED();
1749 CHECK_CLIENT_REGISTRATION(client_id);
1751 ret = gatt_api->client->unregister_client(client_id);
1752 if (ret != BT_STATUS_SUCCESS) {
1753 BT_ERR("GATT client deregister failed: %s", status2string(ret));
1754 return convert_to_oal_status(ret);
1757 return OAL_STATUS_SUCCESS;
1760 oal_status_t gattc_connect(int client_id, bt_address_t *device_address, int isDirect)
1762 int ret = OAL_STATUS_SUCCESS;
1765 OAL_CHECK_PARAMETER(device_address, return);
1766 API_TRACE("Client Connect: [%s]", bdt_bd2str(device_address, &bdstr));
1767 CHECK_OAL_GATT_ENABLED();
1768 CHECK_CLIENT_REGISTRATION(client_id);
1770 /* Handle the actual connection in cb_gattc_connection callback */
1771 ret = gatt_api->client->connect(client_id, (bt_bdaddr_t *)device_address, isDirect);
1772 if (ret != BT_STATUS_SUCCESS) {
1773 BT_ERR("GATT client connect failed: %s", status2string(ret));
1774 return convert_to_oal_status(ret);
1776 return OAL_STATUS_SUCCESS;
1779 oal_status_t gattc_disconnect(int client_id, bt_address_t *device_address, int conn_id)
1782 int ret = OAL_STATUS_SUCCESS;
1785 OAL_CHECK_PARAMETER(device_address, return);
1786 API_TRACE("Client Disconnect: [%s]", bdt_bd2str(device_address, &bdstr));
1787 CHECK_OAL_GATT_ENABLED();
1788 CHECK_CLIENT_REGISTRATION(client_id);
1789 CHECK_CLIENT_CONNECTION(conn_id);
1791 /* Handle actual disconnection in callback */
1792 ret = gatt_api->client->disconnect(client_id, (bt_bdaddr_t *) device_address, conn_id);
1793 if (ret != BT_STATUS_SUCCESS) {
1794 BT_ERR("GATT client disconnect failed: %s", status2string(ret));
1795 return convert_to_oal_status(ret);
1797 return OAL_STATUS_SUCCESS;
1801 oal_status_t gattc_search_service(int conn_id, oal_uuid_t *service_uuid)
1803 int ret = OAL_STATUS_SUCCESS;
1804 char uuid_str[2*BT_UUID_STRING_MAX];
1807 uuid_to_stringname(service_uuid, uuid_str);
1808 API_TRACE("Client Service Search UUID: [%s]", uuid_str);
1810 API_TRACE("Client Service Search All");
1812 CHECK_OAL_GATT_ENABLED();
1813 CHECK_CLIENT_CONNECTION(conn_id);
1814 ret = gatt_api->client->search_service(conn_id, (bt_uuid_t *)service_uuid);
1816 if (ret != BT_STATUS_SUCCESS) {
1817 BT_ERR("GATT client service search failed: %s", status2string(ret));
1818 return convert_to_oal_status(ret);
1820 return OAL_STATUS_SUCCESS;
1823 oal_status_t gattc_get_characteristic(int conn_id, oal_gatt_srvc_id_t *srvc_id, oal_gatt_id_t *char_id)
1825 int ret = OAL_STATUS_SUCCESS;
1826 char uuid_str[2*BT_UUID_STRING_MAX];
1828 OAL_CHECK_PARAMETER(srvc_id, return);
1829 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str);
1830 API_TRACE("Client Get Characteristic, Service_uuid: [%s]", uuid_str);
1831 CHECK_OAL_GATT_ENABLED();
1832 CHECK_CLIENT_CONNECTION(conn_id);
1833 ret = gatt_api->client->get_characteristic(conn_id, (btgatt_srvc_id_t *)srvc_id,
1834 (btgatt_gatt_id_t *)char_id);
1835 if (ret != BT_STATUS_SUCCESS) {
1836 BT_ERR("GATT client get characteristic failed: %s", status2string(ret));
1837 return convert_to_oal_status(ret);
1839 return OAL_STATUS_SUCCESS;
1842 oal_status_t gattc_get_descriptor(int conn_id, oal_gatt_srvc_id_t *srvc_id,
1843 oal_gatt_id_t *char_id, oal_gatt_id_t *desc_id)
1845 int ret = OAL_STATUS_SUCCESS;
1846 char uuid_str1[2*BT_UUID_STRING_MAX];
1847 char uuid_str2[2*BT_UUID_STRING_MAX];
1849 OAL_CHECK_PARAMETER(srvc_id, return);
1850 OAL_CHECK_PARAMETER(char_id, return);
1851 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1852 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1853 API_TRACE("Client Get Descriptor, Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
1854 CHECK_OAL_GATT_ENABLED();
1855 CHECK_CLIENT_CONNECTION(conn_id);
1856 ret = gatt_api->client->get_descriptor(conn_id, (btgatt_srvc_id_t *)srvc_id,
1857 (btgatt_gatt_id_t *)char_id, (btgatt_gatt_id_t *)desc_id);
1858 if (ret != BT_STATUS_SUCCESS) {
1859 BT_ERR("GATT client get descriptor failed: %s", status2string(ret));
1860 return convert_to_oal_status(ret);
1862 return OAL_STATUS_SUCCESS;
1865 oal_status_t gattc_register_for_notification(int client_id, bt_address_t * address,
1866 oal_gatt_srvc_id_t *srvc_id, oal_gatt_id_t *char_id) {
1867 int ret = OAL_STATUS_SUCCESS;
1868 char uuid_str1[2*BT_UUID_STRING_MAX];
1869 char uuid_str2[2*BT_UUID_STRING_MAX];
1872 OAL_CHECK_PARAMETER(address, return);
1873 OAL_CHECK_PARAMETER(srvc_id, return);
1874 OAL_CHECK_PARAMETER(char_id, return);
1875 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1876 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1877 API_TRACE("Client Register Notification: [%s], Service_uuid: [%s], Char_uuid: [%s]", bdt_bd2str(address, &bdstr), uuid_str1, uuid_str2);
1878 CHECK_OAL_GATT_ENABLED();
1879 CHECK_CLIENT_REGISTRATION(client_id);
1881 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);
1882 if (ret != BT_STATUS_SUCCESS) {
1883 BT_ERR("GATT client register notification failed: %s", status2string(ret));
1884 return convert_to_oal_status(ret);
1887 return OAL_STATUS_SUCCESS;
1890 oal_status_t gattc_deregister_for_notification(int client_id, bt_address_t * address,
1891 oal_gatt_srvc_id_t *srvc_id, oal_gatt_id_t *char_id) {
1892 int ret = OAL_STATUS_SUCCESS;
1893 char uuid_str1[2*BT_UUID_STRING_MAX];
1894 char uuid_str2[2*BT_UUID_STRING_MAX];
1897 OAL_CHECK_PARAMETER(address, return);
1898 OAL_CHECK_PARAMETER(srvc_id, return);
1899 OAL_CHECK_PARAMETER(char_id, return);
1900 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1901 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1902 API_TRACE("Client Deregister Notification: [%s], Service_uuid: [%s], Char_uuid: [%s]", bdt_bd2str(address, &bdstr), uuid_str1, uuid_str2);
1903 CHECK_OAL_GATT_ENABLED();
1904 CHECK_CLIENT_REGISTRATION(client_id);
1906 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);
1907 if (ret != BT_STATUS_SUCCESS) {
1908 BT_ERR("GATT client deregister notification failed: %s", status2string(ret));
1909 return convert_to_oal_status(ret);
1912 return OAL_STATUS_SUCCESS;
1916 oal_status_t gattc_read_characteristic(int conn_id, oal_gatt_srvc_id_t *srvc_id,
1917 oal_gatt_id_t *char_id, oal_gatt_auth_req_t auth_req)
1919 int ret = OAL_STATUS_SUCCESS;
1920 char uuid_str1[2*BT_UUID_STRING_MAX];
1921 char uuid_str2[2*BT_UUID_STRING_MAX];
1923 OAL_CHECK_PARAMETER(srvc_id, return);
1924 OAL_CHECK_PARAMETER(char_id, return);
1925 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1926 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1927 API_TRACE("Client Read Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
1928 CHECK_OAL_GATT_ENABLED();
1929 CHECK_CLIENT_CONNECTION(conn_id);
1931 ret = gatt_api->client->read_characteristic(conn_id, (btgatt_srvc_id_t *)srvc_id,
1932 (btgatt_gatt_id_t *)char_id, auth_req);
1933 if (ret != BT_STATUS_SUCCESS) {
1934 BT_ERR("GATT client read characteristic failed: %s", status2string(ret));
1935 return convert_to_oal_status(ret);
1938 return OAL_STATUS_SUCCESS;
1941 oal_status_t gattc_read_descriptor(int conn_id, oal_gatt_srvc_id_t *srvc_id,
1942 oal_gatt_id_t *char_id, oal_gatt_id_t *desc_id, oal_gatt_auth_req_t auth_req)
1944 int ret = OAL_STATUS_SUCCESS;
1945 char uuid_str1[2*BT_UUID_STRING_MAX];
1946 char uuid_str2[2*BT_UUID_STRING_MAX];
1947 char uuid_str3[2*BT_UUID_STRING_MAX];
1949 OAL_CHECK_PARAMETER(srvc_id, return);
1950 OAL_CHECK_PARAMETER(char_id, return);
1951 OAL_CHECK_PARAMETER(desc_id, return);
1952 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1953 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1954 uuid_to_stringname(&(desc_id->uuid), uuid_str3);
1955 API_TRACE("Client Read Descriptor: Service_uuid: [%s], Char_uuid: [%s], Desc_uuid: [%s]",
1956 uuid_str1, uuid_str2, uuid_str3);
1957 CHECK_OAL_GATT_ENABLED();
1958 CHECK_CLIENT_CONNECTION(conn_id);
1960 ret = gatt_api->client->read_descriptor(conn_id, (btgatt_srvc_id_t *)srvc_id,
1961 (btgatt_gatt_id_t *)char_id, (btgatt_gatt_id_t *)desc_id, auth_req);
1962 if (ret != BT_STATUS_SUCCESS) {
1963 BT_ERR("GATT client read descriptor failed: %s", status2string(ret));
1964 return convert_to_oal_status(ret);
1967 return OAL_STATUS_SUCCESS;
1970 oal_status_t gattc_write_characteristic(int conn_id, oal_gatt_srvc_id_t *srvc_id,
1971 oal_gatt_id_t *char_id, oal_gatt_write_type_t write_type,
1972 int len, oal_gatt_auth_req_t auth_req, char* data)
1974 int ret = OAL_STATUS_SUCCESS;
1975 char uuid_str1[2*BT_UUID_STRING_MAX];
1976 char uuid_str2[2*BT_UUID_STRING_MAX];
1977 OAL_CHECK_PARAMETER(srvc_id, return);
1978 OAL_CHECK_PARAMETER(char_id, return);
1979 OAL_CHECK_PARAMETER(data, return);
1980 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1981 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1982 API_TRACE("Client Write Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
1983 CHECK_OAL_GATT_ENABLED();
1984 CHECK_CLIENT_CONNECTION(conn_id);
1986 ret = gatt_api->client->write_characteristic(conn_id, (btgatt_srvc_id_t *)srvc_id,
1987 (btgatt_gatt_id_t *)char_id, write_type, len, auth_req, data);
1988 if (ret != BT_STATUS_SUCCESS) {
1989 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
1990 return convert_to_oal_status(ret);
1993 return OAL_STATUS_SUCCESS;
1996 oal_status_t gattc_write_descriptor(int conn_id, oal_gatt_srvc_id_t *srvc_id,
1997 oal_gatt_id_t *char_id, oal_gatt_id_t *desc_id,
1998 oal_gatt_write_type_t write_type, int len, oal_gatt_auth_req_t auth_req, char* data)
2000 int ret = OAL_STATUS_SUCCESS;
2001 char uuid_str1[2*BT_UUID_STRING_MAX];
2002 char uuid_str2[2*BT_UUID_STRING_MAX];
2003 char uuid_str3[2*BT_UUID_STRING_MAX];
2004 OAL_CHECK_PARAMETER(srvc_id, return);
2005 OAL_CHECK_PARAMETER(char_id, return);
2006 OAL_CHECK_PARAMETER(desc_id, return);
2007 OAL_CHECK_PARAMETER(data, return);
2008 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2009 uuid_to_stringname(&(char_id->uuid), uuid_str2);
2010 uuid_to_stringname(&(desc_id->uuid), uuid_str3);
2011 API_TRACE("Client Write Descriptor: Service_uuid: [%s], Char_uuid: [%s], Desc_uuid: [%s]",
2012 uuid_str1, uuid_str2, uuid_str3);
2013 CHECK_OAL_GATT_ENABLED();
2014 CHECK_CLIENT_CONNECTION(conn_id);
2016 ret = gatt_api->client->write_descriptor(conn_id, (btgatt_srvc_id_t *)srvc_id,
2017 (btgatt_gatt_id_t *)char_id, (btgatt_gatt_id_t *)desc_id, write_type, len, auth_req, data);
2018 if (ret != BT_STATUS_SUCCESS) {
2019 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
2020 return convert_to_oal_status(ret);
2023 return OAL_STATUS_SUCCESS;
2026 oal_status_t gattc_conn_param_update(bt_address_t * address, int min, int max, int latency, int timeout)
2031 OAL_CHECK_PARAMETER(address, return);
2032 API_TRACE("Conn Param Update: [%s]", bdt_bd2str(address, &bdstr));
2033 CHECK_OAL_GATT_ENABLED();
2035 BT_INFO("[%s] min[%d] max[%d] latency[%d] timeout[%d]", bdt_bd2str(address, &bdstr), min, max, latency, timeout);
2036 ret = gatt_api->client->conn_parameter_update((bt_bdaddr_t *)address, min, max, latency, timeout);
2037 if (ret != BT_STATUS_SUCCESS) {
2038 BT_ERR("error: %s", status2string(ret));
2039 return convert_to_oal_status(ret);
2041 return OAL_STATUS_SUCCESS;