Skip to content

Commit

Permalink
[Telink] Fixes to enable and test BLE
Browse files Browse the repository at this point in the history
  • Loading branch information
s07641069 committed May 31, 2024
1 parent d107a4a commit b128a4b
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 21 deletions.
6 changes: 3 additions & 3 deletions config/telink/chip-module/Kconfig.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,9 @@ config CHIP_WIFI
select NET_IPV6_ND # enable Neighbor Discovery to handle Router Advertisements
select NET_IPV6_NBR_CACHE
select NET_STATISTICS_USER_API
select NET_IPV4 # TODO: remove IPv4 when IPv6 will be ready (see CHIP_IPV4)
select NET_CONFIG_NEED_IPV4
select NET_DHCPV4
# select NET_IPV4 # TODO: remove IPv4 when IPv6 will be ready (see CHIP_IPV4)
# select NET_CONFIG_NEED_IPV4
# select NET_DHCPV4

if CHIP_WIFI

Expand Down
1 change: 1 addition & 0 deletions examples/platform/telink/common/include/AppTaskCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class AppTaskCommon
{
kButtonId_ExampleAction = 1,
kButtonId_FactoryReset,
kButtonId_StartWiFi,
kButtonId_StartThread,
kButtonId_StartBleAdv
} ButtonId;
Expand Down
13 changes: 7 additions & 6 deletions examples/platform/telink/common/src/AppTaskCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
#include "LEDManager.h"
#include "PWMManager.h"

#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
#include "ThreadUtil.h"

#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
#elif CHIP_DEVICE_CONFIG_ENABLE_WIFI
#include <platform/telink/wifi/TelinkWiFiDriver.h>
#endif

Expand Down Expand Up @@ -78,7 +78,7 @@ uint8_t sFactoryResetCntr = 0;
bool sIsCommissioningFailed = false;
bool sIsNetworkProvisioned = false;
bool sIsNetworkEnabled = false;
bool sIsThreadAttached = false;
bool sIsNetworkAttached = false;
bool sHaveBLEConnections = false;

