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
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>
#include <getopt.h>
 
#include <boilerplate/ancillaries.h>
#include <alchemy/task.h>
#include <alchemy/timer.h>
 
#include <rtdm/can.h>
 
extern int optind, opterr, optopt;
 
static void print_usage(char *prg)
{
    fprintf(stderr,
       "Usage: %s <can-interface> [Options] <can-msg>\n"
       "<can-msg> can consist of up to 8 bytes given as a space separated list\n"
       "Options:\n"
       " -i, --identifier=ID   CAN Identifier (default = 1)\n"
       " -r  --rtr             send remote request\n"
       " -e  --extended        send extended frame\n"
       " -l  --loop=COUNT      send message COUNT times\n"
       " -c, --count           message count in data[0-3]\n"
       " -d, --delay=MS        delay in ms (default = 1ms)\n"
       " -s, --send            use send instead of sendto\n"
       " -t, --timeout=MS      timeout in ms\n"
       " -L, --loopback=0|1    switch local loopback off or on\n"
       " -v, --verbose         be verbose\n"
       " -p, --print=MODULO    print every MODULO message\n"
       " -h, --help            this help\n",
       prg);
}
 
 
RT_TASK rt_task_desc;
 
static int s=-1, dlc=0, rtr=0, extended=0, verbose=0, loops=1;
static SRTIME delay=1000000;
static int count=0, print=1, use_send=0, loopback=-1;
static nanosecs_rel_t timeout = 0;
static struct can_frame frame;
static struct sockaddr_can to_addr;
 
 
static void cleanup(void)
{
    int ret;
 
    if (verbose)
   printf("Cleaning up...\n");
 
    usleep(100000);
 
    if (s >= 0) {
   ret = close(s);
   s = -1;
   if (ret) {
       fprintf(stderr, "close: %s\n", strerror(errno));
   }
   exit(EXIT_SUCCESS);
    }
}
 
static void cleanup_and_exit(int sig)
{
    if (verbose)
   printf("Signal %d received\n", sig);
    cleanup();
    exit(0);
}
 
static void rt_task(void)
{
    int i, j, ret;
 
    for (i = 0; i < loops; i++) {
   rt_task_sleep(rt_timer_ns2ticks(delay));
   if (count)
       memcpy(&frame.data[0], &i, sizeof(i));
   /* Note: sendto avoids the definiton of a receive filter list */
   if (use_send)
       ret = send(s, (void *)&frame, sizeof(can_frame_t), 0);
   else
       ret = sendto(s, (void *)&frame, sizeof(can_frame_t), 0,
               (struct sockaddr *)&to_addr, sizeof(to_addr));
   if (ret < 0) {
       switch (errno) {
       case ETIMEDOUT:
       if (verbose)
           printf("send(to): timed out\n");
       break;
       case EBADF:
       if (verbose)
           printf("send(to): aborted because socket was closed\n");
       break;
       default:
       fprintf(stderr, "send: %s\n", strerror(errno));
       break;
       }
       i = loops;        /* abort */
       break;
   }
   if (verbose && (i % print) == 0) {
       if (frame.can_id & CAN_EFF_FLAG)
       printf("<0x%08x>", frame.can_id & CAN_EFF_MASK);
       else
       printf("<0x%03x>", frame.can_id & CAN_SFF_MASK);
       printf(" [%d]", frame.can_dlc);
       for (j = 0; j < frame.can_dlc; j++) {
       printf(" %02x", frame.data[j]);
       }
       printf("\n");
   }
    }
}
 
