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 <stdio.h>
#include <string.h> #include <string.h>
#include "freertos/FreeRTOS.h"
#include "esp_wifi.h"
#include "esp_event.h" #include "esp_event.h"
#include "esp_log.h" #include "esp_log.h"
#include "nvs_flash.h"
#include "esp_netif.h" #include "esp_netif.h"
#include "esp_wifi.h"
#include "freertos/FreeRTOS.h"
#include "i2cdev.h" #include "i2cdev.h"
#include "indicator.h" #include "indicator.h"
#include "nconfig.h" #include "nconfig.h"
#include "nvs_flash.h"
#include "system.h" #include "system.h"
#include "wifi.h" #include "wifi.h"
void app_main(void) void app_main(void)
{ {
ESP_ERROR_CHECK(i2cdev_init());; ESP_ERROR_CHECK(i2cdev_init());
;
init_led(); init_led();
led_set(LED_BLU, BLINK_TRIPLE); led_set(LED_BLU, BLINK_TRIPLE);
led_off(LED_BLU); 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_set(enum blink_led led, enum blink_type type);
void led_off(enum blink_led led); void led_off(enum blink_led led);
#endif //LED_H #endif // LED_H

View File

@@ -5,8 +5,8 @@
#ifndef NCONFIG_H #ifndef NCONFIG_H
#define NCONFIG_H #define NCONFIG_H
#include "nvs.h"
#include "esp_err.h" #include "esp_err.h"
#include "nvs.h"
#define NCONFIG_NVS_NAMESPACE "er" #define NCONFIG_NVS_NAMESPACE "er"
#define NCONFIG_NOT_FOUND ESP_ERR_NVS_NOT_FOUND #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 // Remove key
esp_err_t nconfig_delete(enum nconfig_type type); 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 stop_reboot_timer();
void start_webserver(); 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 ap_records A pointer to store the found AP records.
* @param count A pointer to store the number of found APs. * @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. * @brief Gets information about the currently connected access point.
* @param ap_info A pointer to a structure to store the AP information. * @param ap_info A pointer to a structure to store the AP information.
* @return ESP_OK on success, or an error code on failure. * @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. * @brief Gets the current IP information for the STA interface.
* @param ip_info A pointer to a structure to store the IP information. * @param ip_info A pointer to a structure to store the IP information.
* @return ESP_OK on success, or an error code on failure. * @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. * @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. * @param dns_info A pointer to a structure to store the DNS information.
* @return ESP_OK on success, or an error code on failure. * @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. * @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); 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_step_t const* led_mode[] = {
[BLINK_SLOW] = slow_blink, [BLINK_SLOW] = slow_blink, [BLINK_FAST] = fast_blink, [BLINK_DOUBLE] = double_blink,
[BLINK_FAST] = fast_blink, [BLINK_TRIPLE] = triple_blink, [BLINK_SOLID] = solid_blink, [BLINK_MAX] = NULL,
[BLINK_DOUBLE] = double_blink,
[BLINK_TRIPLE] = triple_blink,
[BLINK_SOLID] = solid_blink,
[BLINK_MAX] = NULL,
}; };
led_indicator_handle_t led_handle[LED_MAX] = {0}; led_indicator_handle_t led_handle[LED_MAX] = {0};

View File

