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*);
157 static void cb_indication_confirmation(int conn_id, int trans_id, int attr_handle, bt_bdaddr_t *bda);
158 static void cb_gatts_connection(int conn_id, int server_if, int connected, bt_bdaddr_t *bda);
160 static void cb_gatts_mtu_changed(int conn_id, int mtu);
163 static void cb_notifcation_changed(int conn_id, int trans_id, int attr_handle, bool notify, bt_bdaddr_t *bda);
166 /************************************HAL Interface *************************************/
168 /*TODO GATT Server callbacks will be implemented in subsequent patches */
169 static const btgatt_server_callbacks_t btgatt_server_callbacks = {
170 cb_gatts_register_app,
172 cb_gatts_service_added,
173 cb_gatts_included_service_added,
174 cb_gatts_characteristic_added,
175 cb_gatts_descriptor_added,
176 cb_gatts_service_started,
177 cb_gatts_service_stopped,
178 cb_gatts_service_deleted,
179 cb_indication_confirmation,
180 cb_gatts_request_read,
181 cb_gatts_request_write,
182 NULL, /*cb_gatts_request_exec_write,*/
183 cb_gatts_response_confirmation,
185 cb_gatts_multi_adv_enable,
186 cb_gatts_multi_adv_update,
187 cb_gatts_multi_adv_set_inst_data,
188 cb_gatts_multi_adv_disable,
189 cb_gatts_mtu_changed,
191 cb_notifcation_changed,
193 cb_gatts_acquire_write
196 /* Forward declaration for GATT client callbacks */
197 static void cb_gattc_register_app(int status, int clientIf, bt_uuid_t *app_uuid);
198 static void cb_gattc_scan_result(bt_bdaddr_t* bdaddress, int rssi, uint8_t *adv_data);
199 static void cb_gattc_connection(int conn_id, int status, int client_if, bt_bdaddr_t* bda);
200 static void cb_gattc_disconnect(int conn_id, int status, int client_if, bt_bdaddr_t* bda);
201 static void cb_gattc_search_result(int conn_id, btgatt_srvc_id_t *srvc_id);
202 static void cb_gattc_search_complete(int conn_id, int status);
203 static void cb_gattc_get_characteristics(int conn_id, int status, btgatt_srvc_id_t *srvc_id,
204 btgatt_gatt_id_t *char_id, int char_prop);
205 static void cb_gattc_get_descriptor(int conn_id, int status, btgatt_srvc_id_t *srvc_id,
206 btgatt_gatt_id_t *char_id, btgatt_gatt_id_t *descr_id);
207 static void cb_gattc_read_characteristic(int conn_id, int status, btgatt_read_params_t *p_data);
208 static void cb_gattc_read_descriptor(int conn_id, int status, btgatt_read_params_t *p_data);
209 static void cb_gattc_write_characteristic(int conn_id, int status, btgatt_write_params_t *p_data);
210 static void cb_gattc_write_descriptor(int conn_id, int status, btgatt_write_params_t *p_data);
211 static void cb_gattc_register_for_notification(int conn_id, int registered, int status,
212 btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id);
214 /*TODO GATT CLient callbacks will be implemented in subsequent patches */
215 static const btgatt_client_callbacks_t btgatt_client_callbacks = {
216 cb_gattc_register_app,
217 cb_gattc_scan_result,
218 cb_gattc_connection, /*cb_gattc_connection*/
219 cb_gattc_disconnect, /*cb_gattc_disconnect*/
220 cb_gattc_search_complete, /*cb_gattc_search_complete*/
221 cb_gattc_search_result, /*cb_gattc_search_result*/
222 cb_gattc_get_characteristics, /*cb_gattc_get_characteristics*/
223 cb_gattc_get_descriptor, /*cb_gattc_get_descriptor*/
224 NULL, /*cb_gattc_get_included_service*/
225 cb_gattc_register_for_notification, /*cb_gattc_register_for_notification*/
226 NULL, /*cb_gattc_notify*/
227 cb_gattc_read_characteristic, /*cb_gattc_read_characteristic*/
228 cb_gattc_write_characteristic, /*cb_gattc_write_characteristic*/
229 cb_gattc_read_descriptor, /*cb_gattc_read_descriptor*/
230 cb_gattc_write_descriptor, /*cb_gattc_write_descriptor*/
231 NULL, /*cb_gattc_execute_write*/
232 NULL, /*cb_gattc_remote_rssi*/
233 NULL, /*cb_gattc_configure_mtu_cmpl*/
234 #ifdef PLATFORM_ANDROID_HAL
235 NULL, /*cb_scan_filter_cfg*/
236 NULL, /*cb_scan_filter_param*/
237 NULL, /*cb_scan_filter_status*/
238 NULL, /*cb_congestion*/
239 NULL, /*cb_batchscan_cfg_storag*/
240 NULL, /*cb_batchscan_enb_disable*/
241 NULL, /*cb_batchscan_reports*/
242 NULL, /*cb_batchscan_threshold*/
243 NULL, /*cb_track_adv_event*/
247 static btgatt_callbacks_t btgatt_callbacks = {
248 sizeof(btgatt_callbacks_t),
249 &btgatt_client_callbacks,
250 &btgatt_server_callbacks
253 /*******************************GATT Initialisation - Deinitialisation********************************/
254 oal_status_t gatt_enable(void)
256 const bt_interface_t * blued_api;
260 for (i = 0; i < NUM_SERVER_INST; i++) {
261 gatt_servers[i].server_id = -1;
262 gatt_servers[i].state = GATT_INS_DISABLED;
263 memset(&gatt_servers[i].uuid, 0, sizeof(oal_uuid_t));
264 cur_adv_state[i] = FALSE;
267 /* Get stack interface */
268 blued_api = (const bt_interface_t *) adapter_get_stack_interface();
270 if (blued_api == NULL) {
271 BT_ERR("Stack is not initialized");
272 return OAL_STATUS_NOT_READY;
276 BT_WARN("GATT Interface is already initialized...");
277 return OAL_STATUS_ALREADY_DONE;
280 gatt_api = (const btgatt_interface_t *)blued_api->get_profile_interface(BT_PROFILE_GATT_ID);
281 if (gatt_api == NULL) {
282 BT_ERR("GATT interface failed");
283 return OAL_STATUS_INTERNAL_ERROR;
286 if ((ret = gatt_api->init(&btgatt_callbacks)) != BT_STATUS_SUCCESS) {
287 BT_ERR("Error: Unable to initialise GATT :%s", status2string(ret));
290 return convert_to_oal_status(ret);
293 BT_INFO("GATT successfully initialized");
294 return OAL_STATUS_SUCCESS;
297 oal_status_t gatt_disable(void)
305 for (i = 0; i < NUM_SERVER_INST; i++) {
306 gatt_servers[i].server_id = -1;
307 gatt_servers[i].state = GATT_INS_DISABLED;
308 memset(&gatt_servers[i].uuid, 0, sizeof(oal_uuid_t));
310 return OAL_STATUS_SUCCESS;
313 /************************************GATT Server Functions*************************************/
315 oal_status_t gatts_register(oal_uuid_t* server_uuid)
317 char str[2*BT_UUID_STRING_MAX];
318 int ret = OAL_STATUS_SUCCESS;
321 CHECK_OAL_GATT_ENABLED();
322 uuid_to_stringname((service_uuid_t*)server_uuid->uuid, str);
323 API_TRACE("Register the server instance: UUID: [%s]", str);
325 for (i = 0; i < NUM_SERVER_INST; i++) {
326 if (memcmp(server_uuid->uuid, gatt_servers[i].uuid.uuid, sizeof(oal_uuid_t)) == 0) {
327 if (gatt_servers[i].server_id != -1) {
328 BT_ERR("This is resevered UUID for easy set up application i = %d \
329 gatt_servers[i].server_id = %d", i, gatt_servers[i].server_id);
330 return OAL_STATUS_ALREADY_DONE;
335 ret = gatt_api->server->register_server((bt_uuid_t*)server_uuid);
337 if (ret != BT_STATUS_SUCCESS) {
338 BT_ERR("GATT server registration with UUID:%s failed: %s", str, status2string(ret));
339 return convert_to_oal_status(ret);
341 return OAL_STATUS_SUCCESS;
344 oal_status_t gatts_unregister(int instance_id)
346 int ret = OAL_STATUS_SUCCESS;
347 API_TRACE("Unregister the registered server, instance_id: %d", instance_id);
349 CHECK_OAL_GATT_ENABLED();
350 CHECK_SERVER_INSTANCE(instance_id);
351 CHECK_SERVER_REGISTRATION(instance_id);
353 ret = gatt_api->server->unregister_server(gatt_servers[instance_id - 1].server_id);
355 if (ret != BT_STATUS_SUCCESS) {
356 BT_ERR("GATT server unregistration failed: %d", instance_id);
357 return convert_to_oal_status(ret);
360 gatt_servers[instance_id-1].server_id = -1;
361 gatt_servers[instance_id-1].state = GATT_INS_DISABLED;
363 if (instance_id != 1 && instance_id != 2)
364 memset(&gatt_servers[instance_id-1].uuid, 0, sizeof(oal_uuid_t));
366 return OAL_STATUS_SUCCESS;
369 /* Legacy Advertisement */
370 oal_status_t gatts_start_listen(int instance_id, gboolean enable)
372 int ret = OAL_STATUS_SUCCESS;
373 API_TRACE("server-id:%d enable: %d", gatt_servers[instance_id-1].server_id, enable);
375 CHECK_OAL_GATT_ENABLED();
376 CHECK_SERVER_INSTANCE(instance_id);
377 CHECK_SERVER_REGISTRATION(instance_id);
379 ret = gatt_api->server->listen(gatt_servers[instance_id-1].server_id, enable);
381 if (ret != BT_STATUS_SUCCESS) {
382 BT_ERR("Error:Advertising start/stop on server-id:%d failed With Status: [%s]",
383 gatt_servers[instance_id-1].server_id, status2string(ret));
384 return convert_to_oal_status(ret);
387 return OAL_STATUS_SUCCESS;
390 oal_status_t gatts_stop_advertising(int instance_id)
392 int ret = OAL_STATUS_SUCCESS;
393 API_TRACE("Stop advertising");
395 CHECK_OAL_GATT_ENABLED();
396 CHECK_SERVER_INSTANCE(instance_id);
397 CHECK_SERVER_REGISTRATION(instance_id);
399 ret = gatt_api->server->listen(gatt_servers[instance_id - 1].server_id, FALSE);
401 if (ret != BT_STATUS_SUCCESS) {
402 BT_ERR("Stop Advertising failed for server Instance:%d With Status: %s",
403 instance_id, status2string(ret));
404 return convert_to_oal_status(ret);
406 return OAL_STATUS_SUCCESS;
410 /* Below API's need to be removed as they are hardcoded API's.
411 If at all, product specific API's are required to set specific data, then new naming should
412 be adopted for these API's */
413 oal_status_t gatts_set_advertising_params(int instance_id, float min_intv, float max_intv)
415 int ret = OAL_STATUS_SUCCESS;
417 API_TRACE("Set advertising params min_int:%f, max_int:%f", min_intv, max_intv);
418 CHECK_OAL_GATT_ENABLED();
419 CHECK_SERVER_INSTANCE(instance_id);
420 CHECK_SERVER_REGISTRATION(instance_id);
422 /* Historically used for Legacy Advertising */
423 return OAL_STATUS_NOT_SUPPORT;
426 oal_status_t gatts_set_advertising_data(int instance_id, uint16_t manufac_len, char* manufac_data)
428 int ret = OAL_STATUS_SUCCESS;
430 API_TRACE("Set advertising data");
431 CHECK_OAL_GATT_ENABLED();
432 CHECK_SERVER_INSTANCE(instance_id);
433 CHECK_SERVER_REGISTRATION(instance_id);
435 /* Historically used for Legacy Advertising */
436 return OAL_STATUS_NOT_SUPPORT;
439 oal_status_t gatts_set_scan_response(int instance_id, uint16_t manufac_len, char* manufac_data)
441 int ret = OAL_STATUS_SUCCESS;
443 API_TRACE("Set Scan Response data");
444 CHECK_OAL_GATT_ENABLED();
445 CHECK_SERVER_INSTANCE(instance_id);
446 CHECK_SERVER_REGISTRATION(instance_id);
448 /* Historically used for Legacy Advertising */
449 return OAL_STATUS_NOT_SUPPORT;
453 oal_status_t gatts_multi_adv_enable(int instance_id)
455 int ret = OAL_STATUS_SUCCESS;
456 API_TRACE("Start Multi advertising, srv_if: %d, instance_id: %d",
457 gatt_servers[instance_id - 1].server_id, instance_id);
459 CHECK_OAL_GATT_ENABLED();
460 CHECK_SERVER_INSTANCE(instance_id);
461 CHECK_SERVER_REGISTRATION(instance_id);
463 if (gatt_api->server->multi_adv_enable == NULL)
464 return OAL_STATUS_NOT_SUPPORT;
466 /* Platform Requirement: Advertising to be enabled even when no data is set: i.e Empty Advertisement
467 if (gatt_servers[instance_id - 1].state != GATT_INS_DATA_SET){
468 BT_ERR("Not allowed, state: %d, instance_id: %d",
469 gatt_servers[instance_id - 1].state, instance_id);
470 return OAL_STATUS_BUSY;
473 if (gatt_servers[instance_id - 1].state == GATT_INS_ENABLED) {
474 BT_ERR("Not allowed, state: %d, instance_id: %d",
475 gatt_servers[instance_id - 1].state, instance_id);
476 return OAL_STATUS_ALREADY_DONE;
479 if (gatt_servers[instance_id - 1].state == GATT_INS_ENABLING) {
480 BT_ERR("Not allowed, state: %d, instance_id: %d",
481 gatt_servers[instance_id - 1].state, instance_id);
482 return OAL_STATUS_BUSY;
485 gatt_servers[instance_id - 1].state = GATT_INS_ENABLING;
487 ret = gatt_api->server->multi_adv_enable(gatt_servers[instance_id - 1].server_id);
489 if (ret != BT_STATUS_SUCCESS) {
490 BT_ERR("GATT enable multi advertising failed for server Instance: %d With Status: %s",
491 instance_id, status2string(ret));
492 // gatt_servers[instance_id - 1].state = GATT_INS_DATA_SET;
493 return convert_to_oal_status(ret);
495 return OAL_STATUS_SUCCESS;
498 oal_status_t gatts_multi_adv_disable(int instance_id)
500 int ret = OAL_STATUS_SUCCESS;
501 API_TRACE("Stop Multi advertising, srv_if: %d, instance_id: %d",
502 gatt_servers[instance_id - 1].server_id, instance_id);
504 CHECK_OAL_GATT_ENABLED();
505 CHECK_SERVER_INSTANCE(instance_id);
506 CHECK_SERVER_REGISTRATION(instance_id);
508 if (gatt_api->server->multi_adv_disable == NULL)
509 return OAL_STATUS_NOT_SUPPORT;
511 if ((gatt_servers[instance_id - 1].state != GATT_INS_ENABLED)) {
512 BT_ERR("Not Allowed, state: %d, instance_id: %d",
513 gatt_servers[instance_id - 1].state, instance_id);
514 return OAL_STATUS_BUSY;
517 ret = gatt_api->server->multi_adv_disable(gatt_servers[instance_id - 1].server_id);
519 if (ret != BT_STATUS_SUCCESS) {
520 BT_ERR("GATT disable multi advertising failed for server Instance: %d With Status: %s",
521 instance_id, status2string(ret));
522 return convert_to_oal_status(ret);
524 return OAL_STATUS_SUCCESS;
527 oal_status_t gatts_multi_adv_update(int instance_id,
528 int min_intv, int max_intv,
529 int adv_type, int chnl_map,
530 int tx_power, int timeout_s)
532 int ret = OAL_STATUS_SUCCESS;
533 API_TRACE("Multi advertising Update");
535 CHECK_OAL_GATT_ENABLED();
536 CHECK_SERVER_INSTANCE(instance_id);
537 CHECK_SERVER_REGISTRATION(instance_id);
539 if (gatt_api->server->multi_adv_update == NULL)
540 return OAL_STATUS_NOT_SUPPORT;
542 ret = gatt_api->server->multi_adv_update(gatt_servers[instance_id - 1].server_id,
545 tx_power, timeout_s);
547 if (ret != BT_STATUS_SUCCESS) {
548 BT_ERR("GATT update multi advertising failed for server Instance: %d With Status: %s",
549 instance_id, status2string(ret));
550 return convert_to_oal_status(ret);
552 return OAL_STATUS_SUCCESS;
555 oal_status_t gatts_multi_adv_set_inst_data(int instance_id,
556 oal_ble_multi_adv_param_setup_t *adv_param_setup)
558 int ret = OAL_STATUS_SUCCESS;
559 btgatt_adv_param_setup_t adv_setup;
560 API_TRACE("Set Multi advertising data, srv_if: %d, instance_id: %d",
561 gatt_servers[instance_id - 1].server_id, instance_id);
563 CHECK_OAL_GATT_ENABLED();
564 CHECK_SERVER_INSTANCE(instance_id);
565 CHECK_SERVER_REGISTRATION(instance_id);
567 if (gatt_api->server->multi_adv_set_inst_data == NULL)
568 return OAL_STATUS_NOT_SUPPORT;
570 if ((gatt_servers[instance_id - 1].state >= GATT_INS_DATA_SETTING) && (gatt_servers[instance_id - 1].state != GATT_INS_DATA_SET)) {
571 BT_ERR("Not Allowed, state: %d, instance_id: %d", gatt_servers[instance_id - 1].state, instance_id);
572 return OAL_STATUS_BUSY;
575 gatt_servers[instance_id - 1].state = GATT_INS_DATA_SETTING;
578 adv_setup.set_scan_rsp = adv_param_setup->set_scan_rsp;
579 adv_setup.include_name = adv_param_setup->include_name;
580 adv_setup.include_txpower = adv_param_setup->include_txpower;
581 adv_setup.appearance = adv_param_setup->appearance;
582 adv_setup.manufacturer_data = adv_param_setup->manufacturer_data;
583 adv_setup.manufacturer_data_len = adv_param_setup->manufacturer_data_len;
584 adv_setup.service_data = adv_param_setup->service_data;
585 adv_setup.service_data_len = adv_param_setup->service_data_len;
586 adv_setup.service_uuid = adv_param_setup->service_uuid;
587 adv_setup.service_uuid_len = adv_param_setup->service_uuid_len;
588 /* Solicit UUID handler: Start */
589 adv_setup.solicit_uuid = adv_param_setup->solicit_uuid;
590 adv_setup.solicit_uuid_len = adv_param_setup->solicit_uuid_len;
591 /* Solicit UUID handler: End */
592 adv_setup.min_interval = adv_param_setup->min_interval;
593 adv_setup.max_interval = adv_param_setup->min_interval;
594 adv_setup.adv_type = adv_param_setup->adv_type;
595 adv_setup.chnl_map = adv_param_setup->chnl_map;
596 adv_setup.tx_power = adv_param_setup->tx_power;
597 adv_setup.timeout_s = adv_param_setup->timeout_s;
599 adv_setup.server_if = gatt_servers[instance_id - 1].server_id;
600 if (adv_setup.manufacturer_data_len > 0 && adv_setup.manufacturer_data != NULL) {
601 BT_INFO("Manufacture data.....\n");
602 PRINT_ADV_DATA(adv_setup.manufacturer_data_len, (unsigned char *)adv_setup.manufacturer_data, adv_setup.set_scan_rsp);
605 if (adv_setup.service_uuid_len > 0 && adv_setup.service_uuid != NULL) {
606 BT_INFO("Service uuid.....\n");
607 PRINT_ADV_DATA(adv_setup.service_uuid_len, (unsigned char*)adv_setup.service_uuid, FALSE);
611 ret = gatt_api->server->multi_adv_set_inst_data(adv_setup);
613 if (ret != BT_STATUS_SUCCESS) {
614 BT_ERR("GATT Set Multi advertising data failed: %s", status2string(ret));
615 gatt_servers[instance_id - 1].state = GATT_INS_DISABLED;
616 return convert_to_oal_status(ret);
618 return OAL_STATUS_SUCCESS;
621 /************************************GATT Server Functions*************************************/
623 static void cb_gatts_register_app(int status, int server_if, bt_uuid_t *uuid)
626 char str[2*BT_UUID_STRING_MAX];
627 uuid_to_stringname((service_uuid_t*)uuid, str);
629 event_gatts_register_t *event = g_malloc0(sizeof(event_gatts_register_t));
631 BT_INFO("BTGATT SERVER REGISTER APP CB, status:%d, server_if:%d", status, server_if);
632 BT_INFO("UUID: [%s]", str);
634 for (i = 0; i < NUM_SERVER_INST; i++) {
635 if (gatt_servers[i].server_id == -1) {
636 BT_INFO("Server Instance registered with stack [%d]", i+1);
637 gatt_servers[i].server_id = server_if;
638 event->server_inst = i+1;
639 memcpy(gatt_servers[i].uuid.uuid, uuid->uu, sizeof(bt_uuid_t));
640 memcpy(event->server_uuid.uuid, uuid->uu, sizeof(bt_uuid_t));
644 send_event(OAL_EVENT_BLE_SERVER_INSTANCE_INITIALISED, event, sizeof(event_gatts_register_t));
647 static void cb_gatts_listen(int status, int server_if)
649 gboolean prev_state[NUM_SERVER_INST];
651 event_ble_adv_status *event = g_malloc0(sizeof(event_ble_adv_status));
654 for (instance_id = 1 ; instance_id <= NUM_SERVER_INST ; instance_id++) {
655 if (gatt_servers[instance_id - 1].server_id == server_if) {
656 event->server_inst = instance_id;
659 BT_WARN("Server ID:%d recevied server Id:%d\n", gatt_servers[instance_id - 1].server_id, server_if);
662 if ((event->server_inst > 0) && (event->server_inst <= 10)) {
663 prev_state[event->server_inst - 1] = cur_adv_state[event->server_inst - 1];
664 new_state = (status == BT_STATUS_SUCCESS) ? !(prev_state[event->server_inst - 1]) : (prev_state[event->server_inst - 1]);
665 cur_adv_state[event->server_inst - 1] = new_state;
667 BT_ERR("Invalid Callback...");
672 BT_INFO("Adv State of server instance %d :%d", instance_id, new_state);
674 if (TRUE == new_state)
675 send_event(OAL_EVENT_BLE_ADVERTISING_STARTED, event, sizeof(event_ble_adv_status));
677 send_event(OAL_EVENT_BLE_ADVERTISING_STOPPED, event, sizeof(event_ble_adv_status));
680 static void cb_gatts_multi_adv_enable(int server_if, int status)
683 BT_INFO("BTGATT SERVER MULTI ADV ENABLE CB, status:%d, srv_if: %d", status, server_if);
684 event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
687 if (status != BT_STATUS_SUCCESS)
688 BT_ERR("MULTI ADV ENABLE failed, status:%d, srv_if: %d", status, server_if);
690 for (instance_id = 1 ; instance_id <= NUM_SERVER_INST ; instance_id++) {
691 if (gatt_servers[instance_id - 1].server_id == server_if) {
692 event->server_inst = instance_id;
693 gatt_servers[instance_id - 1].state = GATT_INS_ENABLED;
697 BT_ERR("Invalid Interface, srv_if: %d", server_if);
698 event->server_inst = -1;
700 event->status = convert_to_oal_status(status);
701 send_event(OAL_EVENT_BLE_MULTI_ADVERTISING_ENABLE, event, sizeof(event_ble_multiadv_status));
704 static void cb_gatts_multi_adv_disable(int server_if, int status)
706 BT_INFO("BTGATT SERVER MULTI ADV DISABLE CB, status:%d, srv_if: %d", status, server_if);
707 event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
710 if (status != BT_STATUS_SUCCESS)
711 BT_ERR("MULTI ADV DISABLE failed, status:%d, srv_if: %d", status, server_if);
713 for (instance_id = 1 ; instance_id <= NUM_SERVER_INST; instance_id++) {
714 if (gatt_servers[instance_id - 1].server_id == server_if) {
715 event->server_inst = instance_id;
716 gatt_servers[instance_id - 1].state = GATT_INS_DISABLED;
720 BT_ERR("Invalid Interface, srv_if: %d", server_if);
721 event->server_inst = -1;
723 event->status = convert_to_oal_status(status);
724 send_event(OAL_EVENT_BLE_MULTI_ADVERTISING_DISABLE, event, sizeof(event_ble_multiadv_status));
727 static void cb_gatts_multi_adv_update(int server_if, int status)
729 BT_INFO("BTGATT SERVER MULTI ADV UPDATE CB, status:%d, srv_if: %d", status, server_if);
730 event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
733 if (status != BT_STATUS_SUCCESS)
734 BT_ERR("MULTI ADV UPDATE failed, status:%d, srv_if: %d", status, server_if);
736 for (instance_id = 1 ; instance_id <= NUM_SERVER_INST ; instance_id++) {
737 if (gatt_servers[instance_id - 1].server_id == server_if) {
738 event->server_inst = instance_id;
742 BT_ERR("Invalid Interface, srv_if: %d", server_if);
743 event->server_inst = -1;
745 event->status = convert_to_oal_status(status);
746 send_event(OAL_EVENT_BLE_MULTI_ADVERTISING_UPDATE, event, sizeof(event_ble_multiadv_status));
749 static void cb_gatts_multi_adv_set_inst_data(int server_if, int status)
751 BT_INFO("BTGATT SERVER MULTI ADV SET INST DATA CB, status:%d, srv_if: %d", status, server_if);
752 event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
755 if (status != BT_STATUS_SUCCESS)
756 BT_ERR("MULTI ADV SET DATA failed, status:%d, srv_if: %d", status, server_if);
758 for (instance_id = 1 ; instance_id <= NUM_SERVER_INST ; instance_id++) {
759 if (gatt_servers[instance_id - 1].server_id == server_if) {
760 event->server_inst = instance_id;
761 gatt_servers[instance_id - 1].state = GATT_INS_DATA_SET;
765 BT_ERR("Invalid Interface, srv_if: %d", server_if);
766 event->server_inst = -1;
768 event->status = convert_to_oal_status(status);
769 send_event(OAL_EVENT_BLE_MULTI_ADVERTISING_SET_INST_DATA, event, sizeof(event_ble_multiadv_status));
772 oal_status_t gatts_get_att_mtu(int conn_id, int *mtu)
774 int ret = OAL_STATUS_SUCCESS;
775 API_TRACE("Get ATT MTU, conn_id: %d", conn_id);
776 CHECK_OAL_GATT_ENABLED();
777 OAL_CHECK_PARAMETER(mtu, return);
779 /* To prevent crash in case other libraries not support this api */
780 if (gatt_api->server->get_att_mtu == NULL) {
781 BT_WARN("get_att_mtu is NULL");
782 return OAL_STATUS_NOT_SUPPORT;
785 ret = gatt_api->server->get_att_mtu(conn_id, mtu);
786 if (ret != BT_STATUS_SUCCESS) {
787 BT_ERR("GATT MTU Size failed, status: %d", status2string(ret));
788 return convert_to_oal_status(ret);
791 BT_INFO("Current ATT MTU Size: %d", *mtu);
792 return OAL_STATUS_SUCCESS;
794 oal_status_t gatt_send_response_acquire(int conn_id, int trans_id,
795 int status, int fd, int mtu , void * fdlist)
797 int ret = OAL_STATUS_SUCCESS;
799 API_TRACE("Server Send Response : ConnId = %d, TransId = %d, %d", conn_id, trans_id, fd);
800 CHECK_OAL_GATT_ENABLED();
802 ret = gatt_api->server->send_response_acquire(conn_id, trans_id, status, fd, mtu, fdlist);
803 if (ret != BT_STATUS_SUCCESS) {
804 BT_ERR("GATT Server Send Response failed: %s", status2string(ret));
805 return convert_to_oal_status(ret);
807 return OAL_STATUS_SUCCESS;
810 oal_status_t gatts_add_service(int instance_id, oal_gatt_srvc_id_t *gatt_serv_id,
813 int ret = OAL_STATUS_SUCCESS;
814 btgatt_srvc_id_t btgatt_srvc_id;
815 char str[2*BT_UUID_STRING_MAX];
817 API_TRACE("Server Add Service : instance_id = %d, num_handles = %d", instance_id, num_handles);
818 CHECK_OAL_GATT_ENABLED();
820 CHECK_SERVER_INSTANCE(instance_id);
821 CHECK_SERVER_REGISTRATION(instance_id);
823 if (gatt_serv_id != NULL) {
824 uuid_to_stringname(&(gatt_serv_id->id.uuid), str);
825 API_TRACE("Service uuid: [%s]", str);
826 memcpy(btgatt_srvc_id.id.uuid.uu, gatt_serv_id->id.uuid.uuid, sizeof(gatt_serv_id->id.uuid));
827 btgatt_srvc_id.id.inst_id = gatt_serv_id->id.inst_id;
828 btgatt_srvc_id.is_primary = gatt_serv_id->is_prmry;
830 BT_INFO("GATT Server Service Id is NULL");
831 return OAL_STATUS_INVALID_PARAM;
834 ret = gatt_api->server->add_service(gatt_servers[instance_id - 1].server_id, &btgatt_srvc_id, num_handles);
836 if (ret != BT_STATUS_SUCCESS) {
837 BT_ERR("GATT Server add service failed: %s", status2string(ret));
838 return convert_to_oal_status(ret);
840 return OAL_STATUS_SUCCESS;
843 oal_status_t gatts_add_included_services(int instance_id, int serv_handle,
846 int ret = OAL_STATUS_SUCCESS;
847 API_TRACE("Server Add included Service : instance_id = %d, serv_handle = %d, incl_handle = %d",
848 instance_id, serv_handle, incl_handle);
849 CHECK_OAL_GATT_ENABLED();
851 CHECK_SERVER_INSTANCE(instance_id);
853 CHECK_SERVER_REGISTRATION(instance_id);
855 ret = gatt_api->server->add_included_service(gatt_servers[instance_id - 1].server_id, serv_handle, incl_handle);
856 if (ret != BT_STATUS_SUCCESS) {
857 BT_ERR("GATT Server Add included Service failed: %s", status2string(ret));
858 return convert_to_oal_status(ret);
860 return OAL_STATUS_SUCCESS;
863 oal_status_t gatts_add_characteristics(int instance_id, int serv_handle,
864 oal_uuid_t* charc_uuid, int propts, int permsn)
866 int ret = OAL_STATUS_SUCCESS;
867 char str[2*BT_UUID_STRING_MAX];
868 char prop[MAX_PROP_LEN], perm[MAX_PERM_LEN];
869 char_prop_to_string(propts, prop);
870 char_perm_to_string(permsn, perm);
871 API_TRACE("Server Add Characteristic : instance_id = %d, serv_handle = %d, Properties =%s, Permisison = %s",
872 instance_id, serv_handle, prop, perm);
873 CHECK_OAL_GATT_ENABLED();
875 CHECK_SERVER_INSTANCE(instance_id);
877 CHECK_SERVER_REGISTRATION(instance_id);
879 if (charc_uuid != NULL) {
880 uuid_to_stringname(charc_uuid, str);
881 API_TRACE("uuid: [%s]", str);
884 ret = gatt_api->server->add_characteristic(gatt_servers[instance_id - 1].server_id,
885 serv_handle, (bt_uuid_t*)charc_uuid, propts, permsn);
886 if (ret != BT_STATUS_SUCCESS) {
887 BT_ERR("GATT Server Add Characteristic failed: %s", status2string(ret));
888 return convert_to_oal_status(ret);
890 return OAL_STATUS_SUCCESS;
893 oal_status_t gatts_add_descriptor(int instance_id, int serv_handle,
894 oal_uuid_t* desc_uuid, int permsn)
896 int ret = OAL_STATUS_SUCCESS;
897 char str[2*BT_UUID_STRING_MAX];
898 char perm[MAX_PERM_LEN];
899 char_perm_to_string(permsn, perm);
900 API_TRACE("Server Add Descriptor : instance_id = %d, serv_handle = %d, Permisison = %s",
901 instance_id, serv_handle, perm);
902 CHECK_OAL_GATT_ENABLED();
904 CHECK_SERVER_INSTANCE(instance_id);
906 CHECK_SERVER_REGISTRATION(instance_id);
908 if (desc_uuid != NULL) {
909 uuid_to_stringname(desc_uuid, str);
910 API_TRACE("uuid: [%s]", str);
913 ret = gatt_api->server->add_descriptor(gatt_servers[instance_id - 1].server_id,
914 serv_handle, (bt_uuid_t*)desc_uuid, permsn);
916 if (ret != BT_STATUS_SUCCESS) {
917 BT_ERR("GATT Server Add Descriptor failed: %s", status2string(ret));
918 return convert_to_oal_status(ret);
920 return OAL_STATUS_SUCCESS;
923 oal_status_t gatts_start_service(int instance_id, int svc_handle, int transport)
925 int ret = OAL_STATUS_SUCCESS;
927 API_TRACE("Server Start Service : Ins_id = %d, srvHndl = %d, transport = %d",
928 instance_id, svc_handle, transport);
929 CHECK_OAL_GATT_ENABLED();
931 CHECK_SERVER_INSTANCE(instance_id);
933 CHECK_SERVER_REGISTRATION(instance_id);
935 ret = gatt_api->server->start_service(gatt_servers[instance_id - 1].server_id, svc_handle, transport);
937 if (ret != BT_STATUS_SUCCESS) {
938 BT_ERR("GATT Server Start Service failed: %s", status2string(ret));
939 return convert_to_oal_status(ret);
941 return OAL_STATUS_SUCCESS;
944 oal_status_t gatts_stop_service(int ins_id, int srv_hdl)
946 int ret = OAL_STATUS_SUCCESS;
948 API_TRACE("Server Stop Service : Ins_id = %d, srvHndl = %d, ", ins_id, srv_hdl);
949 CHECK_OAL_GATT_ENABLED();
951 CHECK_SERVER_INSTANCE(ins_id);
952 CHECK_SERVER_REGISTRATION(ins_id);
954 ret = gatt_api->server->stop_service(gatt_servers[ins_id - 1].server_id, srv_hdl);
955 if (ret != BT_STATUS_SUCCESS) {
956 BT_ERR("GATT Server Stop Service failed: %s", status2string(ret));
957 return convert_to_oal_status(ret);
959 return OAL_STATUS_SUCCESS;
962 oal_status_t gatts_delete_service(int ins_id, int srv_hdl)
964 int ret = OAL_STATUS_SUCCESS;
966 API_TRACE("Server Delete Service : Ins_id = %d, srvHndl = %d, ", ins_id, srv_hdl);
967 CHECK_OAL_GATT_ENABLED();
969 CHECK_SERVER_INSTANCE(ins_id);
971 CHECK_SERVER_REGISTRATION(ins_id);
973 ret = gatt_api->server->delete_service(gatt_servers[ins_id - 1].server_id, srv_hdl);
974 if (ret != BT_STATUS_SUCCESS) {
975 BT_ERR("GATT Server Delete Service failed: %s", status2string(ret));
976 return convert_to_oal_status(ret);
978 return OAL_STATUS_SUCCESS;
981 oal_status_t gatts_send_response(int conn_id, int trans_id, int resp_status, oal_gatt_response_t *response)
983 int ret = OAL_STATUS_SUCCESS;
985 API_TRACE("Server Send Response : ConnId = %d, TransId = %d, ", conn_id, trans_id);
986 CHECK_OAL_GATT_ENABLED();
988 if (response == NULL) {
989 BT_ERR("GATT Server attribute value is empty");
990 return OAL_STATUS_INVALID_PARAM;
993 ret = gatt_api->server->send_response(conn_id, trans_id, resp_status, (btgatt_response_t *)response);
994 if (ret != BT_STATUS_SUCCESS) {
995 BT_ERR("GATT Server Send Response failed: %s", status2string(ret));
996 return convert_to_oal_status(ret);
998 return OAL_STATUS_SUCCESS;
1001 oal_status_t gatts_send_indication(int ins_id, int attr_hndl, int conn_id, int len, int confirm, char* value)
1003 int ret = OAL_STATUS_SUCCESS;
1005 API_TRACE("Server Send Indication : Ins_id = %d, srvHndl = %d, [%s]",
1006 ins_id, attr_hndl, confirm == TRUE ? "INDICATION" : "NOTIFICATION");
1007 CHECK_OAL_GATT_ENABLED();
1009 CHECK_SERVER_INSTANCE(ins_id);
1011 CHECK_SERVER_REGISTRATION(ins_id);
1013 if (value == NULL || len == 0) {
1014 BT_ERR("GATT Server attribute value is empty");
1015 return OAL_STATUS_INVALID_PARAM;
1018 ret = gatt_api->server->send_indication(gatt_servers[ins_id - 1].server_id,
1019 attr_hndl, conn_id, len, confirm, value);
1021 if (ret != BT_STATUS_SUCCESS) {
1022 BT_ERR("GATT Server Send Indication failed: %s", status2string(ret));
1023 return convert_to_oal_status(ret);
1025 return OAL_STATUS_SUCCESS;
1028 oal_status_t gatts_update_att_value(int ins_id, oal_gatt_value_t *value)
1030 int ret = OAL_STATUS_SUCCESS;
1032 CHECK_OAL_GATT_ENABLED();
1034 CHECK_SERVER_INSTANCE(ins_id);
1036 CHECK_SERVER_REGISTRATION(ins_id);
1038 if (value == NULL || value->len == 0) {
1039 BT_ERR("GATT Server attribute value is empty");
1040 return OAL_STATUS_INVALID_PARAM;
1043 API_TRACE("Server Update Value: Ins_id = [%d], att handle = [%d] data len [%d]",
1044 ins_id, value->handle, value->len);
1046 ret = gatt_api->server->update_att_value(gatt_servers[ins_id - 1].server_id,
1047 (int)value->handle, (int)value->len, (char*)value->value);
1049 if (ret != BT_STATUS_SUCCESS) {
1050 BT_ERR("GATT Server Update Attribute Value failed: [%s]", status2string(ret));
1051 return convert_to_oal_status(ret);
1053 return OAL_STATUS_SUCCESS;
1056 /* GATT Server Callbacks:Start */
1057 static void cb_gatts_service_added(int status, int server_if,
1058 btgatt_srvc_id_t *psrvc_id, int srvc_handle)
1062 BT_INFO("BTGATT SERVER SERVICE ADDED CB, status:%d, handle:%d", status, srvc_handle);
1063 event_gatts_srvc_prm_t* event = g_new0(event_gatts_srvc_prm_t, 1);
1065 CHECK_CORRECT_SERVER_ID(instance_id, server_if, event->gatt_srvc_stat.server_inst);
1067 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1068 event->gatt_srvc_stat.server_inst = -1;
1070 event->gatt_srvc_stat.servic_hndl = srvc_handle;
1071 if (psrvc_id != NULL) {
1072 event->gatt_srvc_id.id.inst_id = psrvc_id->id.inst_id;
1073 //event->gatt_srvc_id.id.uuid = psrvc_id->id.uuid;
1074 memcpy(event->gatt_srvc_id.id.uuid.uuid, psrvc_id->id.uuid.uu, sizeof(psrvc_id->id.uuid));
1075 event->gatt_srvc_id.is_prmry = psrvc_id->is_primary;
1078 event->gatt_srvc_stat.status = convert_to_oal_status(status);
1080 send_event(OAL_EVENT_GATTS_SERVICE_ADDED, event, sizeof(event_gatts_srvc_prm_t));
1083 static void cb_gatts_included_service_added(int status, int server_if,
1085 int incl_srvc_handle)
1088 BT_INFO("BTGATT SERVER INCLUDED SERVICE ADDED CB, status:%d, srvc handle:%d, incl handle:%d",
1089 status, srvc_handle, incl_srvc_handle);
1091 event_gatts_incld_srvc_t* event = g_new0(event_gatts_incld_srvc_t, 1);
1093 CHECK_CORRECT_SERVER_ID(ins_id, server_if, event->gatt_srvc_stat.server_inst);
1095 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1096 event->gatt_srvc_stat.server_inst = -1;
1098 event->gatt_srvc_stat.servic_hndl = srvc_handle;
1099 event->incl_srvc_hndl = incl_srvc_handle;
1100 event->gatt_srvc_stat.status = convert_to_oal_status(status);
1101 send_event(OAL_EVENT_GATTS_INCLUDED_SERVICE_ADDED, event, sizeof(event_gatts_incld_srvc_t));
1104 static void cb_gatts_characteristic_added(int status, int server_if, bt_uuid_t *char_id,
1105 int srvc_handle, int char_handle)
1109 BT_INFO("BTGATT SERVER CHARACTERISTIC ADDED CB, status:%d, srvc handle:%d, char handle:%d",
1110 status, srvc_handle, char_handle);
1112 event_gatts_srvc_charctr_t* event = g_new0(event_gatts_srvc_charctr_t, 1);
1114 CHECK_CORRECT_SERVER_ID(Ins_id, server_if, event->gatt_srvc_stat.server_inst);
1116 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1117 event->gatt_srvc_stat.server_inst = -1;
1119 event->gatt_srvc_stat.servic_hndl = srvc_handle;
1120 event->charctr_hndl = char_handle;
1121 event->gatt_srvc_stat.status = convert_to_oal_status(status);
1122 memcpy(event->charctr_uuid.uuid, char_id->uu, sizeof(bt_uuid_t));
1123 send_event(OAL_EVENT_GATTS_CHARACTERISTIC_ADDED, event, sizeof(event_gatts_srvc_charctr_t));
1126 static void cb_gatts_descriptor_added(int status, int server_if,
1127 bt_uuid_t *descr_id, int srvc_handle,
1131 BT_INFO("BTGATT SERVER DESCRIPTOR ADDED CB, status:%d, srvc handle:%d, desc handle:%d",
1132 status, srvc_handle, descr_handle);
1134 event_gatts_srvc_descr_t* event = g_new0(event_gatts_srvc_descr_t, 1);
1136 CHECK_CORRECT_SERVER_ID(Ins_id, server_if, event->gatt_srvc_stat.server_inst);
1138 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1139 event->gatt_srvc_stat.server_inst = -1;
1141 event->gatt_srvc_stat.servic_hndl = srvc_handle;
1142 event->descrptr_hndl = descr_handle;
1143 event->gatt_srvc_stat.status = convert_to_oal_status(status);
1144 memcpy(event->descrptr_uuid.uuid, descr_id->uu, sizeof(bt_uuid_t));
1145 send_event(OAL_EVENT_GATTS_DESCRIPTOR_ADDED, event, sizeof(event_gatts_srvc_descr_t));
1148 static void cb_gatts_service_started(int status, int server_if, int srvc_handle)
1151 BT_INFO("BTGATT SERVER SERVICE STARTED CB, status:%d, srvc_handle:%d", status, srvc_handle);
1153 event_gatts_srvc_t* event = g_new0(event_gatts_srvc_t, 1);
1155 CHECK_CORRECT_SERVER_ID(Ins_id, server_if, event->server_inst);
1157 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1158 event->server_inst = -1;
1160 event->servic_hndl = srvc_handle;
1161 event->status = convert_to_oal_status(status);
1162 send_event(OAL_EVENT_GATTS_SERVICE_STARTED, event, sizeof(event_gatts_srvc_t));
1165 static void cb_gatts_service_stopped(int status, int server_if, int srvc_handle)
1168 BT_INFO("BTGATT SERVER SERVICE STOPPED CB, status:%d, srvc_handle:%d", status, srvc_handle);
1170 event_gatts_srvc_t* event = g_new0(event_gatts_srvc_t, 1);
1172 CHECK_CORRECT_SERVER_ID(ins_id, server_if, event->server_inst);
1174 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1175 event->server_inst = -1;
1177 event->servic_hndl = srvc_handle;
1178 event->status = convert_to_oal_status(status);
1179 send_event(OAL_EVENT_GATTS_SERVICE_STOPED, event, sizeof(event_gatts_srvc_t));
1182 static void cb_gatts_service_deleted(int status, int server_if, int srvc_handle)
1185 BT_INFO("BTGATT SERVER SERVICE DELETED CB, status:%d, srvc_handle:%d", status, srvc_handle);
1187 event_gatts_srvc_t* event = g_new0(event_gatts_srvc_t, 1);
1189 CHECK_CORRECT_SERVER_ID(ins_id, server_if, event->server_inst);
1191 BT_ERR("Invalid Interface, srv_if: %d", server_if);
1192 event->server_inst = -1;
1194 event->servic_hndl = srvc_handle;
1195 event->status = convert_to_oal_status(status);
1196 send_event(OAL_EVENT_GATTS_SERVICE_DELETED, event, sizeof(event_gatts_srvc_t));
1199 static void cb_gatts_connection(int conn_id, int server_if, int connected, bt_bdaddr_t *bda)
1202 oal_event_t event_type;
1204 BT_INFO("BTGATT SERVER CONNECTION connnected:%d, conn_id:%d server_if:%d", connected, conn_id, server_if);
1205 event_gatts_conn_t *event = g_new0(event_gatts_conn_t, 1);
1207 for (ins_id = 1 ; ins_id <= NUM_SERVER_INST ; ins_id++) {
1208 if ((gatt_servers[ins_id - 1].server_id == server_if) &&
1209 (gatt_servers[ins_id - 1].state == GATT_INS_ENABLED)) {
1210 event->server_inst = ins_id;
1213 BT_INFO("Server ID:%d recevied server Id:%d\n", gatt_servers[ins_id - 1].server_id, server_if);
1216 if (ins_id == NUM_SERVER_INST+1) {
1217 BT_WARN("Incorrect Server Interface received: %d\n", server_if);
1221 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1222 event->conn_id = conn_id;
1223 event_type = ((connected == TRUE) ? OAL_EVENT_GATTS_CONNECTION_COMPLETED : OAL_EVENT_GATTS_DISCONNECTION_COMPLETED);
1225 send_event(event_type, event, sizeof(event_gatts_conn_t));
1228 static void cb_gatts_request_read(int conn_id, int trans_id, bt_bdaddr_t *bda,
1229 int attr_handle, int offset, bool is_long)
1231 BT_INFO("BTGATT SERVER REQUEST READ CB: conn_id:%d", conn_id);
1233 event_gatts_srvc_read_attr_t* event = g_new0(event_gatts_srvc_read_attr_t, 1);
1235 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1237 event->attr_trans.attr_handle = attr_handle;
1238 event->attr_trans.conn_id = conn_id;
1239 event->attr_trans.trans_id = trans_id;
1240 event->attr_trans.offset = offset;
1241 event->is_long = is_long;
1243 send_event(OAL_EVENT_GATTS_REQUEST_READ, event, sizeof(event_gatts_srvc_read_attr_t));
1246 static void cb_gatts_request_write(int conn_id, int trans_id,
1247 bt_bdaddr_t *bda, int attr_handle,
1248 int offset, int length,
1249 bool need_rsp, bool is_prep, uint8_t* value)
1251 BT_INFO("BTGATT SERVER REQUEST WRITE conn_id:%d", conn_id);
1253 event_gatts_srvc_write_attr_t* event = g_new0(event_gatts_srvc_write_attr_t, 1);
1255 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1257 event->attr_trans.attr_handle = attr_handle;
1258 event->attr_trans.conn_id = conn_id;
1259 event->attr_trans.trans_id = trans_id;
1260 event->attr_trans.offset = offset;
1261 event->need_rsp = need_rsp;
1262 event->is_prep = is_prep;
1264 if (length > 0 && value != NULL) {
1265 if (length > OAL_GATT_MAX_ATTR_LEN)
1266 length = OAL_GATT_MAX_ATTR_LEN;
1267 memcpy(event->value, value, length);
1268 event->length = length;
1270 send_event(OAL_EVENT_GATTS_REQUEST_WRITE, event, sizeof(event_gatts_srvc_write_attr_t));
1273 static void cb_gatts_acquire_write(int mtu, int conn_id, int trans_id, int attr_handle , bt_bdaddr_t *bda)
1276 BT_INFO("BTGATT SERVER REQUEST ACQUIRE WRITE conn_id:%d", conn_id);
1278 event_gatts_srvc_acquire_attr_t* event = g_new0(event_gatts_srvc_acquire_attr_t, 1);
1280 event->attr_trans.attr_handle = attr_handle;
1281 event->attr_trans.conn_id = conn_id;
1282 event->attr_trans.trans_id = trans_id;
1284 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1286 send_event(OAL_EVENT_GATTS_REQUEST_ACQUIRE_WRITE, event, sizeof(event_gatts_srvc_acquire_attr_t));
1289 static void cb_gatts_response_confirmation(int status, int handle)
1291 BT_INFO("BTGATT SERVER RESPONSE CONFIRMATION CB, status:%d, handle:%d", status, handle);
1293 event_gatts_respons_t* event = g_new0(event_gatts_respons_t, 1);
1295 event->hndl = handle;
1296 event->status = convert_to_oal_status(status);
1298 send_event(OAL_EVENT_GATTS_RESPONSE_CONFIRMED, event, sizeof(event_gatts_respons_t));
1301 static void cb_indication_confirmation(int conn_id, int trans_id, int attr_handle, bt_bdaddr_t *bda)
1303 BT_INFO("BTGATT SERVER Indication Confirmation CB, conn_id:%d, trans_id:%d attr_handle:%d",
1304 conn_id, trans_id, attr_handle);
1306 event_gatts_ind_cnfrm_t* event = g_new0(event_gatts_ind_cnfrm_t, 1);
1308 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1310 event->attr_handle = attr_handle;
1311 event->conn_id = conn_id;
1312 event->trans_id = trans_id;
1313 send_event(OAL_EVENT_GATTS_IND_CONFIRM, event, sizeof(event_gatts_ind_cnfrm_t));
1316 static void cb_gatts_mtu_changed(int conn_id, int mtu)
1318 BT_INFO("BTGATT SERVER MTU CHANGED CB, conn_id: %d, mtu_size: %d", conn_id, mtu);
1319 event_gatts_mtu_changed_t *event = g_new0(event_gatts_mtu_changed_t, 1);
1320 event->conn_id = conn_id;
1321 event->mtu_size = mtu;
1322 send_event(OAL_EVENT_GATTS_MTU_CHANGED, event, sizeof(event_gatts_mtu_changed_t));
1326 static void cb_notifcation_changed(int conn_id, int trans_id, int attr_handle, bool notify, bt_bdaddr_t *bda)
1328 BT_INFO("BTGATT SERVER Notication CB, conn_id:%d, trans_id:%d attr_handle:%d Notify:%d",
1329 conn_id, trans_id, attr_handle, notify);
1331 event_gatts_notif_t* event = g_new0(event_gatts_notif_t, 1);
1333 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1335 event->attr_handle = attr_handle;
1336 event->conn_id = conn_id;
1337 event->trans_id = trans_id;
1338 event->notify = notify;
1339 send_event(OAL_EVENT_GATTS_NOTIFICATION, event, sizeof(event_gatts_notif_t));
1344 /* GATT Server Callbacks: End */
1345 /************************************ GATT Client ***********************************/
1346 /* Client Callbacks */
1347 static void cb_gattc_register_app(int status, int clientIf, bt_uuid_t *app_uuid)
1349 char uuid_str[BT_UUID_STRING_MAX];
1350 event_gattc_register_t *event;
1354 /* Check if GATT client registered for Default GATT client UUID */
1355 uuid_to_string((service_uuid_t *)app_uuid, uuid_str);
1356 BT_INFO("UUID:%s", uuid_str);
1358 BT_INFO("BTGATT CLIENT REGISTER APP CB, status:%d, clientIf:%d", status, clientIf);
1359 event = g_new0(event_gattc_register_t, 1);
1360 event->client_if = clientIf;
1361 event->status = (status == 0 ? OAL_STATUS_SUCCESS : OAL_STATUS_INTERNAL_ERROR);
1362 memcpy(event->client_uuid.uuid, app_uuid->uu, sizeof(oal_uuid_t));
1363 send_event(OAL_EVENT_GATTC_REGISTRATION, event, sizeof(event_gattc_register_t));
1366 static void cb_gattc_scan_result(bt_bdaddr_t* bdaddress, int rssi, uint8_t *adv_data)
1368 event_ble_scan_result_info *event;
1370 event = g_new0(event_ble_scan_result_info, 1);
1372 memcpy(event->address.addr, bdaddress->address, BT_ADDRESS_BYTES_NUM);
1373 memcpy(event->adv_data, adv_data, BLE_ADV_DATA_LENGTH);
1374 send_event_bda_trace(OAL_EVENT_BLE_REMOTE_DEVICE_FOUND, event,
1375 sizeof(event_ble_scan_result_info), (bt_address_t *)bdaddress);
1378 static void cb_gattc_connection(int conn_id, int status, int client_if, bt_bdaddr_t* bda)
1380 event_gattc_conn_t *event;
1381 BT_INFO("BTGATT CLIENT CONNECTED CB, status:[%d] client_if[%d] id[%d]",
1382 status, client_if, conn_id);
1384 event = g_new0(event_gattc_conn_t, 1);
1385 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1386 event->client_if = client_if;
1388 if (BT_STATUS_SUCCESS != status) {
1389 event->status = OAL_STATUS_INTERNAL_ERROR;
1390 BT_ERR("gattc connection Error: %d", status);
1392 event->conn_id = conn_id;
1393 event->status = OAL_STATUS_SUCCESS;
1396 send_event_bda_trace(OAL_EVENT_GATTC_CONNECTION_COMPLETED, event,
1397 sizeof(*event), (bt_address_t *)bda);
1401 static void cb_gattc_disconnect(int conn_id, int status, int client_if, bt_bdaddr_t* bda)
1403 event_gattc_conn_t *event;
1404 BT_INFO("BTGATT Client Disconnect Complete cb, status:[%d], client_if[%d] id[%d]",
1405 status, client_if, conn_id);
1406 event = g_new0(event_gattc_conn_t, 1);
1407 event->status = convert_to_oal_status(status);
1408 event->client_if = client_if;
1409 event->conn_id = conn_id;
1410 memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1411 send_event_bda_trace(OAL_EVENT_GATTC_DISCONNECTION_COMPLETED, event,
1412 sizeof(*event), (bt_address_t *)bda);
1415 static void cb_gattc_search_result(int conn_id, btgatt_srvc_id_t *srvc_id)
1417 char uuid_str[2*BT_UUID_STRING_MAX];
1419 BT_INFO("BTGATT Client Service Search Result cb, conn_id:%d", conn_id);
1421 uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str);
1422 BT_INFO("Service=> UUID: [%s], Inst_id: [%u], Type: [%s]",
1423 uuid_str, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1425 event_gattc_service_result_t *event = g_new0(event_gattc_service_result_t, 1);
1426 event->conn_status.status = OAL_STATUS_SUCCESS;
1427 event->conn_status.conn_id = conn_id;
1428 memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1430 send_event(OAL_EVENT_GATTC_SERVICE_SEARCH_RESULT, event, sizeof(*event));
1433 static void cb_gattc_search_complete(int conn_id, int status)
1435 BT_INFO("BTGATT Client Service Search Complete cb, status:%d, conn_id:%d",
1438 event_gattc_conn_status_t *event = g_new0(event_gattc_conn_status_t, 1);
1439 event->conn_id = conn_id;
1440 event->status = convert_to_oal_status(status);
1442 send_event(OAL_EVENT_GATTC_SERVICE_SEARCH_DONE, event, sizeof(*event));
1446 static void cb_gattc_get_characteristics(int conn_id, int status, btgatt_srvc_id_t *srvc_id,
1447 btgatt_gatt_id_t *char_id, int char_prop)
1449 char uuid_str1[2*BT_UUID_STRING_MAX];
1450 char uuid_str2[2*BT_UUID_STRING_MAX];
1452 BT_INFO("BTGATT Client Get Characteristic Callback, conn_id:%d, status:%d", conn_id, status);
1453 uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str1);
1454 BT_INFO("Service=> UUID: [%s], Inst_id: [%u], Type: [%s]",
1455 uuid_str1, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1457 event_gattc_characteristic_result_t *event = g_new0(event_gattc_characteristic_result_t, 1);
1458 event->conn_status.conn_id = conn_id;
1459 event->conn_status.status = convert_to_oal_status(status);
1460 memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1463 uuid_to_stringname((oal_uuid_t *)&(char_id->uuid), uuid_str2);
1464 BT_INFO("Charac=> UUID: [%s], Inst_id: [%u], Prop: [%s]",
1465 uuid_str2, char_id->inst_id, char_prop_to_string(char_prop, str));
1466 event->char_prop = char_prop;
1467 memcpy(&(event->char_id), char_id, sizeof(oal_gatt_id_t));
1469 send_event(OAL_EVENT_GATTC_CHARAC_SERACH_RESULT, event, sizeof(*event));
1474 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)
1476 char uuid_str1[2*BT_UUID_STRING_MAX];
1477 char uuid_str2[2*BT_UUID_STRING_MAX];
1478 char uuid_str3[2*BT_UUID_STRING_MAX];
1479 BT_INFO("BTGATT Client Get Descriptor Callback, conn_id:%d, status:%d", conn_id, status);
1480 uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str1);
1481 uuid_to_stringname((oal_uuid_t *)&(char_id->uuid), uuid_str2);
1482 BT_INFO("Service=> UUID: [%s], Inst_id: [%u], Type: [%s]",
1483 uuid_str1, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1484 BT_INFO("Charac=> UUID: [%s], Inst_id: [%u]", uuid_str2, char_id->inst_id);
1486 event_gattc_descriptor_result_t *event = g_new0(event_gattc_descriptor_result_t, 1);
1487 event->conn_status.conn_id = conn_id;
1488 event->conn_status.status = convert_to_oal_status(status);
1489 memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1490 memcpy(&(event->char_id), char_id, sizeof(oal_gatt_id_t));
1493 uuid_to_stringname((oal_uuid_t *)&(descr_id->uuid), uuid_str3);
1494 BT_INFO("Desc=> UUID: [%s], Inst_id: [%u]", uuid_str3, descr_id->inst_id);
1495 memcpy(&(event->descr_id), descr_id, sizeof(oal_gatt_id_t));
1497 send_event(OAL_EVENT_GATTC_DESC_SERACH_RESULT, event, sizeof(*event));
1500 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)
1502 char uuid_str1[2*BT_UUID_STRING_MAX];
1503 char uuid_str2[2*BT_UUID_STRING_MAX];
1504 BT_INFO("BTGATT Client Register For Notification Callback, client_if:%d, status:%d, registered: %s",
1505 client_if, status, registered == 1 ? "REGISTERED" : "DEREGISTERED");
1506 uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str1);
1507 uuid_to_stringname((oal_uuid_t *)&(char_id->uuid), uuid_str2);
1508 BT_INFO("Service=> UUID: [%s], Inst_id: [%u], Type: [%s]", uuid_str1, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1509 BT_INFO("Charac=> UUID: [%s], Inst_id: [%u]", uuid_str2, char_id->inst_id);
1511 event_gattc_regdereg_notify_t *event = g_new0(event_gattc_regdereg_notify_t, 1);
1512 oal_event_t event_type = (registered == 1 ? OAL_EVENT_GATTC_NOTIFICATION_REGISTERED : OAL_EVENT_GATTC_NOTIFICATION_DEREGISTERED);
1513 event->client_if = client_if;
1514 event->status = convert_to_oal_status(status);
1515 memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1516 memcpy(&(event->char_id), char_id, sizeof(oal_gatt_id_t));
1518 send_event(event_type, event, sizeof(*event));
1522 static void cb_gattc_read_characteristic(int conn_id, int status, btgatt_read_params_t *p_data)
1524 char uuid_str1[2*BT_UUID_STRING_MAX];
1525 char uuid_str2[2*BT_UUID_STRING_MAX];
1526 BT_INFO("BTGATT Client Read Charcateristic Callback, conn_id:%d, read_status:%d", conn_id, status);
1528 event_gattc_read_data *event = g_new0(event_gattc_read_data, 1);
1529 event->uuid_status.conn_status.conn_id = conn_id;
1530 event->uuid_status.conn_status.status = convert_to_oal_status(status);
1531 event->value_type = p_data->value_type;
1532 memcpy(&(event->uuid_status.srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1533 memcpy(&(event->uuid_status.char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1535 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1536 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1537 BT_INFO("Service=> UUID: [%s], Inst_id: [%u], Type: [%s]", uuid_str1,
1538 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1539 BT_INFO("Charac=> UUID: [%s], Inst_id: [%u]", uuid_str2, p_data->char_id.inst_id);
1540 BT_INFO("Len: %u, value_type: %u", p_data->value.len, p_data->value_type);
1541 if (p_data->value.len > 0 && event->value_type == GATTC_READ_VALUE_TYPE_VALUE) {
1543 data = g_malloc(3*p_data->value.len+1);
1545 BT_ERR("memory allocation failed");
1549 convert_hex_2_str((unsigned char *)p_data->value.value, p_data->value.len, data);
1550 BT_INFO("Read Data: [%s]", data);
1551 event->data_len = p_data->value.len;
1552 memcpy(&(event->data), &(p_data->value.value), event->data_len);
1556 send_event(OAL_EVENT_GATTC_READ_CHARAC, event, sizeof(*event));
1559 static void cb_gattc_write_characteristic(int conn_id, int status, btgatt_write_params_t *p_data)
1561 char uuid_str1[2*BT_UUID_STRING_MAX];
1562 char uuid_str2[2*BT_UUID_STRING_MAX];
1563 BT_INFO("BTGATT Client Write Charcateristic Callback, conn_id:%d, write_status:%d", conn_id, status);
1565 event_gattc_write_data *event = g_new0(event_gattc_write_data, 1);
1566 event->conn_status.conn_id = conn_id;
1567 event->conn_status.status = convert_to_oal_status(status);
1568 memcpy(&(event->srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1569 memcpy(&(event->char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1571 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1572 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1573 BT_INFO("Service=> UUID: [%s], Inst_id: [%u], Type: [%s]", uuid_str1,
1574 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1575 BT_INFO("Charac=> UUID: [%s], Inst_id: [%u]", uuid_str2, p_data->char_id.inst_id);
1577 send_event(OAL_EVENT_GATTC_WRITE_CHARAC, event, sizeof(*event));
1580 static void cb_gattc_write_descriptor(int conn_id, int status, btgatt_write_params_t *p_data)
1582 char uuid_str1[2*BT_UUID_STRING_MAX];
1583 char uuid_str2[2*BT_UUID_STRING_MAX];
1584 char uuid_str3[2*BT_UUID_STRING_MAX];
1585 BT_INFO("BTGATT Client Write Descriptor Callback, conn_id:%d, write_status:%d", conn_id, status);
1587 event_gattc_write_data *event = g_new0(event_gattc_write_data, 1);
1588 event->conn_status.conn_id = conn_id;
1589 event->conn_status.status = convert_to_oal_status(status);
1590 memcpy(&(event->srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1591 memcpy(&(event->char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1592 memcpy(&(event->descr_id), &(p_data->descr_id), sizeof(oal_gatt_id_t));
1594 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1595 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1596 uuid_to_stringname((oal_uuid_t *)&(p_data->descr_id.uuid), uuid_str3);
1597 BT_INFO("Service=> UUID: [%s], Inst_id: [%u], Type: [%s]", uuid_str1,
1598 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1599 BT_INFO("Charac=> UUID: [%s], Inst_id: [%u]", uuid_str2, p_data->char_id.inst_id);
1600 BT_INFO("Desc=> UUID: [%s], Inst_id: [%u]", uuid_str3, p_data->descr_id.inst_id);
1602 send_event(OAL_EVENT_GATTC_WRITE_DESCR, event, sizeof(*event));
1607 static void cb_gattc_read_descriptor(int conn_id, int status, btgatt_read_params_t *p_data)
1609 char uuid_str1[2*BT_UUID_STRING_MAX];
1610 char uuid_str2[2*BT_UUID_STRING_MAX];
1611 char uuid_str3[2*BT_UUID_STRING_MAX];
1612 BT_INFO("BTGATT Client Read Descriptor Callback, conn_id:%d, read_status:%d", conn_id, status);
1614 event_gattc_read_data *event = g_new0(event_gattc_read_data, 1);
1615 event->uuid_status.conn_status.conn_id = conn_id;
1616 event->uuid_status.conn_status.status = convert_to_oal_status(status);
1617 event->value_type = p_data->value_type;
1618 memcpy(&(event->uuid_status.srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1619 memcpy(&(event->uuid_status.char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1620 memcpy(&(event->uuid_status.descr_id), &(p_data->descr_id), sizeof(oal_gatt_id_t));
1623 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1624 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1625 uuid_to_stringname((oal_uuid_t *)&(p_data->descr_id.uuid), uuid_str3);
1626 BT_INFO("Service=> UUID: [%s], Inst_id: [%u], Type: [%s]", uuid_str1,
1627 p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1628 BT_INFO("Charac=> UUID: [%s], Inst_id: [%u]", uuid_str2, p_data->char_id.inst_id);
1629 BT_INFO("Desc=> UUID: [%s], Inst_id: [%u]", uuid_str3, p_data->descr_id.inst_id);
1630 BT_INFO("Len: %u, value_type: %u", p_data->value.len, p_data->value_type);
1631 if (p_data->value.len > 0 && event->value_type == GATTC_READ_VALUE_TYPE_VALUE) {
1633 data = g_malloc(3*p_data->value.len+1);
1635 BT_ERR("memory allocation failed");
1639 convert_hex_2_str((unsigned char *)p_data->value.value, p_data->value.len, data);
1640 BT_INFO("Read Data: [%s]", data);
1641 event->data_len = p_data->value.len;
1642 memcpy(&(event->data), &(p_data->value.value), event->data_len);
1646 send_event(OAL_EVENT_GATTC_READ_DESCR, event, sizeof(*event));
1649 oal_status_t gattc_start_le_discovery(int client_id)
1652 int ret = OAL_STATUS_SUCCESS;
1654 API_TRACE("BTGATT CLIENT SCAN START");
1655 CHECK_OAL_GATT_ENABLED();
1656 CHECK_CLIENT_REGISTRATION(client_id);
1658 ret = gatt_api->client->scan(client_id, 1);
1659 if (ret != BT_STATUS_SUCCESS) {
1660 BT_ERR("Error:Start LE Discovery failed: %s", status2string(ret));
1661 return convert_to_oal_status(ret);
1663 send_event(OAL_EVENT_BLE_DISCOVERY_STARTED, NULL, 0);
1664 return OAL_STATUS_SUCCESS;
1667 oal_status_t gattc_stop_le_discovery(int client_id)
1670 int ret = OAL_STATUS_SUCCESS;
1672 API_TRACE("Scan is stopped");
1673 CHECK_OAL_GATT_ENABLED();
1674 CHECK_CLIENT_REGISTRATION(client_id);
1675 ret = gatt_api->client->scan(client_id, 0);
1676 if (ret != BT_STATUS_SUCCESS) {
1677 BT_ERR("Error:Stop LE Discovery failed: %s", status2string(ret));
1678 return convert_to_oal_status(ret);
1680 send_event(OAL_EVENT_BLE_DISCOVERY_STOPPED, NULL, 0);
1681 return OAL_STATUS_SUCCESS;
1684 oal_status_t gattc_set_le_scan_param(int scan_type, int itv, int win)
1688 API_TRACE("Scan is stopped");
1689 CHECK_OAL_GATT_ENABLED();
1692 ret = gatt_api->client->set_scan_parameters(scan_type, itv, win);
1693 if (ret != BT_STATUS_SUCCESS) {
1694 BT_ERR("Error:Set scan parameters failed: %s", status2string(ret));
1695 return convert_to_oal_status(ret);
1698 ret = gatt_api->client->set_scan_parameters(itv, win);
1699 if (ret != BT_STATUS_SUCCESS) {
1700 BT_ERR("Error:Set scan parameters failed: %s", status2string(ret));
1701 return convert_to_oal_status(ret);
1705 return OAL_STATUS_SUCCESS;
1707 /************************************ GATT Client ***********************************/
1708 oal_status_t gattc_register(oal_uuid_t* client_uuid)
1711 char str[2*BT_UUID_STRING_MAX];
1712 int ret = OAL_STATUS_SUCCESS;
1714 CHECK_OAL_GATT_ENABLED();
1715 uuid_to_stringname(client_uuid, str);
1716 API_TRACE("uuid: [%s]", str);
1717 /* We will perform actual registration in cb_gattc_register_app callback */
1718 ret = gatt_api->client->register_client((bt_uuid_t *)client_uuid);
1719 if (ret != BT_STATUS_SUCCESS) {
1720 BT_ERR("GATT client register failed: %s", status2string(ret));
1721 return convert_to_oal_status(ret);
1723 return OAL_STATUS_SUCCESS;
1726 oal_status_t gattc_deregister(int client_id)
1728 int ret = OAL_STATUS_SUCCESS;
1730 API_TRACE("GATT client deregister");
1731 CHECK_OAL_GATT_ENABLED();
1732 CHECK_CLIENT_REGISTRATION(client_id);
1734 ret = gatt_api->client->unregister_client(client_id);
1735 if (ret != BT_STATUS_SUCCESS) {
1736 BT_ERR("GATT client deregister failed: %s", status2string(ret));
1737 return convert_to_oal_status(ret);
1740 return OAL_STATUS_SUCCESS;
1743 oal_status_t gattc_connect(int client_id, bt_address_t *device_address, int isDirect)
1745 int ret = OAL_STATUS_SUCCESS;
1748 OAL_CHECK_PARAMETER(device_address, return);
1749 API_TRACE("Client Connect: [%s]", bdt_bd2str(device_address, &bdstr));
1750 CHECK_OAL_GATT_ENABLED();
1751 CHECK_CLIENT_REGISTRATION(client_id);
1753 /* Handle the actual connection in cb_gattc_connection callback */
1754 ret = gatt_api->client->connect(client_id, (bt_bdaddr_t *)device_address, isDirect);
1755 if (ret != BT_STATUS_SUCCESS) {
1756 BT_ERR("GATT client connect failed: %s", status2string(ret));
1757 return convert_to_oal_status(ret);
1759 return OAL_STATUS_SUCCESS;
1762 oal_status_t gattc_disconnect(int client_id, bt_address_t *device_address, int conn_id)
1765 int ret = OAL_STATUS_SUCCESS;
1768 OAL_CHECK_PARAMETER(device_address, return);
1769 API_TRACE("Client Disconnect: [%s]", bdt_bd2str(device_address, &bdstr));
1770 CHECK_OAL_GATT_ENABLED();
1771 CHECK_CLIENT_REGISTRATION(client_id);
1772 CHECK_CLIENT_CONNECTION(conn_id);
1774 /* Handle actual disconnection in callback */
1775 ret = gatt_api->client->disconnect(client_id, (bt_bdaddr_t *) device_address, conn_id);
1776 if (ret != BT_STATUS_SUCCESS) {
1777 BT_ERR("GATT client disconnect failed: %s", status2string(ret));
1778 return convert_to_oal_status(ret);
1780 return OAL_STATUS_SUCCESS;
1784 oal_status_t gattc_search_service(int conn_id, oal_uuid_t *service_uuid)
1786 int ret = OAL_STATUS_SUCCESS;
1787 char uuid_str[2*BT_UUID_STRING_MAX];
1790 uuid_to_stringname(service_uuid, uuid_str);
1791 API_TRACE("Client Service Search UUID: [%s]", uuid_str);
1793 API_TRACE("Client Service Search All");
1795 CHECK_OAL_GATT_ENABLED();
1796 CHECK_CLIENT_CONNECTION(conn_id);
1797 ret = gatt_api->client->search_service(conn_id, (bt_uuid_t *)service_uuid);
1799 if (ret != BT_STATUS_SUCCESS) {
1800 BT_ERR("GATT client service search failed: %s", status2string(ret));
1801 return convert_to_oal_status(ret);
1803 return OAL_STATUS_SUCCESS;
1806 oal_status_t gattc_get_characteristic(int conn_id, oal_gatt_srvc_id_t *srvc_id, oal_gatt_id_t *char_id)
1808 int ret = OAL_STATUS_SUCCESS;
1809 char uuid_str[2*BT_UUID_STRING_MAX];
1811 OAL_CHECK_PARAMETER(srvc_id, return);
1812 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str);
1813 API_TRACE("Client Get Characteristic, Service_uuid: [%s]", uuid_str);
1814 CHECK_OAL_GATT_ENABLED();
1815 CHECK_CLIENT_CONNECTION(conn_id);
1816 ret = gatt_api->client->get_characteristic(conn_id, (btgatt_srvc_id_t *)srvc_id,
1817 (btgatt_gatt_id_t *)char_id);
1818 if (ret != BT_STATUS_SUCCESS) {
1819 BT_ERR("GATT client get characteristic failed: %s", status2string(ret));
1820 return convert_to_oal_status(ret);
1822 return OAL_STATUS_SUCCESS;
1825 oal_status_t gattc_get_descriptor(int conn_id, oal_gatt_srvc_id_t *srvc_id,
1826 oal_gatt_id_t *char_id, oal_gatt_id_t *desc_id)
1828 int ret = OAL_STATUS_SUCCESS;
1829 char uuid_str1[2*BT_UUID_STRING_MAX];
1830 char uuid_str2[2*BT_UUID_STRING_MAX];
1832 OAL_CHECK_PARAMETER(srvc_id, return);
1833 OAL_CHECK_PARAMETER(char_id, return);
1834 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1835 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1836 API_TRACE("Client Get Descriptor, Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
1837 CHECK_OAL_GATT_ENABLED();
1838 CHECK_CLIENT_CONNECTION(conn_id);
1839 ret = gatt_api->client->get_descriptor(conn_id, (btgatt_srvc_id_t *)srvc_id,
1840 (btgatt_gatt_id_t *)char_id, (btgatt_gatt_id_t *)desc_id);
1841 if (ret != BT_STATUS_SUCCESS) {
1842 BT_ERR("GATT client get descriptor failed: %s", status2string(ret));
1843 return convert_to_oal_status(ret);
1845 return OAL_STATUS_SUCCESS;
1848 oal_status_t gattc_register_for_notification(int client_id, bt_address_t * address,
1849 oal_gatt_srvc_id_t *srvc_id, oal_gatt_id_t *char_id) {
1850 int ret = OAL_STATUS_SUCCESS;
1851 char uuid_str1[2*BT_UUID_STRING_MAX];
1852 char uuid_str2[2*BT_UUID_STRING_MAX];
1855 OAL_CHECK_PARAMETER(address, return);
1856 OAL_CHECK_PARAMETER(srvc_id, return);
1857 OAL_CHECK_PARAMETER(char_id, return);
1858 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1859 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1860 API_TRACE("Client Register Notification: [%s], Service_uuid: [%s], Char_uuid: [%s]", bdt_bd2str(address, &bdstr), uuid_str1, uuid_str2);
1861 CHECK_OAL_GATT_ENABLED();
1862 CHECK_CLIENT_REGISTRATION(client_id);
1864 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);
1865 if (ret != BT_STATUS_SUCCESS) {
1866 BT_ERR("GATT client register notification failed: %s", status2string(ret));
1867 return convert_to_oal_status(ret);
1870 return OAL_STATUS_SUCCESS;
1873 oal_status_t gattc_deregister_for_notification(int client_id, bt_address_t * address,
1874 oal_gatt_srvc_id_t *srvc_id, oal_gatt_id_t *char_id) {
1875 int ret = OAL_STATUS_SUCCESS;
1876 char uuid_str1[2*BT_UUID_STRING_MAX];
1877 char uuid_str2[2*BT_UUID_STRING_MAX];
1880 OAL_CHECK_PARAMETER(address, return);
1881 OAL_CHECK_PARAMETER(srvc_id, return);
1882 OAL_CHECK_PARAMETER(char_id, return);
1883 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1884 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1885 API_TRACE("Client Deregister Notification: [%s], Service_uuid: [%s], Char_uuid: [%s]", bdt_bd2str(address, &bdstr), uuid_str1, uuid_str2);
1886 CHECK_OAL_GATT_ENABLED();
1887 CHECK_CLIENT_REGISTRATION(client_id);
1889 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);
1890 if (ret != BT_STATUS_SUCCESS) {
1891 BT_ERR("GATT client deregister notification failed: %s", status2string(ret));
1892 return convert_to_oal_status(ret);
1895 return OAL_STATUS_SUCCESS;
1899 oal_status_t gattc_read_characteristic(int conn_id, oal_gatt_srvc_id_t *srvc_id,
1900 oal_gatt_id_t *char_id, oal_gatt_auth_req_t auth_req)
1902 int ret = OAL_STATUS_SUCCESS;
1903 char uuid_str1[2*BT_UUID_STRING_MAX];
1904 char uuid_str2[2*BT_UUID_STRING_MAX];
1906 OAL_CHECK_PARAMETER(srvc_id, return);
1907 OAL_CHECK_PARAMETER(char_id, return);
1908 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1909 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1910 API_TRACE("Client Read Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
1911 CHECK_OAL_GATT_ENABLED();
1912 CHECK_CLIENT_CONNECTION(conn_id);
1914 ret = gatt_api->client->read_characteristic(conn_id, (btgatt_srvc_id_t *)srvc_id,
1915 (btgatt_gatt_id_t *)char_id, auth_req);
1916 if (ret != BT_STATUS_SUCCESS) {
1917 BT_ERR("GATT client read characteristic failed: %s", status2string(ret));
1918 return convert_to_oal_status(ret);
1921 return OAL_STATUS_SUCCESS;
1924 oal_status_t gattc_read_descriptor(int conn_id, oal_gatt_srvc_id_t *srvc_id,
1925 oal_gatt_id_t *char_id, oal_gatt_id_t *desc_id, oal_gatt_auth_req_t auth_req)
1927 int ret = OAL_STATUS_SUCCESS;
1928 char uuid_str1[2*BT_UUID_STRING_MAX];
1929 char uuid_str2[2*BT_UUID_STRING_MAX];
1930 char uuid_str3[2*BT_UUID_STRING_MAX];
1932 OAL_CHECK_PARAMETER(srvc_id, return);
1933 OAL_CHECK_PARAMETER(char_id, return);
1934 OAL_CHECK_PARAMETER(desc_id, return);
1935 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1936 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1937 uuid_to_stringname(&(desc_id->uuid), uuid_str3);
1938 API_TRACE("Client Read Descriptor: Service_uuid: [%s], Char_uuid: [%s], Desc_uuid: [%s]",
1939 uuid_str1, uuid_str2, uuid_str3);
1940 CHECK_OAL_GATT_ENABLED();
1941 CHECK_CLIENT_CONNECTION(conn_id);
1943 ret = gatt_api->client->read_descriptor(conn_id, (btgatt_srvc_id_t *)srvc_id,
1944 (btgatt_gatt_id_t *)char_id, (btgatt_gatt_id_t *)desc_id, auth_req);
1945 if (ret != BT_STATUS_SUCCESS) {
1946 BT_ERR("GATT client read descriptor failed: %s", status2string(ret));
1947 return convert_to_oal_status(ret);
1950 return OAL_STATUS_SUCCESS;
1953 oal_status_t gattc_write_characteristic(int conn_id, oal_gatt_srvc_id_t *srvc_id,
1954 oal_gatt_id_t *char_id, oal_gatt_write_type_t write_type,
1955 int len, oal_gatt_auth_req_t auth_req, char* data)
1957 int ret = OAL_STATUS_SUCCESS;
1958 char uuid_str1[2*BT_UUID_STRING_MAX];
1959 char uuid_str2[2*BT_UUID_STRING_MAX];
1960 OAL_CHECK_PARAMETER(srvc_id, return);
1961 OAL_CHECK_PARAMETER(char_id, return);
1962 OAL_CHECK_PARAMETER(data, return);
1963 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1964 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1965 API_TRACE("Client Write Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
1966 CHECK_OAL_GATT_ENABLED();
1967 CHECK_CLIENT_CONNECTION(conn_id);
1969 ret = gatt_api->client->write_characteristic(conn_id, (btgatt_srvc_id_t *)srvc_id,
1970 (btgatt_gatt_id_t *)char_id, write_type, len, auth_req, data);
1971 if (ret != BT_STATUS_SUCCESS) {
1972 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
1973 return convert_to_oal_status(ret);
1976 return OAL_STATUS_SUCCESS;
1979 oal_status_t gattc_write_descriptor(int conn_id, oal_gatt_srvc_id_t *srvc_id,
1980 oal_gatt_id_t *char_id, oal_gatt_id_t *desc_id,
1981 oal_gatt_write_type_t write_type, int len, oal_gatt_auth_req_t auth_req, char* data)
1983 int ret = OAL_STATUS_SUCCESS;
1984 char uuid_str1[2*BT_UUID_STRING_MAX];
1985 char uuid_str2[2*BT_UUID_STRING_MAX];
1986 char uuid_str3[2*BT_UUID_STRING_MAX];
1987 OAL_CHECK_PARAMETER(srvc_id, return);
1988 OAL_CHECK_PARAMETER(char_id, return);
1989 OAL_CHECK_PARAMETER(desc_id, return);
1990 OAL_CHECK_PARAMETER(data, return);
1991 uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1992 uuid_to_stringname(&(char_id->uuid), uuid_str2);
1993 uuid_to_stringname(&(desc_id->uuid), uuid_str3);
1994 API_TRACE("Client Write Descriptor: Service_uuid: [%s], Char_uuid: [%s], Desc_uuid: [%s]",
1995 uuid_str1, uuid_str2, uuid_str3);
1996 CHECK_OAL_GATT_ENABLED();
1997 CHECK_CLIENT_CONNECTION(conn_id);
1999 ret = gatt_api->client->write_descriptor(conn_id, (btgatt_srvc_id_t *)srvc_id,
2000 (btgatt_gatt_id_t *)char_id, (btgatt_gatt_id_t *)desc_id, write_type, len, auth_req, data);
2001 if (ret != BT_STATUS_SUCCESS) {
2002 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
2003 return convert_to_oal_status(ret);
2006 return OAL_STATUS_SUCCESS;
2009 oal_status_t gattc_conn_param_update(bt_address_t * address, int min, int max, int latency, int timeout)
2014 OAL_CHECK_PARAMETER(address, return);
2015 API_TRACE("Conn Param Update: [%s]", bdt_bd2str(address, &bdstr));
2016 CHECK_OAL_GATT_ENABLED();
2018 BT_INFO("[%s] min[%d] max[%d] latency[%d] timeout[%d]", bdt_bd2str(address, &bdstr), min, max, latency, timeout);
2019 ret = gatt_api->client->conn_parameter_update((bt_bdaddr_t *)address, min, max, latency, timeout);
2020 if (ret != BT_STATUS_SUCCESS) {
2021 BT_ERR("error: %s", status2string(ret));
2022 return convert_to_oal_status(ret);
2024 return OAL_STATUS_SUCCESS;