Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API to set advertising scheme on disconnected #2837

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion hal/src/nRF52840/ble_hal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4154,7 +4154,7 @@ int hal_ble_gap_set_auto_advertise(hal_ble_auto_adv_cfg_t config, void* reserved
int hal_ble_gap_get_auto_advertise(hal_ble_auto_adv_cfg_t* cfg, void* reserved) {
BleLock lk;
LOG_DEBUG(TRACE, "hal_ble_gap_get_auto_advertise().");
CHECK_TRUE(BleObject::getInstance().initialized(), BLE_AUTO_ADV_FORBIDDEN);
CHECK_TRUE(BleObject::getInstance().initialized(), SYSTEM_ERROR_INVALID_STATE);
return BleObject::getInstance().broadcaster()->getAutoAdvertiseScheme(cfg);
}

Expand Down
41 changes: 39 additions & 2 deletions hal/src/rtl872x/ble_hal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,8 @@ class BleGap {
int startAdvertising(bool wait = true); // TODO: always wait, now we have command thread to execute asynchronously
int stopAdvertising(bool wait = true); // TODO: always wait, now we have command thread to execute asynchronously
int notifyAdvStop();
int setAutoAdvertiseScheme(hal_ble_auto_adv_cfg_t config);
int getAutoAdvertiseScheme(hal_ble_auto_adv_cfg_t* cfg);

bool isAdvertising() const {
return isAdvertising_;
Expand Down Expand Up @@ -506,6 +508,7 @@ class BleGap {
addr_{},
advParams_{},
advTimeoutTimer_(nullptr),
autoAdvCfg_(BLE_AUTO_ADV_ALWAYS),
isScanning_(false),
notifyScanResult_(false),
scanParams_{},
Expand Down Expand Up @@ -640,6 +643,7 @@ class BleGap {
hal_ble_addr_t addr_;
hal_ble_adv_params_t advParams_;
os_timer_t advTimeoutTimer_; /**< Timer for advertising timeout. */
volatile hal_ble_auto_adv_cfg_t autoAdvCfg_; /**< Automatic advertising configuration. */
volatile bool isScanning_; /**< If it is scanning or not. */
volatile bool notifyScanResult_;
hal_ble_scan_params_t scanParams_; /**< BLE scanning parameters. */
Expand Down Expand Up @@ -1621,6 +1625,28 @@ int BleGap::notifyAdvStop() {
return SYSTEM_ERROR_NONE;
}

int BleGap::setAutoAdvertiseScheme(hal_ble_auto_adv_cfg_t config) {
autoAdvCfg_ = config;
bool connectedAsPeripheral = false;
std::lock_guard<RecursiveMutex> lk(connectionsMutex_);
for (auto& connection : connections_) {
if (connection.info.role == BLE_ROLE_PERIPHERAL) {
connectedAsPeripheral = true;
break;
}
}
if (!connectedAsPeripheral && autoAdvCfg_ == BLE_AUTO_ADV_SINCE_NEXT_CONN) {
// If it is not connected as Peripheral currently.
autoAdvCfg_ = BLE_AUTO_ADV_ALWAYS;
}
return SYSTEM_ERROR_NONE;
}

int BleGap::getAutoAdvertiseScheme(hal_ble_auto_adv_cfg_t* cfg) {
*cfg = autoAdvCfg_;
return SYSTEM_ERROR_NONE;
}

void BleGap::onAdvTimeoutTimerExpired(os_timer_t timer) {
BleGap* gap;
os_timer_get_id(timer, (void**)&gap);
Expand Down Expand Up @@ -2342,6 +2368,15 @@ void BleGap::handleConnectionStateChanged(uint8_t connHandle, T_GAP_CONN_STATE n
// FIXME: check whether it's enabled?
if (isAdvertising()) {
enqueue(BLE_CMD_STOP_ADV);
enqueue(BLE_CMD_START_ADV);
return;
}
if (autoAdvCfg_ == BLE_AUTO_ADV_FORBIDDEN) {
return;
}
if (autoAdvCfg_ == BLE_AUTO_ADV_SINCE_NEXT_CONN) {
autoAdvCfg_ = BLE_AUTO_ADV_ALWAYS;
return;
}
enqueue(BLE_CMD_START_ADV);
}
Expand Down Expand Up @@ -3738,14 +3773,16 @@ int hal_ble_gap_start_advertising(void* reserved) {
int hal_ble_gap_set_auto_advertise(hal_ble_auto_adv_cfg_t config, void* reserved) {
BleLock lk;
LOG_DEBUG(TRACE, "hal_ble_gap_set_auto_advertise().");
CHECK_TRUE(BleGap::getInstance().initialized(), SYSTEM_ERROR_INVALID_STATE);
CHECK_FALSE(BleGap::getInstance().lockMode(), SYSTEM_ERROR_INVALID_STATE);
return SYSTEM_ERROR_NONE;
return BleGap::getInstance().setAutoAdvertiseScheme(config);
}

int hal_ble_gap_get_auto_advertise(hal_ble_auto_adv_cfg_t* cfg, void* reserved) {
BleLock lk;
LOG_DEBUG(TRACE, "hal_ble_gap_get_auto_advertise().");
return SYSTEM_ERROR_NONE;
CHECK_TRUE(BleGap::getInstance().initialized(), SYSTEM_ERROR_INVALID_STATE);
return BleGap::getInstance().getAutoAdvertiseScheme(cfg);
}

int hal_ble_gap_stop_advertising(void* reserved) {
Expand Down
11 changes: 11 additions & 0 deletions user/tests/wiring/api/ble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,14 @@ test(ble_pairing_event_type) {
(void)type;
}

test(ble_advertising_scheme) {
BleAdvertisingScheme scheme;
API_COMPILE({ scheme = BleAdvertisingScheme::STOP_ADV_ON_DISCONNECTED; });
API_COMPILE({ scheme = BleAdvertisingScheme::STOP_ADV_ON_DISCONNECTED_ONCE; });
API_COMPILE({ scheme = BleAdvertisingScheme::RESTART_ADV_ON_DISCONNECTED; });
(void)scheme;
}

test(ble_phys_type) {
BlePhy phy;
API_COMPILE({ phy = BlePhy::BLE_PHYS_AUTO; });
Expand Down Expand Up @@ -680,6 +688,7 @@ test(ble_local_device_class) {
BleUuid charUuid;
BleUuid svcUuid;
BlePeerDevice peer;
BleAdvertisingScheme scheme = BleAdvertisingScheme::RESTART_ADV_ON_DISCONNECTED;

API_COMPILE({ int ret = BleLocalDevice::getInstance().on(); (void)ret; });

Expand Down Expand Up @@ -726,6 +735,8 @@ test(ble_local_device_class) {
API_COMPILE({ size_t ret = BLE.getAdvertisingData(advData); (void)ret; });
API_COMPILE({ size_t ret = BLE.getScanResponseData(&srData); (void)ret; });
API_COMPILE({ size_t ret = BLE.getScanResponseData(srData); (void)ret; });
API_COMPILE({ size_t ret = BLE.setAdvertisingScheme(scheme); (void)ret; });
API_COMPILE({ size_t ret = BLE.getAdvertisingScheme(&scheme); (void)ret; });

API_COMPILE({ int ret = BLE.advertise(); (void)ret; });
API_COMPILE({ int ret = BLE.advertise(&advData); (void)ret; });
Expand Down
9 changes: 9 additions & 0 deletions wiring/inc/spark_wiring_ble.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,12 @@ enum class BlePairingEventType : uint8_t {
NUMERIC_COMPARISON = BLE_EVT_PAIRING_NUMERIC_COMPARISON
};

enum class BleAdvertisingScheme : uint8_t {
STOP_ADV_ON_DISCONNECTED = BLE_AUTO_ADV_FORBIDDEN,
STOP_ADV_ON_DISCONNECTED_ONCE = BLE_AUTO_ADV_SINCE_NEXT_CONN,
RESTART_ADV_ON_DISCONNECTED = BLE_AUTO_ADV_ALWAYS,
};

struct BlePairingStatus {
int status;
bool bonded;
Expand Down Expand Up @@ -1010,6 +1016,9 @@ class BleLocalDevice {
ssize_t getScanResponseData(BleAdvertisingData* scanResponse) const;
ssize_t getScanResponseData(BleAdvertisingData& scanResponse) const;

int setAdvertisingScheme(BleAdvertisingScheme scheme) const;
int getAdvertisingScheme(BleAdvertisingScheme* scheme) const;
XuGuohui marked this conversation as resolved.
Show resolved Hide resolved

// Advertising control
int advertise() const;
int advertise(BleAdvertisingData* advertisingData, BleAdvertisingData* scanResponse = nullptr) const;
Expand Down
11 changes: 11 additions & 0 deletions wiring/src/spark_wiring_ble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2255,6 +2255,17 @@ ssize_t BleLocalDevice::getScanResponseData(BleAdvertisingData& scanResponse) co
return getScanResponseData(&scanResponse);
}

int BleLocalDevice::setAdvertisingScheme(BleAdvertisingScheme scheme) const {
return hal_ble_gap_set_auto_advertise((hal_ble_auto_adv_cfg_t)scheme, nullptr);
}

int BleLocalDevice::getAdvertisingScheme(BleAdvertisingScheme* scheme) const {
hal_ble_auto_adv_cfg_t config;
int ret = hal_ble_gap_get_auto_advertise(&config, nullptr);
*scheme = (BleAdvertisingScheme)config;
return ret;
}

int BleLocalDevice::advertise() const {
return hal_ble_gap_start_advertising(nullptr);
}
Expand Down