sitara_depot/components/free_rtos/ethernet_ip/eth_udp_client.hpp

109 lines
2.6 KiB
C++
Raw Blame History

/*
* eth_udp_client.hpp
*
* Created on: 15 <20><><EFBFBD>. 2023 <20>.
* Author: sychev
*/
#ifndef FREE_RTOS_ETHERNET_IP_ETH_UDP_CLIENT_HPP_
#define FREE_RTOS_ETHERNET_IP_ETH_UDP_CLIENT_HPP_
#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"
#include <vector>
#include <iterator>
#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);
int32_t read_async(uint8_t * p_data, uint32_t len);
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);
}
int32_t write(uint32_t ip_dst_be, uint8_t * p_data, uint32_t len);
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));
}
void clear(); ///<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
private:
void put_data(uint32_t src_ip, uint8_t * p_data, uint32_t len);
int32_t get_data(uint8_t * p_data, uint32_t len);
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;
}
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_;
uint32_t src_ip_; /// Ip <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> UDP
};
}
#endif /* FREE_RTOS_ETHERNET_IP_ETH_UDP_CLIENT_HPP_ */