CVS commit: [bouyer-socketcan] src/sbin/canconfig

2017-04-21 Thread Manuel Bouyer
Module Name:src
Committed By:   bouyer
Date:   Fri Apr 21 13:05:16 UTC 2017

Modified Files:
src/sbin/canconfig [bouyer-socketcan]: canconfig.c

Log Message:
Fix humanize_number() usage.
Display more user-friendly operational parameters (baud rate, tq value,
number of tq).


To generate a diff of this commit:
cvs rdiff -u -r1.1.2.3 -r1.1.2.4 src/sbin/canconfig/canconfig.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sbin/canconfig/canconfig.c
diff -u src/sbin/canconfig/canconfig.c:1.1.2.3 src/sbin/canconfig/canconfig.c:1.1.2.4
--- src/sbin/canconfig/canconfig.c:1.1.2.3	Thu Apr 20 12:59:54 2017
+++ src/sbin/canconfig/canconfig.c	Fri Apr 21 13:05:15 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: canconfig.c,v 1.1.2.3 2017/04/20 12:59:54 bouyer Exp $	*/
+/*	$NetBSD: canconfig.c,v 1.1.2.4 2017/04/21 13:05:15 bouyer Exp $	*/
 
 /*
  * Copyright 2001 Wasabi Systems, Inc.
@@ -38,7 +38,7 @@
 #include 
 
 #ifndef lint
-__RCSID("$NetBSD: canconfig.c,v 1.1.2.3 2017/04/20 12:59:54 bouyer Exp $");
+__RCSID("$NetBSD: canconfig.c,v 1.1.2.4 2017/04/21 13:05:15 bouyer Exp $");
 #endif
 
 
@@ -327,6 +327,28 @@ status(int sock, const char *canifname)
 
 }
 
+static int
+valid_timings(struct can_link_timecaps *cltc, struct can_link_timings *clt)
+{
+	if (clt->clt_brp < cltc->cltc_brp_min ||
+	clt->clt_brp > cltc->cltc_brp_max)
+		return 0;
+
+	if (clt->clt_prop < cltc->cltc_prop_min ||
+	clt->clt_prop > cltc->cltc_prop_max)
+		return 0;
+
+	if (clt->clt_ps1 < cltc->cltc_ps1_min ||
+	clt->clt_ps1 > cltc->cltc_ps1_max)
+		return 0;
+
+	if (clt->clt_ps2 < cltc->cltc_ps2_min ||
+	clt->clt_ps2 > cltc->cltc_ps2_max)
+		return 0;
+
+	return 1;
+}
+
 static void
 show_timings(int sock, const char *canifname, const char *prefix)
 {
@@ -346,8 +368,8 @@ show_timings(int sock, const char *canif
 	0) < 0)
 		err(1, "unable to get can link mode");
 
-	humanize_number(hbuf, sizeof(hbuf), cltc.cltc_clock_freq, "Hz", 0,
-	HN_AUTOSCALE | HN_NOSPACE | HN_DIVISOR_1000);
+	humanize_number(hbuf, sizeof(hbuf), cltc.cltc_clock_freq, "Hz",
+	HN_AUTOSCALE, HN_NOSPACE | HN_DIVISOR_1000);
 
 	printf("%stiming caps:\n", prefix);
 	printf("%s  clock %s, brp [%d..%d]/%d, prop_seg [%d..%d]\n",
@@ -362,7 +384,21 @@ show_timings(int sock, const char *canif
 	printf("%s  ", prefix);
 	printb("capabilities", cltc.cltc_linkmode_caps, CAN_IFFBITS);
 	printf("\n");
-	printf("%soperational timings:\n", prefix);
+	printf("%soperational timings:", prefix);
+	if (valid_timings(, )) {
+		uint32_t tq, ntq, bps;
+		tq = ((uint64_t)clt.clt_brp * (uint64_t)10) /
+		cltc.cltc_clock_freq;
+		ntq = 1 + clt.clt_prop + clt.clt_ps1 + clt.clt_ps2;
+		printf(" %d time quanta of %dns",
+		1 + clt.clt_prop + clt.clt_ps1 + clt.clt_ps2, tq);
+		bps = 10 / (tq * ntq); 
+		humanize_number(hbuf, sizeof(hbuf), bps, "bps",
+		HN_AUTOSCALE, HN_NOSPACE | HN_DIVISOR_1000);
+		printf(", %s", hbuf);
+	};
+	printf("\n");
+
 	printf("%s  brp %d, prop_seg %d, phase_seg1 %d, phase_seg2 %d, sjw %d\n",
 	prefix,
 	clt.clt_brp, clt.clt_prop, clt.clt_ps1, clt.clt_ps2, clt.clt_sjw);



CVS commit: [bouyer-socketcan] src/sbin/canconfig

2017-04-21 Thread Manuel Bouyer
Module Name:src
Committed By:   bouyer
Date:   Fri Apr 21 13:05:16 UTC 2017

Modified Files:
src/sbin/canconfig [bouyer-socketcan]: canconfig.c

Log Message:
Fix humanize_number() usage.
Display more user-friendly operational parameters (baud rate, tq value,
number of tq).


To generate a diff of this commit:
cvs rdiff -u -r1.1.2.3 -r1.1.2.4 src/sbin/canconfig/canconfig.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: [bouyer-socketcan] src/sbin/canconfig

2017-04-20 Thread Manuel Bouyer
Module Name:src
Committed By:   bouyer
Date:   Thu Apr 20 12:59:54 UTC 2017

Modified Files:
src/sbin/canconfig [bouyer-socketcan]: canconfig.8 canconfig.c

Log Message:
Implement listenonly and loopback flags.
Fix do_canflag()


To generate a diff of this commit:
cvs rdiff -u -r1.1.2.1 -r1.1.2.2 src/sbin/canconfig/canconfig.8
cvs rdiff -u -r1.1.2.2 -r1.1.2.3 src/sbin/canconfig/canconfig.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sbin/canconfig/canconfig.8
diff -u src/sbin/canconfig/canconfig.8:1.1.2.1 src/sbin/canconfig/canconfig.8:1.1.2.2
--- src/sbin/canconfig/canconfig.8:1.1.2.1	Mon Apr 17 20:48:36 2017
+++ src/sbin/canconfig/canconfig.8	Thu Apr 20 12:59:54 2017
@@ -1,4 +1,4 @@
-.\"	$NetBSD: canconfig.8,v 1.1.2.1 2017/04/17 20:48:36 bouyer Exp $
+.\"	$NetBSD: canconfig.8,v 1.1.2.2 2017/04/20 12:59:54 bouyer Exp $
 .\"
 .\" Copyright (c) 2017 Manuel Bouyer.
  *
@@ -100,6 +100,16 @@ set the number of tq for the Synchronisa
 enables triple-sampling.
 .It Cm -3samples
 disables triple-sampling.
+.It Cm listenonly
+enables listenonly mode. In this mode the controller is passive, and
+doesn't send ACKs on the bus.
+.It Cm -listenonly
+disables listenonly mode.
+.It Cm loopback
+enables loopback mode. In this mode, the controller doens't expect ACK from
+the bus.
+.It Cm -loopback
+disables loopback mode.
 .El
 .Sh EXAMPLES
 TODO

Index: src/sbin/canconfig/canconfig.c
diff -u src/sbin/canconfig/canconfig.c:1.1.2.2 src/sbin/canconfig/canconfig.c:1.1.2.3
--- src/sbin/canconfig/canconfig.c:1.1.2.2	Wed Apr 19 17:51:16 2017
+++ src/sbin/canconfig/canconfig.c	Thu Apr 20 12:59:54 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: canconfig.c,v 1.1.2.2 2017/04/19 17:51:16 bouyer Exp $	*/
+/*	$NetBSD: canconfig.c,v 1.1.2.3 2017/04/20 12:59:54 bouyer Exp $	*/
 
 /*
  * Copyright 2001 Wasabi Systems, Inc.
@@ -38,7 +38,7 @@
 #include 
 
 #ifndef lint
-__RCSID("$NetBSD: canconfig.c,v 1.1.2.2 2017/04/19 17:51:16 bouyer Exp $");
+__RCSID("$NetBSD: canconfig.c,v 1.1.2.3 2017/04/20 12:59:54 bouyer Exp $");
 #endif
 
 
@@ -77,6 +77,8 @@ static void	cmd_phase_seg1(const struct 
 static void	cmd_phase_seg2(const struct command *, int, const char *, char **);
 static void	cmd_sjw(const struct command *, int, const char *, char **);
 static void	cmd_3samples(const struct command *, int, const char *, char **);
+static void	cmd_listenonly(const struct command *, int, const char *, char **);
+static void	cmd_loopback(const struct command *, int, const char *, char **);
 
 static const struct command command_table[] = {
 	{ "up",			0,	0,		cmd_up },
@@ -91,6 +93,12 @@ static const struct command command_tabl
 	{ "3samples",		0,	0,		cmd_3samples },
 	{ "-3samples",		0,	CMD_INVERT,	cmd_3samples },
 
+	{ "listenonly",		0,	0,		cmd_listenonly },
+	{ "-listenonly",	0,	CMD_INVERT,	cmd_listenonly },
+
+	{ "loopback",		0,	0,		cmd_loopback },
+	{ "-loopback",		0,	CMD_INVERT,	cmd_loopback },
+
 	{ NULL,			0,	0,		NULL },
 };
 
@@ -225,6 +233,8 @@ usage(void)
 		" phase_seg2 ",
 		" sjw ",
 		" 3samples | -3samples",
+		" listenonly | -listenonly",
+		" loopback | -loopback",
 		NULL,
 	};
 	extern const char *__progname;
@@ -353,7 +363,7 @@ show_timings(int sock, const char *canif
 	printb("capabilities", cltc.cltc_linkmode_caps, CAN_IFFBITS);
 	printf("\n");
 	printf("%soperational timings:\n", prefix);
-	printf("%s  brp %d prop_seg %d, phase_seg1 %d, phase_seg2 %d, sjw %d\n",
+	printf("%s  brp %d, prop_seg %d, phase_seg1 %d, phase_seg2 %d, sjw %d\n",
 	prefix,
 	clt.clt_brp, clt.clt_prop, clt.clt_ps1, clt.clt_ps2, clt.clt_sjw);
 	printf("%s  ", prefix);
@@ -418,7 +428,7 @@ do_canflag(int sock, const char *canifna
 		cmd = CANSLINKMODE;
 	else
 		cmd = CANCLINKMODE;
-	return do_cmd(sock, canifname, cmd, , sizeof(flag), set);
+	return do_cmd(sock, canifname, cmd, , sizeof(flag), 1);
 }
 
 static void
@@ -506,7 +516,6 @@ cmd_sjw(const struct command *cmd, int s
 	g_clt.clt_sjw = val;
 	g_clt_updated=1;
 }
-
 static void
 cmd_3samples(const struct command *cmd, int sock, const char *canifname,
 char **argv)
@@ -516,3 +525,23 @@ cmd_3samples(const struct command *cmd, 
 		err(1, "%s", cmd->cmd_keyword);
 
 }
+
+static void
+cmd_listenonly(const struct command *cmd, int sock, const char *canifname,
+char **argv)
+{
+if (do_canflag(sock, canifname, CAN_LINKMODE_LISTENONLY,
+	(cmd->cmd_flags & CMD_INVERT) ? 0 : 1) < 0)
+		err(1, "%s", cmd->cmd_keyword);
+
+}
+
+static void
+cmd_loopback(const struct command *cmd, int sock, const char *canifname,
+char **argv)
+{
+if (do_canflag(sock, canifname, CAN_LINKMODE_LOOPBACK,
+	(cmd->cmd_flags & CMD_INVERT) ? 0 : 1) < 0)
+		err(1, "%s", cmd->cmd_keyword);
+
+}



CVS commit: [bouyer-socketcan] src/sbin/canconfig

2017-04-20 Thread Manuel Bouyer
Module Name:src
Committed By:   bouyer
Date:   Thu Apr 20 12:59:54 UTC 2017

Modified Files:
src/sbin/canconfig [bouyer-socketcan]: canconfig.8 canconfig.c

Log Message:
Implement listenonly and loopback flags.
Fix do_canflag()


To generate a diff of this commit:
cvs rdiff -u -r1.1.2.1 -r1.1.2.2 src/sbin/canconfig/canconfig.8
cvs rdiff -u -r1.1.2.2 -r1.1.2.3 src/sbin/canconfig/canconfig.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: [bouyer-socketcan] src/sbin/canconfig

2017-04-19 Thread Manuel Bouyer
Module Name:src
Committed By:   bouyer
Date:   Wed Apr 19 17:51:16 UTC 2017

Modified Files:
src/sbin/canconfig [bouyer-socketcan]: canconfig.c

Log Message:
Make output better
Fix setting timings parameters


To generate a diff of this commit:
cvs rdiff -u -r1.1.2.1 -r1.1.2.2 src/sbin/canconfig/canconfig.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: [bouyer-socketcan] src/sbin/canconfig

2017-04-19 Thread Manuel Bouyer
Module Name:src
Committed By:   bouyer
Date:   Wed Apr 19 17:51:16 UTC 2017

Modified Files:
src/sbin/canconfig [bouyer-socketcan]: canconfig.c

Log Message:
Make output better
Fix setting timings parameters


To generate a diff of this commit:
cvs rdiff -u -r1.1.2.1 -r1.1.2.2 src/sbin/canconfig/canconfig.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sbin/canconfig/canconfig.c
diff -u src/sbin/canconfig/canconfig.c:1.1.2.1 src/sbin/canconfig/canconfig.c:1.1.2.2
--- src/sbin/canconfig/canconfig.c:1.1.2.1	Mon Apr 17 20:48:36 2017
+++ src/sbin/canconfig/canconfig.c	Wed Apr 19 17:51:16 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: canconfig.c,v 1.1.2.1 2017/04/17 20:48:36 bouyer Exp $	*/
+/*	$NetBSD: canconfig.c,v 1.1.2.2 2017/04/19 17:51:16 bouyer Exp $	*/
 
 /*
  * Copyright 2001 Wasabi Systems, Inc.
@@ -38,7 +38,7 @@
 #include 
 
 #ifndef lint
-__RCSID("$NetBSD: canconfig.c,v 1.1.2.1 2017/04/17 20:48:36 bouyer Exp $");
+__RCSID("$NetBSD: canconfig.c,v 1.1.2.2 2017/04/19 17:51:16 bouyer Exp $");
 #endif
 
 
@@ -202,7 +202,7 @@ main(int argc, char *argv[])
 
 	/* If the timings changed, update them. */
 	if (g_clt_updated &&
-	do_cmd(sock, canifname, CANGLINKTIMINGS, _clt, sizeof(g_clt), 1) < 0) 
+	do_cmd(sock, canifname, CANSLINKTIMINGS, _clt, sizeof(g_clt), 1) < 0) 
 		err(1, "unable to set can link timings");
 
 	/* If the flags changed, update them. */
