config: enabling ivi-resource-manager
[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 { name="interrupt", priority=99, modal=true , share=false, order="fifo" }
66 application_class { name="emergency", priority=80, modal=false, share=false, order="fifo" }
67 application_class { name="alert"    , priority=51, modal=false, share=false, order="fifo" }
68 application_class { name="navigator", priority=50, modal=false, share=true , order="fifo" }
69 application_class { name="phone"    , priority=6 , modal=false, share=true , order="lifo" }
70 application_class { name="camera"   , priority=5 , modal=false, share=false, order="lifo" }
71 application_class { name="event"    , priority=4 , modal=false, share=true , order="fifo" }
72 application_class { name="game"     , priority=3 , modal=false, share=false, order="lifo" }
73 --# doesn't need to be created here, ivi-resource-manager creates it if loaded
74 --#application_class { name="basic"    , priority=2 , modal=false, share=false, order="lifo" }
75 application_class { name="player"   , priority=1 , modal=false, share=true , order="lifo" }
76 application_class { name="implicit" , priority=0 , modal=false, share=false, order="lifo" }
77
78 -- define zone attributes
79 zone.attributes {
80     type = {mdb.string, "common", "rw"},
81     location = {mdb.string, "anywhere", "rw"}
82 }
83
84 -- define zones
85 zone {
86      name = "driver",
87      attributes = {
88          type = "common",
89          location = "front-left"
90      }
91 }
92
93 zone {
94      name = "passanger1",
95      attributes = {
96          type = "private",
97          location = "front-right"
98      }
99 }
100
101 zone {
102      name = "passanger2",
103      attributes = {
104          type = "private",
105          location = "back-left"
106      }
107 }
108
109 zone {
110      name = "passanger3",
111      attributes = {
112          type = "private",
113          location = "back-right"
114      }
115 }
116
117 zone {
118      name = "passanger4",
119      attributes = {
120          type = "private",
121          location = "back-left"
122      }
123 }
124
125
126 -- define resource classes
127 if not m:plugin_exists('ivi-resource-manager') then
128    resource.class {
129         name = "audio_playback",
130         shareable = true,
131         attributes = {
132             role = { mdb.string, "music", "rw" },
133             pid = { mdb.string, "<unknown>", "rw" },
134             policy = { mdb.string, "relaxed", "rw" }
135         }
136    }
137 end
138
139 resource.class {
140      name = "audio_recording",
141      shareable = true,
142      attributes = {
143          role   = { mdb.string, "music"    , "rw" },
144          pid    = { mdb.string, "<unknown>", "rw" },
145          policy = { mdb.string, "relaxed"  , "rw" }
146      }
147 }
148
149 resource.class {
150      name = "video_playback",
151      shareable = false,
152 }
153
154 resource.class {
155      name = "video_recording",
156      shareable = false
157 }
158
159 if not m:plugin_exists('ivi-resource-manager') then
160 resource.method.veto = {
161     function(zone, rset, grant, owners)
162         rset_priority = application_class[rset.application_class].priority
163
164         owner_id = owners.audio_playback.resource_set
165         rset_id = rset.id
166
167         if (rset_priority >= 50 and owner_id ~= rset_id) then
168             print("*** resource-set "..rset_id.." - veto")
169             return false
170         end
171
172         return true
173     end
174 }
175 end
176
177 -- test for creating selections
178 mdb.select {
179            name = "audio_owner",
180            table = "audio_playback_owner",
181            columns = {"application_class"},
182            condition = "zone_name = 'driver'"
183 }
184
185 mdb.select {
186            name = "vehicle_speed",
187            table = "amb_vehicle_speed",
188            columns = {"value"},
189            condition = "key = 'VehicleSpeed'"
190 }
191
192 element.lua {
193    name    = "speed2volume",
194    inputs  = { speed = mdb.select.vehicle_speed, param = 9 },
195    outputs = {  mdb.table { name = "speedvol",
196                             index = {"zone", "device"},
197                             columns = {{"zone", mdb.string, 16},
198                                        {"device", mdb.string, 16},
199                                        {"value", mdb.floating}},
200                             create = true
201                            }
202              },
203    oldvolume = 0.0,
204    update  = function(self)
205                 speed = self.inputs.speed.single_value
206                 if (speed) then
207                     volume = (speed - 144.0) / 7.0
208                 else
209                     volume = 0.0
210                 end
211                 diff = volume - self.oldvolume
212                 if (diff*diff > self.inputs.param) then
213                     print("*** element "..self.name.." update "..volume)
214                     self.oldvolume = volume
215                     mdb.table.speedvol:replace({zone = "driver", device = "speakers", value = volume})
216                 end
217              end
218 }
219
220 -- Night mode processing chain
221
222 mdb.select {
223     name = "exterior_brightness",
224     table = "amb_exterior_brightness",
225     columns = { "value" },
226     condition = "key = 'ExteriorBrightness'"
227 }
228
229 mdb.table {
230     name = "amb_nightmode",
231     index = { "id" },
232     create = true,
233     columns = {
234         { "id", mdb.unsigned },
235         { "night_mode", mdb.unsigned }
236     }
237 }
238
239 element.lua {
240     name    = "nightmode",
241     inputs  = { brightness = mdb.select.exterior_brightness },
242     oldmode = -1;
243     outputs = {
244     mdb.table {
245         name = "mandatory_placeholder_to_prevent_spurious_updates",
246             create = true,
247             columns = { { "id", mdb.unsigned } }
248         }
249     },
250     update = function(self)
251         -- This is a trivial function to calculate night mode. Later, we will
252         -- need a better threshold value and hysteresis to prevent oscillation.
253
254         brightness = self.inputs.brightness.single_value
255
256         if not brightness then
257             return
258         end
259
260         print("*** element "..self.name.." update brightness: "..brightness)
261
262         if brightness > 300 then
263             mode = 0
264         else
265             mode = 1
266         end
267
268         print("*** resulting mode: ".. mode)
269
270         if not (mode == self.oldmode) then
271             mdb.table.amb_nightmode:replace({ id = 0, night_mode = mode })
272         end
273
274         self.oldmode = mode
275     end
276 }
277
278 mdb.select {
279     name = "select_night_mode",
280     table = "amb_nightmode",
281     columns = { "night_mode" },
282     condition = "id = 0"
283 }
284
285 sink.lua {
286     name = "night_mode",
287     inputs = { owner = mdb.select.select_night_mode },
288     property = "NightMode",
289     type = "b",
290     initiate = builtin.method.amb_initiate,
291     update = builtin.method.amb_update
292 }
293
294
295 -- Driving mode processing chain
296
297 mdb.table {
298     name = "amb_drivingmode",
299     index = { "id" },
300     create = true,
301     columns = {
302         { "id", mdb.unsigned },
303         { "driving_mode", mdb.unsigned }
304     }
305 }
306
307 element.lua {
308     name    = "drivingmode",
309     inputs  = { speed = mdb.select.vehicle_speed },
310     oldmode = -1;
311     outputs = {
312     mdb.table {
313         name = "another_mandatory_placeholder_to_prevent_spurious_updates",
314             create = true,
315             columns = { { "id", mdb.unsigned } }
316         }
317     },
318     update = function(self)
319
320         speed = self.inputs.speed.single_value
321
322         if not speed then
323             return
324         end
325
326         if speed == 0 then
327             mode = 0
328         else
329             mode = 1
330         end
331
332         if not (mode == self.oldmode) then
333             mdb.table.amb_drivingmode:replace({ id = 0, driving_mode = mode })
334         end
335
336         self.oldmode = mode
337     end
338 }
339
340 mdb.select {
341     name = "select_driving_mode",
342     table = "amb_drivingmode",
343     columns = { "driving_mode" },
344     condition = "id = 0"
345 }
346
347 sink.lua {
348     name = "driving_mode",
349     inputs = { owner = mdb.select.select_driving_mode },
350     property = "DrivingMode",
351     type = "u",
352     initiate = builtin.method.amb_initiate,
353     update = builtin.method.amb_update
354 }
355
356 -- load the telephony plugin
357 m:try_load_plugin('telephony')