int main(int argc, char **argv)
{
    int i, opt, ret;
    struct can_ifreq ifr;
    char name[32];
 
    struct option long_options[] = {
   { "help", no_argument, 0, 'h' },
   { "identifier", required_argument, 0, 'i'},
   { "rtr", no_argument, 0, 'r'},
   { "extended", no_argument, 0, 'e'},
   { "verbose", no_argument, 0, 'v'},
   { "count", no_argument, 0, 'c'},
   { "print", required_argument, 0, 'p'},
   { "loop", required_argument, 0, 'l'},
   { "delay", required_argument, 0, 'd'},
   { "send", no_argument, 0, 's'},
   { "timeout", required_argument, 0, 't'},
   { "loopback", required_argument, 0, 'L'},
   { 0, 0, 0, 0},
    };
 
    signal(SIGTERM, cleanup_and_exit);
    signal(SIGINT, cleanup_and_exit);
 
    frame.can_id = 1;
 
    while ((opt = getopt_long(argc, argv, "hvi:l:red:t:cp:sL:",
                 long_options, NULL)) != -1) {
   switch (opt) {
   case 'h':
       print_usage(argv[0]);
       exit(0);
 
   case 'p':
       print = strtoul(optarg, NULL, 0);
 
   case 'v':
       verbose = 1;
       break;
 
   case 'c':
       count = 1;
       break;
 
   case 'l':
       loops = strtoul(optarg, NULL, 0);
       break;
 
   case 'i':
       frame.can_id = strtoul(optarg, NULL, 0);
       break;
 
   case 'r':
       rtr = 1;
       break;
 
   case 'e':
       extended = 1;
       break;
 
   case 'd':
       delay = strtoul(optarg, NULL, 0) * 1000000LL;
       break;
 
   case 's':
       use_send = 1;
       break;
 
   case 't':
       timeout = strtoul(optarg, NULL, 0) * 1000000LL;
       break;
 
   case 'L':
       loopback = strtoul(optarg, NULL, 0);
       break;
 
   default:
       fprintf(stderr, "Unknown option %c\n", opt);
       break;
   }
    }
 
    if (optind == argc) {
   print_usage(argv[0]);
   exit(0);
    }
 
    if (argv[optind] == NULL) {
   fprintf(stderr, "No Interface supplied\n");
   exit(-1);
    }
 
    if (verbose)
   printf("interface %s\n", argv[optind]);
 
    ret = socket(PF_CAN, SOCK_RAW, CAN_RAW);
    if (ret < 0) {
   fprintf(stderr, "socket: %s\n", strerror(errno));
   return -1;
    }
    s = ret;
 
    if (loopback >= 0) {
   ret = setsockopt(s, SOL_CAN_RAW, CAN_RAW_LOOPBACK,
               &loopback, sizeof(loopback));
   if (ret < 0) {
       fprintf(stderr, "setsockopt: %s\n", strerror(errno));
       goto failure;
   }
   if (verbose)
       printf("Using loopback=%d\n", loopback);
    }
 
    namecpy(ifr.ifr_name, argv[optind]);
    if (verbose)
   printf("s=%d, ifr_name=%s\n", s, ifr.ifr_name);
 
    ret = ioctl(s, SIOCGIFINDEX, &ifr);
    if (ret < 0) {
   fprintf(stderr, "ioctl: %s\n", strerror(errno));
   goto failure;
    }
 
    memset(&to_addr, 0, sizeof(to_addr));
    to_addr.can_ifindex = ifr.ifr_ifindex;
    to_addr.can_family = AF_CAN;
    if (use_send) {
   /* Suppress definiton of a default receive filter list */
   ret = setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
   if (ret < 0) {
       fprintf(stderr, "setsockopt: %s\n", strerror(errno));
       goto failure;
   }
 
   ret = bind(s, (struct sockaddr *)&to_addr, sizeof(to_addr));
   if (ret < 0) {
       fprintf(stderr, "bind: %s\n", strerror(errno));
       goto failure;
   }
    }
 
    if (count)
   frame.can_dlc = sizeof(int);
    else {
   for (i = optind + 1; i < argc; i++) {
       frame.data[dlc] = strtoul(argv[i], NULL, 0);
       dlc++;
       if( dlc == 8 )
       break;
   }
   frame.can_dlc = dlc;
    }
 
    if (rtr)
   frame.can_id |= CAN_RTR_FLAG;
 
    if (extended)
   frame.can_id |= CAN_EFF_FLAG;
 
    if (timeout) {
   if (verbose)
       printf("Timeout: %lld ns\n", (long long)timeout);
   ret = ioctl(s, RTCAN_RTIOC_SND_TIMEOUT, &timeout);
   if (ret) {
       fprintf(stderr, "ioctl SND_TIMEOUT: %s\n", strerror(errno));
       goto failure;
   }
    }
 
    snprintf(name, sizeof(name), "rtcansend-%d", getpid());
    ret = rt_task_shadow(&rt_task_desc, name, 1, 0);
    if (ret) {
   fprintf(stderr, "rt_task_shadow: %s\n", strerror(-ret));
   goto failure;
    }
 
    rt_task();
 
    cleanup();
    return 0;
 
 failure:
    cleanup();
    return -1;
}