Network Component
Installation
The component requires ESP-IDF 5.3 and higher and is installed using IDF Component Manager.
Add this to the idf_component.yml in the project main directory or in the component directory:
dependencies:
plasmapper/pl_network: "^1.1.2"
Add this to the source code:
#include "pl_network.h"
Add extern "C" to the app_main function:
extern "C" void app_main(void) {...}
Add this to the sdkconfig.defaults in the project directory or configure the values using Project Configuration:
CONFIG_COMPILER_CXX_RTTI=y
CONFIG_LWIP_SO_RCVBUF=y
Features
PL::IpV4AddressandPL::IpV6Address- data types for IPv4 and IPv6 addresses with number and string initialization and ToString methods.PL::NetworkEndpoint- a data type for an IP address (v4 or v6) and a port.PL::NetworkInterface- a base class for any network interface.PL::Ethernet- a base class for any ethernet interface.PL::WiFiStation- a base class for any Wi-Fi station.PL::EspNetworkInterface- is a base class for the internal ESP Ethernet and WiFi station. It implements severalPL::NetworkInterfacemethods using ESP C api for the IP and DHCP configuration.PL::EspEthernet- is aPL::Ethernetimplementation for the internal ESP Ethernet. It is initialized with the specificesp_eth_phy_new_xxxfunction for the PHY chip that is used.PL::EspEthernet::Initialize()installs the Ethernet driver.PL::EspEthernet::Enable()andPL::EspEthernet::Disable()enable and disable the interface.PL::EspEthernet::IsConnected()checks the connection status.PL::EspWiFiStation- is aPL::WiFiStationimplementation for the internal ESP Wi-Fi station.PL::EspWiFiStation::Initialize()installs the Wi-Fi driver.PL::EspWiFiStation::Enable()andPL::EspWiFiStation::Disable()enable and disable the interface.PL::EspWiFiStation::IsConnected()check the connection status.PL::EspWiFiStation::GetSsid(),PL::EspWiFiStation::SetSsid(),PL::EspWiFiStation::GetPassword()andPL::EspWiFiStation::SetPassword()get and set Wi-Fi station SSID and password.PL::NetworkStream- a base class for any network stream. In addition toPL::Streammethods it provides Nagle algorithm enabling/disabling, keep-alive packet configuration and getting local/remote endpoint information.PL::NetworkServer- a base class for any network server. In addition toPL::Servermethods it provides port and maximum number of clients configuration.PL::TcpClient- a TCP client class. It is initialized with an IP address and a port, that can be changed later.PL::TcpClient::Connect()andPL::TcpClient::Disonnect()connect/disconenct the client from the server.PL::TcpClient::GetStream()returns a lockablePL::NetworkStreamfor reading and writing.PL::TcpServer- aPL::NetworkServerimplementation for TCP connections. The descendant class should overridePL::TcpServer::HandleRequest()to handle the client request.PL::TcpServer::HandleRequest()is only called for clients with the incoming data in the internal buffer.
Thread safety
Class method thread safety is implemented by having the PL::Lockable as a base class and creating the class object lock guard at the beginning of the methods.
PL::TcpServer task method locks both the PL::TcpServer and the client PL::NetworkStream objects for the duration of the transaction.
Examples
API reference
- Types
- PL::NetworkInterface class
PL::NetworkInterfaceNetworkInterface()EnableIpV4DhcpClient()DisableIpV4DhcpClient()EnableIpV6DhcpClient()DisableIpV6DhcpClient()IsConnected()IsIpV4DhcpClientEnabled()IsIpV6DhcpClientEnabled()GetIpV4Address()SetIpV4Address()GetIpV4Netmask()SetIpV4Netmask()GetIpV4Gateway()SetIpV4Gateway()GetIpV6LinkLocalAddress()GetIpV6GlobalAddress()SetIpV6GlobalAddress()Initialize()Enable()Disable()IsEnabled()GetName()SetName()Lock()Unlock()connectedEventdisconnectedEventgotIpV4AddressEventlostIpV4AddressEventgotIpV6AddressEventlostIpV6AddressEventenabledEventdisabledEvent
- PL::Ethernet class
PL::EthernetEthernet()EnableIpV4DhcpClient()DisableIpV4DhcpClient()EnableIpV6DhcpClient()DisableIpV6DhcpClient()IsConnected()IsIpV4DhcpClientEnabled()IsIpV6DhcpClientEnabled()GetIpV4Address()SetIpV4Address()GetIpV4Netmask()SetIpV4Netmask()GetIpV4Gateway()SetIpV4Gateway()GetIpV6LinkLocalAddress()GetIpV6GlobalAddress()SetIpV6GlobalAddress()Initialize()Enable()Disable()IsEnabled()GetName()SetName()Lock()Unlock()connectedEventdisconnectedEventgotIpV4AddressEventlostIpV4AddressEventgotIpV6AddressEventlostIpV6AddressEventenabledEventdisabledEvent
- PL::WiFiStation class
PL::WiFiStationWiFiStation()GetSsid()SetSsid()GetPassword()SetPassword()EnableIpV4DhcpClient()DisableIpV4DhcpClient()EnableIpV6DhcpClient()DisableIpV6DhcpClient()IsConnected()IsIpV4DhcpClientEnabled()IsIpV6DhcpClientEnabled()GetIpV4Address()SetIpV4Address()GetIpV4Netmask()SetIpV4Netmask()GetIpV4Gateway()SetIpV4Gateway()GetIpV6LinkLocalAddress()GetIpV6GlobalAddress()SetIpV6GlobalAddress()Initialize()Enable()Disable()IsEnabled()GetName()SetName()Lock()Unlock()connectedEventdisconnectedEventgotIpV4AddressEventlostIpV4AddressEventgotIpV6AddressEventlostIpV6AddressEventenabledEventdisabledEvent
- PL::EspNetworkInterface class
PL::EspNetworkInterfaceEnableIpV4DhcpClient()DisableIpV4DhcpClient()EnableIpV6DhcpClient()DisableIpV6DhcpClient()IsIpV4DhcpClientEnabled()IsIpV6DhcpClientEnabled()GetIpV4Address()SetIpV4Address()GetIpV4Netmask()SetIpV4Netmask()GetIpV4Gateway()SetIpV4Gateway()GetIpV6LinkLocalAddress()GetIpV6GlobalAddress()SetIpV6GlobalAddress()IsConnected()Initialize()Enable()Disable()IsEnabled()GetName()SetName()Lock()Unlock()connectedEventdisconnectedEventgotIpV4AddressEventlostIpV4AddressEventgotIpV6AddressEventlostIpV6AddressEventenabledEventdisabledEventEspNetworkInterface()InitializeNetif()
- PL::EspEthernet class
- PL::EspWiFiStation class
PL::EspWiFiStationEspWiFiStation()Lock()Unlock()Initialize()Enable()Disable()IsEnabled()IsConnected()GetSsid()SetSsid()GetPassword()SetPassword()EnableIpV4DhcpClient()DisableIpV4DhcpClient()EnableIpV6DhcpClient()DisableIpV6DhcpClient()IsIpV4DhcpClientEnabled()IsIpV6DhcpClientEnabled()GetIpV4Address()SetIpV4Address()GetIpV4Netmask()SetIpV4Netmask()GetIpV4Gateway()SetIpV4Gateway()GetIpV6LinkLocalAddress()GetIpV6GlobalAddress()SetIpV6GlobalAddress()GetName()SetName()connectedEventdisconnectedEventgotIpV4AddressEventlostIpV4AddressEventgotIpV6AddressEventlostIpV6AddressEventenabledEventdisabledEventdefaultNameInitializeNetif()
- PL::NetworkStream class
PL::NetworkStreamNetworkStream()NetworkStream()Lock()Unlock()Read()Write()Close()EnableNagleAlgorithm()DisableNagleAlgorithm()EnableKeepAlive()DisableKeepAlive()IsOpen()GetReadableSize()GetReadTimeout()SetReadTimeout()GetLocalEndpoint()GetRemoteEndpoint()SetKeepAliveIdleTime()SetKeepAliveInterval()SetKeepAliveCount()Read()Write()Write()ReadUntil()ReadUntil()ReadUntil()FlushReadBuffer()defaultReadTimeout
- PL::NetworkServer class
- PL::TcpClient class
- PL::TcpServer class
PL::TcpServerTcpServer()Lock()Unlock()Enable()Disable()EnableNagleAlgorithm()DisableNagleAlgorithm()EnableKeepAlive()DisableKeepAlive()IsEnabled()GetPort()SetPort()GetMaxNumberOfClients()SetMaxNumberOfClients()GetClientStreams()SetTaskParameters()SetKeepAliveIdleTime()SetKeepAliveInterval()SetKeepAliveCount()GetName()SetName()clientConnectedEventclientDisconnectedEventenabledEventdisabledEventdefaultTaskParametersdefaultMaxNumberOfClientsdefaultKeepAliveIdleTimedefaultKeepAliveIntervaldefaultKeepAliveCountHandleRequest()