Example 1: Install/Uninstall WASM App Remotely
The related code/working directory of this example resides in directory {WAMR_DIR}/samples/simple
"simple" sample introduction
Note: The sample has been migrated to bytecodealliance/wamr-app-framework/samples/simple.
This sample demonstrates following scenarios:
Use tool "host_tool" to remotely install/uninstall wasm applications from the WAMR runtime over either TCP socket or UART cable
Inter-app communication programming models
Communication between WASM applications and the remote app host_tool
A number of WASM applications built on top of WAMR application framework API sets
Directory structure
src/ext_lib_export.c This file is used to export native APIs. See the
The mechanism of exporting Native API to WASM application
section in WAMR README.md for detail.src/iwam_main.c This file is the implementation by platform integrator. It implements the interfaces that enable the application manager communicating with the host side. See
{WAMR_ROOT}/core/app-mgr/app-mgr-shared/app_manager_export.h
for the definition of the host interface.
Set physical communication between device and remote
The host_init_func
is called when the application manager starts up. And host_send_fun
is called by the application manager to send data to the host.
Define a global variable "interface" of the data structure:
This interface is passed to application manager during the runtime startup:
Note: The connection between simple and host_tool is TCP by default. The simple application works as a server and the host_tool works as a client. You can also use UART connection. To achieve this you have to uncomment the below line in CMakeLists.txt and rebuild.
To run the UART based test, you have to set up a UART hardware connection between host_tool and the simple application. See the help of host_tool for how to specify UART device parameters.
Build the sample
Execute the build.sh script then all binaries including wasm application files would be generated in 'out' directory.
Enter the profile name for starting your build. "host-***" profiles build the sample for executing on your development machine, and "arm-interp" profile will do cross building for ARM target platform. If "arm-interp" is entered, please ensure the ARM cross compiler toolchain is already installed in your development machine. Your should set ARM_A7_COMPILER_DIR and ARM_A7_SDKTARGETSYSROOT environment variable in your ~/.bashrc correctly. refer to the file profiles/arm-interp/toolchain.cmake.
If you need to create additional profile for customizing your runtime, application framework or the target platforms, a new subfolder can be created under the profiles folder, and place your own version of "toolchain.cmake" and "wamr_config_simple.cmake" in it.
Out directory structure
host_tool: A small testing tool to interact with WAMR. See the usage of this tool by executing "./host_tool -h".
./host_tool -h
simple: A simple testing tool running on the host side that interact with WAMR. It is used to install, uninstall and query WASM applications in WAMR, and send request or subscribe event, etc. See the usage of this application by executing "./simple -h".
./simple -h
Run the sample
Enter the out directory
Startup the 'simple' process works in TCP server mode and you would see "App Manager started." is printed.
Query all installed applications
The 69
stands for response code SUCCESS. The payload is printed with JSON format where the num
stands for application installations number and value 0
means currently no application is installed yet.
Install the request handler wasm application
Now the request handler application is running and waiting for host or other wasm application to send a request.
Query again
In the payload, we can see num
is 1 which means 1 application is installed. applet1
stands for the name of the 1st application. heap1
stands for the heap size of the 1st application.
Send request from host to specific wasm application
We can see a response with status 69
and a payload is received.
Output of simple application:
Send a general request from host (not specify target application name)
Output of simple application:
Install the event publisher wasm application
Subscribe event by host_tool
We can see 4 alert/overheat
events are received in 3 seconds which is published by the pub
application.
Output of simple
Install the event subscriber wasm application
The sub
application is installed.
Output of simple
We can see the sub
application receives the alert/overheat
event and dumps it out.
At device side, the event is represented by an attribute container which contains key-value pairs like below:
warning
is the key's name. string
means this is a string value and temperature is over high
is the value.
Uninstall the wasm application
Query again
Note: Here we only installed part of the sample WASM applications. You can try others by yourself.
Note: You have to manually kill the simple process by Ctrl+C after use.
Last updated