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:
wenyongh
2020-01-21 13:26:14 +08:00
committed by Wang Xin
parent 2a4528c749
commit 46b93b9d22
464 changed files with 25137 additions and 7911 deletions

View File

@ -0,0 +1,11 @@
Notes:
=======
This folder is for the source files shared by both WASM APP and native runtime
- The c files in this directory are compiled into both the WASM APP and runtime.
- The header files for distributing to SDK are placed in the "bi-inc" folder.

View File

@ -0,0 +1,830 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "bi-inc/attr_container.h"
typedef union jvalue {
bool z;
int8_t b;
uint16_t c;
int16_t s;
int32_t i;
int64_t j;
float f;
double d;
} jvalue;
#define bh_memcpy_s(dest, dlen, src, slen) do { \
int _ret = slen == 0 ? 0 : b_memcpy_s (dest, dlen, src, slen); \
(void)_ret; \
} while (0)
static inline int16_t get_int16(const char *buf)
{
int16_t ret;
bh_memcpy_s(&ret, sizeof(int16_t), buf, sizeof(int16_t));
return ret;
}
static inline uint16_t get_uint16(const char *buf)
{
return get_int16(buf);
}
static inline int32_t get_int32(const char *buf)
{
int32_t ret;
bh_memcpy_s(&ret, sizeof(int32_t), buf, sizeof(int32_t));
return ret;
}
static inline uint32_t get_uint32(const char *buf)
{
return get_int32(buf);
}
static inline int64_t get_int64(const char *buf)
{
int64_t ret;
bh_memcpy_s(&ret, sizeof(int64_t), buf, sizeof(int64_t));
return ret;
}
static inline uint64_t get_uint64(const char *buf)
{
return get_int64(buf);
}
static inline void set_int16(char *buf, int16_t v)
{
bh_memcpy_s(buf, sizeof(int16_t), &v, sizeof(int16_t));
}
static inline void set_uint16(char *buf, uint16_t v)
{
bh_memcpy_s(buf, sizeof(uint16_t), &v, sizeof(uint16_t));
}
static inline void set_int32(char *buf, int32_t v)
{
bh_memcpy_s(buf, sizeof(int32_t), &v, sizeof(int32_t));
}
static inline void set_uint32(char *buf, uint32_t v)
{
bh_memcpy_s(buf, sizeof(uint32_t), &v, sizeof(uint32_t));
}
static inline void set_int64(char *buf, int64_t v)
{
bh_memcpy_s(buf, sizeof(int64_t), &v, sizeof(int64_t));
}
static inline void set_uint64(char *buf, uint64_t v)
{
bh_memcpy_s(buf, sizeof(uint64_t), &v, sizeof(uint64_t));
}
char*
attr_container_get_attr_begin(const attr_container_t *attr_cont,
uint32_t *p_total_length, uint16_t *p_attr_num)
{
char *p = (char*) attr_cont->buf;
uint16_t str_len, attr_num;
uint32_t total_length;
/* skip total length */
total_length = get_uint32(p);
p += sizeof(uint32_t);
if (!total_length)
return NULL;
/* tag length */
str_len = get_uint16(p);
p += sizeof(uint16_t);
if (!str_len)
return NULL;
/* tag content */
p += str_len;
if (p - attr_cont->buf >= total_length)
return NULL;
/* attribute num */
attr_num = get_uint16(p);
p += sizeof(uint16_t);
if (p - attr_cont->buf >= total_length)
return NULL;
if (p_total_length)
*p_total_length = total_length;
if (p_attr_num)
*p_attr_num = attr_num;
/* first attribute */
return p;
}
static char*
attr_container_get_attr_next(const char *curr_attr)
{
char *p = (char*) curr_attr;
uint8_t type;
/* key length and key */
p += sizeof(uint16_t) + get_uint16(p);
type = *p++;
/* Short type to Boolean type */
if (type >= ATTR_TYPE_SHORT && type <= ATTR_TYPE_BOOLEAN) {
p += 1 << (type & 3);
return p;
}
/* String type */
else if (type == ATTR_TYPE_STRING) {
p += sizeof(uint16_t) + get_uint16(p);
return p;
}
/* ByteArray type */
else if (type == ATTR_TYPE_BYTEARRAY) {
p += sizeof(uint32_t) + get_uint32(p);
return p;
}
return NULL;
}
static const char*
attr_container_find_attr(const attr_container_t *attr_cont, const char *key)
{
uint32_t total_length;
uint16_t str_len, attr_num, i;
const char *p = attr_cont->buf;
if (!key)
return NULL;
if (!(p = attr_container_get_attr_begin(attr_cont, &total_length, &attr_num)))
return NULL;
for (i = 0; i < attr_num; i++) {
/* key length */
if (!(str_len = get_uint16(p)))
return NULL;
if (str_len == strlen(key) + 1
&& memcmp(p + sizeof(uint16_t), key, str_len) == 0) {
if (p + sizeof(uint16_t) + str_len - attr_cont->buf >= total_length)
return NULL;
return p;
}
if (!(p = attr_container_get_attr_next(p)))
return NULL;
}
return NULL;
}
char*
attr_container_get_attr_end(const attr_container_t *attr_cont)
{
uint32_t total_length;
uint16_t attr_num, i;
char *p;
if (!(p = attr_container_get_attr_begin(attr_cont, &total_length, &attr_num)))
return NULL;
for (i = 0; i < attr_num; i++)
if (!(p = attr_container_get_attr_next(p)))
return NULL;
return p;
}
static char*
attr_container_get_msg_end(attr_container_t *attr_cont)
{
char *p = attr_cont->buf;
return p + get_uint32(p);
}
uint16_t attr_container_get_attr_num(const attr_container_t *attr_cont)
{
uint16_t str_len;
/* skip total length */
const char *p = attr_cont->buf + sizeof(uint32_t);
str_len = get_uint16(p);
/* skip tag length and tag */
p += sizeof(uint16_t) + str_len;
/* attribute num */
return get_uint16(p);
}
static void attr_container_inc_attr_num(attr_container_t *attr_cont)
{
uint16_t str_len, attr_num;
/* skip total length */
char *p = attr_cont->buf + sizeof(uint32_t);
str_len = get_uint16(p);
/* skip tag length and tag */
p += sizeof(uint16_t) + str_len;
/* attribute num */
attr_num = get_uint16(p) + 1;
set_uint16(p, attr_num);
}
attr_container_t *
attr_container_create(const char *tag)
{
attr_container_t *attr_cont;
int length, tag_length;
char *p;
tag_length = tag ? strlen(tag) + 1 : 1;
length = offsetof(attr_container_t, buf) +
/* total length + tag length + tag + reserved 100 bytes */
sizeof(uint32_t) + sizeof(uint16_t) + tag_length + 100;
if (!(attr_cont = attr_container_malloc(length))) {
attr_container_printf(
"Create attr_container failed: allocate memory failed.\r\n");
return NULL;
}
memset(attr_cont, 0, length);
p = attr_cont->buf;
/* total length */
set_uint32(p, length - offsetof(attr_container_t, buf));
p += 4;
/* tag length, tag */
set_uint16(p, tag_length);
p += 2;
if (tag)
bh_memcpy_s(p, tag_length, tag, tag_length);
return attr_cont;
}
void attr_container_destroy(const attr_container_t *attr_cont)
{
if (attr_cont)
attr_container_free((char*) attr_cont);
}
static bool check_set_attr(attr_container_t **p_attr_cont, const char *key)
{
uint32_t flags;
if (!p_attr_cont || !*p_attr_cont || !key || strlen(key) == 0) {
attr_container_printf(
"Set attribute failed: invalid input arguments.\r\n");
return false;
}
flags = get_uint32((char*) *p_attr_cont);
if (flags & ATTR_CONT_READONLY_SHIFT) {
attr_container_printf(
"Set attribute failed: attribute container is readonly.\r\n");
return false;
}
return true;
}
bool attr_container_set_attr(attr_container_t **p_attr_cont, const char *key,
int type, const void *value, int value_length)
{
attr_container_t *attr_cont, *attr_cont1;
uint16_t str_len;
uint32_t total_length, attr_len;
char *p, *p1, *attr_end, *msg_end, *attr_buf;
if (!check_set_attr(p_attr_cont, key)) {
return false;
}
attr_cont = *p_attr_cont;
p = attr_cont->buf;
total_length = get_uint32(p);
if (!(attr_end = attr_container_get_attr_end(attr_cont))) {
attr_container_printf("Set attr failed: get attr end failed.\r\n");
return false;
}
msg_end = attr_container_get_msg_end(attr_cont);
/* key len + key + '\0' + type */
attr_len = sizeof(uint16_t) + strlen(key) + 1 + 1;
if (type >= ATTR_TYPE_SHORT && type <= ATTR_TYPE_BOOLEAN)
attr_len += 1 << (type & 3);
else if (type == ATTR_TYPE_STRING)
attr_len += sizeof(uint16_t) + value_length;
else if (type == ATTR_TYPE_BYTEARRAY)
attr_len += sizeof(uint32_t) + value_length;
if (!(p = attr_buf = attr_container_malloc(attr_len))) {
attr_container_printf("Set attr failed: allocate memory failed.\r\n");
return false;
}
/* Set the attr buf */
str_len = strlen(key) + 1;
set_uint16(p, str_len);
p += sizeof(uint16_t);
bh_memcpy_s(p, str_len, key, str_len);
p += str_len;
*p++ = type;
if (type >= ATTR_TYPE_SHORT && type <= ATTR_TYPE_BOOLEAN)
bh_memcpy_s(p, 1 << (type & 3), value, 1 << (type & 3));
else if (type == ATTR_TYPE_STRING) {
set_uint16(p, value_length);
p += sizeof(uint16_t);
bh_memcpy_s(p, value_length, value, value_length);
} else if (type == ATTR_TYPE_BYTEARRAY) {
set_uint32(p, value_length);
p += sizeof(uint32_t);
bh_memcpy_s(p, value_length, value, value_length);
}
if ((p = (char*) attr_container_find_attr(attr_cont, key))) {
/* key found */
p1 = attr_container_get_attr_next(p);
if (p1 - p == attr_len) {
bh_memcpy_s(p, attr_len, attr_buf, attr_len);
attr_container_free(attr_buf);
return true;
}
if (p1 - p + msg_end - attr_end >= attr_len) {
memmove(p, p1, attr_end - p1);
bh_memcpy_s(p + (attr_end - p1), attr_len, attr_buf, attr_len);
attr_container_free(attr_buf);
return true;
}
total_length += attr_len + 100;
if (!(attr_cont1 = attr_container_malloc(
offsetof(attr_container_t, buf) + total_length))) {
attr_container_printf(
"Set attr failed: allocate memory failed.\r\n");
attr_container_free(attr_buf);
return false;
}
bh_memcpy_s(attr_cont1, p - (char* )attr_cont, attr_cont,
p - (char* )attr_cont);
bh_memcpy_s((char* )attr_cont1 + (unsigned )(p - (char* )attr_cont),
attr_end - p1, p1, attr_end - p1);
bh_memcpy_s(
(char* )attr_cont1 + (unsigned )(p - (char* )attr_cont)
+ (unsigned )(attr_end - p1), attr_len, attr_buf,
attr_len);
p = attr_cont1->buf;
set_uint32(p, total_length);
*p_attr_cont = attr_cont1;
/* Free original buffer */
attr_container_free(attr_cont);
attr_container_free(attr_buf);
return true;
} else {
/* key not found */
if (msg_end - attr_end >= attr_len) {
bh_memcpy_s(attr_end, msg_end - attr_end, attr_buf, attr_len);
attr_container_inc_attr_num(attr_cont);
attr_container_free(attr_buf);
return true;
}
total_length += attr_len + 100;
if (!(attr_cont1 = attr_container_malloc(
offsetof(attr_container_t, buf) + total_length))) {
attr_container_printf(
"Set attr failed: allocate memory failed.\r\n");
attr_container_free(attr_buf);
return false;
}
bh_memcpy_s(attr_cont1, attr_end - (char* )attr_cont, attr_cont,
attr_end - (char* )attr_cont);
bh_memcpy_s(
(char* )attr_cont1 + (unsigned )(attr_end - (char* )attr_cont),
attr_len, attr_buf, attr_len);
attr_container_inc_attr_num(attr_cont1);
p = attr_cont1->buf;
set_uint32(p, total_length);
*p_attr_cont = attr_cont1;
/* Free original buffer */
attr_container_free(attr_cont);
attr_container_free(attr_buf);
return true;
}
return false;
}
bool attr_container_set_short(attr_container_t **p_attr_cont, const char *key,
short value)
{
return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_SHORT, &value, 2);
}
bool attr_container_set_int(attr_container_t **p_attr_cont, const char *key,
int value)
{
return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_INT, &value, 4);
}
bool attr_container_set_int64(attr_container_t **p_attr_cont, const char *key,
int64_t value)
{
return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_INT64, &value, 8);
}
bool attr_container_set_byte(attr_container_t **p_attr_cont, const char *key,
int8_t value)
{
return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_BYTE, &value, 1);
}
bool attr_container_set_uint16(attr_container_t **p_attr_cont, const char *key,
uint16_t value)
{
return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_UINT16, &value,
2);
}
bool attr_container_set_float(attr_container_t **p_attr_cont, const char *key,
float value)
{
return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_FLOAT, &value, 4);
}
bool attr_container_set_double(attr_container_t **p_attr_cont, const char *key,
double value)
{
return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_DOUBLE, &value,
8);
}
bool attr_container_set_bool(attr_container_t **p_attr_cont, const char *key,
bool value)
{
int8_t value1 = value ? 1 : 0;
return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_BOOLEAN, &value1,
1);
}
bool attr_container_set_string(attr_container_t **p_attr_cont, const char *key,
const char *value)
{
if (!value) {
attr_container_printf("Set attr failed: invald input arguments.\r\n");
return false;
}
return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_STRING, value,
strlen(value) + 1);;
}
bool attr_container_set_bytearray(attr_container_t **p_attr_cont,
const char *key, const int8_t *value, unsigned length)
{
if (!value) {
attr_container_printf("Set attr failed: invald input arguments.\r\n");
return false;
}
return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_BYTEARRAY, value,
length);;
}
static const char*
attr_container_get_attr(const attr_container_t *attr_cont, const char *key)
{
const char *attr_addr;
if (!attr_cont || !key) {
attr_container_printf(
"Get attribute failed: invalid input arguments.\r\n");
return NULL;
}
if (!(attr_addr = attr_container_find_attr(attr_cont, key))) {
attr_container_printf("Get attribute failed: lookup key failed.\r\n");
return false;
}
/* key len + key + '\0' */
return attr_addr + 2 + strlen(key) + 1;
}
#define TEMPLATE_ATTR_BUF_TO_VALUE(attr, key, var_name) do {\
jvalue val; \
const char *addr = attr_container_get_attr(attr, key); \
uint8_t type; \
if (!addr) \
return 0; \
val.j = 0; \
type = *(uint8_t*)addr++; \
switch (type) { \
case ATTR_TYPE_SHORT: \
case ATTR_TYPE_INT: \
case ATTR_TYPE_INT64: \
case ATTR_TYPE_BYTE: \
case ATTR_TYPE_UINT16: \
case ATTR_TYPE_FLOAT: \
case ATTR_TYPE_DOUBLE: \
case ATTR_TYPE_BOOLEAN: \
bh_memcpy_s(&val, sizeof(val.var_name), addr, 1 << (type & 3)); \
break; \
case ATTR_TYPE_STRING: \
{ \
unsigned len= get_uint16(addr); \
addr += 2; \
if (len > sizeof(val.var_name)) \
len = sizeof(val.var_name); \
bh_memcpy_s(&val.var_name, sizeof(val.var_name), addr, len); \
break; \
} \
case ATTR_TYPE_BYTEARRAY: \
{ \
unsigned len= get_uint32(addr); \
addr += 4; \
if (len > sizeof(val.var_name)) \
len = sizeof(val.var_name); \
bh_memcpy_s(&val.var_name, sizeof(val.var_name), addr, len); \
break; \
} \
} \
return val.var_name; \
} while (0)
short attr_container_get_as_short(const attr_container_t *attr_cont,
const char *key)
{
TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, s);
}
int attr_container_get_as_int(const attr_container_t *attr_cont,
const char *key)
{
TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, i);
}
int64_t attr_container_get_as_int64(const attr_container_t *attr_cont,
const char *key)
{
TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, j);
}
int8_t attr_container_get_as_byte(const attr_container_t *attr_cont,
const char *key)
{
TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, b);
}
uint16_t attr_container_get_as_uint16(const attr_container_t *attr_cont,
const char *key)
{
TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, s);
}
float attr_container_get_as_float(const attr_container_t *attr_cont,
const char *key)
{
TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, f);
}
double attr_container_get_as_double(const attr_container_t *attr_cont,
const char *key)
{
TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, d);
}
bool attr_container_get_as_bool(const attr_container_t *attr_cont,
const char *key)
{
TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, z);
}
const int8_t*
attr_container_get_as_bytearray(const attr_container_t *attr_cont,
const char *key, unsigned *array_length)
{
const char *addr = attr_container_get_attr(attr_cont, key);
uint8_t type;
uint32_t length;
if (!addr)
return NULL;
if (!array_length) {
attr_container_printf("Get attribute failed: invalid input arguments.");
return NULL;
}
type = *(uint8_t*) addr++;
switch (type) {
case ATTR_TYPE_SHORT:
case ATTR_TYPE_INT:
case ATTR_TYPE_INT64:
case ATTR_TYPE_BYTE:
case ATTR_TYPE_UINT16:
case ATTR_TYPE_FLOAT:
case ATTR_TYPE_DOUBLE:
case ATTR_TYPE_BOOLEAN:
length = 1 << (type & 3);
break;
case ATTR_TYPE_STRING:
length = get_uint16(addr);
addr += 2;
break;
case ATTR_TYPE_BYTEARRAY:
length = get_uint32(addr);
addr += 4;
break;
default:
return NULL;
}
*array_length = length;
return (const int8_t*) addr;
}
char*
attr_container_get_as_string(const attr_container_t *attr_cont, const char *key)
{
unsigned array_length;
return (char*) attr_container_get_as_bytearray(attr_cont, key,
&array_length);
}
const char*
attr_container_get_tag(const attr_container_t *attr_cont)
{
return attr_cont ?
attr_cont->buf + sizeof(uint32_t) + sizeof(uint16_t) : NULL;
}
bool attr_container_contain_key(const attr_container_t *attr_cont,
const char *key)
{
if (!attr_cont || !key || !strlen(key)) {
attr_container_printf(
"Check contain key failed: invalid input arguments.\r\n");
return false;
}
return attr_container_find_attr(attr_cont, key) ? true : false;
}
unsigned int attr_container_get_serialize_length(
const attr_container_t *attr_cont)
{
const char *p;
if (!attr_cont) {
attr_container_printf(
"Get container serialize length failed: invalid input arguments.\r\n");
return 0;
}
p = attr_cont->buf;
return sizeof(uint16_t) + get_uint32(p);
}
bool attr_container_serialize(char *buf, const attr_container_t *attr_cont)
{
const char *p;
uint16_t flags;
uint32_t length;
if (!buf || !attr_cont) {
attr_container_printf(
"Container serialize failed: invalid input arguments.\r\n");
return false;
}
p = attr_cont->buf;
length = sizeof(uint16_t) + get_uint32(p);
bh_memcpy_s(buf, length, attr_cont, length);
/* Set readonly */
flags = get_uint16((const char*) attr_cont);
set_uint16(buf, flags | (1 << ATTR_CONT_READONLY_SHIFT));
return true;
}
bool attr_container_is_constant(const attr_container_t* attr_cont)
{
uint16_t flags;
if (!attr_cont) {
attr_container_printf(
"Container check const: invalid input arguments.\r\n");
return false;
}
flags = get_uint16((const char*) attr_cont);
return (flags & (1 << ATTR_CONT_READONLY_SHIFT)) ? true : false;
}
void attr_container_dump(const attr_container_t *attr_cont)
{
uint32_t total_length;
uint16_t attr_num, i, type;
const char *p, *tag, *key;
jvalue value;
if (!attr_cont)
return;
tag = attr_container_get_tag(attr_cont);
if (!tag)
return;
attr_container_printf("Attribute container dump:\n");
attr_container_printf("Tag: %s\n", tag);
p = attr_container_get_attr_begin(attr_cont, &total_length, &attr_num);
if (!p)
return;
attr_container_printf("Attribute list:\n");
for (i = 0; i < attr_num; i++) {
key = p + 2;
/* Skip key len and key */
p += 2 + get_uint16(p);
type = *p++;
attr_container_printf(" key: %s", key);
switch (type) {
case ATTR_TYPE_SHORT:
bh_memcpy_s(&value.s, sizeof(int16_t), p, sizeof(int16_t));
attr_container_printf(", type: short, value: 0x%x\n",
value.s & 0xFFFF);
p += 2;
break;
case ATTR_TYPE_INT:
bh_memcpy_s(&value.i, sizeof(int32_t), p, sizeof(int32_t));
attr_container_printf(", type: int, value: 0x%x\n", value.i);
p += 4;
break;
case ATTR_TYPE_INT64:
bh_memcpy_s(&value.j, sizeof(uint64_t), p, sizeof(uint64_t));
attr_container_printf(", type: int64, value: 0x%llx\n", (long long unsigned int)(value.j));
p += 8;
break;
case ATTR_TYPE_BYTE:
bh_memcpy_s(&value.b, 1, p, 1);
attr_container_printf(", type: byte, value: 0x%x\n",
value.b & 0xFF);
p++;
break;
case ATTR_TYPE_UINT16:
bh_memcpy_s(&value.c, sizeof(uint16_t), p, sizeof(uint16_t));
attr_container_printf(", type: uint16, value: 0x%x\n", value.c);
p += 2;
break;
case ATTR_TYPE_FLOAT:
bh_memcpy_s(&value.f, sizeof(float), p, sizeof(float));
attr_container_printf(", type: float, value: %f\n", value.f);
p += 4;
break;
case ATTR_TYPE_DOUBLE:
bh_memcpy_s(&value.d, sizeof(double), p, sizeof(double));
attr_container_printf(", type: double, value: %f\n", value.d);
p += 8;
break;
case ATTR_TYPE_BOOLEAN:
bh_memcpy_s(&value.z, 1, p, 1);
attr_container_printf(", type: bool, value: 0x%x\n", value.z);
p++;
break;
case ATTR_TYPE_STRING:
attr_container_printf(", type: string, value: %s\n",
p + sizeof(uint16_t));
p += sizeof(uint16_t) + get_uint16(p);
break;
case ATTR_TYPE_BYTEARRAY:
attr_container_printf(", type: byte array, length: %d\n",
get_uint32(p));
p += sizeof(uint32_t) + get_uint32(p);
break;
}
}
attr_container_printf("\n");
}

View File

