Linux Audio

Check our new training course

Loading...
  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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
/** @file
 @brief Network context API

 An API for applications to define a network connection.
 */

/*
 * 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.
 */

#ifdef CONFIG_NETWORK_IP_STACK_DEBUG_CONTEXT
#define DEBUG 1
#endif
#include "contiki/ip/uip-debug.h"

#include <nanokernel.h>
#include <string.h>
#include <errno.h>
#include <stdbool.h>

#include <net/net_ip.h>
#include <net/net_socket.h>

#include "contiki/ip/simple-udp.h"
#include "contiki/ipv6/uip-ds6.h"

#include "contiki/os/lib/random.h"
#include "contiki/ipv6/uip-ds6.h"

#ifdef CONFIG_NETWORKING_WITH_TCP
#include "contiki/os/sys/process.h"
#include "contiki/ip/psock.h"
#endif

#if !defined(CONFIG_NETWORK_IP_STACK_DEBUG_CONTEXT)
#undef NET_DBG
#define NET_DBG(...)
#endif

int net_context_get_receiver_registered(struct net_context *context);

struct net_context {
	/* Connection tuple identifies the connection */
	struct net_tuple tuple;

	/* Application receives data via this fifo */
	struct nano_fifo rx_queue;

	/* Application connection data */
	union {
		struct simple_udp_connection udp;

#ifdef CONFIG_NETWORKING_WITH_TCP
		struct {
			/* Proto socket that handles one TCP connection. */
			struct psock ps;
			struct process tcp;
			enum net_tcp_type tcp_type;
			int connection_status;
			void *conn;
			struct net_buf *pending;
			uint8_t retry_count;
		};
#endif
	};

	bool receiver_registered;
};

/* Override this in makefile if needed */
#if defined(CONFIG_NET_MAX_CONTEXTS)
#define NET_MAX_CONTEXT CONFIG_NET_MAX_CONTEXTS
#else
#define NET_MAX_CONTEXT 5
#endif

static struct net_context contexts[NET_MAX_CONTEXT];
static struct nano_sem contexts_lock;

static void context_sem_give(struct nano_sem *chan)
{
	switch (sys_execution_context_type_get()) {
	case NANO_CTX_FIBER:
		nano_fiber_sem_give(chan);
		break;
	case NANO_CTX_TASK:
		nano_task_sem_give(chan);
		break;
	case NANO_CTX_ISR:
	default:
		/* Invalid context type */
		break;
	}
}

static int context_port_used(enum ip_protocol ip_proto, uint16_t local_port,
			     const struct net_addr *local_addr)

{
	int i;

	for (i = 0; i < NET_MAX_CONTEXT; i++) {
		if (contexts[i].tuple.ip_proto == ip_proto &&
		    contexts[i].tuple.local_port == local_port &&
		    !memcmp(&contexts[i].tuple.local_addr, local_addr,
			   sizeof(struct net_addr))) {
			return -EEXIST;
		}
	}

	return 0;
}

struct net_context *net_context_get(enum ip_protocol ip_proto,
					const struct net_addr *remote_addr,
					uint16_t remote_port,
					struct net_addr *local_addr,
					uint16_t local_port)
{
#ifdef CONFIG_NETWORKING_WITH_IPV6
	const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
	const uip_ds6_addr_t *uip_addr;
	uip_ipaddr_t ipaddr;
#endif
	int i;
	struct net_context *context = NULL;

	/* User must provide storage for the local address. */
	if (!local_addr) {
		return NULL;
	}

#ifdef CONFIG_NETWORKING_WITH_IPV6
	if (memcmp(&local_addr->in6_addr, &in6addr_any,
				  sizeof(in6addr_any)) == 0) {
		uip_addr = uip_ds6_get_global(-1);
		if (!uip_addr) {
			uip_addr = uip_ds6_get_link_local(-1);
		}
		if (!uip_addr) {
			return NULL;
		}

		memcpy(&local_addr->in6_addr, &uip_addr->ipaddr,
		       sizeof(struct in6_addr));
	}
#else
	if (local_addr->in_addr.s_addr[0] == INADDR_ANY) {
		uip_gethostaddr((uip_ipaddr_t *)&local_addr->in_addr);
	}
#endif

	nano_sem_take(&contexts_lock, TICKS_UNLIMITED);

	if (local_port) {
		if (context_port_used(ip_proto, local_port, local_addr) < 0) {
			return NULL;
		}
	} else {
		do {
			local_port = random_rand() | 0x8000;
		} while (context_port_used(ip_proto, local_port,
					   local_addr) == -EEXIST);
	}

	for (i = 0; i < NET_MAX_CONTEXT; i++) {
		if (!contexts[i].tuple.ip_proto) {
			contexts[i].tuple.ip_proto = ip_proto;
			contexts[i].tuple.remote_addr = (struct net_addr *)remote_addr;
			contexts[i].tuple.remote_port = remote_port;
			contexts[i].tuple.local_addr = (struct net_addr *)local_addr;
			contexts[i].tuple.local_port = local_port;
			context = &contexts[i];
			break;
		}
	}

	context_sem_give(&contexts_lock);

	/* Set our local address */
#ifdef CONFIG_NETWORKING_WITH_IPV6
	memcpy(&ipaddr.u8, local_addr->in6_addr.s6_addr, sizeof(ipaddr.u8));
	if (uip_is_addr_mcast(&ipaddr)) {
		uip_ds6_maddr_add(&ipaddr);
	} else {
		uip_ds6_addr_add(&ipaddr, 0, ADDR_MANUAL);
	}
#endif

	return context;
}