@@ -4,24 +4,15 @@
#include "nconfig.h" #include "nconfig.h"
#include "nvs_flash.h"
#include "esp_err.h" #include "esp_err.h"
#include "nvs_flash.h"
static nvs_handle_t handle; static nvs_handle_t handle;
const static char* keys[NCONFIG_TYPE_MAX] = { const static char* keys[NCONFIG_TYPE_MAX] = {
[WIFI_SSID] = "wifi_ssid", [WIFI_SSID] = "wifi_ssid", [WIFI_PASSWORD] = "wifi_pw", [WIFI_MODE] = "wifi_mode", [AP_SSID] = "ap_ssid",
[WIFI_PASSWORD] = "wifi_pw", [AP_PASSWORD] = "ap_pw", [NETIF_HOSTNAME] = "hostname", [NETIF_IP] = "ip", [NETIF_GATEWAY] = "gw",
[WIFI_MODE] = "wifi_mode", [NETIF_SUBNET] = "sn", [NETIF_DNS1] = "dns1", [NETIF_DNS2] = "dns2", [NETIF_TYPE] = "dhcp",
[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", [UART_BAUD_RATE] = "baudrate",
}; };
@@ -47,7 +38,8 @@ struct default_value const default_values[] = {
esp_err_t init_nconfig() esp_err_t init_nconfig()
{ {
esp_err_t ret = nvs_open(NCONFIG_NVS_NAMESPACE, NVS_READWRITE, &handle); 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) 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); return (err != ESP_OK || len <= 1);
} }
esp_err_t nconfig_write(enum nconfig_type type, const char* data) esp_err_t nconfig_write(enum nconfig_type type, const char* data) { return nvs_set_str(handle, keys[type], data); }
{
return nvs_set_str(handle, keys[type], data);
}
esp_err_t nconfig_delete(enum nconfig_type type) esp_err_t nconfig_delete(enum nconfig_type type) { return nvs_erase_key(handle, keys[type]); }
{
return nvs_erase_key(handle, keys[type]);
}
esp_err_t nconfig_get_str_len(enum nconfig_type type, size_t* len) 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 "driver/gpio.h"
#include "esp_http_server.h" #include "esp_http_server.h"
#include "esp_log.h" #include "esp_log.h"
#include "cJSON.h"
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "sw.h" #include "sw.h"
#include "webserver.h"
static esp_err_t control_get_handler(httpd_req_t* req) 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"); 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"); 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"); 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"); 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); 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) void register_control_endpoint(httpd_handle_t server)
{ {
init_sw(); init_sw();
httpd_uri_t get_uri = { httpd_uri_t get_uri = {.uri = "/api/control", .method = HTTP_GET, .handler = control_get_handler, .user_ctx = NULL};
.uri = "/api/control",
.method = HTTP_GET,
.handler = control_get_handler,
.user_ctx = NULL
};
httpd_register_uri_handler(server, &get_uri); httpd_register_uri_handler(server, &get_uri);
httpd_uri_t post_uri = { httpd_uri_t post_uri = {
.uri = "/api/control", .uri = "/api/control", .method = HTTP_POST, .handler = control_post_handler, .user_ctx = NULL};
.method = HTTP_POST,
.handler = control_post_handler,
.user_ctx = NULL
};
httpd_register_uri_handler(server, &post_uri); httpd_register_uri_handler(server, &post_uri);
} }

View File

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

View File

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

View File

@@ -5,47 +5,39 @@
#include "monitor.h" #include "monitor.h"
#include <time.h> #include <time.h>
#include "freertos/FreeRTOS.h"
#include "esp_log.h"
#include "esp_timer.h"
#include "cJSON.h" #include "cJSON.h"
#include "datalog.h"
#include "esp_log.h"
#include "esp_netif.h" #include "esp_netif.h"
#include "esp_timer.h"
#include "esp_wifi_types_generic.h" #include "esp_wifi_types_generic.h"
#include "freertos/FreeRTOS.h"
#include "ina3221.h"
#include "webserver.h" #include "webserver.h"
#include "wifi.h" #include "wifi.h"
#include "datalog.h"
#include "ina3221.h"
#define PM_SDA CONFIG_I2C_GPIO_SDA #define PM_SDA CONFIG_I2C_GPIO_SDA
#define PM_SCL CONFIG_I2C_GPIO_SCL #define PM_SCL CONFIG_I2C_GPIO_SCL
const char* channel_names[] = { const char* channel_names[] = {"USB", "MAIN", "VIN"};
"USB",
"MAIN",
"VIN"
};
ina3221_t ina3221 = ina3221_t ina3221 = {
{
/* shunt values are 100 mOhm for each channel */ /* shunt values are 100 mOhm for each channel */
.shunt = { .shunt = {10, 10, 10},
10,
10,
10
},
.mask.mask_register = INA3221_DEFAULT_MASK, .mask.mask_register = INA3221_DEFAULT_MASK,
.i2c_dev = {0}, .i2c_dev = {0},
.config = { .config =
.mode = true, // mode selection {
.esht = true, // shunt enable .mode = true, // mode selection
.ebus = true, // bus enable .esht = true, // shunt enable
.ch1 = true, // channel 1 enable .ebus = true, // bus enable
.ch2 = true, // channel 2 enable .ch1 = true, // channel 1 enable
.ch3 = true, // channel 3 enable .ch2 = true, // channel 2 enable
.avg = INA3221_AVG_64, // 64 samples average .ch3 = true, // channel 3 enable
.vbus = INA3221_CT_2116, // 2ms by channel (bus) .avg = INA3221_AVG_64, // 64 samples average
.vsht = INA3221_CT_2116, // 2ms by channel (shunt) .vbus = INA3221_CT_2116, // 2ms by channel (bus)
}, .vsht = INA3221_CT_2116, // 2ms by channel (shunt)
},
}; };
// Timer callback function to read sensor data // Timer callback function to read sensor data

