summaryrefslogtreecommitdiffstats
path: root/conf.d/project/lua.d
diff options
context:
space:
mode:
authorFulup Ar Foll <fulup@iot.bzh>2017-08-23 01:00:20 +0200
committerRomain Forlot <romain.forlot@iot.bzh>2018-12-13 15:01:57 +0100
commit4921883434749752ac82a0cbab8e6e57120800c5 (patch)
treec60d2919e1c6941e95931231bf5f75720f9c959d /conf.d/project/lua.d
parent1abf8567328fc22b66f56782ea2061dae5a73d65 (diff)
Restore Basic HTML5 testing Scenario as a Standalone Controller
Diffstat (limited to 'conf.d/project/lua.d')
-rw-r--r--conf.d/project/lua.d/onload-aaaa-01-controls.lua162
-rw-r--r--conf.d/project/lua.d/onload-daemon-00-utils.lua (renamed from conf.d/project/lua.d/onload-aaaa-00-utils.lua)0
-rw-r--r--conf.d/project/lua.d/onload-daemon-01-init.lua48
-rw-r--r--conf.d/project/lua.d/onload-daemon-03-controls.lua47
-rw-r--r--conf.d/project/lua.d/onload-daemon-04-oncall.lua (renamed from conf.d/project/lua.d/onload-aaaa-03-oncall.lua)0
-rw-r--r--conf.d/project/lua.d/onload-daemon-10-event.lua (renamed from conf.d/project/lua.d/onload-aaaa-02-timer.lua)35
-rw-r--r--conf.d/project/lua.d/standalone-sample/onload-daemon-00.lua86
-rw-r--r--conf.d/project/lua.d/standalone-sample/onload-daemon-01.lua60
8 files changed, 115 insertions, 323 deletions
diff --git a/conf.d/project/lua.d/onload-aaaa-01-controls.lua b/conf.d/project/lua.d/onload-aaaa-01-controls.lua
deleted file mode 100644
index 6ffc8d3..0000000
--- a/conf.d/project/lua.d/onload-aaaa-01-controls.lua
+++ /dev/null
@@ -1,162 +0,0 @@
---[[
- Copyright (C) 2016 "IoT.bzh"
- Author Fulup Ar Foll <fulup@iot.bzh>
-
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
-
-
- Provide sample policy function for AGL Advance Audio Agent
---]]
-
--- Global HAL registry
-_Audio_Hal_Registry={}
-
--- Callback when receiving HAL registry
-function _Alsa_Get_Hal_CB (error, result, context)
- -- Initialise an empty table
- local registry={}
-
- -- Only process when response is valid
- if (error) then
- AFB_ErrOr ("[Audio_Init_CB] ErrOr result=%s", result)
- return
- end
-
- -- Extract response from result
- local response=result["response"]
-
- -- Index HAL Bindings APIs by shortname
- for key,value in pairs(response) do
- registry[value["shortname"]]=value["api"]
- end
-
- -- store Exiting HAL for further use
- printf ("-- [Audio_Init_CB] -- Audio_register_Hal=%s", Dump_Table(registry))
- _Audio_Hal_Registry=registry
-
-end
-
--- Function call at binding load time
-function _Alsa_Get_Hal(args)
-
- printf ("[-- Audio_Get_Hal --] args=%s", Dump_Table(argsT))
-
- -- Query AlsaCore for Active HALs (no query, no context)
- AFB:service ('alsacore', 'hallist', {}, "_Alsa_Get_Hal_CB", {})
-
-end
-
--- In sample configuration Query/Args parsing is common to all audio control
-local function Audio_Parse_Request (source, args, query)
-
- local apihal={}
-
- -- In this test we expect targeted device to be given from query (could come for args as well)
- if (query == nil ) then
- AFB:error ("--LUA:Audio_Set_Navigation query should contain and args with targeted apihal|device")
- return -- refuse control
- end
-
- -- Alsa Hook plugin asound sample config provides target sound card by name
- if (query["device"] ~= nil) then
- apihal=_Audio_Hal_Registry[query["device"]]
- end
-
- -- HTML5 test page provides directly HAL api.
- if (query["apihal"] ~= nil) then
- apihal= query["apihal"]
- end
-
- -- if requested HAL is not found then deny the control
- if (apihal == nil) then
- AFB:error ("--LUA:Audio_Set_Navigation No Active HAL Found")
- return -- refuse control
- end
-
- -- return api or nil when not found
- return apihal
-end
-
--- Set Navigation lower sound when play
-function _Audio_Set_Navigation(source, args, query)
-
- -- in strict mode every variables should be declared
- local err=0
- local ctlhal={}
- local response={}
- local apihal={}
-
- AFB:notice ("LUA:Audio_Set_Use_Case source=%d args=%s query=%s", source, args, query);
-
- -- Parse Query/Args and if HAL not found then refuse access
- apihal= Audio_Parse_Request (source, args, query)
- if (apihal == nil) then return 1 end
-
-
- -- if source < 0 then Alsa HookPlugin is closing PCM
- if (source < 0) then
- -- Ramp Up Multimedia channel synchronously
- ctlhal={['label']='Master_Playback_Volume', ['val']=100}
- err, response= AFB:servsync (apihal, 'ctlset',ctlhal)
- else
- -- Ramp Down Multimedia channel synchronously
- ctlhal={['label']='Master_Playback_Volume', ['val']=50}
- err, response= AFB:servsync (apihal, 'ctlset',ctlhal)
- end
-
- if (err) then
- AFB:error("--LUA:Audio_Set_Navigation halapi=%s refuse ctl=%s", apihal, ctlhal)
- return 1 -- control refused
- end
-
-
- return 0 -- control accepted
-end
-
-
--- Select Multimedia mode
-function _Audio_Set_Multimedia (source, args, query)
-
- -- in strict mode every variables should be declared
- local err=0
- local ctlhal={}
- local response={}
- local apihal={}
-
- AFB:notice ("LUA:Audio_Set_Use_Case source=%d args=%s query=%s", source, args, query);
-
- -- Parse Query/Args and if HAL not found then refuse access
- apihal= Audio_Parse_Request (source, args, query)
- if (apihal == nil) then return 1 end
-
-
- -- if Mumtimedia control only increase volume on open
- if (source >= 0) then
- -- Ramp Down Multimedia channel synchronously
- ctlhal={['label']='Master_Playback_Volume', ['val']=100}
- err, response= AFB:servsync (apihal, 'ctlset',ctlhal)
- end
-
- if (err) then
- AFB:error("--LUA:Audio_Set_Navigation halapi=%s refuse ctl=%s", apihal, ctlhal)
- return 1 -- control refused
- end
-
-
- return 0 -- control accepted
-end
-
--- Select Emergency Mode
-function _Audio_Set_Emergency(source, args, query)
- return 1 -- Always refuse in this test
-end
diff --git a/conf.d/project/lua.d/onload-aaaa-00-utils.lua b/conf.d/project/lua.d/onload-daemon-00-utils.lua
index 29d2c70..29d2c70 100644
--- a/conf.d/project/lua.d/onload-aaaa-00-utils.lua
+++ b/conf.d/project/lua.d/onload-daemon-00-utils.lua
diff --git a/conf.d/project/lua.d/onload-daemon-01-init.lua b/conf.d/project/lua.d/onload-daemon-01-init.lua
new file mode 100644
index 0000000..ebdc678
--- /dev/null
+++ b/conf.d/project/lua.d/onload-daemon-01-init.lua
@@ -0,0 +1,48 @@
+--[[
+ Copyright (C) 2016 "IoT.bzh"
+ Author Fulup Ar Foll <fulup@iot.bzh>
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+--]]
+
+-- Global variable SHOULD start with _
+_Global_Context={}
+
+--[[
+ This function is call during controller init phase as describe in onload-daemon-sample.json
+ It receives two argument 1st one is the source (here on load) second one is the arguments
+ as expose in config file.
+
+ In this sample we create an event that take the name of args["zzzz"], the resulting handle
+ is save into _Global_Context for further use.
+
+ Note: init functions are not call from a client and thus do not receive query
+
+--]]
+function _Sample_Controller_Init(source, control)
+
+ printf ("[-- Sample_Controller_Init --] source=%d control=%s", source, Dump_Table(control))
+
+ -- if no argument return now
+ if (control==nil or control["zzzz"]==nil) then
+ printf ("[-- Sample_Controller_Init --] no event name given")
+ return
+ end
+
+ -- set a count to make more visible each call
+ _Global_Context["counter"]=0
+
+ -- just for fun create an event
+ _Global_Context["event"]=AFB:evtmake(control["zzzz"])
+
+end
diff --git a/conf.d/project/lua.d/onload-daemon-03-controls.lua b/conf.d/project/lua.d/onload-daemon-03-controls.lua
new file mode 100644
index 0000000..f65c019
--- /dev/null
+++ b/conf.d/project/lua.d/onload-daemon-03-controls.lua
@@ -0,0 +1,47 @@
+--[[
+ Copyright (C) 2016 "IoT.bzh"
+ Author Fulup Ar Foll <fulup@iot.bzh>
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
+ Following function are called when a client activate a control with
+ controller api -> APi=control VERB=dispatch
+ arguments are
+ - source (0) when requesting the control (-1) when releasing
+ - control comme from config given with 'args' in onload-middlename-xxxxx.json
+ - client is the argument part of the query as providing by client requesting the control.
+
+--]]
+
+
+
+-- Simple Happy(granted) Control
+function _Button_Happy(source, control, client)
+
+ -- print argument to make sure we understant what we get
+ printf ("[-- _Button_Happy --] source=%d control=%s client=%s", source, Dump_Table(client), Dump_Table(control))
+
+ AFB:notice ("[-- _Button_Happy --] To Be Done")
+ return 0 -- control granted
+end
+
+
+-- Simple UnHappy(debu) Control
+function _Button_UnHappy(source, control, client)
+
+ -- print argument to make sure we understant what we get
+ printf ("[-- _Button_UnHappy --] source=%d control=%s client=%s", source, Dump_Table(client), Dump_Table(control))
+
+ AFB:error ("[-- _Button_UnHappy --] To Be Done")
+ return 1 -- control is refused
+end
diff --git a/conf.d/project/lua.d/onload-aaaa-03-oncall.lua b/conf.d/project/lua.d/onload-daemon-04-oncall.lua
index 4e78fd8..4e78fd8 100644
--- a/conf.d/project/lua.d/onload-aaaa-03-oncall.lua
+++ b/conf.d/project/lua.d/onload-daemon-04-oncall.lua
diff --git a/conf.d/project/lua.d/onload-aaaa-02-timer.lua b/conf.d/project/lua.d/onload-daemon-10-event.lua
index f984f3f..c0b21f9 100644
--- a/conf.d/project/lua.d/onload-aaaa-02-timer.lua
+++ b/conf.d/project/lua.d/onload-daemon-10-event.lua
@@ -19,16 +19,16 @@
--]]
-- Create event on Lua script load
-local MyEventHandle=AFB:evtmake("MyTestEvent")
+_MyContext={}
--- Call count time every delay/ms
-local function Timer_Test_CB (timer, context)
+-- WARNING: call back are global and should start with '_'
+function _Timer_Test_CB (timer, context)
local evtinfo= AFB:timerget(timer)
- print ("timer=", Dump_Table(evtinfo))
+ printf ("[-- _Timer_Test_C --] evtInfo=%s", Dump_Table(evtinfo))
--send an event an event with count as value
- AFB:evtpush (MyEventHandle, {["label"]= evtinfo["label"], ["count"]=evtinfo["count"], ["info"]=context["info"]})
+ AFB:evtpush (_MyContext["event"], {["label"]= evtinfo["label"], ["count"]=evtinfo["count"], ["info"]=context["info"]})
-- note when timerCB return!=0 timer is kill
return 0
@@ -36,33 +36,38 @@ local function Timer_Test_CB (timer, context)
end
-- sendback event depending on count and delay
-function _Simple_Timer_Test (request, args)
+function _Simple_Timer_Test (request, client)
local context = {
["info"]="My 1st private Event",
}
+ -- if event does not exit create it now.
+ if (_MyContext["event"] == nil) then
+ _MyContext["event"]= AFB:evtmake(client["label"])
+ end
+
-- if delay not defined default is 5s
- if (args["delay"]==nil) then args["delay"]=5000 end
+ if (client["delay"]==nil) then client["delay"]=5000 end
-- if count is not defined default is 10
- if (args["count"]==nil) then args["count"]=10 end
+ if (client["count"]==nil) then client["count"]=10 end
- -- we could use directly args but it is a sample
+ -- we could use directly client but it is a sample
local myTimer = {
- ["label"]=args["label"],
- ["delay"]=args["delay"],
- ["count"]=args["count"],
+ ["label"]=client["label"],
+ ["delay"]=client["delay"],
+ ["count"]=client["count"],
}
AFB:notice ("Test_Timer myTimer=%s", myTimer)
-- subscribe to event
- AFB:subscribe (request, MyEventHandle)
+ AFB:subscribe (request, _MyContext["event"])
-- settimer take a table with delay+count as input (count==0 means infinite)
- AFB:timerset (myTimer, "Timer_Test_CB", context)
+ AFB:timerset (myTimer, "_Timer_Test_CB", context)
- -- nothing special to return send back args
+ -- nothing special to return send back
AFB:success (request, myTimer)
return 0
diff --git a/conf.d/project/lua.d/standalone-sample/onload-daemon-00.lua b/conf.d/project/lua.d/standalone-sample/onload-daemon-00.lua
deleted file mode 100644
index 29d2c70..0000000
--- a/conf.d/project/lua.d/standalone-sample/onload-daemon-00.lua
+++ /dev/null
@@ -1,86 +0,0 @@
---[[
- Copyright (C) 2016 "IoT.bzh"
- Author Fulup Ar Foll <fulup@iot.bzh>
-
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
-
- Note: this file should be called before any other to assert declare function
- is loaded before anything else.
-
- References:
- http://lua-users.org/wiki/DetectingUndefinedVariables
-
---]]
-
-
---===================================================
---= Niklas Frykholm
--- basically if user tries to create global variable
--- the system will not let them!!
--- call GLOBAL_lock(_G)
---
---===================================================
-function GLOBAL_lock(t)
- local mt = getmetatable(t) or {}
- mt.__newindex = lock_new_index
- setmetatable(t, mt)
-end
-
---===================================================
--- call GLOBAL_unlock(_G)
--- to change things back to normal.
---===================================================
-function GLOBAL_unlock(t)
- local mt = getmetatable(t) or {}
- mt.__newindex = unlock_new_index
- setmetatable(t, mt)
-end
-
-function lock_new_index(t, k, v)
- if (string.sub(k,1,1) ~= "_") then
- GLOBAL_unlock(_G)
- error("GLOBALS are locked -- " .. k ..
- " must be declared local or prefix with '_' for globals.", 2)
- else
- rawset(t, k, v)
- end
-end
-
-function unlock_new_index(t, k, v)
- rawset(t, k, v)
-end
-
--- return serialised version of printable table
-function Dump_Table(o)
- if type(o) == 'table' then
- local s = '{ '
- for k,v in pairs(o) do
- if type(k) ~= 'number' then k = '"'..k..'"' end
- s = s .. '['..k..'] = ' .. Dump_Table(v) .. ','
- end
- return s .. '} '
- else
- return tostring(o)
- end
-end
-
-
--- simulate C prinf function
-printf = function(s,...)
- io.write(s:format(...))
- io.write("\n")
- return
-end
-
--- lock global variable
-GLOBAL_lock(_G)
diff --git a/conf.d/project/lua.d/standalone-sample/onload-daemon-01.lua b/conf.d/project/lua.d/standalone-sample/onload-daemon-01.lua
deleted file mode 100644
index b423914..0000000
--- a/conf.d/project/lua.d/standalone-sample/onload-daemon-01.lua
+++ /dev/null
@@ -1,60 +0,0 @@
---[[
- Copyright (C) 2016 "IoT.bzh"
- Author Fulup Ar Foll <fulup@iot.bzh>
-
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
-
- Note: this file should be called before any other to assert declare function
- is loaded before anything else.
-
- References:
- http://lua-users.org/wiki/DetectingUndefinedVariables
-
---]]
-
--- Set Navigation lower sound when play
-function _Control_Init(source, args)
-
- -- in strict mode every variables should be declared
- local err=0
-
- AFB:notice ("Control_Init args=%s", args);
- AFB:success(source, args)
-
- return 0 -- control accepted
-end
-
--- Set Navigation lower sound when play
-function _Button_Press(source, args, query)
-
- -- in strict mode every variables should be declared
- local err=0
-
- AFB:notice ("Button_Press button=%s", args["button"]);
- AFB:success(source, args)
-
- return 0 -- control accepted
-end
-
--- Set Navigation lower sound when play
-function _Event_Received(source, args, query)
-
- -- in strict mode every variables should be declared
- local err=0
-
- AFB:notice ("Event_Received event=%s", args["evtname"]);
- AFB:success(source, args)
-
- return 0 -- control accepted
-end
-