Using OatLua4 under DS

I started with new VM, Win 7, 64bit.

First install Java JRE, I’ve got Version 7 Update 9. Then download Developer Studio (DS), here I’ve got Embedded_Application_Framework_v2-36_Full_Installer.exe. For my product family I selected AirLink FXT Series 3GPP. In software Selection, tick ARM ELF GCC Toolchain and keep EABI toolchain ticked.

After successful instalation apply bug fix described by [url]https://forum.sierrawireless.com/t/m2m-studio-2-0-0-lua-plugin/4977/3] by blicharski. You have to apply this fix after each upgrade of DS.

Test run DS and accept \workspace directory for your working directory. In Package Manager instal Firmware Package 7.46, LUA and WIP plugins from OAT ESS package 2.36.

Install http://tortoisesvn.net/downloads.html TortoiseSVN, I’ve got Version 1.7.10.

Create new directory (let name it RepoTmp) right click and SVN Checkout from https://svn.anyware-tech.com/wavecom/openat-lua.

We will create three libraries in workspace: oatlua, Ethernet and Shell. Steps for all three are almost identical.

In DS / Main Menu / File / Import select Developer Studio / Existing DS Project. Press Next. Now select RepoTmp\trunk\oatlua as root directory and tick Copy projects into workspace. Finish. Ignore any DS errors for now.

Right click on project in Project Explorer / Build Configuration / Manage / New / Import Predefined / Static Library [Target]_ARM_EABI_GCC_Debug and name it so. OK and Set Active. OK again.

Right click on project / Properties / C/C++ general / Paths and Symbols / Symbols tab add LUA_PLUGIN_VERSION with value 400
Also in Project properties / OpenAT Library select correct Framework Profile (ESS package 2.36 for now). In Libraries tab check Internet Library Package. OK. Yes.

In oatlua library we fix two bugs:

In oatlua\src-porting\luaw_loaders.c:148 we changeluaW_preload() function:

int luaW_preload( lua_State *L, const char *name, lua_CFunction opener) {
// LJWEKO
// remove double quotes
	u8 s, l;
	char name1[65];

	l = strlen(name);
	if (name[0] == 34) {
		s = 1;
		l = l - 2;
	}
	else s = 0;
	strncpy(name1, &name[s], l); name1[l] = 0;
// you can do better, can't you
// LJWEKO
	luaW_debug( "LUA", "DEBUG", "Preloading package %s", name1);
	luaL_findtable(    L, LUA_GLOBALSINDEX, "package.preload", 1);
	lua_pushcfunction( L, opener);
	lua_setfield(      L, -2, name1);
	lua_pop(           L, 1);
	return 0;
}

In oatlua\src-orig\lundump.c:120 we change LoadNumber() function:

static lua_Number LoadNumber(LoadState* S) {
	union { lua_Number x; int b[2]; } u;
	int t;
	LoadVar(S,u.x);
	t=u.b[0]; u.b[0]=u.b[1]; u.b[1]=t;
	return u.x;
}

Clean and Build project oatlua. Library oatlua.lib is created in [Target]_ARM_EABI_GCC_Debug folder.

Now import Ethernet library from RepoTmp\trunk\samples\Ethernet. Repeat steps for oatlib (no bug fix here). Library depends on oatlua, so in Project Properties / C/C++ general / Paths and Symbols / Includes add /oatlua/itf path. You may unchek Project References oatlua. Clean and build project Ethernet.

Now import Shell library from RepoTmp\trunk\samples\Shell. Repeat steps for Ethernet library. Clean and build project Shell.

The file Shell.h in itf subdir should look like:

#ifndef __SHELL_H_INCLUDED__
#define __SHELL_H_INCLUDED__

/* Export lua preloading function */
void luapreload_Shell(); // should not be removed

#endif /* __SHELL_H_INCLUDED__ */

You will need Fastrack modem with IESM Ethernet card installed.

File / New OpenAtLua Project. Name it HelloWorld.

Again we manage configuration, this time we select Executable EABI instead of Static Library, and do similar steps.

In Project Properties / Open AT Application / Package selection we select OAT ESS 2.36 as Framework Profile and in libraries tab do deselect LUA library Package.

Use following main.c and main.lua
Main.c:

