Lua Modules

@TedStriker, I suspect you’ll have problems being a [UDP] Server since it would require that you more or less “block”, or start messing with Lua coroutines that “hang around” to process the incoming data (if you can attach to the MCAST notification)

How comfortable are you with Threads and Socket programming?

If you’ve done this before in either C and/or Java it shouldn’t be too much of a jump given the API’s (although I’m not convinced you’ll be able to capture the MCAST stuff)

Well guys, I got my Vera since three weeks now, and I guess you know how it works when you got some new toys :slight_smile:
For the rest I agree with guessed. That’s the way it goes.

The Calendar plugin is also on my list. To not be depending on google alone, I would like to parse the iCal-file itself. So you can use any webserver where your calendar lies on instead of supporting calDAV or googles calendar api (which is pretty complex).
At least this would be the first step, and then we’ll see where it goes.

Back to the UDP-stuff. I made some experience with sockets a plenty years ago. I have a clue how threads work, but never tried it out.
The coroutines are the point I’m stuck. I don’t know how to start (and control) a thread which is listening to incoming data and hand it to the actual lua script interpreting it. Binding it to a MCAST-address should be not

Thinking about this problems, it seems I will start with the google plugin earlier than expected :wink:
If you want to, we can do it together. Just drop me a line.

[quote=“TedStriker, post:17, topic:164669”]Back to the UDP-stuff. I made some experience with sockets a plenty years ago. I have a clue how threads work, but never tried it out.
The coroutines are the point I’m stuck. I don’t know how to start (and control) a thread which is listening to incoming data and hand it to the actual lua script interpreting it. Binding it to a MCAST-address should be not[/quote]

I wish we could run Java on Vera… Java concurrency, especially the new stuff from Java 5 and later, is pure joy, and so mind boggling.
Shouldn’t we ask to make Java enabled Vera available to public? :wink:

I think Java requires a lot more memory/cpu than Vera can provide. Lua is running in about 500k of RAM.

Java runs even on mobile phones

325xi, Lua has most of what’s needed, as long as the Library problems are sorted out (separate thread). I wouldn’t put Java on this type of thing, it would be ugly-hard to manage, esp since you’d need the SE, not the ME editions.

I don’t even think there’s JVM for Broadcom chipset. It would be nice to have, but I agree that it’s not worth the effort.

Theoretically speaking though, I know that I could do much more and much faster with Java then with Lua - we can’t really compare well designed 3GL language with any of scripting tools, it’s not even apples and oranges.

