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