User | Revision | Line number | New contents of line |
mbed714 |
0:98aadd37a5c5
|
1
|
|
mbed714 |
0:98aadd37a5c5
|
2
|
/*
|
mbed714 |
0:98aadd37a5c5
|
3
|
Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
|
mbed714 |
0:98aadd37a5c5
|
4
|
|
mbed714 |
0:98aadd37a5c5
|
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
mbed714 |
0:98aadd37a5c5
|
6
|
of this software and associated documentation files (the "Software"), to deal
|
mbed714 |
0:98aadd37a5c5
|
7
|
in the Software without restriction, including without limitation the rights
|
mbed714 |
0:98aadd37a5c5
|
8
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
mbed714 |
0:98aadd37a5c5
|
9
|
copies of the Software, and to permit persons to whom the Software is
|
mbed714 |
0:98aadd37a5c5
|
10
|
furnished to do so, subject to the following conditions:
|
mbed714 |
0:98aadd37a5c5
|
11
|
|
mbed714 |
0:98aadd37a5c5
|
12
|
The above copyright notice and this permission notice shall be included in
|
mbed714 |
0:98aadd37a5c5
|
13
|
all copies or substantial portions of the Software.
|
mbed714 |
0:98aadd37a5c5
|
14
|
|
mbed714 |
0:98aadd37a5c5
|
15
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
mbed714 |
0:98aadd37a5c5
|
16
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
mbed714 |
0:98aadd37a5c5
|
17
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
mbed714 |
0:98aadd37a5c5
|
18
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
mbed714 |
0:98aadd37a5c5
|
19
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
mbed714 |
0:98aadd37a5c5
|
20
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
mbed714 |
0:98aadd37a5c5
|
21
|
THE SOFTWARE.
|
mbed714 |
0:98aadd37a5c5
|
22
|
*/
|
mbed714 |
0:98aadd37a5c5
|
23
|
|
mbed714 |
0:98aadd37a5c5
|
24
|
/** \file
|
mbed714 |
0:98aadd37a5c5
|
25
|
UDP Socket header file
|
mbed714 |
0:98aadd37a5c5
|
26
|
*/
|
mbed714 |
0:98aadd37a5c5
|
27
|
|
mbed714 |
0:98aadd37a5c5
|
28
|
#ifndef UDPSOCKET_H
|
mbed714 |
0:98aadd37a5c5
|
29
|
#define UDPSOCKET_H
|
mbed714 |
0:98aadd37a5c5
|
30
|
|
mbed714 |
0:98aadd37a5c5
|
31
|
#include "core/net.h"
|
mbed714 |
0:98aadd37a5c5
|
32
|
#include "core/host.h"
|
mbed714 |
0:98aadd37a5c5
|
33
|
//Essentially it is a safe interface to NetUdpSocket
|
mbed714 |
0:98aadd37a5c5
|
34
|
|
mbed714 |
0:98aadd37a5c5
|
35
|
///UDP Socket error codes
|
mbed714 |
0:98aadd37a5c5
|
36
|
enum UDPSocketErr
|
mbed714 |
0:98aadd37a5c5
|
37
|
{
|
mbed714 |
0:98aadd37a5c5
|
38
|
__UDPSOCKET_MIN = -0xFFFF,
|
mbed714 |
0:98aadd37a5c5
|
39
|
UDPSOCKET_SETUP, ///<UDPSocket not properly configured
|
mbed714 |
0:98aadd37a5c5
|
40
|
UDPSOCKET_IF, ///<Interface has problems, does not exist or is not initialized
|
mbed714 |
0:98aadd37a5c5
|
41
|
UDPSOCKET_MEM, ///<Not enough mem
|
mbed714 |
0:98aadd37a5c5
|
42
|
UDPSOCKET_INUSE, ///<Interface / Port is in use
|
mbed714 |
0:98aadd37a5c5
|
43
|
//...
|
mbed714 |
0:98aadd37a5c5
|
44
|
UDPSOCKET_OK = 0 ///<Success
|
mbed714 |
0:98aadd37a5c5
|
45
|
};
|
mbed714 |
0:98aadd37a5c5
|
46
|
|
mbed714 |
0:98aadd37a5c5
|
47
|
///UDP Socket Event(s)
|
mbed714 |
0:98aadd37a5c5
|
48
|
enum UDPSocketEvent //Only one event here for now, but keeps that model in case we need to implement some others
|
mbed714 |
0:98aadd37a5c5
|
49
|
{
|
mbed714 |
0:98aadd37a5c5
|
50
|
UDPSOCKET_READABLE, ///<Data in buf
|
mbed714 |
0:98aadd37a5c5
|
51
|
};
|
mbed714 |
0:98aadd37a5c5
|
52
|
|
mbed714 |
0:98aadd37a5c5
|
53
|
class NetUdpSocket;
|
mbed714 |
0:98aadd37a5c5
|
54
|
enum NetUdpSocketEvent;
|
mbed714 |
0:98aadd37a5c5
|
55
|
|
mbed714 |
0:98aadd37a5c5
|
56
|
///This is a simple UDP Socket class
|
mbed714 |
0:98aadd37a5c5
|
57
|
/**
|
mbed714 |
0:98aadd37a5c5
|
58
|
This class exposes an API to deal with UDP Sockets
|
mbed714 |
0:98aadd37a5c5
|
59
|
*/
|
mbed714 |
0:98aadd37a5c5
|
60
|
class UDPSocket
|
mbed714 |
0:98aadd37a5c5
|
61
|
{
|
mbed714 |
0:98aadd37a5c5
|
62
|
public:
|
mbed714 |
0:98aadd37a5c5
|
63
|
///Creates a new socket
|
mbed714 |
0:98aadd37a5c5
|
64
|
UDPSocket();
|
mbed714 |
0:98aadd37a5c5
|
65
|
|
mbed714 |
0:98aadd37a5c5
|
66
|
///Closes and destroys socket
|
mbed714 |
0:98aadd37a5c5
|
67
|
~UDPSocket(); //close()
|
mbed714 |
0:98aadd37a5c5
|
68
|
|
mbed714 |
0:98aadd37a5c5
|
69
|
///Binds the socket to local host or a multicast address
|
mbed714 |
0:98aadd37a5c5
|
70
|
UDPSocketErr bind(const Host& me);
|
mbed714 |
0:98aadd37a5c5
|
71
|
|
mbed714 |
0:98aadd37a5c5
|
72
|
///Sends data
|
mbed714 |
0:98aadd37a5c5
|
73
|
/*
|
mbed714 |
0:98aadd37a5c5
|
74
|
@param pHost : host to send data to
|
mbed714 |
0:98aadd37a5c5
|
75
|
@return a negative error code or the number of bytes transmitted
|
mbed714 |
0:98aadd37a5c5
|
76
|
*/
|
mbed714 |
0:98aadd37a5c5
|
77
|
int /*if < 0 : UDPSocketErr*/ sendto(const char* buf, int len, Host* pHost);
|
mbed714 |
0:98aadd37a5c5
|
78
|
|
mbed714 |
0:98aadd37a5c5
|
79
|
///Receives data
|
mbed714 |
0:98aadd37a5c5
|
80
|
/*
|
mbed714 |
0:98aadd37a5c5
|
81
|
@param pHost : host from which this piece of data comes from
|
mbed714 |
0:98aadd37a5c5
|
82
|
@return a negative error code or the number of bytes received
|
mbed714 |
0:98aadd37a5c5
|
83
|
*/
|
mbed714 |
0:98aadd37a5c5
|
84
|
int /*if < 0 : UDPSocketErr*/ recvfrom(char* buf, int len, Host* pHost);
|
mbed714 |
0:98aadd37a5c5
|
85
|
|
mbed714 |
0:98aadd37a5c5
|
86
|
/* TODO NTH : printf / scanf helpers that call send/recv */
|
mbed714 |
0:98aadd37a5c5
|
87
|
|
mbed714 |
0:98aadd37a5c5
|
88
|
///Closes socket
|
mbed714 |
0:98aadd37a5c5
|
89
|
UDPSocketErr close();
|
mbed714 |
0:98aadd37a5c5
|
90
|
|
mbed714 |
0:98aadd37a5c5
|
91
|
//Callbacks
|
mbed714 |
0:98aadd37a5c5
|
92
|
///Setups callback
|
mbed714 |
0:98aadd37a5c5
|
93
|
/**
|
mbed714 |
0:98aadd37a5c5
|
94
|
@param pMethod : callback function
|
mbed714 |
0:98aadd37a5c5
|
95
|
*/
|
mbed714 |
0:98aadd37a5c5
|
96
|
void setOnEvent( void (*pMethod)(UDPSocketEvent) );
|
mbed714 |
0:98aadd37a5c5
|
97
|
|
mbed714 |
0:98aadd37a5c5
|
98
|
class CDummy;
|
mbed714 |
0:98aadd37a5c5
|
99
|
///Setups callback
|
mbed714 |
0:98aadd37a5c5
|
100
|
/**
|
mbed714 |
0:98aadd37a5c5
|
101
|
@param pItem : instance of class on which to execute the callback method
|
mbed714 |
0:98aadd37a5c5
|
102
|
@param pMethod : callback method
|
mbed714 |
0:98aadd37a5c5
|
103
|
*/
|
mbed714 |
0:98aadd37a5c5
|
104
|
template<class T>
|
mbed714 |
0:98aadd37a5c5
|
105
|
void setOnEvent( T* pItem, void (T::*pMethod)(UDPSocketEvent) )
|
mbed714 |
0:98aadd37a5c5
|
106
|
{
|
mbed714 |
0:98aadd37a5c5
|
107
|
m_pCbItem = (CDummy*) pItem;
|
mbed714 |
0:98aadd37a5c5
|
108
|
m_pCbMeth = (void (CDummy::*)(UDPSocketEvent)) pMethod;
|
mbed714 |
0:98aadd37a5c5
|
109
|
}
|
mbed714 |
0:98aadd37a5c5
|
110
|
|
mbed714 |
0:98aadd37a5c5
|
111
|
///Disables callback
|
mbed714 |
0:98aadd37a5c5
|
112
|
void resetOnEvent();
|
mbed714 |
0:98aadd37a5c5
|
113
|
|
mbed714 |
0:98aadd37a5c5
|
114
|
protected:
|
mbed714 |
0:98aadd37a5c5
|
115
|
void onNetUdpSocketEvent(NetUdpSocketEvent e);
|
mbed714 |
0:98aadd37a5c5
|
116
|
UDPSocketErr checkInst();
|
mbed714 |
0:98aadd37a5c5
|
117
|
|
mbed714 |
0:98aadd37a5c5
|
118
|
private:
|
mbed714 |
0:98aadd37a5c5
|
119
|
NetUdpSocket* m_pNetUdpSocket;
|
mbed714 |
0:98aadd37a5c5
|
120
|
|
mbed714 |
0:98aadd37a5c5
|
121
|
CDummy* m_pCbItem;
|
mbed714 |
0:98aadd37a5c5
|
122
|
void (CDummy::*m_pCbMeth)(UDPSocketEvent);
|
mbed714 |
0:98aadd37a5c5
|
123
|
|
mbed714 |
0:98aadd37a5c5
|
124
|
void (*m_pCb)(UDPSocketEvent);
|
mbed714 |
0:98aadd37a5c5
|
125
|
|
mbed714 |
0:98aadd37a5c5
|
126
|
};
|
mbed714 |
0:98aadd37a5c5
|
127
|
|
mbed714 |
0:98aadd37a5c5
|
128
|
#endif
|