1 # Configure the AGL system
5 Connected to the target, here is how to load the virtual CAN device driver and set up a new vcan device :
9 ip link add vcan0 type vcan
13 You also can named your linux CAN device like you want and if you need name it `can0` :
18 ip link add can0 type vcan
22 ## CAN device using the USB CAN adapter
24 Using real connection to CAN bus of your car using the USB CAN adapter connected to the OBD2 connector.
26 Once connected, launch `dmesg` command and search which device to use :
31 [ 131.871441] usb 1-3: new full-speed USB device number 4 using ohci-pci
32 [ 161.860504] can: controller area network core (rev 20120528 abi 9)
33 [ 161.860522] NET: Registered protocol family 29
34 [ 177.561620] usb 1-3: USB disconnect, device number 4
35 [ 191.061423] usb 1-2: USB disconnect, device number 3
36 [ 196.095325] usb 1-2: new full-speed USB device number 5 using ohci-pci
37 [ 327.568882] usb 1-2: USB disconnect, device number 5
38 [ 428.594177] CAN device driver interface
39 [ 1872.551543] usb 1-2: new full-speed USB device number 6 using ohci-pci
40 [ 1872.809302] usb_8dev 1-2:1.0 can0: firmware: 1.7, hardware: 1.0
41 [ 1872.809356] usbcore: registered new interface driver usb_8dev
44 Here device is named `can0`.
46 This instruction assuming a speed of 500000kbps for your CAN bus, you can try others supported bitrate like 125000, 250000 if 500000 doesn't work:
49 ip link set can0 type can bitrate 500000
52 can0: <NOARP,UP,LOWER_UP,ECHO> mtu 16 qdisc pfifo_fast state UNKNOWN qlen 10
54 can state ERROR-ACTIVE (berr-counter tx 0 rx 0) restart-ms 0
55 bitrate 500000 sample-point 0.875
56 tq 125 prop-seg 6 phase-seg1 7 phase-seg2 2 sjw 1
57 sja1000: tseg1 1..16 tseg2 1..8 sjw 1..4 brp 1..64 brp-inc 1
61 On a Rcar Gen3 board, you'll have your CAN device as `can1` because `can0` already exists as an embedded device.
63 The instructions will be the same:
66 ip link set can1 type can bitrate 500000
69 can0: <NOARP,UP,LOWER_UP,ECHO> mtu 16 qdisc pfifo_fast state UNKNOWN qlen 10
71 can state ERROR-ACTIVE (berr-counter tx 0 rx 0) restart-ms 0
72 bitrate 500000 sample-point 0.875
73 tq 125 prop-seg 6 phase-seg1 7 phase-seg2 2 sjw 1
74 sja1000: tseg1 1..16 tseg2 1..8 sjw 1..4 brp 1..64 brp-inc 1
78 ## Rename an existing CAN device
80 You can rename an existing CAN device using following command and doing so move an existing `can0` device to anything else and then use another device as `can0`. For a Rcar Gen3 board do the following by example:
83 sudo ip link set can0 down
84 sudo ip link set can0 name bsp-can0
85 sudo ip link set bsp-can0 up
88 Then connect your USB CAN device that will be named `can0` by default.
90 # Configure the binding
92 The binding reads system configuration file _/etc/dev-mapping.conf_ at start to map logical name from signals described in JSON file to linux devices name initialized by the system.
93 Edit file _/etc/dev-mappping.conf_ and add mapping in section `CANbus-mapping`.
95 Default binding configuration use a CAN bus named `hs` so you need to map it to the real one, here are some examples:
97 * Using virtual CAN device as described in the previous chapter:
104 * Using real CAN device, this example assume CAN bus traffic will be on can0.
111 * On a Rcar Gen3 board there is an embedded CAN device so `can0` already exists. So you might want to use your USB CAN adapter plugged to the OBD2 connector, in this case use `can1`:
117 > **CAUTION VERY IMPORTANT:** Make sure the CAN bus\(es\) you specify in your configuration file match those specified in your generated source file with the `CAN-config-generator`.
119 # Run it, test it, use it !
121 You can run the binding using **afm-util** tool, here is the classic way to go :
124 afm-util run low-can-service@4.0
128 You can find instructions to use afm-util tool [here](http://docs.iot.bzh/docs/apis_services/en/dev/reference/af-main/afm-daemons.html#using-afm-util), as well as documentation about Application Framework.
130 But you can't control nor interact with it because you don't know security token that **Application Framework** gaves it at launch.
132 So, to test it, it is better to launch the binding manually. In the following example, it will use port **1234** and left empty security token for testing purpose:
135 afb-daemon --binding=/var/lib/afm/applications/low-can-service/4.0/lib/afb-low-can.so --rootdir=/var/lib/afm/applications/low-can-service/4.0/ --port=1234 --token=1
136 NOTICE: binding [/usr/lib/afb/afb-dbus-binding.so] calling registering function afbBindingV1Register
137 NOTICE: binding /usr/lib/afb/afb-dbus-binding.so loaded with API prefix dbus
138 NOTICE: binding [/usr/lib/afb/authLogin.so] calling registering function afbBindingV1Register
139 NOTICE: binding /usr/lib/afb/authLogin.so loaded with API prefix auth
140 NOTICE: binding [/var/lib/afm/applications/low-can-service/4.0/libs//low-can-binding.so] calling registering function afbBindingV1Register
141 NOTICE: binding /var/lib/afm/applications/low-can-service/4.0/libs//low-can-binding.so loaded with API prefix low-can
142 NOTICE: Waiting port=1234 rootdir=/var/lib/afm/applications/low-can-service/4.0/
143 NOTICE: Browser URL= http:/*localhost:1234
146 On another terminal, connect to the binding using previously installed _**AFB Websocket CLI**_ tool:
149 afb-client-demo ws://localhost:1234/api?token=1
152 You will be on an interactive session where you can communicate directly with the binding API.
154 The binding provides at this moment 2 verbs, _subscribe_ and _unsubscribe_, which can take argument by a JSON **event** object.
156 The argument value is the CAN message **generic\_name** as described in the JSON file used to generate cpp file for the binding.
158 To use the _**AFB Websocket CLI**_ tool, a command line will be like the following :
161 <api> <verb> <arguments>
166 * API : _**low-can**_.
167 * Verb : _**subscribe**_ or _**unsubscribe**_
168 * Arguments : _**{ "event": "driver.doors.open" }**_
170 ## Subscription and unsubscription
172 You can ask to subscribe to chosen CAN event with a call to _subscribe_ API verb with the CAN messages name as JSON argument.
174 > **NOTE:** If no argument is provided, then you'll subscribe to all signals at once.
176 For example from a websocket session:
179 low-can subscribe { "event": "doors.driver.open" }
180 ON-REPLY 1:low-can/subscribe: {"jtype":"afb-reply","request":{"status":"success","uuid":"a18fd375-b6fa-4c0e-a1d4-9d3955975ae8"}}
183 Subscription and unsubscription can take wildcard in their _event_ value.
185 To receive all doors events :
188 low-can subscribe { "event" : "doors*" }
189 ON-REPLY 1:low-can/subscribe: {"jtype":"afb-reply","request":{"status":"success","uuid":"511c872e-d7f3-4f3b-89c2-aa9a3e9fbbdb"}}
192 Then you will receive an event each time a CAN message is decoded for the event named _doors.driver.open_
195 ON-EVENT low-can/messages.doors.driver.open({"event":"low-can\/messages.doors.driver.open","data":{"name":"messages.doors.driver.open","value":true},"jtype":"afb-event"})
198 Notice that event shows you that the CAN event is named _messages.doors.driver.open_ but you ask for event about _doors.driver.open_.
200 This is because all CAN messages or diagnostic messages are prefixed by the JSON parent node name, **messages** for CAN messages and **diagnostic\_messages** for diagnostic messages like OBD2.
202 This will let you subscribe or unsubcribe to all signals at once, not recommended, and better make filter on subscribe operation based upon their type. Examples:
205 low-can subscribe { "event" : "*speed*" } --> will subscribe to all messages with speed in their name. Search will be make without prefix for it.
206 low-can subscribe { "event" : "speed*" } --> will subscribe to all messages begin by speed in their name. Search will be make without prefix for it.
207 low-can subscribe { "event" : "messages*speed*" } --> will subscribe to all CAN messages with speed in their name. Search will be on prefixed messages here.
208 low-can subscribe { "event" : "messages*speed" } --> will subscribe to all CAN messages ending with speed in their name. Search will be on prefixed messages here.
209 low-can subscribe { "event" : "diagnostic*speed*" } --> will subscribe to all diagnostic messages with speed in their name. Search will be on prefixed messages here.
210 low-can subscribe { "event" : "diagnostic*speed" } --> will subscribe to all diagnostic messages ending with speed in their name. Search will be on prefixed messages here.
213 You can stop receiving event from it by unsubscribe the signal the same way you did for subscribe
216 low-can unsubscribe { "event": "doors.driver.open" }
217 ON-REPLY 2:low-can/unsubscribe: {"jtype":"afb-reply","request":{"status":"success"}}
218 low-can unsubscribe { "event" : "doors*" }
219 ON-REPLY 3:low-can/unsubscribe: {"jtype":"afb-reply","request":{"status":"success"}}
222 ### Filtering capabilities
224 It is possible to limits received event notifications into minimum and maximum boundaries as well as doing frequency thinning. This is possible using the argument filter with one or more of the filters available :
226 * frequency: specify in Hertz the frequency which will be used to getting notified of new CAN events for the designated signal. If, during the blocked time, further changed CAN messages are received, the last valid one will be transferred after the lockout with a RX_CHANGED.
227 * min: Minimum value that the decoded value needs to be above to get pushed to the subscribed client(s).
228 * max: Maximum value that the decoded value needs to be below to get pushed to the subscribed client(s)
230 Order doesn't matter neither the number of filters chosen, you can use one, two or all of them at once.
235 low-can subscribe {"event": "messages.engine.speed", "filter": { "frequency": 3, "min": 1250, "max": 3500}}
236 low-can subscribe {"event": "messages.engine.load", "filter": { "min": 30, "max": 100}}
237 low-can subscribe {"event": "messages.vehicle.speed", "filter": { "frequency": 2}}
240 ## Using CAN utils to monitor CAN activity
242 You can watch CAN traffic and send custom CAN messages using can-utils preinstalled on AGL target.
244 To watch watch going on a CAN bus use:
250 Or for an USB CAN adapter connected to porter board:
256 Send a custom message:
259 cansend can0 ID#DDDDAAAATTTTAAAA
262 You can also replay a previously dumped CAN logfiles. These logfiles can be found in _can_samples_ directory under Git repository. Following examples use a real trip from an Auris Toyota car.
264 Trace has been recorded from a CAN device `can0` so you have to map it to the correct one you use for your tests.
266 Replay on a virtual CAN device `vcan0`:
268 canplayer -I trip_test_with_obd2_vehicle_speed_requests vcan0=can0
271 Replay on a CAN device `can0`:
273 canplayer -I trip_test_with_obd2_vehicle_speed_requests can0
276 Replay on a CAN device `can1` (porter by example):
278 canplayer -I trip_test_with_obd2_vehicle_speed_requests can1=can0