Improve READme instructions.
[apps/agl-service-can-low-level.git] / README.md
1 # Low level CAN signaling binder
2
3 Low level CAN bus binder. Based upon OpenXC vi-firmware project. Purpose of this project is to offer a low level binding to an AGL platform, idea remains the same than the vi-firmware project. It's meant to generate from a JSON file describing CAN messages and diagnostic message (OBD2 for now) present in a car, a cpp file to integrate with the project and compile all together. Result will be a widget file to install on an AGL target system.
4
5 # Prerequirements
6
7 - Make sure you already have set up the AGL SDK before using the following [guide][SDK_instructions].
8
9 - This repo make use of git submodule, make sure to execute the following commands from the repository once cloned :
10
11 ```bash
12 $ git submodule init
13 $ git submodule update
14 ```
15
16 - An [USB CAN adapter][USB_CAN] connected to OBD2 connector through the [right cable][OBD2_cable].
17
18 - Make sure you have installed the AGL generator else you aren't able to generate custom low-level CAN binding. Generator can be found [here][generator] with the attached instruction to install and run it. It will produce a *configuration-generated.cpp* file to paste in the source, *src/*, directory.
19
20 # Getting started
21
22 ## Compile and install the binding
23
24 With an AGL SDK environment correctly set, I encourage you to set the TARGET variable in the root CMakeLists.txt file if you have an AGL target already running in your network. Then you can directly build and install the binding and source directory on your target system.
25
26 Execute commands to get your binding compile :
27
28 ```bash
29 $ mkdir build
30 $ cd build
31 $ cmake ..
32 $ make
33 ```
34
35 And if you have set TARGET variable, you can install it on your AGL system :
36
37 ```bash
38 $ make install
39 [ 16%] Built target bitfield
40 [ 27%] Built target isotp
41 [ 40%] Built target openxc
42 [ 48%] Built target uds
43 [ 97%] Built target low-can-binding
44 [100%] Built target widget
45 Install the project...
46 -- Install configuration: ""
47 true
48 { "added": "low-can-binding@0.1" }
49 ```
50
51 It's possible that you'll see the following message :
52
53 ```bash
54 Error org.freedesktop.DBus.Error.Failed: "system error"
55 ```
56 It's because installation remove the binding before installing it. If it is the first time that you make the installation then you'll have this message in place of ***true***.
57
58 To install it manually, you need to copy the *low-can-binding.wgt* file on your target, then from it execute the following commands :
59
60 On your host, to copy over the network :
61 ```bash
62 $ scp low-can-binding.wgt root@<target_IP>:~
63 ```
64
65 On the target, assuming ***wgt*** file is in the root home directory :
66
67 ```bash
68 ~# afm-util install low-can-binding.wgt
69 { "added": "low-can-binding@0.1" } 
70 ```
71 ## Install AFB Websocket CLI client to test the binding.
72
73 You can test it using afb-client-demo CLI tool provided by the RPM package *libafbwsc-dev*. You can find this package in your build environment, using docker SDK recommended setup the file is */xdt/build/tmp/deploy/rpm/<your-target-arch>/*. Using Renesas RCar Gen2, porter board, you have to copy the file like this if your board is connected to your network and you know its IP address:
74
75 ```bash
76 $ scp /xdt/build/tmp/deploy/rpm/cortex15hf_neon/libafbwsc-dev-1.0-r0.cortexa15hf_neon.rpm root@<target_IP>:~
77 ```
78
79 Else, you have to copy into the SDcard with the AGL image installed on it.
80
81 From the docker image copy RPM to the shared directory between docker image and your host:
82
83 ```bash
84 $ cp /xdt/build/tmp/deploy/rpm/cortex15hf_neon/libafbwsc-dev-1.0-r0.cortexa15hf_neon.rpm ~/share
85 ```
86
87 Then plugin you SDcard in your Linux host (Windows can't read ext4 filesystem AGL runs on) and copy RPM file on it.
88
89 From you host, identify SDcard block device node here it is **sdc** with the correct capacity automounted by the desktop manager:
90
91 ```bash
92 $ lsblk
93 loop1                                                                                     7:1    0     2G  0 loop 
94 └─docker-253:0-3146365-pool                                                             253:3    0   100G  0 dm   
95   └─docker-253:0-3146365-e9f80849a2681e18549d3a4238cbf031e44052e36cd88a0abf041804b799b61c
96     253:4    0    10G  0 dm   /var/lib/docker/devicemapper/mnt/e9f80849a2681e18549d3a4238cbf031e44052e36cd88a0abf041804b799b61c
97 sdb                                                                                       8:16   0 238.5G  0 disk 
98 ├─sdb2                                                                                    8:18   0   238G  0 part 
99 │ └─Shamash-agl                                                                         253:1    0   238G  0 lvm  /home/claneys/Workspace/agl-docker
100 └─sdb1                                                                                    8:17   0   500M  0 part /boot
101 sr0                                                                                      11:0    1  1024M  0 rom  
102 loop0                                                                                     7:0    0   100G  0 loop 
103 └─docker-253:0-3146365-pool                                                             253:3    0   100G  0 dm   
104   └─docker-253:0-3146365-e9f80849a2681e18549d3a4238cbf031e44052e36cd88a0abf041804b799b61c
105     253:4    0    10G  0 dm   /var/lib/docker/devicemapper/mnt/e9f80849a2681e18549d3a4238cbf031e44052e36cd88a0abf041804b799b61c
106 sdc                                                                                       8:32   1  14.9G  0 disk 
107 └─sdc1                                                                                    8:33   1     2G  0 part /run/media/claneys/97f418a5-612f-44e9-b968-a19505695151
108 sda                                                                                       8:0    0 931.5G  0 disk 
109 ├─sda2                                                                                    8:2    0   500G  0 part 
110 │ ├─Shamash-home                                                                        253:2    0   150G  0 lvm  /home
111 │ └─Shamash-root                                                                        253:0    0    50G  0 lvm  /
112 └─sda1                                                                                    8:1    0    16G  0 part [SWAP]
113 ```
114
115 Copy, still from your host:
116
117 **Careful : Make sure to sync IO with sync command before unplug your SDcard. It could be corrupted if removed before all pending IO aren't done.**
118
119 ```bash
120 $ sudo cp ~/devel/docker/share/libafbwsc-dev-1.0-r0.cortexa15hf_neon.rpm /run/media/claneys/97f418a5-612f-44e9-b968-a19505695151/home/root
121 $ sync
122 ```
123
124 Insert the modified SDcard in your Porter board and boot from it. Your are ready to go.
125
126 ## Configure the AGL system
127
128 ### Virtual CAN device
129
130  Connected to the target, here is how to load the virtual CAN device driver and set up a new vcan device :
131
132 ```bash
133  ~# modprobe vcan
134  ~# ip link add vcan0 type vcan
135  ~# ip link set vcan0 up
136  ```
137
138 ### CAN device using the USB CAN adapter
139
140 Using real connection to CAN bus of your car using the USB CAN adapter connected to the OBD2 connector. (this instruction assuming a speed of 500000kbps for your device, you can try supported bitrate like 125000, 250000 if 500000 doesn't work) :
141
142 ```bash
143 ~# modprobe can
144 ~# ip link set can0 type can bitrate 500000
145 ~# ip link set can0 up
146 ~# ip link show can0
147   can0: <NOARP,UP,LOWER_UP,ECHO> mtu 16 qdisc pfifo_fast state UNKNOWN qlen 10
148     link/can 
149     can state ERROR-ACTIVE (berr-counter tx 0 rx 0) restart-ms 0 
150     bitrate 500000 sample-point 0.875 
151     tq 125 prop-seg 6 phase-seg1 7 phase-seg2 2 sjw 1
152     sja1000: tseg1 1..16 tseg2 1..8 sjw 1..4 brp 1..64 brp-inc 1
153     clock 16000000
154 ```
155
156 ## Configure the binding
157
158 Configure the binding specifying in the JSON configuration file the CAN device(s) that it will to connect to. Edit file */var/lib/afm/applications/low-can-binding/0.1/can_buses.json* and change the CAN device name to the one you have :
159
160 ```json
161 {
162         "canbus":  "can0"
163 }
164 ```
165
166 If you have several specify CAN bus devices using an array:
167
168 ```json
169 {
170         "canbus": [ "vcan0", "can0" ]
171 }
172 ```
173
174 ## Run it, test it, use it !
175
176 You can run the binding using **afm-util** tool, here is the classic way to go :
177
178 ```bash
179 ~# afm-util run low-can-binding@0.1
180 1
181 ```
182
183 You can find instructions to use afm-util tool [here][afm-util], as well as documentation about Application Framework.
184
185 But you can't control nor interact with it because you don't know security token that **Application Framework** gave it at launch. 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 :
186
187 ```bash
188 ~#  afb-daemon --ldpaths=/usr/lib/afb:/var/lib/afm/applications/low-can-binding/0.1/libs/ --rootdir=/var/lib/afm/applications/low-can-binding/0.1/ --port=1234 --token=
189 NOTICE: binding [/usr/lib/afb/afb-dbus-binding.so] calling registering function afbBindingV1Register
190 NOTICE: binding /usr/lib/afb/afb-dbus-binding.so loaded with API prefix dbus
191 NOTICE: binding [/usr/lib/afb/authLogin.so] calling registering function afbBindingV1Register
192 NOTICE: binding /usr/lib/afb/authLogin.so loaded with API prefix auth
193 NOTICE: binding [/var/lib/afm/applications/low-can-binding/0.1/libs//low-can-binding.so] calling registering function afbBindingV1Register
194 NOTICE: binding /var/lib/afm/applications/low-can-binding/0.1/libs//low-can-binding.so loaded with API prefix low-can
195 NOTICE: Waiting port=1234 rootdir=/var/lib/afm/applications/low-can-binding/0.1/
196 NOTICE: Browser URL= http:/*localhost:1234
197 NOTICE: vcan0 device opened and reading {binding low-can}
198 NOTICE: Initialized 1/1 can bus device(s) {binding low-can}
199 ```
200
201 Then connect to the binding using previously installed ***AFB Websocket CLI*** tool :
202
203 ```bash
204 ~# afb-client-demo ws://localhost:1234/api?token=
205 ```
206
207 You will be on an interactive session where you can pass ask directly to the binding API. Binding provide for the moment 2 verbs, subscribe and unsubscribe that can take argument by a JSON **event** object taking a CAN message name as value. To use the ***AFB Websocket CLI*** tool, a command line will be like the following :
208
209 ```
210 <api> <verb> <arguments>
211 ```
212
213 Where API will be : ***low-can***.
214 Verb : ***subscribe*** or ***unsubscribe***
215 Arguments : ***{ "event": "driver.doors.open" }***
216
217 ### Subscription and unsubscription
218
219 You can ask to subscribe to chosen CAN event with a call to *subscribe* API verb with the CAN messages name as JSON argument. Example from a websocket session:
220
221 ```bash
222 low-can subscribe { "event": "doors.driver.open" }
223 ON-REPLY 1:low-can/subscribe: {"jtype":"afb-reply","request":{"status":"success","uuid":"a18fd375-b6fa-4c0e-a1d4-9d3955975ae8"}}
224 ```
225
226 Subscription and unsubscription can take wildcard in their *event* value. To reveive all doors events :
227
228 ```bash
229 low-can subscribe { "event" : "doors*" }
230 ON-REPLY 1:low-can/subscribe: {"jtype":"afb-reply","request":{"status":"success","uuid":"511c872e-d7f3-4f3b-89c2-aa9a3e9fbbdb"}}
231 ```
232
233 Then you will receive an event each time a CAN message is decoded for the event named *doors.driver.open*
234
235 ```
236 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"})
237 ```
238
239 Notice that event shows you that the CAN event is named *messages.doors.driver.open* but you ask for event about *doors.driver.open*. 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. 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:
240
241 ```
242 low-can subscribe { "event" : "*speed*" } --> will subscribe to all messages with speed in their name. Search will be make without prefix for it.
243 low-can subscribe { "event" : "speed*" } --> will subscribe to all messages begin by speed in their name. Search will be make without prefix for it.
244 low-can subscribe { "event" : "messages*speed*" } --> will subscribe to all CAN messages with speed in their name. Search will be on prefixed messages here.
245 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.
246 low-can subscribe { "event" : "diagnostic*speed*" } --> will subscribe to all diagnostic messages with speed in their name. Search will be on prefixed messages here.
247 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.
248 ```
249
250 You can stop receiving event from it by unsubscribe the signal the same way you did for subscribe
251
252 ```bash
253 low-can unsubscribe { "event": "doors.driver.open" }
254 ON-REPLY 2:low-can/unsubscribe: {"jtype":"afb-reply","request":{"status":"success"}}
255 low-can unsubscribe { "event" : "doors*" }
256 ON-REPLY 3:low-can/unsubscribe: {"jtype":"afb-reply","request":{"status":"success"}}
257 ```
258
259 [USB_CAN]: http://reference.com/ "USB CAN adapter recommended"
260 [OBD2_cable]: http://foo.bar/ "OBD2<->DB9 recommended cable"
261 [SDK_instructions]: http://docs.iot.bzh/docs/getting_started/en/dev/reference/setup-sdk-environment.html "Setup SDK environment"
262 [generator]: http://github.com/iotbzh/can-config-generator "AGL low level CAN binding Generator"
263 [afm-util]: http://docs.iot.bzh/docs/apis_services/en/dev/reference/af-main/afm-daemons.html#using-afm-util "afm-util usage"