Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/lib/support/lib/bobot_baseboard.lua
blob: 757731a95a319788eaf5a786878882198488cd4c (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#!/usr/bin/lua

--module(..., package.seeall);

local my_path = debug.getinfo(1, "S").source:match[[^@?(.*[\/])[^\/]-$]]

local bobot_device = require("bobot_device")
local bobot = require("bobot")

local NULL_BYTE = string.char(0x00)
local DEFAULT_PACKET_SIZE = 0x04
local GET_USER_MODULES_SIZE_COMMAND = string.char(0x05)
local GET_USER_MODULE_LINE_COMMAND = string.char(0x06)
local GET_HANDLER_SIZE_COMMAND = string.char(0x0A)
local GET_HANDLER_TYPE_COMMAND = string.char(0x0B)
local GET_LINES_RESPONSE_PACKET_SIZE = 5
local GET_LINE_RESPONSE_PACKET_SIZE = 12
local GET_HANDLER_TYPE_PACKET_SIZE = 5
local GET_HANDLER_RESPONSE_PACKET_SIZE = 5 --
local ADMIN_HANDLER_SEND_COMMAND = string.char(0x00)
local ADMIN_MODULE_IN_ENDPOINT = 0x01
local ADMIN_MODULE_OUT_ENDPOINT = 0x81
local GET_USER_MODULE_LINE_PACKET_SIZE = 0x05
local CLOSEALL_BASE_BOARD_COMMAND = string.char(0x07) 
local CLOSEALL_BASE_BOARD_RESPONSE_PACKET_SIZE = 5
local TIMEOUT = 250 --ms
local MAX_RETRY = 5

local BaseBoard = {}


--executes s on the console and returns the output
local function run_shell (s) 
	local f = io.popen(s) -- runs command
	local l = f:read("*a") -- read output of command
	f:close()
	return l
end

openable = {}
hotplug = {}

local function parse_drivers()
	local driver_files=run_shell("sh -c 'ls "..my_path.."../drivers/*.lua 2> /dev/null'")
	for filename in driver_files:gmatch('drivers%/(%S+)%.lua') do
		--print ("Driver openable", filename)
		openable[filename] = true
	end
	driver_files=run_shell("sh -c 'ls "..my_path.."../drivers/hotplug/*.lua 2> /dev/null'")
	for filename in driver_files:gmatch('drivers%/hotplug%/(%S+)%.lua') do
		--print ("Driver hotplug", filename)
		hotplug[filename] = true
	end
end
parse_drivers()

local function load_modules(bb)
	local retry = 0
	bb.modules = {}

	local n_modules=bb:get_user_modules_size()
	while n_modules == nil and retry < MAX_RETRY do
		n_modules=bb:get_user_modules_size()
		bobot.debugprint("u4b:the module list size returned a nil value, trying to recover...")
		retry = retry+1
	end
	if not n_modules then return nil end
	retry=0
	bobot.debugprint ("Reading modules:", n_modules)
	for i = 1, n_modules do
		local modulename=bb:get_user_module_line(i)
		while modulename == nil and retry < MAX_RETRY do
			bobot.debugprint("u4b:the module  returned a nil value, trying to recover...")
			modulename=bb:get_handler_type(i)
			retry = retry+1
		end
		if not modulename then return nil end
		if openable[modulename] then
			bb.modules[i]=modulename
			local d = bobot_device:new({
				module=modulename,
				--name=module, 
				baseboard=bb,
				hotplug=false,
				in_endpoint=0x01, out_endpoint=0x01,
			}) -- in_endpoint=0x01, out_endpoint=0x01})
			bb.devices[d]=true
			bb.devices[#bb.devices+1]=d
			
			bb.modules[modulename]=d
		elseif hotplug[modulename] then
			bb.modules[i]=modulename
			bb.modules[modulename]=true
			bb.hotplug = true -- bb has a hotplug module
		else
			bobot.debugprint("Loading modules: missing driver for",modulename)
		end
	end
	return true
end

local function load_module_handlers(bb)
	local retry = 0
	
	local n_module_handlers=bb:get_handler_size()
	while n_module_handlers == nil and retry < MAX_RETRY do
		n_module_handlers=bb:get_handler_size()
		bobot.debugprint("u4b:the module handler list size returned a nil value, trying to recover...")
		retry = retry+1
	end
	if (not n_module_handlers) or (n_module_handlers > 32) then return nil end
	retry=0
	bobot.debugprint ("Reading moduleshandlers:", n_module_handlers)
	for i=1, n_module_handlers do
		local t_handler = bb:get_handler_type(i)
		while(t_handler == nil and retry < MAX_RETRY) do
			bobot.debugprint("u4b:the module handler returned a nil value, trying to recover...")
			t_handler = bb:get_handler_type(i)
			retry = retry+1
		end
		if not t_handler then return nil end
		if t_handler<255  then
			local modulename=bb.modules[t_handler+1]
			local moduledev=bb.modules[modulename]
			if type(moduledev)=='table' 
			and not moduledev.handler then
				moduledev.handler=i-1
			elseif moduledev==true then 
				--name=name.."@"..(i-1)
				local d = bobot_device:new({
					handler=i-1,
					module=modulename,
					--name=name, 
					baseboard=bb,
					hotplug=(hotplug[modulename]),
					in_endpoint=0x01, out_endpoint=0x01,
				}) -- in_endpoint=0x01, out_endpoint=0x01})
				if d then 
					bb.devices[d]=true
					bb.devices[#bb.devices+1]=d
				end
			else
				bobot.debugprint ("No opened device!")
			end
		end
	end
	return true
end


function BaseBoard:refresh()
	self.devices = {}

	if not load_modules(self) then
		return nil,"failure reading modules"
	end

	if not load_module_handlers(self) then
		return nil,"failure reading module handlers"
	end
	return true
end

--Instantiates BaseBoard object.
--Loads list of modules installed on baseboard
function BaseBoard:new(bb)
	--parameters sanity check
	assert(type(bb)=="table")
	assert(type(bb.comms)=="table")

	--OO boilerplate
   	setmetatable(bb, self)
	self.__index = self
	
	bb:refresh()
	
--bobot.debugprint ('----------------')
	--bb:force_close_all()
--bobot.debugprint ('================')
	return bb
end

--Closes all modules opened on baseboard
function BaseBoard:close()
	--state sanity check
	assert(type(self.devices)=="table")

	for _,d in ipairs(self.devices) do
		if type(d.handler)=="number" then
			bobot.debugprint ("closing", d.name, d.handler)
			d:close()
		end
	end

	--TODO actually close the baseboard
end

--returns number of modules present on baseboard
function BaseBoard:get_user_modules_size()
	--state sanity check
	assert(type(self.comms)=="table")

	local comms=self.comms

	-- In case of get_user_modules_size command is atended by admin module in handler 0 and send operation is 000

	local handler_packet = ADMIN_HANDLER_SEND_COMMAND .. string.char(DEFAULT_PACKET_SIZE) .. NULL_BYTE
	local admin_packet = GET_USER_MODULES_SIZE_COMMAND
	local get_user_modules_size_packet  = handler_packet .. admin_packet    

	local write_res = comms.send(ADMIN_MODULE_IN_ENDPOINT, get_user_modules_size_packet, TIMEOUT)
	if write_res then
         	local data, err = comms.read(ADMIN_MODULE_OUT_ENDPOINT, GET_LINES_RESPONSE_PACKET_SIZE, TIMEOUT)
		if not data then
			bobot.debugprint("u4b:get_user_modules_size:comunication with I/O board read error", err)
			return 0
		else
			local user_modules_size = string.byte(data, 5)
			return user_modules_size
		end
	else	
		bobot.debugprint("u4b:get_user_modules_size:comunication with I/O board write error", write_res)
		return 0
   	end
end

--returns thename of a given (by a 1-based index)module 
function BaseBoard:get_user_module_line(index)
	--state & parameter sanity check
	assert(type(index)=="number")
	assert(index>0)	
	assert(type(self.comms)=="table")


	local comms=self.comms

	-- In case of get_user_module_line command is atended by admin module in handler 0 and send operation is 000
	local get_user_module_line_packet_length = string.char(GET_USER_MODULE_LINE_PACKET_SIZE)
	local handler_packet = ADMIN_HANDLER_SEND_COMMAND .. get_user_module_line_packet_length .. NULL_BYTE
	local admin_packet = GET_USER_MODULE_LINE_COMMAND .. string.char(index-1)
	local get_user_module_line_packet  = handler_packet .. admin_packet

	local write_res = comms.send(ADMIN_MODULE_IN_ENDPOINT, get_user_module_line_packet, TIMEOUT)
    	if write_res then
		local data, err = comms.read(ADMIN_MODULE_OUT_ENDPOINT, GET_LINE_RESPONSE_PACKET_SIZE, TIMEOUT)
		if not data then
			bobot.debugprint("u4b:get_user_modules_line:comunication with I/O board read error", err)
			return
		end
		--the name is between a header and a null
		local end_mark = string.find(data, "\000", GET_USER_MODULE_LINE_PACKET_SIZE, true)
		if not end_mark then
			bobot.debugprint ("u4b:get_user_module_line:Error parsing module name")
			return
		end
		local module_name = string.sub(data, GET_USER_MODULE_LINE_PACKET_SIZE, end_mark-1)
		return module_name
	else	
		bobot.debugprint("u4b:get_user_module_line:comunication with I/O board write error", write_res)
	end
end

function BaseBoard:get_handler_size() ------ NEW LISTI ------
	--state sanity check
	assert(type(self.comms)=="table")

	local comms=self.comms

	local handler_packet = ADMIN_HANDLER_SEND_COMMAND .. string.char(DEFAULT_PACKET_SIZE) .. NULL_BYTE
	local admin_packet = GET_HANDLER_SIZE_COMMAND
	local get_handler_size_packet  = handler_packet .. admin_packet    

	local write_res = comms.send(ADMIN_MODULE_IN_ENDPOINT, get_handler_size_packet, TIMEOUT)
	if write_res then
        local data, err = comms.read(ADMIN_MODULE_OUT_ENDPOINT, GET_HANDLER_RESPONSE_PACKET_SIZE, TIMEOUT)
		if not data then
			bobot.debugprint("u4b:get_handler_size:comunication with I/O board read error", err)
			return 0
		else
			local handler_size = string.byte(data, 5)	
			return handler_size
		end
	else	
		bobot.debugprint("u4b:get_handler_type:comunication with I/O board write error", write_res)
	end
end
function BaseBoard:get_handler_type(index) ------ NEW LISTI ------
	--state & parameter sanity check
	assert(type(index)=="number")
	assert(index>0)	
	assert(type(self.comms)=="table")

	local comms=self.comms

	-- In case of get_handler_type command is atended by admin module in handler 0 and send operation is 000
	local get_handler_type_packet_length = string.char(GET_HANDLER_TYPE_PACKET_SIZE) --GET_USER_MODULE_LINE_PACKET_SIZE
	local handler_packet = ADMIN_HANDLER_SEND_COMMAND .. get_handler_type_packet_length .. NULL_BYTE
	local admin_packet = GET_HANDLER_TYPE_COMMAND .. string.char(index-1)
	local get_handler_type_packet  = handler_packet .. admin_packet

	local write_res = comms.send(ADMIN_MODULE_IN_ENDPOINT, get_handler_type_packet, TIMEOUT)
    if write_res then
		local data, err = comms.read(ADMIN_MODULE_OUT_ENDPOINT, GET_HANDLER_RESPONSE_PACKET_SIZE, TIMEOUT)
		if not data then
			bobot.debugprint("u4b:get_handler_type:comunication with I/O board read error", err)
			return 0
		else
			local handler_type = string.byte(data, 5)	
			return handler_type
		end
	else	
		bobot.debugprint("u4b:get_handler_type:comunication with I/O board write error", write_res)
	end
end

-- resets the baseboard, after this operation the baseboard will claim reenumeration to the operative system
-- this function is deprecated by force_close_all
function BaseBoard:close_all()
	for _,d in ipairs(self.devices) do
		--bobot.debugprint ("===", d.name,d.handler)
		if d.handler then d:close() end
	end
end

-- switch the baseboard to the bootloader program implementend as a usb4all command to the admin module
function BaseBoard:switch_to_bootloader()
	--state & parameter sanity check
	assert(type(self.comms)=="table")
	
	local comms=self.comms
	-- In case of reset_base_board command is atended by admin module in handler 0 and send operation is 000
	local handler_packet = ADMIN_HANDLER_SEND_COMMAND .. string.char(DEFAULT_PACKET_SIZE) .. NULL_BYTE
	local admin_packet = string.char(0x09)  --SWITCH_TO_BOOT_BASE_BOARD_COMMAND
	local boot_base_board_packet  = handler_packet .. admin_packet

	local write_res = comms.send(ADMIN_MODULE_IN_ENDPOINT, boot_base_board_packet, TIMEOUT)
    --from this moment the board is reseted, so there is nothing more to do
end

function BaseBoard:reset()
	--state & parameter sanity check
	assert(type(self.comms)=="table")
	
	local comms=self.comms
	-- In case of reset_base_board command is atended by admin module in handler 0 and send operation is 000
	local handler_packet = ADMIN_HANDLER_SEND_COMMAND .. string.char(DEFAULT_PACKET_SIZE) .. NULL_BYTE
	local admin_packet = string.char(0xFF)  --RESET_BASE_BOARD_COMMAND
	local reset_base_board_packet  = handler_packet .. admin_packet

	local write_res = comms.send(ADMIN_MODULE_IN_ENDPOINT, reset_base_board_packet, TIMEOUT)
    	if write_res then
		-- no tego que leer respuesta porque se reseteo
		--libusb.close(libusb_handler)
		--self.libusb_handler=nil
		--for d_name,d in pairs(self.devices) do
			--bobot.debugprint ("===", d.name,d.handler)
		--	d.handler=nil
		--end
	else	
		bobot.debugprint("u4b:reset:comunication with I/O board write error", write_res)
	end
end

function BaseBoard:force_close_all()
	--state & parameter sanity check
	assert(type(self.comms)=="table")
	
	local comms=self.comms
	-- In case of reset_base_board command is atended by admin module in handler 0 and send operation is 000
	local handler_packet = ADMIN_HANDLER_SEND_COMMAND .. string.char(DEFAULT_PACKET_SIZE) .. NULL_BYTE
	local admin_packet = CLOSEALL_BASE_BOARD_COMMAND
	local reset_base_board_packet  = handler_packet .. admin_packet

--bobot.debugprint ('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
	local write_res = comms.send(ADMIN_MODULE_IN_ENDPOINT, reset_base_board_packet, TIMEOUT)
--bobot.debugprint ('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
    	if write_res then
		local data, err = comms.read(ADMIN_MODULE_OUT_ENDPOINT,	CLOSEALL_BASE_BOARD_RESPONSE_PACKET_SIZE, TIMEOUT)
		if err then
			bobot.debugprint("u4b:force_close_all:comunication with I/O board read error",err)
		else
			--bobot.debugprint("u4b:force_close_all:libusb read",string.byte(data,1,string.len(data)))
		end
		for _,d in ipairs(self.devices) do
			--bobot.debugprint ("===", d.name,d.handler)
			d.handler=nil
		end
	else	
		bobot.debugprint("u4b:force_close_all:comunication with I/O board write error", write_res)
	end
end

return BaseBoard