Skip to content

Commit 1744286

Browse files
authored
Merge branch 'master' into feat/support_rakwireless_rak3112
2 parents 5c8f33c + bc7a549 commit 1744286

File tree

8 files changed

+185
-93
lines changed

8 files changed

+185
-93
lines changed

libraries/ESP32/examples/Camera/CameraWebServer/CameraWebServer.ino

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,10 @@
11
#include "esp_camera.h"
22
#include <WiFi.h>
33

4-
//
5-
// WARNING!!! PSRAM IC required for UXGA resolution and high JPEG quality
6-
// Ensure ESP32 Wrover Module or other board with PSRAM is selected
7-
// Partial images will be transmitted if image exceeds buffer size
8-
//
9-
// You must select partition scheme from the board menu that has at least 3MB APP space.
10-
// Face Recognition is DISABLED for ESP32 and ESP32-S2, because it takes up from 15
11-
// seconds to process single frame. Face Detection is ENABLED if PSRAM is enabled as well
12-
13-
// ===================
14-
// Select camera model
15-
// ===================
16-
//#define CAMERA_MODEL_WROVER_KIT // Has PSRAM
17-
#define CAMERA_MODEL_ESP_EYE // Has PSRAM
18-
//#define CAMERA_MODEL_ESP32S3_EYE // Has PSRAM
19-
//#define CAMERA_MODEL_M5STACK_PSRAM // Has PSRAM
20-
//#define CAMERA_MODEL_M5STACK_V2_PSRAM // M5Camera version B Has PSRAM
21-
//#define CAMERA_MODEL_M5STACK_WIDE // Has PSRAM
22-
//#define CAMERA_MODEL_M5STACK_ESP32CAM // No PSRAM
23-
//#define CAMERA_MODEL_M5STACK_UNITCAM // No PSRAM
24-
//#define CAMERA_MODEL_M5STACK_CAMS3_UNIT // Has PSRAM
25-
//#define CAMERA_MODEL_AI_THINKER // Has PSRAM
26-
//#define CAMERA_MODEL_TTGO_T_JOURNAL // No PSRAM
27-
//#define CAMERA_MODEL_XIAO_ESP32S3 // Has PSRAM
28-
// ** Espressif Internal Boards **
29-
//#define CAMERA_MODEL_ESP32_CAM_BOARD
30-
//#define CAMERA_MODEL_ESP32S2_CAM_BOARD
31-
//#define CAMERA_MODEL_ESP32S3_CAM_LCD
32-
//#define CAMERA_MODEL_DFRobot_FireBeetle2_ESP32S3 // Has PSRAM
33-
//#define CAMERA_MODEL_DFRobot_Romeo_ESP32S3 // Has PSRAM
34-
#include "camera_pins.h"
4+
// ===========================
5+
// Select camera model in board_config.h
6+
// ===========================
7+
#include "board_config.h"
358

369
// ===========================
3710
// Enter your WiFi credentials
@@ -40,7 +13,7 @@ const char *ssid = "**********";
4013
const char *password = "**********";
4114

4215
void startCameraServer();
43-
void setupLedFlash(int pin);
16+
void setupLedFlash();
4417

