2015-01-27 15:05:09 +01:00
|
|
|
/****************************************************************************
|
2015-01-27 18:51:24 +01:00
|
|
|
* net/local/local_sendpacket.c
|
2015-01-27 15:05:09 +01:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2015-01-27 15:05:09 +01:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-01-27 15:05:09 +01:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2015-01-27 15:05:09 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2017-10-11 18:18:30 +02:00
|
|
|
#include <nuttx/fs/fs.h>
|
2020-06-15 10:23:25 +02:00
|
|
|
#include "devif/devif.h"
|
2017-10-11 18:18:30 +02:00
|
|
|
|
2015-01-27 15:05:09 +01:00
|
|
|
#include "local/local.h"
|
|
|
|
|
2017-10-11 18:18:30 +02:00
|
|
|
#if defined(CONFIG_NET) && defined(CONFIG_NET_LOCAL)
|
|
|
|
|
2015-01-27 15:05:09 +01:00
|
|
|
/****************************************************************************
|
2015-01-27 18:51:24 +01:00
|
|
|
* Private Functions
|
2015-01-27 15:05:09 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-01-27 18:51:24 +01:00
|
|
|
* Name: local_fifo_write
|
2015-01-27 15:05:09 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Write a data on the write-only FIFO.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-11-02 15:23:38 +01:00
|
|
|
* filep File structure of write-only FIFO.
|
2015-01-27 15:05:09 +01:00
|
|
|
* buf Data to send
|
|
|
|
* len Length of data to send
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2021-08-20 13:34:55 +02:00
|
|
|
* On success, the number of bytes written are returned (zero indicates
|
|
|
|
* nothing was written). On any failure, a negated errno value is returned
|
2015-01-27 15:05:09 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-11-02 15:23:38 +01:00
|
|
|
static int local_fifo_write(FAR struct file *filep, FAR const uint8_t *buf,
|
|
|
|
size_t len)
|
2015-01-27 15:05:09 +01:00
|
|
|
{
|
2021-08-20 13:34:55 +02:00
|
|
|
ssize_t nwritten = 0;
|
2021-08-25 14:11:30 +02:00
|
|
|
ssize_t ret = 0;
|
2015-01-27 15:05:09 +01:00
|
|
|
|
2021-08-20 13:34:55 +02:00
|
|
|
while (len != nwritten)
|
2015-01-27 15:05:09 +01:00
|
|
|
{
|
2021-08-20 13:34:55 +02:00
|
|
|
ret = file_write(filep, buf + nwritten, len - nwritten);
|
|
|
|
if (ret < 0)
|
2015-01-27 15:05:09 +01:00
|
|
|
{
|
2022-03-29 15:44:21 +02:00
|
|
|
if (ret == -EINTR)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (ret == -EAGAIN)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
2015-01-27 15:05:09 +01:00
|
|
|
{
|
2021-08-20 13:34:55 +02:00
|
|
|
nerr("ERROR: file_write failed: %zd\n", ret);
|
|
|
|
break;
|
2015-01-27 15:05:09 +01:00
|
|
|
}
|
|
|
|
}
|
2021-08-20 13:34:55 +02:00
|
|
|
|
|
|
|
nwritten += ret;
|
2015-01-27 15:05:09 +01:00
|
|
|
}
|
|
|
|
|
2021-08-25 14:11:30 +02:00
|
|
|
return nwritten > 0 ? nwritten : ret;
|
2015-01-27 15:05:09 +01:00
|
|
|
}
|
|
|
|
|
2015-01-27 18:51:24 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-01-27 15:05:09 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_send_packet
|
|
|
|
*
|
|
|
|
* Description:
|
2015-01-27 18:51:24 +01:00
|
|
|
* Send a packet on the write-only FIFO.
|
2015-01-27 15:05:09 +01:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-11-02 15:23:38 +01:00
|
|
|
* filep File structure of write-only FIFO.
|
2015-01-27 15:05:09 +01:00
|
|
|
* buf Data to send
|
|
|
|
* len Length of data to send
|
2021-07-21 09:41:49 +02:00
|
|
|
* preamble Flag to indicate the preamble sync header assembly
|
2015-01-27 15:05:09 +01:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2021-03-12 08:33:16 +01:00
|
|
|
* Packet length is returned on success; a negated errno value is returned
|
|
|
|
* on any failure.
|
2015-01-27 15:05:09 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-12-01 07:55:16 +01:00
|
|
|
int local_send_packet(FAR struct file *filep, FAR const struct iovec *buf,
|
2021-07-21 09:41:49 +02:00
|
|
|
size_t len, bool preamble)
|
2015-01-27 15:05:09 +01:00
|
|
|
{
|
2021-07-21 09:41:49 +02:00
|
|
|
FAR const struct iovec *end = buf + len;
|
2020-12-01 07:55:16 +01:00
|
|
|
FAR const struct iovec *iov;
|
2021-07-21 09:41:49 +02:00
|
|
|
int ret = -EINVAL;
|
2015-01-27 15:05:09 +01:00
|
|
|
uint16_t len16;
|
|
|
|
|
2021-07-21 09:41:49 +02:00
|
|
|
if (preamble)
|
2015-01-27 15:05:09 +01:00
|
|
|
{
|
2021-08-20 13:34:55 +02:00
|
|
|
/* Send the packet length */
|
2021-07-21 09:41:49 +02:00
|
|
|
|
2021-08-20 13:34:55 +02:00
|
|
|
for (len16 = 0, iov = buf; iov != end; iov++)
|
|
|
|
{
|
|
|
|
len16 += iov->iov_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = local_fifo_write(filep, (FAR const uint8_t *)&len16,
|
|
|
|
sizeof(uint16_t));
|
|
|
|
if (ret != sizeof(uint16_t))
|
|
|
|
{
|
|
|
|
return ret;
|
2021-07-21 09:41:49 +02:00
|
|
|
}
|
|
|
|
}
|
2020-12-01 07:55:16 +01:00
|
|
|
|
2021-07-21 09:41:49 +02:00
|
|
|
for (len16 = 0, iov = buf; iov != end; iov++)
|
|
|
|
{
|
|
|
|
ret = local_fifo_write(filep, iov->iov_base, iov->iov_len);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
break;
|
2015-01-27 15:05:09 +01:00
|
|
|
}
|
2021-07-21 09:41:49 +02:00
|
|
|
|
2021-08-20 13:34:55 +02:00
|
|
|
if (ret > 0)
|
|
|
|
{
|
|
|
|
len16 += ret;
|
|
|
|
if (ret != iov->iov_len)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-01-27 15:05:09 +01:00
|
|
|
}
|
|
|
|
|
2022-08-08 05:08:05 +02:00
|
|
|
return len16 > 0 ? len16 : ret;
|
2015-01-27 15:05:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_NET && CONFIG_NET_LOCAL */
|