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

[app] Move CASESessionManager to server internal storage #12778

Merged
merged 4 commits into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 1 addition & 11 deletions examples/ota-requestor-app/linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
* limitations under the License.
*/

#include <app/CASEClientPool.h>
#include <app/OperationalDeviceProxyPool.h>
#include <app/server/Server.h>
#include <controller/ExampleOperationalCredentialsIssuer.h>
#include <credentials/examples/DeviceAttestationCredsExample.h>
Expand All @@ -31,7 +29,6 @@

using chip::BDXDownloader;
using chip::ByteSpan;
using chip::CASEClientPool;
using chip::CharSpan;
using chip::EndpointId;
using chip::FabricIndex;
Expand All @@ -40,7 +37,6 @@ using chip::LinuxOTAImageProcessor;
using chip::NodeId;
using chip::OnDeviceConnected;
using chip::OnDeviceConnectionFailure;
using chip::OperationalDeviceProxyPool;
using chip::OTADownloader;
using chip::OTAImageProcessorParams;
using chip::OTARequestor;
Expand All @@ -55,15 +51,10 @@ using namespace chip::ArgParser;
using namespace chip::Messaging;
using namespace chip::app::Clusters::OtaSoftwareUpdateProvider::Commands;

constexpr size_t kMaxActiveCaseClients = 2;
constexpr size_t kMaxActiveDevices = 8;

OTARequestor gRequestorCore;
LinuxOTARequestorDriver gRequestorUser;
BDXDownloader gDownloader;
LinuxOTAImageProcessor gImageProcessor;
CASEClientPool<kMaxActiveCaseClients> gCASEClientPool;
OperationalDeviceProxyPool<kMaxActiveDevices> gDevicePool;
gjc13 marked this conversation as resolved.
Show resolved Hide resolved

bool HandleOptions(const char * aProgram, OptionSet * aOptions, int aIdentifier, const char * aName, const char * aValue);
void OnStartDelayTimerHandler(Layer * systemLayer, void * appState);
Expand Down Expand Up @@ -195,6 +186,7 @@ int main(int argc, char * argv[])

// Init Data Model and CHIP App Server with user specified UDP port
Server::GetInstance().Init(nullptr, requestorSecurePort);
chip::Dnssd::Resolver::Instance().Init(chip::DeviceLayer::UDPEndPointManager());
ChipLogProgress(SoftwareUpdate, "Initializing the Application Server. Listening on UDP port %d", requestorSecurePort);

// Initialize device attestation config
Expand All @@ -205,8 +197,6 @@ int main(int argc, char * argv[])

// Set server instance used for session establishment
chip::Server * server = &(chip::Server::GetInstance());
server->SetCASEClientPool(&gCASEClientPool);
server->SetDevicePool(&gDevicePool);
gRequestorCore.SetServerInstance(server);

// Connect the Requestor and Requestor Driver objects
Expand Down
1 change: 1 addition & 0 deletions src/app/CASESessionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

#include <app/CASESessionManager.h>
#include <platform/CHIPDeviceLayer.h>

