Library for Bert van Dam's book "ARM MICROCONTROLLERS" For all chapters with internet.

Dependencies:   mbed

Committer:
ICTFBI
Date:
Fri Oct 16 14:28:26 2015 +0000
Revision:
0:4edb816d21e1
Pre-update 16-10-15

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ICTFBI 0:4edb816d21e1 1
ICTFBI 0:4edb816d21e1 2 /*
ICTFBI 0:4edb816d21e1 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
ICTFBI 0:4edb816d21e1 4
ICTFBI 0:4edb816d21e1 5 Permission is hereby granted, free of charge, to any person obtaining a copy
ICTFBI 0:4edb816d21e1 6 of this software and associated documentation files (the "Software"), to deal
ICTFBI 0:4edb816d21e1 7 in the Software without restriction, including without limitation the rights
ICTFBI 0:4edb816d21e1 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
ICTFBI 0:4edb816d21e1 9 copies of the Software, and to permit persons to whom the Software is
ICTFBI 0:4edb816d21e1 10 furnished to do so, subject to the following conditions:
ICTFBI 0:4edb816d21e1 11
ICTFBI 0:4edb816d21e1 12 The above copyright notice and this permission notice shall be included in
ICTFBI 0:4edb816d21e1 13 all copies or substantial portions of the Software.
ICTFBI 0:4edb816d21e1 14
ICTFBI 0:4edb816d21e1 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
ICTFBI 0:4edb816d21e1 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
ICTFBI 0:4edb816d21e1 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
ICTFBI 0:4edb816d21e1 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
ICTFBI 0:4edb816d21e1 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ICTFBI 0:4edb816d21e1 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
ICTFBI 0:4edb816d21e1 21 THE SOFTWARE.
ICTFBI 0:4edb816d21e1 22 */
ICTFBI 0:4edb816d21e1 23
ICTFBI 0:4edb816d21e1 24 #include "lwipNetDnsRequest.h"
ICTFBI 0:4edb816d21e1 25 #include "lwip/err.h" //err_t, ERR_xxx
ICTFBI 0:4edb816d21e1 26 #include "lwip/dns.h"
ICTFBI 0:4edb816d21e1 27
ICTFBI 0:4edb816d21e1 28 #include "netCfg.h"
ICTFBI 0:4edb816d21e1 29 #if NET_LWIP_STACK
ICTFBI 0:4edb816d21e1 30
ICTFBI 0:4edb816d21e1 31 //#define __DEBUG
ICTFBI 0:4edb816d21e1 32 #include "dbg/dbg.h"
ICTFBI 0:4edb816d21e1 33
ICTFBI 0:4edb816d21e1 34 LwipNetDnsRequest::LwipNetDnsRequest(const char* hostname) : NetDnsRequest(hostname), m_state(LWIPNETDNS_START), m_cbFired(false), m_closing(false)
ICTFBI 0:4edb816d21e1 35 {
ICTFBI 0:4edb816d21e1 36 DBG("New LwipNetDnsRequest %p\n", this);
ICTFBI 0:4edb816d21e1 37 }
ICTFBI 0:4edb816d21e1 38
ICTFBI 0:4edb816d21e1 39 LwipNetDnsRequest::LwipNetDnsRequest(Host* pHost) : NetDnsRequest(pHost), m_state(LWIPNETDNS_START), m_cbFired(false), m_closing(false)
ICTFBI 0:4edb816d21e1 40 {
ICTFBI 0:4edb816d21e1 41 DBG("New LwipNetDnsRequest %p\n", this);
ICTFBI 0:4edb816d21e1 42 }
ICTFBI 0:4edb816d21e1 43
ICTFBI 0:4edb816d21e1 44 LwipNetDnsRequest::~LwipNetDnsRequest()
ICTFBI 0:4edb816d21e1 45 {
ICTFBI 0:4edb816d21e1 46 DBG("LwipNetDnsRequest %p destroyed\n", this);
ICTFBI 0:4edb816d21e1 47 }
ICTFBI 0:4edb816d21e1 48
ICTFBI 0:4edb816d21e1 49 /*
ICTFBI 0:4edb816d21e1 50 Main useful function here (see lwip/dns.h):
ICTFBI 0:4edb816d21e1 51 err_t dns_gethostbyname(const char *hostname, ip_addr_t *addr,
ICTFBI 0:4edb816d21e1 52 dns_found_callback found, void *callback_arg);
ICTFBI 0:4edb816d21e1 53 */
ICTFBI 0:4edb816d21e1 54
ICTFBI 0:4edb816d21e1 55 //Execute request & return OK if found, NOTFOUND or ERROR on error, or PROCESSING if the request has not completed yet
ICTFBI 0:4edb816d21e1 56 void LwipNetDnsRequest::poll()
ICTFBI 0:4edb816d21e1 57 {
ICTFBI 0:4edb816d21e1 58 err_t err;
ICTFBI 0:4edb816d21e1 59 switch(m_state)
ICTFBI 0:4edb816d21e1 60 {
ICTFBI 0:4edb816d21e1 61 case LWIPNETDNS_START: //First req, let's call dns_gethostbyname
ICTFBI 0:4edb816d21e1 62 ip_addr_t ipStruct;
ICTFBI 0:4edb816d21e1 63 err = dns_gethostbyname(m_hostname, &ipStruct, LwipNetDnsRequest::sFoundCb, (void*) this );
ICTFBI 0:4edb816d21e1 64 if( err == ERR_OK )
ICTFBI 0:4edb816d21e1 65 {
ICTFBI 0:4edb816d21e1 66 m_ip = IpAddr(&ipStruct);
ICTFBI 0:4edb816d21e1 67 m_state = LWIPNETDNS_OK;
ICTFBI 0:4edb816d21e1 68 DBG("DNS: Ip found in cache.\n");
ICTFBI 0:4edb816d21e1 69 }
ICTFBI 0:4edb816d21e1 70 else if( err == ERR_INPROGRESS)
ICTFBI 0:4edb816d21e1 71 {
ICTFBI 0:4edb816d21e1 72 DBG("DNS: Processing.\n");
ICTFBI 0:4edb816d21e1 73 m_state = LWIPNETDNS_PROCESSING;
ICTFBI 0:4edb816d21e1 74 }
ICTFBI 0:4edb816d21e1 75 else //Likely ERR_VAL
ICTFBI 0:4edb816d21e1 76 {
ICTFBI 0:4edb816d21e1 77 DBG("DNS: Error on init.\n");
ICTFBI 0:4edb816d21e1 78 m_state = LWIPNETDNS_ERROR;
ICTFBI 0:4edb816d21e1 79 }
ICTFBI 0:4edb816d21e1 80 break;
ICTFBI 0:4edb816d21e1 81 case LWIPNETDNS_PROCESSING:
ICTFBI 0:4edb816d21e1 82 break; //Nothing to do, DNS is polled on interrupt
ICTFBI 0:4edb816d21e1 83 case LWIPNETDNS_OK:
ICTFBI 0:4edb816d21e1 84 if(!m_cbFired)
ICTFBI 0:4edb816d21e1 85 {
ICTFBI 0:4edb816d21e1 86 DBG("DNS: Ip found.\n");
ICTFBI 0:4edb816d21e1 87 m_cbFired = true;
ICTFBI 0:4edb816d21e1 88 onReply(NETDNS_FOUND); //Raise callback
ICTFBI 0:4edb816d21e1 89 }
ICTFBI 0:4edb816d21e1 90 break;
ICTFBI 0:4edb816d21e1 91 case LWIPNETDNS_NOTFOUND:
ICTFBI 0:4edb816d21e1 92 if(!m_cbFired)
ICTFBI 0:4edb816d21e1 93 {
ICTFBI 0:4edb816d21e1 94 DBG("DNS: could not be resolved.\n");
ICTFBI 0:4edb816d21e1 95 m_cbFired = true;
ICTFBI 0:4edb816d21e1 96 onReply(NETDNS_NOTFOUND); //Raise callback
ICTFBI 0:4edb816d21e1 97 }
ICTFBI 0:4edb816d21e1 98 break;
ICTFBI 0:4edb816d21e1 99 case LWIPNETDNS_ERROR:
ICTFBI 0:4edb816d21e1 100 default:
ICTFBI 0:4edb816d21e1 101 if(!m_cbFired)
ICTFBI 0:4edb816d21e1 102 {
ICTFBI 0:4edb816d21e1 103 DBG("DNS: Error.\n");
ICTFBI 0:4edb816d21e1 104 m_cbFired = true;
ICTFBI 0:4edb816d21e1 105 onReply(NETDNS_ERROR); //Raise callback
ICTFBI 0:4edb816d21e1 106 }
ICTFBI 0:4edb816d21e1 107 break;
ICTFBI 0:4edb816d21e1 108 }
ICTFBI 0:4edb816d21e1 109 if(m_closing && (m_state!=LWIPNETDNS_PROCESSING)) //Check wether the closure has been reqd
ICTFBI 0:4edb816d21e1 110 {
ICTFBI 0:4edb816d21e1 111 DBG("LwipNetDnsRequest: Closing in poll()\n");
ICTFBI 0:4edb816d21e1 112 NetDnsRequest::close();
ICTFBI 0:4edb816d21e1 113 }
ICTFBI 0:4edb816d21e1 114 }
ICTFBI 0:4edb816d21e1 115
ICTFBI 0:4edb816d21e1 116 void LwipNetDnsRequest::close()
ICTFBI 0:4edb816d21e1 117 {
ICTFBI 0:4edb816d21e1 118 DBG("LwipNetDnsRequest: Close req\n");
ICTFBI 0:4edb816d21e1 119 if(m_state!=LWIPNETDNS_PROCESSING)
ICTFBI 0:4edb816d21e1 120 {
ICTFBI 0:4edb816d21e1 121 DBG("LwipNetDnsRequest: Closing in close()\n");
ICTFBI 0:4edb816d21e1 122 NetDnsRequest::close();
ICTFBI 0:4edb816d21e1 123 }
ICTFBI 0:4edb816d21e1 124 else //Cannot close rightaway, waiting for callback from underlying layer
ICTFBI 0:4edb816d21e1 125 {
ICTFBI 0:4edb816d21e1 126 m_closing = true;
ICTFBI 0:4edb816d21e1 127 }
ICTFBI 0:4edb816d21e1 128 }
ICTFBI 0:4edb816d21e1 129
ICTFBI 0:4edb816d21e1 130 void LwipNetDnsRequest::foundCb(const char *name, ip_addr_t *ipaddr)
ICTFBI 0:4edb816d21e1 131 {
ICTFBI 0:4edb816d21e1 132 if( ipaddr == NULL )
ICTFBI 0:4edb816d21e1 133 {
ICTFBI 0:4edb816d21e1 134 DBG("LwipNetDnsRequest: Callback: Name not found\n");
ICTFBI 0:4edb816d21e1 135 m_state = LWIPNETDNS_NOTFOUND;
ICTFBI 0:4edb816d21e1 136 return;
ICTFBI 0:4edb816d21e1 137 }
ICTFBI 0:4edb816d21e1 138 DBG("LwipNetDnsRequest: Callback: Resolved\n");
ICTFBI 0:4edb816d21e1 139 m_ip = IpAddr(ipaddr);
ICTFBI 0:4edb816d21e1 140 m_state = LWIPNETDNS_OK;
ICTFBI 0:4edb816d21e1 141 }
ICTFBI 0:4edb816d21e1 142
ICTFBI 0:4edb816d21e1 143
ICTFBI 0:4edb816d21e1 144 void LwipNetDnsRequest::sFoundCb(const char *name, ip_addr_t *ipaddr, void *arg)
ICTFBI 0:4edb816d21e1 145 {
ICTFBI 0:4edb816d21e1 146 DBG("LwipNetDnsRequest: Static callback\n");
ICTFBI 0:4edb816d21e1 147 LwipNetDnsRequest* pMe = (LwipNetDnsRequest*) arg;
ICTFBI 0:4edb816d21e1 148 return pMe->foundCb( name, ipaddr );
ICTFBI 0:4edb816d21e1 149 }
ICTFBI 0:4edb816d21e1 150
ICTFBI 0:4edb816d21e1 151 #endif
ICTFBI 0:4edb816d21e1 152