2015-01-26 20:30:27 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* net/local/local_fifo.c
|
|
|
|
*
|
2024-09-11 14:39:39 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
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-26 20:30:27 +01:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-01-26 20:30:27 +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-26 20:30:27 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
2015-01-30 18:14:24 +01:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
2020-11-13 10:28:44 +01:00
|
|
|
#include <inttypes.h>
|
2015-01-26 20:30:27 +01:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
2021-05-14 04:45:57 +02:00
|
|
|
#include <debug.h>
|
2015-01-26 20:30:27 +01:00
|
|
|
|
|
|
|
#include "local/local.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
2020-01-13 21:26:04 +01:00
|
|
|
* Pre-processor Definitions
|
2015-01-26 20:30:27 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2015-01-29 14:47:09 +01:00
|
|
|
#define LOCAL_CS_SUFFIX "CS" /* Name of the client-to-server FIFO */
|
|
|
|
#define LOCAL_SC_SUFFIX "SC" /* Name of the server-to-client FIFO */
|
|
|
|
#define LOCAL_HD_SUFFIX "HD" /* Name of the half duplex datagram FIFO */
|
2015-01-26 20:30:27 +01:00
|
|
|
#define LOCAL_SUFFIX_LEN 2
|
|
|
|
|
2023-08-28 05:05:33 +02:00
|
|
|
#define LOCAL_FULLPATH_LEN (sizeof(CONFIG_NET_LOCAL_VFS_PATH) + \
|
2023-08-30 14:33:05 +02:00
|
|
|
UNIX_PATH_MAX + LOCAL_SUFFIX_LEN + 2 + 8)
|
2015-01-26 20:30:27 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2022-10-10 16:59:56 +02:00
|
|
|
* Name: local_format_name
|
2015-01-26 20:30:27 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2022-10-10 16:59:56 +02:00
|
|
|
* Format the name of the half duplex FIFO.
|
2015-01-26 20:30:27 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-10-10 16:59:56 +02:00
|
|
|
static void local_format_name(FAR const char *inpath, FAR char *outpath,
|
|
|
|
FAR const char *suffix, int32_t id)
|
2015-01-26 20:30:27 +01:00
|
|
|
{
|
net/local: fix build warning on GCC 12.2.0
In file included from local/local_fifo.c:25:
In function ‘local_format_name’,
inlined from ‘local_hd_name’ at local/local_fifo.c:132:3,
inlined from ‘local_open_receiver’ at local/local_fifo.c:661:3:
local/local_fifo.c:77:16: warning: ‘%s’ directive output may be truncated writing up to 107 bytes into a region of size 100 [-Wformat-truncation=]
77 | CONFIG_NET_LOCAL_VFS_PATH "/%s%s", inpath, suffix);
| ^~~~~~~~~~~~~~~~~~~~~~~~~
local/local_fifo.c: In function ‘local_open_receiver’:
local/local_fifo.c:82:44: note: format string is defined here
82 | CONFIG_NET_LOCAL_VFS_PATH "/%s%s%" PRIx32,
| ^~
In function ‘local_format_name’,
inlined from ‘local_hd_name’ at local/local_fifo.c:132:3,
inlined from ‘local_open_receiver’ at local/local_fifo.c:661:3:
local/local_fifo.c:76:7: note: ‘snprintf’ output between 12 and 119 bytes into a destination of size 109
76 | snprintf(outpath, LOCAL_FULLPATH_LEN - 1,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
77 | CONFIG_NET_LOCAL_VFS_PATH "/%s%s", inpath, suffix);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Signed-off-by: chao an <anchao@xiaomi.com>
2023-07-06 04:35:32 +02:00
|
|
|
if (strncmp(inpath, CONFIG_NET_LOCAL_VFS_PATH,
|
2023-09-01 04:58:25 +02:00
|
|
|
sizeof(CONFIG_NET_LOCAL_VFS_PATH) - 1) == 0)
|
2022-12-30 07:37:24 +01:00
|
|
|
{
|
2023-09-01 04:58:25 +02:00
|
|
|
inpath += sizeof(CONFIG_NET_LOCAL_VFS_PATH) - 1;
|
2022-12-30 07:37:24 +01:00
|
|
|
}
|
|
|
|
|
2022-10-10 16:59:56 +02:00
|
|
|
if (id < 0)
|
2015-05-12 15:47:32 +02:00
|
|
|
{
|
2022-10-10 16:59:56 +02:00
|
|
|
snprintf(outpath, LOCAL_FULLPATH_LEN - 1,
|
|
|
|
CONFIG_NET_LOCAL_VFS_PATH "/%s%s", inpath, suffix);
|
2015-05-12 15:47:32 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-10-10 16:59:56 +02:00
|
|
|
snprintf(outpath, LOCAL_FULLPATH_LEN - 1,
|
|
|
|
CONFIG_NET_LOCAL_VFS_PATH "/%s%s%" PRIx32,
|
|
|
|
inpath, suffix, id);
|
2015-05-12 15:47:32 +02:00
|
|
|
}
|
|
|
|
|
2022-10-10 16:59:56 +02:00
|
|
|
outpath[LOCAL_FULLPATH_LEN - 1] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_cs_name
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Create the name of the client-to-server FIFO.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void local_cs_name(FAR struct local_conn_s *conn, FAR char *path)
|
|
|
|
{
|
|
|
|
local_format_name(conn->lc_path, path,
|
|
|
|
LOCAL_CS_SUFFIX, conn->lc_instance_id);
|
2015-01-26 20:30:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-01-29 14:47:09 +01:00
|
|
|
* Name: local_sc_name
|
2015-01-26 20:30:27 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2015-01-29 14:47:09 +01:00
|
|
|
* Create the name of the server-to-client FIFO.
|
2015-01-26 20:30:27 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-10-10 16:59:56 +02:00
|
|
|
static void local_sc_name(FAR struct local_conn_s *conn, FAR char *path)
|
2015-01-26 20:30:27 +01:00
|
|
|
{
|
2022-10-10 16:59:56 +02:00
|
|
|
local_format_name(conn->lc_path, path,
|
|
|
|
LOCAL_SC_SUFFIX, conn->lc_instance_id);
|
2015-01-29 14:47:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_hd_name
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Create the name of the half duplex, datagram FIFO.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-01-31 14:58:51 +01:00
|
|
|
#ifdef CONFIG_NET_LOCAL_DGRAM
|
2022-10-10 16:59:56 +02:00
|
|
|
static void local_hd_name(FAR const char *inpath, FAR char *outpath)
|
2015-01-29 14:47:09 +01:00
|
|
|
{
|
2022-10-10 16:59:56 +02:00
|
|
|
local_format_name(inpath, outpath, LOCAL_HD_SUFFIX, -1);
|
2015-01-26 20:30:27 +01:00
|
|
|
}
|
2015-01-31 14:58:51 +01:00
|
|
|
#endif /* CONFIG_NET_LOCAL_DGRAM */
|
2015-01-26 20:30:27 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_fifo_exists
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Check if a FIFO exists.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static bool local_fifo_exists(FAR const char *path)
|
|
|
|
{
|
|
|
|
struct stat buf;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Create the client-to-server FIFO */
|
|
|
|
|
2020-05-02 16:30:58 +02:00
|
|
|
ret = nx_stat(path, &buf, 1);
|
2015-01-26 20:30:27 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-06-28 16:09:56 +02:00
|
|
|
/* Return true if what we found is a FIFO. What if it is something else?
|
|
|
|
* In that case, we will return false and mkfifo() will fail.
|
2015-01-26 20:30:27 +01:00
|
|
|
*/
|
|
|
|
|
2024-02-28 06:14:19 +01:00
|
|
|
return S_ISFIFO(buf.st_mode);
|
2015-01-26 20:30:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_create_fifo
|
|
|
|
*
|
|
|
|
* Description:
|
2015-01-29 14:47:09 +01:00
|
|
|
* Create the one FIFO.
|
2015-01-26 20:30:27 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2024-08-14 14:41:23 +02:00
|
|
|
static int local_create_fifo(FAR const char *path, uint32_t bufsize)
|
2015-01-26 20:30:27 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Create the client-to-server FIFO if it does not already exist. */
|
|
|
|
|
|
|
|
if (!local_fifo_exists(path))
|
|
|
|
{
|
2024-08-14 14:41:23 +02:00
|
|
|
ret = nx_mkfifo(path, 0644, bufsize);
|
2015-01-26 20:30:27 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2021-02-15 09:08:51 +01:00
|
|
|
nerr("ERROR: Failed to create FIFO %s: %d\n", path, ret);
|
|
|
|
return ret;
|
2015-01-26 20:30:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The FIFO (or some character driver) exists at PATH or we successfully
|
|
|
|
* created the FIFO at that location.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-01-30 16:28:55 +01:00
|
|
|
* Name: local_release_fifo
|
2015-01-26 20:30:27 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2015-01-30 16:28:55 +01:00
|
|
|
* Release a reference from one of the FIFOs used in a connection.
|
2015-01-26 20:30:27 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-01-30 16:28:55 +01:00
|
|
|
static int local_release_fifo(FAR const char *path)
|
2015-01-26 20:30:27 +01:00
|
|
|
{
|
2015-02-01 15:52:26 +01:00
|
|
|
int ret;
|
|
|
|
|
2015-01-30 16:28:55 +01:00
|
|
|
/* Unlink the client-to-server FIFO if it exists. */
|
2015-01-26 20:30:27 +01:00
|
|
|
|
|
|
|
if (local_fifo_exists(path))
|
|
|
|
{
|
2015-02-01 15:52:26 +01:00
|
|
|
/* Un-linking the FIFO removes the FIFO from the namespace. It will
|
|
|
|
* also mark the FIFO device "unlinked". When all of the open
|
|
|
|
* references to the FIFO device are closed, the resources consumed
|
|
|
|
* by the device instance will also be freed.
|
2015-01-30 16:28:55 +01:00
|
|
|
*/
|
2015-01-30 18:14:24 +01:00
|
|
|
|
2021-02-15 09:08:51 +01:00
|
|
|
ret = nx_unlink(path);
|
2015-01-26 20:30:27 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2021-02-15 09:08:51 +01:00
|
|
|
nerr("ERROR: Failed to unlink FIFO %s: %d\n", path, ret);
|
|
|
|
return ret;
|
2015-01-26 20:30:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The FIFO does not exist or we successfully unlinked it. */
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_rx_open
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Open a FIFO for read-only access.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-01-30 19:43:37 +01:00
|
|
|
static int local_rx_open(FAR struct local_conn_s *conn, FAR const char *path,
|
|
|
|
bool nonblock)
|
2015-01-26 20:30:27 +01:00
|
|
|
{
|
2015-01-30 19:43:37 +01:00
|
|
|
int oflags = nonblock ? O_RDONLY | O_NONBLOCK : O_RDONLY;
|
2017-11-02 15:23:38 +01:00
|
|
|
int ret;
|
2015-01-30 19:43:37 +01:00
|
|
|
|
2023-09-13 15:08:13 +02:00
|
|
|
ret = file_open(&conn->lc_infile, path, oflags | O_CLOEXEC);
|
2018-09-15 18:49:41 +02:00
|
|
|
if (ret < 0)
|
2015-01-26 20:30:27 +01:00
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
nerr("ERROR: Failed on open %s for reading: %d\n",
|
2018-09-15 18:49:41 +02:00
|
|
|
path, ret);
|
2015-01-26 20:30:27 +01:00
|
|
|
|
2018-09-15 18:49:41 +02:00
|
|
|
/* Map the error code to something consistent with the return
|
2015-01-26 20:30:27 +01:00
|
|
|
* error codes from connect():
|
|
|
|
*
|
2018-09-15 18:49:41 +02:00
|
|
|
* If error is ENOENT, meaning that the FIFO does exist,
|
2015-01-26 20:30:27 +01:00
|
|
|
* return EFAULT meaning that the socket structure address is
|
|
|
|
* outside the user's address space.
|
|
|
|
*/
|
|
|
|
|
2018-09-15 18:49:41 +02:00
|
|
|
return ret == -ENOENT ? -EFAULT : ret;
|
2017-11-02 15:23:38 +01:00
|
|
|
}
|
|
|
|
|
2015-01-26 20:30:27 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_tx_open
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Open a FIFO for write-only access.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-01-30 19:43:37 +01:00
|
|
|
static int local_tx_open(FAR struct local_conn_s *conn, FAR const char *path,
|
|
|
|
bool nonblock)
|
2015-01-26 20:30:27 +01:00
|
|
|
{
|
2017-11-02 15:23:38 +01:00
|
|
|
int ret;
|
2015-01-30 19:43:37 +01:00
|
|
|
|
2023-09-13 15:08:13 +02:00
|
|
|
ret = file_open(&conn->lc_outfile, path, O_WRONLY | O_NONBLOCK |
|
|
|
|
O_CLOEXEC);
|
2018-09-15 18:49:41 +02:00
|
|
|
if (ret < 0)
|
2015-01-26 20:30:27 +01:00
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
nerr("ERROR: Failed on open %s for writing: %d\n",
|
2018-09-15 18:49:41 +02:00
|
|
|
path, ret);
|
2015-01-26 20:30:27 +01:00
|
|
|
|
2018-09-15 18:49:41 +02:00
|
|
|
/* Map the error code to something consistent with the return
|
2015-01-26 20:30:27 +01:00
|
|
|
* error codes from connect():
|
|
|
|
*
|
2018-09-15 18:49:41 +02:00
|
|
|
* If error is ENOENT, meaning that the FIFO does exist,
|
2015-01-26 20:30:27 +01:00
|
|
|
* return EFAULT meaning that the socket structure address is
|
|
|
|
* outside the user's address space.
|
|
|
|
*/
|
|
|
|
|
2018-09-16 02:13:49 +02:00
|
|
|
return ret == -ENOENT ? -EFAULT : ret;
|
2017-11-02 15:23:38 +01:00
|
|
|
}
|
|
|
|
|
2023-05-11 02:41:24 +02:00
|
|
|
/* Clear O_NONBLOCK if it's meant to be blocking */
|
|
|
|
|
|
|
|
if (nonblock == false)
|
|
|
|
{
|
2024-02-26 21:31:20 +01:00
|
|
|
ret = nonblock;
|
|
|
|
ret = file_ioctl(&conn->lc_outfile, FIONBIO, &ret);
|
2023-05-11 02:41:24 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-26 20:30:27 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2015-01-30 18:14:24 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_set_policy
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Set the FIFO buffer policy:
|
|
|
|
*
|
|
|
|
* 0=Free FIFO resources when the last reference is closed
|
|
|
|
* 1=Free FIFO resources when the buffer is empty.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-11-02 15:23:38 +01:00
|
|
|
static int local_set_policy(FAR struct file *filep, unsigned long policy)
|
2015-01-30 18:14:24 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Set the buffer policy */
|
|
|
|
|
2017-11-02 15:23:38 +01:00
|
|
|
ret = file_ioctl(filep, PIPEIOC_POLICY, policy);
|
2015-01-30 18:14:24 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2019-06-13 13:52:40 +02:00
|
|
|
nerr("ERROR: Failed to set FIFO buffer policy: %d\n", ret);
|
2015-01-30 18:14:24 +01:00
|
|
|
}
|
|
|
|
|
2017-11-02 15:23:38 +01:00
|
|
|
return ret;
|
2015-01-30 18:14:24 +01:00
|
|
|
}
|
|
|
|
|
2023-01-10 13:32:25 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_set_pollinthreshold
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Set the local pollin threshold:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_LOCAL_DGRAM
|
|
|
|
static int local_set_pollinthreshold(FAR struct file *filep,
|
|
|
|
unsigned long threshold)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Set the buffer poll threshold */
|
|
|
|
|
|
|
|
ret = file_ioctl(filep, PIPEIOC_POLLINTHRD, threshold);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nerr("ERROR: Failed to set FIFO pollin threshold: %lu\n",
|
|
|
|
threshold);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_LOCAL_DGRAM */
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_set_polloutthreshold
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Set the local pollout threshold:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_LOCAL_DGRAM
|
|
|
|
static int local_set_polloutthreshold(FAR struct file *filep,
|
|
|
|
unsigned long threshold)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Set the buffer poll threshold */
|
|
|
|
|
|
|
|
ret = file_ioctl(filep, PIPEIOC_POLLOUTTHRD, threshold);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nerr("ERROR: Failed to set FIFO pollout threshold: %lu\n",
|
|
|
|
threshold);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_LOCAL_DGRAM */
|
|
|
|
|
2015-01-26 20:30:27 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-06-27 10:28:09 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_set_pollthreshold
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Set the local pollin and pollout threshold:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_LOCAL_DGRAM
|
|
|
|
int local_set_pollthreshold(FAR struct local_conn_s *conn,
|
|
|
|
unsigned long threshold)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Set the buffer poll threshold */
|
|
|
|
|
|
|
|
ret = local_set_pollinthreshold(&conn->lc_infile, threshold);
|
|
|
|
if (ret > 0)
|
|
|
|
{
|
|
|
|
ret = local_set_polloutthreshold(&conn->lc_outfile, threshold);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_LOCAL_DGRAM */
|
|
|
|
|
2015-01-26 20:30:27 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_create_fifos
|
|
|
|
*
|
|
|
|
* Description:
|
2015-01-29 14:47:09 +01:00
|
|
|
* Create the FIFO pair needed for a SOCK_STREAM connection.
|
2015-01-26 20:30:27 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2024-08-14 14:41:23 +02:00
|
|
|
int local_create_fifos(FAR struct local_conn_s *conn,
|
|
|
|
uint32_t cssize, uint32_t scsize)
|
2015-01-26 20:30:27 +01:00
|
|
|
{
|
|
|
|
char path[LOCAL_FULLPATH_LEN];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Create the client-to-server FIFO if it does not already exist. */
|
|
|
|
|
2015-01-29 14:47:09 +01:00
|
|
|
local_cs_name(conn, path);
|
2024-08-14 14:41:23 +02:00
|
|
|
ret = local_create_fifo(path, cssize);
|
2015-01-26 20:30:27 +01:00
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
/* Create the server-to-client FIFO if it does not already exist. */
|
|
|
|
|
2015-01-29 14:47:09 +01:00
|
|
|
local_sc_name(conn, path);
|
2024-08-14 14:41:23 +02:00
|
|
|
ret = local_create_fifo(path, scsize);
|
2015-01-26 20:30:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-01-29 14:47:09 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_create_halfduplex
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Create the half-duplex FIFO needed for SOCK_DGRAM communication.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-01-31 14:58:51 +01:00
|
|
|
#ifdef CONFIG_NET_LOCAL_DGRAM
|
2020-11-13 10:26:49 +01:00
|
|
|
int local_create_halfduplex(FAR struct local_conn_s *conn,
|
2024-08-14 14:41:23 +02:00
|
|
|
FAR const char *path, uint32_t bufsize)
|
2015-01-29 14:47:09 +01:00
|
|
|
{
|
2015-01-29 18:05:04 +01:00
|
|
|
char fullpath[LOCAL_FULLPATH_LEN];
|
2015-01-29 14:47:09 +01:00
|
|
|
|
|
|
|
/* Create the half duplex FIFO if it does not already exist. */
|
|
|
|
|
2015-01-29 18:05:04 +01:00
|
|
|
local_hd_name(path, fullpath);
|
2024-08-14 14:41:23 +02:00
|
|
|
return local_create_fifo(fullpath, bufsize);
|
2015-01-29 14:47:09 +01:00
|
|
|
}
|
2015-01-31 14:58:51 +01:00
|
|
|
#endif /* CONFIG_NET_LOCAL_DGRAM */
|
2015-01-29 14:47:09 +01:00
|
|
|
|
2015-01-26 20:30:27 +01:00
|
|
|
/****************************************************************************
|
2015-01-30 16:28:55 +01:00
|
|
|
* Name: local_release_fifos
|
2015-01-26 20:30:27 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2015-01-30 16:28:55 +01:00
|
|
|
* Release references to the FIFO pair used for a SOCK_STREAM connection.
|
2015-01-26 20:30:27 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-01-30 16:28:55 +01:00
|
|
|
int local_release_fifos(FAR struct local_conn_s *conn)
|
2015-01-26 20:30:27 +01:00
|
|
|
{
|
|
|
|
char path[LOCAL_FULLPATH_LEN];
|
|
|
|
int ret1;
|
|
|
|
int ret2;
|
|
|
|
|
2024-02-28 06:14:19 +01:00
|
|
|
/* Destroy the server-to-client FIFO if it exists. */
|
2015-01-26 20:30:27 +01:00
|
|
|
|
2015-01-29 14:47:09 +01:00
|
|
|
local_sc_name(conn, path);
|
2015-01-30 16:28:55 +01:00
|
|
|
ret1 = local_release_fifo(path);
|
2015-01-26 20:30:27 +01:00
|
|
|
|
2024-02-28 06:14:19 +01:00
|
|
|
/* Destroy the client-to-server FIFO if it exists. */
|
2015-01-26 20:30:27 +01:00
|
|
|
|
2015-01-29 14:47:09 +01:00
|
|
|
local_cs_name(conn, path);
|
2015-02-01 16:16:55 +01:00
|
|
|
ret2 = local_release_fifo(path);
|
2015-01-26 20:30:27 +01:00
|
|
|
|
|
|
|
/* Return a failure if one occurred. */
|
|
|
|
|
|
|
|
return ret1 < 0 ? ret1 : ret2;
|
|
|
|
}
|
|
|
|
|
2015-01-29 14:47:09 +01:00
|
|
|
/****************************************************************************
|
2015-01-30 16:28:55 +01:00
|
|
|
* Name: local_release_halfduplex
|
2015-01-29 14:47:09 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2015-01-30 16:28:55 +01:00
|
|
|
* Release a reference to the FIFO used for SOCK_DGRAM communication
|
2015-01-29 14:47:09 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-01-31 14:58:51 +01:00
|
|
|
#ifdef CONFIG_NET_LOCAL_DGRAM
|
2015-01-30 16:28:55 +01:00
|
|
|
int local_release_halfduplex(FAR struct local_conn_s *conn)
|
2015-01-29 14:47:09 +01:00
|
|
|
{
|
2015-02-01 16:16:55 +01:00
|
|
|
#if 1
|
2021-12-20 15:49:48 +01:00
|
|
|
/* REVISIT: We need to think about this carefully. Unlike the connection-
|
2015-02-01 16:16:55 +01:00
|
|
|
* oriented Unix domain socket, we don't really know the best time to
|
|
|
|
* release the FIFO resource. It would be extremely inefficient to create
|
|
|
|
* and destroy the FIFO on each packet. But, on the other hand, failing
|
2020-02-22 19:31:14 +01:00
|
|
|
* to destroy the FIFO will leave the FIFO resources in place after the
|
2015-02-01 16:16:55 +01:00
|
|
|
* communications have completed.
|
|
|
|
*
|
2015-02-01 19:23:57 +01:00
|
|
|
* I am thinking that there should be something like a timer. The timer
|
2015-02-01 16:16:55 +01:00
|
|
|
* would be started at the completion of each transfer and cancelled at
|
|
|
|
* the beginning of each transfer. If the timer expires, then the FIFO
|
|
|
|
* would be destroyed.
|
|
|
|
*/
|
|
|
|
|
2023-05-01 15:55:35 +02:00
|
|
|
/* #warning Missing logic */
|
|
|
|
|
2015-02-01 16:16:55 +01:00
|
|
|
return OK;
|
|
|
|
|
|
|
|
#else
|
2015-01-29 14:47:09 +01:00
|
|
|
char path[LOCAL_FULLPATH_LEN];
|
|
|
|
|
|
|
|
/* Destroy the half duplex FIFO if it exists. */
|
|
|
|
|
2015-01-29 17:40:10 +01:00
|
|
|
local_hd_name(conn->lc_path, path);
|
2015-01-30 16:28:55 +01:00
|
|
|
return local_release_fifo(path);
|
2015-02-01 16:16:55 +01:00
|
|
|
#endif
|
2015-01-29 14:47:09 +01:00
|
|
|
}
|
2015-01-31 14:58:51 +01:00
|
|
|
#endif /* CONFIG_NET_LOCAL_DGRAM */
|
2015-01-29 14:47:09 +01:00
|
|
|
|
2015-01-26 20:30:27 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_open_client_rx
|
|
|
|
*
|
|
|
|
* Description:
|
2015-01-29 14:47:09 +01:00
|
|
|
* Open the client-side of the server-to-client FIFO.
|
2015-01-26 20:30:27 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-01-30 19:43:37 +01:00
|
|
|
int local_open_client_rx(FAR struct local_conn_s *client, bool nonblock)
|
2015-01-26 20:30:27 +01:00
|
|
|
{
|
|
|
|
char path[LOCAL_FULLPATH_LEN];
|
2015-01-30 18:14:24 +01:00
|
|
|
int ret;
|
2015-01-26 20:30:27 +01:00
|
|
|
|
|
|
|
/* Get the server-to-client path name */
|
|
|
|
|
2015-01-29 14:47:09 +01:00
|
|
|
local_sc_name(client, path);
|
2015-01-26 20:30:27 +01:00
|
|
|
|
|
|
|
/* Then open the file for read-only access */
|
|
|
|
|
2015-01-30 19:43:37 +01:00
|
|
|
ret = local_rx_open(client, path, nonblock);
|
2015-01-30 18:14:24 +01:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
/* Policy: Free FIFO resources when the last reference is closed */
|
|
|
|
|
2017-11-02 15:23:38 +01:00
|
|
|
ret = local_set_policy(&client->lc_infile, 0);
|
2015-01-30 18:14:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2015-01-26 20:30:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_open_client_tx
|
|
|
|
*
|
|
|
|
* Description:
|
2015-01-29 14:47:09 +01:00
|
|
|
* Open the client-side of the client-to-server FIFO.
|
2015-01-26 20:30:27 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-01-30 19:43:37 +01:00
|
|
|
int local_open_client_tx(FAR struct local_conn_s *client, bool nonblock)
|
2015-01-26 20:30:27 +01:00
|
|
|
{
|
|
|
|
char path[LOCAL_FULLPATH_LEN];
|
2015-01-30 18:14:24 +01:00
|
|
|
int ret;
|
2015-01-26 20:30:27 +01:00
|
|
|
|
|
|
|
/* Get the client-to-server path name */
|
|
|
|
|
2015-01-29 14:47:09 +01:00
|
|
|
local_cs_name(client, path);
|
2015-01-26 20:30:27 +01:00
|
|
|
|
|
|
|
/* Then open the file for write-only access */
|
|
|
|
|
2015-01-30 19:43:37 +01:00
|
|
|
ret = local_tx_open(client, path, nonblock);
|
2015-01-30 18:14:24 +01:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
/* Policy: Free FIFO resources when the last reference is closed */
|
|
|
|
|
2017-11-02 15:23:38 +01:00
|
|
|
ret = local_set_policy(&client->lc_outfile, 0);
|
2015-01-30 18:14:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2015-01-26 20:30:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_open_server_rx
|
|
|
|
*
|
|
|
|
* Description:
|
2015-01-29 14:47:09 +01:00
|
|
|
* Open the server-side of the client-to-server FIFO.
|
2015-01-26 20:30:27 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-01-30 19:43:37 +01:00
|
|
|
int local_open_server_rx(FAR struct local_conn_s *server, bool nonblock)
|
2015-01-26 20:30:27 +01:00
|
|
|
{
|
|
|
|
char path[LOCAL_FULLPATH_LEN];
|
2015-01-30 18:14:24 +01:00
|
|
|
int ret;
|
2015-01-26 20:30:27 +01:00
|
|
|
|
|
|
|
/* Get the client-to-server path name */
|
|
|
|
|
2015-01-29 14:47:09 +01:00
|
|
|
local_cs_name(server, path);
|
2015-01-26 20:30:27 +01:00
|
|
|
|
2024-02-28 06:14:19 +01:00
|
|
|
/* Then open the file for read-only access */
|
2015-01-26 20:30:27 +01:00
|
|
|
|
2015-01-30 19:43:37 +01:00
|
|
|
ret = local_rx_open(server, path, nonblock);
|
2015-01-30 18:14:24 +01:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
/* Policy: Free FIFO resources when the last reference is closed */
|
|
|
|
|
2017-11-02 15:23:38 +01:00
|
|
|
ret = local_set_policy(&server->lc_infile, 0);
|
2015-01-30 18:14:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2015-01-26 20:30:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_open_server_tx
|
|
|
|
*
|
|
|
|
* Description:
|
2024-02-28 06:14:19 +01:00
|
|
|
* Open the server-side of the server-to-client FIFO.
|
2015-01-26 20:30:27 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-01-30 19:43:37 +01:00
|
|
|
int local_open_server_tx(FAR struct local_conn_s *server, bool nonblock)
|
2015-01-26 20:30:27 +01:00
|
|
|
{
|
|
|
|
char path[LOCAL_FULLPATH_LEN];
|
2015-01-30 18:14:24 +01:00
|
|
|
int ret;
|
2015-01-26 20:30:27 +01:00
|
|
|
|
|
|
|
/* Get the server-to-client path name */
|
|
|
|
|
2015-01-29 14:47:09 +01:00
|
|
|
local_sc_name(server, path);
|
2015-01-26 20:30:27 +01:00
|
|
|
|
2024-02-28 06:14:19 +01:00
|
|
|
/* Then open the file for write-only access */
|
2015-01-26 20:30:27 +01:00
|
|
|
|
2015-01-30 19:43:37 +01:00
|
|
|
ret = local_tx_open(server, path, nonblock);
|
2015-01-30 18:14:24 +01:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
/* Policy: Free FIFO resources when the last reference is closed */
|
|
|
|
|
2017-11-02 15:23:38 +01:00
|
|
|
ret = local_set_policy(&server->lc_outfile, 0);
|
2015-01-30 18:14:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2015-01-26 20:30:27 +01:00
|
|
|
}
|
2015-01-29 14:47:09 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_open_receiver
|
|
|
|
*
|
|
|
|
* Description:
|
2024-02-28 06:14:19 +01:00
|
|
|
* Open the receiving side of the half duplex FIFO.
|
2015-01-29 14:47:09 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-01-31 14:58:51 +01:00
|
|
|
#ifdef CONFIG_NET_LOCAL_DGRAM
|
2015-01-30 19:43:37 +01:00
|
|
|
int local_open_receiver(FAR struct local_conn_s *conn, bool nonblock)
|
2015-01-29 14:47:09 +01:00
|
|
|
{
|
|
|
|
char path[LOCAL_FULLPATH_LEN];
|
2015-01-30 18:14:24 +01:00
|
|
|
int ret;
|
2015-01-29 14:47:09 +01:00
|
|
|
|
|
|
|
/* Get the server-to-client path name */
|
|
|
|
|
2015-01-29 17:40:10 +01:00
|
|
|
local_hd_name(conn->lc_path, path);
|
2015-01-29 14:47:09 +01:00
|
|
|
|
|
|
|
/* Then open the file for read-only access */
|
|
|
|
|
2015-01-30 19:43:37 +01:00
|
|
|
ret = local_rx_open(conn, path, nonblock);
|
2015-01-30 18:14:24 +01:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
/* Policy: Free FIFO resources when the buffer is empty. */
|
|
|
|
|
2017-11-02 15:23:38 +01:00
|
|
|
ret = local_set_policy(&conn->lc_infile, 1);
|
2023-01-10 13:32:25 +01:00
|
|
|
if (ret == 0)
|
|
|
|
{
|
2024-02-28 06:34:39 +01:00
|
|
|
/* Set POLLIN threshold bigger than preamble len.
|
2023-01-10 13:32:25 +01:00
|
|
|
* This is to avoid non-blocking read failed with -EAGAIN when
|
|
|
|
* only preamble len is sent and read by reader.
|
|
|
|
*/
|
|
|
|
|
2024-02-28 06:34:39 +01:00
|
|
|
ret = local_set_pollinthreshold(&conn->lc_infile,
|
2024-08-26 08:48:55 +02:00
|
|
|
2 * sizeof(lc_size_t));
|
2023-01-10 13:32:25 +01:00
|
|
|
}
|
2015-01-30 18:14:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2015-01-29 14:47:09 +01:00
|
|
|
}
|
2015-01-31 14:58:51 +01:00
|
|
|
#endif /* CONFIG_NET_LOCAL_DGRAM */
|
2015-01-29 14:47:09 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_open_sender
|
|
|
|
*
|
|
|
|
* Description:
|
2024-02-28 06:14:19 +01:00
|
|
|
* Open the sending side of the half duplex FIFO.
|
2015-01-29 14:47:09 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-01-31 14:58:51 +01:00
|
|
|
#ifdef CONFIG_NET_LOCAL_DGRAM
|
2015-01-30 19:43:37 +01:00
|
|
|
int local_open_sender(FAR struct local_conn_s *conn, FAR const char *path,
|
|
|
|
bool nonblock)
|
2015-01-29 14:47:09 +01:00
|
|
|
{
|
2015-01-29 17:40:10 +01:00
|
|
|
char fullpath[LOCAL_FULLPATH_LEN];
|
2015-01-30 18:14:24 +01:00
|
|
|
int ret;
|
2015-01-29 14:47:09 +01:00
|
|
|
|
|
|
|
/* Get the server-to-client path name */
|
|
|
|
|
2015-01-29 17:40:10 +01:00
|
|
|
local_hd_name(path, fullpath);
|
2015-01-29 14:47:09 +01:00
|
|
|
|
2024-02-28 06:14:19 +01:00
|
|
|
/* Then open the file for write-only access */
|
2015-01-29 14:47:09 +01:00
|
|
|
|
2015-01-30 19:43:37 +01:00
|
|
|
ret = local_tx_open(conn, fullpath, nonblock);
|
2015-01-30 18:14:24 +01:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
/* Policy: Free FIFO resources when the buffer is empty. */
|
|
|
|
|
2017-11-02 15:23:38 +01:00
|
|
|
ret = local_set_policy(&conn->lc_outfile, 1);
|
2023-01-10 13:32:25 +01:00
|
|
|
if (ret == 0)
|
|
|
|
{
|
2024-02-28 06:34:39 +01:00
|
|
|
/* Set POLLOUT threshold bigger than preamble len.
|
2024-02-28 06:14:19 +01:00
|
|
|
* This is to avoid non-blocking write failed with -EAGAIN when
|
2023-01-10 13:32:25 +01:00
|
|
|
* only preamble len is sent and read by reader.
|
|
|
|
*/
|
|
|
|
|
2024-02-28 06:34:39 +01:00
|
|
|
ret = local_set_polloutthreshold(&conn->lc_outfile,
|
2024-08-26 08:48:55 +02:00
|
|
|
2 * sizeof(lc_size_t));
|
2023-01-10 13:32:25 +01:00
|
|
|
}
|
2015-01-30 18:14:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2015-01-29 14:47:09 +01:00
|
|
|
}
|
2015-01-31 14:58:51 +01:00
|
|
|
#endif /* CONFIG_NET_LOCAL_DGRAM */
|
2015-01-29 14:47:09 +01:00
|
|
|
|
2023-12-19 10:37:08 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_set_nonblocking
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Set the local conntion to nonblocking mode
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int local_set_nonblocking(FAR struct local_conn_s *conn)
|
|
|
|
{
|
|
|
|
int nonblock = 1;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Set the conn to nonblocking mode */
|
|
|
|
|
|
|
|
ret = file_ioctl(&conn->lc_infile, FIONBIO, &nonblock);
|
|
|
|
ret |= file_ioctl(&conn->lc_outfile, FIONBIO, &nonblock);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nerr("ERROR: Failed to set the conn to nonblocking mode: %d\n", ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|