hattori&ide

Dependencies:   mbed

Committer:
hattori_atsushi
Date:
Sun Dec 18 08:16:01 2022 +0000
Revision:
0:f77369cabd75
hattori

Who changed what in which revision?

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