Refactor: Apply automated formatting, optimize includes, and consolidate code styles across components.

Signed-off-by: YoungSoo Shin <shinys000114@gmail.com>
This commit is contained in:
2025-09-01 12:29:54 +09:00
parent f5312dab17
commit b642c8baa7
25 changed files with 283 additions and 263 deletions

55
.clang-format Normal file
View File

@@ -0,0 +1,55 @@
# Generated from CLion C/C++ Code Style settings
---
Language: Cpp
BasedOnStyle: LLVM
AccessModifierOffset: -4
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignOperands: false
AlignTrailingComments: false
AlwaysBreakTemplateDeclarations: Yes
BraceWrapping:
AfterCaseLabel: true
AfterClass: true
AfterControlStatement: true
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterStruct: true
AfterUnion: true
AfterExternBlock: false
BeforeCatch: true
BeforeElse: true
BeforeLambdaBody: true
BeforeWhile: true
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBraces: Custom
BreakConstructorInitializers: AfterColon
BreakConstructorInitializersBeforeComma: false
ColumnLimit: 120
ConstructorInitializerAllOnOneLineOrOnePerLine: false
IncludeCategories:
- Regex: '^<.*'
Priority: 1
- Regex: '^".*'
Priority: 2
- Regex: '.*'
Priority: 3
IncludeIsMainRegex: '([-_](test|unittest))?$'
IndentCaseBlocks: true
IndentWidth: 4
InsertNewlineAtEOF: true
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 2
NamespaceIndentation: All
PointerAlignment: Left
SpaceInEmptyParentheses: false
SpacesInAngles: false
SpacesInConditionalStatement: false
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
TabWidth: 4
...

View File

