sitara_depot/components/free_rtos/ethernet_ip/eth_udp_server.cpp
algin ae3cac8a7d feat: First commit
Adds sitara_depot/free_rtos

Original one is on server_gorbunov/SmartForce4.0/sitara_depot
2023-05-03 14:01:32 +03:00

76 lines
1.7 KiB
C++

/*
* eth_udp.cpp
*
* Created on: 15 ìàð. 2023 ã.
* Author: sychev
*/
#include "ethernet_ip/eth_udp_server.hpp"
#include "ethernet_ip/eth_udp_types.h"
#include "ethernet_ip/eth_ip_types.h"
#include "base/swap.h"
free_rtos::EthUdpServer::EthUdpServer(EthIpIface& ip_iface) :
ip_iface_{ip_iface}
{
}
std::shared_ptr<free_rtos::EthUpdClient> free_rtos::EthUdpServer::createClient(uint16_t port_dst,
uint16_t port_src,
bool use_chksum)
{
port_dst = BASE_SWAP16(port_dst);
port_src = BASE_SWAP16(port_src);
/// Êëèåíò óæå çàðåãèñòðèðîâàí íà ýòîò ïîðò
if (connections_.count(port_dst)) {
return nullptr;
}
std::shared_ptr<EthUpdClient> p_client = std::make_shared<EthUpdClient>(ip_iface_, port_id_,
port_dst, port_src, use_chksum);
if (p_client == nullptr) {
return nullptr;
}
connections_[port_dst] = p_client;
return p_client;
}
int32_t free_rtos::EthUdpServer::Process(uint8_t * p_data, uint32_t len)
{
if (len <= sizeof(TUdpHeader)) {
return 0;
}
TUdpHeader * hdr = (TUdpHeader *)p_data;
uint16_t port = hdr->Dst_port;
auto item = connections_.find(port);
/// Òàêîé ïîðò íå çàðåãèñòðèðîâàí
if (item == connections_.end()) {
return 0;
}
auto& client = item->second;
TIpHeader * p_ip_hdr = (TIpHeader*)(p_data - sizeof(TIpHeader));
uint32_t src_ip;
memcpy(&src_ip, p_ip_hdr->ip_src, ETH_IP_ADDR_LEN);
/// Êîïèðóåì äàííûå udp
client->put_data(src_ip, p_data + sizeof(TUdpHeader), len - sizeof(TUdpHeader));
return 0;
}