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/if/eth/EthernetNetIf.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 | #pragma diag_remark 1464 |
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 "EthernetNetIf.h" |
mafischl | 0:d9266031f832 | 25 | |
mafischl | 0:d9266031f832 | 26 | #include "netif/etharp.h" |
mafischl | 0:d9266031f832 | 27 | #include "lwip/dhcp.h" |
mafischl | 0:d9266031f832 | 28 | #include "lwip/dns.h" |
mafischl | 0:d9266031f832 | 29 | #include "lwip/igmp.h" |
mafischl | 0:d9266031f832 | 30 | #include "TextLCD.h" |
mafischl | 0:d9266031f832 | 31 | |
mafischl | 0:d9266031f832 | 32 | #include "drv/eth/eth_drv.h" |
mafischl | 0:d9266031f832 | 33 | #include "mbed.h" |
mafischl | 0:d9266031f832 | 34 | |
mafischl | 0:d9266031f832 | 35 | //#define __DEBUG |
mafischl | 0:d9266031f832 | 36 | #include "dbg/dbg.h" |
mafischl | 0:d9266031f832 | 37 | |
mafischl | 0:d9266031f832 | 38 | #include "netCfg.h" |
mafischl | 0:d9266031f832 | 39 | #if NET_ETH |
mafischl | 0:d9266031f832 | 40 | TextLCD lcd2(p15, p16, p17, p11, p12, p20); // rs, e, d4-d7 |
mafischl | 0:d9266031f832 | 41 | |
mafischl | 0:d9266031f832 | 42 | |
mafischl | 0:d9266031f832 | 43 | EthernetNetIf::EthernetNetIf(const char* hostname) : LwipNetIf(), m_ethArpTimer(), m_dhcpCoarseTimer(), m_dhcpFineTimer(), m_igmpTimer(), m_pNetIf(NULL), |
mafischl | 0:d9266031f832 | 44 | m_netmask(255,255,255,255), m_gateway(), m_hostname(hostname) |
mafischl | 0:d9266031f832 | 45 | { |
mafischl | 0:d9266031f832 | 46 | //m_hostname = NULL; |
mafischl | 0:d9266031f832 | 47 | m_pNetIf = new netif; |
mafischl | 0:d9266031f832 | 48 | m_useDhcp = true; |
mafischl | 0:d9266031f832 | 49 | m_pDhcp = new dhcp; |
mafischl | 0:d9266031f832 | 50 | m_setup = false; |
mafischl | 0:d9266031f832 | 51 | } |
mafischl | 0:d9266031f832 | 52 | |
mafischl | 0:d9266031f832 | 53 | EthernetNetIf::EthernetNetIf(IpAddr ip, IpAddr netmask, IpAddr gateway, IpAddr dns) : LwipNetIf(), m_ethArpTimer(), m_dhcpCoarseTimer(), m_dhcpFineTimer(), m_igmpTimer(), m_pNetIf(NULL), m_hostname(NULL) //W/o DHCP |
mafischl | 0:d9266031f832 | 54 | { |
mafischl | 0:d9266031f832 | 55 | m_hostname = NULL; |
mafischl | 0:d9266031f832 | 56 | m_netmask = netmask; |
mafischl | 0:d9266031f832 | 57 | m_gateway = gateway; |
mafischl | 0:d9266031f832 | 58 | m_ip = ip; |
mafischl | 0:d9266031f832 | 59 | m_pNetIf = new netif; |
mafischl | 0:d9266031f832 | 60 | dns_setserver(0, &dns.getStruct()); |
mafischl | 0:d9266031f832 | 61 | m_useDhcp = false; |
mafischl | 0:d9266031f832 | 62 | m_setup = false; |
mafischl | 0:d9266031f832 | 63 | } |
mafischl | 0:d9266031f832 | 64 | |
mafischl | 0:d9266031f832 | 65 | EthernetNetIf::~EthernetNetIf() |
mafischl | 0:d9266031f832 | 66 | { |
mafischl | 0:d9266031f832 | 67 | if(m_pNetIf) |
mafischl | 0:d9266031f832 | 68 | { |
mafischl | 0:d9266031f832 | 69 | igmp_stop(m_pNetIf); //Stop IGMP processing |
mafischl | 0:d9266031f832 | 70 | netif_set_down(m_pNetIf); |
mafischl | 0:d9266031f832 | 71 | netif_remove(m_pNetIf); |
mafischl | 0:d9266031f832 | 72 | delete m_pNetIf; |
mafischl | 0:d9266031f832 | 73 | eth_free(); |
mafischl | 0:d9266031f832 | 74 | } |
mafischl | 0:d9266031f832 | 75 | |
mafischl | 0:d9266031f832 | 76 | if (m_pDhcp) |
mafischl | 0:d9266031f832 | 77 | delete m_pDhcp; |
mafischl | 0:d9266031f832 | 78 | } |
mafischl | 0:d9266031f832 | 79 | |
mafischl | 0:d9266031f832 | 80 | EthernetErr EthernetNetIf::setup(int timeout_ms /*= 15000*/) |
mafischl | 0:d9266031f832 | 81 | { |
mafischl | 0:d9266031f832 | 82 | if (m_setup) |
mafischl | 0:d9266031f832 | 83 | { |
mafischl | 0:d9266031f832 | 84 | igmp_stop(m_pNetIf); |
mafischl | 0:d9266031f832 | 85 | netif_set_down(m_pNetIf); |
mafischl | 0:d9266031f832 | 86 | netif_remove(m_pNetIf); |
mafischl | 0:d9266031f832 | 87 | delete m_pNetIf; |
mafischl | 0:d9266031f832 | 88 | eth_free(); |
mafischl | 0:d9266031f832 | 89 | m_pNetIf = new netif; |
mafischl | 0:d9266031f832 | 90 | } |
mafischl | 0:d9266031f832 | 91 | |
mafischl | 0:d9266031f832 | 92 | LwipNetIf::init(); |
mafischl | 0:d9266031f832 | 93 | //m_ethArpTicker.attach_us(ðarp_tmr, ARP_TMR_INTERVAL * 1000); // = 5s in etharp.h |
mafischl | 0:d9266031f832 | 94 | m_ethArpTimer.start(); |
mafischl | 0:d9266031f832 | 95 | if(m_useDhcp) |
mafischl | 0:d9266031f832 | 96 | { |
mafischl | 0:d9266031f832 | 97 | //m_dhcpCoarseTicker.attach(&dhcp_coarse_tmr, DHCP_COARSE_TIMER_SECS); // = 60s in dhcp.h |
mafischl | 0:d9266031f832 | 98 | //m_dhcpFineTicker.attach_us(&dhcp_fine_tmr, DHCP_FINE_TIMER_MSECS * 1000); // = 500ms in dhcp.h |
mafischl | 0:d9266031f832 | 99 | m_dhcpCoarseTimer.start(); |
mafischl | 0:d9266031f832 | 100 | m_dhcpFineTimer.start(); |
mafischl | 0:d9266031f832 | 101 | } |
mafischl | 0:d9266031f832 | 102 | m_pNetIf->hwaddr_len = ETHARP_HWADDR_LEN; //6 |
mafischl | 0:d9266031f832 | 103 | eth_address((char *)m_pNetIf->hwaddr); |
mafischl | 0:d9266031f832 | 104 | |
mafischl | 0:d9266031f832 | 105 | DBG("HW Addr is : %02x:%02x:%02x:%02x:%02x:%02x.\n", |
mafischl | 0:d9266031f832 | 106 | m_pNetIf->hwaddr[0], m_pNetIf->hwaddr[1], m_pNetIf->hwaddr[2], |
mafischl | 0:d9266031f832 | 107 | m_pNetIf->hwaddr[3], m_pNetIf->hwaddr[4], m_pNetIf->hwaddr[5]); |
mafischl | 0:d9266031f832 | 108 | |
mafischl | 0:d9266031f832 | 109 | m_pNetIf = netif_add(m_pNetIf, &(m_ip.getStruct()), &(m_netmask.getStruct()), &(m_gateway.getStruct()), NULL, eth_init, ip_input);//ethernet_input);// ip_input); |
mafischl | 0:d9266031f832 | 110 | m_pNetIf->hostname = (char *)m_hostname; |
mafischl | 0:d9266031f832 | 111 | netif_set_default(m_pNetIf); |
mafischl | 0:d9266031f832 | 112 | |
mafischl | 0:d9266031f832 | 113 | //DBG("\r\nStarting DHCP.\r\n"); |
mafischl | 0:d9266031f832 | 114 | |
mafischl | 0:d9266031f832 | 115 | if(m_useDhcp) |
mafischl | 0:d9266031f832 | 116 | { |
mafischl | 0:d9266031f832 | 117 | dhcp_set_struct(m_pNetIf, m_pDhcp); |
mafischl | 0:d9266031f832 | 118 | dhcp_start(m_pNetIf); |
mafischl | 0:d9266031f832 | 119 | DBG("DHCP Started, waiting for IP...\n"); |
mafischl | 0:d9266031f832 | 120 | } |
mafischl | 0:d9266031f832 | 121 | else |
mafischl | 0:d9266031f832 | 122 | { |
mafischl | 0:d9266031f832 | 123 | netif_set_up(m_pNetIf); |
mafischl | 0:d9266031f832 | 124 | } |
mafischl | 0:d9266031f832 | 125 | |
mafischl | 0:d9266031f832 | 126 | Timer timeout; |
mafischl | 0:d9266031f832 | 127 | timeout.start(); |
mafischl | 0:d9266031f832 | 128 | while( !netif_is_up(m_pNetIf) ) //Wait until device is up |
mafischl | 0:d9266031f832 | 129 | { |
mafischl | 0:d9266031f832 | 130 | if(m_useDhcp) |
mafischl | 0:d9266031f832 | 131 | { |
mafischl | 0:d9266031f832 | 132 | if(m_dhcpFineTimer.read_ms()>=DHCP_FINE_TIMER_MSECS) |
mafischl | 0:d9266031f832 | 133 | { |
mafischl | 0:d9266031f832 | 134 | m_dhcpFineTimer.reset(); |
mafischl | 0:d9266031f832 | 135 | dhcp_fine_tmr(); |
mafischl | 0:d9266031f832 | 136 | } |
mafischl | 0:d9266031f832 | 137 | if(m_dhcpCoarseTimer.read()>=DHCP_COARSE_TIMER_SECS) |
mafischl | 0:d9266031f832 | 138 | { |
mafischl | 0:d9266031f832 | 139 | m_dhcpCoarseTimer.reset(); |
mafischl | 0:d9266031f832 | 140 | dhcp_coarse_tmr(); |
mafischl | 0:d9266031f832 | 141 | } |
mafischl | 0:d9266031f832 | 142 | } |
mafischl | 0:d9266031f832 | 143 | poll(); |
mafischl | 0:d9266031f832 | 144 | if( timeout.read_ms() > timeout_ms ) |
mafischl | 0:d9266031f832 | 145 | { |
mafischl | 0:d9266031f832 | 146 | //Abort |
mafischl | 0:d9266031f832 | 147 | if(m_useDhcp) |
mafischl | 0:d9266031f832 | 148 | dhcp_stop(m_pNetIf); |
mafischl | 0:d9266031f832 | 149 | else |
mafischl | 0:d9266031f832 | 150 | netif_set_down(m_pNetIf); |
mafischl | 0:d9266031f832 | 151 | DBG("\r\nTimeout.\r\n"); |
mafischl | 0:d9266031f832 | 152 | m_setup = true; |
mafischl | 0:d9266031f832 | 153 | return ETH_TIMEOUT; |
mafischl | 0:d9266031f832 | 154 | } |
mafischl | 0:d9266031f832 | 155 | } |
mafischl | 0:d9266031f832 | 156 | |
mafischl | 0:d9266031f832 | 157 | #if LWIP_IGMP |
mafischl | 0:d9266031f832 | 158 | igmp_start(m_pNetIf); //Start IGMP processing |
mafischl | 0:d9266031f832 | 159 | #endif |
mafischl | 0:d9266031f832 | 160 | |
mafischl | 0:d9266031f832 | 161 | m_ip = IpAddr(&(m_pNetIf->ip_addr)); |
mafischl | 0:d9266031f832 | 162 | |
mafischl | 0:d9266031f832 | 163 | DBG("Connected, IP : %d.%d.%d.%d\n", m_ip[0], m_ip[1], m_ip[2], m_ip[3]); |
mafischl | 0:d9266031f832 | 164 | lcd2.cls(); |
mafischl | 0:d9266031f832 | 165 | lcd2.locate(0,1); |
mafischl | 0:d9266031f832 | 166 | lcd2.printf("%d.%d.%d.%d\n", m_ip[0], m_ip[1], m_ip[2], m_ip[3]); |
mafischl | 0:d9266031f832 | 167 | m_setup = true; |
mafischl | 0:d9266031f832 | 168 | return ETH_OK; |
mafischl | 0:d9266031f832 | 169 | } |
mafischl | 0:d9266031f832 | 170 | |
mafischl | 0:d9266031f832 | 171 | void EthernetNetIf::poll() |
mafischl | 0:d9266031f832 | 172 | { |
mafischl | 0:d9266031f832 | 173 | if(m_ethArpTimer.read_ms()>=ARP_TMR_INTERVAL) |
mafischl | 0:d9266031f832 | 174 | { |
mafischl | 0:d9266031f832 | 175 | m_ethArpTimer.reset(); |
mafischl | 0:d9266031f832 | 176 | etharp_tmr(); |
mafischl | 0:d9266031f832 | 177 | } |
mafischl | 0:d9266031f832 | 178 | #if LWIP_IGMP |
mafischl | 0:d9266031f832 | 179 | if(m_igmpTimer.read_ms()>=IGMP_TMR_INTERVAL) |
mafischl | 0:d9266031f832 | 180 | { |
mafischl | 0:d9266031f832 | 181 | m_igmpTimer.reset(); |
mafischl | 0:d9266031f832 | 182 | igmp_tmr(); |
mafischl | 0:d9266031f832 | 183 | } |
mafischl | 0:d9266031f832 | 184 | #endif |
mafischl | 0:d9266031f832 | 185 | LwipNetIf::poll(); |
mafischl | 0:d9266031f832 | 186 | eth_poll(); |
mafischl | 0:d9266031f832 | 187 | } |
mafischl | 0:d9266031f832 | 188 | |
mafischl | 0:d9266031f832 | 189 | const char* EthernetNetIf::getHwAddr() const { |
mafischl | 0:d9266031f832 | 190 | return (char*)m_pNetIf->hwaddr; |
mafischl | 0:d9266031f832 | 191 | } |
mafischl | 0:d9266031f832 | 192 | |
mafischl | 0:d9266031f832 | 193 | const char* EthernetNetIf::getHostname() const { |
mafischl | 0:d9266031f832 | 194 | return m_hostname; |
mafischl | 0:d9266031f832 | 195 | } |
mafischl | 0:d9266031f832 | 196 | |
mafischl | 0:d9266031f832 | 197 | #endif |