namespace chip {

Expand Down
8 changes: 6 additions & 2 deletions src/app/CASESessionManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,22 @@ class CASESessionManager : public SessionReleaseDelegate, public Dnssd::Resolver
public:
CASESessionManager() = delete;

CASESessionManager(CASESessionManagerConfig & params)
CASESessionManager(const CASESessionManagerConfig & params)
{
VerifyOrDie(params.sessionInitParams.Validate() == CHIP_NO_ERROR);

mConfig = params;
}

CHIP_ERROR Init()
{
if (mConfig.dnsResolver == nullptr)
{
VerifyOrDie(mDNSResolver.Init(DeviceLayer::UDPEndPointManager()) == CHIP_NO_ERROR);
ReturnErrorOnFailure(mDNSResolver.Init(DeviceLayer::UDPEndPointManager()));
mDNSResolver.SetResolverDelegate(this);
mConfig.dnsResolver = &mDNSResolver;
}
return CHIP_NO_ERROR;
}

virtual ~CASESessionManager() { mDNSResolver.Shutdown(); }
Expand Down
2 changes: 1 addition & 1 deletion src/app/OperationalDeviceProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ struct DeviceProxyInitParams

Optional<ReliableMessageProtocolConfig> mrpLocalConfig = Optional<ReliableMessageProtocolConfig>::Missing();

CHIP_ERROR Validate()
CHIP_ERROR Validate() const
{
ReturnErrorCodeIf(sessionManager == nullptr, CHIP_ERROR_INCORRECT_STATE);
ReturnErrorCodeIf(exchangeMgr == nullptr, CHIP_ERROR_INCORRECT_STATE);
Expand Down
47 changes: 2 additions & 45 deletions src/app/clusters/ota-requestor/OTARequestor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,61 +226,18 @@ EmberAfStatus OTARequestor::HandleAnnounceOTAProvider(app::CommandHandler * comm
return EMBER_ZCL_STATUS_SUCCESS;
}

CHIP_ERROR OTARequestor::SetupCASESessionManager()
{
// A previous CASE session had been established
if (mCASESessionManager != nullptr)
{
return CHIP_NO_ERROR;
}

// CSM has not been setup so create a new instance of it
if (mCASESessionManager == nullptr)
{
DeviceProxyInitParams initParams = {
.sessionManager = &(mServer->GetSecureSessionManager()),
.exchangeMgr = &(mServer->GetExchangeManager()),
.idAllocator = &(mServer->GetSessionIDAllocator()),
.fabricTable = &(mServer->GetFabricTable()),
.clientPool = mServer->GetCASEClientPool(),
// TODO: Determine where this should be instantiated
.imDelegate = Platform::New<Controller::DeviceControllerInteractionModelDelegate>(),
};

CASESessionManagerConfig sessionManagerConfig = {
.sessionInitParams = initParams,
.dnsCache = nullptr,
.devicePool = mServer->GetDevicePool(),
.dnsResolver = nullptr,
};

mCASESessionManager = Platform::New<CASESessionManager>(sessionManagerConfig);
}

if (mCASESessionManager == nullptr)
{
ChipLogError(SoftwareUpdate, "Failed in creating an instance of CASESessionManager");
return CHIP_ERROR_NO_MEMORY;
}

return CHIP_NO_ERROR;
}

void OTARequestor::ConnectToProvider(OnConnectedAction onConnectedAction)
{
CHIP_ERROR err = SetupCASESessionManager();
FabricInfo * fabricInfo = mServer->GetFabricTable().FindFabricWithIndex(mProviderFabricIndex);
VerifyOrReturn(err == CHIP_NO_ERROR,
ChipLogError(SoftwareUpdate, "Cannot setup CASESessionManager: %" CHIP_ERROR_FORMAT, err.Format()));
VerifyOrReturn(fabricInfo != nullptr, ChipLogError(SoftwareUpdate, "Cannot find fabric"));

// Set the action to take once connection is successfully established
mOnConnectedAction = onConnectedAction;

ChipLogDetail(SoftwareUpdate, "Establishing session to provider node ID 0x" ChipLogFormatX64 " on fabric index %d",
ChipLogValueX64(mProviderNodeId), mProviderFabricIndex);
err = mCASESessionManager->FindOrEstablishSession(fabricInfo, mProviderNodeId, &mOnConnectedCallback,
&mOnConnectionFailureCallback);
CHIP_ERROR err = mCASESessionManager->FindOrEstablishSession(fabricInfo, mProviderNodeId, &mOnConnectedCallback,
&mOnConnectionFailureCallback);
VerifyOrReturn(err == CHIP_NO_ERROR,
ChipLogError(SoftwareUpdate, "Cannot establish connection to provider: %" CHIP_ERROR_FORMAT, err.Format()));
}
Expand Down
11 changes: 5 additions & 6 deletions src/app/clusters/ota-requestor/OTARequestor.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@ class OTARequestor : public OTARequestorInterface
* Called to set the server instance which used to get access to the system resources necessary to open CASE sessions and drive
* BDX transfers
*/
void SetServerInstance(Server * server) { mServer = server; }
void SetServerInstance(Server * server)
{
mServer = server;
mCASESessionManager = server->GetCASESessionManager();
}

/**
* Called to establish a session to mProviderNodeId on mProviderFabricIndex. This must be called from the same externally
Expand Down Expand Up @@ -197,11 +201,6 @@ class OTARequestor : public OTARequestorInterface
chip::BDXDownloader * mDownloader;
};

/**
* Setup CASESessionManager used to establish a session with the provider
*/
CHIP_ERROR SetupCASESessionManager();

/**
* Create a QueryImage request using values from the Basic cluster attributes
*/
Expand Down
18 changes: 18 additions & 0 deletions src/app/server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,22 @@ static ::chip::PersistedCounter sDebugEventIdCounter;
static ::chip::app::CircularEventBuffer sLoggingBuffer[CHIP_NUM_EVENT_LOGGING_BUFFERS];
#endif // CHIP_CONFIG_ENABLE_SERVER_IM_EVENT

Server::Server() :
mCASESessionManager(CASESessionManagerConfig {
.sessionInitParams = {
.sessionManager = &mSessions,
.exchangeMgr = &mExchangeMgr,
.idAllocator = &mSessionIDAllocator,
.fabricTable = &mFabrics,
.clientPool = &mCASEClientPool,
.imDelegate = nullptr,
},
.dnsCache = nullptr,
.devicePool = &mDevicePool,
.dnsResolver = nullptr,
}), mCommissioningWindowManager(this), mGroupsProvider(mGroupsStorage)
{}

CHIP_ERROR Server::Init(AppDelegate * delegate, uint16_t secureServicePort, uint16_t unsecureServicePort)
{
mSecuredServicePort = secureServicePort;
Expand Down Expand Up @@ -215,6 +231,8 @@ CHIP_ERROR Server::Init(AppDelegate * delegate, uint16_t secureServicePort, uint
&mSessions, &mFabrics, &mSessionIDAllocator);
SuccessOrExit(err);

err = mCASESessionManager.Init();

exit:
if (err != CHIP_NO_ERROR)
{
Expand Down
19 changes: 9 additions & 10 deletions src/app/server/Server.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
#pragma once

#include <app/CASEClientPool.h>
#include <app/CASESessionManager.h>
#include <app/OperationalDeviceProxyPool.h>
#include <app/server/AppDelegate.h>
#include <app/server/CommissioningWindowManager.h>
#include <credentials/FabricTable.h>
#include <credentials/GroupDataProviderImpl.h>
#include <inet/InetConfig.h>
#include <lib/core/CHIPConfig.h>
#include <lib/support/TestPersistentStorageDelegate.h>
#include <messaging/ExchangeMgr.h>
#include <platform/KeyValueStoreManager.h>
Expand Down Expand Up @@ -67,13 +69,7 @@ class Server

FabricTable & GetFabricTable() { return mFabrics; }

CASEClientPoolDelegate * GetCASEClientPool() { return mCASEClientPool; }

void SetCASEClientPool(CASEClientPoolDelegate * clientPool) { mCASEClientPool = clientPool; }

OperationalDeviceProxyPoolDelegate * GetDevicePool() { return mDevicePool; }

void SetDevicePool(OperationalDeviceProxyPoolDelegate * devicePool) { mDevicePool = devicePool; }
CASESessionManager * GetCASESessionManager() { return &mCASESessionManager; }

Messaging::ExchangeManager & GetExchangeManager() { return mExchangeMgr; }

Expand All @@ -94,7 +90,7 @@ class Server
static Server & GetInstance() { return sServer; }

private:
Server() : mCommissioningWindowManager(this), mGroupsProvider(mGroupsStorage) {}
Server();

static Server sServer;

Expand Down Expand Up @@ -141,8 +137,11 @@ class Server
ServerTransportMgr mTransports;
SessionManager mSessions;
CASEServer mCASEServer;
CASEClientPoolDelegate * mCASEClientPool = nullptr;
OperationalDeviceProxyPoolDelegate * mDevicePool = nullptr;

CASESessionManager mCASESessionManager;
CASEClientPool<CHIP_CONFIG_DEVICE_MAX_ACTIVE_CASE_CLIENTS> mCASEClientPool;
OperationalDeviceProxyPool<CHIP_CONFIG_DEVICE_MAX_ACTIVE_DEVICES> mDevicePool;

Messaging::ExchangeManager mExchangeMgr;
FabricTable mFabrics;
SessionIDAllocator mSessionIDAllocator;
Expand Down
18 changes: 18 additions & 0 deletions src/lib/core/CHIPConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -2570,6 +2570,24 @@ extern const char CHIP_NON_PRODUCTION_MARKER[];
#define CHIP_CONFIG_CONTROLLER_MAX_ACTIVE_CASE_CLIENTS 16
#endif

/**
* @def CHIP_CONFIG_DEVICE_MAX_ACTIVE_CASE_CLIENTS
*
* @brief Number of outgoing CASE sessions can be simutaneously negotiated on an end device.
*/
#ifndef CHIP_CONFIG_DEVICE_MAX_ACTIVE_CASE_CLIENTS
#define CHIP_CONFIG_DEVICE_MAX_ACTIVE_CASE_CLIENTS 2
#endif

/**
* @def CHIP_CONFIG_DEVICE_MAX_ACTIVE_DEVICES
*
* @brief Number of devices an end device can be simultaneously connected to
*/
#ifndef CHIP_CONFIG_DEVICE_MAX_ACTIVE_DEVICES
#define CHIP_CONFIG_DEVICE_MAX_ACTIVE_DEVICES 4
andy31415 marked this conversation as resolved.
Show resolved Hide resolved
#endif

/**
* @def CHIP_CONFIG_MAX_GROUPS_PER_FABRIC
*
Expand Down