View File

@@ -21,4 +21,4 @@ typedef struct
void init_status_monitor(); 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 "cJSON.h"
#include "esp_http_server.h" #include "esp_http_server.h"
#include "esp_log.h" #include "esp_log.h"
#include "nconfig.h"
#include "wifi.h"
#include "system.h"
#include "esp_netif.h" #include "esp_netif.h"
#include "esp_timer.h" #include "esp_timer.h"
#include "nconfig.h"
#include "system.h"
#include "webserver.h"
#include "wifi.h"
static const char* TAG = "webserver"; 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 <= 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; return ESP_FAIL;
} }
buf[received] = '\0'; 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_GATEWAY, gw);
nconfig_write(NETIF_SUBNET, sn); nconfig_write(NETIF_SUBNET, sn);
nconfig_write(NETIF_DNS1, d1); nconfig_write(NETIF_DNS1, d1);
if (d2) nconfig_write(NETIF_DNS2, d2); if (d2)
else nconfig_delete(NETIF_DNS2); nconfig_write(NETIF_DNS2, d2);
else
nconfig_delete(NETIF_DNS2);
wifi_use_static(ip, gw, sn, d1, d2); wifi_use_static(ip, gw, sn, d1, d2);
httpd_resp_sendstr(req, "{\"status\":\"static_config_applied\"}"); 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) void register_wifi_endpoint(httpd_handle_t server)
{ {
httpd_uri_t status = { httpd_uri_t status = {.uri = "/api/setting", .method = HTTP_GET, .handler = setting_get_handler, .user_ctx = NULL};
.uri = "/api/setting",
.method = HTTP_GET,
.handler = setting_get_handler,
.user_ctx = NULL
};
httpd_register_uri_handler(server, &status); httpd_register_uri_handler(server, &status);
httpd_uri_t set = { httpd_uri_t set = {.uri = "/api/setting", .method = HTTP_POST, .handler = setting_post_handler, .user_ctx = NULL};
.uri = "/api/setting",
.method = HTTP_POST,
.handler = setting_post_handler,
.user_ctx = NULL
};
httpd_register_uri_handler(server, &set); httpd_register_uri_handler(server, &set);
httpd_uri_t scan = { httpd_uri_t scan = {.uri = "/api/wifi/scan", .method = HTTP_GET, .handler = wifi_scan, .user_ctx = NULL};
.uri = "/api/wifi/scan",
.method = HTTP_GET,
.handler = wifi_scan,
.user_ctx = NULL
};
httpd_register_uri_handler(server, &scan); httpd_register_uri_handler(server, &scan);
} }

View File

