dev(SF-60): Улучшена работа ServiceLink и UDP

This commit is contained in:
algin 2023-10-19 15:11:51 +03:00
parent c3ca4b1e32
commit 07c4f35520
2 changed files with 107 additions and 63 deletions

View File

@ -34,55 +34,91 @@ void free_rtos::EthUpdClient::put_data(uint32_t src_ip, uint8_t * p_data, uint32
{
LockGuard lock(rx_mut_);
uint32_t new_size = buff_.size() + len;
if(new_size > max_buf_size) {
DebugP_log((char *)"Warning ! UDP client buffer overflow !\r\n");
return;
}
buff_.reserve(new_size);
buff_.insert(buff_.end(), p_data, p_data + len);
state_ = UDPState::UDP_BUSY;
}
std::vector<uint8_t>& buff = buff_[!toggle_];
uint32_t new_size = buff.size() + len;
if(new_size > max_buf_size) {
state_ = UDPState::UDP_READY;
DebugP_log((char *)"Warning ! UDP client buffer overflow !\r\n");
return;
}
buff.reserve(new_size);
buff.insert(buff.end(), p_data, p_data + len);
src_ip_ = src_ip;
{
LockGuard lock(rx_mut_);
state_ = UDPState::UDP_READY;
}
rx_sem_.post();
}
int32_t free_rtos::EthUpdClient::get_data(uint8_t * p_data, uint32_t len)
{
uint32_t size;
std::vector<uint8_t>& buff = buff_[toggle_];
uint32_t size = buff.size();
{
LockGuard lock(rx_mut_);
size = buff_.size();
if (size > len) {
size = len;
}
auto item_begin = buff_.begin();
auto item_end = item_begin + size;
std::copy(item_begin, item_end, p_data);
buff_.erase(item_begin, item_end);
if (size > len) {
size = len;
}
auto item_begin = buff.begin();
auto item_end = item_begin + size;
std::copy(item_begin, item_end, p_data);
buff.erase(item_begin, item_end);
return size;
}
bool free_rtos::EthUpdClient::capture_buffer()
{
LockGuard lock(rx_mut_);
bool toggle = !toggle_;
std::vector<uint8_t>& buff = buff_[toggle];
if(state_ != UDPState::UDP_READY) {
return false;
}
if(buff.empty() == true) {
return false;
}
toggle_ = toggle;
return true;
}
size_t free_rtos::EthUpdClient::get_data_size()
{
if (buff_[toggle_].empty() == true) {
if(capture_buffer() == false) {
return 0;
}
}
return buff_[toggle_].size();
}
int32_t free_rtos::EthUpdClient::read(uint8_t * p_data, uint32_t len)
{
if ((p_data == nullptr) || (len == 0)) {
return 0;
}
/// <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
while (buff_.empty()) {
rx_sem_.pend();
if(buff_[toggle_].empty() == true) {
while (capture_buffer() == false) {
rx_sem_.pend();
}
}
return get_data(p_data, len);
@ -94,9 +130,10 @@ int32_t free_rtos::EthUpdClient::read_async(uint8_t * p_data, uint32_t len)
return 0;
}
/// <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (buff_.empty()) {
return 0;
if (buff_[toggle_].empty() == true) {
if(capture_buffer() == false) {
return 0;
}
}
return get_data(p_data, len);
@ -104,8 +141,7 @@ int32_t free_rtos::EthUpdClient::read_async(uint8_t * p_data, uint32_t len)
void free_rtos::EthUpdClient::clear()
{
LockGuard lock(rx_mut_);
buff_.clear();
buff_[toggle_].clear();
}
bool free_rtos::EthUpdClient::write(uint32_t ip_dst_be, uint8_t * p_data, uint32_t len)

View File

@ -14,12 +14,18 @@
#include "free_rtos/ethernet_ip/eth_ip_iface.hpp"
#include "free_rtos/ethernet/eth_types.h"
#include <array>
#include <vector>
#include <iterator>
#include <cstdint>
namespace free_rtos {
enum UDPState : uint8_t {
UDP_READY = 0,
UDP_BUSY
};
class EthUpdClient {
friend class EthUdpServer;
@ -28,10 +34,12 @@ public:
uint16_t port_dst_be, uint16_t port_src_be,
bool use_chksum);
bool capture_buffer();
size_t get_data_size();
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)
{
@ -39,13 +47,15 @@ public:
return 0;
}
if (buff_.empty()) {
return 0;
if (buff_[toggle_].empty() == true) {
if(capture_buffer() == false) {
return 0;
}
}
return get_data(data, len);
}
*/
bool write(uint32_t ip_dst_be, uint8_t * p_data, uint32_t len);
template<class T, class Allocator>
@ -59,50 +69,48 @@ public:
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;
std::vector<uint8_t>& buff = buff_[toggle_];
uint32_t size = buff.size();
{
LockGuard lock(rx_mut_);
size = buff_.size();
if (size > len) {
size = len;
}
auto item_begin = buff_.begin();
auto item_end = item_begin + size;
data.insert(data.end(), item_begin, item_end);
buff_.erase(item_begin, item_end);
if (size > len) {
size = len;
}
auto item_begin = buff.begin();
auto item_end = item_begin + size;
data.insert(data.end(), item_begin, item_end);
buff.erase(item_begin, item_end);
return size;
}
*/
private:
const uint32_t max_buf_size = 0x10000; // 0x10000 - 65536, 0x11CA - 4554, 0x0BDC - 3036, 0x100000 - 1Mb
Semaphore rx_sem_;
Mutex rx_mut_;
const uint32_t max_buf_size = 0x8000; // 0x8000 - 32768, 0x10000 - 65536, 0x11CA - 4554, 0x0BDC - 3036, 0x100000 - 1Mb
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_;
TEthPkt eth_pkt_;
uint32_t src_ip_; /// Ip <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> UDP
Semaphore rx_sem_;
Mutex rx_mut_;
std::array<std::vector<uint8_t>, 2> buff_;
bool toggle_{false};
UDPState state_{UDPState::UDP_READY};
//std::vector<uint8_t> buff_;
};
}