Skip to content

Commit

Permalink
Merge branch 'master' into ci/style_check
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasssvaz authored Apr 15, 2024
2 parents 7bb7270 + 0596733 commit 67fa719
Show file tree
Hide file tree
Showing 20 changed files with 225 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ uint32_t scanTime = 100; //In 10ms (1000ms)
BLEScan* pBLEScan;

class MyBLEExtAdvertisingCallbacks: public BLEExtAdvertisingCallbacks {
void onResult(esp_ble_gap_ext_adv_reprot_t report) {
void onResult(esp_ble_gap_ext_adv_report_t report) {
if(report.event_type & ESP_BLE_GAP_SET_EXT_ADV_PROP_LEGACY){
// here we can receive regular advertising data from BLE4.x devices
Serial.println("BLE4.2");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static esp_ble_gap_periodic_adv_sync_params_t periodic_adv_sync_params = {

class MyBLEExtAdvertisingCallbacks : public BLEExtAdvertisingCallbacks
{
void onResult(esp_ble_gap_ext_adv_reprot_t params)
void onResult(esp_ble_gap_ext_adv_report_t params)
{
uint8_t *adv_name = NULL;
uint8_t adv_name_len = 0;
Expand Down
2 changes: 1 addition & 1 deletion libraries/BLE/src/BLEAdvertisedDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class BLEExtAdvertisingCallbacks {
* As we are scanning, we will find new devices. When found, this call back is invoked with a reference to the
* device that was found. During any individual scan, a device will only be detected one time.
*/
virtual void onResult(esp_ble_gap_ext_adv_reprot_t report) = 0;
virtual void onResult(esp_ble_gap_ext_adv_report_t report) = 0;
};
#endif // SOC_BLE_50_SUPPORTED

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ const char* HWCDC_Status() {
}

void setup() {
HWCDCSerial.onEvent(usbEventCallback);
HWCDCSerial.begin();

Serial0.begin(115200);
Serial0.setDebugOutput(true);

HWCDCSerial.begin();
HWCDCSerial.onEvent(usbEventCallback);
Serial0.println("Starting...");
}

Expand Down
4 changes: 2 additions & 2 deletions libraries/Ethernet/src/ETH.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,8 @@ bool ETHClass::begin(eth_phy_type_t type, int32_t phy_addr, int mdc, int mdio, i
/* attach to receive events */
initNetif((Network_Interface_ID)(ESP_NETIF_ID_ETH+_eth_index));

Network.onSysEvent(onEthConnected, ARDUINO_EVENT_ETH_CONNECTED);

ret = esp_eth_start(_eth_handle);
if(ret != ESP_OK){
log_e("esp_eth_start failed: %d", ret);
Expand All @@ -308,8 +310,6 @@ bool ETHClass::begin(eth_phy_type_t type, int32_t phy_addr, int mdc, int mdio, i
if(!perimanSetPinBus(_pin_power, ESP32_BUS_TYPE_ETHERNET_PWR, (void *)(this), -1, -1)){ goto err; }
}

Network.onSysEvent(onEthConnected, ARDUINO_EVENT_ETH_CONNECTED);

// holds a few milliseconds to let DHCP start and enter into a good state
// FIX ME -- adresses issue https://github.com/espressif/arduino-esp32/issues/5733
delay(50);
Expand Down
24 changes: 23 additions & 1 deletion libraries/Network/src/NetworkManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,32 @@ int NetworkManager::hostByName(const char* aHostname, IPAddress& aResult)

struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;

// **Workaround**
// LWIP AF_UNSPEC always prefers IPv4 and doesn't check what network is
// available. See https://github.com/espressif/esp-idf/issues/13255
// Until that is fixed, as a work around if we have a global scope IPv6,
// then we check IPv6 only first.
if (hasGlobalV6) {
hints.ai_family = AF_INET6;
err = lwip_getaddrinfo(aHostname, servname, &hints, &res);

if (err == ERR_OK)
{
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)res->ai_addr;
// As an array of u8_t
aResult = IPAddress(IPv6, ipv6->sin6_addr.s6_addr);
log_d("DNS found IPv6 first %s", aResult.toString().c_str());
lwip_freeaddrinfo(res);
return 1;
}
}
// **End Workaround**

hints.ai_family = AF_UNSPEC;
err = lwip_getaddrinfo(aHostname, servname, &hints, &res);

if (err == ERR_OK)
{
if (res->ai_family == AF_INET6)
Expand Down
2 changes: 1 addition & 1 deletion libraries/Update/src/Updater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ bool UpdateClass::setCryptMode(const int cryptMode){
if(cryptMode >= U_AES_DECRYPT_NONE && cryptMode <= U_AES_DECRYPT_ON){
_cryptMode = cryptMode;
}else{
log_e("bad crypt mode arguement %i", cryptMode);
log_e("bad crypt mode argument %i", cryptMode);
return false;
}
return true;
Expand Down
Empty file.
13 changes: 13 additions & 0 deletions libraries/WebServer/examples/UploadHugeFile/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Upload Huge File To SD Over Http

This project is an example of an HTTP server designed to facilitate the transfer of large files using the PUT method, in accordance with RFC specifications.

### Example cURL Command

```bash
curl -X PUT -T ./my-file.mp3 http://esp-ip/upload/my-file.mp3
```

## Resources

- RFC HTTP/1.0 - Additional Request Methods - PUT : [Link](https://datatracker.ietf.org/doc/html/rfc1945#appendix-D.1.1)
88 changes: 88 additions & 0 deletions libraries/WebServer/examples/UploadHugeFile/UploadHugeFile.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <uri/UriRegex.h>
#include <SD.h>

const char* ssid = "**********";
const char* password = "**********";

WebServer server(80);

File rawFile;
void handleCreate() {
server.send(200, "text/plain", "");
}
void handleCreateProcess() {
String path = "/" + server.pathArg(0);
HTTPRaw& raw = server.raw();
if (raw.status == RAW_START) {
if (SD.exists((char *)path.c_str())) {
SD.remove((char *)path.c_str());
}
rawFile = SD.open(path.c_str(), FILE_WRITE);
Serial.print("Upload: START, filename: ");
Serial.println(path);
} else if (raw.status == RAW_WRITE) {
if (rawFile) {
rawFile.write(raw.buf, raw.currentSize);
}
Serial.print("Upload: WRITE, Bytes: ");
Serial.println(raw.currentSize);
} else if (raw.status == RAW_END) {
if (rawFile) {
rawFile.close();
}
Serial.print("Upload: END, Size: ");
Serial.println(raw.totalSize);
}
}

void returnFail(String msg) {
server.send(500, "text/plain", msg + "\r\n");
}

void handleNotFound() {
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
}

void setup(void) {
Serial.begin(115200);

while (!SD.begin()) delay(1);
Serial.println("SD Card initialized.");

WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());

server.on(UriRegex("/upload/(.*)"), HTTP_PUT, handleCreate, handleCreateProcess);
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");

}

void loop(void) {
server.handleClient();
delay(2);//allow the cpu to switch to other tasks
}
25 changes: 24 additions & 1 deletion libraries/WebServer/src/Parsing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,30 @@ bool WebServer::_parseRequest(NetworkClient& client) {
}
}

if (!isForm){
if (!isForm && _currentHandler && _currentHandler->canRaw(_currentUri)){
log_v("Parse raw");
_currentRaw.reset(new HTTPRaw());
_currentRaw->status = RAW_START;
_currentRaw->totalSize = 0;
_currentRaw->currentSize = 0;
log_v("Start Raw");
_currentHandler->raw(*this, _currentUri, *_currentRaw);
_currentRaw->status = RAW_WRITE;

while (_currentRaw->totalSize < _clientContentLength) {
_currentRaw->currentSize = client.readBytes(_currentRaw->buf, HTTP_RAW_BUFLEN);
_currentRaw->totalSize += _currentRaw->currentSize;
if (_currentRaw->currentSize == 0) {
_currentRaw->status = RAW_ABORTED;
_currentHandler->raw(*this, _currentUri, *_currentRaw);
return false;
}
_currentHandler->raw(*this, _currentUri, *_currentRaw);
}
_currentRaw->status = RAW_END;
_currentHandler->raw(*this, _currentUri, *_currentRaw);
log_v("Finish Raw");
} else if (!isForm) {
size_t plainLength;
char* plainBuf = readBytesWithTimeout(client, _clientContentLength, plainLength, HTTP_MAX_POST_WAIT);
if (plainLength < _clientContentLength) {
Expand Down
1 change: 1 addition & 0 deletions libraries/WebServer/src/WebServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ void WebServer::handleClient() {
_currentClient = NetworkClient();
_currentStatus = HC_NONE;
_currentUpload.reset();
_currentRaw.reset();
}

if (callYield) {
Expand Down
16 changes: 16 additions & 0 deletions libraries/WebServer/src/WebServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

enum HTTPUploadStatus { UPLOAD_FILE_START, UPLOAD_FILE_WRITE, UPLOAD_FILE_END,
UPLOAD_FILE_ABORTED };
enum HTTPRawStatus { RAW_START, RAW_WRITE, RAW_END, RAW_ABORTED };
enum HTTPClientStatus { HC_NONE, HC_WAIT_READ, HC_WAIT_CLOSE };
enum HTTPAuthMethod { BASIC_AUTH, DIGEST_AUTH, OTHER_AUTH };

Expand All @@ -42,6 +43,10 @@ enum HTTPAuthMethod { BASIC_AUTH, DIGEST_AUTH, OTHER_AUTH };
#define HTTP_UPLOAD_BUFLEN 1436
#endif

#ifndef HTTP_RAW_BUFLEN
#define HTTP_RAW_BUFLEN 1436
#endif

#define HTTP_MAX_DATA_WAIT 5000 //ms to wait for the client to send the request
#define HTTP_MAX_POST_WAIT 5000 //ms to wait for POST data to arrive
#define HTTP_MAX_SEND_WAIT 5000 //ms to wait for data chunk to be ACKed
Expand All @@ -63,6 +68,15 @@ typedef struct {
uint8_t buf[HTTP_UPLOAD_BUFLEN];
} HTTPUpload;

typedef struct
{
HTTPRawStatus status;
size_t totalSize; // content size
size_t currentSize; // size of data currently in buf
uint8_t buf[HTTP_UPLOAD_BUFLEN];
void *data; // additional data
} HTTPRaw;

#include "detail/RequestHandler.h"

namespace fs {
Expand Down Expand Up @@ -128,6 +142,7 @@ class WebServer
HTTPMethod method() { return _currentMethod; }
virtual NetworkClient & client() { return _currentClient; }
HTTPUpload& upload() { return *_currentUpload; }
HTTPRaw& raw() { return *_currentRaw; }

String pathArg(unsigned int i); // get request path argument by number
String arg(String name); // get request argument value by name
Expand Down Expand Up @@ -232,6 +247,7 @@ class WebServer
RequestArgument* _postArgs;

std::unique_ptr<HTTPUpload> _currentUpload;
std::unique_ptr<HTTPRaw> _currentRaw;

int _headerKeysCount;
RequestArgument* _currentHeaders;
Expand Down
2 changes: 2 additions & 0 deletions libraries/WebServer/src/detail/RequestHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ class RequestHandler {
virtual ~RequestHandler() { }
virtual bool canHandle(HTTPMethod method, String uri) { (void) method; (void) uri; return false; }
virtual bool canUpload(String uri) { (void) uri; return false; }
virtual bool canRaw(String uri) { (void) uri; return false; }
virtual bool handle(WebServer& server, HTTPMethod requestMethod, String requestUri) { (void) server; (void) requestMethod; (void) requestUri; return false; }
virtual void upload(WebServer& server, String requestUri, HTTPUpload& upload) { (void) server; (void) requestUri; (void) upload; }
virtual void raw(WebServer& server, String requestUri, HTTPRaw& raw) { (void) server; (void) requestUri; (void) raw; }

RequestHandler* next() { return _next; }
void next(RequestHandler* r) { _next = r; }
Expand Down
13 changes: 13 additions & 0 deletions libraries/WebServer/src/detail/RequestHandlersImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ class FunctionRequestHandler : public RequestHandler {

return true;
}
bool canRaw(String requestUri) override {
if (!_ufn || _method == HTTP_GET)
return false;

return true;
}

bool handle(WebServer& server, HTTPMethod requestMethod, String requestUri) override {
(void) server;
Expand All @@ -55,6 +61,13 @@ class FunctionRequestHandler : public RequestHandler {
_ufn();
}

void raw(WebServer& server, String requestUri, HTTPRaw& raw) override {
(void)server;
(void)raw;
if (canRaw(requestUri))
_ufn();
}

protected:
WebServer::THandlerFunction _fn;
WebServer::THandlerFunction _ufn;
Expand Down
1 change: 1 addition & 0 deletions libraries/WiFi/src/WiFiGeneric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,7 @@ bool WiFiGenericClass::initiateFTM(uint8_t frm_count, uint16_t burst_period, uin
.channel = channel,
.frm_count = frm_count,
.burst_period = burst_period,
.use_get_report_api = true
};
if(mac != NULL){
memcpy(ftmi_cfg.resp_mac, mac, 6);
Expand Down
Loading

0 comments on commit 67fa719

Please sign in to comment.