@ -0,0 +1,425 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#ifndef _ATTR_CONTAINER_H_
#define _ATTR_CONTAINER_H_
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <stdbool.h>
#include "bh_platform.h"
#ifdef __cplusplus
extern "C" {
#endif
/* Attribute type */
enum {
ATTR_TYPE_BEGIN = 1,
ATTR_TYPE_SHORT = ATTR_TYPE_BEGIN,
ATTR_TYPE_INT,
ATTR_TYPE_INT64,
ATTR_TYPE_BYTE,
ATTR_TYPE_UINT16,
ATTR_TYPE_FLOAT,
ATTR_TYPE_DOUBLE,
ATTR_TYPE_BOOLEAN,
ATTR_TYPE_STRING,
ATTR_TYPE_BYTEARRAY,
ATTR_TYPE_END = ATTR_TYPE_BYTEARRAY
};
#define ATTR_CONT_READONLY_SHIFT 2
typedef struct attr_container {
/* container flag:
* bit0, bit1 denote the implemenation algorithm, 00: buffer, 01: link list
* bit2 denotes the readonly flag: 1 is readonly and attr cannot be set
*/
char flags[2];
/**
* Buffer format
* for buffer implementation:
* buf length (4 bytes)
* tag length (2 bytes)
* tag
* attr num (2bytes)
* attr[0..n-1]:
* attr key length (2 bytes)
* attr type (1byte)
* attr value (length depends on attr type)
*/
char buf[1];
} attr_container_t;
/**
* Create attribute container
*
* @param tag tag of current attribute container
*
* @return the created attribute container, NULL if failed
*/
attr_container_t *
attr_container_create(const char *tag);
/**
* Destroy attribute container
*
* @param attr_cont the attribute container to destroy
*/
void
attr_container_destroy(const attr_container_t *attr_cont);
/**
* Set short attribute in attribute container
*
* @param p_attr_cont pointer to attribute container to set attribute, and
* return the new attribute container if it is re-created
* @param key the attribute key
* @param value the attribute value
*
* @return true if success, false otherwise
*/
bool
attr_container_set_short(attr_container_t **p_attr_cont, const char *key,
short value);
/**
* Set int attribute in attribute container
*
* @param p_attr_cont pointer to attribute container to set attribute, and
* return the new attribute container if it is re-created
* @param key the attribute key
* @param value the attribute value
*
* @return true if success, false otherwise
*/
bool
attr_container_set_int(attr_container_t **p_attr_cont, const char *key,
int value);
/**
* Set int64 attribute in attribute container
*
* @param p_attr_cont pointer to attribute container to set attribute, and
* return the new attribute container if it is re-created
* @param key the attribute key
* @param value the attribute value
*
* @return true if success, false otherwise
*/
bool
attr_container_set_int64(attr_container_t **p_attr_cont, const char *key,
int64_t value);
/**
* Set byte attribute in attribute container
*
* @param p_attr_cont pointer to attribute container to set attribute, and
* return the new attribute container if it is re-created
* @param key the attribute key
* @param value the attribute value
*
* @return true if success, false otherwise
*/
bool
attr_container_set_byte(attr_container_t **p_attr_cont, const char *key,
int8_t value);
/**
* Set uint16 attribute in attribute container
*
* @param p_attr_cont pointer to attribute container to set attribute, and
* return the new attribute container if it is re-created
* @param key the attribute key
* @param value the attribute value
*
* @return true if success, false otherwise
*/
bool
attr_container_set_uint16(attr_container_t **p_attr_cont, const char *key,
uint16_t value);
/**
* Set float attribute in attribute container
*
* @param p_attr_cont pointer to attribute container to set attribute, and
* return the new attribute container if it is re-created
* @param key the attribute key
* @param value the attribute value
*
* @return true if success, false otherwise
*/
bool
attr_container_set_float(attr_container_t **p_attr_cont, const char *key,
float value);
/**
* Set double attribute in attribute container
*
* @param p_attr_cont pointer to attribute container to set attribute, and
* return the new attribute container if it is re-created
* @param key the attribute key
* @param value the attribute value
*
* @return true if success, false otherwise
*/
bool
attr_container_set_double(attr_container_t **p_attr_cont, const char *key,
double value);
/**
* Set bool attribute in attribute container
*
* @param p_attr_cont pointer to attribute container to set attribute, and
* return the new attribute container if it is re-created
* @param key the attribute key
* @param value the attribute value
*
* @return true if success, false otherwise
*/
bool
attr_container_set_bool(attr_container_t **p_attr_cont, const char *key,
bool value);
/**
* Set string attribute in attribute container
*
* @param p_attr_cont pointer to attribute container to set attribute, and
* return the new attribute container if it is re-created
* @param key the attribute key
* @param value the attribute value
*
* @return true if success, false otherwise
*/
bool
attr_container_set_string(attr_container_t **p_attr_cont, const char *key,
const char *value);
/**
* Set bytearray attribute in attribute container
*
* @param p_attr_cont pointer to attribute container to set attribute, and
* return the new attribute container if it is re-created
* @param key the attribute key
* @param value the bytearray buffer
* @param length the bytearray length
*
* @return true if success, false otherwise
*/
bool
attr_container_set_bytearray(attr_container_t **p_attr_cont, const char *key,
const int8_t *value, unsigned length);
/**
* Get tag of current attribute container
*
* @param attr_cont the attribute container
*
* @return tag of current attribute container
*/
const char*
attr_container_get_tag(const attr_container_t *attr_cont);
/**
* Get attribute number of current attribute container
*
* @param attr_cont the attribute container
*
* @return attribute number of current attribute container
*/
uint16_t
attr_container_get_attr_num(const attr_container_t *attr_cont);
/**
* Whether the attribute container contains an attribute key.
*
* @param attr_cont the attribute container
* @param key the attribute key
*
* @return true if key is contained in message, false otherwise
*/
bool
attr_container_contain_key(const attr_container_t *attr_cont, const char *key);
/**
* Get attribute from attribute container and return it as short value,
* return 0 if attribute isn't found in message.
*
* @param attr_cont the attribute container
* @param key the attribute key
*
* @return the short value of the attribute, 0 if key isn't found
*/
short
attr_container_get_as_short(const attr_container_t *attr_cont, const char *key);
/**
* Get attribute from attribute container and return it as int value,
* return 0 if attribute isn't found in message.
*
* @param attr_cont the attribute container
* @param key the attribute key
*
* @return the int value of the attribute, 0 if key isn't found
*/
int
attr_container_get_as_int(const attr_container_t *attr_cont, const char *key);
/**
* Get attribute from attribute container and return it as int64 value,
* return 0 if attribute isn't found in attribute container.
*
* @param attr_cont the attribute container
* @param key the attribute key
*
* @return the long value of the attribute, 0 if key isn't found
*/
int64_t
attr_container_get_as_int64(const attr_container_t *attr_cont, const char *key);
/**
* Get attribute from attribute container and return it as byte value,
* return 0 if attribute isn't found in attribute container.
*
* @param attr_cont the attribute container
* @param key the attribute key
*
* @return the byte value of the attribute, 0 if key isn't found
*/
int8_t
attr_container_get_as_byte(const attr_container_t *attr_cont, const char *key);
/**
* Get attribute from attribute container and return it as uint16 value,
* return 0 if attribute isn't found in attribute container.
*
* @param attr_cont the attribute container
* @param key the attribute key
*
* @return the char value of the attribute, 0 if key isn't found
*/
uint16_t
attr_container_get_as_uint16(const attr_container_t *attr_cont,
const char *key);
/**
* Get attribute from attribute container and return it as float value,
* return 0 if attribute isn't found in attribute container.
*
* @param attr_cont the attribute container
* @param key the attribute key
*
* @return the float value of the attribute, 0 if key isn't found
*/
float
attr_container_get_as_float(const attr_container_t *attr_cont, const char *key);
/**
* Get attribute from attribute container and return it as double value,
* return 0 if attribute isn't found in attribute container.
*
* @param attr_cont the attribute container
* @param key the attribute key
*
* @return the double value of the attribute, 0 if key isn't found
*/
double
attr_container_get_as_double(const attr_container_t *attr_cont,
const char *key);
/**
* Get attribute from attribute container and return it as bool value,
* return false if attribute isn't found in attribute container.
*
* @param attr_cont the attribute container
* @param key the attribute key
*
* @return the bool value of the attribute, 0 if key isn't found
*/
bool
attr_container_get_as_bool(const attr_container_t *attr_cont, const char *key);
/**
* Get attribute from attribute container and return it as string value,
* return NULL if attribute isn't found in attribute container.
*
* @param attr_cont the attribute container
* @param key the attribute key
*
* @return the string value of the attribute, NULL if key isn't found
*/
char*
attr_container_get_as_string(const attr_container_t *attr_cont,
const char *key);
/**
* Get attribute from attribute container and return it as bytearray value,
* return 0 if attribute isn't found in attribute container.
*
* @param attr_cont the attribute container
* @param key the attribute key
*
* @return the bytearray value of the attribute, NULL if key isn't found
*/
const int8_t*
attr_container_get_as_bytearray(const attr_container_t *attr_cont,
const char *key, unsigned *array_length);
/**
* Get the buffer size of attribute container
*
* @param attr_cont the attribute container
*
* @return the buffer size of attribute container
*/
unsigned
attr_container_get_serialize_length(const attr_container_t *attr_cont);
/**
* Serialize attribute container to a buffer
*
* @param buf the buffer to receive the serialized data
* @param attr_cont the attribute container to be serialized
*
* @return true if success, false otherwise
*/
bool
attr_container_serialize(char *buf, const attr_container_t *attr_cont);
/**
* Whether the attribute container is const, or set attribute isn't supported
*
* @param attr_cont the attribute container
*
* @return true if const, false otherwise
*/
bool
attr_container_is_constant(const attr_container_t* attr_cont);
void
attr_container_dump(const attr_container_t *attr_cont);
#ifndef attr_container_malloc
#define attr_container_malloc wa_malloc
#endif
#ifndef attr_container_free
#define attr_container_free wa_free
#endif
#ifndef attr_container_printf
#define attr_container_printf printf
#endif
#ifdef __cplusplus
} /* end of extern "C" */
#endif
#endif /* end of _ATTR_CONTAINER_H_ */

View File

@ -0,0 +1,154 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#ifndef _SHARED_UTILS_H_
#define _SHARED_UTILS_H_
#include "bh_platform.h"
#ifdef __cplusplus
extern "C" {
#endif
#define FMT_ATTR_CONTAINER 99
#define FMT_APP_RAW_BINARY 98
/* the request structure */
typedef struct request {
// message id
uint32 mid;
// url of the request
char *url;
// action of the request, can be PUT/GET/POST/DELETE
int action;
// payload format, currently only support attr_container_t type
int fmt;
// payload of the request, currently only support attr_container_t type
void *payload;
//length in bytes of the payload
int payload_len;
//sender of the request
unsigned long sender;
} request_t;
/* the response structure */
typedef struct response {
// message id
uint32 mid;
// status of the response
int status;
// payload format
int fmt;
// payload of the response,
void *payload;
//length in bytes of the payload
int payload_len;
//receiver of the response
unsigned long reciever;
} response_t;
int
check_url_start(const char* url, int url_len, const char * leading_str);
bool
match_url(char * pattern, char * matched);
char *
find_key_value(char * buffer, int buffer_len, char * key, char * value,
int value_len, char delimiter);
request_t *
clone_request(request_t *request);
void
request_cleaner(request_t *request);
response_t *
clone_response(response_t * response);
void
response_cleaner(response_t * response);
/**
* @brief Set fields of response.
*
* @param response pointer of the response to be set
* @param status status of response
* @param fmt format of the response payload
* @param payload payload of the response
* @param payload_len length in bytes of the response payload
*
* @return pointer to the response
*
* @warning the response pointer MUST NOT be NULL
*/
response_t *
set_response(response_t * response, int status, int fmt,
const char *payload, int payload_len);
/**
* @brief Make a response for a request.
*
* @param request pointer of the request
* @param response pointer of the response to be made
*
* @return pointer to the response
*
* @warning the request and response pointers MUST NOT be NULL
*/
response_t *
make_response_for_request(request_t * request, response_t * response);
/**
* @brief Initialize a request.
*
* @param request pointer of the request to be initialized
* @param url url of the request
* @param action action of the request
* @param fmt format of the request payload
* @param payload payload of the request
* @param payload_len length in bytes of the request payload
*
* @return pointer to the request
*
* @warning the request pointer MUST NOT be NULL
*/
request_t *
init_request(request_t * request, char *url, int action, int fmt,
void *payload, int payload_len);
char *
pack_request(request_t *request, int * size);
request_t *
unpack_request(char * packet, int size, request_t * request);
char *
pack_response(response_t *response, int * size);
response_t *
unpack_response(char * packet, int size, response_t * response);
void
free_req_resp_packet(char * packet);
#ifdef __cplusplus
}
#endif
#endif /* end of _SHARED_UTILS_H_ */

View File

@ -0,0 +1,335 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#ifndef WAMR_GRAPHIC_LIBRARY_SHARED_UTILS_H
#define WAMR_GRAPHIC_LIBRARY_SHARED_UTILS_H
#include "bh_platform.h"
#ifdef __cplusplus
extern "C" {
#endif
#include <stdbool.h>
#include "lv_conf.h"
typedef lv_coord_t wgl_coord_t; /* lv_coord_t is defined in lv_conf.h */
typedef void * wgl_font_user_data_t;
/**
* Represents a point on the screen.
*/
typedef struct
{
lv_coord_t x;
lv_coord_t y;
} wgl_point_t;
/** Represents an area of the screen. */
typedef struct
{
lv_coord_t x1;
lv_coord_t y1;
lv_coord_t x2;
lv_coord_t y2;
} wgl_area_t;
/** Describes the properties of a glyph. */
typedef struct
{
uint16_t adv_w; /**< The glyph needs this space. Draw the next glyph after this width. 8 bit integer, 4 bit fractional */
uint8_t box_w; /**< Width of the glyph's bounding box*/
uint8_t box_h; /**< Height of the glyph's bounding box*/
int8_t ofs_x; /**< x offset of the bounding box*/
int8_t ofs_y; /**< y offset of the bounding box*/
uint8_t bpp; /**< Bit-per-pixel: 1, 2, 4, 8*/
}wgl_font_glyph_dsc_t;
/*Describe the properties of a font*/
typedef struct _wgl_font_struct
{
/** Get a glyph's descriptor from a font*/
bool (*get_glyph_dsc)(const struct _wgl_font_struct *, wgl_font_glyph_dsc_t *, uint32_t letter, uint32_t letter_next);
/** Get a glyph's bitmap from a font*/
const uint8_t * (*get_glyph_bitmap)(const struct _wgl_font_struct *, uint32_t);
/*Pointer to the font in a font pack (must have the same line height)*/
uint8_t line_height; /**< The real line height where any text fits*/
uint8_t base_line; /**< Base line measured from the top of the line_height*/
void * dsc; /**< Store implementation specific data here*/
#if LV_USE_USER_DATA
wgl_font_user_data_t user_data; /**< Custom user data for font. */
#endif
} wgl_font_t;
#if LV_COLOR_DEPTH == 1
#define LV_COLOR_SIZE 8
#elif LV_COLOR_DEPTH == 8
#define LV_COLOR_SIZE 8
#elif LV_COLOR_DEPTH == 16
#define LV_COLOR_SIZE 16
#elif LV_COLOR_DEPTH == 32
#define LV_COLOR_SIZE 32
#else
#error "Invalid LV_COLOR_DEPTH in lv_conf.h! Set it to 1, 8, 16 or 32!"
#endif
/**********************
* TYPEDEFS
**********************/
typedef union
{
uint8_t blue : 1;
uint8_t green : 1;
uint8_t red : 1;
uint8_t full : 1;
} wgl_color1_t;
typedef union
{
struct
{
uint8_t blue : 2;
uint8_t green : 3;
uint8_t red : 3;
} ch;
uint8_t full;
} wgl_color8_t;
typedef union
{
struct
{
#if LV_COLOR_16_SWAP == 0
uint16_t blue : 5;
uint16_t green : 6;
uint16_t red : 5;
#else
uint16_t green_h : 3;
uint16_t red : 5;
uint16_t blue : 5;
uint16_t green_l : 3;
#endif
} ch;
uint16_t full;
} wgl_color16_t;
typedef union
{
struct
{
uint8_t blue;
uint8_t green;
uint8_t red;
uint8_t alpha;
} ch;
uint32_t full;
} wgl_color32_t;
#if LV_COLOR_DEPTH == 1
typedef uint8_t wgl_color_int_t;
typedef wgl_color1_t wgl_color_t;
#elif LV_COLOR_DEPTH == 8
typedef uint8_t wgl_color_int_t;
typedef wgl_color8_t wgl_color_t;
#elif LV_COLOR_DEPTH == 16
typedef uint16_t wgl_color_int_t;
typedef wgl_color16_t wgl_color_t;
#elif LV_COLOR_DEPTH == 32
typedef uint32_t wgl_color_int_t;
typedef wgl_color32_t wgl_color_t;
#else
#error "Invalid LV_COLOR_DEPTH in lv_conf.h! Set it to 1, 8, 16 or 32!"
#endif
typedef uint8_t wgl_opa_t;
/*Border types (Use 'OR'ed values)*/
enum {
WGL_BORDER_NONE = 0x00,
WGL_BORDER_BOTTOM = 0x01,
WGL_BORDER_TOP = 0x02,
WGL_BORDER_LEFT = 0x04,
WGL_BORDER_RIGHT = 0x08,
WGL_BORDER_FULL = 0x0F,
WGL_BORDER_INTERNAL = 0x10, /**< FOR matrix-like objects (e.g. Button matrix)*/
};
typedef uint8_t wgl_border_part_t;
/*Shadow types*/
enum {
WGL_SHADOW_BOTTOM = 0, /**< Only draw bottom shadow */
WGL_SHADOW_FULL, /**< Draw shadow on all sides */
};
typedef uint8_t wgl_shadow_type_t;
/**
* Objects in LittlevGL can be assigned a style - which holds information about
* how the object should be drawn.
*
* This allows for easy customization without having to modify the object's design
* function.
*/
typedef struct
{
uint8_t glass : 1; /**< 1: Do not inherit this style*/
/** Object background. */
struct
{
wgl_color_t main_color; /**< Object's main background color. */
wgl_color_t grad_color; /**< Second color. If not equal to `main_color` a gradient will be drawn for the background. */
wgl_coord_t radius; /**< Object's corner radius. You can use #WGL_RADIUS_CIRCLE if you want to draw a circle. */
wgl_opa_t opa; /**< Object's opacity (0-255). */
struct
{
wgl_color_t color; /**< Border color */
wgl_coord_t width; /**< Border width */
wgl_border_part_t part; /**< Which borders to draw */
wgl_opa_t opa; /**< Border opacity. */
} border;
struct
{
wgl_color_t color;
wgl_coord_t width;
wgl_shadow_type_t type; /**< Which parts of the shadow to draw */
} shadow;
struct
{
wgl_coord_t top;
wgl_coord_t bottom;
wgl_coord_t left;
wgl_coord_t right;
wgl_coord_t inner;
} padding;
} body;
/** Style for text drawn by this object. */
struct
{
wgl_color_t color; /**< Text color */
wgl_color_t sel_color; /**< Text selection background color. */
const wgl_font_t * font;
wgl_coord_t letter_space; /**< Space between letters */
wgl_coord_t line_space; /**< Space between lines (vertical) */
wgl_opa_t opa; /**< Text opacity */
} text;
/**< Style of images. */
struct
{
wgl_color_t color; /**< Color to recolor the image with */
wgl_opa_t intense; /**< Opacity of recoloring (0 means no recoloring) */
wgl_opa_t opa; /**< Opacity of whole image */
} image;
/**< Style of lines (not borders). */
struct
{
wgl_color_t color;
wgl_coord_t width;
wgl_opa_t opa;
uint8_t rounded : 1; /**< 1: rounded line endings*/
} line;
} wgl_style_t;
/* Object native function IDs */
enum {
OBJ_FUNC_ID_DEL,
OBJ_FUNC_ID_DEL_ASYNC,
OBJ_FUNC_ID_CLEAN,
OBJ_FUNC_ID_SET_EVT_CB,
OBJ_FUNC_ID_ALIGN,
/* Number of functions */
_OBJ_FUNC_ID_NUM,
};
/* Button native function IDs */
enum {
BTN_FUNC_ID_CREATE,
BTN_FUNC_ID_SET_TOGGLE,
BTN_FUNC_ID_SET_STATE,
BTN_FUNC_ID_TOGGLE,
BTN_FUNC_ID_SET_INK_IN_TIME,
BTN_FUNC_ID_SET_INK_WAIT_TIME,
BTN_FUNC_ID_SET_INK_OUT_TIME,
BTN_FUNC_ID_GET_STATE,
BTN_FUNC_ID_GET_TOGGLE,
BTN_FUNC_ID_GET_INK_IN_TIME,
BTN_FUNC_ID_GET_INK_WAIT_TIME,
BTN_FUNC_ID_GET_INK_OUT_TIME,
/* Number of functions */
_BTN_FUNC_ID_NUM,
};
/* Check box native function IDs */
enum {
CB_FUNC_ID_CREATE,
CB_FUNC_ID_SET_TEXT,
CB_FUNC_ID_SET_STATIC_TEXT,
CB_FUNC_ID_GET_TEXT,
CB_FUNC_ID_GET_TEXT_LENGTH,
/* Number of functions */
_CB_FUNC_ID_NUM,
};
/* List native function IDs */
enum {
LIST_FUNC_ID_CREATE,
LIST_FUNC_ID_ADD_BTN,
/* Number of functions */
_LIST_FUNC_ID_NUM,
};
/* Label native function IDs */
enum {
LABEL_FUNC_ID_CREATE,
LABEL_FUNC_ID_SET_TEXT,
LABEL_FUNC_ID_SET_ARRAY_TEXT,
LABEL_FUNC_ID_SET_STATIC_TEXT,
LABEL_FUNC_ID_SET_LONG_MODE,
LABEL_FUNC_ID_SET_ALIGN,
LABEL_FUNC_ID_SET_RECOLOR,
LABEL_FUNC_ID_SET_BODY_DRAW,
LABEL_FUNC_ID_SET_ANIM_SPEED,
LABEL_FUNC_ID_SET_TEXT_SEL_START,
LABEL_FUNC_ID_SET_TEXT_SEL_END,
LABEL_FUNC_ID_GET_TEXT,
LABEL_FUNC_ID_GET_TEXT_LENGTH,
LABEL_FUNC_ID_GET_LONG_MODE,
LABEL_FUNC_ID_GET_ALIGN,
LABEL_FUNC_ID_GET_RECOLOR,
LABEL_FUNC_ID_GET_BODY_DRAW,
LABEL_FUNC_ID_GET_ANIM_SPEED,
LABEL_FUNC_ID_GET_LETTER_POS,
LABEL_FUNC_ID_GET_TEXT_SEL_START,
LABEL_FUNC_ID_GET_TEXT_SEL_END,
LABEL_FUNC_ID_INS_TEXT,
LABEL_FUNC_ID_CUT_TEXT,
/* Number of functions */
_LABEL_FUNC_ID_NUM,
};
#ifdef __cplusplus
}
#endif
#endif /* WAMR_GRAPHIC_LIBRARY_SHARED_UTILS_H */

View File

