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