Enable AoT and wamr-sdk, and change arguments of call wasm API (#157)
* Implement memory profiler, optimize memory usage, modify code indent * Implement memory.grow and limit heap space base offset to 1G; modify iwasm build type to Release and 64 bit by default * Add a new extension library: connection * Fix bug of reading magic number and version in big endian platform * Re-org platform APIs: move most platform APIs from iwasm to shared-lib * Enhance wasm loader to fix some security issues * Fix issue about illegal load of EXC_RETURN into PC on stm32 board * Updates that let a restricted version of the interpreter run in SGX * Enable native/app address validation and conversion for wasm app * Remove wasm_application_exectue_* APIs from wasm_export.h which makes confused * Refine binary size and fix several minor issues Optimize interpreter LOAD/STORE opcodes to decrease the binary size Fix issues when using iwasm library: _bh_log undefined, bh_memory.h not found Remove unused _stdin/_stdout/_stderr global variables resolve in libc wrapper Add macros of global heap size, stack size, heap size for Zephyr main.c Clear compile warning of wasm_application.c * Add more strict security checks for libc wrapper API's * Use one libc wrapper copy for sgx and other platforms; remove bh_printf macro for other platform header files * Enhance security of libc strcpy/sprintf wrapper function * Fix issue of call native for x86_64/arm/mips, add module inst parameter for native wrapper functions * Remove get_module_inst() and fix issue of call native * Refine wgl lib: remove module_inst parameter from widget functions; move function index check to runtime instantiate * Refine interpreter call native process, refine memory boudary check * Fix issues of invokeNative function of arm/mips/general version * Add a switch to build simple sample without gui support * Add BUILD_TARGET setting in makefile to replace cpu compiler flags in source code * Re-org shared lib header files, remove unused info; fix compile issues of vxworks * Add build target general * Remove unused files * Update license header * test push * Restore file * Sync up with internal/feature * Sync up with internal/feature * Rename build_wamr_app to build_wasm_app * Fix small issues of README * Enhance malformed wasm file checking Fix issue of print hex int and implement utf8 string check Fix wasi file read/write right issue Fix minor issue of build wasm app doc * Sync up with internal/feature * Sync up with internal/feature: fix interpreter arm issue, fix read leb issue * Sync up with internal/feature * Fix bug of config.h and rename wasi config.h to ssp_config.h * Sync up with internal/feature * Import wamr aot * update document * update document * Update document, disable WASI in 32bit * update document * remove files * update document * Update document * update document * update document * update samples * Sync up with internal repo
This commit is contained in:
101
core/app-framework/base/app/bh_platform.c
Normal file
101
core/app-framework/base/app/bh_platform.c
Normal file
@ -0,0 +1,101 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include "bh_platform.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/*
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
static bool is_little_endian()
|
||||
{
|
||||
long i = 0x01020304;
|
||||
unsigned char* c = (unsigned char*) &i;
|
||||
return (*c == 0x04) ? true : false;
|
||||
}
|
||||
|
||||
static void swap32(uint8* pData)
|
||||
{
|
||||
uint8 value = *pData;
|
||||
*pData = *(pData + 3);
|
||||
*(pData + 3) = value;
|
||||
|
||||
value = *(pData + 1);
|
||||
*(pData + 1) = *(pData + 2);
|
||||
*(pData + 2) = value;
|
||||
}
|
||||
|
||||
static void swap16(uint8* pData)
|
||||
{
|
||||
uint8 value = *pData;
|
||||
*(pData) = *(pData + 1);
|
||||
*(pData + 1) = value;
|
||||
}
|
||||
|
||||
uint32 htonl(uint32 value)
|
||||
{
|
||||
uint32 ret;
|
||||
if (is_little_endian()) {
|
||||
ret = value;
|
||||
swap32((uint8*) &ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
uint32 ntohl(uint32 value)
|
||||
{
|
||||
return htonl(value);
|
||||
}
|
||||
|
||||
uint16 htons(uint16 value)
|
||||
{
|
||||
uint16 ret;
|
||||
if (is_little_endian()) {
|
||||
ret = value;
|
||||
swap16((uint8 *)&ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
uint16 ntohs(uint16 value)
|
||||
{
|
||||
return htons(value);
|
||||
}
|
||||
|
||||
char *wa_strdup(const char *s)
|
||||
{
|
||||
char *s1 = NULL;
|
||||
if (s && (s1 = wa_malloc(strlen(s) + 1)))
|
||||
memcpy(s1, s, strlen(s) + 1);
|
||||
return s1;
|
||||
}
|
||||
|
||||
#define RSIZE_MAX 0x7FFFFFFF
|
||||
int b_memcpy_s(void * s1, unsigned int s1max, const void * s2, unsigned int n)
|
||||
{
|
||||
char *dest = (char*) s1;
|
||||
char *src = (char*) s2;
|
||||
if (n == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (s1 == NULL || s1max > RSIZE_MAX) {
|
||||
return -1;
|
||||
}
|
||||
if (s2 == NULL || n > s1max) {
|
||||
memset(dest, 0, s1max);
|
||||
return -1;
|
||||
}
|
||||
memcpy(dest, src, n);
|
||||
return 0;
|
||||
}
|
||||
48
core/app-framework/base/app/bh_platform.h
Executable file
48
core/app-framework/base/app/bh_platform.h
Executable file
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#ifndef DEPS_IWASM_APP_LIBS_BASE_BH_PLATFORM_H_
|
||||
#define DEPS_IWASM_APP_LIBS_BASE_BH_PLATFORM_H_
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef unsigned char uint8;
|
||||
typedef char int8;
|
||||
typedef unsigned short uint16;
|
||||
typedef short int16;
|
||||
typedef unsigned int uint32;
|
||||
typedef int int32;
|
||||
|
||||
#ifndef NULL
|
||||
# define NULL ((void*) 0)
|
||||
#endif
|
||||
|
||||
#ifndef __cplusplus
|
||||
#define true 1
|
||||
#define false 0
|
||||
#define inline __inline
|
||||
#endif
|
||||
|
||||
// all wasm-app<->native shared source files should use wa_malloc/wa_free.
|
||||
// they will be mapped to different implementations in each side
|
||||
#ifndef wa_malloc
|
||||
#define wa_malloc malloc
|
||||
#endif
|
||||
|
||||
#ifndef wa_free
|
||||
#define wa_free free
|
||||
#endif
|
||||
|
||||
char *wa_strdup(const char *s);
|
||||
|
||||
uint32 htonl(uint32 value);
|
||||
uint32 ntohl(uint32 value);
|
||||
uint16 htons(uint16 value);
|
||||
uint16 ntohs(uint16 value);
|
||||
|
||||
int
|
||||
b_memcpy_s(void * s1, unsigned int s1max, const void * s2, unsigned int n);
|
||||
|
||||
#endif /* DEPS_IWASM_APP_LIBS_BASE_BH_PLATFORM_H_ */
|
||||
32
core/app-framework/base/app/req_resp_api.h
Normal file
32
core/app-framework/base/app/req_resp_api.h
Normal file
@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#ifndef _REQ_RESP_API_H_
|
||||
#define _REQ_RESP_API_H_
|
||||
|
||||
#include "bh_platform.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
bool
|
||||
wasm_response_send(const char *buf, int size);
|
||||
|
||||
void
|
||||
wasm_register_resource(const char *url);
|
||||
|
||||
void
|
||||
wasm_post_request(const char *buf, int size);
|
||||
|
||||
void
|
||||
wasm_sub_event(const char *url);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* end of _REQ_RESP_API_H_ */
|
||||
|
||||
349
core/app-framework/base/app/request.c
Normal file
349
core/app-framework/base/app/request.c
Normal file
@ -0,0 +1,349 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include "bi-inc/attr_container.h"
|
||||
#include "wa-inc/request.h"
|
||||
#include "wa-inc/timer_wasm_app.h"
|
||||
#include "bi-inc/shared_utils.h"
|
||||
#include "wasm_app.h"
|
||||
#include "req_resp_api.h"
|
||||
#include "timer_api.h"
|
||||
|
||||
#define TRANSACTION_TIMEOUT_MS 5000
|
||||
|
||||
typedef enum {
|
||||
Reg_Event, Reg_Request
|
||||
} reg_type_t;
|
||||
|
||||
typedef struct _res_register {
|
||||
struct _res_register *next;
|
||||
const char * url;
|
||||
reg_type_t reg_type;
|
||||
void (*request_handler)(request_t *);
|
||||
} res_register_t;
|
||||
|
||||
typedef struct transaction {
|
||||
struct transaction *next;
|
||||
int mid;
|
||||
unsigned int time; /* start time */
|
||||
response_handler_f handler;
|
||||
void *user_data;
|
||||
} transaction_t;
|
||||
|
||||
static res_register_t * g_resources = NULL;
|
||||
|
||||
static transaction_t *g_transactions = NULL;
|
||||
|
||||
static user_timer_t g_trans_timer = NULL;
|
||||
|
||||
static transaction_t *transaction_find(int mid)
|
||||
{
|
||||
transaction_t *t = g_transactions;
|
||||
|
||||
while (t) {
|
||||
if (t->mid == mid)
|
||||
return t;
|
||||
t = t->next;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* new transaction is added to the tail of the list, so the list
|
||||
* is sorted by expiry time naturally.
|
||||
*/
|
||||
static void transaction_add(transaction_t *trans)
|
||||
{
|
||||
transaction_t *t;
|
||||
|
||||
if (g_transactions == NULL) {
|
||||
g_transactions = trans;
|
||||
return;
|
||||
}
|
||||
|
||||
t = g_transactions;
|
||||
while (t) {
|
||||
if (t->next == NULL) {
|
||||
t->next = trans;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void transaction_remove(transaction_t *trans)
|
||||
{
|
||||
transaction_t *prev = NULL, *current = g_transactions;
|
||||
|
||||
while (current) {
|
||||
if (current == trans) {
|
||||
if (prev == NULL) {
|
||||
g_transactions = current->next;
|
||||
free(current);
|
||||
return;
|
||||
}
|
||||
prev->next = current->next;
|
||||
free(current);
|
||||
return;
|
||||
}
|
||||
prev = current;
|
||||
current = current->next;
|
||||
}
|
||||
}
|
||||
|
||||
static bool is_event_type(request_t * req)
|
||||
{
|
||||
return req->action == COAP_EVENT;
|
||||
}
|
||||
|
||||
static bool register_url_handler(const char *url,
|
||||
request_handler_f request_handler, reg_type_t reg_type)
|
||||
{
|
||||
res_register_t * r = g_resources;
|
||||
|
||||
while (r) {
|
||||
if (reg_type == r->reg_type && strcmp(r->url, url) == 0) {
|
||||
r->request_handler = request_handler;
|
||||
return true;
|
||||
}
|
||||
r = r->next;
|
||||
}
|
||||
|
||||
r = (res_register_t *) malloc(sizeof(res_register_t));
|
||||
if (r == NULL)
|
||||
return false;
|
||||
|
||||
memset(r, 0, sizeof(*r));
|
||||
|
||||
r->url = strdup(url);
|
||||
if (!r->url) {
|
||||
free(r);
|
||||
return false;
|
||||
}
|
||||
|
||||
r->request_handler = request_handler;
|
||||
r->reg_type = reg_type;
|
||||
r->next = g_resources;
|
||||
g_resources = r;
|
||||
|
||||
// tell app mgr to route this url to me
|
||||
if (reg_type == Reg_Request)
|
||||
wasm_register_resource(url);
|
||||
else
|
||||
wasm_sub_event(url);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool api_register_resource_handler(const char *url,
|
||||
request_handler_f request_handler)
|
||||
{
|
||||
return register_url_handler(url, request_handler, Reg_Request);
|
||||
}
|
||||
|
||||
static void transaction_timeout_handler(user_timer_t timer)
|
||||
{
|
||||
transaction_t *cur, *expired = NULL;
|
||||
unsigned int elpased_ms, now = wasm_get_sys_tick_ms();
|
||||
|
||||
/*
|
||||
* Since he transaction list is sorted by expiry time naturally,
|
||||
* we can easily get all expired transactions.
|
||||
* */
|
||||
cur = g_transactions;
|
||||
while (cur) {
|
||||
if (now < cur->time)
|
||||
elpased_ms = now + (0xFFFFFFFF - cur->time) + 1;
|
||||
else
|
||||
elpased_ms = now - cur->time;
|
||||
|
||||
if (elpased_ms >= TRANSACTION_TIMEOUT_MS) {
|
||||
g_transactions = cur->next;
|
||||
cur->next = expired;
|
||||
expired = cur;
|
||||
cur = g_transactions;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* call each transaction's handler with response set to NULL */
|
||||
cur = expired;
|
||||
while (cur) {
|
||||
transaction_t *tmp = cur;
|
||||
cur->handler(NULL, cur->user_data);
|
||||
cur = cur->next;
|
||||
free(tmp);
|
||||
}
|
||||
|
||||
/*
|
||||
* If the transaction list is not empty, restart the timer according
|
||||
* to the first transaction. Otherwise, stop the timer.
|
||||
*/
|
||||
if (g_transactions != NULL) {
|
||||
unsigned int elpased_ms, ms_to_expiry, now = wasm_get_sys_tick_ms();
|
||||
if (now < g_transactions->time) {
|
||||
elpased_ms = now + (0xFFFFFFFF - g_transactions->time) + 1;
|
||||
} else {
|
||||
elpased_ms = now - g_transactions->time;
|
||||
}
|
||||
ms_to_expiry = TRANSACTION_TIMEOUT_MS - elpased_ms;
|
||||
api_timer_restart(g_trans_timer, ms_to_expiry);
|
||||
} else {
|
||||
api_timer_cancel(g_trans_timer);
|
||||
g_trans_timer = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void api_send_request(request_t * request, response_handler_f response_handler,
|
||||
void * user_data)
|
||||
{
|
||||
int size;
|
||||
char *buffer;
|
||||
transaction_t *trans;
|
||||
|
||||
if ((trans = (transaction_t *) malloc(sizeof(transaction_t))) == NULL) {
|
||||
printf(
|
||||
"send request: allocate memory for request transaction failed!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
memset(trans, 0, sizeof(transaction_t));
|
||||
trans->handler = response_handler;
|
||||
trans->mid = request->mid;
|
||||
trans->time = wasm_get_sys_tick_ms();
|
||||
trans->user_data = user_data;
|
||||
|
||||
if ((buffer = pack_request(request, &size)) == NULL) {
|
||||
printf("send request: pack request failed!\n");
|
||||
free(trans);
|
||||
return;
|
||||
}
|
||||
|
||||
transaction_add(trans);
|
||||
|
||||
/* if the trans is the 1st one, start the timer */
|
||||
if (trans == g_transactions) {
|
||||
/* assert(g_trans_timer == NULL); */
|
||||
if (g_trans_timer == NULL) {
|
||||
g_trans_timer = api_timer_create(TRANSACTION_TIMEOUT_MS,
|
||||
false,
|
||||
true, transaction_timeout_handler);
|
||||
}
|
||||
}
|
||||
|
||||
wasm_post_request(buffer, size);
|
||||
|
||||
free_req_resp_packet(buffer);
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
* APIs for the native layers to callback for request/response arrived to this app
|
||||
*
|
||||
*/
|
||||
|
||||
void on_response(char * buffer, int size)
|
||||
{
|
||||
response_t response[1];
|
||||
transaction_t *trans;
|
||||
|
||||
if (NULL == unpack_response(buffer, size, response)) {
|
||||
printf("unpack response failed\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if ((trans = transaction_find(response->mid)) == NULL) {
|
||||
printf("cannot find the transaction\n");
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* When the 1st transaction get response:
|
||||
* 1. If the 2nd trans exist, restart the timer according to its expiry time;
|
||||
* 2. Otherwise, stop the timer since there is no more transactions;
|
||||
*/
|
||||
if (trans == g_transactions) {
|
||||
if (trans->next != NULL) {
|
||||
unsigned int elpased_ms, ms_to_expiry, now = wasm_get_sys_tick_ms();
|
||||
if (now < trans->next->time) {
|
||||
elpased_ms = now + (0xFFFFFFFF - trans->next->time) + 1;
|
||||
} else {
|
||||
elpased_ms = now - trans->next->time;
|
||||
}
|
||||
ms_to_expiry = TRANSACTION_TIMEOUT_MS - elpased_ms;
|
||||
api_timer_restart(g_trans_timer, ms_to_expiry);
|
||||
} else {
|
||||
api_timer_cancel(g_trans_timer);
|
||||
g_trans_timer = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
trans->handler(response, trans->user_data);
|
||||
transaction_remove(trans);
|
||||
}
|
||||
|
||||
void on_request(char *buffer, int size)
|
||||
{
|
||||
request_t request[1];
|
||||
bool is_event;
|
||||
res_register_t *r = g_resources;
|
||||
|
||||
if (NULL == unpack_request(buffer, size, request)) {
|
||||
printf("unpack request failed\n");
|
||||
return;
|
||||
}
|
||||
|
||||
is_event = is_event_type(request);
|
||||
|
||||
while (r) {
|
||||
if ((is_event && r->reg_type == Reg_Event)
|
||||
|| (!is_event && r->reg_type == Reg_Request)) {
|
||||
if (check_url_start(request->url, strlen(request->url), r->url)
|
||||
> 0) {
|
||||
r->request_handler(request);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
r = r->next;
|
||||
}
|
||||
|
||||
printf("on_request: exit. no service handler\n");
|
||||
}
|
||||
|
||||
void api_response_send(response_t *response)
|
||||
{
|
||||
int size;
|
||||
char * buffer = pack_response(response, &size);
|
||||
if (buffer == NULL)
|
||||
return;
|
||||
|
||||
wasm_response_send(buffer, size);
|
||||
free_req_resp_packet(buffer);
|
||||
}
|
||||
|
||||
/// event api
|
||||
|
||||
bool api_publish_event(const char *url, int fmt, void *payload, int payload_len)
|
||||
{
|
||||
int size;
|
||||
request_t request[1];
|
||||
init_request(request, (char *)url, COAP_EVENT, fmt, payload, payload_len);
|
||||
char * buffer = pack_request(request, &size);
|
||||
if (buffer == NULL)
|
||||
return false;
|
||||
wasm_post_request(buffer, size);
|
||||
|
||||
free_req_resp_packet(buffer);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool api_subscribe_event(const char * url, request_handler_f handler)
|
||||
{
|
||||
return register_url_handler(url, handler, Reg_Event);
|
||||
}
|
||||
|
||||
95
core/app-framework/base/app/timer.c
Normal file
95
core/app-framework/base/app/timer.c
Normal file
@ -0,0 +1,95 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "wa-inc/timer_wasm_app.h"
|
||||
#include "timer_api.h"
|
||||
|
||||
#if 1
|
||||
#include <stdio.h>
|
||||
#else
|
||||
#define printf (...)
|
||||
#endif
|
||||
|
||||
struct user_timer {
|
||||
struct user_timer * next;
|
||||
int timer_id;
|
||||
void (*user_timer_callback)(user_timer_t);
|
||||
};
|
||||
|
||||
struct user_timer * g_timers = NULL;
|
||||
|
||||
user_timer_t api_timer_create(int interval, bool is_period, bool auto_start,
|
||||
on_user_timer_update_f on_timer_update)
|
||||
{
|
||||
|
||||
int timer_id = wasm_create_timer(interval, is_period, auto_start);
|
||||
|
||||
//TODO
|
||||
struct user_timer * timer = (struct user_timer *) malloc(
|
||||
sizeof(struct user_timer));
|
||||
if (timer == NULL) {
|
||||
// TODO: remove the timer_id
|
||||
printf("### api_timer_create malloc faild!!! \n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(timer, 0, sizeof(*timer));
|
||||
timer->timer_id = timer_id;
|
||||
timer->user_timer_callback = on_timer_update;
|
||||
|
||||
if (g_timers == NULL)
|
||||
g_timers = timer;
|
||||
else {
|
||||
timer->next = g_timers;
|
||||
g_timers = timer;
|
||||
}
|
||||
|
||||
return timer;
|
||||
}
|
||||
|
||||
void api_timer_cancel(user_timer_t timer)
|
||||
{
|
||||
user_timer_t t = g_timers, prev = NULL;
|
||||
|
||||
wasm_timer_cancel(timer->timer_id);
|
||||
|
||||
while (t) {
|
||||
if (t == timer) {
|
||||
if (prev == NULL) {
|
||||
g_timers = t->next;
|
||||
free(t);
|
||||
} else {
|
||||
prev->next = t->next;
|
||||
free(t);
|
||||
}
|
||||
return;
|
||||
} else {
|
||||
prev = t;
|
||||
t = t->next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void api_timer_restart(user_timer_t timer, int interval)
|
||||
{
|
||||
wasm_timer_restart(timer->timer_id, interval);
|
||||
}
|
||||
|
||||
void on_timer_callback(int timer_id)
|
||||
{
|
||||
struct user_timer * t = g_timers;
|
||||
|
||||
while (t) {
|
||||
if (t->timer_id == timer_id) {
|
||||
t->user_timer_callback(t);
|
||||
break;
|
||||
}
|
||||
t = t->next;
|
||||
}
|
||||
}
|
||||
|
||||
37
core/app-framework/base/app/timer_api.h
Normal file
37
core/app-framework/base/app/timer_api.h
Normal file
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#ifndef _TIMER_API_H_
|
||||
#define _TIMER_API_H_
|
||||
|
||||
#include "bh_platform.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef unsigned int timer_id_t;
|
||||
|
||||
timer_id_t
|
||||
wasm_create_timer(int interval, bool is_period, bool auto_start);
|
||||
|
||||
void
|
||||
wasm_timer_destroy(timer_id_t timer_id);
|
||||
|
||||
void
|
||||
wasm_timer_cancel(timer_id_t timer_id);
|
||||
|
||||
void
|
||||
wasm_timer_restart(timer_id_t timer_id, int interval);
|
||||
|
||||
uint32
|
||||
wasm_get_sys_tick_ms(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* end of _TIMER_API_H_ */
|
||||
|
||||
170
core/app-framework/base/app/wa-inc/request.h
Normal file
170
core/app-framework/base/app/wa-inc/request.h
Normal file
@ -0,0 +1,170 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#ifndef _AEE_REQUEST_H_
|
||||
#define _AEE_REQUEST_H_
|
||||
|
||||
#include "bi-inc/shared_utils.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
/* CoAP request method codes */
|
||||
typedef enum {
|
||||
COAP_GET = 1,
|
||||
COAP_POST,
|
||||
COAP_PUT,
|
||||
COAP_DELETE,
|
||||
COAP_EVENT = (COAP_DELETE + 2)
|
||||
} coap_method_t;
|
||||
|
||||
/* CoAP response codes */
|
||||
typedef enum {
|
||||
NO_ERROR = 0,
|
||||
|
||||
CREATED_2_01 = 65, /* CREATED */
|
||||
DELETED_2_02 = 66, /* DELETED */
|
||||
VALID_2_03 = 67, /* NOT_MODIFIED */
|
||||
CHANGED_2_04 = 68, /* CHANGED */
|
||||
CONTENT_2_05 = 69, /* OK */
|
||||
CONTINUE_2_31 = 95, /* CONTINUE */
|
||||
|
||||
BAD_REQUEST_4_00 = 128, /* BAD_REQUEST */
|
||||
UNAUTHORIZED_4_01 = 129, /* UNAUTHORIZED */
|
||||
BAD_OPTION_4_02 = 130, /* BAD_OPTION */
|
||||
FORBIDDEN_4_03 = 131, /* FORBIDDEN */
|
||||
NOT_FOUND_4_04 = 132, /* NOT_FOUND */
|
||||
METHOD_NOT_ALLOWED_4_05 = 133, /* METHOD_NOT_ALLOWED */
|
||||
NOT_ACCEPTABLE_4_06 = 134, /* NOT_ACCEPTABLE */
|
||||
PRECONDITION_FAILED_4_12 = 140, /* BAD_REQUEST */
|
||||
REQUEST_ENTITY_TOO_LARGE_4_13 = 141, /* REQUEST_ENTITY_TOO_LARGE */
|
||||
UNSUPPORTED_MEDIA_TYPE_4_15 = 143, /* UNSUPPORTED_MEDIA_TYPE */
|
||||
|
||||
INTERNAL_SERVER_ERROR_5_00 = 160, /* INTERNAL_SERVER_ERROR */
|
||||
NOT_IMPLEMENTED_5_01 = 161, /* NOT_IMPLEMENTED */
|
||||
BAD_GATEWAY_5_02 = 162, /* BAD_GATEWAY */
|
||||
SERVICE_UNAVAILABLE_5_03 = 163, /* SERVICE_UNAVAILABLE */
|
||||
GATEWAY_TIMEOUT_5_04 = 164, /* GATEWAY_TIMEOUT */
|
||||
PROXYING_NOT_SUPPORTED_5_05 = 165, /* PROXYING_NOT_SUPPORTED */
|
||||
|
||||
/* Erbium errors */
|
||||
MEMORY_ALLOCATION_ERROR = 192, PACKET_SERIALIZATION_ERROR,
|
||||
|
||||
/* Erbium hooks */
|
||||
MANUAL_RESPONSE, PING_RESPONSE
|
||||
} coap_status_t;
|
||||
|
||||
|
||||
/**
|
||||
* @typedef request_handler_f
|
||||
*
|
||||
* @brief Define the signature of callback function for API
|
||||
* api_register_resource_handler() to handle request or for API
|
||||
* api_subscribe_event() to handle event.
|
||||
*
|
||||
* @param request pointer of the request to be handled
|
||||
*
|
||||
* @see api_register_resource_handler
|
||||
* @see api_subscribe_event
|
||||
*/
|
||||
typedef void (*request_handler_f)(request_t *request);
|
||||
|
||||
/**
|
||||
* @typedef response_handler_f
|
||||
*
|
||||
* @brief Define the signature of callback function for API
|
||||
* api_send_request() to handle response of a request.
|
||||
*
|
||||
* @param response pointer of the response to be handled
|
||||
* @param user_data user data associated with the request which is set when
|
||||
* calling api_send_request().
|
||||
*
|
||||
* @see api_send_request
|
||||
*/
|
||||
typedef void (*response_handler_f)(response_t *response, void *user_data);
|
||||
|
||||
|
||||
/*
|
||||
*****************
|
||||
* Request APIs
|
||||
*****************
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Register resource.
|
||||
*
|
||||
* @param url url of the resource
|
||||
* @param handler callback function to handle the request to the resource
|
||||
*
|
||||
* @return true if success, false otherwise
|
||||
*/
|
||||
bool api_register_resource_handler(const char *url, request_handler_f handler);
|
||||
|
||||
/**
|
||||
* @brief Send request asynchronously.
|
||||
*
|
||||
* @param request pointer of the request to be sent
|
||||
* @param response_handler callback function to handle the response
|
||||
* @param user_data user data
|
||||
*/
|
||||
void api_send_request(request_t * request, response_handler_f response_handler,
|
||||
void * user_data);
|
||||
|
||||
/**
|
||||
* @brief Send response.
|
||||
*
|
||||
* @param response pointer of the response to be sent
|
||||
*
|
||||
* @par
|
||||
* @code
|
||||
* void res1_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);
|
||||
* }
|
||||
* @endcode
|
||||
*/
|
||||
void api_response_send(response_t *response);
|
||||
|
||||
|
||||
/*
|
||||
*****************
|
||||
* Event APIs
|
||||
*****************
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Publish an event.
|
||||
*
|
||||
* @param url url of the event
|
||||
* @param fmt format of the event payload
|
||||
* @param payload payload of the event
|
||||
* @param payload_len length in bytes of the event payload
|
||||
*
|
||||
* @return true if success, false otherwise
|
||||
*/
|
||||
bool api_publish_event(const char *url, int fmt, void *payload,
|
||||
int payload_len);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Subscribe an event.
|
||||
*
|
||||
* @param url url of the event
|
||||
* @param handler callback function to handle the event.
|
||||
*
|
||||
* @return true if success, false otherwise
|
||||
*/
|
||||
bool api_subscribe_event(const char * url, request_handler_f handler);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
68
core/app-framework/base/app/wa-inc/timer_wasm_app.h
Normal file
68
core/app-framework/base/app/wa-inc/timer_wasm_app.h
Normal file
@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#ifndef _AEE_TIMER_H_
|
||||
#define _AEE_TIMER_H_
|
||||
|
||||
#include "bh_platform.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* board producer define user_timer */
|
||||
struct user_timer;
|
||||
typedef struct user_timer * user_timer_t;
|
||||
|
||||
/**
|
||||
* @typedef on_user_timer_update_f
|
||||
*
|
||||
* @brief Define the signature of callback function for API api_timer_create().
|
||||
*
|
||||
* @param timer the timer
|
||||
*
|
||||
* @see api_timer_create
|
||||
*/
|
||||
typedef void (*on_user_timer_update_f)(user_timer_t timer);
|
||||
|
||||
/*
|
||||
*****************
|
||||
* Timer APIs
|
||||
*****************
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Create timer.
|
||||
*
|
||||
* @param interval timer interval
|
||||
* @param is_period whether the timer is periodic
|
||||
* @param auto_start whether start the timer immediately after created
|
||||
* @param on_timer_update callback function called when timer expired
|
||||
*
|
||||
* @return the timer created if success, NULL otherwise
|
||||
*/
|
||||
user_timer_t api_timer_create(int interval, bool is_period, bool auto_start,
|
||||
on_user_timer_update_f on_timer_update);
|
||||
|
||||
/**
|
||||
* @brief Cancel timer.
|
||||
*
|
||||
* @param timer the timer to cancel
|
||||
*/
|
||||
void api_timer_cancel(user_timer_t timer);
|
||||
|
||||
/**
|
||||
* @brief Restart timer.
|
||||
*
|
||||
* @param timer the timer to cancel
|
||||
* @param interval the timer interval
|
||||
*/
|
||||
void api_timer_restart(user_timer_t timer, int interval);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
13
core/app-framework/base/app/wasm_app.cmake
Normal file
13
core/app-framework/base/app/wasm_app.cmake
Normal file
@ -0,0 +1,13 @@
|
||||
# Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
set (WASM_APP_BASE_DIR ${CMAKE_CURRENT_LIST_DIR})
|
||||
|
||||
include_directories(${WASM_APP_BASE_DIR})
|
||||
|
||||
add_definitions (-DWASM_ENABLE_BASE_LIB)
|
||||
|
||||
file (GLOB_RECURSE source_all ${WASM_APP_BASE_DIR}/*.c)
|
||||
|
||||
set (WASM_APP_CURRENT_SOURCE ${source_all})
|
||||
set (WASM_APP_BASE_DIR ${WASM_APP_BASE_DIR} PARENT_SCOPE)
|
||||
21
core/app-framework/base/app/wasm_app.h
Normal file
21
core/app-framework/base/app/wasm_app.h
Normal file
@ -0,0 +1,21 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#ifndef _LIB_AEE_H_
|
||||
#define _LIB_AEE_H_
|
||||
|
||||
#include "bi-inc/shared_utils.h"
|
||||
#include "bi-inc/attr_container.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* end of _LIB_AEE_H_ */
|
||||
31
core/app-framework/base/native/base_lib_export.c
Normal file
31
core/app-framework/base/native/base_lib_export.c
Normal file
@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "lib_export.h"
|
||||
#include "base_lib_export.h"
|
||||
|
||||
static NativeSymbol extended_native_symbol_defs[] = {
|
||||
/* TODO: use macro EXPORT_WASM_API() or EXPORT_WASM_API2() to
|
||||
add functions to register. */
|
||||
EXPORT_WASM_API(wasm_register_resource),
|
||||
EXPORT_WASM_API(wasm_response_send),
|
||||
EXPORT_WASM_API(wasm_post_request),
|
||||
EXPORT_WASM_API(wasm_sub_event),
|
||||
EXPORT_WASM_API(wasm_create_timer),
|
||||
EXPORT_WASM_API(wasm_timer_destroy),
|
||||
EXPORT_WASM_API(wasm_timer_cancel),
|
||||
EXPORT_WASM_API(wasm_timer_restart),
|
||||
EXPORT_WASM_API(wasm_get_sys_tick_ms),
|
||||
};
|
||||
|
||||
int get_base_lib_export_apis(NativeSymbol **p_base_lib_apis)
|
||||
{
|
||||
*p_base_lib_apis = extended_native_symbol_defs;
|
||||
return sizeof(extended_native_symbol_defs) / sizeof(NativeSymbol);
|
||||
}
|
||||
|
||||
13
core/app-framework/base/native/base_lib_export.h
Normal file
13
core/app-framework/base/native/base_lib_export.h
Normal file
@ -0,0 +1,13 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#ifndef _BASE_LIB_EXPORT_H_
|
||||
#define _BASE_LIB_EXPORT_H_
|
||||
|
||||
#include "bi-inc/attr_container.h"
|
||||
#include "native_interface.h"
|
||||
|
||||
#endif /* end of _BASE_LIB_EXPORT_H_ */
|
||||
|
||||
32
core/app-framework/base/native/req_resp_api.h
Normal file
32
core/app-framework/base/native/req_resp_api.h
Normal file
@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#ifndef _REQ_RESP_API_H_
|
||||
#define _REQ_RESP_API_H_
|
||||
|
||||
#include "bh_platform.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
bool
|
||||
wasm_response_send(int32 buf_offset, int size);
|
||||
|
||||
void
|
||||
wasm_register_resource(int32 url_offset);
|
||||
|
||||
void
|
||||
wasm_post_request(int32 buf_offset, int size);
|
||||
|
||||
void
|
||||
wasm_sub_event(int32 url_offset);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* end of _REQ_RESP_API_H_ */
|
||||
|
||||
112
core/app-framework/base/native/request_response.c
Normal file
112
core/app-framework/base/native/request_response.c
Normal file
@ -0,0 +1,112 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include "app_manager_export.h"
|
||||
#include "coap_ext.h"
|
||||
#include "wasm_export.h"
|
||||
#include "bh_assert.h"
|
||||
|
||||
extern void module_request_handler(request_t *request, void *user_data);
|
||||
|
||||
bool
|
||||
wasm_response_send(wasm_exec_env_t exec_env,
|
||||
int32 buffer_offset, int size)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst(exec_env);
|
||||
char *buffer = NULL;
|
||||
|
||||
if (!validate_app_addr(buffer_offset, size))
|
||||
return false;
|
||||
|
||||
buffer = addr_app_to_native(buffer_offset);
|
||||
|
||||
if (buffer != NULL) {
|
||||
response_t response[1];
|
||||
|
||||
if (NULL == unpack_response(buffer, size, response))
|
||||
return false;
|
||||
|
||||
am_send_response(response);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void
|
||||
wasm_register_resource(wasm_exec_env_t exec_env, int32 url_offset)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst(exec_env);
|
||||
char *url = NULL;
|
||||
|
||||
if (!validate_app_str_addr(url_offset))
|
||||
return;
|
||||
|
||||
url = addr_app_to_native(url_offset);
|
||||
|
||||
if (url != NULL) {
|
||||
unsigned int mod_id = app_manager_get_module_id(Module_WASM_App,
|
||||
module_inst);
|
||||
bh_assert(mod_id != ID_NONE);
|
||||
am_register_resource(url, module_request_handler, mod_id);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
wasm_post_request(wasm_exec_env_t exec_env,
|
||||
int32 buffer_offset, int size)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst(exec_env);
|
||||
char *buffer = NULL;
|
||||
|
||||
if (!validate_app_addr(buffer_offset, size))
|
||||
return;
|
||||
|
||||
buffer = addr_app_to_native(buffer_offset);
|
||||
|
||||
if (buffer != NULL) {
|
||||
request_t req[1];
|
||||
|
||||
if (!unpack_request(buffer, size, req))
|
||||
return;
|
||||
|
||||
// TODO: add permission check, ensure app can't do harm
|
||||
|
||||
// set sender to help dispatch the response to the sender ap
|
||||
unsigned int mod_id = app_manager_get_module_id(Module_WASM_App,
|
||||
module_inst);
|
||||
bh_assert(mod_id != ID_NONE);
|
||||
req->sender = mod_id;
|
||||
|
||||
if (req->action == COAP_EVENT) {
|
||||
am_publish_event(req);
|
||||
return;
|
||||
}
|
||||
|
||||
am_dispatch_request(req);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
wasm_sub_event(wasm_exec_env_t exec_env, int32 url_offset)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst(exec_env);
|
||||
char *url = NULL;
|
||||
|
||||
if (!validate_app_str_addr(url_offset))
|
||||
return;
|
||||
|
||||
url = addr_app_to_native(url_offset);
|
||||
|
||||
if (url != NULL) {
|
||||
unsigned int mod_id = app_manager_get_module_id(Module_WASM_App,
|
||||
module_inst);
|
||||
|
||||
bh_assert(mod_id != ID_NONE);
|
||||
am_register_event(url, mod_id);
|
||||
}
|
||||
}
|
||||
|
||||
18
core/app-framework/base/native/runtime_lib.h
Normal file
18
core/app-framework/base/native/runtime_lib.h
Normal file
@ -0,0 +1,18 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#ifndef LIB_BASE_RUNTIME_LIB_H_
|
||||
#define LIB_BASE_RUNTIME_LIB_H_
|
||||
|
||||
|
||||
#include "runtime_timer.h"
|
||||
|
||||
void init_wasm_timer();
|
||||
void exit_wasm_timer();
|
||||
timer_ctx_t get_wasm_timer_ctx();
|
||||
timer_ctx_t create_wasm_timer_ctx(unsigned int module_id, int prealloc_num);
|
||||
void destroy_module_timer_ctx(unsigned int module_id);
|
||||
|
||||
#endif /* LIB_BASE_RUNTIME_LIB_H_ */
|
||||
37
core/app-framework/base/native/timer_api.h
Normal file
37
core/app-framework/base/native/timer_api.h
Normal file
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#ifndef _TIMER_API_H_
|
||||
#define _TIMER_API_H_
|
||||
|
||||
#include "bh_platform.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef unsigned int timer_id_t;
|
||||
|
||||
timer_id_t
|
||||
wasm_create_timer(int interval, bool is_period, bool auto_start);
|
||||
|
||||
void
|
||||
wasm_timer_destroy(timer_id_t timer_id);
|
||||
|
||||
void
|
||||
wasm_timer_cancel(timer_id_t timer_id);
|
||||
|
||||
void
|
||||
wasm_timer_restart(timer_id_t timer_id, int interval);
|
||||
|
||||
uint32
|
||||
wasm_get_sys_tick_ms(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* end of _TIMER_API_H_ */
|
||||
|
||||
196
core/app-framework/base/native/timer_wrapper.c
Normal file
196
core/app-framework/base/native/timer_wrapper.c
Normal file
@ -0,0 +1,196 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include "runtime_timer.h"
|
||||
#include "app_manager_export.h"
|
||||
#include "module_wasm_app.h"
|
||||
#include "bh_list.h"
|
||||
#include "bh_thread.h"
|
||||
#include "bh_time.h"
|
||||
|
||||
static bool timer_thread_run = true;
|
||||
|
||||
bh_list g_timer_ctx_list;
|
||||
korp_cond g_timer_ctx_list_cond;
|
||||
korp_mutex g_timer_ctx_list_mutex;
|
||||
typedef struct {
|
||||
bh_list_link l;
|
||||
timer_ctx_t timer_ctx;
|
||||
} timer_ctx_node_t;
|
||||
|
||||
void wasm_timer_callback(timer_id_t id, unsigned int mod_id)
|
||||
{
|
||||
module_data* module = module_data_list_lookup_id(mod_id);
|
||||
if (module == NULL)
|
||||
return;
|
||||
|
||||
// !!! the length parameter must be 0, so the receiver will
|
||||
// not free the payload pointer.
|
||||
bh_post_msg(module->queue, TIMER_EVENT_WASM, (char *)(uintptr_t)id, 0);
|
||||
}
|
||||
|
||||
///
|
||||
/// why we create a separate link for module timer contexts
|
||||
/// rather than traverse the module list?
|
||||
/// It helps to reduce the lock frequency for the module list.
|
||||
/// Also when we lock the module list and then call the callback for
|
||||
/// timer expire, the callback is request the list lock again for lookup
|
||||
/// the module from module id. It is for avoiding that situation.
|
||||
|
||||
void * thread_modulers_timer_check(void * arg)
|
||||
{
|
||||
int ms_to_expiry;
|
||||
|
||||
while (timer_thread_run) {
|
||||
ms_to_expiry = -1;
|
||||
vm_mutex_lock(&g_timer_ctx_list_mutex);
|
||||
timer_ctx_node_t* elem = (timer_ctx_node_t*)
|
||||
bh_list_first_elem(&g_timer_ctx_list);
|
||||
while (elem) {
|
||||
int next = check_app_timers(elem->timer_ctx);
|
||||
if (next != -1) {
|
||||
if (ms_to_expiry == -1 || ms_to_expiry > next)
|
||||
ms_to_expiry = next;
|
||||
}
|
||||
|
||||
elem = (timer_ctx_node_t*) bh_list_elem_next(elem);
|
||||
}
|
||||
vm_mutex_unlock(&g_timer_ctx_list_mutex);
|
||||
|
||||
if (ms_to_expiry == -1)
|
||||
ms_to_expiry = 60 * 1000;
|
||||
vm_mutex_lock(&g_timer_ctx_list_mutex);
|
||||
vm_cond_reltimedwait(&g_timer_ctx_list_cond, &g_timer_ctx_list_mutex,
|
||||
ms_to_expiry);
|
||||
vm_mutex_unlock(&g_timer_ctx_list_mutex);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void wakeup_modules_timer_thread(timer_ctx_t ctx)
|
||||
{
|
||||
vm_mutex_lock(&g_timer_ctx_list_mutex);
|
||||
vm_cond_signal(&g_timer_ctx_list_cond);
|
||||
vm_mutex_unlock(&g_timer_ctx_list_mutex);
|
||||
}
|
||||
|
||||
void init_wasm_timer()
|
||||
{
|
||||
korp_tid tm_tid;
|
||||
bh_list_init(&g_timer_ctx_list);
|
||||
|
||||
vm_cond_init(&g_timer_ctx_list_cond);
|
||||
/* temp solution for: thread_modulers_timer_check thread would recursive lock the mutex */
|
||||
vm_recursive_mutex_init(&g_timer_ctx_list_mutex);
|
||||
|
||||
vm_thread_create(&tm_tid, thread_modulers_timer_check,
|
||||
NULL, BH_APPLET_PRESERVED_STACK_SIZE);
|
||||
}
|
||||
|
||||
void exit_wasm_timer()
|
||||
{
|
||||
timer_thread_run = false;
|
||||
}
|
||||
|
||||
timer_ctx_t create_wasm_timer_ctx(unsigned int module_id, int prealloc_num)
|
||||
{
|
||||
timer_ctx_t ctx = create_timer_ctx(wasm_timer_callback,
|
||||
wakeup_modules_timer_thread,
|
||||
prealloc_num,
|
||||
module_id);
|
||||
|
||||
if (ctx == NULL)
|
||||
return NULL;
|
||||
|
||||
timer_ctx_node_t * node = (timer_ctx_node_t*)
|
||||
bh_malloc(sizeof(timer_ctx_node_t));
|
||||
if (node == NULL) {
|
||||
destroy_timer_ctx(ctx);
|
||||
return NULL;
|
||||
}
|
||||
memset(node, 0, sizeof(*node));
|
||||
node->timer_ctx = ctx;
|
||||
|
||||
vm_mutex_lock(&g_timer_ctx_list_mutex);
|
||||
bh_list_insert(&g_timer_ctx_list, node);
|
||||
vm_mutex_unlock(&g_timer_ctx_list_mutex);
|
||||
|
||||
return ctx;
|
||||
}
|
||||
|
||||
void destroy_module_timer_ctx(unsigned int module_id)
|
||||
{
|
||||
vm_mutex_lock(&g_timer_ctx_list_mutex);
|
||||
timer_ctx_node_t* elem = (timer_ctx_node_t*)
|
||||
bh_list_first_elem(&g_timer_ctx_list);
|
||||
while (elem) {
|
||||
if (timer_ctx_get_owner(elem->timer_ctx) == module_id) {
|
||||
bh_list_remove(&g_timer_ctx_list, elem);
|
||||
destroy_timer_ctx(elem->timer_ctx);
|
||||
bh_free(elem);
|
||||
break;
|
||||
}
|
||||
|
||||
elem = (timer_ctx_node_t*) bh_list_elem_next(elem);
|
||||
}
|
||||
vm_mutex_unlock(&g_timer_ctx_list_mutex);
|
||||
}
|
||||
|
||||
timer_ctx_t get_wasm_timer_ctx(wasm_module_inst_t module_inst)
|
||||
{
|
||||
module_data * m = app_manager_get_module_data(Module_WASM_App,
|
||||
module_inst);
|
||||
if (m == NULL)
|
||||
return NULL;
|
||||
return m->timer_ctx;
|
||||
}
|
||||
|
||||
timer_id_t
|
||||
wasm_create_timer(wasm_exec_env_t exec_env,
|
||||
int interval, bool is_period, bool auto_start)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst(exec_env);
|
||||
timer_ctx_t timer_ctx = get_wasm_timer_ctx(module_inst);
|
||||
bh_assert(timer_ctx);
|
||||
return sys_create_timer(timer_ctx, interval, is_period, auto_start);
|
||||
}
|
||||
|
||||
void
|
||||
wasm_timer_destroy(wasm_exec_env_t exec_env, timer_id_t timer_id)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst(exec_env);
|
||||
timer_ctx_t timer_ctx = get_wasm_timer_ctx(module_inst);
|
||||
bh_assert(timer_ctx);
|
||||
sys_timer_destroy(timer_ctx, timer_id);
|
||||
}
|
||||
|
||||
void
|
||||
wasm_timer_cancel(wasm_exec_env_t exec_env, timer_id_t timer_id)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst(exec_env);
|
||||
timer_ctx_t timer_ctx = get_wasm_timer_ctx(module_inst);
|
||||
bh_assert(timer_ctx);
|
||||
sys_timer_cancel(timer_ctx, timer_id);
|
||||
}
|
||||
|
||||
void
|
||||
wasm_timer_restart(wasm_exec_env_t exec_env,
|
||||
timer_id_t timer_id, int interval)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst(exec_env);
|
||||
timer_ctx_t timer_ctx = get_wasm_timer_ctx(module_inst);
|
||||
bh_assert(timer_ctx);
|
||||
sys_timer_restart(timer_ctx, timer_id, interval);
|
||||
}
|
||||
|
||||
extern uint32 get_sys_tick_ms();
|
||||
|
||||
uint32
|
||||
wasm_get_sys_tick_ms(wasm_exec_env_t exec_env)
|
||||
{
|
||||
return (uint32) bh_get_tick_ms();
|
||||
}
|
||||
|
||||
13
core/app-framework/base/native/wasm_lib.cmake
Normal file
13
core/app-framework/base/native/wasm_lib.cmake
Normal file
@ -0,0 +1,13 @@
|
||||
# Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
set (WASM_LIB_BASE_DIR ${CMAKE_CURRENT_LIST_DIR})
|
||||
|
||||
add_definitions (-DWASM_ENABLE_BASE_LIB)
|
||||
|
||||
include_directories(${WASM_LIB_BASE_DIR})
|
||||
|
||||
file (GLOB_RECURSE source_all ${WASM_LIB_BASE_DIR}/*.c)
|
||||
|
||||
set (WASM_APP_LIB_CURRENT_SOURCE ${source_all})
|
||||
|
||||
Reference in New Issue
Block a user