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,341 @@
/*
* 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 <stdio.h>
#include <stdbool.h>
#include "display_indev.h"
#include "SDL2/SDL.h"
#include "sys/time.h"
#include "wasm_export.h"
#define MONITOR_HOR_RES 320
#define MONITOR_VER_RES 240
#ifndef MONITOR_ZOOM
#define MONITOR_ZOOM 1
#endif
#define SDL_REFR_PERIOD 50
void monitor_sdl_init(void);
void monitor_sdl_refr_core(void);
void monitor_sdl_clean_up(void);
static uint32_t tft_fb[MONITOR_HOR_RES * MONITOR_VER_RES];
int time_get_ms()
{
static struct timeval tv;
gettimeofday(&tv, NULL);
long long time_in_mill = (tv.tv_sec) * 1000 + (tv.tv_usec) / 1000;
return (int) time_in_mill;
}
SDL_Window * window;
SDL_Renderer * renderer;
SDL_Texture * texture;
static volatile bool sdl_inited = false;
static volatile bool sdl_refr_qry = false;
static volatile bool sdl_quit_qry = false;
void monitor_flush(int32_t x1, int32_t y1, int32_t x2, int32_t y2,
const lv_color_t * color_p)
{
/*Return if the area is out the screen*/
if (x2 < 0 || y2 < 0 || x1 > MONITOR_HOR_RES - 1
|| y1 > MONITOR_VER_RES - 1) {
return;
}
int32_t y;
uint32_t w = x2 - x1 + 1;
for (y = y1; y <= y2; y++) {
memcpy(&tft_fb[y * MONITOR_HOR_RES + x1], color_p,
w * sizeof(lv_color_t));
color_p += w;
}
sdl_refr_qry = true;
/*IMPORTANT! It must be called to tell the system the flush is ready*/
}
/**
* Fill out the marked area with a color
* @param x1 left coordinate
* @param y1 top coordinate
* @param x2 right coordinate
* @param y2 bottom coordinate
* @param color fill color
*/
void monitor_fill(int32_t x1, int32_t y1, int32_t x2, int32_t y2,
lv_color_t color)
{
/*Return if the area is out the screen*/
if (x2 < 0)
return;
if (y2 < 0)
return;
if (x1 > MONITOR_HOR_RES - 1)
return;
if (y1 > MONITOR_VER_RES - 1)
return;
/*Truncate the area to the screen*/
int32_t act_x1 = x1 < 0 ? 0 : x1;
int32_t act_y1 = y1 < 0 ? 0 : y1;
int32_t act_x2 = x2 > MONITOR_HOR_RES - 1 ? MONITOR_HOR_RES - 1 : x2;
int32_t act_y2 = y2 > MONITOR_VER_RES - 1 ? MONITOR_VER_RES - 1 : y2;
int32_t x;
int32_t y;
uint32_t color32 = color.full; //lv_color_to32(color);
for (x = act_x1; x <= act_x2; x++) {
for (y = act_y1; y <= act_y2; y++) {
tft_fb[y * MONITOR_HOR_RES + x] = color32;
}
}
sdl_refr_qry = true;
}
/**
* Put a color map to the marked area
* @param x1 left coordinate
* @param y1 top coordinate
* @param x2 right coordinate
* @param y2 bottom coordinate
* @param color_p an array of colors
*/
void monitor_map(int32_t x1, int32_t y1, int32_t x2, int32_t y2,
const lv_color_t * color_p)
{
/*Return if the area is out the screen*/
if (x2 < 0)
return;
if (y2 < 0)
return;
if (x1 > MONITOR_HOR_RES - 1)
return;
if (y1 > MONITOR_VER_RES - 1)
return;
/*Truncate the area to the screen*/
int32_t act_x1 = x1 < 0 ? 0 : x1;
int32_t act_y1 = y1 < 0 ? 0 : y1;
int32_t act_x2 = x2 > MONITOR_HOR_RES - 1 ? MONITOR_HOR_RES - 1 : x2;
int32_t act_y2 = y2 > MONITOR_VER_RES - 1 ? MONITOR_VER_RES - 1 : y2;
int32_t x;
int32_t y;
for (y = act_y1; y <= act_y2; y++) {
for (x = act_x1; x <= act_x2; x++) {
tft_fb[y * MONITOR_HOR_RES + x] = color_p->full; //lv_color_to32(*color_p);
color_p++;
}
color_p += x2 - act_x2;
}
sdl_refr_qry = true;
}
void display_init(void)
{
}
void display_flush(int32_t x1, int32_t y1, int32_t x2, int32_t y2,
int32 color_p_offset)
{
wasm_module_inst_t module_inst = wasm_runtime_get_current_module_inst();
if (!wasm_runtime_validate_app_addr(module_inst, color_p_offset, 1))
return;
lv_color_t * color_p = wasm_runtime_addr_app_to_native(module_inst,
color_p_offset);
monitor_flush(x1, y1, x2, y2, color_p);
}
void display_fill(int32_t x1, int32_t y1, int32_t x2, int32_t y2,
lv_color_t color_p)
{
monitor_fill(x1, y1, x2, y2, color_p);
}
void display_map(int32_t x1, int32_t y1, int32_t x2, int32_t y2,
const lv_color_t * color_p)
{
monitor_map(x1, y1, x2, y2, color_p);
}
bool display_input_read(int32 data_p_offset)
{
wasm_module_inst_t module_inst = wasm_runtime_get_current_module_inst();
if (!wasm_runtime_validate_app_addr(module_inst, data_p_offset, 1))
return false;
lv_indev_data_t * data = wasm_runtime_addr_app_to_native(module_inst,
data_p_offset);
return mouse_read(data);
}
void display_deinit(void)
{
}
void display_vdb_write(int32 buf_offset, lv_coord_t buf_w, lv_coord_t x,
lv_coord_t y, int32 color_p_offset, lv_opa_t opa)
{
wasm_module_inst_t module_inst = wasm_runtime_get_current_module_inst();
if (!wasm_runtime_validate_app_addr(module_inst, color_p_offset, 1))
return;
lv_color_t *color = wasm_runtime_addr_app_to_native(module_inst,
color_p_offset);
void *buf = wasm_runtime_addr_app_to_native(module_inst, buf_offset);
unsigned char *buf_xy = buf + 4 * x + 4 * y * buf_w;
lv_color_t * temp = (lv_color_t *) buf_xy;
*temp = *color;
/*
if (opa != LV_OPA_COVER) {
lv_color_t mix_color;
mix_color.red = *buf_xy;
mix_color.green = *(buf_xy+1);
mix_color.blue = *(buf_xy+2);
color = lv_color_mix(color, mix_color, opa);
}
*/
/*
*buf_xy = color->red;
*(buf_xy + 1) = color->green;
*(buf_xy + 2) = color->blue;
*/
}
int monitor_sdl_refr_thread(void * param)
{
(void) param;
/*If not OSX initialize SDL in the Thread*/
monitor_sdl_init();
/*Run until quit event not arrives*/
while (sdl_quit_qry == false) {
/*Refresh handling*/
monitor_sdl_refr_core();
}
monitor_sdl_clean_up();
exit(0);
return 0;
}
extern void mouse_handler(SDL_Event *event);
void monitor_sdl_refr_core(void)
{
if (sdl_refr_qry != false) {
sdl_refr_qry = false;
SDL_UpdateTexture(texture, NULL, tft_fb,
MONITOR_HOR_RES * sizeof(uint32_t));
SDL_RenderClear(renderer);
/*Update the renderer with the texture containing the rendered image*/
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
}
SDL_Event event;
while (SDL_PollEvent(&event)) {
mouse_handler(&event);
if ((&event)->type == SDL_WINDOWEVENT) {
switch ((&event)->window.event) {
#if SDL_VERSION_ATLEAST(2, 0, 5)
case SDL_WINDOWEVENT_TAKE_FOCUS:
#endif
case SDL_WINDOWEVENT_EXPOSED:
SDL_UpdateTexture(texture, NULL, tft_fb,
MONITOR_HOR_RES * sizeof(uint32_t));
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
break;
default:
break;
}
}
}
/*Sleep some time*/
SDL_Delay(SDL_REFR_PERIOD);
}
int quit_filter(void * userdata, SDL_Event * event)
{
(void) userdata;
if (event->type == SDL_QUIT) {
sdl_quit_qry = true;
}
return 1;
}
void monitor_sdl_clean_up(void)
{
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}
void monitor_sdl_init(void)
{
/*Initialize the SDL*/
SDL_Init(SDL_INIT_VIDEO);
SDL_SetEventFilter(quit_filter, NULL);
window = SDL_CreateWindow("TFT Simulator", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
MONITOR_HOR_RES * MONITOR_ZOOM, MONITOR_VER_RES * MONITOR_ZOOM, 0); /*last param. SDL_WINDOW_BORDERLESS to hide borders*/
renderer = SDL_CreateRenderer(window, -1, 0);
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STATIC, MONITOR_HOR_RES, MONITOR_VER_RES);
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
/*Initialize the frame buffer to gray (77 is an empirical value) */
memset(tft_fb, 0x44, MONITOR_HOR_RES * MONITOR_VER_RES * sizeof(uint32_t));
SDL_UpdateTexture(texture, NULL, tft_fb,
MONITOR_HOR_RES * sizeof(uint32_t));
sdl_refr_qry = true;
sdl_inited = true;
}
void display_SDL_init()
{
SDL_CreateThread(monitor_sdl_refr_thread, "sdl_refr", NULL);
while (sdl_inited == false)
; /*Wait until 'sdl_refr' initializes the SDL*/
}