@@ -1,20 +1,21 @@
#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "esp_netif.h"
#include "esp_wifi.h"
#include "freertos/FreeRTOS.h"
#include "i2cdev.h"
#include "indicator.h"
#include "nconfig.h"
#include "nvs_flash.h"
#include "system.h"
#include "wifi.h"
void app_main(void)
{
ESP_ERROR_CHECK(i2cdev_init());;
ESP_ERROR_CHECK(i2cdev_init());
;
init_led();
led_set(LED_BLU, BLINK_TRIPLE);
led_off(LED_BLU);

View File

@@ -26,4 +26,4 @@ void init_led(void);
void led_set(enum blink_led led, enum blink_type type);
void led_off(enum blink_led led);
#endif //LED_H
#endif // LED_H

View File

@@ -5,8 +5,8 @@
#ifndef NCONFIG_H
#define NCONFIG_H
#include "nvs.h"
#include "esp_err.h"
#include "nvs.h"
#define NCONFIG_NVS_NAMESPACE "er"
#define NCONFIG_NOT_FOUND ESP_ERR_NVS_NOT_FOUND
@@ -45,4 +45,4 @@ esp_err_t nconfig_read(enum nconfig_type type, char* data, size_t len);
// Remove key
esp_err_t nconfig_delete(enum nconfig_type type);
#endif //NCONFIG_H
#endif // NCONFIG_H

View File

@@ -9,4 +9,4 @@ void start_reboot_timer(int sec);
void stop_reboot_timer();
void start_webserver();
#endif //SYSTEM_H
#endif // SYSTEM_H

View File

@@ -44,21 +44,21 @@ esp_err_t wifi_disconnect(void);
* @param ap_records A pointer to store the found AP records.
* @param count A pointer to store the number of found APs.
*/
void wifi_scan_aps(wifi_ap_record_t **ap_records, uint16_t *count);
void wifi_scan_aps(wifi_ap_record_t** ap_records, uint16_t* count);
/**
* @brief Gets information about the currently connected access point.
* @param ap_info A pointer to a structure to store the AP information.
* @return ESP_OK on success, or an error code on failure.
*/
esp_err_t wifi_get_current_ap_info(wifi_ap_record_t *ap_info);
esp_err_t wifi_get_current_ap_info(wifi_ap_record_t* ap_info);
/**
* @brief Gets the current IP information for the STA interface.
* @param ip_info A pointer to a structure to store the IP information.
* @return ESP_OK on success, or an error code on failure.
*/
esp_err_t wifi_get_current_ip_info(esp_netif_ip_info_t *ip_info);
esp_err_t wifi_get_current_ip_info(esp_netif_ip_info_t* ip_info);
/**
* @brief Gets the DNS server information for the STA interface.
@@ -66,7 +66,7 @@ esp_err_t wifi_get_current_ip_info(esp_netif_ip_info_t *ip_info);
* @param dns_info A pointer to a structure to store the DNS information.
* @return ESP_OK on success, or an error code on failure.
*/
esp_err_t wifi_get_dns_info(esp_netif_dns_type_t type, esp_netif_dns_info_t *dns_info);
esp_err_t wifi_get_dns_info(esp_netif_dns_type_t type, esp_netif_dns_info_t* dns_info);
/**
* @brief Configures the STA interface to use DHCP.
@@ -110,4 +110,4 @@ void sync_time();
*/
esp_err_t wifi_sta_set_ap(const char* ssid, const char* password);
#endif //WIFI_H
#endif // WIFI_H

View File

@@ -49,12 +49,8 @@ static const blink_step_t solid_blink[] = {
};
blink_step_t const* led_mode[] = {
[BLINK_SLOW] = slow_blink,
[BLINK_FAST] = fast_blink,
[BLINK_DOUBLE] = double_blink,
[BLINK_TRIPLE] = triple_blink,
[BLINK_SOLID] = solid_blink,
[BLINK_MAX] = NULL,
[BLINK_SLOW] = slow_blink, [BLINK_FAST] = fast_blink, [BLINK_DOUBLE] = double_blink,
[BLINK_TRIPLE] = triple_blink, [BLINK_SOLID] = solid_blink, [BLINK_MAX] = NULL,
};
led_indicator_handle_t led_handle[LED_MAX] = {0};

View File

@@ -4,24 +4,15 @@
#include "nconfig.h"
#include "nvs_flash.h"
#include "esp_err.h"
#include "nvs_flash.h"
static nvs_handle_t handle;
const static char* keys[NCONFIG_TYPE_MAX] = {
[WIFI_SSID] = "wifi_ssid",
[WIFI_PASSWORD] = "wifi_pw",
[WIFI_MODE] = "wifi_mode",
[AP_SSID] = "ap_ssid",
[AP_PASSWORD] = "ap_pw",
[NETIF_HOSTNAME] = "hostname",
[NETIF_IP] = "ip",
[NETIF_GATEWAY] = "gw",
[NETIF_SUBNET] = "sn",
[NETIF_DNS1] = "dns1",
[NETIF_DNS2] = "dns2",
[NETIF_TYPE] = "dhcp",
[WIFI_SSID] = "wifi_ssid", [WIFI_PASSWORD] = "wifi_pw", [WIFI_MODE] = "wifi_mode", [AP_SSID] = "ap_ssid",
[AP_PASSWORD] = "ap_pw", [NETIF_HOSTNAME] = "hostname", [NETIF_IP] = "ip", [NETIF_GATEWAY] = "gw",
[NETIF_SUBNET] = "sn", [NETIF_DNS1] = "dns1", [NETIF_DNS2] = "dns2", [NETIF_TYPE] = "dhcp",
[UART_BAUD_RATE] = "baudrate",
};
@@ -47,7 +38,8 @@ struct default_value const default_values[] = {
esp_err_t init_nconfig()
{
esp_err_t ret = nvs_open(NCONFIG_NVS_NAMESPACE, NVS_READWRITE, &handle);
if (ret != ESP_OK) return ret;
if (ret != ESP_OK)
return ret;
for (int i = 0; i < sizeof(default_values) / sizeof(default_values[0]); ++i)
{
@@ -70,15 +62,9 @@ bool nconfig_value_is_not_set(enum nconfig_type type)
return (err != ESP_OK || len <= 1);
}
esp_err_t nconfig_write(enum nconfig_type type, const char* data)
{
return nvs_set_str(handle, keys[type], data);
}
esp_err_t nconfig_write(enum nconfig_type type, const char* data) { return nvs_set_str(handle, keys[type], data); }
esp_err_t nconfig_delete(enum nconfig_type type)
{
return nvs_erase_key(handle, keys[type]);
}
esp_err_t nconfig_delete(enum nconfig_type type) { return nvs_erase_key(handle, keys[type]); }
esp_err_t nconfig_get_str_len(enum nconfig_type type, size_t* len)
{

View File

@@ -1,10 +1,10 @@
#include "webserver.h"
#include "cJSON.h"
#include "driver/gpio.h"
#include "esp_http_server.h"
#include "esp_log.h"
#include "cJSON.h"
#include "freertos/FreeRTOS.h"
#include "sw.h"
#include "webserver.h"
static esp_err_t control_get_handler(httpd_req_t* req)
{
@@ -53,16 +53,20 @@ static esp_err_t control_post_handler(httpd_req_t* req)
}
cJSON* item_12v = cJSON_GetObjectItem(root, "load_12v_on");
if (cJSON_IsBool(item_12v)) set_main_load_switch(cJSON_IsTrue(item_12v));
if (cJSON_IsBool(item_12v))
set_main_load_switch(cJSON_IsTrue(item_12v));
cJSON* item_5v = cJSON_GetObjectItem(root, "load_5v_on");
if (cJSON_IsBool(item_5v)) set_usb_load_switch(cJSON_IsTrue(item_5v));
if (cJSON_IsBool(item_5v))
set_usb_load_switch(cJSON_IsTrue(item_5v));
cJSON* power_trigger = cJSON_GetObjectItem(root, "power_trigger");
if (cJSON_IsTrue(power_trigger)) trig_power();
if (cJSON_IsTrue(power_trigger))
trig_power();
cJSON* reset_trigger = cJSON_GetObjectItem(root, "reset_trigger");
if (cJSON_IsTrue(reset_trigger)) trig_reset();
if (cJSON_IsTrue(reset_trigger))
trig_reset();
cJSON_Delete(root);
@@ -73,19 +77,10 @@ static esp_err_t control_post_handler(httpd_req_t* req)
void register_control_endpoint(httpd_handle_t server)
{
init_sw();
httpd_uri_t get_uri = {
.uri = "/api/control",
.method = HTTP_GET,
.handler = control_get_handler,
.user_ctx = NULL
};
httpd_uri_t get_uri = {.uri = "/api/control", .method = HTTP_GET, .handler = control_get_handler, .user_ctx = NULL};
httpd_register_uri_handler(server, &get_uri);
httpd_uri_t post_uri = {
.uri = "/api/control",
.method = HTTP_POST,
.handler = control_post_handler,
.user_ctx = NULL
};
.uri = "/api/control", .method = HTTP_POST, .handler = control_post_handler, .user_ctx = NULL};
httpd_register_uri_handler(server, &post_uri);
}

View File

@@ -1,7 +1,7 @@
#include "datalog.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "esp_littlefs.h"
@@ -66,7 +66,9 @@ void datalog_init(void)
else
{
// Add header for 3 channels
fprintf(f_write, "timestamp,usb_voltage,usb_current,usb_power,main_voltage,main_current,main_power,vin_voltage,vin_current,vin_power\n");
fprintf(f_write,
"timestamp,usb_voltage,usb_current,usb_power,main_voltage,main_current,main_power,vin_voltage,vin_"
"current,vin_power\n");
fclose(f_write);
}
}
@@ -94,8 +96,10 @@ void datalog_add(uint32_t timestamp, const channel_data_t* channel_data)
if (f_read == NULL || f_write == NULL)
{
ESP_LOGE(TAG, "Failed to open files for truncation.");
if (f_read) fclose(f_read);
if (f_write) fclose(f_write);
if (f_read)
fclose(f_read);
if (f_write)
fclose(f_write);
return;
}
@@ -142,14 +146,12 @@ void datalog_add(uint32_t timestamp, const channel_data_t* channel_data)
}
fprintf(f, "%lu", timestamp);
for (int i = 0; i < 3; ++i) {
for (int i = 0; i < 3; ++i)
{
fprintf(f, ",%.3f,%.3f,%.3f", channel_data[i].voltage, channel_data[i].current, channel_data[i].power);
}
fprintf(f, "\n");
fclose(f);
}
const char* datalog_get_path(void)
{
return LOG_FILE_PATH;
}
const char* datalog_get_path(void) { return LOG_FILE_PATH; }

View File

@@ -5,7 +5,8 @@
#define NUM_CHANNELS 3
typedef struct {
typedef struct
{
float voltage;
float current;
float power;

View File

@@ -5,37 +5,29 @@
#include "monitor.h"
#include <time.h>
#include "freertos/FreeRTOS.h"
#include "esp_log.h"
#include "esp_timer.h"
#include "cJSON.h"
#include "datalog.h"
#include "esp_log.h"
#include "esp_netif.h"
#include "esp_timer.h"
#include "esp_wifi_types_generic.h"
#include "freertos/FreeRTOS.h"
#include "ina3221.h"
#include "webserver.h"
#include "wifi.h"
#include "datalog.h"
#include "ina3221.h"
#define PM_SDA CONFIG_I2C_GPIO_SDA
#define PM_SCL CONFIG_I2C_GPIO_SCL
const char* channel_names[] = {
"USB",
"MAIN",
"VIN"
};
const char* channel_names[] = {"USB", "MAIN", "VIN"};
ina3221_t ina3221 =
{
ina3221_t ina3221 = {
/* shunt values are 100 mOhm for each channel */
.shunt = {
10,
10,
10
},
.shunt = {10, 10, 10},
.mask.mask_register = INA3221_DEFAULT_MASK,
.i2c_dev = {0},
.config = {
.config =
{
.mode = true, // mode selection
.esht = true, // shunt enable
.ebus = true, // bus enable

View File

@@ -21,4 +21,4 @@ typedef struct
void init_status_monitor();
#endif //ODROID_REMOTE_HTTP_MONITOR_H
#endif // ODROID_REMOTE_HTTP_MONITOR_H

View File

@@ -1,12 +1,12 @@
#include "webserver.h"
#include "cJSON.h"
#include "esp_http_server.h"
#include "esp_log.h"
#include "nconfig.h"
#include "wifi.h"
#include "system.h"
#include "esp_netif.h"
#include "esp_timer.h"
#include "nconfig.h"
#include "system.h"
#include "webserver.h"
#include "wifi.h"
static const char* TAG = "webserver";
@@ -126,7 +126,8 @@ static esp_err_t setting_post_handler(httpd_req_t* req)
if (received <= 0)
{
if (received == HTTPD_SOCK_ERR_TIMEOUT) httpd_resp_send_408(req);
if (received == HTTPD_SOCK_ERR_TIMEOUT)
httpd_resp_send_408(req);
return ESP_FAIL;
}
buf[received] = '\0';
@@ -210,8 +211,10 @@ static esp_err_t setting_post_handler(httpd_req_t* req)
nconfig_write(NETIF_GATEWAY, gw);
nconfig_write(NETIF_SUBNET, sn);
nconfig_write(NETIF_DNS1, d1);
if (d2) nconfig_write(NETIF_DNS2, d2);
else nconfig_delete(NETIF_DNS2);
if (d2)
nconfig_write(NETIF_DNS2, d2);
else
nconfig_delete(NETIF_DNS2);
wifi_use_static(ip, gw, sn, d1, d2);
httpd_resp_sendstr(req, "{\"status\":\"static_config_applied\"}");
@@ -261,27 +264,12 @@ static esp_err_t setting_post_handler(httpd_req_t* req)
void register_wifi_endpoint(httpd_handle_t server)
{
httpd_uri_t status = {
.uri = "/api/setting",
.method = HTTP_GET,
.handler = setting_get_handler,
.user_ctx = NULL
};
httpd_uri_t status = {.uri = "/api/setting", .method = HTTP_GET, .handler = setting_get_handler, .user_ctx = NULL};
httpd_register_uri_handler(server, &status);
httpd_uri_t set = {
.uri = "/api/setting",
.method = HTTP_POST,
.handler = setting_post_handler,
.user_ctx = NULL
};
httpd_uri_t set = {.uri = "/api/setting", .method = HTTP_POST, .handler = setting_post_handler, .user_ctx = NULL};
httpd_register_uri_handler(server, &set);
httpd_uri_t scan = {
.uri = "/api/wifi/scan",
.method = HTTP_GET,
.handler = wifi_scan,
.user_ctx = NULL
};
httpd_uri_t scan = {.uri = "/api/wifi/scan", .method = HTTP_GET, .handler = wifi_scan, .user_ctx = NULL};
httpd_register_uri_handler(server, &scan);
}

View File

@@ -4,17 +4,17 @@
#include "sw.h"
#include <inttypes.h>
#include <stdio.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <ina3221.h>
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
#include "driver/gpio.h"
#include "esp_log.h"
#include "esp_timer.h"
#include "pca9557.h"
#include "driver/gpio.h"
#define I2C_PORT 0
@@ -72,17 +72,11 @@ void init_sw()
load_switch_5v_status = val != 0 ? true : false;
const esp_timer_create_args_t power_timer_args = {
.callback = &trigger_off_callback,
.arg = (void*)GPIO_PWR,
.name = "power_trigger_off"
};
.callback = &trigger_off_callback, .arg = (void*)GPIO_PWR, .name = "power_trigger_off"};
ESP_ERROR_CHECK(esp_timer_create(&power_timer_args, &power_trigger_timer));
const esp_timer_create_args_t reset_timer_args = {
.callback = &trigger_off_callback,
.arg = (void*)GPIO_RST,
.name = "power_trigger_off"
};
.callback = &trigger_off_callback, .arg = (void*)GPIO_RST, .name = "power_trigger_off"};
ESP_ERROR_CHECK(esp_timer_create(&reset_timer_args, &reset_trigger_timer));
expander_mutex = xSemaphoreCreateMutex();
@@ -119,7 +113,8 @@ void trig_reset()
void set_main_load_switch(bool on)
{
ESP_LOGI(TAG, "Set main load switch to %s", on ? "on" : "off");
if (load_switch_12v_status == on) return;
if (load_switch_12v_status == on)
return;
if (xSemaphoreTake(expander_mutex, MUTEX_TIMEOUT) == pdFALSE)
{
ESP_LOGW(TAG, "Control error");
@@ -134,7 +129,8 @@ void set_main_load_switch(bool on)
void set_usb_load_switch(bool on)
{
ESP_LOGI(TAG, "Set usb load switch to %s", on ? "on" : "off");
if (load_switch_5v_status == on) return;
if (load_switch_5v_status == on)
return;
if (xSemaphoreTake(expander_mutex, MUTEX_TIMEOUT) == pdFALSE)
{
ESP_LOGW(TAG, "Control error");
@@ -146,12 +142,6 @@ void set_usb_load_switch(bool on)
xSemaphoreGive(expander_mutex);
}
bool get_main_load_switch()
{
return load_switch_12v_status;
}
bool get_main_load_switch() { return load_switch_12v_status; }
bool get_usb_load_switch()
{
return load_switch_5v_status;
}
bool get_usb_load_switch() { return load_switch_5v_status; }

View File

@@ -14,4 +14,4 @@ void set_usb_load_switch(bool on);
bool get_main_load_switch();
bool get_usb_load_switch();
#endif //ODROID_POWER_MATE_SW_H
#endif // ODROID_POWER_MATE_SW_H

View File

@@ -1,16 +1,16 @@
#include "webserver.h"
#include <stdio.h>
#include <string.h>
#include "datalog.h"
#include "esp_http_server.h"
#include "esp_log.h"
#include "esp_wifi.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_wifi.h"
#include "esp_log.h"
#include "esp_http_server.h"
#include "nconfig.h"
#include "monitor.h"
#include "datalog.h"
#include "lwip/err.h"
#include "lwip/sys.h"
#include "monitor.h"
#include "nconfig.h"
static const char* TAG = "WEBSERVER";
@@ -73,20 +73,11 @@ void start_webserver(void)
}
// Index page
httpd_uri_t index = {
.uri = "/",
.method = HTTP_GET,
.handler = index_handler,
.user_ctx = NULL
};
httpd_uri_t index = {.uri = "/", .method = HTTP_GET, .handler = index_handler, .user_ctx = NULL};
httpd_register_uri_handler(server, &index);
httpd_uri_t datalog_uri = {
.uri = "/datalog.csv",
.method = HTTP_GET,
.handler = datalog_download_handler,
.user_ctx = NULL
};
.uri = "/datalog.csv", .method = HTTP_GET, .handler = datalog_download_handler, .user_ctx = NULL};
httpd_register_uri_handler(server, &datalog_uri);
register_wifi_endpoint(server);

View File

@@ -14,4 +14,4 @@ void push_data_to_ws(cJSON* data);
void register_reboot_endpoint(httpd_handle_t server);
esp_err_t change_baud_rate(int baud_rate);
#endif //ODROID_REMOTE_HTTP_WEBSERVER_H
#endif // ODROID_REMOTE_HTTP_WEBSERVER_H

View File

@@ -3,13 +3,13 @@
//
#include "cJSON.h"
#include "webserver.h"
#include "driver/uart.h"
#include "esp_err.h"
#include "esp_http_server.h"
#include "esp_log.h"
#include "nconfig.h"
#include "driver/uart.h"
#include "freertos/semphr.h"
#include "nconfig.h"
#include "webserver.h"
#define UART_NUM UART_NUM_1
#define BUF_SIZE (2048)
@@ -197,12 +197,10 @@ static void uart_polling_task(void* arg)
size_t total_processed = 0;
while (available_len > 0 && total_processed < BUF_SIZE)
{
size_t read_size = (available_len > (BUF_SIZE - total_processed))
? (BUF_SIZE - total_processed)
: available_len;
size_t read_size =
(available_len > (BUF_SIZE - total_processed)) ? (BUF_SIZE - total_processed) : available_len;
int bytes_read = uart_read_bytes(UART_NUM, data_buf + total_processed,
read_size, READ_TIMEOUT);
int bytes_read = uart_read_bytes(UART_NUM, data_buf + total_processed, read_size, READ_TIMEOUT);
if (bytes_read <= 0)
{
@@ -221,9 +219,8 @@ static void uart_polling_task(void* arg)
while (offset < total_processed)
{
const size_t chunk_size = (total_processed - offset > CHUNK_SIZE)
? CHUNK_SIZE
: (total_processed - offset);
const size_t chunk_size =
(total_processed - offset > CHUNK_SIZE) ? CHUNK_SIZE : (total_processed - offset);
struct ws_message msg;
msg.type = WS_MSG_UART;
@@ -333,13 +330,7 @@ void register_ws_endpoint(httpd_handle_t server)
ESP_ERROR_CHECK(uart_set_pin(UART_NUM, UART_TX_PIN, UART_RX_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
ESP_ERROR_CHECK(uart_driver_install(UART_NUM, BUF_SIZE * 2, BUF_SIZE * 2, 0, NULL, 0));
httpd_uri_t ws = {
.uri = "/ws",
.method = HTTP_GET,
.handler = ws_handler,
.user_ctx = NULL,
.is_websocket = true
};
httpd_uri_t ws = {.uri = "/ws", .method = HTTP_GET, .handler = ws_handler, .user_ctx = NULL, .is_websocket = true};
httpd_register_uri_handler(server, &ws);
client_fd_mutex = xSemaphoreCreateMutex();
@@ -361,7 +352,4 @@ void push_data_to_ws(cJSON* data)
}
}
esp_err_t change_baud_rate(int baud_rate)
{
return uart_set_baudrate(UART_NUM, baud_rate);
}
esp_err_t change_baud_rate(int baud_rate) { return uart_set_baudrate(UART_NUM, baud_rate); }

View File

@@ -6,9 +6,9 @@
#include <esp_log.h>
#include <esp_timer.h>
#include <string.h>
#include "esp_http_server.h"
#include "esp_system.h"
#include <string.h>
static const char* TAG = "odroid";
@@ -30,10 +30,7 @@ void start_reboot_timer(int sec)
ESP_LOGI(TAG, "Device will reboot in %d seconds.", sec);
const esp_timer_create_args_t reboot_timer_args = {
.callback = &reboot_timer_callback,
.name = "reboot-timer"
};
const esp_timer_create_args_t reboot_timer_args = {.callback = &reboot_timer_callback, .name = "reboot-timer"};
if (esp_timer_create(&reboot_timer_args, &reboot_timer_handle) != ESP_OK)
{
@@ -77,10 +74,6 @@ void stop_reboot_timer()
void register_reboot_endpoint(httpd_handle_t server)
{
httpd_uri_t post_uri = {
.uri = "/api/reboot",
.method = HTTP_POST,
.handler = reboot_post_handler,
.user_ctx = NULL
};
.uri = "/api/reboot", .method = HTTP_POST, .handler = reboot_post_handler, .user_ctx = NULL};
httpd_register_uri_handler(server, &post_uri);
}

View File

@@ -2,16 +2,16 @@
// Created by shinys on 25. 9. 1.
//
#include "priv_wifi.h"
#include <string.h>
#include "esp_wifi.h"
#include "esp_log.h"
#include "nconfig.h"
#include "esp_netif.h"
#include "wifi.h"
#include "esp_wifi.h"
#include "lwip/ip4_addr.h"
#include "nconfig.h"
#include "priv_wifi.h"
#include "wifi.h"
static const char *TAG = "AP";
static const char* TAG = "AP";
#define DEFAULT_AP_SSID "odroid-pm"
#define DEFAULT_AP_PASS "powermate"
@@ -24,9 +24,10 @@ static const char *TAG = "AP";
void wifi_init_ap(void)
{
// Get the network interface handle for the AP
esp_netif_t *p_netif_ap = esp_netif_get_handle_from_ifkey("WIFI_AP_DEF");
esp_netif_t* p_netif_ap = esp_netif_get_handle_from_ifkey("WIFI_AP_DEF");
if (p_netif_ap) {
if (p_netif_ap)
{
ESP_LOGI(TAG, "Setting AP static IP to 192.168.4.1");
esp_netif_dhcps_stop(p_netif_ap); // Stop DHCP server to apply new IP settings
@@ -41,11 +42,13 @@ void wifi_init_ap(void)
// Configure Wi-Fi AP settings
wifi_config_t wifi_config = {
.ap = {
.ap =
{
.channel = AP_CHANNEL,
.max_connection = AP_MAX_CONN,
.authmode = WIFI_AUTH_WPA2_PSK,
.pmf_cfg = {
.pmf_cfg =
{
.required = false,
},
},
@@ -53,25 +56,32 @@ void wifi_init_ap(void)
// Read SSID and password from NVS (nconfig)
size_t len;
if (nconfig_get_str_len(AP_SSID, &len) == ESP_OK && len > 1) {
if (nconfig_get_str_len(AP_SSID, &len) == ESP_OK && len > 1)
{
nconfig_read(AP_SSID, (char*)wifi_config.ap.ssid, sizeof(wifi_config.ap.ssid));
} else {
}
else
{
strcpy((char*)wifi_config.ap.ssid, DEFAULT_AP_SSID);
}
if (nconfig_get_str_len(AP_PASSWORD, &len) == ESP_OK && len > 1) {
if (nconfig_get_str_len(AP_PASSWORD, &len) == ESP_OK && len > 1)
{
nconfig_read(AP_PASSWORD, (char*)wifi_config.ap.password, sizeof(wifi_config.ap.password));
} else {
}
else
{
strcpy((char*)wifi_config.ap.password, DEFAULT_AP_PASS);
}
// If password is not set, use open authentication
if (strlen((char*)wifi_config.ap.password) == 0) {
if (strlen((char*)wifi_config.ap.password) == 0)
{
wifi_config.ap.authmode = WIFI_AUTH_OPEN;
}
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config));
ESP_LOGI(TAG, "wifi_init_ap finished. SSID: %s, Password: %s, Channel: %d",
(char*)wifi_config.ap.ssid, "********", AP_CHANNEL);
ESP_LOGI(TAG, "wifi_init_ap finished. SSID: %s, Password: %s, Channel: %d", (char*)wifi_config.ap.ssid, "********",
AP_CHANNEL);
}

View File

@@ -9,4 +9,4 @@ void wifi_init_sta(void);
void wifi_init_ap(void);
void initialize_sntp(void);
#endif //ODROID_POWER_MATE_PRIV_WIFI_H
#endif // ODROID_POWER_MATE_PRIV_WIFI_H

View File

@@ -2,13 +2,13 @@
// Created by shinys on 25. 9. 1.
//
#include "priv_wifi.h"
#include <string.h>
#include "esp_wifi.h"
#include "esp_log.h"
#include "nconfig.h"
#include "esp_netif.h"
#include "esp_wifi.h"
#include "lwip/inet.h"
#include "nconfig.h"
#include "priv_wifi.h"
#include "wifi.h"
static const char* TAG = "STA";

View File

@@ -2,16 +2,16 @@
// Created by shinys on 25. 9. 1.
//
#include "priv_wifi.h"
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include "esp_mac.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "nconfig.h"
#include "priv_wifi.h"
#include "wifi.h"
@@ -19,18 +19,17 @@
static const char* TAG = "WIFI";
static void wifi_event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
static void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
{
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_STACONNECTED)
{
wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*)event_data;
ESP_LOGI(TAG, "Station "MACSTR" joined, AID=%d", MAC2STR(event->mac), event->aid);
ESP_LOGI(TAG, "Station " MACSTR " joined, AID=%d", MAC2STR(event->mac), event->aid);
}
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_STADISCONNECTED)
{
wifi_event_ap_stadisconnected_t* event = (wifi_event_ap_stadisconnected_t*)event_data;
ESP_LOGI(TAG, "Station "MACSTR" left, AID=%d", MAC2STR(event->mac), event->aid);
ESP_LOGI(TAG, "Station " MACSTR " left, AID=%d", MAC2STR(event->mac), event->aid);
}
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START)
{

View File

@@ -2,10 +2,10 @@
// Created by shinys on 25. 9. 1.
//
#include "priv_wifi.h"
#include <time.h>
#include "esp_log.h"
#include "esp_sntp.h"
#include "priv_wifi.h"
#include "wifi.h"
static const char* TAG = "WIFI_HELPER";
@@ -55,39 +55,72 @@ const char* wifi_reason_str(wifi_err_reason_t reason)
{
switch (reason)
{
case WIFI_REASON_UNSPECIFIED: return "UNSPECIFIED";
case WIFI_REASON_AUTH_EXPIRE: return "AUTH_EXPIRE";
case WIFI_REASON_AUTH_LEAVE: return "AUTH_LEAVE";
case WIFI_REASON_ASSOC_EXPIRE: return "ASSOC_EXPIRE";
case WIFI_REASON_ASSOC_TOOMANY: return "ASSOC_TOOMANY";
case WIFI_REASON_NOT_AUTHED: return "NOT_AUTHED";
case WIFI_REASON_NOT_ASSOCED: return "NOT_ASSOCED";
case WIFI_REASON_ASSOC_LEAVE: return "ASSOC_LEAVE";
case WIFI_REASON_ASSOC_NOT_AUTHED: return "ASSOC_NOT_AUTHED";
case WIFI_REASON_DISASSOC_PWRCAP_BAD: return "DISASSOC_PWRCAP_BAD";
case WIFI_REASON_DISASSOC_SUPCHAN_BAD: return "DISASSOC_SUPCHAN_BAD";
case WIFI_REASON_IE_INVALID: return "IE_INVALID";
case WIFI_REASON_MIC_FAILURE: return "MIC_FAILURE";
case WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT: return "4WAY_HANDSHAKE_TIMEOUT";
case WIFI_REASON_GROUP_KEY_UPDATE_TIMEOUT: return "GROUP_KEY_UPDATE_TIMEOUT";
case WIFI_REASON_IE_IN_4WAY_DIFFERS: return "IE_IN_4WAY_DIFFERS";
case WIFI_REASON_GROUP_CIPHER_INVALID: return "GROUP_CIPHER_INVALID";
case WIFI_REASON_PAIRWISE_CIPHER_INVALID: return "PAIRWISE_CIPHER_INVALID";
case WIFI_REASON_AKMP_INVALID: return "AKMP_INVALID";
case WIFI_REASON_UNSUPP_RSN_IE_VERSION: return "UNSUPP_RSN_IE_VERSION";
case WIFI_REASON_INVALID_RSN_IE_CAP: return "INVALID_RSN_IE_CAP";
case WIFI_REASON_802_1X_AUTH_FAILED: return "802_1X_AUTH_FAILED";
case WIFI_REASON_CIPHER_SUITE_REJECTED: return "CIPHER_SUITE_REJECTED";
case WIFI_REASON_INVALID_PMKID: return "INVALID_PMKID";
case WIFI_REASON_BEACON_TIMEOUT: return "BEACON_TIMEOUT";
case WIFI_REASON_NO_AP_FOUND: return "NO_AP_FOUND";
case WIFI_REASON_AUTH_FAIL: return "AUTH_FAIL";
case WIFI_REASON_ASSOC_FAIL: return "ASSOC_FAIL";
case WIFI_REASON_HANDSHAKE_TIMEOUT: return "HANDSHAKE_TIMEOUT";
case WIFI_REASON_CONNECTION_FAIL: return "CONNECTION_FAIL";
case WIFI_REASON_AP_TSF_RESET: return "AP_TSF_RESET";
case WIFI_REASON_ROAMING: return "ROAMING";
default: return "UNKNOWN";
case WIFI_REASON_UNSPECIFIED:
return "UNSPECIFIED";
case WIFI_REASON_AUTH_EXPIRE:
return "AUTH_EXPIRE";
case WIFI_REASON_AUTH_LEAVE:
return "AUTH_LEAVE";
case WIFI_REASON_ASSOC_EXPIRE:
return "ASSOC_EXPIRE";
case WIFI_REASON_ASSOC_TOOMANY:
return "ASSOC_TOOMANY";
case WIFI_REASON_NOT_AUTHED:
return "NOT_AUTHED";
case WIFI_REASON_NOT_ASSOCED:
return "NOT_ASSOCED";
case WIFI_REASON_ASSOC_LEAVE:
return "ASSOC_LEAVE";
case WIFI_REASON_ASSOC_NOT_AUTHED:
return "ASSOC_NOT_AUTHED";
case WIFI_REASON_DISASSOC_PWRCAP_BAD:
return "DISASSOC_PWRCAP_BAD";
case WIFI_REASON_DISASSOC_SUPCHAN_BAD:
return "DISASSOC_SUPCHAN_BAD";
case WIFI_REASON_IE_INVALID:
return "IE_INVALID";
case WIFI_REASON_MIC_FAILURE:
return "MIC_FAILURE";
case WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT:
return "4WAY_HANDSHAKE_TIMEOUT";
case WIFI_REASON_GROUP_KEY_UPDATE_TIMEOUT:
return "GROUP_KEY_UPDATE_TIMEOUT";
case WIFI_REASON_IE_IN_4WAY_DIFFERS:
return "IE_IN_4WAY_DIFFERS";
case WIFI_REASON_GROUP_CIPHER_INVALID:
return "GROUP_CIPHER_INVALID";
case WIFI_REASON_PAIRWISE_CIPHER_INVALID:
return "PAIRWISE_CIPHER_INVALID";
case WIFI_REASON_AKMP_INVALID:
return "AKMP_INVALID";
case WIFI_REASON_UNSUPP_RSN_IE_VERSION:
return "UNSUPP_RSN_IE_VERSION";
case WIFI_REASON_INVALID_RSN_IE_CAP:
return "INVALID_RSN_IE_CAP";
case WIFI_REASON_802_1X_AUTH_FAILED:
return "802_1X_AUTH_FAILED";
case WIFI_REASON_CIPHER_SUITE_REJECTED:
return "CIPHER_SUITE_REJECTED";
case WIFI_REASON_INVALID_PMKID:
return "INVALID_PMKID";
case WIFI_REASON_BEACON_TIMEOUT:
return "BEACON_TIMEOUT";
case WIFI_REASON_NO_AP_FOUND:
return "NO_AP_FOUND";
case WIFI_REASON_AUTH_FAIL:
return "AUTH_FAIL";
case WIFI_REASON_ASSOC_FAIL:
return "ASSOC_FAIL";
case WIFI_REASON_HANDSHAKE_TIMEOUT:
return "HANDSHAKE_TIMEOUT";
case WIFI_REASON_CONNECTION_FAIL:
return "CONNECTION_FAIL";
case WIFI_REASON_AP_TSF_RESET:
return "AP_TSF_RESET";
case WIFI_REASON_ROAMING:
return "ROAMING";
default:
return "UNKNOWN";
}
}