2017-03-28 22:08:54 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* net/sixlowpan/sixlowpan_send.c
|
|
|
|
*
|
2020-04-12 18:46:40 +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
|
2017-03-28 22:08:54 +02:00
|
|
|
*
|
2020-04-12 18:46:40 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2017-03-28 22:08:54 +02:00
|
|
|
*
|
2020-04-12 18:46:40 +02: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-03-28 22:08:54 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2017-04-03 01:46:22 +02:00
|
|
|
#include <assert.h>
|
2017-03-28 22:08:54 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2017-04-01 17:07:16 +02:00
|
|
|
#include <nuttx/semaphore.h>
|
|
|
|
#include <nuttx/net/net.h>
|
2017-08-20 15:10:48 +02:00
|
|
|
#include <nuttx/net/radiodev.h>
|
2017-03-28 22:08:54 +02:00
|
|
|
|
|
|
|
#include "netdev/netdev.h"
|
2017-03-31 23:09:07 +02:00
|
|
|
#include "devif/devif.h"
|
2017-04-01 17:07:16 +02:00
|
|
|
|
2017-03-30 02:07:52 +02:00
|
|
|
#include "sixlowpan/sixlowpan_internal.h"
|
2017-03-28 22:08:54 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_6LOWPAN
|
|
|
|
|
2017-04-01 20:34:08 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* These are temporary stubs. Something like this would be needed to
|
|
|
|
* monitor the health of a IPv6 neighbor.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define neighbor_reachable(dev)
|
|
|
|
#define neighbor_notreachable(dev)
|
|
|
|
|
2017-03-31 23:09:07 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-06-23 22:09:06 +02:00
|
|
|
/* This is the state data provided to the send event handler. No actions
|
2017-03-31 23:09:07 +02:00
|
|
|
* can be taken until the until we receive the TX poll, then we can call
|
|
|
|
* sixlowpan_queue_frames() with this data strurcture.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct sixlowpan_send_s
|
|
|
|
{
|
|
|
|
FAR struct devif_callback_s *s_cb; /* Reference to callback instance */
|
|
|
|
sem_t s_waitsem; /* Supports waiting for driver events */
|
|
|
|
int s_result; /* The result of the transfer */
|
2017-06-25 00:00:41 +02:00
|
|
|
FAR const struct ipv6_hdr_s *s_ipv6hdr; /* IPv6 header, followed by UDP or ICMP header. */
|
2017-07-31 03:10:51 +02:00
|
|
|
FAR const struct netdev_varaddr_s *s_destmac; /* Destination MAC address */
|
2017-03-31 23:09:07 +02:00
|
|
|
FAR const void *s_buf; /* Data to send */
|
|
|
|
size_t s_len; /* Length of data in buf */
|
|
|
|
};
|
|
|
|
|
2017-03-28 22:08:54 +02:00
|
|
|
/****************************************************************************
|
2017-03-29 22:28:51 +02:00
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-03-31 23:09:07 +02:00
|
|
|
/****************************************************************************
|
2017-08-29 22:08:04 +02:00
|
|
|
* Name: send_eventhandler
|
2017-03-31 23:09:07 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2018-06-23 22:09:06 +02:00
|
|
|
* This function is called with the network locked to perform the actual
|
2017-03-31 23:09:07 +02:00
|
|
|
* send operation when polled by the lower, device interfacing layer.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Input Parameters:
|
2018-06-23 22:09:06 +02:00
|
|
|
* dev - The structure of the network driver that generated the event.
|
2017-03-31 23:09:07 +02:00
|
|
|
* conn - The connection structure associated with the socket
|
|
|
|
* flags - Set of events describing why the callback was invoked
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* The network is locked.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-08-29 22:08:04 +02:00
|
|
|
static uint16_t send_eventhandler(FAR struct net_driver_s *dev,
|
|
|
|
FAR void *pvconn,
|
|
|
|
FAR void *pvpriv, uint16_t flags)
|
2017-03-31 23:09:07 +02:00
|
|
|
{
|
|
|
|
FAR struct sixlowpan_send_s *sinfo = (FAR struct sixlowpan_send_s *)pvpriv;
|
|
|
|
|
|
|
|
ninfo("flags: %04x: %d\n", flags);
|
|
|
|
|
2017-07-29 21:42:26 +02:00
|
|
|
/* Verify that this is a compatible network driver. */
|
2017-04-15 16:53:42 +02:00
|
|
|
|
2017-07-29 21:42:26 +02:00
|
|
|
if (dev->d_lltype != NET_LL_IEEE802154 &&
|
|
|
|
dev->d_lltype != NET_LL_PKTRADIO)
|
2017-04-15 16:53:42 +02:00
|
|
|
{
|
2017-07-29 21:42:26 +02:00
|
|
|
ninfo("Not a compatible network device\n");
|
2017-04-15 16:53:42 +02:00
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* REVISIT: Verify that this is the correct IEEE802.15.4 network driver to
|
|
|
|
* route the outgoing frame(s). Chances are that there is only one
|
|
|
|
* IEEE802.15.4 network driver
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Check if the IEEE802.15.4 network driver went down */
|
2017-03-31 23:09:07 +02:00
|
|
|
|
|
|
|
if ((flags & NETDEV_DOWN) != 0)
|
|
|
|
{
|
2017-06-20 18:37:32 +02:00
|
|
|
nwarn("WARNING: Device is down\n");
|
2017-03-31 23:09:07 +02:00
|
|
|
sinfo->s_result = -ENOTCONN;
|
|
|
|
goto end_wait;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for a poll for TX data. */
|
|
|
|
|
|
|
|
if ((flags & WPAN_NEWDATA) == 0)
|
|
|
|
{
|
|
|
|
DEBUGASSERT((flags & WPAN_POLL) != 0);
|
|
|
|
|
2017-04-07 01:56:22 +02:00
|
|
|
/* Transfer the frame list to the IEEE802.15.4 MAC device */
|
2017-03-31 23:09:07 +02:00
|
|
|
|
2017-04-01 17:07:16 +02:00
|
|
|
sinfo->s_result =
|
2017-08-19 16:48:52 +02:00
|
|
|
sixlowpan_queue_frames((FAR struct radio_driver_s *)dev,
|
2017-04-07 02:11:04 +02:00
|
|
|
sinfo->s_ipv6hdr, sinfo->s_buf, sinfo->s_len,
|
|
|
|
sinfo->s_destmac);
|
2017-04-01 17:07:16 +02:00
|
|
|
|
2017-03-31 23:09:07 +02:00
|
|
|
flags &= ~WPAN_POLL;
|
2017-04-01 20:34:08 +02:00
|
|
|
neighbor_reachable(dev);
|
2017-03-31 23:09:07 +02:00
|
|
|
goto end_wait;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Continue waiting */
|
|
|
|
|
|
|
|
return flags;
|
|
|
|
|
|
|
|
end_wait:
|
2019-10-25 19:31:42 +02:00
|
|
|
|
2017-03-31 23:09:07 +02:00
|
|
|
/* Do not allow any further callbacks */
|
|
|
|
|
|
|
|
sinfo->s_cb->flags = 0;
|
|
|
|
sinfo->s_cb->priv = NULL;
|
|
|
|
sinfo->s_cb->event = NULL;
|
|
|
|
|
|
|
|
/* Wake up the waiting thread */
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&sinfo->s_waitsem);
|
2017-03-31 23:09:07 +02:00
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: sixlowpan_send
|
|
|
|
*
|
|
|
|
* Description:
|
2017-06-25 00:00:41 +02:00
|
|
|
* Process an outgoing UDP or ICMPv6 packet. Takes an IP packet and formats
|
2017-03-31 23:09:07 +02:00
|
|
|
* it to be sent on an 802.15.4 network using 6lowpan. Called from common
|
2017-06-25 00:00:41 +02:00
|
|
|
* UDP/ICMPv6 send logic.
|
2017-03-31 23:09:07 +02:00
|
|
|
*
|
2017-05-03 17:40:42 +02:00
|
|
|
* The payload data is in the caller 'buf' and is of length 'buflen'.
|
2017-03-31 23:09:07 +02:00
|
|
|
* Compressed headers will be added and if necessary the packet is
|
2017-05-03 17:40:42 +02:00
|
|
|
* fragmented. The resulting packet/fragments are submitted to the MAC
|
2017-07-29 21:42:26 +02:00
|
|
|
* via the network driver r_req_data method.
|
2017-03-31 23:09:07 +02:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dev - The IEEE802.15.4 MAC network driver interface.
|
2018-06-23 22:09:06 +02:00
|
|
|
* list - Head of callback list for send events
|
2017-06-25 00:00:41 +02:00
|
|
|
* ipv6hdr - IPv6 header followed by UDP or ICMPv6 header.
|
2017-03-31 23:09:07 +02:00
|
|
|
* buf - Data to send
|
|
|
|
* len - Length of data to send
|
2017-04-01 17:07:16 +02:00
|
|
|
* destmac - The IEEE802.15.4 MAC address of the destination
|
2020-01-04 11:37:46 +01:00
|
|
|
* timeout - Send timeout in milliseconds
|
2017-03-31 23:09:07 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2020-02-23 09:50:23 +01:00
|
|
|
* Ok is returned on success; Otherwise a negated errno value is returned.
|
2017-03-31 23:09:07 +02:00
|
|
|
* This function is expected to fail if the driver is not an IEEE802.15.4
|
2017-06-25 00:00:41 +02:00
|
|
|
* MAC network driver. In that case, the logic will fall back to normal
|
2017-03-31 23:09:07 +02:00
|
|
|
* IPv4/IPv6 formatting.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Called with the network locked.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int sixlowpan_send(FAR struct net_driver_s *dev,
|
2017-04-06 19:57:43 +02:00
|
|
|
FAR struct devif_callback_s **list,
|
2017-04-01 17:07:16 +02:00
|
|
|
FAR const struct ipv6_hdr_s *ipv6hdr, FAR const void *buf,
|
2017-07-31 03:10:51 +02:00
|
|
|
size_t len, FAR const struct netdev_varaddr_s *destmac,
|
2020-04-12 18:46:40 +02:00
|
|
|
unsigned int timeout)
|
2017-03-31 23:09:07 +02:00
|
|
|
{
|
|
|
|
struct sixlowpan_send_s sinfo;
|
|
|
|
|
2017-06-20 18:37:32 +02:00
|
|
|
ninfo("len=%lu timeout=%u\n", (unsigned long)len, timeout);
|
|
|
|
|
2017-03-31 23:09:07 +02:00
|
|
|
/* Initialize the send state structure */
|
|
|
|
|
2017-10-03 20:51:15 +02:00
|
|
|
nxsem_init(&sinfo.s_waitsem, 0, 0);
|
2020-01-02 17:49:34 +01:00
|
|
|
nxsem_setprotocol(&sinfo.s_waitsem, SEM_PRIO_NONE);
|
2017-03-31 23:09:07 +02:00
|
|
|
|
|
|
|
sinfo.s_result = -EBUSY;
|
2017-04-07 02:11:04 +02:00
|
|
|
sinfo.s_ipv6hdr = ipv6hdr;
|
2017-04-01 17:07:16 +02:00
|
|
|
sinfo.s_destmac = destmac;
|
2017-03-31 23:09:07 +02:00
|
|
|
sinfo.s_buf = buf;
|
|
|
|
sinfo.s_len = len;
|
|
|
|
|
|
|
|
net_lock();
|
|
|
|
if (len > 0)
|
|
|
|
{
|
2017-04-01 00:33:21 +02:00
|
|
|
/* Allocate resources to receive a callback.
|
|
|
|
*
|
|
|
|
* The second parameter is NULL meaning that we can get only
|
|
|
|
* device related events, no connect-related events.
|
2017-03-31 23:09:07 +02:00
|
|
|
*/
|
|
|
|
|
2017-06-20 18:37:32 +02:00
|
|
|
sinfo.s_cb = devif_callback_alloc(dev, list);
|
2017-03-31 23:09:07 +02:00
|
|
|
if (sinfo.s_cb != NULL)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Set up the callback in the connection */
|
|
|
|
|
2017-06-20 18:37:32 +02:00
|
|
|
sinfo.s_cb->flags = (NETDEV_DOWN | WPAN_POLL);
|
|
|
|
sinfo.s_cb->priv = (FAR void *)&sinfo;
|
2017-08-29 22:08:04 +02:00
|
|
|
sinfo.s_cb->event = send_eventhandler;
|
2017-03-31 23:09:07 +02:00
|
|
|
|
2017-05-11 21:35:56 +02:00
|
|
|
/* Notify the IEEE802.15.4 MAC that we have data to send. */
|
2017-03-31 23:09:07 +02:00
|
|
|
|
2017-04-01 00:33:21 +02:00
|
|
|
netdev_txnotify_dev(dev);
|
2017-03-31 23:09:07 +02:00
|
|
|
|
2018-06-23 22:09:06 +02:00
|
|
|
/* Wait for the send to complete or an error to occur.
|
2020-01-04 11:37:46 +01:00
|
|
|
* net_timedwait will also terminate if a signal is received.
|
2017-03-31 23:09:07 +02:00
|
|
|
*/
|
|
|
|
|
2017-06-20 18:37:32 +02:00
|
|
|
ninfo("Wait for send complete\n");
|
|
|
|
|
2020-01-04 11:37:46 +01:00
|
|
|
ret = net_timedwait(&sinfo.s_waitsem, timeout);
|
2017-03-31 23:09:07 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-01-04 11:37:46 +01:00
|
|
|
if (ret == -ETIMEDOUT)
|
|
|
|
{
|
|
|
|
neighbor_notreachable(dev);
|
|
|
|
}
|
2020-01-11 16:40:32 +01:00
|
|
|
|
2017-09-04 14:55:28 +02:00
|
|
|
sinfo.s_result = ret;
|
2017-03-31 23:09:07 +02:00
|
|
|
}
|
|
|
|
|
2018-06-23 22:09:06 +02:00
|
|
|
/* Make sure that no further events are processed */
|
2017-03-31 23:09:07 +02:00
|
|
|
|
2018-06-23 22:09:06 +02:00
|
|
|
devif_conn_callback_free(dev, sinfo.s_cb, list);
|
2017-03-31 23:09:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_destroy(&sinfo.s_waitsem);
|
2017-03-31 23:09:07 +02:00
|
|
|
net_unlock();
|
|
|
|
|
2017-04-01 00:33:21 +02:00
|
|
|
return (sinfo.s_result < 0 ? sinfo.s_result : len);
|
2017-03-31 23:09:07 +02:00
|
|
|
}
|
2017-03-28 22:08:54 +02:00
|
|
|
|
|
|
|
#endif /* CONFIG_NET_6LOWPAN */
|