Some quick code to use UDP-only (no TCP) with mBed. Echos received packets and sends packets when a button is pressed

Dependencies:   mbed

Committer:
pehrhovey
Date:
Sun Mar 14 00:54:12 2010 +0000
Revision:
0:a548a085de55

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pehrhovey 0:a548a085de55 1 #ifndef HTTPCLIENT_H
pehrhovey 0:a548a085de55 2 #define HTTPCLIENT_H
pehrhovey 0:a548a085de55 3
pehrhovey 0:a548a085de55 4 #include "TCPConnection.h"
pehrhovey 0:a548a085de55 5 #include "NetServer.h"
pehrhovey 0:a548a085de55 6 #include "iputil.h"
pehrhovey 0:a548a085de55 7
pehrhovey 0:a548a085de55 8 /* Class: HTTPClient
pehrhovey 0:a548a085de55 9 * A simple Class to fetch HTTP Pages.
pehrhovey 0:a548a085de55 10 */
pehrhovey 0:a548a085de55 11 class HTTPClient : public TCPConnection {
pehrhovey 0:a548a085de55 12 public:
pehrhovey 0:a548a085de55 13 /* Constructor: HTTPClient
pehrhovey 0:a548a085de55 14 * Creates an HTTPClient object. You might want to initialise the network server befor.
pehrhovey 0:a548a085de55 15 * If you dont do it it will be happen by the first post or get request you make.
pehrhovey 0:a548a085de55 16 *
pehrhovey 0:a548a085de55 17 * To initialize the network server on creation of the HTTPClient object it's possible to parse some arguments.
pehrhovey 0:a548a085de55 18 *
pehrhovey 0:a548a085de55 19 * Variables:
pehrhovey 0:a548a085de55 20 * hostname - A host name for the device. Might take a while to appear in the network,
pehrhovey 0:a548a085de55 21 * depends on the network infrastructure. Furthermore in most cases you have
pehrhovey 0:a548a085de55 22 * to add your domainname after the host name to address the device.
pehrhovey 0:a548a085de55 23 * Default is NULL.
pehrhovey 0:a548a085de55 24 * ip - The device ipaddress or ip_addr_any for dhcp. Default is ip_addr_any
pehrhovey 0:a548a085de55 25 * nm - The device netmask or ip_addr_any for dhcp. Default is ip_addr_any.
pehrhovey 0:a548a085de55 26 * gw - The device gateway or ip_addr_any for dhcp. Default is ip_addr_any.
pehrhovey 0:a548a085de55 27 * dns - The device first dns server ip or ip_addr_any for dhcp. Default is ip_addr_any.
pehrhovey 0:a548a085de55 28 *
pehrhovey 0:a548a085de55 29 * Example:
pehrhovey 0:a548a085de55 30 * > HTTPClient http; // Simple DHCP, brings up the TCP/IP stack on first get/post reguest.
pehrhovey 0:a548a085de55 31 *
pehrhovey 0:a548a085de55 32 * > HTTPClient http("worf"); // Brings up the device with DHCP and sets the host name "worf"
pehrhovey 0:a548a085de55 33 * > // The device will be available under worf.<your local domain>
pehrhovey 0:a548a085de55 34 * > // for example worf.1-2-3-4.dynamic.sky.com
pehrhovey 0:a548a085de55 35 *
pehrhovey 0:a548a085de55 36 * > HTTPClient http("wolf", // Brings up the device with static IP address and domain name.
pehrhovey 0:a548a085de55 37 * > IPv4(192,168,0,44), // IPv4 is a helper function which allows to rtype ipaddresses direct
pehrhovey 0:a548a085de55 38 * > IPv4(255,255,255,0), // as numbers in C++.
pehrhovey 0:a548a085de55 39 * > IPv4(192,168,0,1), // the device address is set to 192.168.0.44, netmask 255.255.255.0
pehrhovey 0:a548a085de55 40 * > IPv4(192,168,0,1)); // default gateway is 192.168.0.1 and dns to 192.168.0.1 as well.
pehrhovey 0:a548a085de55 41 * >
pehrhovey 0:a548a085de55 42 */
pehrhovey 0:a548a085de55 43 HTTPClient(const char *hostname = NULL, struct ip_addr ip = ip_addr_any, struct ip_addr nm = ip_addr_any, struct ip_addr gw = ip_addr_any, struct ip_addr dns = ip_addr_any);
pehrhovey 0:a548a085de55 44
pehrhovey 0:a548a085de55 45 /* Destructor: ~HTTPClient
pehrhovey 0:a548a085de55 46 * Destroys the HTTPClient class.
pehrhovey 0:a548a085de55 47 */
pehrhovey 0:a548a085de55 48 virtual ~HTTPClient() {
pehrhovey 0:a548a085de55 49 if(_auth) {
pehrhovey 0:a548a085de55 50 delete _auth;
pehrhovey 0:a548a085de55 51 _auth = NULL;
pehrhovey 0:a548a085de55 52 }
pehrhovey 0:a548a085de55 53 }
pehrhovey 0:a548a085de55 54
pehrhovey 0:a548a085de55 55 /* Function: headers
pehrhovey 0:a548a085de55 56 * Add header additional Information to the next post or get requests.
pehrhovey 0:a548a085de55 57 * Somtimes it is useful to add further header information. For example an auth field.
pehrhovey 0:a548a085de55 58 * Each time you call this function it will be replace the header fields given by an
pehrhovey 0:a548a085de55 59 * prior call.
pehrhovey 0:a548a085de55 60 *
pehrhovey 0:a548a085de55 61 * It will not free your data.
pehrhovey 0:a548a085de55 62 * Variables:
pehrhovey 0:a548a085de55 63 * fields - A string containing all fields you want to add. Seperated by "\\r\\n".
pehrhovey 0:a548a085de55 64 *
pehrhovey 0:a548a085de55 65 */
pehrhovey 0:a548a085de55 66 void headers(const char *fields);
pehrhovey 0:a548a085de55 67
pehrhovey 0:a548a085de55 68 /* Function: auth
pehrhovey 0:a548a085de55 69 * Enables basic authentication. Just enter username and password
pehrhovey 0:a548a085de55 70 * and they will be used for all requests.
pehrhovey 0:a548a085de55 71 * If you want to lean your username and passwort just insert NULL, NULL.
pehrhovey 0:a548a085de55 72 *
pehrhovey 0:a548a085de55 73 * Variables:
pehrhovey 0:a548a085de55 74 * user - Username for ure auth or NULL.
pehrhovey 0:a548a085de55 75 * password - Password for auth or NULL.
pehrhovey 0:a548a085de55 76 */
pehrhovey 0:a548a085de55 77 void auth(const char *user, const char *password);
pehrhovey 0:a548a085de55 78
pehrhovey 0:a548a085de55 79 /* Function: get
pehrhovey 0:a548a085de55 80 * A simple get-request just insert the url.
pehrhovey 0:a548a085de55 81 * But if you want you can get the result back as a string.
pehrhovey 0:a548a085de55 82 * Sometimes you want get a large result, more than 64 Bytes
pehrhovey 0:a548a085de55 83 * than define your size.
pehrhovey 0:a548a085de55 84 *
pehrhovey 0:a548a085de55 85 * Variables:
pehrhovey 0:a548a085de55 86 * url - The requested URL.
pehrhovey 0:a548a085de55 87 * result - The answere to your request, by default you have not to take it. But if you want it, it has a default length from 64 Bytes.
pehrhovey 0:a548a085de55 88 * rsize - The maximum size of your result. By default 64 Bytes.
pehrhovey 0:a548a085de55 89 * returns - The length of your demanted result or 1 if the request is succssesful or 0 if it failed. But it might be 0 too wether your result has 0 in length.
pehrhovey 0:a548a085de55 90 */
pehrhovey 0:a548a085de55 91 unsigned int get(const char *url, char *result = NULL, int rsize = 64);
pehrhovey 0:a548a085de55 92
pehrhovey 0:a548a085de55 93 /* Function: get
pehrhovey 0:a548a085de55 94 * A simple get-request just insert the url and a FILE Pointer.
pehrhovey 0:a548a085de55 95 * This get request will save yor result to an file. Very helpful if you demat a big bunch of data.
pehrhovey 0:a548a085de55 96 *
pehrhovey 0:a548a085de55 97 * Variables:
pehrhovey 0:a548a085de55 98 * url - The requested URL.
pehrhovey 0:a548a085de55 99 * result - The FILE Pointer in which you want to store the result.
pehrhovey 0:a548a085de55 100 * returns - The length of your demanted result or 1 if the request is succssesful or 0 if it failed. But it might be 0 too wether your result has 0 in length.
pehrhovey 0:a548a085de55 101 */
pehrhovey 0:a548a085de55 102 unsigned int get(const char *url, FILE *result);
pehrhovey 0:a548a085de55 103
pehrhovey 0:a548a085de55 104 /* Function: post
pehrhovey 0:a548a085de55 105 * A simple post-request just insert the url.
pehrhovey 0:a548a085de55 106 * You can send data if you want but they should be NULL-Terminated.
pehrhovey 0:a548a085de55 107 * If you want you can get the result back as a string.
pehrhovey 0:a548a085de55 108 * Sometimes you want get a large result, more than 64 Bytes
pehrhovey 0:a548a085de55 109 * than define your size.
pehrhovey 0:a548a085de55 110 *
pehrhovey 0:a548a085de55 111 * Variables:
pehrhovey 0:a548a085de55 112 * url - The requested URL.
pehrhovey 0:a548a085de55 113 * data - A char array of the data you might want to send.
pehrhovey 0:a548a085de55 114 * result - The answere to your request, by default you have not to take it. But if you want it, it has a default length from 64 Bytes.
pehrhovey 0:a548a085de55 115 * rsize - The maximum size of your result. By default 64 Bytes.
pehrhovey 0:a548a085de55 116 * returns - The length of your demanted result or 1 if the request is succssesful or 0 if it failed. But it might be 0 too wether your result has 0 in length.
pehrhovey 0:a548a085de55 117 */
pehrhovey 0:a548a085de55 118 unsigned int post(const char *url, const char *data = NULL, char *result = NULL, int rsize = 64);
pehrhovey 0:a548a085de55 119
pehrhovey 0:a548a085de55 120 /* Function: post
pehrhovey 0:a548a085de55 121 * A simple get-request just insert the url and a FILE Pointer.
pehrhovey 0:a548a085de55 122 * You can send data if you want but they should be NULL-Terminated.
pehrhovey 0:a548a085de55 123 * This get request will save yor result to an file. Very helpful if you demat a big bunch of data.
pehrhovey 0:a548a085de55 124 *
pehrhovey 0:a548a085de55 125 * Variables:
pehrhovey 0:a548a085de55 126 * url - The requested URL.
pehrhovey 0:a548a085de55 127 * data - A char array of the data you might want to send.
pehrhovey 0:a548a085de55 128 * result - The FILE Pointer in which you want to store the result.
pehrhovey 0:a548a085de55 129 * returns - The length of your demanted result or 1 if the request is succssesful or 0 if it failed. But it might be 0 too wether your result has 0 in length.
pehrhovey 0:a548a085de55 130 */
pehrhovey 0:a548a085de55 131 unsigned int post(const char *url, const char *data, FILE *result);
pehrhovey 0:a548a085de55 132
pehrhovey 0:a548a085de55 133 /* Function: post
pehrhovey 0:a548a085de55 134 * A simple get-request just insert the url and a two FILE Pointers to send the content of the file out and store you results.
pehrhovey 0:a548a085de55 135 * Your data to sent can come from a file.
pehrhovey 0:a548a085de55 136 * This get request will save yor result to an file. Very helpful if you demat a big bunch of data.
pehrhovey 0:a548a085de55 137 *
pehrhovey 0:a548a085de55 138 * Variables:
pehrhovey 0:a548a085de55 139 * url - The requested URL.
pehrhovey 0:a548a085de55 140 * data - A FILE Pointer of a file you might want to send.
pehrhovey 0:a548a085de55 141 * result - The FILE Pointer in which you want to store the result.
pehrhovey 0:a548a085de55 142 * returns - The length of your demanted result or 1 if the request is succssesful or 0 if it failed. But it might be 0 too wether your result has 0 in length.
pehrhovey 0:a548a085de55 143 */
pehrhovey 0:a548a085de55 144 unsigned int post(const char *url, FILE *data, FILE *result);
pehrhovey 0:a548a085de55 145
pehrhovey 0:a548a085de55 146 /* Function: post
pehrhovey 0:a548a085de55 147 * A simple get-request just insert the url and a two FILE Pointers to send the content of the file out and store you results.
pehrhovey 0:a548a085de55 148 * Your data to sent can come from a file.
pehrhovey 0:a548a085de55 149 * If you want you can get the result back as a string.
pehrhovey 0:a548a085de55 150 * Sometimes you want get a large result, more than 64 Bytes
pehrhovey 0:a548a085de55 151 * than define your size.
pehrhovey 0:a548a085de55 152 *
pehrhovey 0:a548a085de55 153 * url - The requested URL.
pehrhovey 0:a548a085de55 154 * data - A FILE Pointer of a file you might want to send.
pehrhovey 0:a548a085de55 155 * result - The answere to your request, by default you have not to take it. But if you want it, it has a default length from 64 Bytes.
pehrhovey 0:a548a085de55 156 * length - The maximum size of your result. By default 64 Bytes.
pehrhovey 0:a548a085de55 157 * returns - The length of your demanted result or 1 if the request is succssesful or 0 if it failed. But it might be 0 too wether your result has 0 in length.
pehrhovey 0:a548a085de55 158 */
pehrhovey 0:a548a085de55 159 unsigned int post(const char *url, FILE *data = NULL, char *result = NULL, int length = 64);
pehrhovey 0:a548a085de55 160
pehrhovey 0:a548a085de55 161 /* Function: timeout
pehrhovey 0:a548a085de55 162 * Sets the timout for a HTTP request.
pehrhovey 0:a548a085de55 163 * The timout is the time wich is allowed to spent between two incomming TCP packets.
pehrhovey 0:a548a085de55 164 * If the time is passed the connection will be closed.
pehrhovey 0:a548a085de55 165 */
pehrhovey 0:a548a085de55 166 void timeout(int value);
pehrhovey 0:a548a085de55 167
pehrhovey 0:a548a085de55 168 private:
pehrhovey 0:a548a085de55 169 virtual void err(err_t err);
pehrhovey 0:a548a085de55 170 virtual err_t poll();
pehrhovey 0:a548a085de55 171 virtual err_t sent(u16_t len) {return ERR_OK;};
pehrhovey 0:a548a085de55 172 virtual err_t connected(err_t err);
pehrhovey 0:a548a085de55 173 virtual err_t recv(struct pbuf *q, err_t err);
pehrhovey 0:a548a085de55 174 virtual void dnsreply(const char *hostname, struct ip_addr *ipaddr);
pehrhovey 0:a548a085de55 175 unsigned int make(const char *);
pehrhovey 0:a548a085de55 176
pehrhovey 0:a548a085de55 177 char *_auth;
pehrhovey 0:a548a085de55 178 bool _ready;
pehrhovey 0:a548a085de55 179 char _mode;
pehrhovey 0:a548a085de55 180 char _state;
pehrhovey 0:a548a085de55 181 int _timeout;
pehrhovey 0:a548a085de55 182 const char *_host;
pehrhovey 0:a548a085de55 183 const char *_path;
pehrhovey 0:a548a085de55 184 void *_result;
pehrhovey 0:a548a085de55 185 void *_data;
pehrhovey 0:a548a085de55 186 const char *_request;
pehrhovey 0:a548a085de55 187 const char *_headerfields;
pehrhovey 0:a548a085de55 188 unsigned int _hostlen;
pehrhovey 0:a548a085de55 189 unsigned int _resultoff;
pehrhovey 0:a548a085de55 190 unsigned int _resultleft;
pehrhovey 0:a548a085de55 191 int _timeout_max;
pehrhovey 0:a548a085de55 192 };
pehrhovey 0:a548a085de55 193
pehrhovey 0:a548a085de55 194 #endif