hc
2024-11-01 2f529f9b558ca1c1bd74be7437a84e4711743404
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
 * Round-Trip-Time Test - sends and receives messages and measures the
 *                        time in between.
 *
 * Copyright (C) 2006 Wolfgang Grandegger <wg@grandegger.com>
 *
 * Based on RTnet's examples/xenomai/posix/rtt-sender.c.
 *
 * Copyright (C) 2002 Ulrich Marx <marx@kammer.uni-hannover.de>
 *               2002 Marc Kleine-Budde <kleine-budde@gmx.de>
 *               2006 Jan Kiszka <jan.kiszka@web.de>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *
 * The program sends out CAN messages periodically and copies the current
 * time-stamp to the payload. At reception, that time-stamp is compared
 * with the current time to determine the round-trip time. The jitter
 * values are printer out regularly. Concurrent tests can be carried out
 * by starting the program with different message identifiers. It is also
 * possible to use this program on a remote system as simple repeater to
 * loopback messages.
 */
 
#include <errno.h>
#include <mqueue.h>
#include <signal.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include <getopt.h>
#include <memory.h>
#include <netinet/in.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <rtdm/can.h>
#include <xenomai/init.h>
 
#define NSEC_PER_SEC 1000000000
 
static unsigned int cycle = 10000; /* 10 ms */
static canid_t can_id = 0x1;
 
static pthread_t txthread, rxthread;
static int txsock, rxsock;
static mqd_t mq;
static int txcount, rxcount;
static int overruns;
static int repeater;
 
struct rtt_stat {
    long long rtt;
    long long rtt_min;
    long long rtt_max;
    long long rtt_sum;
    long long rtt_sum_last;
    int counts_per_sec;
};
 
void application_usage(void)
{
    fprintf(stderr, "usage: %s [options] <tx-can-interface> <rx-can-interface>:\n",
       get_program_name());
    fprintf(stderr,
       " -r, --repeater            Repeater, send back received messages\n"
       " -i, --id=ID            CAN Identifier (default = 0x1)\n"
       " -c, --cycle            Cycle time in us (default = 10000us)\n");
}
 
static void *transmitter(void *arg)
{
    struct sched_param  param = { .sched_priority = 80 };
    struct timespec next_period;
    struct timespec time;
    struct can_frame frame;
    long long *rtt_time = (long long *)&frame.data, t;
 
    /* Pre-fill CAN frame */
    frame.can_id = can_id;
    frame.can_dlc = sizeof(*rtt_time);
 
    pthread_setname_np(pthread_self(), "rtcan_rtt_transmitter");
    pthread_setschedparam(pthread_self(), SCHED_FIFO, &param);
 
    clock_gettime(CLOCK_MONOTONIC, &next_period);
 
    while(1) {
   next_period.tv_nsec += cycle * 1000;
   while (next_period.tv_nsec >= NSEC_PER_SEC) {
       next_period.tv_nsec -= NSEC_PER_SEC;
       next_period.tv_sec++;
   }
 
   clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &next_period, NULL);
 
   if (rxcount != txcount) {
       overruns++;
       continue;
   }
 
   clock_gettime(CLOCK_MONOTONIC, &time);
   t = (long long)time.tv_sec * NSEC_PER_SEC + time.tv_nsec;
   memcpy(rtt_time, &t, sizeof(t));
 
   /* Transmit the message containing the local time */
   if (send(txsock, (void *)&frame, sizeof(struct can_frame), 0) < 0) {
       if (errno == EBADF)
       printf("terminating transmitter thread\n");
       else
       perror("send failed");
       return NULL;
   }
   txcount++;
    }
}
 
 
static void *receiver(void *arg)
{
    struct sched_param param = { .sched_priority = 82 };
    struct timespec time;
    struct can_frame frame;
    long long *rtt_time = (long long *)frame.data, t;
    struct rtt_stat rtt_stat = {0, 1000000000000000000LL, -1000000000000000000LL,
               0, 0, 0};
 
    pthread_setname_np(pthread_self(), "rtcan_rtt_receiver");
    pthread_setschedparam(pthread_self(), SCHED_FIFO, &param);
 
    rtt_stat.counts_per_sec = 1000000 / cycle;
 
    while (1) {
   if (recv(rxsock, (void *)&frame, sizeof(struct can_frame), 0) < 0) {
       if (errno == EBADF)
       printf("terminating receiver thread\n");
       else
       perror("recv failed");
       return NULL;
   }
   if (repeater) {
       /* Transmit the message back as is */
       if (send(txsock, (void *)&frame, sizeof(struct can_frame), 0) < 0) {
       if (errno == EBADF)
           printf("terminating transmitter thread\n");
       else
           perror("send failed");
       return NULL;
       }
       txcount++;
   } else {
       memcpy(&t, rtt_time, sizeof(t));
       clock_gettime(CLOCK_MONOTONIC, &time);
       if (rxcount > 0) {
       rtt_stat.rtt = ((long long)time.tv_sec * 1000000000LL +
               time.tv_nsec - t);
       rtt_stat.rtt_sum += rtt_stat.rtt;
       if (rtt_stat.rtt <  rtt_stat.rtt_min)
           rtt_stat.rtt_min = rtt_stat.rtt;
       if (rtt_stat.rtt > rtt_stat.rtt_max)
           rtt_stat.rtt_max = rtt_stat.rtt;
       }
   }
   rxcount++;
 
   if ((rxcount % rtt_stat.counts_per_sec) == 0) {
       mq_send(mq, (char *)&rtt_stat, sizeof(rtt_stat), 0);
       rtt_stat.rtt_sum_last = rtt_stat.rtt_sum;
   }
    }
}
 
