Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/lib/support/bobot_server/bobot-server-process.lua
blob: 18231cbcdee5c83b24b8f44de928ae1f779bed4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/lua

module(..., package.seeall);

local bobot = require("bobot")

--local devices=devices
--local DEBUG = false

local function check_open_device(d, ep1, ep2)
	if not d then return end
	if d.handler or d.name=='pnp' then return true end

	-- if the device is not open, then open the device
	bobot.debugprint ("Opening", d.name, d.handler)
	return d:open(ep1 or 1, ep2 or 1) --TODO asignacion de ep?
end

process = {}

process["INIT"] = function () --to check the new state of hardware on the fly
	server_init()
end
process["REFRESH"] = function () --to check the new state of hardware on the fly
	--for _, bb in ipairs(bobot.baseboards) do
	--	bb:refresh()
	--end
	server_refresh()
	return 'ok'
end


process["LIST"] = function ()
	local ret,comma = "", ""
	for _, d in ipairs(devices) do
		ret = ret .. comma .. d.name
		comma=","
	end
	return ret
end


process["LISTI"] = function ()
    if bobot.baseboards then
        bobot.debugprint("listing instanced modules...")
        for _, bb in ipairs(bobot.baseboards) do
    	    local handler_size=bb:get_handler_size()
            for i=1, handler_size do
                t_handler = bb:get_handler_type(i)
                bobot.debugprint("handler=", i-1 ," type=" ,t_handler)
            end
        end
    end
end


process["OPEN"] = function (parameters)
	local d  = parameters[2]
	local ep1= tonumber(parameters[3])
	local ep2= tonumber(parameters[4])

	if not d then
		bobot.debugprint("ls:Missing 'device' parameter")
		return
	end
	
	local device = devices[d]
	if check_open_device(device, ep1, ep2) then	
		return "ok"
	else
		return "fail"
	end
end
process["DESCRIBE"] = function (parameters)
	local d  = parameters[2]
	local ep1= tonumber(parameters[3])
	local ep2= tonumber(parameters[4])

	if not d then
		bobot.debugprint("ls:Missing \"device\" parameter")
		return
	end
	
	local device = devices[d]
	if not check_open_device(device, ep1, ep2) then	
		return "fail"
	end
	if not device.api then
		return "missing driver"
	end

	local ret = "{"
	for fname, fdef in pairs(device.api) do
		ret = ret .. fname .. "={"
		ret = ret .. " parameters={"
		for i,pars in ipairs(fdef.parameters) do
			ret = ret .. "[" ..i.."]={"
			for k, v in pairs(pars) do
				ret = ret .."['".. k .."']='"..tostring(v).."',"
			end
			ret = ret .. "},"
		end
		ret = ret .. "}, returns={"
		for i,rets in ipairs(fdef.returns) do
			ret = ret .. "[" ..i.."]={"
			for k, v in pairs(rets) do
				ret = ret .."['".. k .."']='"..tostring(v).."',"
			end
			ret = ret .. "},"
		end
		ret = ret .. "}}," 
	end
	ret=ret.."}"

	return ret
end
process["CALL"] = function (parameters)
	local d  = parameters[2]
	local call  = parameters[3]

	if not (d and call) then
		bobot.debugprint("ls:Missing parameters", d, call)
		return
	end

	local device = devices[d]
	if not check_open_device(device, nil, nil) then	
		return "fail"
	end

	if not device.api then return "missing driver" end
	local api_call=device.api[call];
	if not api_call then return "missing call" end
	
	if api_call.call then
		--local tini=socket.gettime()
		local ok, ret = pcall (api_call.call, unpack(parameters,4))
		if not ok then bobot.debugprint ("Error calling", ret) end
		--print ('%%%%%%%%%%%%%%%% bobot-server',socket.gettime()-tini)
		return ret
	end
end
process["CLOSEALL"] = function ()
	if bobot.baseboards then
		for _, bb in ipairs(bobot.baseboards) do
            --this command closes all the open user modules
            --it does not have sense with plug and play
			bb:force_close_all() --modif andrew
		end
	end
	return "ok"
end
process["BOOTLOADER"] = function ()
	if bobot.baseboards then
		for _, bb in ipairs(bobot.baseboards) do
			bb:switch_to_bootloader()
		end
	end
	return "ok"
end
process["DEBUG"] = function (parameters) --disable debug mode Andrew code!
	local debug = parameters[2]
	if not debug then return "missing parameter" end
	if debug=="ON" then
		bobot.debugprint = print --function(...) print (...) end  --enable printing
	elseif debug=="OFF" then
		bobot.debugprint = function() end  --do not print anything
	end
	return "ok"
end
process["QUIT"] = function () 
	bobot.debugprint("Requested EXIT...")
	os.exit()
	return "ok"
end