This is a SLIP interface for the STM32F446RE Nucleo Board. It is designed to work specifically with the esp-link software for the ESP8266. The program is an example of a rest command.
STMClientRest.h@12:0df73cbe5cbf, 2016-10-10 (annotated)
- Committer:
- ShaneKirkbride
- Date:
- Mon Oct 10 04:41:04 2016 +0000
- Revision:
- 12:0df73cbe5cbf
- Parent:
- 0:70a6082c1bf7
the latest;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ShaneKirkbride | 0:70a6082c1bf7 | 1 | |
ShaneKirkbride | 0:70a6082c1bf7 | 2 | #ifndef _STM_CLIENT_REST_H_ |
ShaneKirkbride | 0:70a6082c1bf7 | 3 | #define _STM_CLIENT_REST_H_ |
ShaneKirkbride | 0:70a6082c1bf7 | 4 | |
ShaneKirkbride | 0:70a6082c1bf7 | 5 | #include "FP.h" |
ShaneKirkbride | 0:70a6082c1bf7 | 6 | #include "STMClient.h" |
ShaneKirkbride | 0:70a6082c1bf7 | 7 | |
ShaneKirkbride | 0:70a6082c1bf7 | 8 | // Default timeout for REST requests when waiting for a response |
ShaneKirkbride | 0:70a6082c1bf7 | 9 | #define DEFAULT_REST_TIMEOUT 5000 |
ShaneKirkbride | 0:70a6082c1bf7 | 10 | |
ShaneKirkbride | 0:70a6082c1bf7 | 11 | typedef enum { |
ShaneKirkbride | 0:70a6082c1bf7 | 12 | HTTP_STATUS_OK = 200 |
ShaneKirkbride | 0:70a6082c1bf7 | 13 | } HTTP_STATUS; |
ShaneKirkbride | 0:70a6082c1bf7 | 14 | |
ShaneKirkbride | 0:70a6082c1bf7 | 15 | // The STMClientRest class makes simple REST requests to a remote server. Each instance |
ShaneKirkbride | 0:70a6082c1bf7 | 16 | // is used to communicate with one server and multiple instances can be created to make |
ShaneKirkbride | 0:70a6082c1bf7 | 17 | // requests to multiple servers. |
ShaneKirkbride | 0:70a6082c1bf7 | 18 | // The STMClientRest class does not support concurrent requests to the same server because |
ShaneKirkbride | 0:70a6082c1bf7 | 19 | // only a single response can be recevied at a time and the responses of the two requests |
ShaneKirkbride | 0:70a6082c1bf7 | 20 | // may arrive out of order. |
ShaneKirkbride | 0:70a6082c1bf7 | 21 | // A major limitation of the REST class is that it does not store the response body. The |
ShaneKirkbride | 0:70a6082c1bf7 | 22 | // response status is saved in the class instance, so after a request completes and before |
ShaneKirkbride | 0:70a6082c1bf7 | 23 | // the next request is made a call to getResponse will return the status. However, only a pointer |
ShaneKirkbride | 0:70a6082c1bf7 | 24 | // to the response body is saved, which means that if any other message arrives and is |
ShaneKirkbride | 0:70a6082c1bf7 | 25 | // processed then the response body is overwritten by it. What this means is that if you |
ShaneKirkbride | 0:70a6082c1bf7 | 26 | // need the response body you best use waitResponse or ensure that any call to STMClient::process |
ShaneKirkbride | 0:70a6082c1bf7 | 27 | // is followed by a call to getResponse. Ideally someone improves this class to take a callback |
ShaneKirkbride | 0:70a6082c1bf7 | 28 | // into the user's sketch? |
ShaneKirkbride | 0:70a6082c1bf7 | 29 | // Another limitation is that the response body is 100 chars long at most, this is due to the |
ShaneKirkbride | 0:70a6082c1bf7 | 30 | // limitation of the SLIP protocol buffer available. |
ShaneKirkbride | 0:70a6082c1bf7 | 31 | class STMClientRest { |
ShaneKirkbride | 0:70a6082c1bf7 | 32 | public: |
ShaneKirkbride | 0:70a6082c1bf7 | 33 | STMClientRest(STMClient *e); |
ShaneKirkbride | 0:70a6082c1bf7 | 34 | |
ShaneKirkbride | 0:70a6082c1bf7 | 35 | // Initialize communication to a remote server, this communicates with esp-link but does not |
ShaneKirkbride | 0:70a6082c1bf7 | 36 | // open a connection to the remote server. Host may be a hostname or an IP address, |
ShaneKirkbride | 0:70a6082c1bf7 | 37 | // security causes HTTPS to be used (not yet supported). Returns 0 if the set-up is |
ShaneKirkbride | 0:70a6082c1bf7 | 38 | // successful, returns a negative error code if it failed. |
ShaneKirkbride | 0:70a6082c1bf7 | 39 | int begin(const char* host, uint16_t port=80, bool security=false); |
ShaneKirkbride | 0:70a6082c1bf7 | 40 | |
ShaneKirkbride | 0:70a6082c1bf7 | 41 | // Make a request to the remote server. The data must be null-terminated |
ShaneKirkbride | 0:70a6082c1bf7 | 42 | void request(const char* path, const char* method, const char* data=NULL); |
ShaneKirkbride | 0:70a6082c1bf7 | 43 | |
ShaneKirkbride | 0:70a6082c1bf7 | 44 | // Make a request to the remote server. |
ShaneKirkbride | 0:70a6082c1bf7 | 45 | void request(const char* path, const char* method, const char* data, int len); |
ShaneKirkbride | 0:70a6082c1bf7 | 46 | |
ShaneKirkbride | 0:70a6082c1bf7 | 47 | // Make a GET request to the remote server with NULL-terminated data |
ShaneKirkbride | 0:70a6082c1bf7 | 48 | void get(const char* path, const char* data=NULL); |
ShaneKirkbride | 0:70a6082c1bf7 | 49 | |
ShaneKirkbride | 0:70a6082c1bf7 | 50 | // Make a POST request to the remote server with NULL-terminated data |
ShaneKirkbride | 0:70a6082c1bf7 | 51 | void post(const char* path, const char* data); |
ShaneKirkbride | 0:70a6082c1bf7 | 52 | |
ShaneKirkbride | 0:70a6082c1bf7 | 53 | // Make a PUT request to the remote server with NULL-terminated data |
ShaneKirkbride | 0:70a6082c1bf7 | 54 | void put(const char* path, const char* data); |
ShaneKirkbride | 0:70a6082c1bf7 | 55 | |
ShaneKirkbride | 0:70a6082c1bf7 | 56 | // Make a DELETE request to the remote server |
ShaneKirkbride | 0:70a6082c1bf7 | 57 | void del(const char* path); |
ShaneKirkbride | 0:70a6082c1bf7 | 58 | |
ShaneKirkbride | 0:70a6082c1bf7 | 59 | // Retrieve the response from the remote server, returns the HTTP status code, 0 if no |
ShaneKirkbride | 0:70a6082c1bf7 | 60 | // response (may need to wait longer) |
ShaneKirkbride | 0:70a6082c1bf7 | 61 | uint16_t getResponse(char* data, uint16_t maxLen); |
ShaneKirkbride | 0:70a6082c1bf7 | 62 | |
ShaneKirkbride | 0:70a6082c1bf7 | 63 | // Wait for the response from the remote server, returns the HTTP status code, 0 if no |
ShaneKirkbride | 0:70a6082c1bf7 | 64 | // response (timeout occurred) |
ShaneKirkbride | 0:70a6082c1bf7 | 65 | uint16_t waitResponse(char* data, uint16_t maxLen, uint32_t timeout=DEFAULT_REST_TIMEOUT); |
ShaneKirkbride | 0:70a6082c1bf7 | 66 | |
ShaneKirkbride | 0:70a6082c1bf7 | 67 | // Set the user-agent for all subsequent requests |
ShaneKirkbride | 0:70a6082c1bf7 | 68 | void setUserAgent(const char* value); |
ShaneKirkbride | 0:70a6082c1bf7 | 69 | |
ShaneKirkbride | 0:70a6082c1bf7 | 70 | // Set the Content-Type Header for all subsequent requests |
ShaneKirkbride | 0:70a6082c1bf7 | 71 | void setContentType(const char* value); |
ShaneKirkbride | 0:70a6082c1bf7 | 72 | |
ShaneKirkbride | 0:70a6082c1bf7 | 73 | // Set a custom header for all subsequent requests |
ShaneKirkbride | 0:70a6082c1bf7 | 74 | void setHeader(const char* value); |
ShaneKirkbride | 0:70a6082c1bf7 | 75 | |
ShaneKirkbride | 0:70a6082c1bf7 | 76 | private: |
ShaneKirkbride | 0:70a6082c1bf7 | 77 | int32_t remote_instance; |
ShaneKirkbride | 0:70a6082c1bf7 | 78 | STMClient *_elc; |
ShaneKirkbride | 0:70a6082c1bf7 | 79 | void restCallback(void* resp); |
ShaneKirkbride | 0:70a6082c1bf7 | 80 | FP<void, void*> restCb; |
ShaneKirkbride | 0:70a6082c1bf7 | 81 | |
ShaneKirkbride | 0:70a6082c1bf7 | 82 | int16_t _status; |
ShaneKirkbride | 0:70a6082c1bf7 | 83 | uint16_t _len; |
ShaneKirkbride | 0:70a6082c1bf7 | 84 | void *_data; |
ShaneKirkbride | 0:70a6082c1bf7 | 85 | |
ShaneKirkbride | 0:70a6082c1bf7 | 86 | |
ShaneKirkbride | 0:70a6082c1bf7 | 87 | }; |
ShaneKirkbride | 0:70a6082c1bf7 | 88 | #endif // _STM_CLIENT_REST_H_ |