Import app manager, samples and test-tools

This commit is contained in:
wenyongh
2019-05-17 17:15:25 +08:00
parent b6e29e2153
commit dd5b133fa5
164 changed files with 21123 additions and 496 deletions

View File

@ -0,0 +1,95 @@
cmake_minimum_required (VERSION 2.8)
project (simple)
set (TARGET_PLATFORM "linux")
# Reset default linker flags
set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
set (CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
# Enable repl mode if want to test spec cases
# add_definitions(-DWASM_ENABLE_REPL)
if (NOT ("$ENV{VALGRIND}" STREQUAL "YES"))
add_definitions(-DNVALGRIND)
endif ()
# Currently build as 32-bit by default.
set (BUILD_AS_64BIT_SUPPORT "NO")
if (CMAKE_SIZEOF_VOID_P EQUAL 8)
if (${BUILD_AS_64BIT_SUPPORT} STREQUAL "YES")
# Add -fPIC flag if build as 64-bit
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS} -fPIC")
else ()
add_definitions (-m32)
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -m32")
set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -m32")
endif ()
endif ()
if (NOT CMAKE_BUILD_TYPE)
SET(CMAKE_BUILD_TYPE Debug)
endif (NOT CMAKE_BUILD_TYPE)
message ("CMAKE_BUILD_TYPE = " ${CMAKE_BUILD_TYPE})
if (NOT PLATFORM)
SET(PLATFORM linux)
endif (NOT PLATFORM)
message ("PLATFORM = " ${PLATFORM})
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ffunction-sections -fdata-sections -Wall -Wno-unused-parameter -Wno-pedantic")
set(REPO_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../../..)
set(WASM_DIR ${REPO_ROOT_DIR}/wamr/core/iwasm)
set(APP_MGR_DIR ${REPO_ROOT_DIR}/wamr/core/app-mgr)
set(SHARED_DIR ${REPO_ROOT_DIR}/wamr/core/shared-lib)
enable_language (ASM)
include (${WASM_DIR}/runtime/platform/${TARGET_PLATFORM}/platform.cmake)
include (${WASM_DIR}/runtime/utils/utils.cmake)
include (${WASM_DIR}/runtime/vmcore-wasm/vmcore.cmake)
include (${WASM_DIR}/lib/native/base/wasm_lib_base.cmake)
include (${WASM_DIR}/lib/native/libc/wasm_libc.cmake)
include (${WASM_DIR}/lib/native/extension/sensor/wasm_lib_sensor.cmake)
include (${WASM_DIR}/lib/native-interface/native_interface.cmake)
include (${APP_MGR_DIR}/app-manager/app_mgr.cmake)
include (${APP_MGR_DIR}/app-mgr-shared/app_mgr_shared.cmake)
include (${SHARED_DIR}/platform/${TARGET_PLATFORM}/shared_platform.cmake)
include (${SHARED_DIR}/utils/shared_utils.cmake)
include (${SHARED_DIR}/mem-alloc/mem_alloc.cmake)
include (${SHARED_DIR}/coap/lib_coap.cmake)
include_directories(${SHARED_DIR}/include)
#Note: uncomment below line to use UART mode
#add_definitions (-DCONNECTION_UART)
add_definitions (-DWASM_ENABLE_BASE_LIB)
add_definitions (-Dattr_container_malloc=bh_malloc)
add_definitions (-Dattr_container_free=bh_free)
add_library (vmlib
${WASM_PLATFORM_LIB_SOURCE}
${WASM_UTILS_LIB_SOURCE}
${VMCORE_LIB_SOURCE}
${WASM_LIBC_SOURCE}
${APP_MGR_SOURCE}
${WASM_LIB_BASE_SOURCE}
${WASM_LIB_EXT_SOURCE}
${WASM_LIB_SENSOR_SOURCE}
${PLATFORM_SHARED_SOURCE}
${UTILS_SHARED_SOURCE}
${MEM_ALLOC_SHARED_SOURCE}
${NATIVE_INTERFACE_SOURCE}
)
add_executable (simple src/main.c src/iwasm_main.c src/ext_lib_export.c)
target_link_libraries (simple vmlib -lm -ldl -lpthread)

323
samples/simple/README.md Normal file
View File

