Read NTP time from Ethernet and drive six 7-segment LEDs using a MAX7221 to show time, date, month, week, year etc....
Dependencies: NTPClient_NetServices mbed
LPC1768/api/DNSRequest.h@0:e083abcfe7a8, 2011-06-22 (annotated)
- Committer:
- cheungderek
- Date:
- Wed Jun 22 01:31:47 2011 +0000
- Revision:
- 0:e083abcfe7a8
First release.
Just some testing but working code to read NTP time and drive 6 LEDs to display time, date, month, year etc...
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cheungderek | 0:e083abcfe7a8 | 1 | |
cheungderek | 0:e083abcfe7a8 | 2 | /* |
cheungderek | 0:e083abcfe7a8 | 3 | Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
cheungderek | 0:e083abcfe7a8 | 4 | |
cheungderek | 0:e083abcfe7a8 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
cheungderek | 0:e083abcfe7a8 | 6 | of this software and associated documentation files (the "Software"), to deal |
cheungderek | 0:e083abcfe7a8 | 7 | in the Software without restriction, including without limitation the rights |
cheungderek | 0:e083abcfe7a8 | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
cheungderek | 0:e083abcfe7a8 | 9 | copies of the Software, and to permit persons to whom the Software is |
cheungderek | 0:e083abcfe7a8 | 10 | furnished to do so, subject to the following conditions: |
cheungderek | 0:e083abcfe7a8 | 11 | |
cheungderek | 0:e083abcfe7a8 | 12 | The above copyright notice and this permission notice shall be included in |
cheungderek | 0:e083abcfe7a8 | 13 | all copies or substantial portions of the Software. |
cheungderek | 0:e083abcfe7a8 | 14 | |
cheungderek | 0:e083abcfe7a8 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
cheungderek | 0:e083abcfe7a8 | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
cheungderek | 0:e083abcfe7a8 | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
cheungderek | 0:e083abcfe7a8 | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
cheungderek | 0:e083abcfe7a8 | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
cheungderek | 0:e083abcfe7a8 | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
cheungderek | 0:e083abcfe7a8 | 21 | THE SOFTWARE. |
cheungderek | 0:e083abcfe7a8 | 22 | */ |
cheungderek | 0:e083abcfe7a8 | 23 | |
cheungderek | 0:e083abcfe7a8 | 24 | /** \file |
cheungderek | 0:e083abcfe7a8 | 25 | DNS Request header file |
cheungderek | 0:e083abcfe7a8 | 26 | */ |
cheungderek | 0:e083abcfe7a8 | 27 | |
cheungderek | 0:e083abcfe7a8 | 28 | #ifndef DNSREQUEST_H |
cheungderek | 0:e083abcfe7a8 | 29 | #define DNSREQUEST_H |
cheungderek | 0:e083abcfe7a8 | 30 | |
cheungderek | 0:e083abcfe7a8 | 31 | #include "core/net.h" |
cheungderek | 0:e083abcfe7a8 | 32 | #include "core/ipaddr.h" |
cheungderek | 0:e083abcfe7a8 | 33 | #include "core/host.h" |
cheungderek | 0:e083abcfe7a8 | 34 | //Essentially it is a safe interface to NetDnsRequest |
cheungderek | 0:e083abcfe7a8 | 35 | |
cheungderek | 0:e083abcfe7a8 | 36 | ///DNS Request error codes |
cheungderek | 0:e083abcfe7a8 | 37 | enum DNSRequestErr |
cheungderek | 0:e083abcfe7a8 | 38 | { |
cheungderek | 0:e083abcfe7a8 | 39 | __DNS_MIN = -0xFFFF, |
cheungderek | 0:e083abcfe7a8 | 40 | DNS_SETUP, ///<DNSRequest not properly configured |
cheungderek | 0:e083abcfe7a8 | 41 | DNS_IF, ///<Interface has problems, does not exist or is not initialized |
cheungderek | 0:e083abcfe7a8 | 42 | DNS_MEM, ///<Not enough mem |
cheungderek | 0:e083abcfe7a8 | 43 | DNS_INUSE, ///<Interface / Port is in use |
cheungderek | 0:e083abcfe7a8 | 44 | DNS_PROCESSING, ///<Request has not completed |
cheungderek | 0:e083abcfe7a8 | 45 | //... |
cheungderek | 0:e083abcfe7a8 | 46 | DNS_OK = 0 ///<Success |
cheungderek | 0:e083abcfe7a8 | 47 | }; |
cheungderek | 0:e083abcfe7a8 | 48 | |
cheungderek | 0:e083abcfe7a8 | 49 | ///DNS Request Result Events |
cheungderek | 0:e083abcfe7a8 | 50 | enum DNSReply |
cheungderek | 0:e083abcfe7a8 | 51 | { |
cheungderek | 0:e083abcfe7a8 | 52 | DNS_PRTCL, |
cheungderek | 0:e083abcfe7a8 | 53 | DNS_NOTFOUND, ///Hostname is unknown |
cheungderek | 0:e083abcfe7a8 | 54 | DNS_ERROR, ///Problem with DNS Service |
cheungderek | 0:e083abcfe7a8 | 55 | //... |
cheungderek | 0:e083abcfe7a8 | 56 | DNS_FOUND, |
cheungderek | 0:e083abcfe7a8 | 57 | }; |
cheungderek | 0:e083abcfe7a8 | 58 | |
cheungderek | 0:e083abcfe7a8 | 59 | class NetDnsRequest; |
cheungderek | 0:e083abcfe7a8 | 60 | enum NetDnsReply; |
cheungderek | 0:e083abcfe7a8 | 61 | |
cheungderek | 0:e083abcfe7a8 | 62 | ///This is a simple DNS Request class |
cheungderek | 0:e083abcfe7a8 | 63 | /** |
cheungderek | 0:e083abcfe7a8 | 64 | This class exposes an API to deal with DNS Requests |
cheungderek | 0:e083abcfe7a8 | 65 | */ |
cheungderek | 0:e083abcfe7a8 | 66 | class DNSRequest |
cheungderek | 0:e083abcfe7a8 | 67 | { |
cheungderek | 0:e083abcfe7a8 | 68 | public: |
cheungderek | 0:e083abcfe7a8 | 69 | ///Creates a new request |
cheungderek | 0:e083abcfe7a8 | 70 | DNSRequest(); |
cheungderek | 0:e083abcfe7a8 | 71 | |
cheungderek | 0:e083abcfe7a8 | 72 | ///Terminates and closes request |
cheungderek | 0:e083abcfe7a8 | 73 | ~DNSRequest(); |
cheungderek | 0:e083abcfe7a8 | 74 | |
cheungderek | 0:e083abcfe7a8 | 75 | ///Resolves an hostname |
cheungderek | 0:e083abcfe7a8 | 76 | /** |
cheungderek | 0:e083abcfe7a8 | 77 | @param hostname : hostname to resolve |
cheungderek | 0:e083abcfe7a8 | 78 | */ |
cheungderek | 0:e083abcfe7a8 | 79 | DNSRequestErr resolve(const char* hostname); |
cheungderek | 0:e083abcfe7a8 | 80 | |
cheungderek | 0:e083abcfe7a8 | 81 | ///Resolves an hostname |
cheungderek | 0:e083abcfe7a8 | 82 | /** |
cheungderek | 0:e083abcfe7a8 | 83 | @param host : hostname to resolve, the result will be stored in the IpAddr field of this object |
cheungderek | 0:e083abcfe7a8 | 84 | */ |
cheungderek | 0:e083abcfe7a8 | 85 | DNSRequestErr resolve(Host* pHost); |
cheungderek | 0:e083abcfe7a8 | 86 | |
cheungderek | 0:e083abcfe7a8 | 87 | ///Setups callback |
cheungderek | 0:e083abcfe7a8 | 88 | /** |
cheungderek | 0:e083abcfe7a8 | 89 | The callback function will be called on result. |
cheungderek | 0:e083abcfe7a8 | 90 | @param pMethod : callback function |
cheungderek | 0:e083abcfe7a8 | 91 | */ |
cheungderek | 0:e083abcfe7a8 | 92 | void setOnReply( void (*pMethod)(DNSReply) ); |
cheungderek | 0:e083abcfe7a8 | 93 | |
cheungderek | 0:e083abcfe7a8 | 94 | class CDummy; |
cheungderek | 0:e083abcfe7a8 | 95 | ///Setups callback |
cheungderek | 0:e083abcfe7a8 | 96 | /** |
cheungderek | 0:e083abcfe7a8 | 97 | The callback function will be called on result. |
cheungderek | 0:e083abcfe7a8 | 98 | @param pItem : instance of class on which to execute the callback method |
cheungderek | 0:e083abcfe7a8 | 99 | @param pMethod : callback method |
cheungderek | 0:e083abcfe7a8 | 100 | */ |
cheungderek | 0:e083abcfe7a8 | 101 | template<class T> |
cheungderek | 0:e083abcfe7a8 | 102 | void setOnReply( T* pItem, void (T::*pMethod)(DNSReply) ) |
cheungderek | 0:e083abcfe7a8 | 103 | { |
cheungderek | 0:e083abcfe7a8 | 104 | m_pCbItem = (CDummy*) pItem; |
cheungderek | 0:e083abcfe7a8 | 105 | m_pCbMeth = (void (CDummy::*)(DNSReply)) pMethod; |
cheungderek | 0:e083abcfe7a8 | 106 | } |
cheungderek | 0:e083abcfe7a8 | 107 | |
cheungderek | 0:e083abcfe7a8 | 108 | ///Gets IP address once it has been resolved |
cheungderek | 0:e083abcfe7a8 | 109 | /** |
cheungderek | 0:e083abcfe7a8 | 110 | @param pIp : pointer to an IpAddr instance in which to store the resolved IP address |
cheungderek | 0:e083abcfe7a8 | 111 | */ |
cheungderek | 0:e083abcfe7a8 | 112 | DNSRequestErr getResult(IpAddr* pIp); |
cheungderek | 0:e083abcfe7a8 | 113 | |
cheungderek | 0:e083abcfe7a8 | 114 | ///Closes DNS Request before completion |
cheungderek | 0:e083abcfe7a8 | 115 | DNSRequestErr close(); |
cheungderek | 0:e083abcfe7a8 | 116 | |
cheungderek | 0:e083abcfe7a8 | 117 | protected: |
cheungderek | 0:e083abcfe7a8 | 118 | void onNetDnsReply(NetDnsReply r); |
cheungderek | 0:e083abcfe7a8 | 119 | DNSRequestErr checkInst(); |
cheungderek | 0:e083abcfe7a8 | 120 | |
cheungderek | 0:e083abcfe7a8 | 121 | private: |
cheungderek | 0:e083abcfe7a8 | 122 | NetDnsRequest* m_pNetDnsRequest; |
cheungderek | 0:e083abcfe7a8 | 123 | |
cheungderek | 0:e083abcfe7a8 | 124 | CDummy* m_pCbItem; |
cheungderek | 0:e083abcfe7a8 | 125 | void (CDummy::*m_pCbMeth)(DNSReply); |
cheungderek | 0:e083abcfe7a8 | 126 | |
cheungderek | 0:e083abcfe7a8 | 127 | void (*m_pCb)(DNSReply); |
cheungderek | 0:e083abcfe7a8 | 128 | |
cheungderek | 0:e083abcfe7a8 | 129 | }; |
cheungderek | 0:e083abcfe7a8 | 130 | |
cheungderek | 0:e083abcfe7a8 | 131 | #endif |