void net_context_put(struct net_context *context)
{
	if (!context) {
		return;
	}

	nano_sem_take(&contexts_lock, TICKS_UNLIMITED);

	if (context->tuple.ip_proto == IPPROTO_UDP) {
		if (net_context_get_receiver_registered(context)) {
			struct simple_udp_connection *udp =
				net_context_get_udp_connection(context);
				simple_udp_unregister(udp);
		}
	}

#ifdef CONFIG_NETWORKING_WITH_TCP
	if (context->tcp_type == NET_TCP_TYPE_SERVER) {
		tcp_unlisten(UIP_HTONS(context->tuple.local_port),
			     &context->tcp);
	}
#endif

	memset(&context->tuple, 0, sizeof(context->tuple));
	memset(&context->udp, 0, sizeof(context->udp));
	context->receiver_registered = false;

	context_sem_give(&contexts_lock);
}

struct net_tuple *net_context_get_tuple(struct net_context *context)
{
	if (!context) {
		return NULL;
	}

	return &context->tuple;
}

struct nano_fifo *net_context_get_queue(struct net_context *context)
{
	if (!context)
		return NULL;

	return &context->rx_queue;
}

struct simple_udp_connection *
net_context_get_udp_connection(struct net_context *context)
{
	if (!context) {
		return NULL;
	}

	return &context->udp;
}

#ifdef CONFIG_NETWORKING_WITH_TCP
static int handle_tcp_connection(struct psock *p, enum tcp_event_type type,
				 struct net_buf *buf)
{
	PSOCK_BEGIN(p);

	if (type == TCP_WRITE_EVENT) {
		NET_DBG("Trying to send %d bytes data\n", uip_appdatalen(buf));
		PSOCK_SEND(p, buf);
	}

	PSOCK_END(p);
}

int net_context_tcp_send(struct net_buf *buf)
{
	bool connected, reset;

	/* Prepare data to be sent */

	process_post_synch(&ip_buf_context(buf)->tcp,
			   tcpip_event,
			   INT_TO_POINTER(TCP_WRITE_EVENT),
			   buf);

	connected = uip_flags(buf) & UIP_CONNECTED;
	reset = uip_flags(buf) & UIP_ABORT;

	/* If the buffer ref is 1, then the buffer was sent and it
	 * is cleared already.
	 */
	if (buf->ref == 1) {
		return 0;
	}

	return ip_buf_sent_status(buf);
}

/* This is called by contiki/ip/tcpip.c:tcpip_uipcall() when packet
 * is processed.
 */
