Skip to content

Commit 7fb456c

Browse files
committed
feat: add message port example for tizen
Signed-off-by: Hosung Kim [email protected]
1 parent 1b2eade commit 7fb456c

File tree

8 files changed

+269
-0
lines changed

8 files changed

+269
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
out
2+
lib/
3+
bin/
4+
include/
5+
packaging/*.p12
6+
.git
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
cmake_minimum_required(VERSION 3.9.4)
2+
3+
project(lwnode-example-messageport)
4+
5+
find_library(LWNODE_LIB lwnode HINT lib)
6+
7+
find_package(PkgConfig REQUIRED)
8+
pkg_check_modules (TIZEN_PKGS REQUIRED dlog)
9+
10+
set(SAMPLE_DEFINITIONS
11+
-DNODE_ARCH=arm,
12+
-DNODE_PLATFORM=linux,
13+
-DNODE_WANT_INTERNALS=1,
14+
)
15+
16+
add_executable(${PROJECT_NAME} src/main.cc)
17+
target_include_directories(${PROJECT_NAME} PRIVATE "include" ${TIZEN_PKGS_INCLUDE_DIRS})
18+
target_link_libraries(${PROJECT_NAME} PRIVATE ${LWNODE_LIB} ${TIZEN_PKGS_LIBRARIES} -pthread -Wl,-rpath='\$\$ORIGIN/../lib')
19+
target_link_directories(${PROJECT_NAME} PRIVATE ${TIZEN_PKGS_LIBRARY_DIRS})
20+
target_compile_definitions(${PROJECT_NAME} PRIVATE ${SAMPLE_DEFINITIONS})
21+

examples/tizen/message-port/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
### Copy lightweight node header and library
3+
copy `liblwnode.so` to `lib` folder
4+
copy `libescargot.so` to `lib` folder
5+
copy `lwnode.dat` to `bin` folder
6+
copy `lwnode-public.h` to `include` folder
7+
copy `message-port.h` to `include` folder
8+
```bash
9+
cd <app_project_root>
10+
cp <lwnode_root>/out/tizen/arm/Release/lib/liblwnode.so ./lib/
11+
cp <lwnode_root>/out/tizen/arm/Release/gen/escargot/libescargot.so ./lib/
12+
cp <lwnode_root>/out/tizen/arm/Release/lwnode.dat ./bin/
13+
cp <lwnode_root>/include/lwnode/lwnode-public.h ./include/
14+
cp <lwnode_root>/include/lwnode/message-port.h ./include/
15+
```
16+
17+
### Copy certification profile files
18+
If you want to sign your app, copy some files to `packaging` folder.
19+
```bash
20+
cp tizen_author.p12 ./packaging/
21+
cp tizen-distributor-partner-manufacturer-signer.p12 ./packaging/
22+
```
23+
24+
### Package tpk
25+
To package tpk, you should build gbs.
26+
For example,
27+
``` bash
28+
gbs -c ~/gbs.conf build -A armv7l -B ~/GBS-ROOT/helloworld --incremental --include-all
29+
```
30+
After build, you can find tpk file in `out` folder.
31+
32+
33+
### Show log
34+
To view node console log, use `dlogutil lwnode` command.
35+
``` bash
36+
dlogutil lwnode
37+
```
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<manifest>
2+
<request>
3+
<domain name="_" />
4+
</request>
5+
</manifest>
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
Name: lwnode-example-messageport
2+
Summary: -
3+
Version: 1.0.0
4+
Release: 1
5+
Group: System/Servers
6+
License: MIT
7+
Source: %{name}-%{version}.tar.gz
8+
Source1001: lwnode-example-messageport.manifest
9+
10+
Requires(post): /sbin/ldconfig
11+
Requires(postun): /sbin/ldconfig
12+
13+
%if ("%{tizen_version}" >= "7.0")
14+
BuildRequires: sdk-core
15+
BuildRequires: sdk-rootstraps
16+
%else
17+
BuildRequires: builtin-java
18+
BuildRequires: builtin-tizensdk, builtin-tizensdk-rootstraps
19+
BuildRequires: app-signer, python-xml
20+
%endif
21+
22+
BuildRequires: cmake, ninja
23+
BuildRequires: rsync
24+
BuildRequires: pkgconfig(dlog)
25+
BuildRequires: pkgconfig(glib-2.0)
26+
BuildRequires: pkgconfig(aul)
27+
BuildRequires: pkgconfig(capi-appfw-app-common)
28+
29+
%define _signer_url http://10.40.68.214/kuep_net_signer.sh
30+
31+
%description
32+
sample
33+
34+
# Configure
35+
36+
# Initialize the variables
37+
%define _output ./out
38+
%define _native_out ./out/%{name}
39+
%define _jsroot res
40+
%define _lib_dist ./out/project/%{_jsroot}/lib
41+
%define _native_dist ./out/project/%{_jsroot}/lib/build
42+
%define _project ./out/project
43+
%define _certi ./packaging
44+
45+
%prep
46+
%setup -q
47+
48+
%build
49+
mkdir -p %{_native_out}
50+
cmake . -B%{_native_out} -H. -G Ninja
51+
ninja -v -C %{_native_out}
52+
53+
# project scaffolding
54+
rm -rf %{_project}
55+
mkdir -p %{_project}/%{_jsroot}
56+
57+
# copy lwnode binary
58+
cp -rf bin %{_project}
59+
cp -rf lib %{_project}
60+
61+
# copy JavaScript files
62+
rsync -avm --include='*.js' -f 'hide,! */' ./%{_jsroot}/* %{_project}/%{_jsroot}
63+
rsync -avm --include='*.node' -f 'hide,! */' ./%{_jsroot}/* %{_project}/%{_jsroot}
64+
rsync -avm --include='*.json' -f 'hide,! */' ./%{_jsroot}/* %{_project}/%{_jsroot}
65+
cp -rf tizen-manifest.xml %{_project}
66+
67+
# copy exec file
68+
cp -f %{_native_out}/%{name} %{_project}/bin
69+
70+
# setup tizen certificate profile
71+
tizen cli-config "profiles.path=%{_prefix}/tizen-studio-data/profile/profiles.xml"
72+
tizen security-profiles add -n ABS -a %{_certi}/tizen_author.p12 -p \
73+
tizenauthor -d %{_certi}/tizen-distributor-partner-manufacturer-signer.p12 \
74+
-dp tizenpkcs12passfordsigner
75+
76+
# signing
77+
curl -o ./kuep_net_signer.sh %{_signer_url} && chmod +x ./kuep_net_signer.sh
78+
./kuep_net_signer.sh -s -tizen_major_ver %{?tizen_version_major} %{_project}/lib/liblwnode.*
79+
./kuep_net_signer.sh -s -tizen_major_ver %{?tizen_version_major} %{_project}/lib/libescargot.*
80+
./kuep_net_signer.sh -s -tizen_major_ver %{?tizen_version_major} %{_project}/bin/%{name}
81+
82+
# packaging
83+
cd %{_project}
84+
zip ../%{name}.tpk * -r
85+
cd - > /dev/null
86+
tizen package -t tpk -s ABS -- %{_output}/%{name}.tpk
87+
88+
%install
89+
90+
%clean
91+
rm -f ./*.list
92+
rm -f ./*.manifest
93+
rm -rf %{_project}
94+
rm -rf %{_native_out}
95+
rm -f kuep_net_signer.sh
96+
97+
%post
98+
/sbin/ldconfig
99+
100+
%postun
101+
/sbin/ldconfig
102+
103+
%files
104+
%defattr(-,root,root,-)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
console.log('[JS] started...');
2+
3+
const lwnode = process.lwnode;
4+
const port = process.lwnode.port;
5+
6+
// Keep the process alive until we receive a message.
7+
// lwnode is not terminated until lwnode.unref() is called, which decreases the reference count.
8+
lwnode.ref();
9+
10+
// Listen for messages from the native app.
11+
port.onmessage = (event) => {
12+
console.log(`[JS] receive ${event.data}`);
13+
if (event.data == "ping") {
14+
15+
// send a response back to the native app. (c -> js)
16+
port.postMessage("pong");
17+
18+
// Unref the process. If you want to keep it alive, remove this line.
19+
lwnode.unref();
20+
}
21+
};
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <dlog.h>
2+
#include <cstdlib>
3+
#include <future>
4+
#include <thread>
5+
6+
#include "lwnode-public.h"
7+
8+
#define APP_LOG_TAG "lwnode"
9+
10+
int main(int argc, char* argv[]) {
11+
dlog_print(
12+
DLOG_INFO, APP_LOG_TAG, "[Native] message-port example app started...");
13+
14+
std::promise<void> promise;
15+
std::future<void> init_future = promise.get_future();
16+
17+
auto runtime = std::make_shared<lwnode::Runtime>();
18+
19+
std::thread t([&]() {
20+
dlog_print(DLOG_INFO, APP_LOG_TAG, "[Native] start thread");
21+
22+
lwnode::SetDlogID(APP_LOG_TAG); // set dlog tag for lwnode
23+
24+
std::string path = "/opt/usr/apps/lwnode-example-messageport/res/";
25+
26+
if (!lwnode::InitScriptRootPath(path)) {
27+
dlog_print(DLOG_ERROR,
28+
APP_LOG_TAG,
29+
"[Native] fail to set script root path: %s",
30+
path.c_str());
31+
32+
return;
33+
}
34+
35+
char* args[] = {
36+
const_cast<char*>(""), const_cast<char*>("index.js"), nullptr};
37+
38+
dlog_print(DLOG_INFO, APP_LOG_TAG, "[Native] runtime.Start() called...");
39+
runtime->Start(2, args, std::move(promise));
40+
41+
dlog_print(DLOG_INFO, APP_LOG_TAG, "[Native] end thread");
42+
});
43+
44+
// Wait until the js script is initialized to get the port.
45+
init_future.wait();
46+
47+
std::shared_ptr<Port> port = runtime->GetPort();
48+
49+
// Register a callback function to receive messages from the js script.
50+
port->OnMessage([](const MessageEvent* event) {
51+
dlog_print(DLOG_INFO,
52+
APP_LOG_TAG,
53+
"[Native] received message from js: %s",
54+
event->data().c_str());
55+
});
56+
57+
// Send a message to the js script. The main script will send back a
58+
// message.
59+
port->PostMessage(MessageEvent::New("ping"));
60+
61+
t.join();
62+
63+
return 0;
64+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns="http://tizen.org/ns/packages" api-version="8.0" package="lwnode-example-messageport" version="1.0.0">
3+
<profile name="tv-samsung" />
4+
<service-application appid="lwnode-example-messageport" exec="lwnode-example-messageport" type="c++app" multiple="false" taskmanage="false" nodisplay="true" on-boot="false">
5+
<icon>service.png</icon>
6+
<label>Lightweight Example</label>
7+
</service-application>
8+
<privileges>
9+
<!-- <privilege>http://tizen.org/privilege/application.info</privilege> -->
10+
</privileges>
11+
</manifest>

0 commit comments

Comments
 (0)