• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * iplink_can.c	CAN device support
3  *
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.
8  *
9  * Authors:	Wolfgang Grandegger <wg@grandegger.com>
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 
print_usage(FILE * f)22 static void print_usage(FILE *f)
23 {
24 	fprintf(f,
25 		"Usage: ip link set DEVICE type can\n"
26 		"\t[ bitrate BITRATE [ sample-point SAMPLE-POINT] ] | \n"
27 		"\t[ tq TQ prop-seg PROP_SEG phase-seg1 PHASE-SEG1\n "
28 		"\t  phase-seg2 PHASE-SEG2 [ sjw SJW ] ]\n"
29 		"\n"
30 		"\t[ dbitrate BITRATE [ dsample-point SAMPLE-POINT] ] | \n"
31 		"\t[ dtq TQ dprop-seg PROP_SEG dphase-seg1 PHASE-SEG1\n "
32 		"\t  dphase-seg2 PHASE-SEG2 [ dsjw SJW ] ]\n"
33 		"\n"
34 		"\t[ loopback { on | off } ]\n"
35 		"\t[ listen-only { on | off } ]\n"
36 		"\t[ triple-sampling { on | off } ]\n"
37 		"\t[ one-shot { on | off } ]\n"
38 		"\t[ berr-reporting { on | off } ]\n"
39 		"\t[ fd { on | off } ]\n"
40 		"\t[ fd-non-iso { on | off } ]\n"
41 		"\t[ presume-ack { on | off } ]\n"
42 		"\n"
43 		"\t[ restart-ms TIME-MS ]\n"
44 		"\t[ restart ]\n"
45 		"\n"
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"
54 		);
55 }
56 
usage(void)57 static void usage(void)
58 {
59 	print_usage(stderr);
60 }
61 
get_float(float * val,const char * arg)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 
set_ctrlmode(char * name,char * arg,struct can_ctrlmode * cm,__u32 flags)76 static void set_ctrlmode(char* name, char *arg,
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,
83 			"Error: argument of \"%s\" must be \"on\" or \"off\", not \"%s\"\n",
84 			name, arg);
85 		exit(-1);
86 	}
87 	cm->mask |= flags;
88 }
89 
print_ctrlmode(FILE * f,__u32 cm)90 static void print_ctrlmode(FILE *f, __u32 cm)
91 {
92 	fprintf(f, "<");
93 #define _PF(cmflag, cmname)					\
94 	if (cm & cmflag) {					\
95 		cm &= ~cmflag;					\
96 		fprintf(f, "%s%s", cmname, cm ? "," : "");	\
97 	}
98 	_PF(CAN_CTRLMODE_LOOPBACK, "LOOPBACK");
99 	_PF(CAN_CTRLMODE_LISTENONLY, "LISTEN-ONLY");
100 	_PF(CAN_CTRLMODE_3_SAMPLES, "TRIPLE-SAMPLING");
101 	_PF(CAN_CTRLMODE_ONE_SHOT, "ONE-SHOT");
102 	_PF(CAN_CTRLMODE_BERR_REPORTING, "BERR-REPORTING");
103 	_PF(CAN_CTRLMODE_FD, "FD");
104 	_PF(CAN_CTRLMODE_FD_NON_ISO, "FD-NON-ISO");
105 	_PF(CAN_CTRLMODE_PRESUME_ACK, "PRESUME-ACK");
106 #undef _PF
107 	if (cm)
108 		fprintf(f, "%x", cm);
109 	fprintf(f, "> ");
110 }
111 
can_parse_opt(struct link_util * lu,int argc,char ** argv,struct nlmsghdr * n)112 static int can_parse_opt(struct link_util *lu, int argc, char **argv,
113 			 struct nlmsghdr *n)
114 {
115 	struct can_bittiming bt, dbt;
116 	struct can_ctrlmode cm = {0, 0};
117 
118 	memset(&bt, 0, sizeof(bt));
119 	memset(&dbt, 0, sizeof(dbt));
120 	while (argc > 0) {
121 		if (matches(*argv, "bitrate") == 0) {
122 			NEXT_ARG();
123 			if (get_u32(&bt.bitrate, *argv, 0))
124 				invarg("invalid \"bitrate\" value\n", *argv);
125 		} else if (matches(*argv, "sample-point") == 0) {
126 			float sp;
127 
128 			NEXT_ARG();
129 			if (get_float(&sp, *argv))
130 				invarg("invalid \"sample-point\" value\n",
131 				       *argv);
132 			bt.sample_point = (__u32)(sp * 1000);
133 		} else if (matches(*argv, "tq") == 0) {
134 			NEXT_ARG();
135 			if (get_u32(&bt.tq, *argv, 0))
136 				invarg("invalid \"tq\" value\n", *argv);
137 		} else if (matches(*argv, "prop-seg") == 0) {
138 			NEXT_ARG();
139 			if (get_u32(&bt.prop_seg, *argv, 0))
140 				invarg("invalid \"prop-seg\" value\n", *argv);
141 		} else if (matches(*argv, "phase-seg1") == 0) {
142 			NEXT_ARG();
143 			if (get_u32(&bt.phase_seg1, *argv, 0))
144 				invarg("invalid \"phase-seg1\" value\n", *argv);
145 		} else if (matches(*argv, "phase-seg2") == 0) {
146 			NEXT_ARG();
147 			if (get_u32(&bt.phase_seg2, *argv, 0))
148 				invarg("invalid \"phase-seg2\" value\n", *argv);
149 		} else if (matches(*argv, "sjw") == 0) {
150 			NEXT_ARG();
151 			if (get_u32(&bt.sjw, *argv, 0))
152 				invarg("invalid \"sjw\" value\n", *argv);
153 		} else if (matches(*argv, "dbitrate") == 0) {
154 			NEXT_ARG();
155 			if (get_u32(&dbt.bitrate, *argv, 0))
156 				invarg("invalid \"dbitrate\" value\n", *argv);
157 		} else if (matches(*argv, "dsample-point") == 0) {
158 			float sp;
159 
160 			NEXT_ARG();
161 			if (get_float(&sp, *argv))
162 				invarg("invalid \"dsample-point\" value\n", *argv);
163 			dbt.sample_point = (__u32)(sp * 1000);
164 		} else if (matches(*argv, "dtq") == 0) {
165 			NEXT_ARG();
166 			if (get_u32(&dbt.tq, *argv, 0))
167 				invarg("invalid \"dtq\" value\n", *argv);
168 		} else if (matches(*argv, "dprop-seg") == 0) {
169 			NEXT_ARG();
170 			if (get_u32(&dbt.prop_seg, *argv, 0))
171 				invarg("invalid \"dprop-seg\" value\n", *argv);
172 		} else if (matches(*argv, "dphase-seg1") == 0) {
173 			NEXT_ARG();
174 			if (get_u32(&dbt.phase_seg1, *argv, 0))
175 				invarg("invalid \"dphase-seg1\" value\n", *argv);
176 		} else if (matches(*argv, "dphase-seg2") == 0) {
177 			NEXT_ARG();
178 			if (get_u32(&dbt.phase_seg2, *argv, 0))
179 				invarg("invalid \"dphase-seg2\" value\n", *argv);
180 		} else if (matches(*argv, "dsjw") == 0) {
181 			NEXT_ARG();
182 			if (get_u32(&dbt.sjw, *argv, 0))
183 				invarg("invalid \"dsjw\" value\n", *argv);
184 		} else if (matches(*argv, "loopback") == 0) {
185 			NEXT_ARG();
186 			set_ctrlmode("loopback", *argv, &cm,
187 				     CAN_CTRLMODE_LOOPBACK);
188 		} else if (matches(*argv, "listen-only") == 0) {
189 			NEXT_ARG();
190 			set_ctrlmode("listen-only", *argv, &cm,
191 				     CAN_CTRLMODE_LISTENONLY);
192 		} else if (matches(*argv, "triple-sampling") == 0) {
193 			NEXT_ARG();
194 			set_ctrlmode("triple-sampling", *argv, &cm,
195 				     CAN_CTRLMODE_3_SAMPLES);
196 		} else if (matches(*argv, "one-shot") == 0) {
197 			NEXT_ARG();
198 			set_ctrlmode("one-shot", *argv, &cm,
199 				     CAN_CTRLMODE_ONE_SHOT);
200 		} else if (matches(*argv, "berr-reporting") == 0) {
201 			NEXT_ARG();
202 			set_ctrlmode("berr-reporting", *argv, &cm,
203 				     CAN_CTRLMODE_BERR_REPORTING);
204 		} else if (matches(*argv, "fd") == 0) {
205 			NEXT_ARG();
206 			set_ctrlmode("fd", *argv, &cm,
207 				     CAN_CTRLMODE_FD);
208 		} else if (matches(*argv, "fd-non-iso") == 0) {
209 			NEXT_ARG();
210 			set_ctrlmode("fd-non-iso", *argv, &cm,
211 				     CAN_CTRLMODE_FD_NON_ISO);
212 		} else if (matches(*argv, "presume-ack") == 0) {
213 			NEXT_ARG();
214 			set_ctrlmode("presume-ack", *argv, &cm,
215 				     CAN_CTRLMODE_PRESUME_ACK);
216 		} else if (matches(*argv, "restart") == 0) {
217 			__u32 val = 1;
218 
219 			addattr32(n, 1024, IFLA_CAN_RESTART, val);
220 		} else if (matches(*argv, "restart-ms") == 0) {
221 			__u32 val;
222 
223 			NEXT_ARG();
224 			if (get_u32(&val, *argv, 0))
225 				invarg("invalid \"restart-ms\" value\n", *argv);
226 			addattr32(n, 1024, IFLA_CAN_RESTART_MS, val);
227 		} else if (matches(*argv, "help") == 0) {
228 			usage();
229 			return -1;
230 		} else {
231 			fprintf(stderr, "can: unknown option \"%s\"\n", *argv);
232 			usage();
233 			return -1;
234 		}
235 		argc--, argv++;
236 	}
237 
238 	if (bt.bitrate || bt.tq)
239 		addattr_l(n, 1024, IFLA_CAN_BITTIMING, &bt, sizeof(bt));
240 	if (dbt.bitrate || dbt.tq)
241 		addattr_l(n, 1024, IFLA_CAN_DATA_BITTIMING, &dbt, sizeof(dbt));
242 	if (cm.mask)
243 		addattr_l(n, 1024, IFLA_CAN_CTRLMODE, &cm, sizeof(cm));
244 
245 	return 0;
246 }
247 
248 static const char *can_state_names[] = {
249 	[CAN_STATE_ERROR_ACTIVE] = "ERROR-ACTIVE",
250 	[CAN_STATE_ERROR_WARNING] = "ERROR-WARNING",
251 	[CAN_STATE_ERROR_PASSIVE] = "ERROR-PASSIVE",
252 	[CAN_STATE_BUS_OFF] = "BUS-OFF",
253 	[CAN_STATE_STOPPED] = "STOPPED",
254 	[CAN_STATE_SLEEPING] = "SLEEPING"
255 };
256 
can_print_opt(struct link_util * lu,FILE * f,struct rtattr * tb[])257 static void can_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
258 {
259 	if (!tb)
260 		return;
261 
262 	if (tb[IFLA_CAN_CTRLMODE]) {
263 		struct can_ctrlmode *cm = RTA_DATA(tb[IFLA_CAN_CTRLMODE]);
264 
265 		if (cm->flags)
266 			print_ctrlmode(f, cm->flags);
267 	}
268 
269 	if (tb[IFLA_CAN_STATE]) {
270 		int *state = RTA_DATA(tb[IFLA_CAN_STATE]);
271 
272 		fprintf(f, "state %s ", *state <= CAN_STATE_MAX ?
273 			can_state_names[*state] : "UNKNOWN");
274 	}
275 
276 	if (tb[IFLA_CAN_BERR_COUNTER]) {
277 		struct can_berr_counter *bc =
278 			RTA_DATA(tb[IFLA_CAN_BERR_COUNTER]);
279 
280 		fprintf(f, "(berr-counter tx %d rx %d) ", bc->txerr, bc->rxerr);
281 	}
282 
283 	if (tb[IFLA_CAN_RESTART_MS]) {
284 		__u32 *restart_ms = RTA_DATA(tb[IFLA_CAN_RESTART_MS]);
285 
286 		fprintf(f, "restart-ms %d ", *restart_ms);
287 	}
288 
289 	if (tb[IFLA_CAN_BITTIMING]) {
290 		struct can_bittiming *bt = RTA_DATA(tb[IFLA_CAN_BITTIMING]);
291 
292 		fprintf(f, "\n	  "
293 			"bitrate %d sample-point %.3f ",
294 			bt->bitrate, (float)bt->sample_point / 1000.);
295 		fprintf(f, "\n	  "
296 			"tq %d prop-seg %d phase-seg1 %d phase-seg2 %d sjw %d",
297 			bt->tq, bt->prop_seg, bt->phase_seg1, bt->phase_seg2,
298 			bt->sjw);
299 	}
300 
301 	if (tb[IFLA_CAN_BITTIMING_CONST]) {
302 		struct can_bittiming_const *btc =
303 			RTA_DATA(tb[IFLA_CAN_BITTIMING_CONST]);
304 
305 		fprintf(f, "\n	  "
306 			"%s: tseg1 %d..%d tseg2 %d..%d "
307 			"sjw 1..%d brp %d..%d brp-inc %d",
308 			btc->name, btc->tseg1_min, btc->tseg1_max,
309 			btc->tseg2_min, btc->tseg2_max, btc->sjw_max,
310 			btc->brp_min, btc->brp_max, btc->brp_inc);
311 	}
312 
313 	if (tb[IFLA_CAN_DATA_BITTIMING]) {
314 		struct can_bittiming *dbt =
315 			RTA_DATA(tb[IFLA_CAN_DATA_BITTIMING]);
316 
317 		fprintf(f, "\n	  "
318 			"dbitrate %d dsample-point %.3f ",
319 			dbt->bitrate, (float)dbt->sample_point / 1000.);
320 		fprintf(f, "\n	  "
321 			"dtq %d dprop-seg %d dphase-seg1 %d "
322 			"dphase-seg2 %d dsjw %d",
323 			dbt->tq, dbt->prop_seg, dbt->phase_seg1,
324 			dbt->phase_seg2, dbt->sjw);
325 	}
326 
327 	if (tb[IFLA_CAN_DATA_BITTIMING_CONST]) {
328 		struct can_bittiming_const *dbtc =
329 			RTA_DATA(tb[IFLA_CAN_DATA_BITTIMING_CONST]);
330 
331 		fprintf(f, "\n	  "
332 			"%s: dtseg1 %d..%d dtseg2 %d..%d "
333 			"dsjw 1..%d dbrp %d..%d dbrp-inc %d",
334 			dbtc->name, dbtc->tseg1_min, dbtc->tseg1_max,
335 			dbtc->tseg2_min, dbtc->tseg2_max, dbtc->sjw_max,
336 			dbtc->brp_min, dbtc->brp_max, dbtc->brp_inc);
337 	}
338 
339 	if (tb[IFLA_CAN_CLOCK]) {
340 		struct can_clock *clock = RTA_DATA(tb[IFLA_CAN_CLOCK]);
341 
342 		fprintf(f, "\n	  clock %d", clock->freq);
343 	}
344 
345 }
346 
can_print_xstats(struct link_util * lu,FILE * f,struct rtattr * xstats)347 static void can_print_xstats(struct link_util *lu,
348 			     FILE *f, struct rtattr *xstats)
349 {
350 	struct can_device_stats *stats;
351 
352 	if (xstats && RTA_PAYLOAD(xstats) == sizeof(*stats)) {
353 		stats = RTA_DATA(xstats);
354 		fprintf(f, "\n	  "
355 			"re-started bus-errors arbit-lost "
356 			"error-warn error-pass bus-off");
357 		fprintf(f, "\n	  %-10d %-10d %-10d %-10d %-10d %-10d",
358 			stats->restarts, stats->bus_error,
359 			stats->arbitration_lost, stats->error_warning,
360 			stats->error_passive, stats->bus_off);
361 	}
362 }
363 
can_print_help(struct link_util * lu,int argc,char ** argv,FILE * f)364 static void can_print_help(struct link_util *lu, int argc, char **argv,
365 	FILE *f)
366 {
367 	print_usage(f);
368 }
369 
370 struct link_util can_link_util = {
371 	.id		= "can",
372 	.maxattr	= IFLA_CAN_MAX,
373 	.parse_opt	= can_parse_opt,
374 	.print_opt	= can_print_opt,
375 	.print_xstats	= can_print_xstats,
376 	.print_help	= can_print_help,
377 };
378