@ -0,0 +1,15 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
set (NATIVE_INTERFACE_DIR ${CMAKE_CURRENT_LIST_DIR})
include_directories(${NATIVE_INTERFACE_DIR})
file (GLOB_RECURSE source_all ${NATIVE_INTERFACE_DIR}/*.c)
set (NATIVE_INTERFACE_SOURCE ${source_all})
set (WASM_APP_BI_INC_DIR "${NATIVE_INTERFACE_DIR}/bi-inc")
LIST (APPEND RUNTIME_LIB_HEADER_LIST "${NATIVE_INTERFACE_DIR}/native_interface.h")

View File

@ -0,0 +1,133 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#ifndef _NATIVE_INTERFACE_H_
#define _NATIVE_INTERFACE_H_
/* Note: the bh_plaform.h is the only head file separately
implemented by both [app] and [native] worlds */
#include "bh_platform.h"
#include "wasm_export.h"
#define get_module_inst(exec_env) \
wasm_runtime_get_module_inst(exec_env)
#define validate_app_addr(offset, size) \
wasm_runtime_validate_app_addr(module_inst, offset, size)
#define validate_app_str_addr(offset) \
wasm_runtime_validate_app_str_addr(module_inst, offset)
#define addr_app_to_native(offset) \
wasm_runtime_addr_app_to_native(module_inst, offset)
#define addr_native_to_app(ptr) \
wasm_runtime_addr_native_to_app(module_inst, ptr)
#define module_malloc(size) \
wasm_runtime_module_malloc(module_inst, size)
#define module_free(offset) \
wasm_runtime_module_free(module_inst, offset)
/*char *wa_strdup(const char *);*/
/*
* request/response interfaces
*/
bool
wasm_response_send(wasm_exec_env_t exec_env,
int32 buffer_offset, int size);
void
wasm_register_resource(wasm_exec_env_t exec_env,
int32 url_offset);
void
wasm_post_request(wasm_exec_env_t exec_env,
int32 buffer_offset, int size);
void
wasm_sub_event(wasm_exec_env_t exec_env,
int32 url_offset);
/*
* sensor interfaces
*/
bool
wasm_sensor_config(wasm_exec_env_t exec_env,
uint32 sensor, int interval, int bit_cfg, int delay);
uint32
wasm_sensor_open(wasm_exec_env_t exec_env,
int32 name_offset, int instance);
bool
wasm_sensor_config_with_attr_container(wasm_exec_env_t exec_env,
uint32 sensor,
int32 buffer_offset, int len);
bool
wasm_sensor_close(wasm_exec_env_t exec_env,
uint32 sensor);
/*
* timer interfaces
*/
typedef unsigned int timer_id_t;
timer_id_t
wasm_create_timer(wasm_exec_env_t exec_env,
int interval, bool is_period, bool auto_start);
void
wasm_timer_destroy(wasm_exec_env_t exec_env, timer_id_t timer_id);
void
wasm_timer_cancel(wasm_exec_env_t exec_env, timer_id_t timer_id);
void
wasm_timer_restart(wasm_exec_env_t exec_env,
timer_id_t timer_id, int interval);
uint32
wasm_get_sys_tick_ms(wasm_exec_env_t exec_env);
/*
* connection interfaces
*/
uint32
wasm_open_connection(wasm_exec_env_t exec_env,
int32 name_offset, int32 args_offset, uint32 len);
void
wasm_close_connection(wasm_exec_env_t exec_env,
uint32 handle);
int
wasm_send_on_connection(wasm_exec_env_t exec_env,
uint32 handle, int32 data_offset, uint32 len);
bool
wasm_config_connection(wasm_exec_env_t exec_env,
uint32 handle, int32 cfg_offset, uint32 len);
/**
* gui interfaces
*/
void
wasm_obj_native_call(wasm_exec_env_t exec_env,
int32 func_id, uint32 argv_offset, uint32 argc);
void
wasm_btn_native_call(wasm_exec_env_t exec_env,
int32 func_id, uint32 argv_offset, uint32 argc);
void
wasm_label_native_call(wasm_exec_env_t exec_env,
int32 func_id, uint32 argv_offset, uint32 argc);
void
wasm_cb_native_call(wasm_exec_env_t exec_env,
int32 func_id, uint32 argv_offset, uint32 argc);
void
wasm_list_native_call(wasm_exec_env_t exec_env,
int32 func_id, uint32 argv_offset, uint32 argc);
#endif /* end of _NATIVE_INTERFACE_H */

View File

@ -0,0 +1,418 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
#include "bi-inc/shared_utils.h"
/* Serialization of request and response message
*
* Choices:
* We considered a few options:
* 1. coap
* 2. flatbuffer
* 3. cbor
* 4. attr-containers of our own
* 5. customized serialization for request/response
*
* Now we choose the #5 mainly because we need to quickly get the URL for dispatching
* and sometimes we want to change the URL in the original packet. the request format:
* fixed part: version: (1 byte), code (1 byte), fmt(2 byte), mid (4 bytes), sender_id(4 bytes), url_len(2 bytes), payload_len(4bytes)
* dynamic part: url (bytes in url_len), payload
*
* response format:
* fixed part: (1 byte), code (1 byte), fmt(2 byte), mid (4 bytes), sender_id(4 bytes), payload_len(4bytes)
* dynamic part: payload
*/
#define REQUES_PACKET_VER 1
#define REQUEST_PACKET_FIX_PART_LEN 18
#define REQUEST_PACKET_URL_OFFSET REQUEST_PACKET_FIX_PART_LEN
#define REQUEST_PACKET_URL_LEN *((uint16*)( (char*) buffer + 12))) //!!! to ensure little endian
#define REQUEST_PACKET_PAYLOAD_LEN *((uint32*)( (char*) buffer + 14))) //!!! to ensure little endian
#define REQUEST_PACKET_URL(buffer) ((char*) buffer + REQUEST_PACKET_URL_OFFSET)
#define REQUEST_PACKET_PAYLOAD(buffer) ((char*) buffer + REQUEST_PACKET_URL_OFFSET + REQUEST_PACKET_URL_LEN(buffer))
#define RESPONSE_PACKET_FIX_PART_LEN 16
char * pack_request(request_t *request, int * size)
{
int url_len = strlen(request->url) + 1;
int len = REQUEST_PACKET_FIX_PART_LEN + url_len + request->payload_len;
char * packet = (char*) wa_malloc(len);
if (packet == NULL)
return NULL;
// TODO: ensure little endian for words and dwords
*packet = REQUES_PACKET_VER;
*((uint8*) (packet + 1)) = request->action;
*((uint16*) (packet + 2)) = htons(request->fmt);
*((uint32*) (packet + 4)) = htonl(request->mid);
*((uint32*) (packet + 8)) = htonl(request->sender);
*((uint16*) (packet + 12)) = htons(url_len);
*((uint32*) (packet + 14)) = htonl(request->payload_len);
strcpy(packet + REQUEST_PACKET_URL_OFFSET, request->url);
memcpy(packet + REQUEST_PACKET_URL_OFFSET + url_len, request->payload,
request->payload_len);
*size = len;
return packet;
}
void free_req_resp_packet(char * packet)
{
wa_free(packet);
}
request_t * unpack_request(char * packet, int size, request_t * request)
{
if (*packet != REQUES_PACKET_VER) {
printf("version fail\n");
return NULL;
}
if (size < REQUEST_PACKET_FIX_PART_LEN) {
printf("size error: %d\n", size);
return NULL;
}
uint16 url_len = ntohs(*((uint16*) (packet + 12)));
uint32 payload_len = ntohl(*((uint32*) (packet + 14)));
if (size != ( REQUEST_PACKET_FIX_PART_LEN + url_len + payload_len)) {
printf("size error: %d, expect: %d\n", size,
REQUEST_PACKET_FIX_PART_LEN + url_len + payload_len);
return NULL;
}
if (*(packet + REQUEST_PACKET_FIX_PART_LEN + url_len - 1) != 0) {
printf("url not end with 0\n");
return NULL;
}
request->action = *((uint8*) (packet + 1));
request->fmt = ntohs(*((uint16*) (packet + 2)));
request->mid = ntohl(*((uint32*) (packet + 4)));
request->sender = ntohl(*((uint32*) (packet + 8)));
request->payload_len = payload_len;
request->url = REQUEST_PACKET_URL(packet);
if (payload_len > 0)
request->payload = packet + REQUEST_PACKET_URL_OFFSET + url_len;
else
request->payload = NULL;
return request;
}
char * pack_response(response_t *response, int * size)
{
int len = RESPONSE_PACKET_FIX_PART_LEN + response->payload_len;
char * packet = (char*) wa_malloc(len);
if (packet == NULL)
return NULL;
// TODO: ensure little endian for words and dwords
*packet = REQUES_PACKET_VER;
*((uint8*) (packet + 1)) = response->status;
*((uint16*) (packet + 2)) = htons(response->fmt);
*((uint32*) (packet + 4)) = htonl(response->mid);
*((uint32*) (packet + 8)) = htonl(response->reciever);
*((uint32*) (packet + 12)) = htonl(response->payload_len);
memcpy(packet + RESPONSE_PACKET_FIX_PART_LEN, response->payload,
response->payload_len);
*size = len;
return packet;
}
response_t * unpack_response(char * packet, int size, response_t * response)
{
if (*packet != REQUES_PACKET_VER)
return NULL;
if (size < RESPONSE_PACKET_FIX_PART_LEN)
return NULL;
uint32 payload_len = ntohl(*((uint32*) (packet + 12)));
if (size != ( RESPONSE_PACKET_FIX_PART_LEN + payload_len))
return NULL;
response->status = *((uint8*) (packet + 1));
response->fmt = ntohs(*((uint16*) (packet + 2)));
response->mid = ntohl(*((uint32*) (packet + 4)));
response->reciever = ntohl(*((uint32*) (packet + 8)));
response->payload_len = payload_len;
if (payload_len > 0)
response->payload = packet + RESPONSE_PACKET_FIX_PART_LEN;
else
response->payload = NULL;
return response;
}
request_t *clone_request(request_t *request)
{
/* deep clone */
request_t *req = (request_t *) wa_malloc(sizeof(request_t));
if (req == NULL)
return NULL;
memset(req, 0, sizeof(*req));
req->action = request->action;
req->fmt = request->fmt;
req->url = wa_strdup(request->url);
req->sender = request->sender;
req->mid = request->mid;
if (req->url == NULL)
goto fail;
req->payload_len = request->payload_len;
if (request->payload_len) {
req->payload = (char *) wa_malloc(request->payload_len);
if (!req->payload)
goto fail;
memcpy(req->payload, request->payload, request->payload_len);
} else {
// when payload_len is 0, the payload may be used for carrying some handle or integer
req->payload = request->payload;
}
return req;
fail:
request_cleaner(req);
return NULL;
}
void request_cleaner(request_t *request)
{
if (request->url != NULL)
wa_free(request->url);
if (request->payload != NULL && request->payload_len > 0)
wa_free(request->payload);
wa_free(request);
}
void response_cleaner(response_t * response)
{
if (response->payload != NULL && response->payload_len > 0)
wa_free(response->payload);
wa_free(response);
}
response_t * clone_response(response_t * response)
{
response_t *clone = (response_t *) wa_malloc(sizeof(response_t));
if (clone == NULL)
return NULL;
memset(clone, 0, sizeof(*clone));
clone->fmt = response->fmt;
clone->mid = response->mid;
clone->status = response->status;
clone->reciever = response->reciever;
clone->payload_len = response->payload_len;
if (clone->payload_len) {
clone->payload = (char *) wa_malloc(response->payload_len);
if (!clone->payload)
goto fail;
memcpy(clone->payload, response->payload, response->payload_len);
} else {
// when payload_len is 0, the payload may be used for carrying some handle or integer
clone->payload = response->payload;
}
return clone;
fail:
response_cleaner(clone);
return NULL;
}
response_t * set_response(response_t * response, int status, int fmt,
const char *payload, int payload_len)
{
response->payload = (void *)payload;
response->payload_len = payload_len;
response->status = status;
response->fmt = fmt;
return response;
}
response_t * make_response_for_request(request_t * request,
response_t * response)
{
response->mid = request->mid;
response->reciever = request->sender;
return response;
}
request_t * init_request(request_t * request, char *url, int action, int fmt,
void *payload, int payload_len)
{
static unsigned int mid = 0;
request->url = url;
request->action = action;
request->fmt = fmt;
request->payload = payload;
request->payload_len = payload_len;
request->mid = ++mid;
return request;
}
/*
check if the "url" is starting with "leading_str"
return: 0 - not match; >0 - the offset of matched url, include any "/" at the end
notes:
1. it ensures the leading_str "/abc" can pass "/abc/cde" and "/abc/, but fail "/ab" and "/abcd".
leading_str "/abc/" can pass "/abc"
2. it omit the '/' at the first char
3. it ensure the leading_str "/abc" can pass "/abc?cde
*/
int check_url_start(const char* url, int url_len, const char * leading_str)
{
int offset = 0;
if (*leading_str == '/')
leading_str++;
if (url_len > 0 && *url == '/') {
url_len--;
url++;
offset++;
}
int len = strlen(leading_str);
if (len == 0)
return 0;
// ensure leading_str not end with "/"
if (leading_str[len - 1] == '/') {
len--;
if (len == 0)
return 0;
}
// equal length
if (url_len == len) {
if (memcmp(url, leading_str, url_len) == 0) {
return (offset + len);
} else {
return 0;
}
}
if (url_len < len)
return 0;
else if (memcmp(url, leading_str, len) != 0)
return 0;
else if (url[len] != '/' && url[len] != '?')
return 0;
else
return (offset + len + 1);
}
// * @pattern:
// * sample 1: /abcd, match /abcd only
// * sample 2: /abcd/ match match "/abcd" and "/abcd/*"
// * sample 3: /abcd*, match any url started with "/abcd"
// * sample 4: /abcd/*, exclude "/abcd"
bool match_url(char * pattern, char * matched)
{
if (*pattern == '/')
pattern++;
if (*matched == '/')
matched++;
int matched_len = strlen(matched);
if (matched_len == 0)
return false;
if (matched[matched_len - 1] == '/') {
matched_len--;
if (matched_len == 0)
return false;
}
int len = strlen(pattern);
if (len == 0)
return false;
if (pattern[len - 1] == '/') {
len--;
if (strncmp(pattern, matched, len) != 0)
return false;
if (len == matched_len)
return true;
if (matched_len > len && matched[len] == '/')
return true;
return false;
} else if (pattern[len - 1] == '*') {
if (pattern[len - 2] == '/') {
if (strncmp(pattern, matched, len - 1) == 0)
return true;
else
return false;
} else {
return (strncmp(pattern, matched, len - 1) == 0);
}
} else {
return (strcmp(pattern, matched) == 0);
}
}
/*
* get the value of the key from following format buffer:
* key1=value1;key2=value2;key3=value3
*/
char * find_key_value(char * buffer, int buffer_len, char * key, char * value,
int value_len, char delimiter)
{
char * p = buffer;
int remaining = buffer_len;
int key_len = strlen(key);
while (*p != 0 && remaining > 0) {
while (*p == ' ' || *p == delimiter) {
p++;
remaining--;
}
if (remaining <= key_len)
return NULL;
// find the key
if (0 == strncmp(p, key, key_len) && p[key_len] == '=') {
p += (key_len + 1);
remaining -= (key_len + 1);
char * v = value;
memset(value, 0, value_len);
value_len--; // ensure last char is 0
while (*p != delimiter && remaining > 0 && value_len > 0) {
*v++ = *p++;
remaining--;
value_len--;
}
return value;
}
// goto next key
while (*p != delimiter && remaining > 0) {
p++;
remaining--;
}
}
return NULL;
}

View File

@ -0,0 +1,86 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
set (APP_FRAMEWORK_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR})
# app-native-shared and base are required
include (${APP_FRAMEWORK_ROOT_DIR}/app-native-shared/native_interface.cmake)
LIST (APPEND WASM_APP_SOURCE_ALL ${NATIVE_INTERFACE_SOURCE})
MACRO(SUBDIRLIST result curdir)
FILE(GLOB children RELATIVE ${curdir} ${curdir}/*)
SET(dirlist "")
FOREACH(child ${children})
IF(IS_DIRECTORY ${curdir}/${child})
LIST(APPEND dirlist ${child})
ENDIF()
ENDFOREACH()
SET(${result} ${dirlist})
ENDMACRO()
function (add_module_native arg)
message ("Add native module ${ARGV0}")
include (${APP_FRAMEWORK_ROOT_DIR}/${ARGV0}/native/wasm_lib.cmake)
file (GLOB header
${APP_FRAMEWORK_ROOT_DIR}/${ARGV0}/native/*.h
${APP_FRAMEWORK_ROOT_DIR}/${ARGV0}/native/*.inl
)
LIST (APPEND RUNTIME_LIB_HEADER_LIST ${header})
set (RUNTIME_LIB_HEADER_LIST ${RUNTIME_LIB_HEADER_LIST} PARENT_SCOPE)
LIST (APPEND WASM_APP_LIB_SOURCE_ALL ${WASM_APP_LIB_CURRENT_SOURCE})
set (WASM_APP_LIB_SOURCE_ALL ${WASM_APP_LIB_SOURCE_ALL} PARENT_SCOPE)
# VARIABLES in function are only used in this scope,
# set PARENT_SCOPE to pass to top CMakeLists
set (WASM_LIB_BASE_SOURCE ${WASM_LIB_BASE_SOURCE} PARENT_SCOPE)
endfunction ()
function (add_module_app arg)
message ("Add app module ${ARGV0}")
include (${APP_FRAMEWORK_ROOT_DIR}/${ARGV0}/app/wasm_app.cmake)
LIST (APPEND WASM_APP_WA_INC_DIR_LIST "${APP_FRAMEWORK_ROOT_DIR}/${ARGV0}/app/wa-inc")
set (WASM_APP_WA_INC_DIR_LIST ${WASM_APP_WA_INC_DIR_LIST} PARENT_SCOPE)
LIST (APPEND WASM_APP_NAME ${ARGV0})
set (WASM_APP_NAME ${WASM_APP_NAME} PARENT_SCOPE)
LIST (APPEND WASM_APP_SOURCE_ALL ${WASM_APP_CURRENT_SOURCE})
set (WASM_APP_SOURCE_ALL ${WASM_APP_SOURCE_ALL} PARENT_SCOPE)
endfunction ()
if ("${WAMR_BUILD_APP_LIST}" STREQUAL "WAMR_APP_BUILD_ALL")
# add all modules under this folder
SUBDIRLIST(SUBDIRS ${APP_FRAMEWORK_ROOT_DIR})
FOREACH(subdir ${SUBDIRS})
if ("${subdir}" STREQUAL "app-native-shared")
continue()
endif ()
if ("${subdir}" STREQUAL "template")
continue()
endif ()
if ( NOT DEFINED APP_FRAMEWORK_INCLUDE_TYPE )
add_module_native (${subdir})
else ()
add_module_app (${subdir})
endif ()
ENDFOREACH()
else ()
# add each module in the list
FOREACH (dir IN LISTS WAMR_BUILD_APP_LIST)
string(REPLACE "WAMR_APP_BUILD_" "" dir ${dir})
string(TOLOWER ${dir} dir)
if ( NOT DEFINED APP_FRAMEWORK_INCLUDE_TYPE )
add_module_native (${dir})
else ()
add_module_app (${dir})
endif ()
ENDFOREACH (dir)
endif()

View 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;
}

View 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_ */

View 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_ */

View 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);
}

View 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;
}
}

View 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_ */

View 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

View 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

View 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)

View 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_ */

View 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);
}

View 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_ */

View 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_ */

View 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);
}
}

View 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_ */

View 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_ */

View 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();
}

View 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})

View File

@ -0,0 +1,119 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "wa-inc/connection.h"
#include "connection_api.h"
/* Raw connection structure */
typedef struct _connection {
/* Next connection */
struct _connection *next;
/* Handle of the connection */
uint32 handle;
/* Callback function called when event on this connection occurs */
on_connection_event_f on_event;
/* User data */
void *user_data;
} connection_t;
/* Raw connections list */
static connection_t *g_conns = NULL;
connection_t *api_open_connection(const char *name,
attr_container_t *args,
on_connection_event_f on_event,
void *user_data)
{
connection_t *conn;
char *args_buffer = (char *)args;
uint32 handle, args_len = attr_container_get_serialize_length(args);
handle = wasm_open_connection(name, args_buffer, args_len);
if (handle == -1)
return NULL;
conn = (connection_t *)malloc(sizeof(*conn));
if (conn == NULL) {
wasm_close_connection(handle);
return NULL;
}
memset(conn, 0, sizeof(*conn));
conn->handle = handle;
conn->on_event = on_event;
conn->user_data = user_data;
if (g_conns != NULL) {
conn->next = g_conns;
g_conns = conn;
} else {
g_conns = conn;
}
return conn;
}
void api_close_connection(connection_t *c)
{
connection_t *conn = g_conns, *prev = NULL;
while (conn) {
if (conn == c) {
wasm_close_connection(c->handle);
if (prev != NULL)
prev->next = conn->next;
else
g_conns = conn->next;
free(conn);
return;
} else {
prev = conn;
conn = conn->next;
}
}
}
int api_send_on_connection(connection_t *conn, const char *data, uint32 len)
{
return wasm_send_on_connection(conn->handle, data, len);
}
bool api_config_connection(connection_t *conn, attr_container_t *cfg)
{
char *cfg_buffer = (char *)cfg;
uint32 cfg_len = attr_container_get_serialize_length(cfg);
return wasm_config_connection(conn->handle, cfg_buffer, cfg_len);
}
void on_connection_data(uint32 handle, char *buffer, uint32 len)
{
connection_t *conn = g_conns;
while (conn != NULL) {
if (conn->handle == handle) {
if (len == 0) {
conn->on_event(conn,
CONN_EVENT_TYPE_DISCONNECT,
NULL,
0,
conn->user_data);
} else {
conn->on_event(conn,
CONN_EVENT_TYPE_DATA,
buffer,
len,
conn->user_data);
}
return;
}
conn = conn->next;
}
}

View File

@ -0,0 +1,32 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#ifndef CONNECTION_API_H_
#define CONNECTION_API_H_
#include "bh_platform.h"
#ifdef __cplusplus
extern "C" {
#endif
uint32
wasm_open_connection(const char *name, char *args_buf, uint32 args_buf_len);
void
wasm_close_connection(uint32 handle);
int
wasm_send_on_connection(uint32 handle, const char *data, uint32 data_len);
bool
wasm_config_connection(uint32 handle, const char *cfg_buf, uint32 cfg_buf_len);
#ifdef __cplusplus
}
#endif
#endif /* end of CONNECTION_API_H_ */

View File

@ -0,0 +1,95 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#ifndef _CONNECTION_H_
#define _CONNECTION_H_
#include "bi-inc/attr_container.h"
#ifdef __cplusplus
extern "C" {
#endif
struct _connection;
typedef struct _connection connection_t;
/* Connection event type */
typedef enum {
/* Data is received */
CONN_EVENT_TYPE_DATA = 1,
/* Connection is disconnected */
CONN_EVENT_TYPE_DISCONNECT
} conn_event_type_t;
/*
* @typedef on_connection_event_f
*
* @param conn the connection that the event belongs to
* @param type event type
* @param data the data received for CONN_EVENT_TYPE_DATA event
* @param len length of the data in byte
* @param user_data user data
*/
typedef void (*on_connection_event_f)(connection_t *conn,
conn_event_type_t type,
const char *data,
uint32 len,
void *user_data);
/*
*****************
* Connection API's
*****************
*/
/*
* @brief Open a connection.
*
* @param name name of the connection, "TCP", "UDP" or "UART"
* @param args connection arguments, such as: ip:127.0.0.1, port:8888
* @param on_event callback function called when event occurs
* @param user_data user data
*
* @return the connection or NULL means fail
*/
connection_t *api_open_connection(const char *name,
attr_container_t *args,
on_connection_event_f on_event,
void *user_data);
/*
* @brief Close a connection.
*
* @param conn connection
*/
void api_close_connection(connection_t *conn);
/*
* Send data to the connection in non-blocking manner which returns immediately
*
* @param conn the connection
* @param data data buffer to be sent
* @param len length of the data in byte
*
* @return actual length sent, or -1 if fail(maybe underlying buffer is full)
*/
int api_send_on_connection(connection_t *conn, const char *data, uint32 len);
/*
* @brief Configure connection.
*
* @param conn the connection
* @param cfg configurations
*
* @return true if success, false otherwise
*/
bool api_config_connection(connection_t *conn, attr_container_t *cfg);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,11 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
set (WASM_APP_CONN_DIR ${CMAKE_CURRENT_LIST_DIR})
include_directories(${WASM_APP_CONN_DIR})
file (GLOB source_all ${WASM_APP_CONN_DIR}/*.c)
set (WASM_APP_CURRENT_SOURCE ${source_all})

View File

@ -0,0 +1,9 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
EXPORT_WASM_API(wasm_open_connection),
EXPORT_WASM_API(wasm_close_connection),
EXPORT_WASM_API(wasm_send_on_connection),
EXPORT_WASM_API(wasm_config_connection),

View File

@ -0,0 +1,32 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#ifndef CONNECTION_API_H_
#define CONNECTION_API_H_
#include "bh_platform.h"
#ifdef __cplusplus
extern "C" {
#endif
uint32
wasm_open_connection(int32 name_offset, int32 args_buf_offset, uint32 args_buf_len);
void
wasm_close_connection(uint32 handle);
int
wasm_send_on_connection(uint32 handle, int32 data_offset, uint32 data_len);
bool
wasm_config_connection(uint32 handle, int32 cfg_buf_offset, uint32 cfg_buf_len);
#ifdef __cplusplus
}
#endif
#endif /* end of CONNECTION_API_H_ */

View File

@ -0,0 +1,77 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#ifndef CONNECTION_LIB_H_
#define CONNECTION_LIB_H_
#include "bi-inc/attr_container.h"
#include "wasm_export.h"
#ifdef __cplusplus
extern "C" {
#endif
/*
*****************
* This file defines connection library which should be implemented by different platforms
*****************
*/
/*
* @brief Open a connection.
*
* @param name name of the connection, "TCP", "UDP" or "UART"
* @param args connection arguments, such as: ip:127.0.0.1, port:8888
*
* @return 0~0xFFFFFFFE means id of the connection, otherwise(-1) means fail
*/
typedef uint32 (*connection_open_f)(wasm_module_inst_t module_inst,
const char *name, attr_container_t *args);
/*
* @brief Close a connection.
*
* @param handle of the connection
*/
typedef void (*connection_close_f)(uint32 handle);
/*
* @brief Send data to the connection in non-blocking manner.
*
* @param handle of the connection
* @param data data buffer to be sent
* @param len length of the data in byte
*
* @return actual length sent, -1 if fail
*/
typedef int (*connection_send_f)(uint32 handle, const char *data, int len);
/*
* @brief Configure connection.
*
* @param handle of the connection
* @param cfg configurations
*
* @return true if success, false otherwise
*/
typedef bool (*connection_config_f)(uint32 handle, attr_container_t *cfg);
/* Raw connection interface for platform to implement */
typedef struct _connection_interface {
connection_open_f _open;
connection_close_f _close;
connection_send_f _send;
connection_config_f _config;
} connection_interface_t;
/* Platform must define this interface */
extern connection_interface_t connection_impl;
#ifdef __cplusplus
}
#endif
#endif /* CONNECTION_LIB_H_ */

View File

@ -0,0 +1,79 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "connection_lib.h"
#include "wasm_export.h"
#include "native_interface.h"
/* Note:
*
* This file is the consumer of connection lib which is implemented by different platforms
*/
uint32
wasm_open_connection(wasm_exec_env_t exec_env,
int32 name_offset, int32 args_offset, uint32 len)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
attr_container_t *args;
char *name, *args_buf;
if (!validate_app_str_addr(name_offset) ||
!validate_app_addr(args_offset, len) ||
!(name = addr_app_to_native(name_offset)) ||
!(args_buf = addr_app_to_native(args_offset)))
return -1;
args = (attr_container_t *)args_buf;
if (connection_impl._open != NULL)
return connection_impl._open(module_inst, name, args);
return -1;
}
void
wasm_close_connection(wasm_exec_env_t exec_env, uint32 handle)
{
if (connection_impl._close != NULL)
connection_impl._close(handle);
}
int
wasm_send_on_connection(wasm_exec_env_t exec_env,
uint32 handle, int32 data_offset, uint32 len)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
char *data;
if (!validate_app_addr(data_offset, len) ||
!(data = addr_app_to_native(data_offset)))
return -1;
if (connection_impl._send != NULL)
return connection_impl._send(handle, data, len);
return -1;
}
bool
wasm_config_connection(wasm_exec_env_t exec_env,
uint32 handle, int32 cfg_offset, uint32 len)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
char *cfg_buf;
attr_container_t *cfg;
if (!validate_app_addr(cfg_offset, len) ||
!(cfg_buf = addr_app_to_native(cfg_offset)))
return false;
cfg = (attr_container_t *)cfg_buf;
if (connection_impl._config != NULL)
return connection_impl._config(handle, cfg);
return false;
}

