Skip to content

Commit

Permalink
bluetooth: Fix bug that BLE applications can not be repeated
Browse files Browse the repository at this point in the history
  • Loading branch information
SPRESENSE committed Mar 7, 2023
2 parents 01aef86 + f278bd8 commit 466ecc0
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 10 deletions.
9 changes: 3 additions & 6 deletions sdk/modules/bluetooth/bluetooth_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include <bluetooth/hal/bt_if.h>

#include "bluetooth_hal_init.h"
#include "bluetooth_le_gatt.h"

/****************************************************************************
* Pre-processor Definitions
Expand All @@ -55,12 +56,6 @@
#define CONFIG_BLUETOOTH_LE_NAME "SONY-BLE-CLASSIC"
#endif

/****************************************************************************
* Function prototypes
****************************************************************************/

extern void ble_gatt_init(struct ble_state_s *ble_state);

/****************************************************************************
* Private Types
****************************************************************************/
Expand Down Expand Up @@ -400,6 +395,8 @@ int bt_finalize(void)
ret = bt_hal_common_ops->finalize();
}

ble_gatt_finalize();

return ret;
}

Expand Down
13 changes: 13 additions & 0 deletions sdk/modules/bluetooth/bluetooth_le_gatt.c
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,19 @@ void ble_gatt_init(struct ble_state_s *ble_state)
g_ble_gatt_state.ble_state = ble_state;
}

/****************************************************************************
* Name: ble_gatt_finalize
*
* Description:
* Clear GATT information.
*
****************************************************************************/

void ble_gatt_finalize(void)
{
memset(&g_ble_gatt_state, 0, sizeof(g_ble_gatt_state));
}

/****************************************************************************
* Name: ble_create_service
*
Expand Down
65 changes: 65 additions & 0 deletions sdk/modules/bluetooth/bluetooth_le_gatt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/****************************************************************************
* modules/bluetooth/bluetooth_le_gatt.h
*
* Copyright 2023 Sony Semiconductor Solutions Corporation
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name of Sony Semiconductor Solutions Corporation nor
* the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/

#ifndef __MODULES_BLUETOOTH_BLUETOOTH_LE_GATT_H
#define __MODULES_BLUETOOTH_BLUETOOTH_LE_GATT_H

/****************************************************************************
* Public Function prototype
****************************************************************************/

/****************************************************************************
* Name: ble_gatt_init
*
* Description:
* Set BLE instance into GATT module.
*
****************************************************************************/

void ble_gatt_init(struct ble_state_s *ble_state);

/****************************************************************************
* Name: ble_gatt_finalize
*
* Description:
* Clear GATT information.
*
****************************************************************************/

void ble_gatt_finalize(void);


#endif /* __MODULES_BLUETOOTH_BLUETOOTH_LE_GATT_H */

75 changes: 71 additions & 4 deletions sdk/modules/bluetooth/hal/nrf52/ble_comm.c
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,36 @@ static
int bleStackFin(void)
{
int ret = 0;

switch (g_ble_context.conn_sts)
{
case BLE_CONN_STS_CONNECTED:
BLE_GapDisconnectLink(g_ble_context.ble_conn_handle);
g_ble_context.ble_conn_handle = CONN_HANDLE_INVALED;
break;

case BLE_CONN_STS_CONNECTING:
BLE_GapCancelConnecting();
break;

default:
break;
}

g_ble_context.conn_sts = BLE_CONN_STS_NOTCONNECTED;

if (g_ble_context.is_advertising)
{
BLE_GapStopAdv();
g_ble_context.is_advertising = false;
}

if (g_ble_context.is_scanning)
{
BLE_GapStopScan();
g_ble_context.is_scanning = false;
}

ret = nrf_sdh_disable_request();
return bleConvertErrorCode(ret);
}
Expand Down Expand Up @@ -804,12 +834,16 @@ static void ble_advertise(int mode)

