blob: 546a31362c1cd28468e83bfc150b9f9adb71a9e3 [file] [log] [blame]
/*
*
* Copyright (c) 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 <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
#include <platform/internal/GenericConnectivityManagerImpl_WiFi.h>
#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
#include <platform/internal/GenericConnectivityManagerImpl_BLE.h>
#else
#include <platform/internal/GenericConnectivityManagerImpl_NoBLE.h>
#endif
#include <platform/internal/GenericConnectivityManagerImpl_NoThread.h>
#include "lega_wlan_api.h"
#include <lib/support/BitFlags.h>
#include <platform/ASR/NetworkCommissioningDriver.h>
namespace Inet {
class IPAddress;
} // namespace Inet
namespace chip {
namespace DeviceLayer {
class PlatformManagerImpl;
/**
* Concrete implementation of the ConnectivityManager singleton object for the ASR platform.
*/
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
public Internal::GenericConnectivityManagerImpl_WiFi<ConnectivityManagerImpl>,
#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
public Internal::GenericConnectivityManagerImpl_BLE<ConnectivityManagerImpl>,
#else
public Internal::GenericConnectivityManagerImpl_NoBLE<ConnectivityManagerImpl>,
#endif
public Internal::GenericConnectivityManagerImpl_NoThread<ConnectivityManagerImpl>
{
// Allow the ConnectivityManager interface class to delegate method calls to
// the implementation methods provided by this class.
friend class ConnectivityManager;
public:
CHIP_ERROR ping_thread(void);
private:
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);
void ChangeWiFiStationState(WiFiStationState newState);
WiFiAPMode _GetWiFiAPMode(void);
CHIP_ERROR _SetWiFiAPMode(WiFiAPMode val);
bool _IsWiFiAPActive(void);
bool _IsWiFiAPApplicationControlled(void);
void _DemandStartWiFiAP(void);
void _StopOnDemandWiFiAP(void);
void _MaintainOnDemandWiFiAP(void);
System::Clock::Timeout _GetWiFiAPIdleTimeout(void);
void _SetWiFiAPIdleTimeout(System::Clock::Timeout val);
// Service connectivity methods
CHIP_ERROR _Init(void);
void _OnPlatformEvent(const ChipDeviceEvent * event);
bool _CanStartWiFiScan();
void _OnWiFiScanDone();
void _OnWiFiStationProvisionChange();
// ===== Members for internal use by the following friends.
friend ConnectivityManager & ConnectivityMgr(void);
friend ConnectivityManagerImpl & ConnectivityMgrImpl(void);
static ConnectivityManagerImpl sInstance;
// ===== Private members reserved for use by this class only.
System::Clock::Timestamp mLastStationConnectFailTime;
System::Clock::Timestamp mLastAPDemandTime;
WiFiStationMode mWiFiStationMode;
WiFiStationState mWiFiStationState;
WiFiAPMode mWiFiAPMode;
WiFiAPState mWiFiAPState;
System::Clock::Timeout mWiFiStationReconnectInterval;
System::Clock::Timeout mWiFiAPIdleTimeout;
BitFlags<Flags> mFlags;
bool eventcallback;
static void DriveStationState(::chip::System::Layer * aLayer, void * aAppState);
static void DriveAPState(::chip::System::Layer * aLayer, void * aAppState);
void DriveStationState(void);
void OnStationConnected(void);
void OnStationDisconnected(void);
void DriveAPState(void);
CHIP_ERROR ConfigureWiFiAP(void);
void ChangeWiFiAPState(WiFiAPState newState);
void UpdateInternetConnectivityState(void);
static void lega_wifi_connect_state(lega_wifi_event_e stat);
static void lega_wifi_get_ip_ind(lega_wlan_ip_stat_t * pnet);
static void lega_wifi_get_ip6_ind(lega_wlan_ip_stat_t * pnet);
static void lega_wifi_ap_peer_change(lega_wlan_client_addr_info_t * peer_info, uint8_t connect);
static void lega_wlan_err_stat_handler(lega_wlan_err_status_e err_info);
CHIP_ERROR WiFi_init(void);
};
inline bool ConnectivityManagerImpl::_IsWiFiStationApplicationControlled(void)
{
return mWiFiStationMode == kWiFiStationMode_ApplicationControlled;
}
inline bool ConnectivityManagerImpl::_IsWiFiStationConnected(void)
{
return mWiFiStationState == kWiFiStationState_Connected;
}
inline bool ConnectivityManagerImpl::_IsWiFiAPApplicationControlled(void)
{
return mWiFiAPMode == kWiFiAPMode_ApplicationControlled;
}
inline System::Clock::Timeout ConnectivityManagerImpl::_GetWiFiStationReconnectInterval(void)
{
return mWiFiStationReconnectInterval;
}
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::_CanStartWiFiScan()
{
return mWiFiStationState != kWiFiStationState_Connecting;
}
/**
* 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 ASR platform.
*/
inline ConnectivityManagerImpl & ConnectivityMgrImpl(void)
{
return ConnectivityManagerImpl::sInstance;
}
} // namespace DeviceLayer
} // namespace chip