Skip to content

Commit

Permalink
Refactor code logic according to suggestions for PR #521
Browse files Browse the repository at this point in the history
  • Loading branch information
GGZ8 committed May 14, 2024
1 parent 4b37474 commit 9a9fac1
Showing 1 changed file with 32 additions and 23 deletions.
55 changes: 32 additions & 23 deletions canplayer.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <stdbool.h>

#include <linux/can.h>
#include <linux/can/raw.h>
Expand Down Expand Up @@ -89,11 +90,11 @@ const int canfx_on = 1;

extern int optind, opterr, optopt;

typedef struct {
struct sleep {
struct timeval *sleep_vector;
size_t idx;
size_t size;
} sleep_struct;
};

static void print_usage(char *prg)
{
Expand Down Expand Up @@ -260,28 +261,36 @@ static int add_assignment(char *mode, int socket, char *txname,
return 0;
}

void load_gaps_from_file(FILE *fp, sleep_struct *a)
void load_gaps_from_file(FILE *fp, struct sleep *timestamps)
{
char *line = NULL;
size_t len = 0;
ssize_t read;

while ((read = getline(&line, &len, fp)) != -1) {
if (a->idx == a->size) {
a->size++;
a->sleep_vector = realloc(a->sleep_vector, a->size * sizeof(struct timespec));
if (timestamps->idx == timestamps->size) {
timestamps->size++;
timestamps->sleep_vector = realloc(timestamps->sleep_vector, timestamps->size * sizeof(struct timeval));
if (!timestamps->sleep_vector) {
fprintf(stderr, "Failed to create the timestamps vector!\n");
exit(1);
}
}
sscanf(line, "(%lu.%lu)", &a->sleep_vector[a->idx].tv_sec, &a->sleep_vector[a->idx].tv_usec);
a->idx++;
sscanf(line, "(%lu.%lu)", &timestamps->sleep_vector[timestamps->idx].tv_sec, &timestamps->sleep_vector[timestamps->idx].tv_usec);
timestamps->idx++;
}
free(line);
}

void init_sleep_struct(sleep_struct *a, size_t init_size)
void init_timestamps(struct sleep *timestamps, size_t init_size)
{
a->size = init_size;
a->sleep_vector = malloc(init_size * sizeof(struct timespec));
a->idx = 0;
timestamps->size = init_size;
timestamps->sleep_vector = calloc(init_size, sizeof(*(timestamps->sleep_vector)));
if (!timestamps->sleep_vector) {
fprintf(stderr, "Failed to create the timestamps vector!\n");
exit(1);
}
timestamps->idx = 0;
}

int main(int argc, char **argv)
Expand Down Expand Up @@ -310,8 +319,8 @@ int main(int argc, char **argv)
int eof, txmtu, i, j;
char *fret;
unsigned long long sec, usec;
int gap_from_file = 0;
sleep_struct time_arr;
bool gap_from_file = false;
struct sleep timestamps;
struct timeval send_time, act_time, init_time;

while ((opt = getopt(argc, argv, "I:l:tin:g:s:xvr?")) != -1) {
Expand Down Expand Up @@ -370,14 +379,14 @@ int main(int argc, char **argv)
break;

case 'r':
if (infile == stdin) {
if (!isatty(fileno(infile))) {
fprintf(stderr, "Specify an input file for option -r !\n");
exit(EXIT_FAILURE);
}
gap_from_file = 1; /* using time delta from file */
init_sleep_struct(&time_arr, 1);
load_gaps_from_file(infile, &time_arr);
time_arr.idx = 0;
gap_from_file = true; /* using time delta from file */
init_timestamps(&timestamps, 1);
load_gaps_from_file(infile, &timestamps);
timestamps.idx = 0;
break;

case '?':
Expand Down Expand Up @@ -555,8 +564,8 @@ int main(int argc, char **argv)
addr.can_family = AF_CAN;
addr.can_ifindex = txidx; /* send via this interface */

if (gap_from_file && time_arr.idx > 0) {
send_time = time_arr.sleep_vector[time_arr.idx];
if (gap_from_file && timestamps.idx > 0) {
send_time = timestamps.sleep_vector[timestamps.idx];
gettimeofday(&act_time, NULL);
timersub(&act_time, &init_time, &act_time);

Expand All @@ -565,12 +574,12 @@ int main(int argc, char **argv)
timersub(&act_time, &init_time, &act_time);
}
}
if (gap_from_file && time_arr.idx == 0) {
if (gap_from_file && timestamps.idx == 0) {
gettimeofday(&init_time, NULL);
gettimeofday(&act_time, NULL);
timersub(&act_time, &init_time, &act_time);
}
time_arr.idx++;
timestamps.idx++;

if (sendto(s, &cu, txmtu, 0, (struct sockaddr *)&addr, sizeof(addr)) != txmtu) {
perror("sendto");
Expand Down

0 comments on commit 9a9fac1

Please sign in to comment.