C - LWIP ECHO 服务器:如何在 ITOA 函数中增加缓冲区大小



我正在使用 Xilinx Ethernetlite (LWIP) 设计。只有当buf = 32时,我才能通过以太网将数据从KC板传输到PC(大力神)。但我的实际缓冲区大小是 1024。如何将缓冲区大小从 32 增加到 1024

我无法确定错误是在代码中还是在大力神中。为了读取大力神中的值(整数),我正在执行此功能。

最初,我将从大力神向董事会发送 Hello 命令,然后接受该请求。之后,板会将数据(整数值)输出到赫拉克勒斯。

伊托亚的 C 代码

char* itoa(int val, int base)
{
static char buf[32] = {0};          //buf size 
int i = 30;
for(; val && i ; --i, val /= base)
buf[i] = "0123456789abcdef"[val % base];
return &buf[i+1];
}

修改后的代码

#define  DAQ_FIFO_DEPTH  128
int transfer_data() 
{
return 0;
}
err_t tcp_write_u32_string(struct tcp_pcb *pcb, unsigned char   prefix, u32_t value)
{
unsigned char  buf[11]; /* enough room for prefix and value. */
err_t          result;
u16_t          len;
unsigned char *p = buf + sizeof buf;
do {
/* ASCII encoding: '0' = 48, '1' = 49, ..., '9' = 57. */
*(--p) = 48 + (value % 10u);
value /= 10;
} while (value);
if (prefix)
*(--p) = prefix;
len = buf + sizeof buf - p;
if (tcp_sndbuf(pcb) < len) 
{
result = tcp_output(pcb);
if (result != ERR_OK)
return result;
}
return tcp_write(pcb, p, len, TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE);
}
err_t send_list(struct tcp_pcb *pcb, const u32_t data[], u16_t len)
{
static const char  newline[2] = { 13, 10 }; /* ASCII rn */
err_t              result;
if (len > 0) {
u16_t  i;

result = tcp_write_u32_string(pcb, 0, data[0]);
if (result != ERR_OK)
return result;
for (i = 1; i < len; i++) 
{
/* ASCII comma is code 44. (Use 32 for space, or 9 for tab.) */
result = tcp_write_u32_string(pcb, 44, data[i]);
if (result != ERR_OK)
return result;
}
}
result = tcp_write(pcb, newline, 2, 0);
if (result)
return result; 
return tcp_output(pcb);
}
int application_connection(void *arg, struct tcp_pcb *conn, err_t err)
{
struct netif *netif = arg; /* Because of tcp_arg(, netif). */
u32_t         data[DAQ_FIFO_DEPTH];
u32_t         i, n;
if (err != ERR_OK) {
tcp_abort(conn);
return ERR_ABRT;
}
err = daq_setup();
if (err != ERR_OK) 
{
tcp_abort(conn);
return ERR_ABRT;
}
while (1) 
{
xemacif_input(netif);
tcp_tmr();
tcp_output(conn);
n = daq_acquire(data, DAQ_FIFO_DEPTH);
if (n > DAQ_FIFO_DEPTH)
break;
if (tcp_write(conn, data, n * sizeof data[0], TCP_WRITE_FLAG_COPY) != ERR_OK)
break;
}
// daq_close();
/* Close the TCP connection. */
if (tcp_close(conn) == ERR_OK)
return ERR_OK;
/* Close failed. Abort it, then. */
tcp_abort(conn);
return ERR_ABRT;
}
int application_main(struct netif *netif, unsigned int port)
{
struct tcp_pcb *pcb;
err_t           err;
pcb = tcp_new();
if (!pcb) {
/* Out of memory error */
return -1;
}
err = tcp_bind(pcb, IP_ADDR_ANY, port);
if (err != ERR_OK) {
/* TCP error */
return -1;
}
pcb = tcp_listen_with_backlog(pcb, 1);
if (!pcb) {
/* Out of memory. */
return -1;
}
tcp_arg(pcb, netif); 
tcp_accept(pcb, application_connection);
while (1)
xemacif_input(netif);
}

大力神输出 在此处输入图像描述

