resource: generate missing acquire event when autoacquire is set
[profile/ivi/murphy.git] / packaging.in / murphy.lua
1 m = murphy.get()
2
3 -- try loading the various logging plugins
4 m:try_load_plugin('systemd')
5 m:try_load_plugin('dlog')
6
7 -- load the console plugin
8 m:try_load_plugin('console')
9
10 m:try_load_plugin('console.disabled', 'webconsole', {
11                   address = 'wsck:127.0.0.1:3000/murphy',
12                   httpdir = '/usr/share/murphy/webconsole' });
13
14 -- load the dbus plugin
15 if m:plugin_exists('dbus') then
16     m:load_plugin('dbus')
17 end
18
19 -- load the native resource plugin
20 if m:plugin_exists('resource-native') then
21     m:load_plugin('resource-native')
22     m:info("native resource plugin loaded")
23 else
24     m:info("No native resource plugin found...")
25 end
26
27 -- load the dbus resource plugin
28 m:try_load_plugin('resource-dbus', {
29     dbus_bus = "system",
30     dbus_service = "org.Murphy",
31     dbus_track = true,
32     default_zone = "driver",
33     default_class = "implicit"
34 })
35
36 -- load the domain control plugin
37 if m:plugin_exists('domain-control') then
38     m:load_plugin('domain-control')
39 else
40     m:info("No domain-control plugin found...")
41 end
42
43 -- load the AMB plugin
44 if m:plugin_exists('amb') then
45     m:load_plugin('amb')
46 else
47     m:info("No amb plugin found...")
48 end
49
50 -- load the ASM resource plugin
51 if m:plugin_exists('resource-asm') then
52     m:load_plugin('resource-asm', {
53         zone = "driver",
54         share_mmplayer = "player:AVP,mandatory,exclusive,relaxed"
55     })
56 else
57     m:info("No audio session manager plugin found...")
58 end
59
60 if m:plugin_exists('ivi-resource-manager') then
61     m:load_plugin('ivi-resource-manager')
62 end
63
64 -- define application classes
65 application_class {
66     name     = "interrupt",
67     priority = 99,
68     modal    = true ,
69     share    = false,
70     order    = "fifo"
71 }
72
73 application_class {
74     name     = "emergency",
75     priority = 80,
76     modal    = false,
77     share    = false,
78     order    = "fifo"
79 }
80 application_class {
81     name     = "alert",
82     priority = 51,
83     modal    = false,
84     share    = false,
85     order    = "fifo"
86 }
87
88 application_class {
89     name     = "navigator",
90     priority = 50,
91     modal    = false,
92     share    = true,
93     order    = "fifo"
94 }
95
96 application_class {
97     name     = "phone",
98     priority = 6 ,
99     modal    = false,
100     share    = true ,
101     order    = "lifo"
102 }
103 application_class {
104     name     = "camera",
105     priority = 5,
106     modal    = false,
107     share    = false,
108     order    = "lifo"
109 }
110
111 application_class { name="event"    , priority=4 , modal=false, share=true , order="fifo" }
112 application_class { name="game"     , priority=3 , modal=false, share=false, order="lifo" }
113 --# doesn't need to be created here, ivi-resource-manager creates it if loaded
114 --#application_class { name="basic"    , priority=2 , modal=false, share=false, order="lifo" }
115 application_class { name="player"   , priority=1 , modal=false, share=true , order="lifo" }
116 application_class { name="implicit" , priority=0 , modal=false, share=false, order="lifo" }
117
118 -- define zone attributes
119 zone.attributes {
120     type = {mdb.string, "common", "rw"},
121     location = {mdb.string, "anywhere", "rw"}
122 }
123
124 -- define zones
125 zone {
126      name = "driver",
127      attributes = {
128          type = "common",
129          location = "front-left"
130      }
131 }
132
133 zone {
134      name = "passanger1",
135      attributes = {
136          type = "private",
137          location = "front-right"
138      }
139 }
140
141 zone {
142      name = "passanger2",
143      attributes = {
144          type = "private",
145          location = "back-left"
146      }
147 }
148
149 zone {
150      name = "passanger3",
151      attributes = {
152          type = "private",
153          location = "back-right"
154      }
155 }
156
157 zone {
158      name = "passanger4",
159      attributes = {
160          type = "private",
161          location = "back-left"
162      }
163 }
164
165
166 -- define resource classes
167 if not m:plugin_exists('ivi-resource-manager') then
168    resource.class {
169         name = "audio_playback",
170         shareable = true,
171         attributes = {
172             role = { mdb.string, "music", "rw" },
173             pid = { mdb.string, "<unknown>", "rw" },
174             policy = { mdb.string, "relaxed", "rw" }
175         }
176    }
177 end
178
179 resource.class {
180      name = "audio_recording",
181      shareable = true,
182      attributes = {
183          role   = { mdb.string, "music"    , "rw" },
184          pid    = { mdb.string, "<unknown>", "rw" },
185          policy = { mdb.string, "relaxed"  , "rw" }
186      }
187 }
188
189 resource.class {
190      name = "video_playback",
191      shareable = false,
192 }
193
194 resource.class {
195      name = "video_recording",
196      shareable = false
197 }
198
199 if not m:plugin_exists('ivi-resource-manager') then
200 resource.method.veto = {
201     function(zone, rset, grant, owners)
202         rset_priority = application_class[rset.application_class].priority
203
204         owner_id = owners.audio_playback.resource_set
205         rset_id = rset.id
206
207         if (rset_priority >= 50 and owner_id ~= rset_id) then
208             print("*** resource-set "..rset_id.." - veto")
209             return false
210         end
211
212         return true
213     end
214 }
215 end
216
217 -- test for creating selections
218 mdb.select {
219            name = "audio_owner",
220            table = "audio_playback_owner",
221            columns = {"application_class"},
222            condition = "zone_name = 'driver'"
223 }
224
225 mdb.select {
226            name = "vehicle_speed",
227            table = "amb_vehicle_speed",
228            columns = {"value"},
229            condition = "key = 'VehicleSpeed'"
230 }
231
232 element.lua {
233    name    = "speed2volume",
234    inputs  = { speed = mdb.select.vehicle_speed, param = 9 },
235    outputs = {  mdb.table { name = "speedvol",
236                             index = {"zone", "device"},
237                             columns = {{"zone", mdb.string, 16},
238                                        {"device", mdb.string, 16},
239                                        {"value", mdb.floating}},
240                             create = true
241                            }
242              },
243    oldvolume = 0.0,
244    update  = function(self)
245                 speed = self.inputs.speed.single_value
246                 if (speed) then
247                     volume = (speed - 144.0) / 7.0
248                 else
249                     volume = 0.0
250                 end
251                 diff = volume - self.oldvolume
252                 if (diff*diff > self.inputs.param) then
253                     print("*** element "..self.name.." update "..volume)
254                     self.oldvolume = volume
255                     mdb.table.speedvol:replace({zone = "driver", device = "speakers", value = volume})
256                 end
257              end
258 }
259
260 -- Night mode processing chain
261
262 mdb.select {
263     name = "exterior_brightness",
264     table = "amb_exterior_brightness",
265     columns = { "value" },
266     condition = "key = 'ExteriorBrightness'"
267 }
268
269 mdb.table {
270     name = "amb_nightmode",
271     index = { "id" },
272     create = true,
273     columns = {
274         { "id", mdb.unsigned },
275         { "night_mode", mdb.unsigned }
276     }
277 }
278
279 element.lua {
280     name    = "nightmode",
281     inputs  = { brightness = mdb.select.exterior_brightness },
282     oldmode = -1;
283     outputs = {
284     mdb.table {
285         name = "mandatory_placeholder_to_prevent_spurious_updates",
286             create = true,
287             columns = { { "id", mdb.unsigned } }
288         }
289     },
290     update = function(self)
291         -- This is a trivial function to calculate night mode. Later, we will
292         -- need a better threshold value and hysteresis to prevent oscillation.
293
294         brightness = self.inputs.brightness.single_value
295
296         if not brightness then
297             return
298         end
299
300         print("*** element "..self.name.." update brightness: "..brightness)
301
302         if brightness > 300 then
303             mode = 0
304         else
305             mode = 1
306         end
307
308         print("*** resulting mode: ".. mode)
309
310         if not (mode == self.oldmode) then
311             mdb.table.amb_nightmode:replace({ id = 0, night_mode = mode })
312         end
313
314         self.oldmode = mode
315     end
316 }
317
318 mdb.select {
319     name = "select_night_mode",
320     table = "amb_nightmode",
321     columns = { "night_mode" },
322     condition = "id = 0"
323 }
324
325 sink.lua {
326     name = "night_mode",
327     inputs = { owner = mdb.select.select_night_mode },
328     property = "NightMode",
329     type = "b",
330     initiate = builtin.method.amb_initiate,
331     update = builtin.method.amb_update
332 }
333
334
335 -- Driving mode processing chain
336
337 mdb.table {
338     name = "amb_drivingmode",
339     index = { "id" },
340     create = true,
341     columns = {
342         { "id", mdb.unsigned },
343         { "driving_mode", mdb.unsigned }
344     }
345 }
346
347 element.lua {
348     name    = "drivingmode",
349     inputs  = { speed = mdb.select.vehicle_speed },
350     oldmode = -1;
351     outputs = {
352     mdb.table {
353         name = "another_mandatory_placeholder_to_prevent_spurious_updates",
354             create = true,
355             columns = { { "id", mdb.unsigned } }
356         }
357     },
358     update = function(self)
359
360         speed = self.inputs.speed.single_value
361
362         if not speed then
363             return
364         end
365
366         if speed == 0 then
367             mode = 0
368         else
369             mode = 1
370         end
371
372         if not (mode == self.oldmode) then
373             mdb.table.amb_drivingmode:replace({ id = 0, driving_mode = mode })
374         end
375
376         self.oldmode = mode
377     end
378 }
379
380 mdb.select {
381     name = "select_driving_mode",
382     table = "amb_drivingmode",
383     columns = { "driving_mode" },
384     condition = "id = 0"
385 }
386
387 sink.lua {
388     name = "driving_mode",
389     inputs = { owner = mdb.select.select_driving_mode },
390     property = "DrivingMode",
391     type = "u",
392     initiate = builtin.method.amb_initiate,
393     update = builtin.method.amb_update
394 }
395
396 -- load the telephony plugin
397 m:try_load_plugin('telephony')