#include "oal-common.h"
#define DEV_APPEARANCE 0x0000
+#define DEFAULT_GATT_CLIENT_UUID "0000A00A-1111-1111-0123-456789ABCDEF"
#define NUM_SERVER_INST 10 // Maximum slots supported by stack is set to 10
#define CHECK_OAL_GATT_ENABLED() \
"DUAL_MODE",
};
+typedef enum {
+ OAL_REQ_NONE,
+ OAL_REQ_LE_SCAN,
+ OAL_REQ_LE_CONNECT,
+} oal_pending_gattc_req_e;
+
static const btgatt_interface_t * gatt_api;
static gboolean cur_adv_state[NUM_SERVER_INST];
static gatt_server_t gatt_servers[NUM_SERVER_INST];
+static int default_client_id = -1;
+static int pending_gattc_req = OAL_REQ_NONE;
+
/* Forward declarations of GATT Server callbacks */
static void cb_gatts_register_app(int status, int server_if, bt_uuid_t *uuid);
static void cb_gatts_multi_adv_enable(int server_if, int status);
NULL, /*cb_gatts_mtu_changed*/
};
+/* Forward declaration for GATT client callbacks */
+static void cb_gattc_register_app(int status, int clientIf, bt_uuid_t *app_uuid);
+static void cb_gattc_scan_result(bt_bdaddr_t* bdaddress, int rssi, uint8_t *adv_data);
+
/*TODO GATT CLient callbacks will be implemented in subsequent patches */
static const btgatt_client_callbacks_t btgatt_client_callbacks = {
- NULL, /*cb_gattc_register_app*/
- NULL, /*cb_gattc_scan_result*/
+ cb_gattc_register_app,
+ cb_gattc_scan_result,
NULL, /*cb_gattc_connection*/
NULL, /*cb_gattc_disconnect*/
NULL, /*cb_gattc_search_complete*/
}
gatt_api = (const btgatt_interface_t *)blued_api->get_profile_interface(BT_PROFILE_GATT_ID);
-
if (gatt_api == NULL) {
BT_ERR("GATT interface failed");
return OAL_STATUS_INTERNAL_ERROR;
BT_INFO("Current ATT MTU Size: %d", *mtu);
return OAL_STATUS_SUCCESS;
}
+
+/************************************ GATT Client ***********************************/
+/* Client Callbacks */
+static void cb_gattc_register_app(int status, int clientIf, bt_uuid_t *app_uuid)
+{
+ char uuid_str[BT_UUID_STRING_MAX];
+ event_gattc_register_t *event;
+
+ /* Check if GATT client registered for Default GATT client UUID */
+ uuid_to_string((service_uuid_t *)app_uuid, uuid_str);
+ if (!strncasecmp(DEFAULT_GATT_CLIENT_UUID,
+ uuid_str, strlen(DEFAULT_GATT_CLIENT_UUID))) {
+ default_client_id = clientIf;
+ switch (pending_gattc_req) {
+ case OAL_REQ_LE_SCAN:
+ if (0 != status) {
+ default_client_id = -1;
+ pending_gattc_req = OAL_REQ_NONE;
+ BT_ERR("Default client app creation failed");
+ send_event(OAL_EVENT_BLE_DISCOVERY_STOPPED, NULL, 0);
+ break;
+ }
+ gattc_start_le_discovery();
+ break;
+ default:
+ BT_ERR("Unknown pending request: %d", pending_gattc_req);
+ }
+
+ /* Return from here, no need to send GATTC_REGISTRATION event */
+ return;
+ }
+
+ BT_INFO("BTGATT CLIENT REGISTER APP CB, status:%d, clientIf:%d", status, clientIf);
+ event = g_new0(event_gattc_register_t, 1);
+ event->client_if = clientIf;
+ event->status = (status == 0 ? OAL_STATUS_SUCCESS : OAL_STATUS_INTERNAL_ERROR);
+ memcpy(event->client_uuid.uuid, app_uuid->uu, sizeof(oal_uuid_t));
+ send_event_no_trace(OAL_EVENT_GATTC_REGISTRATION, event, sizeof(event_gattc_register_t));
+}
+
+static void cb_gattc_scan_result(bt_bdaddr_t* bdaddress, int rssi, uint8_t *adv_data)
+{
+ event_ble_scan_result_info *event;
+
+ event = g_new0(event_ble_scan_result_info, 1);
+ event->rssi = rssi;
+ memcpy(event->address.addr, bdaddress->address, BT_ADDRESS_BYTES_NUM);
+ memcpy(event->adv_data, adv_data, BLE_ADV_DATA_LENGTH);
+ send_event_bda_trace(OAL_EVENT_BLE_REMOTE_DEVICE_FOUND, event,
+ sizeof(event_ble_scan_result_info), (bt_address_t *)bdaddress);
+}
+
+/* Register default gatt client to be used in LE scan etc. */
+static oal_status_t __register_default_gatt_client(void)
+{
+ bt_uuid_t uuid;
+ int ret;
+
+ API_TRACE("GATT client register");
+ CHECK_OAL_GATT_ENABLED();
+
+ if (default_client_id >= 0) {
+ BT_ERR("Client Already registered");
+ return OAL_STATUS_ALREADY_DONE;
+ }
+
+ string_to_uuid(DEFAULT_GATT_CLIENT_UUID, (service_uuid_t *)&uuid);
+ ret = gatt_api->client->register_client(&uuid);
+ if (ret != BT_STATUS_SUCCESS) {
+ BT_ERR("GATT client register failed: %s", status2string(ret));
+ return convert_to_oal_status(ret);
+ }
+
+ return OAL_STATUS_SUCCESS;
+}
+
+oal_status_t gattc_start_le_discovery(void)
+{
+ int ret;
+
+ API_TRACE("BTGATT CLIENT SCAN START");
+ CHECK_OAL_GATT_ENABLED();
+
+ if (default_client_id < 0) {
+ if (pending_gattc_req != OAL_REQ_NONE) {
+ BT_ERR("Another request: %d already in progress", pending_gattc_req);
+ return OAL_STATUS_INTERNAL_ERROR;
+ }
+
+ pending_gattc_req = OAL_REQ_LE_SCAN;
+ ret = __register_default_gatt_client();
+ if (OAL_STATUS_SUCCESS != ret) {
+ BT_ERR("__register_default_gatt_client failed");
+ pending_gattc_req = OAL_REQ_NONE;
+ return OAL_STATUS_INTERNAL_ERROR;
+ }
+
+ return OAL_STATUS_SUCCESS;
+ }
+
+ ret = gatt_api->client->scan(default_client_id, true);
+ if(ret != BT_STATUS_SUCCESS) {
+ BT_ERR("Error:Start LE Discovery failed: %s",status2string(ret));
+ return convert_to_oal_status(ret);
+ }
+
+ send_event(OAL_EVENT_BLE_DISCOVERY_STARTED, NULL, 0);
+ return OAL_STATUS_SUCCESS;
+}
+
+oal_status_t gattc_stop_le_discovery(void)
+{
+ int ret;
+
+ API_TRACE("Scan is stopped");
+ CHECK_OAL_GATT_ENABLED();
+
+ ret = gatt_api->client->scan(default_client_id, false);
+ if(ret != BT_STATUS_SUCCESS) {
+ BT_ERR("Error:Stop LE Discovery failed: %s",status2string(ret));
+ return convert_to_oal_status(ret);
+ }
+
+ send_event(OAL_EVENT_BLE_DISCOVERY_STOPPED, NULL, 0);
+ return OAL_STATUS_SUCCESS;
+}
+
+oal_status_t gattc_set_le_scan_param(int scan_type, int itv, int win)
+{
+ int ret;
+
+ API_TRACE("Scan is stopped");
+ CHECK_OAL_GATT_ENABLED();
+
+#ifdef TIZEN_BT_HAL
+ ret = gatt_api->client->set_scan_parameters(scan_type, itv, win);
+ if(ret != BT_STATUS_SUCCESS) {
+ BT_ERR("Error:Set scan parameters failed: %s",status2string(ret));
+ return convert_to_oal_status(ret);
+ }
+#else
+ ret = gatt_api->client->set_scan_parameters(itv, win);
+ if(ret != BT_STATUS_SUCCESS) {
+ BT_ERR("Error:Set scan parameters failed: %s",status2string(ret));
+ return convert_to_oal_status(ret);
+ }
+#endif
+
+ return OAL_STATUS_SUCCESS;
+}
+/************************************ GATT Client ***********************************/