@@ -4,17 +4,17 @@
#include "sw.h" #include "sw.h"
#include <inttypes.h>
#include <stdio.h>
#include <freertos/FreeRTOS.h> #include <freertos/FreeRTOS.h>
#include <freertos/task.h> #include <freertos/task.h>
#include <ina3221.h> #include <ina3221.h>
#include <inttypes.h>
#include <stdio.h>
#include <string.h> #include <string.h>
#include "driver/gpio.h"
#include "esp_log.h" #include "esp_log.h"
#include "esp_timer.h" #include "esp_timer.h"
#include "pca9557.h" #include "pca9557.h"
#include "driver/gpio.h"
#define I2C_PORT 0 #define I2C_PORT 0
@@ -72,17 +72,11 @@ void init_sw()
load_switch_5v_status = val != 0 ? true : false; load_switch_5v_status = val != 0 ? true : false;
const esp_timer_create_args_t power_timer_args = { const esp_timer_create_args_t power_timer_args = {
.callback = &trigger_off_callback, .callback = &trigger_off_callback, .arg = (void*)GPIO_PWR, .name = "power_trigger_off"};
.arg = (void*)GPIO_PWR,
.name = "power_trigger_off"
};
ESP_ERROR_CHECK(esp_timer_create(&power_timer_args, &power_trigger_timer)); ESP_ERROR_CHECK(esp_timer_create(&power_timer_args, &power_trigger_timer));
const esp_timer_create_args_t reset_timer_args = { const esp_timer_create_args_t reset_timer_args = {
.callback = &trigger_off_callback, .callback = &trigger_off_callback, .arg = (void*)GPIO_RST, .name = "power_trigger_off"};
.arg = (void*)GPIO_RST,
.name = "power_trigger_off"
};
ESP_ERROR_CHECK(esp_timer_create(&reset_timer_args, &reset_trigger_timer)); ESP_ERROR_CHECK(esp_timer_create(&reset_timer_args, &reset_trigger_timer));
expander_mutex = xSemaphoreCreateMutex(); expander_mutex = xSemaphoreCreateMutex();
@@ -119,7 +113,8 @@ void trig_reset()
void set_main_load_switch(bool on) void set_main_load_switch(bool on)
{ {
ESP_LOGI(TAG, "Set main load switch to %s", on ? "on" : "off"); 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) if (xSemaphoreTake(expander_mutex, MUTEX_TIMEOUT) == pdFALSE)
{ {
ESP_LOGW(TAG, "Control error"); ESP_LOGW(TAG, "Control error");
@@ -134,7 +129,8 @@ void set_main_load_switch(bool on)
void set_usb_load_switch(bool on) void set_usb_load_switch(bool on)
{ {
ESP_LOGI(TAG, "Set usb load switch to %s", on ? "on" : "off"); 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) if (xSemaphoreTake(expander_mutex, MUTEX_TIMEOUT) == pdFALSE)
{ {
ESP_LOGW(TAG, "Control error"); ESP_LOGW(TAG, "Control error");
@@ -146,12 +142,6 @@ void set_usb_load_switch(bool on)
xSemaphoreGive(expander_mutex); xSemaphoreGive(expander_mutex);
} }
bool get_main_load_switch() bool get_main_load_switch() { return load_switch_12v_status; }
{
return load_switch_12v_status;
}
bool get_usb_load_switch() bool get_usb_load_switch() { return load_switch_5v_status; }
{
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_main_load_switch();
bool get_usb_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 "webserver.h"
#include <stdio.h> #include <stdio.h>
#include <string.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/FreeRTOS.h"
#include "freertos/task.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/err.h"
#include "lwip/sys.h" #include "lwip/sys.h"
#include "monitor.h"
#include "nconfig.h"
static const char* TAG = "WEBSERVER"; static const char* TAG = "WEBSERVER";
@@ -73,20 +73,11 @@ void start_webserver(void)
} }
// Index page // Index page
httpd_uri_t index = { httpd_uri_t index = {.uri = "/", .method = HTTP_GET, .handler = index_handler, .user_ctx = NULL};
.uri = "/",
.method = HTTP_GET,
.handler = index_handler,
.user_ctx = NULL
};
httpd_register_uri_handler(server, &index); httpd_register_uri_handler(server, &index);
httpd_uri_t datalog_uri = { httpd_uri_t datalog_uri = {
.uri = "/datalog.csv", .uri = "/datalog.csv", .method = HTTP_GET, .handler = datalog_download_handler, .user_ctx = NULL};
.method = HTTP_GET,
.handler = datalog_download_handler,
.user_ctx = NULL
};
httpd_register_uri_handler(server, &datalog_uri); httpd_register_uri_handler(server, &datalog_uri);
register_wifi_endpoint(server); 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); void register_reboot_endpoint(httpd_handle_t server);
esp_err_t change_baud_rate(int baud_rate); 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 "cJSON.h"
#include "webserver.h" #include "driver/uart.h"
#include "esp_err.h" #include "esp_err.h"
#include "esp_http_server.h" #include "esp_http_server.h"
#include "esp_log.h" #include "esp_log.h"
#include "nconfig.h"
#include "driver/uart.h"
#include "freertos/semphr.h" #include "freertos/semphr.h"
#include "nconfig.h"
#include "webserver.h"
#define UART_NUM UART_NUM_1 #define UART_NUM UART_NUM_1
#define BUF_SIZE (2048) #define BUF_SIZE (2048)
@@ -197,12 +197,10 @@ static void uart_polling_task(void* arg)
size_t total_processed = 0; size_t total_processed = 0;
while (available_len > 0 && total_processed < BUF_SIZE) while (available_len > 0 && total_processed < BUF_SIZE)
{ {
size_t read_size = (available_len > (BUF_SIZE - total_processed)) size_t read_size =
? (BUF_SIZE - total_processed) (available_len > (BUF_SIZE - total_processed)) ? (BUF_SIZE - total_processed) : available_len;
: available_len;
int bytes_read = uart_read_bytes(UART_NUM, data_buf + total_processed, int bytes_read = uart_read_bytes(UART_NUM, data_buf + total_processed, read_size, READ_TIMEOUT);
read_size, READ_TIMEOUT);
if (bytes_read <= 0) if (bytes_read <= 0)
{ {
@@ -221,9 +219,8 @@ static void uart_polling_task(void* arg)
while (offset < total_processed) while (offset < total_processed)
{ {
const size_t chunk_size = (total_processed - offset > CHUNK_SIZE) const size_t chunk_size =
? CHUNK_SIZE (total_processed - offset > CHUNK_SIZE) ? CHUNK_SIZE : (total_processed - offset);
: (total_processed - offset);
struct ws_message msg; struct ws_message msg;
msg.type = WS_MSG_UART; 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_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)); ESP_ERROR_CHECK(uart_driver_install(UART_NUM, BUF_SIZE * 2, BUF_SIZE * 2, 0, NULL, 0));
httpd_uri_t ws = { httpd_uri_t ws = {.uri = "/ws", .method = HTTP_GET, .handler = ws_handler, .user_ctx = NULL, .is_websocket = true};
.uri = "/ws",
.method = HTTP_GET,
.handler = ws_handler,
.user_ctx = NULL,
.is_websocket = true
};
httpd_register_uri_handler(server, &ws); httpd_register_uri_handler(server, &ws);
client_fd_mutex = xSemaphoreCreateMutex(); client_fd_mutex = xSemaphoreCreateMutex();
@@ -361,7 +352,4 @@ void push_data_to_ws(cJSON* data)
} }
} }
esp_err_t change_baud_rate(int baud_rate) esp_err_t change_baud_rate(int baud_rate) { return uart_set_baudrate(UART_NUM, baud_rate); }
{
return uart_set_baudrate(UART_NUM, baud_rate);
}

View File

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

View File

@@ -2,16 +2,16 @@
// Created by shinys on 25. 9. 1. // Created by shinys on 25. 9. 1.
// //
#include "priv_wifi.h"
#include <string.h> #include <string.h>
#include "esp_wifi.h"
#include "esp_log.h" #include "esp_log.h"
#include "nconfig.h"
#include "esp_netif.h" #include "esp_netif.h"
#include "wifi.h" #include "esp_wifi.h"
#include "lwip/ip4_addr.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_SSID "odroid-pm"
#define DEFAULT_AP_PASS "powermate" #define DEFAULT_AP_PASS "powermate"
@@ -24,9 +24,10 @@ static const char *TAG = "AP";
void wifi_init_ap(void) void wifi_init_ap(void)
{ {
// Get the network interface handle for the AP // 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_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 esp_netif_dhcps_stop(p_netif_ap); // Stop DHCP server to apply new IP settings
@@ -41,37 +42,46 @@ void wifi_init_ap(void)
// Configure Wi-Fi AP settings // Configure Wi-Fi AP settings
wifi_config_t wifi_config = { wifi_config_t wifi_config = {
.ap = { .ap =
.channel = AP_CHANNEL, {
.max_connection = AP_MAX_CONN, .channel = AP_CHANNEL,
.authmode = WIFI_AUTH_WPA2_PSK, .max_connection = AP_MAX_CONN,
.pmf_cfg = { .authmode = WIFI_AUTH_WPA2_PSK,
.required = false, .pmf_cfg =
{
.required = false,
},
}, },
},
}; };
// Read SSID and password from NVS (nconfig) // Read SSID and password from NVS (nconfig)
size_t len; 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)); 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); 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)); 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); strcpy((char*)wifi_config.ap.password, DEFAULT_AP_PASS);
} }
// If password is not set, use open authentication // 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; wifi_config.ap.authmode = WIFI_AUTH_OPEN;
} }
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config)); 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", ESP_LOGI(TAG, "wifi_init_ap finished. SSID: %s, Password: %s, Channel: %d", (char*)wifi_config.ap.ssid, "********",
(char*)wifi_config.ap.ssid, "********", AP_CHANNEL); AP_CHANNEL);
} }

