2023-05-03 14:01:32 +03:00
/*
* eth_udp_client . hpp
*
2023-07-17 17:51:18 +03:00
* Created on : 15 <EFBFBD> <EFBFBD> <EFBFBD> . 2023 <EFBFBD> .
2023-05-03 14:01:32 +03:00
* Author : sychev
*/
# ifndef FREE_RTOS_ETHERNET_IP_ETH_UDP_CLIENT_HPP_
# define FREE_RTOS_ETHERNET_IP_ETH_UDP_CLIENT_HPP_
2023-06-26 18:22:30 +03:00
# include "free_rtos/semaphore/semaphore.hpp"
# include "free_rtos/mutex/mutex.hpp"
# include "free_rtos/ethernet/eth_frame.h"
# include "free_rtos/ethernet_ip/eth_ip_iface.hpp"
# include "free_rtos/ethernet/eth_types.h"
2023-05-03 14:01:32 +03:00
# include <vector>
2023-07-20 12:48:59 +03:00
# include <iterator>
2023-05-03 14:01:32 +03:00
# include <cstdint>
namespace free_rtos {
class EthUpdClient {
friend class EthUdpServer ;
public :
EthUpdClient ( EthIpIface & ip_iface , TEthMacPorts port_id ,
uint16_t port_dst_be , uint16_t port_src_be ,
bool use_chksum ) ;
int32_t read ( uint8_t * p_data , uint32_t len ) ;
2023-07-17 17:51:18 +03:00
int32_t read_async ( uint8_t * p_data , uint32_t len ) ;
2023-07-20 12:48:59 +03:00
template < class T , class Allocator >
int32_t read_async ( std : : vector < T , Allocator > & data , uint32_t len = 65535 )
{
if ( len = = 0 ) {
return 0 ;
}
if ( buff_ . empty ( ) ) {
return 0 ;
}
return get_data ( data , len ) ;
}
2023-05-03 14:01:32 +03:00
int32_t write ( uint32_t ip_dst_be , uint8_t * p_data , uint32_t len ) ;
2023-07-20 12:48:59 +03:00
template < class T , class Allocator >
int32_t write ( uint32_t ip_dst_be , std : : vector < T , Allocator > & data )
{
return write ( ip_dst_be , data . data ( ) , data . size ( ) * sizeof ( T ) ) ;
}
2023-07-17 17:51:18 +03:00
void clear ( ) ; ///<2F> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
2023-05-03 14:01:32 +03:00
private :
void put_data ( uint32_t src_ip , uint8_t * p_data , uint32_t len ) ;
2023-07-17 17:51:18 +03:00
int32_t get_data ( uint8_t * p_data , uint32_t len ) ;
2023-05-03 14:01:32 +03:00
2023-07-20 12:48:59 +03:00
template < class T , class Allocator >
int32_t get_data ( std : : vector < T , Allocator > & data , uint32_t len )
{
uint32_t size = buff_ . size ( ) ;
if ( size > len ) {
size = len ;
}
{
LockGuard lock ( rx_mut_ ) ;
auto item_begin = buff_ . begin ( ) ;
auto item_end = item_begin + size ;
std : : copy ( item_begin , item_end , std : : back_inserter ( data ) ) ;
buff_ . erase ( item_begin , item_end ) ;
}
return size ;
}
2023-05-03 14:01:32 +03:00
private :
const uint32_t max_buf_size = 0x100000 ; // 1Mb
Semaphore rx_sem_ ;
Mutex rx_mut_ ;
const uint16_t port_dst_be_ ; /// big endian
const uint16_t port_src_be_ ; /// big endian
const bool use_checksum_ ;
std : : vector < uint8_t > buff_ ;
TEthPkt eth_pkt_ ;
EthIpIface & ip_iface_ ;
const TEthMacPorts port_id_ ;
2023-07-17 17:51:18 +03:00
uint32_t src_ip_ ; /// Ip <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> UDP
2023-05-03 14:01:32 +03:00
} ;
}
# endif /* FREE_RTOS_ETHERNET_IP_ETH_UDP_CLIENT_HPP_ */