Path from CANoe db to JSON files using OpenXC utilities.
[apps/agl-service-can-low-level.git] / docs / 2-Installation.md
1 # Prerequisites
2
3 * An AGL system installed with latest Chinook version \(>3.0.2\).
4
5 * Make sure you built the AGL generator else you will not be able to generate custom low-level CAN binding. Generator can be found [here](http://github.com/iotbzh/can-config-generator) with the attached instruction to install and run it.
6
7 It will produce a _configuration-generated.cpp_ file to paste in the source, _src/_, directory.
8
9 * Make sure you already set up the AGL SDK using the following [guide](http://docs.iot.bzh/docs/getting_started/en/dev/reference/setup-sdk-environment.html).
10
11 To get the correct SDK version installed, you **must** prepare your environment with the **chinook-next** version. To do so, run the following command in your docker image:
12
13 ```bash
14 $ prepare_meta -f chinook-next -o /xdt -l /home/devel/mirror -p /home/devel/share/proprietary-renesas-rcar/ -t porter -e wipeconfig -e rm_work
15 ```
16
17 * Check that you updated git submodules, executing the following commands from this repository:
18
19 ```bash
20 $ git submodule init
21 $ git submodule update
22 ```
23
24 * An [USB CAN adapter](http://shop.8devices.com/usb2can) connected to connector through the [right cable](http://www.mouser.fr/ProductDetail/EasySync/OBD-M-DB9-F-ES/)).
25
26 # Getting started
27
28 ## Use of CAN config generator
29
30 ### Build requirements
31
32 * CMake version 3.0 or later
33 * G++, Clang++ or any C++11 complient compiler.
34 * Boost
35   * filesystem
36   * program\_options
37   * system
38
39 You can install any of these using your package manager. For instance, inside the iotbzh's docker image, you must enter this command :
40
41 ```bash
42 $ sudo apt-get install cmake libboost-system-dev libboost-filesystem-dev libboost-program-options-dev
43 ```
44
45 You may want to install `libboost-all-dev` to get all boost components even if it's not required.
46
47 ### Compile
48
49 > **CAUTION** It is **very important** that you do not source the SDK environment file to compile this project because some build requirements aren't installed in the AGL SDK for now. 
50
51 ```bash
52 $ git clone https://github.com/iotbzh/can-config-generator.git
53 Cloning into 'can-config-generator'...
54 remote: Counting objects: 74, done.
55 remote: Compressing objects: 100% (42/42), done.
56 remote: Total 74 (delta 30), reused 67 (delta 27), pack-reused 0
57 Unpacking objects: 100% (74/74), done.
58 Checking connectivity... done.
59
60 <b>devel@bsp-devkit:~/projects/$</b> cd can-config-generator
61 <b>devel@bsp-devkit:~/projects/can-config-generator/(master)$</b> mkdir build
62 <b>devel@bsp-devkit:~/projects/can-config-generator/build(master)$</b> cmake -G "Unix Makefiles" ..
63 -- The C compiler identification is GNU 4.9.2
64 -- The CXX compiler identification is GNU 4.9.2
65 -- Check for working C compiler: /usr/bin/cc
66 -- Check for working C compiler: /usr/bin/cc -- works
67 -- Detecting C compiler ABI info
68 -- Detecting C compiler ABI info - done
69 -- Check for working CXX compiler: /usr/bin/c++
70 -- Check for working CXX compiler: /usr/bin/c++ -- works
71 -- Detecting CXX compiler ABI info
72 -- Detecting CXX compiler ABI info - done
73 -- Boost version: 1.55.0
74 -- Found the following Boost libraries:
75 --   program_options
76 --   filesystem
77 --   system
78 -- Configuring done
79 -- Generating done
80 -- Build files have been written to: /home/devel/projects/can-config-generator/build
81 <b>devel@bsp-devkit:~/projects/can-config-generator/build(master)$</b> make
82 Scanning dependencies of target can-config-generator
83 [ 12%] Building CXX object CMakeFiles/can-config-generator.dir/src/main.cpp.o
84 [ 25%] Building CXX object CMakeFiles/can-config-generator.dir/src/openxc/message_set.cpp.o
85 [ 37%] Building CXX object CMakeFiles/can-config-generator.dir/src/openxc/can_bus.cpp.o
86 [ 50%] Building CXX object CMakeFiles/can-config-generator.dir/src/openxc/can_message.cpp.o
87 [ 62%] Building CXX object CMakeFiles/can-config-generator.dir/src/openxc/command.cpp.o
88 [ 75%] Building CXX object CMakeFiles/can-config-generator.dir/src/openxc/diagnostic_message.cpp.o
89 [ 87%] Building CXX object CMakeFiles/can-config-generator.dir/src/openxc/mapping.cpp.o
90 [100%] Building CXX object CMakeFiles/can-config-generator.dir/src/openxc/signal.cpp.o
91 Linking CXX executable can-config-generator
92 [100%] Built target can-config-generator
93 ```
94
95 ### Naming convention
96
97 We choose a doted naming convention because it's a well know schema.
98
99 It separates and organize names into hierarchy. From the left to right, you describe your names using the more common ancestor at the left then more you go to the right the more it will be accurate.
100
101 Let's take an example, here is an example about standard PID name following this convention:
102
103 ```
104 engine.load
105 engine.coolant.temperature
106 fuel.pressure
107 intake.manifold.pressure
108 engine.speed
109 vehicle.speed
110 intake.air.temperature
111 mass.airflow
112 throttle.position
113 running.time
114 EGR.error
115 fuel.level
116 barometric.pressure
117 commanded.throttle.position
118 ethanol.fuel.percentage
119 accelerator.pedal.position
120 hybrid.battery-pack.remaining.life
121 engine.oil.temperature
122 engine.torque
123 ```
124
125 > **NOTE** It's recommended that you follow this naming convention to named your CAN signals.
126 > There is only character `*` that is forbidden in names because it's used as wildcard for subscription and unsubscrition.
127 > This described in the below chapter.
128
129 ### Generating JSON from Vector CANoe Database
130
131 If you use Canoe to store your `gold standard` CAN signal definitions, you may be able to use the OpenXC  `xml_to_json.py` script to make your JSON for you. First, export the Canoe .dbc file as XML - you can do this with Vector CANdb++. Next, create a JSON file according to the format defined above, but only define:
132
133 - CAN messages.
134 - Name of CAN signals within messages and their generic_name.
135 - Optionnaly name of diagnostic messages and their name.
136
137 To install the OpenXC utilities and runs `xml_to_json.py` script:
138
139 ```bash
140 $ sudo pip install openxc
141 $ cd /usr/local/lib/python2.7/dist-packages/openxc-0.13.0-py2.7.egg/openxc/generator
142 ```
143
144 Assuming the data exported from Vector is in `signals.xml` and your minimal mapping file is `mapping.json`, run the script:
145
146 ```bash
147 $ ./xml_to_json.py signals.xml mapping.json signals.json
148 ```
149
150 The script scans `mapping.json` to identify the CAN messages and signals that you want to use from the XML file. It pulls the neccessary details of the messages (bit position, bit size, offset, etc) and outputs the resulting subset as JSON into the output file, `signals.json`.
151
152 The resulting file together with `mapping.json` will work as input to the code generation script.
153
154 ### Generate your config file
155
156 To generate your config file you just have to run the generator using the `-m` option to specify your JSON file.
157
158 ```bash
159 $ can-config-generator -m ../tests/basic.json -o configuration-generated.cpp
160 ```
161
162 If you omit the `-o` option, then code is generated on the stdout.  
163 You also can specify a header and a footer file.  
164 These files must be valid C++ fragment as long as they will be inserted as is.  
165 Use the `-h` option to display help.
166
167 > **CAUTION:** Each `diagnostic_message` must define the same `bus` as the binding will use only one bus.
168
169 ### Supported OpenXC items
170
171 About now, compliance with OpenXC reference is in progress, can-config-generator and CAN\_signaling will implement them soon.  
172 `initializers`, `loopers`, `commands` and `handlers` nodes are ignored for now.
173
174 This generator will follow OpenXC support status of the low level CAN signaling binding.
175
176 > **NOTE**: The `buses` item will not be supported by this generator because the binding use another way to declare and configure buses. Please refer to the binding's documentation.
177
178 ## Compile and install the binding
179
180 With an AGL SDK environment correctly configured and **sourced**, I suggest you to set the TARGET variable in the root CMakeLists.txt file if you have an AGL target already running in your network.
181
182 Then you can directly build and install the binding and source directory on your target system.
183
184 Execute these commands to get your binding compile :
185
186 ```bash
187 $ mkdir build
188 $ cd build
189 $ cmake ..
190 $ make
191 ```
192
193 And if you have set TARGET variable, you can install it on your AGL system :
194
195 ```bash
196 $ make install
197 [ 16%] Built target bitfield
198 [ 27%] Built target isotp
199 [ 40%] Built target openxc
200 [ 48%] Built target uds
201 [ 97%] Built target low-can-binding
202 [100%] Built target widget
203 Install the project...
204 -- Install configuration: ""
205 true
206 { "added": "low-can-binding@0.1" }
207 ```
208
209 It's possible that you'll see the following message :
210
211 ```bash
212 Error org.freedesktop.DBus.Error.Failed: "system error"
213 ```
214
215 It's because installation remove the binding before installing it.
216
217 If it is the first time that you make the installation then you'll have this message in place of _**true**_.
218
219 To install it manually, you need to copy the _low-can-binding.wgt_ file on your target, then from it execute the following commands :
220
221 On your host, to copy over the network :
222
223 ```bash
224 $ scp low-can-binding.wgt root@<target_IP>:~
225 ```
226
227 On the target, assuming _**wgt**_ file is in the root home directory :
228
229 ```bash
230 ~# afm-util install low-can-binding.wgt
231 { "added": "low-can-binding@0.1" }
232 ```