![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
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.cpp@0:70a6082c1bf7, 2016-07-14 (annotated)
- Committer:
- ShaneKirkbride
- Date:
- Thu Jul 14 20:38:18 2016 +0000
- Revision:
- 0:70a6082c1bf7
first commit. Rest example working
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ShaneKirkbride | 0:70a6082c1bf7 | 1 | #include "STMClientRest.h" |
ShaneKirkbride | 0:70a6082c1bf7 | 2 | #include "millis.h" |
ShaneKirkbride | 0:70a6082c1bf7 | 3 | |
ShaneKirkbride | 0:70a6082c1bf7 | 4 | typedef enum { |
ShaneKirkbride | 0:70a6082c1bf7 | 5 | HEADER_GENERIC = 0, |
ShaneKirkbride | 0:70a6082c1bf7 | 6 | HEADER_CONTENT_TYPE, |
ShaneKirkbride | 0:70a6082c1bf7 | 7 | HEADER_USER_AGENT |
ShaneKirkbride | 0:70a6082c1bf7 | 8 | } HEADER_TYPE; |
ShaneKirkbride | 0:70a6082c1bf7 | 9 | |
ShaneKirkbride | 0:70a6082c1bf7 | 10 | STMClientRest::STMClientRest(STMClient *e) |
ShaneKirkbride | 0:70a6082c1bf7 | 11 | { |
ShaneKirkbride | 0:70a6082c1bf7 | 12 | _elc = e; |
ShaneKirkbride | 0:70a6082c1bf7 | 13 | remote_instance = -1; |
ShaneKirkbride | 0:70a6082c1bf7 | 14 | } |
ShaneKirkbride | 0:70a6082c1bf7 | 15 | |
ShaneKirkbride | 0:70a6082c1bf7 | 16 | void STMClientRest::restCallback(void *res) |
ShaneKirkbride | 0:70a6082c1bf7 | 17 | { |
ShaneKirkbride | 0:70a6082c1bf7 | 18 | if (!res) return; |
ShaneKirkbride | 0:70a6082c1bf7 | 19 | |
ShaneKirkbride | 0:70a6082c1bf7 | 20 | STMClientResponse *resp = (STMClientResponse *)res; |
ShaneKirkbride | 0:70a6082c1bf7 | 21 | |
ShaneKirkbride | 0:70a6082c1bf7 | 22 | resp->popArg(&_status, sizeof(_status)); |
ShaneKirkbride | 0:70a6082c1bf7 | 23 | _elc->_debug->printf("REST code %i \n\r",_status); |
ShaneKirkbride | 0:70a6082c1bf7 | 24 | |
ShaneKirkbride | 0:70a6082c1bf7 | 25 | _len = resp->popArgPtr(&_data); |
ShaneKirkbride | 0:70a6082c1bf7 | 26 | } |
ShaneKirkbride | 0:70a6082c1bf7 | 27 | |
ShaneKirkbride | 0:70a6082c1bf7 | 28 | int STMClientRest::begin(const char* host, uint16_t port, bool security) |
ShaneKirkbride | 0:70a6082c1bf7 | 29 | { |
ShaneKirkbride | 0:70a6082c1bf7 | 30 | uint8_t sec = !!security; |
ShaneKirkbride | 0:70a6082c1bf7 | 31 | restCb.attach(this, &STMClientRest::restCallback); |
ShaneKirkbride | 0:70a6082c1bf7 | 32 | |
ShaneKirkbride | 0:70a6082c1bf7 | 33 | _elc->Request(CMD_REST_SETUP, (uint32_t)&restCb, 3); |
ShaneKirkbride | 0:70a6082c1bf7 | 34 | _elc->Request(host, strlen(host)); |
ShaneKirkbride | 0:70a6082c1bf7 | 35 | _elc->Request(&port, 2); |
ShaneKirkbride | 0:70a6082c1bf7 | 36 | _elc->Request(&sec, 1); |
ShaneKirkbride | 0:70a6082c1bf7 | 37 | _elc->Request(); |
ShaneKirkbride | 0:70a6082c1bf7 | 38 | |
ShaneKirkbride | 0:70a6082c1bf7 | 39 | STMClientPacket *pkt = _elc->WaitReturn(); |
ShaneKirkbride | 0:70a6082c1bf7 | 40 | if (pkt && (int32_t)pkt->value >= 0) { |
ShaneKirkbride | 0:70a6082c1bf7 | 41 | remote_instance = pkt->value; |
ShaneKirkbride | 0:70a6082c1bf7 | 42 | return 0; |
ShaneKirkbride | 0:70a6082c1bf7 | 43 | } |
ShaneKirkbride | 0:70a6082c1bf7 | 44 | return (int)pkt->value; |
ShaneKirkbride | 0:70a6082c1bf7 | 45 | } |
ShaneKirkbride | 0:70a6082c1bf7 | 46 | |
ShaneKirkbride | 0:70a6082c1bf7 | 47 | void STMClientRest::request(const char* path, const char* method, const char* data, int len) |
ShaneKirkbride | 0:70a6082c1bf7 | 48 | { |
ShaneKirkbride | 0:70a6082c1bf7 | 49 | _status = 0; |
ShaneKirkbride | 0:70a6082c1bf7 | 50 | if (remote_instance < 0) return; |
ShaneKirkbride | 0:70a6082c1bf7 | 51 | if (data != 0 && len > 0) _elc->Request(CMD_REST_REQUEST, remote_instance, 3); |
ShaneKirkbride | 0:70a6082c1bf7 | 52 | else _elc->Request(CMD_REST_REQUEST, remote_instance, 2); |
ShaneKirkbride | 0:70a6082c1bf7 | 53 | _elc->Request(method, strlen(method)); |
ShaneKirkbride | 0:70a6082c1bf7 | 54 | _elc->Request(path, strlen(path)); |
ShaneKirkbride | 0:70a6082c1bf7 | 55 | if (data != NULL && len > 0) { |
ShaneKirkbride | 0:70a6082c1bf7 | 56 | _elc->Request(data, len); |
ShaneKirkbride | 0:70a6082c1bf7 | 57 | } |
ShaneKirkbride | 0:70a6082c1bf7 | 58 | |
ShaneKirkbride | 0:70a6082c1bf7 | 59 | _elc->Request(); |
ShaneKirkbride | 0:70a6082c1bf7 | 60 | } |
ShaneKirkbride | 0:70a6082c1bf7 | 61 | |
ShaneKirkbride | 0:70a6082c1bf7 | 62 | void STMClientRest::request(const char* path, const char* method, const char* data) |
ShaneKirkbride | 0:70a6082c1bf7 | 63 | { |
ShaneKirkbride | 0:70a6082c1bf7 | 64 | request(path, method, data, strlen(data)); |
ShaneKirkbride | 0:70a6082c1bf7 | 65 | } |
ShaneKirkbride | 0:70a6082c1bf7 | 66 | |
ShaneKirkbride | 0:70a6082c1bf7 | 67 | void STMClientRest::get(const char* path, const char* data) { request(path, "GET", data); } |
ShaneKirkbride | 0:70a6082c1bf7 | 68 | void STMClientRest::post(const char* path, const char* data) { request(path, "POST", data); } |
ShaneKirkbride | 0:70a6082c1bf7 | 69 | void STMClientRest::put(const char* path, const char* data) { request(path, "PUT", data); } |
ShaneKirkbride | 0:70a6082c1bf7 | 70 | void STMClientRest::del(const char* path) { request(path, "DELETE", 0); } |
ShaneKirkbride | 0:70a6082c1bf7 | 71 | |
ShaneKirkbride | 0:70a6082c1bf7 | 72 | void STMClientRest::setHeader(const char* value) |
ShaneKirkbride | 0:70a6082c1bf7 | 73 | { |
ShaneKirkbride | 0:70a6082c1bf7 | 74 | uint8_t header_index = HEADER_GENERIC; |
ShaneKirkbride | 0:70a6082c1bf7 | 75 | _elc->Request(CMD_REST_SETHEADER, remote_instance, 2); |
ShaneKirkbride | 0:70a6082c1bf7 | 76 | _elc->Request(&header_index, 1); |
ShaneKirkbride | 0:70a6082c1bf7 | 77 | _elc->Request(value, strlen(value)); |
ShaneKirkbride | 0:70a6082c1bf7 | 78 | _elc->Request(); |
ShaneKirkbride | 0:70a6082c1bf7 | 79 | } |
ShaneKirkbride | 0:70a6082c1bf7 | 80 | |
ShaneKirkbride | 0:70a6082c1bf7 | 81 | void STMClientRest::setContentType(const char* value) |
ShaneKirkbride | 0:70a6082c1bf7 | 82 | { |
ShaneKirkbride | 0:70a6082c1bf7 | 83 | uint8_t header_index = HEADER_CONTENT_TYPE; |
ShaneKirkbride | 0:70a6082c1bf7 | 84 | _elc->Request(CMD_REST_SETHEADER, remote_instance, 2); |
ShaneKirkbride | 0:70a6082c1bf7 | 85 | _elc->Request(&header_index, 1); |
ShaneKirkbride | 0:70a6082c1bf7 | 86 | _elc->Request(value, strlen(value)); |
ShaneKirkbride | 0:70a6082c1bf7 | 87 | _elc->Request(); |
ShaneKirkbride | 0:70a6082c1bf7 | 88 | } |
ShaneKirkbride | 0:70a6082c1bf7 | 89 | |
ShaneKirkbride | 0:70a6082c1bf7 | 90 | void STMClientRest::setUserAgent(const char* value) |
ShaneKirkbride | 0:70a6082c1bf7 | 91 | { |
ShaneKirkbride | 0:70a6082c1bf7 | 92 | uint8_t header_index = HEADER_USER_AGENT; |
ShaneKirkbride | 0:70a6082c1bf7 | 93 | _elc->Request(CMD_REST_SETHEADER, remote_instance, 2); |
ShaneKirkbride | 0:70a6082c1bf7 | 94 | _elc->Request(&header_index, 1); |
ShaneKirkbride | 0:70a6082c1bf7 | 95 | _elc->Request(value, strlen(value)); |
ShaneKirkbride | 0:70a6082c1bf7 | 96 | _elc->Request(); |
ShaneKirkbride | 0:70a6082c1bf7 | 97 | } |
ShaneKirkbride | 0:70a6082c1bf7 | 98 | |
ShaneKirkbride | 0:70a6082c1bf7 | 99 | uint16_t STMClientRest::getResponse(char* data, uint16_t maxLen) |
ShaneKirkbride | 0:70a6082c1bf7 | 100 | { |
ShaneKirkbride | 0:70a6082c1bf7 | 101 | if (_status == 0) return 0; |
ShaneKirkbride | 0:70a6082c1bf7 | 102 | memcpy(data, _data, _len>maxLen?maxLen:_len); |
ShaneKirkbride | 0:70a6082c1bf7 | 103 | int16_t s = _status; |
ShaneKirkbride | 0:70a6082c1bf7 | 104 | _status = 0; |
ShaneKirkbride | 0:70a6082c1bf7 | 105 | return s; |
ShaneKirkbride | 0:70a6082c1bf7 | 106 | } |
ShaneKirkbride | 0:70a6082c1bf7 | 107 | |
ShaneKirkbride | 0:70a6082c1bf7 | 108 | uint16_t STMClientRest::waitResponse(char* data, uint16_t maxLen, uint32_t timeout) |
ShaneKirkbride | 0:70a6082c1bf7 | 109 | { |
ShaneKirkbride | 0:70a6082c1bf7 | 110 | uint32_t wait = millis(); |
ShaneKirkbride | 0:70a6082c1bf7 | 111 | while (_status == 0 && (millis() - wait < timeout)) { |
ShaneKirkbride | 0:70a6082c1bf7 | 112 | _elc->Process(); |
ShaneKirkbride | 0:70a6082c1bf7 | 113 | } |
ShaneKirkbride | 0:70a6082c1bf7 | 114 | return getResponse(data, maxLen); |
ShaneKirkbride | 0:70a6082c1bf7 | 115 | } |