It works! You can call any function in a shard library without compiling!
I'll update the official Lua package soon. I attach the preliminary version for those who want to play with it (install on the Iliad with ipkg install).
Iņigo: thanks for pushing me into a new adventure
Code:
#!/usr/bin/env lua
require "alien"
require 'bit'
local bor, band, lshift = bit.bor, bit.band, bit.lshift
local function _IOR(x,y,t)
local IOCPARM_MASK = 0x7f
local IOC_READ = 0x80000000
local a = lshift(string.byte(x),8)
local b = lshift(band(t, IOCPARM_MASK), 16)
return bor(IOC_READ, a, b, y)
end
-- open the library (the functions we want are in the default libs)
local lib = alien.default
-- get the functions we need
local open, close, ioctl = lib.open, lib.close, lib.ioctl
-- specify the API
open:types {ret='int', 'string', 'int'}
close:types {ret='int', 'int'}
ioctl:types {ret='int', 'int', 'int', 'ref int'}
local ret, fd, val
local READ_CHARGE = _IOR('b', 1, 4)
local READ_TIME = _IOR('b', 2, 4)
fd = open("/dev/battery", 0)
ret, val = ioctl(fd, READ_TIME, 0)
if val == 0xffff then
ret, val = ioctl(fd, READ_CHARGE, 0)
print(string.format('Battery: %d%%, now charging', val))
else
print(string.format("Battery: %d:%2d hours remaining", math.floor(val/60), val%60))
end
close(fd)