diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..2987a7f --- /dev/null +++ b/.clang-format @@ -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 +... diff --git a/main/app/odroid-power-mate.c b/main/app/odroid-power-mate.c index 575eea0..0206e65 100644 --- a/main/app/odroid-power-mate.c +++ b/main/app/odroid-power-mate.c @@ -1,20 +1,21 @@ #include #include -#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); diff --git a/main/include/indicator.h b/main/include/indicator.h index 76ec9e4..40e9802 100644 --- a/main/include/indicator.h +++ b/main/include/indicator.h @@ -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 \ No newline at end of file +#endif // LED_H diff --git a/main/include/nconfig.h b/main/include/nconfig.h index 63a2d87..b1db042 100644 --- a/main/include/nconfig.h +++ b/main/include/nconfig.h @@ -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 \ No newline at end of file +#endif // NCONFIG_H diff --git a/main/include/system.h b/main/include/system.h index 6118c80..e61057c 100644 --- a/main/include/system.h +++ b/main/include/system.h @@ -9,4 +9,4 @@ void start_reboot_timer(int sec); void stop_reboot_timer(); void start_webserver(); -#endif //SYSTEM_H \ No newline at end of file +#endif // SYSTEM_H diff --git a/main/include/wifi.h b/main/include/wifi.h index e06df5d..6c810ad 100644 --- a/main/include/wifi.h +++ b/main/include/wifi.h @@ -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 diff --git a/main/indicator/indicator.c b/main/indicator/indicator.c index 4e4a318..692e913 100644 --- a/main/indicator/indicator.c +++ b/main/indicator/indicator.c @@ -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}; diff --git a/main/nconfig/nconfig.c b/main/nconfig/nconfig.c index 6f80c85..597ccea 100644 --- a/main/nconfig/nconfig.c +++ b/main/nconfig/nconfig.c @@ -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) { @@ -93,4 +79,4 @@ esp_err_t nconfig_read(enum nconfig_type type, char* data, size_t len) esp_err_t nconfig_read_bool(enum nconfig_type type, char* data, size_t len) { return nvs_get_str(handle, keys[type], data, &len); -} \ No newline at end of file +} diff --git a/main/service/control.c b/main/service/control.c index d5ba5b5..429a5f9 100644 --- a/main/service/control.c +++ b/main/service/control.c @@ -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); } diff --git a/main/service/datalog.c b/main/service/datalog.c index 1885e8d..d626c12 100644 --- a/main/service/datalog.c +++ b/main/service/datalog.c @@ -1,7 +1,7 @@ #include "datalog.h" #include -#include #include +#include #include #include #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; } diff --git a/main/service/datalog.h b/main/service/datalog.h index 1117c4b..71bfded 100644 --- a/main/service/datalog.h +++ b/main/service/datalog.h @@ -5,7 +5,8 @@ #define NUM_CHANNELS 3 -typedef struct { +typedef struct +{ float voltage; float current; float power; diff --git a/main/service/monitor.c b/main/service/monitor.c index c37f522..d281285 100644 --- a/main/service/monitor.c +++ b/main/service/monitor.c @@ -5,47 +5,39 @@ #include "monitor.h" #include -#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 diff --git a/main/service/monitor.h b/main/service/monitor.h index 6bfc42d..138b165 100644 --- a/main/service/monitor.h +++ b/main/service/monitor.h @@ -21,4 +21,4 @@ typedef struct void init_status_monitor(); -#endif //ODROID_REMOTE_HTTP_MONITOR_H +#endif // ODROID_REMOTE_HTTP_MONITOR_H diff --git a/main/service/setting.c b/main/service/setting.c index 22879b2..0b233b7 100644 --- a/main/service/setting.c +++ b/main/service/setting.c @@ -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); } diff --git a/main/service/sw.c b/main/service/sw.c index 0d57dba..944a8f7 100644 --- a/main/service/sw.c +++ b/main/service/sw.c @@ -4,17 +4,17 @@ #include "sw.h" -#include -#include #include #include #include +#include +#include #include +#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; } diff --git a/main/service/sw.h b/main/service/sw.h index 973f0bb..37e4695 100644 --- a/main/service/sw.h +++ b/main/service/sw.h @@ -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 diff --git a/main/service/webserver.c b/main/service/webserver.c index 87ce2aa..0518b56 100644 --- a/main/service/webserver.c +++ b/main/service/webserver.c @@ -1,16 +1,16 @@ #include "webserver.h" #include #include +#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); diff --git a/main/service/webserver.h b/main/service/webserver.h index a796980..cac3025 100644 --- a/main/service/webserver.h +++ b/main/service/webserver.h @@ -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 diff --git a/main/service/ws.c b/main/service/ws.c index 3c3978b..76969db 100644 --- a/main/service/ws.c +++ b/main/service/ws.c @@ -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); } diff --git a/main/system/system.c b/main/system/system.c index 934adf4..179e391 100644 --- a/main/system/system.c +++ b/main/system/system.c @@ -6,9 +6,9 @@ #include #include +#include #include "esp_http_server.h" #include "esp_system.h" -#include 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); } diff --git a/main/wifi/ap.c b/main/wifi/ap.c index 7ffa8f5..2fbf515 100644 --- a/main/wifi/ap.c +++ b/main/wifi/ap.c @@ -2,16 +2,16 @@ // Created by shinys on 25. 9. 1. // -#include "priv_wifi.h" #include -#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,37 +42,46 @@ void wifi_init_ap(void) // Configure Wi-Fi AP settings wifi_config_t wifi_config = { - .ap = { - .channel = AP_CHANNEL, - .max_connection = AP_MAX_CONN, - .authmode = WIFI_AUTH_WPA2_PSK, - .pmf_cfg = { - .required = false, + .ap = + { + .channel = AP_CHANNEL, + .max_connection = AP_MAX_CONN, + .authmode = WIFI_AUTH_WPA2_PSK, + .pmf_cfg = + { + .required = false, + }, }, - }, }; // 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); } diff --git a/main/wifi/priv_wifi.h b/main/wifi/priv_wifi.h index c15b36f..1ff4bb3 100644 --- a/main/wifi/priv_wifi.h +++ b/main/wifi/priv_wifi.h @@ -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 \ No newline at end of file +#endif // ODROID_POWER_MATE_PRIV_WIFI_H diff --git a/main/wifi/sta.c b/main/wifi/sta.c index 8b2e08e..79cdd1a 100644 --- a/main/wifi/sta.c +++ b/main/wifi/sta.c @@ -2,13 +2,13 @@ // Created by shinys on 25. 9. 1. // -#include "priv_wifi.h" #include -#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"; diff --git a/main/wifi/wifi.c b/main/wifi/wifi.c index 7e0846f..b8ba2c6 100644 --- a/main/wifi/wifi.c +++ b/main/wifi/wifi.c @@ -2,16 +2,16 @@ // Created by shinys on 25. 9. 1. // -#include "priv_wifi.h" #include -#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) { diff --git a/main/wifi/wifi_helper.c b/main/wifi/wifi_helper.c index 780c928..95d3c92 100644 --- a/main/wifi/wifi_helper.c +++ b/main/wifi/wifi_helper.c @@ -2,10 +2,10 @@ // Created by shinys on 25. 9. 1. // -#include "priv_wifi.h" #include #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"; } }