diff --git a/core/src/drivers/plugin_driver.h b/core/src/drivers/plugin_driver.h index 5bb70109..7f271043 100644 --- a/core/src/drivers/plugin_driver.h +++ b/core/src/drivers/plugin_driver.h @@ -17,11 +17,11 @@ typedef enum } plugin_type_t; typedef int (*plugin_init_func_t)(void *); -typedef void (*plugin_start_loop_func_t)(); -typedef void (*plugin_stop_loop_func_t)(); -typedef void (*plugin_cycle_start_func_t)(); -typedef void (*plugin_cycle_end_func_t)(); -typedef void (*plugin_cleanup_func_t)(); +typedef void (*plugin_start_loop_func_t)(void); +typedef void (*plugin_stop_loop_func_t)(void); +typedef void (*plugin_cycle_start_func_t)(void); +typedef void (*plugin_cycle_end_func_t)(void); +typedef void (*plugin_cleanup_func_t)(void); // Logging function pointer types typedef void (*plugin_log_info_func_t)(const char *fmt, ...); diff --git a/core/src/plc_app/scan_cycle_manager.c b/core/src/plc_app/scan_cycle_manager.c index 78ef6534..55b92ee0 100644 --- a/core/src/plc_app/scan_cycle_manager.c +++ b/core/src/plc_app/scan_cycle_manager.c @@ -27,7 +27,7 @@ static uint64_t ts_now_us(void) return (uint64_t)ts.tv_sec * 1000000ull + ts.tv_nsec / 1000; } -void scan_cycle_time_start() +void scan_cycle_time_start(void) { uint64_t now_us = ts_now_us(); @@ -78,7 +78,7 @@ void scan_cycle_time_start() pthread_mutex_unlock(&stats_mutex); } -void scan_cycle_time_end() +void scan_cycle_time_end(void) { uint64_t now_us = ts_now_us(); diff --git a/core/src/plc_app/scan_cycle_manager.h b/core/src/plc_app/scan_cycle_manager.h index c624fe2e..7b62fdc9 100644 --- a/core/src/plc_app/scan_cycle_manager.h +++ b/core/src/plc_app/scan_cycle_manager.h @@ -22,8 +22,8 @@ typedef struct int64_t overruns; } plc_timing_stats_t; -void scan_cycle_time_start(); -void scan_cycle_time_end(); +void scan_cycle_time_start(void); +void scan_cycle_time_end(void); // Thread-safe function to get a snapshot of timing stats // Returns true if stats are valid (scan_count > 0), false otherwise diff --git a/core/src/plc_app/unix_socket.c b/core/src/plc_app/unix_socket.c index 7c8663b5..c74cb085 100644 --- a/core/src/plc_app/unix_socket.c +++ b/core/src/plc_app/unix_socket.c @@ -215,7 +215,7 @@ void close_unix_socket(int server_fd) } } -int setup_unix_socket() +int setup_unix_socket(void) { int server_fd; struct sockaddr_un address; diff --git a/core/src/plc_app/unix_socket.h b/core/src/plc_app/unix_socket.h index fc0ec452..d0df1f1a 100644 --- a/core/src/plc_app/unix_socket.h +++ b/core/src/plc_app/unix_socket.h @@ -6,8 +6,8 @@ #define MAX_RESPONSE_SIZE 16384 #define MAX_CLIENTS 1 -int setup_unix_socket(); -void close_unix_socket(); +int setup_unix_socket(void); +void close_unix_socket(int server_fd); void *unix_socket_thread(void *arg); #endif // UNIX_SOCKET_H diff --git a/core/src/plc_app/utils/log.c b/core/src/plc_app/utils/log.c index 099454dc..08e906e3 100644 --- a/core/src/plc_app/utils/log.c +++ b/core/src/plc_app/utils/log.c @@ -1,45 +1,43 @@ #include "log.h" +#include #include +#include #include -#include -#include -#include -#include -#include -#include -#include #include -#include +#include +#include +#include #include #include -#include +#include #include -#include -static LogLevel current_level = LOG_LEVEL_INFO; +static LogLevel current_level = LOG_LEVEL_INFO; static pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER; -int socket_fd = -1; -bool print_logs = false; +int socket_fd = -1; +bool print_logs = false; extern volatile sig_atomic_t keep_running; -void log_set_level(LogLevel level) { current_level = level; } +void log_set_level(LogLevel level) +{ + current_level = level; +} // Create circular buffer for unsent logs #define LOG_BUFFER_SIZE 1024 #define LOG_MESSAGE_SIZE 2048 char log_buffer[LOG_BUFFER_SIZE][LOG_MESSAGE_SIZE]; int log_buffer_start = 0; -int log_buffer_end = 0; - +int log_buffer_end = 0; -void *log_thread_management(void *arg) +void *log_thread_management(void *arg) { char *unix_socket_path = (char *)arg; - while(keep_running) + while (keep_running) { - if (socket_fd < 0) + if (socket_fd < 0) { struct sockaddr_un addr; socket_fd = socket(AF_UNIX, SOCK_STREAM, 0); @@ -72,7 +70,7 @@ void *log_thread_management(void *arg) return NULL; } -void store_on_buffer(const char *msg) +static void store_on_buffer(const char *msg) { strncpy(log_buffer[log_buffer_end], msg, sizeof(log_buffer[log_buffer_end]) - 1); log_buffer[log_buffer_end][sizeof(log_buffer[log_buffer_end]) - 1] = '\0'; @@ -85,19 +83,19 @@ void store_on_buffer(const char *msg) } } -char *retrieve_from_buffer() +static char *retrieve_from_buffer(void) { if (log_buffer_start == log_buffer_end) { return NULL; // Buffer is empty } - char *msg = log_buffer[log_buffer_start]; + char *msg = log_buffer[log_buffer_start]; log_buffer_start = (log_buffer_start + 1) % LOG_BUFFER_SIZE; return msg; } -int log_init(char *unix_socket_path) +int log_init(char *unix_socket_path) { // Create a copy of the socket path in the heap char *path_copy = malloc(strlen(unix_socket_path) + 1); @@ -110,7 +108,7 @@ int log_init(char *unix_socket_path) // Create the logging thread pthread_t thread_id; - if (pthread_create(&thread_id, NULL, log_thread_management, path_copy) != 0) + if (pthread_create(&thread_id, NULL, log_thread_management, path_copy) != 0) { free(path_copy); perror("Failed to create log thread"); @@ -120,31 +118,30 @@ int log_init(char *unix_socket_path) return 0; // Success } - -static const char *level_to_str(LogLevel level) +static const char *level_to_str(LogLevel level) { - switch (level) + switch (level) { - case LOG_LEVEL_DEBUG: - return "DEBUG"; - case LOG_LEVEL_INFO: - return "INFO"; - case LOG_LEVEL_WARN: - return "WARN"; - case LOG_LEVEL_ERROR: - return "ERROR"; - default: - return "UNKNOWN"; + case LOG_LEVEL_DEBUG: + return "DEBUG"; + case LOG_LEVEL_INFO: + return "INFO"; + case LOG_LEVEL_WARN: + return "WARN"; + case LOG_LEVEL_ERROR: + return "ERROR"; + default: + return "UNKNOWN"; } } -static void log_write(LogLevel level, const char *fmt, va_list args) +static void log_write(LogLevel level, const char *fmt, va_list args) { if (level < current_level) { return; } - + // Capture time for timestamp time_t now = time(NULL); struct tm t; @@ -163,7 +160,8 @@ static void log_write(LogLevel level, const char *fmt, va_list args) char time_buf[20]; strftime(time_buf, sizeof(time_buf), "%Y-%m-%d %H:%M:%S", &t); - int n = snprintf(stdout_msg, sizeof(stdout_msg), "[%s] [%s] ", time_buf, level_to_str(level)); + int n = + snprintf(stdout_msg, sizeof(stdout_msg), "[%s] [%s] ", time_buf, level_to_str(level)); n += vsnprintf(stdout_msg + n, sizeof(stdout_msg) - n, fmt, args_copy); snprintf(stdout_msg + n, sizeof(stdout_msg) - n, "\n"); @@ -172,13 +170,15 @@ static void log_write(LogLevel level, const char *fmt, va_list args) } // Format the log message in JSON format - int n = snprintf(log_msg, sizeof(log_msg), "{\"timestamp\":\"%ld\",\"level\":\"%s\",\"message\":\"", (long)now, level_to_str(level)); + int n = + snprintf(log_msg, sizeof(log_msg), "{\"timestamp\":\"%ld\",\"level\":\"%s\",\"message\":\"", + (long)now, level_to_str(level)); n += vsnprintf(log_msg + n, sizeof(log_msg) - n, fmt, args); snprintf(log_msg + n, sizeof(log_msg) - n, "\"}\n"); // Send to unix socket if connected pthread_mutex_lock(&log_mutex); - if (socket_fd >= 0) + if (socket_fd >= 0) { // Send any buffered messages first char *buffered_msg = retrieve_from_buffer(); @@ -224,7 +224,7 @@ static void log_write(LogLevel level, const char *fmt, va_list args) pthread_mutex_unlock(&log_mutex); } -void log_info(const char *fmt, ...) +void log_info(const char *fmt, ...) { va_list args; va_start(args, fmt); @@ -232,7 +232,7 @@ void log_info(const char *fmt, ...) va_end(args); } -void log_debug(const char *fmt, ...) +void log_debug(const char *fmt, ...) { va_list args; va_start(args, fmt); @@ -240,7 +240,7 @@ void log_debug(const char *fmt, ...) va_end(args); } -void log_warn(const char *fmt, ...) +void log_warn(const char *fmt, ...) { va_list args; va_start(args, fmt); @@ -248,7 +248,7 @@ void log_warn(const char *fmt, ...) va_end(args); } -void log_error(const char *fmt, ...) +void log_error(const char *fmt, ...) { va_list args; va_start(args, fmt); diff --git a/core/src/plc_app/utils/watchdog.c b/core/src/plc_app/utils/watchdog.c index cd630c1c..ee723965 100644 --- a/core/src/plc_app/utils/watchdog.c +++ b/core/src/plc_app/utils/watchdog.c @@ -5,10 +5,10 @@ #include #include -#include "watchdog.h" +#include "../plc_state_manager.h" #include "log.h" #include "utils.h" -#include "../plc_state_manager.h" +#include "watchdog.h" atomic_long plc_heartbeat; extern PLCState plc_state; @@ -18,19 +18,22 @@ void *watchdog_thread(void *arg) (void)arg; long last = atomic_load(&plc_heartbeat); - while (1) + while (1) { sleep(2); // Watch every 2 seconds - if (plc_get_state() != PLC_STATE_RUNNING) + if (plc_get_state() != PLC_STATE_RUNNING) { continue; // Only monitor when PLC is running } - + long now = atomic_load(&plc_heartbeat); - if (now == last) + if (now == last) { - fprintf(stderr, "[Watchdog] No heartbeat! PLC unresponsive.\n"); // Use stderr to ensure visibility and avoid lockups in log system + fprintf( + stderr, + "[Watchdog] No heartbeat! PLC unresponsive.\n"); // Use stderr to ensure visibility + // and avoid lockups in log system exit(EXIT_FAILURE); } @@ -40,10 +43,10 @@ void *watchdog_thread(void *arg) return NULL; } -int watchdog_init() +int watchdog_init(void) { pthread_t wd_thread; - if (pthread_create(&wd_thread, NULL, watchdog_thread, NULL) != 0) + if (pthread_create(&wd_thread, NULL, watchdog_thread, NULL) != 0) { log_error("Failed to create watchdog thread"); return -1; diff --git a/core/src/plc_app/utils/watchdog.h b/core/src/plc_app/utils/watchdog.h index 238423ba..065c6ccd 100644 --- a/core/src/plc_app/utils/watchdog.h +++ b/core/src/plc_app/utils/watchdog.h @@ -1,12 +1,10 @@ #ifndef WATCHDOG_H #define WATCHDOG_H - /** * @brief Initialize the watchdog * @return int 0 on success, -1 on failure */ -int watchdog_init(); - +int watchdog_init(void); -#endif // WATCHDOG_H \ No newline at end of file +#endif // WATCHDOG_H