4518
void setup() {
4619
Serial.begin(115200);
@@ -130,7 +103,7 @@ void setup() {
130103

131104
// Setup LED FLash if LED pin is defined in camera_pins.h
132105
#if defined(LED_GPIO_NUM)
133-
setupLedFlash(LED_GPIO_NUM);
106+
setupLedFlash();
134107
#endif
135108

136109
WiFi.begin(ssid, password);

libraries/ESP32/examples/Camera/CameraWebServer/app_httpd.cpp

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,14 @@
1919
#include "esp32-hal-ledc.h"
2020
#include "sdkconfig.h"
2121
#include "camera_index.h"
22+
#include "board_config.h"
2223

2324
#if defined(ARDUINO_ARCH_ESP32) && defined(CONFIG_ARDUHAL_ESP_LOG)
2425
#include "esp32-hal-log.h"
2526
#endif
2627

27-
// Enable LED FLASH setting
28-
#define CONFIG_LED_ILLUMINATOR_ENABLED 1
29-
3028
// LED FLASH setup
31-
#if CONFIG_LED_ILLUMINATOR_ENABLED
32-
33-
#define LED_LEDC_GPIO 22 //configure LED pin
29+
#if defined(LED_GPIO_NUM)
3430
#define CONFIG_LED_MAX_INTENSITY 255
3531

3632
int led_duty = 0;
@@ -91,13 +87,13 @@ static int ra_filter_run(ra_filter_t *filter, int value) {
9187
}
9288
#endif
9389

94-
#if CONFIG_LED_ILLUMINATOR_ENABLED
90+
#if defined(LED_GPIO_NUM)
9591
void enable_led(bool en) { // Turn LED On or Off
9692
int duty = en ? led_duty : 0;
9793
if (en && isStreaming && (led_duty > CONFIG_LED_MAX_INTENSITY)) {
9894
duty = CONFIG_LED_MAX_INTENSITY;
9995
}
100-
ledcWrite(LED_LEDC_GPIO, duty);
96+
ledcWrite(LED_GPIO_NUM, duty);
10197
//ledc_set_duty(CONFIG_LED_LEDC_SPEED_MODE, CONFIG_LED_LEDC_CHANNEL, duty);
10298
//ledc_update_duty(CONFIG_LED_LEDC_SPEED_MODE, CONFIG_LED_LEDC_CHANNEL);
10399
log_i("Set LED intensity to %d", duty);
@@ -162,7 +158,7 @@ static esp_err_t capture_handler(httpd_req_t *req) {
162158
int64_t fr_start = esp_timer_get_time();
163159
#endif
164160

165-
#if CONFIG_LED_ILLUMINATOR_ENABLED
161+
#if defined(LED_GPIO_NUM)
166162
enable_led(true);
167163
vTaskDelay(150 / portTICK_PERIOD_MS); // The LED needs to be turned on ~150ms before the call to esp_camera_fb_get()
168164
fb = esp_camera_fb_get(); // or it won't be visible in the frame. A better way to do this is needed.
@@ -230,7 +226,7 @@ static esp_err_t stream_handler(httpd_req_t *req) {
230226
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
231227
httpd_resp_set_hdr(req, "X-Framerate", "60");
232228

233-
#if CONFIG_LED_ILLUMINATOR_ENABLED
229+
#if defined(LED_GPIO_NUM)
234230
isStreaming = true;
235231
enable_led(true);
236232
#endif
@@ -293,7 +289,7 @@ static esp_err_t stream_handler(httpd_req_t *req) {
293289
);
294290
}
295291

296-
#if CONFIG_LED_ILLUMINATOR_ENABLED
292+
#if defined(LED_GPIO_NUM)
297293
isStreaming = false;
298294
enable_led(false);
299295
#endif
@@ -393,7 +389,7 @@ static esp_err_t cmd_handler(httpd_req_t *req) {
393389
} else if (!strcmp(variable, "ae_level")) {
394390
res = s->set_ae_level(s, val);
395391
}
396-
#if CONFIG_LED_ILLUMINATOR_ENABLED
392+
#if defined(LED_GPIO_NUM)
397393
else if (!strcmp(variable, "led_intensity")) {
398394
led_duty = val;
399395
if (isStreaming) {
@@ -481,7 +477,7 @@ static esp_err_t status_handler(httpd_req_t *req) {
481477
p += sprintf(p, "\"vflip\":%u,", s->status.vflip);
482478
p += sprintf(p, "\"dcw\":%u,", s->status.dcw);
483479
p += sprintf(p, "\"colorbar\":%u", s->status.colorbar);
484-
#if CONFIG_LED_ILLUMINATOR_ENABLED
480+
#if defined(LED_GPIO_NUM)
485481
p += sprintf(p, ",\"led_intensity\":%u", led_duty);
486482
#else
487483
p += sprintf(p, ",\"led_intensity\":%d", -1);
@@ -843,10 +839,10 @@ void startCameraServer() {
843839
}
844840
}
845841

846-
void setupLedFlash(int pin) {
847-
#if CONFIG_LED_ILLUMINATOR_ENABLED
848-
ledcAttach(pin, 5000, 8);
842+
void setupLedFlash() {
843+
#if defined(LED_GPIO_NUM)
844+
ledcAttach(LED_GPIO_NUM, 5000, 8);
849845
#else
850-
log_i("LED flash is disabled -> CONFIG_LED_ILLUMINATOR_ENABLED = 0");
846+
log_i("LED flash is disabled -> LED_GPIO_NUM undefined");
851847
#endif
852848
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#ifndef BOARD_CONFIG_H
2+
#define BOARD_CONFIG_H
3+
4+
//
5+
// WARNING!!! PSRAM IC required for UXGA resolution and high JPEG quality
6+
// Ensure ESP32 Wrover Module or other board with PSRAM is selected
7+
// Partial images will be transmitted if image exceeds buffer size
8+
//
9+
// You must select partition scheme from the board menu that has at least 3MB APP space.
10+
11+
// ===================
12+
// Select camera model
13+
// ===================
14+
//#define CAMERA_MODEL_WROVER_KIT // Has PSRAM
15+
#define CAMERA_MODEL_ESP_EYE // Has PSRAM
16+
//#define CAMERA_MODEL_ESP32S3_EYE // Has PSRAM
17+
//#define CAMERA_MODEL_M5STACK_PSRAM // Has PSRAM
18+
//#define CAMERA_MODEL_M5STACK_V2_PSRAM // M5Camera version B Has PSRAM
19+
//#define CAMERA_MODEL_M5STACK_WIDE // Has PSRAM
20+
//#define CAMERA_MODEL_M5STACK_ESP32CAM // No PSRAM
21+
//#define CAMERA_MODEL_M5STACK_UNITCAM // No PSRAM
22+
//#define CAMERA_MODEL_M5STACK_CAMS3_UNIT // Has PSRAM
23+
//#define CAMERA_MODEL_AI_THINKER // Has PSRAM
24+
//#define CAMERA_MODEL_TTGO_T_JOURNAL // No PSRAM
25+
//#define CAMERA_MODEL_XIAO_ESP32S3 // Has PSRAM
26+
// ** Espressif Internal Boards **
27+
//#define CAMERA_MODEL_ESP32_CAM_BOARD
28+
//#define CAMERA_MODEL_ESP32S2_CAM_BOARD
29+
//#define CAMERA_MODEL_ESP32S3_CAM_LCD
30+
//#define CAMERA_MODEL_DFRobot_FireBeetle2_ESP32S3 // Has PSRAM
31+
//#define CAMERA_MODEL_DFRobot_Romeo_ESP32S3 // Has PSRAM
32+
#include "camera_pins.h"
33+
34+
#endif // BOARD_CONFIG_H

libraries/HTTPUpdateServer/src/HTTPUpdateServer.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ static const char serverIndex[] PROGMEM =
2727
</body>
2828
</html>)";
2929
static const char successResponse[] PROGMEM = "<META http-equiv=\"refresh\" content=\"15;URL=/\">Update Success! Rebooting...";
30+
static const char *csrfHeaders[2] = {"Origin", "Host"};
3031

3132
class HTTPUpdateServer {
3233
public:
@@ -56,6 +57,9 @@ class HTTPUpdateServer {
5657
_username = username;
5758
_password = password;
5859

60+
// collect headers for CSRF verification
61+
_server->collectHeaders(csrfHeaders, 2);
62+
5963
// handler for the /update form page
6064
_server->on(path.c_str(), HTTP_GET, [&]() {
6165
if (_username != emptyString && _password != emptyString && !_server->authenticate(_username.c_str(), _password.c_str())) {
@@ -69,6 +73,10 @@ class HTTPUpdateServer {
6973
path.c_str(), HTTP_POST,
7074
[&]() {
7175
if (!_authenticated) {
76+
if (_username == emptyString || _password == emptyString) {
77+
_server->send(200, F("text/html"), String(F("Update error: Wrong origin received!")));
78+
return;
79+
}
7280
return _server->requestAuthentication();
7381
}
7482
if (Update.hasError()) {
@@ -100,6 +108,17 @@ class HTTPUpdateServer {
100108
return;
101109
}
102110

111+
String origin = _server->header(String(csrfHeaders[0]));
112+
String host = _server->header(String(csrfHeaders[1]));
113+
String expectedOrigin = String("http://") + host;
114+
if (origin != expectedOrigin) {
115+
if (_serial_output) {
116+
Serial.printf("Wrong origin received! Expected: %s, Received: %s\n", expectedOrigin.c_str(), origin.c_str());
117+
}
118+
_authenticated = false;
119+
return;
120+
}
121+
103122
if (_serial_output) {
104123
Serial.printf("Update: %s\n", upload.filename.c_str());
105124
}

libraries/Update/examples/OTAWebUpdater/OTAWebUpdater.ino

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,17 @@
88
#define SSID_FORMAT "ESP32-%06lX" // 12 chars total
99
//#define PASSWORD "test123456" // generate if remarked
1010

11+
// Set the username and password for firmware upload
12+
const char *authUser = "........";
13+
const char *authPass = "........";
14+
1115
WebServer server(80);
1216
Ticker tkSecond;
1317
uint8_t otaDone = 0;
1418

19+
const char *csrfHeaders[2] = {"Origin", "Host"};
20+
static bool authenticated = false;
21+
1522
const char *alphanum = "0123456789!@#$%^&*abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
1623
String generatePass(uint8_t str_len) {
1724
String buff;
@@ -38,13 +45,17 @@ void apMode() {
3845
}
3946

4047
void handleUpdateEnd() {
48+
if (!authenticated) {
49+
return server.requestAuthentication();
50+
}
4151
server.sendHeader("Connection", "close");
4252
if (Update.hasError()) {
4353
server.send(502, "text/plain", Update.errorString());
4454
} else {
4555
server.sendHeader("Refresh", "10");
4656
server.sendHeader("Location", "/");
4757
server.send(307);
58+
delay(500);
4859
ESP.restart();
4960
}
5061
}
@@ -56,18 +67,34 @@ void handleUpdate() {
5667
}
5768
HTTPUpload &upload = server.upload();
5869
if (upload.status == UPLOAD_FILE_START) {
70+
authenticated = server.authenticate(authUser, authPass);
71+
if (!authenticated) {
72+
Serial.println("Authentication fail!");
73+
otaDone = 0;
74+
return;
75+
}
76+
String origin = server.header(String(csrfHeaders[0]));
77+
String host = server.header(String(csrfHeaders[1]));
78+
String expectedOrigin = String("http://") + host;
79+
if (origin != expectedOrigin) {
80+
Serial.printf("Wrong origin received! Expected: %s, Received: %s\n", expectedOrigin.c_str(), origin.c_str());
81+
authenticated = false;
82+
otaDone = 0;
83+
return;
84+
}
85+
5986
Serial.printf("Receiving Update: %s, Size: %d\n", upload.filename.c_str(), fsize);
6087
if (!Update.begin(fsize)) {
6188
otaDone = 0;
6289
Update.printError(Serial);
6390
}
64-
} else if (upload.status == UPLOAD_FILE_WRITE) {
91+
} else if (authenticated && upload.status == UPLOAD_FILE_WRITE) {
6592
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
6693
Update.printError(Serial);
6794
} else {
6895
otaDone = 100 * Update.progress() / Update.size();
6996
}
70-
} else if (upload.status == UPLOAD_FILE_END) {
97+
} else if (authenticated && upload.status == UPLOAD_FILE_END) {
7198
if (Update.end(true)) {
7299
Serial.printf("Update Success: %u bytes\nRebooting...\n", upload.totalSize);
73100
} else {
@@ -78,6 +105,7 @@ void handleUpdate() {
78105
}
79106

80107
void webServerInit() {
108+
server.collectHeaders(csrfHeaders, 2);
81109
server.on(
82110
"/update", HTTP_POST,
83111
[]() {
@@ -92,6 +120,9 @@ void webServerInit() {
92120
server.send_P(200, "image/x-icon", favicon_ico_gz, favicon_ico_gz_len);
93121
});
94122
server.onNotFound([]() {
123+
if (!server.authenticate(authUser, authPass)) {
124+
return server.requestAuthentication();
125+
}
95126
server.send(200, "text/html", indexHtml);
96127
});
97128
server.begin();

0 commit comments

Comments
 (0)