eecf5a8e7a89248bbf4689a54bdcc1f7de29bbaf
[apps/agl-service-can-low-level.git] / docs / 3-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 set up a new vcan device :
6
7 ```bash
8 modprobe vcan
9 ip link add vcan0 type vcan
10 ip link set vcan0 up
11 ```
12
13 You also can named your linux CAN device like you want and if you need name it `can0` :
14
15
16 ```bash
17 modprobe vcan
18 ip link add can0 type vcan
19 ip link set can0 up
20 ```
21
22 ## CAN device using the USB CAN adapter
23
24 Using real connection to CAN bus of your car using the USB CAN adapter connected to the OBD2 connector.
25
26 Once connected, launch `dmesg` command and search which device to use :
27
28 ```bash
29 dmesg
30 [...]
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
42 ```
43
44 Here device is named `can0`.
45
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:
47
48 ```bash
49 ip link set can0 type can bitrate 500000
50 ip link set can0 up
51 ip link show can0
52   can0: <NOARP,UP,LOWER_UP,ECHO> mtu 16 qdisc pfifo_fast state UNKNOWN qlen 10
53     link/can
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
58     clock 16000000
59 ```
60
61 On a Rcar Gen3 board, you'll have your CAN device as `can1` because `can0` already exists as an embedded device.
62
63 The instructions will be the same:
64
65 ```bash
66 ip link set can1 type can bitrate 500000
67 ip link set can1 up
68 ip link show can1
69   can0: <NOARP,UP,LOWER_UP,ECHO> mtu 16 qdisc pfifo_fast state UNKNOWN qlen 10
70     link/can
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
75     clock 16000000
76 ```
77
78 ## Rename an existing CAN device
79
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:
81
82 ```bash
83 sudo ip link set can0 down
84 sudo ip link set can0 name bsp-can0
85 sudo ip link set bsp-can0 up
86 ```
87
88 Then connect your USB CAN device that will be named `can0` by default.
89
90 # Configure the binding
91
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`.
94
95 Default binding configuration use a CAN bus named `hs` so you need to map it to the real one, here are some examples:
96
97 * Using virtual CAN device as described in the previous chapter:
98 ```ini
99 [CANbus-mapping]
100 hs="vcan0"
101 ls="vcan1"
102 ```
103
104 * Using real CAN device, this example assume CAN bus traffic will be on can0.
105 ```ini
106 [CANbus-mapping]
107 hs="can0"
108 ls="can1"
109 ```
110
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`:
112 ```ini
113 [CANbus-mapping]
114 hs="can1"
115 ```
116
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`.
118
119 # Run it, test it, use it !
120
121 You can run the binding using **afm-util** tool, here is the classic way to go :
122
123 ```bash
124 afm-util run low-can-service@4.0
125 1
126 ```
127
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.
129
130 But you can't control nor interact with it because you don't know security token that **Application Framework** gaves it at launch.
131
132 So, to test it, it is better to launch the binding manually. In the following example, we will use port **1234** and left empty security token for testing purpose:
133
134 ```bash
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
144 ```
145
146 On another terminal, connect to the binding using previously installed _**AFB Websocket CLI**_ tool:
147
148 ```bash
149 afb-client-demo ws://localhost:1234/api?token=1
150 ```
151
152 You will be on an interactive session where you can communicate directly with the binding API.
153
154 The binding provides at this moment 2 verbs, _subscribe_ and _unsubscribe_, which can take argument by a JSON **event** object.
155
156 The argument value is the CAN message **generic\_name** as described in the JSON file used to generate cpp file for the binding.
157
158 To use the _**AFB Websocket CLI**_ tool, a command line will be like the following :
159
160 ```
161 <api> <verb> <arguments>
162 ```
163
164 Where:
165
166 * API : _**low-can**_.
167 * Verb : _**subscribe**_ or _**unsubscribe**_
168 * Arguments : _**{ "event": "driver.doors.open" }**_
169
170 ## Subscription and unsubscription
171
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.
173
174 > **NOTE:** If no argument is provided, then you'll subscribe to all signals at once.
175
176 For example from a websocket session:
177
178 ```json
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"}}
181 ```
182
183 Subscription and unsubscription can take wildcard in their _event_ value.
184
185 To receive all doors events :
186
187 ```json
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"}}
190 ```
191
192 Then you will receive an event each time a CAN message is decoded for the event named _doors.driver.open_
193
194 ```json
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"})
196 ```
197
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_.
199
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.
201
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:
203
204 ```json
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.
211 ```
212
213 You can stop receiving event from it by unsubscribe the signal the same way you did for subscribe
214
215 ```json
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"}}
220 ```
221
222 ### Filtering capabilities
223
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 :
225
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)
229
230 Order doesn't matter neither the number of filters chosen, you can use one, two or all of them at once.
231
232 Usage examples :
233
234 ```json
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}}
238 ```
239
240 ## Using CAN utils to monitor CAN activity
241
242 You can watch CAN traffic and send custom CAN messages using can-utils preinstalled on AGL target.
243
244 To watch watch going on a CAN bus use:
245
246 ```bash
247 candump can0
248 ```
249
250 Or for an USB CAN adapter connected to porter board:
251
252 ```bash
253 candump can1
254 ```
255
256 Send a custom message:
257
258 ```bash
259 cansend can0 ID#DDDDAAAATTTTAAAA
260 ```
261
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.
263
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.
265
266 Replay on a virtual CAN device `vcan0`:
267 ```bash
268 canplayer -I trip_test_with_obd2_vehicle_speed_requests vcan0=can0
269 ```
270
271 Replay on a CAN device `can0`:
272 ```bash
273 canplayer -I trip_test_with_obd2_vehicle_speed_requests can0
274 ```
275
276 Replay on a CAN device `can1` (porter by example):
277 ```bash
278 canplayer -I trip_test_with_obd2_vehicle_speed_requests can1=can0
279 ```