Hi Wolfgang,

On Tue, Nov 24, 2009 at 03:46:22PM +0100, Wolfgang Grandegger wrote:
> Hi Fu,
> 
> Luotao Fu wrote:
> > Hi folks,
> > 
> > I was trying to integrate socketcan control in an application the last days 
> > and
> > spent quite a lot of time getting clear how to deal with the netlink 
> > interface.
> 
> I can image. Such a library is on my to-do list since a long time, but
> so far I didn't find enough time to implement it. Thanks for the effort.
> 

Ah, luckily we didn't do doubled work. ;-)

> > Besides I just couldn't remember the correct order of parameter when using 
> > the
> > iproute tool for can ;-). So I stripped some code from the iproute package 
> > and
> > put them with some own stuffs together in a library to access can 
> > configuration
> > functions with netlink, which I call libsocketcan-nl. (nl stand for 
> > netlink, not
> > netherland ;-)). The library provides some simple APIs to do common CAN 
> > stuffs,
> > lacking of a proper documentation I'll start here with the content of the 
> > header
> > file:
> > int scan_set_restart(const char *name);
> 
> scan_do_restart() would be more appropriate.
> 

Hm, I actually want to keep the name scheme with _set_ and _get_. I'll
consider a better name for this.

> > int scan_set_bitrate(const char *name, __u32 bitrate);
> > int scan_set_restart_ms(const char *name, __u32 restart_ms);
> > int scan_set_ctrlmode(const char *name, __u32 mode, __u32 flags);
> > 
> > int scan_get_state(const char *name, int *state);
> > int scan_get_restart_ms(const char *name, __u32 *restart_ms);
> > int scan_get_bittiming(const char *name, struct can_bittiming *bt);
> > int scan_get_ctrlmode(const char *name, struct can_ctrlmode *cm);
> > 
> > Usage is quite straight forward. The functions await simply the name of the 
> > can
> > interface and, if any, configuration values and return 0 for success and -1 
> > if
> > something went wrong.
> > 
> > This library provides some benefits, which the hookup code in iproute 
> > doesn't do:
> > * more clear error messages and takes care of special cases, e.g. 
> > restarting a
> > device can only be done while the interface is in BUS_OFF. Neither kernel 
> > nor
> 
> Hm, you cannot restart the device when it's not bus-off. The driver does
> not allow that. See:
> 
> http://lxr.linux.no/#linux+v2.6.31/drivers/net/can/dev.c#L376
> 

Yeah I know. What I mean was that if one calls "ip link can0 type can
set restart" (no guarantee for correctness of the parameters, I'm still
struggling with this ;-)) while the device is not in BUS_OFF, he will
eventually end up with an EINVAL or EBUSY without knowing why. The
iproute tool cannot do any pre-detection while calling can_parse_opt
function since it's hooked deeply in the iproute given infrastructure
and the kernel simply returns a EBUSY or EINVAL without any error
message in such cases. I myself had to look up in the kernelcode to find
what was really going wrong since I didn't know about the BUS_OFF
limitation. So I add some detection in the library to make clear why
restart is not supposed to work. Addtionally I also added if_up if_down
callbacks into several _set_ callbacks, to make the usage as easy as
possible.

> > the iproute tool provides some useful hint if restarting failed.
> > * simple portabilty
> > * API for use in own applications.
> > 
> > I updated the canutils tool to make use of this library. Now we can finally
> > again use the canconfig tool with really simple parameters:
> > 
> > r...@target:~ canconfig can0
> > can0 bitrate: 250000
> > can0 state: ACTIVE
> > can0 restart_ms: 1000
> > can0 mode: loopback[OFF], listen-only[OFF], tripple-sampling[OFF]
> > 
> > Setting works as well, usage is just like in the older versions:
> > 
> > r...@target:~ canconfig can0 bitrate 125000
> > can0 bitrate: 125000
> > r...@target:~ canconfig can0 mode loopback on
> > can0 mode: loopback[ON], listen-only[OFF], tripple-sampling[OFF]
> > 
> > and so on.
> > 
> > The repository of the library can be obtained at
> > git://git.pengutronix.de/git/tools/libsocketcan-nl.git
> > Repo of Canutils can be found at
> > git://git.pengutronix.de/git/tools/canutils.git
> > 
> > Additionally I put the recent version of the library and the tool in our ftp
> > server. They might stay there only as long as as I don't have a proper 
> > webspace
> > for the stuff:
> > ftp://ftp.pengutronix.de/pub/socketcan/
> > 
> > The library is still not complete. For example there's still no 
> > documentation
> > for this, also we are only able to set the bitrate, but not the bittimings. 
> > I
> > will add these stuffs later.
> > 
> > Comments, tests and reviews are highly welcome.
> 
> That's great news for the CAN users, especially if they want to do a
> manual restart of the CAN devices from their application. We should add
> the libsocketcan and canconfig to the SVN trunk a.s.a.p., or some more
> appropriate place, e.g. GIT at BerliOS (or kernel.org).

that'd be nice.
/me advocate GIT ;-)

> I will do some testing when time permits.

Thx.

cheers
Luotao Fu
-- 
Pengutronix e.K.                           | Dipl.-Ing. Luotao Fu        |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

Attachment: signature.asc
Description: Digital signature

_______________________________________________
Socketcan-users mailing list
[email protected]
https://lists.berlios.de/mailman/listinfo/socketcan-users

Reply via email to