mirror of
https://github.com/sle118/squeezelite-esp32.git
synced 2025-12-07 12:07:09 +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")
|
||||
set(COMPONENT_ADD_INCLUDEDIRS ".")
|
||||
set(COMPONENT_REQUIRES console spi_flash)
|
||||
idf_component_register(SRCS "cmd_i2ctools.c"
|
||||
INCLUDE_DIRS "." "../tools/"
|
||||
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.
|
||||
#
|
||||
CFLAGS += -D LOG_LOCAL_LEVEL=ESP_LOG_DEBUG \
|
||||
-I$(COMPONENT_PATH)/../tools
|
||||
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 recovery = false;
|
||||
@@ -189,16 +202,25 @@ $(document).ready(function(){
|
||||
$("input#autoexec-cb").on("click", function() {
|
||||
var data = { 'timestamp': Date.now() };
|
||||
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');
|
||||
$.ajax({
|
||||
url: '/config.json',
|
||||
dataType: 'text',
|
||||
method: 'POST',
|
||||
cache: false,
|
||||
headers: { "X-Custom-autoexec": autoexec },
|
||||
// headers: { "X-Custom-autoexec": autoexec },
|
||||
contentType: 'application/json; charset=utf-8',
|
||||
data: JSON.stringify(data),
|
||||
data: JSON.stringify(data),
|
||||
|
||||
error: function (xhr, ajaxOptions, thrownError) {
|
||||
console.log(xhr.status);
|
||||
console.log(thrownError);
|
||||
@@ -232,14 +254,20 @@ $(document).ready(function(){
|
||||
$("input#save-autoexec1").on("click", function() {
|
||||
var data = { 'timestamp': Date.now() };
|
||||
autoexec1 = $("#autoexec1").val();
|
||||
data['autoexec1'] = autoexec1;
|
||||
data['config'] = {};
|
||||
data['config'] = {
|
||||
autoexec1 : {
|
||||
value : autoexec1,
|
||||
type : 33
|
||||
}
|
||||
}
|
||||
|
||||
$.ajax({
|
||||
url: '/config.json',
|
||||
dataType: 'text',
|
||||
method: 'POST',
|
||||
cache: false,
|
||||
headers: { "X-Custom-autoexec1": autoexec1 },
|
||||
// headers: { "X-Custom-autoexec1": autoexec1 },
|
||||
contentType: 'application/json; charset=utf-8',
|
||||
data: JSON.stringify(data),
|
||||
error: function (xhr, ajaxOptions, thrownError) {
|
||||
@@ -254,15 +282,19 @@ $(document).ready(function(){
|
||||
|
||||
$("input#save-gpio").on("click", function() {
|
||||
var data = { 'timestamp': Date.now() };
|
||||
var config = {};
|
||||
|
||||
var headers = {};
|
||||
$("input.gpio").each(function() {
|
||||
var id = $(this)[0].id;
|
||||
var pin = $(this).val();
|
||||
if (pin != '') {
|
||||
headers["X-Custom-"+id] = pin;
|
||||
data[id] = pin;
|
||||
config[id] = {};
|
||||
config[id].value = pin;
|
||||
config[id].type = nvs_type_t.NVS_TYPE_STR;
|
||||
}
|
||||
});
|
||||
data['config'] = config;
|
||||
$.ajax({
|
||||
url: '/config.json',
|
||||
dataType: 'text',
|
||||
@@ -284,23 +316,40 @@ $(document).ready(function(){
|
||||
$("#save-nvs").on("click", function() {
|
||||
var headers = {};
|
||||
var data = { 'timestamp': Date.now() };
|
||||
var config = {};
|
||||
$("input.nvs").each(function() {
|
||||
var key = $(this)[0].id;
|
||||
var val = $(this).val();
|
||||
var nvs_type = parseInt($(this)[0].attributes.nvs_type.nodeValue,10);
|
||||
if (key != '') {
|
||||
headers["X-Custom-"+key] = val;
|
||||
data[key] = {};
|
||||
data[key].value = val;
|
||||
data[key].type = 33;
|
||||
config[key] = {};
|
||||
if(nvs_type == nvs_type_t.NVS_TYPE_U8
|
||||
|| nvs_type == nvs_type_t.NVS_TYPE_I8
|
||||
|| 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 val = $("#nvs-new-value").val();
|
||||
if (key != '') {
|
||||
headers["X-Custom-"+key] = val;
|
||||
data[key] = {};
|
||||
data[key].value = val;
|
||||
// headers["X-Custom-" +key] = val;
|
||||
config[key] = {};
|
||||
config[key].value = val;
|
||||
config[key].type = 33;
|
||||
}
|
||||
data['config'] = config;
|
||||
$.ajax({
|
||||
url: '/config.json',
|
||||
dataType: 'text',
|
||||
@@ -308,7 +357,7 @@ $(document).ready(function(){
|
||||
cache: false,
|
||||
headers: headers,
|
||||
contentType: 'application/json; charset=utf-8',
|
||||
data: JSON.stringify(data),
|
||||
data : JSON.stringify(data),
|
||||
error: function (xhr, ajaxOptions, thrownError) {
|
||||
console.log(xhr.status);
|
||||
console.log(thrownError);
|
||||
@@ -324,15 +373,21 @@ $(document).ready(function(){
|
||||
if (blockFlashButton) return;
|
||||
blockFlashButton = true;
|
||||
var url = $("#fwurl").val();
|
||||
data['fwurl'] = url;
|
||||
data['config'] = {
|
||||
fwurl : {
|
||||
value : url,
|
||||
type : 33
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
$.ajax({
|
||||
url: '/config.json',
|
||||
dataType: 'text',
|
||||
method: 'POST',
|
||||
cache: false,
|
||||
headers: { "X-Custom-fwurl": url },
|
||||
contentType: 'application/json; charset=utf-8',
|
||||
data: JSON.stringify(data),
|
||||
data: JSON.stringify(data),
|
||||
error: function (xhr, ajaxOptions, thrownError) {
|
||||
console.log(xhr.status);
|
||||
console.log(thrownError);
|
||||
@@ -509,9 +564,13 @@ function performConnect(conntype){
|
||||
dataType: 'text',
|
||||
method: 'POST',
|
||||
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',
|
||||
data: { 'timestamp': Date.now()},
|
||||
data: JSON.stringify({ 'timestamp': Date.now(),
|
||||
'ssid' : selectedSSID,
|
||||
'pwd' : pwd,
|
||||
'host_name' : dhcpname
|
||||
}),
|
||||
error: function (xhr, ajaxOptions, thrownError) {
|
||||
console.log(xhr.status);
|
||||
console.log(thrownError);
|
||||
@@ -737,7 +796,7 @@ function getConfig() {
|
||||
"<tr>"+
|
||||
"<td>"+key+"</td>"+
|
||||
"<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>"+
|
||||
"</tr>"
|
||||
);
|
||||
@@ -750,7 +809,7 @@ function getConfig() {
|
||||
"<input type='text' class='form-control' id='nvs-new-key' placeholder='new key'>"+
|
||||
"</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>"+
|
||||
"</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
|
||||
#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
|
||||
COMPONENT_ADD_INCLUDEDIRS := .
|
||||
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 <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include "esp_http_server.h"
|
||||
#include "wifi_manager.h"
|
||||
#include "freertos/FreeRTOS.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/tcp_priv.h"
|
||||
#include "lwip/priv/tcpip_priv.h"
|
||||
#include "esp_vfs.h"
|
||||
#define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#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 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_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 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);
|
||||
#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.
|
||||
* @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);
|
||||
|
||||
/* @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.
|
||||
|
||||
@@ -5,19 +5,19 @@
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
|
||||
<meta name="apple-mobile-web-app-capable" content="yes" />
|
||||
<!-- TODO delete -->
|
||||
<link rel="stylesheet" href="/test/bootstrap.min.css">
|
||||
<link rel="stylesheet" href="/bootstrap.css">
|
||||
<link rel="stylesheet" href="/style.css">
|
||||
<script src="/jquery.js"></script>
|
||||
<script src="/popper.js"></script>
|
||||
<script src="/bootstrap.js"></script>
|
||||
<link rel="stylesheet" href="/res/test/bootstrap.min.css">
|
||||
<link rel="stylesheet" href="/res/bootstrap.css">
|
||||
<link rel="stylesheet" href="/res/style.css">
|
||||
<script src="/res/jquery.js"></script>
|
||||
<script src="/res/popper.js"></script>
|
||||
<script src="/res/bootstrap.js"></script>
|
||||
|
||||
<!-- TODO delete -->
|
||||
<script src="/test/jquery.min.js"></script>
|
||||
<script src="/test/popper.min.js"></script>
|
||||
<script src="/test/bootstrap.min.js"></script>
|
||||
<script src="/res/test/jquery.min.js"></script>
|
||||
<script src="/res/test/popper.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>
|
||||
|
||||
@@ -63,6 +63,7 @@ Contains the freeRTOS task and all necessary support
|
||||
#include "cmd_system.h"
|
||||
#include "http_server_handlers.h"
|
||||
|
||||
|
||||
#ifndef RECOVERY_APPLICATION
|
||||
#define RECOVERY_APPLICATION 0
|
||||
#endif
|
||||
@@ -509,7 +510,7 @@ char * get_mac_string(uint8_t mac[6]){
|
||||
|
||||
char * macStr=malloc(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;
|
||||
|
||||
}
|
||||
@@ -1093,7 +1094,7 @@ void wifi_manager( void * pvParameters ){
|
||||
BaseType_t xStatus;
|
||||
EventBits_t uxBits;
|
||||
uint8_t retries = 0;
|
||||
esp_err_t err=ESP_OK;
|
||||
|
||||
/* start http server */
|
||||
http_server_start();
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
* 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 "esp_log.h"
|
||||
#include "esp_http_server.h"
|
||||
@@ -32,19 +32,8 @@
|
||||
|
||||
static const char TAG[] = "http_server";
|
||||
|
||||
static httpd_handle_t server = NULL;
|
||||
#define FILE_PATH_MAX (ESP_VFS_PATH_MAX + 128)
|
||||
#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__)
|
||||
|
||||
|
||||
static httpd_handle_t _server = NULL;
|
||||
rest_server_context_t *rest_context = NULL;
|
||||
|
||||
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 };
|
||||
@@ -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_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_LOGI(REST_TAG, "Initializing HTTP Server");
|
||||
rest_server_context_t *rest_context = calloc(1, sizeof(rest_server_context_t));
|
||||
ESP_LOGI(TAG, "Initializing HTTP Server");
|
||||
rest_context = calloc(1, sizeof(rest_server_context_t));
|
||||
if(rest_context==NULL){
|
||||
ESP_LOGE(TAG,"No memory for http context");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
strlcpy(rest_context->base_path, "/res/", sizeof(rest_context->base_path));
|
||||
|
||||
httpd_handle_t server = NULL;
|
||||
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;
|
||||
//todo: use this to configure session token?
|
||||
//todo: use the endpoint below to configure session token?
|
||||
// config.open_fn
|
||||
|
||||
ESP_LOGI(REST_TAG, "Starting HTTP Server");
|
||||
esp_err_t err= httpd_start(&server, &config);
|
||||
ESP_LOGI(TAG, "Starting HTTP Server");
|
||||
esp_err_t err= httpd_start(&_server, &config);
|
||||
if(err != ESP_OK){
|
||||
ESP_LOGE_LOC(TAG,"Start server failed");
|
||||
}
|
||||
else {
|
||||
|
||||
register_common_handlers(server);
|
||||
register_regular_handlers(server);
|
||||
register_common_handlers(_server);
|
||||
register_regular_handlers(_server);
|
||||
}
|
||||
register_err_handler(server, HTTPD_404_NOT_FOUND,&err_handler);
|
||||
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/* Function to free context */
|
||||
void adder_free_func(void *ctx)
|
||||
{
|
||||
@@ -124,135 +141,6 @@ void adder_free_func(void *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)
|
||||
{
|
||||
@@ -264,7 +152,7 @@ void stop_webserver(httpd_handle_t server)
|
||||
|
||||
|
||||
|
||||
|
||||
#if 0
|
||||
|
||||
if(strstr(line, "GET / ")) {
|
||||
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);
|
||||
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -70,30 +70,30 @@ void config_set_entry_changed_flag(cJSON * entry, cJSON_bool flag);
|
||||
void * pval = config_alloc_get(nt, key);\
|
||||
if(pval!=NULL){ *value = *(t * )pval; free(pval); return ESP_OK; }\
|
||||
return ESP_FAIL;}
|
||||
#ifdef RECOVERY_APPLICATION
|
||||
#ifndef RECOVERY_APPLICATION
|
||||
static void * malloc_fn(size_t sz){
|
||||
|
||||
void * ptr = heap_caps_malloc(sz, MALLOC_CAP_SPIRAM);
|
||||
if(ptr==NULL){
|
||||
ESP_LOGE(TAG,"malloc_fn: unable to allocate memory!");
|
||||
}
|
||||
return ptr;
|
||||
}
|
||||
static void * free_fn(void * ptr){
|
||||
if(ptr!=NULL){
|
||||
free(ptr);
|
||||
}
|
||||
else {
|
||||
ESP_LOGW(TAG,"free_fn: Cannot free null pointer!");
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
//static void * free_fn(void * ptr){
|
||||
// if(ptr!=NULL){
|
||||
// free(ptr);
|
||||
// }
|
||||
// else {
|
||||
// ESP_LOGW(TAG,"free_fn: Cannot free null pointer!");
|
||||
// }
|
||||
// return NULL;
|
||||
//}
|
||||
#endif
|
||||
void init_cJSON(){
|
||||
|
||||
#ifndef RECOVERY_APPLICATION
|
||||
static cJSON_Hooks hooks;
|
||||
// initialize cJSON hooks it uses SPIRAM memory
|
||||
// as opposed to IRAM
|
||||
#ifndef RECOVERY_APPLICATION
|
||||
// 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.
|
||||
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);
|
||||
if(nvs_type != type){
|
||||
if (nvs_type != type){
|
||||
// requested value type different than the stored type
|
||||
char * entry_str = cJSON_PrintUnformatted(entry);
|
||||
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;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <string.h>
|
||||
#include "esp_system.h"
|
||||
#include "nvs_utilities.h"
|
||||
#include "cJSON.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
|
||||
Reference in New Issue
Block a user