Hack IPCAM [ Anyka ] – Cross-Compile NodeJS and call native code

In this tutorial I will give quick tips on how to compile NodeJS for ARMv5 architecture used in camera v380. And also how to call native code through javascript.

This is a series of tutorials, you can see the topic list here

First of all, node is already available in the rootfs version that I make available at:https://github.com/ricardojlrufino/anyka_v380ipcam_experiments

Compatibility

In the case as we are focused on a camera with an old architecture and kernel, I made several build attempts and got the following results:

node v0.10.38 – OK (working)
node v0.10.48 – OK (working)
node v0.11.1 – FAIL **
node v4.9.0 – compile (but Segmentation fault)
node v4.9.1 – compile (but Segmentation fault)
node v5.11.1 – FAIL
node v6.17.1 – FAIL
node v8.17.0 – FAIL
node v10.24.1 – FAIL

On node v0.11.1, v8 engine 3.18.0. Removed ARM support for VFP2.
https://github.com/nodejs/node/commit/9f682265d6631a29457abeb53827d01fa77493c8

Cross-Compiling

I prepared a script that does the whole process, from downloading, configuring the variables and compiling.

https://github.com/ricardojlrufino/anyka_v380ipcam_experiments/blob/master/nodejs/v0.10_arm/build.sh

Download repository, goto folder /nodejs/v0.10_arm , and run ./build.sh

This will create a folder called: node_install, and static binary in /bin/node

This binary can be copied to camera and run on camera without dependencies..

Limitations

Although it also has NPM it is not very functional, because the file system is read only, it takes a long time to run. So the package management I do manually, copying the node_modules on the PC to the SDCARD.

Some modules I wanted to use like ‘sqlite3’ use native integration (C++).
As modules generally use the N-API API, and this api was only introduced in Node v8, libraries that use this feature will not work.

Below I present an alternative to be able to call C++ code using NodeJs from old versions

Calling C++ code from Node using NAN

We are going to use this library: https://github.com/nodejs/nan

Despite it claiming that it supports node (0.8, 0.10, 0.12), I had to downgrade the version (“nan”: “2.10.0”) (https://github.com/nodejs/nan/issues/933)

In order for us to compile the libraries, we also need to compile Node v0.10.48 for the desktop.

I already did it, you can find it at:
https://github.com/ricardojlrufino/anyka_v380ipcam_experiments/tree/master/nodejs/v0.10_linux64

If you need compile, you only need execute normal build.

git clone –depth 1 https://github.com/nodejs/node -b v0.10.48 node
cd node
./configure –prefix=$(pwd)/install
make && make install

Compiling a node library that uses C++

In the folder ‘/nodejs/ex_node_nativelib‘ there is an example.
To compile, use the script: build.sh in the directory above

This will generate

Working With the Extension on camera

You can unpack this folder on camera, and execute:

$ node
> var NativeExtension = require('./')
undefined
> NativeExtension.aString()
'This is a thing.'
> NativeExtension.aBoolean()
false

Exemple using library:

I prepared another example that uses this library: /nodejs/ex_nativelib_demo

You can just copy to camera and run. Note that here I am making use of an extra package “bindings”, it is responsible for helping to load the native library

TODO

I’ll provide more examples later, how to use typescript, and automatically generate a file with the modules in just a single file.

So that’s it, we have a high-level language to speed up the development process and when you need to be able to integrate with native code.

References

https://ctrl-v.biz/en/blog/63-node-js-cross-compilation-for-socionext-development-board
https://chrislea.com/2018/08/20/cross-compiling-node-js-for-arm-on-ubuntu/

Deixe um comentário

Preencha os seus dados abaixo ou clique em um ícone para log in:

Logotipo do WordPress.com

Você está comentando utilizando sua conta WordPress.com. Sair /  Alterar )

Imagem do Twitter

Você está comentando utilizando sua conta Twitter. Sair /  Alterar )

Foto do Facebook

Você está comentando utilizando sua conta Facebook. Sair /  Alterar )

Conectando a %s