57ce35f41e819ee344b32b5965e1a7f9a58f54e0
[bertos.git] / bertos / drv / eth.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \author Andrea Righi <arighi@develer.com>
34  *
35  * \brief Ethernet standard descriptors
36  *
37  * $WIZ$ module_name = "eth"
38  * $WIZ$ module_configuration = "bertos/cfg/cfg_eth.h"
39  * $WIZ$ module_supports = "at91sam7x or sam3x"
40  * $WIZ$ module_depends = "timer"
41  */
42
43 #ifndef DRV_ETH_H
44 #define DRV_ETH_H
45
46 #include <cpu/types.h>
47
48 #define ETH_ADDR_LEN    6
49 #define ETH_HEAD_LEN    14
50 #define ETH_DATA_LEN    1500
51 #define ETH_FRAME_LEN   (ETH_HEAD_LEN + ETH_DATA_LEN)
52
53 #define ETH_TYPE_IP     0x0800
54
55 typedef union Ethernet
56 {
57         struct
58         {
59                 uint8_t  dst[ETH_ADDR_LEN];
60                 uint8_t  src[ETH_ADDR_LEN];
61                 uint16_t type;
62                 uint8_t data[0];
63         };
64         uint8_t raw[ETH_FRAME_LEN];
65 } PACKED Ethernet;
66
67 /**
68  * Determine if ethernet address \a addr is a all zero.
69  */
70 INLINE int eth_addrIsZero(const uint8_t *addr)
71 {
72         return !(addr[0] | addr[1] | addr[2] |
73                  addr[3] | addr[4] | addr[5]);
74 }
75
76 /**
77  * Determine if ethernet address \a addr is a multicast address.
78  */
79 INLINE int eth_addrIsMcast(const uint8_t *addr)
80 {
81         return (0x01 & addr[0]);
82 }
83
84 /**
85  * Determine if ethernet address \a addr is locally-assigned (IEEE 802).
86  */
87 INLINE int eth_addrIsLocal(const uint8_t *addr)
88 {
89         return (0x02 & addr[0]);
90 }
91
92 /**
93  * Determine if ethernet address \a addr is broadcast.
94  */
95 INLINE bool eth_addrIsBcast(const uint8_t *addr)
96 {
97         return (addr[0] & addr[1] & addr[2] &
98                 addr[3] & addr[4] & addr[5]) == 0xff;
99 }
100
101 /**
102  * Check if the ethernet address \a addr is not all zero, is not a multicast
103  * address, and is not broadcast.
104  */
105 INLINE bool eth_addrIsValid(const uint8_t *addr)
106 {
107         return !eth_addrIsMcast(addr) && !eth_addrIsZero(addr);
108 }
109
110 /**
111  * Compare two ethernet addresses: \a addr1 and \a addr2, returns 0 if equal.
112  */
113 INLINE bool eth_addrCmp(const uint8_t *addr1, const uint8_t *addr2)
114 {
115         return !!((addr1[0] ^ addr2[0]) |
116                         (addr1[1] ^ addr2[1]) |
117                         (addr1[2] ^ addr2[2]) |
118                         (addr1[3] ^ addr2[3]) |
119                         (addr1[4] ^ addr2[4]) |
120                         (addr1[5] ^ addr2[5]));
121 }
122
123 ssize_t eth_putFrame(const uint8_t *buf, size_t len);
124 void eth_sendFrame(void);
125
126 size_t eth_getFrameLen(void);
127 ssize_t eth_getFrame(uint8_t *buf, size_t len);
128
129 ssize_t eth_send(const uint8_t *buf, size_t len);
130 ssize_t eth_recv(uint8_t *buf, size_t len);
131
132 int eth_init(void);
133
134 extern const uint8_t mac_addr[ETH_ADDR_LEN];
135
136 #endif /* DRV_ETH_H */