@ -0,0 +1,323 @@
Introduction
==============
This project builds out both host tools running on the host side, an application running on the device side. The device application consists of iwasm, application library, application manager, timers and sensors support. The device runs on Linux OS and interacts with host tools.
It demonstrates an end to end scenario, the wasm applications life cycle management and communication programming models.
Directory structure
------------------------------
```
simple/
├── build.sh
├── CMakeLists.txt
├── README.md
├── src
│   ├── ext_lib_export.c
│   ├── iwasm_main.c
│   └── main.c
└── wasm-apps
├── event_publisher
│   └── event_publisher.c
├── event_subscriber
│   └── event_subscriber.c
├── request_handler
│   └── request_handler.c
├── request_sender
│   └── request_sender.c
├── sensor
│   └── sensor.c
└── timer
   └── timer.c
```
- build.sh<br/>
The script to build all binaries.
- CMakeLists.txt<br/>
CMake file used to build the simple application.
- README.md<br/>
The file you are reading currently.
- src/ext_lib_export.c<br/>
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<br/>
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.
```
/* Interfaces of host communication */
typedef struct host_interface {
host_init_func init;
host_send_fun send;
host_destroy_fun destroy;
} host_interface;
```
```
host_interface interface = {
.init = host_init,
.send = host_send,
.destroy = host_destroy
};
```
This interface is passed to application manager by calling
```
app_manager_startup(&interface);
```
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.
>**Note:** Currently application manager keeps running and never exit, `host_destroy_fun` has no chance to get executed. So you can leave this API implementation empty.
- src/main.c<br/>
The main file.
- wasm-apps<br/>
Source files of sample wasm applications.
Build all binaries
==============
Execute the build.sh script then all binaries including wasm application files would be generated in 'out' directory.
`./build.sh`
Out directory structure
------------------------------
```
out/
├── host_tool
├── simple
└── wasm-apps
├── event_publisher.wasm
├── event_subscriber.wasm
├── request_handler.wasm
├── request_sender.wasm
├── sensor.wasm
└── timer.wasm
```
- 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`
>****Note:**** The connection between simple and host_tool is TCP by default and is what this guide uses. 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. You have to set up a UART hardware connection between 2 machines one of which runs the host_tool and the other runs the simple application. See the help of host_tool and the simple application to know how to specify UART device parameters.<br/>
`#add_definitions (-DCONNECTION_UART)`
- wasm-apps:
Sample wasm applications that demonstrate all APIs of the WAMR programming model. The source codes are in the wasm-apps directory under the root of this project.
+ event_publisher.wasm<br/>
This application shows the sub/pub programming model. The pub application publishes the event "alert/overheat" by calling api_publish_event() API. The subscriber could be host_tool or other wasm application.
+ event_subscriber.wasm<br/>
This application shows the sub/pub programming model. The sub application subscribes the "alert/overheat" event by calling api_subscribe_event() API so that it is able to receive the event once generated and published by the pub application. To make the process clear to interpret, the sub application dumps the event when receiving it.
+ request_handler.wasm<br/>
This application shows the request/response programming model. The request handler application registers 2 resources(/url1 and /url2) by calling api_register_resource_handler() API. The request sender could be host_tool or other wasm application.
+ request_sender.wasm<br/>
This application shows the request/response programming model. The sender application sends 2 requests, one is "/app/request_handler/url1" and the other is "url1". The former is an accurate request which explicitly specifies the name of request handler application in the middle of the URL and the later is a general request.
+ sensor.wasm<br/>
This application shows the sensor programming model. It opens a test sensor and configures the sensor event generating interval to 1 second. To make the process clear to interpret, the application dumps the sensor event when receiving it.
+ timer.wasm<br/>
This application shows the timer programming model. It creates a periodic timer that prints the current expiry number in every second.
Run the scenario
==========================
- Enter the out directory<br/>
```
$ cd ./out/
```
- Startup the 'simple' process works in TCP server mode and you would see "App Manager started." is printed.<br/>
```
$ ./simple -s
App Manager started.
```
- Query all installed applications<br/>
```
$ ./host_tool -q
response status 69
{
"num": 0
}
```
The `69` stands for response status to this query request which means query success and a payload is attached with the response. See `{WAMR_ROOT}/core/iwasm/lib/app-libs/base/wasm_app.h` for the definitions of response codes. 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<br/>
```
$ ./host_tool -i request_handler -f ./wasm-apps/request_handler.wasm
response status 65
```
The `65` stands for response status to this installation request which means success.
Output of simple
```
Install WASM app success!
sent 16 bytes to host
WASM app 'request_handler' started
```
Now the request handler application is running and waiting for host or other wasm application to send a request.
- Query again<br/>
```
$ ./host_tool -q
response status 69
{
"num": 1,
"applet1": "request_handler",
"heap1": 49152
}
```
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<br/>
```
$ ./host_tool -r /app/request_handler/url1 -A GET
response status 69
{
"key1": "value1",
"key2": "value2"
}
```
We can see a response with status `69` and a payload is received.
Output of simple
```
connection established!
Send request to applet: request_handler
Send request to app request_handler success.
App request_handler got request, url url1, action 1
[resp] ### user resource 1 handler called
sent 150 bytes to host
Wasm app process request success.
```
- Send a general request from host (not specify target application name)<br/>
```
$ ./host_tool -r /url1 -A GET
response status 69
{
"key1": "value1",
"key2": "value2"
}
```
Output of simple
```
connection established!
Send request to app request_handler success.
App request_handler got request, url /url1, action 1
[resp] ### user resource 1 handler called
sent 150 bytes to host
Wasm app process request success.
```
- Install the event publisher wasm application<br/>
```
$ ./host_tool -i pub -f ./wasm-apps/event_publisher.wasm
response status 65
```
- Subscribe event by host_tool<br/>
```
$ ./host_tool -s /alert/overheat -a 3000
response status 69
received an event alert/overheat
{
"warning": "temperature is over high"
}
received an event alert/overheat
{
"warning": "temperature is over high"
}
received an event alert/overheat
{
"warning": "temperature is over high"
}
received an event alert/overheat
{
"warning": "temperature is over high"
}
```
We can see 4 `alert/overheat` events are received in 3 seconds which is published by the `pub` application.
Output of simple
```
connection established!
am_register_event adding url:(alert/overheat)
client: -3 registered event (alert/overheat)
sent 16 bytes to host
sent 142 bytes to host
sent 142 bytes to host
sent 142 bytes to host
sent 142 bytes to host
```
- Install the event subscriber wasm application<br/>
```
$ ./host_tool -i sub -f ./wasm-apps/event_subscriber.wasm
response status 65
```
The `sub` application is installed.
Output of simple
```
connection established!
Install WASM app success!
WASM app 'sub' started
am_register_event adding url:(alert/overheat)
client: 3 registered event (alert/overheat)
sent 16 bytes to host
Send request to app sub success.
App sub got request, url alert/overheat, action 6
### user over heat event handler called
Attribute container dump:
Tag:
Attribute list:
key: warning, type: string, value: temperature is over high
Wasm app process request success.
```
We can see the `sub` application receives the `alert/overheat` event and dumps it out.<br/>
At device side, the event is represented by an attribute container which contains key-value pairs like below:
```
Attribute container dump:
Tag:
Attribute list:
key: warning, type: string, value: temperature is over high
```
`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<br/>
```
$ ./host_tool -u request_handler
response status 66
$ ./host_tool -u pub
response status 66
$ ./host_tool -u sub
response status 66
```
- Query again<br/>
```
$ ./host_tool -q
response status 69
{
"num": 0
}
```
>**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.

102
samples/simple/build.sh Executable file
View File

@ -0,0 +1,102 @@
#!/bin/bash
CURR_DIR=$PWD
ROOT_DIR=${PWD}/../../..
OUT_DIR=${PWD}/out
BUILD_DIR=${PWD}/build
IWASM_ROOT=${PWD}/../../core/iwasm
APP_LIBS=${IWASM_ROOT}/lib/app-libs
NATIVE_LIBS=${IWASM_ROOT}/lib/native-interface
APP_LIB_SRC="${APP_LIBS}/base/*.c ${APP_LIBS}/extension/sensor/*.c ${NATIVE_LIBS}/*.c"
WASM_APPS=${PWD}/wasm-apps
rm -rf ${OUT_DIR}
mkdir ${OUT_DIR}
mkdir ${OUT_DIR}/wasm-apps
cd ${ROOT_DIR}/wamr/core/shared-lib/mem-alloc
if [ ! -d "tlsf" ]; then
git clone https://github.com/mattconte/tlsf
fi
echo "#####################build simple project"
cd ${CURR_DIR}
mkdir -p cmake_build
cd cmake_build
cmake ..
make
if [ $? != 0 ];then
echo "BUILD_FAIL simple exit as $?\n"
exit 2
fi
cp -a simple ${OUT_DIR}
echo "#####################build simple project success"
echo "#####################build host-tool"
cd ${ROOT_DIR}/wamr/test-tools/host-tool
mkdir -p bin
cd bin
cmake ..
make
if [ $? != 0 ];then
echo "BUILD_FAIL host tool exit as $?\n"
exit 2
fi
cp host_tool ${OUT_DIR}
echo "#####################build host-tool success"
echo "#####################build wasm apps"
cd ${CURR_DIR}
APP_SRC="${WASM_APPS}/timer/timer.c ${APP_LIB_SRC}"
emcc -O3 -I${APP_LIBS}/base -I${APP_LIBS}/extension/sensor -I${NATIVE_LIBS} \
-s WASM=1 -s SIDE_MODULE=1 -s ASSERTIONS=1 -s STACK_OVERFLOW_CHECK=2 \
-s TOTAL_MEMORY=65536 -s TOTAL_STACK=4096 \
-s "EXPORTED_FUNCTIONS=['_on_init', '_on_destroy', '_on_request', '_on_response', \
'_on_sensor_event', '_on_timer_callback']" \
-o ${OUT_DIR}/wasm-apps/timer.wasm ${APP_SRC}
APP_SRC="${WASM_APPS}/request_handler/request_handler.c ${APP_LIB_SRC}"
emcc -O3 -I${APP_LIBS}/base -I${APP_LIBS}/extension/sensor -I${NATIVE_LIBS} \
-s WASM=1 -s SIDE_MODULE=1 -s ASSERTIONS=1 -s STACK_OVERFLOW_CHECK=2 \
-s TOTAL_MEMORY=65536 -s TOTAL_STACK=4096 \
-s "EXPORTED_FUNCTIONS=['_on_init', '_on_destroy', '_on_request', '_on_response', \
'_on_sensor_event', '_on_timer_callback']" \
-o ${OUT_DIR}/wasm-apps/request_handler.wasm ${APP_SRC}
APP_SRC="${WASM_APPS}/request_sender/request_sender.c ${APP_LIB_SRC}"
emcc -O3 -I${APP_LIBS}/base -I${APP_LIBS}/extension/sensor -I${NATIVE_LIBS} \
-s WASM=1 -s SIDE_MODULE=1 -s ASSERTIONS=1 -s STACK_OVERFLOW_CHECK=2 \
-s TOTAL_MEMORY=65536 -s TOTAL_STACK=4096 \
-s "EXPORTED_FUNCTIONS=['_on_init', '_on_destroy', '_on_request', '_on_response', \
'_on_sensor_event', '_on_timer_callback']" \
-o ${OUT_DIR}/wasm-apps/request_sender.wasm ${APP_SRC}
APP_SRC="${WASM_APPS}/event_publisher/event_publisher.c ${APP_LIB_SRC}"
emcc -O3 -I${APP_LIBS}/base -I${APP_LIBS}/extension/sensor -I${NATIVE_LIBS} \
-s WASM=1 -s SIDE_MODULE=1 -s ASSERTIONS=1 -s STACK_OVERFLOW_CHECK=2 \
-s TOTAL_MEMORY=65536 -s TOTAL_STACK=4096 \
-s "EXPORTED_FUNCTIONS=['_on_init', '_on_destroy', '_on_request', '_on_response', \
'_on_sensor_event', '_on_timer_callback']" \
-o ${OUT_DIR}/wasm-apps/event_publisher.wasm ${APP_SRC}
APP_SRC="${WASM_APPS}/event_subscriber/event_subscriber.c ${APP_LIB_SRC}"
emcc -O3 -I${APP_LIBS}/base -I${APP_LIBS}/extension/sensor -I${NATIVE_LIBS} \
-s WASM=1 -s SIDE_MODULE=1 -s ASSERTIONS=1 -s STACK_OVERFLOW_CHECK=2 \
-s TOTAL_MEMORY=65536 -s TOTAL_STACK=4096 \
-s "EXPORTED_FUNCTIONS=['_on_init', '_on_destroy', '_on_request', '_on_response', \
'_on_sensor_event', '_on_timer_callback']" \
-o ${OUT_DIR}/wasm-apps/event_subscriber.wasm ${APP_SRC}
APP_SRC="${WASM_APPS}/sensor/sensor.c ${APP_LIB_SRC}"
emcc -O3 -I${APP_LIBS}/base -I${APP_LIBS}/extension/sensor -I${NATIVE_LIBS} \
-s WASM=1 -s SIDE_MODULE=1 -s ASSERTIONS=1 -s STACK_OVERFLOW_CHECK=2 \
-s TOTAL_MEMORY=65536 -s TOTAL_STACK=4096 \
-s "EXPORTED_FUNCTIONS=['_on_init', '_on_destroy', '_on_request', '_on_response', \
'_on_sensor_event', '_on_timer_callback']" \
-o ${OUT_DIR}/wasm-apps/sensor.wasm ${APP_SRC}
echo "#####################build wasm apps success"

View File

@ -0,0 +1,8 @@
#include "lib_export.h"
#include "sensor_api.h"
static NativeSymbol extended_native_symbol_defs[] = {
#include "runtime_sensor.inl"
};
#include "ext_lib_export.h"

View File

@ -0,0 +1,465 @@
#ifndef CONNECTION_UART
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#else
#include <termios.h>
#endif
#include <arpa/inet.h>
#include <unistd.h>
#include <getopt.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/types.h>
#include <fcntl.h>
#include <pthread.h>
#include <signal.h>
#include <unistd.h>
#include <strings.h>
#include "runtime_lib.h"
#include "runtime_timer.h"
#include "native_interface.h"
#include "app_manager_export.h"
#include "bh_common.h"
#include "bh_queue.h"
#include "bh_thread.h"
#include "bh_memory.h"
#include "runtime_sensor.h"
#include "attr_container.h"
#include "module_wasm_app.h"
#include "wasm_export.h"
#define MAX 2048
#ifndef CONNECTION_UART
#define SA struct sockaddr
static char *host_address = "127.0.0.1";
static int port = 8888;
#else
static char *uart_device = "/dev/ttyS2";
static int baudrate = B115200;
#endif
extern void * thread_timer_check(void *);
extern void init_sensor_framework();
extern int aee_host_msg_callback(void *msg, uint16_t msg_len);
#ifndef CONNECTION_UART
int listenfd = -1;
int sockfd = -1;
static pthread_mutex_t sock_lock = PTHREAD_MUTEX_INITIALIZER;
#else
int uartfd = -1;
#endif
#ifndef CONNECTION_UART
static bool server_mode = false;
// Function designed for chat between client and server.
void* func(void* arg)
{
char buff[MAX];
int n;
struct sockaddr_in servaddr;
while (1) {
if (sockfd != -1)
close(sockfd);
// socket create and verification
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
printf("socket creation failed...\n");
return NULL;
} else
printf("Socket successfully created..\n");
bzero(&servaddr, sizeof(servaddr));
// assign IP, PORT
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr(host_address);
servaddr.sin_port = htons(port);
// connect the client socket to server socket
if (connect(sockfd, (SA*) &servaddr, sizeof(servaddr)) != 0) {
printf("connection with the server failed...\n");
sleep(10);
continue;
} else {
printf("connected to the server..\n");
}
// infinite loop for chat
for (;;) {
bzero(buff, MAX);
// read the message from client and copy it in buffer
n = read(sockfd, buff, sizeof(buff));
// print buffer which contains the client contents
//fprintf(stderr, "recieved %d bytes from host: %s", n, buff);
// socket disconnected
if (n <= 0)
break;
aee_host_msg_callback(buff, n);
}
}
// After chatting close the socket
close(sockfd);
}
static bool host_init()
{
return true;
}
int host_send(void * ctx, const char *buf, int size)
{
int ret;
if (pthread_mutex_trylock(&sock_lock) == 0) {
if (sockfd == -1) {
pthread_mutex_unlock(&sock_lock);
return 0;
}
ret = write(sockfd, buf, size);
pthread_mutex_unlock(&sock_lock);
return ret;
}
return -1;
}
void host_destroy()
{
if (server_mode)
close(listenfd);
pthread_mutex_lock(&sock_lock);
close(sockfd);
pthread_mutex_unlock(&sock_lock);
}
host_interface interface = {
.init = host_init,
.send = host_send,
.destroy = host_destroy
};
void* func_server_mode(void* arg)
{
int clilent;
struct sockaddr_in serv_addr, cli_addr;
int n;
char buff[MAX];
struct sigaction sa;
sa.sa_handler = SIG_IGN;
sigaction(SIGPIPE, &sa, 0);
/* First call to socket() function */
listenfd = socket(AF_INET, SOCK_STREAM, 0);
if (listenfd < 0) {
perror("ERROR opening socket");
exit(1);
}
/* Initialize socket structure */
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(port);
/* Now bind the host address using bind() call.*/
if (bind(listenfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
perror("ERROR on binding");
exit(1);
}
listen(listenfd, 5);
clilent = sizeof(cli_addr);
while (1) {
pthread_mutex_lock(&sock_lock);
sockfd = accept(listenfd, (struct sockaddr *) &cli_addr, &clilent);
pthread_mutex_unlock(&sock_lock);
if (sockfd < 0) {
perror("ERROR on accept");
exit(1);
}
printf("connection established!\n");
for (;;) {
bzero(buff, MAX);
// read the message from client and copy it in buffer
n = read(sockfd, buff, sizeof(buff));
// socket disconnected
if (n <= 0) {
pthread_mutex_lock(&sock_lock);
close(sockfd);
sockfd = -1;
pthread_mutex_unlock(&sock_lock);
sleep(2);
break;
}
aee_host_msg_callback(buff, n);
}
}
}
#else
static int parse_baudrate(int baud)
{
switch (baud) {
case 9600:
return B9600;
case 19200:
return B19200;
case 38400:
return B38400;
case 57600:
return B57600;
case 115200:
return B115200;
case 230400:
return B230400;
case 460800:
return B460800;
case 500000:
return B500000;
case 576000:
return B576000;
case 921600:
return B921600;
case 1000000:
return B1000000;
case 1152000:
return B1152000;
case 1500000:
return B1500000;
case 2000000:
return B2000000;
case 2500000:
return B2500000;
case 3000000:
return B3000000;
case 3500000:
return B3500000;
case 4000000:
return B4000000;
default:
return -1;
}
}
static bool uart_init(const char *device, int baudrate, int *fd)
{
int uart_fd;
struct termios uart_term;
uart_fd = open(device, O_RDWR | O_NOCTTY);
if (uart_fd <= 0)
return false;
memset(&uart_term, 0, sizeof(uart_term));
uart_term.c_cflag = baudrate | CS8 | CLOCAL | CREAD;
uart_term.c_iflag = IGNPAR;
uart_term.c_oflag = 0;
/* set noncanonical mode */
uart_term.c_lflag = 0;
uart_term.c_cc[VTIME] = 30;
uart_term.c_cc[VMIN] = 1;
tcflush(uart_fd, TCIFLUSH);
if (tcsetattr(uart_fd, TCSANOW, &uart_term) != 0) {
close(uart_fd);
return false;
}
*fd = uart_fd;
return true;
}
static void *func_uart_mode(void *arg)
{
int n;
char buff[MAX];
if (!uart_init(uart_device, baudrate, &uartfd)) {
printf("open uart fail! %s\n", uart_device);
return NULL;
}
for (;;) {
bzero(buff, MAX);
n = read(uartfd, buff, sizeof(buff));
if (n <= 0) {
close(uartfd);
uartfd = -1;
break;
}
aee_host_msg_callback(buff, n);
}
return NULL;
}
static int uart_send(void * ctx, const char *buf, int size)
{
int ret;
ret = write(uartfd, buf, size);
return ret;
}
static void uart_destroy()
{
close(uartfd);
}
static host_interface interface = { .send = uart_send, .destroy = uart_destroy };
#endif
static char global_heap_buf[512 * 1024] = { 0 };
static void showUsage()
{
#ifndef CONNECTION_UART
printf("Usage:\n");
printf("\nWork as TCP server mode:\n");
printf("\tsimple -s|--server_mode -p|--port <Port>\n");
printf("where\n");
printf("\t<Port> represents the port that would be listened on and the default is 8888\n");
printf("\nWork as TCP client mode:\n");
printf("\tsimple -a|--host_address <Host Address> -p|--port <Port>\n");
printf("where\n");
printf("\t<Host Address> represents the network address of host and the default is 127.0.0.1\n");
printf("\t<Port> represents the listen port of host and the default is 8888\n");
#else
printf("Usage:\n");
printf("\tsimple -u <Uart Device> -b <Baudrate>\n\n");
printf("where\n");
printf("\t<Uart Device> represents the UART device name and the default is /dev/ttyS2\n");
printf("\t<Baudrate> represents the UART device baudrate and the default is 115200\n");
#endif
}
static bool parse_args(int argc, char *argv[])
{
int c;
while (1) {
int optIndex = 0;
static struct option longOpts[] = {
#ifndef CONNECTION_UART
{ "server_mode", no_argument, NULL, 's' },
{ "host_address", required_argument, NULL, 'a' },
{ "port", required_argument, NULL, 'p' },
#else
{ "uart", required_argument, NULL, 'u' },
{ "baudrate", required_argument, NULL, 'b' },
#endif
{ "help", required_argument, NULL, 'h' },
{ 0, 0, 0, 0 }
};
c = getopt_long(argc, argv, "sa:p:u:b:h", longOpts, &optIndex);
if (c == -1)
break;
switch (c) {
#ifndef CONNECTION_UART
case 's':
server_mode = true;
break;
case 'a':
host_address = optarg;
printf("host address: %s\n", host_address);
break;
case 'p':
port = atoi(optarg);
printf("port: %d\n", port);
break;
#else
case 'u':
uart_device = optarg;
printf("uart device: %s\n", uart_device);
break;
case 'b':
baudrate = parse_baudrate(atoi(optarg));
printf("uart baudrate: %s\n", optarg);
break;
#endif
case 'h':
showUsage();
return false;
default:
showUsage();
return false;
}
}
return true;
}
// Driver function
int iwasm_main(int argc, char *argv[])
{
korp_thread tid;
if (!parse_args(argc, argv))
return -1;
if (bh_memory_init_with_pool(global_heap_buf, sizeof(global_heap_buf))
!= 0) {
printf("Init global heap failed.\n");
return -1;
}
if (vm_thread_sys_init() != 0) {
goto fail1;
}
init_sensor_framework();
// timer manager
init_wasm_timer();
#ifndef CONNECTION_UART
if (server_mode)
vm_thread_create(&tid, func_server_mode, NULL,
BH_APPLET_PRESERVED_STACK_SIZE);
else
vm_thread_create(&tid, func, NULL, BH_APPLET_PRESERVED_STACK_SIZE);
#else
vm_thread_create(&tid, func_uart_mode, NULL, BH_APPLET_PRESERVED_STACK_SIZE);
#endif
// TODO:
app_manager_startup(&interface);
fail1: bh_memory_destroy();
return -1;
}

View File

@ -0,0 +1,5 @@
extern void iwasm_main();
int main(int argc, char *argv[])
{
iwasm_main(argc, argv);
}

View File

@ -0,0 +1,57 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "wasm_app.h"
int num = 0;
void publish_overheat_event()
{
attr_container_t *event;
event = attr_container_create("event");
attr_container_set_string(&event, "warning", "temperature is over high");
api_publish_event("alert/overheat", FMT_ATTR_CONTAINER, event,
attr_container_get_serialize_length(event));
attr_container_destroy(event);
}
/* Timer callback */
void timer1_update(user_timer_t timer)
{
publish_overheat_event();
}
void start_timer()
{
user_timer_t timer;
/* set up a timer */
timer = api_timer_create(1000, true, false, timer1_update);
api_timer_restart(timer, 1000);
}
void on_init()
{
start_timer();
}
void on_destroy()
{
/* real destroy work including killing timer and closing sensor is accomplished in wasm app library version of on_destroy() */
}

View File

@ -0,0 +1,36 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "wasm_app.h"
void over_heat_event_handler(request_t *request)
{
printf("### user over heat event handler called\n");
if (request->payload != NULL && request->fmt == FMT_ATTR_CONTAINER)
attr_container_dump((attr_container_t *) request->payload);
}
void on_init()
{
api_subscribe_event("alert/overheat", over_heat_event_handler);
}
void on_destroy()
{
/* real destroy work including killing timer and closing sensor is
accomplished in wasm app library version of on_destroy() */
}

View File

@ -0,0 +1,67 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "wasm_app.h"
static void url1_request_handler(request_t *request)
{
response_t response[1];
attr_container_t *payload;
printf("[resp] ### user resource 1 handler called\n");
if (request->payload != NULL && request->fmt == FMT_ATTR_CONTAINER)
attr_container_dump((attr_container_t *) request->payload);
payload = attr_container_create("wasm app response payload");
if (payload == NULL)
return;
attr_container_set_string(&payload, "key1", "value1");
attr_container_set_string(&payload, "key2", "value2");
make_response_for_request(request, response);
set_response(response, CONTENT_2_05,
FMT_ATTR_CONTAINER,
(void *)payload,
attr_container_get_serialize_length(payload));
api_response_send(response);
attr_container_destroy(payload);
}
static void url2_request_handler(request_t *request)
{
response_t response[1];
make_response_for_request(request, response);
set_response(response, DELETED_2_02, 0, NULL, 0);
api_response_send(response);
printf("### user resource 2 handler called\n");
}
void on_init()
{
/* register resource uri */
api_register_resource_handler("/url1", url1_request_handler);
api_register_resource_handler("/url2", url2_request_handler);
}
void on_destroy()
{
/* real destroy work including killing timer and closing sensor is
accomplished in wasm app library version of on_destroy() */
}

View File

@ -0,0 +1,58 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "wasm_app.h"
static void my_response_handler(response_t *response, void *user_data)
{
char *tag = (char *) user_data;
if (response == NULL) {
printf("[req] request timeout!\n");
return;
}
printf("[req] response handler called mid:%d, status:%d, fmt:%d, payload:%p, len:%d, tag:%s\n",
response->mid, response->status, response->fmt, response->payload,
response->payload_len, tag);
if (response->payload != NULL
&& response->payload_len > 0
&& response->fmt == FMT_ATTR_CONTAINER) {
printf("[req] dump the response payload:\n");
attr_container_dump((attr_container_t *) response->payload);
}
}
static void test_send_request(char *url, char *tag)
{
request_t request[1];
init_request(request, url, COAP_PUT, 0, NULL, 0);
api_send_request(request, my_response_handler, tag);
}
void on_init()
{
test_send_request("/app/request_handler/url1", "a request to target app");
test_send_request("url1", "a general request");
}
void on_destroy()
{
/* real destroy work including killing timer and closing sensor is
accomplished in wasm app library version of on_destroy() */
}

View File

@ -0,0 +1,59 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "wasm_app.h"
static sensor_t sensor = NULL;
/* Sensor event callback*/
void sensor_event_handler(sensor_t sensor, attr_container_t *event,
void *user_data)
{
printf("### app get sensor event\n");
attr_container_dump(event);
}
void on_init()
{
char *user_data;
attr_container_t *config;
printf("### app on_init 1\n");
/* open a sensor */
user_data = malloc(100);
printf("### app on_init 2\n");
sensor = sensor_open("sensor_test", 0, sensor_event_handler, user_data);
printf("### app on_init 3\n");
/* config the sensor */
sensor_config(sensor, 1000, 0, 0);
printf("### app on_init 4\n");
/*
config = attr_container_create("sensor config");
sensor_config(sensor, config);
attr_container_destroy(config);
*/
}
void on_destroy()
{
if (NULL != sensor) {
sensor_config(sensor, 0, 0, 0);
}
/* real destroy work including killing timer and closing sensor is
accomplished in wasm app library version of on_destroy() */
}

View File

@ -0,0 +1,41 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "wasm_app.h"
/* User global variable */
static int num = 0;
/* Timer callback */
void timer1_update(user_timer_t timer)
{
printf("Timer update %d\n", num++);
}
void on_init()
{
user_timer_t timer;
/* set up a timer */
timer = api_timer_create(1000, true, false, timer1_update);
api_timer_restart(timer, 1000);
}
void on_destroy()
{
/* real destroy work including killing timer and closing sensor is
accomplished in wasm app library version of on_destroy() */
}