sitara_depot/components/free_rtos/ethernet_ip/eth_udp_server.cpp

86 lines
2.4 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* eth_udp.cpp
*
* Created on: 15 апр. 2023 г.
* Author: sychev
*/
#include "free_rtos/ethernet_ip/eth_udp_server.hpp"
#include "free_rtos/ethernet_ip/eth_udp_types.h"
#include "free_rtos/ethernet_ip/eth_ip_types.h"
#include "free_rtos/base/swap.h"
free_rtos::EthUdpServer::EthUdpServer(EthIpIface& ip_iface, EthArpIface& arp)
: ip_iface_{ip_iface}
, arp_{arp}
{
}
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);
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>
if (connections_.count(port_dst)) {
return nullptr;
}
std::shared_ptr<EthUpdClient> p_client = std::make_shared<EthUpdClient>(ip_iface_, arp_, 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);
/// <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (item == connections_.end()) {
//EnetAppUtils_print("EthUdpServer::Process(): Port not found %d\r\n", port);
return 0;
}
auto& client = item->second;
TEthFrameHeader * p_eth_hdr = (TEthFrameHeader *)(p_data - sizeof(TEthFrameHeader) - sizeof(TIpHeader));
TIpHeader * p_ip_hdr = (TIpHeader*)(p_data - sizeof(TIpHeader));
uint64_t src_mac;
uint32_t src_ip;
memcpy(&src_mac, p_eth_hdr->mac_src, ETH_FRAME_MAC_ADDR_LEN_BYTES);
memcpy(&src_ip, p_ip_hdr->ip_src, ETH_IP_ADDR_LEN);
client->set_src_ip(src_ip, src_mac);
client->put_data(p_data + sizeof(TUdpHeader), BASE_SWAP16(hdr->Length) - sizeof(TUdpHeader));
rx_sem_.post();
return 0;
}
uint16_t free_rtos::EthUdpServer::Sender(HandlerArgs& handlerArgs, size_t scatter_segment)
{
return 0;
}