@@ -313,8 +313,7 @@ status(int sock, const char *canifname)
 	printb("flags", ifr.ifr_flags, IFFBITS);
 	printf("\n");
 
-	printf("\tTimings:\n");
-	show_timings(sock, canifname, "\t\t");
+	show_timings(sock, canifname, "\t");
 
 }
 
@@ -340,7 +339,7 @@ show_timings(int sock, const char *canif
 	humanize_number(hbuf, sizeof(hbuf), cltc.cltc_clock_freq, "Hz", 0,
 	HN_AUTOSCALE | HN_NOSPACE | HN_DIVISOR_1000);
 
-	printf("%scaps:\n", prefix);
+	printf("%stiming caps:\n", prefix);
 	printf("%s  clock %s, brp [%d..%d]/%d, prop_seg [%d..%d]\n",
 	prefix, hbuf,
 	cltc.cltc_brp_min, cltc.cltc_brp_max, cltc.cltc_brp_inc,
@@ -352,12 +351,14 @@ show_timings(int sock, const char *canif
 	cltc.cltc_sjw_max);
 	printf("%s  ", prefix);
 	printb("capabilities", cltc.cltc_linkmode_caps, CAN_IFFBITS);
-	printf("%soperational:\n", prefix);
+	printf("\n");
+	printf("%soperational timings:\n", prefix);
 	printf("%s  brp %d prop_seg %d, phase_seg1 %d, phase_seg2 %d, sjw %d\n",
 	prefix,
 	clt.clt_brp, clt.clt_prop, clt.clt_ps1, clt.clt_ps2, clt.clt_sjw);
 	printf("%s  ", prefix);
 	printb("mode", linkmode, CAN_IFFBITS);
+	printf("\n");
 }
 
 static int
@@ -417,7 +418,7 @@ do_canflag(int sock, const char *canifna
 		cmd = CANSLINKMODE;
 	else
 		cmd = CANCLINKMODE;
-	return do_cmd(sock, canifname, cmd, , sizeof(flag), 1);
+	return do_cmd(sock, canifname, cmd, , sizeof(flag), set);
 }
 
 static void