Contiki 3.x
eth-proc.c
1 /*
2  * Copyright (C) 2015, Intel Corporation. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  *
13  * 3. Neither the name of the copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28  * OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <stdio.h>
32 
33 #include "contiki-net.h"
34 #include "net/ipv4/uip-neighbor.h"
35 #include "net/eth-proc.h"
36 #include "eth.h"
37 
38 #define BUF ((struct uip_eth_hdr *)&uip_buf[0])
39 #define IPBUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
40 
41 PROCESS(eth_process, "Ethernet");
42 
43 /*---------------------------------------------------------------------------*/
44 #if NETSTACK_CONF_WITH_IPV6
45 static uint8_t
46 output(const uip_lladdr_t *dest_mac)
47 {
48  if (dest_mac == NULL) {
49  /* broadcast packet */
50  memset(&BUF->dest, 0xFF, UIP_LLH_LEN);
51  } else {
52  memcpy(&BUF->dest, dest_mac, UIP_LLH_LEN);
53  }
54  memcpy(&BUF->src, uip_lladdr.addr, UIP_LLH_LEN);
55  quarkX1000_eth_send();
56 
57  return 0;
58 }
59 #else
60 static uint8_t
61 output(void)
62 {
63  uip_arp_out();
64  quarkX1000_eth_send();
65 
66  return 0;
67 }
68 #endif /* NETSTACK_CONF_WITH_IPV6 */
69 /*---------------------------------------------------------------------------*/
70 static void
71 pollhandler(void)
72 {
73  process_poll(&eth_process);
74  quarkX1000_eth_poll(&uip_len);
75 
76  if(uip_len > 0) {
77 #if NETSTACK_CONF_WITH_IPV6
78  if(BUF->type == uip_htons(UIP_ETHTYPE_IPV6)) {
79  tcpip_input();
80  }
81 #else
82  if(BUF->type == uip_htons(UIP_ETHTYPE_IP)) {
83  uip_len -= sizeof(struct uip_eth_hdr);
84  tcpip_input();
85  } else if(BUF->type == uip_htons(UIP_ETHTYPE_ARP)) {
86  uip_arp_arpin();
87  /* If the above function invocation resulted in data that
88  should be sent out on the network, the global variable
89  uip_len is set to a value > 0. */
90  if(uip_len > 0) {
91  quarkX1000_eth_send();
92  }
93  }
94 #endif /* NETSTACK_CONF_WITH_IPV6 */
95  }
96 }
97 /*---------------------------------------------------------------------------*/
98 PROCESS_THREAD(eth_process, ev, data)
99 {
101 
102  PROCESS_BEGIN();
103 
104  tcpip_set_outputfunc(output);
105 
106  process_poll(&eth_process);
107 
108  PROCESS_WAIT_UNTIL(ev == PROCESS_EVENT_EXIT);
109 
110  PROCESS_END();
111 }
112 /*---------------------------------------------------------------------------*/
uip_len
The length of the packet in the uip_buf buffer.
Definition: tcp_loader.c:75
void process_poll(struct process *p)
Request a process to be polled.
Definition: process.c:371
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
#define UIP_LLH_LEN
The link level header length.
Definition: uipopt.h:160
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
Definition: process.h:273
#define PROCESS_POLLHANDLER(handler)
Specify an action when a process is polled.
Definition: process.h:242
The Ethernet header.
Definition: uip_arp.h:60
static void pollhandler(void)
USB Poll Handler.
Definition: usb_task.c:167
#define NULL
The null pointer.
void uip_arp_out(void)
Prepend Ethernet header to an outbound IP packet and see if we need to send out an ARP request...
Definition: uip_arp.c:363
802.3 address
Definition: uip.h:129
static uint8_t output(const uip_lladdr_t *localdest)
Take an IP packet and format it to be sent on an 802.15.4 network using 6lowpan.
Definition: sicslowpan.c:1275
CCIF uip_lladdr_t uip_lladdr
Host L2 address.
Definition: uip.c:118
#define PROCESS_WAIT_UNTIL(c)
Wait for a condition to occur.
Definition: process.h:192
void uip_arp_arpin(void)
ARP processing for incoming IP packets.
Definition: uip_arp.c:283
void tcpip_input(void)
Deliver an incoming packet to the TCP/IP stack.
Definition: tcpip.c:522
Header file for database of link-local neighbors, used by IPv6 code and to be used by future ...
CCIF uint16_t uip_htons(uint16_t val)
Convert a 16-bit quantity from host byte order to network byte order.
Definition: uip.c:1948
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120