Skip to content

Commit

Permalink
[Ameba] Support NetworkCommissioning (#13576)
Browse files Browse the repository at this point in the history
* Support NetworkCommissioning

* Check for index during ReorderNetwork

* Use pvPortMalloc and vPortFree in right positions

* Fix BLE commissioning issue due to CASESession fail

* Remove unused ServiceProvisioning files

* Fix restyled errors

* Update SystemTimeSupport
  • Loading branch information
pankore authored and pull[bot] committed Jan 4, 2024
1 parent f9e8704 commit e07a195
Show file tree
Hide file tree
Showing 11 changed files with 482 additions and 83 deletions.
13 changes: 13 additions & 0 deletions examples/all-clusters-app/ameba/main/chipinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@
#include <lwip_netconf.h>

#include <app/clusters/identify-server/identify-server.h>
#include <app/clusters/network-commissioning/network-commissioning.h>
#include <app/server/OnboardingCodesUtil.h>
#include <app/server/Server.h>
#include <credentials/DeviceAttestationCredsProvider.h>
#include <credentials/examples/DeviceAttestationCredsExample.h>
#include <lib/support/ErrorStr.h>
#include <platform/Ameba/AmebaConfig.h>
#include <platform/Ameba/NetworkCommissioningDriver.h>
#include <platform/CHIPDeviceLayer.h>
#include <setup_payload/ManualSetupPayloadGenerator.h>
#include <setup_payload/QRCodeSetupPayloadGenerator.h>
Expand All @@ -53,6 +55,16 @@ using namespace ::chip::DeviceManager;
using namespace ::chip::DeviceLayer;
using namespace ::chip::System;

namespace {
app::Clusters::NetworkCommissioning::Instance
sWiFiNetworkCommissioningInstance(0 /* Endpoint Id */, &(NetworkCommissioning::AmebaWiFiDriver::GetInstance()));
} // namespace

void NetWorkCommissioningInstInit()
{
sWiFiNetworkCommissioningInstance.Init();
}

Identify gIdentify0 = {
chip::EndpointId{ 0 },
[](Identify *) { ChipLogProgress(Zcl, "onIdentifyStart"); },
Expand Down Expand Up @@ -268,6 +280,7 @@ extern "C" void ChipTest(void)

// Initialize device attestation config
SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider());
NetWorkCommissioningInstInit();

if (RTW_SUCCESS != wifi_is_connected_to_ap())
{
Expand Down
4 changes: 2 additions & 2 deletions src/platform/Ameba/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ static_library("Ameba") {
"KeyValueStoreManagerImpl.h",
"Logging.cpp",
"LwIPCoreLock.cpp",
"NetworkCommissioningDriver.h",
"NetworkCommissioningWiFiDriver.cpp",
"PlatformManagerImpl.cpp",
"PlatformManagerImpl.h",
"ServiceProvisioning.cpp",
"ServiceProvisioning.h",
"SoftwareUpdateManagerImpl.h",
"SystemTimeSupport.cpp",
"SystemTimeSupport.h",
Expand Down
1 change: 1 addition & 0 deletions src/platform/Ameba/CHIPDevicePlatformEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ enum InternalPlatformSpecificEventTypes
kCHIPoBLERXCharWriteEvent,
kCHIPoBLETXCharWriteEvent,
kRtkWiFiStationConnectedEvent,
kRtkWiFiScanCompletedEvent,
};

} // namespace DeviceEventType
Expand Down
27 changes: 26 additions & 1 deletion src/platform/Ameba/ConnectivityManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <platform/internal/GenericConnectivityManagerImpl_WiFi.cpp>
#endif

#include <platform/Ameba/NetworkCommissioningDriver.h>
#include <platform/internal/BLEManager.h>
#include <support/CodeUtils.h>
#include <support/logging/CHIPLogging.h>
Expand Down Expand Up @@ -69,6 +70,9 @@ CHIP_ERROR ConnectivityManagerImpl::_Init()
mWiFiAPIdleTimeout = System::Clock::Milliseconds32(CHIP_DEVICE_CONFIG_WIFI_AP_IDLE_TIMEOUT);
mFlags.SetRaw(0);

// Set callback functions from chip_porting
chip_connmgr_set_callback_func((chip_connmgr_callback)(conn_callback_dispatcher), this);

// Ensure that station mode is enabled.
wifi_on(RTW_MODE_STA);

Expand Down Expand Up @@ -136,14 +140,19 @@ void ConnectivityManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event)
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
if (event->Type == DeviceEventType::kRtkWiFiStationConnectedEvent)
{
ChipLogProgress(DeviceLayer, "WIFI_EVENT_STA_CONNECTED");
ChipLogProgress(DeviceLayer, "WiFiStationConnected");
if (mWiFiStationState == kWiFiStationState_Connecting)
{
ChangeWiFiStationState(kWiFiStationState_Connecting_Succeeded);
}
DriveStationState();
DHCPProcess();
}
if (event->Type == DeviceEventType::kRtkWiFiScanCompletedEvent)
{
ChipLogProgress(DeviceLayer, "WiFiScanCompleted");
NetworkCommissioning::AmebaWiFiDriver::GetInstance().OnScanWiFiNetworkDone();
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI
}