View File

@ -0,0 +1,51 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "conn_tcp.h"
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <unistd.h>
int tcp_open(char *address, uint16 port)
{
int sock, ret;
struct sockaddr_in servaddr;
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr(address);
servaddr.sin_port = htons(port);
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock == -1)
return -1;
ret = connect(sock, (struct sockaddr*)&servaddr, sizeof(servaddr));
if (ret == -1) {
close(sock);
return -1;
}
/* Put the socket in non-blocking mode */
if (fcntl(sock, F_SETFL, fcntl(sock, F_GETFL) | O_NONBLOCK) < 0) {
close(sock);
return -1;
}
return sock;
}
int tcp_send(int sock, const char *data, int size)
{
return send(sock, data, size, 0);
}
int tcp_recv(int sock, char *buffer, int buf_size)
{
return recv(sock, buffer, buf_size, 0);
}

View File

@ -0,0 +1,26 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#ifndef CONN_LINUX_TCP_H_
#define CONN_LINUX_TCP_H_
#include "bh_platform.h"
#ifdef __cplusplus
extern "C" {
#endif
int tcp_open(char *address, uint16 port);
int tcp_send(int sock, const char *data, int size);
int tcp_recv(int sock, char *buffer, int buf_size);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,99 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "conn_uart.h"
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
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;
}
}
int uart_open(char* device, int baudrate)
{
int uart_fd;
struct termios uart_term;
uart_fd = open(device, O_RDWR | O_NOCTTY);
if (uart_fd < 0)
return -1;
memset(&uart_term, 0, sizeof(uart_term));
uart_term.c_cflag = parse_baudrate(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 -1;
}
/* Put the fd in non-blocking mode */
if (fcntl(uart_fd, F_SETFL, fcntl(uart_fd, F_GETFL) | O_NONBLOCK) < 0) {
close(uart_fd);
return -1;
}
return uart_fd;
}
int uart_send(int fd, const char *data, int size)
{
return write(fd, data, size);
}
int uart_recv(int fd, char *buffer, int buf_size)
{
return read(fd, buffer, buf_size);
}

View File

@ -0,0 +1,26 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#ifndef CONN_LINUX_UART_H_
#define CONN_LINUX_UART_H_
#include "bh_platform.h"
#ifdef __cplusplus
extern "C" {
#endif
int uart_open(char* device, int baudrate);
int uart_send(int fd, const char *data, int size);
int uart_recv(int fd, char *buffer, int buf_size);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,59 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "conn_udp.h"
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <unistd.h>
int udp_open(uint16 port)
{
int sock, ret;
struct sockaddr_in addr;
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock == -1)
return -1;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(port);
ret = bind(sock, (struct sockaddr*)&addr, sizeof(addr));
if (ret == -1) {
close(sock);
return -1;
}
/* Put the socket in non-blocking mode */
if (fcntl(sock, F_SETFL, fcntl(sock, F_GETFL) | O_NONBLOCK) < 0) {
close(sock);
return -1;
}
return sock;
}
int udp_send(int sock, struct sockaddr *dest, const char *data, int size)
{
return sendto(sock, data, size, MSG_CONFIRM, dest, sizeof(*dest));
}
int udp_recv(int sock, char *buffer, int buf_size)
{
struct sockaddr_in remaddr;
socklen_t addrlen = sizeof(remaddr);
return recvfrom(sock,
buffer,
buf_size,
0,
(struct sockaddr *)&remaddr,
&addrlen);
}

View File

@ -0,0 +1,26 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#ifndef CONN_LINUX_UDP_H_
#define CONN_LINUX_UDP_H_
#include "bh_platform.h"
#ifdef __cplusplus
extern "C" {
#endif
int udp_open(uint16 port);
int udp_send(int sock, struct sockaddr *dest, const char *data, int size);
int udp_recv(int sock, char *buffer, int buf_size);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,587 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
/*
* Note:
* This file implements the linux version connection library which is
* defined in connection_lib.h.
* It also provides a reference implementation of connections manager.
*/
#include "connection_lib.h"
#include "bh_thread.h"
#include "app_manager_export.h"
#include "module_wasm_app.h"
#include "conn_tcp.h"
#include "conn_udp.h"
#include "conn_uart.h"
#include "bh_common.h"
#include "bh_assert.h"
#include <unistd.h>
#include <sys/epoll.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <fcntl.h>
#define MAX_EVENTS 10
#define IO_BUF_SIZE 256
static bool polling_thread_run = true;
/* Connection type */
typedef enum conn_type {
CONN_TYPE_TCP,
CONN_TYPE_UDP,
CONN_TYPE_UART,
CONN_TYPE_UNKNOWN
} conn_type_t;
/* Sys connection */
typedef struct sys_connection {
/* Next connection */
struct sys_connection *next;
/* Type */
conn_type_t type;
/* Handle to interact with wasm app */
uint32 handle;
/* Underlying connection ID, may be socket fd */
int fd;
/* Module id that the connection belongs to */
uint32 module_id;
/* Argument, such as dest addr for udp */
void *arg;
} sys_connection_t;
/* Epoll instance */
static int epollfd;
/* Connections list */
static sys_connection_t *g_connections = NULL;
/* Max handle */
static uint32 g_handle_max = 0;
/* Lock to protect g_connections and g_handle_max */
static korp_mutex g_lock;
/* Epoll events */
static struct epoll_event epoll_events[MAX_EVENTS];
/* Buffer to receive data */
static char io_buf[IO_BUF_SIZE];
static uint32 _conn_open(wasm_module_inst_t module_inst,
const char *name, attr_container_t *args);
static void _conn_close(uint32 handle);
static int _conn_send(uint32 handle, const char *data, int len);
static bool _conn_config(uint32 handle, attr_container_t *cfg);
/*
* Platform implementation of connection library
*/
connection_interface_t connection_impl = {
._open = _conn_open,
._close = _conn_close,
._send = _conn_send,
._config = _conn_config
};
static void add_connection(sys_connection_t *conn)
{
vm_mutex_lock(&g_lock);
g_handle_max++;
if (g_handle_max == -1)
g_handle_max++;
conn->handle = g_handle_max;
if (g_connections) {
conn->next = g_connections;
g_connections = conn;
} else {
g_connections = conn;
}
vm_mutex_unlock(&g_lock);
}
#define FREE_CONNECTION(conn) do { \
if (conn->arg) \
bh_free(conn->arg); \
bh_free(conn); \
} while (0)
static int get_app_conns_num(uint32 module_id)
{
sys_connection_t *conn;
int num = 0;
vm_mutex_lock(&g_lock);
conn = g_connections;
while (conn) {
if (conn->module_id == module_id)
num++;
conn = conn->next;
}
vm_mutex_unlock(&g_lock);
return num;
}
static sys_connection_t *find_connection(uint32 handle, bool remove_found)
{
sys_connection_t *conn, *prev = NULL;
vm_mutex_lock(&g_lock);
conn = g_connections;
while (conn) {
if (conn->handle == handle) {
if (remove_found) {
if (prev != NULL) {
prev->next = conn->next;
} else {
g_connections = conn->next;
}
}
vm_mutex_unlock(&g_lock);
return conn;
} else {
prev = conn;
conn = conn->next;
}
}
vm_mutex_unlock(&g_lock);
return NULL;
}
static void cleanup_connections(uint32 module_id)
{
sys_connection_t *conn, *prev = NULL;
vm_mutex_lock(&g_lock);
conn = g_connections;
while (conn) {
if (conn->module_id == module_id) {
epoll_ctl(epollfd, EPOLL_CTL_DEL, conn->fd, NULL);
close(conn->fd);
if (prev != NULL) {
prev->next = conn->next;
FREE_CONNECTION(conn);
conn = prev->next;
} else {
g_connections = conn->next;
FREE_CONNECTION(conn);
conn = g_connections;
}
} else {
prev = conn;
conn = conn->next;
}
}
vm_mutex_unlock(&g_lock);
}
static conn_type_t get_conn_type(const char *name)
{
if (strcmp(name, "TCP") == 0)
return CONN_TYPE_TCP;
if (strcmp(name, "UDP") == 0)
return CONN_TYPE_UDP;
if (strcmp(name, "UART") == 0)
return CONN_TYPE_UART;
return CONN_TYPE_UNKNOWN;
}
/* --- connection lib function --- */
static uint32 _conn_open(wasm_module_inst_t module_inst,
const char *name, attr_container_t *args)
{
int fd;
sys_connection_t *conn;
struct epoll_event ev;
uint32 module_id = app_manager_get_module_id(Module_WASM_App,
module_inst);
bh_assert(module_id != ID_NONE);
if (get_app_conns_num(module_id) >= MAX_CONNECTION_PER_APP)
return -1;
conn = (sys_connection_t *)bh_malloc(sizeof(*conn));
if (conn == NULL)
return -1;
memset(conn, 0, sizeof(*conn));
conn->module_id = module_id;
conn->type = get_conn_type(name);
/* Generate a handle and add to list */
add_connection(conn);
if (conn->type == CONN_TYPE_TCP) {
char *address;
uint16 port;
/* Check and parse connection parameters */
if (!attr_container_contain_key(args, "address") ||
!attr_container_contain_key(args, "port"))
goto fail;
address = attr_container_get_as_string(args, "address");
port = attr_container_get_as_uint16(args, "port");
/* Connect to TCP server */
if (!address || (fd = tcp_open(address, port)) == -1)
goto fail;
} else if (conn->type == CONN_TYPE_UDP) {
uint16 port;
/* Check and parse connection parameters */
if (!attr_container_contain_key(args, "bind port"))
goto fail;
port = attr_container_get_as_uint16(args, "bind port");
/* Bind port */
if ((fd = udp_open(port)) == -1)
goto fail;
} else if (conn->type == CONN_TYPE_UART) {
char *device;
int baud;
/* Check and parse connection parameters */
if (!attr_container_contain_key(args, "device") ||
!attr_container_contain_key(args, "baudrate"))
goto fail;
device = attr_container_get_as_string(args, "device");
baud = attr_container_get_as_int(args, "baudrate");
/* Open device */
if (!device || (fd = uart_open(device, baud)) == -1)
goto fail;
} else {
goto fail;
}
conn->fd = fd;
/* Set current connection as event data */
ev.events = EPOLLIN;
ev.data.ptr = conn;
/* Monitor incoming data */
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev) == -1) {
close(fd);
goto fail;
}
return conn->handle;
fail:
find_connection(conn->handle, true);
bh_free(conn);
return -1;
}
/* --- connection lib function --- */
static void _conn_close(uint32 handle)
{
sys_connection_t *conn = find_connection(handle, true);
if (conn != NULL) {
epoll_ctl(epollfd, EPOLL_CTL_DEL, conn->fd, NULL);
close(conn->fd);
FREE_CONNECTION(conn);
}
}
/* --- connection lib function --- */
static int _conn_send(uint32 handle, const char *data, int len)
{
sys_connection_t *conn = find_connection(handle, false);
if (conn == NULL)
return -1;
if (conn->type == CONN_TYPE_TCP)
return tcp_send(conn->fd, data, len);
if (conn->type == CONN_TYPE_UDP) {
struct sockaddr *addr = (struct sockaddr *)conn->arg;
return udp_send(conn->fd, addr, data, len);
}
if (conn->type == CONN_TYPE_UART)
return uart_send(conn->fd, data, len);
return -1;
}
/* --- connection lib function --- */
static bool _conn_config(uint32 handle, attr_container_t *cfg)
{
sys_connection_t *conn = find_connection(handle, false);
if (conn == NULL)
return false;
if (conn->type == CONN_TYPE_UDP) {
char *address;
uint16_t port;
struct sockaddr_in *addr;
/* Parse remote address/port */
if (!attr_container_contain_key(cfg, "address") ||
!attr_container_contain_key(cfg, "port"))
return false;
if (!(address = attr_container_get_as_string(cfg, "address")))
return false;
port = attr_container_get_as_uint16(cfg, "port");
if (conn->arg == NULL) {
addr = (struct sockaddr_in *)bh_malloc(sizeof(*addr));
if (addr == NULL)
return false;
memset(addr, 0, sizeof(*addr));
addr->sin_family = AF_INET;
addr->sin_addr.s_addr = inet_addr(address);
addr->sin_port = htons(port);
/* Set remote address as connection arg */
conn->arg = addr;
} else {
addr = (struct sockaddr_in *)conn->arg;
addr->sin_addr.s_addr = inet_addr(address);
addr->sin_port = htons(port);
}
return true;
}
return false;
}
/* --- connection manager reference implementation ---*/
typedef struct connection_event {
uint32 handle;
char *data;
uint32 len;
} connection_event_t;
static void connection_event_cleaner(connection_event_t *conn_event)
{
if (conn_event->data != NULL)
bh_free(conn_event->data);
bh_free(conn_event);
}
static void post_msg_to_module(sys_connection_t *conn,
char *data,
uint32 len)
{
module_data *module = module_data_list_lookup_id(conn->module_id);
char *data_copy = NULL;
connection_event_t *conn_data_event;
bh_message_t msg;
if (module == NULL)
return;
conn_data_event = (connection_event_t *)bh_malloc(sizeof(*conn_data_event));
if (conn_data_event == NULL)
return;
if (len > 0) {
data_copy = (char *)bh_malloc(len);
if (data_copy == NULL) {
bh_free(conn_data_event);
return;
}
bh_memcpy_s(data_copy, len, data, len);
}
memset(conn_data_event, 0, sizeof(*conn_data_event));
conn_data_event->handle = conn->handle;
conn_data_event->data = data_copy;
conn_data_event->len = len;
msg = bh_new_msg(CONNECTION_EVENT_WASM,
conn_data_event,
sizeof(*conn_data_event),
connection_event_cleaner);
if (!msg) {
connection_event_cleaner(conn_data_event);
return;
}
bh_post_msg2(module->queue, msg);
}
static void* polling_thread_routine (void *arg)
{
while (polling_thread_run) {
int i, n;
n = epoll_wait(epollfd, epoll_events, MAX_EVENTS, -1);
if (n == -1 && errno != EINTR)
continue;
for (i = 0; i < n; i++) {
sys_connection_t *conn
= (sys_connection_t *)epoll_events[i].data.ptr;
if (conn->type == CONN_TYPE_TCP) {
int count = tcp_recv(conn->fd, io_buf, IO_BUF_SIZE);
if (count <= 0) {
/* Connection is closed by peer */
post_msg_to_module(conn, NULL, 0);
_conn_close(conn->handle);
} else {
/* Data is received */
post_msg_to_module(conn, io_buf, count);
}
} else if (conn->type == CONN_TYPE_UDP) {
int count = udp_recv(conn->fd, io_buf, IO_BUF_SIZE);
if (count > 0)
post_msg_to_module(conn, io_buf, count);
} else if (conn->type == CONN_TYPE_UART) {
int count = uart_recv(conn->fd, io_buf, IO_BUF_SIZE);
if (count > 0)
post_msg_to_module(conn, io_buf, count);
}
}
}
return NULL;
}
void app_mgr_connection_event_callback(module_data *m_data, bh_message_t msg)
{
uint32 argv[3];
wasm_function_inst_t func_on_conn_data;
bh_assert(CONNECTION_EVENT_WASM == bh_message_type(msg));
wasm_data *wasm_app_data = (wasm_data*)m_data->internal_data;
wasm_module_inst_t inst = wasm_app_data->wasm_module_inst;
connection_event_t *conn_event
= (connection_event_t *)bh_message_payload(msg);
int32 data_offset;
if (conn_event == NULL)
return;
func_on_conn_data = wasm_runtime_lookup_function(inst, "_on_connection_data",
"(i32i32i32)");
if (!func_on_conn_data)
func_on_conn_data = wasm_runtime_lookup_function(inst, "on_connection_data",
"(i32i32i32)");
if (!func_on_conn_data) {
printf("Cannot find function on_connection_data\n");
return;
}
/* 0 len means connection closed */
if (conn_event->len == 0) {
argv[0] = conn_event->handle;
argv[1] = 0;
argv[2] = 0;
if (!wasm_runtime_call_wasm(wasm_app_data->exec_env, func_on_conn_data,
3, argv)) {
const char *exception = wasm_runtime_get_exception(inst);
bh_assert(exception);
printf(":Got exception running wasm code: %s\n",
exception);
wasm_runtime_clear_exception(inst);
return;
}
} else {
data_offset = wasm_runtime_module_dup_data(inst,
conn_event->data,
conn_event->len);
if (data_offset == 0) {
const char *exception = wasm_runtime_get_exception(inst);
if (exception) {
printf("Got exception running wasm code: %s\n",
exception);
wasm_runtime_clear_exception(inst);
}
return;
}
argv[0] = conn_event->handle;
argv[1] = (uint32) data_offset;
argv[2] = conn_event->len;
if (!wasm_runtime_call_wasm(wasm_app_data->exec_env, func_on_conn_data,
3, argv)) {
const char *exception = wasm_runtime_get_exception(inst);
bh_assert(exception);
printf(":Got exception running wasm code: %s\n",
exception);
wasm_runtime_clear_exception(inst);
wasm_runtime_module_free(inst, data_offset);
return;
}
wasm_runtime_module_free(inst, data_offset);
}
}
bool init_connection_framework()
{
korp_thread tid;
epollfd = epoll_create(MAX_EVENTS);
if (epollfd == -1)
return false;
if (vm_mutex_init(&g_lock) != 0) {
close(epollfd);
return false;
}
if (!wasm_register_cleanup_callback(cleanup_connections)) {
goto fail;
}
if (!wasm_register_msg_callback(CONNECTION_EVENT_WASM,
app_mgr_connection_event_callback)) {
goto fail;
}
if (vm_thread_create(&tid,
polling_thread_routine,
NULL,
BH_APPLET_PRESERVED_STACK_SIZE) != 0) {
goto fail;
}
return true;
fail:
vm_mutex_destroy(&g_lock);
close(epollfd);
return false;
}
void exit_connection_framework()
{
polling_thread_run = false;
}