View File

@ -0,0 +1,467 @@
#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[1024 * 1024] = { 0 };
static void showUsage()
{
#ifndef CONNECTION_UART
printf("Usage:\n");
printf("\nWork as TCP server mode:\n");
printf("\tvgl_wasm_runtime -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("\tvgl_wasm_runtime -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("\tvgl_wasm_runtime -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;
}
extern void display_SDL_init();
display_SDL_init();
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,20 @@
/*
* 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.
*/
extern void iwasm_main(int argc, char *argv[]);
int main(int argc, char *argv[])
{
iwasm_main(argc,argv);
}

View File

@ -0,0 +1,96 @@
/**
* @file mouse.c
*
*/
/*********************
* INCLUDES
*********************/
#include "display_indev.h"
#include "SDL2/SDL.h"
#if USE_MOUSE != 0
/*********************
* DEFINES
*********************/
#ifndef MONITOR_ZOOM
#define MONITOR_ZOOM 1
#endif
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
/**********************
* STATIC VARIABLES
**********************/
static bool left_button_down = false;
static int16_t last_x = 0;
static int16_t last_y = 0;
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
/**
* Initialize the mouse
*/
void mouse_init(void)
{
}
/**
* Get the current position and state of the mouse
* @param data store the mouse data here
* @return false: because the points are not buffered, so no more data to be read
*/
bool mouse_read(lv_indev_data_t * data)
{
/*Store the collected data*/
data->point.x = last_x;
data->point.y = last_y;
data->state = left_button_down ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL;
return false;
}
/**
* It will be called from the main SDL thread
*/
void mouse_handler(SDL_Event * event)
{
switch (event->type) {
case SDL_MOUSEBUTTONUP:
if (event->button.button == SDL_BUTTON_LEFT)
left_button_down = false;
break;
case SDL_MOUSEBUTTONDOWN:
if (event->button.button == SDL_BUTTON_LEFT) {
left_button_down = true;
last_x = event->motion.x / MONITOR_ZOOM;
last_y = event->motion.y / MONITOR_ZOOM;
}
break;
case SDL_MOUSEMOTION:
last_x = event->motion.x / MONITOR_ZOOM;
last_y = event->motion.y / MONITOR_ZOOM;
break;
}
}
/**********************
* STATIC FUNCTIONS
**********************/
#endif