-
Notifications
You must be signed in to change notification settings - Fork 46
/
WiServer.h
272 lines (209 loc) · 7.47 KB
/
WiServer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/******************************************************************************
Filename: WiSever.h
Description: Main header file for the WiServer library
******************************************************************************
Copyright(c) 2009 Mark A. Patel All rights reserved.
This program is free software; you can redistribute it and/or modify it
under the terms of version 2 of the GNU General Public License as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59
Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Contact Information:
Author Date Comment
---------------------------------------------------------------
Mark A. Patel 05/27/2009 Initial version
Mark A. Patel 06/05/2009 Revised to work with stack version 1.1,
plus added various enhancements such as
multi-pass transmission, local client
checks, activity LED support, etc.
Mark A. Patel 06/22/2009 Revised client API
*****************************************************************************/
#ifndef WISERVER_H_
#define WISERVER_H_
extern "C" {
#include "witypes.h"
#include "server.h"
#include "config.h"
#include "uip.h"
}
#include "Print.h"
/*
* Function for handling data returned by a POST or GET request
*/
typedef void (*returnFunction)(char* data, int len);
/*
* Function for serving web pages
*/
typedef boolean (*pageServingFunction)(char* URL);
/*
* Function for providing the body of a POST request
*/
typedef void (*bodyFunction)();
#ifdef ENABLE_CLIENT_MODE
/*********** Classes for client-mode functionality *********/
/*
* Class that encapsulates an HTTP GET request
*/
class GETrequest
{
public:
/*
* Creates a new GETrequest with the provided IP, port, host name and URL
*/
GETrequest(uint8* ipAddr, int port, char* hostName, char* URL);
/*
* Submits the request and prevents further changes to the request until it has been
* processed.
*/
void submit();
/*
* Sets the function that should be called with data returned by the server.
*/
void setReturnFunc(returnFunction func);
/*
* Sets the authorization string for the request. Calls to this method will be ignored if the request
* has been submitted and is still being processed by WiServer.
*/
void setAuth(char* auth);
/*
* Checks if this request is currently being processed by the WiServer (i.e. it is awaiting a connection
* or is currently connected and communicating with the server). If it is, any calls that attempt
* to change the properties of the request will be ignored.
*/
boolean isActive();
/*
* Sets the URL for the request. Calls to this method will be ignored if the request
* has been submitted and is currently being processed by WiServer.
*/
void setURL(char* URL);
// Server IP address (network byte order)
uip_ipaddr_t ipAddr;
// Server port number
int port;
// Server domain name
char* hostName;
// Request URL
char* URL;
// Authorization string (may be NULL)
char* auth;
// Return value callback function (may be NULL)
returnFunction returnFunc;
// Indicates if the request is currently active (i.e. has a connection)
boolean active;
// Body data callback function (may be NULL)
bodyFunction body;
// Body preamble (may be NULL)
char* bodyPreamble;
// Pointer to the next request in the queue
GETrequest* next;
};
/*
* Class that encapsulates aN HTTP POST request
*/
class POSTrequest : public GETrequest
{
public:
/*
* Creates a new POSTrequest with the provided IP, port, host name, URL and body function.
*/
POSTrequest(uint8* ipAddr, int port, char* hostName, char* URL, bodyFunction body);
void setBodyFunc(bodyFunction body);
};
/*
* Class that encapsulates a TWEET request (basically a POST request populated with Twitter-specific data)
*/
class TWEETrequest : public POSTrequest
{
public:
/*
* Creates a new TWEETrequest with the provided auth string and message function.
* The body function will be called to provide the contents of the message; the
* 'status=' prefix is automatically inserted before the message.
*/
TWEETrequest(char* auth, bodyFunction message);
};
#endif // ENABLE_CLIENT_MODE
class Server: public Print
{
public:
/**
* Inits the server with the provided page serving function
*
* @param pageServerFunc name of the sketch's page serving function
*/
void init(pageServingFunction function);
/*
* Enables or disables verbose mode. If verbose mode is true, then WiServer
* will output log info via the Serial class. Verbose mode is disabled by
* default, but is automatically enabled if DEBUG is defined
*/
void enableVerboseMode(boolean enable);
/**
* The server task method (must be called in the main loop to run the WiServer)
*/
void server_task();
/**
* Writes a single byte to the current connection buffer
*/
virtual void write(uint8_t);
/**
* Prints a string that is stored in program memory
*/
void print_P(const char[]);
/**
* Prints a string that is stored in program memory followed by a line feed
*/
void println_P(const char[]);
/**
* Writes data of a specified length that is stored in program memory
*/
void write_P(const char[], int len);
/**
* Prints a time value in the form HH:MM:SS. The time value is in milliseconds.
*
*/
void printTime(long t);
/**
* Indicates if a page is currently being sent, and that a subsequent call to the page
* serving function may occur to request more data or to retransmit data that was lost
* in the network. Changes to the content of the page should only be made if this method
* returns false.
*/
boolean sendInProgress();
/**
* Checks if the client for the current server request resides on the same local network
* as the server. This information can be used to control access to data
* and capabilities based on where the client is located. The check is made
* by comparing the server and client addresses using the subnet mask.
* <br>
* For example, a web page front-end for a HVAC system might display read-only data
* to all clients, but may restrict changes to the settings to only those clients
* connected via the building's local network.
* <br>
* Note that security checks based on the client IP address are not 100% reliable,
* so this feature should not be relied upon to control access to sensitive data!
*/
boolean clientIsLocal();
/**
* Sets the pins used to indicate TX and RX activity over the network.
* These pins will be set to a HIGH output when activity occurs, and may
* be connected to LEDs via a suitable resistor to provide indicator lights.
* A value of -1 disables activity indication.
*/
void setIndicatorPins(int tx, int rx);
#ifdef ENABLE_CLIENT_MODE
/*
* Called by request classes to submit themselves to the queue
*/
void submitRequest(GETrequest *req);
char* base64encode(char* data);
#endif // ENABLE_CLIENT_MODE
};
// Single instance of the Server
extern Server WiServer;
#endif /* WISERVER_H_ */