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/if/net/nettcpsocket.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 | #ifndef NETTCPSOCKET_H |
cheungderek | 0:e083abcfe7a8 | 25 | #define NETTCPSOCKET_H |
cheungderek | 0:e083abcfe7a8 | 26 | |
cheungderek | 0:e083abcfe7a8 | 27 | #include "net.h" |
cheungderek | 0:e083abcfe7a8 | 28 | #include "host.h" |
cheungderek | 0:e083abcfe7a8 | 29 | |
cheungderek | 0:e083abcfe7a8 | 30 | #include <queue> |
cheungderek | 0:e083abcfe7a8 | 31 | using std::queue; |
cheungderek | 0:e083abcfe7a8 | 32 | |
cheungderek | 0:e083abcfe7a8 | 33 | //Implements a Berkeley-like socket if |
cheungderek | 0:e083abcfe7a8 | 34 | //Can be interfaced either to lwip or a Telit module |
cheungderek | 0:e083abcfe7a8 | 35 | |
cheungderek | 0:e083abcfe7a8 | 36 | enum NetTcpSocketErr |
cheungderek | 0:e083abcfe7a8 | 37 | { |
cheungderek | 0:e083abcfe7a8 | 38 | __NETTCPSOCKET_MIN = -0xFFFF, |
cheungderek | 0:e083abcfe7a8 | 39 | NETTCPSOCKET_SETUP, //NetTcpSocket not properly configured |
cheungderek | 0:e083abcfe7a8 | 40 | NETTCPSOCKET_TIMEOUT, |
cheungderek | 0:e083abcfe7a8 | 41 | NETTCPSOCKET_IF, //If has problems |
cheungderek | 0:e083abcfe7a8 | 42 | NETTCPSOCKET_MEM, //Not enough mem |
cheungderek | 0:e083abcfe7a8 | 43 | NETTCPSOCKET_INUSE, //If/Port is in use |
cheungderek | 0:e083abcfe7a8 | 44 | NETTCPSOCKET_EMPTY, //Connections queue is empty |
cheungderek | 0:e083abcfe7a8 | 45 | NETTCPSOCKET_RST, // Connection was reset by remote host |
cheungderek | 0:e083abcfe7a8 | 46 | //... |
cheungderek | 0:e083abcfe7a8 | 47 | NETTCPSOCKET_OK = 0 |
cheungderek | 0:e083abcfe7a8 | 48 | }; |
cheungderek | 0:e083abcfe7a8 | 49 | |
cheungderek | 0:e083abcfe7a8 | 50 | enum NetTcpSocketEvent |
cheungderek | 0:e083abcfe7a8 | 51 | { |
cheungderek | 0:e083abcfe7a8 | 52 | NETTCPSOCKET_CONNECTED, //Connected to host, must call accept() if we were listening |
cheungderek | 0:e083abcfe7a8 | 53 | NETTCPSOCKET_ACCEPT, //Connected to client |
cheungderek | 0:e083abcfe7a8 | 54 | NETTCPSOCKET_READABLE, //Data in buf |
cheungderek | 0:e083abcfe7a8 | 55 | NETTCPSOCKET_WRITEABLE, //Can write data to buf |
cheungderek | 0:e083abcfe7a8 | 56 | NETTCPSOCKET_CONTIMEOUT, |
cheungderek | 0:e083abcfe7a8 | 57 | NETTCPSOCKET_CONRST, |
cheungderek | 0:e083abcfe7a8 | 58 | NETTCPSOCKET_CONABRT, |
cheungderek | 0:e083abcfe7a8 | 59 | NETTCPSOCKET_ERROR, |
cheungderek | 0:e083abcfe7a8 | 60 | NETTCPSOCKET_DISCONNECTED |
cheungderek | 0:e083abcfe7a8 | 61 | }; |
cheungderek | 0:e083abcfe7a8 | 62 | |
cheungderek | 0:e083abcfe7a8 | 63 | |
cheungderek | 0:e083abcfe7a8 | 64 | class NetTcpSocket |
cheungderek | 0:e083abcfe7a8 | 65 | { |
cheungderek | 0:e083abcfe7a8 | 66 | public: |
cheungderek | 0:e083abcfe7a8 | 67 | NetTcpSocket(); |
cheungderek | 0:e083abcfe7a8 | 68 | virtual ~NetTcpSocket(); //close() |
cheungderek | 0:e083abcfe7a8 | 69 | |
cheungderek | 0:e083abcfe7a8 | 70 | virtual NetTcpSocketErr bind(const Host& me) = 0; |
cheungderek | 0:e083abcfe7a8 | 71 | virtual NetTcpSocketErr listen() = 0; |
cheungderek | 0:e083abcfe7a8 | 72 | virtual NetTcpSocketErr connect(const Host& host) = 0; |
cheungderek | 0:e083abcfe7a8 | 73 | virtual NetTcpSocketErr accept(Host* pClient, NetTcpSocket** ppNewNetTcpSocket) = 0; |
cheungderek | 0:e083abcfe7a8 | 74 | |
cheungderek | 0:e083abcfe7a8 | 75 | virtual int /*if < 0 : NetTcpSocketErr*/ send(const char* buf, int len) = 0; |
cheungderek | 0:e083abcfe7a8 | 76 | virtual int /*if < 0 : NetTcpSocketErr*/ recv(char* buf, int len) = 0; |
cheungderek | 0:e083abcfe7a8 | 77 | |
cheungderek | 0:e083abcfe7a8 | 78 | virtual NetTcpSocketErr close() = 0; |
cheungderek | 0:e083abcfe7a8 | 79 | |
cheungderek | 0:e083abcfe7a8 | 80 | virtual NetTcpSocketErr poll() = 0; |
cheungderek | 0:e083abcfe7a8 | 81 | |
cheungderek | 0:e083abcfe7a8 | 82 | class CDummy; |
cheungderek | 0:e083abcfe7a8 | 83 | //Callbacks |
cheungderek | 0:e083abcfe7a8 | 84 | template<class T> |
cheungderek | 0:e083abcfe7a8 | 85 | //Linker bug : Must be defined here :( |
cheungderek | 0:e083abcfe7a8 | 86 | void setOnEvent( T* pItem, void (T::*pMethod)(NetTcpSocketEvent) ) |
cheungderek | 0:e083abcfe7a8 | 87 | { |
cheungderek | 0:e083abcfe7a8 | 88 | m_pCbItem = (CDummy*) pItem; |
cheungderek | 0:e083abcfe7a8 | 89 | m_pCbMeth = (void (CDummy::*)(NetTcpSocketEvent)) pMethod; |
cheungderek | 0:e083abcfe7a8 | 90 | } |
cheungderek | 0:e083abcfe7a8 | 91 | |
cheungderek | 0:e083abcfe7a8 | 92 | void resetOnEvent(); //Disable callback |
cheungderek | 0:e083abcfe7a8 | 93 | |
cheungderek | 0:e083abcfe7a8 | 94 | protected: |
cheungderek | 0:e083abcfe7a8 | 95 | void queueEvent(NetTcpSocketEvent e); |
cheungderek | 0:e083abcfe7a8 | 96 | void discardEvents(); |
cheungderek | 0:e083abcfe7a8 | 97 | void flushEvents(); //to be called during polling |
cheungderek | 0:e083abcfe7a8 | 98 | |
cheungderek | 0:e083abcfe7a8 | 99 | Host m_host; |
cheungderek | 0:e083abcfe7a8 | 100 | Host m_client; |
cheungderek | 0:e083abcfe7a8 | 101 | |
cheungderek | 0:e083abcfe7a8 | 102 | friend class Net; |
cheungderek | 0:e083abcfe7a8 | 103 | int m_refs; |
cheungderek | 0:e083abcfe7a8 | 104 | |
cheungderek | 0:e083abcfe7a8 | 105 | bool m_closed; |
cheungderek | 0:e083abcfe7a8 | 106 | bool m_removed; |
cheungderek | 0:e083abcfe7a8 | 107 | |
cheungderek | 0:e083abcfe7a8 | 108 | private: |
cheungderek | 0:e083abcfe7a8 | 109 | //We do not want to execute user code in interrupt routines, so we queue events until the server is polled |
cheungderek | 0:e083abcfe7a8 | 110 | //If we port this to a multithreaded OS, we could avoid this (however some functions here are not thread-safe, so beware ;) ) |
cheungderek | 0:e083abcfe7a8 | 111 | void onEvent(NetTcpSocketEvent e); //To be called on poll |
cheungderek | 0:e083abcfe7a8 | 112 | CDummy* m_pCbItem; |
cheungderek | 0:e083abcfe7a8 | 113 | void (CDummy::*m_pCbMeth)(NetTcpSocketEvent); |
cheungderek | 0:e083abcfe7a8 | 114 | queue<NetTcpSocketEvent> m_events; |
cheungderek | 0:e083abcfe7a8 | 115 | |
cheungderek | 0:e083abcfe7a8 | 116 | }; |
cheungderek | 0:e083abcfe7a8 | 117 | |
cheungderek | 0:e083abcfe7a8 | 118 | #endif |