/* * * Copyright (c) 2020-2021 Project CHIP Authors * Copyright (c) 2018 Nest Labs, Inc. * All rights reserved. * * 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 #include #include #include #include #if INET_CONFIG_ENABLE_TCP_ENDPOINT #include #endif #if CHIP_DEVICE_CONFIG_ENABLE_WIFI #include #else #include #endif #if CHIP_DEVICE_CONFIG_ENABLE_THREAD #include #else #include #endif #if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE #include #else #include #endif #include #include "esp_event.h" #include "esp_netif_types.h" namespace chip { namespace Inet { class IPAddress; } // namespace Inet namespace DeviceLayer { class PlatformManagerImpl; /** * Concrete implementation of the ConnectivityManager singleton object for the ESP32 platform. */ class ConnectivityManagerImpl final : public ConnectivityManager, public Internal::GenericConnectivityManagerImpl, public Internal::GenericConnectivityManagerImpl_UDP, #if INET_CONFIG_ENABLE_TCP_ENDPOINT public Internal::GenericConnectivityManagerImpl_TCP, #endif #if CHIP_DEVICE_CONFIG_ENABLE_WIFI public Internal::GenericConnectivityManagerImpl_WiFi, #else public Internal::GenericConnectivityManagerImpl_NoWiFi, #endif #if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE public Internal::GenericConnectivityManagerImpl_BLE, #else public Internal::GenericConnectivityManagerImpl_NoBLE, #endif #if CHIP_DEVICE_CONFIG_ENABLE_THREAD public Internal::GenericConnectivityManagerImpl_Thread #else public Internal::GenericConnectivityManagerImpl_NoThread #endif { // Allow the ConnectivityManager interface class to delegate method calls to // the implementation methods provided by this class. friend class ConnectivityManager; private: CHIP_ERROR _Init(void); void _OnPlatformEvent(const ChipDeviceEvent * event); #if CHIP_DEVICE_CONFIG_ENABLE_ETHERNET CHIP_ERROR InitEthernet(void); void OnEthernetPlatformEvent(const ChipDeviceEvent * event); #endif #if CHIP_DEVICE_CONFIG_ENABLE_WIFI using Flags = GenericConnectivityManagerImpl_WiFi::ConnectivityFlags; // ===== Members that implement the ConnectivityManager abstract interface. WiFiStationMode _GetWiFiStationMode(void); CHIP_ERROR _SetWiFiStationMode(WiFiStationMode val); bool _IsWiFiStationEnabled(void); bool _IsWiFiStationApplicationControlled(void); bool _IsWiFiStationConnected(void); System::Clock::Timeout _GetWiFiStationReconnectInterval(void); CHIP_ERROR _SetWiFiStationReconnectInterval(System::Clock::Timeout val); bool _IsWiFiStationProvisioned(void); void _ClearWiFiStationProvision(void); CHIP_ERROR _GetAndLogWiFiStatsCounters(void); bool _CanStartWiFiScan(); void _OnWiFiScanDone(); void _OnWiFiStationProvisionChange(); #if CHIP_CONFIG_ENABLE_ICD_SERVER CHIP_ERROR _SetPollingInterval(System::Clock::Milliseconds32 pollingInterval); #endif // ===== Private members reserved for use by this class only. System::Clock::Timestamp mLastStationConnectFailTime; WiFiStationMode mWiFiStationMode; WiFiStationState mWiFiStationState; System::Clock::Timeout mWiFiStationReconnectInterval; BitFlags mFlags; CHIP_ERROR InitWiFi(void); void OnWiFiPlatformEvent(const ChipDeviceEvent * event); void DriveStationState(void); void OnStationConnected(void); void OnStationDisconnected(void); void ChangeWiFiStationState(WiFiStationState newState); static void DriveStationState(::chip::System::Layer * aLayer, void * aAppState); #if CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP WiFiAPMode _GetWiFiAPMode(void); CHIP_ERROR _SetWiFiAPMode(WiFiAPMode val); bool _IsWiFiAPActive(void); void _DemandStartWiFiAP(void); void _StopOnDemandWiFiAP(void); void _MaintainOnDemandWiFiAP(void); System::Clock::Timeout _GetWiFiAPIdleTimeout(void); void _SetWiFiAPIdleTimeout(System::Clock::Timeout val); bool _IsWiFiAPApplicationControlled(void); System::Clock::Timestamp mLastAPDemandTime; WiFiAPMode mWiFiAPMode; WiFiAPState mWiFiAPState; System::Clock::Timeout mWiFiAPIdleTimeout; void DriveAPState(void); CHIP_ERROR ConfigureWiFiAP(void); void ChangeWiFiAPState(WiFiAPState newState); static void DriveAPState(::chip::System::Layer * aLayer, void * aAppState); #endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP void UpdateInternetConnectivityState(void); void OnStationIPv4AddressAvailable(const ip_event_got_ip_t & got_ip); void OnStationIPv4AddressLost(void); void OnStationIPv6AddressAvailable(const ip_event_got_ip6_t & got_ip); #endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI #if CHIP_DEVICE_CONFIG_ENABLE_ETHERNET void OnEthernetIPv4AddressAvailable(const ip_event_got_ip_t & got_ip); void OnEthernetIPv4AddressLost(void); void OnEthernetIPv6AddressAvailable(const ip_event_got_ip6_t & got_ip); #endif // CHIP_DEVICE_CONFIG_ENABLE_ETHERNET // ===== Members for internal use by the following friends. friend ConnectivityManager & ConnectivityMgr(void); friend ConnectivityManagerImpl & ConnectivityMgrImpl(void); static ConnectivityManagerImpl sInstance; }; #if CHIP_DEVICE_CONFIG_ENABLE_WIFI inline bool ConnectivityManagerImpl::_IsWiFiStationApplicationControlled(void) { return mWiFiStationMode == kWiFiStationMode_ApplicationControlled; } inline bool ConnectivityManagerImpl::_IsWiFiStationConnected(void) { return mWiFiStationState == kWiFiStationState_Connected; } inline System::Clock::Timeout ConnectivityManagerImpl::_GetWiFiStationReconnectInterval(void) { return mWiFiStationReconnectInterval; } #if CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP inline ConnectivityManager::WiFiAPMode ConnectivityManagerImpl::_GetWiFiAPMode(void) { return mWiFiAPMode; } inline bool ConnectivityManagerImpl::_IsWiFiAPActive(void) { return mWiFiAPState == kWiFiAPState_Active; } inline System::Clock::Timeout ConnectivityManagerImpl::_GetWiFiAPIdleTimeout(void) { return mWiFiAPIdleTimeout; } inline bool ConnectivityManagerImpl::_IsWiFiAPApplicationControlled(void) { return mWiFiAPMode == kWiFiAPMode_ApplicationControlled; } #endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP inline bool ConnectivityManagerImpl::_CanStartWiFiScan() { return mWiFiStationState != kWiFiStationState_Connecting; } #endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI /** * Returns the public interface of the ConnectivityManager singleton object. * * Chip applications should use this to access features of the ConnectivityManager object * that are common to all platforms. */ inline ConnectivityManager & ConnectivityMgr(void) { return ConnectivityManagerImpl::sInstance; } /** * Returns the platform-specific implementation of the ConnectivityManager singleton object. * * Chip applications can use this to gain access to features of the ConnectivityManager * that are specific to the ESP32 platform. */ inline ConnectivityManagerImpl & ConnectivityMgrImpl(void) { return ConnectivityManagerImpl::sInstance; } } // namespace DeviceLayer } // namespace chip