a humble request to MCV gurus for a Lua module: luaproc ([url=http://luaforge.net/projects/luaproc]http://luaforge.net/projects/luaproc[/url]
LuaProc is a light-weight concurrency library for Lua using pthreads. Since it is inevitable (?) that we need it for performance reasons, (all network I/O with external data sources is inherently prone to latencies and jitter), I suggest we use it in Luup to speed things up. Source is just 50k of C code so I guess that the compiled library wouldn’t be that large.

Elias

I know this is an old post, but it seems an appropriate place to ask this question, but does anyone know if it”s possible to see which Lua modules you have available to call upon e.g lfs, aes etc.

I came across this Stackoverflow post - code, below but it does not seem to work for me…

function isModuleAvailable(name)
  if package.loaded[name] then
    return true
  else
    for _, searcher in ipairs(package.searchers or package.loaders) do
      local loader = searcher(name)
      if type(loader) == 'function' then
        package.preload[name] = loader
        return true
      end
    end
    return false
  end
end

That code will tell you what modules have been loaded, but not which are available to be loaded. Can you clarify which you are looking for?

Hi @rigpapa

I just get an error when I run that code…

Regarding my interest/need - I’ve come across some lua code to control my TV but due to the security/encryption needed it requires ‘aes’ and encdec’ modules. Link below.

https://forum.logicmachine.net/showthread.php?tid=232

You will not find those on Vera. You may be able to install them on openLuup, depending on what’s built for the distro you’re running on.

On Vera, the modules you can use are found in /usr/lib/lua, and the easiest way to figure out what is there is to go look (SSH in and run ls /usr/lib/lua)

Thanks

As I’ve managed to find the aes.lua module, can i just copy it into /usr/lib/lua?

It depends on what is needed to support it. If it’s a standalone module with everything written in Lua, you’re probably fine, although I would not put it into a system directory, you can just put it into /etc/cmh-ludl/ along with the rest of your plugin files (they won’t be erased on future firmware upgrades, but /usr/lib/lua definitely could be).

If it’s not a standalone, pure Lua module, that is, if it is supported by runtime utilities or libraries (compiled code in other languages), you need to make sure those dependencies are met. For compiled libraries, unless you have a build environment for the odd, ancient version of OpenWrt that Vera is using, you’re not going to be able to get that part of the code up and running and will need to find another solution.

I’ve found a few aes.lua versions e.g.

Looking at the code encdec seems to be a library, (see below)

https://openrb.com/docs/lua.htm#14

to reference things like

encdec.hmacsha256(ciphertext, hmac_key, true)
 encdec.base64enc(ciphertext .. sig)

I guess it’s just a case of trial and error.

For the hmacsha256 one…

encdec.hmacsha256(ciphertext, hmac_key, true)

I’ll give something like this a go, from LuaCrypto: A Lua frontend to OpenSSL

crypto.hmac.digest('sha256', user-access-token, app-secret, false)

Reference - HMAC - crypto.hmac

crypto.hmac.digest(dtype, string, key [, raw])

This function returns the HMAC of the string. The hashing algorithm to use is specified by dtype. The value provided in key will be used as the seed for the HMAC generation. The optional raw flag, defaulted to false, is a boolean indicating whether the output should be a direct binary equivalent of the HMAC or formatted as a hexadecimal string (the default).

crypto.hmac.new(dtype, key)

Creates a new HMAC object using the algorithm specified by type. The HMAC seed key to use is provided by key.

hmac:reset()

Resets the HMAC object to a clean slate.

hmac:clone()

Returns a new HMAC object which is a clone of the object and its current state, including data loaded to this point. DOES NOT WORK YET. Just returns a new pointer to the same object.

hmac:update(string)

Appends the data in string to the current internal data set to be hashed.

hmac:final([string] [, raw])

Generates the HMAC for the loaded data, optionally appending on new data provided by string prior to hashing. The optional raw flag, defaulted to false, is a boolean indicating whether the output should be a direct binary equivalent of the message digest or formatted as a hexadecimal string (the default). Note that you can only run this method once on an object; running it a second time will product a bogus HMAC because the internal state is irrecoverably destroyed after the first call.

For the base64,

This looks possible - lua-users wiki: Base Sixty Four

-- character table string
local b='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'

-- encoding
function enc(data)
    return ((data:gsub('.', function(x) 
        local r,b='',x:byte()
        for i=8,1,-1 do r=r..(b%2^i-b%2^(i-1)>0 and '1' or '0') end
        return r;
    end)..'0000'):gsub('%d%d%d?%d?%d?%d?', function(x)
        if (#x < 6) then return '' end
        local c=0
        for i=1,6 do c=c+(x:sub(i,i)=='1' and 2^(6-i) or 0) end
        return b:sub(c+1,c+1)
    end)..({ '', '==', '=' })[#data%3+1])
end

HI,

On the Vera you find the nixio library that has some of these functions CHANGELOG: Luadocs Index

Cheers Rene

The mime library that’s part of LuaSocket will take care of base64 for you.

An alternative to packages is using the command-line openssl utility. You’re already familiar with io.popen(), and you can use that or os.execute() and just have it write files you can read, either way.

Thanks @rigpapa, looking at LuaSocket: MIME module - is it a case of changing what they had below…

local encdec = require(‘encdec’)
to
local encdec = mime.decode(“base64”)

and then, I noticed that they have ‘aes’ prefixed with the word user ?

local aes = require(‘user.aes’)

What does the period/full stop symbolise to need to specifically call it out in the require part ?

To avoid clogging up this generic thread, I’ve spun off a focussed Panasonic encryption one here - Panasonic TV - Pincode and encryption support - #3 by parkerc - Developer's Corner - Ezlo Community