PROCESS_THREAD(tcp, ev, data, buf, user_data)
{
	NET_DBG("tcp %p ev %p data %p buf %p user_data %p next line %d\n",
		process_thread_tcp, ev, data, buf, user_data,
		process_pt->lc);

	PROCESS_BEGIN();

	while(1) {
		PROCESS_YIELD_UNTIL(ev == tcpip_event);

	try_send:
		if (POINTER_TO_INT(data) == TCP_WRITE_EVENT) {
			/* We want to send data to peer. */
			struct net_context *context = user_data;

			if (!context) {
				continue;
			}

			context->connection_status = ip_buf_sent_status(buf);

			do {
				context = user_data;
				if (!context || !buf) {
					break;
				}

				if (!context->ps.net_buf ||
				    context->ps.net_buf != buf) {
					NET_DBG("psock init %p buf %p\n",
						&context->ps, buf);
					PSOCK_INIT(&context->ps, buf);
				}

				handle_tcp_connection(&context->ps,
						      POINTER_TO_INT(data),
						      buf);

				PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event);

				if (uip_timedout(buf)) {
					break;
				}

				if (POINTER_TO_INT(data) != TCP_WRITE_EVENT) {
					goto read_data;
				}
			} while(!(uip_closed(buf)  ||
				  uip_aborted(buf) ||
				  uip_timedout(buf)));

			context = user_data;

			if (uip_timedout(buf)) {
				ip_buf_sent_status(buf) = -ETIMEDOUT;
				if (context) {
					context->connection_status = -ETIMEDOUT;
				}
				continue;
			}

			if (context &&
			    context->tcp_type == NET_TCP_TYPE_CLIENT) {
				NET_DBG("\nConnection closed.\n");
				ip_buf_sent_status(buf) = -ECONNRESET;
			}

			continue;
		} else {
			if (buf && uip_aborted(buf)) {
				struct net_context *context = user_data;
				NET_DBG("Connection aborted context %p\n",
					user_data);
				context->connection_status = -ECONNRESET;
				continue;
			}

			if (buf && uip_connected(buf)) {
				struct net_context *context = user_data;
				NET_DBG("Connection established context %p\n",
					user_data);
				context->connection_status = -EALREADY;
				data = INT_TO_POINTER(TCP_WRITE_EVENT);
				goto try_send;
			}
		}

	read_data:
		/* We are receiving data from peer. */
		if (buf && uip_newdata(buf)) {
			struct net_buf *clone;

			if (!uip_len(buf)) {
				continue;
			}

			/* Note that uIP stack will reuse the buffer when
			 * sending ACK to peer host. The sending will happen
			 * right after this function returns. Because of this
			 * we cannot use the same buffer to pass data to
			 * application.
			 */
			clone = net_buf_clone(buf);
			if (!clone) {
				NET_ERR("No enough RX buffers, "
					"packet %p discarded\n", buf);
				continue;
			}

			ip_buf_appdata(clone) = uip_buf(clone) +
				(ip_buf_appdata(buf) - (void *)uip_buf(buf));
			ip_buf_appdatalen(clone) = uip_len(buf);
			ip_buf_len(clone) = uip_len(buf) + UIP_IPTCPH_LEN + UIP_LLH_LEN;
			ip_buf_context(clone) = user_data;
			if (!ip_buf_context(buf)) {
				ip_buf_context(buf) = user_data;
			}
			uip_set_conn(clone) = uip_conn(buf);
			uip_flags(clone) = uip_flags(buf);
			uip_flags(clone) |= UIP_CONNECTED;

			NET_DBG("packet received context %p buf %p len %d "
				"appdata %p appdatalen %d\n",
				ip_buf_context(clone),
				clone,
				ip_buf_len(clone),
				ip_buf_appdata(clone),
				ip_buf_appdatalen(clone));

			nano_fifo_put(net_context_get_queue(user_data), clone);

			ip_buf_sent_status(buf) = 1;

			/* We let the application to read the data now */
			fiber_yield();
		}
	}

	PROCESS_END();
}

int net_context_tcp_init(struct net_context *context, struct net_buf *buf,
			 enum net_tcp_type tcp_type)
{
	if (!context || context->tuple.ip_proto != IPPROTO_TCP) {
		return -EINVAL;
	}

	if (context->receiver_registered) {
		return 0;
	}

	context->receiver_registered = true;

	if (context->tcp_type == NET_TCP_TYPE_UNKNOWN) {
		/* This is the first call to this init func.
		 * If we are called by net_receive() first, then
		 * we are working as a server, if net_send() called
		 * us first, then we are the client.
		 */
		context->tcp_type = tcp_type;
	} else if (context->tcp_type != tcp_type) {
		/* This means that we have already selected that we
		 * are either client or server. Use the context
		 * value.
		 */
		return 0;
	}

	context->tcp.thread = process_thread_tcp;

