Compare commits
2 Commits
c33ad367f3
...
0b4fb09d97
| Author | SHA1 | Date | |
|---|---|---|---|
|
0b4fb09d97
|
|||
|
8abf191502
|
236
main.c
236
main.c
@ -59,6 +59,10 @@
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <poll.h>
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
@ -169,6 +173,9 @@ typedef struct main_state_typedef{
|
||||
uint32_t run_I; // № current run
|
||||
uint32_t runs_N; // total number of runs
|
||||
char data_path[200]; // base directory for data files
|
||||
char pipe_path[200]; // path to named pipe (FIFO) for streaming data
|
||||
int pipe_fd; // file descriptor for the pipe
|
||||
int save_to_files; // flag: save data to files (0=false, 1=true)
|
||||
} main_state;
|
||||
|
||||
static void main_state_set_defaults(main_state* st) {
|
||||
@ -180,6 +187,10 @@ static void main_state_set_defaults(main_state* st) {
|
||||
st->runs_N = 1;
|
||||
strncpy(st->data_path, "data", sizeof(st->data_path));
|
||||
st->data_path[sizeof(st->data_path)-1] = '\0';
|
||||
strncpy(st->pipe_path, "/tmp/radar_data_pipe", sizeof(st->pipe_path));
|
||||
st->pipe_path[sizeof(st->pipe_path)-1] = '\0';
|
||||
st->pipe_fd = -1;
|
||||
st->save_to_files = 0; // default: false
|
||||
}
|
||||
|
||||
static char* f_trim(char* s) {
|
||||
@ -254,6 +265,15 @@ static void parse_cmd_file(const char* filename, main_state* st) {
|
||||
} else if (strcmp(field, "data_path") == 0) {
|
||||
strncpy(st->data_path, value, sizeof(st->data_path));
|
||||
st->data_path[sizeof(st->data_path)-1] = '\0';
|
||||
} else if (strcmp(field, "pipe_path") == 0) {
|
||||
strncpy(st->pipe_path, value, sizeof(st->pipe_path));
|
||||
st->pipe_path[sizeof(st->pipe_path)-1] = '\0';
|
||||
} else if (strcmp(field, "save_to_files") == 0) {
|
||||
if (strcmp(value, "true") == 0 || strcmp(value, "1") == 0) {
|
||||
st->save_to_files = 1;
|
||||
} else if (strcmp(value, "false") == 0 || strcmp(value, "0") == 0) {
|
||||
st->save_to_files = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
fclose(f);
|
||||
@ -367,6 +387,7 @@ static t_x502_hnd f_dev_select_open(int argc, char** argv) {
|
||||
fnd_devcnt = f_get_all_devrec(&devrec_list, ip_dev_list, ip_cnt);
|
||||
|
||||
if (fnd_devcnt == 0) {
|
||||
|
||||
printf("Не найдено ни одного модуля\n");
|
||||
} else {
|
||||
// выводим информацию по списку модулей //
|
||||
@ -694,9 +715,159 @@ void insert_marker_to_file(char* logfilename, char* marker_text){
|
||||
fclose(logfile_ptr);
|
||||
}
|
||||
|
||||
#ifndef _WIN32
|
||||
// Function to check if pipe reader is present and ready
|
||||
// Returns: 1 if ready to write, 0 if should wait, -1 on error
|
||||
int check_pipe_ready(int pipe_fd) {
|
||||
if (pipe_fd < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct pollfd pfd;
|
||||
pfd.fd = pipe_fd;
|
||||
pfd.events = POLLOUT;
|
||||
pfd.revents = 0;
|
||||
|
||||
void receive_to_file(t_x502_hnd hnd, char* logfilename, uint32_t* inp_buff, uint32_t max_total_words, uint32_t timeout){
|
||||
// Check with 0 timeout (non-blocking check)
|
||||
int ret = poll(&pfd, 1, 0);
|
||||
|
||||
if (ret < 0) {
|
||||
fprintf(stderr, "Error polling pipe: %s\n", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (ret == 0) {
|
||||
// Timeout - pipe buffer is full, reader is slow or absent
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Check for errors
|
||||
if (pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
|
||||
if (pfd.revents & POLLERR) {
|
||||
fprintf(stderr, "Pipe error detected\n");
|
||||
}
|
||||
if (pfd.revents & POLLHUP) {
|
||||
fprintf(stderr, "Pipe hangup - reader disconnected\n");
|
||||
}
|
||||
if (pfd.revents & POLLNVAL) {
|
||||
fprintf(stderr, "Invalid pipe fd\n");
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
// POLLOUT is set - ready to write
|
||||
if (pfd.revents & POLLOUT) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Function to wait for pipe to become ready
|
||||
// Returns: 1 if ready, 0 if interrupted, -1 on error
|
||||
int wait_for_pipe_reader(int pipe_fd) {
|
||||
if (pipe_fd < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("Waiting for pipe reader to become ready...\n");
|
||||
|
||||
struct pollfd pfd;
|
||||
pfd.fd = pipe_fd;
|
||||
pfd.events = POLLOUT;
|
||||
|
||||
while (1) {
|
||||
pfd.revents = 0;
|
||||
|
||||
// Wait with 1 second timeout
|
||||
int ret = poll(&pfd, 1, 1000);
|
||||
|
||||
if (ret < 0) {
|
||||
if (errno == EINTR) {
|
||||
// Interrupted by signal, continue
|
||||
continue;
|
||||
}
|
||||
fprintf(stderr, "Error waiting for pipe: %s\n", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (ret == 0) {
|
||||
// Timeout - print status and continue waiting
|
||||
printf("Still waiting for pipe reader...\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check for errors
|
||||
if (pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
|
||||
fprintf(stderr, "Pipe error while waiting\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Ready to write
|
||||
if (pfd.revents & POLLOUT) {
|
||||
printf("Pipe reader is ready!\n");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Function to initialize named pipe (FIFO)
|
||||
int init_pipe(const char* pipe_path) {
|
||||
// Remove existing pipe if it exists
|
||||
unlink(pipe_path);
|
||||
|
||||
// Create named pipe with read/write permissions
|
||||
if (mkfifo(pipe_path, 0666) != 0) {
|
||||
if (errno != EEXIST) {
|
||||
fprintf(stderr, "Error creating pipe %s: %s\n", pipe_path, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
printf("Named pipe created: %s\n", pipe_path);
|
||||
|
||||
// Try to open in non-blocking mode first to check if reader is present
|
||||
int fd = open(pipe_path, O_WRONLY | O_NONBLOCK);
|
||||
if (fd < 0) {
|
||||
if (errno == ENXIO) {
|
||||
// No reader present, try blocking mode
|
||||
printf("No reader detected. Waiting for reader to connect...\n");
|
||||
printf("(You can start the reader program now, e.g.: ./pipe_reader_test.py)\n");
|
||||
|
||||
// Open in blocking mode - will wait for reader
|
||||
fd = open(pipe_path, O_WRONLY);
|
||||
if (fd < 0) {
|
||||
fprintf(stderr, "Error opening pipe for writing: %s\n", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
printf("Reader connected!\n");
|
||||
} else {
|
||||
fprintf(stderr, "Warning: Could not open pipe for writing: %s\n", strerror(errno));
|
||||
fprintf(stderr, "Pipe will be skipped. Data will only be saved to file.\n");
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
printf("Reader already connected!\n");
|
||||
// Set back to blocking mode for writes
|
||||
int flags = fcntl(fd, F_GETFL);
|
||||
fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
|
||||
}
|
||||
|
||||
printf("Pipe opened successfully (fd=%d)\n", fd);
|
||||
return fd;
|
||||
}
|
||||
|
||||
// Function to close pipe
|
||||
void close_pipe(int pipe_fd, const char* pipe_path) {
|
||||
if (pipe_fd >= 0) {
|
||||
close(pipe_fd);
|
||||
unlink(pipe_path);
|
||||
printf("Pipe closed and removed\n");
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
void receive_to_file(t_x502_hnd hnd, char* logfilename, uint32_t* inp_buff, uint32_t max_total_words, uint32_t timeout, int pipe_fd){
|
||||
|
||||
|
||||
|
||||
@ -723,6 +894,44 @@ void receive_to_file(t_x502_hnd hnd, char* logfilename, uint32_t* inp_buff, uint
|
||||
printf("received %ld words\n", received_words);
|
||||
fclose(logfile_ptr);
|
||||
rename(logfilename_tmp, logfilename);
|
||||
|
||||
#ifndef _WIN32
|
||||
// Write data to pipe if it's open
|
||||
if (pipe_fd >= 0) {
|
||||
// Check if pipe is ready before writing
|
||||
int pipe_status = check_pipe_ready(pipe_fd);
|
||||
|
||||
if (pipe_status == 0) {
|
||||
// Pipe not ready (buffer full or no reader) - wait for reader
|
||||
printf("Pipe not ready - waiting for reader to catch up...\n");
|
||||
int wait_result = wait_for_pipe_reader(pipe_fd);
|
||||
if (wait_result != 1) {
|
||||
fprintf(stderr, "Warning: Could not write to pipe\n");
|
||||
return;
|
||||
}
|
||||
} else if (pipe_status < 0) {
|
||||
fprintf(stderr, "Warning: Pipe error detected, skipping pipe write\n");
|
||||
return;
|
||||
}
|
||||
|
||||
// Pipe is ready, write data
|
||||
ssize_t bytes_written = write(pipe_fd, inp_buff, received_words * sizeof(uint32_t));
|
||||
if (bytes_written < 0) {
|
||||
if (errno == EPIPE) {
|
||||
fprintf(stderr, "Warning: Broken pipe (reader disconnected)\n");
|
||||
} else if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
||||
fprintf(stderr, "Warning: Pipe write would block\n");
|
||||
} else {
|
||||
fprintf(stderr, "Warning: Error writing to pipe: %s\n", strerror(errno));
|
||||
}
|
||||
} else if (bytes_written < (ssize_t)(received_words * sizeof(uint32_t))) {
|
||||
printf("Warning: Partial write to pipe: %zd of %zu bytes\n",
|
||||
bytes_written, received_words * sizeof(uint32_t));
|
||||
} else {
|
||||
printf("Written %zd bytes to pipe\n", bytes_written);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}else if (recv_Err_code == 0){
|
||||
printf("no data received. timeout\n");
|
||||
}else{
|
||||
@ -789,9 +998,9 @@ int main(int argc, char** argv) {
|
||||
main_state_set_defaults(&state);
|
||||
parse_cmd_file(cmd_filename, &state);
|
||||
// отладочный вывод принятых значений
|
||||
printf("Parsed state: run_mode=%d, BF_mode=%d, BlackFin_mode=%d, run_length=%u, runs_N=%u, run_I=%u, data_path=%s\n",
|
||||
printf("Parsed state: run_mode=%d, BF_mode=%d, BlackFin_mode=%d, run_length=%u, runs_N=%u, run_I=%u, data_path=%s, save_to_files=%d\n",
|
||||
(int)state.run_mode, (int)state.BF_mode, (int)state.BlackFin_mode,
|
||||
state.run_length, state.runs_N, state.run_I, state.data_path);
|
||||
state.run_length, state.runs_N, state.run_I, state.data_path, state.save_to_files);
|
||||
|
||||
/* Removed unused temporary buffers and FFT/LFSM placeholders */
|
||||
|
||||
@ -876,6 +1085,13 @@ int main(int argc, char** argv) {
|
||||
streams_start_Err = X502_StreamsStart(hnd);
|
||||
printf("Streams start err: %d \n", streams_start_Err);
|
||||
|
||||
#ifndef _WIN32
|
||||
// Initialize named pipe
|
||||
printf("Initializing named pipe: %s\n", state.pipe_path);
|
||||
state.pipe_fd = init_pipe(state.pipe_path);
|
||||
#else
|
||||
state.pipe_fd = -1;
|
||||
#endif
|
||||
|
||||
if (state.run_mode == RUN_MODE_FINITE){
|
||||
if (state.BF_mode == BF_MODE_TRANSPARENT){
|
||||
@ -910,7 +1126,7 @@ int main(int argc, char** argv) {
|
||||
//sprintf(&logfilename, "data/received_data_%ld.csv", seconds);
|
||||
snprintf(tmp_data_filename, sizeof(tmp_data_filename), "%s/received_data_%ld.%ld.csv", state.data_path, ts.tv_sec, ts.tv_nsec);
|
||||
printf("%u/%u dumping to file: %s\n", (unsigned)state.run_I, (unsigned)state.runs_N, tmp_data_filename);
|
||||
receive_to_file(hnd, tmp_data_filename, inp_buff, max_total_words, state.run_length);
|
||||
receive_to_file(hnd, tmp_data_filename, inp_buff, max_total_words, state.run_length, state.pipe_fd);
|
||||
if (runs_since_streams_clean >= 10){
|
||||
runs_since_streams_clean = 0;
|
||||
X502_StreamsStop(hnd);
|
||||
@ -919,6 +1135,9 @@ int main(int argc, char** argv) {
|
||||
state.run_I++;
|
||||
runs_since_streams_clean++; }
|
||||
free(inp_buff);
|
||||
#ifndef _WIN32
|
||||
close_pipe(state.pipe_fd, state.pipe_path);
|
||||
#endif
|
||||
X502_Close(hnd);
|
||||
// освобождаем описатель
|
||||
X502_Free(hnd);
|
||||
@ -959,7 +1178,7 @@ int main(int argc, char** argv) {
|
||||
//sprintf(&logfilename, "data/received_data_%ld.csv", seconds);
|
||||
snprintf(tmp_data_filename, sizeof(tmp_data_filename), "%s/received_data_%ld.%ld.csv", state.data_path, ts.tv_sec, ts.tv_nsec);
|
||||
printf("%u dumping to file: %s\n", (unsigned)state.run_I, tmp_data_filename);
|
||||
receive_to_file(hnd, tmp_data_filename, inp_buff, max_total_words, state.run_length);
|
||||
receive_to_file(hnd, tmp_data_filename, inp_buff, max_total_words, state.run_length, state.pipe_fd);
|
||||
if (runs_since_streams_clean >= 10){
|
||||
runs_since_streams_clean = 0;
|
||||
X502_StreamsStop(hnd);
|
||||
@ -969,6 +1188,9 @@ int main(int argc, char** argv) {
|
||||
runs_since_streams_clean++;
|
||||
}
|
||||
free(inp_buff);
|
||||
#ifndef _WIN32
|
||||
close_pipe(state.pipe_fd, state.pipe_path);
|
||||
#endif
|
||||
X502_Close(hnd);
|
||||
// освобождаем описатель
|
||||
X502_Free(hnd);
|
||||
@ -1100,7 +1322,7 @@ int main(int argc, char** argv) {
|
||||
struct timespec time_receive_started, time_receive_ended;
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &time_receive_started);
|
||||
receive_to_file(hnd, logfilename, inp_buff, max_total_words, 10000);
|
||||
receive_to_file(hnd, logfilename, inp_buff, max_total_words, 10000, state.pipe_fd);
|
||||
clock_gettime(CLOCK_MONOTONIC, &time_receive_ended);
|
||||
|
||||
|
||||
@ -1136,7 +1358,7 @@ int main(int argc, char** argv) {
|
||||
|
||||
// printf("\n dbg value: ");
|
||||
// BF_exec_cmd_simple(hnd, 0x800A, 10, 1);
|
||||
receive_to_file(hnd, logfilename, inp_buff, max_total_words, 10000);
|
||||
receive_to_file(hnd, logfilename, inp_buff, max_total_words, 10000, state.pipe_fd);
|
||||
printf("\n dbg value: ");
|
||||
BF_exec_cmd_simple(hnd, 0x800A, 10, 1);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user