Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions core/src/drivers/plugin_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...);
Expand Down
4 changes: 2 additions & 2 deletions core/src/plc_app/scan_cycle_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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();

Expand Down
4 changes: 2 additions & 2 deletions core/src/plc_app/scan_cycle_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion core/src/plc_app/unix_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions core/src/plc_app/unix_socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
92 changes: 46 additions & 46 deletions core/src/plc_app/utils/log.c
Original file line number Diff line number Diff line change
@@ -1,45 +1,43 @@
#include "log.h"
#include <errno.h>
#include <pthread.h>
#include <signal.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdatomic.h>
#include <signal.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <errno.h>
#include <time.h>
#include <unistd.h>
#include <stdbool.h>

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);
Expand Down Expand Up @@ -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';
Expand All @@ -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);
Expand All @@ -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");
Expand All @@ -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;
Expand All @@ -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");

Expand All @@ -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();
Expand Down Expand Up @@ -224,31 +224,31 @@ 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);
log_write(LOG_LEVEL_INFO, fmt, args);
va_end(args);
}

void log_debug(const char *fmt, ...)
void log_debug(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
log_write(LOG_LEVEL_DEBUG, fmt, args);
va_end(args);
}

void log_warn(const char *fmt, ...)
void log_warn(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
log_write(LOG_LEVEL_WARN, fmt, args);
va_end(args);
}

void log_error(const char *fmt, ...)
void log_error(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
Expand Down
21 changes: 12 additions & 9 deletions core/src/plc_app/utils/watchdog.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
#include <time.h>
#include <unistd.h>

#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;
Expand All @@ -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);
}

Expand All @@ -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;
Expand Down
6 changes: 2 additions & 4 deletions core/src/plc_app/utils/watchdog.h
Original file line number Diff line number Diff line change
@@ -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
#endif // WATCHDOG_H