Compare commits
No commits in common. "f3df59b0fe0f0feaf01798d7e32b78f619a349a7" and "07de1352ab9822127cdfd41eadd58a54f05d7e1b" have entirely different histories.
f3df59b0fe
...
07de1352ab
3
.gitmodules
vendored
3
.gitmodules
vendored
@ -1,6 +1,3 @@
|
||||
[submodule "clock-pcb/vfd-lib"]
|
||||
path = clock-pcb/vfd-lib
|
||||
url = https://github.com/jh1995/KiCad-libs.git
|
||||
[submodule "firmware/components/etlcpp/etl"]
|
||||
path = firmware/components/etlcpp/etl
|
||||
url = https://github.com/ETLCPP/etl.git
|
||||
|
||||
@ -426,42 +426,4 @@
|
||||
)
|
||||
)
|
||||
)
|
||||
(symbol "mEZD7120xA-y" (in_bom yes) (on_board yes)
|
||||
(property "Reference" "U" (at 0 7.62 0)
|
||||
(effects (font (size 1.27 1.27)))
|
||||
)
|
||||
(property "Value" "mEZD7120xA-y" (at 0 5.08 0)
|
||||
(effects (font (size 1.27 1.27)))
|
||||
)
|
||||
(property "Footprint" "Connector_PinHeader_2.54mm:PinHeader_1x03_P2.54mm_Horizontal" (at -2.54 2.54 0)
|
||||
(effects (font (size 1.27 1.27)) hide)
|
||||
)
|
||||
(property "Datasheet" "https://www.monolithicpower.com/en/mezd71202a-g.html" (at 0 1.27 0)
|
||||
(effects (font (size 1.27 1.27)) hide)
|
||||
)
|
||||
(property "ki_keywords" "power converter" (at 0 0 0)
|
||||
(effects (font (size 1.27 1.27)) hide)
|
||||
)
|
||||
(property "ki_description" "Monolithic Power Systems 3-pin power converter." (at 0 0 0)
|
||||
(effects (font (size 1.27 1.27)) hide)
|
||||
)
|
||||
(symbol "mEZD7120xA-y_1_1"
|
||||
(rectangle (start -6.35 3.81) (end 6.35 -5.08)
|
||||
(stroke (width 0) (type default))
|
||||
(fill (type background))
|
||||
)
|
||||
(pin power_in line (at -8.89 1.27 0) (length 2.54)
|
||||
(name "VIN" (effects (font (size 1.27 1.27))))
|
||||
(number "1" (effects (font (size 1.27 1.27))))
|
||||
)
|
||||
(pin passive line (at 0 -7.62 90) (length 2.54)
|
||||
(name "GND" (effects (font (size 1.27 1.27))))
|
||||
(number "2" (effects (font (size 1.27 1.27))))
|
||||
)
|
||||
(pin power_out line (at 8.89 1.27 180) (length 2.54)
|
||||
(name "VOUT" (effects (font (size 1.27 1.27))))
|
||||
(number "3" (effects (font (size 1.27 1.27))))
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -1,67 +0,0 @@
|
||||
# Generated from CLion C/C++ Code Style settings
|
||||
BasedOnStyle: LLVM
|
||||
AccessModifierOffset: -2
|
||||
AlignAfterOpenBracket: Align
|
||||
#AlignConsecutiveAssignments: false
|
||||
AlignOperands: true
|
||||
AllowAllArgumentsOnNextLine: false
|
||||
AllowAllConstructorInitializersOnNextLine: false
|
||||
AllowAllParametersOfDeclarationOnNextLine: false
|
||||
AllowShortBlocksOnASingleLine: Always
|
||||
AllowShortCaseLabelsOnASingleLine: false
|
||||
AllowShortFunctionsOnASingleLine: All
|
||||
AllowShortIfStatementsOnASingleLine: Always
|
||||
AllowShortLambdasOnASingleLine: All
|
||||
AllowShortLoopsOnASingleLine: true
|
||||
AlwaysBreakAfterReturnType: None
|
||||
AlwaysBreakTemplateDeclarations: Yes
|
||||
BreakBeforeBraces: Custom
|
||||
BraceWrapping:
|
||||
AfterCaseLabel: true
|
||||
AfterClass: true
|
||||
AfterControlStatement: Always
|
||||
AfterEnum: true
|
||||
AfterFunction: true
|
||||
AfterNamespace: true
|
||||
AfterUnion: true
|
||||
AfterStruct: true
|
||||
BeforeCatch: true
|
||||
BeforeElse: true
|
||||
IndentBraces: false
|
||||
SplitEmptyFunction: false
|
||||
SplitEmptyRecord: true
|
||||
BreakBeforeBinaryOperators: None
|
||||
BreakBeforeTernaryOperators: true
|
||||
BreakConstructorInitializers: BeforeColon
|
||||
BreakInheritanceList: BeforeComma
|
||||
ColumnLimit: 0
|
||||
CompactNamespaces: false
|
||||
ContinuationIndentWidth: 8
|
||||
IndentCaseLabels: true
|
||||
IndentPPDirectives: None
|
||||
IndentWidth: 2
|
||||
KeepEmptyLinesAtTheStartOfBlocks: true
|
||||
MaxEmptyLinesToKeep: 2
|
||||
NamespaceIndentation: None
|
||||
ObjCSpaceAfterProperty: false
|
||||
ObjCSpaceBeforeProtocolList: true
|
||||
PointerAlignment: Left
|
||||
ReflowComments: false
|
||||
SpaceAfterCStyleCast: true
|
||||
SpaceAfterLogicalNot: false
|
||||
SpaceAfterTemplateKeyword: false
|
||||
SpaceBeforeAssignmentOperators: true
|
||||
SpaceBeforeCpp11BracedList: false
|
||||
SpaceBeforeCtorInitializerColon: true
|
||||
SpaceBeforeInheritanceColon: true
|
||||
SpaceBeforeParens: ControlStatements
|
||||
SpaceBeforeRangeBasedForLoopColon: true
|
||||
SpaceInEmptyParentheses: false
|
||||
SpacesBeforeTrailingComments: 0
|
||||
SpacesInAngles: false
|
||||
SpacesInCStyleCastParentheses: false
|
||||
SpacesInContainerLiterals: false
|
||||
SpacesInParentheses: false
|
||||
SpacesInSquareBrackets: false
|
||||
TabWidth: 2
|
||||
UseTab: Never
|
||||
10
firmware/.gitignore
vendored
10
firmware/.gitignore
vendored
@ -1,10 +0,0 @@
|
||||
build/
|
||||
sdkconfig
|
||||
sdkconfig.old
|
||||
|
||||
managed_components/
|
||||
|
||||
.vscode/
|
||||
.idea/
|
||||
|
||||
cmake-build-*/
|
||||
@ -1,6 +0,0 @@
|
||||
# The following lines of boilerplate have to be in your project's
|
||||
# CMakeLists in this exact order for cmake to work correctly
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(hello_world)
|
||||
@ -1 +0,0 @@
|
||||
idf_component_register(INCLUDE_DIRS "include" "etl/include")
|
||||
@ -1 +0,0 @@
|
||||
Subproject commit 0f1840a70d11a3d317afa55b8361829b93c8eeb8
|
||||
@ -1,21 +0,0 @@
|
||||
//
|
||||
// Created by erki on 13/01/24.
|
||||
//
|
||||
|
||||
#ifndef ETL_COMPILER_H
|
||||
#define ETL_COMPILER_H
|
||||
|
||||
#define ETL_COMPILER_GCC
|
||||
#define ETL_CPP23_SUPPORTED 1
|
||||
|
||||
#if !defined(CONFIG_OPTIMIZATION_ASSERTIONS_ENABLED)
|
||||
#define ETL_NO_CHECKS
|
||||
#endif
|
||||
|
||||
#if defined(CONFIG_CXX_EXCEPTIONS)
|
||||
#define ETL_THROW_EXCEPTIONS
|
||||
#endif
|
||||
|
||||
#include "etl/profiles/gcc_generic.h"
|
||||
|
||||
#endif // ETL_COMPILER_H
|
||||
@ -1,6 +0,0 @@
|
||||
idf_component_register(
|
||||
SRCS "main.cpp" "wifi_provisioner.cpp"
|
||||
INCLUDE_DIRS ""
|
||||
REQUIRES nvs_flash esp_wifi esp_http_server etlcpp json
|
||||
EMBED_FILES "${CMAKE_CURRENT_LIST_DIR}/static/index.html"
|
||||
)
|
||||
@ -1,23 +0,0 @@
|
||||
//
|
||||
// Created by erki on 16/01/24.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <expected>
|
||||
#include <esp_err.h>
|
||||
|
||||
#define TRY(x) ({ \
|
||||
const auto& _x = (x); \
|
||||
if (!_x) { \
|
||||
return std::unexpected(_x.error()); \
|
||||
} \
|
||||
_x.value(); \
|
||||
})
|
||||
|
||||
#define TRY_ESP(x) ({ \
|
||||
const esp_err_t& _x = (x); \
|
||||
if (_x != ESP_OK) \
|
||||
return std::unexpected(_x); \
|
||||
_x; \
|
||||
})
|
||||
@ -1,52 +0,0 @@
|
||||
#include "nvs_flash.h"
|
||||
#include "nvs.h"
|
||||
#include "nvs_handle.hpp"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
#include "sdkconfig.h"
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdio.h>
|
||||
#include <expected>
|
||||
|
||||
#include "wifi_provisioner.hpp"
|
||||
|
||||
extern "C" void app_main(void)
|
||||
{
|
||||
esp_err_t err = nvs_flash_init();
|
||||
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND)
|
||||
{
|
||||
// NVS partition was truncated and needs to be erased
|
||||
// Retry nvs_flash_init
|
||||
ESP_ERROR_CHECK(nvs_flash_erase());
|
||||
err = nvs_flash_init();
|
||||
}
|
||||
|
||||
ESP_ERROR_CHECK(err);
|
||||
|
||||
auto* provisioner = new WifiProvisioner("wifi_settings", [](const auto& params)
|
||||
{
|
||||
printf("Settings successfully done.");
|
||||
});
|
||||
if (!provisioner->addParameter("SSID", "ssid", WifiProvisioner::Parameter::Type::STRING).has_value())
|
||||
{
|
||||
printf("Error adding parameter.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!provisioner->addParameter("Password", "password", WifiProvisioner::Parameter::Type::STRING).has_value())
|
||||
{
|
||||
printf("Error adding parameter.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!provisioner->parametersAreConfigured())
|
||||
{
|
||||
auto result = provisioner->startProvisioning();
|
||||
if (!result.has_value())
|
||||
printf("Error: %s\n", esp_err_to_name(result.error()));
|
||||
else
|
||||
printf("Provisioning started...\n");
|
||||
}
|
||||
}
|
||||
@ -1,110 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<style>
|
||||
body {
|
||||
background-color: #ffffff;
|
||||
}
|
||||
</style>
|
||||
<title>ESP Captive Portal</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>ESP Captive Portal</h1>
|
||||
<p>Hello World, this is ESP32!</p>
|
||||
|
||||
<div id="interface">
|
||||
<form id="submit_form">
|
||||
</form>
|
||||
</div>
|
||||
<script>
|
||||
function outputSuccess(response_object) {
|
||||
let root = document.getElementById("interface");
|
||||
root.innerHTML = "";
|
||||
|
||||
root.innerHTML = `<b>${response_object.response_str}</b>`;
|
||||
}
|
||||
|
||||
function outputFailure(response_object) {
|
||||
let root = document.getElementById("interface");
|
||||
root.innerHTML = "";
|
||||
|
||||
root.innerHTML = `<p><b>Request failed.</b></br>Reason:${response_object.response_str}</p>`;
|
||||
}
|
||||
|
||||
function submitFormHandler(event) {
|
||||
console.log("In the post handler.");
|
||||
event.preventDefault();
|
||||
|
||||
const form_data = new FormData(document.getElementById("submit_form"));
|
||||
let configuration_data = [];
|
||||
for (const pair of form_data.entries()) {
|
||||
configuration_data.push({
|
||||
nvs_name: pair[0],
|
||||
value: pair[1]
|
||||
});
|
||||
}
|
||||
|
||||
console.log(`Posting data: ${JSON.stringify(configuration_data)}`);
|
||||
|
||||
fetch("configure", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body: JSON.stringify(configuration_data)
|
||||
}).then((r) => {
|
||||
console.log("Got a response.");
|
||||
console.log(r);
|
||||
|
||||
if (r.ok) {
|
||||
outputSuccess(r.json());
|
||||
} else {
|
||||
outputFailure(r.json());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async function getParameterFields() {
|
||||
const response = await fetch("parameters", {
|
||||
method: "GET",
|
||||
})
|
||||
|
||||
return response.json();
|
||||
}
|
||||
|
||||
async function populateSubmitForm(form_root) {
|
||||
const data = await getParameterFields();
|
||||
for (let param of data) {
|
||||
let input = document.createElement("input");
|
||||
input.setAttribute("id", param.nvs_name);
|
||||
input.setAttribute("name", param.nvs_name);
|
||||
|
||||
if (param.type === 0 || param.type === 1) {
|
||||
input.setAttribute("type", "number");
|
||||
} else {
|
||||
input.setAttribute("type", "text");
|
||||
}
|
||||
|
||||
let label = document.createElement("label");
|
||||
label.setAttribute("for", param.nvs_name);
|
||||
label.innerHTML = param.name + ":";
|
||||
|
||||
form_root.appendChild(label);
|
||||
form_root.appendChild(document.createElement("br"));
|
||||
form_root.appendChild(input);
|
||||
form_root.appendChild(document.createElement("br"));
|
||||
}
|
||||
|
||||
let submit = document.createElement("input");
|
||||
submit.setAttribute("type", "submit");
|
||||
submit.setAttribute("value", "Configure");
|
||||
|
||||
form_root.appendChild(submit);
|
||||
|
||||
form_root.addEventListener("submit", submitFormHandler, true);
|
||||
}
|
||||
|
||||
populateSubmitForm(document.getElementById("submit_form"));
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@ -1,461 +0,0 @@
|
||||
//
|
||||
// Created by erki on 07/01/24.
|
||||
//
|
||||
|
||||
#include "wifi_provisioner.hpp"
|
||||
|
||||
#include <cstring>
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_mac.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
#include <etl/string.h>
|
||||
#include <cJSON.h>
|
||||
|
||||
#include "esp_expected.hpp"
|
||||
|
||||
#define EXAMPLE_ESP_WIFI_SSID "ESP_TEST"
|
||||
#define EXAMPLE_ESP_WIFI_PASS "1234567891"
|
||||
#define EXAMPLE_ESP_WIFI_CHANNEL 5
|
||||
#define EXAMPLE_MAX_STA_CONN 5
|
||||
|
||||
extern const char index_start[] asm("_binary_index_html_start");
|
||||
extern const char index_end[] asm("_binary_index_html_end");
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
const char* TAG = "wifi softAP";
|
||||
const char* NVS_IS_INITED = "_is_inited";
|
||||
|
||||
ssize_t indexHtmlLength()
|
||||
{
|
||||
return std::distance(index_start, index_end);
|
||||
}
|
||||
|
||||
void wifiEventHandler_(void* arg, esp_event_base_t event_base,
|
||||
int32_t event_id, void* event_data)
|
||||
{
|
||||
if (event_id == WIFI_EVENT_AP_STACONNECTED) {
|
||||
wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*) event_data;
|
||||
ESP_LOGI(TAG, "station join, AID=%d",
|
||||
event->aid);
|
||||
} else if (event_id == WIFI_EVENT_AP_STADISCONNECTED) {
|
||||
wifi_event_ap_stadisconnected_t* event = (wifi_event_ap_stadisconnected_t*) event_data;
|
||||
ESP_LOGI(TAG, "station leave, AID=%d",
|
||||
event->aid);
|
||||
}
|
||||
}
|
||||
|
||||
esp_err_t rootGetHandler_(httpd_req_t* req)
|
||||
{
|
||||
httpd_resp_set_type(req, "text/html");
|
||||
httpd_resp_send(req, index_start, indexHtmlLength());
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t rootPostHandler_(httpd_req_t* req)
|
||||
{
|
||||
etl::string<1024> content;
|
||||
|
||||
content.initialize_free_space();
|
||||
const std::size_t size = MIN(req->content_len, content.max_size() - 1);
|
||||
|
||||
ESP_LOGI(TAG, "Post handler.");
|
||||
|
||||
if (const int ret = httpd_req_recv(req, content.data_end(), size);
|
||||
ret <= 0)
|
||||
{
|
||||
ESP_LOGE(TAG, "Error, socket timeout.");
|
||||
if (ret == HTTPD_SOCK_ERR_TIMEOUT)
|
||||
httpd_resp_send_408(req);
|
||||
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
content.trim_to_terminator();
|
||||
|
||||
auto dispatch_error = [req](const char* error_str)
|
||||
{
|
||||
ESP_LOGE(TAG, "Request error: %s", error_str);
|
||||
auto json_root = CJsonPtr(cJSON_CreateObject());
|
||||
cJSON_AddStringToObject(json_root.get(), "response_str", error_str);
|
||||
auto response_str = CJsonStrPtr(cJSON_Print(json_root.get()));
|
||||
|
||||
httpd_resp_set_status(req, "400 Bad Request");
|
||||
httpd_resp_set_type(req, HTTPD_TYPE_JSON);
|
||||
httpd_resp_sendstr(req, response_str.get());
|
||||
};
|
||||
|
||||
etl::string<100> content_type;
|
||||
if (const auto err = httpd_req_get_hdr_value_str(req, "Content-Type", content_type.data(), content_type.max_size() - 1);
|
||||
err != ESP_OK)
|
||||
{
|
||||
dispatch_error("Cannot parse header.");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
content_type.trim_to_terminator();
|
||||
|
||||
if (content_type != "application/json")
|
||||
{
|
||||
ESP_LOGE(TAG, "Invalid content-type header: %s", content_type.c_str());
|
||||
dispatch_error("Invalid Content-Type header.");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
auto json_object = CJsonPtr(cJSON_Parse(content.c_str()));
|
||||
if (!json_object || !cJSON_IsArray(json_object.get()))
|
||||
{
|
||||
dispatch_error("Invalid JSON sent.");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
auto* provisioner = static_cast<WifiProvisioner*>(req->user_ctx);
|
||||
std::expected<void, const char*> result = provisioner->tryConfigureParameters(std::move(json_object));
|
||||
|
||||
if (result)
|
||||
{
|
||||
httpd_resp_set_type(req, HTTPD_TYPE_JSON);
|
||||
|
||||
auto json_root = CJsonPtr(cJSON_CreateObject());
|
||||
cJSON_AddStringToObject(json_root.get(), "response_str", "Configuration successful. ESP rebooting.");
|
||||
auto response_str = CJsonStrPtr(cJSON_Print(json_root.get()));
|
||||
httpd_resp_sendstr(req, response_str.get());
|
||||
|
||||
ESP_LOGI(TAG, "Post successful.");
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
else
|
||||
{
|
||||
dispatch_error(result.error());
|
||||
return ESP_FAIL;
|
||||
}
|
||||
}
|
||||
|
||||
esp_err_t parametersGetHandler_(httpd_req_t* req)
|
||||
{
|
||||
auto* provisioner = static_cast<WifiProvisioner*>(req->user_ctx);
|
||||
CJsonPtr data = provisioner->getParameterObjects();
|
||||
cJSON* raw_data = data.get();
|
||||
|
||||
auto stringified = CJsonStrPtr(cJSON_Print(raw_data));
|
||||
|
||||
if (!stringified)
|
||||
{
|
||||
httpd_resp_send_500(req);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
httpd_resp_send(req, stringified.get(), HTTPD_RESP_USE_STRLEN);
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
std::expected<WifiProvisioner::Parameter::Value, esp_err_t> WifiProvisioner::Parameter::getValue(nvs::NVSHandle* file_handle) const
|
||||
{
|
||||
if (type == Type::INT)
|
||||
{
|
||||
std::int32_t value = 0;
|
||||
TRY_ESP(file_handle->get_item(nvs_name.c_str(), value));
|
||||
|
||||
return value;
|
||||
}
|
||||
else if (type == Type::FLOAT)
|
||||
{
|
||||
std::int32_t value_raw = 0;
|
||||
TRY_ESP(file_handle->get_item(nvs_name.c_str(), value_raw));
|
||||
return std::bit_cast<float>(value_raw);
|
||||
}
|
||||
else if (type == Type::STRING)
|
||||
{
|
||||
etl::string<100> value;
|
||||
value.initialize_free_space();
|
||||
|
||||
TRY_ESP(file_handle->get_string(nvs_name.c_str(), value.data(), value.size() - 1));
|
||||
value.trim_to_terminator();
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
std::expected<WifiProvisioner::Parameter::Value, const char*> WifiProvisioner::Parameter::tryValidateAndAssign(cJSON* object)
|
||||
{
|
||||
if (!cJSON_IsObject(object))
|
||||
return std::unexpected("Invalid parameter JSON.");
|
||||
|
||||
const cJSON* in_nvs_name = cJSON_GetObjectItem(object, "nvs_name");
|
||||
if (!cJSON_IsString(in_nvs_name))
|
||||
return std::unexpected("Invalid parameter JSON.");
|
||||
|
||||
if (cJSON_GetStringValue(in_nvs_name) != nvs_name)
|
||||
return std::unexpected("Mismatching parameter nvs_name. Invalid sequence or invalid parameter.");
|
||||
|
||||
const cJSON* in_value = cJSON_GetObjectItem(object, "value");
|
||||
if (type == Type::INT || type == Type::FLOAT)
|
||||
{
|
||||
if (!cJSON_IsNumber(in_value))
|
||||
return std::unexpected("Mismatching or missing parameter value.");
|
||||
|
||||
const double json_value = cJSON_GetNumberValue(in_value);
|
||||
if (type == Type::INT)
|
||||
return {data = Value{std::int32_t(json_value)}};
|
||||
else
|
||||
return {data = Value{float(json_value)}};
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!cJSON_IsString(in_value))
|
||||
return std::unexpected("Mismatching or missing parameter value.");
|
||||
|
||||
const char* json_value = cJSON_GetStringValue(in_value);
|
||||
return {data = Value{json_value}};
|
||||
}
|
||||
}
|
||||
|
||||
WifiProvisioner::WifiProvisioner(const char* settings_namespace, etl::delegate<void (const etl::vector<Parameter, 10>&)> success_cb)
|
||||
: success_cb_(success_cb)
|
||||
{
|
||||
esp_err_t err = ESP_OK;
|
||||
file_handle_ = nvs::open_nvs_handle(settings_namespace, NVS_READWRITE, &err);
|
||||
|
||||
ESP_ERROR_CHECK(err);
|
||||
|
||||
err = file_handle_->get_item(NVS_IS_INITED, settings_initialized_);
|
||||
|
||||
if (err != ESP_OK)
|
||||
{
|
||||
ESP_ERROR_CHECK(initializeNvsNamespace_());
|
||||
}
|
||||
}
|
||||
|
||||
WifiProvisioner::~WifiProvisioner()
|
||||
{
|
||||
if (http_server_)
|
||||
{
|
||||
httpd_stop(http_server_);
|
||||
}
|
||||
}
|
||||
|
||||
bool WifiProvisioner::parametersAreConfigured()
|
||||
{
|
||||
if (params_.empty())
|
||||
return false;
|
||||
|
||||
for (const auto& param : params_)
|
||||
{
|
||||
if (const auto value = param.getValue(file_handle_.get());
|
||||
!value.has_value())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
std::expected<void, esp_err_t> WifiProvisioner::startProvisioning()
|
||||
{
|
||||
TRY(initializeWifiAp_());
|
||||
|
||||
http_server_ = TRY(initializeCaptivePortal_());
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
std::expected<void, std::error_code> WifiProvisioner::addParameter(const char* name, const char* nvs_name, const Parameter::Type type)
|
||||
{
|
||||
if (params_.full())
|
||||
return std::unexpected(std::make_error_code(std::errc::not_enough_memory));
|
||||
|
||||
auto new_param = Parameter(name, nvs_name, type);
|
||||
params_.push_back(new_param);
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
CJsonPtr WifiProvisioner::getParameterObjects() const
|
||||
{
|
||||
auto root = CJsonPtr(cJSON_CreateArray());
|
||||
|
||||
for (const Parameter& param : params_)
|
||||
{
|
||||
cJSON* element = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(element, "name", param.name.c_str());
|
||||
cJSON_AddStringToObject(element, "nvs_name", param.nvs_name.c_str());
|
||||
cJSON_AddNumberToObject(element, "type", int(param.type));
|
||||
|
||||
cJSON_AddItemToArray(root.get(), element);
|
||||
}
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
std::expected<void, const char*> WifiProvisioner::tryConfigureParameters(CJsonPtr json_root)
|
||||
{
|
||||
if (params_.size() != cJSON_GetArraySize(json_root.get()))
|
||||
return std::unexpected("Mismatching parameters array sizes.");
|
||||
|
||||
cJSON* entry = nullptr;
|
||||
int i = 0;
|
||||
cJSON_ArrayForEach(entry, json_root)
|
||||
{
|
||||
auto& param = params_[i];
|
||||
TRY(param.tryValidateAndAssign(entry));
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
const auto& result = writeAndCommitParameters_();
|
||||
if (!result.has_value())
|
||||
return std::unexpected(esp_err_to_name(result.error()));
|
||||
|
||||
if (settings_initialized_ && success_cb_)
|
||||
success_cb_(params_);
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
esp_err_t WifiProvisioner::initializeNvsNamespace_()
|
||||
{
|
||||
esp_err_t err = file_handle_->set_item(NVS_IS_INITED, false);
|
||||
|
||||
if (err != ESP_OK)
|
||||
return err;
|
||||
|
||||
err = file_handle_->commit();
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
std::expected<void, esp_err_t> WifiProvisioner::initializeWifiAp_()
|
||||
{
|
||||
TRY_ESP(esp_netif_init());
|
||||
TRY_ESP(esp_event_loop_create_default());
|
||||
esp_netif_create_default_wifi_ap();
|
||||
|
||||
wifi_init_config_t config = WIFI_INIT_CONFIG_DEFAULT();
|
||||
TRY_ESP(esp_wifi_init(&config));
|
||||
|
||||
TRY_ESP(esp_event_handler_instance_register(
|
||||
WIFI_EVENT, ESP_EVENT_ANY_ID, &wifiEventHandler_, this, nullptr
|
||||
));
|
||||
|
||||
//https://github.com/espressif/esp-idf/blob/5524b692ee5d04d7a1000eb0c41640746fc67f3c/examples/wifi/getting_started/softAP/main/softap_example_main.c
|
||||
wifi_config_t wifi_config = {
|
||||
.ap = {
|
||||
.ssid = EXAMPLE_ESP_WIFI_SSID,
|
||||
.password = EXAMPLE_ESP_WIFI_PASS,
|
||||
.ssid_len = std::uint8_t(strlen(EXAMPLE_ESP_WIFI_SSID)),
|
||||
.channel = EXAMPLE_ESP_WIFI_CHANNEL,
|
||||
#ifdef CONFIG_ESP_WIFI_SOFTAP_SAE_SUPPORT
|
||||
.authmode = WIFI_AUTH_WPA3_PSK,
|
||||
#else /* CONFIG_ESP_WIFI_SOFTAP_SAE_SUPPORT */
|
||||
.authmode = WIFI_AUTH_WPA2_PSK,
|
||||
#endif
|
||||
.ssid_hidden = 0,
|
||||
.max_connection = EXAMPLE_MAX_STA_CONN,
|
||||
.pmf_cfg = {
|
||||
.required = true,
|
||||
},
|
||||
#ifdef CONFIG_ESP_WIFI_SOFTAP_SAE_SUPPORT
|
||||
.sae_pwe_h2e = WPA3_SAE_PWE_BOTH,
|
||||
#endif
|
||||
}
|
||||
};
|
||||
|
||||
if (strlen(EXAMPLE_ESP_WIFI_PASS) == 0)
|
||||
wifi_config.ap.authmode = WIFI_AUTH_OPEN;
|
||||
|
||||
TRY_ESP(esp_wifi_set_mode(WIFI_MODE_AP));
|
||||
TRY_ESP(esp_wifi_set_config(WIFI_IF_AP, &wifi_config));
|
||||
TRY_ESP(esp_wifi_start());
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
std::expected<httpd_handle_t, esp_err_t> WifiProvisioner::initializeCaptivePortal_()
|
||||
{
|
||||
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
|
||||
httpd_handle_t server = nullptr;
|
||||
|
||||
TRY_ESP(httpd_start(&server, &config));
|
||||
|
||||
static httpd_uri_t uri_main = {
|
||||
.uri = "/",
|
||||
.method = HTTP_GET,
|
||||
.handler = rootGetHandler_,
|
||||
.user_ctx = this
|
||||
};
|
||||
|
||||
static httpd_uri_t uri_post = {
|
||||
.uri = "/configure",
|
||||
.method = HTTP_POST,
|
||||
.handler = rootPostHandler_,
|
||||
.user_ctx = this
|
||||
};
|
||||
|
||||
static httpd_uri_t uri_get_params = {
|
||||
.uri = "/parameters",
|
||||
.method = HTTP_GET,
|
||||
.handler = parametersGetHandler_,
|
||||
.user_ctx = this
|
||||
};
|
||||
|
||||
if (const auto err = httpd_register_uri_handler(server, &uri_main);
|
||||
err != ESP_OK)
|
||||
{
|
||||
httpd_stop(server);
|
||||
return std::unexpected(err);
|
||||
}
|
||||
|
||||
if (const auto err = httpd_register_uri_handler(server, &uri_post);
|
||||
err != ESP_OK)
|
||||
{
|
||||
httpd_stop(server);
|
||||
return std::unexpected(err);
|
||||
}
|
||||
|
||||
if (const auto err = httpd_register_uri_handler(server, &uri_get_params);
|
||||
err != ESP_OK)
|
||||
{
|
||||
httpd_stop(server);
|
||||
return std::unexpected(err);
|
||||
}
|
||||
|
||||
return server;
|
||||
}
|
||||
|
||||
std::expected<void, esp_err_t> WifiProvisioner::writeAndCommitParameters_()
|
||||
{
|
||||
for (const auto& param : params_)
|
||||
{
|
||||
if (param.type == Parameter::Type::INT)
|
||||
{
|
||||
TRY_ESP(file_handle_->set_item(param.nvs_name.c_str(), std::get<std::int32_t>(param.data)));
|
||||
}
|
||||
else if (param.type == Parameter::Type::FLOAT)
|
||||
{
|
||||
const auto temporary = std::bit_cast<std::int32_t>(std::get<float>(param.data));
|
||||
TRY_ESP(file_handle_->set_item(param.nvs_name.c_str(), temporary));
|
||||
}
|
||||
else
|
||||
{
|
||||
const auto temporary = std::get<etl::string<100>>(param.data);
|
||||
TRY_ESP(file_handle_->set_string(param.nvs_name.c_str(), temporary.c_str()));
|
||||
}
|
||||
}
|
||||
|
||||
TRY_ESP(file_handle_->set_item(NVS_IS_INITED, true));
|
||||
TRY_ESP(file_handle_->commit());
|
||||
|
||||
settings_initialized_ = true;
|
||||
|
||||
return {};
|
||||
}
|
||||
@ -1,99 +0,0 @@
|
||||
//
|
||||
// Created by erki on 07/01/24.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "nvs_handle.hpp"
|
||||
#include <esp_http_server.h>
|
||||
#include <etl/vector.h>
|
||||
#include <etl/string.h>
|
||||
#include <etl/delegate.h>
|
||||
#include <expected>
|
||||
#include <variant>
|
||||
#include <memory>
|
||||
|
||||
#include <cJSON.h>
|
||||
|
||||
struct CJsonStringDeleter
|
||||
{
|
||||
void operator()(char* json_str) const
|
||||
{
|
||||
if (json_str)
|
||||
cJSON_free(json_str);
|
||||
}
|
||||
};
|
||||
|
||||
using CJsonStrPtr = std::unique_ptr<char, CJsonStringDeleter>;
|
||||
|
||||
struct CJsonDeleter
|
||||
{
|
||||
void operator()(cJSON* element) const
|
||||
{
|
||||
if (element)
|
||||
cJSON_Delete(element);
|
||||
}
|
||||
};
|
||||
|
||||
using CJsonPtr = std::unique_ptr<cJSON, CJsonDeleter>;
|
||||
|
||||
class WifiProvisioner
|
||||
{
|
||||
public:
|
||||
struct Parameter
|
||||
{
|
||||
etl::string<100> name;
|
||||
etl::string<15> nvs_name;
|
||||
enum class Type
|
||||
{
|
||||
INT,
|
||||
FLOAT,
|
||||
STRING
|
||||
};
|
||||
|
||||
Type type;
|
||||
|
||||
using Value = std::variant<std::int32_t, float, etl::string<100>>;
|
||||
Value data;
|
||||
|
||||
Parameter() = default;
|
||||
Parameter(const char* name, const char* nvs_name, const Parameter::Type type)
|
||||
: name(name)
|
||||
, nvs_name(nvs_name)
|
||||
, type(type)
|
||||
{ }
|
||||
|
||||
std::expected<Value, esp_err_t> getValue(nvs::NVSHandle* file_handle) const;
|
||||
std::expected<Value, const char*> tryValidateAndAssign(cJSON* object);
|
||||
};
|
||||
|
||||
explicit WifiProvisioner(const char* settings_namespace, etl::delegate<void (const etl::vector<Parameter, 10>&)> success_cb);
|
||||
~WifiProvisioner();
|
||||
|
||||
WifiProvisioner() = delete;
|
||||
WifiProvisioner(const WifiProvisioner&) = delete;
|
||||
WifiProvisioner(WifiProvisioner&&) = delete;
|
||||
WifiProvisioner& operator=(const WifiProvisioner&) = delete;
|
||||
WifiProvisioner& operator=(WifiProvisioner&&) = delete;
|
||||
|
||||
bool parametersAreConfigured();
|
||||
|
||||
std::expected<void, esp_err_t> startProvisioning();
|
||||
|
||||
std::expected<void, std::error_code> addParameter(const char* name, const char* nvs_name, const Parameter::Type type);
|
||||
CJsonPtr getParameterObjects() const;
|
||||
|
||||
std::expected<void, const char*> tryConfigureParameters(CJsonPtr json_root);
|
||||
|
||||
private:
|
||||
std::unique_ptr<nvs::NVSHandle> file_handle_;
|
||||
bool settings_initialized_ = false;
|
||||
httpd_handle_t http_server_ = nullptr;
|
||||
etl::vector<Parameter, 10> params_;
|
||||
etl::delegate<void (const etl::vector<Parameter, 10>& params)> success_cb_;
|
||||
|
||||
esp_err_t initializeNvsNamespace_();
|
||||
std::expected<void, esp_err_t> initializeWifiAp_();
|
||||
std::expected<httpd_handle_t, esp_err_t> initializeCaptivePortal_();
|
||||
std::expected<void, esp_err_t> writeAndCommitParameters_();
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user