Expand Down Expand Up @@ -557,6 +566,7 @@ void ConnectivityManagerImpl::DriveStationState()

void ConnectivityManagerImpl::OnStationConnected()
{
NetworkCommissioning::AmebaWiFiDriver::GetInstance().OnConnectWiFiNetwork();
// Alert other components of the new state.
ChipDeviceEvent event;
event.Type = DeviceEventType::kWiFiConnectivityChange;
Expand Down Expand Up @@ -776,6 +786,14 @@ void ConnectivityManagerImpl::RtkWiFiStationConnectedHandler(char * buf, int buf
PlatformMgr().PostEventOrDie(&event);
}

void ConnectivityManagerImpl::RtkWiFiScanCompletedHandler(void)
{
ChipDeviceEvent event;
memset(&event, 0, sizeof(event));
event.Type = DeviceEventType::kRtkWiFiScanCompletedEvent;
PlatformMgr().PostEventOrDie(&event);
}

void ConnectivityManagerImpl::DHCPProcessThread(void * param)
{
LwIP_DHCP(0, DHCP_START);
Expand All @@ -798,6 +816,13 @@ void ConnectivityManagerImpl::DHCPProcess(void)
xTaskCreate(DHCPProcessThread, "DHCPProcess", 4096 / sizeof(StackType_t), this, 1, NULL);
}

int ConnectivityManagerImpl::conn_callback_dispatcher(void * object)
{
ConnectivityManagerImpl * connmgr = static_cast<ConnectivityManagerImpl *>(object);
connmgr->RtkWiFiScanCompletedHandler();
return 0;
}

#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI

} // namespace DeviceLayer
Expand Down
2 changes: 2 additions & 0 deletions src/platform/Ameba/ConnectivityManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,10 @@ class ConnectivityManagerImpl final : public ConnectivityManager,
static ConnectivityManagerImpl sInstance;
static void RefreshMessageLayer(void);
static void RtkWiFiStationConnectedHandler(char * buf, int buf_len, int flags, void * userdata);
static void RtkWiFiScanCompletedHandler(void);
void DHCPProcess(void);
static void DHCPProcessThread(void * param);
static int conn_callback_dispatcher(void * object);
};

#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
Expand Down
4 changes: 2 additions & 2 deletions src/platform/Ameba/DeviceNetworkProvisioningDelegateImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include <support/logging/CHIPLogging.h>

#include "DeviceNetworkProvisioningDelegateImpl.h"
#include "ServiceProvisioning.h"
#include "NetworkCommissioningDriver.h"