View 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_CONN_MGR_DIR ${CMAKE_CURRENT_LIST_DIR})
include_directories(${WASM_LIB_CONN_MGR_DIR})
file (GLOB_RECURSE source_all ${WASM_LIB_CONN_MGR_DIR}/*.c)
set (WASM_LIB_CONN_MGR_SOURCE ${source_all})

View File

@ -0,0 +1,15 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
set (WASM_LIB_CONN_DIR ${CMAKE_CURRENT_LIST_DIR})
include_directories(${WASM_LIB_CONN_DIR})
include (${CMAKE_CURRENT_LIST_DIR}/${WAMR_BUILD_PLATFORM}/connection_mgr.cmake)
file (GLOB source_all
${WASM_LIB_CONN_MGR_SOURCE}
${WASM_LIB_CONN_DIR}/*.c
)
set (WASM_APP_LIB_CURRENT_SOURCE ${source_all})

View File

@ -0,0 +1,23 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
/*
* Note:
* This file implements the linux version connection library which is
* defined in connection_lib.h.
* It also provides a reference impl of connections manager.
*/
#include "connection_lib.h"
/*
* Platform implementation of connection library
*/
connection_interface_t connection_impl = {
._open = NULL,
._close = NULL,
._send = NULL,
._config = NULL
};

View 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_CONN_MGR_DIR ${CMAKE_CURRENT_LIST_DIR})
include_directories(${WASM_LIB_CONN_MGR_DIR})
file (GLOB_RECURSE source_all ${WASM_LIB_CONN_MGR_DIR}/*.c)
set (WASM_LIB_CONN_MGR_SOURCE ${source_all})

View File

@ -0,0 +1,114 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "wa-inc/sensor.h"
#include "sensor_api.h"
typedef struct _sensor {
struct _sensor * next;
char *name;
uint32 handle;
void (*sensor_callback)(sensor_t, attr_container_t *, void *);
void *user_data;
} sensor;
static sensor_t g_sensors = NULL;
sensor_t sensor_open(const char* name, int index,
sensor_event_handler_f sensor_event_handler,
void *user_data)
{
uint32 id = wasm_sensor_open(name, index);
if (id == -1)
return NULL;
//create local node for holding the user callback
sensor_t sensor = (sensor_t) malloc(sizeof(struct _sensor));
if (sensor == NULL)
return NULL;
memset(sensor, 0, sizeof(struct _sensor));
sensor->handle = id;
sensor->name = strdup(name);
sensor->user_data = user_data;
sensor->sensor_callback = sensor_event_handler;
if (!sensor->name) {
free(sensor);
return NULL;
}
if (g_sensors == NULL) {
g_sensors = sensor;
} else {
sensor->next = g_sensors;
g_sensors = sensor;
}
return sensor;
}
bool sensor_config_with_attr_container(sensor_t sensor, attr_container_t *cfg)
{
char *buffer = (char *)cfg;
int len = attr_container_get_serialize_length(cfg);
return wasm_sensor_config_with_attr_container(sensor->handle, buffer, len);
}
bool sensor_config(sensor_t sensor, int interval, int bit_cfg, int delay)
{
bool ret = wasm_sensor_config(sensor->handle, interval, bit_cfg, delay);
return ret;
}
bool sensor_close(sensor_t sensor)
{
wasm_sensor_close(sensor->handle);
// remove local node
sensor_t s = g_sensors;
sensor_t prev = NULL;
while (s) {
if (s == sensor) {
if (prev == NULL) {
g_sensors = s->next;
} else {
prev->next = s->next;
}
free(s->name);
free(s);
return true;
} else {
prev = s;
s = s->next;
}
}
return false;
}
/*
*
* API for native layer to callback for sensor events
*
*/
void on_sensor_event(uint32 sensor_id, char * buffer, int len)
{
attr_container_t * sensor_data = (attr_container_t *) buffer;
// lookup the sensor and call the handlers
sensor_t s = g_sensors;
sensor_t prev = NULL;
while (s) {
if (s->handle == sensor_id) {
s->sensor_callback(s, sensor_data, s->user_data);
break;
}
s = s->next;
}
}

View File

@ -0,0 +1,32 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#ifndef _SENSOR_API_H_
#define _SENSOR_API_H_
#include "bh_platform.h"
#ifdef __cplusplus
extern "C" {
#endif
uint32
wasm_sensor_open(const char* name, int instance);
bool
wasm_sensor_config(uint32 sensor, int interval, int bit_cfg, int delay);
bool
wasm_sensor_config_with_attr_container(uint32 sensor, char *buffer, int len);
bool
wasm_sensor_close(uint32 sensor);
#ifdef __cplusplus
}
#endif
#endif /* end of _SENSOR_API_H_ */

View File

@ -0,0 +1,92 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#ifndef _AEE_SENSOR_H_
#define _AEE_SENSOR_H_
#include "bi-inc/attr_container.h"
#ifdef __cplusplus
extern "C" {
#endif
/* board producer define sensor */
struct _sensor;
typedef struct _sensor *sensor_t;
/**
* @typedef sensor_event_handler_f
*
* @brief Define the signature of callback function for API
* sensor_open() to handle sensor event.
*
* @param sensor the sensor which the event belong to
* @param sensor_event the sensor event
* @param user_data user data associated with the sensor which is set when
* calling sensor_open().
*
* @see sensor_open
*/
typedef void (*sensor_event_handler_f)(sensor_t sensor,
attr_container_t *sensor_event,
void *user_data);
/*
*****************
* Sensor APIs
*****************
*/
/**
* @brief Open sensor.
*
* @param name sensor name
* @param index sensor index
* @param handler callback function to handle the sensor event
* @param user_data user data
*
* @return the sensor opened if success, NULL otherwise
*/
sensor_t sensor_open(const char* name,
int index,
sensor_event_handler_f handler,
void *user_data);
/**
* @brief Configure sensor with interval/bit_cfg/delay values.
*
* @param sensor the sensor to be configured
* @param interval sensor event interval
* @param bit_cfg sensor bit config
* @param delay sensor delay
*
* @return true if success, false otherwise
*/
bool sensor_config(sensor_t sensor, int interval, int bit_cfg, int delay);
/**
* @brief Configure sensor with attr_container_t object.
*
* @param sensor the sensor to be configured
* @param cfg the configuration
*
* @return true if success, false otherwise
*/
bool sensor_config_with_attr_container(sensor_t sensor, attr_container_t *cfg);
/**
* @brief Close sensor.
*
* @param sensor the sensor to be closed
*
* @return true if success, false otherwise
*/
bool sensor_close(sensor_t sensor);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,11 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
set (WASM_APP_SENSOR_DIR ${CMAKE_CURRENT_LIST_DIR})
include_directories(${WASM_APP_SENSOR_DIR})
file (GLOB_RECURSE source_all ${WASM_APP_SENSOR_DIR}/*.c)
set (WASM_APP_CURRENT_SOURCE ${source_all})

View File

@ -0,0 +1,437 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "runtime_sensor.h"
#include "app_manager_export.h"
#include "module_wasm_app.h"
#include "bh_thread.h"
#include "bh_time.h"
#include "bh_common.h"
#include "bh_assert.h"
static sys_sensor_t * g_sys_sensors = NULL;
static int g_sensor_id_max = 0;
static sensor_client_t *
find_sensor_client(sys_sensor_t * sensor,
unsigned int client_id, bool remove_if_found);
void (*rechedule_sensor_callback)() = NULL;
/*
* API for the applications to call - don't call it from the runtime
*
*/
static void
sensor_event_cleaner(sensor_event_data_t *sensor_event)
{
if (sensor_event->data != NULL) {
if (sensor_event->data_fmt == FMT_ATTR_CONTAINER)
attr_container_destroy(sensor_event->data);
else
bh_free(sensor_event->data);
}
bh_free(sensor_event);
}
static void
wasm_sensor_callback(void *client, uint32 sensor_id, void *user_data)
{
attr_container_t *sensor_data = (attr_container_t *) user_data;
attr_container_t *sensor_data_clone;
int sensor_data_len;
sensor_event_data_t *sensor_event;
bh_message_t msg;
sensor_client_t *c = (sensor_client_t *) client;
module_data *module = module_data_list_lookup_id(c->client_id);
if (module == NULL)
return;
if (sensor_data == NULL)
return;
sensor_data_len = attr_container_get_serialize_length(sensor_data);
sensor_data_clone = (attr_container_t *)bh_malloc(sensor_data_len);
if (sensor_data_clone == NULL)
return;
/* multiple sensor clients may use/free the sensor data, so make a copy */
bh_memcpy_s(sensor_data_clone, sensor_data_len,
sensor_data, sensor_data_len);
sensor_event = (sensor_event_data_t *)bh_malloc(sizeof(*sensor_event));
if (sensor_event == NULL) {
bh_free(sensor_data_clone);
return;
}
memset(sensor_event, 0, sizeof(*sensor_event));
sensor_event->sensor_id = sensor_id;
sensor_event->data = sensor_data_clone;
sensor_event->data_fmt = FMT_ATTR_CONTAINER;
msg = bh_new_msg(SENSOR_EVENT_WASM,
sensor_event,
sizeof(*sensor_event),
sensor_event_cleaner);
if (!msg) {
sensor_event_cleaner(sensor_event);
return;
}
bh_post_msg2(module->queue, msg);
}
bool
wasm_sensor_config(wasm_exec_env_t exec_env,
uint32 sensor, int interval,
int bit_cfg, int delay)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
attr_container_t * attr_cont;
sensor_client_t * c;
sensor_obj_t s = find_sys_sensor_id(sensor);
if (s == NULL)
return false;
unsigned int mod_id = app_manager_get_module_id(Module_WASM_App,
module_inst);
bh_assert(mod_id != ID_NONE);
vm_mutex_lock(&s->lock);
c = find_sensor_client(s, mod_id, false);
if (c == NULL) {
vm_mutex_unlock(&s->lock);
return false;
}
c->interval = interval;
c->bit_cfg = bit_cfg;
c->delay = delay;
vm_mutex_unlock(&s->lock);
if (s->config != NULL) {
attr_cont = attr_container_create("config sensor");
attr_container_set_int(&attr_cont, "interval", interval);
attr_container_set_int(&attr_cont, "bit_cfg", bit_cfg);
attr_container_set_int(&attr_cont, "delay", delay);
s->config(s, attr_cont);
attr_container_destroy(attr_cont);
}
refresh_read_interval(s);
reschedule_sensor_read();
return true;
}
uint32
wasm_sensor_open(wasm_exec_env_t exec_env,
int32 name_offset, int instance)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
char *name = NULL;
if (!validate_app_str_addr(name_offset))
return -1;
name = addr_app_to_native(name_offset);
if (name != NULL) {
sensor_client_t *c;
sys_sensor_t *s = find_sys_sensor(name, instance);
if (s == NULL)
return -1;
unsigned int mod_id = app_manager_get_module_id(Module_WASM_App,
module_inst);
bh_assert(mod_id != ID_NONE);
vm_mutex_lock(&s->lock);
c = find_sensor_client(s, mod_id, false);
if (c) {
// the app already opened this sensor
vm_mutex_unlock(&s->lock);
return -1;
}
sensor_client_t * client = (sensor_client_t*) bh_malloc(
sizeof(sensor_client_t));
if (client == NULL) {
vm_mutex_unlock(&s->lock);
return -1;
}
memset(client, 0, sizeof(sensor_client_t));
client->client_id = mod_id;
client->client_callback = (void *)wasm_sensor_callback;
client->interval = s->default_interval;
client->next = s->clients;
s->clients = client;
vm_mutex_unlock(&s->lock);
refresh_read_interval(s);
reschedule_sensor_read();
return s->sensor_id;
}
return -1;
}
bool
wasm_sensor_config_with_attr_container(wasm_exec_env_t exec_env,
uint32 sensor, int32 buffer_offset,
int len)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
char *buffer = NULL;
if (!validate_app_addr(buffer_offset, len))
return false;
buffer = addr_app_to_native(buffer_offset);
if (buffer != NULL) {
attr_container_t *cfg = (attr_container_t *)buffer;
sensor_obj_t s = find_sys_sensor_id(sensor);
if (s == NULL)
return false;
if (s->config == NULL)
return false;
return s->config(s, cfg);
}
return false;
}
bool
wasm_sensor_close(wasm_exec_env_t exec_env, uint32 sensor)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
unsigned int mod_id = app_manager_get_module_id(Module_WASM_App,
module_inst);
unsigned int client_id = mod_id;
sensor_obj_t s = find_sys_sensor_id(sensor);
sensor_client_t *c;
bh_assert(mod_id != ID_NONE);
if (s == NULL)
return false;
vm_mutex_lock(&s->lock);
if ((c = find_sensor_client(s, client_id, true)) != NULL)
bh_free(c);
vm_mutex_unlock(&s->lock);
refresh_read_interval(s);
reschedule_sensor_read();
return true;
}
/*
*
* sensor framework API - don't expose to the applications
*
*/
void set_sensor_reshceduler(void (*callback)())
{
rechedule_sensor_callback = callback;
}
// used for other threads to wakeup the sensor read thread
void reschedule_sensor_read()
{
if (rechedule_sensor_callback)
rechedule_sensor_callback();
}
void refresh_read_interval(sensor_obj_t sensor)
{
sensor_client_t *c;
uint32 interval = sensor->default_interval;
vm_mutex_lock(&sensor->lock);
c = sensor->clients;
if (c)
interval = c->interval;
while (c) {
if (c->interval < interval)
interval = c->interval;
c = c->next;
}
vm_mutex_unlock(&sensor->lock);
sensor->read_interval = interval;
}
sensor_obj_t
add_sys_sensor(char * name, char * description, int instance,
uint32 default_interval, void * read_func, void * config_func)
{
sys_sensor_t * s = (sys_sensor_t *) bh_malloc(sizeof(sys_sensor_t));
if (s == NULL)
return NULL;
memset(s, 0, sizeof(*s));
s->name = bh_strdup(name);
s->sensor_instance = instance;
s->default_interval = default_interval;
if (!s->name) {
bh_free(s);
return NULL;
}
if (description) {
s->description = bh_strdup(description);
if (!s->description) {
bh_free(s->name);
bh_free(s);
return NULL;
}
}
g_sensor_id_max++;
if (g_sensor_id_max == -1)
g_sensor_id_max++;
s->sensor_id = g_sensor_id_max;
s->read = read_func;
s->config = config_func;
if (g_sys_sensors == NULL) {
g_sys_sensors = s;
} else {
s->next = g_sys_sensors;
g_sys_sensors = s;
}
vm_mutex_init(&s->lock);
return s;
}
sensor_obj_t find_sys_sensor(const char* name, int instance)
{
sys_sensor_t * s = g_sys_sensors;
while (s) {
if (strcmp(s->name, name) == 0 && s->sensor_instance == instance)
return s;
s = s->next;
}
return NULL;
}
sensor_obj_t find_sys_sensor_id(uint32 sensor_id)
{
sys_sensor_t * s = g_sys_sensors;
while (s) {
if (s->sensor_id == sensor_id)
return s;
s = s->next;
}
return NULL;
}
sensor_client_t *find_sensor_client(sys_sensor_t * sensor,
unsigned int client_id, bool remove_if_found)
{
sensor_client_t *prev = NULL, *c = sensor->clients;
while (c) {
sensor_client_t *next = c->next;
if (c->client_id == client_id) {
if (remove_if_found) {
if (prev)
prev->next = next;
else
sensor->clients = next;
}
return c;
} else {
c = c->next;
}
}
return NULL;
}
// return the milliseconds to next check
int check_sensor_timers()
{
int ms_to_next_check = -1;
uint32 now = (uint32) bh_get_tick_ms();
sys_sensor_t * s = g_sys_sensors;
while (s) {
uint32 last_read = s->last_read;
uint32 elpased_ms = bh_get_elpased_ms(&last_read);
if (s->read_interval <= 0 || s->clients == NULL) {
s = s->next;
continue;
}
if (elpased_ms >= s->read_interval) {
attr_container_t * data = s->read(s);
if (data) {
sensor_client_t * client = s->clients;
while (client) {
client->client_callback(client, s->sensor_id, data);
client = client->next;
}
attr_container_destroy(data);
}
s->last_read = now;
if (ms_to_next_check == -1 || (ms_to_next_check < s->read_interval))
ms_to_next_check = s->read_interval;
} else {
int remaining = s->read_interval - elpased_ms;
if (ms_to_next_check == -1 || (ms_to_next_check < remaining))
ms_to_next_check = remaining;
}
s = s->next;
}
return ms_to_next_check;
}
void sensor_cleanup_callback(uint32 module_id)
{
sys_sensor_t * s = g_sys_sensors;
while (s) {
sensor_client_t *c;
vm_mutex_lock(&s->lock);
if ((c = find_sensor_client(s, module_id, true)) != NULL) {
bh_free(c);
}
vm_mutex_unlock(&s->lock);
s = s->next;
}
}

View File

@ -0,0 +1,69 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#ifndef LIB_EXTENSION_RUNTIME_SENSOR_H_
#define LIB_EXTENSION_RUNTIME_SENSOR_H_
#include "bh_platform.h"
#include "bi-inc/attr_container.h"
#include "wasm_export.h"
struct _sys_sensor;
typedef struct _sys_sensor* sensor_obj_t;
typedef struct _sensor_client {
struct _sensor_client * next;
unsigned int client_id; // the app id
int interval;
int bit_cfg;
int delay;
void (*client_callback)(void * client, uint32, attr_container_t *);
} sensor_client_t;
typedef struct _sys_sensor {
struct _sys_sensor * next;
char * name;
int sensor_instance;
char * description;
uint32 sensor_id;
sensor_client_t * clients;
/* app, sensor mgr and app mgr may access the clients at the same time,
* so need a lock to protect the clients */
korp_mutex lock;
uint32 last_read;
uint32 read_interval;
uint32 default_interval;
attr_container_t * (*read)(void *); /* TODO: may support other type return value, such as 'cbor' */
bool (*config)(void *, void *);
} sys_sensor_t;
sensor_obj_t add_sys_sensor(char * name, char * description, int instance,
uint32 default_interval, void * read_func, void * config_func);
sensor_obj_t find_sys_sensor(const char* name, int instance);
sensor_obj_t find_sys_sensor_id(uint32 sensor_id);
void refresh_read_interval(sensor_obj_t sensor);
void sensor_cleanup_callback(uint32 module_id);
int check_sensor_timers();
void reschedule_sensor_read();
uint32
wasm_sensor_open(wasm_exec_env_t exec_env,
int32 name_offset, int instance);
bool
wasm_sensor_config(wasm_exec_env_t exec_env,
uint32 sensor, int interval, int bit_cfg, int delay);
bool
wasm_sensor_config_with_attr_container(wasm_exec_env_t exec_env,
uint32 sensor, int32 buffer_offset,
int len);
bool
wasm_sensor_close(wasm_exec_env_t exec_env, uint32 sensor);
#endif /* LIB_EXTENSION_RUNTIME_SENSOR_H_ */

View File

@ -0,0 +1,9 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
EXPORT_WASM_API(wasm_sensor_open),
EXPORT_WASM_API(wasm_sensor_config),
EXPORT_WASM_API(wasm_sensor_config_with_attr_container),
EXPORT_WASM_API(wasm_sensor_close),

View File

@ -0,0 +1,32 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#ifndef _SENSOR_API_H_
#define _SENSOR_API_H_
#include "bh_platform.h"
#ifdef __cplusplus
extern "C" {
#endif
uint32
wasm_sensor_open(int32 name_offset, int instance);
bool
wasm_sensor_config(uint32 sensor, int interval, int bit_cfg, int delay);
bool
wasm_sensor_config_with_attr_container(uint32 sensor, int32 buffer_offset, int len);
bool
wasm_sensor_close(uint32 sensor);
#ifdef __cplusplus
}
#endif
#endif /* end of _SENSOR_API_H_ */

View File

@ -0,0 +1,148 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "bh_common.h"
#include "bh_queue.h"
#include "bh_thread.h"
#include "runtime_sensor.h"
#include "bi-inc/attr_container.h"
#include "module_wasm_app.h"
#include "wasm_export.h"
/*
*
* One reference implementation for sensor manager
*
*
*/
static korp_cond cond;
static korp_mutex mutex;
static bool sensor_check_thread_run = true;
void app_mgr_sensor_event_callback(module_data *m_data, bh_message_t msg)
{
uint32 argv[3];
wasm_function_inst_t func_onSensorEvent;
bh_assert(SENSOR_EVENT_WASM == bh_message_type(msg));
wasm_data *wasm_app_data = (wasm_data*)m_data->internal_data;
wasm_module_inst_t inst = wasm_app_data->wasm_module_inst;
sensor_event_data_t *payload = (sensor_event_data_t*)
bh_message_payload(msg);
if (payload == NULL)
return;
func_onSensorEvent = wasm_runtime_lookup_function(inst, "_on_sensor_event",
"(i32i32i32)");
if (!func_onSensorEvent)
func_onSensorEvent = wasm_runtime_lookup_function(inst, "on_sensor_event",
"(i32i32i32)");
if (!func_onSensorEvent) {
printf("Cannot find function on_sensor_event\n");
} else {
int32 sensor_data_offset;
uint32 sensor_data_len;
if (payload->data_fmt == FMT_ATTR_CONTAINER) {
sensor_data_len = attr_container_get_serialize_length(payload->data);
} else {
printf("Unsupported sensor data format: %d\n", payload->data_fmt);
return;
}
sensor_data_offset = wasm_runtime_module_dup_data(inst, payload->data,
sensor_data_len);
if (sensor_data_offset == 0) {
const char *exception = wasm_runtime_get_exception(inst);
if (exception) {
printf("Got exception running wasm code: %s\n",
exception);
wasm_runtime_clear_exception(inst);
}
return;
}
argv[0] = payload->sensor_id;
argv[1] = (uint32) sensor_data_offset;
argv[2] = sensor_data_len;
if (!wasm_runtime_call_wasm(wasm_app_data->exec_env, func_onSensorEvent,
3, argv)) {
const char *exception = wasm_runtime_get_exception(inst);
bh_assert(exception);
printf(":Got exception running wasm code: %s\n",
exception);
wasm_runtime_clear_exception(inst);
wasm_runtime_module_free(inst, sensor_data_offset);
return;
}
wasm_runtime_module_free(inst, sensor_data_offset);
}
}
static attr_container_t * read_test_sensor(void * sensor)
{
//luc: for test
attr_container_t *attr_obj = attr_container_create("read test sensor data");
if (attr_obj) {
attr_container_set_string(&attr_obj, "name", "read test sensor");
return attr_obj;
}
return NULL;
}
static bool config_test_sensor(void * s, void * config)
{
return false;
}
static void thread_sensor_check(void * arg)
{
while (sensor_check_thread_run) {
int ms_to_expiry = check_sensor_timers();
if (ms_to_expiry == -1)
ms_to_expiry = 5000;
vm_mutex_lock(&mutex);
vm_cond_reltimedwait(&cond, &mutex, ms_to_expiry);
vm_mutex_unlock(&mutex);
}
}
static void cb_wakeup_thread()
{
vm_cond_signal(&cond);
}
void set_sensor_reshceduler(void (*callback)());
void init_sensor_framework()
{
// init the mutext and conditions
korp_thread tid;
vm_cond_init(&cond);
vm_mutex_init(&mutex);
// add the sys sensor objects
add_sys_sensor("sensor_test", "This is a sensor for test", 0, 1000,
read_test_sensor, config_test_sensor);
set_sensor_reshceduler(cb_wakeup_thread);
wasm_register_msg_callback(SENSOR_EVENT_WASM,
app_mgr_sensor_event_callback);
wasm_register_cleanup_callback(sensor_cleanup_callback);
vm_thread_create(&tid, (void *)thread_sensor_check, NULL,
BH_APPLET_PRESERVED_STACK_SIZE);
}
void exit_sensor_framework()
{
sensor_check_thread_run = false;
}

View File

@ -0,0 +1,12 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
set (WASM_LIB_SENSOR_DIR ${CMAKE_CURRENT_LIST_DIR})
include_directories(${WASM_LIB_SENSOR_DIR})
file (GLOB_RECURSE source_all ${WASM_LIB_SENSOR_DIR}/*.c)
set (WASM_APP_LIB_CURRENT_SOURCE ${source_all})

View File

@ -0,0 +1,8 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
/*
header file for wasm application
*/

View File

@ -0,0 +1,16 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
set (WASM_APP_CURRENT_DIR ${CMAKE_CURRENT_LIST_DIR})
include_directories(
${WASM_APP_CURRENT_DIR}
# Add your include dir here
)
file (GLOB_RECURSE source_all
${WASM_APP_CURRENT_DIR}/*.c
# Add your source file here
)
set (WASM_APP_CURRENT_SOURCE ${source_all})

View File

@ -0,0 +1,6 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
/* EXPORT_WASM_API(your_api_here), */

View File

@ -0,0 +1,17 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
set (WASM_LIB_CURRENT_DIR ${CMAKE_CURRENT_LIST_DIR})
include_directories(
${WASM_LIB_CURRENT_DIR}
# Add your include dir here
)
file (GLOB_RECURSE source_all
${WASM_LIB_CURRENT_DIR}/*.c
# Add your source file here
)
set (WASM_APP_LIB_CURRENT_SOURCE ${source_all})

View File

@ -0,0 +1,36 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#ifndef _GUI_API_H_
#define _GUI_API_H_
#include "bh_platform.h"
#ifdef __cplusplus
extern "C" {
#endif
void
wasm_obj_native_call(int32 func_id, uint32 *argv, uint32 argc);
void
wasm_btn_native_call(int32 func_id, uint32 *argv, uint32 argc);
void
wasm_label_native_call(int32 func_id, uint32 *argv, uint32 argc);
void
wasm_cb_native_call(int32 func_id, uint32 *argv, uint32 argc);
void
wasm_list_native_call(int32 func_id, uint32 *argv, uint32 argc);
#ifdef __cplusplus
}
#endif
#endif /* end of _GUI_API_H_ */

View File

@ -0,0 +1,121 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "wa-inc/wgl.h"
#include "bh_platform.h"
#include "gui_api.h"
#define ARGC sizeof(argv)/sizeof(uint32)
#define CALL_BTN_NATIVE_FUNC(id) wasm_btn_native_call(id, argv, ARGC)
wgl_obj_t wgl_btn_create(wgl_obj_t par, wgl_obj_t copy)
{
uint32 argv[2] = {0};
argv[0] = (uint32)par;
argv[1] = (uint32)copy;
CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_CREATE);
return (wgl_obj_t)argv[0];
}
void wgl_btn_set_toggle(wgl_obj_t btn, bool tgl)
{
uint32 argv[2] = {0};
argv[0] = (uint32)btn;
argv[1] = tgl;
CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_SET_TOGGLE);
}
void wgl_btn_set_state(wgl_obj_t btn, wgl_btn_state_t state)
{
uint32 argv[2] = {0};
argv[0] = (uint32)btn;
argv[1] = state;
CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_SET_STATE);
}
void wgl_btn_toggle(wgl_obj_t btn)
{
uint32 argv[1] = {0};
argv[0] = (uint32)btn;
CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_TOGGLE);
}
void wgl_btn_set_ink_in_time(wgl_obj_t btn, uint16_t time)
{
uint32 argv[2] = {0};
argv[0] = (uint32)btn;
argv[1] = time;
CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_SET_INK_IN_TIME);
}
void wgl_btn_set_ink_wait_time(wgl_obj_t btn, uint16_t time)
{
uint32 argv[2] = {0};
argv[0] = (uint32)btn;
argv[1] = time;
CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_SET_INK_WAIT_TIME);
}
void wgl_btn_set_ink_out_time(wgl_obj_t btn, uint16_t time)
{
uint32 argv[2] = {0};
argv[0] = (uint32)btn;
argv[1] = time;
CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_SET_INK_OUT_TIME);
}
//void wgl_btn_set_style(wgl_obj_t btn, wgl_btn_style_t type, const wgl_style_t * style)
//{
// //TODO: pack style
// //wasm_btn_set_style(btn, type, style);
//}
//
wgl_btn_state_t wgl_btn_get_state(const wgl_obj_t btn)
{
uint32 argv[1] = {0};
argv[0] = (uint32)btn;
CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_GET_STATE);
return (wgl_btn_state_t)argv[0];
}
bool wgl_btn_get_toggle(const wgl_obj_t btn)
{
uint32 argv[1] = {0};
argv[0] = (uint32)btn;
CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_GET_TOGGLE);
return (bool)argv[0];
}
uint16_t wgl_btn_get_ink_in_time(const wgl_obj_t btn)
{
uint32 argv[1] = {0};
argv[0] = (uint32)btn;
CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_GET_INK_IN_TIME);
return (uint16_t)argv[0];
}
uint16_t wgl_btn_get_ink_wait_time(const wgl_obj_t btn)
{
uint32 argv[1] = {0};
argv[0] = (uint32)btn;
CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_GET_INK_WAIT_TIME);
return (uint16_t)argv[0];
}
uint16_t wgl_btn_get_ink_out_time(const wgl_obj_t btn)
{
uint32 argv[1] = {0};
argv[0] = (uint32)btn;
CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_GET_INK_OUT_TIME);
return (uint16_t)argv[0];
}
//
//const wgl_style_t * wgl_btn_get_style(const wgl_obj_t btn, wgl_btn_style_t type)
//{
// //TODO: pack style
// //wasm_btn_get_style(btn, type);
// return NULL;
//}

View File

@ -0,0 +1,73 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "wa-inc/wgl.h"
#include "gui_api.h"
#include <string.h>
#define ARGC sizeof(argv)/sizeof(uint32)
#define CALL_CB_NATIVE_FUNC(id) wasm_cb_native_call(id, argv, ARGC)
wgl_obj_t wgl_cb_create(wgl_obj_t par, const wgl_obj_t copy)
{
uint32 argv[2] = {0};
argv[0] = (uint32)par;
argv[1] = (uint32)copy;
CALL_CB_NATIVE_FUNC(CB_FUNC_ID_CREATE);
return (wgl_obj_t)argv[0];
}
void wgl_cb_set_text(wgl_obj_t cb, const char * txt)
{
uint32 argv[3] = {0};
argv[0] = (uint32)cb;
argv[1] = (uint32)txt;
argv[2] = strlen(txt) + 1;
CALL_CB_NATIVE_FUNC(CB_FUNC_ID_SET_TEXT);
}
void wgl_cb_set_static_text(wgl_obj_t cb, const char * txt)
{
uint32 argv[3] = {0};
argv[0] = (uint32)cb;
argv[1] = (uint32)txt;
argv[2] = strlen(txt) + 1;
CALL_CB_NATIVE_FUNC(CB_FUNC_ID_SET_STATIC_TEXT);
}
//void wgl_cb_set_style(wgl_obj_t cb, wgl_cb_style_t type, const wgl_style_t * style)
//{
// //TODO:
//}
//
unsigned int wgl_cb_get_text_length(wgl_obj_t cb)
{
uint32 argv[1] = {0};
argv[0] = (uint32)cb;
CALL_CB_NATIVE_FUNC(CB_FUNC_ID_GET_TEXT_LENGTH);
return argv[0];
}
char *wgl_cb_get_text(wgl_obj_t cb, char *buffer, int buffer_len)
{
uint32 argv[3] = {0};
argv[0] = (uint32)cb;
argv[1] = (uint32)buffer;
argv[2] = buffer_len;
CALL_CB_NATIVE_FUNC(CB_FUNC_ID_GET_TEXT);
return (char *)argv[0];
}
//const wgl_style_t * wgl_cb_get_style(const wgl_obj_t cb, wgl_cb_style_t type)
//{
// //TODO
// return NULL;
//}
//

View File

