Update the documentation for J1939.
[apps/agl-service-can-low-level.git] / docs / 4-Usage.md
1 # Configure the AGL system
2
3 ## Virtual CAN device
4
5 Connected to the target, here is how to load the virtual CAN device driver and
6 set up a new vcan device :
7
8 ```bash
9 modprobe vcan
10 ip link add vcan0 type vcan
11 ip link set vcan0 up
12 ```
13
14 You also can named your linux CAN device like you want and if you need name it
15 `can0` :
16
17 ```bash
18 modprobe vcan
19 ip link add can0 type vcan
20 ip link set can0 up
21 ```
22
23 ## CAN device using the USB CAN adapter
24
25 Using real connection to CAN bus of your car using the USB CAN adapter
26 connected to the OBD2 connector.
27
28 Once connected, launch `dmesg` command and search which device to use:
29
30 ```bash
31 dmesg
32 [...]
33 [  131.871441] usb 1-3: new full-speed USB device number 4 using ohci-pci
34 [  161.860504] can: controller area network core (rev 20120528 abi 9)
35 [  161.860522] NET: Registered protocol family 29
36 [  177.561620] usb 1-3: USB disconnect, device number 4
37 [  191.061423] usb 1-2: USB disconnect, device number 3
38 [  196.095325] usb 1-2: new full-speed USB device number 5 using ohci-pci
39 [  327.568882] usb 1-2: USB disconnect, device number 5
40 [  428.594177] CAN device driver interface
41 [ 1872.551543] usb 1-2: new full-speed USB device number 6 using ohci-pci
42 [ 1872.809302] usb_8dev 1-2:1.0 can0: firmware: 1.7, hardware: 1.0
43 [ 1872.809356] usbcore: registered new interface driver usb_8dev
44 ```
45
46 Here device is named `can0`.
47
48 This instruction assuming a speed of 500000kbps for your CAN bus, you can try
49 others supported bitrate like 125000, 250000 if 500000 doesn't work:
50
51 ```bash
52 ip link set can0 type can bitrate 500000
53 ip link set can0 up
54 ip link show can0
55   can0: <NOARP,UP,LOWER_UP,ECHO> mtu 16 qdisc pfifo_fast state UNKNOWN qlen 10
56     link/can
57     can state ERROR-ACTIVE (berr-counter tx 0 rx 0) restart-ms 0
58     bitrate 500000 sample-point 0.875
59     tq 125 prop-seg 6 phase-seg1 7 phase-seg2 2 sjw 1
60     sja1000: tseg1 1..16 tseg2 1..8 sjw 1..4 brp 1..64 brp-inc 1
61     clock 16000000
62 ```
63
64 On a Rcar Gen3 board, you'll have your CAN device as `can1` because `can0`
65 already exists as an embedded device.
66
67 The instructions will be the same:
68
69 ```bash
70 ip link set can1 type can bitrate 500000
71 ip link set can1 up
72 ip link show can1
73   can0: <NOARP,UP,LOWER_UP,ECHO> mtu 16 qdisc pfifo_fast state UNKNOWN qlen 10
74     link/can
75     can state ERROR-ACTIVE (berr-counter tx 0 rx 0) restart-ms 0
76     bitrate 500000 sample-point 0.875
77     tq 125 prop-seg 6 phase-seg1 7 phase-seg2 2 sjw 1
78     sja1000: tseg1 1..16 tseg2 1..8 sjw 1..4 brp 1..64 brp-inc 1
79     clock 16000000
80 ```
81
82 ## Rename an existing CAN device
83
84 You can rename an existing CAN device using following command and doing so move
85 an existing `can0` device to anything else and then use another device as `can0`
86 . For a Rcar Gen3 board do the following by example:
87
88 ```bash
89 sudo ip link set can0 down
90 sudo ip link set can0 name bsp-can0
91 sudo ip link set bsp-can0 up
92 ```
93
94 Then connect your USB CAN device that will be named `can0` by default.
95
96 # Configure the binding
97
98 The binding reads system configuration file _/etc/dev-mapping.conf_ at start to
99 map logical name from signals described in JSON file to linux devices name
100 initialized by the system.
101
102 Edit file _/etc/dev-mapping.conf_ and add mapping in section `CANbus-mapping`.
103
104 Default binding configuration use a CAN bus named `hs` so you need to map it to
105 the real one, here are some examples:
106
107 * Using virtual CAN device as described in the previous chapter:
108
109 ```ini
110 [CANbus-mapping]
111 hs="vcan0"
112 ls="vcan1"
113 ```
114
115 * Using real CAN device, this example assume CAN bus traffic will be on can0.
116
117 ```ini
118 [CANbus-mapping]
119 hs="can0"
120 ls="can1"
121 ```
122
123 * 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`:
124
125 ```ini
126 [CANbus-mapping]
127 hs="can1"
128 ```
129
130 * You can use this configuration for j1939:
131
132 ```ini
133 [CANbus-mapping]
134 hs="can0"
135 ls="can1"
136 j1939="can2"
137 ```
138
139 > **CAUTION VERY IMPORTANT:** Make sure the CAN bus\(es\) you specify in your
140 > configuration file match those specified in your generated source file with
141 > the `CAN-config-generator`.
142
143 # Run it, test it, use it.
144
145 You can run the binding using **afm-util** tool, here is the classic way to go :
146
147 ```bash
148 afm-util run low-can-service@4.0
149 1
150 ```
151
152 You can find instructions to use afm-util tool
153 [here](../../reference/af-main/1-afm-daemons.html#using-afm-util),
154  as well as documentation about Application Framework.
155
156 But you can't control nor interact with it because you don't know security
157 token that **Application Framework** gaves it at launch.
158
159 So, to test it, it is better to launch the binding manually. In the following
160 example, it will use port **1234** and left empty security token for testing
161 purpose:
162
163 ```bash
164 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
165 NOTICE: binding [/usr/lib/afb/afb-dbus-binding.so] calling registering function afbBindingV1Register
166 NOTICE: binding /usr/lib/afb/afb-dbus-binding.so loaded with API prefix dbus
167 NOTICE: binding [/usr/lib/afb/authLogin.so] calling registering function afbBindingV1Register
168 NOTICE: binding /usr/lib/afb/authLogin.so loaded with API prefix auth
169 NOTICE: binding [/var/lib/afm/applications/low-can-service/4.0/libs//low-can-binding.so] calling registering function afbBindingV1Register
170 NOTICE: binding /var/lib/afm/applications/low-can-service/4.0/libs//low-can-binding.so loaded with API prefix low-can
171 NOTICE: Waiting port=1234 rootdir=/var/lib/afm/applications/low-can-service/4.0/
172 NOTICE: Browser URL= http:/*localhost:1234
173 ```
174
175 On another terminal, connect to the binding using previously installed
176 **AFB Websocket CLI** tool:
177
178 ```bash
179 afb-client-demo ws://localhost:1234/api?token=1
180 ```
181
182 You will be on an interactive session where you can communicate directly with
183 the binding API.
184
185 The binding provides at this moment 2 verbs, _subscribe_ and _unsubscribe_,
186 which can take argument by a JSON **event** object.
187
188 The argument value is the CAN message **generic\_name** as described in the
189 JSON file used to generate cpp file for the binding.
190
191 To use the _**AFB Websocket CLI**_ tool, a command line will be like the
192 following:
193
194 ```
195 <api> <verb> <arguments>
196 ```
197
198 Where:
199
200 * API : _**low-can**_.
201 * Verb : _**subscribe**_ or _**unsubscribe**_
202 * Arguments : _**{ "event": "driver.doors.open" }**_
203
204 ## Subscription and unsubscription
205
206 You can ask to subscribe to chosen CAN event with a call to _subscribe_ API
207 verb with the CAN messages name as JSON argument.
208
209 > **NOTE:** If no argument is provided, then you'll subscribe to all signals
210 > at once.
211
212 For example from a websocket session:
213
214 ```json
215 low-can subscribe { "event": "doors.driver.open" }
216 ON-REPLY 1:low-can/subscribe: {"jtype":"afb-reply","request":{"status":"success","uuid":"a18fd375-b6fa-4c0e-a1d4-9d3955975ae8"}}
217 ```
218
219 Subscription and unsubscription can take wildcard in their _event_ value.
220
221 To receive all doors events :
222
223 ```json
224 low-can subscribe { "event" : "doors*" }
225 ON-REPLY 1:low-can/subscribe: {"jtype":"afb-reply","request":{"status":"success","uuid":"511c872e-d7f3-4f3b-89c2-aa9a3e9fbbdb"}}
226 ```
227
228 Then you will receive an event each time a CAN message is decoded for the event
229 named _doors.driver.open_ with its received timestamp if available:
230
231 ```json
232 ON-EVENT low-can/messages.doors.driver.open({"event":"low-can\/messages.doors.driver.open","data":{"name":"messages.doors.driver.open","value":true, "timestamp": 1505812906020023},"jtype":"afb-event"})
233 ```
234
235 Notice that event shows you that the CAN event is named
236 _messages.doors.driver.open_ but you ask for event about
237 _doors.driver.open_.
238
239 This is because all CAN messages or diagnostic messages are prefixed by the
240 JSON parent node name, **messages** for CAN messages and
241 **diagnostic\_messages** for diagnostic messages like OBD2.
242
243 This will let you subscribe or unsubcribe to all signals at once, not
244 recommended, and better make filter on subscribe operation based upon their type. Examples:
245
246 ```json
247 low-can subscribe { "event" : "*speed*" } --> will subscribe to all messages with speed in their name. Search will be make without prefix for it.
248 low-can subscribe { "event" : "speed*" } --> will subscribe to all messages begin by speed in their name. Search will be make without prefix for it.
249 low-can subscribe { "event" : "messages*speed*" } --> will subscribe to all CAN messages with speed in their name. Search will be on prefixed messages here.
250 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.
251 low-can subscribe { "event" : "diagnostic*speed*" } --> will subscribe to all diagnostic messages with speed in their name. Search will be on prefixed messages here.
252 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.
253 ```
254
255 You can also subscribe to an event with the ID or the PGN of the message definition :
256
257
258 ```json
259 low-can subscribe { "id" : 1568}
260 low-can subscribe { "pgn" : 61442}
261 ```
262
263 You can stop receiving event from it by unsubscribe the signal the same way you did for subscribe
264
265 ```json
266 low-can unsubscribe { "event": "doors.driver.open" }
267 ON-REPLY 2:low-can/unsubscribe: {"jtype":"afb-reply","request":{"status":"success"}}
268 low-can unsubscribe { "event" : "doors*" }
269 ON-REPLY 3:low-can/unsubscribe: {"jtype":"afb-reply","request":{"status":"success"}}
270 ```
271
272 ### Filtering capabilities
273
274 It is possible to limits received event notifications into minimum and maximum
275 boundaries as well as doing frequency thinning. This is possible using the
276 argument filter with one or more of the filters available :
277
278 * frequency: specify in Hertz the frequency which will be used to getting
279  notified of new CAN events for the designated signal. If, during the blocked
280  time, further changed CAN messages are received, the last valid one will be
281  transferred after the lockout with a RX_CHANGED.
282 * min: Minimum value that the decoded value needs to be above to get pushed to
283  the subscribed client(s).
284 * max: Maximum value that the decoded value needs to be below to get pushed to
285  the subscribed client(s)
286
287 Order doesn't matter neither the number of filters chosen, you can use one, two
288 or all of them at once.
289
290 Usage examples :
291
292 ```json
293 low-can subscribe {"event": "messages.engine.speed", "filter": { "frequency": 3, "min": 1250, "max": 3500}}
294 low-can subscribe {"event": "messages.engine.load", "filter": { "min": 30, "max": 100}}
295 low-can subscribe {"event": "messages.vehicle.speed", "filter": { "frequency": 2}}
296 ```
297
298 ## Get last signal value and list of configured signals
299
300 You can also ask for a particular signal value on one shot using **get** verb, like
301 this:
302
303 ```json
304 low-can get {"event": "messages.engine.speed"}
305 ON-REPLY 1:low-can/get: {"response":[{"event":"messages.engine.speed","value":0}],"jtype":"afb-reply","request":{"status":"success"}}
306 ```
307
308 > **CAUTION** Only one event could be requested.
309
310 Also, if you want to know the supported CAN signals loaded by **low-can**, use
311 verb **list**
312
313 ```json
314 low-can list
315 ON-REPLY 2:low-can/list: {"response":["messages.hvac.fan.speed","messages.hvac.temperature.left","messages.hvac.temperature.right","messages.hvac.temperature.average","messages.engine.speed","messages.fuel.level.low","messages.fuel.level","messages.vehicle.average.speed","messages.engine.oil.temp","messages.engine.oil.temp.high","messages.doors.boot.open","messages.doors.front_left.open","messages.doors.front_right.open","messages.doors.rear_left.open","messages.doors.rear_right.open","messages.windows.front_left.open","messages.windows.front_right.open","messages.windows.rear_left.open","messages.windows.rear_right.open","diagnostic_messages.engine.load","diagnostic_messages.engine.coolant.temperature","diagnostic_messages.fuel.pressure","diagnostic_messages.intake.manifold.pressure","diagnostic_messages.engine.speed","diagnostic_messages.vehicle.speed","diagnostic_messages.intake.air.temperature","diagnostic_messages.mass.airflow","diagnostic_messages.throttle.position","diagnostic_messages.running.time","diagnostic_messages.EGR.error","diagnostic_messages.fuel.level","diagnostic_messages.barometric.pressure","diagnostic_messages.ambient.air.temperature","diagnostic_messages.commanded.throttle.position","diagnostic_messages.ethanol.fuel.percentage","diagnostic_messages.accelerator.pedal.position","diagnostic_messages.hybrid.battery-pack.remaining.life","diagnostic_messages.engine.oil.temperature","diagnostic_messages.engine.fuel.rate","diagnostic_messages.engine.torque"],"jtype":"afb-reply","request":{"status":"success","uuid":"32df712a-c7fa-4d58-b70b-06a87f03566b"}}
316 ```
317
318 ## Write on CAN buses
319
320 A new capability as been introcuded to be able to write on handled CAN buses.
321 Two modes could be used for that which is either specifying the CAN bus and a
322 *RAW* CAN message either by specifying a defined signal and its value.
323
324 Examples:
325
326 ```json
327 # Write a raw can frame to the CAN id 0x620
328 low-can write { "bus_name": "hs", "frame": { "can_id": 1568, "can_dlc":
329 8, "can_data": [ 255,255,255,255,255,255,255,255]} }
330 # Write a signal's value.
331 low-can write { "signal_name": "engine.speed", "signal_value": 1256}
332 ```
333
334 To be able to use write capability, you need to add the permission
335  ```urn:AGL:permission::platform:can:write``` to your package configuration
336  file that need to write on CAN bus through **low-can** api.
337
338 Then in order to write on bus, your app needs to call verb **auth**
339 before calling **write**, to raise its **LOA**, Level Of Assurance,
340 which controls usage of verb **write**.
341
342 ## Using CAN utils to monitor CAN activity
343
344 You can watch CAN traffic and send custom CAN messages using can-utils
345 preinstalled on AGL target.
346
347 To watch watch going on a CAN bus use:
348
349 ```bash
350 candump can0
351 ```
352
353 Or for an USB CAN adapter connected to porter board:
354
355 ```bash
356 candump can1
357 ```
358
359 Send a custom message:
360
361 ```bash
362 cansend can0 ID#DDDDAAAATTTTAAAA
363 ```
364
365 You can also replay a previously dumped CAN logfiles. These logfiles can be
366 found in _can_samples_ directory under Git repository. Following examples use
367 a real trip from an Auris Toyota car.
368
369 Trace has been recorded from a CAN device `can0` so you have to map it to the
370 correct one you use for your tests.
371
372 Replay on a virtual CAN device `vcan0`:
373
374 ```bash
375 canplayer -I trip_test_with_obd2_vehicle_speed_requests vcan0=can0
376 ```
377
378 Replay on a CAN device `can0`:
379
380 ```bash
381 canplayer -I trip_test_with_obd2_vehicle_speed_requests can0
382 ```
383
384 Replay on a CAN device `can1` (porter by example):
385
386 ```bash
387 canplayer -I trip_test_with_obd2_vehicle_speed_requests can1=can0
388 ```