static void catch_signal(int sig)
{
    mq_close(mq);
    close(rxsock);
    close(txsock);
}
 
 
int main(int argc, char *argv[])
{
    struct sched_param param = { .sched_priority = 1 };
    pthread_attr_t thattr;
    struct mq_attr mqattr;
    struct sockaddr_can rxaddr, txaddr;
    struct can_filter rxfilter[1];
    struct rtt_stat rtt_stat;
    char mqname[32];
    char *txdev, *rxdev;
    struct can_ifreq ifr;
    int ret, opt;
 
    struct option long_options[] = {
   { "id", required_argument, 0, 'i'},
   { "cycle", required_argument, 0, 'c'},
   { "repeater", no_argument, 0, 'r'},
   { 0, 0, 0, 0},
    };
 
    while ((opt = getopt_long(argc, argv, "ri:c:",
                 long_options, NULL)) != -1) {
   switch (opt) {
   case 'c':
       cycle = atoi(optarg);
       break;
 
   case 'i':
       can_id = strtoul(optarg, NULL, 0);
       break;
 
   case 'r':
       repeater = 1;
       break;
 
   default:
       fprintf(stderr, "Unknown option %c\n", opt);
       exit(-1);
   }
    }
 
    printf("%d %d\n", optind, argc);
    if (optind + 2 != argc) {
   xenomai_usage();
   exit(0);
    }
 
    txdev = argv[optind];
    rxdev = argv[optind + 1];
 
    /* Create and configure RX socket */
    if ((rxsock = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
   perror("RX socket failed");
   return -1;
    }
 
    namecpy(ifr.ifr_name, rxdev);
    printf("RX rxsock=%d, ifr_name=%s\n", rxsock, ifr.ifr_name);
 
    if (ioctl(rxsock, SIOCGIFINDEX, &ifr) < 0) {
   perror("RX ioctl SIOCGIFINDEX failed");
   goto failure1;
    }
 
    /* We only want to receive our own messages */
    rxfilter[0].can_id = can_id;
    rxfilter[0].can_mask = 0x3ff;
    if (setsockopt(rxsock, SOL_CAN_RAW, CAN_RAW_FILTER,
          &rxfilter, sizeof(struct can_filter)) < 0) {
   perror("RX setsockopt CAN_RAW_FILTER failed");
   goto failure1;
    }
    memset(&rxaddr, 0, sizeof(rxaddr));
    rxaddr.can_ifindex = ifr.ifr_ifindex;
    rxaddr.can_family = AF_CAN;
    if (bind(rxsock, (struct sockaddr *)&rxaddr, sizeof(rxaddr)) < 0) {
   perror("RX bind failed\n");
   goto failure1;
    }
 
    /* Create and configure TX socket */
 
    if (strcmp(rxdev, txdev) == 0) {
   txsock = rxsock;
    } else {
   if ((txsock = socket(PF_CAN, SOCK_RAW, 0)) < 0) {
       perror("TX socket failed");
       goto failure1;
   }
 
   namecpy(ifr.ifr_name, txdev);
   printf("TX txsock=%d, ifr_name=%s\n", txsock, ifr.ifr_name);
 
   if (ioctl(txsock, SIOCGIFINDEX, &ifr) < 0) {
       perror("TX ioctl SIOCGIFINDEX failed");
       goto failure2;
   }
 
   /* Suppress definiton of a default receive filter list */
   if (setsockopt(txsock, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0) < 0) {
       perror("TX setsockopt CAN_RAW_FILTER failed");
       goto failure2;
   }
 
   memset(&txaddr, 0, sizeof(txaddr));
   txaddr.can_ifindex = ifr.ifr_ifindex;
   txaddr.can_family = AF_CAN;
 
   if (bind(txsock, (struct sockaddr *)&txaddr, sizeof(txaddr)) < 0) {
       perror("TX bind failed\n");
       goto failure2;
   }
    }
 
    signal(SIGTERM, catch_signal);
    signal(SIGINT, catch_signal);
    signal(SIGHUP, catch_signal);
 
    printf("Round-Trip-Time test %s -> %s with CAN ID 0x%x\n",
      argv[optind], argv[optind + 1], can_id);
    printf("Cycle time: %d us\n", cycle);
    printf("All RTT timing figures are in us.\n");
 
    /* Create statistics message queue */
    snprintf(mqname, sizeof(mqname), "/rtcan_rtt-%d", getpid());
    mqattr.mq_flags   = 0;
    mqattr.mq_maxmsg  = 100;
    mqattr.mq_msgsize = sizeof(struct rtt_stat);
    mq = mq_open(mqname, O_RDWR | O_CREAT | O_EXCL, 0600, &mqattr);
    if (mq == (mqd_t)-1) {
   perror("opening mqueue failed");
   goto failure2;
    }
 
    /* Create receiver RT-thread */
    pthread_attr_init(&thattr);
    pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_JOINABLE);
    ret = pthread_create(&rxthread, &thattr, &receiver, NULL);
    if (ret) {
   fprintf(stderr, "%s: pthread_create(receiver) failed\n",
       strerror(-ret));
   goto failure3;
    }
 
    if (!repeater) {
   /* Create transitter RT-thread */
   ret = pthread_create(&txthread, &thattr, &transmitter, NULL);
   if (ret) {
       fprintf(stderr, "%s: pthread_create(transmitter) failed\n",
           strerror(-ret));
       goto failure4;
   }
    }
 
    pthread_setschedparam(pthread_self(), SCHED_FIFO, &param);
 
    if (repeater)
   printf("Messages\n");
    else
   printf("Messages RTTlast RTT_avg RTT_min RTT_max Overruns\n");
 
    while (1) {
   long long rtt_avg;
 
   ret = mq_receive(mq, (char *)&rtt_stat, sizeof(rtt_stat), NULL);
   if (ret != sizeof(rtt_stat)) {
       if (ret < 0) {
       if (errno == EBADF)
           printf("terminating mq_receive\n");
       else
           perror("mq_receive failed");
       } else
       fprintf(stderr,
           "mq_receive returned invalid length %d\n", ret);
       break;
   }
 
   if (repeater) {
       printf("%8d\n", rxcount);
   } else {
       rtt_avg = ((rtt_stat.rtt_sum - rtt_stat.rtt_sum_last) /
              rtt_stat.counts_per_sec);
       printf("%8d %7ld %7ld %7ld %7ld %8d\n", rxcount,
          (long)(rtt_stat.rtt / 1000), (long)(rtt_avg / 1000),
          (long)(rtt_stat.rtt_min / 1000),
          (long)(rtt_stat.rtt_max / 1000),
          overruns);
   }
    }
 
    /* This call also leaves primary mode, required for socket cleanup. */
    printf("shutting down\n");
 
    /* Important: First close the sockets! */
    close(rxsock);
    close(txsock);
    pthread_join(txthread, NULL);
    pthread_cancel(rxthread);
    pthread_join(rxthread, NULL);
 
    return 0;
 
 failure4:
    pthread_cancel(rxthread);
    pthread_join(rxthread, NULL);
 failure3:
    mq_close(mq);
 failure2:
    close(txsock);
 failure1:
    close(rxsock);
 
    return 1;
}