@ -0,0 +1,252 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "wa-inc/wgl.h"
#include "gui_api.h"
#include <string.h>
#define ARGC sizeof(argv)/sizeof(uint32)
#define CALL_LABEL_NATIVE_FUNC(id) wasm_label_native_call(id, argv, ARGC)
wgl_obj_t wgl_label_create(wgl_obj_t par, wgl_obj_t copy)
{
uint32 argv[2] = {0};
argv[0] = (uint32)par;
argv[1] = (uint32)copy;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_CREATE);
return (wgl_obj_t)argv[0];
}
void wgl_label_set_text(wgl_obj_t label, const char * text)
{
uint32 argv[3] = {0};
argv[0] = (uint32)label;
argv[1] = (uint32)text;
argv[2] = strlen(text) + 1;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_TEXT);
}
void wgl_label_set_array_text(wgl_obj_t label, const char * array, uint16_t size)
{
uint32 argv[3] = {0};
argv[0] = (uint32)label;
argv[1] = (uint32)array;
argv[2] = size;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_ARRAY_TEXT);
}
void wgl_label_set_static_text(wgl_obj_t label, const char * text)
{
uint32 argv[3] = {0};
argv[0] = (uint32)label;
argv[1] = (uint32)text;
argv[2] = strlen(text) + 1;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_STATIC_TEXT);
}
void wgl_label_set_long_mode(wgl_obj_t label, wgl_label_long_mode_t long_mode)
{
uint32 argv[2] = {0};
argv[0] = (uint32)label;
argv[1] = long_mode;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_LONG_MODE);
}
void wgl_label_set_align(wgl_obj_t label, wgl_label_align_t align)
{
uint32 argv[2] = {0};
argv[0] = (uint32)label;
argv[1] = align;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_ALIGN);
}
void wgl_label_set_recolor(wgl_obj_t label, bool en)
{
uint32 argv[2] = {0};
argv[0] = (uint32)label;
argv[1] = en;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_RECOLOR);
}
void wgl_label_set_body_draw(wgl_obj_t label, bool en)
{
uint32 argv[2] = {0};
argv[0] = (uint32)label;
argv[1] = en;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_BODY_DRAW);
}
void wgl_label_set_anim_speed(wgl_obj_t label, uint16_t anim_speed)
{
uint32 argv[2] = {0};
argv[0] = (uint32)label;
argv[1] = anim_speed;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_ANIM_SPEED);
}
void wgl_label_set_text_sel_start(wgl_obj_t label, uint16_t index)
{
uint32 argv[2] = {0};
argv[0] = (uint32)label;
argv[1] = index;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_TEXT_SEL_START);
}
void wgl_label_set_text_sel_end(wgl_obj_t label, uint16_t index)
{
uint32 argv[2] = {0};
argv[0] = (uint32)label;
argv[1] = index;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_TEXT_SEL_END);
}
unsigned int wgl_label_get_text_length(wgl_obj_t label)
{
uint32 argv[1] = {0};
argv[0] = (uint32)label;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_TEXT_LENGTH);
return argv[0];
}
char * wgl_label_get_text(wgl_obj_t label, char *buffer, int buffer_len)
{
uint32 argv[3] = {0};
argv[0] = (uint32)label;
argv[1] = (uint32)buffer;
argv[2] = buffer_len;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_TEXT);
return (char *)argv[0];
}
wgl_label_long_mode_t wgl_label_get_long_mode(const wgl_obj_t label)
{
uint32 argv[1] = {0};
argv[0] = (uint32)label;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_LONG_MODE);
return (wgl_label_long_mode_t)argv[0];
}
wgl_label_align_t wgl_label_get_align(const wgl_obj_t label)
{
uint32 argv[1] = {0};
argv[0] = (uint32)label;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_ALIGN);
return (wgl_label_align_t)argv[0];
}
bool wgl_label_get_recolor(const wgl_obj_t label)
{
uint32 argv[1] = {0};
argv[0] = (uint32)label;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_RECOLOR);
return (bool)argv[0];
}
bool wgl_label_get_body_draw(const wgl_obj_t label)
{
uint32 argv[1] = {0};
argv[0] = (uint32)label;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_BODY_DRAW);
return (bool)argv[0];
}
uint16_t wgl_label_get_anim_speed(const wgl_obj_t label)
{
uint32 argv[1] = {0};
argv[0] = (uint32)label;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_ANIM_SPEED);
return (uint16_t)argv[0];
}
void wgl_label_get_letter_pos(const wgl_obj_t label, uint16_t index, wgl_point_t * pos)
{
uint32 argv[4] = {0};
argv[0] = (uint32)label;
argv[1] = index;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_LETTER_POS);
pos->x = argv[2];
pos->y = argv[3];
}
uint16_t wgl_label_get_letter_on(const wgl_obj_t label, wgl_point_t * pos)
{
uint32 argv[3] = {0};
argv[0] = (uint32)label;
argv[1] = pos->x;
argv[2] = pos->y;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_LETTER_POS);
return (uint16_t)argv[0];
}
bool wgl_label_is_char_under_pos(const wgl_obj_t label, wgl_point_t * pos)
{
uint32 argv[3] = {0};
argv[0] = (uint32)label;
argv[1] = pos->x;
argv[2] = pos->y;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_LETTER_POS);
return (bool)argv[0];
}
uint16_t wgl_label_get_text_sel_start(const wgl_obj_t label)
{
uint32 argv[1] = {0};
argv[0] = (uint32)label;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_TEXT_SEL_START);
return (uint16_t)argv[0];
}
uint16_t wgl_label_get_text_sel_end(const wgl_obj_t label)
{
uint32 argv[1] = {0};
argv[0] = (uint32)label;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_TEXT_SEL_END);
return (uint16_t)argv[0];
}
void wgl_label_ins_text(wgl_obj_t label, uint32_t pos, const char * txt)
{
uint32 argv[4] = {0};
argv[0] = (uint32)label;
argv[1] = pos;
argv[2] = (uint32)txt;
argv[3] = strlen(txt) + 1;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_INS_TEXT);
}
void wgl_label_cut_text(wgl_obj_t label, uint32_t pos, uint32_t cnt)
{
uint32 argv[3] = {0};
argv[0] = (uint32)label;
argv[1] = pos;
argv[2] = cnt;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_CUT_TEXT);
}

View File

@ -0,0 +1,154 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "wa-inc/wgl.h"
#include "gui_api.h"
#include <string.h>
#define ARGC sizeof(argv)/sizeof(uint32)
#define CALL_LIST_NATIVE_FUNC(id) wasm_list_native_call(id, argv, ARGC)
wgl_obj_t wgl_list_create(wgl_obj_t par, const wgl_obj_t copy)
{
uint32 argv[2] = {0};
argv[0] = (uint32)par;
argv[1] = (uint32)copy;
CALL_LIST_NATIVE_FUNC(LIST_FUNC_ID_CREATE);
return (wgl_obj_t)argv[0];
}
//
//
//void wgl_list_clean(wgl_obj_t obj)
//{
// wasm_list_clean(obj);
//}
//
wgl_obj_t wgl_list_add_btn(wgl_obj_t list, const void * img_src, const char * txt)
{
uint32 argv[3] = {0};
(void)img_src; /* doesn't support img src currently */
argv[0] = (uint32)list;
argv[1] = (uint32)txt;
argv[2] = strlen(txt) + 1;
CALL_LIST_NATIVE_FUNC(LIST_FUNC_ID_ADD_BTN);
return (wgl_obj_t)argv[0];
}
//
//
//bool wgl_list_remove(const wgl_obj_t list, uint16_t index)
//{
// return wasm_list_remove(list, index);
//}
//
//
//void wgl_list_set_single_mode(wgl_obj_t list, bool mode)
//{
// wasm_list_set_single_mode(list, mode);
//}
//
//#if LV_USE_GROUP
//
//
//void wgl_list_set_btn_selected(wgl_obj_t list, wgl_obj_t btn)
//{
// wasm_list_set_btn_selected(list, btn);
//}
//#endif
//
//
//void wgl_list_set_style(wgl_obj_t list, wgl_list_style_t type, const wgl_style_t * style)
//{
// //TODO
//}
//
//
//bool wgl_list_get_single_mode(wgl_obj_t list)
//{
// return wasm_list_get_single_mode(list);
//}
//
//
//const char * wgl_list_get_btn_text(const wgl_obj_t btn)
//{
// return wasm_list_get_btn_text(btn);
//}
//
//wgl_obj_t wgl_list_get_btn_label(const wgl_obj_t btn)
//{
// return wasm_list_get_btn_label(btn);
//}
//
//
//wgl_obj_t wgl_list_get_btn_img(const wgl_obj_t btn)
//{
// return wasm_list_get_btn_img(btn);
//}
//
//
//wgl_obj_t wgl_list_get_prev_btn(const wgl_obj_t list, wgl_obj_t prev_btn)
//{
// return wasm_list_get_prev_btn(list, prev_btn);
//}
//
//
//wgl_obj_t wgl_list_get_next_btn(const wgl_obj_t list, wgl_obj_t prev_btn)
//{
// return wasm_list_get_next_btn(list, prev_btn);
//}
//
//
//int32_t wgl_list_get_btn_index(const wgl_obj_t list, const wgl_obj_t btn)
//{
// return wasm_list_get_btn_index(list, btn);
//}
//
//
//uint16_t wgl_list_get_size(const wgl_obj_t list)
//{
// return wasm_list_get_size(list);
//}
//
//#if LV_USE_GROUP
//
//wgl_obj_t wgl_list_get_btn_selected(const wgl_obj_t list)
//{
// return wasm_list_get_btn_selected(list);
//}
//#endif
//
//
//
//const wgl_style_t * wgl_list_get_style(const wgl_obj_t list, wgl_list_style_t type)
//{
// //TODO
// return NULL;
//}
//
//
//void wgl_list_up(const wgl_obj_t list)
//{
// wasm_list_up(list);
//}
//
//void wgl_list_down(const wgl_obj_t list)
//{
// wasm_list_down(list);
//}
//
//
//void wgl_list_focus(const wgl_obj_t btn, wgl_anim_enable_t anim)
//{
// wasm_list_focus(btn, anim);
//}
//

View File

@ -0,0 +1,116 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "wa-inc/wgl.h"
#include "gui_api.h"
#include <stdlib.h>
#include <string.h>
#define ARGC sizeof(argv)/sizeof(uint32)
#define CALL_OBJ_NATIVE_FUNC(id) wasm_obj_native_call(id, argv, ARGC)
typedef struct _obj_evt_cb {
struct _obj_evt_cb *next;
wgl_obj_t obj;
wgl_event_cb_t event_cb;
} obj_evt_cb_t;
static obj_evt_cb_t *g_obj_evt_cb_list = NULL;
/* For lvgl compatible */
char g_widget_text[100];
wgl_res_t wgl_obj_del(wgl_obj_t obj)
{
uint32 argv[1] = {0};
argv[0] = (uint32)obj;
CALL_OBJ_NATIVE_FUNC(OBJ_FUNC_ID_DEL);
return (wgl_res_t)argv[0];
}
void wgl_obj_del_async(wgl_obj_t obj)
{
uint32 argv[1] = {0};
argv[0] = (uint32)obj;
CALL_OBJ_NATIVE_FUNC(OBJ_FUNC_ID_DEL_ASYNC);
}
void wgl_obj_clean(wgl_obj_t obj)
{
uint32 argv[1] = {0};
argv[0] = (uint32)obj;
CALL_OBJ_NATIVE_FUNC(OBJ_FUNC_ID_CLEAN);
}
void wgl_obj_align(wgl_obj_t obj, const wgl_obj_t base, wgl_align_t align, wgl_coord_t x_mod, wgl_coord_t y_mod)
{
uint32 argv[5] = {0};
argv[0] = (uint32)obj;
argv[1] = (uint32)base;
argv[2] = align;
argv[3] = x_mod;
argv[4] = y_mod;
CALL_OBJ_NATIVE_FUNC(OBJ_FUNC_ID_ALIGN);
}
wgl_event_cb_t wgl_obj_get_event_cb(const wgl_obj_t obj)
{
obj_evt_cb_t *obj_evt_cb = g_obj_evt_cb_list;
while (obj_evt_cb != NULL) {
if (obj_evt_cb->obj == obj) {
return obj_evt_cb->event_cb;
}
obj_evt_cb = obj_evt_cb->next;
}
return NULL;
}
void wgl_obj_set_event_cb(wgl_obj_t obj, wgl_event_cb_t event_cb)
{
obj_evt_cb_t *obj_evt_cb;
uint32 argv[1] = {0};
obj_evt_cb = g_obj_evt_cb_list;
while (obj_evt_cb) {
if (obj_evt_cb->obj == obj) {
obj_evt_cb->event_cb = event_cb;
return;
}
}
obj_evt_cb = (obj_evt_cb_t *)malloc(sizeof(*obj_evt_cb));
if (obj_evt_cb == NULL)
return;
memset(obj_evt_cb, 0, sizeof(*obj_evt_cb));
obj_evt_cb->obj = obj;
obj_evt_cb->event_cb = event_cb;
if (g_obj_evt_cb_list != NULL) {
obj_evt_cb->next = g_obj_evt_cb_list;
g_obj_evt_cb_list = obj_evt_cb;
} else {
g_obj_evt_cb_list = obj_evt_cb;
}
argv[0] = (uint32)obj;
CALL_OBJ_NATIVE_FUNC(OBJ_FUNC_ID_SET_EVT_CB);
}
void on_widget_event(wgl_obj_t obj, wgl_event_t event)
{
obj_evt_cb_t *obj_evt_cb = g_obj_evt_cb_list;
while (obj_evt_cb != NULL) {
if (obj_evt_cb->obj == obj) {
obj_evt_cb->event_cb(obj, event);
return;
}
obj_evt_cb = obj_evt_cb->next;
}
}

View File

@ -0,0 +1,8 @@
MIT licence
Copyright (c) 2016 Gábor Kiss-Vámosi
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@ -0,0 +1,162 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#ifndef WAMR_GRAPHIC_LIBRARY_BTN_H
#define WAMR_GRAPHIC_LIBRARY_BTN_H
#ifdef __cplusplus
extern "C" {
#endif
/** Possible states of a button.
* It can be used not only by buttons but other button-like objects too*/
enum {
/**Released*/
WGL_BTN_STATE_REL,
/**Pressed*/
WGL_BTN_STATE_PR,
/**Toggled released*/
WGL_BTN_STATE_TGL_REL,
/**Toggled pressed*/
WGL_BTN_STATE_TGL_PR,
/**Inactive*/
WGL_BTN_STATE_INA,
/**Number of states*/
_WGL_BTN_STATE_NUM,
};
typedef uint8_t wgl_btn_state_t;
/**Styles*/
enum {
/** Release style */
WGL_BTN_STYLE_REL,
/**Pressed style*/
WGL_BTN_STYLE_PR,
/** Toggle released style*/
WGL_BTN_STYLE_TGL_REL,
/** Toggle pressed style */
WGL_BTN_STYLE_TGL_PR,
/** Inactive style*/
WGL_BTN_STYLE_INA,
};
typedef uint8_t wgl_btn_style_t;
/* Create a button */
wgl_obj_t wgl_btn_create(wgl_obj_t par, wgl_obj_t copy);
/*=====================
* Setter functions
*====================*/
/**
* Enable the toggled states. On release the button will change from/to toggled state.
* @param btn pointer to a button object
* @param tgl true: enable toggled states, false: disable
*/
void wgl_btn_set_toggle(wgl_obj_t btn, bool tgl);
/**
* Set the state of the button
* @param btn pointer to a button object
* @param state the new state of the button (from wgl_btn_state_t enum)
*/
void wgl_btn_set_state(wgl_obj_t btn, wgl_btn_state_t state);
/**
* Toggle the state of the button (ON->OFF, OFF->ON)
* @param btn pointer to a button object
*/
void wgl_btn_toggle(wgl_obj_t btn);
/**
* Set time of the ink effect (draw a circle on click to animate in the new state)
* @param btn pointer to a button object
* @param time the time of the ink animation
*/
void wgl_btn_set_ink_in_time(wgl_obj_t btn, uint16_t time);
/**
* Set the wait time before the ink disappears
* @param btn pointer to a button object
* @param time the time of the ink animation
*/
void wgl_btn_set_ink_wait_time(wgl_obj_t btn, uint16_t time);
/**
* Set time of the ink out effect (animate to the released state)
* @param btn pointer to a button object
* @param time the time of the ink animation
*/
void wgl_btn_set_ink_out_time(wgl_obj_t btn, uint16_t time);
/**
* Set a style of a button.
* @param btn pointer to button object
* @param type which style should be set
* @param style pointer to a style
* */
//void wgl_btn_set_style(wgl_obj_t btn, wgl_btn_style_t type, const wgl_style_t * style);
/*=====================
* Getter functions
*====================*/
/**
* Get the current state of the button
* @param btn pointer to a button object
* @return the state of the button (from wgl_btn_state_t enum)
*/
wgl_btn_state_t wgl_btn_get_state(wgl_obj_t btn);
/**
* Get the toggle enable attribute of the button
* @param btn pointer to a button object
* @return true: toggle enabled, false: disabled
*/
bool wgl_btn_get_toggle(wgl_obj_t btn);
/**
* Get time of the ink in effect (draw a circle on click to animate in the new state)
* @param btn pointer to a button object
* @return the time of the ink animation
*/
uint16_t wgl_btn_get_ink_in_time(wgl_obj_t btn);
/**
* Get the wait time before the ink disappears
* @param btn pointer to a button object
* @return the time of the ink animation
*/
uint16_t wgl_btn_get_ink_wait_time(wgl_obj_t btn);
/**
* Get time of the ink out effect (animate to the releases state)
* @param btn pointer to a button object
* @return the time of the ink animation
*/
uint16_t wgl_btn_get_ink_out_time(wgl_obj_t btn);
/**
* Get style of a button.
* @param btn pointer to button object
* @param type which style should be get
* @return style pointer to the style
* */
//const wgl_style_t * wgl_btn_get_style(const wgl_obj_t btn, wgl_btn_style_t type);
#ifdef __cplusplus
}
#endif
#endif /* WAMR_GRAPHIC_LIBRARY_BTN_H */

View File

@ -0,0 +1,79 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#ifndef WAMR_GRAPHIC_LIBRARY_CB_H
#define WAMR_GRAPHIC_LIBRARY_CB_H
#ifdef __cplusplus
extern "C" {
#endif
/** Checkbox styles. */
enum {
WGL_CB_STYLE_BG, /**< Style of object background. */
WGL_CB_STYLE_BOX_REL, /**< Style of box (released). */
WGL_CB_STYLE_BOX_PR, /**< Style of box (pressed). */
WGL_CB_STYLE_BOX_TGL_REL, /**< Style of box (released but checked). */
WGL_CB_STYLE_BOX_TGL_PR, /**< Style of box (pressed and checked). */
WGL_CB_STYLE_BOX_INA, /**< Style of disabled box */
};
typedef uint8_t wgl_cb_style_t;
/**
* Create a check box objects
* @param par pointer to an object, it will be the parent of the new check box
* @param copy pointer to a check box object, if not NULL then the new object will be copied from it
* @return pointer to the created check box
*/
wgl_obj_t wgl_cb_create(wgl_obj_t par, const wgl_obj_t copy);
/*=====================
* Setter functions
*====================*/
/**
* Set the text of a check box. `txt` will be copied and may be deallocated
* after this function returns.
* @param cb pointer to a check box
* @param txt the text of the check box. NULL to refresh with the current text.
*/
void wgl_cb_set_text(wgl_obj_t cb, const char * txt);
/**
* Set the text of a check box. `txt` must not be deallocated during the life
* of this checkbox.
* @param cb pointer to a check box
* @param txt the text of the check box. NULL to refresh with the current text.
*/
void wgl_cb_set_static_text(wgl_obj_t cb, const char * txt);
/*=====================
* Getter functions
*====================*/
/**
* Get the length of the text of a check box
* @param label the check box object
* @return the length of the text of the check box
*/
unsigned int wgl_cb_get_text_length(wgl_obj_t cb);
/**
* Get the text of a check box
* @param label the check box object
* @param buffer buffer to save the text
* @param buffer_len length of the buffer
* @return the text of the check box, note that the text will be truncated if buffer is not long enough
*/
char *wgl_cb_get_text(wgl_obj_t cb, char *buffer, int buffer_len);
#ifdef __cplusplus
}
#endif
#endif /* WAMR_GRAPHIC_LIBRARY_CB_H */

View File

@ -0,0 +1,66 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#ifndef WAMR_GRAPHIC_LIBRARY_LABEL_H
#define WAMR_GRAPHIC_LIBRARY_LABEL_H
#ifdef __cplusplus
extern "C" {
#endif
/** Long mode behaviors. Used in 'wgl_label_ext_t' */
enum {
WGL_LABEL_LONG_EXPAND, /**< Expand the object size to the text size*/
WGL_LABEL_LONG_BREAK, /**< Keep the object width, break the too long lines and expand the object
height*/
WGL_LABEL_LONG_DOT, /**< Keep the size and write dots at the end if the text is too long*/
WGL_LABEL_LONG_SROLL, /**< Keep the size and roll the text back and forth*/
WGL_LABEL_LONG_SROLL_CIRC, /**< Keep the size and roll the text circularly*/
WGL_LABEL_LONG_CROP, /**< Keep the size and crop the text out of it*/
};
typedef uint8_t wgl_label_long_mode_t;
/** Label align policy*/
enum {
WGL_LABEL_ALIGN_LEFT, /**< Align text to left */
WGL_LABEL_ALIGN_CENTER, /**< Align text to center */
WGL_LABEL_ALIGN_RIGHT, /**< Align text to right */
};
typedef uint8_t wgl_label_align_t;
/** Label styles*/
enum {
WGL_LABEL_STYLE_MAIN,
};
typedef uint8_t wgl_label_style_t;
/* Create a label */
wgl_obj_t wgl_label_create(wgl_obj_t par, wgl_obj_t copy);
/* Set text for the label */
void wgl_label_set_text(wgl_obj_t label, const char * text);
/**
* Get the length of the text of a label
* @param label the label object
* @return the length of the text of the label
*/
unsigned int wgl_label_get_text_length(wgl_obj_t label);
/**
* Get the text of a label
* @param label the label object
* @param buffer buffer to save the text
* @param buffer_len length of the buffer
* @return the text of the label, note that the text will be truncated if buffer is not long enough
*/
char *wgl_label_get_text(wgl_obj_t label, char *buffer, int buffer_len);
#ifdef __cplusplus
}
#endif
#endif /* WAMR_GRAPHIC_LIBRARY_LABEL_H */

View File

@ -0,0 +1,54 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#ifndef WAMR_GRAPHIC_LIBRARY_LIST_H
#define WAMR_GRAPHIC_LIBRARY_LIST_H
#ifdef __cplusplus
extern "C" {
#endif
/** List styles. */
enum {
WGL_LIST_STYLE_BG, /**< List background style */
WGL_LIST_STYLE_SCRL, /**< List scrollable area style. */
WGL_LIST_STYLE_SB, /**< List scrollbar style. */
WGL_LIST_STYLE_EDGE_FLASH, /**< List edge flash style. */
WGL_LIST_STYLE_BTN_REL, /**< Same meaning as the ordinary button styles. */
WGL_LIST_STYLE_BTN_PR,
WGL_LIST_STYLE_BTN_TGL_REL,
WGL_LIST_STYLE_BTN_TGL_PR,
WGL_LIST_STYLE_BTN_INA,
};
typedef uint8_t wgl_list_style_t;
/**
* Create a list objects
* @param par pointer to an object, it will be the parent of the new list
* @param copy pointer to a list object, if not NULL then the new object will be copied from it
* @return pointer to the created list
*/
wgl_obj_t wgl_list_create(wgl_obj_t par, wgl_obj_t copy);
/*======================
* Add/remove functions
*=====================*/
/**
* Add a list element to the list
* @param list pointer to list object
* @param img_fn file name of an image before the text (NULL if unused)
* @param txt text of the list element (NULL if unused)
* @return pointer to the new list element which can be customized (a button)
*/
wgl_obj_t wgl_list_add_btn(wgl_obj_t list, const void * img_src, const char * txt);
#ifdef __cplusplus
}
#endif
#endif /* WAMR_GRAPHIC_LIBRARY_LIST_H */

View File

