2015-06-03 21:47:41 +08:00
|
|
|
/* dummy_15_4_radio.c - 802.15.4 radio driver loopbacks Tx frames back to us */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2015 Intel Corporation
|
|
|
|
*
|
2015-10-07 00:00:37 +08:00
|
|
|
* Licensed 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
|
2015-06-03 21:47:41 +08:00
|
|
|
*
|
2015-10-07 00:00:37 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-06-03 21:47:41 +08:00
|
|
|
*
|
2015-10-07 00:00:37 +08: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.
|
2015-06-03 21:47:41 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "contiki.h"
|
|
|
|
|
2015-10-30 23:42:03 +08:00
|
|
|
#include <net/l2_buf.h>
|
2015-11-25 15:11:30 +08:00
|
|
|
#include <console/uart_pipe.h>
|
2015-06-03 21:47:41 +08:00
|
|
|
|
2015-11-05 03:37:31 +08:00
|
|
|
#include "contiki/packetbuf.h"
|
|
|
|
#include "contiki/netstack.h"
|
2015-06-03 21:47:41 +08:00
|
|
|
#include "dummy_15_4_radio.h"
|
|
|
|
#include "net_driver_15_4.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#if UIP_CONF_LOGGING
|
|
|
|
#define DEBUG DEBUG_FULL
|
|
|
|
#else
|
|
|
|
#define DEBUG DEBUG_NONE
|
|
|
|
#endif
|
2015-11-05 03:37:31 +08:00
|
|
|
#include "contiki/ip/uip-debug.h"
|
2015-06-03 21:47:41 +08:00
|
|
|
|
|
|
|
#if UIP_LOGGING
|
|
|
|
#include <stdio.h>
|
|
|
|
void uip_log(char *msg);
|
|
|
|
#define UIP_LOG(m) uip_log(m)
|
|
|
|
#else
|
|
|
|
#define UIP_LOG(m)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define FOOTER_LEN 2
|
|
|
|
#define NETWORK_TEST_MAX_PACKET_LEN PACKETBUF_SIZE
|
|
|
|
|
|
|
|
static volatile uint16_t last_packet_timestamp;
|
2015-07-10 19:04:35 +08:00
|
|
|
|
|
|
|
/* Data sending and receiving is done in TLV way. */
|
|
|
|
#if defined CONFIG_NETWORKING_WITH_15_4_LOOPBACK_UART
|
|
|
|
#define DUMMY_RADIO_15_4_FRAME_TYPE 0xF0
|
|
|
|
static uint8_t input[NETWORK_TEST_MAX_PACKET_LEN];
|
2015-08-19 18:50:19 +08:00
|
|
|
static uint8_t input_len, input_offset, input_type;
|
2015-09-25 21:47:20 +08:00
|
|
|
static bool starting = true;
|
|
|
|
#define PRINT_DATA 1
|
|
|
|
#undef PRINT_DATA /* comment this to print transferred bytes */
|
2015-07-10 19:04:35 +08:00
|
|
|
#else
|
2015-06-03 21:47:41 +08:00
|
|
|
static uint8_t loopback[NETWORK_TEST_MAX_PACKET_LEN];
|
2015-07-10 19:04:35 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
#if defined CONFIG_NETWORKING_WITH_15_4_LOOPBACK_UART
|
|
|
|
static uint8_t *recv_cb(uint8_t *buf, size_t *off)
|
|
|
|
{
|
2015-09-25 21:47:20 +08:00
|
|
|
#if PRINT_DATA
|
|
|
|
PRINTF("dummy154radio: %s(): input[] %d data 0x%x uart offset %d\n",
|
|
|
|
__FUNCTION__, input_offset, buf[0], *off);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (starting) {
|
|
|
|
if (buf[0] == 0) {
|
|
|
|
goto done;
|
|
|
|
} else {
|
|
|
|
starting = false;
|
|
|
|
}
|
|
|
|
}
|
2015-07-10 19:04:35 +08:00
|
|
|
if (input_len == 0 && input_offset == 0 &&
|
|
|
|
buf[0] == DUMMY_RADIO_15_4_FRAME_TYPE) {
|
2015-08-19 18:50:19 +08:00
|
|
|
input_type = buf[0];
|
2015-07-10 19:04:35 +08:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (input_len == 0 && input_offset == 0 &&
|
2015-08-19 18:50:19 +08:00
|
|
|
input_type == DUMMY_RADIO_15_4_FRAME_TYPE) {
|
2015-07-10 19:04:35 +08:00
|
|
|
input_len = buf[0];
|
2015-10-01 18:37:59 +08:00
|
|
|
|
|
|
|
if (input_len >= NETWORK_TEST_MAX_PACKET_LEN) {
|
|
|
|
PRINTF("dummy154radio: too long message %d max is %d, "
|
|
|
|
"discarding packet\n", input_len, NETWORK_TEST_MAX_PACKET_LEN);
|
|
|
|
} else {
|
|
|
|
PRINTF("dummy154radio: will receive %d bytes\n", input_len);
|
|
|
|
}
|
2015-07-10 19:04:35 +08:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (input_len) {
|
2015-10-01 18:37:59 +08:00
|
|
|
static bool printed;
|
|
|
|
if (input_offset >= NETWORK_TEST_MAX_PACKET_LEN) {
|
|
|
|
if (!printed) {
|
|
|
|
PRINTF("dummy154radio: too long message (offset %d), "
|
|
|
|
"discarding packet\n", input_offset);
|
|
|
|
printed = true;
|
|
|
|
}
|
2015-11-05 16:29:08 +08:00
|
|
|
goto fail;
|
2015-10-01 18:37:59 +08:00
|
|
|
} else {
|
|
|
|
printed = false;
|
|
|
|
input[input_offset++] = buf[0];
|
|
|
|
}
|
2015-07-10 19:04:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (input_len && input_len == input_offset) {
|
2015-10-01 18:37:59 +08:00
|
|
|
if (input_len < NETWORK_TEST_MAX_PACKET_LEN) {
|
2015-10-30 23:42:03 +08:00
|
|
|
struct net_buf *mbuf;
|
2015-10-01 18:37:59 +08:00
|
|
|
|
2015-10-30 23:42:03 +08:00
|
|
|
mbuf = l2_buf_get_reserve(0);
|
2015-10-01 18:37:59 +08:00
|
|
|
if (mbuf) {
|
|
|
|
packetbuf_copyfrom(mbuf, input, input_len);
|
|
|
|
packetbuf_set_datalen(mbuf, input_len);
|
|
|
|
packetbuf_set_attr(mbuf, PACKETBUF_ATTR_TIMESTAMP,
|
|
|
|
last_packet_timestamp);
|
|
|
|
PRINTF("dummy154radio: received %d bytes\n", input_len);
|
|
|
|
|
|
|
|
if (net_driver_15_4_recv_from_hw(mbuf) < 0) {
|
|
|
|
PRINTF("dummy154radio: rdc input failed, packet discarded\n");
|
2015-10-30 23:42:03 +08:00
|
|
|
l2_buf_unref(mbuf);
|
2015-10-01 18:37:59 +08:00
|
|
|
}
|
2015-07-10 19:04:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-05 16:29:08 +08:00
|
|
|
fail:
|
2015-08-19 18:50:19 +08:00
|
|
|
input_len = input_offset = input_type = 0;
|
2015-07-10 19:04:35 +08:00
|
|
|
memset(input, 0, sizeof(input));
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
*off = 0;
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
#endif
|
2015-06-03 21:47:41 +08:00
|
|
|
|
2015-09-25 21:47:20 +08:00
|
|
|
#if defined CONFIG_NETWORKING_WITH_15_4_LOOPBACK_UART
|
2016-03-02 23:50:15 +08:00
|
|
|
static void uart_send(unsigned char c)
|
2015-09-25 21:47:20 +08:00
|
|
|
{
|
|
|
|
uint8_t buf[1] = { c };
|
|
|
|
|
|
|
|
#if PRINT_DATA
|
|
|
|
PRINTF("dummy154radio: %s(): writing 0x%x\n",
|
|
|
|
__FUNCTION__, buf[0]);
|
|
|
|
#endif
|
|
|
|
|
2016-03-02 23:50:15 +08:00
|
|
|
uart_pipe_send(&buf[0], 1);
|
2015-09-25 21:47:20 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-06-03 21:47:41 +08:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
init(void)
|
|
|
|
{
|
2015-07-10 19:04:35 +08:00
|
|
|
#if defined CONFIG_NETWORKING_WITH_15_4_LOOPBACK_UART
|
|
|
|
/* Use small temp buffer for receiving data */
|
|
|
|
static uint8_t buf[1];
|
|
|
|
|
2015-11-25 15:11:30 +08:00
|
|
|
uart_pipe_register(buf, sizeof(buf), recv_cb);
|
2015-09-25 21:47:20 +08:00
|
|
|
|
|
|
|
/* It seems that some of the start bytes are lost so
|
|
|
|
* send some null bytes in the start in order to sync
|
|
|
|
* the link.
|
|
|
|
*/
|
|
|
|
uart_send(0);
|
|
|
|
uart_send(0);
|
|
|
|
uart_send(0);
|
|
|
|
uart_send(0);
|
|
|
|
uart_send(0);
|
2015-07-10 19:04:35 +08:00
|
|
|
#endif
|
|
|
|
|
2015-06-03 21:47:41 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
prepare(const void *payload, unsigned short payload_len)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
2015-10-30 23:42:03 +08:00
|
|
|
transmit(struct net_buf *buf, unsigned short transmit_len)
|
2015-06-03 21:47:41 +08:00
|
|
|
{
|
|
|
|
return RADIO_TX_OK;
|
|
|
|
}
|
|
|
|
|
2015-07-10 19:04:35 +08:00
|
|
|
#ifndef CONFIG_NETWORKING_WITH_15_4_LOOPBACK_UART
|
2015-10-30 23:42:03 +08:00
|
|
|
static void route_buf(struct net_buf *buf)
|
2015-06-03 21:47:41 +08:00
|
|
|
{
|
|
|
|
int len;
|
2015-10-30 23:42:03 +08:00
|
|
|
struct net_buf *mbuf;
|
2015-06-03 21:47:41 +08:00
|
|
|
|
|
|
|
len = packetbuf_copyto(buf, loopback);
|
|
|
|
/* Receiver buffer that is passed to 15.4 Rx fiber */
|
|
|
|
PRINTF("dummy154radio: got %d bytes\n", len);
|
|
|
|
|
2015-10-30 23:42:03 +08:00
|
|
|
mbuf = l2_buf_get_reserve(0);
|
2015-06-03 21:47:41 +08:00
|
|
|
if (mbuf) {
|
|
|
|
packetbuf_copyfrom(mbuf, loopback, len);
|
|
|
|
packetbuf_set_datalen(mbuf, len);
|
|
|
|
packetbuf_set_attr(mbuf, PACKETBUF_ATTR_TIMESTAMP,
|
|
|
|
last_packet_timestamp);
|
|
|
|
PRINTF("dummy154radio: 15.4 Rx input %d bytes\n", len);
|
|
|
|
|
|
|
|
if (net_driver_15_4_recv_from_hw(mbuf) < 0) {
|
|
|
|
PRINTF("dummy154radio: rdc input failed, "
|
|
|
|
"packet discarded\n");
|
2015-10-30 23:42:03 +08:00
|
|
|
l2_buf_unref(mbuf);
|
2015-06-03 21:47:41 +08:00
|
|
|
}
|
2015-07-01 15:52:17 +08:00
|
|
|
|
|
|
|
NET_BUF_CHECK_IF_NOT_IN_USE(mbuf);
|
2015-06-03 21:47:41 +08:00
|
|
|
}
|
|
|
|
}
|
2015-07-10 19:04:35 +08:00
|
|
|
#endif
|
|
|
|
|
2015-06-03 21:47:41 +08:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
2015-10-30 23:42:03 +08:00
|
|
|
send(struct net_buf *buf, const void *payload, unsigned short payload_len)
|
2015-06-03 21:47:41 +08:00
|
|
|
{
|
2015-07-10 19:04:35 +08:00
|
|
|
#if defined CONFIG_NETWORKING_WITH_15_4_LOOPBACK_UART
|
|
|
|
static uint8_t output[NETWORK_TEST_MAX_PACKET_LEN];
|
|
|
|
uint8_t len, i;
|
|
|
|
|
|
|
|
len = packetbuf_copyto(buf, output);
|
2015-09-25 17:35:51 +08:00
|
|
|
if (len != payload_len) {
|
|
|
|
PRINTF("dummy154radio: sending %d bytes, payload %d bytes\n",
|
|
|
|
len, payload_len);
|
|
|
|
} else {
|
|
|
|
PRINTF("dummy154radio: sending %d bytes\n", len);
|
|
|
|
}
|
2015-07-10 19:04:35 +08:00
|
|
|
|
2016-03-02 23:50:15 +08:00
|
|
|
uart_send(DUMMY_RADIO_15_4_FRAME_TYPE); /* Type */
|
|
|
|
uart_send(len); /* Length */
|
2015-08-19 18:50:19 +08:00
|
|
|
|
2015-07-10 19:04:35 +08:00
|
|
|
for (i = 0; i < len; i++) {
|
2016-03-02 23:50:15 +08:00
|
|
|
uart_send(output[i]);
|
2015-07-10 19:04:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return RADIO_TX_OK;
|
|
|
|
#else
|
2015-06-03 21:47:41 +08:00
|
|
|
route_buf(buf);
|
|
|
|
return transmit(buf, payload_len);
|
2015-07-10 19:04:35 +08:00
|
|
|
#endif
|
2015-06-03 21:47:41 +08:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
radio_read(void *buf, unsigned short buf_len)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
channel_clear(void)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
receiving_packet(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
pending_packet(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
on(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
off(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static radio_result_t
|
|
|
|
get_value(radio_param_t param, radio_value_t *value)
|
|
|
|
{
|
|
|
|
return RADIO_RESULT_NOT_SUPPORTED;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static radio_result_t
|
|
|
|
set_value(radio_param_t param, radio_value_t value)
|
|
|
|
{
|
|
|
|
return RADIO_RESULT_NOT_SUPPORTED;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static radio_result_t
|
|
|
|
get_object(radio_param_t param, void *dest, size_t size)
|
|
|
|
{
|
|
|
|
return RADIO_RESULT_NOT_SUPPORTED;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static radio_result_t
|
|
|
|
set_object(radio_param_t param, const void *src, size_t size)
|
|
|
|
{
|
|
|
|
return RADIO_RESULT_NOT_SUPPORTED;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
const struct radio_driver dummy154radio_driver =
|
|
|
|
{
|
|
|
|
.init = init,
|
|
|
|
.prepare = prepare,
|
|
|
|
.transmit = transmit,
|
|
|
|
.send = send,
|
|
|
|
.read = radio_read,
|
|
|
|
.channel_clear = channel_clear,
|
|
|
|
.receiving_packet = receiving_packet,
|
|
|
|
.pending_packet = pending_packet,
|
|
|
|
.on = on,
|
|
|
|
.off = off,
|
|
|
|
.get_value = get_value,
|
|
|
|
.set_value = set_value,
|
|
|
|
.get_object = get_object,
|
|
|
|
.set_object = set_object
|
|
|
|
};
|
|
|
|
/*---------------------------------------------------------------------------*/
|