Measure system

Dependencies:   EthernetNetIf mbed RF12B

Committer:
benecsj
Date:
Thu Mar 10 19:56:45 2011 +0000
Revision:
1:b26ab2467b1a

        

Who changed what in which revision?

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