forked from khoih-prog/AsyncHTTPRequest_Generic
Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
7635f3df69 |
39
README.md
39
README.md
@ -45,6 +45,11 @@ Chunked responses are recognized and handled transparently.
|
||||
---
|
||||
---
|
||||
|
||||
### Releases v1.0.1
|
||||
|
||||
1. Restore cpp code besides Impl.h code to use in case of `multiple definition` linker error. Thanks to [Daniel Brunner](https://github.com/0xFEEDC0DE64) to report and make PR in [**Fixed linker errors when included in multiple .cpp files**](https://github.com/khoih-prog/AsyncHTTPRequest_Generic/pull/1). See [**HOWTO Fix `Multiple Definitions` Linker Error**](https://github.com/khoih-prog/AsyncHTTPRequest_Generic#HOWTO-Fix-Multiple-Definitions-Linker-Error)
|
||||
|
||||
|
||||
### Releases v1.0.0
|
||||
|
||||
1. Initial coding to add support to **STM32F/L/H/G/WB/MP1** using built-in LAN8742A Ethernet (Nucleo-144, DISCOVERY, etc).
|
||||
@ -212,6 +217,19 @@ theses files must be copied into the corresponding directory:
|
||||
---
|
||||
---
|
||||
|
||||
### HOWTO Fix `Multiple Definitions` Linker Error
|
||||
|
||||
The current library implementation, using xyz-Impl.h instead of standard xyz.cpp, possibly creates certain `Multiple Definitions` Linker error in certain use cases. Although it's simple to just modify several lines of code, either in the library or in the application, the library is adding a separate source directory, named src_cpp, besides the standard src directory.
|
||||
|
||||
To use the old standard cpp way, just
|
||||
|
||||
1. **Rename the h-only src directory into src_h.**
|
||||
2. **Then rename the cpp src_cpp directory into src.**
|
||||
3. Close then reopen the application code in Arduino IDE, etc. to recompile from scratch.
|
||||
|
||||
---
|
||||
---
|
||||
|
||||
### Examples
|
||||
|
||||
Also see examples:
|
||||
@ -442,7 +460,7 @@ AsyncHTTPRequest @ IP : 192.168.2.72
|
||||
|
||||
**************************************
|
||||
abbreviation: EDT
|
||||
client_ip: 216.154.52.212
|
||||
client_ip: aaa.bbb.ccc.ddd
|
||||
datetime: 2020-09-13T18:22:59.555816-04:00
|
||||
day_of_week: 0
|
||||
day_of_year: 257
|
||||
@ -459,7 +477,7 @@ week_number: 37
|
||||
|
||||
**************************************
|
||||
abbreviation: EDT
|
||||
client_ip: 216.154.52.212
|
||||
client_ip: aaa.bbb.ccc.ddd
|
||||
datetime: 2020-09-13T18:27:57.586325-04:00
|
||||
day_of_week: 0
|
||||
day_of_year: 257
|
||||
@ -489,7 +507,7 @@ After waiting 3.43 secs more in setup(), connection result is connected. Local I
|
||||
H
|
||||
**************************************
|
||||
abbreviation: EDT
|
||||
client_ip: 216.154.52.212
|
||||
client_ip: aaa.bbb.ccc.ddd
|
||||
datetime: 2020-09-13T19:35:37.951609-04:00
|
||||
day_of_week: 0
|
||||
day_of_year: 257
|
||||
@ -521,7 +539,7 @@ After waiting 2.35 secs more in setup(), connection result is connected. Local I
|
||||
H
|
||||
**************************************
|
||||
abbreviation: EDT
|
||||
client_ip: 216.154.52.212
|
||||
client_ip: aaa.bbb.ccc.ddd
|
||||
datetime: 2020-09-13T19:37:02.118166-04:00
|
||||
day_of_week: 0
|
||||
day_of_year: 257
|
||||
@ -539,7 +557,7 @@ week_number: 37
|
||||
HHHHHHHHH HHHHHHHHHH HHHHHHHHHH H
|
||||
**************************************
|
||||
abbreviation: EDT
|
||||
client_ip: 216.154.52.212
|
||||
client_ip: aaa.bbb.ccc.ddd
|
||||
datetime: 2020-09-13T19:42:01.507586-04:00
|
||||
day_of_week: 0
|
||||
day_of_year: 257
|
||||
@ -570,7 +588,7 @@ HTTP WebServer is @ IP : 192.168.2.81
|
||||
|
||||
**************************************
|
||||
abbreviation: EDT
|
||||
client_ip: 216.154.52.212
|
||||
client_ip: aaa.bbb.ccc.ddd
|
||||
datetime: 2020-09-13T19:56:28.295033-04:00
|
||||
day_of_week: 0
|
||||
day_of_year: 257
|
||||
@ -690,6 +708,10 @@ Submit issues to: [AsyncHTTPRequest_Generic issues](https://github.com/khoih-pro
|
||||
---
|
||||
---
|
||||
|
||||
### Releases v1.0.1
|
||||
|
||||
1. Restore cpp code besides Impl.h code to use in case of `multiple definition` linker error. Thanks to [Daniel Brunner](https://github.com/0xFEEDC0DE64) to report and make PR in [**Fixed linker errors when included in multiple .cpp files**](https://github.com/khoih-prog/AsyncHTTPRequest_Generic/pull/1). See [**HOWTO Fix `Multiple Definitions` Linker Error**](https://github.com/khoih-prog/AsyncHTTPRequest_Generic#HOWTO-Fix-Multiple-Definitions-Linker-Error)
|
||||
|
||||
### Releases v1.0.0
|
||||
|
||||
1. Initial coding to add support to **STM32F/L/H/G/WB/MP1** using built-in LAN8742A Ethernet (Nucleo-144, DISCOVERY, etc).
|
||||
@ -702,13 +724,14 @@ Submit issues to: [AsyncHTTPRequest_Generic issues](https://github.com/khoih-pro
|
||||
|
||||
This library is based on, modified, bug-fixed and improved from:
|
||||
|
||||
1. [Bob Lemaire's asyncHTTPrequest Library](https://github.com/boblemaire/asyncHTTPrequest)
|
||||
1. [Bob Lemaire's **asyncHTTPrequest Library**](https://github.com/boblemaire/asyncHTTPrequest) to use the better **asynchronous** features of these following Async TCP Libraries : ( [`ESPAsyncTCP`](https://github.com/me-no-dev/ESPAsyncTCP), [`AsyncTCP`](https://github.com/me-no-dev/AsyncTCP), and [`STM32AsyncTCP`](https://github.com/philbowles/STM32AsyncTCP) ).
|
||||
|
||||
to use the better **asynchronous** features of these following Async TCP Libraries : ( [`ESPAsyncTCP`](https://github.com/me-no-dev/ESPAsyncTCP), [`AsyncTCP`](https://github.com/me-no-dev/AsyncTCP), and [`STM32AsyncTCP`](https://github.com/philbowles/STM32AsyncTCP) ).
|
||||
2. Thanks to [Daniel Brunner](https://github.com/0xFEEDC0DE64) to report and make PR in [Fixed linker errors when included in multiple .cpp files](https://github.com/khoih-prog/AsyncHTTPRequest_Generic/pull/1) leading to v1.0.1. See [**HOWTO Fix `Multiple Definitions` Linker Error**](https://github.com/khoih-prog/AsyncHTTPRequest_Generic#HOWTO-Fix-Multiple-Definitions-Linker-Error)
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<td align="center"><a href="https://github.com/boblemaire"><img src="https://github.com/boblemaire.png" width="100px;" alt="boblemaire"/><br /><sub><b>⭐️ Bob Lemaire</b></sub></a><br /></td>
|
||||
<td align="center"><a href="https://github.com/0xFEEDC0DE64"><img src="https://github.com/0xFEEDC0DE64.png" width="100px;" alt="0xFEEDC0DE64"/><br /><sub><b>Daniel Brunner</b></sub></a><br /></td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
|
@ -17,11 +17,12 @@
|
||||
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, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
Version: 1.0.0
|
||||
Version: 1.0.1
|
||||
|
||||
Version Modified By Date Comments
|
||||
------- ----------- ---------- -----------
|
||||
1.0.0 K Hoang 14/09/2020 Initial coding to add support to STM32 using built-in Ethernet (Nucleo-144, DISCOVERY, etc).
|
||||
1.0.1 K Hoang 09/10/2020 Restore cpp code besides Impl.h code.
|
||||
*****************************************************************************************************************************/
|
||||
|
||||
#include "defines.h"
|
||||
|
@ -19,11 +19,12 @@
|
||||
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, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
Version: 1.0.0
|
||||
Version: 1.0.1
|
||||
|
||||
Version Modified By Date Comments
|
||||
------- ----------- ---------- -----------
|
||||
1.0.0 K Hoang 14/09/2020 Initial coding to add support to STM32 using built-in Ethernet (Nucleo-144, DISCOVERY, etc).
|
||||
1.0.1 K Hoang 09/10/2020 Restore cpp code besides Impl.h code.
|
||||
*****************************************************************************************************************************/
|
||||
/*
|
||||
Currently support
|
||||
|
@ -17,11 +17,12 @@
|
||||
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, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
Version: 1.0.0
|
||||
Version: 1.0.1
|
||||
|
||||
Version Modified By Date Comments
|
||||
------- ----------- ---------- -----------
|
||||
1.0.0 K Hoang 14/09/2020 Initial coding to add support to STM32 using built-in Ethernet (Nucleo-144, DISCOVERY, etc).
|
||||
1.0.1 K Hoang 09/10/2020 Restore cpp code besides Impl.h code.
|
||||
*****************************************************************************************************************************/
|
||||
|
||||
/**
|
||||
|
@ -17,11 +17,12 @@
|
||||
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, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
Version: 1.0.0
|
||||
Version: 1.0.1
|
||||
|
||||
Version Modified By Date Comments
|
||||
------- ----------- ---------- -----------
|
||||
1.0.0 K Hoang 14/09/2020 Initial coding to add support to STM32 using built-in Ethernet (Nucleo-144, DISCOVERY, etc).
|
||||
1.0.1 K Hoang 09/10/2020 Restore cpp code besides Impl.h code.
|
||||
*****************************************************************************************************************************/
|
||||
|
||||
// Dweet.io POST client. Connects to dweet.io once every ten seconds, sends a POST request and a request body.
|
||||
|
@ -17,11 +17,12 @@
|
||||
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, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
Version: 1.0.0
|
||||
Version: 1.0.1
|
||||
|
||||
Version Modified By Date Comments
|
||||
------- ----------- ---------- -----------
|
||||
1.0.0 K Hoang 14/09/2020 Initial coding to add support to STM32 using built-in Ethernet (Nucleo-144, DISCOVERY, etc).
|
||||
1.0.1 K Hoang 09/10/2020 Restore cpp code besides Impl.h code.
|
||||
*****************************************************************************************************************************/
|
||||
//************************************************************************************************************
|
||||
//
|
||||
|
@ -17,11 +17,12 @@
|
||||
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, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
Version: 1.0.0
|
||||
Version: 1.0.1
|
||||
|
||||
Version Modified By Date Comments
|
||||
------- ----------- ---------- -----------
|
||||
1.0.0 K Hoang 14/09/2020 Initial coding to add support to STM32 using built-in Ethernet (Nucleo-144, DISCOVERY, etc).
|
||||
1.0.1 K Hoang 09/10/2020 Restore cpp code besides Impl.h code.
|
||||
*****************************************************************************************************************************/
|
||||
//************************************************************************************************************
|
||||
//
|
||||
|
@ -17,11 +17,12 @@
|
||||
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, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
Version: 1.0.0
|
||||
Version: 1.0.1
|
||||
|
||||
Version Modified By Date Comments
|
||||
------- ----------- ---------- -----------
|
||||
1.0.0 K Hoang 14/09/2020 Initial coding to add support to STM32 using built-in Ethernet (Nucleo-144, DISCOVERY, etc).
|
||||
1.0.1 K Hoang 09/10/2020 Restore cpp code besides Impl.h code.
|
||||
*****************************************************************************************************************************/
|
||||
//************************************************************************************************************
|
||||
//
|
||||
|
@ -17,11 +17,12 @@
|
||||
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, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
Version: 1.0.0
|
||||
Version: 1.0.1
|
||||
|
||||
Version Modified By Date Comments
|
||||
------- ----------- ---------- -----------
|
||||
1.0.0 K Hoang 14/09/2020 Initial coding to add support to STM32 using built-in Ethernet (Nucleo-144, DISCOVERY, etc).
|
||||
1.0.1 K Hoang 09/10/2020 Restore cpp code besides Impl.h code.
|
||||
*****************************************************************************************************************************/
|
||||
|
||||
#include "defines.h"
|
||||
|
@ -17,11 +17,12 @@
|
||||
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, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
Version: 1.0.0
|
||||
Version: 1.0.1
|
||||
|
||||
Version Modified By Date Comments
|
||||
------- ----------- ---------- -----------
|
||||
1.0.0 K Hoang 14/09/2020 Initial coding to add support to STM32 using built-in Ethernet (Nucleo-144, DISCOVERY, etc).
|
||||
1.0.1 K Hoang 09/10/2020 Restore cpp code besides Impl.h code.
|
||||
*****************************************************************************************************************************/
|
||||
|
||||
#include "defines.h"
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name":"AsyncHTTPRequest_Generic",
|
||||
"version": "1.0.0",
|
||||
"version": "1.0.1",
|
||||
"description":"Simple Async HTTP Request library, supporting GET and POST, on top of AsyncTCP libraries, such as AsyncTCP, ESPAsyncTCP, AsyncTCP_STM32, etc.. for ESP32, ESP8266 and currently STM32 with built-in LAN8742A Ethernet.",
|
||||
"keywords":"async,tcp,http,ESP8266,ESP32,ESPAsyncTCP,AsyncTCP,stm32,ethernet,wifi,lan8742a",
|
||||
"authors": [
|
||||
|
@ -1,5 +1,5 @@
|
||||
name=AsyncHTTPRequest_Generic
|
||||
version=1.0.0
|
||||
version=1.0.1
|
||||
author=Bob Lemaire,Khoi Hoang
|
||||
maintainer=Khoi Hoang <khoih.prog@gmail.com>
|
||||
license=MIT
|
||||
|
@ -17,15 +17,15 @@
|
||||
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, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
Version: 1.0.0
|
||||
Version: 1.0.1
|
||||
|
||||
Version Modified By Date Comments
|
||||
------- ----------- ---------- -----------
|
||||
1.0.0 K Hoang 14/09/2020 Initial coding to add support to STM32 using built-in Ethernet (Nucleo-144, DISCOVERY, etc).
|
||||
1.0.1 K Hoang 09/10/2020 Restore cpp code besides Impl.h code.
|
||||
*****************************************************************************************************************************/
|
||||
|
||||
#ifndef AsyncHTTPRequest_Debug_STM32_H
|
||||
#define AsyncHTTPRequest_Debug_STM32_H
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifdef ASYNC_HTTP_DEBUG_PORT
|
||||
#define A_DBG_PORT ASYNC_HTTP_DEBUG_PORT
|
||||
@ -68,4 +68,3 @@
|
||||
#define AHTTP_LOGDEBUG2(x,y,z) if(_ASYNC_HTTP_LOGLEVEL_>3) { A_DBG_PORT.print("[AHTTP] "); A_DBG_PORT.print(x); A_DBG_PORT.print(" "); A_DBG_PORT.print(y); A_DBG_PORT.print(" "); A_DBG_PORT.println(z); }
|
||||
#define AHTTP_LOGDEBUG3(x,y,z,w) if(_ASYNC_HTTP_LOGLEVEL_>3) { A_DBG_PORT.print("[AHTTP] "); A_DBG_PORT.print(x); A_DBG_PORT.print(" "); A_DBG_PORT.print(y); A_DBG_PORT.print(" "); A_DBG_PORT.print(z); A_DBG_PORT.print(" "); A_DBG_PORT.println(w); }
|
||||
|
||||
#endif // AsyncHTTPRequest_Debug_STM32_H
|
||||
|
@ -17,15 +17,15 @@
|
||||
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, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
Version: 1.0.0
|
||||
Version: 1.0.1
|
||||
|
||||
Version Modified By Date Comments
|
||||
------- ----------- ---------- -----------
|
||||
1.0.0 K Hoang 14/09/2020 Initial coding to add support to STM32 using built-in Ethernet (Nucleo-144, DISCOVERY, etc).
|
||||
1.0.1 K Hoang 09/10/2020 Restore cpp code besides Impl.h code.
|
||||
*****************************************************************************************************************************/
|
||||
|
||||
#ifndef AsyncHTTPRequest_Generic_h
|
||||
#define AsyncHTTPRequest_Generic_h
|
||||
|
||||
#pragma once
|
||||
|
||||
#define AsyncHTTPRequest_Generic_version "1.0.0"
|
||||
|
||||
@ -265,5 +265,3 @@ class AsyncHTTPRequest
|
||||
};
|
||||
|
||||
#include "AsyncHTTPRequest_Impl_Generic.h"
|
||||
|
||||
#endif // AsyncHTTPRequest_Generic_h
|
||||
|
@ -17,18 +17,16 @@
|
||||
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, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
Version: 1.0.0
|
||||
Version: 1.0.1
|
||||
|
||||
Version Modified By Date Comments
|
||||
------- ----------- ---------- -----------
|
||||
1.0.0 K Hoang 14/09/2020 Initial coding to add support to STM32 using built-in Ethernet (Nucleo-144, DISCOVERY, etc).
|
||||
1.0.1 K Hoang 09/10/2020 Restore cpp code besides Impl.h code.
|
||||
*****************************************************************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef AsyncHTTPRequest_Impl_Generic_h
|
||||
#define AsyncHTTPRequest_Impl_Generic_h
|
||||
|
||||
|
||||
//**************************************************************************************************************
|
||||
AsyncHTTPRequest::AsyncHTTPRequest(): _readyState(readyStateUnsent), _HTTPcode(0), _chunked(false), _debug(DEBUG_IOTA_HTTP_SET)
|
||||
@ -529,6 +527,8 @@ bool AsyncHTTPRequest::_buildRequest()
|
||||
_request->write(_URL->path);
|
||||
_request->write(_URL->query);
|
||||
_request->write(" HTTP/1.1\r\n");
|
||||
|
||||
AHTTP_LOGDEBUG3(_HTTPmethod == HTTPmethodGET ? "GET " : "POST ", _URL->path, _URL->query, " HTTP/1.1\r\n" );
|
||||
|
||||
delete _URL;
|
||||
|
||||
@ -541,6 +541,9 @@ bool AsyncHTTPRequest::_buildRequest()
|
||||
_request->write(':');
|
||||
_request->write(hdr->value);
|
||||
_request->write("\r\n");
|
||||
|
||||
AHTTP_LOGDEBUG3(hdr->name, ":", hdr->value, "\r\n" );
|
||||
|
||||
hdr = hdr->next;
|
||||
}
|
||||
|
||||
@ -1187,5 +1190,3 @@ char* AsyncHTTPRequest::_charstar(const __FlashStringHelper * str)
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#endif // AsyncHTTPRequest_Impl_Generic_h
|
||||
|
@ -17,11 +17,12 @@
|
||||
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, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
Version: 1.0.0
|
||||
Version: 1.0.1
|
||||
|
||||
Version Modified By Date Comments
|
||||
------- ----------- ---------- -----------
|
||||
1.0.0 K Hoang 14/09/2020 Initial coding to add support to STM32 using built-in Ethernet (Nucleo-144, DISCOVERY, etc).
|
||||
1.0.1 K Hoang 09/10/2020 Restore cpp code besides Impl.h code.
|
||||
*****************************************************************************************************************************/
|
||||
|
||||
/********************************************************************************************
|
||||
@ -44,9 +45,6 @@
|
||||
********************************************************************************************/
|
||||
#pragma once
|
||||
|
||||
#ifndef xbuf_h
|
||||
#define xbuf_h
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
struct xseg
|
||||
@ -150,4 +148,3 @@ class xbuf: public Print
|
||||
|
||||
#include "utility/xbuf_Impl.h"
|
||||
|
||||
#endif // xbuf_h
|
||||
|
@ -17,17 +17,15 @@
|
||||
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, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
Version: 1.0.0
|
||||
Version: 1.0.1
|
||||
|
||||
Version Modified By Date Comments
|
||||
------- ----------- ---------- -----------
|
||||
1.0.0 K Hoang 14/09/2020 Initial coding to add support to STM32 using built-in Ethernet (Nucleo-144, DISCOVERY, etc).
|
||||
1.0.1 K Hoang 09/10/2020 Restore cpp code besides Impl.h code.
|
||||
*****************************************************************************************************************************/
|
||||
|
||||
#ifndef xbuf_Impl_h
|
||||
#define xbuf_Impl_h
|
||||
|
||||
//#include "utility/xbuf.h"
|
||||
#pragma once
|
||||
|
||||
xbuf::xbuf(const uint16_t segSize) : _head(nullptr), _tail(nullptr), _used(0), _free(0), _offset(0)
|
||||
{
|
||||
@ -392,4 +390,3 @@ void xbuf::remSeg()
|
||||
_offset = 0;
|
||||
}
|
||||
|
||||
#endif // xbuf_Impl_h
|
||||
|
70
src_cpp/AsyncHTTPRequest_Debug_Generic.h
Normal file
70
src_cpp/AsyncHTTPRequest_Debug_Generic.h
Normal file
@ -0,0 +1,70 @@
|
||||
/****************************************************************************************************************************
|
||||
src_cpp/AsyncHTTPRequest_Debug_Generic.h - Dead simple AsyncHTTPRequest for ESP8266, ESP32 and currently STM32 with built-in LAN8742A Ethernet
|
||||
|
||||
For ESP8266, ESP32 and STM32 with built-in LAN8742A Ethernet (Nucleo-144, DISCOVERY, etc)
|
||||
|
||||
AsyncHTTPRequest_STM32 is a library for the ESP8266, ESP32 and currently STM32 run built-in Ethernet WebServer
|
||||
|
||||
Based on and modified from asyncHTTPrequest Library (https://github.com/boblemaire/asyncHTTPrequest)
|
||||
|
||||
Built by Khoi Hoang https://github.com/khoih-prog/AsyncHTTPRequest_Generic
|
||||
Licensed under MIT license
|
||||
|
||||
Copyright (C) <2018> <Bob Lemaire, IoTaWatt, Inc.>
|
||||
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
|
||||
as published bythe Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
||||
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, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
Version: 1.0.1
|
||||
|
||||
Version Modified By Date Comments
|
||||
------- ----------- ---------- -----------
|
||||
1.0.0 K Hoang 14/09/2020 Initial coding to add support to STM32 using built-in Ethernet (Nucleo-144, DISCOVERY, etc).
|
||||
1.0.1 K Hoang 09/10/2020 Restore cpp code besides Impl.h code.
|
||||
*****************************************************************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifdef ASYNC_HTTP_DEBUG_PORT
|
||||
#define A_DBG_PORT ASYNC_HTTP_DEBUG_PORT
|
||||
#else
|
||||
#define A_DBG_PORT Serial
|
||||
#endif
|
||||
|
||||
// Change _ASYNC_HTTP_LOGLEVEL_ to set tracing and logging verbosity
|
||||
// 0: DISABLED: no logging
|
||||
// 1: ERROR: errors
|
||||
// 2: WARN: errors and warnings
|
||||
// 3: INFO: errors, warnings and informational (default)
|
||||
// 4: DEBUG: errors, warnings, informational and debug
|
||||
|
||||
#ifndef _ASYNC_HTTP_LOGLEVEL_
|
||||
#define _ASYNC_HTTP_LOGLEVEL_ 0
|
||||
#endif
|
||||
|
||||
#define AHTTP_LOGERROR(x) if(_ASYNC_HTTP_LOGLEVEL_>0) { A_DBG_PORT.print("[AHTTP] "); A_DBG_PORT.println(x); }
|
||||
#define AHTTP_LOGERROR0(x) if(_ASYNC_HTTP_LOGLEVEL_>0) { A_DBG_PORT.print(x); }
|
||||
#define AHTTP_LOGERROR1(x,y) if(_ASYNC_HTTP_LOGLEVEL_>0) { A_DBG_PORT.print("[AHTTP] "); A_DBG_PORT.print(x); A_DBG_PORT.print(" "); A_DBG_PORT.println(y); }
|
||||
#define AHTTP_LOGERROR2(x,y,z) if(_ASYNC_HTTP_LOGLEVEL_>0) { A_DBG_PORT.print("[AHTTP] "); A_DBG_PORT.print(x); A_DBG_PORT.print(" "); A_DBG_PORT.print(y); A_DBG_PORT.print(" "); A_DBG_PORT.println(z); }
|
||||
#define AHTTP_LOGERROR3(x,y,z,w) if(_ASYNC_HTTP_LOGLEVEL_>0) { A_DBG_PORT.print("[AHTTP] "); A_DBG_PORT.print(x); A_DBG_PORT.print(" "); A_DBG_PORT.print(y); A_DBG_PORT.print(" "); A_DBG_PORT.print(z); A_DBG_PORT.print(" "); A_DBG_PORT.println(w); }
|
||||
|
||||
#define AHTTP_LOGWARN(x) if(_ASYNC_HTTP_LOGLEVEL_>1) { A_DBG_PORT.print("[AHTTP] "); A_DBG_PORT.println(x); }
|
||||
#define AHTTP_LOGWARN0(x) if(_ASYNC_HTTP_LOGLEVEL_>1) { A_DBG_PORT.print(x); }
|
||||
#define AHTTP_LOGWARN1(x,y) if(_ASYNC_HTTP_LOGLEVEL_>1) { A_DBG_PORT.print("[AHTTP] "); A_DBG_PORT.print(x); A_DBG_PORT.print(" "); A_DBG_PORT.println(y); }
|
||||
#define AHTTP_LOGWARN2(x,y,z) if(_ASYNC_HTTP_LOGLEVEL_>1) { A_DBG_PORT.print("[AHTTP] "); A_DBG_PORT.print(x); A_DBG_PORT.print(" "); A_DBG_PORT.print(y); A_DBG_PORT.print(" "); A_DBG_PORT.println(z); }
|
||||
#define AHTTP_LOGWARN3(x,y,z,w) if(_ASYNC_HTTP_LOGLEVEL_>1) { A_DBG_PORT.print("[AHTTP] "); A_DBG_PORT.print(x); A_DBG_PORT.print(" "); A_DBG_PORT.print(y); A_DBG_PORT.print(" "); A_DBG_PORT.print(z); A_DBG_PORT.print(" "); A_DBG_PORT.println(w); }
|
||||
|
||||
#define AHTTP_LOGINFO(x) if(_ASYNC_HTTP_LOGLEVEL_>2) { A_DBG_PORT.print("[AHTTP] "); A_DBG_PORT.println(x); }
|
||||
#define AHTTP_LOGINFO0(x) if(_ASYNC_HTTP_LOGLEVEL_>2) { A_DBG_PORT.print(x); }
|
||||
#define AHTTP_LOGINFO1(x,y) if(_ASYNC_HTTP_LOGLEVEL_>2) { A_DBG_PORT.print("[AHTTP] "); A_DBG_PORT.print(x); A_DBG_PORT.print(" "); A_DBG_PORT.println(y); }
|
||||
#define AHTTP_LOGINFO2(x,y,z) if(_ASYNC_HTTP_LOGLEVEL_>2) { A_DBG_PORT.print("[AHTTP] "); A_DBG_PORT.print(x); A_DBG_PORT.print(" "); A_DBG_PORT.print(y); A_DBG_PORT.print(" "); A_DBG_PORT.println(z); }
|
||||
#define AHTTP_LOGINFO3(x,y,z,w) if(_ASYNC_HTTP_LOGLEVEL_>2) { A_DBG_PORT.print("[AHTTP] "); A_DBG_PORT.print(x); A_DBG_PORT.print(" "); A_DBG_PORT.print(y); A_DBG_PORT.print(" "); A_DBG_PORT.print(z); A_DBG_PORT.print(" "); A_DBG_PORT.println(w); }
|
||||
|
||||
#define AHTTP_LOGDEBUG(x) if(_ASYNC_HTTP_LOGLEVEL_>3) { A_DBG_PORT.print("[AHTTP] "); A_DBG_PORT.println(x); }
|
||||
#define AHTTP_LOGDEBUG0(x) if(_ASYNC_HTTP_LOGLEVEL_>3) { A_DBG_PORT.print(x); }
|
||||
#define AHTTP_LOGDEBUG1(x,y) if(_ASYNC_HTTP_LOGLEVEL_>3) { A_DBG_PORT.print("[AHTTP] "); A_DBG_PORT.print(x); A_DBG_PORT.print(" "); A_DBG_PORT.println(y); }
|
||||
#define AHTTP_LOGDEBUG2(x,y,z) if(_ASYNC_HTTP_LOGLEVEL_>3) { A_DBG_PORT.print("[AHTTP] "); A_DBG_PORT.print(x); A_DBG_PORT.print(" "); A_DBG_PORT.print(y); A_DBG_PORT.print(" "); A_DBG_PORT.println(z); }
|
||||
#define AHTTP_LOGDEBUG3(x,y,z,w) if(_ASYNC_HTTP_LOGLEVEL_>3) { A_DBG_PORT.print("[AHTTP] "); A_DBG_PORT.print(x); A_DBG_PORT.print(" "); A_DBG_PORT.print(y); A_DBG_PORT.print(" "); A_DBG_PORT.print(z); A_DBG_PORT.print(" "); A_DBG_PORT.println(w); }
|
||||
|
1192
src_cpp/AsyncHTTPRequest_Generic.cpp
Normal file
1192
src_cpp/AsyncHTTPRequest_Generic.cpp
Normal file
File diff suppressed because it is too large
Load Diff
265
src_cpp/AsyncHTTPRequest_Generic.h
Normal file
265
src_cpp/AsyncHTTPRequest_Generic.h
Normal file
@ -0,0 +1,265 @@
|
||||
/****************************************************************************************************************************
|
||||
src_cpp/AsyncHTTPRequest_Generic.h - Dead simple AsyncHTTPRequest for ESP8266, ESP32 and currently STM32 with built-in LAN8742A Ethernet
|
||||
|
||||
For ESP8266, ESP32 and STM32 with built-in LAN8742A Ethernet (Nucleo-144, DISCOVERY, etc)
|
||||
|
||||
AsyncHTTPRequest_STM32 is a library for the ESP8266, ESP32 and currently STM32 run built-in Ethernet WebServer
|
||||
|
||||
Based on and modified from asyncHTTPrequest Library (https://github.com/boblemaire/asyncHTTPrequest)
|
||||
|
||||
Built by Khoi Hoang https://github.com/khoih-prog/AsyncHTTPRequest_Generic
|
||||
Licensed under MIT license
|
||||
|
||||
Copyright (C) <2018> <Bob Lemaire, IoTaWatt, Inc.>
|
||||
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
|
||||
as published bythe Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
||||
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, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
Version: 1.0.1
|
||||
|
||||
Version Modified By Date Comments
|
||||
------- ----------- ---------- -----------
|
||||
1.0.0 K Hoang 14/09/2020 Initial coding to add support to STM32 using built-in Ethernet (Nucleo-144, DISCOVERY, etc).
|
||||
1.0.1 K Hoang 09/10/2020 Restore cpp code besides Impl.h code.
|
||||
*****************************************************************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#define AsyncHTTPRequest_Generic_version "1.0.0"
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#include "AsyncHTTPRequest_Debug_Generic.h"
|
||||
|
||||
#ifndef DEBUG_IOTA_PORT
|
||||
#define DEBUG_IOTA_PORT Serial
|
||||
#endif
|
||||
|
||||
#ifdef DEBUG_IOTA_HTTP
|
||||
#define DEBUG_IOTA_HTTP_SET true
|
||||
#else
|
||||
#define DEBUG_IOTA_HTTP_SET false
|
||||
#endif
|
||||
|
||||
#if ESP32
|
||||
|
||||
#include <AsyncTCP.h>
|
||||
#define _lock xSemaphoreTakeRecursive(threadLock,portMAX_DELAY)
|
||||
#define _unlock xSemaphoreGiveRecursive(threadLock)
|
||||
|
||||
#elif ESP8266
|
||||
|
||||
#include <ESPAsyncTCP.h>
|
||||
#define _lock
|
||||
#define _unlock
|
||||
|
||||
#elif ( defined(STM32F0) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) ||defined(STM32F4) || defined(STM32F7) || \
|
||||
defined(STM32L0) || defined(STM32L1) || defined(STM32L4) || defined(STM32H7) ||defined(STM32G0) || defined(STM32G4) || \
|
||||
defined(STM32WB) || defined(STM32MP1) )
|
||||
|
||||
#include "STM32AsyncTCP.h"
|
||||
#define _lock
|
||||
#define _unlock
|
||||
|
||||
#endif
|
||||
|
||||
#include <pgmspace.h>
|
||||
#include <utility/xbuf.h>
|
||||
|
||||
#define DEBUG_HTTP(format,...) if(_debug){\
|
||||
DEBUG_IOTA_PORT.printf("Debug(%3ld): ", millis()-_requestStartTime);\
|
||||
DEBUG_IOTA_PORT.printf_P(PSTR(format),##__VA_ARGS__);}
|
||||
|
||||
#define DEFAULT_RX_TIMEOUT 3 // Seconds for timeout
|
||||
|
||||
#define HTTPCODE_CONNECTION_REFUSED (-1)
|
||||
#define HTTPCODE_SEND_HEADER_FAILED (-2)
|
||||
#define HTTPCODE_SEND_PAYLOAD_FAILED (-3)
|
||||
#define HTTPCODE_NOT_CONNECTED (-4)
|
||||
#define HTTPCODE_CONNECTION_LOST (-5)
|
||||
#define HTTPCODE_NO_STREAM (-6)
|
||||
#define HTTPCODE_NO_HTTP_SERVER (-7)
|
||||
#define HTTPCODE_TOO_LESS_RAM (-8)
|
||||
#define HTTPCODE_ENCODING (-9)
|
||||
#define HTTPCODE_STREAM_WRITE (-10)
|
||||
#define HTTPCODE_TIMEOUT (-11)
|
||||
|
||||
typedef enum
|
||||
{
|
||||
readyStateUnsent = 0, // Client created, open not yet called
|
||||
readyStateOpened = 1, // open() has been called, connected
|
||||
readyStateHdrsRecvd = 2, // send() called, response headers available
|
||||
readyStateLoading = 3, // receiving, partial data available
|
||||
readyStateDone = 4 // Request complete, all data available.
|
||||
} reqStates;
|
||||
|
||||
class AsyncHTTPRequest
|
||||
{
|
||||
struct header
|
||||
{
|
||||
header* next;
|
||||
char* name;
|
||||
char* value;
|
||||
|
||||
header(): next(nullptr), name(nullptr), value(nullptr)
|
||||
{};
|
||||
|
||||
~header()
|
||||
{
|
||||
delete[] name;
|
||||
delete[] value;
|
||||
delete next;
|
||||
}
|
||||
};
|
||||
|
||||
struct URL
|
||||
{
|
||||
char* scheme;
|
||||
char* user;
|
||||
char* pwd;
|
||||
char* host;
|
||||
int port;
|
||||
char* path;
|
||||
char* query;
|
||||
char* fragment;
|
||||
|
||||
URL(): scheme(nullptr), user(nullptr), pwd(nullptr), host(nullptr),
|
||||
port(80), path(nullptr), query(nullptr), fragment(nullptr)
|
||||
{};
|
||||
|
||||
~URL()
|
||||
{
|
||||
delete[] scheme;
|
||||
delete[] user;
|
||||
delete[] pwd;
|
||||
delete[] host;
|
||||
delete[] path;
|
||||
delete[] query;
|
||||
delete[] fragment;
|
||||
}
|
||||
};
|
||||
|
||||
typedef std::function<void(void*, AsyncHTTPRequest*, int readyState)> readyStateChangeCB;
|
||||
typedef std::function<void(void*, AsyncHTTPRequest*, size_t available)> onDataCB;
|
||||
|
||||
public:
|
||||
AsyncHTTPRequest();
|
||||
~AsyncHTTPRequest();
|
||||
|
||||
|
||||
//External functions in typical order of use:
|
||||
//__________________________________________________________________________________________________________*/
|
||||
void setDebug(bool); // Turn debug message on/off
|
||||
bool debug(); // is debug on or off?
|
||||
|
||||
bool open(const char* /*GET/POST*/, const char* URL); // Initiate a request
|
||||
void onReadyStateChange(readyStateChangeCB, void* arg = 0); // Optional event handler for ready state change
|
||||
// or you can simply poll readyState()
|
||||
void setTimeout(int); // overide default timeout (seconds)
|
||||
|
||||
void setReqHeader(const char* name, const char* value); // add a request header
|
||||
void setReqHeader(const char* name, int32_t value); // overload to use integer value
|
||||
|
||||
#if (ESP32 || ESP8266)
|
||||
void setReqHeader(const char* name, const __FlashStringHelper* value);
|
||||
void setReqHeader(const __FlashStringHelper *name, const char* value);
|
||||
void setReqHeader(const __FlashStringHelper *name, const __FlashStringHelper* value);
|
||||
void setReqHeader(const __FlashStringHelper *name, int32_t value);
|
||||
#endif
|
||||
|
||||
bool send(); // Send the request (GET)
|
||||
bool send(String body); // Send the request (POST)
|
||||
bool send(const char* body); // Send the request (POST)
|
||||
bool send(const uint8_t* buffer, size_t len); // Send the request (POST) (binary data?)
|
||||
bool send(xbuf* body, size_t len); // Send the request (POST) data in an xbuf
|
||||
void abort(); // Abort the current operation
|
||||
|
||||
reqStates readyState(); // Return the ready state
|
||||
|
||||
int respHeaderCount(); // Retrieve count of response headers
|
||||
char* respHeaderName(int index); // Return header name by index
|
||||
char* respHeaderValue(int index); // Return header value by index
|
||||
char* respHeaderValue(const char* name); // Return header value by name
|
||||
|
||||
bool respHeaderExists(const char* name); // Does header exist by name?
|
||||
|
||||
#if (ESP32 || ESP8266)
|
||||
char* respHeaderValue(const __FlashStringHelper *name);
|
||||
bool respHeaderExists(const __FlashStringHelper *name);
|
||||
#endif
|
||||
|
||||
String headers(); // Return all headers as String
|
||||
|
||||
void onData(onDataCB, void* arg = 0); // Notify when min data is available
|
||||
size_t available(); // response available
|
||||
size_t responseLength(); // indicated response length or sum of chunks to date
|
||||
int responseHTTPcode(); // HTTP response code or (negative) error code
|
||||
String responseText(); // response (whole* or partial* as string)
|
||||
size_t responseRead(uint8_t* buffer, size_t len); // Read response into buffer
|
||||
uint32_t elapsedTime(); // Elapsed time of in progress transaction or last completed (ms)
|
||||
String version(); // Version of AsyncHTTPRequest
|
||||
//___________________________________________________________________________________________________________________________________
|
||||
|
||||
private:
|
||||
|
||||
enum {HTTPmethodGET, HTTPmethodPOST} _HTTPmethod;
|
||||
|
||||
reqStates _readyState;
|
||||
|
||||
int16_t _HTTPcode; // HTTP response code or (negative) exception code
|
||||
bool _chunked; // Processing chunked response
|
||||
bool _debug; // Debug state
|
||||
uint32_t _timeout; // Default or user overide RxTimeout in seconds
|
||||
uint32_t _lastActivity; // Time of last activity
|
||||
uint32_t _requestStartTime; // Time last open() issued
|
||||
uint32_t _requestEndTime; // Time of last disconnect
|
||||
URL* _URL; // -> URL data structure
|
||||
char* _connectedHost; // Host when connected
|
||||
int _connectedPort; // Port when connected
|
||||
AsyncClient* _client; // ESPAsyncTCP AsyncClient instance
|
||||
size_t _contentLength; // content-length header value or sum of chunk headers
|
||||
size_t _contentRead; // number of bytes retrieved by user since last open()
|
||||
readyStateChangeCB _readyStateChangeCB; // optional callback for readyState change
|
||||
void* _readyStateChangeCBarg; // associated user argument
|
||||
onDataCB _onDataCB; // optional callback when data received
|
||||
void* _onDataCBarg; // associated user argument
|
||||
|
||||
#ifdef ESP32
|
||||
SemaphoreHandle_t threadLock;
|
||||
#endif
|
||||
|
||||
// request and response String buffers and header list (same queue for request and response).
|
||||
|
||||
xbuf* _request; // Tx data buffer
|
||||
xbuf* _response; // Rx data buffer for headers
|
||||
xbuf* _chunks; // First stage for chunked response
|
||||
header* _headers; // request or (readyState > readyStateHdrsRcvd) response headers
|
||||
|
||||
// Protected functions
|
||||
|
||||
header* _addHeader(const char*, const char*);
|
||||
header* _getHeader(const char*);
|
||||
header* _getHeader(int);
|
||||
bool _buildRequest();
|
||||
bool _parseURL(const char*);
|
||||
bool _parseURL(String);
|
||||
void _processChunks();
|
||||
bool _connect();
|
||||
size_t _send();
|
||||
void _setReadyState(reqStates);
|
||||
|
||||
#if (ESP32 || ESP8266)
|
||||
char* _charstar(const __FlashStringHelper *str);
|
||||
#endif
|
||||
|
||||
// callbacks
|
||||
|
||||
void _onConnect(AsyncClient*);
|
||||
void _onDisconnect(AsyncClient*);
|
||||
void _onData(void*, size_t);
|
||||
void _onError(AsyncClient*, int8_t);
|
||||
void _onPoll(AsyncClient*);
|
||||
bool _collectHeaders();
|
||||
};
|
392
src_cpp/utility/xbuf.cpp
Normal file
392
src_cpp/utility/xbuf.cpp
Normal file
@ -0,0 +1,392 @@
|
||||
/****************************************************************************************************************************
|
||||
src_cpp/utility/xbuf_Impl.h - Dead simple AsyncHTTPRequest for ESP8266, ESP32 and currently STM32 with built-in LAN8742A Ethernet
|
||||
|
||||
For ESP8266, ESP32 and STM32 with built-in LAN8742A Ethernet (Nucleo-144, DISCOVERY, etc)
|
||||
|
||||
AsyncHTTPRequest_STM32 is a library for the ESP8266, ESP32 and currently STM32 run built-in Ethernet WebServer
|
||||
|
||||
Based on and modified from asyncHTTPrequest Library (https://github.com/boblemaire/asyncHTTPrequest)
|
||||
|
||||
Built by Khoi Hoang https://github.com/khoih-prog/AsyncHTTPRequest_Generic
|
||||
Licensed under MIT license
|
||||
|
||||
Copyright (C) <2018> <Bob Lemaire, IoTaWatt, Inc.>
|
||||
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
|
||||
as published bythe Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
||||
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, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
Version: 1.0.1
|
||||
|
||||
Version Modified By Date Comments
|
||||
------- ----------- ---------- -----------
|
||||
1.0.0 K Hoang 14/09/2020 Initial coding to add support to STM32 using built-in Ethernet (Nucleo-144, DISCOVERY, etc).
|
||||
1.0.1 K Hoang 09/10/2020 Restore cpp code besides Impl.h code.
|
||||
*****************************************************************************************************************************/
|
||||
|
||||
#include "utility/xbuf.h"
|
||||
|
||||
xbuf::xbuf(const uint16_t segSize) : _head(nullptr), _tail(nullptr), _used(0), _free(0), _offset(0)
|
||||
{
|
||||
_segSize = (segSize + 3) & -4;//((segSize + 3) >> 2) << 2;
|
||||
}
|
||||
|
||||
//*******************************************************************************************************************
|
||||
xbuf::~xbuf()
|
||||
{
|
||||
flush();
|
||||
}
|
||||
|
||||
//*******************************************************************************************************************
|
||||
size_t xbuf::write(const uint8_t byte)
|
||||
{
|
||||
return write((uint8_t*) &byte, 1);
|
||||
}
|
||||
|
||||
//*******************************************************************************************************************
|
||||
size_t xbuf::write(const char* buf)
|
||||
{
|
||||
return write((uint8_t*)buf, strlen(buf));
|
||||
}
|
||||
|
||||
//*******************************************************************************************************************
|
||||
size_t xbuf::write(String string)
|
||||
{
|
||||
return write((uint8_t*)string.c_str(), string.length());
|
||||
}
|
||||
|
||||
//*******************************************************************************************************************
|
||||
size_t xbuf::write(const uint8_t* buf, const size_t len)
|
||||
{
|
||||
size_t supply = len;
|
||||
|
||||
while (supply)
|
||||
{
|
||||
if (!_free)
|
||||
{
|
||||
addSeg();
|
||||
}
|
||||
|
||||
size_t demand = _free < supply ? _free : supply;
|
||||
memcpy(_tail->data + ((_offset + _used) % _segSize), buf + (len - supply), demand);
|
||||
_free -= demand;
|
||||
_used += demand;
|
||||
supply -= demand;
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
//*******************************************************************************************************************
|
||||
size_t xbuf::write(xbuf* buf, const size_t len)
|
||||
{
|
||||
size_t supply = len;
|
||||
|
||||
if (supply > buf->available())
|
||||
{
|
||||
supply = buf->available();
|
||||
}
|
||||
|
||||
size_t read = 0;
|
||||
|
||||
while (supply)
|
||||
{
|
||||
if (!_free)
|
||||
{
|
||||
addSeg();
|
||||
}
|
||||
|
||||
size_t demand = _free < supply ? _free : supply;
|
||||
read += buf->read(_tail->data + ((_offset + _used) % _segSize), demand);
|
||||
_free -= demand;
|
||||
_used += demand;
|
||||
supply -= demand;
|
||||
}
|
||||
|
||||
return read;
|
||||
}
|
||||
|
||||
//*******************************************************************************************************************
|
||||
uint8_t xbuf::read()
|
||||
{
|
||||
uint8_t byte = 0;
|
||||
read((uint8_t*) &byte, 1);
|
||||
|
||||
return byte;
|
||||
}
|
||||
|
||||
//*******************************************************************************************************************
|
||||
uint8_t xbuf::peek()
|
||||
{
|
||||
uint8_t byte = 0;
|
||||
peek((uint8_t*) &byte, 1);
|
||||
|
||||
return byte;
|
||||
}
|
||||
|
||||
//*******************************************************************************************************************
|
||||
size_t xbuf::read(uint8_t* buf, const size_t len)
|
||||
{
|
||||
size_t read = 0;
|
||||
|
||||
while (read < len && _used)
|
||||
{
|
||||
size_t supply = (_offset + _used) > _segSize ? _segSize - _offset : _used;
|
||||
size_t demand = len - read;
|
||||
size_t chunk = supply < demand ? supply : demand;
|
||||
memcpy(buf + read, _head->data + _offset, chunk);
|
||||
_offset += chunk;
|
||||
_used -= chunk;
|
||||
read += chunk;
|
||||
|
||||
if (_offset == _segSize)
|
||||
{
|
||||
remSeg();
|
||||
_offset = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! _used)
|
||||
{
|
||||
flush();
|
||||
}
|
||||
|
||||
return read;
|
||||
}
|
||||
|
||||
//*******************************************************************************************************************
|
||||
size_t xbuf::peek(uint8_t* buf, const size_t len)
|
||||
{
|
||||
size_t read = 0;
|
||||
xseg* seg = _head;
|
||||
size_t offset = _offset;
|
||||
size_t used = _used;
|
||||
|
||||
while (read < len && used)
|
||||
{
|
||||
size_t supply = (offset + used) > _segSize ? _segSize - offset : used;
|
||||
size_t demand = len - read;
|
||||
size_t chunk = supply < demand ? supply : demand;
|
||||
|
||||
memcpy(buf + read, seg->data + offset, chunk);
|
||||
|
||||
offset += chunk;
|
||||
used -= chunk;
|
||||
read += chunk;
|
||||
|
||||
if (offset == _segSize)
|
||||
{
|
||||
seg = seg->next;
|
||||
offset = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return read;
|
||||
}
|
||||
|
||||
//*******************************************************************************************************************
|
||||
size_t xbuf::available()
|
||||
{
|
||||
return _used;
|
||||
}
|
||||
|
||||
//*******************************************************************************************************************
|
||||
int xbuf::indexOf(const char target, const size_t begin)
|
||||
{
|
||||
char targetstr[2] = " ";
|
||||
targetstr[0] = target;
|
||||
|
||||
return indexOf(targetstr, begin);
|
||||
}
|
||||
|
||||
//*******************************************************************************************************************
|
||||
int xbuf::indexOf(const char* target, const size_t begin)
|
||||
{
|
||||
size_t targetLen = strlen(target);
|
||||
|
||||
if (targetLen > _segSize || targetLen > _used)
|
||||
return -1;
|
||||
|
||||
size_t searchPos = _offset + begin;
|
||||
size_t searchEnd = _offset + _used - targetLen;
|
||||
|
||||
if (searchPos > searchEnd)
|
||||
return -1;
|
||||
|
||||
size_t searchSeg = searchPos / _segSize;
|
||||
xseg* seg = _head;
|
||||
|
||||
while (searchSeg)
|
||||
{
|
||||
seg = seg->next;
|
||||
searchSeg --;
|
||||
}
|
||||
|
||||
size_t segPos = searchPos % _segSize;
|
||||
|
||||
while (searchPos <= searchEnd)
|
||||
{
|
||||
size_t compLen = targetLen;
|
||||
|
||||
if (compLen <= (_segSize - segPos))
|
||||
{
|
||||
if (memcmp(target, seg->data + segPos, compLen) == 0)
|
||||
{
|
||||
return searchPos - _offset;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
size_t compLen = _segSize - segPos;
|
||||
|
||||
if (memcmp(target, seg->data + segPos, compLen) == 0)
|
||||
{
|
||||
compLen = targetLen - compLen;
|
||||
|
||||
if (memcmp(target + targetLen - compLen, seg->next->data, compLen) == 0)
|
||||
{
|
||||
return searchPos - _offset;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
searchPos++;
|
||||
segPos++;
|
||||
|
||||
if (segPos == _segSize)
|
||||
{
|
||||
seg = seg->next;
|
||||
segPos = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
//*******************************************************************************************************************
|
||||
String xbuf::readStringUntil(const char target)
|
||||
{
|
||||
return readString(indexOf(target) + 1);
|
||||
}
|
||||
|
||||
//*******************************************************************************************************************
|
||||
String xbuf::readStringUntil(const char* target)
|
||||
{
|
||||
int index = indexOf(target);
|
||||
|
||||
if (index < 0)
|
||||
return String();
|
||||
|
||||
return readString(index + strlen(target));
|
||||
}
|
||||
|
||||
//*******************************************************************************************************************
|
||||
String xbuf::readString(int endPos)
|
||||
{
|
||||
String result;
|
||||
|
||||
if ( ! result.reserve(endPos + 1))
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
if (endPos > _used)
|
||||
{
|
||||
endPos = _used;
|
||||
}
|
||||
|
||||
if (endPos > 0 && result.reserve(endPos + 1))
|
||||
{
|
||||
while (endPos--)
|
||||
{
|
||||
result += (char)_head->data[_offset++];
|
||||
_used--;
|
||||
|
||||
if (_offset >= _segSize)
|
||||
{
|
||||
remSeg();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//*******************************************************************************************************************
|
||||
String xbuf::peekString(int endPos)
|
||||
{
|
||||
String result;
|
||||
|
||||
xseg* seg = _head;
|
||||
size_t offset = _offset;
|
||||
|
||||
if (endPos > _used)
|
||||
{
|
||||
endPos = _used;
|
||||
}
|
||||
|
||||
if (endPos > 0 && result.reserve(endPos + 1))
|
||||
{
|
||||
while (endPos--)
|
||||
{
|
||||
result += (char)seg->data[offset++];
|
||||
|
||||
if ( offset >= _segSize)
|
||||
{
|
||||
seg = seg->next;
|
||||
offset = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//*******************************************************************************************************************
|
||||
void xbuf::flush()
|
||||
{
|
||||
while (_head)
|
||||
remSeg();
|
||||
|
||||
_tail = nullptr;
|
||||
_offset = 0;
|
||||
_used = 0;
|
||||
_free = 0;
|
||||
}
|
||||
|
||||
//*******************************************************************************************************************
|
||||
void xbuf::addSeg()
|
||||
{
|
||||
if (_tail)
|
||||
{
|
||||
_tail->next = (xseg*) new uint32_t[_segSize / 4 + 1];
|
||||
_tail = _tail->next;
|
||||
}
|
||||
else
|
||||
{
|
||||
_tail = _head = (xseg*) new uint32_t[_segSize / 4 + 1];
|
||||
}
|
||||
|
||||
_tail->next = nullptr;
|
||||
_free += _segSize;
|
||||
}
|
||||
|
||||
//*******************************************************************************************************************
|
||||
void xbuf::remSeg()
|
||||
{
|
||||
if (_head)
|
||||
{
|
||||
xseg *next = _head->next;
|
||||
delete[] (uint32_t*) _head;
|
||||
_head = next;
|
||||
|
||||
if ( ! _head)
|
||||
{
|
||||
_tail = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
_offset = 0;
|
||||
}
|
||||
|
148
src_cpp/utility/xbuf.h
Normal file
148
src_cpp/utility/xbuf.h
Normal file
@ -0,0 +1,148 @@
|
||||
/****************************************************************************************************************************
|
||||
xbuf.h - Dead simple AsyncHTTPRequest for ESP8266, ESP32 and currently STM32 with built-in LAN8742A Ethernet
|
||||
|
||||
For ESP8266, ESP32 and STM32 with built-in LAN8742A Ethernet (Nucleo-144, DISCOVERY, etc)
|
||||
|
||||
AsyncHTTPRequest_STM32 is a library for the ESP8266, ESP32 and currently STM32 run built-in Ethernet WebServer
|
||||
|
||||
Based on and modified from asyncHTTPrequest Library (https://github.com/boblemaire/asyncHTTPrequest)
|
||||
|
||||
Built by Khoi Hoang https://github.com/khoih-prog/AsyncHTTPRequest_Generic
|
||||
Licensed under MIT license
|
||||
|
||||
Copyright (C) <2018> <Bob Lemaire, IoTaWatt, Inc.>
|
||||
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
|
||||
as published bythe Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
||||
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, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
Version: 1.0.1
|
||||
|
||||
Version Modified By Date Comments
|
||||
------- ----------- ---------- -----------
|
||||
1.0.0 K Hoang 14/09/2020 Initial coding to add support to STM32 using built-in Ethernet (Nucleo-144, DISCOVERY, etc).
|
||||
1.0.1 K Hoang 09/10/2020 Restore cpp code besides Impl.h code.
|
||||
*****************************************************************************************************************************/
|
||||
|
||||
/********************************************************************************************
|
||||
xbuf is a dynamic buffering system that supports reading and writing much like cbuf.
|
||||
The class has it's own provision for writing from buffers, Strings and other xbufs
|
||||
as well as the inherited Print functions.
|
||||
Rather than use a large contiguous heap allocation, xbuf uses a linked chain of segments
|
||||
to dynamically grow and shrink with the contents.
|
||||
There are other benefits as well to using smaller heap allocation units:
|
||||
1) A buffer can work fine in a fragmented heap environment (admittedly contributing to it)
|
||||
2) xbuf contents can be copied from one buffer to another without the need for
|
||||
2x heap during the copy.
|
||||
The segment size defaults to 64 but can be dynamically set in the constructor at creation.
|
||||
The inclusion of indexOf and read/peek until functions make it useful for handling
|
||||
data streams like HTTP, and in fact is why it was created.
|
||||
|
||||
NOTE: The size of the indexOf() search string is limited to the segment size.
|
||||
It could be extended but didn't seem to be a practical consideration.
|
||||
|
||||
********************************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
struct xseg
|
||||
{
|
||||
xseg *next;
|
||||
uint8_t data[];
|
||||
};
|
||||
|
||||
class xbuf: public Print
|
||||
{
|
||||
public:
|
||||
|
||||
xbuf(const uint16_t segSize = 64);
|
||||
virtual ~xbuf();
|
||||
|
||||
size_t write(const uint8_t);
|
||||
size_t write(const char*);
|
||||
size_t write(const uint8_t*, const size_t);
|
||||
size_t write(xbuf*, const size_t);
|
||||
size_t write(String);
|
||||
size_t available();
|
||||
int indexOf(const char, const size_t begin = 0);
|
||||
int indexOf(const char*, const size_t begin = 0);
|
||||
uint8_t read();
|
||||
size_t read(uint8_t*, size_t);
|
||||
String readStringUntil(const char);
|
||||
String readStringUntil(const char*);
|
||||
String readString(int);
|
||||
|
||||
String readString()
|
||||
{
|
||||
return readString(available());
|
||||
}
|
||||
|
||||
void flush();
|
||||
|
||||
uint8_t peek();
|
||||
size_t peek(uint8_t*, const size_t);
|
||||
|
||||
String peekStringUntil(const char target)
|
||||
{
|
||||
return peekString(indexOf(target, 0));
|
||||
}
|
||||
|
||||
String peekStringUntil(const char* target)
|
||||
{
|
||||
return peekString(indexOf(target, 0));
|
||||
}
|
||||
|
||||
String peekString()
|
||||
{
|
||||
return peekString(_used);
|
||||
}
|
||||
|
||||
String peekString(int);
|
||||
|
||||
/* In addition to the above functions,
|
||||
the following inherited functions from the Print class are available.
|
||||
|
||||
size_t printf(const char * format, ...) __attribute__ ((format (printf, 2, 3)));
|
||||
size_t printf_P(PGM_P format, ...) __attribute__((format(printf, 2, 3)));
|
||||
size_t print(const __FlashStringHelper *);
|
||||
size_t print(const String &);
|
||||
size_t print(const char[]);
|
||||
size_t print(char);
|
||||
size_t print(unsigned char, int = DEC);
|
||||
size_t print(int, int = DEC);
|
||||
size_t print(unsigned int, int = DEC);
|
||||
size_t print(long, int = DEC);
|
||||
size_t print(unsigned long, int = DEC);
|
||||
size_t print(double, int = 2);
|
||||
size_t print(const Printable&);
|
||||
|
||||
size_t println(const __FlashStringHelper *);
|
||||
size_t println(const String &s);
|
||||
size_t println(const char[]);
|
||||
size_t println(char);
|
||||
size_t println(unsigned char, int = DEC);
|
||||
size_t println(int, int = DEC);
|
||||
size_t println(unsigned int, int = DEC);
|
||||
size_t println(long, int = DEC);
|
||||
size_t println(unsigned long, int = DEC);
|
||||
size_t println(double, int = 2);
|
||||
size_t println(const Printable&);
|
||||
size_t println(void);
|
||||
*/
|
||||
|
||||
protected:
|
||||
|
||||
xseg *_head;
|
||||
xseg *_tail;
|
||||
uint16_t _used;
|
||||
uint16_t _free;
|
||||
uint16_t _offset;
|
||||
uint16_t _segSize;
|
||||
|
||||
void addSeg();
|
||||
void remSeg();
|
||||
};
|
||||
|
Reference in New Issue
Block a user