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
00001 #include "STMClientRest.h" 00002 #include "millis.h" 00003 00004 typedef enum { 00005 HEADER_GENERIC = 0, 00006 HEADER_CONTENT_TYPE, 00007 HEADER_USER_AGENT 00008 } HEADER_TYPE; 00009 00010 STMClientRest::STMClientRest(STMClient *e) 00011 { 00012 _elc = e; 00013 remote_instance = -1; 00014 } 00015 00016 void STMClientRest::restCallback(void *res) 00017 { 00018 if (!res) return; 00019 00020 STMClientResponse *resp = (STMClientResponse *)res; 00021 00022 resp->popArg(&_status, sizeof(_status)); 00023 _elc->_debug->printf("REST code %i \n\r",_status); 00024 00025 _len = resp->popArgPtr(&_data); 00026 } 00027 00028 int STMClientRest::begin(const char* host, uint16_t port, bool security) 00029 { 00030 uint8_t sec = !!security; 00031 restCb.attach(this, &STMClientRest::restCallback); 00032 00033 _elc->Request(CMD_REST_SETUP, (uint32_t)&restCb, 3); 00034 _elc->Request(host, strlen(host)); 00035 _elc->Request(&port, 2); 00036 _elc->Request(&sec, 1); 00037 _elc->Request(); 00038 00039 STMClientPacket *pkt = _elc->WaitReturn(); 00040 if (pkt && (int32_t)pkt->value >= 0) { 00041 remote_instance = pkt->value; 00042 return 0; 00043 } 00044 return (int)pkt->value; 00045 } 00046 00047 void STMClientRest::request(const char* path, const char* method, const char* data, int len) 00048 { 00049 _status = 0; 00050 if (remote_instance < 0) return; 00051 if (data != 0 && len > 0) _elc->Request(CMD_REST_REQUEST, remote_instance, 3); 00052 else _elc->Request(CMD_REST_REQUEST, remote_instance, 2); 00053 _elc->Request(method, strlen(method)); 00054 _elc->Request(path, strlen(path)); 00055 if (data != NULL && len > 0) { 00056 _elc->Request(data, len); 00057 } 00058 00059 _elc->Request(); 00060 } 00061 00062 void STMClientRest::request(const char* path, const char* method, const char* data) 00063 { 00064 request(path, method, data, strlen(data)); 00065 } 00066 00067 void STMClientRest::get(const char* path, const char* data) { request(path, "GET", data); } 00068 void STMClientRest::post(const char* path, const char* data) { request(path, "POST", data); } 00069 void STMClientRest::put(const char* path, const char* data) { request(path, "PUT", data); } 00070 void STMClientRest::del(const char* path) { request(path, "DELETE", 0); } 00071 00072 void STMClientRest::setHeader(const char* value) 00073 { 00074 uint8_t header_index = HEADER_GENERIC; 00075 _elc->Request(CMD_REST_SETHEADER, remote_instance, 2); 00076 _elc->Request(&header_index, 1); 00077 _elc->Request(value, strlen(value)); 00078 _elc->Request(); 00079 } 00080 00081 void STMClientRest::setContentType(const char* value) 00082 { 00083 uint8_t header_index = HEADER_CONTENT_TYPE; 00084 _elc->Request(CMD_REST_SETHEADER, remote_instance, 2); 00085 _elc->Request(&header_index, 1); 00086 _elc->Request(value, strlen(value)); 00087 _elc->Request(); 00088 } 00089 00090 void STMClientRest::setUserAgent(const char* value) 00091 { 00092 uint8_t header_index = HEADER_USER_AGENT; 00093 _elc->Request(CMD_REST_SETHEADER, remote_instance, 2); 00094 _elc->Request(&header_index, 1); 00095 _elc->Request(value, strlen(value)); 00096 _elc->Request(); 00097 } 00098 00099 uint16_t STMClientRest::getResponse(char* data, uint16_t maxLen) 00100 { 00101 if (_status == 0) return 0; 00102 memcpy(data, _data, _len>maxLen?maxLen:_len); 00103 int16_t s = _status; 00104 _status = 0; 00105 return s; 00106 } 00107 00108 uint16_t STMClientRest::waitResponse(char* data, uint16_t maxLen, uint32_t timeout) 00109 { 00110 uint32_t wait = millis(); 00111 while (_status == 0 && (millis() - wait < timeout)) { 00112 _elc->Process(); 00113 } 00114 return getResponse(data, maxLen); 00115 }
Generated on Thu Jul 14 2022 21:27:07 by 1.7.2