@ -0,0 +1,90 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#ifndef WAMR_GRAPHIC_LIBRARY_OBJ_H
#define WAMR_GRAPHIC_LIBRARY_OBJ_H
#ifdef __cplusplus
extern "C" {
#endif
typedef void * wgl_obj_t;
enum {
WGL_EVENT_PRESSED, /**< The object has been pressed*/
WGL_EVENT_PRESSING, /**< The object is being pressed (called continuously while pressing)*/
WGL_EVENT_PRESS_LOST, /**< User is still pressing but slid cursor/finger off of the object */
WGL_EVENT_SHORT_CLICKED, /**< User pressed object for a short period of time, then released it. Not called if dragged. */
WGL_EVENT_LONG_PRESSED, /**< Object has been pressed for at least `WGL_INDEV_LONG_PRESS_TIME`. Not called if dragged.*/
WGL_EVENT_LONG_PRESSED_REPEAT, /**< Called after `WGL_INDEV_LONG_PRESS_TIME` in every
`WGL_INDEV_LONG_PRESS_REP_TIME` ms. Not called if dragged.*/
WGL_EVENT_CLICKED, /**< Called on release if not dragged (regardless to long press)*/
WGL_EVENT_RELEASED, /**< Called in every cases when the object has been released*/
WGL_EVENT_DRAG_BEGIN,
WGL_EVENT_DRAG_END,
WGL_EVENT_DRAG_THROW_BEGIN,
WGL_EVENT_KEY,
WGL_EVENT_FOCUSED,
WGL_EVENT_DEFOCUSED,
WGL_EVENT_VALUE_CHANGED, /**< The object's value has changed (i.e. slider moved) */
WGL_EVENT_INSERT,
WGL_EVENT_REFRESH,
WGL_EVENT_APPLY, /**< "Ok", "Apply" or similar specific button has clicked*/
WGL_EVENT_CANCEL, /**< "Close", "Cancel" or similar specific button has clicked*/
WGL_EVENT_DELETE, /**< Object is being deleted */
};
typedef uint8_t wgl_event_t; /**< Type of event being sent to the object. */
/** Object alignment. */
enum {
WGL_ALIGN_CENTER = 0,
WGL_ALIGN_IN_TOP_LEFT,
WGL_ALIGN_IN_TOP_MID,
WGL_ALIGN_IN_TOP_RIGHT,
WGL_ALIGN_IN_BOTTOM_LEFT,
WGL_ALIGN_IN_BOTTOM_MID,
WGL_ALIGN_IN_BOTTOM_RIGHT,
WGL_ALIGN_IN_LEFT_MID,
WGL_ALIGN_IN_RIGHT_MID,
WGL_ALIGN_OUT_TOP_LEFT,
WGL_ALIGN_OUT_TOP_MID,
WGL_ALIGN_OUT_TOP_RIGHT,
WGL_ALIGN_OUT_BOTTOM_LEFT,
WGL_ALIGN_OUT_BOTTOM_MID,
WGL_ALIGN_OUT_BOTTOM_RIGHT,
WGL_ALIGN_OUT_LEFT_TOP,
WGL_ALIGN_OUT_LEFT_MID,
WGL_ALIGN_OUT_LEFT_BOTTOM,
WGL_ALIGN_OUT_RIGHT_TOP,
WGL_ALIGN_OUT_RIGHT_MID,
WGL_ALIGN_OUT_RIGHT_BOTTOM,
};
typedef uint8_t wgl_align_t;
enum {
WGL_DRAG_DIR_HOR = 0x1, /**< Object can be dragged horizontally. */
WGL_DRAG_DIR_VER = 0x2, /**< Object can be dragged vertically. */
WGL_DRAG_DIR_ALL = 0x3, /**< Object can be dragged in all directions. */
};
typedef uint8_t wgl_drag_dir_t;
typedef void (*wgl_event_cb_t)(wgl_obj_t obj, wgl_event_t event);
void wgl_obj_align(wgl_obj_t obj, wgl_obj_t base, wgl_align_t align, wgl_coord_t x_mod, wgl_coord_t y_mod);
void wgl_obj_set_event_cb(wgl_obj_t obj, wgl_event_cb_t event_cb);
wgl_res_t wgl_obj_del(wgl_obj_t obj);
void wgl_obj_del_async(wgl_obj_t obj);
void wgl_obj_clean(wgl_obj_t obj);
#ifdef __cplusplus
}
#endif
#endif /* WAMR_GRAPHIC_LIBRARY_OBJ_H */

View File

@ -0,0 +1,29 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#ifndef WAMR_GRAPHIC_LIBRARY_TYPES_H
#define WAMR_GRAPHIC_LIBRARY_TYPES_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
/**
* WGL error codes.
*/
enum {
WGL_RES_INV = 0, /*Typically indicates that the object is deleted (become invalid) in the action
function or an operation was failed*/
WGL_RES_OK, /*The object is valid (no deleted) after the action*/
};
typedef uint8_t wgl_res_t;
#ifdef __cplusplus
}
#endif
#endif /* WAMR_GRAPHIC_LIBRARY_TYPES_H */

View File

@ -0,0 +1,8 @@
MIT licence
Copyright (c) 2016 Gábor Kiss-Vámosi
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@ -0,0 +1,75 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#ifndef WAMR_GRAPHIC_LIBRARY_BTN_LVGL_COMPATIBLE_H
#define WAMR_GRAPHIC_LIBRARY_BTN_LVGL_COMPATIBLE_H
#ifdef __cplusplus
extern "C" {
#endif
#include "../inc/wgl_btn.h"
/** Possible states of a button.
* It can be used not only by buttons but other button-like objects too*/
enum {
/**Released*/
LV_BTN_STATE_REL,
/**Pressed*/
LV_BTN_STATE_PR,
/**Toggled released*/
LV_BTN_STATE_TGL_REL,
/**Toggled pressed*/
LV_BTN_STATE_TGL_PR,
/**Inactive*/
LV_BTN_STATE_INA,
/**Number of states*/
_LV_BTN_STATE_NUM,
};
typedef wgl_btn_state_t lv_btn_state_t;
/**Styles*/
enum {
/** Release style */
LV_BTN_STYLE_REL,
/**Pressed style*/
LV_BTN_STYLE_PR,
/** Toggle released style*/
LV_BTN_STYLE_TGL_REL,
/** Toggle pressed style */
LV_BTN_STYLE_TGL_PR,
/** Inactive style*/
LV_BTN_STYLE_INA,
};
typedef wgl_btn_style_t lv_btn_style_t;
#define lv_btn_create wgl_btn_create
#define lv_btn_set_toggle wgl_btn_set_toggle
#define lv_btn_set_state wgl_btn_set_state
#define lv_btn_toggle wgl_btn_toggle
#define lv_btn_set_ink_in_time wgl_btn_set_ink_in_time
#define lv_btn_set_ink_wait_time wgl_btn_set_ink_wait_time
#define lv_btn_set_ink_out_time wgl_btn_set_ink_out_time
#define lv_btn_get_state wgl_btn_get_state
#define lv_btn_get_toggle wgl_btn_get_toggle
#define lv_btn_get_ink_in_time wgl_btn_get_ink_in_time
#define lv_btn_get_ink_wait_time wgl_btn_get_ink_wait_time
#define lv_btn_get_ink_out_time wgl_btn_get_ink_out_time
#ifdef __cplusplus
}
#endif
#endif /* WAMR_GRAPHIC_LIBRARY_BTN_LVGL_COMPATIBLE_H */

View File

@ -0,0 +1,36 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#ifndef WAMR_GRAPHIC_LIBRARY_CB_LVGL_COMPATIBLE_H
#define WAMR_GRAPHIC_LIBRARY_CB_LVGL_COMPATIBLE_H
#ifdef __cplusplus
extern "C" {
#endif
#include "../inc/wgl_cb.h"
/** Checkbox styles. */
enum {
LV_CB_STYLE_BG, /**< Style of object background. */
LV_CB_STYLE_BOX_REL, /**< Style of box (released). */
LV_CB_STYLE_BOX_PR, /**< Style of box (pressed). */
LV_CB_STYLE_BOX_TGL_REL, /**< Style of box (released but checked). */
LV_CB_STYLE_BOX_TGL_PR, /**< Style of box (pressed and checked). */
LV_CB_STYLE_BOX_INA, /**< Style of disabled box */
};
typedef wgl_cb_style_t lv_cb_style_t;
#define lv_cb_create wgl_cb_create
#define lv_cb_set_text wgl_cb_set_text
#define lv_cb_set_static_text wgl_cb_set_static_text
#define lv_cb_get_text(cb) wgl_cb_get_text(cb, g_widget_text, 100)
#ifdef __cplusplus
}
#endif
#endif /* WAMR_GRAPHIC_LIBRARY_CB_LVGL_COMPATIBLE_H */

View File

@ -0,0 +1,50 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#ifndef WAMR_GRAPHIC_LIBRARY_LABEL_LVGL_COMPATIBLE_H
#define WAMR_GRAPHIC_LIBRARY_LABEL_LVGL_COMPATIBLE_H
#ifdef __cplusplus
extern "C" {
#endif
#include "../inc/wgl_label.h"
/** Long mode behaviors. Used in 'lv_label_ext_t' */
enum {
LV_LABEL_LONG_EXPAND, /**< Expand the object size to the text size*/
LV_LABEL_LONG_BREAK, /**< Keep the object width, break the too long lines and expand the object
height*/
LV_LABEL_LONG_DOT, /**< Keep the size and write dots at the end if the text is too long*/
LV_LABEL_LONG_SROLL, /**< Keep the size and roll the text back and forth*/
LV_LABEL_LONG_SROLL_CIRC, /**< Keep the size and roll the text circularly*/
LV_LABEL_LONG_CROP, /**< Keep the size and crop the text out of it*/
};
typedef wgl_label_long_mode_t lv_label_long_mode_t;
/** Label align policy*/
enum {
LV_LABEL_ALIGN_LEFT, /**< Align text to left */
LV_LABEL_ALIGN_CENTER, /**< Align text to center */
LV_LABEL_ALIGN_RIGHT, /**< Align text to right */
};
typedef wgl_label_align_t lv_label_align_t;
/** Label styles*/
enum {
LV_LABEL_STYLE_MAIN,
};
typedef wgl_label_style_t lv_label_style_t;
#define lv_label_create wgl_label_create
#define lv_label_set_text wgl_label_set_text
#define lv_label_get_text(label) wgl_label_get_text(label, g_widget_text, 100)
#ifdef __cplusplus
}
#endif
#endif /* WAMR_GRAPHIC_LIBRARY_LABEL_LVGL_COMPATIBLE_H */

View File

@ -0,0 +1,37 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#ifndef WAMR_GRAPHIC_LIBRARY_LIST_LVGL_COMPATIBLE_H
#define WAMR_GRAPHIC_LIBRARY_LIST_LVGL_COMPATIBLE_H
#ifdef __cplusplus
extern "C" {
#endif
#include "../inc/wgl_list.h"
/** List styles. */
enum {
LV_LIST_STYLE_BG, /**< List background style */
LV_LIST_STYLE_SCRL, /**< List scrollable area style. */
LV_LIST_STYLE_SB, /**< List scrollbar style. */
LV_LIST_STYLE_EDGE_FLASH, /**< List edge flash style. */
LV_LIST_STYLE_BTN_REL, /**< Same meaning as the ordinary button styles. */
LV_LIST_STYLE_BTN_PR,
LV_LIST_STYLE_BTN_TGL_REL,
LV_LIST_STYLE_BTN_TGL_PR,
LV_LIST_STYLE_BTN_INA,
};
typedef wgl_list_style_t lv_list_style_t;
#define lv_list_create wgl_list_create
#define lv_list_add_btn wgl_list_add_btn
#ifdef __cplusplus
}
#endif
#endif /* WAMR_GRAPHIC_LIBRARY_LIST_LVGL_COMPATIBLE_H */

View File

@ -0,0 +1,89 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#ifndef WAMR_GRAPHIC_LIBRARY_OBJ_LVGL_COMPATIBLE_H
#define WAMR_GRAPHIC_LIBRARY_OBJ_LVGL_COMPATIBLE_H
#ifdef __cplusplus
extern "C" {
#endif
#include "../inc/wgl_obj.h"
typedef void lv_obj_t;
enum {
LV_EVENT_PRESSED,
LV_EVENT_PRESSING,
LV_EVENT_PRESS_LOST,
LV_EVENT_SHORT_CLICKED,
LV_EVENT_LONG_PRESSED,
LV_EVENT_LONG_PRESSED_REPEAT,
LV_EVENT_CLICKED,
LV_EVENT_RELEASED,
LV_EVENT_DRAG_BEGIN,
LV_EVENT_DRAG_END,
LV_EVENT_DRAG_THROW_BEGIN,
LV_EVENT_KEY,
LV_EVENT_FOCUSED,
LV_EVENT_DEFOCUSED,
LV_EVENT_VALUE_CHANGED,
LV_EVENT_INSERT,
LV_EVENT_REFRESH,
LV_EVENT_APPLY,
LV_EVENT_CANCEL,
LV_EVENT_DELETE,
};
typedef wgl_event_t lv_event_t;
/** Object alignment. */
enum {
LV_ALIGN_CENTER,
LV_ALIGN_IN_TOP_LEFT,
LV_ALIGN_IN_TOP_MID,
LV_ALIGN_IN_TOP_RIGHT,
LV_ALIGN_IN_BOTTOM_LEFT,
LV_ALIGN_IN_BOTTOM_MID,
LV_ALIGN_IN_BOTTOM_RIGHT,
LV_ALIGN_IN_LEFT_MID,
LV_ALIGN_IN_RIGHT_MID,
LV_ALIGN_OUT_TOP_LEFT,
LV_ALIGN_OUT_TOP_MID,
LV_ALIGN_OUT_TOP_RIGHT,
LV_ALIGN_OUT_BOTTOM_LEFT,
LV_ALIGN_OUT_BOTTOM_MID,
LV_ALIGN_OUT_BOTTOM_RIGHT,
LV_ALIGN_OUT_LEFT_TOP,
LV_ALIGN_OUT_LEFT_MID,
LV_ALIGN_OUT_LEFT_BOTTOM,
LV_ALIGN_OUT_RIGHT_TOP,
LV_ALIGN_OUT_RIGHT_MID,
LV_ALIGN_OUT_RIGHT_BOTTOM,
};
typedef wgl_align_t lv_align_t;
enum {
LV_DRAG_DIR_HOR,
LV_DRAG_DIR_VER,
LV_DRAG_DIR_ALL,
};
typedef wgl_drag_dir_t lv_drag_dir_t;
#define lv_obj_align wgl_obj_align
#define lv_obj_set_event_cb wgl_obj_set_event_cb
#define lv_obj_del wgl_obj_del
#define lv_obj_del_async wgl_obj_del_async
#define lv_obj_clean wgl_obj_clean
#ifdef __cplusplus
}
#endif
#endif /* WAMR_GRAPHIC_LIBRARY_OBJ_LVGL_COMPATIBLE_H */

View File

@ -0,0 +1,28 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#ifndef WAMR_GRAPHIC_LIBRARY_TYPES_LVGL_COMPATIBLE_H
#define WAMR_GRAPHIC_LIBRARY_TYPES_LVGL_COMPATIBLE_H
#ifdef __cplusplus
extern "C" {
#endif
#include "../inc/wgl_types.h"
/**
* error codes.
*/
enum {
LV_RES_INV,
LV_RES_OK,
};
typedef wgl_res_t lv_res_t;
#ifdef __cplusplus
}
#endif
#endif /* WAMR_GRAPHIC_LIBRARY_TYPES_LVGL_COMPATIBLE_H */

View File

@ -0,0 +1,26 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#ifndef WAMR_GRAPHIC_LIBRARY_LVGL_COMPATIBLE_H
#define WAMR_GRAPHIC_LIBRARY_LVGL_COMPATIBLE_H
#ifdef __cplusplus
extern "C" {
#endif
#include "bi-inc/wgl_shared_utils.h" /* shared types between app and native */
#include "lvgl-compatible/lv_types.h"
#include "lvgl-compatible/lv_obj.h"
#include "lvgl-compatible/lv_btn.h"
#include "lvgl-compatible/lv_cb.h"
#include "lvgl-compatible/lv_label.h"
#include "lvgl-compatible/lv_list.h"
#ifdef __cplusplus
}
#endif
#endif /* WAMR_GRAPHIC_LIBRARY_LVGL_COMPATIBLE_H */

View File

@ -0,0 +1,26 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#ifndef WAMR_GRAPHIC_LIBRARY_H
#define WAMR_GRAPHIC_LIBRARY_H
#ifdef __cplusplus
extern "C" {
#endif
#include "bi-inc/wgl_shared_utils.h" /* shared types between app and native */
#include "inc/wgl_types.h"
#include "inc/wgl_obj.h"
#include "inc/wgl_btn.h"
#include "inc/wgl_cb.h"
#include "inc/wgl_label.h"
#include "inc/wgl_list.h"
#ifdef __cplusplus
}
#endif
#endif /* WAMR_GRAPHIC_LIBRARY_H */

View File

@ -0,0 +1,15 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
set (WASM_APP_GUI_DIR ${CMAKE_CURRENT_LIST_DIR})
set (DEPS_DIR ${WASM_APP_GUI_DIR}/../../../deps)
include_directories(${WASM_APP_GUI_DIR}
${DEPS_DIR}
${DEPS_DIR}/lvgl
${DEPS_DIR}/lvgl/src)
file (GLOB_RECURSE source_all ${WASM_APP_GUI_DIR}/src/*.c)
set (WASM_APP_CURRENT_SOURCE ${source_all})

View File

@ -0,0 +1,36 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#ifndef _GUI_API_H_
#define _GUI_API_H_
#include "bh_platform.h"
#ifdef __cplusplus
extern "C" {
#endif
void
wasm_obj_native_call(int32 func_id, int32 argv_offset, uint32 argc);
void
wasm_btn_native_call(int32 func_id, int32 argv_offset, uint32 argc);
void
wasm_label_native_call(int32 func_id, int32 argv_offset, uint32 argc);
void
wasm_cb_native_call(int32 func_id, int32 argv_offset, uint32 argc);
void
wasm_list_native_call(int32 func_id, int32 argv_offset, uint32 argc);
#ifdef __cplusplus
}
#endif
#endif /* end of _GUI_API_H_ */

View File

@ -0,0 +1,28 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
/* button */
EXPORT_WASM_API(wasm_btn_native_call),
/* obj */
EXPORT_WASM_API(wasm_obj_native_call),
/* label */
EXPORT_WASM_API(wasm_label_native_call),
/* cont */
//EXPORT_WASM_API(wasm_cont_native_call),
/* page */
//EXPORT_WASM_API(wasm_page_native_call),
/* list */
EXPORT_WASM_API(wasm_list_native_call),
/* drop down list */
//EXPORT_WASM_API(wasm_ddlist_native_call),
/* check box */
EXPORT_WASM_API(wasm_cb_native_call),

View File

@ -0,0 +1,18 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
set (WASM_LIB_GUI_DIR ${CMAKE_CURRENT_LIST_DIR})
set (DEPS_DIR ${WASM_LIB_GUI_DIR}/../../../deps)
add_definitions(-DLV_CONF_INCLUDE_SIMPLE)
include_directories(${WASM_LIB_GUI_DIR}
${DEPS_DIR}
${DEPS_DIR}/lvgl
${DEPS_DIR}/lvgl/src)
file (GLOB_RECURSE lvgl_source ${DEPS_DIR}/lvgl/*.c)
file (GLOB_RECURSE wrapper_source ${WASM_LIB_GUI_DIR}/*.c)
set (WASM_APP_LIB_CURRENT_SOURCE ${wrapper_source} ${lvgl_source})

View File

@ -0,0 +1,20 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#ifndef WAMR_GRAPHIC_LIBRARY_H
#define WAMR_GRAPHIC_LIBRARY_H
#ifdef __cplusplus
extern "C" {
#endif
void wgl_init(void);
void wgl_exit(void);
#ifdef __cplusplus
}
#endif
#endif /* WAMR_GRAPHIC_LIBRARY_H */

View File

@ -0,0 +1,140 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "native_interface.h"
#include "lvgl.h"
#include "module_wasm_app.h"
#include "wgl_native_utils.h"
/* -------------------------------------------------------------------------
* Button widget native function wrappers
* -------------------------------------------------------------------------*/
static int32
lv_btn_create_wrapper(wasm_module_inst_t module_inst,
lv_obj_t *par, lv_obj_t *copy)
{
return wgl_native_wigdet_create(WIDGET_TYPE_BTN, par, copy, module_inst);
}
static void
lv_btn_set_toggle_wrapper(wasm_module_inst_t module_inst,
lv_obj_t * btn, bool tgl)
{
(void)module_inst;
lv_btn_set_toggle(btn, tgl);
}
static void
lv_btn_set_state_wrapper(wasm_module_inst_t module_inst,
lv_obj_t * btn, lv_btn_state_t state)
{
(void)module_inst;
lv_btn_set_state(btn, state);
}
static void
lv_btn_set_ink_in_time_wrapper(wasm_module_inst_t module_inst,
lv_obj_t * btn, uint16_t time)
{
(void)module_inst;
lv_btn_set_ink_in_time(btn, time);
}
static void
lv_btn_set_ink_out_time_wrapper(wasm_module_inst_t module_inst,
lv_obj_t * btn, uint16_t time)
{
(void)module_inst;
lv_btn_set_ink_out_time(btn, time);
}
static void
lv_btn_set_ink_wait_time_wrapper(wasm_module_inst_t module_inst,
lv_obj_t * btn, uint16_t time)
{
(void)module_inst;
lv_btn_set_ink_wait_time(btn, time);
}
static uint16_t
lv_btn_get_ink_in_time_wrapper(wasm_module_inst_t module_inst,
lv_obj_t * btn)
{
(void)module_inst;
return lv_btn_get_ink_in_time(btn);
}
static uint16_t
lv_btn_get_ink_out_time_wrapper(wasm_module_inst_t module_inst,
lv_obj_t * btn)
{
(void)module_inst;
return lv_btn_get_ink_out_time(btn);
}
static uint16_t
lv_btn_get_ink_wait_time_wrapper(wasm_module_inst_t module_inst,
lv_obj_t * btn)
{
(void)module_inst;
return lv_btn_get_ink_wait_time(btn);
}
static lv_btn_state_t
lv_btn_get_state_wrapper(wasm_module_inst_t module_inst,
lv_obj_t * btn)
{
(void)module_inst;
return lv_btn_get_state(btn);
}
static bool
lv_btn_get_toggle_wrapper(wasm_module_inst_t module_inst,
lv_obj_t * btn)
{
(void)module_inst;
return lv_btn_get_toggle(btn);
}
static void
lv_btn_toggle_wrapper(wasm_module_inst_t module_inst,
lv_obj_t * btn)
{
(void)module_inst;
lv_btn_toggle(btn);
}
static WGLNativeFuncDef btn_native_func_defs[] = {
{ BTN_FUNC_ID_CREATE, lv_btn_create_wrapper, HAS_RET, 3, {1 | NULL_OK, 2 | NULL_OK, -1}, {-1} },
{ BTN_FUNC_ID_SET_TOGGLE, lv_btn_set_toggle_wrapper, NO_RET, 3, {1, -1}, {-1} },
{ BTN_FUNC_ID_SET_STATE, lv_btn_set_state_wrapper, NO_RET, 3, {1, -1}, {-1} },
// { BTN_FUNC_ID_SET_STYLE, _btn_set_style, NO_RET, 2, {0, -1}, {-1} },
{ BTN_FUNC_ID_SET_INK_IN_TIME, lv_btn_set_ink_in_time_wrapper, NO_RET, 3, {1, -1}, {-1} },
{ BTN_FUNC_ID_SET_INK_OUT_TIME, lv_btn_set_ink_out_time_wrapper, NO_RET, 3, {1, -1}, {-1} },
{ BTN_FUNC_ID_SET_INK_WAIT_TIME, lv_btn_set_ink_wait_time_wrapper, NO_RET, 3, {1, -1}, {-1} },
{ BTN_FUNC_ID_GET_INK_IN_TIME, lv_btn_get_ink_in_time_wrapper, HAS_RET, 2, {1, -1}, {-1} },
{ BTN_FUNC_ID_GET_INK_OUT_TIME, lv_btn_get_ink_out_time_wrapper, HAS_RET, 2, {1, -1}, {-1} },
{ BTN_FUNC_ID_GET_INK_WAIT_TIME, lv_btn_get_ink_wait_time_wrapper, HAS_RET, 2, {1, -1}, {-1} },
{ BTN_FUNC_ID_GET_STATE, lv_btn_get_state_wrapper, HAS_RET, 2, {1, -1}, {-1} },
{ BTN_FUNC_ID_GET_TOGGLE, lv_btn_get_toggle_wrapper, HAS_RET, 2, {1, -1}, {-1} },
{ BTN_FUNC_ID_TOGGLE, lv_btn_toggle_wrapper, NO_RET, 2, {1, -1}, {-1} },
};
/*************** Native Interface to Wasm App ***********/
void
wasm_btn_native_call(wasm_exec_env_t exec_env,
int32 func_id, uint32 argv_offset, uint32 argc)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
uint32 size = sizeof(btn_native_func_defs) / sizeof(WGLNativeFuncDef);
wgl_native_func_call(module_inst,
btn_native_func_defs,
size,
func_id,
argv_offset,
argc);
}

View File

@ -0,0 +1,87 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "lvgl.h"
#include "wasm_export.h"
#include "native_interface.h"
#include "module_wasm_app.h"
#include "wgl_native_utils.h"
/* -------------------------------------------------------------------------
* Label widget native function wrappers
* -------------------------------------------------------------------------*/
static int32
lv_cb_create_wrapper(wasm_module_inst_t module_inst,
lv_obj_t *par, lv_obj_t *copy)
{
return wgl_native_wigdet_create(WIDGET_TYPE_CB, par, copy, module_inst);
}
static void
lv_cb_set_text_wrapper(wasm_module_inst_t module_inst,
lv_obj_t * cb, const char * txt)
{
(void)module_inst;
lv_cb_set_text(cb, txt);
}
static void
lv_cb_set_static_text_wrapper(wasm_module_inst_t module_inst,
lv_obj_t * cb, const char * txt)
{
(void)module_inst;
lv_cb_set_static_text(cb, txt);
}
static int32
lv_cb_get_text_length_wrapper(wasm_module_inst_t module_inst,
lv_obj_t *cb)
{
const char *text = lv_cb_get_text(cb);
if (text == NULL)
return 0;
return strlen(text);
}
static char *
lv_cb_get_text_wrapper(wasm_module_inst_t module_inst,
lv_obj_t *cb, char *buffer, int buffer_len)
{
const char *text = lv_cb_get_text(cb);
if (text == NULL)
return 0;
strncpy(buffer, text, buffer_len - 1);
buffer[buffer_len - 1] = '\0';
return buffer;
}
static WGLNativeFuncDef cb_native_func_defs[] = {
{ CB_FUNC_ID_CREATE, lv_cb_create_wrapper, HAS_RET, 3, {1 | NULL_OK, 2 | NULL_OK, -1}, {-1} },
{ CB_FUNC_ID_SET_TEXT, lv_cb_set_text_wrapper, NO_RET, 3, {1, -1}, {2, -1} },
{ CB_FUNC_ID_SET_STATIC_TEXT, lv_cb_set_static_text_wrapper, NO_RET, 3, {1, -1}, {2, -1} },
{ CB_FUNC_ID_GET_TEXT_LENGTH, lv_cb_get_text_length_wrapper, HAS_RET, 2, {1, -1}, {-1} },
{ CB_FUNC_ID_GET_TEXT, lv_cb_get_text_wrapper, RET_PTR, 4, {1, -1}, {2, -1} },
};
/*************** Native Interface to Wasm App ***********/
void
wasm_cb_native_call(wasm_exec_env_t exec_env,
int32 func_id, uint32 argv_offset, uint32 argc)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
uint32 size = sizeof(cb_native_func_defs) / sizeof(WGLNativeFuncDef);
wgl_native_func_call(module_inst,
cb_native_func_defs,
size,
func_id,
argv_offset,
argc);
}