static void on_connected(const BLE_EvtConnected* evt)
{
struct ble_event_conn_stat_t conn_stat_evt;

LOG_OUT("Connected: handle %d, role %d\n", evt->handle, evt->role);
g_ble_context.ble_srv_sds.conn_handle = evt->handle;
g_ble_context.ble_conn_handle = evt->handle;
g_ble_context.ble_role = evt->role;
g_ble_context.conn_sts = BLE_CONN_STS_CONNECTED;
g_ble_context.is_scanning = false;
g_ble_context.is_advertising = false;

struct ble_event_conn_stat_t conn_stat_evt;
conn_stat_evt.connected = true;
conn_stat_evt.handle = evt->handle;
memcpy(conn_stat_evt.addr.address, evt->addr.addr, BT_ADDR_LEN);
Expand All @@ -826,9 +860,11 @@ static void on_connected(const BLE_EvtConnected* evt)

static void on_disconnected(const BLE_EvtDisconnected* evt)
{
struct ble_event_conn_stat_t conn_stat_evt;

LOG_OUT("Disconnected: HCI status 0x%x\n", evt->reason);
g_ble_context.ble_conn_handle = CONN_HANDLE_INVALED;
struct ble_event_conn_stat_t conn_stat_evt;
g_ble_context.conn_sts = BLE_CONN_STS_NOTCONNECTED;

conn_stat_evt.connected = false;

Expand All @@ -846,6 +882,10 @@ static void on_disconnected(const BLE_EvtDisconnected* evt)
{
LOG_OUT("BLE_GapStartAdv failed, ret=%d\n", ret);
}
else
{
g_ble_context.is_advertising = true;
}
}
}

Expand Down Expand Up @@ -2721,6 +2761,8 @@ static int ble_stop(void)

static int nrf52_ble_start_scan(bool duplicate_filter)
{
int ret;

/* Return error if duplicate_filter is true,
* because nRF52 HW do not support duplicate scan result filter.
*/
Expand All @@ -2730,7 +2772,13 @@ static int nrf52_ble_start_scan(bool duplicate_filter)
return BT_FAIL;
}

return BLE_GapStartScan();
ret = BLE_GapStartScan();
if (ret == BLE_SUCCESS)
{
g_ble_context.is_scanning = true;
}

return ret;
}

/****************************************************************************
Expand All @@ -2743,7 +2791,14 @@ static int nrf52_ble_start_scan(bool duplicate_filter)

static int nrf52_ble_stop_scan(void)
{
return BLE_GapStopScan();
int ret;
ret = BLE_GapStopScan();
if (ret == BLE_SUCCESS)
{
g_ble_context.is_scanning = false;
}

return ret;
}

/****************************************************************************
Expand All @@ -2763,6 +2818,10 @@ static int nrf52_ble_connect(const BT_ADDR *addr)
memcpy(gap_addr.addr, addr->address, sizeof(gap_addr.addr));

ret = BLE_GapConnect(&gap_addr);
if (ret == BT_SUCCESS)
{
g_ble_context.conn_sts = BLE_CONN_STS_CONNECTING;
}

return ret;
}
Expand Down Expand Up @@ -2798,6 +2857,10 @@ static int nrf52_ble_advertise(bool enable)
{
LOG_OUT("ble enable advertise failed, ret=%d\n", ret);
}
else
{
g_ble_context.is_advertising = true;
}
}
else
{
Expand All @@ -2807,6 +2870,10 @@ static int nrf52_ble_advertise(bool enable)
{
LOG_OUT("ble disable advertise failed, ret=%d\n", ret);
}
else
{
g_ble_context.is_advertising = false;
}
}
return ret;
}
Expand Down
10 changes: 10 additions & 0 deletions sdk/modules/bluetooth/hal/nrf52/ble_comm_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,23 @@ typedef struct ble_srv_sds
BLE_GattsCharHandles char_ri_handle;
} BLE_SrvSds;

typedef enum ble_conn_sts
{
BLE_CONN_STS_NOTCONNECTED = 0,
BLE_CONN_STS_CONNECTING,
BLE_CONN_STS_CONNECTED,
} BLE_ConnSts;

typedef struct ble_context
{
BLE_EvtCtx ble_evt_ctx;
BLE_GapAddr ble_addr;
BLE_GapDeviceConfig ble_dev_cfg;
BLE_GapName ble_name;
BLE_GapConnHandle ble_conn_handle;
BLE_ConnSts conn_sts;
bool is_scanning;
bool is_advertising;
uint8_t ble_role;
BLE_SrvSds ble_srv_sds;
BLE_GapPairingFeature pairing_feature;
Expand Down

0 comments on commit 466ecc0

Please sign in to comment.