]>
Commit | Line | Data |
---|---|---|
5a204478 WG |
1 | /* |
2 | * iplink_can.c CAN device support | |
3 | * | |
3bbff7df OH |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU General Public License | |
6 | * as published by the Free Software Foundation; either version | |
7 | * 2 of the License, or (at your option) any later version. | |
5a204478 | 8 | * |
3bbff7df | 9 | * Authors: Wolfgang Grandegger <wg@grandegger.com> |
5a204478 WG |
10 | */ |
11 | ||
12 | #include <stdio.h> | |
13 | #include <stdlib.h> | |
14 | #include <string.h> | |
15 | ||
16 | #include <linux/can/netlink.h> | |
17 | ||
18 | #include "rt_names.h" | |
19 | #include "utils.h" | |
20 | #include "ip_common.h" | |
21 | ||
561e650e | 22 | static void print_usage(FILE *f) |
5a204478 | 23 | { |
561e650e | 24 | fprintf(f, |
5a204478 | 25 | "Usage: ip link set DEVICE type can\n" |
56f5daac SH |
26 | "\t[ bitrate BITRATE [ sample-point SAMPLE-POINT] ] |\n" |
27 | "\t[ tq TQ prop-seg PROP_SEG phase-seg1 PHASE-SEG1\n \t phase-seg2 PHASE-SEG2 [ sjw SJW ] ]\n" | |
5a204478 | 28 | "\n" |
56f5daac SH |
29 | "\t[ dbitrate BITRATE [ dsample-point SAMPLE-POINT] ] |\n" |
30 | "\t[ dtq TQ dprop-seg PROP_SEG dphase-seg1 PHASE-SEG1\n \t dphase-seg2 PHASE-SEG2 [ dsjw SJW ] ]\n" | |
2bfe0470 | 31 | "\n" |
3bbff7df OH |
32 | "\t[ loopback { on | off } ]\n" |
33 | "\t[ listen-only { on | off } ]\n" | |
34 | "\t[ triple-sampling { on | off } ]\n" | |
35 | "\t[ one-shot { on | off } ]\n" | |
36 | "\t[ berr-reporting { on | off } ]\n" | |
2bfe0470 | 37 | "\t[ fd { on | off } ]\n" |
82a307e8 | 38 | "\t[ fd-non-iso { on | off } ]\n" |
d26caee7 | 39 | "\t[ presume-ack { on | off } ]\n" |
5a204478 | 40 | "\n" |
3bbff7df OH |
41 | "\t[ restart-ms TIME-MS ]\n" |
42 | "\t[ restart ]\n" | |
5a204478 | 43 | "\n" |
759fa608 RK |
44 | "\t[ termination { 0..65535 } ]\n" |
45 | "\n" | |
3bbff7df OH |
46 | "\tWhere: BITRATE := { 1..1000000 }\n" |
47 | "\t SAMPLE-POINT := { 0.000..0.999 }\n" | |
48 | "\t TQ := { NUMBER }\n" | |
49 | "\t PROP-SEG := { 1..8 }\n" | |
50 | "\t PHASE-SEG1 := { 1..8 }\n" | |
51 | "\t PHASE-SEG2 := { 1..8 }\n" | |
52 | "\t SJW := { 1..4 }\n" | |
53 | "\t RESTART-MS := { 0 | NUMBER }\n" | |
5a204478 WG |
54 | ); |
55 | } | |
56 | ||
561e650e | 57 | static void usage(void) |
58 | { | |
59 | print_usage(stderr); | |
60 | } | |
61 | ||
5a204478 WG |
62 | static int get_float(float *val, const char *arg) |
63 | { | |
64 | float res; | |
65 | char *ptr; | |
66 | ||
67 | if (!arg || !*arg) | |
68 | return -1; | |
69 | res = strtof(arg, &ptr); | |
70 | if (!ptr || ptr == arg || *ptr) | |
71 | return -1; | |
72 | *val = res; | |
73 | return 0; | |
74 | } | |
75 | ||
56f5daac | 76 | static void set_ctrlmode(char *name, char *arg, |
5a204478 WG |
77 | struct can_ctrlmode *cm, __u32 flags) |
78 | { | |
79 | if (strcmp(arg, "on") == 0) { | |
80 | cm->flags |= flags; | |
81 | } else if (strcmp(arg, "off") != 0) { | |
82 | fprintf(stderr, | |
14645ec2 KR |
83 | "Error: argument of \"%s\" must be \"on\" or \"off\", not \"%s\"\n", |
84 | name, arg); | |
5a204478 WG |
85 | exit(-1); |
86 | } | |
87 | cm->mask |= flags; | |
88 | } | |
89 | ||
90 | static void print_ctrlmode(FILE *f, __u32 cm) | |
91 | { | |
52922600 JF |
92 | open_json_array(PRINT_ANY, is_json_context() ? "ctrlmode" : "<"); |
93 | #define _PF(cmflag, cmname) \ | |
94 | if (cm & cmflag) { \ | |
95 | cm &= ~cmflag; \ | |
96 | print_string(PRINT_ANY, NULL, cm ? "%s," : "%s", cmname); \ | |
5a204478 WG |
97 | } |
98 | _PF(CAN_CTRLMODE_LOOPBACK, "LOOPBACK"); | |
99 | _PF(CAN_CTRLMODE_LISTENONLY, "LISTEN-ONLY"); | |
100 | _PF(CAN_CTRLMODE_3_SAMPLES, "TRIPLE-SAMPLING"); | |
8a517946 WG |
101 | _PF(CAN_CTRLMODE_ONE_SHOT, "ONE-SHOT"); |
102 | _PF(CAN_CTRLMODE_BERR_REPORTING, "BERR-REPORTING"); | |
2bfe0470 | 103 | _PF(CAN_CTRLMODE_FD, "FD"); |
82a307e8 | 104 | _PF(CAN_CTRLMODE_FD_NON_ISO, "FD-NON-ISO"); |
d26caee7 | 105 | _PF(CAN_CTRLMODE_PRESUME_ACK, "PRESUME-ACK"); |
5a204478 WG |
106 | #undef _PF |
107 | if (cm) | |
52922600 JF |
108 | print_hex(PRINT_ANY, NULL, "%x", cm); |
109 | close_json_array(PRINT_ANY, "> "); | |
5a204478 WG |
110 | } |
111 | ||
112 | static int can_parse_opt(struct link_util *lu, int argc, char **argv, | |
113 | struct nlmsghdr *n) | |
114 | { | |
d17b136f | 115 | struct can_bittiming bt = {}, dbt = {}; |
5a204478 WG |
116 | struct can_ctrlmode cm = {0, 0}; |
117 | ||
5a204478 WG |
118 | while (argc > 0) { |
119 | if (matches(*argv, "bitrate") == 0) { | |
120 | NEXT_ARG(); | |
121 | if (get_u32(&bt.bitrate, *argv, 0)) | |
122 | invarg("invalid \"bitrate\" value\n", *argv); | |
123 | } else if (matches(*argv, "sample-point") == 0) { | |
124 | float sp; | |
125 | ||
126 | NEXT_ARG(); | |
127 | if (get_float(&sp, *argv)) | |
128 | invarg("invalid \"sample-point\" value\n", | |
129 | *argv); | |
130 | bt.sample_point = (__u32)(sp * 1000); | |
131 | } else if (matches(*argv, "tq") == 0) { | |
132 | NEXT_ARG(); | |
133 | if (get_u32(&bt.tq, *argv, 0)) | |
134 | invarg("invalid \"tq\" value\n", *argv); | |
135 | } else if (matches(*argv, "prop-seg") == 0) { | |
136 | NEXT_ARG(); | |
137 | if (get_u32(&bt.prop_seg, *argv, 0)) | |
138 | invarg("invalid \"prop-seg\" value\n", *argv); | |
139 | } else if (matches(*argv, "phase-seg1") == 0) { | |
140 | NEXT_ARG(); | |
141 | if (get_u32(&bt.phase_seg1, *argv, 0)) | |
142 | invarg("invalid \"phase-seg1\" value\n", *argv); | |
143 | } else if (matches(*argv, "phase-seg2") == 0) { | |
144 | NEXT_ARG(); | |
145 | if (get_u32(&bt.phase_seg2, *argv, 0)) | |
146 | invarg("invalid \"phase-seg2\" value\n", *argv); | |
147 | } else if (matches(*argv, "sjw") == 0) { | |
148 | NEXT_ARG(); | |
149 | if (get_u32(&bt.sjw, *argv, 0)) | |
150 | invarg("invalid \"sjw\" value\n", *argv); | |
2bfe0470 OH |
151 | } else if (matches(*argv, "dbitrate") == 0) { |
152 | NEXT_ARG(); | |
153 | if (get_u32(&dbt.bitrate, *argv, 0)) | |
154 | invarg("invalid \"dbitrate\" value\n", *argv); | |
155 | } else if (matches(*argv, "dsample-point") == 0) { | |
156 | float sp; | |
157 | ||
158 | NEXT_ARG(); | |
159 | if (get_float(&sp, *argv)) | |
160 | invarg("invalid \"dsample-point\" value\n", *argv); | |
161 | dbt.sample_point = (__u32)(sp * 1000); | |
162 | } else if (matches(*argv, "dtq") == 0) { | |
163 | NEXT_ARG(); | |
164 | if (get_u32(&dbt.tq, *argv, 0)) | |
165 | invarg("invalid \"dtq\" value\n", *argv); | |
166 | } else if (matches(*argv, "dprop-seg") == 0) { | |
167 | NEXT_ARG(); | |
168 | if (get_u32(&dbt.prop_seg, *argv, 0)) | |
169 | invarg("invalid \"dprop-seg\" value\n", *argv); | |
170 | } else if (matches(*argv, "dphase-seg1") == 0) { | |
171 | NEXT_ARG(); | |
172 | if (get_u32(&dbt.phase_seg1, *argv, 0)) | |
173 | invarg("invalid \"dphase-seg1\" value\n", *argv); | |
174 | } else if (matches(*argv, "dphase-seg2") == 0) { | |
175 | NEXT_ARG(); | |
176 | if (get_u32(&dbt.phase_seg2, *argv, 0)) | |
177 | invarg("invalid \"dphase-seg2\" value\n", *argv); | |
178 | } else if (matches(*argv, "dsjw") == 0) { | |
179 | NEXT_ARG(); | |
180 | if (get_u32(&dbt.sjw, *argv, 0)) | |
181 | invarg("invalid \"dsjw\" value\n", *argv); | |
5a204478 WG |
182 | } else if (matches(*argv, "loopback") == 0) { |
183 | NEXT_ARG(); | |
184 | set_ctrlmode("loopback", *argv, &cm, | |
185 | CAN_CTRLMODE_LOOPBACK); | |
186 | } else if (matches(*argv, "listen-only") == 0) { | |
187 | NEXT_ARG(); | |
188 | set_ctrlmode("listen-only", *argv, &cm, | |
189 | CAN_CTRLMODE_LISTENONLY); | |
190 | } else if (matches(*argv, "triple-sampling") == 0) { | |
191 | NEXT_ARG(); | |
192 | set_ctrlmode("triple-sampling", *argv, &cm, | |
193 | CAN_CTRLMODE_3_SAMPLES); | |
8a517946 WG |
194 | } else if (matches(*argv, "one-shot") == 0) { |
195 | NEXT_ARG(); | |
196 | set_ctrlmode("one-shot", *argv, &cm, | |
197 | CAN_CTRLMODE_ONE_SHOT); | |
198 | } else if (matches(*argv, "berr-reporting") == 0) { | |
199 | NEXT_ARG(); | |
200 | set_ctrlmode("berr-reporting", *argv, &cm, | |
201 | CAN_CTRLMODE_BERR_REPORTING); | |
2bfe0470 OH |
202 | } else if (matches(*argv, "fd") == 0) { |
203 | NEXT_ARG(); | |
204 | set_ctrlmode("fd", *argv, &cm, | |
205 | CAN_CTRLMODE_FD); | |
82a307e8 OH |
206 | } else if (matches(*argv, "fd-non-iso") == 0) { |
207 | NEXT_ARG(); | |
208 | set_ctrlmode("fd-non-iso", *argv, &cm, | |
209 | CAN_CTRLMODE_FD_NON_ISO); | |
d26caee7 NEB |
210 | } else if (matches(*argv, "presume-ack") == 0) { |
211 | NEXT_ARG(); | |
212 | set_ctrlmode("presume-ack", *argv, &cm, | |
213 | CAN_CTRLMODE_PRESUME_ACK); | |
5a204478 WG |
214 | } else if (matches(*argv, "restart") == 0) { |
215 | __u32 val = 1; | |
216 | ||
217 | addattr32(n, 1024, IFLA_CAN_RESTART, val); | |
218 | } else if (matches(*argv, "restart-ms") == 0) { | |
219 | __u32 val; | |
220 | ||
221 | NEXT_ARG(); | |
222 | if (get_u32(&val, *argv, 0)) | |
223 | invarg("invalid \"restart-ms\" value\n", *argv); | |
224 | addattr32(n, 1024, IFLA_CAN_RESTART_MS, val); | |
759fa608 RK |
225 | } else if (matches(*argv, "termination") == 0) { |
226 | __u16 val; | |
227 | ||
228 | NEXT_ARG(); | |
229 | if (get_u16(&val, *argv, 0)) | |
230 | invarg("invalid \"termination\" value\n", | |
231 | *argv); | |
232 | addattr16(n, 1024, IFLA_CAN_TERMINATION, val); | |
5a204478 WG |
233 | } else if (matches(*argv, "help") == 0) { |
234 | usage(); | |
235 | return -1; | |
236 | } else { | |
14645ec2 | 237 | fprintf(stderr, "can: unknown option \"%s\"\n", *argv); |
5a204478 WG |
238 | usage(); |
239 | return -1; | |
240 | } | |
241 | argc--, argv++; | |
242 | } | |
243 | ||
244 | if (bt.bitrate || bt.tq) | |
245 | addattr_l(n, 1024, IFLA_CAN_BITTIMING, &bt, sizeof(bt)); | |
2bfe0470 OH |
246 | if (dbt.bitrate || dbt.tq) |
247 | addattr_l(n, 1024, IFLA_CAN_DATA_BITTIMING, &dbt, sizeof(dbt)); | |
5a204478 WG |
248 | if (cm.mask) |
249 | addattr_l(n, 1024, IFLA_CAN_CTRLMODE, &cm, sizeof(cm)); | |
250 | ||
251 | return 0; | |
252 | } | |
253 | ||
254 | static const char *can_state_names[] = { | |
3bbff7df OH |
255 | [CAN_STATE_ERROR_ACTIVE] = "ERROR-ACTIVE", |
256 | [CAN_STATE_ERROR_WARNING] = "ERROR-WARNING", | |
257 | [CAN_STATE_ERROR_PASSIVE] = "ERROR-PASSIVE", | |
258 | [CAN_STATE_BUS_OFF] = "BUS-OFF", | |
259 | [CAN_STATE_STOPPED] = "STOPPED", | |
260 | [CAN_STATE_SLEEPING] = "SLEEPING" | |
5a204478 WG |
261 | }; |
262 | ||
52922600 JF |
263 | static void can_print_json_timing_min_max(const char *attr, int min, int max) |
264 | { | |
265 | open_json_object(attr); | |
266 | print_int(PRINT_JSON, "min", NULL, min); | |
267 | print_int(PRINT_JSON, "max", NULL, max); | |
268 | close_json_object(); | |
269 | } | |
270 | ||
5a204478 WG |
271 | static void can_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) |
272 | { | |
273 | if (!tb) | |
274 | return; | |
275 | ||
276 | if (tb[IFLA_CAN_CTRLMODE]) { | |
277 | struct can_ctrlmode *cm = RTA_DATA(tb[IFLA_CAN_CTRLMODE]); | |
278 | ||
279 | if (cm->flags) | |
280 | print_ctrlmode(f, cm->flags); | |
281 | } | |
282 | ||
283 | if (tb[IFLA_CAN_STATE]) { | |
9f1370c0 | 284 | uint32_t state = rta_getattr_u32(tb[IFLA_CAN_STATE]); |
5a204478 | 285 | |
52922600 JF |
286 | if (state <= CAN_STATE_MAX) |
287 | print_string(PRINT_ANY, "state", "state %s ", | |
288 | can_state_names[state]); | |
289 | else | |
290 | print_null(PRINT_ANY, "state", "state UNKNOWN", NULL); | |
5a204478 WG |
291 | } |
292 | ||
8a517946 WG |
293 | if (tb[IFLA_CAN_BERR_COUNTER]) { |
294 | struct can_berr_counter *bc = | |
295 | RTA_DATA(tb[IFLA_CAN_BERR_COUNTER]); | |
296 | ||
52922600 JF |
297 | if (is_json_context()) { |
298 | open_json_object("berr_counter"); | |
299 | print_int(PRINT_JSON, "tx", NULL, bc->txerr); | |
300 | print_int(PRINT_JSON, "rx", NULL, bc->rxerr); | |
301 | close_json_object(); | |
302 | } else { | |
303 | fprintf(f, "(berr-counter tx %d rx %d) ", | |
304 | bc->txerr, bc->rxerr); | |
305 | } | |
8a517946 WG |
306 | } |
307 | ||
5a204478 WG |
308 | if (tb[IFLA_CAN_RESTART_MS]) { |
309 | __u32 *restart_ms = RTA_DATA(tb[IFLA_CAN_RESTART_MS]); | |
310 | ||
52922600 JF |
311 | print_int(PRINT_ANY, |
312 | "restart_ms", | |
313 | "restart-ms %d ", | |
314 | *restart_ms); | |
5a204478 WG |
315 | } |
316 | ||
759fa608 RK |
317 | /* bittiming is irrelevant if fixed bitrate is defined */ |
318 | if (tb[IFLA_CAN_BITTIMING] && !tb[IFLA_CAN_BITRATE_CONST]) { | |
5a204478 WG |
319 | struct can_bittiming *bt = RTA_DATA(tb[IFLA_CAN_BITTIMING]); |
320 | ||
52922600 JF |
321 | if (is_json_context()) { |
322 | open_json_object("bittiming"); | |
323 | print_int(PRINT_ANY, "bitrate", NULL, bt->bitrate); | |
324 | jsonw_float_field_fmt(get_json_writer(), | |
325 | "sample_point", "%.3f", | |
326 | (float) bt->sample_point / 1000.); | |
327 | print_int(PRINT_ANY, "tq", NULL, bt->tq); | |
328 | print_int(PRINT_ANY, "prop_seg", NULL, bt->prop_seg); | |
329 | print_int(PRINT_ANY, "phase_seg1", | |
330 | NULL, bt->phase_seg1); | |
331 | print_int(PRINT_ANY, "phase_seg2", | |
332 | NULL, bt->phase_seg2); | |
333 | print_int(PRINT_ANY, "sjw", NULL, bt->sjw); | |
334 | close_json_object(); | |
335 | } else { | |
336 | fprintf(f, "\n bitrate %d sample-point %.3f ", | |
337 | bt->bitrate, (float) bt->sample_point / 1000.); | |
338 | fprintf(f, | |
339 | "\n tq %d prop-seg %d phase-seg1 %d phase-seg2 %d sjw %d", | |
340 | bt->tq, bt->prop_seg, | |
341 | bt->phase_seg1, bt->phase_seg2, | |
342 | bt->sjw); | |
343 | } | |
5a204478 WG |
344 | } |
345 | ||
759fa608 RK |
346 | /* bittiming const is irrelevant if fixed bitrate is defined */ |
347 | if (tb[IFLA_CAN_BITTIMING_CONST] && !tb[IFLA_CAN_BITRATE_CONST]) { | |
5a204478 WG |
348 | struct can_bittiming_const *btc = |
349 | RTA_DATA(tb[IFLA_CAN_BITTIMING_CONST]); | |
350 | ||
52922600 JF |
351 | if (is_json_context()) { |
352 | open_json_object("bittiming_const"); | |
353 | print_string(PRINT_JSON, "name", NULL, btc->name); | |
354 | can_print_json_timing_min_max("tseg1", | |
355 | btc->tseg1_min, | |
356 | btc->tseg1_max); | |
357 | can_print_json_timing_min_max("tseg2", | |
358 | btc->tseg2_min, | |
359 | btc->tseg2_max); | |
360 | can_print_json_timing_min_max("sjw", 1, btc->sjw_max); | |
361 | can_print_json_timing_min_max("brp", | |
362 | btc->brp_min, | |
363 | btc->brp_max); | |
364 | print_int(PRINT_JSON, "brp_inc", NULL, btc->brp_inc); | |
365 | close_json_object(); | |
366 | } else { | |
367 | fprintf(f, "\n %s: tseg1 %d..%d tseg2 %d..%d " | |
368 | "sjw 1..%d brp %d..%d brp-inc %d", | |
369 | btc->name, btc->tseg1_min, btc->tseg1_max, | |
370 | btc->tseg2_min, btc->tseg2_max, btc->sjw_max, | |
371 | btc->brp_min, btc->brp_max, btc->brp_inc); | |
372 | } | |
5a204478 WG |
373 | } |
374 | ||
759fa608 RK |
375 | if (tb[IFLA_CAN_BITRATE_CONST]) { |
376 | __u32 *bitrate_const = RTA_DATA(tb[IFLA_CAN_BITRATE_CONST]); | |
377 | int bitrate_cnt = RTA_PAYLOAD(tb[IFLA_CAN_BITRATE_CONST]) / | |
52922600 | 378 | sizeof(*bitrate_const); |
759fa608 RK |
379 | int i; |
380 | __u32 bitrate = 0; | |
381 | ||
382 | if (tb[IFLA_CAN_BITTIMING]) { | |
383 | struct can_bittiming *bt = | |
52922600 | 384 | RTA_DATA(tb[IFLA_CAN_BITTIMING]); |
759fa608 RK |
385 | bitrate = bt->bitrate; |
386 | } | |
387 | ||
52922600 JF |
388 | if (is_json_context()) { |
389 | print_uint(PRINT_JSON, | |
390 | "bittiming_bitrate", | |
391 | NULL, bitrate); | |
392 | open_json_array(PRINT_JSON, "bitrate_const"); | |
393 | for (i = 0; i < bitrate_cnt; ++i) | |
394 | print_uint(PRINT_JSON, NULL, NULL, | |
395 | bitrate_const[i]); | |
396 | close_json_array(PRINT_JSON, NULL); | |
397 | } else { | |
398 | fprintf(f, "\n bitrate %u", bitrate); | |
399 | fprintf(f, "\n ["); | |
400 | ||
401 | for (i = 0; i < bitrate_cnt - 1; ++i) { | |
402 | /* This will keep lines below 80 signs */ | |
403 | if (!(i % 6) && i) | |
404 | fprintf(f, "\n "); | |
405 | ||
406 | fprintf(f, "%8u, ", bitrate_const[i]); | |
407 | } | |
759fa608 | 408 | |
759fa608 RK |
409 | if (!(i % 6) && i) |
410 | fprintf(f, "\n "); | |
52922600 | 411 | fprintf(f, "%8u ]", bitrate_const[i]); |
759fa608 | 412 | } |
759fa608 RK |
413 | } |
414 | ||
415 | /* data bittiming is irrelevant if fixed bitrate is defined */ | |
416 | if (tb[IFLA_CAN_DATA_BITTIMING] && !tb[IFLA_CAN_DATA_BITRATE_CONST]) { | |
2bfe0470 OH |
417 | struct can_bittiming *dbt = |
418 | RTA_DATA(tb[IFLA_CAN_DATA_BITTIMING]); | |
419 | ||
52922600 JF |
420 | if (is_json_context()) { |
421 | open_json_object("data_bittiming"); | |
422 | print_int(PRINT_JSON, "bitrate", NULL, dbt->bitrate); | |
423 | jsonw_float_field_fmt(get_json_writer(), | |
424 | "sample_point", | |
425 | "%.3f", | |
426 | (float) dbt->sample_point / 1000.); | |
427 | print_int(PRINT_JSON, "tq", NULL, dbt->tq); | |
428 | print_int(PRINT_JSON, "prop_seg", NULL, dbt->prop_seg); | |
429 | print_int(PRINT_JSON, "phase_seg1", | |
430 | NULL, dbt->phase_seg1); | |
431 | print_int(PRINT_JSON, "phase_seg2", | |
432 | NULL, dbt->phase_seg2); | |
433 | print_int(PRINT_JSON, "sjw", NULL, dbt->sjw); | |
434 | close_json_object(); | |
435 | } else { | |
436 | fprintf(f, "\n dbitrate %d dsample-point %.3f ", | |
437 | dbt->bitrate, | |
438 | (float) dbt->sample_point / 1000.); | |
439 | fprintf(f, "\n dtq %d dprop-seg %d dphase-seg1 %d " | |
440 | "dphase-seg2 %d dsjw %d", | |
441 | dbt->tq, dbt->prop_seg, dbt->phase_seg1, | |
442 | dbt->phase_seg2, dbt->sjw); | |
443 | } | |
2bfe0470 OH |
444 | } |
445 | ||
759fa608 RK |
446 | /* data bittiming const is irrelevant if fixed bitrate is defined */ |
447 | if (tb[IFLA_CAN_DATA_BITTIMING_CONST] && | |
448 | !tb[IFLA_CAN_DATA_BITRATE_CONST]) { | |
2bfe0470 OH |
449 | struct can_bittiming_const *dbtc = |
450 | RTA_DATA(tb[IFLA_CAN_DATA_BITTIMING_CONST]); | |
451 | ||
52922600 JF |
452 | if (is_json_context()) { |
453 | open_json_object("data_bittiming_const"); | |
454 | print_string(PRINT_JSON, "name", NULL, dbtc->name); | |
455 | can_print_json_timing_min_max("tseg1", | |
456 | dbtc->tseg1_min, | |
457 | dbtc->tseg1_max); | |
458 | can_print_json_timing_min_max("tseg2", | |
459 | dbtc->tseg2_min, | |
460 | dbtc->tseg2_max); | |
461 | can_print_json_timing_min_max("sjw", 1, dbtc->sjw_max); | |
462 | can_print_json_timing_min_max("brp", | |
463 | dbtc->brp_min, | |
464 | dbtc->brp_max); | |
465 | ||
466 | print_int(PRINT_JSON, "brp_inc", NULL, dbtc->brp_inc); | |
467 | close_json_object(); | |
468 | } else { | |
469 | fprintf(f, "\n %s: dtseg1 %d..%d dtseg2 %d..%d " | |
470 | "dsjw 1..%d dbrp %d..%d dbrp-inc %d", | |
471 | dbtc->name, dbtc->tseg1_min, dbtc->tseg1_max, | |
472 | dbtc->tseg2_min, dbtc->tseg2_max, dbtc->sjw_max, | |
473 | dbtc->brp_min, dbtc->brp_max, dbtc->brp_inc); | |
474 | } | |
2bfe0470 OH |
475 | } |
476 | ||
759fa608 RK |
477 | if (tb[IFLA_CAN_DATA_BITRATE_CONST]) { |
478 | __u32 *dbitrate_const = | |
52922600 | 479 | RTA_DATA(tb[IFLA_CAN_DATA_BITRATE_CONST]); |
759fa608 | 480 | int dbitrate_cnt = |
52922600 JF |
481 | RTA_PAYLOAD(tb[IFLA_CAN_DATA_BITRATE_CONST]) / |
482 | sizeof(*dbitrate_const); | |
759fa608 RK |
483 | int i; |
484 | __u32 dbitrate = 0; | |
485 | ||
486 | if (tb[IFLA_CAN_DATA_BITTIMING]) { | |
487 | struct can_bittiming *dbt = | |
52922600 | 488 | RTA_DATA(tb[IFLA_CAN_DATA_BITTIMING]); |
759fa608 RK |
489 | dbitrate = dbt->bitrate; |
490 | } | |
491 | ||
52922600 JF |
492 | if (is_json_context()) { |
493 | print_uint(PRINT_JSON, "data_bittiming_bitrate", | |
494 | NULL, dbitrate); | |
495 | open_json_array(PRINT_JSON, "data_bitrate_const"); | |
496 | for (i = 0; i < dbitrate_cnt; ++i) | |
497 | print_uint(PRINT_JSON, NULL, NULL, | |
498 | dbitrate_const[i]); | |
499 | close_json_array(PRINT_JSON, NULL); | |
500 | } else { | |
501 | fprintf(f, "\n dbitrate %u", dbitrate); | |
502 | fprintf(f, "\n ["); | |
503 | ||
504 | for (i = 0; i < dbitrate_cnt - 1; ++i) { | |
505 | /* This will keep lines below 80 signs */ | |
506 | if (!(i % 6) && i) | |
507 | fprintf(f, "\n "); | |
508 | ||
509 | fprintf(f, "%8u, ", dbitrate_const[i]); | |
510 | } | |
759fa608 | 511 | |
759fa608 RK |
512 | if (!(i % 6) && i) |
513 | fprintf(f, "\n "); | |
52922600 | 514 | fprintf(f, "%8u ]", dbitrate_const[i]); |
759fa608 | 515 | } |
759fa608 RK |
516 | } |
517 | ||
518 | if (tb[IFLA_CAN_TERMINATION_CONST] && tb[IFLA_CAN_TERMINATION]) { | |
519 | __u16 *trm = RTA_DATA(tb[IFLA_CAN_TERMINATION]); | |
520 | __u16 *trm_const = RTA_DATA(tb[IFLA_CAN_TERMINATION_CONST]); | |
521 | int trm_cnt = RTA_PAYLOAD(tb[IFLA_CAN_TERMINATION_CONST]) / | |
52922600 | 522 | sizeof(*trm_const); |
759fa608 RK |
523 | int i; |
524 | ||
52922600 JF |
525 | if (is_json_context()) { |
526 | print_hu(PRINT_JSON, "termination", NULL, *trm); | |
527 | open_json_array(PRINT_JSON, "termination_const"); | |
528 | for (i = 0; i < trm_cnt; ++i) | |
529 | print_hu(PRINT_JSON, NULL, NULL, trm_const[i]); | |
530 | close_json_array(PRINT_JSON, NULL); | |
531 | } else { | |
532 | fprintf(f, "\n termination %hu [ ", *trm); | |
759fa608 | 533 | |
52922600 JF |
534 | for (i = 0; i < trm_cnt - 1; ++i) |
535 | fprintf(f, "%hu, ", trm_const[i]); | |
759fa608 | 536 | |
52922600 JF |
537 | fprintf(f, "%hu ]", trm_const[i]); |
538 | } | |
759fa608 RK |
539 | } |
540 | ||
5a204478 WG |
541 | if (tb[IFLA_CAN_CLOCK]) { |
542 | struct can_clock *clock = RTA_DATA(tb[IFLA_CAN_CLOCK]); | |
543 | ||
52922600 JF |
544 | print_int(PRINT_ANY, |
545 | "clock", | |
546 | "\n clock %d", | |
547 | clock->freq); | |
5a204478 WG |
548 | } |
549 | ||
550 | } | |
551 | ||
552 | static void can_print_xstats(struct link_util *lu, | |
553 | FILE *f, struct rtattr *xstats) | |
554 | { | |
555 | struct can_device_stats *stats; | |
556 | ||
557 | if (xstats && RTA_PAYLOAD(xstats) == sizeof(*stats)) { | |
558 | stats = RTA_DATA(xstats); | |
52922600 JF |
559 | |
560 | if (is_json_context()) { | |
561 | print_int(PRINT_JSON, "restarts", | |
562 | NULL, stats->restarts); | |
563 | print_int(PRINT_JSON, "bus_error", | |
564 | NULL, stats->bus_error); | |
565 | print_int(PRINT_JSON, "arbitration_lost", | |
566 | NULL, stats->arbitration_lost); | |
567 | print_int(PRINT_JSON, "error_warning", | |
568 | NULL, stats->error_warning); | |
569 | print_int(PRINT_JSON, "error_passive", | |
570 | NULL, stats->error_passive); | |
571 | print_int(PRINT_JSON, "bus_off", NULL, stats->bus_off); | |
572 | } else { | |
573 | fprintf(f, "\n re-started bus-errors arbit-lost " | |
574 | "error-warn error-pass bus-off"); | |
575 | fprintf(f, "\n %-10d %-10d %-10d %-10d %-10d %-10d", | |
576 | stats->restarts, stats->bus_error, | |
577 | stats->arbitration_lost, stats->error_warning, | |
578 | stats->error_passive, stats->bus_off); | |
579 | } | |
5a204478 WG |
580 | } |
581 | } | |
582 | ||
561e650e | 583 | static void can_print_help(struct link_util *lu, int argc, char **argv, |
52922600 | 584 | FILE *f) |
561e650e | 585 | { |
586 | print_usage(f); | |
587 | } | |
588 | ||
5a204478 WG |
589 | struct link_util can_link_util = { |
590 | .id = "can", | |
591 | .maxattr = IFLA_CAN_MAX, | |
592 | .parse_opt = can_parse_opt, | |
593 | .print_opt = can_print_opt, | |
4806867a | 594 | .print_xstats = can_print_xstats, |
561e650e | 595 | .print_help = can_print_help, |
5a204478 | 596 | }; |