View File

@ -0,0 +1,7 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "lvgl.h"
#include "module_wasm_app.h"

View File

@ -0,0 +1,78 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "lvgl.h"
#include "wasm_export.h"
#include "native_interface.h"
#include "module_wasm_app.h"
#include "wgl_native_utils.h"
/* -------------------------------------------------------------------------
* Label widget native function wrappers
* -------------------------------------------------------------------------*/
static int32
lv_label_create_wrapper(wasm_module_inst_t module_inst,
lv_obj_t *par, lv_obj_t *copy)
{
return wgl_native_wigdet_create(WIDGET_TYPE_LABEL, par, copy, module_inst);
}
static void
lv_label_set_text_wrapper(wasm_module_inst_t module_inst,
lv_obj_t * label, const char * text)
{
(void)module_inst;
lv_label_set_text(label, text);
}
static int32
lv_label_get_text_length_wrapper(wasm_module_inst_t module_inst,
lv_obj_t *label)
{
char *text = lv_label_get_text(label);
if (text == NULL)
return 0;
return strlen(text);
}
static char *
lv_label_get_text_wrapper(wasm_module_inst_t module_inst,
lv_obj_t *label, char *buffer, int buffer_len)
{
char *text = lv_label_get_text(label);
if (text == NULL)
return 0;
strncpy(buffer, text, buffer_len - 1);
buffer[buffer_len - 1] = '\0';
return buffer;
}
static WGLNativeFuncDef label_native_func_defs[] = {
{ LABEL_FUNC_ID_CREATE, lv_label_create_wrapper, HAS_RET, 3, {1 | NULL_OK, 2 | NULL_OK, -1}, {-1} },
{ LABEL_FUNC_ID_SET_TEXT, lv_label_set_text_wrapper, NO_RET, 3, {1, -1}, {2, -1} },
{ LABEL_FUNC_ID_GET_TEXT_LENGTH, lv_label_get_text_length_wrapper, HAS_RET, 2, {1, -1}, {-1} },
{ LABEL_FUNC_ID_GET_TEXT, lv_label_get_text_wrapper, RET_PTR, 4, {1, -1}, {2, -1} },
};
/*************** Native Interface to Wasm App ***********/
void
wasm_label_native_call(wasm_exec_env_t exec_env,
int32 func_id, uint32 argv_offset, uint32 argc)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
uint32 size = sizeof(label_native_func_defs) / sizeof(WGLNativeFuncDef);
wgl_native_func_call(module_inst,
label_native_func_defs,
size,
func_id,
argv_offset,
argc);
}

View File

@ -0,0 +1,63 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "native_interface.h"
#include "lvgl.h"
#include "module_wasm_app.h"
#include "wgl_native_utils.h"
#include "bh_assert.h"
/* -------------------------------------------------------------------------
* List widget native function wrappers
* -------------------------------------------------------------------------*/
static int32
lv_list_create_wrapper(wasm_module_inst_t module_inst,
lv_obj_t *par, lv_obj_t *copy)
{
return wgl_native_wigdet_create(WIDGET_TYPE_LIST, par, copy, module_inst);
}
static int32
lv_list_add_btn_wrapper(wasm_module_inst_t module_inst,
lv_obj_t *list, const char *text)
{
uint32 btn_obj_id;
lv_obj_t *btn;
uint32 mod_id;
btn = lv_list_add_btn(list, NULL, text);
if (btn == NULL)
return 0;
mod_id = app_manager_get_module_id(Module_WASM_App, module_inst);
bh_assert(mod_id != ID_NONE);
if (wgl_native_add_object(btn, mod_id, &btn_obj_id))
return btn_obj_id; /* success return */
return 0;
}
static WGLNativeFuncDef list_native_func_defs[] = {
{ LIST_FUNC_ID_CREATE, lv_list_create_wrapper, HAS_RET, 3, {1 | NULL_OK, 2 | NULL_OK, -1}, {-1} },
{ LIST_FUNC_ID_ADD_BTN, lv_list_add_btn_wrapper, HAS_RET, 3, {1, -1}, {2, -1} },
};
/*************** Native Interface to Wasm App ***********/
void
wasm_list_native_call(wasm_exec_env_t exec_env,
int32 func_id, uint32 argv_offset, uint32 argc)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
uint32 size = sizeof(list_native_func_defs) / sizeof(WGLNativeFuncDef);
wgl_native_func_call(module_inst,
list_native_func_defs,
size,
func_id,
argv_offset,
argc);
}

View File

@ -0,0 +1,215 @@
#include "wgl_native_utils.h"
#include "lvgl.h"
#include "module_wasm_app.h"
#include "wasm_export.h"
#include "bh_assert.h"
#include <stdint.h>
#define THROW_EXC(msg) wasm_runtime_set_exception(module_inst, msg);
void
wasm_runtime_set_exception(wasm_module_inst_t module, const char *exception);
uint32 wgl_native_wigdet_create(int8 widget_type, lv_obj_t *par, lv_obj_t *copy,
wasm_module_inst_t module_inst)
{
uint32 obj_id;
lv_obj_t *wigdet = NULL;
uint32 mod_id;
//TODO: limit total widget number
if (par == NULL)
par = lv_disp_get_scr_act(NULL);
if (widget_type == WIDGET_TYPE_BTN)
wigdet = lv_btn_create(par, copy);
else if (widget_type == WIDGET_TYPE_LABEL)
wigdet = lv_label_create(par, copy);
else if (widget_type == WIDGET_TYPE_CB)
wigdet = lv_cb_create(par, copy);
else if (widget_type == WIDGET_TYPE_LIST)
wigdet = lv_list_create(par, copy);
else if (widget_type == WIDGET_TYPE_DDLIST)
wigdet = lv_ddlist_create(par, copy);
if (wigdet == NULL)
return 0;
mod_id = app_manager_get_module_id(Module_WASM_App, module_inst);
bh_assert(mod_id != ID_NONE);
if (wgl_native_add_object(wigdet, mod_id, &obj_id))
return obj_id; /* success return */
return 0;
}
static void invokeNative(intptr_t argv[], uint32 argc, void (*native_code)())
{
bh_assert(argc >= 1);
switch(argc) {
case 1:
native_code(argv[0]);
break;
case 2:
native_code(argv[0], argv[1]);
break;
case 3:
native_code(argv[0], argv[1], argv[2]);
break;
case 4:
native_code(argv[0], argv[1], argv[2], argv[3]);
break;
case 5:
native_code(argv[0], argv[1], argv[2], argv[3], argv[4]);
break;
case 6:
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]);
break;
case 7:
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5],
argv[6]);
break;
case 8:
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5],
argv[6], argv[7]);
break;
case 9:
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5],
argv[6], argv[7], argv[8]);
break;
case 10:
native_code(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5],
argv[6], argv[7], argv[8], argv[9]);
break;
default:
{
/* FIXME: If this happen, add more cases. */
wasm_module_inst_t module_inst = (wasm_module_inst_t)argv[0];
THROW_EXC("the argument number of native function exceeds maximum");
return;
}
}
}
typedef void (*GenericFunctionPointer)();
typedef int32 (*Int32FuncPtr)(intptr_t *, uint32, GenericFunctionPointer);
typedef void (*VoidFuncPtr)(intptr_t *, uint32, GenericFunctionPointer);
static Int32FuncPtr invokeNative_Int32 = (Int32FuncPtr)invokeNative;
static VoidFuncPtr invokeNative_Void = (VoidFuncPtr)invokeNative;
void wgl_native_func_call(wasm_module_inst_t module_inst,
WGLNativeFuncDef *funcs,
uint32 size,
int32 func_id,
uint32 argv_offset,
uint32 argc)
{
WGLNativeFuncDef *func_def = funcs;
WGLNativeFuncDef *func_def_end = func_def + size;
uint32 *argv;
if (!validate_app_addr(argv_offset, argc * sizeof(uint32)))
return;
argv = addr_app_to_native(argv_offset);
while (func_def < func_def_end) {
if (func_def->func_id == func_id) {
int i, obj_arg_num = 0, ptr_arg_num = 0, argc1 = 0;
intptr_t argv_copy_buf[16];
intptr_t *argv_copy = argv_copy_buf;
argc1++; /* module_inst */
argc1 += func_def->arg_num;
if (argc1 > 16) {
argv_copy = (intptr_t *)bh_malloc(func_def->arg_num *
sizeof(intptr_t));
if (argv_copy == NULL)
return;
}
/* Init argv_copy */
argv_copy[0] = (intptr_t)module_inst;
for (i = 0; i < func_def->arg_num; i++)
argv_copy[i + 1] = (intptr_t)argv[i];
/* Validate object arguments */
i = 0;
for (; i < OBJ_ARG_NUM_MAX && func_def->obj_arg_indexes[i] != 0xff;
i++, obj_arg_num++) {
uint8 index = func_def->obj_arg_indexes[i];
bool null_ok = index & NULL_OK;
index = index & (~NULL_OK);
/* Some API's allow to pass NULL obj, such as xxx_create() */
if (argv_copy[index] == 0) {
if (!null_ok) {
THROW_EXC("the object id is 0 and invalid");
goto fail;
}
/* Continue so that to pass null object validation */
continue;
}
if (!wgl_native_validate_object(argv_copy[index],
(lv_obj_t **)&argv_copy[index])) {
THROW_EXC("the object is invalid");
goto fail;
}
}
/* Validate address arguments */
i = 0;
for (; i < PTR_ARG_NUM_MAX && func_def->ptr_arg_indexes[i] != 0xff;
i++, ptr_arg_num++) {
uint8 index = func_def->ptr_arg_indexes[i];
/* The index+1 arg is the data size to be validated */
if (!validate_app_addr(argv_copy[index], argv_copy[index + 1]))
goto fail;
/* Convert to native address before call lvgl function */
argv_copy[index] = (intptr_t)addr_app_to_native(argv_copy[index]);
}
if (func_def->has_ret == NO_RET)
invokeNative_Void(argv_copy,
argc1,
func_def->func_ptr);
else {
argv[0] = invokeNative_Int32(argv_copy,
argc1,
func_def->func_ptr);
/* Convert to app memory offset if return value is a
* native address pointer */
if (func_def->has_ret == RET_PTR)
argv[0] = addr_native_to_app((char *)(intptr_t)argv[0]);
}
if (argv_copy != argv_copy_buf)
bh_free(argv_copy);
/* success return */
return;
fail:
if (argv_copy != argv_copy_buf)
bh_free(argv_copy);
return;
}
func_def++;
}
THROW_EXC("the native widget function is not found!");
}

View File

@ -0,0 +1,84 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#ifndef WAMR_GRAPHIC_LIBRARY_NATIVE_UTILS_H
#define WAMR_GRAPHIC_LIBRARY_NATIVE_UTILS_H
#ifdef __cplusplus
extern "C" {
#endif
#include "bh_platform.h"
#include "lvgl.h"
#include "wasm_export.h"
#include "bi-inc/wgl_shared_utils.h"
#define OBJ_ARG_NUM_MAX 4
#define PTR_ARG_NUM_MAX 4
#define NULL_OK 0x80
enum {
/* The function has a normal return value (not a pointer) */
HAS_RET,
/* The function doesn't have return value */
NO_RET,
/* The function's return value is a native address pointer */
RET_PTR
};
enum {
WIDGET_TYPE_BTN,
WIDGET_TYPE_LABEL,
WIDGET_TYPE_CB,
WIDGET_TYPE_LIST,
WIDGET_TYPE_DDLIST,
_WIDGET_TYPE_NUM,
};
typedef struct WGLNativeFuncDef {
/* Function id */
int32 func_id;
/* Native function pointer */
void *func_ptr;
/* whether has return value */
uint8 has_ret;
/* argument number */
uint8 arg_num;
/* low 7 bit: obj argument index
* highest 1 bit: allow obj be null or not
* -1 means the end of this array */
uint8 obj_arg_indexes[OBJ_ARG_NUM_MAX];
/* pointer argument indexes, -1 means the end of this array */
uint8 ptr_arg_indexes[PTR_ARG_NUM_MAX];
} WGLNativeFuncDef;
bool wgl_native_validate_object(int32 obj_id, lv_obj_t **obj);
bool wgl_native_add_object(lv_obj_t *obj, uint32 module_id, uint32 *obj_id);
uint32 wgl_native_wigdet_create(int8 widget_type,
lv_obj_t *par,
lv_obj_t *copy,
wasm_module_inst_t module_inst);
void wgl_native_func_call(wasm_module_inst_t module_inst,
WGLNativeFuncDef *funcs,
uint32 size,
int32 func_id,
uint32 argv_offset,
uint32 argc);
#ifdef __cplusplus
}
#endif
#endif /* WAMR_GRAPHIC_LIBRARY_NATIVE_UTILS_H */

View File

@ -0,0 +1,389 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "lvgl.h"
#include "app_manager_export.h"
#include "module_wasm_app.h"
#include "bh_list.h"
#include "bh_thread.h"
#include "wgl_native_utils.h"
#include "wgl.h"
typedef struct {
bh_list_link l;
/* The object id. */
uint32 obj_id;
/* The lv object */
lv_obj_t *obj;
/* Module id that the obj belongs to */
uint32 module_id;
} object_node_t;
typedef struct {
int32 obj_id;
lv_event_t event;
} object_event_t;
/* Max obj id */
static uint32 g_obj_id_max = 0;
static bh_list g_object_list;
static korp_mutex g_object_list_mutex;
static bool lv_task_handler_thread_run = true;
static void app_mgr_object_event_callback(module_data *m_data, bh_message_t msg)
{
uint32 argv[2];
wasm_function_inst_t func_on_object_event;
bh_assert(WIDGET_EVENT_WASM == bh_message_type(msg));
wasm_data *wasm_app_data = (wasm_data*)m_data->internal_data;
wasm_module_inst_t inst = wasm_app_data->wasm_module_inst;
object_event_t *object_event
= (object_event_t *)bh_message_payload(msg);
if (object_event == NULL)
return;
func_on_object_event = wasm_runtime_lookup_function(inst, "_on_widget_event",
"(i32i32)");
if (!func_on_object_event)
func_on_object_event = wasm_runtime_lookup_function(inst, "on_widget_event",
"(i32i32)");
if (!func_on_object_event) {
printf("Cannot find function on_widget_event\n");
return;
}
argv[0] = object_event->obj_id;
argv[1] = object_event->event;
if (!wasm_runtime_call_wasm(wasm_app_data->exec_env, func_on_object_event,
2, argv)) {
const char *exception = wasm_runtime_get_exception(inst);
bh_assert(exception);
printf(":Got exception running wasm code: %s\n",
exception);
wasm_runtime_clear_exception(inst);
return;
}
}
static void cleanup_object_list(uint32 module_id)
{
object_node_t *elem;
vm_mutex_lock(&g_object_list_mutex);
while (true) {
bool found = false;
elem = (object_node_t *)bh_list_first_elem(&g_object_list);
while (elem) {
/* delete the leaf node belongs to the module firstly */
if (module_id == elem->module_id &&
lv_obj_count_children(elem->obj) == 0) {
object_node_t *next = (object_node_t *)bh_list_elem_next(elem);
found = true;
lv_obj_del(elem->obj);
bh_list_remove(&g_object_list, elem);
bh_free(elem);
elem = next;
} else {
elem = (object_node_t *)bh_list_elem_next(elem);
}
}
if (!found)
break;
}
vm_mutex_unlock(&g_object_list_mutex);
}
static bool init_object_event_callback_framework()
{
if (!wasm_register_cleanup_callback(cleanup_object_list)) {
goto fail;
}
if (!wasm_register_msg_callback(WIDGET_EVENT_WASM,
app_mgr_object_event_callback)) {
goto fail;
}
return true;
fail:
return false;
}
bool wgl_native_validate_object(int32 obj_id, lv_obj_t **obj)
{
object_node_t *elem;
vm_mutex_lock(&g_object_list_mutex);
elem = (object_node_t *)bh_list_first_elem(&g_object_list);
while (elem) {
if (obj_id == elem->obj_id) {
if (obj != NULL)
*obj = elem->obj;
vm_mutex_unlock(&g_object_list_mutex);
return true;
}
elem = (object_node_t *) bh_list_elem_next(elem);
}
vm_mutex_unlock(&g_object_list_mutex);
return false;
}
bool wgl_native_add_object(lv_obj_t *obj, uint32 module_id, uint32 *obj_id)
{
object_node_t *node;
node = (object_node_t *) bh_malloc(sizeof(object_node_t));
if (node == NULL)
return false;
/* Generate an obj id */
g_obj_id_max++;
if (g_obj_id_max == -1)
g_obj_id_max = 1;
memset(node, 0, sizeof(*node));
node->obj = obj;
node->obj_id = g_obj_id_max;
node->module_id = module_id;
vm_mutex_lock(&g_object_list_mutex);
bh_list_insert(&g_object_list, node);
vm_mutex_unlock(&g_object_list_mutex);
if (obj_id != NULL)
*obj_id = node->obj_id;
return true;
}
static void _obj_del_recursive(lv_obj_t *obj)
{
object_node_t *elem;
lv_obj_t * i;
lv_obj_t * i_next;
i = lv_ll_get_head(&(obj->child_ll));
while (i != NULL) {
/*Get the next object before delete this*/
i_next = lv_ll_get_next(&(obj->child_ll), i);
/*Call the recursive del to the child too*/
_obj_del_recursive(i);
/*Set i to the next node*/
i = i_next;
}
vm_mutex_lock(&g_object_list_mutex);
elem = (object_node_t *)bh_list_first_elem(&g_object_list);
while (elem) {
if (obj == elem->obj) {
bh_list_remove(&g_object_list, elem);
bh_free(elem);
vm_mutex_unlock(&g_object_list_mutex);
return;
}
elem = (object_node_t *) bh_list_elem_next(elem);
}
vm_mutex_unlock(&g_object_list_mutex);
}
static void _obj_clean_recursive(lv_obj_t *obj)
{
lv_obj_t * i;
lv_obj_t * i_next;
i = lv_ll_get_head(&(obj->child_ll));
while (i != NULL) {
/*Get the next object before delete this*/
i_next = lv_ll_get_next(&(obj->child_ll), i);
/*Call the recursive del to the child too*/
_obj_del_recursive(i);
/*Set i to the next node*/
i = i_next;
}
}
static void post_widget_msg_to_module(object_node_t *object_node, lv_event_t event)
{
module_data *module = module_data_list_lookup_id(object_node->module_id);
object_event_t *object_event;
if (module == NULL)
return;
object_event = (object_event_t *)bh_malloc(sizeof(*object_event));
if (object_event == NULL)
return;
memset(object_event, 0, sizeof(*object_event));
object_event->obj_id = object_node->obj_id;
object_event->event = event;
bh_post_msg(module->queue,
WIDGET_EVENT_WASM,
object_event,
sizeof(*object_event));
}
static void internal_lv_obj_event_cb(lv_obj_t *obj, lv_event_t event)
{
object_node_t *elem;
vm_mutex_lock(&g_object_list_mutex);
elem = (object_node_t *)bh_list_first_elem(&g_object_list);
while (elem) {
if (obj == elem->obj) {
post_widget_msg_to_module(elem, event);
vm_mutex_unlock(&g_object_list_mutex);
return;
}
elem = (object_node_t *) bh_list_elem_next(elem);
}
vm_mutex_unlock(&g_object_list_mutex);
}
static void* lv_task_handler_thread_routine (void *arg)
{
korp_sem sem;
vm_sem_init(&sem, 0);
while (lv_task_handler_thread_run) {
vm_sem_reltimedwait(&sem, 100);
lv_task_handler();
}
vm_sem_destroy(&sem);
return NULL;
}
void wgl_init(void)
{
korp_thread tid;
lv_init();
bh_list_init(&g_object_list);
vm_recursive_mutex_init(&g_object_list_mutex);
init_object_event_callback_framework();
/* new a thread, call lv_task_handler periodically */
vm_thread_create(&tid,
lv_task_handler_thread_routine,
NULL,
BH_APPLET_PRESERVED_STACK_SIZE);
}
void wgl_exit(void)
{
lv_task_handler_thread_run = false;
}
/* -------------------------------------------------------------------------
* Obj native function wrappers
* -------------------------------------------------------------------------*/
static lv_res_t
lv_obj_del_wrapper(wasm_module_inst_t module_inst, lv_obj_t *obj)
{
(void)module_inst;
/* Recursively delete object node in the list belong to this
* parent object including itself */
_obj_del_recursive(obj);
return lv_obj_del(obj);
}
static void
lv_obj_del_async_wrapper(wasm_module_inst_t module_inst, lv_obj_t * obj)
{
(void)module_inst;
lv_obj_del_async(obj);
}
static void
lv_obj_clean_wrapper(wasm_module_inst_t module_inst, lv_obj_t *obj)
{
(void)module_inst;
/* Recursively delete child object node in the list belong to this
* parent object */
_obj_clean_recursive(obj);
/* Delete all of its children */
lv_obj_clean(obj);
}
static void
lv_obj_align_wrapper(wasm_module_inst_t module_inst,
lv_obj_t * obj,
const lv_obj_t * base,
lv_align_t align,
lv_coord_t x_mod,
lv_coord_t y_mod)
{
(void)module_inst;
lv_obj_align(obj, base, align, x_mod, y_mod);
}
static void
lv_obj_set_event_cb_wrapper(wasm_module_inst_t module_inst, lv_obj_t *obj)
{
(void)module_inst;
lv_obj_set_event_cb(obj, internal_lv_obj_event_cb);
}
/* ------------------------------------------------------------------------- */
static WGLNativeFuncDef obj_native_func_defs[] = {
{ OBJ_FUNC_ID_DEL, lv_obj_del_wrapper, HAS_RET, 2, {1, -1}, {-1} },
{ OBJ_FUNC_ID_DEL_ASYNC, lv_obj_del_async_wrapper, NO_RET, 2, {1, -1}, {-1} },
{ OBJ_FUNC_ID_CLEAN, lv_obj_clean_wrapper, NO_RET, 2, {1, -1}, {-1} },
{ OBJ_FUNC_ID_ALIGN, lv_obj_align_wrapper, NO_RET, 6, {1, 2 | NULL_OK, -1}, {-1} },
{ OBJ_FUNC_ID_SET_EVT_CB, lv_obj_set_event_cb_wrapper, NO_RET, 2, {1, -1}, {-1} },
};
/*************** Native Interface to Wasm App ***********/
void
wasm_obj_native_call(wasm_exec_env_t exec_env,
int32 func_id, uint32 argv_offset, uint32 argc)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
uint32 size = sizeof(obj_native_func_defs) / sizeof(WGLNativeFuncDef);
wgl_native_func_call(module_inst,
obj_native_func_defs,
size,
func_id,
argv_offset,
argc);
}