summaryrefslogtreecommitdiff
path: root/net/ip/dummy_15_4_radio.c
blob: 3ac1412a0f1a92deb97ffd63fae5b2b0c2566c21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/* dummy_15_4_radio.c - 802.15.4 radio driver loopbacks Tx frames back to us */

/*
 * Copyright (c) 2015 Intel Corporation
 *
 * 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
 *
 *     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.
 */

#include "contiki.h"

#include <net/l2_buf.h>
#include <console/uart_pipe.h>

#include "contiki/packetbuf.h"
#include "contiki/netstack.h"
#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
#include "contiki/ip/uip-debug.h"

#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;

/* 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];
static uint8_t input_len, input_offset, input_type;
static bool starting = true;
#define PRINT_DATA 1
#undef PRINT_DATA /* comment this to print transferred bytes */
#else
static uint8_t loopback[NETWORK_TEST_MAX_PACKET_LEN];
#endif

/*---------------------------------------------------------------------------*/
#if defined CONFIG_NETWORKING_WITH_15_4_LOOPBACK_UART
static uint8_t *recv_cb(uint8_t *buf, size_t *off)
{
#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;
    }
  }
  if (input_len == 0 && input_offset == 0 &&
       buf[0] == DUMMY_RADIO_15_4_FRAME_TYPE) {
    input_type = buf[0];
    goto done;
  }

  if (input_len == 0 && input_offset == 0 &&
       input_type == DUMMY_RADIO_15_4_FRAME_TYPE) {
    input_len = buf[0];

    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);
    }
    goto done;
  }

  if (input_len) {
    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;
      }
      goto fail;
    } else {
      printed = false;
      input[input_offset++] = buf[0];
    }
  }

  if (input_len && input_len == input_offset) {
     if (input_len < NETWORK_TEST_MAX_PACKET_LEN) {
       struct net_buf *mbuf;

       mbuf = l2_buf_get_reserve(0);
       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");
	   l2_buf_unref(mbuf);
	 }
       }
     }

  fail:
     input_len = input_offset = input_type = 0;
     memset(input, 0, sizeof(input));
  }

done:
  *off = 0;
  return buf;
}
#endif

#if defined CONFIG_NETWORKING_WITH_15_4_LOOPBACK_UART
static int uart_send(unsigned char c)
{
  uint8_t buf[1] = { c };

#if PRINT_DATA
  PRINTF("dummy154radio: %s(): writing 0x%x\n",
	 __FUNCTION__, buf[0]);
#endif

  return uart_pipe_send(&buf[0], 1);
}
#endif

/*---------------------------------------------------------------------------*/
static int
init(void)
{
#if defined CONFIG_NETWORKING_WITH_15_4_LOOPBACK_UART
  /* Use small temp buffer for receiving data */
  static uint8_t buf[1];

  uart_pipe_register(buf, sizeof(buf), recv_cb);

  /* 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);
#endif

  return 0;
}
/*---------------------------------------------------------------------------*/
static int
prepare(const void *payload, unsigned short payload_len)
{
  return 1;
}
/*---------------------------------------------------------------------------*/
static int
transmit(struct net_buf *buf, unsigned short transmit_len)
{
  return RADIO_TX_OK;
}

#ifndef CONFIG_NETWORKING_WITH_15_4_LOOPBACK_UART
static void route_buf(struct net_buf *buf)
{
	int len;
	struct net_buf *mbuf;

	len = packetbuf_copyto(buf, loopback);
	/* Receiver buffer that is passed to 15.4 Rx fiber */
	PRINTF("dummy154radio: got %d bytes\n", len);

	mbuf = l2_buf_get_reserve(0);
	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");
			l2_buf_unref(mbuf);
		}

		NET_BUF_CHECK_IF_NOT_IN_USE(mbuf);
	}
}
#endif

/*---------------------------------------------------------------------------*/
static int
send(struct net_buf *buf, const void *payload, unsigned short payload_len)
{
#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);
  if (len != payload_len) {
    PRINTF("dummy154radio: sending %d bytes, payload %d bytes\n",
	   len, payload_len);
  } else {
    PRINTF("dummy154radio: sending %d bytes\n", len);
  }

  if(!uart_send(DUMMY_RADIO_15_4_FRAME_TYPE)) { /* Type */
      PRINTF("uart_send failed (frame type)\n");
      return RADIO_TX_ERR;
  }

  if(!uart_send(len)) {  /* Length */
      PRINTF("uart_send failed (length)\n");
      return RADIO_TX_ERR;
  }

  for (i = 0; i < len; i++) {
    if (!uart_send(output[i])) {
      PRINTF("uart_send failed\n");
      return RADIO_TX_ERR;
    }
  }

  return RADIO_TX_OK;
#else
  route_buf(buf);
  return transmit(buf, payload_len);
#endif
}
/*---------------------------------------------------------------------------*/
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
  };
/*---------------------------------------------------------------------------*/