Bonjour/Zerconf library

Dependencies:   mbed

Committer:
dirkx
Date:
Wed Jul 21 19:25:56 2010 +0000
Revision:
0:355018f44c9f

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dirkx 0:355018f44c9f 1
dirkx 0:355018f44c9f 2 /*
dirkx 0:355018f44c9f 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
dirkx 0:355018f44c9f 4
dirkx 0:355018f44c9f 5 Permission is hereby granted, free of charge, to any person obtaining a copy
dirkx 0:355018f44c9f 6 of this software and associated documentation files (the "Software"), to deal
dirkx 0:355018f44c9f 7 in the Software without restriction, including without limitation the rights
dirkx 0:355018f44c9f 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
dirkx 0:355018f44c9f 9 copies of the Software, and to permit persons to whom the Software is
dirkx 0:355018f44c9f 10 furnished to do so, subject to the following conditions:
dirkx 0:355018f44c9f 11
dirkx 0:355018f44c9f 12 The above copyright notice and this permission notice shall be included in
dirkx 0:355018f44c9f 13 all copies or substantial portions of the Software.
dirkx 0:355018f44c9f 14
dirkx 0:355018f44c9f 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
dirkx 0:355018f44c9f 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
dirkx 0:355018f44c9f 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
dirkx 0:355018f44c9f 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
dirkx 0:355018f44c9f 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
dirkx 0:355018f44c9f 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
dirkx 0:355018f44c9f 21 THE SOFTWARE.
dirkx 0:355018f44c9f 22 */
dirkx 0:355018f44c9f 23
dirkx 0:355018f44c9f 24 #include "netCfg.h"
dirkx 0:355018f44c9f 25 #if NET_ETH
dirkx 0:355018f44c9f 26
dirkx 0:355018f44c9f 27 #include "mbed.h"
dirkx 0:355018f44c9f 28
dirkx 0:355018f44c9f 29 Ethernet *pEth = NULL;
dirkx 0:355018f44c9f 30 #ifdef __cplusplus
dirkx 0:355018f44c9f 31 extern "C" {
dirkx 0:355018f44c9f 32 #endif
dirkx 0:355018f44c9f 33
dirkx 0:355018f44c9f 34 #include "lwip/opt.h"
dirkx 0:355018f44c9f 35
dirkx 0:355018f44c9f 36 #include "lwip/def.h"
dirkx 0:355018f44c9f 37 #include "lwip/pbuf.h"
dirkx 0:355018f44c9f 38 #include "lwip/sys.h"
dirkx 0:355018f44c9f 39 #include "lwip/stats.h"
dirkx 0:355018f44c9f 40 #include "netif/etharp.h"
dirkx 0:355018f44c9f 41 #include "string.h"
dirkx 0:355018f44c9f 42
dirkx 0:355018f44c9f 43 //#include "eth_drv.h"
dirkx 0:355018f44c9f 44
dirkx 0:355018f44c9f 45 #define IFNAME0 'E'
dirkx 0:355018f44c9f 46 #define IFNAME1 'X'
dirkx 0:355018f44c9f 47
dirkx 0:355018f44c9f 48 #define min(x,y) (((x)<(y))?(x):(y))
dirkx 0:355018f44c9f 49
dirkx 0:355018f44c9f 50 struct netif* eth_netif;
dirkx 0:355018f44c9f 51
dirkx 0:355018f44c9f 52 static err_t eth_output(struct netif *netif, struct pbuf *p) {
dirkx 0:355018f44c9f 53 #if ETH_PAD_SIZE
dirkx 0:355018f44c9f 54 pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
dirkx 0:355018f44c9f 55 #endif
dirkx 0:355018f44c9f 56
dirkx 0:355018f44c9f 57 do {
dirkx 0:355018f44c9f 58 pEth->write((const char *)p->payload, p->len);
dirkx 0:355018f44c9f 59 } while((p = p->next)!=NULL);
dirkx 0:355018f44c9f 60
dirkx 0:355018f44c9f 61 pEth->send();
dirkx 0:355018f44c9f 62
dirkx 0:355018f44c9f 63 #if ETH_PAD_SIZE
dirkx 0:355018f44c9f 64 pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
dirkx 0:355018f44c9f 65 #endif
dirkx 0:355018f44c9f 66
dirkx 0:355018f44c9f 67 LINK_STATS_INC(link.xmit);
dirkx 0:355018f44c9f 68 return ERR_OK;
dirkx 0:355018f44c9f 69 }
dirkx 0:355018f44c9f 70
dirkx 0:355018f44c9f 71 /*
dirkx 0:355018f44c9f 72 void show(char *buf, int size) {
dirkx 0:355018f44c9f 73 printf("Destination: %02hx:%02hx:%02hx:%02hx:%02hx:%02hx\n",
dirkx 0:355018f44c9f 74 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
dirkx 0:355018f44c9f 75 printf("Source: %02hx:%02hx:%02hx:%02hx:%02hx:%02hx\n",
dirkx 0:355018f44c9f 76 buf[6], buf[7], buf[8], buf[9], buf[10], buf[11]);
dirkx 0:355018f44c9f 77
dirkx 0:355018f44c9f 78 printf("Type %hd\n", htons((short)buf[12]));
dirkx 0:355018f44c9f 79
dirkx 0:355018f44c9f 80 // hexview(buf, size);
dirkx 0:355018f44c9f 81 }
dirkx 0:355018f44c9f 82 */
dirkx 0:355018f44c9f 83
dirkx 0:355018f44c9f 84 void eth_poll() {
dirkx 0:355018f44c9f 85 struct eth_hdr *ethhdr;
dirkx 0:355018f44c9f 86 struct pbuf *frame, *p;
dirkx 0:355018f44c9f 87 int len, read;
dirkx 0:355018f44c9f 88
dirkx 0:355018f44c9f 89 while((len = pEth->receive()) != 0) {
dirkx 0:355018f44c9f 90 frame = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
dirkx 0:355018f44c9f 91 if(frame == NULL) {
dirkx 0:355018f44c9f 92 return;
dirkx 0:355018f44c9f 93 }
dirkx 0:355018f44c9f 94 p = frame;
dirkx 0:355018f44c9f 95 /* no packet could be read, silently ignore this */
dirkx 0:355018f44c9f 96 if (p == NULL) return;
dirkx 0:355018f44c9f 97 do {
dirkx 0:355018f44c9f 98 read = pEth->read((char *)p->payload, p->len);
dirkx 0:355018f44c9f 99 p = p->next;
dirkx 0:355018f44c9f 100 } while(p != NULL && read != 0);
dirkx 0:355018f44c9f 101
dirkx 0:355018f44c9f 102 #if ETH_PAD_SIZE
dirkx 0:355018f44c9f 103 pbuf_header(p, ETH_PAD_SIZE);
dirkx 0:355018f44c9f 104 #endif
dirkx 0:355018f44c9f 105
dirkx 0:355018f44c9f 106 ethhdr = (struct eth_hdr *)(frame->payload);
dirkx 0:355018f44c9f 107
dirkx 0:355018f44c9f 108 // show((char*)ethhdr, 13);
dirkx 0:355018f44c9f 109
dirkx 0:355018f44c9f 110 /*
dirkx 0:355018f44c9f 111 switch(htons(ethhdr->type)) {
dirkx 0:355018f44c9f 112
dirkx 0:355018f44c9f 113 case ETHTYPE_IP:
dirkx 0:355018f44c9f 114 etharp_ip_input(gnetif, frame);
dirkx 0:355018f44c9f 115 pbuf_header(frame, -((s16_t) sizeof(struct eth_hdr)));
dirkx 0:355018f44c9f 116 gnetif->input(frame, gnetif);
dirkx 0:355018f44c9f 117 break;
dirkx 0:355018f44c9f 118
dirkx 0:355018f44c9f 119 case ETHTYPE_ARP:
dirkx 0:355018f44c9f 120 etharp_arp_input(gnetif, (struct eth_addr *)(gnetif->hwaddr), frame);
dirkx 0:355018f44c9f 121 break;
dirkx 0:355018f44c9f 122
dirkx 0:355018f44c9f 123 default:
dirkx 0:355018f44c9f 124 break;
dirkx 0:355018f44c9f 125 }*/
dirkx 0:355018f44c9f 126
dirkx 0:355018f44c9f 127
dirkx 0:355018f44c9f 128
dirkx 0:355018f44c9f 129 //ethernet_input(frame, gnetif);
dirkx 0:355018f44c9f 130
dirkx 0:355018f44c9f 131 switch (htons(ethhdr->type)) {
dirkx 0:355018f44c9f 132 /* IP or ARP packet? */
dirkx 0:355018f44c9f 133 case ETHTYPE_IP:
dirkx 0:355018f44c9f 134 case ETHTYPE_ARP:
dirkx 0:355018f44c9f 135 #if PPPOE_SUPPORT
dirkx 0:355018f44c9f 136 /* PPPoE packet? */
dirkx 0:355018f44c9f 137 case ETHTYPE_PPPOEDISC:
dirkx 0:355018f44c9f 138 case ETHTYPE_PPPOE:
dirkx 0:355018f44c9f 139 #endif /* PPPOE_SUPPORT */
dirkx 0:355018f44c9f 140 /* full packet send to tcpip_thread to process */
dirkx 0:355018f44c9f 141 //if (netif->input(p, gnetif)!=ERR_OK)
dirkx 0:355018f44c9f 142 if (ethernet_input(frame, eth_netif)!=ERR_OK)
dirkx 0:355018f44c9f 143 { LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
dirkx 0:355018f44c9f 144 pbuf_free(frame);
dirkx 0:355018f44c9f 145 frame = NULL;
dirkx 0:355018f44c9f 146 }
dirkx 0:355018f44c9f 147 break;
dirkx 0:355018f44c9f 148
dirkx 0:355018f44c9f 149 default:
dirkx 0:355018f44c9f 150 pbuf_free(frame);
dirkx 0:355018f44c9f 151 frame = NULL;
dirkx 0:355018f44c9f 152 break;
dirkx 0:355018f44c9f 153 }
dirkx 0:355018f44c9f 154
dirkx 0:355018f44c9f 155 /* pbuf_free(frame); */
dirkx 0:355018f44c9f 156 }
dirkx 0:355018f44c9f 157
dirkx 0:355018f44c9f 158
dirkx 0:355018f44c9f 159
dirkx 0:355018f44c9f 160
dirkx 0:355018f44c9f 161 }
dirkx 0:355018f44c9f 162
dirkx 0:355018f44c9f 163 err_t eth_init(struct netif *netif) {
dirkx 0:355018f44c9f 164 LWIP_ASSERT("netif != NULL", (netif != NULL));
dirkx 0:355018f44c9f 165
dirkx 0:355018f44c9f 166 NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, 0x2EA);
dirkx 0:355018f44c9f 167
dirkx 0:355018f44c9f 168 /* maximum transfer unit */
dirkx 0:355018f44c9f 169 netif->mtu = 0x2EA;
dirkx 0:355018f44c9f 170
dirkx 0:355018f44c9f 171 /* device capabilities */
dirkx 0:355018f44c9f 172 /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
dirkx 0:355018f44c9f 173 netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
dirkx 0:355018f44c9f 174
dirkx 0:355018f44c9f 175 netif->state = NULL;
dirkx 0:355018f44c9f 176 eth_netif = netif;
dirkx 0:355018f44c9f 177
dirkx 0:355018f44c9f 178 netif->name[0] = IFNAME0;
dirkx 0:355018f44c9f 179 netif->name[1] = IFNAME1;
dirkx 0:355018f44c9f 180
dirkx 0:355018f44c9f 181 /* We directly use etharp_output() here to save a function call.
dirkx 0:355018f44c9f 182 * You can instead declare your own function an call etharp_output()
dirkx 0:355018f44c9f 183 * from it if you have to do some checks before sending (e.g. if link
dirkx 0:355018f44c9f 184 * is available...) */
dirkx 0:355018f44c9f 185 netif->output = etharp_output;
dirkx 0:355018f44c9f 186 netif->linkoutput = eth_output;
dirkx 0:355018f44c9f 187
dirkx 0:355018f44c9f 188 pEth = new Ethernet();
dirkx 0:355018f44c9f 189
dirkx 0:355018f44c9f 190 return ERR_OK;
dirkx 0:355018f44c9f 191 }
dirkx 0:355018f44c9f 192
dirkx 0:355018f44c9f 193 void eth_free()
dirkx 0:355018f44c9f 194 {
dirkx 0:355018f44c9f 195 if(pEth)
dirkx 0:355018f44c9f 196 delete pEth;
dirkx 0:355018f44c9f 197 pEth = NULL;
dirkx 0:355018f44c9f 198 }
dirkx 0:355018f44c9f 199
dirkx 0:355018f44c9f 200 void eth_address(char* mac) {
dirkx 0:355018f44c9f 201 pEth->address(mac);
dirkx 0:355018f44c9f 202 }
dirkx 0:355018f44c9f 203
dirkx 0:355018f44c9f 204 #ifdef __cplusplus
dirkx 0:355018f44c9f 205 };
dirkx 0:355018f44c9f 206 #endif
dirkx 0:355018f44c9f 207
dirkx 0:355018f44c9f 208 #endif