View File

@@ -9,4 +9,4 @@ void wifi_init_sta(void);
void wifi_init_ap(void); void wifi_init_ap(void);
void initialize_sntp(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. // Created by shinys on 25. 9. 1.
// //
#include "priv_wifi.h"
#include <string.h> #include <string.h>
#include "esp_wifi.h"
#include "esp_log.h" #include "esp_log.h"
#include "nconfig.h"
#include "esp_netif.h" #include "esp_netif.h"
#include "esp_wifi.h"
#include "lwip/inet.h" #include "lwip/inet.h"
#include "nconfig.h"
#include "priv_wifi.h"
#include "wifi.h" #include "wifi.h"
static const char* TAG = "STA"; static const char* TAG = "STA";

View File

@@ -2,16 +2,16 @@
// Created by shinys on 25. 9. 1. // Created by shinys on 25. 9. 1.
// //
#include "priv_wifi.h"
#include <string.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_event.h"
#include "esp_log.h" #include "esp_log.h"
#include "esp_mac.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 "nconfig.h"
#include "priv_wifi.h"
#include "wifi.h" #include "wifi.h"
@@ -19,18 +19,17 @@
static const char* TAG = "WIFI"; static const char* TAG = "WIFI";
static void wifi_event_handler(void* arg, esp_event_base_t event_base, static void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
int32_t event_id, void* event_data)
{ {
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_STACONNECTED) if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_STACONNECTED)
{ {
wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*)event_data; 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) 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; 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) 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. // Created by shinys on 25. 9. 1.
// //
#include "priv_wifi.h"
#include <time.h> #include <time.h>
#include "esp_log.h" #include "esp_log.h"
#include "esp_sntp.h" #include "esp_sntp.h"
#include "priv_wifi.h"
#include "wifi.h" #include "wifi.h"
static const char* TAG = "WIFI_HELPER"; static const char* TAG = "WIFI_HELPER";
@@ -55,39 +55,72 @@ const char* wifi_reason_str(wifi_err_reason_t reason)
{ {
switch (reason) switch (reason)
{ {
case WIFI_REASON_UNSPECIFIED: return "UNSPECIFIED"; case WIFI_REASON_UNSPECIFIED:
case WIFI_REASON_AUTH_EXPIRE: return "AUTH_EXPIRE"; return "UNSPECIFIED";
case WIFI_REASON_AUTH_LEAVE: return "AUTH_LEAVE"; case WIFI_REASON_AUTH_EXPIRE:
case WIFI_REASON_ASSOC_EXPIRE: return "ASSOC_EXPIRE"; return "AUTH_EXPIRE";
case WIFI_REASON_ASSOC_TOOMANY: return "ASSOC_TOOMANY"; case WIFI_REASON_AUTH_LEAVE:
case WIFI_REASON_NOT_AUTHED: return "NOT_AUTHED"; return "AUTH_LEAVE";
case WIFI_REASON_NOT_ASSOCED: return "NOT_ASSOCED"; case WIFI_REASON_ASSOC_EXPIRE:
case WIFI_REASON_ASSOC_LEAVE: return "ASSOC_LEAVE"; return "ASSOC_EXPIRE";
case WIFI_REASON_ASSOC_NOT_AUTHED: return "ASSOC_NOT_AUTHED"; case WIFI_REASON_ASSOC_TOOMANY:
case WIFI_REASON_DISASSOC_PWRCAP_BAD: return "DISASSOC_PWRCAP_BAD"; return "ASSOC_TOOMANY";
case WIFI_REASON_DISASSOC_SUPCHAN_BAD: return "DISASSOC_SUPCHAN_BAD"; case WIFI_REASON_NOT_AUTHED:
case WIFI_REASON_IE_INVALID: return "IE_INVALID"; return "NOT_AUTHED";
case WIFI_REASON_MIC_FAILURE: return "MIC_FAILURE"; case WIFI_REASON_NOT_ASSOCED:
case WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT: return "4WAY_HANDSHAKE_TIMEOUT"; return "NOT_ASSOCED";
case WIFI_REASON_GROUP_KEY_UPDATE_TIMEOUT: return "GROUP_KEY_UPDATE_TIMEOUT"; case WIFI_REASON_ASSOC_LEAVE:
case WIFI_REASON_IE_IN_4WAY_DIFFERS: return "IE_IN_4WAY_DIFFERS"; return "ASSOC_LEAVE";
case WIFI_REASON_GROUP_CIPHER_INVALID: return "GROUP_CIPHER_INVALID"; case WIFI_REASON_ASSOC_NOT_AUTHED:
case WIFI_REASON_PAIRWISE_CIPHER_INVALID: return "PAIRWISE_CIPHER_INVALID"; return "ASSOC_NOT_AUTHED";
case WIFI_REASON_AKMP_INVALID: return "AKMP_INVALID"; case WIFI_REASON_DISASSOC_PWRCAP_BAD:
case WIFI_REASON_UNSUPP_RSN_IE_VERSION: return "UNSUPP_RSN_IE_VERSION"; return "DISASSOC_PWRCAP_BAD";
case WIFI_REASON_INVALID_RSN_IE_CAP: return "INVALID_RSN_IE_CAP"; case WIFI_REASON_DISASSOC_SUPCHAN_BAD:
case WIFI_REASON_802_1X_AUTH_FAILED: return "802_1X_AUTH_FAILED"; return "DISASSOC_SUPCHAN_BAD";
case WIFI_REASON_CIPHER_SUITE_REJECTED: return "CIPHER_SUITE_REJECTED"; case WIFI_REASON_IE_INVALID:
case WIFI_REASON_INVALID_PMKID: return "INVALID_PMKID"; return "IE_INVALID";
case WIFI_REASON_BEACON_TIMEOUT: return "BEACON_TIMEOUT"; case WIFI_REASON_MIC_FAILURE:
case WIFI_REASON_NO_AP_FOUND: return "NO_AP_FOUND"; return "MIC_FAILURE";
case WIFI_REASON_AUTH_FAIL: return "AUTH_FAIL"; case WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT:
case WIFI_REASON_ASSOC_FAIL: return "ASSOC_FAIL"; return "4WAY_HANDSHAKE_TIMEOUT";
case WIFI_REASON_HANDSHAKE_TIMEOUT: return "HANDSHAKE_TIMEOUT"; case WIFI_REASON_GROUP_KEY_UPDATE_TIMEOUT:
case WIFI_REASON_CONNECTION_FAIL: return "CONNECTION_FAIL"; return "GROUP_KEY_UPDATE_TIMEOUT";
case WIFI_REASON_AP_TSF_RESET: return "AP_TSF_RESET"; case WIFI_REASON_IE_IN_4WAY_DIFFERS:
case WIFI_REASON_ROAMING: return "ROAMING"; return "IE_IN_4WAY_DIFFERS";
default: return "UNKNOWN"; 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";
} }
} }