[Adapt: OAL] Added GATT client APIs for LE Scan 41/123241/1
authorAtul Rai <a.rai@samsung.com>
Thu, 30 Mar 2017 11:54:46 +0000 (17:24 +0530)
committerAtul Rai <a.rai@samsung.com>
Thu, 30 Mar 2017 11:54:46 +0000 (17:24 +0530)
This patch implements GATT client APIs and events in OAL to enable
Bluetooth LE scan.

Change-Id: I4bba54eac30ebdc6eac2f93c85f2975b251f647b
Signed-off-by: Atul Rai <a.rai@samsung.com>
bt-oal/include/oal-event.h
bt-oal/include/oal-gatt.h
bt-oal/oal-gatt.c

index e86a1d0..4719798 100755 (executable)
@@ -123,6 +123,10 @@ extern "C" {
         EVENT(OAL_EVENT_BLE_MULTI_ADVERTISING_DISABLE)          /* NULL*/\
         EVENT(OAL_EVENT_BLE_MULTI_ADVERTISING_SET_INST_DATA)            /* NULL*/\
         EVENT(OAL_EVENT_BLE_MULTI_ADVERTISING_UPDATE)           /* NULL*/\
+       EVENT(OAL_EVENT_GATTC_REGISTRATION)                     /* gattc Registration Confirmed */\
+       EVENT(OAL_EVENT_BLE_DISCOVERY_STARTED)                          /* NULL */\
+       EVENT(OAL_EVENT_BLE_DISCOVERY_STOPPED)                          /* NULL */\
+       EVENT(OAL_EVENT_BLE_REMOTE_DEVICE_FOUND)                /* event_ble_scan_result_info */\
        EVENT(OAL_EVENT_END)                                /* End of event*/\
 
 
@@ -254,6 +258,19 @@ typedef struct {
         int status;
 } event_ble_multiadv_status;
 
+/* BLE: Scan Result Callback data */
+typedef struct {
+       int rssi;
+       bt_address_t address;
+       uint8_t adv_data[BLE_ADV_DATA_LENGTH];
+} event_ble_scan_result_info;
+
+typedef struct {
+       oal_status_t status;
+       int client_if;
+       oal_uuid_t client_uuid;
+} event_gattc_register_t;
+
 #ifdef __cplusplus
 }
 #endif /* __cplusplus */
index 8e27cff..3235dc4 100644 (file)
@@ -205,4 +205,43 @@ oal_status_t gatts_multi_adv_set_inst_data(int instance_id,
  */
 oal_status_t gatts_get_att_mtu(int conn_id, int *mtu);
 
+/**
+ * @brief Start LE Device discovery
+ *
+ * @remarks  BLE discovery will be started
+ *
+ * @return OAL_STATUS_SUCCESS on success, otherwise a non-zero error value.
+ * @retval #OAL_STATUS_SUCCESS  Successful
+ *
+ * @pre OAL API should be enabled with gatt_enable().
+ *
+ * @see  gatt_enable()
+ */
+oal_status_t gattc_start_le_discovery(void);
+
+/**
+ * @brief Stop LE Device discovery
+ *
+ * @remarks  BLE discovery will be stopped
+ *
+ * @return OAL_STATUS_SUCCESS on success, otherwise a non-zero error value.
+ * @retval #OAL_STATUS_SUCCESS  Successful
+ *
+ * @pre BLE Discovery should be started using the gattc_start_le_discovery().
+ *
+ * @see  gattc_start_le_discovery()
+ */
+oal_status_t gattc_stop_le_discovery(void);
+
+/**
+ * @brief Set scan parameters
+ *
+ * @remarks  scan parameters will be set
+ *
+ * @return OAL_STATUS_SUCCESS on success, otherwise a non-zero error value.
+ * @retval #OAL_STATUS_SUCCESS  Successful
+ *
+ */
+oal_status_t gattc_set_le_scan_param(int scan_type, int itv, int win);
+
 #endif /* OAL_GATT_H_ */
index 3e0c047..980d904 100644 (file)
@@ -28,6 +28,7 @@
 #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() \
@@ -99,10 +100,19 @@ const char *oal_device_type[]={
        "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);
@@ -138,10 +148,14 @@ static const btgatt_server_callbacks_t btgatt_server_callbacks = {
        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*/
@@ -205,7 +219,6 @@ oal_status_t gatt_enable(void)
        }
 
        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;
@@ -709,3 +722,154 @@ oal_status_t gatts_get_att_mtu(int conn_id, int *mtu)
        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 ***********************************/