* wireless/ieee802154/mac802154_device.c
*
* SPDX-License-Identifier: Apache-2.0
*
* 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.
*
****************************************************************************/
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <debug.h>
#include <errno.h>
#include <time.h>
#include <fcntl.h>
#include <nuttx/arch.h>
#include <nuttx/mutex.h>
#include <nuttx/kmalloc.h>
#include <nuttx/signal.h>
#include <nuttx/mm/iob.h>
#include <nuttx/wireless/ieee802154/ieee802154_device.h>
#include <nuttx/wireless/ieee802154/ieee802154_mac.h>
#include "mac802154.h"
* Pre-processor Definitions
****************************************************************************/
#define DEVNAME_FMT "/dev/ieee%d"
#define DEVNAME_FMTLEN (9 + 3 + 1)
* Private Types
****************************************************************************/
struct mac802154dev_open_s
{
FAR struct mac802154dev_open_s *md_flink;
volatile bool md_closing;
};
struct mac802154dev_callback_s
{
struct mac802154_maccb_s mc_cb;
* the MAC layer
*/
FAR struct mac802154_chardevice_s *mc_priv;
};
struct mac802154_chardevice_s
{
MACHANDLE md_mac;
struct mac802154dev_callback_s md_cb;
mutex_t md_lock;
bool enableevents : 1;
bool geteventpending : 1;
sem_t geteventsem;
sq_queue_t primitive_queue;
* MAC device.
*/
FAR struct mac802154dev_open_s *md_open;
bool readpending;
sem_t readsem;
sq_queue_t dataind_queue;
bool md_notify_registered;
pid_t md_notify_pid;
struct sigevent md_notify_event;
struct sigwork_s md_notify_work;
};
* Private Function Prototypes
****************************************************************************/
static int mac802154dev_notify(FAR struct mac802154_maccb_s *maccb,
FAR struct ieee802154_primitive_s *primitive);
static int mac802154dev_rxframe(FAR struct mac802154_chardevice_s *dev,
FAR struct ieee802154_data_ind_s *ind);
static int mac802154dev_open(FAR struct file *filep);
static int mac802154dev_close(FAR struct file *filep);
static ssize_t mac802154dev_read(FAR struct file *filep, FAR char *buffer,
size_t len);
static ssize_t mac802154dev_write(FAR struct file *filep,
FAR const char *buffer, size_t len);
static int mac802154dev_ioctl(FAR struct file *filep, int cmd,
unsigned long arg);
* Private Data
****************************************************************************/
static const struct file_operations g_mac802154dev_fops =
{
mac802154dev_open,
mac802154dev_close,
mac802154dev_read,
mac802154dev_write,
NULL,
mac802154dev_ioctl,
};
* Private Functions
****************************************************************************/
* Name: mac802154dev_open
*
* Description:
* Open the 802.15.4 MAC character device.
*
****************************************************************************/
static int mac802154dev_open(FAR struct file *filep)
{
FAR struct inode *inode;
FAR struct mac802154_chardevice_s *dev;
FAR struct mac802154dev_open_s *opriv;
int ret;
inode = filep->f_inode;
dev = inode->i_private;
DEBUGASSERT(dev != NULL);
ret = nxmutex_lock(&dev->md_lock);
if (ret < 0)
{
wlerr("ERROR: nxsem_wait failed: %d\n", ret);
return ret;
}
opriv = (FAR struct mac802154dev_open_s *)
kmm_zalloc(sizeof(struct mac802154dev_open_s));
if (!opriv)
{
wlerr("ERROR: Failed to allocate new open struct\n");
ret = -ENOMEM;
goto errout_with_lock;
}
opriv->md_flink = dev->md_open;
dev->md_open = opriv;
filep->f_priv = (FAR void *)opriv;
ret = OK;
errout_with_lock:
nxmutex_unlock(&dev->md_lock);
return ret;
}
* Name: mac802154dev_close
*
* Description:
* Close the 802.15.4 MAC character device.
*
****************************************************************************/
static int mac802154dev_close(FAR struct file *filep)
{
FAR struct inode *inode;
FAR struct mac802154_chardevice_s *dev;
FAR struct mac802154dev_open_s *opriv;
FAR struct mac802154dev_open_s *curr;
FAR struct mac802154dev_open_s *prev;
bool closing;
int ret;
DEBUGASSERT(filep->f_priv);
opriv = filep->f_priv;
inode = filep->f_inode;
DEBUGASSERT(inode->i_private);
dev = inode->i_private;
ret = nxmutex_lock(&dev->md_lock);
if (ret < 0)
{
wlerr("ERROR: nxsem_wait failed: %d\n", ret);
return ret;
}
* and set.
*
* This is actually a pretty feeble attempt to handle this. The
* improbable race condition occurs if two different threads try to
* close the driver at the same time. The rule: don't do
* that! It is feeble because we do not really enforce stale pointer
* detection anyway.
*/
closing = opriv->md_closing;
opriv->md_closing = true;
if (closing)
{
nxmutex_unlock(&dev->md_lock);
return OK;
}
for (prev = NULL, curr = dev->md_open;
curr && curr != opriv;
prev = curr, curr = curr->md_flink);
DEBUGASSERT(curr);
if (!curr)
{
wlerr("ERROR: Failed to find open entry\n");
ret = -ENOENT;
goto errout_with_lock;
}
if (prev)
{
prev->md_flink = opriv->md_flink;
}
else
{
dev->md_open = opriv->md_flink;
}
kmm_free(opriv);
* not registered, purge the list of events.
*/
if (dev->md_open)
{
FAR struct ieee802154_primitive_s *primitive;
primitive =
(FAR struct ieee802154_primitive_s *)
sq_remfirst(&dev->primitive_queue);
while (primitive)
{
ieee802154_primitive_free(primitive);
primitive =
(FAR struct ieee802154_primitive_s *)
sq_remfirst(&dev->primitive_queue);
}
}
ret = OK;
errout_with_lock:
nxmutex_unlock(&dev->md_lock);
return ret;
}
* Name: mac802154dev_read
*
* Description:
* Return the last received packet.
*
****************************************************************************/
static ssize_t mac802154dev_read(FAR struct file *filep, FAR char *buffer,
size_t len)
{
FAR struct inode *inode;
FAR struct mac802154_chardevice_s *dev;
FAR struct mac802154dev_rxframe_s *rx;
FAR struct ieee802154_data_ind_s *ind;
struct ieee802154_get_req_s req;
int ret;
inode = filep->f_inode;
DEBUGASSERT(inode->i_private);
dev = inode->i_private;
if (len != sizeof(struct mac802154dev_rxframe_s))
{
wlerr("ERROR: buffer isn't a mac802154dev_rxframe_s: %lu\n",
(unsigned long)len);
return -EINVAL;
}
DEBUGASSERT(buffer != NULL);
rx = (FAR struct mac802154dev_rxframe_s *)buffer;
while (1)
{
ret = nxmutex_lock(&dev->md_lock);
if (ret < 0)
{
wlerr("ERROR: nxsem_wait failed: %d\n", ret);
return ret;
}
ind = (FAR struct ieee802154_data_ind_s *)
sq_remfirst(&dev->dataind_queue);
* data
*/
if (ind != NULL)
{
nxmutex_unlock(&dev->md_lock);
break;
}
* operation already pending, don't block. This driver returns EAGAIN
* even when configured as non-blocking if another read operation is
* already pending. This situation should be rare.
* It will only occur when there are 2 calls to read from separate
* threads and there was no data in the rx list.
*/
if ((filep->f_oflags & O_NONBLOCK) || dev->readpending)
{
nxmutex_unlock(&dev->md_lock);
return -EAGAIN;
}
dev->readpending = true;
nxmutex_unlock(&dev->md_lock);
ret = nxsem_wait(&dev->readsem);
if (ret < 0)
{
dev->readpending = false;
return ret;
}
* this time, it should have a data indication
*/
}
req.attr = IEEE802154_ATTR_MAC_PROMISCUOUS_MODE;
ret = mac802154_ioctl(dev->md_mac, MAC802154IOC_MLME_GET_REQUEST,
(unsigned long)&req);
if (ret == 0 && req.attrval.mac.promisc_mode)
{
* by the PHY layer's FCS length.
*/
req.attr = IEEE802154_ATTR_PHY_FCS_LEN;
ret = mac802154_ioctl(dev->md_mac, MAC802154IOC_MLME_GET_REQUEST,
(unsigned long)&req);
if (ret == OK)
{
rx->length = ind->frame->io_len + req.attrval.phy.fcslen;
rx->offset = ind->frame->io_offset;
}
memcpy(&rx->payload[0], &ind->frame->io_data[0], rx->length);
}
else
{
rx->length = (ind->frame->io_len - ind->frame->io_offset);
rx->offset = 0;
memcpy(&rx->payload[0], &ind->frame->io_data[ind->frame->io_offset],
rx->length);
}
memcpy(&rx->meta, ind, sizeof(struct ieee802154_data_ind_s));
rx->meta.flink = NULL;
rx->meta.frame = NULL;
iob_free(ind->frame);
ieee802154_primitive_free((FAR struct ieee802154_primitive_s *)ind);
return OK;
}
* Name: mac802154dev_write
*
* Description:
* Send a packet over the IEEE802.15.4 network.
*
****************************************************************************/
static ssize_t mac802154dev_write(FAR struct file *filep,
FAR const char *buffer, size_t len)
{
FAR struct inode *inode;
FAR struct mac802154_chardevice_s *dev;
FAR struct mac802154dev_txframe_s *tx;
FAR struct iob_s *iob;
int ret;
inode = filep->f_inode;
DEBUGASSERT(inode->i_private);
dev = inode->i_private;
if (len != sizeof(struct mac802154dev_txframe_s))
{
wlerr("ERROR: buffer isn't a mac802154dev_txframe_s: %lu\n",
(unsigned long)len);
return -EINVAL;
}
DEBUGASSERT(buffer != NULL);
tx = (FAR struct mac802154dev_txframe_s *)buffer;
iob = iob_alloc(false);
DEBUGASSERT(iob != NULL);
ret = mac802154_get_mhrlen(dev->md_mac, &tx->meta);
if (ret < 0)
{
wlerr("ERROR: TX meta-data is invalid");
return ret;
}
iob->io_offset = ret;
iob->io_len = iob->io_offset;
memcpy(&iob->io_data[iob->io_offset], tx->payload, tx->length);
iob->io_len += tx->length;
ret = mac802154_req_data(dev->md_mac, &tx->meta, iob);
if (ret < 0)
{
iob_free(iob);
wlerr("ERROR: req_data failed %d\n", ret);
return ret;
}
return OK;
}
* Name: mac802154dev_ioctl
*
* Description:
* Control the MAC layer via IOCTL commands.
*
****************************************************************************/
static int mac802154dev_ioctl(FAR struct file *filep, int cmd,
unsigned long arg)
{
FAR struct inode *inode;
FAR struct mac802154_chardevice_s *dev;
FAR union ieee802154_macarg_u *macarg =
(FAR union ieee802154_macarg_u *)((uintptr_t)arg);
int ret;
DEBUGASSERT(filep->f_priv != NULL &&
filep->f_inode != NULL);
inode = filep->f_inode;
DEBUGASSERT(inode->i_private);
dev = inode->i_private;
ret = nxmutex_lock(&dev->md_lock);
if (ret < 0)
{
wlerr("ERROR: nxsem_wait failed: %d\n", ret);
return ret;
}
switch (cmd)
{
* Description: Register to receive a signal whenever there is a
* event primitive sent from the MAC layer.
* Argument: The signal number to use.
* Return: Zero (OK) on success. Minus one will be returned on
* failure with the errno value set appropriately.
*/
case MAC802154IOC_NOTIFY_REGISTER:
{
dev->md_notify_event = macarg->event;
dev->md_notify_pid = nxsched_getpid();
dev->md_notify_registered = true;
ret = OK;
}
break;
case MAC802154IOC_GET_EVENT:
{
FAR struct ieee802154_primitive_s *primitive;
while (1)
{
primitive = (FAR struct ieee802154_primitive_s *)
sq_remfirst(&dev->primitive_queue);
* and free it from the MAC layer's memory.
*/
if (primitive != NULL)
{
memcpy(&macarg->primitive,
primitive,
sizeof(struct ieee802154_primitive_s));
ieee802154_primitive_free(primitive);
ret = OK;
break;
}
* getevent operation already pending, don't block. This driver
* returns EAGAIN even when configured as non-blocking if
* another getevent operation is already pending This situation
* should be rare.
* It will only occur when there are 2 calls from separate
* threads and there was no events in the queue.
*/
if ((filep->f_oflags & O_NONBLOCK) || dev->geteventpending)
{
ret = -EAGAIN;
break;
}
dev->geteventpending = true;
nxmutex_unlock(&dev->md_lock);
ret = nxsem_wait(&dev->geteventsem);
if (ret < 0)
{
dev->geteventpending = false;
return ret;
}
* and pop an event off the queue
*/
ret = nxmutex_lock(&dev->md_lock);
if (ret < 0)
{
wlerr("ERROR: nxsem_wait failed: %d\n", ret);
return ret;
}
}
}
break;
case MAC802154IOC_ENABLE_EVENTS:
{
dev->enableevents = macarg->enable;
ret = OK;
}
break;
default:
{
ret = mac802154_ioctl(dev->md_mac, cmd, arg);
}
break;
}
nxmutex_unlock(&dev->md_lock);
return ret;
}
static int mac802154dev_notify(FAR struct mac802154_maccb_s *maccb,
FAR struct ieee802154_primitive_s *primitive)
{
FAR struct mac802154dev_callback_s *cb =
(FAR struct mac802154dev_callback_s *)maccb;
FAR struct mac802154_chardevice_s *dev;
DEBUGASSERT(cb != NULL && cb->mc_priv != NULL);
dev = cb->mc_priv;
if (primitive->type == IEEE802154_PRIMITIVE_IND_DATA)
{
return mac802154dev_rxframe(dev, &primitive->u.dataind);
}
* signal the receiver. Events should be popped from the queue from the
* application at a reasonable rate in order for the MAC layer to be able
* to allocate new notifications.
*/
if (dev->enableevents &&
(dev->md_open != NULL || dev->md_notify_registered))
{
* any signals so if we see one, just go back to trying to get access
* again
*/
while (nxmutex_lock(&dev->md_lock) != 0);
sq_addlast((FAR sq_entry_t *)primitive, &dev->primitive_queue);
if (dev->geteventpending)
{
dev->geteventpending = false;
nxsem_post(&dev->geteventsem);
}
if (dev->md_notify_registered)
{
dev->md_notify_event.sigev_value.sival_int = primitive->type;
nxsig_notification(dev->md_notify_pid, &dev->md_notify_event,
SI_QUEUE, &dev->md_notify_work);
}
nxmutex_unlock(&dev->md_lock);
return OK;
}
* the primitive and it will free it for us
*/
return -1;
}
* Name: mac802154dev_rxframe
*
* Description:
* Handle received frames forward by the IEEE 802.15.4 MAC.
*
* Returned Value:
* any failure. On success, the ind and its contained iob will be freed.
* The ind will be intact if this function returns a failure.
*
****************************************************************************/
static int mac802154dev_rxframe(FAR struct mac802154_chardevice_s *dev,
FAR struct ieee802154_data_ind_s *ind)
{
* signals so if we see one, just go back to trying to get access again
*/
while (nxmutex_lock(&dev->md_lock) != 0);
sq_addlast((FAR sq_entry_t *)ind, &dev->dataind_queue);
if (dev->readpending)
{
dev->readpending = false;
nxsem_post(&dev->readsem);
}
nxmutex_unlock(&dev->md_lock);
return OK;
}
* Public Functions
****************************************************************************/
* Name: mac802154dev_register
*
* Description:
* Register a character driver to access the IEEE 802.15.4 MAC layer from
* user-space
*
* Input Parameters:
* mac - Pointer to the mac layer struct to be registered.
* minor - The device minor number. The IEEE802.15.4 MAC character device
* will be registered as /dev/ieeeN where N is the minor number
*
* Returned Value:
* Zero (OK) is returned on success. Otherwise a negated errno value is
* returned to indicate the nature of the failure.
*
****************************************************************************/
int mac802154dev_register(MACHANDLE mac, int minor)
{
FAR struct mac802154_chardevice_s *dev;
FAR struct mac802154_maccb_s *maccb;
char devname[DEVNAME_FMTLEN];
int ret;
dev = kmm_zalloc(sizeof(struct mac802154_chardevice_s));
if (!dev)
{
wlerr("ERROR: Failed to allocate device structure\n");
return -ENOMEM;
}
dev->md_mac = mac;
nxmutex_init(&dev->md_lock);
* before blocking */
nxsem_init(&dev->readsem, 0, 0);
dev->readpending = false;
sq_init(&dev->dataind_queue);
dev->geteventpending = false;
nxsem_init(&dev->geteventsem, 0, 0);
sq_init(&dev->primitive_queue);
dev->enableevents = true;
dev->md_notify_registered = false;
dev->md_cb.mc_priv = dev;
maccb = &dev->md_cb.mc_cb;
maccb->flink = NULL;
maccb->prio = CONFIG_IEEE802154_MACDEV_RECVRPRIO;
maccb->notify = mac802154dev_notify;
ret = mac802154_bind(mac, maccb);
if (ret < 0)
{
nerr("ERROR: Failed to bind the MAC callbacks: %d\n", ret);
goto errout_with_priv;
}
snprintf(devname, sizeof(devname), DEVNAME_FMT, minor);
ret = register_driver(devname, &g_mac802154dev_fops, 0666, dev);
if (ret < 0)
{
wlerr("ERROR: register_driver failed: %d\n", ret);
goto errout_with_priv;
}
return OK;
errout_with_priv:
nxmutex_destroy(&dev->md_lock);
kmm_free(dev);
return ret;
}