2017-08-19 16:48:52 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* net/ieee802154/ieee802154_input.c
|
|
|
|
* Handle incoming IEEE 802.15.4 frame input
|
|
|
|
*
|
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
|
2017-08-19 16:48:52 +02:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2017-08-19 16:48:52 +02: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.
|
2017-08-19 16:48:52 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2017-08-20 02:50:50 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
2017-08-19 16:48:52 +02:00
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/mm/iob.h>
|
2017-08-20 15:10:48 +02:00
|
|
|
#include <nuttx/net/radiodev.h>
|
2017-08-19 16:48:52 +02:00
|
|
|
#include <nuttx/net/ieee802154.h>
|
|
|
|
|
|
|
|
#include "devif/devif.h"
|
|
|
|
#include "ieee802154/ieee802154.h"
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IEEE802154
|
|
|
|
|
2017-08-21 15:13:34 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-08-21 15:52:43 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: ieee802154_count_frames
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Return the number of frames in the RX queue.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-08-21 15:52:43 +02:00
|
|
|
* conn - The socket connection structure.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2017-08-21 15:52:43 +02:00
|
|
|
* The number of frames in the queue.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-03-29 08:00:56 +02:00
|
|
|
#if CONFIG_NET_IEEE802154_BACKLOG > 0
|
2017-08-21 16:18:53 +02:00
|
|
|
static int ieee802154_count_frames(FAR struct ieee802154_conn_s *conn)
|
2017-08-21 15:52:43 +02:00
|
|
|
{
|
|
|
|
FAR struct ieee802154_container_s *container;
|
|
|
|
int count;
|
|
|
|
|
|
|
|
for (count = 0, container = conn->rxhead;
|
|
|
|
container != NULL;
|
|
|
|
count++, container = container->ic_flink)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-08-21 15:13:34 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: ieee802154_queue_frame
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Add one frame to the connection's RX queue.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-08-21 15:13:34 +02:00
|
|
|
* conn - The socket connection structure.
|
|
|
|
* framel - A single frame to add to the RX queue.
|
2018-03-30 23:20:19 +02:00
|
|
|
* meta - Meta data characterizing the received frame.
|
2017-08-21 15:13:34 +02:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2017-08-21 15:13:34 +02:00
|
|
|
* Zero (OK) is returned on success; A negated errno value is returned on
|
|
|
|
* any failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-08-21 16:18:53 +02:00
|
|
|
static int ieee802154_queue_frame(FAR struct ieee802154_conn_s *conn,
|
|
|
|
FAR struct iob_s *frame,
|
|
|
|
FAR struct ieee802154_data_ind_s *meta)
|
2017-08-21 15:13:34 +02:00
|
|
|
{
|
|
|
|
FAR struct ieee802154_container_s *container;
|
|
|
|
|
|
|
|
/* Allocate a container for the frame */
|
|
|
|
|
|
|
|
container = ieee802154_container_allocate();
|
|
|
|
if (container == NULL)
|
|
|
|
{
|
|
|
|
nerr("ERROR: Failed to allocate a container\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the container */
|
|
|
|
|
|
|
|
memset(&container->ic_src, 0, sizeof(struct ieee802154_saddr_s));
|
|
|
|
|
|
|
|
DEBUGASSERT(meta->src.mode != IEEE802154_ADDRMODE_NONE);
|
|
|
|
container->ic_src.s_mode = meta->src.mode;
|
|
|
|
IEEE802154_PANIDCOPY(container->ic_src.s_panid, meta->src.panid);
|
|
|
|
|
|
|
|
if (meta->src.mode == IEEE802154_ADDRMODE_SHORT)
|
|
|
|
{
|
|
|
|
IEEE802154_SADDRCOPY(container->ic_src.s_saddr, meta->src.saddr);
|
|
|
|
}
|
|
|
|
else if (meta->src.mode == IEEE802154_ADDRMODE_EXTENDED)
|
|
|
|
{
|
|
|
|
IEEE802154_EADDRCOPY(container->ic_src.s_eaddr, meta->src.eaddr);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUGASSERT(frame != NULL);
|
|
|
|
container->ic_iob = frame;
|
|
|
|
|
|
|
|
/* Add the container to the tail of the list of incoming frames */
|
|
|
|
|
|
|
|
container->ic_flink = NULL;
|
|
|
|
if (conn->rxtail == NULL)
|
|
|
|
{
|
|
|
|
conn->rxhead = container;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
conn->rxtail->ic_flink = container;
|
|
|
|
}
|
|
|
|
|
2017-08-21 15:52:43 +02:00
|
|
|
#if CONFIG_NET_IEEE802154_BACKLOG > 0
|
2018-10-29 23:25:57 +01:00
|
|
|
/* If incrementing the count would exceed the maximum backlog value, then
|
|
|
|
* delete the oldest frame from the head of the RX queue.
|
|
|
|
*/
|
2017-08-21 15:52:43 +02:00
|
|
|
|
2019-07-03 02:02:23 +02:00
|
|
|
if (conn->backlog >= CONFIG_NET_IEEE802154_BACKLOG)
|
|
|
|
{
|
2017-08-21 16:18:53 +02:00
|
|
|
DEBUGASSERT(conn->backlog == CONFIG_NET_IEEE802154_BACKLOG);
|
|
|
|
|
2017-08-21 15:52:43 +02:00
|
|
|
/* Remove the container from the tail RX input queue. */
|
|
|
|
|
|
|
|
container = conn->rxhead;
|
|
|
|
DEBUGASSERT(container != NULL);
|
|
|
|
conn->rxhead = container->ic_flink;
|
|
|
|
container->ic_flink = NULL;
|
|
|
|
|
|
|
|
/* Did the RX queue become empty? */
|
|
|
|
|
|
|
|
if (conn->rxhead == NULL)
|
|
|
|
{
|
|
|
|
conn->rxtail = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUGASSERT(container != NULL && container->ic_iob != NULL);
|
|
|
|
|
|
|
|
/* Free both the IOB and the container */
|
|
|
|
|
2020-01-02 15:52:28 +01:00
|
|
|
iob_free(container->ic_iob, IOBUSER_NET_SOCK_IEEE802154);
|
|
|
|
ieee802154_container_free(container);
|
2019-07-03 02:02:23 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Increment the count of frames in the queue. */
|
2017-08-21 16:18:53 +02:00
|
|
|
|
2019-07-03 02:02:23 +02:00
|
|
|
conn->backlog++;
|
|
|
|
}
|
2017-08-21 16:18:53 +02:00
|
|
|
|
2019-07-03 02:02:23 +02:00
|
|
|
DEBUGASSERT((int)conn->backlog == ieee802154_count_frames(conn));
|
2017-08-21 15:52:43 +02:00
|
|
|
#endif
|
|
|
|
|
2017-08-21 15:13:34 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2017-08-19 16:48:52 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ieee802154_input
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle incoming IEEE 802.15.4 input
|
|
|
|
*
|
|
|
|
* This function is called when the radio device driver has received an
|
|
|
|
* frame from the network. The frame from the device driver must be
|
|
|
|
* provided in by the IOB frame argument of the function call:
|
|
|
|
*
|
|
|
|
* - The frame data is in the IOB io_data[] buffer,
|
|
|
|
* - The length of the frame is in the IOB io_len field, and
|
|
|
|
* - The offset past and radio MAC header is provided in the io_offset
|
|
|
|
* field.
|
|
|
|
*
|
|
|
|
* The frame argument may refer to a single frame (a list of length one)
|
|
|
|
* or may it be the head of a list of multiple frames.
|
|
|
|
*
|
|
|
|
* - The io_flink field points to the next frame in the list (if enable)
|
|
|
|
* - The last frame in the list will have io_flink == NULL.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-08-19 16:48:52 +02:00
|
|
|
* radio The radio network driver interface.
|
|
|
|
* framelist - The head of an incoming list of frames. Normally this
|
|
|
|
* would be a single frame. A list may be provided if
|
|
|
|
* appropriate, however.
|
2018-03-30 23:20:19 +02:00
|
|
|
* meta - Meta data characterizing the received frame.
|
2017-08-19 16:48:52 +02:00
|
|
|
*
|
2018-03-30 23:20:19 +02:00
|
|
|
* If there are multiple frames in the list, this metadata
|
2017-08-19 16:48:52 +02:00
|
|
|
* must apply to all of the frames in the list.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2017-08-19 16:48:52 +02:00
|
|
|
* OK The IEEE 802.15.4 has been processed and can be deleted
|
|
|
|
* ERROR Hold the IEEE 802.15.4 and try again later. There is a listening
|
|
|
|
* socket but no recv in place to catch the IEEE 802.15.4 yet.
|
|
|
|
* Useful when a packet arrives before a recv call is in place.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Called from the network diver with the network locked.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int ieee802154_input(FAR struct radio_driver_s *radio,
|
|
|
|
FAR struct iob_s *framelist,
|
2017-08-20 02:50:50 +02:00
|
|
|
FAR struct ieee802154_data_ind_s *meta)
|
2017-08-19 16:48:52 +02:00
|
|
|
{
|
|
|
|
FAR struct ieee802154_conn_s *conn;
|
2017-08-21 15:13:34 +02:00
|
|
|
FAR struct iob_s *frame;
|
|
|
|
FAR struct iob_s *next;
|
2017-08-19 16:48:52 +02:00
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
/* Check if there is a connection that will accept this packet */
|
|
|
|
|
2017-08-20 02:50:50 +02:00
|
|
|
conn = ieee802154_conn_active(meta);
|
|
|
|
if (conn != NULL)
|
2017-08-19 16:48:52 +02:00
|
|
|
{
|
|
|
|
/* Setup for the application callback (NOTE: These should not be
|
|
|
|
* used by PF_IEEE802154 sockets).
|
|
|
|
*/
|
|
|
|
|
|
|
|
radio->r_dev.d_appdata = radio->r_dev.d_buf;
|
|
|
|
radio->r_dev.d_len = 0;
|
|
|
|
radio->r_dev.d_sndlen = 0;
|
|
|
|
|
2017-08-21 15:13:34 +02:00
|
|
|
/* The framelist probably contains only a single frame, but we will
|
|
|
|
* process it as a list of frames.
|
|
|
|
*/
|
2017-08-20 02:50:50 +02:00
|
|
|
|
2017-08-21 15:13:34 +02:00
|
|
|
for (frame = framelist; frame != NULL; frame = next)
|
2017-08-20 02:50:50 +02:00
|
|
|
{
|
2017-08-21 15:13:34 +02:00
|
|
|
/* Remove the frame from the list */
|
2017-08-20 02:50:50 +02:00
|
|
|
|
2017-08-21 15:13:34 +02:00
|
|
|
next = frame->io_flink;
|
|
|
|
frame->io_flink = NULL;
|
2017-08-20 02:50:50 +02:00
|
|
|
|
2017-08-21 15:13:34 +02:00
|
|
|
/* Add the frame to the RX queue */
|
2017-08-20 02:50:50 +02:00
|
|
|
|
2017-08-21 15:13:34 +02:00
|
|
|
ret = ieee802154_queue_frame(conn, frame, meta);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nerr("ERROR: Failed to queue frame: %d\n", ret);
|
2019-08-17 00:42:25 +02:00
|
|
|
iob_free(frame, IOBUSER_NET_SOCK_IEEE802154);
|
2017-08-21 15:13:34 +02:00
|
|
|
}
|
2017-08-20 02:50:50 +02:00
|
|
|
}
|
2017-08-19 16:48:52 +02:00
|
|
|
|
2017-08-21 15:13:34 +02:00
|
|
|
/* Perform the application callback. The frame may be processed now
|
|
|
|
* if there is a user wait for an incoming frame. Or it may pend in
|
|
|
|
* the RX queue until some user process reads the frame. NOTE: The
|
|
|
|
* return value from ieee802154_callback would distinguish these
|
|
|
|
* cases: IEEE802154_NEWDATA will still be processed if the frame
|
|
|
|
* was not consumed.
|
2017-08-19 16:48:52 +02:00
|
|
|
*/
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
ieee802154_callback(radio, conn, IEEE802154_NEWDATA);
|
2017-08-19 16:48:52 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-08-21 15:13:34 +02:00
|
|
|
nwarn("WARNING: No listener\n");
|
2017-08-19 16:48:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_NET_IEEE802154 */
|