namespace chip {
namespace DeviceLayer {
Expand All @@ -30,7 +30,7 @@ CHIP_ERROR DeviceNetworkProvisioningDelegateImpl::_ProvisionWiFiNetwork(const ch
CHIP_ERROR err = CHIP_NO_ERROR;

ChipLogProgress(NetworkProvisioning, "AmebaNetworkProvisioningDelegate: SSID: %s", ssid);
err = SetWiFiStationProvisioning(ssid, key);
err = NetworkCommissioning::AmebaWiFiDriver::GetInstance().ConnectWiFiNetwork(ssid, strlen(ssid), key, strlen(key));
if (err != CHIP_NO_ERROR)
{
ChipLogError(NetworkProvisioning, "Failed to connect to WiFi network: %s", chip::ErrorStr(err));
Expand Down
133 changes: 133 additions & 0 deletions src/platform/Ameba/NetworkCommissioningDriver.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
*
* Copyright (c) 2021 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once
#include "chip_porting.h"
#include <platform/NetworkCommissioning.h>
#include <wifi_structures.h>

namespace chip {
namespace DeviceLayer {
namespace NetworkCommissioning {

namespace {
constexpr uint8_t kMaxWiFiNetworks = 1;
constexpr uint8_t kWiFiScanNetworksTimeOutSeconds = 10;
constexpr uint8_t kWiFiConnectNetworkTimeoutSeconds = 20;
} // namespace

class AmebaScanResponseIterator : public Iterator<WiFiScanResponse>
{
public:
AmebaScanResponseIterator(const size_t size, const rtw_scan_result_t * scanResults) : mSize(size), mpScanResults(scanResults) {}
size_t Count() override { return mSize; }
bool Next(WiFiScanResponse & item) override
{
if (mIternum >= mSize)
{
return false;
}

// copy the available information into WiFiScanResponse struct, which will be copied to the result to be sent
item.security = mpScanResults[mIternum].security;
item.ssidLen = mpScanResults[mIternum].SSID.len;
item.channel = mpScanResults[mIternum].channel;
item.wiFiBand = chip::DeviceLayer::NetworkCommissioning::WiFiBand::k2g4;
// item.rssi = mpScanResults[mIternum].rssi;
memcpy(item.ssid, mpScanResults[mIternum].SSID.val, item.ssidLen);
memcpy(item.bssid, mpScanResults[mIternum].BSSID.octet, 6);

mIternum++;
return true;
}
void Release() override {}

private:
const size_t mSize; // no of network scanned
const rtw_scan_result_t * mpScanResults; // list of scanned network info of size mSize
size_t mIternum = 0; // to iterate through mpScanResults of size mSize
};

class AmebaWiFiDriver final : public WiFiDriver
{
public:
class WiFiNetworkIterator final : public NetworkIterator
{
public:
WiFiNetworkIterator(AmebaWiFiDriver * aDriver) : mDriver(aDriver) {}
size_t Count() override;
bool Next(Network & item) override;
void Release() override { delete this; }
~WiFiNetworkIterator() = default;

private:
AmebaWiFiDriver * mDriver;
bool mExhausted = false;
};

struct WiFiNetwork
{
char ssid[DeviceLayer::Internal::kMaxWiFiSSIDLength];
uint8_t ssidLen = 0;
char credentials[DeviceLayer::Internal::kMaxWiFiKeyLength];
uint8_t credentialsLen = 0;
};

// BaseDriver
NetworkIterator * GetNetworks() override { return new WiFiNetworkIterator(this); }
CHIP_ERROR Init() override;
CHIP_ERROR Shutdown() override;

// WirelessDriver
uint8_t GetMaxNetworks() override { return kMaxWiFiNetworks; }
uint8_t GetScanNetworkTimeoutSeconds() override { return kWiFiScanNetworksTimeOutSeconds; }
uint8_t GetConnectNetworkTimeoutSeconds() override { return kWiFiConnectNetworkTimeoutSeconds; }

CHIP_ERROR CommitConfiguration() override;
CHIP_ERROR RevertConfiguration() override;

Status RemoveNetwork(ByteSpan networkId) override;
Status ReorderNetwork(ByteSpan networkId, uint8_t index) override;
void ConnectNetwork(ByteSpan networkId, ConnectCallback * callback) override;

// WiFiDriver
Status AddOrUpdateNetwork(ByteSpan ssid, ByteSpan credentials) override;
void ScanNetworks(ByteSpan ssid, ScanCallback * callback) override;

CHIP_ERROR ConnectWiFiNetwork(const char * ssid, uint8_t ssidLen, const char * key, uint8_t keyLen);
void OnConnectWiFiNetwork();
void OnScanWiFiNetworkDone();
static AmebaWiFiDriver & GetInstance()
{
static AmebaWiFiDriver instance;
return instance;
}

private:
bool NetworkMatch(const WiFiNetwork & network, ByteSpan networkId);
CHIP_ERROR StartScanWiFiNetworks(ByteSpan ssid);

WiFiNetworkIterator mWiFiIterator = WiFiNetworkIterator(this);
WiFiNetwork mSavedNetwork;
WiFiNetwork mStagingNetwork;
ScanCallback * mpScanCallback;
ConnectCallback * mpConnectCallback;
};

} // namespace NetworkCommissioning
} // namespace DeviceLayer
} // namespace chip
Loading

0 comments on commit e07a195

Please sign in to comment.