Web server based weather station using Sparkfun Weather Meters.

Dependencies:   FatFileSystem mbed WeatherMeters SDFileSystem

Committer:
AdamGreen
Date:
Thu Feb 23 21:38:39 2012 +0000
Revision:
0:616601bde9fb

        

Who changed what in which revision?

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