	if (context->tcp_type == NET_TCP_TYPE_SERVER) {
		context->tcp.name = "TCP server";

		NET_DBG("Listen to TCP port %d\n", context->tuple.local_port);
		tcp_listen(UIP_HTONS(context->tuple.local_port),
			   &context->tcp);
#if UIP_ACTIVE_OPEN
	} else {
		context->tcp.name = "TCP client";
		context->connection_status = -EINPROGRESS;

#ifdef CONFIG_NETWORKING_WITH_IPV6
		NET_DBG("Connecting to ");
		PRINT6ADDR((const uip_ipaddr_t *)&context->tuple.remote_addr->in6_addr);
		PRINTF(" port %d\n", context->tuple.remote_port);

		tcp_connect((uip_ipaddr_t *)
			    &context->tuple.remote_addr->in6_addr,
			    UIP_HTONS(context->tuple.remote_port),
			    context, &context->tcp, buf);
#else /* CONFIG_NETWORKING_WITH_IPV6 */
		NET_DBG("Connecting to ");
		PRINT6ADDR((const uip_ipaddr_t *)&context->tuple.remote_addr->in_addr);
		PRINTF(" port %d\n", context->tuple.remote_port);

		tcp_connect((uip_ipaddr_t *)
			    &context->tuple.remote_addr->in_addr,
			    UIP_HTONS(context->tuple.remote_port),
			    context, &context->tcp, buf);
#endif /* CONFIG_NETWORKING_WITH_IPV6 */
#endif /* UIP_ACTIVE_OPEN */
	}

	context->tcp.next = NULL;
	process_start(&context->tcp, NULL, context);

	return 0;
}
#endif /* TCP */

void net_context_init(void)
{
	int i;

	nano_sem_init(&contexts_lock);

	memset(contexts, 0, sizeof(contexts));

	for (i = 0; i < NET_MAX_CONTEXT; i++) {
		nano_fifo_init(&contexts[i].rx_queue);
	}

	context_sem_give(&contexts_lock);
}

int net_context_get_receiver_registered(struct net_context *context)
{
	if (!context) {
		return -ENOENT;
	}

	if (context->receiver_registered) {
		return true;
	}

	return false;
}

void net_context_set_receiver_registered(struct net_context *context)
{
	if (!context) {
		return;
	}

	context->receiver_registered = true;
}

void net_context_unset_receiver_registered(struct net_context *context)
{
	if (!context) {
		return;
	}

	context->receiver_registered = false;
}

int net_context_get_connection_status(struct net_context *context)
{
	if (!context) {
		return -ENOENT;
	}

#if !defined(CONFIG_NETWORKING_WITH_TCP)
	return 0;
#else
	if (context->tuple.ip_proto == IPPROTO_TCP) {
		return context->connection_status;
	} else {
		return 0;
	}
#endif
}

void net_context_set_connection_status(struct net_context *context,
				      int status)
{
#if !defined(CONFIG_NETWORKING_WITH_TCP)
	return;
#else
	if (!context) {
		return;
	}

	if (context->tuple.ip_proto == IPPROTO_TCP) {
		NET_DBG("context %p status %d\n", context, status);
		context->connection_status = status;
	}
#endif
}

void *net_context_get_internal_connection(struct net_context *context)
{
#if !defined(CONFIG_NETWORKING_WITH_TCP)
	return NULL;
#else
	if (!context) {
		return NULL;
	}

	if (context->tuple.ip_proto == IPPROTO_TCP) {
		return context->conn;
	} else {
		return NULL;
	}
#endif
}

void net_context_set_internal_connection(struct net_context *context,
					 void *conn)
{
#if !defined(CONFIG_NETWORKING_WITH_TCP)
	return;
#else
	if (!context) {
		return;
	}

	if (context->tuple.ip_proto == IPPROTO_TCP) {
		context->conn = conn;
	}
#endif
}

struct net_context *net_context_find_internal_connection(void *conn)
{
#if !defined(CONFIG_NETWORKING_WITH_TCP)
	return NULL;
#else
	int i;

	for (i = 0; i < NET_MAX_CONTEXT; i++) {
		if (contexts[i].conn == conn) {
			return &contexts[i];
		}
	}

	return NULL;
#endif
}

struct net_buf *net_context_tcp_get_pending(struct net_context *context)
{
#if !defined(CONFIG_NETWORKING_WITH_TCP)
	return NULL;
#else
	if (!context) {
		return NULL;
	}

	return context->pending;
#endif
}

void net_context_tcp_set_pending(struct net_context *context,
				 struct net_buf *buf)
{
#if !defined(CONFIG_NETWORKING_WITH_TCP)
	return;
#else
	if (!context) {
		return;
	}

	context->pending = buf;
#endif
}

void net_context_tcp_set_retry_count(struct net_context *context,
				     uint8_t count)
{
#if !defined(CONFIG_NETWORKING_WITH_TCP)
	return;
#else
	if (!context) {
		return;
	}

	context->retry_count = count;
#endif
}

uint8_t net_context_tcp_get_retry_count(struct net_context *context)
{
#if !defined(CONFIG_NETWORKING_WITH_TCP)
	return 0;
#else
	if (!context) {
		return 0;
	}

	return context->retry_count;
#endif
}