trying to make telnetserver repo public

Dependents:   Telnet_server

Fork of NTPClient_NetServices by Donatien Garnier

Files at this revision

API Documentation at this revision

Comitter:
donatien
Date:
Fri Jun 11 16:27:11 2010 +0000
Child:
1:1f436e56925f
Commit message:

Changed in this revision

LPC1768/NTPClient.ar Show annotated file Show diff for this revision Revisions of this file
LPC1768/dbg/dbg.h Show annotated file Show diff for this revision Revisions of this file
LPC1768/services/ntp/NTPClient.h Show annotated file Show diff for this revision Revisions of this file
LPC2368/NTPClient.ar Show annotated file Show diff for this revision Revisions of this file
LPC2368/dbg/dbg.h Show annotated file Show diff for this revision Revisions of this file
LPC2368/services/ntp/NTPClient.h Show annotated file Show diff for this revision Revisions of this file
Binary file LPC1768/NTPClient.ar has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LPC1768/dbg/dbg.h	Fri Jun 11 16:27:11 2010 +0000
@@ -0,0 +1,73 @@
+
+/*
+Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
+ 
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+ 
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+ 
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+//#ifdef DBG_H
+//#define DBG_H
+
+#ifdef __LWIP_DEBUG
+#define __DEBUG
+#endif
+
+#ifdef __DEBUG
+
+#ifndef __DEBUGSTREAM
+#define __DEBUGSTREAM
+
+
+class DebugStream
+{
+public:
+static void debug(const char* format, ...);
+static void release();
+private:
+
+};
+
+#undef DBG
+#undef DBG_END
+#define DBG DebugStream::debug
+#define DBG_END DebugStream::release
+#endif
+
+#else
+#undef DBG
+#undef DBG_END
+#define DBG(...)
+#define DBG_END()
+#endif
+
+#ifdef __LWIP_DEBUG
+#ifndef __SNPRINTF
+#define __SNPRINTF
+#include "mbed.h"
+
+//int snprintf(char *str, int size, const char *format, ...);
+#endif
+#endif
+
+#ifdef __LWIP_DEBUG
+#undef __DEBUG
+#endif
+
+//#endif
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LPC1768/services/ntp/NTPClient.h	Fri Jun 11 16:27:11 2010 +0000
@@ -0,0 +1,140 @@
+
+/*
+Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
+ 
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+ 
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+ 
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+#ifndef NTP_CLIENT_H
+#define NTP_CLIENT_H
+
+#include "if/net/net.h"
+#include "api/UDPSocket.h"
+#include "api/DNSRequest.h"
+#include "mbed.h"
+
+enum NTPResult
+{
+  NTP_OK,
+  NTP_PROCESSING,
+  NTP_PRTCL, //Protocol error
+  NTP_TIMEOUT, //Connection timeout
+  NTP_DNS //Could not resolve DNS Addr
+};
+
+class NTPClient
+{
+public:
+  NTPClient();
+  virtual ~NTPClient();
+  
+  //High level setup functions
+  NTPResult setTime(const Host& host); //Blocking
+  NTPResult setTime(const Host& host, void (*pMethod)(NTPResult)); //Non blocking
+  template<class T> 
+  NTPResult setTime(const Host& host, T* pItem, void (T::*pMethod)(NTPResult)) //Non blocking
+  {
+    setOnResult(pItem, pMethod);
+    doSetTime(host);
+    return NTP_PROCESSING;
+  }
+  
+  void doSetTime(const Host& host);
+  
+  void setOnResult( void (*pMethod)(NTPResult) );
+  class CDummy;
+  template<class T> 
+  void setOnResult( T* pItem, void (T::*pMethod)(NTPResult) )
+  {
+    m_pCbItem = (CDummy*) pItem;
+    m_pCbMeth = (void (CDummy::*)(NTPResult)) pMethod;
+  }
+  
+  void init();
+  void close();
+  
+private:
+  __packed struct NTPPacket //See RFC 4330 for Simple NTP
+  {
+    //WARN: We are in LE! Network is BE!
+    //LSb first
+    unsigned mode : 3;
+    unsigned vn : 3;
+    unsigned li : 2;
+    
+    uint8_t stratum;
+    uint8_t poll;
+    uint8_t precision;
+    //32 bits header
+    
+    uint32_t rootDelay;
+    uint32_t rootDispersion;
+    uint32_t refId;
+    
+    uint32_t refTm_s;
+    uint32_t refTm_f;
+    uint32_t origTm_s;
+    uint32_t origTm_f;
+    uint32_t rxTm_s;
+    uint32_t rxTm_f;
+    uint32_t txTm_s;
+    uint32_t txTm_f;
+  };
+
+  void process(); //Main state-machine
+
+  void setTimeout(int ms);
+  void resetTimeout();
+  
+  void onTimeout(); //Connection has timed out
+  void onDNSReply(DNSReply r);
+  void onUDPSocketEvent(UDPSocketEvent e);
+  void onResult(NTPResult r); //Called when exchange completed or on failure
+  
+  NTPResult blockingProcess(); //Called in blocking mode, calls Net::poll() until return code is available
+
+  UDPSocket* m_pUDPSocket;
+
+  enum NTPStep
+  {
+    NTP_PING,
+    NTP_PONG
+  };
+  
+  NTPStep m_state;
+  
+  CDummy* m_pCbItem;
+  void (CDummy::*m_pCbMeth)(NTPResult);
+  
+  void (*m_pCb)(NTPResult);
+  
+  Timeout m_watchdog;
+  int m_timeout;
+  
+  bool m_closed;
+  
+  Host m_host;
+  
+  DNSRequest* m_pDnsReq;
+  
+  NTPResult m_blockingResult; //Result if blocking mode
+
+};
+
+#endif
Binary file LPC2368/NTPClient.ar has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LPC2368/dbg/dbg.h	Fri Jun 11 16:27:11 2010 +0000
@@ -0,0 +1,73 @@
+
+/*
+Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
+ 
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+ 
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+ 
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+//#ifdef DBG_H
+//#define DBG_H
+
+#ifdef __LWIP_DEBUG
+#define __DEBUG
+#endif
+
+#ifdef __DEBUG
+
+#ifndef __DEBUGSTREAM
+#define __DEBUGSTREAM
+
+
+class DebugStream
+{
+public:
+static void debug(const char* format, ...);
+static void release();
+private:
+
+};
+
+#undef DBG
+#undef DBG_END
+#define DBG DebugStream::debug
+#define DBG_END DebugStream::release
+#endif
+
+#else
+#undef DBG
+#undef DBG_END
+#define DBG(...)
+#define DBG_END()
+#endif
+
+#ifdef __LWIP_DEBUG
+#ifndef __SNPRINTF
+#define __SNPRINTF
+#include "mbed.h"
+
+//int snprintf(char *str, int size, const char *format, ...);
+#endif
+#endif
+
+#ifdef __LWIP_DEBUG
+#undef __DEBUG
+#endif
+
+//#endif
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LPC2368/services/ntp/NTPClient.h	Fri Jun 11 16:27:11 2010 +0000
@@ -0,0 +1,140 @@
+
+/*
+Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
+ 
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+ 
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+ 
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+#ifndef NTP_CLIENT_H
+#define NTP_CLIENT_H
+
+#include "if/net/net.h"
+#include "api/UDPSocket.h"
+#include "api/DNSRequest.h"
+#include "mbed.h"
+
+enum NTPResult
+{
+  NTP_OK,
+  NTP_PROCESSING,
+  NTP_PRTCL, //Protocol error
+  NTP_TIMEOUT, //Connection timeout
+  NTP_DNS //Could not resolve DNS Addr
+};
+
+class NTPClient
+{
+public:
+  NTPClient();
+  virtual ~NTPClient();
+  
+  //High level setup functions
+  NTPResult setTime(const Host& host); //Blocking
+  NTPResult setTime(const Host& host, void (*pMethod)(NTPResult)); //Non blocking
+  template<class T> 
+  NTPResult setTime(const Host& host, T* pItem, void (T::*pMethod)(NTPResult)) //Non blocking
+  {
+    setOnResult(pItem, pMethod);
+    doSetTime(host);
+    return NTP_PROCESSING;
+  }
+  
+  void doSetTime(const Host& host);
+  
+  void setOnResult( void (*pMethod)(NTPResult) );
+  class CDummy;
+  template<class T> 
+  void setOnResult( T* pItem, void (T::*pMethod)(NTPResult) )
+  {
+    m_pCbItem = (CDummy*) pItem;
+    m_pCbMeth = (void (CDummy::*)(NTPResult)) pMethod;
+  }
+  
+  void init();
+  void close();
+  
+private:
+  __packed struct NTPPacket //See RFC 4330 for Simple NTP
+  {
+    //WARN: We are in LE! Network is BE!
+    //LSb first
+    unsigned mode : 3;
+    unsigned vn : 3;
+    unsigned li : 2;
+    
+    uint8_t stratum;
+    uint8_t poll;
+    uint8_t precision;
+    //32 bits header
+    
+    uint32_t rootDelay;
+    uint32_t rootDispersion;
+    uint32_t refId;
+    
+    uint32_t refTm_s;
+    uint32_t refTm_f;
+    uint32_t origTm_s;
+    uint32_t origTm_f;
+    uint32_t rxTm_s;
+    uint32_t rxTm_f;
+    uint32_t txTm_s;
+    uint32_t txTm_f;
+  };
+
+  void process(); //Main state-machine
+
+  void setTimeout(int ms);
+  void resetTimeout();
+  
+  void onTimeout(); //Connection has timed out
+  void onDNSReply(DNSReply r);
+  void onUDPSocketEvent(UDPSocketEvent e);
+  void onResult(NTPResult r); //Called when exchange completed or on failure
+  
+  NTPResult blockingProcess(); //Called in blocking mode, calls Net::poll() until return code is available
+
+  UDPSocket* m_pUDPSocket;
+
+  enum NTPStep
+  {
+    NTP_PING,
+    NTP_PONG
+  };
+  
+  NTPStep m_state;
+  
+  CDummy* m_pCbItem;
+  void (CDummy::*m_pCbMeth)(NTPResult);
+  
+  void (*m_pCb)(NTPResult);
+  
+  Timeout m_watchdog;
+  int m_timeout;
+  
+  bool m_closed;
+  
+  Host m_host;
+  
+  DNSRequest* m_pDnsReq;
+  
+  NTPResult m_blockingResult; //Result if blocking mode
+
+};
+
+#endif