Skip to content

Commit

Permalink
Move UDPEndPointManager out of Globals.cpp
Browse files Browse the repository at this point in the history
Currently there is no way for platforms to provide storage for
UDPEndPointManager or to provide an alternate instance, as there is a
prescribed static instance in Globals.cpp.

Move this instance to ConnectivityManager, which platforms provide the
implementation for.

Note to porters: You can inherit from
GenericConnectivityManagerImpl_UDP<ConnectivityManagerImpl> if previous
behavior was working for you.
  • Loading branch information
mspang committed Jun 21, 2022
1 parent 98a1c6b commit f89b36e
Show file tree
Hide file tree
Showing 26 changed files with 312 additions and 25 deletions.
12 changes: 9 additions & 3 deletions src/controller/CHIPDeviceControllerFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ CHIP_ERROR DeviceControllerFactory::InitSystemState()
if (mSystemState != nullptr)
{
params.systemLayer = mSystemState->SystemLayer();
params.tcpEndPointManager = mSystemState->TCPEndPointManager();
params.udpEndPointManager = mSystemState->UDPEndPointManager();
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
params.tcpEndPointManager = mSystemState->TCPEndPointManager();
#endif
#if CONFIG_NETWORK_LAYER_BLE
params.bleLayer = mSystemState->BleLayer();
#endif
Expand Down Expand Up @@ -109,8 +111,10 @@ CHIP_ERROR DeviceControllerFactory::InitSystemState(FactoryInitParams params)
ReturnErrorOnFailure(DeviceLayer::PlatformMgr().InitChipStack());

stateParams.systemLayer = &DeviceLayer::SystemLayer();
stateParams.tcpEndPointManager = DeviceLayer::TCPEndPointManager();
stateParams.udpEndPointManager = DeviceLayer::UDPEndPointManager();
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
stateParams.tcpEndPointManager = DeviceLayer::TCPEndPointManager();
#endif
#else
stateParams.systemLayer = params.systemLayer;
stateParams.tcpEndPointManager = params.tcpEndPointManager;
Expand Down Expand Up @@ -408,8 +412,10 @@ CHIP_ERROR DeviceControllerSystemState::Shutdown()
}

mSystemLayer = nullptr;
mTCPEndPointManager = nullptr;
mUDPEndPointManager = nullptr;
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
mTCPEndPointManager = nullptr;
#endif
#if CONFIG_NETWORK_LAYER_BLE
mBleLayer = nullptr;
#endif // CONFIG_NETWORK_LAYER_BLE
Expand Down
14 changes: 12 additions & 2 deletions src/include/platform/CHIPDeviceLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,24 @@ namespace DeviceLayer {
void SetSystemLayerForTesting(System::LayerImpl * layer);

// These functions are defined in src/platform/Globals.cpp
chip::Inet::EndPointManager<Inet::UDPEndPoint> * UDPEndPointManager();
chip::Inet::EndPointManager<Inet::TCPEndPoint> * TCPEndPointManager();
chip::System::Layer & SystemLayer();

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
chip::System::LayerSockets & SystemLayerSockets();
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS

inline chip::Inet::EndPointManager<Inet::UDPEndPoint> * UDPEndPointManager()
{
return &ConnectivityMgr().UDPEndPointManager();
}

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
inline chip::Inet::EndPointManager<Inet::TCPEndPoint> * TCPEndPointManager()
{
return &ConnectivityMgr().TCPEndPointManager();
}
#endif

} // namespace DeviceLayer
} // namespace chip

Expand Down
23 changes: 23 additions & 0 deletions src/include/platform/ConnectivityManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <memory>

#include <app/AttributeAccessInterface.h>
#include <inet/UDPEndPoint.h>
#include <lib/support/CodeUtils.h>
#include <platform/CHIPDeviceBuildConfig.h>
#include <platform/CHIPDeviceConfig.h>
Expand All @@ -33,6 +34,10 @@
#include <app-common/zap-generated/cluster-objects.h>
#include <app/util/basic-types.h>

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
#include <inet/TCPEndPoint.h>
#endif

