ReactOS 0.4.16-dev-258-g81860b4
NO_SYS_SampleCode.c
Go to the documentation of this file.
1void
3{
4 /* Service MAC IRQ here */
5
6 /* Allocate pbuf from pool (avoid using heap in interrupts) */
7 struct pbuf* p = pbuf_alloc(PBUF_RAW, eth_data_count, PBUF_POOL);
8
9 if(p != NULL) {
10 /* Copy ethernet frame into pbuf */
11 pbuf_take(p, eth_data, eth_data_count);
12
13 /* Put in a queue which is processed in main loop */
14 if(!queue_try_put(&queue, p)) {
15 /* queue is full -> packet loss */
16 pbuf_free(p);
17 }
18 }
19}
20
21static err_t
22netif_output(struct netif *netif, struct pbuf *p)
23{
24 LINK_STATS_INC(link.xmit);
25
26 /* Update SNMP stats (only if you use SNMP) */
27 MIB2_STATS_NETIF_ADD(netif, ifoutoctets, p->tot_len);
28 int unicast = ((p->payload[0] & 0x01) == 0);
29 if (unicast) {
30 MIB2_STATS_NETIF_INC(netif, ifoutucastpkts);
31 } else {
32 MIB2_STATS_NETIF_INC(netif, ifoutnucastpkts);
33 }
34
35 lock_interrupts();
36 pbuf_copy_partial(p, mac_send_buffer, p->tot_len, 0);
37 /* Start MAC transmit here */
38 unlock_interrupts();
39
40 return ERR_OK;
41}
42
43static void
45{
46 printf("netif status changed %s\n", ip4addr_ntoa(netif_ip4_addr(netif)));
47}
48
49static err_t
51{
53 netif->output = etharp_output;
54 netif->output_ip6 = ethip6_output;
55 netif->mtu = ETHERNET_MTU;
57 MIB2_INIT_NETIF(netif, snmp_ifType_ethernet_csmacd, 100000000);
58
59 SMEMCPY(netif->hwaddr, your_mac_address_goes_here, ETH_HWADDR_LEN);
61
62 return ERR_OK;
63}
64
65void
66main(void)
67{
68 struct netif netif;
69
70 lwip_init();
71
72 netif_add(&netif, IP4_ADDR_ANY, IP4_ADDR_ANY, IP4_ADDR_ANY, NULL, netif_init, netif_input);
73 netif.name[0] = 'e';
74 netif.name[1] = '0';
75 netif_create_ip6_linklocal_address(&netif, 1);
76 netif_set_status_callback(&netif, netif_status_callback);
79
80 /* Start DHCP and HTTPD */
81 dhcp_start(&netif );
82 httpd_init();
83
84 while(1) {
85 /* Check link state, e.g. via MDIO communication with PHY */
86 if(link_state_changed()) {
87 if(link_is_up()) {
89 } else {
91 }
92 }
93
94 /* Check for received frames, feed them to lwIP */
95 lock_interrupts();
96 struct pbuf* p = queue_try_get(&queue);
97 unlock_interrupts();
98
99 if(p != NULL) {
100 LINK_STATS_INC(link.recv);
101
102 /* Update SNMP stats (only if you use SNMP) */
103 MIB2_STATS_NETIF_ADD(netif, ifinoctets, p->tot_len);
104 int unicast = ((p->payload[0] & 0x01) == 0);
105 if (unicast) {
106 MIB2_STATS_NETIF_INC(netif, ifinucastpkts);
107 } else {
108 MIB2_STATS_NETIF_INC(netif, ifinnucastpkts);
109 }
110
111 if(netif.input(p, &netif) != ERR_OK) {
112 pbuf_free(p);
113 }
114 }
115
116 /* Cyclic lwIP timers check */
117 sys_check_timeouts();
118
119 /* your application goes here */
120 }
121}
static void netif_status_callback(struct netif *netif)
static err_t netif_output(struct netif *netif, struct pbuf *p)
void main(void)
void eth_mac_irq()
Definition: _queue.h:67
const WCHAR * link
Definition: db.cpp:997
#define NULL
Definition: types.h:112
void httpd_init(void)
#define MIB2_STATS_NETIF_ADD(n, x, val)
Definition: snmp.h:140
#define MIB2_STATS_NETIF_INC(n, x)
Definition: snmp.h:139
#define MIB2_INIT_NETIF(netif, type, speed)
Definition: snmp.h:138
#define printf
Definition: freeldr.h:97
GLfloat GLfloat p
Definition: glext.h:8902
s8_t err_t
Definition: err.h:96
@ ERR_OK
Definition: err.h:55
void lwip_init(void)
Definition: init.c:339
err_t netif_input(struct pbuf *p, struct netif *inp)
Definition: netif.c:228
#define SMEMCPY(dst, src, len)
Definition: opt.h:145
#define NETIF_FLAG_ETHERNET
Definition: netif.h:101
#define NETIF_FLAG_ETHARP
Definition: netif.h:97
#define NETIF_FLAG_MLD6
Definition: netif.h:107
#define NETIF_FLAG_IGMP
Definition: netif.h:104
#define NETIF_FLAG_BROADCAST
Definition: netif.h:87
void netif_set_link_down(struct netif *netif)
Definition: netif.c:1056
struct netif * netif_add(struct netif *netif, void *state, netif_init_fn init, netif_input_fn input)
Definition: netif.c:287
void netif_set_default(struct netif *netif)
Definition: netif.c:849
void netif_set_link_up(struct netif *netif)
Definition: netif.c:1018
void netif_set_up(struct netif *netif)
Definition: netif.c:871
struct pbuf * pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
Definition: pbuf.c:224
u8_t pbuf_free(struct pbuf *p)
Definition: pbuf.c:727
u16_t pbuf_copy_partial(const struct pbuf *buf, void *dataptr, u16_t len, u16_t offset)
Definition: pbuf.c:1058
err_t pbuf_take(struct pbuf *buf, const void *dataptr, u16_t len)
Definition: pbuf.c:1227
@ PBUF_POOL
Definition: pbuf.h:167
@ PBUF_RAW
Definition: pbuf.h:111
#define ETH_HWADDR_LEN
Definition: ethernet.h:51
void netif_init(void)
Definition: netif.c:188
#define LINK_STATS_INC(x)
Definition: stats.h:384
Definition: netif.h:269
u8_t flags
Definition: netif.h:354
char name[2]
Definition: netif.h:356
netif_input_fn input
Definition: netif.h:297
u8_t hwaddr[NETIF_MAX_HWADDR_LEN]
Definition: netif.h:350
u16_t mtu
Definition: netif.h:344
netif_linkoutput_fn linkoutput
Definition: netif.h:308
u8_t hwaddr_len
Definition: netif.h:352
Definition: pbuf.h:186