#if APP_SET_DEVICE_INFO_PROVIDER
Expand Down Expand Up @@ -460,7 +460,7 @@ void AppTaskCommon::UpdateStatusLED()
{
if (sIsNetworkProvisioned && sIsNetworkEnabled)
{
if (sIsThreadAttached)
if (sIsNetworkAttached)
{
LedManager::getInstance().setLed(LedManager::EAppLed_Status, 950, 50);
}
Expand Down Expand Up @@ -524,8 +524,8 @@ void AppTaskCommon::StartBleAdvHandler(AppEvent * aEvent)
{
LOG_INF("StartBleAdvHandler");

// Don't allow on starting Matter service BLE advertising after Thread provisioning.
if (ConnectivityMgr().IsThreadProvisioned())
// Don't allow on starting Matter service BLE advertising after device provisioning.
if (ConnectivityMgr().IsThreadProvisioned() || ConnectivityMgr().IsWiFiStationProvisioned())
{
LOG_INF("Device already commissioned");
return;
Expand Down Expand Up @@ -728,6 +728,7 @@ void AppTaskCommon::ChipEventHandler(const ChipDeviceEvent * event, intptr_t /*
case DeviceEventType::kWiFiConnectivityChange:
sIsNetworkProvisioned = ConnectivityMgr().IsWiFiStationProvisioned();
sIsNetworkEnabled = ConnectivityMgr().IsWiFiStationEnabled();
sIsNetworkAttached = ConnectivityMgr().IsWiFiStationConnected();
#if CONFIG_CHIP_OTA_REQUESTOR
if (event->WiFiConnectivityChange.Result == kConnectivity_Established)
{
Expand Down
3 changes: 2 additions & 1 deletion examples/platform/telink/common/src/mainCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ int main(void)
#elif CHIP_DEVICE_CONFIG_ENABLE_WIFI
sWiFiCommissioningInstance.Init();
#else
return CHIP_ERROR_INTERNAL;
err = CHIP_ERROR_INTERNAL;
goto exit;
#endif /* CHIP_DEVICE_CONFIG_ENABLE_THREAD */

err = GetAppTask().StartApp();
Expand Down
40 changes: 29 additions & 11 deletions src/platform/telink/BLEManagerImpl.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
*
* Copyright (c) 2020-2022 Project CHIP Authors
* Copyright (c) 2020-2024 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -45,9 +45,16 @@
#include <zephyr/sys/byteorder.h>
#include <zephyr/sys/util.h>

#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
#include <platform/telink/wifi/TelinkWiFiDriver.h>
#endif

// TODO: need common mac_init solution for B9X & W91
#ifndef CONFIG_BOARD_TLSR9118BDK40D
extern "C" {
#include <b9x_bt_flash.h>
}
#endif

#if defined(CONFIG_PM) && !defined(CONFIG_CHIP_ENABLE_PM_DURING_BLE)
#include <zephyr/pm/policy.h>
Expand Down Expand Up @@ -123,7 +130,10 @@ CHIP_ERROR InitBLEMACAddress()
int error = 0;
bt_addr_le_t addr;

// TODO: need common mac_init solution for B9X & W91
#ifndef CONFIG_BOARD_TLSR9118BDK40D
b9x_bt_blc_mac_init(addr.a.val);
#endif

if (BT_ADDR_IS_STATIC(&addr.a)) // in case of Random static address, create a new id
{
Expand Down Expand Up @@ -290,16 +300,19 @@ inline CHIP_ERROR BLEManagerImpl::PrepareAdvertisingRequest(void)

CHIP_ERROR BLEManagerImpl::StartAdvertising(void)
{
if (ConnectivityMgr().IsThreadProvisioned())
if (ConnectivityMgr().IsThreadProvisioned() || ConnectivityMgr().IsWiFiStationProvisioned())
{
ChipLogProgress(DeviceLayer, "Thread provisioned, can't StartAdvertising");
ChipLogProgress(DeviceLayer, "Device provisioned, can't StartAdvertising");

return CHIP_ERROR_INCORRECT_STATE;
}
// TODO: check if WiFi scanning required for Amazon ecosystem
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
else if (!mBLERadioInitialized)
{
ThreadStackMgrImpl().StartThreadScan(mInternalScanCallback);
}
#endif
else
{
return StartAdvertisingProcess();
Expand All @@ -314,11 +327,13 @@ CHIP_ERROR BLEManagerImpl::StartAdvertisingProcess(void)

if (!mBLERadioInitialized)
{
/* Switch off Thread */
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
// Deinit Thread
ThreadStackMgrImpl().SetThreadEnabled(false);
ThreadStackMgrImpl().SetRadioBlocked(true);
#endif

/* Init BLE stack */
// Init BLE
err = bt_enable(NULL);
VerifyOrReturnError(err == 0, MapErrorZephyr(err));

Expand Down Expand Up @@ -385,9 +400,9 @@ CHIP_ERROR BLEManagerImpl::StartAdvertisingProcess(void)

CHIP_ERROR BLEManagerImpl::StopAdvertising(void)
{
if (ConnectivityMgr().IsThreadProvisioned())
if (ConnectivityMgr().IsThreadProvisioned() || ConnectivityMgr().IsWiFiStationProvisioned())
{
ChipLogProgress(DeviceLayer, "Thread provisioned, StopAdvertising done");
ChipLogProgress(DeviceLayer, "Device provisioned, StopAdvertising done");

return CHIP_ERROR_INCORRECT_STATE;
}
Expand Down Expand Up @@ -948,31 +963,34 @@ CHIP_ERROR BLEManagerImpl::HandleThreadStateChange(const ChipDeviceEvent * event

CHIP_ERROR BLEManagerImpl::HandleBleConnectionClosed(const ChipDeviceEvent * event)
{
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
if (ThreadStackMgrImpl().IsReadyToAttach())
{
SwitchToIeee802154();
}
#endif

return CHIP_NO_ERROR;
}

/* @todo: move to RadioSwitch module */
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
void BLEManagerImpl::SwitchToIeee802154(void)
{
ChipLogProgress(DeviceLayer, "SwitchToIeee802154");
ChipLogProgress(DeviceLayer, "Switch context from BLE to Thread");

/* Deinit BLE stack */
// Deinit BLE
bt_disable();
mBLERadioInitialized = false;

#if defined(CONFIG_PM) && !defined(CONFIG_CHIP_ENABLE_PM_DURING_BLE)
pm_policy_state_lock_put(PM_STATE_SUSPEND_TO_IDLE, PM_ALL_SUBSTATES);
#endif

/* Init IEEE802154 */
// Init Thread
ThreadStackMgrImpl().SetRadioBlocked(false);
ThreadStackMgrImpl().SetThreadEnabled(true);
}
#endif

} // namespace Internal
} // namespace DeviceLayer
Expand Down

0 comments on commit b128a4b

Please sign in to comment.