Merge pull request #482 from marckleinebudde/codingstyle

Codingstyle
pull/486/head
Marc Kleine-Budde 2023-12-11 09:41:45 +01:00 committed by GitHub
commit 76002c0892
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 53 deletions

View File

@ -75,16 +75,16 @@
#define TIMESTAMPSZ 50 /* string 'absolute with date' requires max 49 bytes */ #define TIMESTAMPSZ 50 /* string 'absolute with date' requires max 49 bytes */
#define MAXSOCK 16 /* max. number of CAN interfaces given on the cmdline */ #define MAXSOCK 16 /* max. number of CAN interfaces given on the cmdline */
#define MAXIFNAMES 30 /* size of receive name index to omit ioctls */ #define MAXIFNAMES 30 /* size of receive name index to omit ioctls */
#define MAXCOL 6 /* number of different colors for colorized output */ #define MAXCOL 6 /* number of different colors for colorized output */
#define ANYDEV "any" /* name of interface to receive from any CAN interface */ #define ANYDEV "any" /* name of interface to receive from any CAN interface */
#define ANL "\r\n" /* newline in ASC mode */ #define ANL "\r\n" /* newline in ASC mode */
#define SILENT_INI 42 /* detect user setting on commandline */ #define SILENT_INI 42 /* detect user setting on commandline */
#define SILENT_OFF 0 /* no silent mode */ #define SILENT_OFF 0 /* no silent mode */
#define SILENT_ANI 1 /* silent mode with animation */ #define SILENT_ANI 1 /* silent mode with animation */
#define SILENT_ON 2 /* silent mode (completely silent) */ #define SILENT_ON 2 /* silent mode (completely silent) */
#define BOLD ATTBOLD #define BOLD ATTBOLD
#define RED (ATTBOLD FGRED) #define RED (ATTBOLD FGRED)
@ -106,7 +106,7 @@ struct if_info { /* bundled information per open socket */
static struct if_info sock_info[MAXSOCK]; static struct if_info sock_info[MAXSOCK];
static char *progname; static char *progname;
static char devname[MAXIFNAMES][IFNAMSIZ+1]; static char devname[MAXIFNAMES][IFNAMSIZ + 1];
static int dindex[MAXIFNAMES]; static int dindex[MAXIFNAMES];
static int max_devname_len; /* to prevent frazzled device name output */ static int max_devname_len; /* to prevent frazzled device name output */
static const int canfd_on = 1; static const int canfd_on = 1;
@ -175,7 +175,6 @@ static void sigterm(int signo)
static int idx2dindex(int ifidx, int socket) static int idx2dindex(int ifidx, int socket)
{ {
int i; int i;
struct ifreq ifr; struct ifreq ifr;
@ -245,9 +244,9 @@ static inline void sprint_timestamp(const char timestamp, const struct timeval *
{ {
struct timeval diff; struct timeval diff;
if (last_tv->tv_sec == 0) /* first init */ if (last_tv->tv_sec == 0) /* first init */
*last_tv = *tv; *last_tv = *tv;
diff.tv_sec = tv->tv_sec - last_tv->tv_sec; diff.tv_sec = tv->tv_sec - last_tv->tv_sec;
diff.tv_usec = tv->tv_usec - last_tv->tv_usec; diff.tv_usec = tv->tv_usec - last_tv->tv_usec;
if (diff.tv_usec < 0) if (diff.tv_usec < 0)
diff.tv_sec--, diff.tv_usec += 1000000; diff.tv_sec--, diff.tv_usec += 1000000;
@ -474,8 +473,8 @@ int main(int argc, char **argv)
} }
for (i = 0; i < currmax; i++) { for (i = 0; i < currmax; i++) {
struct if_info* obj = &sock_info[i]; struct if_info *obj = &sock_info[i];
ptr = argv[optind+i]; ptr = argv[optind + i];
nptr = strchr(ptr, ','); nptr = strchr(ptr, ',');
pr_debug("open %d '%s'.\n", i, ptr); pr_debug("open %d '%s'.\n", i, ptr);
@ -495,7 +494,7 @@ int main(int argc, char **argv)
obj->cmdlinename = ptr; /* save pointer to cmdline name of this socket */ obj->cmdlinename = ptr; /* save pointer to cmdline name of this socket */
if (nptr) if (nptr)
nbytes = nptr - ptr; /* interface name is up the first ',' */ nbytes = nptr - ptr; /* interface name is up the first ',' */
else else
nbytes = strlen(ptr); /* no ',' found => no filter definitions */ nbytes = strlen(ptr); /* no ',' found => no filter definitions */
@ -714,8 +713,8 @@ int main(int argc, char **argv)
continue; continue;
} }
for (i = 0; i < num_events; i++) { /* check waiting CAN RAW sockets */ for (i = 0; i < num_events; i++) { /* check waiting CAN RAW sockets */
struct if_info* obj = events_pending[i].data.ptr; struct if_info *obj = events_pending[i].data.ptr;
int idx; int idx;
char *extra_info = ""; char *extra_info = "";
@ -838,9 +837,9 @@ int main(int argc, char **argv)
if (extra_msg_info) { if (extra_msg_info) {
if (msg.msg_flags & MSG_DONTROUTE) if (msg.msg_flags & MSG_DONTROUTE)
printf (" TX %s", extra_m_info[frame.flags & 3]); printf(" TX %s", extra_m_info[frame.flags & 3]);
else else
printf (" RX %s", extra_m_info[frame.flags & 3]); printf(" RX %s", extra_m_info[frame.flags & 3]);
} }
printf("%s ", (color == 1) ? col_off : ""); printf("%s ", (color == 1) ? col_off : "");
@ -850,7 +849,7 @@ int main(int argc, char **argv)
printf("%s", (color > 1) ? col_off : ""); printf("%s", (color > 1) ? col_off : "");
printf("\n"); printf("\n");
out_fflush: out_fflush:
fflush(stdout); fflush(stdout);
} }
} }

View File

@ -40,11 +40,11 @@
#include <linux/can.h> #include <linux/can.h>
#include <linux/can/raw.h> #include <linux/can/raw.h>
#define CAN_MSG_ID_PING 0x77 #define CAN_MSG_ID_PING 0x77
#define CAN_MSG_ID_PONG 0x78 #define CAN_MSG_ID_PONG 0x78
#define CAN_MSG_LEN 8 #define CAN_MSG_LEN 8
#define CAN_MSG_COUNT 50 #define CAN_MSG_COUNT 50
#define CAN_MSG_WAIT 27 #define CAN_MSG_WAIT 27
static int running = 1; static int running = 1;
static int verbose; static int verbose;
@ -82,7 +82,7 @@ static void print_usage(char *prg)
"\n" "\n"
"With the option '-g' CAN messages are generated and checked\n" "With the option '-g' CAN messages are generated and checked\n"
"on <can-interface>, otherwise all messages received on the\n" "on <can-interface>, otherwise all messages received on the\n"
"<can-interface> are sent back incrementing the CAN id and\n" "<can-interface> are sent back incrementing the CAN id and\n"
"all data bytes. The program can be aborted with ^C.\n" "all data bytes. The program can be aborted with ^C.\n"
"\n" "\n"
"Examples:\n" "Examples:\n"
@ -110,14 +110,9 @@ static void print_frame(canid_t id, const uint8_t *data, int dlc, int inc_data)
printf("\n"); printf("\n");
} }
static void print_compare( static void print_compare(canid_t exp_id, const uint8_t *exp_data, uint8_t exp_dlc,
canid_t exp_id, canid_t rec_id, const uint8_t *rec_data, uint8_t rec_dlc,
const uint8_t *exp_data, int inc)
uint8_t exp_dlc,
canid_t rec_id,
const uint8_t *rec_data,
uint8_t rec_dlc,
int inc)
{ {
printf("expected: "); printf("expected: ");
print_frame(exp_id, exp_data, exp_dlc, inc); print_frame(exp_id, exp_data, exp_dlc, inc);
@ -255,7 +250,7 @@ static int check_frame(const struct canfd_frame *frame)
} }
for (i = 1; i < frame->len; i++) { for (i = 1; i < frame->len; i++) {
if (frame->data[i] != (uint8_t)(frame->data[i-1] + 1)) { if (frame->data[i] != (uint8_t)(frame->data[i - 1] + 1)) {
printf("Frame inconsistent!\n"); printf("Frame inconsistent!\n");
print_frame(frame->can_id, frame->data, frame->len, 0); print_frame(frame->can_id, frame->data, frame->len, 0);
err = -1; err = -1;
@ -263,7 +258,7 @@ static int check_frame(const struct canfd_frame *frame)
} }
} }
out: out:
return err; return err;
} }
@ -324,11 +319,11 @@ static int can_echo_gen(void)
int err = 0; int err = 0;
int i; int i;
tx_frames = calloc(inflight_count, sizeof(* tx_frames)); tx_frames = calloc(inflight_count, sizeof(*tx_frames));
if (!tx_frames) if (!tx_frames)
return -1; return -1;
recv_tx = calloc(inflight_count, sizeof(* recv_tx)); recv_tx = calloc(inflight_count, sizeof(*recv_tx));
if (!recv_tx) { if (!recv_tx) {
err = -1; err = -1;
goto out_free_tx_frames; goto out_free_tx_frames;
@ -400,9 +395,9 @@ static int can_echo_gen(void)
printf("\nTest messages sent and received: %d\n", loops); printf("\nTest messages sent and received: %d\n", loops);
out_free: out_free:
free(recv_tx); free(recv_tx);
out_free_tx_frames: out_free_tx_frames:
free(tx_frames); free(tx_frames);
return err; return err;
@ -523,7 +518,7 @@ int main(int argc, char *argv[])
if (echo_gen) { if (echo_gen) {
if (setsockopt(sockfd, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS, if (setsockopt(sockfd, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS,
&enable_socket_option, sizeof(enable_socket_option)) == -1) { &enable_socket_option, sizeof(enable_socket_option)) == -1) {
perror("setsockopt CAN_RAW_RECV_OWN_MSGS"); perror("setsockopt CAN_RAW_RECV_OWN_MSGS");
return 1; return 1;
} }
@ -531,7 +526,7 @@ int main(int argc, char *argv[])
if (is_can_fd) { if (is_can_fd) {
if (setsockopt(sockfd, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, if (setsockopt(sockfd, SOL_CAN_RAW, CAN_RAW_FD_FRAMES,
&enable_socket_option, sizeof(enable_socket_option)) == -1) { &enable_socket_option, sizeof(enable_socket_option)) == -1) {
perror("setsockopt CAN_RAW_FD_FRAMES"); perror("setsockopt CAN_RAW_FD_FRAMES");
return 1; return 1;
} }
@ -567,7 +562,7 @@ int main(int argc, char *argv[])
}; };
if (setsockopt(sockfd, SOL_CAN_RAW, CAN_RAW_FILTER, filters, if (setsockopt(sockfd, SOL_CAN_RAW, CAN_RAW_FILTER, filters,
sizeof(struct can_filter) * (1 + echo_gen))) { sizeof(struct can_filter) * (1 + echo_gen))) {
perror("setsockopt()"); perror("setsockopt()");
close(sockfd); close(sockfd);
return 1; return 1;

View File

@ -146,7 +146,7 @@ static struct timespec timespec_add(struct timespec ts1, struct timespec ts2)
struct timespec double_to_timespec(double s) struct timespec double_to_timespec(double s)
{ {
struct timespec ts = { struct timespec ts = {
.tv_sec = s, .tv_sec = s,
.tv_nsec = (s - (long)(s)) * NSEC_PER_SEC, .tv_nsec = (s - (long)(s)) * NSEC_PER_SEC,
}; };
@ -156,7 +156,7 @@ struct timespec double_to_timespec(double s)
static struct timespec ns_to_timespec(int64_t ns) static struct timespec ns_to_timespec(int64_t ns)
{ {
struct timespec ts = { struct timespec ts = {
.tv_sec = ns / NSEC_PER_SEC, .tv_sec = ns / NSEC_PER_SEC,
.tv_nsec = ns % NSEC_PER_SEC, .tv_nsec = ns % NSEC_PER_SEC,
}; };
@ -324,7 +324,7 @@ static int do_send_one(int fd, void *buf, size_t len, int timeout)
memcpy(CMSG_DATA(cm), &tdeliver, sizeof(tdeliver)); memcpy(CMSG_DATA(cm), &tdeliver, sizeof(tdeliver));
} }
resend: resend:
nbytes = sendmsg(fd, &msg, 0); nbytes = sendmsg(fd, &msg, 0);
if (nbytes < 0) { if (nbytes < 0) {
ret = -errno; ret = -errno;
@ -460,7 +460,7 @@ int main(int argc, char **argv)
int incdlc = 0; int incdlc = 0;
unsigned long rnd; unsigned long rnd;
unsigned char fixdata[CANFD_MAX_DLEN]; unsigned char fixdata[CANFD_MAX_DLEN];
unsigned char rand_position[CANFD_MAX_DLEN] = {0}; unsigned char rand_position[CANFD_MAX_DLEN] = { 0 };
int opt; int opt;
int s; /* socket */ int s; /* socket */
@ -490,7 +490,6 @@ int main(int argc, char **argv)
while ((opt = getopt_long(argc, argv, "g:atefbER8mI:L:D:p:n:ixc:vh?", long_options, NULL)) != -1) { while ((opt = getopt_long(argc, argv, "g:atefbER8mI:L:D:p:n:ixc:vh?", long_options, NULL)) != -1) {
switch (opt) { switch (opt) {
case 'g': case 'g':
gap = strtod(optarg, NULL); gap = strtod(optarg, NULL);
break; break;
@ -793,7 +792,7 @@ int main(int argc, char **argv)
if (data_mode == MODE_RANDOM_FIX) { if (data_mode == MODE_RANDOM_FIX) {
int i; int i;
memcpy(frame.data, fixdata, CANFD_MAX_DLEN); memcpy(frame.data, fixdata, CANFD_MAX_DLEN);
for (i = 0; i < frame.len; i++) { for (i = 0; i < frame.len; i++) {

View File

@ -81,11 +81,9 @@ void print_usage(char *prg)
" 1F334455#1122334455667788_B / 123#R / 00000123#R3 / 333#R8_E\n\n"); " 1F334455#1122334455667788_B / 123#R / 00000123#R3 / 333#R8_E\n\n");
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int s; /* can raw socket */ int s; /* can raw socket */
int required_mtu; int required_mtu;
int mtu; int mtu;
int enable_canfd = 1; int enable_canfd = 1;
@ -101,7 +99,7 @@ int main(int argc, char **argv)
/* parse CAN frame */ /* parse CAN frame */
required_mtu = parse_canframe(argv[2], &frame); required_mtu = parse_canframe(argv[2], &frame);
if (!required_mtu){ if (!required_mtu) {
fprintf(stderr, "\nWrong CAN-frame format!\n\n"); fprintf(stderr, "\nWrong CAN-frame format!\n\n");
print_usage(argv[0]); print_usage(argv[0]);
return 1; return 1;
@ -126,7 +124,6 @@ int main(int argc, char **argv)
addr.can_ifindex = ifr.ifr_ifindex; addr.can_ifindex = ifr.ifr_ifindex;
if (required_mtu > (int)CAN_MTU) { if (required_mtu > (int)CAN_MTU) {
/* check if the frame fits into the CAN netdevice */ /* check if the frame fits into the CAN netdevice */
if (ioctl(s, SIOCGIFMTU, &ifr) < 0) { if (ioctl(s, SIOCGIFMTU, &ifr) < 0) {
perror("SIOCGIFMTU"); perror("SIOCGIFMTU");