Trial code integration web page update with analogue data and ntp support

Dependencies:   NTPClient_NetServices mbed

Committer:
pmr1
Date:
Fri Aug 06 17:57:45 2010 +0000
Revision:
0:8cc2035bebfc

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pmr1 0:8cc2035bebfc 1
pmr1 0:8cc2035bebfc 2 /*
pmr1 0:8cc2035bebfc 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
pmr1 0:8cc2035bebfc 4
pmr1 0:8cc2035bebfc 5 Permission is hereby granted, free of charge, to any person obtaining a copy
pmr1 0:8cc2035bebfc 6 of this software and associated documentation files (the "Software"), to deal
pmr1 0:8cc2035bebfc 7 in the Software without restriction, including without limitation the rights
pmr1 0:8cc2035bebfc 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
pmr1 0:8cc2035bebfc 9 copies of the Software, and to permit persons to whom the Software is
pmr1 0:8cc2035bebfc 10 furnished to do so, subject to the following conditions:
pmr1 0:8cc2035bebfc 11
pmr1 0:8cc2035bebfc 12 The above copyright notice and this permission notice shall be included in
pmr1 0:8cc2035bebfc 13 all copies or substantial portions of the Software.
pmr1 0:8cc2035bebfc 14
pmr1 0:8cc2035bebfc 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
pmr1 0:8cc2035bebfc 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
pmr1 0:8cc2035bebfc 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
pmr1 0:8cc2035bebfc 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
pmr1 0:8cc2035bebfc 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
pmr1 0:8cc2035bebfc 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
pmr1 0:8cc2035bebfc 21 THE SOFTWARE.
pmr1 0:8cc2035bebfc 22 */
pmr1 0:8cc2035bebfc 23
pmr1 0:8cc2035bebfc 24 #ifndef NETUDPSOCKET_H
pmr1 0:8cc2035bebfc 25 #define NETUDPSOCKET_H
pmr1 0:8cc2035bebfc 26
pmr1 0:8cc2035bebfc 27 #include "net.h"
pmr1 0:8cc2035bebfc 28 #include "host.h"
pmr1 0:8cc2035bebfc 29
pmr1 0:8cc2035bebfc 30 #include <queue>
pmr1 0:8cc2035bebfc 31 using std::queue;
pmr1 0:8cc2035bebfc 32
pmr1 0:8cc2035bebfc 33 //Implements a Berkeley-like socket if
pmr1 0:8cc2035bebfc 34 //Can be interfaced either to lwip or a Telit module
pmr1 0:8cc2035bebfc 35
pmr1 0:8cc2035bebfc 36 enum NetUdpSocketErr
pmr1 0:8cc2035bebfc 37 {
pmr1 0:8cc2035bebfc 38 __NETUDPSOCKET_MIN = -0xFFFF,
pmr1 0:8cc2035bebfc 39 NETUDPSOCKET_SETUP, //NetUdpSocket not properly configured
pmr1 0:8cc2035bebfc 40 NETUDPSOCKET_IF, //If has problems
pmr1 0:8cc2035bebfc 41 NETUDPSOCKET_MEM, //Not enough mem
pmr1 0:8cc2035bebfc 42 NETUDPSOCKET_INUSE, //If/Port is in use
pmr1 0:8cc2035bebfc 43 //...
pmr1 0:8cc2035bebfc 44 NETUDPSOCKET_OK = 0
pmr1 0:8cc2035bebfc 45 };
pmr1 0:8cc2035bebfc 46
pmr1 0:8cc2035bebfc 47 enum NetUdpSocketEvent //Only one lonely event here... but who knows, maybe some day there'll be another one!
pmr1 0:8cc2035bebfc 48 {
pmr1 0:8cc2035bebfc 49 NETUDPSOCKET_READABLE, //Data in buf
pmr1 0:8cc2035bebfc 50 };
pmr1 0:8cc2035bebfc 51
pmr1 0:8cc2035bebfc 52
pmr1 0:8cc2035bebfc 53 class NetUdpSocket
pmr1 0:8cc2035bebfc 54 {
pmr1 0:8cc2035bebfc 55 public:
pmr1 0:8cc2035bebfc 56 NetUdpSocket();
pmr1 0:8cc2035bebfc 57 virtual ~NetUdpSocket(); //close()
pmr1 0:8cc2035bebfc 58
pmr1 0:8cc2035bebfc 59 virtual NetUdpSocketErr bind(const Host& me) = 0;
pmr1 0:8cc2035bebfc 60
pmr1 0:8cc2035bebfc 61 virtual int /*if < 0 : NetUdpSocketErr*/ sendto(const char* buf, int len, Host* pHost) = 0;
pmr1 0:8cc2035bebfc 62 virtual int /*if < 0 : NetUdpSocketErr*/ recvfrom(char* buf, int len, Host* pHost) = 0;
pmr1 0:8cc2035bebfc 63
pmr1 0:8cc2035bebfc 64 /* TODO NTH : printf / scanf helpers that call send/recv */
pmr1 0:8cc2035bebfc 65
pmr1 0:8cc2035bebfc 66 virtual NetUdpSocketErr close() = 0;
pmr1 0:8cc2035bebfc 67
pmr1 0:8cc2035bebfc 68 virtual NetUdpSocketErr poll() = 0;
pmr1 0:8cc2035bebfc 69
pmr1 0:8cc2035bebfc 70 class CDummy;
pmr1 0:8cc2035bebfc 71 //Callbacks
pmr1 0:8cc2035bebfc 72 template<class T>
pmr1 0:8cc2035bebfc 73 //Linker bug : Must be defined here :(
pmr1 0:8cc2035bebfc 74 void setOnEvent( T* pItem, void (T::*pMethod)(NetUdpSocketEvent) )
pmr1 0:8cc2035bebfc 75 {
pmr1 0:8cc2035bebfc 76 m_pCbItem = (CDummy*) pItem;
pmr1 0:8cc2035bebfc 77 m_pCbMeth = (void (CDummy::*)(NetUdpSocketEvent)) pMethod;
pmr1 0:8cc2035bebfc 78 }
pmr1 0:8cc2035bebfc 79
pmr1 0:8cc2035bebfc 80 void resetOnEvent(); //Disable callback
pmr1 0:8cc2035bebfc 81
pmr1 0:8cc2035bebfc 82 protected:
pmr1 0:8cc2035bebfc 83 void queueEvent(NetUdpSocketEvent e);
pmr1 0:8cc2035bebfc 84 void discardEvents();
pmr1 0:8cc2035bebfc 85 void flushEvents(); //to be called during polling
pmr1 0:8cc2035bebfc 86
pmr1 0:8cc2035bebfc 87 Host m_host;
pmr1 0:8cc2035bebfc 88 Host m_client;
pmr1 0:8cc2035bebfc 89
pmr1 0:8cc2035bebfc 90 friend class Net;
pmr1 0:8cc2035bebfc 91 int m_refs;
pmr1 0:8cc2035bebfc 92
pmr1 0:8cc2035bebfc 93 bool m_closed;
pmr1 0:8cc2035bebfc 94 bool m_removed;
pmr1 0:8cc2035bebfc 95
pmr1 0:8cc2035bebfc 96 private:
pmr1 0:8cc2035bebfc 97 //We do not want to execute user code in interrupt routines, so we queue events until the server is polled
pmr1 0:8cc2035bebfc 98 //If we port this to a multithreaded OS, we could avoid this (however some functions here are not thread-safe, so beware ;) )
pmr1 0:8cc2035bebfc 99 void onEvent(NetUdpSocketEvent e); //To be called on poll
pmr1 0:8cc2035bebfc 100 CDummy* m_pCbItem;
pmr1 0:8cc2035bebfc 101 void (CDummy::*m_pCbMeth)(NetUdpSocketEvent);
pmr1 0:8cc2035bebfc 102 queue<NetUdpSocketEvent> m_events;
pmr1 0:8cc2035bebfc 103
pmr1 0:8cc2035bebfc 104 };
pmr1 0:8cc2035bebfc 105
pmr1 0:8cc2035bebfc 106 #endif