那么,这是赛灵思论坛讨论的延续吗?

itoa()函数将一个无符号整数(存储在int中)转换为缓冲区buf中的前30

个字符左右。recv_callback()函数几乎没有意义。

aurora_rx_main()的调用被记录为"FUNCTION CALL",这并没有多大帮助(因为我们不知道它做了什么),甚至它的返回值也被完全忽略了。

第一个for循环在DestinationBuffer[]中转储前 100 个u32的内容以进行调试,因此代码与手头的任务无关。但是,我们不知道是谁或什么填满了DestinationBuffer.它可能已被aurora_rx_main()调用填充,也可能未填充;无论哪种方式,我们都没有被告知。

(tcp_*()函数似乎遵循 Wikia 的 lwIP Wiki 中描述的 API。

如果p参数为 NULL,则调用tcp_close(tcpb),后跟tcp_recv(tcpb, NULL)调用。这是最没有意义的:为什么要在关闭后尝试接收任何内容(以及为什么使用 NULL 参数)?

下一部分同样令人困惑。看起来if测试检查 TCP 发送缓冲区的大小是否超过 1024 字节。否则,将释放p缓冲区。否则,for循环会尝试将DestinationBuffer中的每个u32转换为字符串,将该字符串写入 TCP 缓冲区;但是,它不是使用正确的API标志,而是使用常量1,甚至不检查附加到TCP发送缓冲区是否有效。

总之,这看起来像是一堆复制粘贴的代码,没有任何明智的作用。增加 itoa 函数中的缓冲区大小不仅是不必要的(一个u32,即使转换为int,也总是适合 12 个字符(不包括减号或末尾的 nul 字节,所以总共 13 个字符),而且与它应该解决的问题完全无关。

根本问题是代码很糟糕。修改它就像将车身填充物放在一块旧口香糖上,以"修复"它。 正确的解决方法是完全删除垃圾代码,并使用更好的代码。

编辑:OP声明他们是一个新程序员,所以上面的评论应该被视为对所显示代码的直接,诚实的意见,而不是关于OP本身。让我们看看我们是否可以帮助OP产生更好的代码。


首先,显示的itoa()函数很愚蠢。假设意图实际上是将DestinationBuffer中的u32_t作为十进制字符串发送回,那么实现一个帮助程序函数来进行转换要好得多。 由于该值前面要加上逗号(或其他分隔符),因此我们也可以简单地添加它。由于它将使用tcp_write()发送,我们将结合功能:

err_t tcp_write_u32_string(struct tcp_pcb *pcb,
unsigned char   prefix, /* 0 for none */
u32_t           value)
{
/* Because 0 <= u32_t <= 4294967295, the value itself is at most 10 digits long. */
unsigned char  buf[11]; /* enough room for prefix and value. */
err_t          result;
u16_t          len;
unsigned char *p = buf + sizeof buf;
/* Construct the value first, from right to left. */
do {
/* ASCII encoding: '0' = 48, '1' = 49, ..., '9' = 57. */
*(--p) = 48 + (value % 10u);
value /= 10;
} while (value);
/* Prepend the prefix, if any. */
if (prefix)
*(--p) = prefix;
/* Calculate the length of this part. */
len = buf + sizeof buf - p;
/* If the TCP buffer does not have enough free space, flush it. */
if (tcp_sendbuf(pcb) < len) {
result = tcp_output(pcb);
if (result != ERR_OK)
return result;
}
/* Append the buffer to the TCP send buffer.
We also assume the packet is not done yet. */
return tcp_write(pcb, p, len, TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE);
}

这样,要将指定数组中的numu32_ts作为十进制字符串发送,末尾有一个换行符,您可以使用

err_t send_list(struct tcp_pcb *pcb,
const u32_t data[],
u16_t len)
{
static const char  newline[2] = { 13, 10 }; /* ASCII rn */
err_t              result;
if (len > 0) {
u16_t  i;
/* The first number has no prefix. */
result = tcp_write_u32_string(pcb, 0, data[0]);
if (result != ERR_OK)
return result;
/* The following numbers have a comma prefix. */
for (i = 1; i < len; i++) {
/* ASCII comma is code 44. (Use 32 for space, or 9 for tab.) */
result = tcp_write_u32_string(pcb, 44, data[i]);
if (result != ERR_OK)
return result;
}
}
/* We add a final newline.
Note that this one can be referenced,
and it does complete what we wanted to send thus far. */
result = tcp_write(pcb, newline, 2, 0);
if (result)
return result;
/* and flush the buffer, so the packet gets sent right now. */
return tcp_output(pcb);
}

现在,我还没有为 Xilinx 编写 C 或根本没有使用 lwIP 堆栈,所以上面的代码是盲写的。 然而,我很有信心它有效(除非有任何错别字或想法;如果你发现任何,请在评论中报告它们,我会验证并修复)。

两个缓冲区(bufnewline)被声明为static,因此尽管它们只在各自的函数中可见,但它们的值在全局范围内是有效的。

由于 TCP 是流协议,因此不必将每个响应都适合单个数据包。除了 11 个字符(对于每个数字及其前缀字符)和 2 个字符(换行符)缓冲区之外,您唯一需要的大缓冲区是 TCP 发送缓冲区(最大传输单元或最大段大小,因为我不确定 lwIP 如何在内部使用缓冲区),通常在 536 到 1518 字节之间。

上述两个函数尝试在数字之间拆分数据包,但这只是因为它比尝试精确填充每个数据包更容易。如果下一个(逗号和)值适合缓冲区,则将其添加到缓冲区中;否则,首先刷新缓冲区,然后将下一个(逗号和)值添加到缓冲区。

从接收方,您应该使用例如netcat获得一个漂亮的,可读的流。(我不知道Hercules是一个应用程序,还是只是本地机器的名称。由于TCP是一种流协议,因此接收方无法(可靠地)检测到数据包边界的位置(与UDP数据报不同)。实际上,TCP连接只是两个数据流,每个数据流都是一个方向,拆分为数据包只是一个协议细节应用程序程序员不需要担心。 对于 lwIP,因为它是一个如此低级的库,所以需要注意一点,但从上面的代码可以看出,它真的没有那么多。


OP在评论中解释说,他们不是很有经验,总体目的是让设备接受TCP连接,并通过连接将数据(由单独的采集板获取的样本)作为无符号的32位整数进行流式传输。

因为我很想拥有其中一个FPGA板(我有几个任务,我可以看看是否可以卸载到FPGA),但没有资源来获得一个,所以我将尝试在这里概述整个应用程序。请注意,我唯一需要继续的信息是 Xilinx 操作系统和库文档集 (UG643) 的 2018 版 (PDF)。看起来 OP 想要使用原始 API,以实现高性能。

将示例转换为文本是愚蠢的,尤其是在需要高性能的情况下。我们应该只使用原始二进制文件,以及 KC705 使用的任何字节序。(我没有从文档中快速浏览它,但我怀疑它是小端序)。

根据文档,原始 APImain()类似于以下内容:

int main(void)
{
/* MAC address. Use an unique one. */
unsigned char  mac[6] = { 0x00, 0x0A, 0x35, 0x00, 0x01, 0x02 };
struct netif  *netif = NULL;
ip_addr_t      ipaddr, netmask, gateway;
/* Define IP address, netmask, and gateway. */
IP4_ADDR(&ipaddr,  192, 168,   1,  1);
IP4_ADDR(&netmask, 255, 255, 255,  0);
IP4_ADDR(&gateway,   0,   0,   0,  0);
/* Initialize lwIP networking stack. */
lwip_init();
/* Add this networking interface, and make it the default one */
if (!xemac_add(netif, &ipaddr, &netmask, &gateway, mac, EMAC_BASEADDR)) {
printf("Error adding network interfacenr");
return -1;
}
netif_set_default(netif);
platform_enable_interrupts();
/* Bring the network interface up (activate it) */
netif_set_up(netif);
/* Our application listens on port 7. */
return application_main(netif, 7);
}

在文档示例中,您将看到对start_application()的调用,而不是return application_main(netif);,然后是一个定期调用xemacif_input(netif)的无限循环。这只是意味着application_main()必须定期呼叫xemacif_input(netif),以便能够接收数据。(lwIP 文档说我们还应该定期调用sys_check_timeouts()tcp_tmr()

请注意,我省略了报告 printfs 的错误,并且它不会优雅地从错误中恢复,而是只会返回(从main());我不确定这是否会导致 KC705 重新启动还是什么。

int application_main(struct netif *netif, unsigned int port)
{
struct tcp_pcb *pcb;
err_t           err;
pcb = tcp_new();
if (!pcb) {
/* Out of memory error */
return -1;
}
/* Listen for incoming connections on the specified port. */
err = tcp_bind(pcb, IP_ADDR_ANY, port);
if (err != ERR_OK) {
/* TCP error */
return -1;
}
pcb = tcp_listen_with_backlog(pcb, 1);
if (!pcb) {
/* Out of memory. */
return -1;
}
/* The accept callback function gets the network interface
structure as the extra parameter. */
tcp_arg(pcb, netif);
/* For each incoming connection, call application_connection(). */
tcp_accept(pcb, application_connection);
/* In the mean time, process incoming data. */
while (1)
xemacif_input(netif);
}

对于与端口的每个TCP连接,我们都会收到对application_connection()的调用。这是设置数据采集板的功能,只要接收者需要,就可以传输数据。

/* How many DAQ samples to process in each batch.
* Should be around the DAQ FIFO depth or so, I think. */
#define  DAQ_FIFO_DEPTH  128
err_t application_connection(void *arg, struct tcp_pcb *conn, err_t err)
{
struct netif *netif = arg; /* Because of tcp_arg(, netif). */
u32_t         data[DAQ_FIFO_DEPTH];
u32_t         i, n;
/* Drop the connection if there was an error. */
if (err != ERR_OK) {
tcp_abort(conn);
return ERR_ABRT;
}
/* Setup the data aquisition. */
err = daq_setup();
if (err != ERR_OK) {
tcp_abort(conn);
return ERR_ABRT;
}
/* Data acquisition to TCP loop. */
while (1) {
/* Keep the networking stack running. */
xemacif_input(netif);
tcp_tmr();
/* Tell the networking stack to output what it can. */
tcp_output(conn);
/* Acquire up to DAQ_FIFO_DEPTH samples. */
n = daq_acquire(data, DAQ_FIFO_DEPTH);
if (n > DAQ_FIFO_DEPTH)
break;
/* Write data as-is to the tcp buffer. */
if (tcp_write(conn, data, n * sizeof data[0], TCP_WRITE_FLAG_COPY) != ERR_OK)
break;
}
/* Stop data acquisition. */
daq_close();
/* Close the TCP connection. */
if (tcp_close(conn) == ERR_OK)
return ERR_OK;
/* Close failed. Abort it, then. */
tcp_abort(conn);
return ERR_ABRT;
}

还有三个功能要实现:daq_setup(),它应该设置数据采集和FIFO;daq_acquire(u32_t *data, u32_t count)存储多达count个样本以data[],并返回存储的实际样本数 - 最好只是耗尽 FIFO,而不是等待新样本到达--,并最终daq_close(),从而停止数据采集。

我相信它们应该是这样的:

XLlFifo         daq_fifo;
err_t daq_setup(void)
{
XLlFifo_Config *config = NULL;
config = XLlFifo_LookupConfig(DAQ_FIFO_ID);
if (!config)
return ERR_RTE;
if (XLlFifo_CfgInitialize(&daq_fifo, config, config->BaseAddress) != XST_SUCCESS)
return ERR_RTE;
}
u32_t daq_acquire(u32_t *data, u32_t max)
{
u32_t len, have;
have = XLlFifo_iRxGetLen(&daq_fifo);
if (have < 1)
return 0;
else
if (have < max)
max = have;
for (len = 0; len < max; len++)
data[len] = XLlFifo_RxGetWork(&daq_fifo);
return len;
}
err_t daq_close(void)
{
/* How to stop the FIFO? Do we need to? */
}

仅此而已。

最新更新