#include "adl_global.h"
#include "wip.h"
#include "oatlua.h"
#include "Ethernet.h"
#include "Shell.h"
#include "HelloWorld.h"

/* Comment/Remove this define out to turn off verbose logs. */
#define VERBOSE

#define K 1024

const u16 wm_apmCustomStackSize       = 32*K;
const u32 wm_apmIRQLowLevelStackSize  =  2*K;
const u32 wm_apmIRQHighLevelStackSize =  8*K;

#ifdef LUAW_USE_POOLS
static unsigned pools[] = {
	/* block size, number of blocks */
	0 };
#endif

static const luaW_initSetup setup =
	{ .gc_limit        = 700*K
	, .dlmalloc_total  = 800*K
	, .dlmalloc_buffer = NULL
#	ifdef LUAW_USE_POOLS
	, .pool_total      = 0
	, .pool_buffer     = NULL
	, .pool_config     = pools
#	endif
	};

void DelayedMain(u8 id, void *Ctx) {

	TRACE((1, "Delayed"));
	wip_debug( "[APP] start the VM\r\n");
	luaW_start( & setup);

#	ifndef VERBOSE
	luaW_setLogLevel( "WARNING");
#	endif

	wip_debug( "[APP] preload the application & libraries\r\n");
	luapreload_HelloWorld(L);

	wip_debug( "[APP] run\r\n");
	luaW_run( "require 'HelloWorld.main'");
}


void adl_main( adl_InitType_e InitType) {
	s8 r;

/* initialize the internet & TCP/IP plug-In */
	r = wip_netInitOpts(
#		ifdef VERBOSE
		WIP_NET_OPT_DEBUG_PORT, WIP_NET_DEBUG_PORT_USB, // low level traces
#		endif
		WIP_NET_OPT_END);
	adl_tmrSubscribe(FALSE, 100,  ADL_TMR_TYPE_100MS, DelayedMain);
}

void _exit(int i) {
}

char _fini(char i) {
	return(0);
}

char _fstat(char i) {
	return(0);
}

char _getpid(char i) {
	return(0);
}

char _isatty(char i) {
	return(0);
}

char _kill(char i) {
	return(0);
}

Main.lua:

require 'strict'       -- Cause an error on undefined global variables
require 'flashvars'    -- save and reload Lua variables in flash objects
require 'at'           -- Send AT commands to the modem
require 'at.atpluslua' -- Execute Lua code through "AT+LUA=" AT commands
require 'ethernet'     -- Ethernet driver
require 'shell'        -- Shell over Telnet
require 'shell.telnet'
require 'varispeed'

function main()
	varispeed.boost()
	at.unsollicited '\r\n+OATLUA: "Open AT Hello World"\r\n'

	run(bearer.new, 'ETH', {ip_dhcp=true, device="fastrack"})
	shell.telnet.init {
		port        = 23,
		editmode    = 'edit',
		historysize = 20,
	}
end

main()

In Project Properties / C/C++ general / Includes add oatlua/itf, Ethernet/itf and Shell/itf paths.
In Symbols tab add LUA_PLUGIN_VERSION with value 400. OK.
In Project/src delete lua_init.c file.
In Project Properties / C/C++ Build / Settings / ARM EABI GCC Linker / Misc / Add other objects add all three built libraries in workspace: oatlua.lib, Ethernet.lib, Shell.lib

If linker complains about: no memory region specified for loadable section .init_array.00000, locate autogenerated file gcc.lkopt in [Target]_ARM_EABI_GCC_Debug subfolder and add trailing zeros to init.array entry. This can be annoying, so if anyone has a solution to this, please let us know.

The file luapreload_HelloWorld.c should include following code:

#	define PRELOADLIB( name) do { \
		extern int luapreload_##name( lua_State *L); \
		luapreload_##name( L); \
	} while( 0)
	PRELOADLIB( Ethernet);
	PRELOADLIB( Shell);
#	undef PRELOADLIB

If PRELOADLIB( Shell) is missing, tweak Shell.h file from previous post.

Clean and Build. Download dwl into device.
If device is Ethernet connected, yellow LED should turn on, AT+ETH command gives assigned IP address.
Connect with Telnet client and enjoy.

Now you are on your own.