2011-03-12 04:42:08 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/net/slip.c
|
|
|
|
*
|
2020-03-31 16:05:48 +02: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
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* 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.
|
2011-03-12 04:42:08 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-03-31 16:05:48 +02:00
|
|
|
/* Reference: RFC 1055 */
|
|
|
|
|
2011-03-12 04:42:08 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <poll.h>
|
2011-03-12 04:42:08 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2023-05-25 18:12:53 +02:00
|
|
|
#include <assert.h>
|
2011-03-12 04:42:08 +01:00
|
|
|
#include <debug.h>
|
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
|
|
#include <nuttx/arch.h>
|
2011-03-12 04:42:08 +01:00
|
|
|
#include <nuttx/irq.h>
|
2017-10-06 18:15:01 +02:00
|
|
|
#include <nuttx/signal.h>
|
2023-05-25 18:12:53 +02:00
|
|
|
#include <nuttx/wdog.h>
|
|
|
|
#include <nuttx/wqueue.h>
|
2014-06-24 17:28:44 +02:00
|
|
|
#include <nuttx/net/netdev.h>
|
2011-03-12 04:42:08 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
#ifdef CONFIG_NET_SLIP
|
2011-03-12 04:42:08 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-03-15 00:24:42 +01:00
|
|
|
/* NOTE: Slip requires UART hardware handshake. If hardware handshake is
|
|
|
|
* not available with your UART, then you might try the 'slattach' option
|
2011-03-16 18:17:58 +01:00
|
|
|
* -L which enable "3-wire operation." That allows operation without the
|
|
|
|
* hardware handshake (but with the possibility of data overrun).
|
2011-03-15 00:24:42 +01:00
|
|
|
*/
|
|
|
|
|
2011-03-12 04:42:08 +01:00
|
|
|
/* Configuration ************************************************************/
|
|
|
|
|
2011-03-16 18:17:58 +01:00
|
|
|
/* The Linux slip module hard-codes its MTU size to 296 (40 bytes for the
|
2020-02-02 07:20:43 +01:00
|
|
|
* IP+TCP headers plus 256 bytes of data). So you might as well set
|
2018-07-04 22:10:40 +02:00
|
|
|
* CONFIG_NET_SLIP_PKTSIZE to 296 as well.
|
2011-03-16 18:17:58 +01:00
|
|
|
*
|
|
|
|
* There may be an issue with this setting, however. I see that Linux uses
|
|
|
|
* a MTU of 296 and window of 256, but actually only sends 168 bytes of data:
|
|
|
|
* 40 + 128. I believe that is to allow for the 2x worst cast packet
|
|
|
|
* expansion. Ideally we would like to advertise the 256 MSS, but restrict
|
2015-08-06 22:32:12 +02:00
|
|
|
* transfers to 128 bytes (possibly by modifying the tcp_mss() macro).
|
2011-03-15 02:54:49 +01:00
|
|
|
*/
|
|
|
|
|
2018-07-04 22:10:40 +02:00
|
|
|
#if CONFIG_NET_SLIP_PKTSIZE < 296
|
|
|
|
# error "CONFIG_NET_SLIP_PKTSIZE >= 296 is required"
|
2011-03-15 00:24:42 +01:00
|
|
|
#endif
|
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
/* Work queue support is required. */
|
|
|
|
|
|
|
|
#if !defined(CONFIG_SCHED_WORKQUEUE)
|
|
|
|
# error Work queue support is required in this configuration (CONFIG_SCHED_WORKQUEUE)
|
|
|
|
#else
|
|
|
|
|
|
|
|
/* The low priority work queue is preferred. If it is not enabled, LPWORK
|
|
|
|
* will be the same as HPWORK.
|
|
|
|
*
|
|
|
|
* NOTE: However, the network should NEVER run on the high priority work
|
|
|
|
* queue! That queue is intended only to service short back end interrupt
|
|
|
|
* processing that never suspends. Suspending the high priority work queue
|
|
|
|
* may bring the system to its knees!
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define SLIPWORK LPWORK
|
|
|
|
|
|
|
|
/* CONFIG_NET_SLIP_NINTERFACES determines the number of
|
|
|
|
* physical interfaces that will be supported.
|
2011-03-12 04:42:08 +01:00
|
|
|
*/
|
|
|
|
|
2015-08-06 23:08:15 +02:00
|
|
|
#ifndef CONFIG_NET_SLIP_NINTERFACES
|
2023-05-20 00:32:34 +02:00
|
|
|
# define CONFIG_NET_SLIP_NINTERFACES 1
|
2011-03-12 04:42:08 +01:00
|
|
|
#endif
|
|
|
|
|
2020-03-31 16:05:48 +02:00
|
|
|
/* SLIP special character codes ********************************************/
|
2011-03-12 04:42:08 +01:00
|
|
|
|
2011-03-16 02:37:40 +01:00
|
|
|
#define SLIP_END 0300 /* Indicates end of packet */
|
|
|
|
#define SLIP_ESC 0333 /* Indicates byte stuffing */
|
|
|
|
#define SLIP_ESC_END 0334 /* ESC ESC_END means SLIP_END data byte */
|
|
|
|
#define SLIP_ESC_ESC 0335 /* ESC ESC_ESC means ESC data byte */
|
2011-03-12 04:42:08 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* The slip_driver_s encapsulates all state information for a single hardware
|
|
|
|
* interface
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct slip_driver_s
|
|
|
|
{
|
2023-05-25 18:12:53 +02:00
|
|
|
bool bifup; /* true:ifup false:ifdown */
|
|
|
|
struct work_s irqwork; /* For deferring interrupt work */
|
|
|
|
struct work_s pollwork; /* For deferring poll work to the work queue */
|
|
|
|
struct file tty; /* TTY file */
|
|
|
|
struct pollfd pollfd; /* Polling TTY for read- or writeable */
|
|
|
|
|
|
|
|
uint8_t rxbuf[2 * CONFIG_NET_SLIP_PKTSIZE + 2];
|
|
|
|
size_t rxlen;
|
|
|
|
|
|
|
|
uint8_t txbuf[2 * CONFIG_NET_SLIP_PKTSIZE + 2];
|
|
|
|
size_t txlen;
|
|
|
|
size_t txsent;
|
2011-03-12 04:42:08 +01:00
|
|
|
|
2016-05-30 17:37:34 +02:00
|
|
|
/* This holds the information visible to the NuttX network */
|
2011-03-12 04:42:08 +01:00
|
|
|
|
2016-05-30 17:37:34 +02:00
|
|
|
struct net_driver_s dev; /* Interface understood by the network */
|
2011-03-12 04:42:08 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
/* Driver state structure */
|
2011-03-12 04:42:08 +01:00
|
|
|
|
2015-08-06 23:08:15 +02:00
|
|
|
static struct slip_driver_s g_slip[CONFIG_NET_SLIP_NINTERFACES];
|
2011-03-12 04:42:08 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Common TX logic */
|
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
static void slip_transmit(FAR struct slip_driver_s *self);
|
|
|
|
static int slip_txpoll(FAR struct net_driver_s *dev);
|
|
|
|
|
|
|
|
/* Interrupt handling */
|
2011-03-12 04:42:08 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
static void slip_reply(FAR struct slip_driver_s *self);
|
|
|
|
static void slip_receive(FAR struct slip_driver_s *self);
|
|
|
|
static void slip_txdone(FAR struct slip_driver_s *self);
|
2011-03-12 04:42:08 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
static void slip_interrupt_work(FAR void *arg);
|
|
|
|
static void slip_pollfd_cb(FAR struct pollfd *pollfd);
|
|
|
|
static void slip_set_pollfd_events(FAR struct slip_driver_s *self,
|
|
|
|
short events);
|
2011-03-12 04:42:08 +01:00
|
|
|
|
|
|
|
/* NuttX callback functions */
|
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
static int slip_ifup(FAR struct net_driver_s *dev);
|
|
|
|
static int slip_ifdown(FAR struct net_driver_s *dev);
|
|
|
|
|
|
|
|
static void slip_txavail_work(FAR void *arg);
|
|
|
|
static int slip_txavail(FAR struct net_driver_s *dev);
|
2011-03-12 04:42:08 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2023-05-25 18:12:53 +02:00
|
|
|
* Name: slip_pollfd_cb
|
2011-03-12 04:42:08 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2023-05-25 18:12:53 +02:00
|
|
|
* TTY reports to be read- or writable
|
2011-03-12 04:42:08 +01:00
|
|
|
*
|
2015-08-06 22:32:12 +02:00
|
|
|
* Input Parameters:
|
2023-05-25 18:12:53 +02:00
|
|
|
* pollfd - Information about the event that happened.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* OK on success
|
2011-03-12 04:42:08 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
static void slip_pollfd_cb(FAR struct pollfd *pollfd)
|
2011-03-12 04:42:08 +01:00
|
|
|
{
|
2023-05-25 18:12:53 +02:00
|
|
|
FAR struct slip_driver_s *self = (FAR struct slip_driver_s *)pollfd->arg;
|
|
|
|
|
|
|
|
DEBUGASSERT(self != NULL);
|
|
|
|
|
|
|
|
/* Schedule to perform the processing on the worker thread. */
|
2011-03-15 11:40:39 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
if (work_available(&self->irqwork))
|
2011-03-15 11:40:39 +01:00
|
|
|
{
|
2023-05-25 18:12:53 +02:00
|
|
|
work_queue(SLIPWORK, &self->irqwork, slip_interrupt_work, self, 0);
|
2011-03-15 11:40:39 +01:00
|
|
|
}
|
2011-03-12 04:42:08 +01:00
|
|
|
}
|
|
|
|
|
2023-01-28 04:58:58 +01:00
|
|
|
/****************************************************************************
|
2023-05-25 18:12:53 +02:00
|
|
|
* Name: slip_set_pollfd_events
|
2023-01-28 04:58:58 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2023-05-25 18:12:53 +02:00
|
|
|
* Setup TTY to report poll events (such as POLLIN and POLLOUT)
|
2023-01-28 04:58:58 +01:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
2023-05-25 18:12:53 +02:00
|
|
|
* self - The SLIP interface to register for poll events
|
|
|
|
* events - The poll events to request reporting for
|
2023-01-28 04:58:58 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
static void slip_set_pollfd_events(FAR struct slip_driver_s *self,
|
|
|
|
short events)
|
2023-01-28 04:58:58 +01:00
|
|
|
{
|
2023-05-25 18:12:53 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Teardown any potentially pending poll, if applicable */
|
|
|
|
|
|
|
|
if (self->pollfd.events != 0)
|
2023-01-28 04:58:58 +01:00
|
|
|
{
|
2023-05-25 18:12:53 +02:00
|
|
|
ret = file_poll(&self->tty, &self->pollfd, false);
|
2023-01-28 04:58:58 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
nerr("file_poll(false) failed: %d\n", ret);
|
|
|
|
}
|
2023-01-28 04:58:58 +01:00
|
|
|
}
|
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
memset(&self->pollfd, 0, sizeof(self->pollfd));
|
2011-03-12 04:42:08 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
/* Setup requested poll, if applicable */
|
|
|
|
|
|
|
|
if (events != 0)
|
|
|
|
{
|
|
|
|
self->pollfd.arg = self;
|
|
|
|
self->pollfd.cb = slip_pollfd_cb;
|
|
|
|
self->pollfd.events = events;
|
|
|
|
self->pollfd.revents = 0;
|
|
|
|
self->pollfd.priv = NULL;
|
|
|
|
|
|
|
|
ret = file_poll(&self->tty, &self->pollfd, true);
|
|
|
|
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
nerr("file_poll(true) failed: %d\n", ret);
|
|
|
|
}
|
|
|
|
}
|
2011-03-12 04:42:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: slip_transmit
|
2011-03-12 04:42:08 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Start hardware transmission. Called either from the txdone interrupt
|
|
|
|
* handling or from watchdog based polling.
|
|
|
|
*
|
2015-08-06 22:32:12 +02:00
|
|
|
* Input Parameters:
|
2023-05-25 18:12:53 +02:00
|
|
|
* self - Reference to the driver state structure
|
2011-03-12 04:42:08 +01:00
|
|
|
*
|
2023-05-25 18:12:53 +02:00
|
|
|
* Assumptions:
|
|
|
|
* The network is locked.
|
2011-03-12 04:42:08 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
static void slip_transmit(FAR struct slip_driver_s *self)
|
2011-03-12 04:42:08 +01:00
|
|
|
{
|
2023-05-25 18:12:53 +02:00
|
|
|
ssize_t ssz;
|
|
|
|
uint8_t *p;
|
2011-03-12 04:42:08 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
DEBUGASSERT(self->dev.d_len > 0);
|
|
|
|
|
|
|
|
/* Verify that the hardware is ready to send another packet. If we get
|
|
|
|
* here, then we are committed to sending a packet; Higher level logic
|
|
|
|
* must have assured that there is no transmission in progress.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (self->txlen > 0)
|
|
|
|
{
|
|
|
|
/* Transmission of previous packet is still pending. This might happen
|
|
|
|
* in the 'slip_receive' -> 'slip_reply' -> 'slip_transmit' case. Try
|
|
|
|
* to forward pending packet into UART's transmit buffer. Timeout on
|
|
|
|
* packet if not forwarded within a second.
|
|
|
|
*/
|
2011-03-12 04:42:08 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
for (int i = 0; (i < 10) && (self->txsent != self->txlen); )
|
|
|
|
{
|
|
|
|
ssz = file_write(&self->tty,
|
|
|
|
&self->txbuf[self->txsent],
|
|
|
|
self->txlen - self->txsent);
|
|
|
|
if (ssz <= 0)
|
|
|
|
{
|
|
|
|
nxsig_usleep(10000);
|
|
|
|
i++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
self->txsent += ssz;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self->txsent == self->txlen)
|
|
|
|
{
|
|
|
|
slip_txdone(self);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NETDEV_TXTIMEOUTS(&self->dev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self->txlen = 0;
|
|
|
|
self->txsent = 0;
|
2011-03-12 04:42:08 +01:00
|
|
|
|
|
|
|
/* Send an initial END character to flush out any data that may have
|
|
|
|
* accumulated in the receiver due to line noise
|
|
|
|
*/
|
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
self->txbuf[self->txlen++] = SLIP_END;
|
2011-03-12 04:42:08 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
/* Now copy the I/O buffer into self->txbuf */
|
2011-03-12 04:42:08 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
for (unsigned int bytesread = 0; bytesread < self->dev.d_len; )
|
2011-03-12 04:42:08 +01:00
|
|
|
{
|
2023-05-25 18:12:53 +02:00
|
|
|
unsigned int chunk_sz = sizeof(self->txbuf) - self->txlen;
|
|
|
|
int copied;
|
|
|
|
|
|
|
|
if (self->dev.d_len - bytesread < chunk_sz)
|
2011-03-12 04:42:08 +01:00
|
|
|
{
|
2023-05-25 18:12:53 +02:00
|
|
|
chunk_sz = self->dev.d_len - bytesread;
|
|
|
|
}
|
2011-03-12 04:42:08 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
copied = iob_copyout(&self->txbuf[self->txlen],
|
|
|
|
self->dev.d_iob,
|
|
|
|
chunk_sz,
|
|
|
|
bytesread);
|
|
|
|
if (copied <= 0)
|
|
|
|
{
|
|
|
|
goto error;
|
|
|
|
}
|
2011-03-12 04:42:08 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
bytesread += (unsigned int)copied;
|
|
|
|
self->txlen += (size_t)copied;
|
|
|
|
}
|
2011-03-12 04:42:08 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
/* SLIP encode self->txbuf. First escape the ESC bytes. */
|
2011-03-12 04:42:08 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
for (p = memchr(&self->txbuf[1], SLIP_ESC, self->txlen - 1);
|
|
|
|
p != NULL;
|
|
|
|
p = memchr(p, SLIP_ESC, self->txlen - 1))
|
|
|
|
{
|
|
|
|
ssize_t postfix_len = self->txlen - (p - self->txbuf) - 1;
|
2011-03-12 04:42:08 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
if (self->txlen >= sizeof(self->txbuf))
|
|
|
|
{
|
|
|
|
goto error;
|
|
|
|
}
|
2011-03-12 04:42:08 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
if (postfix_len > 0)
|
|
|
|
{
|
|
|
|
memmove(p + 2, p + 1, postfix_len);
|
|
|
|
}
|
2011-03-12 04:42:08 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
p++;
|
|
|
|
*p = SLIP_ESC_ESC;
|
|
|
|
self->txlen++;
|
|
|
|
}
|
2011-03-12 04:42:08 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
/* SLIP encode self->txbuf. Then escape the END bytes. */
|
2011-03-12 04:42:08 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
for (p = memchr(&self->txbuf[1], SLIP_END, self->txlen - 1);
|
|
|
|
p != NULL;
|
|
|
|
p = memchr(p, SLIP_END, self->txlen - 1))
|
|
|
|
{
|
|
|
|
ssize_t postfix_len = self->txlen - (p - self->txbuf) - 1;
|
2011-03-12 04:42:08 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
if (self->txlen >= sizeof(self->txbuf))
|
|
|
|
{
|
|
|
|
goto error;
|
|
|
|
}
|
2011-03-12 04:42:08 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
if (postfix_len > 0)
|
|
|
|
{
|
|
|
|
memmove(p + 2, p + 1, postfix_len);
|
2011-03-12 04:42:08 +01:00
|
|
|
}
|
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
*p = SLIP_ESC;
|
|
|
|
p++;
|
|
|
|
*p = SLIP_ESC_END;
|
|
|
|
self->txlen++;
|
|
|
|
}
|
2011-03-12 04:42:08 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
/* Append the END token */
|
|
|
|
|
|
|
|
if (self->txlen >= sizeof(self->txbuf))
|
|
|
|
{
|
|
|
|
goto error;
|
2011-03-12 04:42:08 +01:00
|
|
|
}
|
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
self->txbuf[self->txlen++] = SLIP_END;
|
|
|
|
|
|
|
|
/* Increment statistics */
|
|
|
|
|
|
|
|
NETDEV_TXPACKETS(&self->dev);
|
|
|
|
|
|
|
|
/* Try to send packet */
|
2011-03-12 04:42:08 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
ssz = file_write(&self->tty, self->txbuf, self->txlen);
|
|
|
|
|
|
|
|
if (ssz > 0)
|
|
|
|
{
|
|
|
|
self->txsent = (size_t)ssz;
|
|
|
|
}
|
|
|
|
else
|
2011-03-12 04:42:08 +01:00
|
|
|
{
|
2023-05-25 18:12:53 +02:00
|
|
|
self->txsent = 0;
|
2011-03-12 04:42:08 +01:00
|
|
|
}
|
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
if (self->txsent == self->txlen)
|
|
|
|
{
|
|
|
|
/* Complete packet went out at first try. */
|
2011-03-12 04:42:08 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
slip_txdone(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
error:
|
|
|
|
|
|
|
|
/* Drop the packet and reset the receiver logic. */
|
|
|
|
|
|
|
|
self->txlen = 0;
|
|
|
|
self->txsent = 0;
|
|
|
|
|
|
|
|
NETDEV_TXERRORS(&self->dev);
|
2011-03-12 04:42:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: slip_txpoll
|
2011-03-12 04:42:08 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2023-05-25 18:12:53 +02:00
|
|
|
* The transmitter is available, check if the network has any outgoing
|
|
|
|
* packets ready to send. This is a callback from devif_poll().
|
|
|
|
* devif_poll() may be called:
|
2011-03-12 04:42:08 +01:00
|
|
|
*
|
2023-05-25 18:12:53 +02:00
|
|
|
* 1. When the preceding TX packet send is complete,
|
|
|
|
* 2. When the preceding TX packet send timesout and the interface is reset
|
|
|
|
* 3. During normal TX polling
|
2011-03-12 04:42:08 +01:00
|
|
|
*
|
2015-08-06 22:32:12 +02:00
|
|
|
* Input Parameters:
|
2023-05-25 18:12:53 +02:00
|
|
|
* dev - Reference to the NuttX driver state structure
|
2011-03-12 04:42:08 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* OK on success; a negated errno on failure
|
|
|
|
*
|
|
|
|
* Assumptions:
|
2023-05-25 18:12:53 +02:00
|
|
|
* The network is locked.
|
2011-03-12 04:42:08 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-08-16 18:23:17 +02:00
|
|
|
static int slip_txpoll(FAR struct net_driver_s *dev)
|
2011-03-12 04:42:08 +01:00
|
|
|
{
|
2023-05-25 18:12:53 +02:00
|
|
|
FAR struct slip_driver_s *self =
|
|
|
|
(FAR struct slip_driver_s *)dev->d_private;
|
|
|
|
|
|
|
|
/* Send the packet */
|
2011-03-12 04:42:08 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
slip_transmit(self);
|
2011-03-12 04:42:08 +01:00
|
|
|
|
2020-03-31 16:05:48 +02:00
|
|
|
/* If zero is returned, the polling will continue until all connections
|
2023-05-25 18:12:53 +02:00
|
|
|
* have been examined. We return -EBUSY if there is still transmission
|
|
|
|
* data pending in the TTY's buffer.
|
2011-03-12 04:42:08 +01:00
|
|
|
*/
|
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
return (self->txlen > 0) ? -EBUSY : OK;
|
2011-03-12 04:42:08 +01:00
|
|
|
}
|
|
|
|
|
2011-03-15 11:40:39 +01:00
|
|
|
/****************************************************************************
|
2023-05-25 18:12:53 +02:00
|
|
|
* Name: slip_reply
|
2011-03-15 11:40:39 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2023-05-25 18:12:53 +02:00
|
|
|
* After a packet has been received and dispatched to the network, it
|
|
|
|
* may return return with an outgoing packet. This function checks for
|
|
|
|
* that case and performs the transmission if necessary.
|
2011-03-15 11:40:39 +01:00
|
|
|
*
|
2015-08-06 22:32:12 +02:00
|
|
|
* Input Parameters:
|
2023-05-25 18:12:53 +02:00
|
|
|
* self - Reference to the driver state structure
|
2011-03-15 11:40:39 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
2023-05-25 18:12:53 +02:00
|
|
|
* Assumptions:
|
|
|
|
* The network is locked.
|
|
|
|
*
|
2011-03-15 11:40:39 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
static void slip_reply(struct slip_driver_s *self)
|
2011-03-15 11:40:39 +01:00
|
|
|
{
|
2023-05-25 18:12:53 +02:00
|
|
|
/* If the packet dispatch resulted in data that should be sent out on the
|
|
|
|
* network, the field d_len will set to a value > 0.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (self->dev.d_len > 0)
|
|
|
|
{
|
|
|
|
/* And send the packet */
|
|
|
|
|
|
|
|
slip_transmit(self);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: slip_receive
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* An interrupt was received indicating the availability of a new RX packet
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* self - Reference to the driver state structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* The network is locked.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void slip_receive(FAR struct slip_driver_s *self)
|
|
|
|
{
|
|
|
|
FAR struct net_driver_s *dev = &self->dev;
|
|
|
|
FAR struct iob_s *iob;
|
|
|
|
FAR uint8_t *p;
|
|
|
|
FAR uint8_t *pend;
|
|
|
|
size_t remaining;
|
|
|
|
size_t copied;
|
2020-03-31 16:05:48 +02:00
|
|
|
int ret;
|
2011-03-15 21:26:34 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
/* Drop potential prefix SLIP_ENDs */
|
2011-03-15 11:40:39 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
while ((self->rxbuf[0] == SLIP_END) && (self->rxlen > 0))
|
|
|
|
{
|
|
|
|
self->rxlen--;
|
|
|
|
memmove(&self->rxbuf[0], &self->rxbuf[1], self->rxlen);
|
|
|
|
}
|
2011-03-15 11:40:39 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
/* Find end of packet */
|
2011-03-15 11:40:39 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
pend = memchr(self->rxbuf, SLIP_END, self->rxlen);
|
2011-03-15 11:40:39 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
if (pend == NULL)
|
2011-03-16 02:37:40 +01:00
|
|
|
{
|
2023-05-25 18:12:53 +02:00
|
|
|
/* No complete packet present. Let's wait for more bytes to arrive. */
|
2011-03-16 02:37:40 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
if (self->rxlen == sizeof(self->rxbuf))
|
2020-03-31 16:05:48 +02:00
|
|
|
{
|
2023-05-25 18:12:53 +02:00
|
|
|
/* Purge receive buffer overflow due to overflow. */
|
|
|
|
|
|
|
|
NETDEV_RXERRORS(&self->dev);
|
|
|
|
self->rxlen = 0;
|
2020-03-31 16:05:48 +02:00
|
|
|
}
|
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = self->rxbuf;
|
|
|
|
remaining = pend - p;
|
|
|
|
copied = 0;
|
|
|
|
iob = iob_alloc(false);
|
|
|
|
iob_reserve(iob, CONFIG_NET_LL_GUARDSIZE);
|
|
|
|
|
|
|
|
while (remaining)
|
|
|
|
{
|
|
|
|
uint8_t *pesc = memchr(p, SLIP_ESC, remaining);
|
|
|
|
|
|
|
|
if (pesc != NULL)
|
2015-01-30 15:09:38 +01:00
|
|
|
{
|
2023-05-25 18:12:53 +02:00
|
|
|
unsigned int prefix_len = (unsigned int)(pesc - p);
|
|
|
|
|
|
|
|
if (prefix_len > 0)
|
|
|
|
{
|
|
|
|
ret = iob_copyin(iob, p, prefix_len, copied, false);
|
|
|
|
|
|
|
|
DEBUGASSERT(ret >= 0);
|
|
|
|
DEBUGASSERT((unsigned int)ret == prefix_len);
|
|
|
|
|
|
|
|
copied += prefix_len;
|
|
|
|
remaining -= prefix_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = pesc + 1;
|
|
|
|
remaining--;
|
|
|
|
|
|
|
|
switch (*p)
|
|
|
|
{
|
|
|
|
case SLIP_ESC_END:
|
|
|
|
*p = SLIP_END;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SLIP_ESC_ESC:
|
|
|
|
*p = SLIP_ESC;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
/* SLIP protocol error */
|
|
|
|
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = iob_copyin(iob, p, 1, copied, false);
|
|
|
|
DEBUGASSERT(ret == 1);
|
|
|
|
p++;
|
|
|
|
copied++;
|
|
|
|
remaining--;
|
2015-01-30 15:09:38 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-05-25 18:12:53 +02:00
|
|
|
ret = iob_copyin(iob, p, remaining, copied, false);
|
|
|
|
DEBUGASSERT(ret >= 0);
|
|
|
|
DEBUGASSERT((unsigned int)ret == remaining);
|
|
|
|
p += remaining;
|
|
|
|
copied += remaining;
|
|
|
|
remaining = 0;
|
2015-01-30 15:09:38 +01:00
|
|
|
}
|
2023-05-25 18:12:53 +02:00
|
|
|
}
|
2011-03-16 02:37:40 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
/* Move remaining bytes in rxbuf to the front */
|
2011-03-16 02:37:40 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
DEBUGASSERT((pend - self->rxbuf) <= self->rxlen);
|
|
|
|
self->rxlen -= (pend - self->rxbuf);
|
|
|
|
memmove(self->rxbuf, pend, self->rxlen);
|
2011-03-16 02:37:40 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
/* Handle the IP input. */
|
2015-01-30 15:09:38 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
netdev_iob_replace(&self->dev, iob);
|
|
|
|
iob = NULL;
|
2015-08-02 15:20:36 +02:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
NETDEV_RXPACKETS(&self->dev);
|
2015-01-30 15:09:38 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
/* All packets are assumed to be IP packets (we don't have a choice..
|
|
|
|
* there is no Ethernet header containing the EtherType). So pass the
|
|
|
|
* received packet on for IP processing -- but only if it is big
|
|
|
|
* enough to hold an IP header.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((IPv4BUF->vhl & IP_VERSION_MASK) == IPv4_VERSION)
|
|
|
|
{
|
|
|
|
NETDEV_RXIPV4(&self->dev);
|
|
|
|
|
|
|
|
ipv4_input(&self->dev);
|
|
|
|
|
|
|
|
slip_reply(self);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NETDEV_RXDROPPED(&self->dev);
|
2011-03-16 02:37:40 +01:00
|
|
|
}
|
2020-02-02 08:21:03 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
return;
|
2011-03-15 11:40:39 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
error:
|
2011-03-12 04:42:08 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
NETDEV_RXERRORS(&self->dev);
|
2011-03-15 11:40:39 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
if (iob)
|
2011-03-15 11:40:39 +01:00
|
|
|
{
|
2023-05-25 18:12:53 +02:00
|
|
|
iob_free_chain(iob);
|
|
|
|
iob = NULL;
|
2011-03-15 11:40:39 +01:00
|
|
|
}
|
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
/* Move remaining bytes in rxbuf to the front */
|
|
|
|
|
|
|
|
DEBUGASSERT((pend - self->rxbuf) <= self->rxlen);
|
|
|
|
self->rxlen -= (pend - self->rxbuf);
|
|
|
|
memmove(self->rxbuf, pend, self->rxlen);
|
2011-03-12 04:42:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2023-05-25 18:12:53 +02:00
|
|
|
* Name: slip_txdone
|
2011-03-12 04:42:08 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2023-05-25 18:12:53 +02:00
|
|
|
* An interrupt was received indicating that the last TX packet(s) is done
|
2011-03-12 04:42:08 +01:00
|
|
|
*
|
2015-08-06 22:32:12 +02:00
|
|
|
* Input Parameters:
|
2023-05-25 18:12:53 +02:00
|
|
|
* self - Reference to the driver state structure
|
2011-03-12 04:42:08 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
2023-05-25 18:12:53 +02:00
|
|
|
* Assumptions:
|
|
|
|
* The network is locked.
|
|
|
|
*
|
2011-03-12 04:42:08 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
static void slip_txdone(FAR struct slip_driver_s *self)
|
2011-03-12 04:42:08 +01:00
|
|
|
{
|
2023-05-25 18:12:53 +02:00
|
|
|
/* Update statistics */
|
2011-03-12 04:42:08 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
self->txlen = 0;
|
|
|
|
self->txsent = 0;
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
NETDEV_TXDONE(&self->dev);
|
2015-01-30 15:09:38 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
/* Poll the network for new TX data */
|
2015-01-30 15:09:38 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
if (work_available(&self->pollwork))
|
|
|
|
{
|
|
|
|
work_queue(SLIPWORK, &self->pollwork, slip_txavail_work, self, 0);
|
2011-03-12 04:42:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2023-05-25 18:12:53 +02:00
|
|
|
* Name: slip_interrupt_work
|
2011-03-12 04:42:08 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2023-05-25 18:12:53 +02:00
|
|
|
* Perform interrupt related work from the worker thread
|
2011-03-12 04:42:08 +01:00
|
|
|
*
|
2015-08-06 22:32:12 +02:00
|
|
|
* Input Parameters:
|
2023-05-25 18:12:53 +02:00
|
|
|
* arg - The argument passed when work_queue() was called.
|
2011-03-12 04:42:08 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2023-05-25 18:12:53 +02:00
|
|
|
* OK on success
|
2011-03-12 04:42:08 +01:00
|
|
|
*
|
|
|
|
* Assumptions:
|
2023-05-25 18:12:53 +02:00
|
|
|
* Runs on a worker thread.
|
2011-03-12 04:42:08 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
static void slip_interrupt_work(FAR void *arg)
|
2011-03-12 04:42:08 +01:00
|
|
|
{
|
2023-05-25 18:12:53 +02:00
|
|
|
FAR struct slip_driver_s *self = (FAR struct slip_driver_s *)arg;
|
|
|
|
ssize_t ssz;
|
2011-03-12 04:42:08 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
if (!self->bifup)
|
2011-03-12 04:42:08 +01:00
|
|
|
{
|
2023-05-25 18:12:53 +02:00
|
|
|
return;
|
|
|
|
}
|
2011-03-16 02:37:40 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
/* Lock the network and serialize driver operations if necessary.
|
|
|
|
* NOTE: Serialization is only required in the case where the driver work
|
|
|
|
* is performed on an LP worker thread and where more than one LP worker
|
|
|
|
* thread has been configured.
|
|
|
|
*/
|
2011-03-16 02:37:40 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
net_lock();
|
2023-01-28 04:58:58 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
/* Process pending Ethernet interrupts */
|
2023-01-28 04:58:58 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
/* Get and clear interrupt status bits */
|
2023-01-28 04:58:58 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
/* Handle interrupts according to status bit settings */
|
2011-03-12 04:42:08 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
if (self->rxlen < sizeof(self->rxbuf))
|
|
|
|
{
|
|
|
|
ssz = file_read(&self->tty,
|
|
|
|
&self->rxbuf[self->rxlen],
|
|
|
|
sizeof(self->rxbuf) - self->rxlen);
|
|
|
|
if (ssz > 0)
|
2011-03-12 04:42:08 +01:00
|
|
|
{
|
2023-05-25 18:12:53 +02:00
|
|
|
self->rxlen += (size_t)ssz;
|
2011-03-13 16:12:31 +01:00
|
|
|
}
|
2023-05-25 18:12:53 +02:00
|
|
|
}
|
2011-03-13 16:12:31 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
if (self->txsent < self->txlen)
|
|
|
|
{
|
|
|
|
ssz = file_write(&self->tty,
|
|
|
|
&self->txbuf[self->txsent],
|
|
|
|
self->txlen - self->txsent);
|
|
|
|
if (ssz > 0)
|
2011-03-13 16:12:31 +01:00
|
|
|
{
|
2023-05-25 18:12:53 +02:00
|
|
|
self->txsent += (size_t)ssz;
|
2011-03-13 16:12:31 +01:00
|
|
|
}
|
2023-05-25 18:12:53 +02:00
|
|
|
}
|
2011-03-13 16:12:31 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
/* Check if we received an incoming packet, if so, call slip_receive() */
|
2016-12-03 23:28:19 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
if (self->rxlen == sizeof(self->rxbuf) ||
|
|
|
|
memchr(self->rxbuf, SLIP_END, self->rxlen))
|
|
|
|
{
|
|
|
|
slip_receive(self);
|
|
|
|
}
|
2018-11-09 16:41:24 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
/* Check if a packet transmission just completed. If so, call skel_txdone.
|
|
|
|
* This may disable further Tx interrupts if there are no pending
|
|
|
|
* transmissions.
|
|
|
|
*/
|
2018-11-09 16:41:24 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
if (self->txlen > 0 && self->txsent == self->txlen)
|
|
|
|
{
|
|
|
|
slip_txdone(self);
|
2011-03-12 04:42:08 +01:00
|
|
|
}
|
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
net_unlock();
|
2011-03-12 04:42:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: slip_ifup
|
2011-03-12 04:42:08 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* NuttX Callback: Bring up the Ethernet interface when an IP address is
|
2014-04-13 22:32:20 +02:00
|
|
|
* provided
|
2011-03-12 04:42:08 +01:00
|
|
|
*
|
2015-08-06 22:32:12 +02:00
|
|
|
* Input Parameters:
|
2023-05-25 18:12:53 +02:00
|
|
|
* dev - Reference to the NuttX driver state structure
|
2011-03-12 04:42:08 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
2023-05-25 18:12:53 +02:00
|
|
|
* The network is locked.
|
2011-03-12 04:42:08 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-08-16 18:23:17 +02:00
|
|
|
static int slip_ifup(FAR struct net_driver_s *dev)
|
2011-03-12 04:42:08 +01:00
|
|
|
{
|
2023-05-25 18:12:53 +02:00
|
|
|
FAR struct slip_driver_s *self =
|
|
|
|
(FAR struct slip_driver_s *)dev->d_private;
|
2011-03-12 04:42:08 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
ninfo("Bringing up: %d.%d.%d.%d\n",
|
|
|
|
(int)dev->d_ipaddr & 0xff,
|
|
|
|
(int)(dev->d_ipaddr >> 8) & 0xff,
|
|
|
|
(int)(dev->d_ipaddr >> 16) & 0xff,
|
|
|
|
(int)dev->d_ipaddr >> 24);
|
|
|
|
|
|
|
|
/* Enable POLLIN and POLLOUT events on the TTY */
|
|
|
|
|
|
|
|
slip_set_pollfd_events(self, POLLIN | POLLOUT);
|
|
|
|
|
|
|
|
/* Mark the device "up" */
|
2011-03-12 04:42:08 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
self->bifup = true;
|
2011-03-12 04:42:08 +01:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: slip_ifdown
|
2011-03-12 04:42:08 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* NuttX Callback: Stop the interface.
|
|
|
|
*
|
2015-08-06 22:32:12 +02:00
|
|
|
* Input Parameters:
|
2023-05-25 18:12:53 +02:00
|
|
|
* dev - Reference to the NuttX driver state structure
|
2011-03-12 04:42:08 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
2023-05-25 18:12:53 +02:00
|
|
|
* The network is locked.
|
2011-03-12 04:42:08 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-08-16 18:23:17 +02:00
|
|
|
static int slip_ifdown(FAR struct net_driver_s *dev)
|
2011-03-12 04:42:08 +01:00
|
|
|
{
|
2023-05-25 18:12:53 +02:00
|
|
|
FAR struct slip_driver_s *self =
|
2020-03-31 16:05:48 +02:00
|
|
|
(FAR struct slip_driver_s *)dev->d_private;
|
2011-03-12 04:42:08 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
/* Disable the Ethernet interrupt */
|
|
|
|
|
|
|
|
slip_set_pollfd_events(self, 0);
|
2021-12-21 09:38:47 +01:00
|
|
|
|
2011-03-12 04:42:08 +01:00
|
|
|
/* Mark the device "down" */
|
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
self->bifup = false;
|
|
|
|
|
2011-03-12 04:42:08 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2023-05-25 18:12:53 +02:00
|
|
|
* Name: slip_txavail_work
|
2011-03-12 04:42:08 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2023-05-25 18:12:53 +02:00
|
|
|
* Perform an out-of-cycle poll on the worker thread.
|
2011-03-12 04:42:08 +01:00
|
|
|
*
|
2015-08-06 22:32:12 +02:00
|
|
|
* Input Parameters:
|
2023-05-25 18:12:53 +02:00
|
|
|
* arg - Reference to the NuttX driver state structure (cast to void*)
|
2011-03-12 04:42:08 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
2023-05-25 18:12:53 +02:00
|
|
|
* Assumptions:
|
|
|
|
* Runs on a work queue thread.
|
|
|
|
*
|
2011-03-12 04:42:08 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
static void slip_txavail_work(FAR void *arg)
|
2011-03-12 04:42:08 +01:00
|
|
|
{
|
2023-05-25 18:12:53 +02:00
|
|
|
FAR struct slip_driver_s *self = (FAR struct slip_driver_s *)arg;
|
|
|
|
|
|
|
|
/* Lock the network and serialize driver operations if necessary.
|
|
|
|
* NOTE: Serialization is only required in the case where the driver work
|
|
|
|
* is performed on an LP worker thread and where more than one LP worker
|
|
|
|
* thread has been configured.
|
|
|
|
*/
|
|
|
|
|
|
|
|
net_lock();
|
2011-03-12 04:42:08 +01:00
|
|
|
|
2011-03-16 02:37:40 +01:00
|
|
|
/* Ignore the notification if the interface is not yet up */
|
2011-03-12 04:42:08 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
if (self->bifup)
|
2011-03-12 04:42:08 +01:00
|
|
|
{
|
2023-05-25 18:12:53 +02:00
|
|
|
/* Check if there is room in the hardware to hold another packet. */
|
|
|
|
|
|
|
|
if (self->txlen == 0)
|
|
|
|
{
|
|
|
|
/* If so, then poll the network for new XMIT data */
|
2011-03-12 04:42:08 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
self->dev.d_buf = NULL;
|
|
|
|
|
|
|
|
devif_poll(&self->dev, slip_txpoll);
|
|
|
|
}
|
2011-03-12 04:42:08 +01:00
|
|
|
}
|
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
net_unlock();
|
2011-03-12 04:42:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2023-05-25 18:12:53 +02:00
|
|
|
* Name: slip_txavail
|
2011-03-12 04:42:08 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2023-05-25 18:12:53 +02:00
|
|
|
* Driver callback invoked when new TX data is available. This is a
|
|
|
|
* stimulus perform an out-of-cycle poll and, thereby, reduce the TX
|
|
|
|
* latency.
|
2011-03-12 04:42:08 +01:00
|
|
|
*
|
2015-08-06 22:32:12 +02:00
|
|
|
* Input Parameters:
|
2023-05-25 18:12:53 +02:00
|
|
|
* dev - Reference to the NuttX driver state structure
|
2011-03-12 04:42:08 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
2023-05-25 18:12:53 +02:00
|
|
|
* The network is locked.
|
2011-03-12 04:42:08 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
static int slip_txavail(FAR struct net_driver_s *dev)
|
2011-03-12 04:42:08 +01:00
|
|
|
{
|
2023-05-25 18:12:53 +02:00
|
|
|
FAR struct slip_driver_s *self =
|
|
|
|
(FAR struct slip_driver_s *)dev->d_private;
|
2011-03-12 04:42:08 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
/* Is our single work structure available? It may not be if there are
|
|
|
|
* pending interrupt actions and we will have to ignore the Tx
|
|
|
|
* availability action.
|
|
|
|
*/
|
2011-03-12 04:42:08 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
if (work_available(&self->pollwork))
|
|
|
|
{
|
|
|
|
/* Schedule to serialize the poll on the worker thread. */
|
2011-03-12 04:42:08 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
work_queue(SLIPWORK, &self->pollwork, slip_txavail_work, self, 0);
|
|
|
|
}
|
2011-03-12 04:42:08 +01:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: slip_initialize
|
2011-03-12 04:42:08 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Instantiate a SLIP network interface.
|
|
|
|
*
|
2015-08-06 22:32:12 +02:00
|
|
|
* Input Parameters:
|
|
|
|
* intf - In the case where there are multiple SLIP interfaces, this
|
|
|
|
* value identifies which is to be initialized. The number of
|
|
|
|
* possible SLIP interfaces is determined by
|
|
|
|
* devname - This is the path to the serial device that will support SLIP.
|
|
|
|
* For example, this might be "/dev/ttyS1"
|
2011-03-12 04:42:08 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* OK on success; Negated errno on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-08-16 18:23:17 +02:00
|
|
|
int slip_initialize(int intf, FAR const char *devname)
|
2011-03-12 04:42:08 +01:00
|
|
|
{
|
2023-05-25 18:12:53 +02:00
|
|
|
FAR struct slip_driver_s *self;
|
2020-02-02 08:21:03 +01:00
|
|
|
int ret;
|
2011-03-12 04:42:08 +01:00
|
|
|
|
|
|
|
/* Get the interface structure associated with this interface number. */
|
|
|
|
|
2015-08-06 23:08:15 +02:00
|
|
|
DEBUGASSERT(intf < CONFIG_NET_SLIP_NINTERFACES);
|
2023-05-25 18:12:53 +02:00
|
|
|
self = &g_slip[intf];
|
2011-03-12 04:42:08 +01:00
|
|
|
|
|
|
|
/* Initialize the driver structure */
|
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
memset(self, 0, sizeof(struct slip_driver_s));
|
|
|
|
self->dev.d_ifup = slip_ifup; /* I/F up (new IP address) callback */
|
|
|
|
self->dev.d_ifdown = slip_ifdown; /* I/F down callback */
|
|
|
|
self->dev.d_txavail = slip_txavail; /* New TX data callback */
|
|
|
|
self->dev.d_private = self; /* Used to recover SLIP I/F instance */
|
2011-03-12 04:42:08 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
ret = file_open(&self->tty, devname, O_RDWR | O_NONBLOCK);
|
2011-03-12 04:42:08 +01:00
|
|
|
|
2020-02-02 08:21:03 +01:00
|
|
|
if (ret < 0)
|
2011-03-12 04:42:08 +01:00
|
|
|
{
|
2020-02-02 08:21:03 +01:00
|
|
|
nerr("ERROR: Failed to open %s: %d\n", devname, ret);
|
|
|
|
return ret;
|
2011-03-12 04:42:08 +01:00
|
|
|
}
|
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
/* Put the interface in the down state. This usually amounts to resetting
|
|
|
|
* the device and/or calling slip_ifdown().
|
|
|
|
*/
|
2011-03-12 04:42:08 +01:00
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
slip_set_pollfd_events(self, 0);
|
|
|
|
self->bifup = false;
|
2011-03-12 04:42:08 +01:00
|
|
|
|
|
|
|
/* Register the device with the OS so that socket IOCTLs can be performed */
|
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
netdev_register(&self->dev, NET_LL_SLIP);
|
|
|
|
|
2011-03-12 04:42:08 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2023-05-25 18:12:53 +02:00
|
|
|
#endif /* !defined(CONFIG_SCHED_WORKQUEUE) */
|
|
|
|
|
|
|
|
#endif /* CONFIG_NET_SLIP */
|