2015-02-10 15:50:00 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Freie Universität Berlin
|
|
|
|
*
|
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser
|
|
|
|
* General Public License v2.1. See the file LICENSE in the top level
|
|
|
|
* directory for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @{
|
|
|
|
* @ingroup net_nomac
|
|
|
|
* @file
|
|
|
|
* @brief Implementation of the NOMAC MAC protocol
|
|
|
|
*
|
|
|
|
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "kernel.h"
|
|
|
|
#include "msg.h"
|
|
|
|
#include "thread.h"
|
2015-08-17 15:41:29 +02:00
|
|
|
#include "net/gnrc/nomac.h"
|
2015-08-10 02:41:08 +02:00
|
|
|
#include "net/gnrc.h"
|
2015-02-10 15:50:00 +01:00
|
|
|
|
|
|
|
#define ENABLE_DEBUG (0)
|
|
|
|
#include "debug.h"
|
|
|
|
|
2015-07-06 18:05:59 +02:00
|
|
|
#if ENABLE_DEBUG
|
|
|
|
/* For PRIu16 etc. */
|
|
|
|
#include <inttypes.h>
|
|
|
|
#endif
|
|
|
|
|
2015-02-10 15:50:00 +01:00
|
|
|
/**
|
|
|
|
* @brief Function called by the device driver on device events
|
|
|
|
*
|
|
|
|
* @param[in] event type of event
|
|
|
|
* @param[in] data optional parameter
|
|
|
|
*/
|
2015-08-17 15:41:29 +02:00
|
|
|
static void _event_cb(gnrc_netdev_event_t event, void *data)
|
2015-02-10 15:50:00 +01:00
|
|
|
{
|
|
|
|
DEBUG("nomac: event triggered -> %i\n", event);
|
|
|
|
/* NOMAC only understands the RX_COMPLETE event... */
|
|
|
|
if (event == NETDEV_EVENT_RX_COMPLETE) {
|
2015-08-17 15:41:29 +02:00
|
|
|
gnrc_pktsnip_t *pkt;
|
2015-02-10 15:50:00 +01:00
|
|
|
|
|
|
|
/* get pointer to the received packet */
|
2015-08-17 15:41:29 +02:00
|
|
|
pkt = (gnrc_pktsnip_t *)data;
|
2015-07-01 13:41:15 +02:00
|
|
|
/* send the packet to everyone interested in it's type */
|
2015-08-17 15:41:29 +02:00
|
|
|
if (!gnrc_netapi_dispatch_receive(pkt->type, GNRC_NETREG_DEMUX_CTX_ALL, pkt)) {
|
2015-02-10 15:50:00 +01:00
|
|
|
DEBUG("nomac: unable to forward packet of type %i\n", pkt->type);
|
2015-08-17 15:41:29 +02:00
|
|
|
gnrc_pktbuf_release(pkt);
|
2015-02-10 15:50:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Startup code and event loop of the NOMAC layer
|
|
|
|
*
|
|
|
|
* @param[in] args expects a pointer to the underlying netdev device
|
|
|
|
*
|
|
|
|
* @return never returns
|
|
|
|
*/
|
|
|
|
static void *_nomac_thread(void *args)
|
|
|
|
{
|
2015-08-17 15:41:29 +02:00
|
|
|
gnrc_netdev_t *dev = (gnrc_netdev_t *)args;
|
|
|
|
gnrc_netapi_opt_t *opt;
|
2015-02-10 15:50:00 +01:00
|
|
|
int res;
|
2015-08-17 15:41:29 +02:00
|
|
|
msg_t msg, reply, msg_queue[GNRC_NOMAC_MSG_QUEUE_SIZE];
|
2015-02-10 15:50:00 +01:00
|
|
|
|
|
|
|
/* setup the MAC layers message queue */
|
2015-08-17 15:41:29 +02:00
|
|
|
msg_init_queue(msg_queue, GNRC_NOMAC_MSG_QUEUE_SIZE);
|
2015-02-10 15:50:00 +01:00
|
|
|
/* save the PID to the device descriptor and register the device */
|
|
|
|
dev->mac_pid = thread_getpid();
|
2015-08-17 15:41:29 +02:00
|
|
|
gnrc_netif_add(dev->mac_pid);
|
2015-02-10 15:50:00 +01:00
|
|
|
/* register the event callback with the device driver */
|
|
|
|
dev->driver->add_event_callback(dev, _event_cb);
|
|
|
|
|
|
|
|
/* start the event loop */
|
|
|
|
while (1) {
|
|
|
|
DEBUG("nomac: waiting for incoming messages\n");
|
|
|
|
msg_receive(&msg);
|
|
|
|
/* dispatch NETDEV and NETAPI messages */
|
|
|
|
switch (msg.type) {
|
2015-08-17 15:41:29 +02:00
|
|
|
case GNRC_NETDEV_MSG_TYPE_EVENT:
|
|
|
|
DEBUG("nomac: GNRC_NETDEV_MSG_TYPE_EVENT received\n");
|
2015-02-10 15:50:00 +01:00
|
|
|
dev->driver->isr_event(dev, msg.content.value);
|
|
|
|
break;
|
2015-08-17 15:41:29 +02:00
|
|
|
case GNRC_NETAPI_MSG_TYPE_SND:
|
|
|
|
DEBUG("nomac: GNRC_NETAPI_MSG_TYPE_SND received\n");
|
|
|
|
dev->driver->send_data(dev, (gnrc_pktsnip_t *)msg.content.ptr);
|
2015-02-10 15:50:00 +01:00
|
|
|
break;
|
2015-08-17 15:41:29 +02:00
|
|
|
case GNRC_NETAPI_MSG_TYPE_SET:
|
2015-02-10 15:50:00 +01:00
|
|
|
/* TODO: filter out MAC layer options -> for now forward
|
|
|
|
everything to the device driver */
|
2015-08-17 15:41:29 +02:00
|
|
|
DEBUG("nomac: GNRC_NETAPI_MSG_TYPE_SET received\n");
|
2015-02-10 15:50:00 +01:00
|
|
|
/* read incoming options */
|
2015-08-17 15:41:29 +02:00
|
|
|
opt = (gnrc_netapi_opt_t *)msg.content.ptr;
|
2015-02-10 15:50:00 +01:00
|
|
|
/* set option for device driver */
|
|
|
|
res = dev->driver->set(dev, opt->opt, opt->data, opt->data_len);
|
2015-03-13 19:41:56 +01:00
|
|
|
DEBUG("nomac: response of netdev->set: %i\n", res);
|
2015-02-10 15:50:00 +01:00
|
|
|
/* send reply to calling thread */
|
2015-08-17 15:41:29 +02:00
|
|
|
reply.type = GNRC_NETAPI_MSG_TYPE_ACK;
|
2015-02-10 15:50:00 +01:00
|
|
|
reply.content.value = (uint32_t)res;
|
|
|
|
msg_reply(&msg, &reply);
|
|
|
|
break;
|
2015-08-17 15:41:29 +02:00
|
|
|
case GNRC_NETAPI_MSG_TYPE_GET:
|
2015-02-10 15:50:00 +01:00
|
|
|
/* TODO: filter out MAC layer options -> for now forward
|
|
|
|
everything to the device driver */
|
2015-08-17 15:41:29 +02:00
|
|
|
DEBUG("nomac: GNRC_NETAPI_MSG_TYPE_GET received\n");
|
2015-02-10 15:50:00 +01:00
|
|
|
/* read incoming options */
|
2015-08-17 15:41:29 +02:00
|
|
|
opt = (gnrc_netapi_opt_t *)msg.content.ptr;
|
2015-02-10 15:50:00 +01:00
|
|
|
/* get option from device driver */
|
2015-03-13 19:41:56 +01:00
|
|
|
res = dev->driver->get(dev, opt->opt, opt->data, opt->data_len);
|
|
|
|
DEBUG("nomac: response of netdev->get: %i\n", res);
|
2015-02-10 15:50:00 +01:00
|
|
|
/* send reply to calling thread */
|
2015-08-17 15:41:29 +02:00
|
|
|
reply.type = GNRC_NETAPI_MSG_TYPE_ACK;
|
2015-02-10 15:50:00 +01:00
|
|
|
reply.content.value = (uint32_t)res;
|
|
|
|
msg_reply(&msg, &reply);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
DEBUG("nomac: Unknown command %" PRIu16 "\n", msg.type);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* never reached */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-08-17 15:41:29 +02:00
|
|
|
kernel_pid_t gnrc_nomac_init(char *stack, int stacksize, char priority,
|
|
|
|
const char *name, gnrc_netdev_t *dev)
|
2015-02-10 15:50:00 +01:00
|
|
|
{
|
|
|
|
kernel_pid_t res;
|
|
|
|
|
|
|
|
/* check if given netdev device is defined and the driver is set */
|
|
|
|
if (dev == NULL || dev->driver == NULL) {
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
/* create new NOMAC thread */
|
2015-12-02 12:00:19 +01:00
|
|
|
res = thread_create(stack, stacksize, priority, THREAD_CREATE_STACKTEST,
|
2015-07-01 13:41:44 +02:00
|
|
|
_nomac_thread, (void *)dev, name);
|
2015-02-10 15:50:00 +01:00
|
|
|
if (res <= 0) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|