doc: add new document quick-tutorial
[src/app-framework-main.git] / doc / quick-tutorial.md
1 AGL Application Framework: A Quick Tutorial
2 ===========================================
3 [TOC]
4
5 Introduction
6 ------------
7 This document proposes a quick tutorial to demonstrate the major functionnalities of the AGL Application Framework. For more complete information, please refer to the inline documentation available in the main git repository: 
8 https://gerrit.automotivelinux.org/gerrit/#/admin/projects/src/app-framework-main
9 https://gerrit.automotivelinux.org/gerrit/#/admin/projects/src/app-framework-binder
10
11 For more information on AGL, please visit:
12 https://www.automotivelinux.org/
13
14 ----------
15
16 Sample applications
17 -------------------
18 4 sample applications (.wgt files) are prebuilt and available at the following address:
19 https://github.com/iotbzh/afm-widget-examples
20
21 You can get them by cloning this git repository on your desktop (will be useful later in this tutorial):
22
23 ```
24 $ git clone https://github.com/iotbzh/afm-widget-examples
25 ```
26
27 Using the CLI tool
28 ------------------
29
30 ### Setup Environment
31 Connect your AGL target board to the network and copy some sample widgets on it through SSH (set BOARDIP with your board IP address) :
32 ```
33 $ cd afm-widget-examples
34 $ BOARDIP=1.2.3.4
35 $ scp *.wgt root@$BOARDIP:~/
36 ```
37
38 Connect through SSH on the target board and check for Application Framework daemons:
39
40     $ ssh root@$BOARDIP
41     root@porter:~# ps -ef|grep bin/afm
42     afm        409     1  0 13:00 ?        00:00:00 /usr/bin/afm-system-daemon
43     root       505   499  0 13:01 ?        00:00:00 /usr/bin/afm-user-daemon
44     root       596   550  0 13:22 pts/0    00:00:00 grep afm
45
46 We can see that there are two daemons running:
47 * **afm-system-daemon** runs with a system user 'afm' and is responsible for installing/uninstalling packages
48 * **afm-user-daemon** runs as a user daemon (currently as root because it's the only real user on the target board) and is responsible for the whole lifecycle of the applications running inside the user session.
49
50 The application framework has a tool running on the Command Line Interface (CLI). Using the **afm-util** command, you can install, uninstall, list, run, stop ... applications. 
51
52 To begin, run '**afm-util help**' to get a quick help on commands:
53
54     root@porter:~# afm-util help
55     usage: afm-util command [arg]
56     
57     The commands are:
58     
59       list
60       runnables      list the runnable widgets installed
61     
62       add wgt
63       install wgt    install the wgt file
64     
65       remove id
66       uninstall id   remove the installed widget of id
67     
68       info id
69       detail id      print detail about the installed widget of id
70     
71       ps
72       runners        list the running instance
73     
74       run id
75       start id       start an instance of the widget of id
76     
77       kill rid
78       terminate rid       terminate the running instance rid
79     
80       stop rid
81       pause rid      stop the running instance rid
82     
83       resume rid
84       continue rid   continue the previously rid
85     
86       status rid
87       state rid     get status of the running instance rid
88
89 ### Install an application
90
91 You can then install your first application:
92
93     root@porter:~# afm-util install /home/root/annex.wgt 
94     { "added": "webapps-annex@0.0" }
95
96 Let's install a second application:
97
98     root@porter:~# afm-util install /home/root/memory-match.wgt 
99     { "added": "webapps-memory-match@1.1" }
100
101 Note that usually, **afm-util** will return a **JSON result**, which is the common format for messages returned by the Application Framework daemons.
102
103 ### List installed applications
104 You can then list all installed applications:
105
106     root@porter:~# afm-util list
107     [ { "id": "webapps-annex@0.0", "version": "0.0.10", "width": 0, "height": 0, "name": "Annex", "description": "Reversi\/Othello", "shortname": "", "author": "Todd Brandt <todd.e.brandt@intel.com>" },
108      { "id": "webapps-memory-match@1.1", "version": "1.1.7", "width": 0, "height": 0, "name": "MemoryMatch", "description": "Memory match", "shortname": "", "author": "Todd Brandt <todd.e.brandt@intel.com>" } ]
109
110 Here, we can see the two previously installed applications.
111
112 ### Get information about an application
113 Let's get some details about the first application:
114
115     root@porter:~# afm-util info webapps-annex@0.0
116     { "id": "webapps-annex@0.0", "version": "0.0.10", "width": 0, "height": 0, "name": "Annex", "description": "Reversi\/Othello", "shortname": "", "author": "Todd Brandt <todd.e.brandt@intel.com>" }
117
118 Note that AGL applications are mostly handled by afm-util through their IDs. In our example, the application ID is 'webapps-annex@0.0'.
119
120 ### Start application
121 Let's start the first application Annex:
122
123     root@porter:~# afm-util start webapps-annex@0.0
124     1
125
126 As the application is a HTML5 game, you should then get a webview running with QML on the board display.
127
128 ### Security Context
129 The application has been started in the user session, with a dedicated security context, enforced by SMACK. To illustrate this, we can take a look at the running processes and their respective SMACK labels:
130
131     root@porter:~# ps -efZ |grep webapps-annex | grep -v grep
132     User::App::webapps-annex        root       716   491  0 13:19 ?        00:00:00 /usr/bin/afb-daemon --mode=local --readyfd=8 --alias=/icons /usr/share/afm/icons --port=12348 --rootdir=/usr/share/afm/applications/webapps-annex/0.0 --token=7D6D2F16 --sessiondir=/home/root/app-data/webapps-annex/.afb-daemon
133     User::App::webapps-annex        root       717   491  0 13:19 ?        00:00:00 /usr/bin/qt5/qmlscene http://localhost:12348/index.html?token=7D6D2F16 /usr/bin/web-runtime-webkit.qml
134
135 In the previous result, we see that the application is composed of two processes:
136 * the application binder (afb-daemon)
137 * the application UI (qmlscene ...)
138
139 While most system processes run with the label 'System', we see that the application runs with a specific SMACK label 'User::App::webapps-annex': this label is used to force the application to follow a Mandatory Access Control (MAC) scheme. This means that those processes run in their own security context, isolated from the rest of the system (and other applications). Global security rules can then be applied to restrict access to all other user or system resources.
140
141 ### Check running applications
142 To check for running applications, just run:
143
144     root@porter:~# afm-util ps
145     [ { "runid": 1, "state": "running", "id": "webapps-annex@0.0" } ]
146
147 The 'runid' is the application instance ID and is used as an argument for the subcommands controlling the application runtime state (kill/stop/resume/status)
148
149 ### Stop application
150 To stop the application that was just started (the one with RUNID 1), just run the stop command:
151
152     root@porter:~# afm-util terminate 1
153     true
154     
155 The application is now stopped, as confirmed by a list of running apps:
156
157     root@porter:~# afm-util ps
158     [ ]
159
160
161 ### Uninstall application
162 To uninstall an application, simply use its ID:
163
164     root@porter:~# afm-util uninstall webapps-annex@0.0
165     true
166
167 Then list the installed apps to confirm the removal:
168
169     root@porter:~# afm-util list
170     [ { "id": "webapps-memory-match@1.1", "version": "1.1.7", "width": 0, "height": 0, "name": "MemoryMatch", "description": "Memory match", "shortname": "", "author": "Todd Brandt <todd.e.brandt@intel.com>" } ]
171
172 afm-client: a sample HTML5 'Homescreen'
173 ---------------------------------------
174
175 **afm-client** is a HTML5 UI that allows to install/uninstall applications as well as starting/stopping them as already demonstrated with afm-util.
176
177 The HTML5 UI is accessible remotely through this URL: 
178 http://[board_ip]:1234/opa?token=132456789
179
180 ### Installing an application
181
182 By clicking on the '**Upload**' button on the right, you can send an application package (WGT file) and install it. Select for example the file '**rabbit.wgt**' that was cloned initially from the git repository afm-widget-examples.
183
184 Then a popup requester ask for a confirmation: 'Upload Application rabbit.wgt ?'. Click on the '**Install**' button.
185
186 You should then see some changes in the toolbar: a new icon appeared, representing the freshly installed application.
187
188 ### Running an application
189 In the toolbar, click on the button representing the Rabbit application. You'll get a popup asking to:
190 * start the application
191 * or get some info about it
192 * or uninstall it
193
194 Click on the 'start' item: the application starts and should be visible as a webview on the target board display. Note that at this point, we could also run the application remotely, that is in the same browser as the Homescreen app. By default, the application framework is configured to run applications 'locally' on the board display.
195
196 ### Stopping an application
197
198 In the Homescreen application, click again on the Rabbit application button, then select 'stop': the application then stops.
199
200 ### Uninstalling an application
201
202 From the same popup menu, you can select 'uninstall' to remove the application from the system. As a consequence, the application icon should disappear from the toolbar.
203
204 afb-client: a template for Angular Applications
205 -----------------------------------------------
206
207 Another package '**afb-client**' is also available for testing.
208 This is a sample HTML5 application demonstrating various basic capabilities of the Binder daemon. It can be used by developers as a template to start writing real AGL Applications.
209
210 This application is not available as WGT file yet and it should be started manually without any specific security context:
211
212     root@porter:~# /usr/bin/afb-daemon --mode=remote --port=1235 --token='' --sessiondir=/home/root/.afm-daemon --rootdir=/usr/share/agl/afb-client --alias=/icons:/usr/share/afm/icons
213
214 Then you can access it from a browser:
215 http://[board_ip]:1235/opa/?token=132456789
216
217 afb-client is a simple application to demonstrate the built-in capabilities of the binder daemon (handling sessions and security tokens, testing POSTs uploads...) and was used during the application framework development to validate the proposed features.