mirror of
https://github.com/sle118/squeezelite-esp32.git
synced 2025-12-08 04:27:12 +03:00
WIP httpd - saving current work. likely won't compile right now!
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
set(COMPONENT_SRCS "cmd_i2ctools.c")
|
idf_component_register(SRCS "cmd_i2ctools.c"
|
||||||
set(COMPONENT_ADD_INCLUDEDIRS ".")
|
INCLUDE_DIRS "." "../tools/"
|
||||||
set(COMPONENT_REQUIRES console spi_flash)
|
REQUIRES esp_common
|
||||||
|
PRIV_REQUIRES freertos esp32 spi_flash newlib log console pthread
|
||||||
|
)
|
||||||
|
|
||||||
register_component()
|
|
||||||
|
|||||||
@@ -1,4 +1,9 @@
|
|||||||
#
|
#
|
||||||
# Main Makefile. This is basically the same as a component makefile.
|
# Main Makefile. This is basically the same as a component makefile.
|
||||||
#
|
#
|
||||||
|
CFLAGS += -D LOG_LOCAL_LEVEL=ESP_LOG_DEBUG \
|
||||||
|
-I$(COMPONENT_PATH)/../tools
|
||||||
COMPONENT_ADD_INCLUDEDIRS := .
|
COMPONENT_ADD_INCLUDEDIRS := .
|
||||||
|
COMPONENT_ADD_INCLUDEDIRS += $(COMPONENT_PATH)/../tools
|
||||||
|
COMPONENT_ADD_INCLUDEDIRS += $(COMPONENT_PATH)/../squeezelite-ota
|
||||||
|
COMPONENT_EXTRA_INCLUDES += $(PROJECT_PATH)/main/
|
||||||
|
|||||||
@@ -10,6 +10,19 @@ if (!String.prototype.format) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
var nvs_type_t = {
|
||||||
|
NVS_TYPE_U8 : 0x01, /*!< Type uint8_t */
|
||||||
|
NVS_TYPE_I8 : 0x11, /*!< Type int8_t */
|
||||||
|
NVS_TYPE_U16 : 0x02, /*!< Type uint16_t */
|
||||||
|
NVS_TYPE_I16 : 0x12, /*!< Type int16_t */
|
||||||
|
NVS_TYPE_U32 : 0x04, /*!< Type uint32_t */
|
||||||
|
NVS_TYPE_I32 : 0x14, /*!< Type int32_t */
|
||||||
|
NVS_TYPE_U64 : 0x08, /*!< Type uint64_t */
|
||||||
|
NVS_TYPE_I64 : 0x18, /*!< Type int64_t */
|
||||||
|
NVS_TYPE_STR : 0x21, /*!< Type string */
|
||||||
|
NVS_TYPE_BLOB : 0x42, /*!< Type blob */
|
||||||
|
NVS_TYPE_ANY : 0xff /*!< Must be last */
|
||||||
|
} ;
|
||||||
|
|
||||||
var releaseURL = 'https://api.github.com/repos/sle118/squeezelite-esp32/releases';
|
var releaseURL = 'https://api.github.com/repos/sle118/squeezelite-esp32/releases';
|
||||||
var recovery = false;
|
var recovery = false;
|
||||||
@@ -189,16 +202,25 @@ $(document).ready(function(){
|
|||||||
$("input#autoexec-cb").on("click", function() {
|
$("input#autoexec-cb").on("click", function() {
|
||||||
var data = { 'timestamp': Date.now() };
|
var data = { 'timestamp': Date.now() };
|
||||||
autoexec = (this.checked)?1:0;
|
autoexec = (this.checked)?1:0;
|
||||||
data['autoexec'] = autoexec;
|
data['config'] = {};
|
||||||
|
data['config'] = {
|
||||||
|
autoexec : {
|
||||||
|
value : autoexec,
|
||||||
|
type : 33
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
showMessage('please wait for the ESP32 to reboot', 'WARNING');
|
showMessage('please wait for the ESP32 to reboot', 'WARNING');
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: '/config.json',
|
url: '/config.json',
|
||||||
dataType: 'text',
|
dataType: 'text',
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
cache: false,
|
cache: false,
|
||||||
headers: { "X-Custom-autoexec": autoexec },
|
// headers: { "X-Custom-autoexec": autoexec },
|
||||||
contentType: 'application/json; charset=utf-8',
|
contentType: 'application/json; charset=utf-8',
|
||||||
data: JSON.stringify(data),
|
data: JSON.stringify(data),
|
||||||
|
|
||||||
error: function (xhr, ajaxOptions, thrownError) {
|
error: function (xhr, ajaxOptions, thrownError) {
|
||||||
console.log(xhr.status);
|
console.log(xhr.status);
|
||||||
console.log(thrownError);
|
console.log(thrownError);
|
||||||
@@ -232,14 +254,20 @@ $(document).ready(function(){
|
|||||||
$("input#save-autoexec1").on("click", function() {
|
$("input#save-autoexec1").on("click", function() {
|
||||||
var data = { 'timestamp': Date.now() };
|
var data = { 'timestamp': Date.now() };
|
||||||
autoexec1 = $("#autoexec1").val();
|
autoexec1 = $("#autoexec1").val();
|
||||||
data['autoexec1'] = autoexec1;
|
data['config'] = {};
|
||||||
|
data['config'] = {
|
||||||
|
autoexec1 : {
|
||||||
|
value : autoexec1,
|
||||||
|
type : 33
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: '/config.json',
|
url: '/config.json',
|
||||||
dataType: 'text',
|
dataType: 'text',
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
cache: false,
|
cache: false,
|
||||||
headers: { "X-Custom-autoexec1": autoexec1 },
|
// headers: { "X-Custom-autoexec1": autoexec1 },
|
||||||
contentType: 'application/json; charset=utf-8',
|
contentType: 'application/json; charset=utf-8',
|
||||||
data: JSON.stringify(data),
|
data: JSON.stringify(data),
|
||||||
error: function (xhr, ajaxOptions, thrownError) {
|
error: function (xhr, ajaxOptions, thrownError) {
|
||||||
@@ -254,15 +282,19 @@ $(document).ready(function(){
|
|||||||
|
|
||||||
$("input#save-gpio").on("click", function() {
|
$("input#save-gpio").on("click", function() {
|
||||||
var data = { 'timestamp': Date.now() };
|
var data = { 'timestamp': Date.now() };
|
||||||
|
var config = {};
|
||||||
|
|
||||||
var headers = {};
|
var headers = {};
|
||||||
$("input.gpio").each(function() {
|
$("input.gpio").each(function() {
|
||||||
var id = $(this)[0].id;
|
var id = $(this)[0].id;
|
||||||
var pin = $(this).val();
|
var pin = $(this).val();
|
||||||
if (pin != '') {
|
if (pin != '') {
|
||||||
headers["X-Custom-"+id] = pin;
|
config[id] = {};
|
||||||
data[id] = pin;
|
config[id].value = pin;
|
||||||
|
config[id].type = nvs_type_t.NVS_TYPE_STR;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
data['config'] = config;
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: '/config.json',
|
url: '/config.json',
|
||||||
dataType: 'text',
|
dataType: 'text',
|
||||||
@@ -284,23 +316,40 @@ $(document).ready(function(){
|
|||||||
$("#save-nvs").on("click", function() {
|
$("#save-nvs").on("click", function() {
|
||||||
var headers = {};
|
var headers = {};
|
||||||
var data = { 'timestamp': Date.now() };
|
var data = { 'timestamp': Date.now() };
|
||||||
|
var config = {};
|
||||||
$("input.nvs").each(function() {
|
$("input.nvs").each(function() {
|
||||||
var key = $(this)[0].id;
|
var key = $(this)[0].id;
|
||||||
var val = $(this).val();
|
var val = $(this).val();
|
||||||
|
var nvs_type = parseInt($(this)[0].attributes.nvs_type.nodeValue,10);
|
||||||
if (key != '') {
|
if (key != '') {
|
||||||
headers["X-Custom-"+key] = val;
|
config[key] = {};
|
||||||
data[key] = {};
|
if(nvs_type == nvs_type_t.NVS_TYPE_U8
|
||||||
data[key].value = val;
|
|| nvs_type == nvs_type_t.NVS_TYPE_I8
|
||||||
data[key].type = 33;
|
|| nvs_type == nvs_type_t.NVS_TYPE_U16
|
||||||
|
|| nvs_type == nvs_type_t.NVS_TYPE_I16
|
||||||
|
|| nvs_type == nvs_type_t.NVS_TYPE_U32
|
||||||
|
|| nvs_type == nvs_type_t.NVS_TYPE_I32
|
||||||
|
|| nvs_type == nvs_type_t.NVS_TYPE_U64
|
||||||
|
|| nvs_type == nvs_type_t.NVS_TYPE_I64) {
|
||||||
|
config[key].value = parseInt(val);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
config[key].value = val;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
config[key].type = nvs_type;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
var key = $("#nvs-new-key").val();
|
var key = $("#nvs-new-key").val();
|
||||||
var val = $("#nvs-new-value").val();
|
var val = $("#nvs-new-value").val();
|
||||||
if (key != '') {
|
if (key != '') {
|
||||||
headers["X-Custom-"+key] = val;
|
// headers["X-Custom-" +key] = val;
|
||||||
data[key] = {};
|
config[key] = {};
|
||||||
data[key].value = val;
|
config[key].value = val;
|
||||||
|
config[key].type = 33;
|
||||||
}
|
}
|
||||||
|
data['config'] = config;
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: '/config.json',
|
url: '/config.json',
|
||||||
dataType: 'text',
|
dataType: 'text',
|
||||||
@@ -308,7 +357,7 @@ $(document).ready(function(){
|
|||||||
cache: false,
|
cache: false,
|
||||||
headers: headers,
|
headers: headers,
|
||||||
contentType: 'application/json; charset=utf-8',
|
contentType: 'application/json; charset=utf-8',
|
||||||
data: JSON.stringify(data),
|
data : JSON.stringify(data),
|
||||||
error: function (xhr, ajaxOptions, thrownError) {
|
error: function (xhr, ajaxOptions, thrownError) {
|
||||||
console.log(xhr.status);
|
console.log(xhr.status);
|
||||||
console.log(thrownError);
|
console.log(thrownError);
|
||||||
@@ -324,13 +373,19 @@ $(document).ready(function(){
|
|||||||
if (blockFlashButton) return;
|
if (blockFlashButton) return;
|
||||||
blockFlashButton = true;
|
blockFlashButton = true;
|
||||||
var url = $("#fwurl").val();
|
var url = $("#fwurl").val();
|
||||||
data['fwurl'] = url;
|
data['config'] = {
|
||||||
|
fwurl : {
|
||||||
|
value : url,
|
||||||
|
type : 33
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: '/config.json',
|
url: '/config.json',
|
||||||
dataType: 'text',
|
dataType: 'text',
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
cache: false,
|
cache: false,
|
||||||
headers: { "X-Custom-fwurl": url },
|
|
||||||
contentType: 'application/json; charset=utf-8',
|
contentType: 'application/json; charset=utf-8',
|
||||||
data: JSON.stringify(data),
|
data: JSON.stringify(data),
|
||||||
error: function (xhr, ajaxOptions, thrownError) {
|
error: function (xhr, ajaxOptions, thrownError) {
|
||||||
@@ -509,9 +564,13 @@ function performConnect(conntype){
|
|||||||
dataType: 'text',
|
dataType: 'text',
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
cache: false,
|
cache: false,
|
||||||
headers: { 'X-Custom-ssid': selectedSSID, 'X-Custom-pwd': pwd, 'X-Custom-host_name': dhcpname },
|
// headers: { 'X-Custom-ssid': selectedSSID, 'X-Custom-pwd': pwd, 'X-Custom-host_name': dhcpname },
|
||||||
contentType: 'application/json; charset=utf-8',
|
contentType: 'application/json; charset=utf-8',
|
||||||
data: { 'timestamp': Date.now()},
|
data: JSON.stringify({ 'timestamp': Date.now(),
|
||||||
|
'ssid' : selectedSSID,
|
||||||
|
'pwd' : pwd,
|
||||||
|
'host_name' : dhcpname
|
||||||
|
}),
|
||||||
error: function (xhr, ajaxOptions, thrownError) {
|
error: function (xhr, ajaxOptions, thrownError) {
|
||||||
console.log(xhr.status);
|
console.log(xhr.status);
|
||||||
console.log(thrownError);
|
console.log(thrownError);
|
||||||
@@ -737,7 +796,7 @@ function getConfig() {
|
|||||||
"<tr>"+
|
"<tr>"+
|
||||||
"<td>"+key+"</td>"+
|
"<td>"+key+"</td>"+
|
||||||
"<td class='value'>"+
|
"<td class='value'>"+
|
||||||
"<input type='text' class='form-control nvs' id='"+key+"'>"+
|
"<input type='text' class='form-control nvs' id='"+key+"' nvs_type="+data[key].type+" >"+
|
||||||
"</td>"+
|
"</td>"+
|
||||||
"</tr>"
|
"</tr>"
|
||||||
);
|
);
|
||||||
@@ -750,7 +809,7 @@ function getConfig() {
|
|||||||
"<input type='text' class='form-control' id='nvs-new-key' placeholder='new key'>"+
|
"<input type='text' class='form-control' id='nvs-new-key' placeholder='new key'>"+
|
||||||
"</td>"+
|
"</td>"+
|
||||||
"<td>"+
|
"<td>"+
|
||||||
"<input type='text' class='form-control' id='nvs-new-value' placeholder='new value'>"+
|
"<input type='text' class='form-control' id='nvs-new-value' placeholder='new value' nvs_type=33 >"+ // todo: provide a way to choose field type
|
||||||
"</td>"+
|
"</td>"+
|
||||||
"</tr>"
|
"</tr>"
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
#
|
#
|
||||||
COMPONENT_EMBED_FILES := style.css code.js index.html bootstrap.min.css.gz jquery.min.js.gz popper.min.js.gz bootstrap.min.js.gz
|
COMPONENT_EMBED_FILES := style.css code.js index.html bootstrap.min.css.gz jquery.min.js.gz popper.min.js.gz bootstrap.min.js.gz
|
||||||
#CFLAGS += -D LOG_LOCAL_LEVEL=ESP_LOG_DEBUG
|
#CFLAGS += -D LOG_LOCAL_LEVEL=ESP_LOG_DEBUG
|
||||||
CFLAGS += -D LOG_LOCAL_LEVEL=ESP_LOG_INFO \
|
CFLAGS += -D LOG_LOCAL_LEVEL=ESP_LOG_DEBUG \
|
||||||
-I$(COMPONENT_PATH)/../tools
|
-I$(COMPONENT_PATH)/../tools
|
||||||
COMPONENT_ADD_INCLUDEDIRS := .
|
COMPONENT_ADD_INCLUDEDIRS := .
|
||||||
COMPONENT_ADD_INCLUDEDIRS += $(COMPONENT_PATH)/../tools
|
COMPONENT_ADD_INCLUDEDIRS += $(COMPONENT_PATH)/../tools
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -37,6 +37,7 @@ function to process requests, decode URLs, serve files, etc. etc.
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include "esp_http_server.h"
|
||||||
#include "wifi_manager.h"
|
#include "wifi_manager.h"
|
||||||
#include "freertos/FreeRTOS.h"
|
#include "freertos/FreeRTOS.h"
|
||||||
#include "freertos/task.h"
|
#include "freertos/task.h"
|
||||||
@@ -58,10 +59,21 @@ function to process requests, decode URLs, serve files, etc. etc.
|
|||||||
#include "lwip/priv/api_msg.h"
|
#include "lwip/priv/api_msg.h"
|
||||||
#include "lwip/priv/tcp_priv.h"
|
#include "lwip/priv/tcp_priv.h"
|
||||||
#include "lwip/priv/tcpip_priv.h"
|
#include "lwip/priv/tcpip_priv.h"
|
||||||
|
#include "esp_vfs.h"
|
||||||
|
#define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
|
||||||
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#define ESP_LOGE_LOC(t,str, ...) ESP_LOGE(t, "%s(%d): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__)
|
||||||
|
#define ESP_LOGI_LOC(t,str, ...) ESP_LOGI(t, "%s(%d): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__)
|
||||||
|
#define ESP_LOGD_LOC(t,str, ...) ESP_LOGD(t, "%s(%d): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__)
|
||||||
|
#define ESP_LOGW_LOC(t,str, ...) ESP_LOGW(t, "%s(%d): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__)
|
||||||
|
#define ESP_LOGV_LOC(t,str, ...) ESP_LOGV(t, "%s(%d): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__)
|
||||||
|
|
||||||
|
|
||||||
esp_err_t root_get_handler(httpd_req_t *req);
|
esp_err_t root_get_handler(httpd_req_t *req);
|
||||||
esp_err_t resource_filehandler(httpd_req_t *req);
|
esp_err_t resource_filehandler(httpd_req_t *req);
|
||||||
esp_err_t resource_filehandler(httpd_req_t *req);
|
esp_err_t resource_filehandler(httpd_req_t *req);
|
||||||
@@ -77,12 +89,20 @@ esp_err_t connect_delete_handler(httpd_req_t *req);
|
|||||||
esp_err_t reboot_ota_post_handler(httpd_req_t *req);
|
esp_err_t reboot_ota_post_handler(httpd_req_t *req);
|
||||||
esp_err_t reboot_post_handler(httpd_req_t *req);
|
esp_err_t reboot_post_handler(httpd_req_t *req);
|
||||||
esp_err_t recovery_post_handler(httpd_req_t *req);
|
esp_err_t recovery_post_handler(httpd_req_t *req);
|
||||||
esp_err_t status_post_handler(httpd_req_t *req);
|
esp_err_t status_get_handler(httpd_req_t *req);
|
||||||
esp_err_t ap_scan_handler(httpd_req_t *req);
|
esp_err_t ap_scan_handler(httpd_req_t *req);
|
||||||
|
esp_err_t redirect_ev_handler(httpd_req_t *req);
|
||||||
|
esp_err_t redirect_200_ev_handler(httpd_req_t *req);
|
||||||
|
|
||||||
|
|
||||||
esp_err_t err_handler(httpd_req_t *req, httpd_err_code_t error);
|
esp_err_t err_handler(httpd_req_t *req, httpd_err_code_t error);
|
||||||
|
#define SCRATCH_BUFSIZE (10240)
|
||||||
|
#define FILE_PATH_MAX (ESP_VFS_PATH_MAX + 128)
|
||||||
|
|
||||||
|
typedef struct rest_server_context {
|
||||||
|
char base_path[ESP_VFS_PATH_MAX + 1];
|
||||||
|
char scratch[SCRATCH_BUFSIZE];
|
||||||
|
} rest_server_context_t;
|
||||||
/**
|
/**
|
||||||
* @brief RTOS task for the HTTP server. Do not start manually.
|
* @brief RTOS task for the HTTP server. Do not start manually.
|
||||||
* @see void http_server_start()
|
* @see void http_server_start()
|
||||||
@@ -93,7 +113,7 @@ void CODE_RAM_LOCATION http_server(void *pvParameters);
|
|||||||
void CODE_RAM_LOCATION http_server_netconn_serve(struct netconn *conn);
|
void CODE_RAM_LOCATION http_server_netconn_serve(struct netconn *conn);
|
||||||
|
|
||||||
/* @brief create the task for the http server */
|
/* @brief create the task for the http server */
|
||||||
void CODE_RAM_LOCATION http_server_start();
|
esp_err_t CODE_RAM_LOCATION http_server_start();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief gets a char* pointer to the first occurence of header_name withing the complete http request request.
|
* @brief gets a char* pointer to the first occurence of header_name withing the complete http request request.
|
||||||
|
|||||||
@@ -5,19 +5,19 @@
|
|||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
|
||||||
<meta name="apple-mobile-web-app-capable" content="yes" />
|
<meta name="apple-mobile-web-app-capable" content="yes" />
|
||||||
<!-- TODO delete -->
|
<!-- TODO delete -->
|
||||||
<link rel="stylesheet" href="/test/bootstrap.min.css">
|
<link rel="stylesheet" href="/res/test/bootstrap.min.css">
|
||||||
<link rel="stylesheet" href="/bootstrap.css">
|
<link rel="stylesheet" href="/res/bootstrap.css">
|
||||||
<link rel="stylesheet" href="/style.css">
|
<link rel="stylesheet" href="/res/style.css">
|
||||||
<script src="/jquery.js"></script>
|
<script src="/res/jquery.js"></script>
|
||||||
<script src="/popper.js"></script>
|
<script src="/res/popper.js"></script>
|
||||||
<script src="/bootstrap.js"></script>
|
<script src="/res/bootstrap.js"></script>
|
||||||
|
|
||||||
<!-- TODO delete -->
|
<!-- TODO delete -->
|
||||||
<script src="/test/jquery.min.js"></script>
|
<script src="/res/test/jquery.min.js"></script>
|
||||||
<script src="/test/popper.min.js"></script>
|
<script src="/res/test/popper.min.js"></script>
|
||||||
<script src="/test/bootstrap.min.js"></script>
|
<script src="/res/test/bootstrap.min.js"></script>
|
||||||
|
|
||||||
<script src="/code.js"></script>
|
<script src="/res/code.js"></script>
|
||||||
|
|
||||||
|
|
||||||
<title>esp32-wifi-manager</title>
|
<title>esp32-wifi-manager</title>
|
||||||
|
|||||||
@@ -63,6 +63,7 @@ Contains the freeRTOS task and all necessary support
|
|||||||
#include "cmd_system.h"
|
#include "cmd_system.h"
|
||||||
#include "http_server_handlers.h"
|
#include "http_server_handlers.h"
|
||||||
|
|
||||||
|
|
||||||
#ifndef RECOVERY_APPLICATION
|
#ifndef RECOVERY_APPLICATION
|
||||||
#define RECOVERY_APPLICATION 0
|
#define RECOVERY_APPLICATION 0
|
||||||
#endif
|
#endif
|
||||||
@@ -509,7 +510,7 @@ char * get_mac_string(uint8_t mac[6]){
|
|||||||
|
|
||||||
char * macStr=malloc(LOCAL_MAC_SIZE);
|
char * macStr=malloc(LOCAL_MAC_SIZE);
|
||||||
memset(macStr, 0x00, LOCAL_MAC_SIZE);
|
memset(macStr, 0x00, LOCAL_MAC_SIZE);
|
||||||
snprintf(macStr, LOCAL_MAC_SIZE-1,MACSTR, MAC2STR(mac));
|
snprintf(macStr, LOCAL_MAC_SIZE,MACSTR, MAC2STR(mac));
|
||||||
return macStr;
|
return macStr;
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1093,7 +1094,7 @@ void wifi_manager( void * pvParameters ){
|
|||||||
BaseType_t xStatus;
|
BaseType_t xStatus;
|
||||||
EventBits_t uxBits;
|
EventBits_t uxBits;
|
||||||
uint8_t retries = 0;
|
uint8_t retries = 0;
|
||||||
esp_err_t err=ESP_OK;
|
|
||||||
/* start http server */
|
/* start http server */
|
||||||
http_server_start();
|
http_server_start();
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,7 @@
|
|||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
#define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
|
|
||||||
#include "http_server_handlers.h"
|
#include "http_server_handlers.h"
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
#include "esp_http_server.h"
|
#include "esp_http_server.h"
|
||||||
@@ -32,19 +32,8 @@
|
|||||||
|
|
||||||
static const char TAG[] = "http_server";
|
static const char TAG[] = "http_server";
|
||||||
|
|
||||||
static httpd_handle_t server = NULL;
|
static httpd_handle_t _server = NULL;
|
||||||
#define FILE_PATH_MAX (ESP_VFS_PATH_MAX + 128)
|
rest_server_context_t *rest_context = NULL;
|
||||||
#define SCRATCH_BUFSIZE (10240)
|
|
||||||
|
|
||||||
typedef struct rest_server_context {
|
|
||||||
char base_path[ESP_VFS_PATH_MAX + 1];
|
|
||||||
char scratch[SCRATCH_BUFSIZE];
|
|
||||||
} rest_server_context_t;
|
|
||||||
#define ESP_LOGE_LOC(t,str, ...) ESP_LOGE(t, "%s(%d): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__)
|
|
||||||
#define ESP_LOGI_LOC(t,str, ...) ESP_LOGI(t, "%s(%d): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__)
|
|
||||||
#define ESP_LOGD_LOC(t,str, ...) ESP_LOGD(t, "%s(%d): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void register_common_handlers(httpd_handle_t server){
|
void register_common_handlers(httpd_handle_t server){
|
||||||
httpd_uri_t res_get = { .uri = "/res/*", .method = HTTP_GET, .handler = resource_filehandler, .user_ctx = rest_context };
|
httpd_uri_t res_get = { .uri = "/res/*", .method = HTTP_GET, .handler = resource_filehandler, .user_ctx = rest_context };
|
||||||
@@ -80,43 +69,71 @@ void register_regular_handlers(httpd_handle_t server){
|
|||||||
|
|
||||||
httpd_uri_t connect_delete = { .uri = "/connect.json", .method = HTTP_DELETE, .handler = connect_delete_handler, .user_ctx = rest_context };
|
httpd_uri_t connect_delete = { .uri = "/connect.json", .method = HTTP_DELETE, .handler = connect_delete_handler, .user_ctx = rest_context };
|
||||||
httpd_register_uri_handler(server, &connect_delete);
|
httpd_register_uri_handler(server, &connect_delete);
|
||||||
|
|
||||||
|
|
||||||
|
// from https://github.com/tripflex/wifi-captive-portal/blob/master/src/mgos_wifi_captive_portal.c
|
||||||
|
// https://unix.stackexchange.com/questions/432190/why-isnt-androids-captive-portal-detection-triggering-a-browser-window
|
||||||
|
// Known HTTP GET requests to check for Captive Portal
|
||||||
|
|
||||||
|
///kindle-wifi/wifiredirect.html Kindle when requested with com.android.captiveportallogin
|
||||||
|
///kindle-wifi/wifistub.html Kindle before requesting with captive portal login window (maybe for detection?)
|
||||||
|
|
||||||
|
|
||||||
|
httpd_uri_t connect_redirect_1 = { .uri = "/mobile/status.php", .method = HTTP_GET, .handler = redirect_200_ev_handler, .user_ctx = rest_context };// Android 8.0 (Samsung s9+)
|
||||||
|
httpd_register_uri_handler(server, &connect_redirect_1);
|
||||||
|
httpd_uri_t connect_redirect_2 = { .uri = "/generate_204", .method = HTTP_GET, .handler = redirect_200_ev_handler, .user_ctx = rest_context };// Android
|
||||||
|
httpd_register_uri_handler(server, &connect_redirect_2);
|
||||||
|
httpd_uri_t connect_redirect_3 = { .uri = "/gen_204", .method = HTTP_GET, .handler = redirect_ev_handler, .user_ctx = rest_context };// Android 9.0
|
||||||
|
httpd_register_uri_handler(server, &connect_redirect_3);
|
||||||
|
httpd_uri_t connect_redirect_4 = { .uri = "/ncsi.txt", .method = HTTP_GET, .handler = redirect_ev_handler, .user_ctx = rest_context };// Windows
|
||||||
|
httpd_register_uri_handler(server, &connect_redirect_4);
|
||||||
|
httpd_uri_t connect_redirect_5 = { .uri = "/hotspot-detect.html", .method = HTTP_GET, .handler = redirect_ev_handler, .user_ctx = rest_context }; // iOS 8/9
|
||||||
|
httpd_register_uri_handler(server, &connect_redirect_5);
|
||||||
|
httpd_uri_t connect_redirect_6 = { .uri = "/library/test/success.html", .method = HTTP_GET, .handler = redirect_ev_handler, .user_ctx = rest_context };// iOS 8/9
|
||||||
|
httpd_register_uri_handler(server, &connect_redirect_6);
|
||||||
|
httpd_uri_t connect_redirect_7 = { .uri = "/hotspotdetect.html", .method = HTTP_GET, .handler = redirect_ev_handler, .user_ctx = rest_context }; // iOS
|
||||||
|
httpd_register_uri_handler(server, &connect_redirect_7);
|
||||||
|
httpd_uri_t connect_redirect_8 = { .uri = "/success.txt", .method = HTTP_GET, .handler = redirect_ev_handler, .user_ctx = rest_context }; // OSX
|
||||||
|
httpd_register_uri_handler(server, &connect_redirect_8);
|
||||||
|
|
||||||
|
|
||||||
|
ESP_LOGD(TAG,"Registering default error handler for 404");
|
||||||
|
httpd_register_err_handler(server, HTTPD_404_NOT_FOUND,&err_handler);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t http_server_start()
|
esp_err_t http_server_start()
|
||||||
{
|
{
|
||||||
ESP_LOGI(REST_TAG, "Initializing HTTP Server");
|
ESP_LOGI(TAG, "Initializing HTTP Server");
|
||||||
rest_server_context_t *rest_context = calloc(1, sizeof(rest_server_context_t));
|
rest_context = calloc(1, sizeof(rest_server_context_t));
|
||||||
if(rest_context==NULL){
|
if(rest_context==NULL){
|
||||||
ESP_LOGE(TAG,"No memory for http context");
|
ESP_LOGE(TAG,"No memory for http context");
|
||||||
return ESP_FAIL;
|
return ESP_FAIL;
|
||||||
}
|
}
|
||||||
strlcpy(rest_context->base_path, "/res/", sizeof(rest_context->base_path));
|
strlcpy(rest_context->base_path, "/res/", sizeof(rest_context->base_path));
|
||||||
|
|
||||||
httpd_handle_t server = NULL;
|
|
||||||
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
|
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
|
||||||
|
config.max_uri_handlers = 20;
|
||||||
|
config.max_open_sockets = 8;
|
||||||
config.uri_match_fn = httpd_uri_match_wildcard;
|
config.uri_match_fn = httpd_uri_match_wildcard;
|
||||||
//todo: use this to configure session token?
|
//todo: use the endpoint below to configure session token?
|
||||||
// config.open_fn
|
// config.open_fn
|
||||||
|
|
||||||
ESP_LOGI(REST_TAG, "Starting HTTP Server");
|
ESP_LOGI(TAG, "Starting HTTP Server");
|
||||||
esp_err_t err= httpd_start(&server, &config);
|
esp_err_t err= httpd_start(&_server, &config);
|
||||||
if(err != ESP_OK){
|
if(err != ESP_OK){
|
||||||
ESP_LOGE_LOC(TAG,"Start server failed");
|
ESP_LOGE_LOC(TAG,"Start server failed");
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
||||||
register_common_handlers(server);
|
register_common_handlers(_server);
|
||||||
register_regular_handlers(server);
|
register_regular_handlers(_server);
|
||||||
}
|
}
|
||||||
register_err_handler(server, HTTPD_404_NOT_FOUND,&err_handler);
|
|
||||||
|
|
||||||
|
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* Function to free context */
|
/* Function to free context */
|
||||||
void adder_free_func(void *ctx)
|
void adder_free_func(void *ctx)
|
||||||
{
|
{
|
||||||
@@ -124,135 +141,6 @@ void adder_free_func(void *ctx)
|
|||||||
free(ctx);
|
free(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* This handler keeps accumulating data that is posted to it into a per
|
|
||||||
* socket/session context. And returns the result.
|
|
||||||
*/
|
|
||||||
esp_err_t adder_post_handler(httpd_req_t *req)
|
|
||||||
{
|
|
||||||
/* Log total visitors */
|
|
||||||
unsigned *visitors = (unsigned *)req->user_ctx;
|
|
||||||
ESP_LOGI(TAG, "/adder visitor count = %d", ++(*visitors));
|
|
||||||
|
|
||||||
char buf[10];
|
|
||||||
char outbuf[50];
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
/* Read data received in the request */
|
|
||||||
ret = httpd_req_recv(req, buf, sizeof(buf));
|
|
||||||
if (ret <= 0) {
|
|
||||||
if (ret == HTTPD_SOCK_ERR_TIMEOUT) {
|
|
||||||
httpd_resp_send_408(req);
|
|
||||||
}
|
|
||||||
return ESP_FAIL;
|
|
||||||
}
|
|
||||||
|
|
||||||
buf[ret] = '\0';
|
|
||||||
int val = atoi(buf);
|
|
||||||
ESP_LOGI(TAG, "/adder handler read %d", val);
|
|
||||||
|
|
||||||
/* Create session's context if not already available */
|
|
||||||
if (! req->sess_ctx) {
|
|
||||||
ESP_LOGI(TAG, "/adder allocating new session");
|
|
||||||
req->sess_ctx = malloc(sizeof(int));
|
|
||||||
req->free_ctx = adder_free_func;
|
|
||||||
*(int *)req->sess_ctx = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Add the received data to the context */
|
|
||||||
int *adder = (int *)req->sess_ctx;
|
|
||||||
*adder += val;
|
|
||||||
|
|
||||||
/* Respond with the accumulated value */
|
|
||||||
snprintf(outbuf, sizeof(outbuf),"%d", *adder);
|
|
||||||
httpd_resp_send(req, outbuf, strlen(outbuf));
|
|
||||||
return ESP_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* This handler gets the present value of the accumulator */
|
|
||||||
esp_err_t adder_get_handler(httpd_req_t *req)
|
|
||||||
{
|
|
||||||
/* Log total visitors */
|
|
||||||
unsigned *visitors = (unsigned *)req->user_ctx;
|
|
||||||
ESP_LOGI(TAG, "/adder visitor count = %d", ++(*visitors));
|
|
||||||
|
|
||||||
char outbuf[50];
|
|
||||||
|
|
||||||
/* Create session's context if not already available */
|
|
||||||
if (! req->sess_ctx) {
|
|
||||||
ESP_LOGI(TAG, "/adder GET allocating new session");
|
|
||||||
req->sess_ctx = malloc(sizeof(int));
|
|
||||||
req->free_ctx = adder_free_func;
|
|
||||||
*(int *)req->sess_ctx = 0;
|
|
||||||
}
|
|
||||||
ESP_LOGI(TAG, "/adder GET handler send %d", *(int *)req->sess_ctx);
|
|
||||||
|
|
||||||
/* Respond with the accumulated value */
|
|
||||||
snprintf(outbuf, sizeof(outbuf),"%d", *((int *)req->sess_ctx));
|
|
||||||
httpd_resp_send(req, outbuf, strlen(outbuf));
|
|
||||||
return ESP_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* This handler resets the value of the accumulator */
|
|
||||||
esp_err_t adder_put_handler(httpd_req_t *req)
|
|
||||||
{
|
|
||||||
/* Log total visitors */
|
|
||||||
unsigned *visitors = (unsigned *)req->user_ctx;
|
|
||||||
ESP_LOGI(TAG, "/adder visitor count = %d", ++(*visitors));
|
|
||||||
|
|
||||||
char buf[10];
|
|
||||||
char outbuf[50];
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
/* Read data received in the request */
|
|
||||||
ret = httpd_req_recv(req, buf, sizeof(buf));
|
|
||||||
if (ret <= 0) {
|
|
||||||
if (ret == HTTPD_SOCK_ERR_TIMEOUT) {
|
|
||||||
httpd_resp_send_408(req);
|
|
||||||
}
|
|
||||||
return ESP_FAIL;
|
|
||||||
}
|
|
||||||
|
|
||||||
buf[ret] = '\0';
|
|
||||||
int val = atoi(buf);
|
|
||||||
ESP_LOGI(TAG, "/adder PUT handler read %d", val);
|
|
||||||
|
|
||||||
/* Create session's context if not already available */
|
|
||||||
if (! req->sess_ctx) {
|
|
||||||
ESP_LOGI(TAG, "/adder PUT allocating new session");
|
|
||||||
req->sess_ctx = malloc(sizeof(int));
|
|
||||||
req->free_ctx = adder_free_func;
|
|
||||||
}
|
|
||||||
*(int *)req->sess_ctx = val;
|
|
||||||
|
|
||||||
/* Respond with the reset value */
|
|
||||||
snprintf(outbuf, sizeof(outbuf),"%d", *((int *)req->sess_ctx));
|
|
||||||
httpd_resp_send(req, outbuf, strlen(outbuf));
|
|
||||||
return ESP_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Maintain a variable which stores the number of times
|
|
||||||
* the "/adder" URI has been visited */
|
|
||||||
static unsigned visitors = 0;
|
|
||||||
|
|
||||||
|
|
||||||
httpd_handle_t start_webserver(void)
|
|
||||||
{
|
|
||||||
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
|
|
||||||
// Start the httpd server
|
|
||||||
ESP_LOGI(TAG, "Starting server on port: '%d'", config.server_port);
|
|
||||||
|
|
||||||
if (httpd_start(&server, &config) == ESP_OK) {
|
|
||||||
// Set URI handlers
|
|
||||||
ESP_LOGI(TAG, "Registering URI handlers");
|
|
||||||
httpd_register_uri_handler(server, &adder_get);
|
|
||||||
httpd_register_uri_handler(server, &adder_put);
|
|
||||||
httpd_register_uri_handler(server, &adder_post);
|
|
||||||
return server;
|
|
||||||
}
|
|
||||||
|
|
||||||
ESP_LOGI(TAG, "Error starting server!");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
void stop_webserver(httpd_handle_t server)
|
void stop_webserver(httpd_handle_t server)
|
||||||
{
|
{
|
||||||
@@ -264,7 +152,7 @@ void stop_webserver(httpd_handle_t server)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
|
||||||
if(strstr(line, "GET / ")) {
|
if(strstr(line, "GET / ")) {
|
||||||
netconn_write(conn, http_html_hdr, sizeof(http_html_hdr) - 1, NETCONN_NOCOPY);
|
netconn_write(conn, http_html_hdr, sizeof(http_html_hdr) - 1, NETCONN_NOCOPY);
|
||||||
@@ -481,3 +369,4 @@ netconn_write(conn, http_404_hdr, sizeof(http_404_hdr) - 1, NETCONN_NOCOPY);
|
|||||||
free(host);
|
free(host);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|||||||
@@ -70,30 +70,30 @@ void config_set_entry_changed_flag(cJSON * entry, cJSON_bool flag);
|
|||||||
void * pval = config_alloc_get(nt, key);\
|
void * pval = config_alloc_get(nt, key);\
|
||||||
if(pval!=NULL){ *value = *(t * )pval; free(pval); return ESP_OK; }\
|
if(pval!=NULL){ *value = *(t * )pval; free(pval); return ESP_OK; }\
|
||||||
return ESP_FAIL;}
|
return ESP_FAIL;}
|
||||||
#ifdef RECOVERY_APPLICATION
|
#ifndef RECOVERY_APPLICATION
|
||||||
static void * malloc_fn(size_t sz){
|
static void * malloc_fn(size_t sz){
|
||||||
|
|
||||||
void * ptr = heap_caps_malloc(sz, MALLOC_CAP_SPIRAM);
|
void * ptr = heap_caps_malloc(sz, MALLOC_CAP_SPIRAM);
|
||||||
if(ptr==NULL){
|
if(ptr==NULL){
|
||||||
ESP_LOGE(TAG,"malloc_fn: unable to allocate memory!");
|
ESP_LOGE(TAG,"malloc_fn: unable to allocate memory!");
|
||||||
}
|
}
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
static void * free_fn(void * ptr){
|
//static void * free_fn(void * ptr){
|
||||||
if(ptr!=NULL){
|
// if(ptr!=NULL){
|
||||||
free(ptr);
|
// free(ptr);
|
||||||
}
|
// }
|
||||||
else {
|
// else {
|
||||||
ESP_LOGW(TAG,"free_fn: Cannot free null pointer!");
|
// ESP_LOGW(TAG,"free_fn: Cannot free null pointer!");
|
||||||
}
|
// }
|
||||||
return NULL;
|
// return NULL;
|
||||||
}
|
//}
|
||||||
#endif
|
#endif
|
||||||
void init_cJSON(){
|
void init_cJSON(){
|
||||||
|
|
||||||
|
#ifndef RECOVERY_APPLICATION
|
||||||
static cJSON_Hooks hooks;
|
static cJSON_Hooks hooks;
|
||||||
// initialize cJSON hooks it uses SPIRAM memory
|
// initialize cJSON hooks it uses SPIRAM memory
|
||||||
// as opposed to IRAM
|
// as opposed to IRAM
|
||||||
#ifndef RECOVERY_APPLICATION
|
|
||||||
// In squeezelite mode, allocate memory from PSRAM. Otherwise allocate from internal RAM
|
// In squeezelite mode, allocate memory from PSRAM. Otherwise allocate from internal RAM
|
||||||
// as recovery will lock flash access when erasing FLASH or writing to OTA partition.
|
// as recovery will lock flash access when erasing FLASH or writing to OTA partition.
|
||||||
hooks.malloc_fn=&malloc_fn;
|
hooks.malloc_fn=&malloc_fn;
|
||||||
@@ -320,7 +320,7 @@ void * config_safe_alloc_get_entry_value(nvs_type_t nvs_type, cJSON * entry){
|
|||||||
}
|
}
|
||||||
|
|
||||||
nvs_type_t type = config_get_entry_type(entry);
|
nvs_type_t type = config_get_entry_type(entry);
|
||||||
if(nvs_type != type){
|
if (nvs_type != type){
|
||||||
// requested value type different than the stored type
|
// requested value type different than the stored type
|
||||||
char * entry_str = cJSON_PrintUnformatted(entry);
|
char * entry_str = cJSON_PrintUnformatted(entry);
|
||||||
if(entry_str!=NULL){
|
if(entry_str!=NULL){
|
||||||
@@ -704,31 +704,6 @@ esp_err_t config_set_value(nvs_type_t nvs_type, const char *key, void * value){
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t config_set_value(nvs_type_t nvs_type, const char *key, void * value){
|
|
||||||
esp_err_t result = ESP_OK;
|
|
||||||
if(!config_lock(LOCK_MAX_WAIT/portTICK_PERIOD_MS)){
|
|
||||||
ESP_LOGE(TAG, "Unable to lock config after %d ms",LOCK_MAX_WAIT);
|
|
||||||
result = ESP_FAIL;
|
|
||||||
}
|
|
||||||
cJSON * entry = config_set_value_safe(nvs_type, key, value);
|
|
||||||
if(entry == NULL){
|
|
||||||
result = ESP_FAIL;
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
char * entry_str = cJSON_PrintUnformatted(entry);
|
|
||||||
if(entry_str!=NULL){
|
|
||||||
ESP_LOGV(TAG,"config_set_value result: \n%s",entry_str);
|
|
||||||
free(entry_str);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
ESP_LOGV(TAG,"config_set_value completed");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
config_unlock();
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
IMPLEMENT_SET_DEFAULT(uint8_t,NVS_TYPE_U8);
|
IMPLEMENT_SET_DEFAULT(uint8_t,NVS_TYPE_U8);
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "esp_system.h"
|
#include "esp_system.h"
|
||||||
#include "nvs_utilities.h"
|
#include "nvs_utilities.h"
|
||||||
|
#include "cJSON.h"
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
|||||||
Reference in New Issue
Block a user