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

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,47 +5,39 @@
#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 = {
.mode = true, // mode selection
.esht = true, // shunt enable
.ebus = true, // bus enable
.ch1 = true, // channel 1 enable
.ch2 = true, // channel 2 enable
.ch3 = true, // channel 3 enable
.avg = INA3221_AVG_64, // 64 samples average
.vbus = INA3221_CT_2116, // 2ms by channel (bus)
.vsht = INA3221_CT_2116, // 2ms by channel (shunt)
},
.config =
{
.mode = true, // mode selection
.esht = true, // shunt enable
.ebus = true, // bus enable
.ch1 = true, // channel 1 enable
.ch2 = true, // channel 2 enable
.ch3 = true, // channel 3 enable
.avg = INA3221_AVG_64, // 64 samples average
.vbus = INA3221_CT_2116, // 2ms by channel (bus)
.vsht = INA3221_CT_2116, // 2ms by channel (shunt)
},
};
// Timer callback function to read sensor data

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); }