namespace chip {

namespace Ble {
Expand Down Expand Up @@ -158,6 +163,12 @@ class ConnectivityManager
void SetDelegate(ConnectivityManagerDelegate * delegate) { mDelegate = delegate; }
ConnectivityManagerDelegate * GetDelegate() const { return mDelegate; }

chip::Inet::EndPointManager<Inet::UDPEndPoint> & UDPEndPointManager();

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
chip::Inet::EndPointManager<Inet::TCPEndPoint> & TCPEndPointManager();
#endif

// WiFi station methods
WiFiStationMode GetWiFiStationMode();
CHIP_ERROR SetWiFiStationMode(WiFiStationMode val);
Expand Down Expand Up @@ -323,6 +334,18 @@ extern ConnectivityManagerImpl & ConnectivityMgrImpl();
namespace chip {
namespace DeviceLayer {

inline chip::Inet::EndPointManager<Inet::UDPEndPoint> & ConnectivityManager::UDPEndPointManager()
{
return static_cast<ImplClass *>(this)->_UDPEndPointManager();
}

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
inline chip::Inet::EndPointManager<Inet::TCPEndPoint> & ConnectivityManager::TCPEndPointManager()
{
return static_cast<ImplClass *>(this)->_TCPEndPointManager();
}
#endif

inline ConnectivityManager::WiFiStationMode ConnectivityManager::GetWiFiStationMode()
{
return static_cast<ImplClass *>(this)->_GetWiFiStationMode();
Expand Down
54 changes: 54 additions & 0 deletions src/include/platform/internal/GenericConnectivityManagerImpl_TCP.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
*
* Copyright (c) 2022 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.
*/

/**
* @file
* Provides a generic implementation of ConnectivityManager features
* for use on platforms that use TCP.
*/

#pragma once

#include <inet/TCPEndPointImpl.h>

namespace chip {
namespace DeviceLayer {
namespace Internal {

template <class ImplClass>
class GenericConnectivityManagerImpl_TCP
{
public:
// ConnectivityManager:
chip::Inet::EndPointManager<Inet::TCPEndPoint> & _TCPEndPointManager();

private:
static chip::Inet::TCPEndPointManagerImpl sTCPEndPointManagerImpl;
};

template <class ImplClass>
chip::Inet::TCPEndPointManagerImpl GenericConnectivityManagerImpl_TCP<ImplClass>::sTCPEndPointManagerImpl;

template <class ImplClass>
inline chip::Inet::EndPointManager<Inet::TCPEndPoint> & GenericConnectivityManagerImpl_TCP<ImplClass>::_TCPEndPointManager()
{
return sTCPEndPointManagerImpl;
}

} // namespace Internal
} // namespace DeviceLayer
} // namespace chip
54 changes: 54 additions & 0 deletions src/include/platform/internal/GenericConnectivityManagerImpl_UDP.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
*
* Copyright (c) 2022 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.
*/

/**
* @file
* Provides a generic implementation of ConnectivityManager features
* for use on platforms that use UDP.
*/

#pragma once

#include <inet/UDPEndPointImpl.h>

namespace chip {
namespace DeviceLayer {
namespace Internal {

template <class ImplClass>
class GenericConnectivityManagerImpl_UDP
{
public:
// ConnectivityManager:
chip::Inet::EndPointManager<Inet::UDPEndPoint> & _UDPEndPointManager();

private:
static chip::Inet::UDPEndPointManagerImpl sUDPEndPointManagerImpl;
};

template <class ImplClass>
chip::Inet::UDPEndPointManagerImpl GenericConnectivityManagerImpl_UDP<ImplClass>::sUDPEndPointManagerImpl;

template <class ImplClass>
inline chip::Inet::EndPointManager<Inet::UDPEndPoint> & GenericConnectivityManagerImpl_UDP<ImplClass>::_UDPEndPointManager()
{
return sUDPEndPointManagerImpl;
}

} // namespace Internal
} // namespace DeviceLayer
} // namespace chip
9 changes: 9 additions & 0 deletions src/platform/Ameba/ConnectivityManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@

#include <platform/ConnectivityManager.h>
#include <platform/internal/GenericConnectivityManagerImpl.h>
#include <platform/internal/GenericConnectivityManagerImpl_UDP.h>

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
#include <platform/internal/GenericConnectivityManagerImpl_TCP.h>
#endif

#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
#include <platform/internal/GenericConnectivityManagerImpl_WiFi.h>
Expand Down Expand Up @@ -55,6 +60,10 @@ class PlatformManagerImpl;

class ConnectivityManagerImpl final : public ConnectivityManager,
public Internal::GenericConnectivityManagerImpl<ConnectivityManagerImpl>,
public Internal::GenericConnectivityManagerImpl_UDP<ConnectivityManagerImpl>,
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
public Internal::GenericConnectivityManagerImpl_TCP<ConnectivityManagerImpl>,
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
public Internal::GenericConnectivityManagerImpl_WiFi<ConnectivityManagerImpl>,
#else
Expand Down
2 changes: 2 additions & 0 deletions src/platform/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,10 @@ if (chip_device_platform != "none") {
"../include/platform/internal/GenericConnectivityManagerImpl_NoBLE.h",
"../include/platform/internal/GenericConnectivityManagerImpl_NoThread.h",
"../include/platform/internal/GenericConnectivityManagerImpl_NoWiFi.h",
"../include/platform/internal/GenericConnectivityManagerImpl_TCP.h",
"../include/platform/internal/GenericConnectivityManagerImpl_Thread.h",
"../include/platform/internal/GenericConnectivityManagerImpl_Thread.ipp",
"../include/platform/internal/GenericConnectivityManagerImpl_UDP.h",
"../include/platform/internal/GenericConnectivityManagerImpl_WiFi.h",
"../include/platform/internal/GenericConnectivityManagerImpl_WiFi.ipp",
"../include/platform/internal/GenericDeviceInstanceInfoProvider.h",
Expand Down
9 changes: 9 additions & 0 deletions src/platform/CYW30739/ConnectivityManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@

#include <platform/ConnectivityManager.h>
#include <platform/internal/GenericConnectivityManagerImpl.h>
#include <platform/internal/GenericConnectivityManagerImpl_UDP.h>
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
#include <platform/internal/GenericConnectivityManagerImpl_TCP.h>
#endif

#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
#include <platform/internal/GenericConnectivityManagerImpl_BLE.h>
#else
Expand All @@ -40,6 +45,10 @@ namespace DeviceLayer {
*/
class ConnectivityManagerImpl final : public ConnectivityManager,
public Internal::GenericConnectivityManagerImpl<ConnectivityManagerImpl>,
public Internal::GenericConnectivityManagerImpl_UDP<ConnectivityManagerImpl>,
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
public Internal::GenericConnectivityManagerImpl_TCP<ConnectivityManagerImpl>,
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
public Internal::GenericConnectivityManagerImpl_BLE<ConnectivityManagerImpl>,
#else
Expand Down
9 changes: 9 additions & 0 deletions src/platform/Darwin/ConnectivityManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@

#include <platform/ConnectivityManager.h>
#include <platform/internal/GenericConnectivityManagerImpl.h>
#include <platform/internal/GenericConnectivityManagerImpl_UDP.h>
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
#include <platform/internal/GenericConnectivityManagerImpl_TCP.h>
#endif

#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
#include <platform/internal/GenericConnectivityManagerImpl_BLE.h>
#else
Expand Down Expand Up @@ -56,6 +61,10 @@ class ConnectivityManagerImpl final : public ConnectivityManager,
public Internal::GenericConnectivityManagerImpl_NoThread<ConnectivityManagerImpl>,
#endif
public Internal::GenericConnectivityManagerImpl_NoWiFi<ConnectivityManagerImpl>,
public Internal::GenericConnectivityManagerImpl_UDP<ConnectivityManagerImpl>,
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
public Internal::GenericConnectivityManagerImpl_TCP<ConnectivityManagerImpl>,
#endif
public Internal::GenericConnectivityManagerImpl<ConnectivityManagerImpl>
{
// Allow the ConnectivityManager interface class to delegate method calls to
Expand Down
9 changes: 9 additions & 0 deletions src/platform/EFR32/ConnectivityManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@

#include <platform/ConnectivityManager.h>
#include <platform/internal/GenericConnectivityManagerImpl.h>
#include <platform/internal/GenericConnectivityManagerImpl_UDP.h>
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
#include <platform/internal/GenericConnectivityManagerImpl_TCP.h>
#endif

#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
#include <platform/internal/GenericConnectivityManagerImpl_BLE.h>
#else
Expand Down Expand Up @@ -50,6 +55,10 @@ class PlatformManagerImpl;
*/
class ConnectivityManagerImpl final : public ConnectivityManager,
public Internal::GenericConnectivityManagerImpl<ConnectivityManagerImpl>,
public Internal::GenericConnectivityManagerImpl_UDP<ConnectivityManagerImpl>,
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
public Internal::GenericConnectivityManagerImpl_TCP<ConnectivityManagerImpl>,
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
public Internal::GenericConnectivityManagerImpl_BLE<ConnectivityManagerImpl>,
#else
Expand Down
9 changes: 9 additions & 0 deletions src/platform/ESP32/ConnectivityManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@

#include <platform/ConnectivityManager.h>
#include <platform/internal/GenericConnectivityManagerImpl.h>
#include <platform/internal/GenericConnectivityManagerImpl_UDP.h>

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
#include <platform/internal/GenericConnectivityManagerImpl_TCP.h>
#endif

#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
#include <platform/internal/GenericConnectivityManagerImpl_WiFi.h>
Expand Down Expand Up @@ -62,6 +67,10 @@ class PlatformManagerImpl;
*/
class ConnectivityManagerImpl final : public ConnectivityManager,
public Internal::GenericConnectivityManagerImpl<ConnectivityManagerImpl>,
public Internal::GenericConnectivityManagerImpl_UDP<ConnectivityManagerImpl>,
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
public Internal::GenericConnectivityManagerImpl_TCP<ConnectivityManagerImpl>,
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
public Internal::GenericConnectivityManagerImpl_WiFi<ConnectivityManagerImpl>,
#else
Expand Down
20 changes: 0 additions & 20 deletions src/platform/Globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,11 @@
/* this file behaves like a config.h, comes first */
#include <platform/internal/CHIPDeviceLayerInternal.h>

#include <inet/TCPEndPointImpl.h>
#include <inet/UDPEndPointImpl.h>
#include <system/SystemLayerImpl.h>

namespace chip {
namespace DeviceLayer {

chip::Inet::EndPointManager<Inet::UDPEndPoint> * UDPEndPointManager()
{
static chip::Inet::UDPEndPointManagerImpl gUDPEndPointManager;
return &gUDPEndPointManager;
}

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
chip::Inet::EndPointManager<Inet::TCPEndPoint> * TCPEndPointManager()
{
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
static chip::Inet::TCPEndPointManagerImpl gTCPEndPointManager;
return &gTCPEndPointManager;
#else // INET_CONFIG_ENABLE_TCP_ENDPOINT
return nullptr;
#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT
}
#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT

chip::System::LayerImpl * gMockedSystemLayer = nullptr;

void SetSystemLayerForTesting(System::LayerImpl * layer)
Expand Down
Loading

0 comments on commit f89b36e

Please sign in to comment.