Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 059700ed authored by Andre Eisenbach's avatar Andre Eisenbach Committed by Matthew Xie
Browse files

LE: Check encryption state before starting encryption

Previously, the btif layer kept track of when a link was encrypted using
an internal list to track encryptino callbacks. This change switches to
querying the stack for the actual encryption state, since the btif layer
may not receive all encryption related callbacks.

Bug: BLTH02382147
Change-Id: I426b87d77e9a18531c2d42733a7e736a73bb116f
parent b657ffeb
Loading
Loading
Loading
Loading
+0 −2
Original line number Diff line number Diff line
@@ -33,8 +33,6 @@ uint16_t set_read_value(btgatt_read_params_t *p_dest, tBTA_GATTC_READ *p_src);
uint16_t get_uuid16(tBT_UUID *p_uuid);

void btif_gatt_check_encrypted_link(BD_ADDR bd_addr);
void btif_gatt_remove_encrypted_link(BD_ADDR bd_addr);


#endif
+0 −3
Original line number Diff line number Diff line
@@ -437,9 +437,6 @@ static void btif_gattc_upstreams_evt(uint16_t event, char* p_param)
            bdcpy(bda.address, p_data->close.remote_bda);
            HAL_CBACK(bt_gatt_callbacks, client->close_cb, p_data->close.conn_id
                , p_data->status, p_data->close.client_if, &bda);

            if(p_data->status == BTA_GATT_OK)
                btif_gatt_remove_encrypted_link(p_data->close.remote_bda);
            break;
        }

+0 −2
Original line number Diff line number Diff line
@@ -206,8 +206,6 @@ static void btapp_gatts_handle_cback(uint16_t event, char* p_param)

            HAL_CBACK(bt_gatt_callbacks, server->connection_cb,
                      p_data->conn.conn_id, p_data->conn.server_if, FALSE, &bda);

            btif_gatt_remove_encrypted_link(p_data->conn.remote_bda);
            break;
        }

+3 −65
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@

#include "bta_api.h"
#include "bta_gatt_api.h"
#include "bta_jv_api.h"
#include "bd.h"
#include "btif_storage.h"

@@ -41,21 +42,11 @@
#define GATTC_READ_VALUE_TYPE_VALUE          0x0000  /* Attribute value itself */
#define GATTC_READ_VALUE_TYPE_AGG_FORMAT     0x2905  /* Characteristic Aggregate Format*/

#define BTIF_GATT_MAX_ENC_LINK_RECORDS       10

typedef struct
{
    BD_ADDR         bd_addr;
    BOOLEAN         in_use;
}__attribute__((packed)) btif_gatt_encrypted_link_t;

static char BASE_UUID[16] = {
    0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
    0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

static btif_gatt_encrypted_link_t encrypted_links[BTIF_GATT_MAX_ENC_LINK_RECORDS];

extern bt_status_t btif_dm_remove_bond(const bt_bdaddr_t *bd_addr);

int uuidType(unsigned char* p_uuid)
@@ -271,72 +262,19 @@ static void btif_gatt_set_encryption_cb (BD_ADDR bd_addr, tBTA_STATUS result);

static BOOLEAN btif_gatt_is_link_encrypted (BD_ADDR bd_addr)
{
    btif_gatt_encrypted_link_t *p_link = &encrypted_links[0];
    int i;

    if (bd_addr == NULL)
        return FALSE;

    for (i = 0; i != BTIF_GATT_MAX_ENC_LINK_RECORDS; ++i, ++p_link)
    {
        if (p_link->in_use && (!memcmp(p_link->bd_addr, bd_addr, BD_ADDR_LEN)))
            return TRUE;
    }
    return FALSE;
}

static BOOLEAN btif_gatt_add_encrypted_link (BD_ADDR bd_addr)
{
    btif_gatt_encrypted_link_t *p_link = &encrypted_links[0];
    int i;

    if (bd_addr == NULL)
        return FALSE;

    if (btif_gatt_is_link_encrypted(bd_addr))
        return TRUE;

    for (i = 0; i != BTIF_GATT_MAX_ENC_LINK_RECORDS; ++i, ++p_link)
    {
        if (!p_link->in_use)
        {
            p_link->in_use = TRUE;
            memcpy( p_link->bd_addr, bd_addr, sizeof(BD_ADDR) );
            return  TRUE;
        }
    }

    return FALSE;
}

void btif_gatt_remove_encrypted_link (BD_ADDR bd_addr)
{
    btif_gatt_encrypted_link_t *p_link = &encrypted_links[0];
    int i;

    if (bd_addr == NULL)
        return;

    for (i = 0; i != BTIF_GATT_MAX_ENC_LINK_RECORDS; ++i, ++p_link)
    {
        if (p_link->in_use && (!memcmp (p_link->bd_addr, bd_addr, BD_ADDR_LEN)))
        {
            p_link->in_use = FALSE;
            break;
        }
    }
    return BTA_JvIsEncrypted(bd_addr);
}

static void btif_gatt_set_encryption_cb (BD_ADDR bd_addr, tBTA_STATUS result)
{
    if (result == BTA_SUCCESS)
    if (result != BTA_SUCCESS)
    {
        btif_gatt_add_encrypted_link(bd_addr);
    } else {
        bt_bdaddr_t bda;
        bdcpy(bda.address, bd_addr);

        btif_gatt_remove_encrypted_link(bd_addr);
        btif_dm_remove_bond(&bda);
    }
}