]> rtime.felk.cvut.cz Git - canping.git/blobdiff - src/vca_canping.c
Link vca_canping with libm
[canping.git] / src / vca_canping.c
index 64ea3c27289fff216dce1aa83fd9253e81dd131d..87c3fd3272ccc679ecef2efb9de02d75434734fe 100644 (file)
@@ -2,8 +2,8 @@
 /* File: vca_canping.c - utility to test CAN functionality and throughput */
 /*                                                                        */
 /* LibVCA - Versatile CAN/CANopen API library                             */
-/* Copyright (C) 2005-2006 Michal Sojka, DCE FEE CTU Prague               */
-/* Copyright (C) 2006-2009 Pavel Pisa <pisa@cmp.felk.cvut.cz>             */
+/* Copyright (C) 2005-2009 Michal Sojka, DCE FEE CTU Prague               */
+/* Copyright (C) 2006-2023 Pavel Pisa <pisa@cmp.felk.cvut.cz>             */
 /*                                                                        */
 /* LibVCA is free software; you can redistribute it and/or modify it      */
 /* under terms of the GNU General Public License as published by the      */
 #include <signal.h>
 #include <sys/types.h>
 #include <sys/time.h>
+#include <sys/select.h>
 #include <ul_list.h>
 #include <errno.h>
 #include <semaphore.h>
 #include <stdbool.h>
+#ifndef OMK_FOR_TARGET
 #include <error.h>
+#define error_with_status error
+#else /*OMK_FOR_TARGET*/
+#include <stdarg.h>
+static inline void error_with_status (int __status, int __errnum, const char *__format, ...)
+{
+       va_list ap;
+       va_start (ap, __format);
+       vfprintf (stderr, __format, ap);
+       va_end (ap);
+}
+#endif
 
 /* TODO: Handle the case where there are more canping slaves running
  * on one CAN bus. */
@@ -59,7 +72,12 @@ int sched_rtprio;
 #define dbg(level, fmt, arg...) do { if (level <= DEBUG) { printf("candping: " fmt, ## arg); } } while (0)
 #endif
 
+#if !defined(OMK_FOR_TARGET) || defined(ERESTART)
 #define NOT_INTERRUPTED_SYSCALL (errno != EINTR && errno != ERESTART)
+#else /*OMK_FOR_TARGET*/
+#define NOT_INTERRUPTED_SYSCALL (errno != EINTR)
+#endif /*OMK_FOR_TARGET*/
+
 #define IS_FINISH_FLAG() (finish_flag)
 
 /* Exit codes */
@@ -88,7 +106,7 @@ sem_t ready_sem;             /* Thread is ready for execution */
 
 
 /* Command line options */
-char *option_device = "/dev/can0";
+char *option_device = VCA_DEV_NAME;
 int option_masters = 0;
 long int option_first_id = 1000;
 int option_slaves = 0;
@@ -170,8 +188,12 @@ void histogram_fprint(struct histogram *h, FILE *f)
                sum += h->data[i];
        cum = sum;
        for (i = 0; i < h->allocated; i++) {
-               if (h->data[i] != 0)
-                       fprintf(f, "%d %lld\n", i*h->resolution, cum);
+               if (h->data[i] != 0) {
+                       if (!getenv("CANPING_MS"))
+                               fprintf(f, "%d %lld\n", i*h->resolution, cum);
+                       else
+                               fprintf(f, "%g %lld\n", 1e-3*(i*h->resolution), cum);
+               }
                cum -= h->data[i];
        }
 }
@@ -244,8 +266,9 @@ void *master_thread(void *arg)
        struct canfilt_t canfilt; /* filter for received messages */
 
        if (!option_open_once) {
-               /* Open can driver */
-               if(vca_open_handle(&vcah, option_device, NULL, 0) < 0) {
+               /* Open the CAN driver and disable (D) reception of
+                * all messages until we setup a filter below. */
+               if(vca_open_handle(&vcah, option_device, "D", 0) < 0) {
                        perror("open");
                        fprintf(stderr, "Error opening %s (for id %d)\n", option_device, ping_id);
                        exit(EXIT_CANNOT_OPEN); 
@@ -275,7 +298,7 @@ void *master_thread(void *arg)
        if (option_save_all_times) {
                td->times = malloc(sizeof(*td->times)*option_count);
                if (!td->times)
-                       error(1, errno, "malloc times");
+                       error_with_status(1, errno, "malloc times");
        }
        
        /* Prepare data structures for the select syscall. */
@@ -402,8 +425,12 @@ void *master_thread(void *arg)
                int i;
                snprintf(buf, sizeof(buf), "%s-%d.dat", option_save_all_times, ping_id);
                f = fopen(buf, "w");
-               for (i=0; i<option_count; i++)
-                       fprintf(f, "%u\n", td->times[i]);
+               for (i=0; i<option_count; i++) {
+                       if (!getenv("CANPING_MS")) 
+                               fprintf(f, "%u\n", td->times[i]);
+                       else
+                               fprintf(f, "%g\n", 1e-3*td->times[i]);
+               }
                fclose(f);
        }
 
@@ -458,8 +485,9 @@ void *slave_thread(void *arg)
        struct canfilt_t canfilt; /* filter for received messages */
 
        if (!option_open_once) {
-               /* Open the CAN driver */
-               if(vca_open_handle(&vcah, option_device, NULL, 0) < 0) {
+               /* Open the CAN driver and disable (D) reception of
+                * all messages until we setup a filter below. */
+               if(vca_open_handle(&vcah, option_device, "D", 0) < 0) {
                        perror("open");
                        printf("Error opening %s (for id %d)\n", option_device, ping_id);
                        exit(EXIT_CANNOT_OPEN); 
@@ -623,7 +651,9 @@ int parse_options(int argc, char *argv[])
 {
        int c;
        
+#ifndef OMK_FOR_TARGET
        opterr = 0;
+#endif /*OMK_FOR_TARGET*/
        while ((c = getopt (argc, argv, "bc:d:e:g:hi:l:m:n:os:t:vw:yrR:")) != -1)
                switch (c)
                {
@@ -797,7 +827,9 @@ int main(int argc, char *argv[])
        if(sched_policy != SCHED_OTHER) {
                if(set_sched_policy_and_prio(sched_policy, sched_rtprio) <0)
                        exit(EXIT_BAD_PARAM);
+#if !defined(OMK_FOR_TARGET) || (defined(MCL_CURRENT) && defined(MCL_FUTURE))
                mlockall(MCL_CURRENT | MCL_FUTURE);
+#endif /*OMK_FOR_TARGET*/
        }
 #endif
 
@@ -827,17 +859,19 @@ int main(int argc, char *argv[])
        sem_t *child_ready;
        int fork_ret = 0;
        if ((child_ready = sem_open("canping", O_CREAT)) == NULL)
-               error(1, errno, "sem_open");
+               error_with_status(1, errno, "sem_open");
+#ifndef OMK_FOR_TARGET
        if (option_background) {
                /* Go to background when everything is ready */
                fork_ret = fork();
                if (fork_ret < 0)
-                       error(1, errno, "Cannot go to background");
+                       error_with_status(1, errno, "Cannot go to background");
                if (fork_ret == 0) {
                        int devnull = open("/dev/null", O_WRONLY);
                        dup2(devnull, 1);
                }
        }
+#endif /*OMK_FOR_TARGET*/
 
        if (fork_ret == 0) {
                /* Child */