etc/services - sync with NetBSD-8
[minix.git] / minix / lib / liblwip / dist / doc / NO_SYS_SampleCode.c
blobf5c6c10b612a37fa11ac5d10d2136269bbd72c5f
1 void eth_mac_irq()
3 /* Service MAC IRQ here */
5 /* Allocate pbuf from pool (avoid using heap in interrupts) */
6 struct pbuf* p = pbuf_alloc(PBUF_RAW, eth_data_count, PBUF_POOL);
8 if(p != NULL) {
9 /* Copy ethernet frame into pbuf */
10 pbuf_take(p, eth_data, eth_data_count);
12 /* Put in a queue which is processed in main loop */
13 if(!queue_try_put(&queue, p)) {
14 /* queue is full -> packet loss */
15 pbuf_free(p);
20 static err_t netif_output(struct netif *netif, struct pbuf *p)
22 LINK_STATS_INC(link.xmit);
24 /* Update SNMP stats (only if you use SNMP) */
25 MIB2_STATS_NETIF_ADD(netif, ifoutoctets, p->tot_len);
26 int unicast = ((p->payload[0] & 0x01) == 0);
27 if (unicast) {
28 MIB2_STATS_NETIF_INC(netif, ifoutucastpkts);
29 } else {
30 MIB2_STATS_NETIF_INC(netif, ifoutnucastpkts);
33 lock_interrupts();
34 pbuf_copy_partial(p, mac_send_buffer, p->tot_len, 0);
35 /* Start MAC transmit here */
36 unlock_interrupts();
38 return ERR_OK;
41 static void netif_status_callback(struct netif *netif)
43 printf("netif status changed %s\n", ip4addr_ntoa(netif_ip4_addr(netif)));
46 static err_t netif_init(struct netif *netif)
48 netif->linkoutput = netif_output;
49 netif->output = etharp_output;
50 netif->output_ip6 = ethip6_output;
51 netif->mtu = ETHERNET_MTU;
52 netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET | NETIF_FLAG_IGMP | NETIF_FLAG_MLD6;
53 MIB2_INIT_NETIF(netif, snmp_ifType_ethernet_csmacd, 100000000);
55 SMEMCPY(netif->hwaddr, your_mac_address_goes_here, sizeof(netif->hwaddr));
56 netif->hwaddr_len = sizeof(netif->hwaddr);
58 return ERR_OK;
61 void main(void)
63 struct netif netif;
65 lwip_init();
67 netif_add(&netif, IP4_ADDR_ANY, IP4_ADDR_ANY, IP4_ADDR_ANY, NULL, netif_init, netif_input);
68 netif.name[0] = 'e';
69 netif.name[1] = '0';
70 netif_create_ip6_linklocal_address(&netif, 1);
71 netif.ip6_autoconfig_enabled = 1;
72 netif_set_status_callback(&netif, netif_status_callback);
73 netif_set_default(&netif);
74 netif_set_up(&netif);
76 /* Start DHCP and HTTPD */
77 dhcp_init();
78 httpd_init();
80 while(1) {
81 /* Check link state, e.g. via MDIO communication with PHY */
82 if(link_state_changed()) {
83 if(link_is_up()) {
84 netif_set_link_up(&netif);
85 } else {
86 netif_set_link_down(&netif);
90 /* Check for received frames, feed them to lwIP */
91 lock_interrupts();
92 struct pbuf* p = queue_try_get(&queue);
93 unlock_interrupts();
95 if(p != NULL) {
96 LINK_STATS_INC(link.recv);
98 /* Update SNMP stats (only if you use SNMP) */
99 MIB2_STATS_NETIF_ADD(netif, ifinoctets, p->tot_len);
100 int unicast = ((p->payload[0] & 0x01) == 0);
101 if (unicast) {
102 MIB2_STATS_NETIF_INC(netif, ifinucastpkts);
103 } else {
104 MIB2_STATS_NETIF_INC(netif, ifinnucastpkts);
107 if(netif.input(p, &netif) != ERR_OK) {
108 pbuf_free(p);
112 /* Cyclic lwIP timers check */
113 sys_check_timeouts();
115 /* your application goes here */