Apply clang-format for more source files (#795)

Apply clang-format for C source files in folder core/app-mgr,
core/app-framework, and test-tools.
And rename folder component_test to component-test, update
zephyr build document.

Signed-off-by: Wenyong Huang <wenyong.huang@intel.com>
This commit is contained in:
Wenyong Huang 2021-10-21 13:58:34 +08:00 committed by GitHub
parent 225f5d0a64
commit 32242988ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
143 changed files with 5377 additions and 4627 deletions

View File

@ -33,7 +33,7 @@ jobs:
uses: actions/checkout@v2 uses: actions/checkout@v2
with: with:
fetch-depth: 0 fetch-depth: 0
# github.event.pull_request.base.label = ${{github.repository}}/${{github.base_ref}} # github.event.pull_request.base.label = ${{github.repository}}/${{github.base_ref}}
- name: Run Coding Guidelines Checks - name: Run Coding Guidelines Checks
run: /usr/bin/env python3 ./ci/coding_guidelines_check.py --commits ${{ github.event.pull_request.base.sha }}..HEAD run: /usr/bin/env python3 ./ci/coding_guidelines_check.py --commits ${{ github.event.pull_request.base.sha }}..HEAD

View File

@ -16,79 +16,89 @@ typedef union jvalue {
double d; double d;
} jvalue; } jvalue;
static inline int16_t
get_int16(const char *buf)
static inline int16_t get_int16(const char *buf)
{ {
int16_t ret; int16_t ret;
bh_memcpy_s(&ret, sizeof(int16_t), buf, sizeof(int16_t)); bh_memcpy_s(&ret, sizeof(int16_t), buf, sizeof(int16_t));
return ret; return ret;
} }
static inline uint16_t get_uint16(const char *buf) static inline uint16_t
get_uint16(const char *buf)
{ {
return get_int16(buf); return get_int16(buf);
} }
static inline int32_t get_int32(const char *buf) static inline int32_t
get_int32(const char *buf)
{ {
int32_t ret; int32_t ret;
bh_memcpy_s(&ret, sizeof(int32_t), buf, sizeof(int32_t)); bh_memcpy_s(&ret, sizeof(int32_t), buf, sizeof(int32_t));
return ret; return ret;
} }
static inline uint32_t get_uint32(const char *buf) static inline uint32_t
get_uint32(const char *buf)
{ {
return get_int32(buf); return get_int32(buf);
} }
static inline int64_t get_int64(const char *buf) static inline int64_t
get_int64(const char *buf)
{ {
int64_t ret; int64_t ret;
bh_memcpy_s(&ret, sizeof(int64_t), buf, sizeof(int64_t)); bh_memcpy_s(&ret, sizeof(int64_t), buf, sizeof(int64_t));
return ret; return ret;
} }
static inline uint64_t get_uint64(const char *buf) static inline uint64_t
get_uint64(const char *buf)
{ {
return get_int64(buf); return get_int64(buf);
} }
static inline void set_int16(char *buf, int16_t v) static inline void
set_int16(char *buf, int16_t v)
{ {
bh_memcpy_s(buf, sizeof(int16_t), &v, sizeof(int16_t)); bh_memcpy_s(buf, sizeof(int16_t), &v, sizeof(int16_t));
} }
static inline void set_uint16(char *buf, uint16_t v) static inline void
set_uint16(char *buf, uint16_t v)
{ {
bh_memcpy_s(buf, sizeof(uint16_t), &v, sizeof(uint16_t)); bh_memcpy_s(buf, sizeof(uint16_t), &v, sizeof(uint16_t));
} }
static inline void set_int32(char *buf, int32_t v) static inline void
set_int32(char *buf, int32_t v)
{ {
bh_memcpy_s(buf, sizeof(int32_t), &v, sizeof(int32_t)); bh_memcpy_s(buf, sizeof(int32_t), &v, sizeof(int32_t));
} }
static inline void set_uint32(char *buf, uint32_t v) static inline void
set_uint32(char *buf, uint32_t v)
{ {
bh_memcpy_s(buf, sizeof(uint32_t), &v, sizeof(uint32_t)); bh_memcpy_s(buf, sizeof(uint32_t), &v, sizeof(uint32_t));
} }
static inline void set_int64(char *buf, int64_t v) static inline void
set_int64(char *buf, int64_t v)
{ {
bh_memcpy_s(buf, sizeof(int64_t), &v, sizeof(int64_t)); bh_memcpy_s(buf, sizeof(int64_t), &v, sizeof(int64_t));
} }
static inline void set_uint64(char *buf, uint64_t v) static inline void
set_uint64(char *buf, uint64_t v)
{ {
bh_memcpy_s(buf, sizeof(uint64_t), &v, sizeof(uint64_t)); bh_memcpy_s(buf, sizeof(uint64_t), &v, sizeof(uint64_t));
} }
char* char *
attr_container_get_attr_begin(const attr_container_t *attr_cont, attr_container_get_attr_begin(const attr_container_t *attr_cont,
uint32_t *p_total_length, uint16_t *p_attr_num) uint32_t *p_total_length, uint16_t *p_attr_num)
{ {
char *p = (char*) attr_cont->buf; char *p = (char *)attr_cont->buf;
uint16_t str_len, attr_num; uint16_t str_len, attr_num;
uint32_t total_length; uint32_t total_length;
@ -125,10 +135,10 @@ attr_container_get_attr_begin(const attr_container_t *attr_cont,
return p; return p;
} }
static char* static char *
attr_container_get_attr_next(const char *curr_attr) attr_container_get_attr_next(const char *curr_attr)
{ {
char *p = (char*) curr_attr; char *p = (char *)curr_attr;
uint8_t type; uint8_t type;
/* key length and key */ /* key length and key */
@ -154,7 +164,7 @@ attr_container_get_attr_next(const char *curr_attr)
return NULL; return NULL;
} }
static const char* static const char *
attr_container_find_attr(const attr_container_t *attr_cont, const char *key) attr_container_find_attr(const attr_container_t *attr_cont, const char *key)
{ {
uint32_t total_length; uint32_t total_length;
@ -164,7 +174,8 @@ attr_container_find_attr(const attr_container_t *attr_cont, const char *key)
if (!key) if (!key)
return NULL; return NULL;
if (!(p = attr_container_get_attr_begin(attr_cont, &total_length, &attr_num))) if (!(p = attr_container_get_attr_begin(attr_cont, &total_length,
&attr_num)))
return NULL; return NULL;
for (i = 0; i < attr_num; i++) { for (i = 0; i < attr_num; i++) {
@ -173,9 +184,9 @@ attr_container_find_attr(const attr_container_t *attr_cont, const char *key)
return NULL; return NULL;
if (str_len == strlen(key) + 1 if (str_len == strlen(key) + 1
&& memcmp(p + sizeof(uint16_t), key, str_len) == 0) { && memcmp(p + sizeof(uint16_t), key, str_len) == 0) {
if ((uint32_t)(p + sizeof(uint16_t) + str_len if ((uint32_t)(p + sizeof(uint16_t) + str_len - attr_cont->buf)
- attr_cont->buf) >= total_length) >= total_length)
return NULL; return NULL;
return p; return p;
} }
@ -187,14 +198,15 @@ attr_container_find_attr(const attr_container_t *attr_cont, const char *key)
return NULL; return NULL;
} }
char* char *
attr_container_get_attr_end(const attr_container_t *attr_cont) attr_container_get_attr_end(const attr_container_t *attr_cont)
{ {
uint32_t total_length; uint32_t total_length;
uint16_t attr_num, i; uint16_t attr_num, i;
char *p; char *p;
if (!(p = attr_container_get_attr_begin(attr_cont, &total_length, &attr_num))) if (!(p = attr_container_get_attr_begin(attr_cont, &total_length,
&attr_num)))
return NULL; return NULL;
for (i = 0; i < attr_num; i++) for (i = 0; i < attr_num; i++)
@ -204,14 +216,15 @@ attr_container_get_attr_end(const attr_container_t *attr_cont)
return p; return p;
} }
static char* static char *
attr_container_get_msg_end(attr_container_t *attr_cont) attr_container_get_msg_end(attr_container_t *attr_cont)
{ {
char *p = attr_cont->buf; char *p = attr_cont->buf;
return p + get_uint32(p); return p + get_uint32(p);
} }
uint16_t attr_container_get_attr_num(const attr_container_t *attr_cont) uint16_t
attr_container_get_attr_num(const attr_container_t *attr_cont)
{ {
uint16_t str_len; uint16_t str_len;
/* skip total length */ /* skip total length */
@ -225,7 +238,8 @@ uint16_t attr_container_get_attr_num(const attr_container_t *attr_cont)
return get_uint16(p); return get_uint16(p);
} }
static void attr_container_inc_attr_num(attr_container_t *attr_cont) static void
attr_container_inc_attr_num(attr_container_t *attr_cont)
{ {
uint16_t str_len, attr_num; uint16_t str_len, attr_num;
/* skip total length */ /* skip total length */
@ -249,12 +263,12 @@ attr_container_create(const char *tag)
tag_length = tag ? strlen(tag) + 1 : 1; tag_length = tag ? strlen(tag) + 1 : 1;
length = offsetof(attr_container_t, buf) + length = offsetof(attr_container_t, buf) +
/* total length + tag length + tag + reserved 100 bytes */ /* total length + tag length + tag + reserved 100 bytes */
sizeof(uint32_t) + sizeof(uint16_t) + tag_length + 100; sizeof(uint32_t) + sizeof(uint16_t) + tag_length + 100;
if (!(attr_cont = attr_container_malloc(length))) { if (!(attr_cont = attr_container_malloc(length))) {
attr_container_printf( attr_container_printf(
"Create attr_container failed: allocate memory failed.\r\n"); "Create attr_container failed: allocate memory failed.\r\n");
return NULL; return NULL;
} }
@ -274,34 +288,37 @@ attr_container_create(const char *tag)
return attr_cont; return attr_cont;
} }
void attr_container_destroy(const attr_container_t *attr_cont) void
attr_container_destroy(const attr_container_t *attr_cont)
{ {
if (attr_cont) if (attr_cont)
attr_container_free((char*) attr_cont); attr_container_free((char *)attr_cont);
} }
static bool check_set_attr(attr_container_t **p_attr_cont, const char *key) static bool
check_set_attr(attr_container_t **p_attr_cont, const char *key)
{ {
uint32_t flags; uint32_t flags;
if (!p_attr_cont || !*p_attr_cont || !key || strlen(key) == 0) { if (!p_attr_cont || !*p_attr_cont || !key || strlen(key) == 0) {
attr_container_printf( attr_container_printf(
"Set attribute failed: invalid input arguments.\r\n"); "Set attribute failed: invalid input arguments.\r\n");
return false; return false;
} }
flags = get_uint32((char*) *p_attr_cont); flags = get_uint32((char *)*p_attr_cont);
if (flags & ATTR_CONT_READONLY_SHIFT) { if (flags & ATTR_CONT_READONLY_SHIFT) {
attr_container_printf( attr_container_printf(
"Set attribute failed: attribute container is readonly.\r\n"); "Set attribute failed: attribute container is readonly.\r\n");
return false; return false;
} }
return true; return true;
} }
bool attr_container_set_attr(attr_container_t **p_attr_cont, const char *key, bool
int type, const void *value, int value_length) 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; attr_container_t *attr_cont, *attr_cont1;
uint16_t str_len; uint16_t str_len;
@ -351,13 +368,14 @@ bool attr_container_set_attr(attr_container_t **p_attr_cont, const char *key,
set_uint16(p, value_length); set_uint16(p, value_length);
p += sizeof(uint16_t); p += sizeof(uint16_t);
bh_memcpy_s(p, value_length, value, value_length); bh_memcpy_s(p, value_length, value, value_length);
} else if (type == ATTR_TYPE_BYTEARRAY) { }
else if (type == ATTR_TYPE_BYTEARRAY) {
set_uint32(p, value_length); set_uint32(p, value_length);
p += sizeof(uint32_t); p += sizeof(uint32_t);
bh_memcpy_s(p, value_length, value, value_length); bh_memcpy_s(p, value_length, value, value_length);
} }
if ((p = (char*) attr_container_find_attr(attr_cont, key))) { if ((p = (char *)attr_container_find_attr(attr_cont, key))) {
/* key found */ /* key found */
p1 = attr_container_get_attr_next(p); p1 = attr_container_get_attr_next(p);
@ -375,22 +393,21 @@ bool attr_container_set_attr(attr_container_t **p_attr_cont, const char *key,
} }
total_length += attr_len + 100; total_length += attr_len + 100;
if (!(attr_cont1 = attr_container_malloc( if (!(attr_cont1 = attr_container_malloc(offsetof(attr_container_t, buf)
offsetof(attr_container_t, buf) + total_length))) { + total_length))) {
attr_container_printf( attr_container_printf(
"Set attr failed: allocate memory failed.\r\n"); "Set attr failed: allocate memory failed.\r\n");
attr_container_free(attr_buf); attr_container_free(attr_buf);
return false; return false;
} }
bh_memcpy_s(attr_cont1, p - (char* )attr_cont, attr_cont, bh_memcpy_s(attr_cont1, p - (char *)attr_cont, attr_cont,
p - (char* )attr_cont); p - (char *)attr_cont);
bh_memcpy_s((char* )attr_cont1 + (unsigned )(p - (char* )attr_cont), bh_memcpy_s((char *)attr_cont1 + (unsigned)(p - (char *)attr_cont),
attr_end - p1, p1, attr_end - p1); attr_end - p1, p1, attr_end - p1);
bh_memcpy_s( bh_memcpy_s((char *)attr_cont1 + (unsigned)(p - (char *)attr_cont)
(char* )attr_cont1 + (unsigned )(p - (char* )attr_cont) + (unsigned)(attr_end - p1),
+ (unsigned )(attr_end - p1), attr_len, attr_buf, attr_len, attr_buf, attr_len);
attr_len);
p = attr_cont1->buf; p = attr_cont1->buf;
set_uint32(p, total_length); set_uint32(p, total_length);
*p_attr_cont = attr_cont1; *p_attr_cont = attr_cont1;
@ -398,7 +415,8 @@ bool attr_container_set_attr(attr_container_t **p_attr_cont, const char *key,
attr_container_free(attr_cont); attr_container_free(attr_cont);
attr_container_free(attr_buf); attr_container_free(attr_buf);
return true; return true;
} else { }
else {
/* key not found */ /* key not found */
if ((uint32_t)(msg_end - attr_end) >= attr_len) { if ((uint32_t)(msg_end - attr_end) >= attr_len) {
bh_memcpy_s(attr_end, msg_end - attr_end, attr_buf, attr_len); bh_memcpy_s(attr_end, msg_end - attr_end, attr_buf, attr_len);
@ -408,19 +426,19 @@ bool attr_container_set_attr(attr_container_t **p_attr_cont, const char *key,
} }
total_length += attr_len + 100; total_length += attr_len + 100;
if (!(attr_cont1 = attr_container_malloc( if (!(attr_cont1 = attr_container_malloc(offsetof(attr_container_t, buf)
offsetof(attr_container_t, buf) + total_length))) { + total_length))) {
attr_container_printf( attr_container_printf(
"Set attr failed: allocate memory failed.\r\n"); "Set attr failed: allocate memory failed.\r\n");
attr_container_free(attr_buf); attr_container_free(attr_buf);
return false; return false;
} }
bh_memcpy_s(attr_cont1, attr_end - (char* )attr_cont, attr_cont, bh_memcpy_s(attr_cont1, attr_end - (char *)attr_cont, attr_cont,
attr_end - (char* )attr_cont); attr_end - (char *)attr_cont);
bh_memcpy_s( bh_memcpy_s((char *)attr_cont1
(char* )attr_cont1 + (unsigned )(attr_end - (char* )attr_cont), + (unsigned)(attr_end - (char *)attr_cont),
attr_len, attr_buf, attr_len); attr_len, attr_buf, attr_len);
attr_container_inc_attr_num(attr_cont1); attr_container_inc_attr_num(attr_cont1);
p = attr_cont1->buf; p = attr_cont1->buf;
set_uint32(p, total_length); set_uint32(p, total_length);
@ -434,88 +452,101 @@ bool attr_container_set_attr(attr_container_t **p_attr_cont, const char *key,
return false; return false;
} }
bool attr_container_set_short(attr_container_t **p_attr_cont, const char *key, bool
short value) 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); 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, bool
int value) 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); 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, bool
int64_t value) 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); 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, bool
int8_t value) 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); 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, bool
uint16_t value) 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, return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_UINT16, &value,
2); 2);
} }
bool attr_container_set_float(attr_container_t **p_attr_cont, const char *key, bool
float value) 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); 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, bool
double value) 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, return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_DOUBLE, &value,
8); 8);
} }
bool attr_container_set_bool(attr_container_t **p_attr_cont, const char *key, bool
bool value) attr_container_set_bool(attr_container_t **p_attr_cont, const char *key,
bool value)
{ {
int8_t value1 = value ? 1 : 0; int8_t value1 = value ? 1 : 0;
return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_BOOLEAN, &value1, return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_BOOLEAN, &value1,
1); 1);
} }
bool attr_container_set_string(attr_container_t **p_attr_cont, const char *key, bool
const char *value) attr_container_set_string(attr_container_t **p_attr_cont, const char *key,
const char *value)
{ {
if (!value) { if (!value) {
attr_container_printf("Set attr failed: invald input arguments.\r\n"); attr_container_printf("Set attr failed: invald input arguments.\r\n");
return false; return false;
} }
return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_STRING, value, return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_STRING, value,
strlen(value) + 1);; strlen(value) + 1);
} }
bool attr_container_set_bytearray(attr_container_t **p_attr_cont, bool
const char *key, const int8_t *value, unsigned length) attr_container_set_bytearray(attr_container_t **p_attr_cont, const char *key,
const int8_t *value, unsigned length)
{ {
if (!value) { if (!value) {
attr_container_printf("Set attr failed: invald input arguments.\r\n"); attr_container_printf("Set attr failed: invald input arguments.\r\n");
return false; return false;
} }
return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_BYTEARRAY, value, return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_BYTEARRAY, value,
length);; length);
} }
static const char* static const char *
attr_container_get_attr(const attr_container_t *attr_cont, const char *key) attr_container_get_attr(const attr_container_t *attr_cont, const char *key)
{ {
const char *attr_addr; const char *attr_addr;
if (!attr_cont || !key) { if (!attr_cont || !key) {
attr_container_printf( attr_container_printf(
"Get attribute failed: invalid input arguments.\r\n"); "Get attribute failed: invalid input arguments.\r\n");
return NULL; return NULL;
} }
@ -528,101 +559,103 @@ attr_container_get_attr(const attr_container_t *attr_cont, const char *key)
return attr_addr + 2 + strlen(key) + 1; return attr_addr + 2 + strlen(key) + 1;
} }
#define TEMPLATE_ATTR_BUF_TO_VALUE(attr, key, var_name) do {\ #define TEMPLATE_ATTR_BUF_TO_VALUE(attr, key, var_name) \
jvalue val; \ do { \
const char *addr = attr_container_get_attr(attr, key); \ jvalue val; \
uint8_t type; \ const char *addr = attr_container_get_attr(attr, key); \
if (!addr) \ uint8_t type; \
return 0; \ if (!addr) \
val.j = 0; \ return 0; \
type = *(uint8_t*)addr++; \ val.j = 0; \
switch (type) { \ type = *(uint8_t *)addr++; \
case ATTR_TYPE_SHORT: \ switch (type) { \
case ATTR_TYPE_INT: \ case ATTR_TYPE_SHORT: \
case ATTR_TYPE_INT64: \ case ATTR_TYPE_INT: \
case ATTR_TYPE_BYTE: \ case ATTR_TYPE_INT64: \
case ATTR_TYPE_UINT16: \ case ATTR_TYPE_BYTE: \
case ATTR_TYPE_FLOAT: \ case ATTR_TYPE_UINT16: \
case ATTR_TYPE_DOUBLE: \ case ATTR_TYPE_FLOAT: \
case ATTR_TYPE_BOOLEAN: \ case ATTR_TYPE_DOUBLE: \
bh_memcpy_s(&val, sizeof(val.var_name), addr, 1 << (type & 3)); \ case ATTR_TYPE_BOOLEAN: \
break; \ bh_memcpy_s(&val, sizeof(val.var_name), addr, \
case ATTR_TYPE_STRING: \ 1 << (type & 3)); \
{ \ break; \
unsigned len= get_uint16(addr); \ case ATTR_TYPE_STRING: \
addr += 2; \ { \
if (len > sizeof(val.var_name)) \ unsigned len = get_uint16(addr); \
len = sizeof(val.var_name); \ addr += 2; \
bh_memcpy_s(&val.var_name, sizeof(val.var_name), addr, len); \ if (len > sizeof(val.var_name)) \
break; \ len = sizeof(val.var_name); \
} \ bh_memcpy_s(&val.var_name, sizeof(val.var_name), addr, len); \
case ATTR_TYPE_BYTEARRAY: \ break; \
{ \ } \
unsigned len= get_uint32(addr); \ case ATTR_TYPE_BYTEARRAY: \
addr += 4; \ { \
if (len > sizeof(val.var_name)) \ unsigned len = get_uint32(addr); \
len = sizeof(val.var_name); \ addr += 4; \
bh_memcpy_s(&val.var_name, sizeof(val.var_name), addr, len); \ if (len > sizeof(val.var_name)) \
break; \ len = sizeof(val.var_name); \
} \ bh_memcpy_s(&val.var_name, sizeof(val.var_name), addr, len); \
default: \ break; \
bh_assert(0); \ } \
break; \ default: \
} \ bh_assert(0); \
return val.var_name; \ break; \
} while (0) } \
return val.var_name; \
} while (0)
short attr_container_get_as_short(const attr_container_t *attr_cont, short
const char *key) attr_container_get_as_short(const attr_container_t *attr_cont, const char *key)
{ {
TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, s); TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, s);
} }
int attr_container_get_as_int(const attr_container_t *attr_cont, int
const char *key) attr_container_get_as_int(const attr_container_t *attr_cont, const char *key)
{ {
TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, i); TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, i);
} }
int64_t attr_container_get_as_int64(const attr_container_t *attr_cont, int64_t
const char *key) attr_container_get_as_int64(const attr_container_t *attr_cont, const char *key)
{ {
TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, j); TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, j);
} }
int8_t attr_container_get_as_byte(const attr_container_t *attr_cont, int8_t
const char *key) attr_container_get_as_byte(const attr_container_t *attr_cont, const char *key)
{ {
TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, b); TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, b);
} }
uint16_t attr_container_get_as_uint16(const attr_container_t *attr_cont, uint16_t
const char *key) attr_container_get_as_uint16(const attr_container_t *attr_cont, const char *key)
{ {
TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, s); TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, s);
} }
float attr_container_get_as_float(const attr_container_t *attr_cont, float
const char *key) attr_container_get_as_float(const attr_container_t *attr_cont, const char *key)
{ {
TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, f); TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, f);
} }
double attr_container_get_as_double(const attr_container_t *attr_cont, double
const char *key) attr_container_get_as_double(const attr_container_t *attr_cont, const char *key)
{ {
TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, d); TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, d);
} }
bool attr_container_get_as_bool(const attr_container_t *attr_cont, bool
const char *key) attr_container_get_as_bool(const attr_container_t *attr_cont, const char *key)
{ {
TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, z); TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, z);
} }
const int8_t* const int8_t *
attr_container_get_as_bytearray(const attr_container_t *attr_cont, attr_container_get_as_bytearray(const attr_container_t *attr_cont,
const char *key, unsigned *array_length) const char *key, unsigned *array_length)
{ {
const char *addr = attr_container_get_attr(attr_cont, key); const char *addr = attr_container_get_attr(attr_cont, key);
uint8_t type; uint8_t type;
@ -636,68 +669,68 @@ attr_container_get_as_bytearray(const attr_container_t *attr_cont,
return NULL; return NULL;
} }
type = *(uint8_t*) addr++; type = *(uint8_t *)addr++;
switch (type) { switch (type) {
case ATTR_TYPE_SHORT: case ATTR_TYPE_SHORT:
case ATTR_TYPE_INT: case ATTR_TYPE_INT:
case ATTR_TYPE_INT64: case ATTR_TYPE_INT64:
case ATTR_TYPE_BYTE: case ATTR_TYPE_BYTE:
case ATTR_TYPE_UINT16: case ATTR_TYPE_UINT16:
case ATTR_TYPE_FLOAT: case ATTR_TYPE_FLOAT:
case ATTR_TYPE_DOUBLE: case ATTR_TYPE_DOUBLE:
case ATTR_TYPE_BOOLEAN: case ATTR_TYPE_BOOLEAN:
length = 1 << (type & 3); length = 1 << (type & 3);
break; break;
case ATTR_TYPE_STRING: case ATTR_TYPE_STRING:
length = get_uint16(addr); length = get_uint16(addr);
addr += 2; addr += 2;
break; break;
case ATTR_TYPE_BYTEARRAY: case ATTR_TYPE_BYTEARRAY:
length = get_uint32(addr); length = get_uint32(addr);
addr += 4; addr += 4;
break; break;
default: default:
return NULL; return NULL;
} }
*array_length = length; *array_length = length;
return (const int8_t*) addr; return (const int8_t *)addr;
} }
char* char *
attr_container_get_as_string(const attr_container_t *attr_cont, const char *key) attr_container_get_as_string(const attr_container_t *attr_cont, const char *key)
{ {
unsigned array_length; unsigned array_length;
return (char*) attr_container_get_as_bytearray(attr_cont, key, return (char *)attr_container_get_as_bytearray(attr_cont, key,
&array_length); &array_length);
} }
const char* const char *
attr_container_get_tag(const attr_container_t *attr_cont) attr_container_get_tag(const attr_container_t *attr_cont)
{ {
return attr_cont ? return attr_cont ? attr_cont->buf + sizeof(uint32_t) + sizeof(uint16_t)
attr_cont->buf + sizeof(uint32_t) + sizeof(uint16_t) : NULL; : NULL;
} }
bool attr_container_contain_key(const attr_container_t *attr_cont, bool
const char *key) attr_container_contain_key(const attr_container_t *attr_cont, const char *key)
{ {
if (!attr_cont || !key || !strlen(key)) { if (!attr_cont || !key || !strlen(key)) {
attr_container_printf( attr_container_printf(
"Check contain key failed: invalid input arguments.\r\n"); "Check contain key failed: invalid input arguments.\r\n");
return false; return false;
} }
return attr_container_find_attr(attr_cont, key) ? true : false; return attr_container_find_attr(attr_cont, key) ? true : false;
} }
unsigned int attr_container_get_serialize_length( unsigned int
const attr_container_t *attr_cont) attr_container_get_serialize_length(const attr_container_t *attr_cont)
{ {
const char *p; const char *p;
if (!attr_cont) { if (!attr_cont) {
attr_container_printf( attr_container_printf("Get container serialize length failed: invalid "
"Get container serialize length failed: invalid input arguments.\r\n"); "input arguments.\r\n");
return 0; return 0;
} }
@ -705,7 +738,8 @@ unsigned int attr_container_get_serialize_length(
return sizeof(uint16_t) + get_uint32(p); return sizeof(uint16_t) + get_uint32(p);
} }
bool attr_container_serialize(char *buf, const attr_container_t *attr_cont) bool
attr_container_serialize(char *buf, const attr_container_t *attr_cont)
{ {
const char *p; const char *p;
uint16_t flags; uint16_t flags;
@ -713,7 +747,7 @@ bool attr_container_serialize(char *buf, const attr_container_t *attr_cont)
if (!buf || !attr_cont) { if (!buf || !attr_cont) {
attr_container_printf( attr_container_printf(
"Container serialize failed: invalid input arguments.\r\n"); "Container serialize failed: invalid input arguments.\r\n");
return false; return false;
} }
@ -721,27 +755,29 @@ bool attr_container_serialize(char *buf, const attr_container_t *attr_cont)
length = sizeof(uint16_t) + get_uint32(p); length = sizeof(uint16_t) + get_uint32(p);
bh_memcpy_s(buf, length, attr_cont, length); bh_memcpy_s(buf, length, attr_cont, length);
/* Set readonly */ /* Set readonly */
flags = get_uint16((const char*) attr_cont); flags = get_uint16((const char *)attr_cont);
set_uint16(buf, flags | (1 << ATTR_CONT_READONLY_SHIFT)); set_uint16(buf, flags | (1 << ATTR_CONT_READONLY_SHIFT));
return true; return true;
} }
bool attr_container_is_constant(const attr_container_t* attr_cont) bool
attr_container_is_constant(const attr_container_t *attr_cont)
{ {
uint16_t flags; uint16_t flags;
if (!attr_cont) { if (!attr_cont) {
attr_container_printf( attr_container_printf(
"Container check const: invalid input arguments.\r\n"); "Container check const: invalid input arguments.\r\n");
return false; return false;
} }
flags = get_uint16((const char*) attr_cont); flags = get_uint16((const char *)attr_cont);
return (flags & (1 << ATTR_CONT_READONLY_SHIFT)) ? true : false; return (flags & (1 << ATTR_CONT_READONLY_SHIFT)) ? true : false;
} }
void attr_container_dump(const attr_container_t *attr_cont) void
attr_container_dump(const attr_container_t *attr_cont)
{ {
uint32_t total_length; uint32_t total_length;
uint16_t attr_num, i, type; uint16_t attr_num, i, type;
@ -771,64 +807,64 @@ void attr_container_dump(const attr_container_t *attr_cont)
attr_container_printf(" key: %s", key); attr_container_printf(" key: %s", key);
switch (type) { switch (type) {
case ATTR_TYPE_SHORT: case ATTR_TYPE_SHORT:
bh_memcpy_s(&value.s, sizeof(int16_t), p, sizeof(int16_t)); bh_memcpy_s(&value.s, sizeof(int16_t), p, sizeof(int16_t));
attr_container_printf(", type: short, value: 0x%x\n", attr_container_printf(", type: short, value: 0x%x\n",
value.s & 0xFFFF); value.s & 0xFFFF);
p += 2; p += 2;
break; break;
case ATTR_TYPE_INT: case ATTR_TYPE_INT:
bh_memcpy_s(&value.i, sizeof(int32_t), p, sizeof(int32_t)); bh_memcpy_s(&value.i, sizeof(int32_t), p, sizeof(int32_t));
attr_container_printf(", type: int, value: 0x%x\n", value.i); attr_container_printf(", type: int, value: 0x%x\n", value.i);
p += 4; p += 4;
break; break;
case ATTR_TYPE_INT64: case ATTR_TYPE_INT64:
bh_memcpy_s(&value.j, sizeof(uint64_t), p, sizeof(uint64_t)); 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)); attr_container_printf(", type: int64, value: 0x%llx\n",
p += 8; (long long unsigned int)(value.j));
break; p += 8;
case ATTR_TYPE_BYTE: break;
bh_memcpy_s(&value.b, 1, p, 1); case ATTR_TYPE_BYTE:
attr_container_printf(", type: byte, value: 0x%x\n", bh_memcpy_s(&value.b, 1, p, 1);
value.b & 0xFF); attr_container_printf(", type: byte, value: 0x%x\n",
p++; value.b & 0xFF);
break; p++;
case ATTR_TYPE_UINT16: break;
bh_memcpy_s(&value.c, sizeof(uint16_t), p, sizeof(uint16_t)); case ATTR_TYPE_UINT16:
attr_container_printf(", type: uint16, value: 0x%x\n", value.c); bh_memcpy_s(&value.c, sizeof(uint16_t), p, sizeof(uint16_t));
p += 2; attr_container_printf(", type: uint16, value: 0x%x\n", value.c);
break; p += 2;
case ATTR_TYPE_FLOAT: break;
bh_memcpy_s(&value.f, sizeof(float), p, sizeof(float)); case ATTR_TYPE_FLOAT:
attr_container_printf(", type: float, value: %f\n", value.f); bh_memcpy_s(&value.f, sizeof(float), p, sizeof(float));
p += 4; attr_container_printf(", type: float, value: %f\n", value.f);
break; p += 4;
case ATTR_TYPE_DOUBLE: break;
bh_memcpy_s(&value.d, sizeof(double), p, sizeof(double)); case ATTR_TYPE_DOUBLE:
attr_container_printf(", type: double, value: %f\n", value.d); bh_memcpy_s(&value.d, sizeof(double), p, sizeof(double));
p += 8; attr_container_printf(", type: double, value: %f\n", value.d);
break; p += 8;
case ATTR_TYPE_BOOLEAN: break;
bh_memcpy_s(&value.z, 1, p, 1); case ATTR_TYPE_BOOLEAN:
attr_container_printf(", type: bool, value: 0x%x\n", value.z); bh_memcpy_s(&value.z, 1, p, 1);
p++; attr_container_printf(", type: bool, value: 0x%x\n", value.z);
break; p++;
case ATTR_TYPE_STRING: break;
attr_container_printf(", type: string, value: %s\n", case ATTR_TYPE_STRING:
p + sizeof(uint16_t)); attr_container_printf(", type: string, value: %s\n",
p += sizeof(uint16_t) + get_uint16(p); p + sizeof(uint16_t));
break; p += sizeof(uint16_t) + get_uint16(p);
case ATTR_TYPE_BYTEARRAY: break;
attr_container_printf(", type: byte array, length: %d\n", case ATTR_TYPE_BYTEARRAY:
get_uint32(p)); attr_container_printf(", type: byte array, length: %d\n",
p += sizeof(uint32_t) + get_uint32(p); get_uint32(p));
break; p += sizeof(uint32_t) + get_uint32(p);
default: break;
bh_assert(0); default:
break; bh_assert(0);
break;
} }
} }
attr_container_printf("\n"); attr_container_printf("\n");
} }

View File

@ -34,7 +34,7 @@ enum {
ATTR_TYPE_END = ATTR_TYPE_BYTEARRAY ATTR_TYPE_END = ATTR_TYPE_BYTEARRAY
}; };
#define ATTR_CONT_READONLY_SHIFT 2 #define ATTR_CONT_READONLY_SHIFT 2
typedef struct attr_container { typedef struct attr_container {
/* container flag: /* container flag:
@ -87,7 +87,7 @@ attr_container_destroy(const attr_container_t *attr_cont);
*/ */
bool bool
attr_container_set_short(attr_container_t **p_attr_cont, const char *key, attr_container_set_short(attr_container_t **p_attr_cont, const char *key,
short value); short value);
/** /**
* Set int attribute in attribute container * Set int attribute in attribute container
@ -101,7 +101,7 @@ attr_container_set_short(attr_container_t **p_attr_cont, const char *key,
*/ */
bool bool
attr_container_set_int(attr_container_t **p_attr_cont, const char *key, attr_container_set_int(attr_container_t **p_attr_cont, const char *key,
int value); int value);
/** /**
* Set int64 attribute in attribute container * Set int64 attribute in attribute container
@ -115,7 +115,7 @@ attr_container_set_int(attr_container_t **p_attr_cont, const char *key,
*/ */
bool bool
attr_container_set_int64(attr_container_t **p_attr_cont, const char *key, attr_container_set_int64(attr_container_t **p_attr_cont, const char *key,
int64_t value); int64_t value);
/** /**
* Set byte attribute in attribute container * Set byte attribute in attribute container
@ -129,7 +129,7 @@ attr_container_set_int64(attr_container_t **p_attr_cont, const char *key,
*/ */
bool bool
attr_container_set_byte(attr_container_t **p_attr_cont, const char *key, attr_container_set_byte(attr_container_t **p_attr_cont, const char *key,
int8_t value); int8_t value);
/** /**
* Set uint16 attribute in attribute container * Set uint16 attribute in attribute container
@ -143,7 +143,7 @@ attr_container_set_byte(attr_container_t **p_attr_cont, const char *key,
*/ */
bool bool
attr_container_set_uint16(attr_container_t **p_attr_cont, const char *key, attr_container_set_uint16(attr_container_t **p_attr_cont, const char *key,
uint16_t value); uint16_t value);
/** /**
* Set float attribute in attribute container * Set float attribute in attribute container
@ -157,7 +157,7 @@ attr_container_set_uint16(attr_container_t **p_attr_cont, const char *key,
*/ */
bool bool
attr_container_set_float(attr_container_t **p_attr_cont, const char *key, attr_container_set_float(attr_container_t **p_attr_cont, const char *key,
float value); float value);
/** /**
* Set double attribute in attribute container * Set double attribute in attribute container
@ -171,7 +171,7 @@ attr_container_set_float(attr_container_t **p_attr_cont, const char *key,
*/ */
bool bool
attr_container_set_double(attr_container_t **p_attr_cont, const char *key, attr_container_set_double(attr_container_t **p_attr_cont, const char *key,
double value); double value);
/** /**
* Set bool attribute in attribute container * Set bool attribute in attribute container
@ -185,7 +185,7 @@ attr_container_set_double(attr_container_t **p_attr_cont, const char *key,
*/ */
bool bool
attr_container_set_bool(attr_container_t **p_attr_cont, const char *key, attr_container_set_bool(attr_container_t **p_attr_cont, const char *key,
bool value); bool value);
/** /**
* Set string attribute in attribute container * Set string attribute in attribute container
@ -199,7 +199,7 @@ attr_container_set_bool(attr_container_t **p_attr_cont, const char *key,
*/ */
bool bool
attr_container_set_string(attr_container_t **p_attr_cont, const char *key, attr_container_set_string(attr_container_t **p_attr_cont, const char *key,
const char *value); const char *value);
/** /**
* Set bytearray attribute in attribute container * Set bytearray attribute in attribute container
@ -214,7 +214,7 @@ attr_container_set_string(attr_container_t **p_attr_cont, const char *key,
*/ */
bool bool
attr_container_set_bytearray(attr_container_t **p_attr_cont, const char *key, attr_container_set_bytearray(attr_container_t **p_attr_cont, const char *key,
const int8_t *value, unsigned length); const int8_t *value, unsigned length);
/** /**
* Get tag of current attribute container * Get tag of current attribute container
@ -223,7 +223,7 @@ attr_container_set_bytearray(attr_container_t **p_attr_cont, const char *key,
* *
* @return tag of current attribute container * @return tag of current attribute container
*/ */
const char* const char *
attr_container_get_tag(const attr_container_t *attr_cont); attr_container_get_tag(const attr_container_t *attr_cont);
/** /**
@ -306,7 +306,7 @@ attr_container_get_as_byte(const attr_container_t *attr_cont, const char *key);
*/ */
uint16_t uint16_t
attr_container_get_as_uint16(const attr_container_t *attr_cont, attr_container_get_as_uint16(const attr_container_t *attr_cont,
const char *key); const char *key);
/** /**
* Get attribute from attribute container and return it as float value, * Get attribute from attribute container and return it as float value,
@ -331,7 +331,7 @@ attr_container_get_as_float(const attr_container_t *attr_cont, const char *key);
*/ */
double double
attr_container_get_as_double(const attr_container_t *attr_cont, attr_container_get_as_double(const attr_container_t *attr_cont,
const char *key); const char *key);
/** /**
* Get attribute from attribute container and return it as bool value, * Get attribute from attribute container and return it as bool value,
@ -354,9 +354,9 @@ attr_container_get_as_bool(const attr_container_t *attr_cont, const char *key);
* *
* @return the string value of the attribute, NULL if key isn't found * @return the string value of the attribute, NULL if key isn't found
*/ */
char* char *
attr_container_get_as_string(const attr_container_t *attr_cont, attr_container_get_as_string(const attr_container_t *attr_cont,
const char *key); const char *key);
/** /**
* Get attribute from attribute container and return it as bytearray value, * Get attribute from attribute container and return it as bytearray value,
@ -367,9 +367,9 @@ attr_container_get_as_string(const attr_container_t *attr_cont,
* *
* @return the bytearray value of the attribute, NULL if key isn't found * @return the bytearray value of the attribute, NULL if key isn't found
*/ */
const int8_t* const int8_t *
attr_container_get_as_bytearray(const attr_container_t *attr_cont, attr_container_get_as_bytearray(const attr_container_t *attr_cont,
const char *key, unsigned *array_length); const char *key, unsigned *array_length);
/** /**
* Get the buffer size of attribute container * Get the buffer size of attribute container
@ -400,7 +400,7 @@ attr_container_serialize(char *buf, const attr_container_t *attr_cont);
* @return true if const, false otherwise * @return true if const, false otherwise
*/ */
bool bool
attr_container_is_constant(const attr_container_t* attr_cont); attr_container_is_constant(const attr_container_t *attr_cont);
void void
attr_container_dump(const attr_container_t *attr_cont); attr_container_dump(const attr_container_t *attr_cont);
@ -422,4 +422,3 @@ attr_container_dump(const attr_container_t *attr_cont);
#endif #endif
#endif /* end of _ATTR_CONTAINER_H_ */ #endif /* end of _ATTR_CONTAINER_H_ */

View File

@ -12,8 +12,8 @@
extern "C" { extern "C" {
#endif #endif
#define FMT_ATTR_CONTAINER 99 #define FMT_ATTR_CONTAINER 99
#define FMT_APP_RAW_BINARY 98 #define FMT_APP_RAW_BINARY 98
/* the request structure */ /* the request structure */
typedef struct request { typedef struct request {
@ -32,10 +32,10 @@ typedef struct request {
// payload of the request, currently only support attr_container_t type // payload of the request, currently only support attr_container_t type
void *payload; void *payload;
//length in bytes of the payload // length in bytes of the payload
int payload_len; int payload_len;
//sender of the request // sender of the request
unsigned long sender; unsigned long sender;
} request_t; } request_t;
@ -53,21 +53,21 @@ typedef struct response {
// payload of the response, // payload of the response,
void *payload; void *payload;
//length in bytes of the payload // length in bytes of the payload
int payload_len; int payload_len;
//receiver of the response // receiver of the response
unsigned long reciever; unsigned long reciever;
} response_t; } response_t;
int int
check_url_start(const char* url, int url_len, const char * leading_str); check_url_start(const char *url, int url_len, const char *leading_str);
bool bool
match_url(char * pattern, char * matched); match_url(char *pattern, char *matched);
char * char *
find_key_value(char * buffer, int buffer_len, char * key, char * value, find_key_value(char *buffer, int buffer_len, char *key, char *value,
int value_len, char delimiter); int value_len, char delimiter);
request_t * request_t *
@ -77,10 +77,10 @@ void
request_cleaner(request_t *request); request_cleaner(request_t *request);
response_t * response_t *
clone_response(response_t * response); clone_response(response_t *response);
void void
response_cleaner(response_t * response); response_cleaner(response_t *response);
/** /**
* @brief Set fields of response. * @brief Set fields of response.
@ -96,8 +96,8 @@ response_cleaner(response_t * response);
* @warning the response pointer MUST NOT be NULL * @warning the response pointer MUST NOT be NULL
*/ */
response_t * response_t *
set_response(response_t * response, int status, int fmt, set_response(response_t *response, int status, int fmt, const char *payload,
const char *payload, int payload_len); int payload_len);
/** /**
* @brief Make a response for a request. * @brief Make a response for a request.
@ -110,7 +110,7 @@ set_response(response_t * response, int status, int fmt,
* @warning the request and response pointers MUST NOT be NULL * @warning the request and response pointers MUST NOT be NULL
*/ */
response_t * response_t *
make_response_for_request(request_t * request, response_t * response); make_response_for_request(request_t *request, response_t *response);
/** /**
* @brief Initialize a request. * @brief Initialize a request.
@ -127,28 +127,27 @@ make_response_for_request(request_t * request, response_t * response);
* @warning the request pointer MUST NOT be NULL * @warning the request pointer MUST NOT be NULL
*/ */
request_t * request_t *
init_request(request_t * request, char *url, int action, int fmt, init_request(request_t *request, char *url, int action, int fmt, void *payload,
void *payload, int payload_len); int payload_len);
char * char *
pack_request(request_t *request, int * size); pack_request(request_t *request, int *size);
request_t * request_t *
unpack_request(char * packet, int size, request_t * request); unpack_request(char *packet, int size, request_t *request);
char * char *
pack_response(response_t *response, int * size); pack_response(response_t *response, int *size);
response_t * response_t *
unpack_response(char * packet, int size, response_t * response); unpack_response(char *packet, int size, response_t *response);
void void
free_req_resp_packet(char * packet); free_req_resp_packet(char *packet);
char * char *
wa_strdup(const char *str); wa_strdup(const char *str);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -8,14 +8,12 @@
#include "bh_platform.h" #include "bh_platform.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
#include <stdbool.h> #include <stdbool.h>
/* Object native function IDs */ /* Object native function IDs */
enum { enum {
OBJ_FUNC_ID_DEL, OBJ_FUNC_ID_DEL,

View File

@ -10,6 +10,4 @@
implemented by both [app] and [native] worlds */ implemented by both [app] and [native] worlds */
#include "bh_platform.h" #include "bh_platform.h"
#endif /* end of _NATIVE_INTERFACE_H */ #endif /* end of _NATIVE_INTERFACE_H */

View File

@ -20,22 +20,27 @@
* 4. attr-containers of our own * 4. attr-containers of our own
* 5. customized serialization for request/response * 5. customized serialization for request/response
* *
* Now we choose the #5 mainly because we need to quickly get the URL for dispatching * Now we choose the #5 mainly because we need to quickly get the URL for
* and sometimes we want to change the URL in the original packet. the request format: * dispatching and sometimes we want to change the URL in the original packet.
* 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) * the request format: fixed part: version: (1 byte), code (1 byte), fmt(2
* dynamic part: url (bytes in url_len), payload * 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: * response format:
* fixed part: (1 byte), code (1 byte), fmt(2 byte), mid (4 bytes), sender_id(4 bytes), payload_len(4bytes) * fixed part: (1 byte), code (1 byte), fmt(2 byte), mid (4 bytes), sender_id(4
* dynamic part: payload * bytes), payload_len(4bytes) dynamic part: payload
*/ */
#define REQUES_PACKET_VER 1 #define REQUES_PACKET_VER 1
#define REQUEST_PACKET_FIX_PART_LEN 18 #define REQUEST_PACKET_FIX_PART_LEN 18
#define REQUEST_PACKET_URL_OFFSET REQUEST_PACKET_FIX_PART_LEN #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_URL_LEN \
#define REQUEST_PACKET_PAYLOAD_LEN *((uint32*)((char*) buffer + 14)) /* to ensure little endian */ *((uint16 *)((char *)buffer + 12)) /* to ensure little endian */
#define REQUEST_PACKET_URL(buffer) ((char*) buffer + REQUEST_PACKET_URL_OFFSET) #define REQUEST_PACKET_PAYLOAD_LEN \
#define REQUEST_PACKET_PAYLOAD(buffer) ((char*) buffer + REQUEST_PACKET_URL_OFFSET + REQUEST_PACKET_URL_LEN(buffer)) *((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 #define RESPONSE_PACKET_FIX_PART_LEN 16
@ -48,17 +53,17 @@ pack_request(request_t *request, int *size)
uint32 u32; uint32 u32;
char *packet; char *packet;
if ((packet = (char*) WA_MALLOC(len)) == NULL) if ((packet = (char *)WA_MALLOC(len)) == NULL)
return NULL; return NULL;
/* TODO: ensure little endian for words and dwords */ /* TODO: ensure little endian for words and dwords */
*packet = REQUES_PACKET_VER; *packet = REQUES_PACKET_VER;
*((uint8*) (packet + 1)) = request->action; *((uint8 *)(packet + 1)) = request->action;
u16 = htons(request->fmt); u16 = htons(request->fmt);
memcpy(packet + 2, &u16, 2); memcpy(packet + 2, &u16, 2);
u32 = htonl(request->mid); u32 = htonl(request->mid);
memcpy(packet + 4, &u32, 4); memcpy(packet + 4, &u32, 4);
u32 = htonl(request->sender); u32 = htonl(request->sender);
@ -72,7 +77,7 @@ pack_request(request_t *request, int *size)
strcpy(packet + REQUEST_PACKET_URL_OFFSET, request->url); strcpy(packet + REQUEST_PACKET_URL_OFFSET, request->url);
memcpy(packet + REQUEST_PACKET_URL_OFFSET + url_len, request->payload, memcpy(packet + REQUEST_PACKET_URL_OFFSET + url_len, request->payload,
request->payload_len); request->payload_len);
*size = len; *size = len;
return packet; return packet;
@ -111,7 +116,7 @@ unpack_request(char *packet, int size, request_t *request)
return NULL; return NULL;
} }
request->action = *((uint8*) (packet + 1)); request->action = *((uint8 *)(packet + 1));
memcpy(&u16, packet + 2, 2); memcpy(&u16, packet + 2, 2);
request->fmt = ntohs(u16); request->fmt = ntohs(u16);
@ -141,12 +146,12 @@ pack_response(response_t *response, int *size)
uint32 u32; uint32 u32;
char *packet; char *packet;
if ((packet = (char*) WA_MALLOC(len)) == NULL) if ((packet = (char *)WA_MALLOC(len)) == NULL)
return NULL; return NULL;
/* TODO: ensure little endian for words and dwords */ /* TODO: ensure little endian for words and dwords */
*packet = REQUES_PACKET_VER; *packet = REQUES_PACKET_VER;
*((uint8*) (packet + 1)) = response->status; *((uint8 *)(packet + 1)) = response->status;
u16 = htons(response->fmt); u16 = htons(response->fmt);
memcpy(packet + 2, &u16, 2); memcpy(packet + 2, &u16, 2);
@ -161,7 +166,7 @@ pack_response(response_t *response, int *size)
memcpy(packet + 12, &u32, 4); memcpy(packet + 12, &u32, 4);
memcpy(packet + RESPONSE_PACKET_FIX_PART_LEN, response->payload, memcpy(packet + RESPONSE_PACKET_FIX_PART_LEN, response->payload,
response->payload_len); response->payload_len);
*size = len; *size = len;
return packet; return packet;
@ -184,7 +189,7 @@ unpack_response(char *packet, int size, response_t *response)
if (size != (RESPONSE_PACKET_FIX_PART_LEN + payload_len)) if (size != (RESPONSE_PACKET_FIX_PART_LEN + payload_len))
return NULL; return NULL;
response->status = *((uint8*) (packet + 1)); response->status = *((uint8 *)(packet + 1));
memcpy(&u16, packet + 2, 2); memcpy(&u16, packet + 2, 2);
response->fmt = ntohs(u16); response->fmt = ntohs(u16);
@ -208,7 +213,7 @@ request_t *
clone_request(request_t *request) clone_request(request_t *request)
{ {
/* deep clone */ /* deep clone */
request_t *req = (request_t *) WA_MALLOC(sizeof(request_t)); request_t *req = (request_t *)WA_MALLOC(sizeof(request_t));
if (req == NULL) if (req == NULL)
return NULL; return NULL;
@ -278,7 +283,7 @@ clone_response(response_t *response)
clone->reciever = response->reciever; clone->reciever = response->reciever;
clone->payload_len = response->payload_len; clone->payload_len = response->payload_len;
if (clone->payload_len) { if (clone->payload_len) {
clone->payload = (char *) WA_MALLOC(response->payload_len); clone->payload = (char *)WA_MALLOC(response->payload_len);
if (!clone->payload) if (!clone->payload)
goto fail; goto fail;
memcpy(clone->payload, response->payload, response->payload_len); memcpy(clone->payload, response->payload, response->payload_len);
@ -296,8 +301,8 @@ fail:
} }
response_t * response_t *
set_response(response_t *response, int status, int fmt, set_response(response_t *response, int status, int fmt, const char *payload,
const char *payload, int payload_len) int payload_len)
{ {
response->payload = (void *)payload; response->payload = (void *)payload;
response->payload_len = payload_len; response->payload_len = payload_len;
@ -307,8 +312,7 @@ set_response(response_t *response, int status, int fmt,
} }
response_t * response_t *
make_response_for_request(request_t *request, make_response_for_request(request_t *request, response_t *response)
response_t *response)
{ {
response->mid = request->mid; response->mid = request->mid;
response->reciever = request->sender; response->reciever = request->sender;
@ -319,8 +323,8 @@ make_response_for_request(request_t *request,
static unsigned int mid = 0; static unsigned int mid = 0;
request_t * request_t *
init_request(request_t *request, char *url, int action, int fmt, init_request(request_t *request, char *url, int action, int fmt, void *payload,
void *payload, int payload_len) int payload_len)
{ {
request->url = url; request->url = url;
request->action = action; request->action = action;
@ -334,10 +338,10 @@ init_request(request_t *request, char *url, int action, int fmt,
/* /*
check if the "url" is starting with "leading_str" check if the "url" is starting with "leading_str"
return: 0 - not match; >0 - the offset of matched url, include any "/" at the end return: 0 - not match; >0 - the offset of matched url, include any "/" at the
notes: end notes:
1. it ensures the leading_str "/abc" can pass "/abc/cde" and "/abc/, but fail "/ab" and "/abcd". 1. it ensures the leading_str "/abc" can pass "/abc/cde" and "/abc/, but fail
leading_str "/abc/" can pass "/abc" "/ab" and "/abcd". leading_str "/abc/" can pass "/abc"
2. it omit the '/' at the first char 2. it omit the '/' at the first char
3. it ensure the leading_str "/abc" can pass "/abc?cde 3. it ensure the leading_str "/abc" can pass "/abc?cde
*/ */
@ -425,7 +429,6 @@ match_url(char *pattern, char *matched)
return true; return true;
return false; return false;
} }
else if (pattern[len - 1] == '*') { else if (pattern[len - 1] == '*') {
if (pattern[len - 2] == '/') { if (pattern[len - 2] == '/') {
@ -468,9 +471,9 @@ find_key_value(char *buffer, int buffer_len, char *key, char *value,
if (0 == strncmp(p, key, key_len) && p[key_len] == '=') { if (0 == strncmp(p, key, key_len) && p[key_len] == '=') {
p += (key_len + 1); p += (key_len + 1);
remaining -= (key_len + 1); remaining -= (key_len + 1);
char * v = value; char *v = value;
memset(value, 0, value_len); memset(value, 0, value_len);
value_len--; /* ensure last char is 0 */ value_len--; /* ensure last char is 0 */
while (*p != delimiter && remaining > 0 && value_len > 0) { while (*p != delimiter && remaining > 0 && value_len > 0) {
*v++ = *p++; *v++ = *p++;
remaining--; remaining--;

View File

@ -1,33 +1,33 @@
#include "lib_export.h" #include "lib_export.h"
#ifdef APP_FRAMEWORK_SENSOR #ifdef APP_FRAMEWORK_SENSOR
#include "sensor_native_api.h" #include "sensor_native_api.h"
#endif #endif
#ifdef APP_FRAMEWORK_CONNECTION #ifdef APP_FRAMEWORK_CONNECTION
#include "connection_native_api.h" #include "connection_native_api.h"
#endif #endif
#ifdef APP_FRAMEWORK_WGL #ifdef APP_FRAMEWORK_WGL
#include "gui_native_api.h" #include "gui_native_api.h"
#endif #endif
/* More header file here */ /* More header file here */
static NativeSymbol extended_native_symbol_defs[] = { static NativeSymbol extended_native_symbol_defs[] = {
#ifdef APP_FRAMEWORK_SENSOR #ifdef APP_FRAMEWORK_SENSOR
#include "runtime_sensor.inl" #include "runtime_sensor.inl"
#endif #endif
#ifdef APP_FRAMEWORK_CONNECTION #ifdef APP_FRAMEWORK_CONNECTION
#include "connection.inl" #include "connection.inl"
#endif #endif
#ifdef APP_FRAMEWORK_WGL #ifdef APP_FRAMEWORK_WGL
#include "wamr_gui.inl" #include "wamr_gui.inl"
#endif #endif
/* More inl file here */ /* More inl file here */
}; };
int int
@ -36,4 +36,3 @@ get_ext_lib_export_apis(NativeSymbol **p_ext_lib_apis)
*p_ext_lib_apis = extended_native_symbol_defs; *p_ext_lib_apis = extended_native_symbol_defs;
return sizeof(extended_native_symbol_defs) / sizeof(NativeSymbol); return sizeof(extended_native_symbol_defs) / sizeof(NativeSymbol);
} }

View File

@ -13,14 +13,16 @@
* *
*/ */
static bool is_little_endian() static bool
is_little_endian()
{ {
long i = 0x01020304; long i = 0x01020304;
unsigned char* c = (unsigned char*) &i; unsigned char *c = (unsigned char *)&i;
return (*c == 0x04) ? true : false; return (*c == 0x04) ? true : false;
} }
static void swap32(uint8* pData) static void
swap32(uint8 *pData)
{ {
uint8 value = *pData; uint8 value = *pData;
*pData = *(pData + 3); *pData = *(pData + 3);
@ -31,31 +33,35 @@ static void swap32(uint8* pData)
*(pData + 2) = value; *(pData + 2) = value;
} }
static void swap16(uint8* pData) static void
swap16(uint8 *pData)
{ {
uint8 value = *pData; uint8 value = *pData;
*(pData) = *(pData + 1); *(pData) = *(pData + 1);
*(pData + 1) = value; *(pData + 1) = value;
} }
uint32 htonl(uint32 value) uint32
htonl(uint32 value)
{ {
uint32 ret; uint32 ret;
if (is_little_endian()) { if (is_little_endian()) {
ret = value; ret = value;
swap32((uint8*) &ret); swap32((uint8 *)&ret);
return ret; return ret;
} }
return value; return value;
} }
uint32 ntohl(uint32 value) uint32
ntohl(uint32 value)
{ {
return htonl(value); return htonl(value);
} }
uint16 htons(uint16 value) uint16
htons(uint16 value)
{ {
uint16 ret; uint16 ret;
if (is_little_endian()) { if (is_little_endian()) {
@ -67,12 +73,14 @@ uint16 htons(uint16 value)
return value; return value;
} }
uint16 ntohs(uint16 value) uint16
ntohs(uint16 value)
{ {
return htons(value); return htons(value);
} }
char *wa_strdup(const char *s) char *
wa_strdup(const char *s)
{ {
char *s1 = NULL; char *s1 = NULL;
if (s && (s1 = WA_MALLOC(strlen(s) + 1))) if (s && (s1 = WA_MALLOC(strlen(s) + 1)))

37
core/app-framework/base/app/bh_platform.h Executable file → Normal file
View File

@ -16,7 +16,7 @@ typedef unsigned int uint32;
typedef int int32; typedef int int32;
#ifndef NULL #ifndef NULL
# define NULL ((void*) 0) #define NULL ((void *)0)
#endif #endif
#ifndef __cplusplus #ifndef __cplusplus
@ -35,28 +35,31 @@ typedef int int32;
#define WA_FREE free #define WA_FREE free
#endif #endif
uint32
uint32 htonl(uint32 value); htonl(uint32 value);
uint32 ntohl(uint32 value); uint32
uint16 htons(uint16 value); ntohl(uint32 value);
uint16 ntohs(uint16 value); uint16
htons(uint16 value);
uint16
ntohs(uint16 value);
// We are not worried for the WASM world since the sandbox will catch it. // We are not worried for the WASM world since the sandbox will catch it.
#define bh_memcpy_s(dst, dst_len, src, src_len) memcpy(dst, src, src_len) #define bh_memcpy_s(dst, dst_len, src, src_len) memcpy(dst, src, src_len)
#ifdef NDEBUG #ifdef NDEBUG
#define bh_assert(v) (void)0 #define bh_assert(v) (void)0
#else #else
#define bh_assert(v) do { \ #define bh_assert(v) \
if (!(v)) { \ do { \
int _count; \ if (!(v)) { \
printf("ASSERTION FAILED: %s, at %s, line %d",\ int _count; \
#v, __FILE__, __LINE__); \ printf("ASSERTION FAILED: %s, at %s, line %d", #v, __FILE__, \
_count = printf("\n"); \ __LINE__); \
printf("%d\n", _count / (_count - 1)); \ _count = printf("\n"); \
} \ printf("%d\n", _count / (_count - 1)); \
} while (0) } \
} while (0)
#endif #endif
#endif /* DEPS_IWASM_APP_LIBS_BASE_BH_PLATFORM_H_ */ #endif /* DEPS_IWASM_APP_LIBS_BASE_BH_PLATFORM_H_ */

View File

@ -29,4 +29,3 @@ wasm_sub_event(const char *url);
#endif #endif
#endif /* end of _REQ_RESP_API_H_ */ #endif /* end of _REQ_RESP_API_H_ */

View File

@ -13,13 +13,11 @@
#define TRANSACTION_TIMEOUT_MS 5000 #define TRANSACTION_TIMEOUT_MS 5000
typedef enum { typedef enum { Reg_Event, Reg_Request } reg_type_t;
Reg_Event, Reg_Request
} reg_type_t;
typedef struct _res_register { typedef struct _res_register {
struct _res_register *next; struct _res_register *next;
const char * url; const char *url;
reg_type_t reg_type; reg_type_t reg_type;
void (*request_handler)(request_t *); void (*request_handler)(request_t *);
} res_register_t; } res_register_t;
@ -32,13 +30,14 @@ typedef struct transaction {
void *user_data; void *user_data;
} transaction_t; } transaction_t;
static res_register_t * g_resources = NULL; static res_register_t *g_resources = NULL;
static transaction_t *g_transactions = NULL; static transaction_t *g_transactions = NULL;
static user_timer_t g_trans_timer = NULL; static user_timer_t g_trans_timer = NULL;
static transaction_t *transaction_find(int mid) static transaction_t *
transaction_find(int mid)
{ {
transaction_t *t = g_transactions; transaction_t *t = g_transactions;
@ -55,7 +54,8 @@ static transaction_t *transaction_find(int mid)
* new transaction is added to the tail of the list, so the list * new transaction is added to the tail of the list, so the list
* is sorted by expiry time naturally. * is sorted by expiry time naturally.
*/ */
static void transaction_add(transaction_t *trans) static void
transaction_add(transaction_t *trans)
{ {
transaction_t *t; transaction_t *t;
@ -73,7 +73,8 @@ static void transaction_add(transaction_t *trans)
} }
} }
static void transaction_remove(transaction_t *trans) static void
transaction_remove(transaction_t *trans)
{ {
transaction_t *prev = NULL, *current = g_transactions; transaction_t *prev = NULL, *current = g_transactions;
@ -93,15 +94,17 @@ static void transaction_remove(transaction_t *trans)
} }
} }
static bool is_event_type(request_t * req) static bool
is_event_type(request_t *req)
{ {
return req->action == COAP_EVENT; return req->action == COAP_EVENT;
} }
static bool register_url_handler(const char *url, static bool
request_handler_f request_handler, reg_type_t reg_type) register_url_handler(const char *url, request_handler_f request_handler,
reg_type_t reg_type)
{ {
res_register_t * r = g_resources; res_register_t *r = g_resources;
while (r) { while (r) {
if (reg_type == r->reg_type && strcmp(r->url, url) == 0) { if (reg_type == r->reg_type && strcmp(r->url, url) == 0) {
@ -111,7 +114,7 @@ static bool register_url_handler(const char *url,
r = r->next; r = r->next;
} }
r = (res_register_t *) malloc(sizeof(res_register_t)); r = (res_register_t *)malloc(sizeof(res_register_t));
if (r == NULL) if (r == NULL)
return false; return false;
@ -137,13 +140,15 @@ static bool register_url_handler(const char *url,
return true; return true;
} }
bool api_register_resource_handler(const char *url, bool
request_handler_f request_handler) api_register_resource_handler(const char *url,
request_handler_f request_handler)
{ {
return register_url_handler(url, request_handler, Reg_Request); return register_url_handler(url, request_handler, Reg_Request);
} }
static void transaction_timeout_handler(user_timer_t timer) static void
transaction_timeout_handler(user_timer_t timer)
{ {
transaction_t *cur, *expired = NULL; transaction_t *cur, *expired = NULL;
unsigned int elpased_ms, now = wasm_get_sys_tick_ms(); unsigned int elpased_ms, now = wasm_get_sys_tick_ms();
@ -164,7 +169,8 @@ static void transaction_timeout_handler(user_timer_t timer)
cur->next = expired; cur->next = expired;
expired = cur; expired = cur;
cur = g_transactions; cur = g_transactions;
} else { }
else {
break; break;
} }
} }
@ -186,27 +192,30 @@ static void transaction_timeout_handler(user_timer_t timer)
unsigned int elpased_ms, ms_to_expiry, now = wasm_get_sys_tick_ms(); unsigned int elpased_ms, ms_to_expiry, now = wasm_get_sys_tick_ms();
if (now < g_transactions->time) { if (now < g_transactions->time) {
elpased_ms = now + (0xFFFFFFFF - g_transactions->time) + 1; elpased_ms = now + (0xFFFFFFFF - g_transactions->time) + 1;
} else { }
else {
elpased_ms = now - g_transactions->time; elpased_ms = now - g_transactions->time;
} }
ms_to_expiry = TRANSACTION_TIMEOUT_MS - elpased_ms; ms_to_expiry = TRANSACTION_TIMEOUT_MS - elpased_ms;
api_timer_restart(g_trans_timer, ms_to_expiry); api_timer_restart(g_trans_timer, ms_to_expiry);
} else { }
else {
api_timer_cancel(g_trans_timer); api_timer_cancel(g_trans_timer);
g_trans_timer = NULL; g_trans_timer = NULL;
} }
} }
void api_send_request(request_t * request, response_handler_f response_handler, void
void * user_data) api_send_request(request_t *request, response_handler_f response_handler,
void *user_data)
{ {
int size; int size;
char *buffer; char *buffer;
transaction_t *trans; transaction_t *trans;
if ((trans = (transaction_t *) malloc(sizeof(transaction_t))) == NULL) { if ((trans = (transaction_t *)malloc(sizeof(transaction_t))) == NULL) {
printf( printf(
"send request: allocate memory for request transaction failed!\n"); "send request: allocate memory for request transaction failed!\n");
return; return;
} }
@ -228,9 +237,8 @@ void api_send_request(request_t * request, response_handler_f response_handler,
if (trans == g_transactions) { if (trans == g_transactions) {
/* assert(g_trans_timer == NULL); */ /* assert(g_trans_timer == NULL); */
if (g_trans_timer == NULL) { if (g_trans_timer == NULL) {
g_trans_timer = api_timer_create(TRANSACTION_TIMEOUT_MS, g_trans_timer = api_timer_create(TRANSACTION_TIMEOUT_MS, false,
false, true, transaction_timeout_handler);
true, transaction_timeout_handler);
} }
} }
@ -241,11 +249,13 @@ void api_send_request(request_t * request, response_handler_f response_handler,
/* /*
* *
* APIs for the native layers to callback for request/response arrived to this app * APIs for the native layers to callback for request/response arrived to this
* app
* *
*/ */
void on_response(char * buffer, int size) void
on_response(char *buffer, int size)
{ {
response_t response[1]; response_t response[1];
transaction_t *trans; transaction_t *trans;
@ -262,7 +272,8 @@ void on_response(char * buffer, int size)
/* /*
* When the 1st transaction get response: * When the 1st transaction get response:
* 1. If the 2nd trans exist, restart the timer according to its expiry time; * 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; * 2. Otherwise, stop the timer since there is no more transactions;
*/ */
if (trans == g_transactions) { if (trans == g_transactions) {
@ -270,12 +281,14 @@ void on_response(char * buffer, int size)
unsigned int elpased_ms, ms_to_expiry, now = wasm_get_sys_tick_ms(); unsigned int elpased_ms, ms_to_expiry, now = wasm_get_sys_tick_ms();
if (now < trans->next->time) { if (now < trans->next->time) {
elpased_ms = now + (0xFFFFFFFF - trans->next->time) + 1; elpased_ms = now + (0xFFFFFFFF - trans->next->time) + 1;
} else { }
else {
elpased_ms = now - trans->next->time; elpased_ms = now - trans->next->time;
} }
ms_to_expiry = TRANSACTION_TIMEOUT_MS - elpased_ms; ms_to_expiry = TRANSACTION_TIMEOUT_MS - elpased_ms;
api_timer_restart(g_trans_timer, ms_to_expiry); api_timer_restart(g_trans_timer, ms_to_expiry);
} else { }
else {
api_timer_cancel(g_trans_timer); api_timer_cancel(g_trans_timer);
g_trans_timer = NULL; g_trans_timer = NULL;
} }
@ -285,7 +298,8 @@ void on_response(char * buffer, int size)
transaction_remove(trans); transaction_remove(trans);
} }
void on_request(char *buffer, int size) void
on_request(char *buffer, int size)
{ {
request_t request[1]; request_t request[1];
bool is_event; bool is_event;
@ -300,9 +314,9 @@ void on_request(char *buffer, int size)
while (r) { while (r) {
if ((is_event && r->reg_type == Reg_Event) if ((is_event && r->reg_type == Reg_Event)
|| (!is_event && r->reg_type == Reg_Request)) { || (!is_event && r->reg_type == Reg_Request)) {
if (check_url_start(request->url, strlen(request->url), r->url) if (check_url_start(request->url, strlen(request->url), r->url)
> 0) { > 0) {
r->request_handler(request); r->request_handler(request);
return; return;
} }
@ -314,10 +328,11 @@ void on_request(char *buffer, int size)
printf("on_request: exit. no service handler\n"); printf("on_request: exit. no service handler\n");
} }
void api_response_send(response_t *response) void
api_response_send(response_t *response)
{ {
int size; int size;
char * buffer = pack_response(response, &size); char *buffer = pack_response(response, &size);
if (buffer == NULL) if (buffer == NULL)
return; return;
@ -327,12 +342,13 @@ void api_response_send(response_t *response)
/// event api /// event api
bool api_publish_event(const char *url, int fmt, void *payload, int payload_len) bool
api_publish_event(const char *url, int fmt, void *payload, int payload_len)
{ {
int size; int size;
request_t request[1]; request_t request[1];
init_request(request, (char *)url, COAP_EVENT, fmt, payload, payload_len); init_request(request, (char *)url, COAP_EVENT, fmt, payload, payload_len);
char * buffer = pack_request(request, &size); char *buffer = pack_request(request, &size);
if (buffer == NULL) if (buffer == NULL)
return false; return false;
wasm_post_request(buffer, size); wasm_post_request(buffer, size);
@ -342,8 +358,8 @@ bool api_publish_event(const char *url, int fmt, void *payload, int payload_len)
return true; return true;
} }
bool api_subscribe_event(const char * url, request_handler_f handler) bool
api_subscribe_event(const char *url, request_handler_f handler)
{ {
return register_url_handler(url, handler, Reg_Event); return register_url_handler(url, handler, Reg_Event);
} }

View File

@ -16,22 +16,23 @@
#endif #endif
struct user_timer { struct user_timer {
struct user_timer * next; struct user_timer *next;
int timer_id; int timer_id;
void (*user_timer_callback)(user_timer_t); void (*user_timer_callback)(user_timer_t);
}; };
struct user_timer * g_timers = NULL; struct user_timer *g_timers = NULL;
user_timer_t api_timer_create(int interval, bool is_period, bool auto_start, user_timer_t
on_user_timer_update_f on_timer_update) 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); int timer_id = wasm_create_timer(interval, is_period, auto_start);
//TODO // TODO
struct user_timer * timer = (struct user_timer *) malloc( struct user_timer *timer =
sizeof(struct user_timer)); (struct user_timer *)malloc(sizeof(struct user_timer));
if (timer == NULL) { if (timer == NULL) {
// TODO: remove the timer_id // TODO: remove the timer_id
printf("### api_timer_create malloc faild!!! \n"); printf("### api_timer_create malloc faild!!! \n");
@ -52,7 +53,8 @@ user_timer_t api_timer_create(int interval, bool is_period, bool auto_start,
return timer; return timer;
} }
void api_timer_cancel(user_timer_t timer) void
api_timer_cancel(user_timer_t timer)
{ {
user_timer_t t = g_timers, prev = NULL; user_timer_t t = g_timers, prev = NULL;
@ -63,26 +65,30 @@ void api_timer_cancel(user_timer_t timer)
if (prev == NULL) { if (prev == NULL) {
g_timers = t->next; g_timers = t->next;
free(t); free(t);
} else { }
else {
prev->next = t->next; prev->next = t->next;
free(t); free(t);
} }
return; return;
} else { }
else {
prev = t; prev = t;
t = t->next; t = t->next;
} }
} }
} }
void api_timer_restart(user_timer_t timer, int interval) void
api_timer_restart(user_timer_t timer, int interval)
{ {
wasm_timer_restart(timer->timer_id, interval); wasm_timer_restart(timer->timer_id, interval);
} }
void on_timer_callback(int timer_id) void
on_timer_callback(int timer_id)
{ {
struct user_timer * t = g_timers; struct user_timer *t = g_timers;
while (t) { while (t) {
if (t->timer_id == timer_id) { if (t->timer_id == timer_id) {
@ -92,4 +98,3 @@ void on_timer_callback(int timer_id)
t = t->next; t = t->next;
} }
} }

View File

@ -34,4 +34,3 @@ wasm_get_sys_tick_ms(void);
#endif #endif
#endif /* end of _TIMER_API_H_ */ #endif /* end of _TIMER_API_H_ */

View File

@ -12,7 +12,6 @@
extern "C" { extern "C" {
#endif #endif
/* CoAP request method codes */ /* CoAP request method codes */
typedef enum { typedef enum {
COAP_GET = 1, COAP_GET = 1,
@ -26,39 +25,40 @@ typedef enum {
typedef enum { typedef enum {
NO_ERROR = 0, NO_ERROR = 0,
CREATED_2_01 = 65, /* CREATED */ CREATED_2_01 = 65, /* CREATED */
DELETED_2_02 = 66, /* DELETED */ DELETED_2_02 = 66, /* DELETED */
VALID_2_03 = 67, /* NOT_MODIFIED */ VALID_2_03 = 67, /* NOT_MODIFIED */
CHANGED_2_04 = 68, /* CHANGED */ CHANGED_2_04 = 68, /* CHANGED */
CONTENT_2_05 = 69, /* OK */ CONTENT_2_05 = 69, /* OK */
CONTINUE_2_31 = 95, /* CONTINUE */ CONTINUE_2_31 = 95, /* CONTINUE */
BAD_REQUEST_4_00 = 128, /* BAD_REQUEST */ BAD_REQUEST_4_00 = 128, /* BAD_REQUEST */
UNAUTHORIZED_4_01 = 129, /* UNAUTHORIZED */ UNAUTHORIZED_4_01 = 129, /* UNAUTHORIZED */
BAD_OPTION_4_02 = 130, /* BAD_OPTION */ BAD_OPTION_4_02 = 130, /* BAD_OPTION */
FORBIDDEN_4_03 = 131, /* FORBIDDEN */ FORBIDDEN_4_03 = 131, /* FORBIDDEN */
NOT_FOUND_4_04 = 132, /* NOT_FOUND */ NOT_FOUND_4_04 = 132, /* NOT_FOUND */
METHOD_NOT_ALLOWED_4_05 = 133, /* METHOD_NOT_ALLOWED */ METHOD_NOT_ALLOWED_4_05 = 133, /* METHOD_NOT_ALLOWED */
NOT_ACCEPTABLE_4_06 = 134, /* NOT_ACCEPTABLE */ NOT_ACCEPTABLE_4_06 = 134, /* NOT_ACCEPTABLE */
PRECONDITION_FAILED_4_12 = 140, /* BAD_REQUEST */ PRECONDITION_FAILED_4_12 = 140, /* BAD_REQUEST */
REQUEST_ENTITY_TOO_LARGE_4_13 = 141, /* REQUEST_ENTITY_TOO_LARGE */ REQUEST_ENTITY_TOO_LARGE_4_13 = 141, /* REQUEST_ENTITY_TOO_LARGE */
UNSUPPORTED_MEDIA_TYPE_4_15 = 143, /* UNSUPPORTED_MEDIA_TYPE */ UNSUPPORTED_MEDIA_TYPE_4_15 = 143, /* UNSUPPORTED_MEDIA_TYPE */
INTERNAL_SERVER_ERROR_5_00 = 160, /* INTERNAL_SERVER_ERROR */ INTERNAL_SERVER_ERROR_5_00 = 160, /* INTERNAL_SERVER_ERROR */
NOT_IMPLEMENTED_5_01 = 161, /* NOT_IMPLEMENTED */ NOT_IMPLEMENTED_5_01 = 161, /* NOT_IMPLEMENTED */
BAD_GATEWAY_5_02 = 162, /* BAD_GATEWAY */ BAD_GATEWAY_5_02 = 162, /* BAD_GATEWAY */
SERVICE_UNAVAILABLE_5_03 = 163, /* SERVICE_UNAVAILABLE */ SERVICE_UNAVAILABLE_5_03 = 163, /* SERVICE_UNAVAILABLE */
GATEWAY_TIMEOUT_5_04 = 164, /* GATEWAY_TIMEOUT */ GATEWAY_TIMEOUT_5_04 = 164, /* GATEWAY_TIMEOUT */
PROXYING_NOT_SUPPORTED_5_05 = 165, /* PROXYING_NOT_SUPPORTED */ PROXYING_NOT_SUPPORTED_5_05 = 165, /* PROXYING_NOT_SUPPORTED */
/* Erbium errors */ /* Erbium errors */
MEMORY_ALLOCATION_ERROR = 192, PACKET_SERIALIZATION_ERROR, MEMORY_ALLOCATION_ERROR = 192,
PACKET_SERIALIZATION_ERROR,
/* Erbium hooks */ /* Erbium hooks */
MANUAL_RESPONSE, PING_RESPONSE MANUAL_RESPONSE,
PING_RESPONSE
} coap_status_t; } coap_status_t;
/** /**
* @typedef request_handler_f * @typedef request_handler_f
* *
@ -87,7 +87,6 @@ typedef void (*request_handler_f)(request_t *request);
*/ */
typedef void (*response_handler_f)(response_t *response, void *user_data); typedef void (*response_handler_f)(response_t *response, void *user_data);
/* /*
***************** *****************
* Request APIs * Request APIs
@ -102,7 +101,8 @@ typedef void (*response_handler_f)(response_t *response, void *user_data);
* *
* @return true if success, false otherwise * @return true if success, false otherwise
*/ */
bool api_register_resource_handler(const char *url, request_handler_f handler); bool
api_register_resource_handler(const char *url, request_handler_f handler);
/** /**
* @brief Send request asynchronously. * @brief Send request asynchronously.
@ -111,8 +111,9 @@ bool api_register_resource_handler(const char *url, request_handler_f handler);
* @param response_handler callback function to handle the response * @param response_handler callback function to handle the response
* @param user_data user data * @param user_data user data
*/ */
void api_send_request(request_t * request, response_handler_f response_handler, void
void * user_data); api_send_request(request_t *request, response_handler_f response_handler,
void *user_data);
/** /**
* @brief Send response. * @brief Send response.
@ -130,8 +131,8 @@ void api_send_request(request_t * request, response_handler_f response_handler,
* } * }
* @endcode * @endcode
*/ */
void api_response_send(response_t *response); void
api_response_send(response_t *response);
/* /*
***************** *****************
@ -149,9 +150,8 @@ void api_response_send(response_t *response);
* *
* @return true if success, false otherwise * @return true if success, false otherwise
*/ */
bool api_publish_event(const char *url, int fmt, void *payload, bool
int payload_len); api_publish_event(const char *url, int fmt, void *payload, int payload_len);
/** /**
* @brief Subscribe an event. * @brief Subscribe an event.
@ -161,7 +161,8 @@ bool api_publish_event(const char *url, int fmt, void *payload,
* *
* @return true if success, false otherwise * @return true if success, false otherwise
*/ */
bool api_subscribe_event(const char * url, request_handler_f handler); bool
api_subscribe_event(const char *url, request_handler_f handler);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -14,7 +14,7 @@ extern "C" {
/* board producer define user_timer */ /* board producer define user_timer */
struct user_timer; struct user_timer;
typedef struct user_timer * user_timer_t; typedef struct user_timer *user_timer_t;
/** /**
* @typedef on_user_timer_update_f * @typedef on_user_timer_update_f
@ -43,15 +43,17 @@ typedef void (*on_user_timer_update_f)(user_timer_t timer);
* *
* @return the timer created if success, NULL otherwise * @return the timer created if success, NULL otherwise
*/ */
user_timer_t api_timer_create(int interval, bool is_period, bool auto_start, user_timer_t
on_user_timer_update_f on_timer_update); api_timer_create(int interval, bool is_period, bool auto_start,
on_user_timer_update_f on_timer_update);
/** /**
* @brief Cancel timer. * @brief Cancel timer.
* *
* @param timer the timer to cancel * @param timer the timer to cancel
*/ */
void api_timer_cancel(user_timer_t timer); void
api_timer_cancel(user_timer_t timer);
/** /**
* @brief Restart timer. * @brief Restart timer.
@ -59,7 +61,8 @@ void api_timer_cancel(user_timer_t timer);
* @param timer the timer to cancel * @param timer the timer to cancel
* @param interval the timer interval * @param interval the timer interval
*/ */
void api_timer_restart(user_timer_t timer, int interval); void
api_timer_restart(user_timer_t timer, int interval);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -13,7 +13,6 @@
extern "C" { extern "C" {
#endif #endif
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -10,16 +10,15 @@
#include "req_resp_native_api.h" #include "req_resp_native_api.h"
#include "timer_native_api.h" #include "timer_native_api.h"
static NativeSymbol extended_native_symbol_defs[] = { static NativeSymbol extended_native_symbol_defs[] = {
/* TODO: use macro EXPORT_WASM_API() or EXPORT_WASM_API2() to /* TODO: use macro EXPORT_WASM_API() or EXPORT_WASM_API2() to
add functions to register. */ add functions to register. */
#include "base_lib.inl" #include "base_lib.inl"
}; };
uint32 get_base_lib_export_apis(NativeSymbol **p_base_lib_apis) uint32
get_base_lib_export_apis(NativeSymbol **p_base_lib_apis)
{ {
*p_base_lib_apis = extended_native_symbol_defs; *p_base_lib_apis = extended_native_symbol_defs;
return sizeof(extended_native_symbol_defs) / sizeof(NativeSymbol); return sizeof(extended_native_symbol_defs) / sizeof(NativeSymbol);
} }

View File

@ -27,4 +27,3 @@ wasm_sub_event(wasm_exec_env_t exec_env, char *url);
#endif #endif
#endif /* end of _REQ_RESP_API_H_ */ #endif /* end of _REQ_RESP_API_H_ */

View File

@ -8,7 +8,8 @@
#include "wasm_export.h" #include "wasm_export.h"
#include "bh_assert.h" #include "bh_assert.h"
extern void module_request_handler(request_t *request, void *user_data); extern void
module_request_handler(request_t *request, void *user_data);
bool bool
wasm_response_send(wasm_exec_env_t exec_env, char *buffer, int size) wasm_response_send(wasm_exec_env_t exec_env, char *buffer, int size)
@ -33,8 +34,8 @@ wasm_register_resource(wasm_exec_env_t exec_env, char *url)
wasm_module_inst_t module_inst = get_module_inst(exec_env); wasm_module_inst_t module_inst = get_module_inst(exec_env);
if (url != NULL) { if (url != NULL) {
unsigned int mod_id = app_manager_get_module_id(Module_WASM_App, unsigned int mod_id =
module_inst); app_manager_get_module_id(Module_WASM_App, module_inst);
bh_assert(mod_id != ID_NONE); bh_assert(mod_id != ID_NONE);
am_register_resource(url, module_request_handler, mod_id); am_register_resource(url, module_request_handler, mod_id);
} }
@ -54,8 +55,8 @@ wasm_post_request(wasm_exec_env_t exec_env, char *buffer, int size)
// TODO: add permission check, ensure app can't do harm // TODO: add permission check, ensure app can't do harm
// set sender to help dispatch the response to the sender ap // set sender to help dispatch the response to the sender ap
unsigned int mod_id = app_manager_get_module_id(Module_WASM_App, unsigned int mod_id =
module_inst); app_manager_get_module_id(Module_WASM_App, module_inst);
bh_assert(mod_id != ID_NONE); bh_assert(mod_id != ID_NONE);
req->sender = mod_id; req->sender = mod_id;
@ -74,11 +75,10 @@ wasm_sub_event(wasm_exec_env_t exec_env, char *url)
wasm_module_inst_t module_inst = get_module_inst(exec_env); wasm_module_inst_t module_inst = get_module_inst(exec_env);
if (url != NULL) { if (url != NULL) {
unsigned int mod_id = app_manager_get_module_id(Module_WASM_App, unsigned int mod_id =
module_inst); app_manager_get_module_id(Module_WASM_App, module_inst);
bh_assert(mod_id != ID_NONE); bh_assert(mod_id != ID_NONE);
am_register_event(url, mod_id); am_register_event(url, mod_id);
} }
} }

View File

@ -6,13 +6,17 @@
#ifndef LIB_BASE_RUNTIME_LIB_H_ #ifndef LIB_BASE_RUNTIME_LIB_H_
#define LIB_BASE_RUNTIME_LIB_H_ #define LIB_BASE_RUNTIME_LIB_H_
#include "runtime_timer.h" #include "runtime_timer.h"
void init_wasm_timer(); void
void exit_wasm_timer(); init_wasm_timer();
timer_ctx_t get_wasm_timer_ctx(); void
timer_ctx_t create_wasm_timer_ctx(unsigned int module_id, int prealloc_num); exit_wasm_timer();
void destroy_module_timer_ctx(unsigned int module_id); 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_ */ #endif /* LIB_BASE_RUNTIME_LIB_H_ */

View File

@ -9,7 +9,6 @@
#include "bh_platform.h" #include "bh_platform.h"
#include "wasm_export.h" #include "wasm_export.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
@ -23,15 +22,14 @@ typedef unsigned int timer_id_t;
typedef unsigned int timer_id_t; typedef unsigned int timer_id_t;
timer_id_t timer_id_t
wasm_create_timer(wasm_exec_env_t exec_env, wasm_create_timer(wasm_exec_env_t exec_env, int interval, bool is_period,
int interval, bool is_period, bool auto_start); bool auto_start);
void void
wasm_timer_destroy(wasm_exec_env_t exec_env, timer_id_t timer_id); wasm_timer_destroy(wasm_exec_env_t exec_env, timer_id_t timer_id);
void void
wasm_timer_cancel(wasm_exec_env_t exec_env, timer_id_t timer_id); wasm_timer_cancel(wasm_exec_env_t exec_env, timer_id_t timer_id);
void void
wasm_timer_restart(wasm_exec_env_t exec_env, wasm_timer_restart(wasm_exec_env_t exec_env, timer_id_t timer_id, int interval);
timer_id_t timer_id, int interval);
uint32 uint32
wasm_get_sys_tick_ms(wasm_exec_env_t exec_env); wasm_get_sys_tick_ms(wasm_exec_env_t exec_env);
@ -40,4 +38,3 @@ wasm_get_sys_tick_ms(wasm_exec_env_t exec_env);
#endif #endif
#endif /* end of _TIMER_API_H_ */ #endif /* end of _TIMER_API_H_ */

View File

@ -22,7 +22,7 @@ static korp_mutex g_timer_ctx_list_mutex;
void void
wasm_timer_callback(timer_id_t id, unsigned int mod_id) wasm_timer_callback(timer_id_t id, unsigned int mod_id)
{ {
module_data* module = module_data_list_lookup_id(mod_id); module_data *module = module_data_list_lookup_id(mod_id);
if (module == NULL) if (module == NULL)
return; return;
@ -40,7 +40,8 @@ wasm_timer_callback(timer_id_t id, unsigned int mod_id)
* the module from module id. It is for avoiding that situation. * the module from module id. It is for avoiding that situation.
*/ */
void * thread_modulers_timer_check(void * arg) void *
thread_modulers_timer_check(void *arg)
{ {
uint32 ms_to_expiry; uint32 ms_to_expiry;
uint64 us_to_wait; uint64 us_to_wait;
@ -48,8 +49,8 @@ void * thread_modulers_timer_check(void * arg)
while (timer_thread_run) { while (timer_thread_run) {
ms_to_expiry = (uint32)-1; ms_to_expiry = (uint32)-1;
os_mutex_lock(&g_timer_ctx_list_mutex); os_mutex_lock(&g_timer_ctx_list_mutex);
timer_ctx_node_t* elem = (timer_ctx_node_t*) timer_ctx_node_t *elem =
bh_list_first_elem(&g_timer_ctx_list); (timer_ctx_node_t *)bh_list_first_elem(&g_timer_ctx_list);
while (elem) { while (elem) {
uint32 next = check_app_timers(elem->timer_ctx); uint32 next = check_app_timers(elem->timer_ctx);
if (next != (uint32)-1) { if (next != (uint32)-1) {
@ -57,7 +58,7 @@ void * thread_modulers_timer_check(void * arg)
ms_to_expiry = next; ms_to_expiry = next;
} }
elem = (timer_ctx_node_t*) bh_list_elem_next(elem); elem = (timer_ctx_node_t *)bh_list_elem_next(elem);
} }
os_mutex_unlock(&g_timer_ctx_list_mutex); os_mutex_unlock(&g_timer_ctx_list_mutex);
@ -93,8 +94,8 @@ init_wasm_timer()
would recursive lock the mutex */ would recursive lock the mutex */
os_recursive_mutex_init(&g_timer_ctx_list_mutex); os_recursive_mutex_init(&g_timer_ctx_list_mutex);
os_thread_create(&tm_tid, thread_modulers_timer_check, os_thread_create(&tm_tid, thread_modulers_timer_check, NULL,
NULL, BH_APPLET_PRESERVED_STACK_SIZE); BH_APPLET_PRESERVED_STACK_SIZE);
} }
void void
@ -106,15 +107,15 @@ exit_wasm_timer()
timer_ctx_t timer_ctx_t
create_wasm_timer_ctx(unsigned int module_id, int prealloc_num) create_wasm_timer_ctx(unsigned int module_id, int prealloc_num)
{ {
timer_ctx_t ctx = create_timer_ctx(wasm_timer_callback, timer_ctx_t ctx =
wakeup_modules_timer_thread, create_timer_ctx(wasm_timer_callback, wakeup_modules_timer_thread,
prealloc_num, module_id); prealloc_num, module_id);
if (ctx == NULL) if (ctx == NULL)
return NULL; return NULL;
timer_ctx_node_t * node = (timer_ctx_node_t*) timer_ctx_node_t *node =
wasm_runtime_malloc(sizeof(timer_ctx_node_t)); (timer_ctx_node_t *)wasm_runtime_malloc(sizeof(timer_ctx_node_t));
if (node == NULL) { if (node == NULL) {
destroy_timer_ctx(ctx); destroy_timer_ctx(ctx);
return NULL; return NULL;
@ -132,11 +133,10 @@ create_wasm_timer_ctx(unsigned int module_id, int prealloc_num)
void void
destroy_module_timer_ctx(unsigned int module_id) destroy_module_timer_ctx(unsigned int module_id)
{ {
timer_ctx_node_t* elem; timer_ctx_node_t *elem;
os_mutex_lock(&g_timer_ctx_list_mutex); os_mutex_lock(&g_timer_ctx_list_mutex);
elem = (timer_ctx_node_t*) elem = (timer_ctx_node_t *)bh_list_first_elem(&g_timer_ctx_list);
bh_list_first_elem(&g_timer_ctx_list);
while (elem) { while (elem) {
if (timer_ctx_get_owner(elem->timer_ctx) == module_id) { if (timer_ctx_get_owner(elem->timer_ctx) == module_id) {
bh_list_remove(&g_timer_ctx_list, elem); bh_list_remove(&g_timer_ctx_list, elem);
@ -145,7 +145,7 @@ destroy_module_timer_ctx(unsigned int module_id)
break; break;
} }
elem = (timer_ctx_node_t*) bh_list_elem_next(elem); elem = (timer_ctx_node_t *)bh_list_elem_next(elem);
} }
os_mutex_unlock(&g_timer_ctx_list_mutex); os_mutex_unlock(&g_timer_ctx_list_mutex);
} }
@ -153,16 +153,15 @@ destroy_module_timer_ctx(unsigned int module_id)
timer_ctx_t timer_ctx_t
get_wasm_timer_ctx(wasm_module_inst_t module_inst) get_wasm_timer_ctx(wasm_module_inst_t module_inst)
{ {
module_data * m = app_manager_get_module_data(Module_WASM_App, module_data *m = app_manager_get_module_data(Module_WASM_App, module_inst);
module_inst);
if (m == NULL) if (m == NULL)
return NULL; return NULL;
return m->timer_ctx; return m->timer_ctx;
} }
timer_id_t timer_id_t
wasm_create_timer(wasm_exec_env_t exec_env, wasm_create_timer(wasm_exec_env_t exec_env, int interval, bool is_period,
int interval, bool is_period, bool auto_start) bool auto_start)
{ {
wasm_module_inst_t module_inst = get_module_inst(exec_env); wasm_module_inst_t module_inst = get_module_inst(exec_env);
timer_ctx_t timer_ctx = get_wasm_timer_ctx(module_inst); timer_ctx_t timer_ctx = get_wasm_timer_ctx(module_inst);
@ -189,8 +188,7 @@ wasm_timer_cancel(wasm_exec_env_t exec_env, timer_id_t timer_id)
} }
void void
wasm_timer_restart(wasm_exec_env_t exec_env, wasm_timer_restart(wasm_exec_env_t exec_env, timer_id_t timer_id, int interval)
timer_id_t timer_id, int interval)
{ {
wasm_module_inst_t module_inst = get_module_inst(exec_env); wasm_module_inst_t module_inst = get_module_inst(exec_env);
timer_ctx_t timer_ctx = get_wasm_timer_ctx(module_inst); timer_ctx_t timer_ctx = get_wasm_timer_ctx(module_inst);
@ -203,4 +201,3 @@ wasm_get_sys_tick_ms(wasm_exec_env_t exec_env)
{ {
return (uint32)bh_get_tick_ms(); return (uint32)bh_get_tick_ms();
} }

View File

@ -24,10 +24,9 @@ typedef struct _connection {
/* Raw connections list */ /* Raw connections list */
static connection_t *g_conns = NULL; static connection_t *g_conns = NULL;
connection_t *api_open_connection(const char *name, connection_t *
attr_container_t *args, api_open_connection(const char *name, attr_container_t *args,
on_connection_event_f on_event, on_connection_event_f on_event, void *user_data)
void *user_data)
{ {
connection_t *conn; connection_t *conn;
char *args_buffer = (char *)args; char *args_buffer = (char *)args;
@ -51,14 +50,16 @@ connection_t *api_open_connection(const char *name,
if (g_conns != NULL) { if (g_conns != NULL) {
conn->next = g_conns; conn->next = g_conns;
g_conns = conn; g_conns = conn;
} else { }
else {
g_conns = conn; g_conns = conn;
} }
return conn; return conn;
} }
void api_close_connection(connection_t *c) void
api_close_connection(connection_t *c)
{ {
connection_t *conn = g_conns, *prev = NULL; connection_t *conn = g_conns, *prev = NULL;
@ -71,19 +72,22 @@ void api_close_connection(connection_t *c)
g_conns = conn->next; g_conns = conn->next;
free(conn); free(conn);
return; return;
} else { }
else {
prev = conn; prev = conn;
conn = conn->next; conn = conn->next;
} }
} }
} }
int api_send_on_connection(connection_t *conn, const char *data, uint32 len) int
api_send_on_connection(connection_t *conn, const char *data, uint32 len)
{ {
return wasm_send_on_connection(conn->handle, data, len); return wasm_send_on_connection(conn->handle, data, len);
} }
bool api_config_connection(connection_t *conn, attr_container_t *cfg) bool
api_config_connection(connection_t *conn, attr_container_t *cfg)
{ {
char *cfg_buffer = (char *)cfg; char *cfg_buffer = (char *)cfg;
uint32 cfg_len = attr_container_get_serialize_length(cfg); uint32 cfg_len = attr_container_get_serialize_length(cfg);
@ -91,23 +95,19 @@ bool api_config_connection(connection_t *conn, attr_container_t *cfg)
return wasm_config_connection(conn->handle, cfg_buffer, cfg_len); return wasm_config_connection(conn->handle, cfg_buffer, cfg_len);
} }
void on_connection_data(uint32 handle, char *buffer, uint32 len) void
on_connection_data(uint32 handle, char *buffer, uint32 len)
{ {
connection_t *conn = g_conns; connection_t *conn = g_conns;
while (conn != NULL) { while (conn != NULL) {
if (conn->handle == handle) { if (conn->handle == handle) {
if (len == 0) { if (len == 0) {
conn->on_event(conn, conn->on_event(conn, CONN_EVENT_TYPE_DISCONNECT, NULL, 0,
CONN_EVENT_TYPE_DISCONNECT,
NULL,
0,
conn->user_data); conn->user_data);
} else { }
conn->on_event(conn, else {
CONN_EVENT_TYPE_DATA, conn->on_event(conn, CONN_EVENT_TYPE_DATA, buffer, len,
buffer,
len,
conn->user_data); conn->user_data);
} }
@ -116,4 +116,3 @@ void on_connection_data(uint32 handle, char *buffer, uint32 len)
conn = conn->next; conn = conn->next;
} }
} }

View File

@ -28,5 +28,4 @@ wasm_config_connection(uint32 handle, const char *cfg_buf, uint32 cfg_buf_len);
} }
#endif #endif
#endif /* end of CONNECTION_API_H_ */ #endif /* end of CONNECTION_API_H_ */

View File

@ -33,10 +33,8 @@ typedef enum {
* @param user_data user data * @param user_data user data
*/ */
typedef void (*on_connection_event_f)(connection_t *conn, typedef void (*on_connection_event_f)(connection_t *conn,
conn_event_type_t type, conn_event_type_t type, const char *data,
const char *data, uint32 len, void *user_data);
uint32 len,
void *user_data);
/* /*
***************** *****************
@ -54,17 +52,17 @@ typedef void (*on_connection_event_f)(connection_t *conn,
* *
* @return the connection or NULL means fail * @return the connection or NULL means fail
*/ */
connection_t *api_open_connection(const char *name, connection_t *
attr_container_t *args, api_open_connection(const char *name, attr_container_t *args,
on_connection_event_f on_event, on_connection_event_f on_event, void *user_data);
void *user_data);
/* /*
* @brief Close a connection. * @brief Close a connection.
* *
* @param conn connection * @param conn connection
*/ */
void api_close_connection(connection_t *conn); void
api_close_connection(connection_t *conn);
/* /*
* Send data to the connection in non-blocking manner which returns immediately * Send data to the connection in non-blocking manner which returns immediately
@ -75,7 +73,8 @@ void api_close_connection(connection_t *conn);
* *
* @return actual length sent, or -1 if fail(maybe underlying buffer is full) * @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); int
api_send_on_connection(connection_t *conn, const char *data, uint32 len);
/* /*
* @brief Configure connection. * @brief Configure connection.
@ -85,8 +84,8 @@ int api_send_on_connection(connection_t *conn, const char *data, uint32 len);
* *
* @return true if success, false otherwise * @return true if success, false otherwise
*/ */
bool api_config_connection(connection_t *conn, attr_container_t *cfg); bool
api_config_connection(connection_t *conn, attr_container_t *cfg);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -13,10 +13,9 @@
extern "C" { extern "C" {
#endif #endif
/* /**
***************** * This file defines connection library which should be implemented by
* This file defines connection library which should be implemented by different platforms * different platforms
*****************
*/ */
/* /*
@ -73,5 +72,4 @@ extern connection_interface_t connection_impl;
} }
#endif #endif
#endif /* CONNECTION_LIB_H_ */ #endif /* CONNECTION_LIB_H_ */

View File

@ -13,29 +13,24 @@
extern "C" { extern "C" {
#endif #endif
/* /*
* connection interfaces * connection interfaces
*/ */
uint32 uint32
wasm_open_connection(wasm_exec_env_t exec_env, wasm_open_connection(wasm_exec_env_t exec_env, char *name, char *args_buf,
char *name, char *args_buf, uint32 len); uint32 len);
void void
wasm_close_connection(wasm_exec_env_t exec_env, wasm_close_connection(wasm_exec_env_t exec_env, uint32 handle);
uint32 handle);
int int
wasm_send_on_connection(wasm_exec_env_t exec_env, wasm_send_on_connection(wasm_exec_env_t exec_env, uint32 handle, char *data,
uint32 handle, char *data, uint32 len); uint32 len);
bool bool
wasm_config_connection(wasm_exec_env_t exec_env, wasm_config_connection(wasm_exec_env_t exec_env, uint32 handle, char *cfg_buf,
uint32 handle, char *cfg_buf, uint32 len); uint32 len);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif /* end of CONNECTION_API_H_ */ #endif /* end of CONNECTION_API_H_ */

View File

@ -8,15 +8,15 @@
#include "native_interface.h" #include "native_interface.h"
#include "connection_native_api.h" #include "connection_native_api.h"
/* Note: /* Note:
* *
* This file is the consumer of connection lib which is implemented by different platforms * This file is the consumer of connection lib which is implemented by different
* platforms
*/ */
uint32 uint32
wasm_open_connection(wasm_exec_env_t exec_env, wasm_open_connection(wasm_exec_env_t exec_env, char *name, char *args_buf,
char *name, char *args_buf, uint32 len) uint32 len)
{ {
wasm_module_inst_t module_inst = get_module_inst(exec_env); wasm_module_inst_t module_inst = get_module_inst(exec_env);
attr_container_t *args; attr_container_t *args;
@ -37,8 +37,8 @@ wasm_close_connection(wasm_exec_env_t exec_env, uint32 handle)
} }
int int
wasm_send_on_connection(wasm_exec_env_t exec_env, wasm_send_on_connection(wasm_exec_env_t exec_env, uint32 handle, char *data,
uint32 handle, char *data, uint32 len) uint32 len)
{ {
if (connection_impl._send != NULL) if (connection_impl._send != NULL)
return connection_impl._send(handle, data, len); return connection_impl._send(handle, data, len);
@ -47,8 +47,8 @@ wasm_send_on_connection(wasm_exec_env_t exec_env,
} }
bool bool
wasm_config_connection(wasm_exec_env_t exec_env, wasm_config_connection(wasm_exec_env_t exec_env, uint32 handle, char *cfg_buf,
uint32 handle, char *cfg_buf, uint32 len) uint32 len)
{ {
attr_container_t *cfg; attr_container_t *cfg;

View File

@ -11,7 +11,8 @@
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
int tcp_open(char *address, uint16 port) int
tcp_open(char *address, uint16 port)
{ {
int sock, ret; int sock, ret;
struct sockaddr_in servaddr; struct sockaddr_in servaddr;
@ -25,7 +26,7 @@ int tcp_open(char *address, uint16 port)
if (sock == -1) if (sock == -1)
return -1; return -1;
ret = connect(sock, (struct sockaddr*)&servaddr, sizeof(servaddr)); ret = connect(sock, (struct sockaddr *)&servaddr, sizeof(servaddr));
if (ret == -1) { if (ret == -1) {
close(sock); close(sock);
return -1; return -1;
@ -40,12 +41,14 @@ int tcp_open(char *address, uint16 port)
return sock; return sock;
} }
int tcp_send(int sock, const char *data, int size) int
tcp_send(int sock, const char *data, int size)
{ {
return send(sock, data, size, 0); return send(sock, data, size, 0);
} }
int tcp_recv(int sock, char *buffer, int buf_size) int
tcp_recv(int sock, char *buffer, int buf_size)
{ {
return recv(sock, buffer, buf_size, 0); return recv(sock, buffer, buf_size, 0);
} }

View File

@ -12,15 +12,17 @@
extern "C" { extern "C" {
#endif #endif
int tcp_open(char *address, uint16 port); int
tcp_open(char *address, uint16 port);
int tcp_send(int sock, const char *data, int size); int
tcp_send(int sock, const char *data, int size);
int tcp_recv(int sock, char *buffer, int buf_size); int
tcp_recv(int sock, char *buffer, int buf_size);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif #endif

View File

@ -9,7 +9,8 @@
#include <termios.h> #include <termios.h>
#include <unistd.h> #include <unistd.h>
static int parse_baudrate(int baud) static int
parse_baudrate(int baud)
{ {
switch (baud) { switch (baud) {
case 9600: case 9600:
@ -53,7 +54,8 @@ static int parse_baudrate(int baud)
} }
} }
int uart_open(char* device, int baudrate) int
uart_open(char *device, int baudrate)
{ {
int uart_fd; int uart_fd;
struct termios uart_term; struct termios uart_term;
@ -88,12 +90,14 @@ int uart_open(char* device, int baudrate)
return uart_fd; return uart_fd;
} }
int uart_send(int fd, const char *data, int size) int
uart_send(int fd, const char *data, int size)
{ {
return write(fd, data, size); return write(fd, data, size);
} }
int uart_recv(int fd, char *buffer, int buf_size) int
uart_recv(int fd, char *buffer, int buf_size)
{ {
return read(fd, buffer, buf_size); return read(fd, buffer, buf_size);
} }

View File

@ -12,15 +12,17 @@
extern "C" { extern "C" {
#endif #endif
int uart_open(char* device, int baudrate); int
uart_open(char *device, int baudrate);
int uart_send(int fd, const char *data, int size); int
uart_send(int fd, const char *data, int size);
int uart_recv(int fd, char *buffer, int buf_size); int
uart_recv(int fd, char *buffer, int buf_size);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif #endif

View File

@ -11,7 +11,8 @@
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
int udp_open(uint16 port) int
udp_open(uint16 port)
{ {
int sock, ret; int sock, ret;
struct sockaddr_in addr; struct sockaddr_in addr;
@ -25,7 +26,7 @@ int udp_open(uint16 port)
addr.sin_addr.s_addr = htonl(INADDR_ANY); addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(port); addr.sin_port = htons(port);
ret = bind(sock, (struct sockaddr*)&addr, sizeof(addr)); ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr));
if (ret == -1) { if (ret == -1) {
close(sock); close(sock);
return -1; return -1;
@ -40,20 +41,18 @@ int udp_open(uint16 port)
return sock; return sock;
} }
int udp_send(int sock, struct sockaddr *dest, const char *data, int size) int
udp_send(int sock, struct sockaddr *dest, const char *data, int size)
{ {
return sendto(sock, data, size, MSG_CONFIRM, dest, sizeof(*dest)); return sendto(sock, data, size, MSG_CONFIRM, dest, sizeof(*dest));
} }
int udp_recv(int sock, char *buffer, int buf_size) int
udp_recv(int sock, char *buffer, int buf_size)
{ {
struct sockaddr_in remaddr; struct sockaddr_in remaddr;
socklen_t addrlen = sizeof(remaddr); socklen_t addrlen = sizeof(remaddr);
return recvfrom(sock, return recvfrom(sock, buffer, buf_size, 0, (struct sockaddr *)&remaddr,
buffer,
buf_size,
0,
(struct sockaddr *)&remaddr,
&addrlen); &addrlen);
} }

View File

@ -12,15 +12,17 @@
extern "C" { extern "C" {
#endif #endif
int udp_open(uint16 port); int
udp_open(uint16 port);
int udp_send(int sock, struct sockaddr *dest, const char *data, int size); int
udp_send(int sock, struct sockaddr *dest, const char *data, int size);
int udp_recv(int sock, char *buffer, int buf_size); int
udp_recv(int sock, char *buffer, int buf_size);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif #endif

View File

@ -76,23 +76,30 @@ static struct epoll_event epoll_events[MAX_EVENTS];
/* Buffer to receive data */ /* Buffer to receive data */
static char io_buf[IO_BUF_SIZE]; static char io_buf[IO_BUF_SIZE];
static uint32 _conn_open(wasm_module_inst_t module_inst, static uint32
const char *name, attr_container_t *args); _conn_open(wasm_module_inst_t module_inst, const char *name,
static void _conn_close(uint32 handle); attr_container_t *args);
static int _conn_send(uint32 handle, const char *data, int len); static void
static bool _conn_config(uint32 handle, attr_container_t *cfg); _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);
/* clang-format off */
/* /*
* Platform implementation of connection library * Platform implementation of connection library
*/ */
connection_interface_t connection_impl = { connection_interface_t connection_impl = {
._open = _conn_open, ._open = _conn_open,
._close = _conn_close, ._close = _conn_close,
._send = _conn_send, ._send = _conn_send,
._config = _conn_config ._config = _conn_config
}; };
/* clang-format on */
static void add_connection(sys_connection_t *conn) static void
add_connection(sys_connection_t *conn)
{ {
os_mutex_lock(&g_lock); os_mutex_lock(&g_lock);
@ -104,20 +111,23 @@ static void add_connection(sys_connection_t *conn)
if (g_connections) { if (g_connections) {
conn->next = g_connections; conn->next = g_connections;
g_connections = conn; g_connections = conn;
} else { }
else {
g_connections = conn; g_connections = conn;
} }
os_mutex_unlock(&g_lock); os_mutex_unlock(&g_lock);
} }
#define FREE_CONNECTION(conn) do { \ #define FREE_CONNECTION(conn) \
if (conn->arg) \ do { \
wasm_runtime_free(conn->arg); \ if (conn->arg) \
wasm_runtime_free(conn); \ wasm_runtime_free(conn->arg); \
} while (0) wasm_runtime_free(conn); \
} while (0)
static int get_app_conns_num(uint32 module_id) static int
get_app_conns_num(uint32 module_id)
{ {
sys_connection_t *conn; sys_connection_t *conn;
int num = 0; int num = 0;
@ -136,7 +146,8 @@ static int get_app_conns_num(uint32 module_id)
return num; return num;
} }
static sys_connection_t *find_connection(uint32 handle, bool remove_found) static sys_connection_t *
find_connection(uint32 handle, bool remove_found)
{ {
sys_connection_t *conn, *prev = NULL; sys_connection_t *conn, *prev = NULL;
@ -148,13 +159,15 @@ static sys_connection_t *find_connection(uint32 handle, bool remove_found)
if (remove_found) { if (remove_found) {
if (prev != NULL) { if (prev != NULL) {
prev->next = conn->next; prev->next = conn->next;
} else { }
else {
g_connections = conn->next; g_connections = conn->next;
} }
} }
os_mutex_unlock(&g_lock); os_mutex_unlock(&g_lock);
return conn; return conn;
} else { }
else {
prev = conn; prev = conn;
conn = conn->next; conn = conn->next;
} }
@ -165,7 +178,8 @@ static sys_connection_t *find_connection(uint32 handle, bool remove_found)
return NULL; return NULL;
} }
static void cleanup_connections(uint32 module_id) static void
cleanup_connections(uint32 module_id)
{ {
sys_connection_t *conn, *prev = NULL; sys_connection_t *conn, *prev = NULL;
@ -181,12 +195,14 @@ static void cleanup_connections(uint32 module_id)
prev->next = conn->next; prev->next = conn->next;
FREE_CONNECTION(conn); FREE_CONNECTION(conn);
conn = prev->next; conn = prev->next;
} else { }
else {
g_connections = conn->next; g_connections = conn->next;
FREE_CONNECTION(conn); FREE_CONNECTION(conn);
conn = g_connections; conn = g_connections;
} }
} else { }
else {
prev = conn; prev = conn;
conn = conn->next; conn = conn->next;
} }
@ -195,7 +211,8 @@ static void cleanup_connections(uint32 module_id)
os_mutex_unlock(&g_lock); os_mutex_unlock(&g_lock);
} }
static conn_type_t get_conn_type(const char *name) static conn_type_t
get_conn_type(const char *name)
{ {
if (strcmp(name, "TCP") == 0) if (strcmp(name, "TCP") == 0)
return CONN_TYPE_TCP; return CONN_TYPE_TCP;
@ -208,14 +225,14 @@ static conn_type_t get_conn_type(const char *name)
} }
/* --- connection lib function --- */ /* --- connection lib function --- */
static uint32 _conn_open(wasm_module_inst_t module_inst, static uint32
const char *name, attr_container_t *args) _conn_open(wasm_module_inst_t module_inst, const char *name,
attr_container_t *args)
{ {
int fd; int fd;
sys_connection_t *conn; sys_connection_t *conn;
struct epoll_event ev; struct epoll_event ev;
uint32 module_id = app_manager_get_module_id(Module_WASM_App, uint32 module_id = app_manager_get_module_id(Module_WASM_App, module_inst);
module_inst);
bh_assert(module_id != ID_NONE); bh_assert(module_id != ID_NONE);
if (get_app_conns_num(module_id) >= MAX_CONNECTION_PER_APP) if (get_app_conns_num(module_id) >= MAX_CONNECTION_PER_APP)
@ -237,8 +254,8 @@ static uint32 _conn_open(wasm_module_inst_t module_inst,
uint16 port; uint16 port;
/* Check and parse connection parameters */ /* Check and parse connection parameters */
if (!attr_container_contain_key(args, "address") || if (!attr_container_contain_key(args, "address")
!attr_container_contain_key(args, "port")) || !attr_container_contain_key(args, "port"))
goto fail; goto fail;
address = attr_container_get_as_string(args, "address"); address = attr_container_get_as_string(args, "address");
@ -247,8 +264,8 @@ static uint32 _conn_open(wasm_module_inst_t module_inst,
/* Connect to TCP server */ /* Connect to TCP server */
if (!address || (fd = tcp_open(address, port)) == -1) if (!address || (fd = tcp_open(address, port)) == -1)
goto fail; goto fail;
}
} else if (conn->type == CONN_TYPE_UDP) { else if (conn->type == CONN_TYPE_UDP) {
uint16 port; uint16 port;
/* Check and parse connection parameters */ /* Check and parse connection parameters */
@ -259,14 +276,14 @@ static uint32 _conn_open(wasm_module_inst_t module_inst,
/* Bind port */ /* Bind port */
if ((fd = udp_open(port)) == -1) if ((fd = udp_open(port)) == -1)
goto fail; goto fail;
}
} else if (conn->type == CONN_TYPE_UART) { else if (conn->type == CONN_TYPE_UART) {
char *device; char *device;
int baud; int baud;
/* Check and parse connection parameters */ /* Check and parse connection parameters */
if (!attr_container_contain_key(args, "device") || if (!attr_container_contain_key(args, "device")
!attr_container_contain_key(args, "baudrate")) || !attr_container_contain_key(args, "baudrate"))
goto fail; goto fail;
device = attr_container_get_as_string(args, "device"); device = attr_container_get_as_string(args, "device");
baud = attr_container_get_as_int(args, "baudrate"); baud = attr_container_get_as_int(args, "baudrate");
@ -274,7 +291,8 @@ static uint32 _conn_open(wasm_module_inst_t module_inst,
/* Open device */ /* Open device */
if (!device || (fd = uart_open(device, baud)) == -1) if (!device || (fd = uart_open(device, baud)) == -1)
goto fail; goto fail;
} else { }
else {
goto fail; goto fail;
} }
@ -299,7 +317,8 @@ fail:
} }
/* --- connection lib function --- */ /* --- connection lib function --- */
static void _conn_close(uint32 handle) static void
_conn_close(uint32 handle)
{ {
sys_connection_t *conn = find_connection(handle, true); sys_connection_t *conn = find_connection(handle, true);
@ -311,7 +330,8 @@ static void _conn_close(uint32 handle)
} }
/* --- connection lib function --- */ /* --- connection lib function --- */
static int _conn_send(uint32 handle, const char *data, int len) static int
_conn_send(uint32 handle, const char *data, int len)
{ {
sys_connection_t *conn = find_connection(handle, false); sys_connection_t *conn = find_connection(handle, false);
@ -333,7 +353,8 @@ static int _conn_send(uint32 handle, const char *data, int len)
} }
/* --- connection lib function --- */ /* --- connection lib function --- */
static bool _conn_config(uint32 handle, attr_container_t *cfg) static bool
_conn_config(uint32 handle, attr_container_t *cfg)
{ {
sys_connection_t *conn = find_connection(handle, false); sys_connection_t *conn = find_connection(handle, false);
@ -346,8 +367,8 @@ static bool _conn_config(uint32 handle, attr_container_t *cfg)
struct sockaddr_in *addr; struct sockaddr_in *addr;
/* Parse remote address/port */ /* Parse remote address/port */
if (!attr_container_contain_key(cfg, "address") || if (!attr_container_contain_key(cfg, "address")
!attr_container_contain_key(cfg, "port")) || !attr_container_contain_key(cfg, "port"))
return false; return false;
if (!(address = attr_container_get_as_string(cfg, "address"))) if (!(address = attr_container_get_as_string(cfg, "address")))
return false; return false;
@ -365,7 +386,8 @@ static bool _conn_config(uint32 handle, attr_container_t *cfg)
/* Set remote address as connection arg */ /* Set remote address as connection arg */
conn->arg = addr; conn->arg = addr;
} else { }
else {
addr = (struct sockaddr_in *)conn->arg; addr = (struct sockaddr_in *)conn->arg;
addr->sin_addr.s_addr = inet_addr(address); addr->sin_addr.s_addr = inet_addr(address);
addr->sin_port = htons(port); addr->sin_port = htons(port);
@ -385,16 +407,16 @@ typedef struct connection_event {
uint32 len; uint32 len;
} connection_event_t; } connection_event_t;
static void connection_event_cleaner(connection_event_t *conn_event) static void
connection_event_cleaner(connection_event_t *conn_event)
{ {
if (conn_event->data != NULL) if (conn_event->data != NULL)
wasm_runtime_free(conn_event->data); wasm_runtime_free(conn_event->data);
wasm_runtime_free(conn_event); wasm_runtime_free(conn_event);
} }
static void post_msg_to_module(sys_connection_t *conn, static void
char *data, post_msg_to_module(sys_connection_t *conn, char *data, uint32 len)
uint32 len)
{ {
module_data *module = module_data_list_lookup_id(conn->module_id); module_data *module = module_data_list_lookup_id(conn->module_id);
char *data_copy = NULL; char *data_copy = NULL;
@ -404,7 +426,8 @@ static void post_msg_to_module(sys_connection_t *conn,
if (module == NULL) if (module == NULL)
return; return;
conn_data_event = (connection_event_t *)wasm_runtime_malloc(sizeof(*conn_data_event)); conn_data_event =
(connection_event_t *)wasm_runtime_malloc(sizeof(*conn_data_event));
if (conn_data_event == NULL) if (conn_data_event == NULL)
return; return;
@ -422,10 +445,8 @@ static void post_msg_to_module(sys_connection_t *conn,
conn_data_event->data = data_copy; conn_data_event->data = data_copy;
conn_data_event->len = len; conn_data_event->len = len;
msg = bh_new_msg(CONNECTION_EVENT_WASM, msg = bh_new_msg(CONNECTION_EVENT_WASM, conn_data_event,
conn_data_event, sizeof(*conn_data_event), connection_event_cleaner);
sizeof(*conn_data_event),
connection_event_cleaner);
if (!msg) { if (!msg) {
connection_event_cleaner(conn_data_event); connection_event_cleaner(conn_data_event);
return; return;
@ -434,7 +455,8 @@ static void post_msg_to_module(sys_connection_t *conn,
bh_post_msg2(module->queue, msg); bh_post_msg2(module->queue, msg);
} }
static void* polling_thread_routine (void *arg) static void *
polling_thread_routine(void *arg)
{ {
while (polling_thread_run) { while (polling_thread_run) {
int i, n; int i, n;
@ -445,8 +467,8 @@ static void* polling_thread_routine (void *arg)
continue; continue;
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
sys_connection_t *conn sys_connection_t *conn =
= (sys_connection_t *)epoll_events[i].data.ptr; (sys_connection_t *)epoll_events[i].data.ptr;
if (conn->type == CONN_TYPE_TCP) { if (conn->type == CONN_TYPE_TCP) {
int count = tcp_recv(conn->fd, io_buf, IO_BUF_SIZE); int count = tcp_recv(conn->fd, io_buf, IO_BUF_SIZE);
@ -454,15 +476,18 @@ static void* polling_thread_routine (void *arg)
/* Connection is closed by peer */ /* Connection is closed by peer */
post_msg_to_module(conn, NULL, 0); post_msg_to_module(conn, NULL, 0);
_conn_close(conn->handle); _conn_close(conn->handle);
} else { }
else {
/* Data is received */ /* Data is received */
post_msg_to_module(conn, io_buf, count); post_msg_to_module(conn, io_buf, count);
} }
} else if (conn->type == CONN_TYPE_UDP) { }
else if (conn->type == CONN_TYPE_UDP) {
int count = udp_recv(conn->fd, io_buf, IO_BUF_SIZE); int count = udp_recv(conn->fd, io_buf, IO_BUF_SIZE);
if (count > 0) if (count > 0)
post_msg_to_module(conn, io_buf, count); post_msg_to_module(conn, io_buf, count);
} else if (conn->type == CONN_TYPE_UART) { }
else if (conn->type == CONN_TYPE_UART) {
int count = uart_recv(conn->fd, io_buf, IO_BUF_SIZE); int count = uart_recv(conn->fd, io_buf, IO_BUF_SIZE);
if (count > 0) if (count > 0)
post_msg_to_module(conn, io_buf, count); post_msg_to_module(conn, io_buf, count);
@ -473,25 +498,26 @@ static void* polling_thread_routine (void *arg)
return NULL; return NULL;
} }
void app_mgr_connection_event_callback(module_data *m_data, bh_message_t msg) void
app_mgr_connection_event_callback(module_data *m_data, bh_message_t msg)
{ {
uint32 argv[3]; uint32 argv[3];
wasm_function_inst_t func_on_conn_data; wasm_function_inst_t func_on_conn_data;
bh_assert(CONNECTION_EVENT_WASM == bh_message_type(msg)); bh_assert(CONNECTION_EVENT_WASM == bh_message_type(msg));
wasm_data *wasm_app_data = (wasm_data*)m_data->internal_data; wasm_data *wasm_app_data = (wasm_data *)m_data->internal_data;
wasm_module_inst_t inst = wasm_app_data->wasm_module_inst; wasm_module_inst_t inst = wasm_app_data->wasm_module_inst;
connection_event_t *conn_event connection_event_t *conn_event =
= (connection_event_t *)bh_message_payload(msg); (connection_event_t *)bh_message_payload(msg);
int32 data_offset; int32 data_offset;
if (conn_event == NULL) if (conn_event == NULL)
return; return;
func_on_conn_data = wasm_runtime_lookup_function(inst, "_on_connection_data", func_on_conn_data = wasm_runtime_lookup_function(
"(i32i32i32)"); inst, "_on_connection_data", "(i32i32i32)");
if (!func_on_conn_data) if (!func_on_conn_data)
func_on_conn_data = wasm_runtime_lookup_function(inst, "on_connection_data", func_on_conn_data = wasm_runtime_lookup_function(
"(i32i32i32)"); inst, "on_connection_data", "(i32i32i32)");
if (!func_on_conn_data) { if (!func_on_conn_data) {
printf("Cannot find function on_connection_data\n"); printf("Cannot find function on_connection_data\n");
return; return;
@ -506,34 +532,31 @@ void app_mgr_connection_event_callback(module_data *m_data, bh_message_t msg)
3, argv)) { 3, argv)) {
const char *exception = wasm_runtime_get_exception(inst); const char *exception = wasm_runtime_get_exception(inst);
bh_assert(exception); bh_assert(exception);
printf(":Got exception running wasm code: %s\n", printf(":Got exception running wasm code: %s\n", exception);
exception);
wasm_runtime_clear_exception(inst); wasm_runtime_clear_exception(inst);
return; return;
} }
} else { }
data_offset = wasm_runtime_module_dup_data(inst, else {
conn_event->data, data_offset = wasm_runtime_module_dup_data(inst, conn_event->data,
conn_event->len); conn_event->len);
if (data_offset == 0) { if (data_offset == 0) {
const char *exception = wasm_runtime_get_exception(inst); const char *exception = wasm_runtime_get_exception(inst);
if (exception) { if (exception) {
printf("Got exception running wasm code: %s\n", printf("Got exception running wasm code: %s\n", exception);
exception);
wasm_runtime_clear_exception(inst); wasm_runtime_clear_exception(inst);
} }
return; return;
} }
argv[0] = conn_event->handle; argv[0] = conn_event->handle;
argv[1] = (uint32) data_offset; argv[1] = (uint32)data_offset;
argv[2] = conn_event->len; argv[2] = conn_event->len;
if (!wasm_runtime_call_wasm(wasm_app_data->exec_env, func_on_conn_data, if (!wasm_runtime_call_wasm(wasm_app_data->exec_env, func_on_conn_data,
3, argv)) { 3, argv)) {
const char *exception = wasm_runtime_get_exception(inst); const char *exception = wasm_runtime_get_exception(inst);
bh_assert(exception); bh_assert(exception);
printf(":Got exception running wasm code: %s\n", printf(":Got exception running wasm code: %s\n", exception);
exception);
wasm_runtime_clear_exception(inst); wasm_runtime_clear_exception(inst);
wasm_runtime_module_free(inst, data_offset); wasm_runtime_module_free(inst, data_offset);
return; return;
@ -542,7 +565,8 @@ void app_mgr_connection_event_callback(module_data *m_data, bh_message_t msg)
} }
} }
bool init_connection_framework() bool
init_connection_framework()
{ {
korp_tid tid; korp_tid tid;
@ -560,14 +584,13 @@ bool init_connection_framework()
} }
if (!wasm_register_msg_callback(CONNECTION_EVENT_WASM, if (!wasm_register_msg_callback(CONNECTION_EVENT_WASM,
app_mgr_connection_event_callback)) { app_mgr_connection_event_callback)) {
goto fail; goto fail;
} }
if (os_thread_create(&tid, if (os_thread_create(&tid, polling_thread_routine, NULL,
polling_thread_routine, BH_APPLET_PRESERVED_STACK_SIZE)
NULL, != 0) {
BH_APPLET_PRESERVED_STACK_SIZE) != 0) {
goto fail; goto fail;
} }
@ -579,7 +602,8 @@ fail:
return false; return false;
} }
void exit_connection_framework() void
exit_connection_framework()
{ {
polling_thread_run = false; polling_thread_run = false;
} }

View File

@ -12,12 +12,14 @@
#include "connection_lib.h" #include "connection_lib.h"
/* clang-format off */
/* /*
* Platform implementation of connection library * Platform implementation of connection library
*/ */
connection_interface_t connection_impl = { connection_interface_t connection_impl = {
._open = NULL, ._open = NULL,
._close = NULL, ._close = NULL,
._send = NULL, ._send = NULL,
._config = NULL ._config = NULL
}; };
/* clang-format on */

View File

@ -8,7 +8,7 @@
#include "sensor_api.h" #include "sensor_api.h"
typedef struct _sensor { typedef struct _sensor {
struct _sensor * next; struct _sensor *next;
char *name; char *name;
uint32 handle; uint32 handle;
void (*sensor_callback)(sensor_t, attr_container_t *, void *); void (*sensor_callback)(sensor_t, attr_container_t *, void *);
@ -17,16 +17,16 @@ typedef struct _sensor {
static sensor_t g_sensors = NULL; static sensor_t g_sensors = NULL;
sensor_t sensor_open(const char* name, int index, sensor_t
sensor_event_handler_f sensor_event_handler, sensor_open(const char *name, int index,
void *user_data) sensor_event_handler_f sensor_event_handler, void *user_data)
{ {
uint32 id = wasm_sensor_open(name, index); uint32 id = wasm_sensor_open(name, index);
if (id == -1) if (id == -1)
return NULL; return NULL;
//create local node for holding the user callback // create local node for holding the user callback
sensor_t sensor = (sensor_t) malloc(sizeof(struct _sensor)); sensor_t sensor = (sensor_t)malloc(sizeof(struct _sensor));
if (sensor == NULL) if (sensor == NULL)
return NULL; return NULL;
@ -43,7 +43,8 @@ sensor_t sensor_open(const char* name, int index,
if (g_sensors == NULL) { if (g_sensors == NULL) {
g_sensors = sensor; g_sensors = sensor;
} else { }
else {
sensor->next = g_sensors; sensor->next = g_sensors;
g_sensors = sensor; g_sensors = sensor;
} }
@ -51,7 +52,8 @@ sensor_t sensor_open(const char* name, int index,
return sensor; return sensor;
} }
bool sensor_config_with_attr_container(sensor_t sensor, attr_container_t *cfg) bool
sensor_config_with_attr_container(sensor_t sensor, attr_container_t *cfg)
{ {
char *buffer = (char *)cfg; char *buffer = (char *)cfg;
int len = attr_container_get_serialize_length(cfg); int len = attr_container_get_serialize_length(cfg);
@ -59,13 +61,15 @@ bool sensor_config_with_attr_container(sensor_t sensor, attr_container_t *cfg)
return wasm_sensor_config_with_attr_container(sensor->handle, buffer, len); 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
sensor_config(sensor_t sensor, int interval, int bit_cfg, int delay)
{ {
bool ret = wasm_sensor_config(sensor->handle, interval, bit_cfg, delay); bool ret = wasm_sensor_config(sensor->handle, interval, bit_cfg, delay);
return ret; return ret;
} }
bool sensor_close(sensor_t sensor) bool
sensor_close(sensor_t sensor)
{ {
wasm_sensor_close(sensor->handle); wasm_sensor_close(sensor->handle);
@ -76,13 +80,15 @@ bool sensor_close(sensor_t sensor)
if (s == sensor) { if (s == sensor) {
if (prev == NULL) { if (prev == NULL) {
g_sensors = s->next; g_sensors = s->next;
} else { }
else {
prev->next = s->next; prev->next = s->next;
} }
free(s->name); free(s->name);
free(s); free(s);
return true; return true;
} else { }
else {
prev = s; prev = s;
s = s->next; s = s->next;
} }
@ -97,9 +103,10 @@ bool sensor_close(sensor_t sensor)
* *
*/ */
void on_sensor_event(uint32 sensor_id, char * buffer, int len) void
on_sensor_event(uint32 sensor_id, char *buffer, int len)
{ {
attr_container_t * sensor_data = (attr_container_t *) buffer; attr_container_t *sensor_data = (attr_container_t *)buffer;
// lookup the sensor and call the handlers // lookup the sensor and call the handlers
sensor_t s = g_sensors; sensor_t s = g_sensors;

View File

@ -13,7 +13,7 @@ extern "C" {
#endif #endif
uint32 uint32
wasm_sensor_open(const char* name, int instance); wasm_sensor_open(const char *name, int instance);
bool bool
wasm_sensor_config(uint32 sensor, int interval, int bit_cfg, int delay); wasm_sensor_config(uint32 sensor, int interval, int bit_cfg, int delay);
@ -29,4 +29,3 @@ wasm_sensor_close(uint32 sensor);
#endif #endif
#endif /* end of _SENSOR_API_H_ */ #endif /* end of _SENSOR_API_H_ */

View File

@ -30,8 +30,8 @@ typedef struct _sensor *sensor_t;
* @see sensor_open * @see sensor_open
*/ */
typedef void (*sensor_event_handler_f)(sensor_t sensor, typedef void (*sensor_event_handler_f)(sensor_t sensor,
attr_container_t *sensor_event, attr_container_t *sensor_event,
void *user_data); void *user_data);
/* /*
***************** *****************
@ -49,10 +49,9 @@ typedef void (*sensor_event_handler_f)(sensor_t sensor,
* *
* @return the sensor opened if success, NULL otherwise * @return the sensor opened if success, NULL otherwise
*/ */
sensor_t sensor_open(const char* name, sensor_t
int index, sensor_open(const char *name, int index, sensor_event_handler_f handler,
sensor_event_handler_f handler, void *user_data);
void *user_data);
/** /**
* @brief Configure sensor with interval/bit_cfg/delay values. * @brief Configure sensor with interval/bit_cfg/delay values.
@ -64,7 +63,8 @@ sensor_t sensor_open(const char* name,
* *
* @return true if success, false otherwise * @return true if success, false otherwise
*/ */
bool sensor_config(sensor_t sensor, int interval, int bit_cfg, int delay); bool
sensor_config(sensor_t sensor, int interval, int bit_cfg, int delay);
/** /**
* @brief Configure sensor with attr_container_t object. * @brief Configure sensor with attr_container_t object.
@ -74,7 +74,8 @@ bool sensor_config(sensor_t sensor, int interval, int bit_cfg, int delay);
* *
* @return true if success, false otherwise * @return true if success, false otherwise
*/ */
bool sensor_config_with_attr_container(sensor_t sensor, attr_container_t *cfg); bool
sensor_config_with_attr_container(sensor_t sensor, attr_container_t *cfg);
/** /**
* @brief Close sensor. * @brief Close sensor.
@ -83,7 +84,8 @@ bool sensor_config_with_attr_container(sensor_t sensor, attr_container_t *cfg);
* *
* @return true if success, false otherwise * @return true if success, false otherwise
*/ */
bool sensor_close(sensor_t sensor); bool
sensor_close(sensor_t sensor);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -8,12 +8,12 @@
#include "module_wasm_app.h" #include "module_wasm_app.h"
#include "bh_platform.h" #include "bh_platform.h"
static sys_sensor_t * g_sys_sensors = NULL; static sys_sensor_t *g_sys_sensors = NULL;
static int g_sensor_id_max = 0; static int g_sensor_id_max = 0;
static sensor_client_t * static sensor_client_t *
find_sensor_client(sys_sensor_t * sensor, find_sensor_client(sys_sensor_t *sensor, unsigned int client_id,
unsigned int client_id, bool remove_if_found); bool remove_if_found);
void (*rechedule_sensor_callback)() = NULL; void (*rechedule_sensor_callback)() = NULL;
@ -38,30 +38,32 @@ sensor_event_cleaner(sensor_event_data_t *sensor_event)
static void static void
wasm_sensor_callback(void *client, uint32 sensor_id, void *user_data) 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 = (attr_container_t *)user_data;
attr_container_t *sensor_data_clone; attr_container_t *sensor_data_clone;
int sensor_data_len; int sensor_data_len;
sensor_event_data_t *sensor_event; sensor_event_data_t *sensor_event;
bh_message_t msg; bh_message_t msg;
sensor_client_t *c = (sensor_client_t *) client; sensor_client_t *c = (sensor_client_t *)client;
module_data *module = module_data_list_lookup_id(c->client_id); module_data *module = module_data_list_lookup_id(c->client_id);
if (module == NULL) if (module == NULL)
return; return;
if (sensor_data == NULL) if (sensor_data == NULL)
return; return;
sensor_data_len = attr_container_get_serialize_length(sensor_data); sensor_data_len = attr_container_get_serialize_length(sensor_data);
sensor_data_clone = (attr_container_t *)wasm_runtime_malloc(sensor_data_len); sensor_data_clone =
(attr_container_t *)wasm_runtime_malloc(sensor_data_len);
if (sensor_data_clone == NULL) if (sensor_data_clone == NULL)
return; return;
/* multiple sensor clients may use/free the sensor data, so make a copy */ /* multiple sensor clients may use/free the sensor data, so make a copy */
bh_memcpy_s(sensor_data_clone, sensor_data_len, bh_memcpy_s(sensor_data_clone, sensor_data_len, sensor_data,
sensor_data, sensor_data_len); sensor_data_len);
sensor_event = (sensor_event_data_t *)wasm_runtime_malloc(sizeof(*sensor_event)); sensor_event =
(sensor_event_data_t *)wasm_runtime_malloc(sizeof(*sensor_event));
if (sensor_event == NULL) { if (sensor_event == NULL) {
wasm_runtime_free(sensor_data_clone); wasm_runtime_free(sensor_data_clone);
return; return;
@ -72,9 +74,7 @@ wasm_sensor_callback(void *client, uint32 sensor_id, void *user_data)
sensor_event->data = sensor_data_clone; sensor_event->data = sensor_data_clone;
sensor_event->data_fmt = FMT_ATTR_CONTAINER; sensor_event->data_fmt = FMT_ATTR_CONTAINER;
msg = bh_new_msg(SENSOR_EVENT_WASM, msg = bh_new_msg(SENSOR_EVENT_WASM, sensor_event, sizeof(*sensor_event),
sensor_event,
sizeof(*sensor_event),
sensor_event_cleaner); sensor_event_cleaner);
if (!msg) { if (!msg) {
sensor_event_cleaner(sensor_event); sensor_event_cleaner(sensor_event);
@ -85,19 +85,18 @@ wasm_sensor_callback(void *client, uint32 sensor_id, void *user_data)
} }
bool bool
wasm_sensor_config(wasm_exec_env_t exec_env, wasm_sensor_config(wasm_exec_env_t exec_env, uint32 sensor, int interval,
uint32 sensor, int interval,
int bit_cfg, int delay) int bit_cfg, int delay)
{ {
wasm_module_inst_t module_inst = get_module_inst(exec_env); wasm_module_inst_t module_inst = get_module_inst(exec_env);
attr_container_t * attr_cont; attr_container_t *attr_cont;
sensor_client_t * c; sensor_client_t *c;
sensor_obj_t s = find_sys_sensor_id(sensor); sensor_obj_t s = find_sys_sensor_id(sensor);
if (s == NULL) if (s == NULL)
return false; return false;
unsigned int mod_id = app_manager_get_module_id(Module_WASM_App, unsigned int mod_id =
module_inst); app_manager_get_module_id(Module_WASM_App, module_inst);
bh_assert(mod_id != ID_NONE); bh_assert(mod_id != ID_NONE);
os_mutex_lock(&s->lock); os_mutex_lock(&s->lock);
@ -131,8 +130,7 @@ wasm_sensor_config(wasm_exec_env_t exec_env,
} }
uint32 uint32
wasm_sensor_open(wasm_exec_env_t exec_env, wasm_sensor_open(wasm_exec_env_t exec_env, char *name, int instance)
char *name, int instance)
{ {
wasm_module_inst_t module_inst = get_module_inst(exec_env); wasm_module_inst_t module_inst = get_module_inst(exec_env);
@ -142,8 +140,8 @@ wasm_sensor_open(wasm_exec_env_t exec_env,
if (s == NULL) if (s == NULL)
return -1; return -1;
unsigned int mod_id = app_manager_get_module_id(Module_WASM_App, unsigned int mod_id =
module_inst); app_manager_get_module_id(Module_WASM_App, module_inst);
bh_assert(mod_id != ID_NONE); bh_assert(mod_id != ID_NONE);
os_mutex_lock(&s->lock); os_mutex_lock(&s->lock);
@ -155,8 +153,8 @@ wasm_sensor_open(wasm_exec_env_t exec_env,
return -1; return -1;
} }
sensor_client_t * client = (sensor_client_t*) wasm_runtime_malloc( sensor_client_t *client =
sizeof(sensor_client_t)); (sensor_client_t *)wasm_runtime_malloc(sizeof(sensor_client_t));
if (client == NULL) { if (client == NULL) {
os_mutex_unlock(&s->lock); os_mutex_unlock(&s->lock);
return -1; return -1;
@ -182,8 +180,8 @@ wasm_sensor_open(wasm_exec_env_t exec_env,
} }
bool bool
wasm_sensor_config_with_attr_container(wasm_exec_env_t exec_env, wasm_sensor_config_with_attr_container(wasm_exec_env_t exec_env, uint32 sensor,
uint32 sensor, char *buffer, int len) char *buffer, int len)
{ {
if (buffer != NULL) { if (buffer != NULL) {
attr_container_t *cfg = (attr_container_t *)buffer; attr_container_t *cfg = (attr_container_t *)buffer;
@ -204,8 +202,8 @@ bool
wasm_sensor_close(wasm_exec_env_t exec_env, uint32 sensor) wasm_sensor_close(wasm_exec_env_t exec_env, uint32 sensor)
{ {
wasm_module_inst_t module_inst = get_module_inst(exec_env); wasm_module_inst_t module_inst = get_module_inst(exec_env);
unsigned int mod_id = app_manager_get_module_id(Module_WASM_App, unsigned int mod_id =
module_inst); app_manager_get_module_id(Module_WASM_App, module_inst);
unsigned int client_id = mod_id; unsigned int client_id = mod_id;
sensor_obj_t s = find_sys_sensor_id(sensor); sensor_obj_t s = find_sys_sensor_id(sensor);
sensor_client_t *c; sensor_client_t *c;
@ -232,19 +230,22 @@ wasm_sensor_close(wasm_exec_env_t exec_env, uint32 sensor)
* sensor framework API - don't expose to the applications * sensor framework API - don't expose to the applications
* *
*/ */
void set_sensor_reshceduler(void (*callback)()) void
set_sensor_reshceduler(void (*callback)())
{ {
rechedule_sensor_callback = callback; rechedule_sensor_callback = callback;
} }
// used for other threads to wakeup the sensor read thread // used for other threads to wakeup the sensor read thread
void reschedule_sensor_read() void
reschedule_sensor_read()
{ {
if (rechedule_sensor_callback) if (rechedule_sensor_callback)
rechedule_sensor_callback(); rechedule_sensor_callback();
} }
void refresh_read_interval(sensor_obj_t sensor) void
refresh_read_interval(sensor_obj_t sensor)
{ {
sensor_client_t *c; sensor_client_t *c;
uint32 interval = sensor->default_interval; uint32 interval = sensor->default_interval;
@ -266,10 +267,10 @@ void refresh_read_interval(sensor_obj_t sensor)
} }
sensor_obj_t sensor_obj_t
add_sys_sensor(char * name, char * description, int instance, add_sys_sensor(char *name, char *description, int instance,
uint32 default_interval, void * read_func, void * config_func) uint32 default_interval, void *read_func, void *config_func)
{ {
sys_sensor_t * s = (sys_sensor_t *) wasm_runtime_malloc(sizeof(sys_sensor_t)); sys_sensor_t *s = (sys_sensor_t *)wasm_runtime_malloc(sizeof(sys_sensor_t));
if (s == NULL) if (s == NULL)
return NULL; return NULL;
@ -302,7 +303,8 @@ add_sys_sensor(char * name, char * description, int instance,
if (g_sys_sensors == NULL) { if (g_sys_sensors == NULL) {
g_sys_sensors = s; g_sys_sensors = s;
} else { }
else {
s->next = g_sys_sensors; s->next = g_sys_sensors;
g_sys_sensors = s; g_sys_sensors = s;
} }
@ -312,9 +314,10 @@ add_sys_sensor(char * name, char * description, int instance,
return s; return s;
} }
sensor_obj_t find_sys_sensor(const char* name, int instance) sensor_obj_t
find_sys_sensor(const char *name, int instance)
{ {
sys_sensor_t * s = g_sys_sensors; sys_sensor_t *s = g_sys_sensors;
while (s) { while (s) {
if (strcmp(s->name, name) == 0 && s->sensor_instance == instance) if (strcmp(s->name, name) == 0 && s->sensor_instance == instance)
return s; return s;
@ -324,9 +327,10 @@ sensor_obj_t find_sys_sensor(const char* name, int instance)
return NULL; return NULL;
} }
sensor_obj_t find_sys_sensor_id(uint32 sensor_id) sensor_obj_t
find_sys_sensor_id(uint32 sensor_id)
{ {
sys_sensor_t * s = g_sys_sensors; sys_sensor_t *s = g_sys_sensors;
while (s) { while (s) {
if (s->sensor_id == sensor_id) if (s->sensor_id == sensor_id)
return s; return s;
@ -336,8 +340,9 @@ sensor_obj_t find_sys_sensor_id(uint32 sensor_id)
return NULL; return NULL;
} }
sensor_client_t *find_sensor_client(sys_sensor_t * sensor, sensor_client_t *
unsigned int client_id, bool remove_if_found) find_sensor_client(sys_sensor_t *sensor, unsigned int client_id,
bool remove_if_found)
{ {
sensor_client_t *prev = NULL, *c = sensor->clients; sensor_client_t *prev = NULL, *c = sensor->clients;
@ -351,7 +356,8 @@ sensor_client_t *find_sensor_client(sys_sensor_t * sensor,
sensor->clients = next; sensor->clients = next;
} }
return c; return c;
} else { }
else {
c = c->next; c = c->next;
} }
} }
@ -360,12 +366,13 @@ sensor_client_t *find_sensor_client(sys_sensor_t * sensor,
} }
// return the milliseconds to next check // return the milliseconds to next check
int check_sensor_timers() int
check_sensor_timers()
{ {
int ms_to_next_check = -1; int ms_to_next_check = -1;
uint32 now = (uint32)bh_get_tick_ms(); uint32 now = (uint32)bh_get_tick_ms();
sys_sensor_t * s = g_sys_sensors; sys_sensor_t *s = g_sys_sensors;
while (s) { while (s) {
uint32 last_read = s->last_read; uint32 last_read = s->last_read;
uint32 elpased_ms = bh_get_elpased_ms(&last_read); uint32 elpased_ms = bh_get_elpased_ms(&last_read);
@ -376,9 +383,9 @@ int check_sensor_timers()
} }
if (elpased_ms >= s->read_interval) { if (elpased_ms >= s->read_interval) {
attr_container_t * data = s->read(s); attr_container_t *data = s->read(s);
if (data) { if (data) {
sensor_client_t * client = s->clients; sensor_client_t *client = s->clients;
while (client) { while (client) {
client->client_callback(client, s->sensor_id, data); client->client_callback(client, s->sensor_id, data);
client = client->next; client = client->next;
@ -390,11 +397,11 @@ int check_sensor_timers()
if (ms_to_next_check == -1 || (ms_to_next_check < s->read_interval)) if (ms_to_next_check == -1 || (ms_to_next_check < s->read_interval))
ms_to_next_check = s->read_interval; ms_to_next_check = s->read_interval;
} else { }
else {
int remaining = s->read_interval - elpased_ms; int remaining = s->read_interval - elpased_ms;
if (ms_to_next_check == -1 || (ms_to_next_check < remaining)) if (ms_to_next_check == -1 || (ms_to_next_check < remaining))
ms_to_next_check = remaining; ms_to_next_check = remaining;
} }
s = s->next; s = s->next;
@ -403,9 +410,10 @@ int check_sensor_timers()
return ms_to_next_check; return ms_to_next_check;
} }
void sensor_cleanup_callback(uint32 module_id) void
sensor_cleanup_callback(uint32 module_id)
{ {
sys_sensor_t * s = g_sys_sensors; sys_sensor_t *s = g_sys_sensors;
while (s) { while (s) {
sensor_client_t *c; sensor_client_t *c;

View File

@ -12,49 +12,58 @@
#include "sensor_native_api.h" #include "sensor_native_api.h"
struct _sys_sensor; struct _sys_sensor;
typedef struct _sys_sensor* sensor_obj_t; typedef struct _sys_sensor *sensor_obj_t;
typedef struct _sensor_client { typedef struct _sensor_client {
struct _sensor_client * next; struct _sensor_client *next;
unsigned int client_id; // the app id unsigned int client_id; // the app id
int interval; int interval;
int bit_cfg; int bit_cfg;
int delay; int delay;
void (*client_callback)(void * client, uint32, attr_container_t *); void (*client_callback)(void *client, uint32, attr_container_t *);
} sensor_client_t; } sensor_client_t;
typedef struct _sys_sensor { typedef struct _sys_sensor {
struct _sys_sensor * next; struct _sys_sensor *next;
char * name; char *name;
int sensor_instance; int sensor_instance;
char * description; char *description;
uint32 sensor_id; uint32 sensor_id;
sensor_client_t * clients; sensor_client_t *clients;
/* app, sensor mgr and app mgr may access the clients at the same time, /* app, sensor mgr and app mgr may access the clients at the same time,
* so need a lock to protect the clients */ so need a lock to protect the clients */
korp_mutex lock; korp_mutex lock;
uint32 last_read; uint32 last_read;
uint32 read_interval; uint32 read_interval;
uint32 default_interval; uint32 default_interval;
attr_container_t * (*read)(void *); /* TODO: may support other type return value, such as 'cbor' */ /* TODO: may support other type return value, such as 'cbor' */
attr_container_t *(*read)(void *);
bool (*config)(void *, void *); bool (*config)(void *, void *);
} sys_sensor_t; } sys_sensor_t;
sensor_obj_t add_sys_sensor(char * name, char * description, int instance, sensor_obj_t
uint32 default_interval, void * read_func, void * config_func); add_sys_sensor(char *name, char *description, int instance,
sensor_obj_t find_sys_sensor(const char* name, int instance); uint32 default_interval, void *read_func, void *config_func);
sensor_obj_t find_sys_sensor_id(uint32 sensor_id); sensor_obj_t
void refresh_read_interval(sensor_obj_t sensor); find_sys_sensor(const char *name, int instance);
void sensor_cleanup_callback(uint32 module_id); sensor_obj_t
int check_sensor_timers(); find_sys_sensor_id(uint32 sensor_id);
void reschedule_sensor_read(); void
refresh_read_interval(sensor_obj_t sensor);
void init_sensor_framework(); void
void start_sensor_framework(); sensor_cleanup_callback(uint32 module_id);
void exit_sensor_framework(); int
check_sensor_timers();
void
reschedule_sensor_read();
void
init_sensor_framework();
void
start_sensor_framework();
void
exit_sensor_framework();
#endif /* LIB_EXTENSION_RUNTIME_SENSOR_H_ */ #endif /* LIB_EXTENSION_RUNTIME_SENSOR_H_ */

View File

@ -19,60 +19,62 @@ static korp_cond cond;
static korp_mutex mutex; static korp_mutex mutex;
static bool sensor_check_thread_run = true; static bool sensor_check_thread_run = true;
void app_mgr_sensor_event_callback(module_data *m_data, bh_message_t msg) void
app_mgr_sensor_event_callback(module_data *m_data, bh_message_t msg)
{ {
uint32 argv[3]; uint32 argv[3];
wasm_function_inst_t func_onSensorEvent; wasm_function_inst_t func_onSensorEvent;
bh_assert(SENSOR_EVENT_WASM == bh_message_type(msg)); bh_assert(SENSOR_EVENT_WASM == bh_message_type(msg));
wasm_data *wasm_app_data = (wasm_data*)m_data->internal_data; wasm_data *wasm_app_data = (wasm_data *)m_data->internal_data;
wasm_module_inst_t inst = wasm_app_data->wasm_module_inst; wasm_module_inst_t inst = wasm_app_data->wasm_module_inst;
sensor_event_data_t *payload = (sensor_event_data_t*) sensor_event_data_t *payload =
bh_message_payload(msg); (sensor_event_data_t *)bh_message_payload(msg);
if (payload == NULL) if (payload == NULL)
return; return;
func_onSensorEvent = wasm_runtime_lookup_function(inst, "_on_sensor_event", func_onSensorEvent =
"(i32i32i32)"); wasm_runtime_lookup_function(inst, "_on_sensor_event", "(i32i32i32)");
if (!func_onSensorEvent) if (!func_onSensorEvent)
func_onSensorEvent = wasm_runtime_lookup_function(inst, "on_sensor_event", func_onSensorEvent = wasm_runtime_lookup_function(
"(i32i32i32)"); inst, "on_sensor_event", "(i32i32i32)");
if (!func_onSensorEvent) { if (!func_onSensorEvent) {
printf("Cannot find function on_sensor_event\n"); printf("Cannot find function on_sensor_event\n");
} else { }
else {
int32 sensor_data_offset; int32 sensor_data_offset;
uint32 sensor_data_len; uint32 sensor_data_len;
if (payload->data_fmt == FMT_ATTR_CONTAINER) { if (payload->data_fmt == FMT_ATTR_CONTAINER) {
sensor_data_len = attr_container_get_serialize_length(payload->data); sensor_data_len =
} else { attr_container_get_serialize_length(payload->data);
}
else {
printf("Unsupported sensor data format: %d\n", payload->data_fmt); printf("Unsupported sensor data format: %d\n", payload->data_fmt);
return; return;
} }
sensor_data_offset = wasm_runtime_module_dup_data(inst, payload->data, sensor_data_offset =
sensor_data_len); wasm_runtime_module_dup_data(inst, payload->data, sensor_data_len);
if (sensor_data_offset == 0) { if (sensor_data_offset == 0) {
const char *exception = wasm_runtime_get_exception(inst); const char *exception = wasm_runtime_get_exception(inst);
if (exception) { if (exception) {
printf("Got exception running wasm code: %s\n", printf("Got exception running wasm code: %s\n", exception);
exception);
wasm_runtime_clear_exception(inst); wasm_runtime_clear_exception(inst);
} }
return; return;
} }
argv[0] = payload->sensor_id; argv[0] = payload->sensor_id;
argv[1] = (uint32) sensor_data_offset; argv[1] = (uint32)sensor_data_offset;
argv[2] = sensor_data_len; argv[2] = sensor_data_len;
if (!wasm_runtime_call_wasm(wasm_app_data->exec_env, func_onSensorEvent, if (!wasm_runtime_call_wasm(wasm_app_data->exec_env, func_onSensorEvent,
3, argv)) { 3, argv)) {
const char *exception = wasm_runtime_get_exception(inst); const char *exception = wasm_runtime_get_exception(inst);
bh_assert(exception); bh_assert(exception);
printf(":Got exception running wasm code: %s\n", printf(":Got exception running wasm code: %s\n", exception);
exception);
wasm_runtime_clear_exception(inst); wasm_runtime_clear_exception(inst);
wasm_runtime_module_free(inst, sensor_data_offset); wasm_runtime_module_free(inst, sensor_data_offset);
return; return;
@ -82,8 +84,8 @@ void app_mgr_sensor_event_callback(module_data *m_data, bh_message_t msg)
} }
} }
static void
static void thread_sensor_check(void * arg) thread_sensor_check(void *arg)
{ {
while (sensor_check_thread_run) { while (sensor_check_thread_run) {
int ms_to_expiry = check_sensor_timers(); int ms_to_expiry = check_sensor_timers();
@ -95,46 +97,44 @@ static void thread_sensor_check(void * arg)
} }
} }
static void cb_wakeup_thread() static void
cb_wakeup_thread()
{ {
os_cond_signal(&cond); os_cond_signal(&cond);
} }
void set_sensor_reshceduler(void (*callback)()); void
set_sensor_reshceduler(void (*callback)());
void init_sensor_framework() void
init_sensor_framework()
{ {
// init the mutext and conditions // init the mutext and conditions
os_cond_init(&cond); os_cond_init(&cond);
os_mutex_init(&mutex); os_mutex_init(&mutex);
set_sensor_reshceduler(cb_wakeup_thread); set_sensor_reshceduler(cb_wakeup_thread);
wasm_register_msg_callback(SENSOR_EVENT_WASM, wasm_register_msg_callback(SENSOR_EVENT_WASM,
app_mgr_sensor_event_callback); app_mgr_sensor_event_callback);
wasm_register_cleanup_callback(sensor_cleanup_callback); wasm_register_cleanup_callback(sensor_cleanup_callback);
} }
void start_sensor_framework() void
start_sensor_framework()
{ {
korp_tid tid; korp_tid tid;
os_thread_create(&tid, os_thread_create(&tid, (void *)thread_sensor_check, NULL,
(void *)thread_sensor_check, BH_APPLET_PRESERVED_STACK_SIZE);
NULL,
BH_APPLET_PRESERVED_STACK_SIZE);
} }
void
void exit_sensor_framework() exit_sensor_framework()
{ {
sensor_check_thread_run = false; sensor_check_thread_run = false;
reschedule_sensor_read(); reschedule_sensor_read();
//todo: wait the sensor thread termination // todo: wait the sensor thread termination
} }

View File

@ -14,16 +14,14 @@ extern "C" {
#endif #endif
bool bool
wasm_sensor_config(wasm_exec_env_t exec_env, wasm_sensor_config(wasm_exec_env_t exec_env, uint32 sensor, int interval,
uint32 sensor, int interval,
int bit_cfg, int delay); int bit_cfg, int delay);
uint32 uint32
wasm_sensor_open(wasm_exec_env_t exec_env, wasm_sensor_open(wasm_exec_env_t exec_env, char *name, int instance);
char *name, int instance);
bool bool
wasm_sensor_config_with_attr_container(wasm_exec_env_t exec_env, wasm_sensor_config_with_attr_container(wasm_exec_env_t exec_env, uint32 sensor,
uint32 sensor, char *buffer, int len); char *buffer, int len);
bool bool
wasm_sensor_close(wasm_exec_env_t exec_env, uint32 sensor); wasm_sensor_close(wasm_exec_env_t exec_env, uint32 sensor);
@ -33,4 +31,3 @@ wasm_sensor_close(wasm_exec_env_t exec_env, uint32 sensor);
#endif #endif
#endif /* end of _SENSOR_NATIVE_API_H_ */ #endif /* end of _SENSOR_NATIVE_API_H_ */

View File

@ -28,10 +28,8 @@ wasm_cb_native_call(int32 func_id, uint32 *argv, uint32 argc);
void void
wasm_list_native_call(int32 func_id, uint32 *argv, uint32 argc); wasm_list_native_call(int32 func_id, uint32 *argv, uint32 argc);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif /* end of _GUI_API_H_ */ #endif /* end of _GUI_API_H_ */

View File

@ -7,12 +7,13 @@
#include "bh_platform.h" #include "bh_platform.h"
#include "gui_api.h" #include "gui_api.h"
#define ARGC sizeof(argv)/sizeof(uint32) #define ARGC sizeof(argv) / sizeof(uint32)
#define CALL_BTN_NATIVE_FUNC(id) wasm_btn_native_call(id, argv, ARGC) #define CALL_BTN_NATIVE_FUNC(id) wasm_btn_native_call(id, argv, ARGC)
lv_obj_t * lv_btn_create(lv_obj_t * par, const lv_obj_t * copy) lv_obj_t *
lv_btn_create(lv_obj_t *par, const lv_obj_t *copy)
{ {
uint32 argv[2] = {0}; uint32 argv[2] = { 0 };
argv[0] = (uint32)par; argv[0] = (uint32)par;
argv[1] = (uint32)copy; argv[1] = (uint32)copy;
@ -20,100 +21,113 @@ lv_obj_t * lv_btn_create(lv_obj_t * par, const lv_obj_t * copy)
return (lv_obj_t *)argv[0]; return (lv_obj_t *)argv[0];
} }
void lv_btn_set_toggle(lv_obj_t * btn, bool tgl) void
lv_btn_set_toggle(lv_obj_t *btn, bool tgl)
{ {
uint32 argv[2] = {0}; uint32 argv[2] = { 0 };
argv[0] = (uint32)btn; argv[0] = (uint32)btn;
argv[1] = tgl; argv[1] = tgl;
CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_SET_TOGGLE); CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_SET_TOGGLE);
} }
void lv_btn_set_state(lv_obj_t * btn, lv_btn_state_t state) void
lv_btn_set_state(lv_obj_t *btn, lv_btn_state_t state)
{ {
uint32 argv[2] = {0}; uint32 argv[2] = { 0 };
argv[0] = (uint32)btn; argv[0] = (uint32)btn;
argv[1] = state; argv[1] = state;
CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_SET_STATE); CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_SET_STATE);
} }
void lv_btn_toggle(lv_obj_t * btn) void
lv_btn_toggle(lv_obj_t *btn)
{ {
uint32 argv[1] = {0}; uint32 argv[1] = { 0 };
argv[0] = (uint32)btn; argv[0] = (uint32)btn;
CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_TOGGLE); CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_TOGGLE);
} }
void lv_btn_set_ink_in_time(lv_obj_t * btn, uint16_t time) void
lv_btn_set_ink_in_time(lv_obj_t *btn, uint16_t time)
{ {
uint32 argv[2] = {0}; uint32 argv[2] = { 0 };
argv[0] = (uint32)btn; argv[0] = (uint32)btn;
argv[1] = time; argv[1] = time;
CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_SET_INK_IN_TIME); CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_SET_INK_IN_TIME);
} }
void lv_btn_set_ink_wait_time(lv_obj_t * btn, uint16_t time) void
lv_btn_set_ink_wait_time(lv_obj_t *btn, uint16_t time)
{ {
uint32 argv[2] = {0}; uint32 argv[2] = { 0 };
argv[0] = (uint32)btn; argv[0] = (uint32)btn;
argv[1] = time; argv[1] = time;
CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_SET_INK_WAIT_TIME); CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_SET_INK_WAIT_TIME);
} }
void lv_btn_set_ink_out_time(lv_obj_t * btn, uint16_t time) void
lv_btn_set_ink_out_time(lv_obj_t *btn, uint16_t time)
{ {
uint32 argv[2] = {0}; uint32 argv[2] = { 0 };
argv[0] = (uint32)btn; argv[0] = (uint32)btn;
argv[1] = time; argv[1] = time;
CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_SET_INK_OUT_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) // void wgl_btn_set_style(wgl_obj_t btn, wgl_btn_style_t type,
// const wgl_style_t *style)
//{ //{
// //TODO: pack style // //TODO: pack style
// //wasm_btn_set_style(btn, type, style); // //wasm_btn_set_style(btn, type, style);
//} //}
// //
lv_btn_state_t lv_btn_get_state(const lv_obj_t * btn) lv_btn_state_t
lv_btn_get_state(const lv_obj_t *btn)
{ {
uint32 argv[1] = {0}; uint32 argv[1] = { 0 };
argv[0] = (uint32)btn; argv[0] = (uint32)btn;
CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_GET_STATE); CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_GET_STATE);
return (lv_btn_state_t)argv[0]; return (lv_btn_state_t)argv[0];
} }
bool lv_btn_get_toggle(const lv_obj_t * btn) bool
lv_btn_get_toggle(const lv_obj_t *btn)
{ {
uint32 argv[1] = {0}; uint32 argv[1] = { 0 };
argv[0] = (uint32)btn; argv[0] = (uint32)btn;
CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_GET_TOGGLE); CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_GET_TOGGLE);
return (bool)argv[0]; return (bool)argv[0];
} }
uint16_t lv_btn_get_ink_in_time(const lv_obj_t * btn) uint16_t
lv_btn_get_ink_in_time(const lv_obj_t *btn)
{ {
uint32 argv[1] = {0}; uint32 argv[1] = { 0 };
argv[0] = (uint32)btn; argv[0] = (uint32)btn;
CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_GET_INK_IN_TIME); CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_GET_INK_IN_TIME);
return (uint16_t)argv[0]; return (uint16_t)argv[0];
} }
uint16_t lv_btn_get_ink_wait_time(const lv_obj_t * btn) uint16_t
lv_btn_get_ink_wait_time(const lv_obj_t *btn)
{ {
uint32 argv[1] = {0}; uint32 argv[1] = { 0 };
argv[0] = (uint32)btn; argv[0] = (uint32)btn;
CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_GET_INK_WAIT_TIME); CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_GET_INK_WAIT_TIME);
return (uint16_t)argv[0]; return (uint16_t)argv[0];
} }
uint16_t lv_btn_get_ink_out_time(const lv_obj_t * btn) uint16_t
lv_btn_get_ink_out_time(const lv_obj_t *btn)
{ {
uint32 argv[1] = {0}; uint32 argv[1] = { 0 };
argv[0] = (uint32)btn; argv[0] = (uint32)btn;
CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_GET_INK_OUT_TIME); CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_GET_INK_OUT_TIME);
return (uint16_t)argv[0]; return (uint16_t)argv[0];
} }
// //
//const wgl_style_t * wgl_btn_get_style(const wgl_obj_t btn, wgl_btn_style_t type) // const wgl_style_t * wgl_btn_get_style(const wgl_obj_t btn,
// wgl_btn_style_t type)
//{ //{
// //TODO: pack style // //TODO: pack style
// //wasm_btn_get_style(btn, type); // //wasm_btn_get_style(btn, type);

View File

@ -3,18 +3,18 @@
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/ */
#include "wa-inc/lvgl/lvgl.h" #include "wa-inc/lvgl/lvgl.h"
#include "gui_api.h" #include "gui_api.h"
#include <string.h> #include <string.h>
#define ARGC sizeof(argv)/sizeof(uint32) #define ARGC sizeof(argv) / sizeof(uint32)
#define CALL_CB_NATIVE_FUNC(id) wasm_cb_native_call(id, argv, ARGC) #define CALL_CB_NATIVE_FUNC(id) wasm_cb_native_call(id, argv, ARGC)
lv_obj_t * lv_cb_create(lv_obj_t * par, const lv_obj_t * copy) lv_obj_t *
lv_cb_create(lv_obj_t *par, const lv_obj_t *copy)
{ {
uint32 argv[2] = {0}; uint32 argv[2] = { 0 };
argv[0] = (uint32)par; argv[0] = (uint32)par;
argv[1] = (uint32)copy; argv[1] = (uint32)copy;
@ -22,41 +22,46 @@ lv_obj_t * lv_cb_create(lv_obj_t * par, const lv_obj_t * copy)
return (lv_obj_t *)argv[0]; return (lv_obj_t *)argv[0];
} }
void lv_cb_set_text(lv_obj_t * cb, const char * txt) void
lv_cb_set_text(lv_obj_t *cb, const char *txt)
{ {
uint32 argv[3] = {0}; uint32 argv[3] = { 0 };
argv[0] = (uint32)cb; argv[0] = (uint32)cb;
argv[1] = (uint32)txt; argv[1] = (uint32)txt;
argv[2] = strlen(txt) + 1; argv[2] = strlen(txt) + 1;
CALL_CB_NATIVE_FUNC(CB_FUNC_ID_SET_TEXT); CALL_CB_NATIVE_FUNC(CB_FUNC_ID_SET_TEXT);
} }
void lv_cb_set_static_text(lv_obj_t * cb, const char * txt) void
lv_cb_set_static_text(lv_obj_t *cb, const char *txt)
{ {
uint32 argv[3] = {0}; uint32 argv[3] = { 0 };
argv[0] = (uint32)cb; argv[0] = (uint32)cb;
argv[1] = (uint32)txt; argv[1] = (uint32)txt;
argv[2] = strlen(txt) + 1; argv[2] = strlen(txt) + 1;
CALL_CB_NATIVE_FUNC(CB_FUNC_ID_SET_STATIC_TEXT); 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) // void wgl_cb_set_style(wgl_obj_t cb, wgl_cb_style_t type,
// const wgl_style_t *style)
//{ //{
// //TODO: // //TODO:
//} //}
// //
static unsigned int wgl_cb_get_text_length(lv_obj_t * cb) static unsigned int
wgl_cb_get_text_length(lv_obj_t *cb)
{ {
uint32 argv[1] = {0}; uint32 argv[1] = { 0 };
argv[0] = (uint32)cb; argv[0] = (uint32)cb;
CALL_CB_NATIVE_FUNC(CB_FUNC_ID_GET_TEXT_LENGTH); CALL_CB_NATIVE_FUNC(CB_FUNC_ID_GET_TEXT_LENGTH);
return argv[0]; return argv[0];
} }
static char *wgl_cb_get_text(lv_obj_t * cb, char *buffer, int buffer_len) static char *
wgl_cb_get_text(lv_obj_t *cb, char *buffer, int buffer_len)
{ {
uint32 argv[3] = {0}; uint32 argv[3] = { 0 };
argv[0] = (uint32)cb; argv[0] = (uint32)cb;
argv[1] = (uint32)buffer; argv[1] = (uint32)buffer;
argv[2] = buffer_len; argv[2] = buffer_len;
@ -65,18 +70,17 @@ static char *wgl_cb_get_text(lv_obj_t * cb, char *buffer, int buffer_len)
} }
// TODO: need to use a global data buffer for the returned text // TODO: need to use a global data buffer for the returned text
const char * lv_cb_get_text(const lv_obj_t * cb) const char *
lv_cb_get_text(const lv_obj_t *cb)
{ {
return NULL; return NULL;
} }
// const wgl_style_t * wgl_cb_get_style(const wgl_obj_t cb,
//const wgl_style_t * wgl_cb_get_style(const wgl_obj_t cb, wgl_cb_style_t type) // wgl_cb_style_t type)
//{ //{
// //TODO // //TODO
// return NULL; // return NULL;
//} //}
// //

View File

@ -3,19 +3,17 @@
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/ */
#include "wa-inc/lvgl/lvgl.h" #include "wa-inc/lvgl/lvgl.h"
#include "gui_api.h" #include "gui_api.h"
#include <string.h> #include <string.h>
#define ARGC sizeof(argv) / sizeof(uint32)
#define ARGC sizeof(argv)/sizeof(uint32)
#define CALL_LABEL_NATIVE_FUNC(id) wasm_label_native_call(id, argv, ARGC) #define CALL_LABEL_NATIVE_FUNC(id) wasm_label_native_call(id, argv, ARGC)
lv_obj_t * lv_label_create(lv_obj_t * par, const lv_obj_t * copy) lv_obj_t *
lv_label_create(lv_obj_t *par, const lv_obj_t *copy)
{ {
uint32 argv[2] = {0}; uint32 argv[2] = { 0 };
argv[0] = (uint32)par; argv[0] = (uint32)par;
argv[1] = (uint32)copy; argv[1] = (uint32)copy;
@ -23,109 +21,112 @@ lv_obj_t * lv_label_create(lv_obj_t * par, const lv_obj_t * copy)
return (lv_obj_t *)argv[0]; return (lv_obj_t *)argv[0];
} }
void lv_label_set_text(lv_obj_t * label, const char * text) void
lv_label_set_text(lv_obj_t *label, const char *text)
{ {
uint32 argv[3] = {0}; uint32 argv[3] = { 0 };
argv[0] = (uint32)label; argv[0] = (uint32)label;
argv[1] = (uint32)text; argv[1] = (uint32)text;
argv[2] = strlen(text) + 1; argv[2] = strlen(text) + 1;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_TEXT); CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_TEXT);
} }
void
void lv_label_set_array_text(lv_obj_t * label, const char * array, uint16_t size) lv_label_set_array_text(lv_obj_t *label, const char *array, uint16_t size)
{ {
uint32 argv[3] = {0}; uint32 argv[3] = { 0 };
argv[0] = (uint32)label; argv[0] = (uint32)label;
argv[1] = (uint32)array; argv[1] = (uint32)array;
argv[2] = size; argv[2] = size;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_ARRAY_TEXT); CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_ARRAY_TEXT);
} }
void
void lv_label_set_static_text(lv_obj_t * label, const char * text) lv_label_set_static_text(lv_obj_t *label, const char *text)
{ {
uint32 argv[3] = {0}; uint32 argv[3] = { 0 };
argv[0] = (uint32)label; argv[0] = (uint32)label;
argv[1] = (uint32)text; argv[1] = (uint32)text;
argv[2] = strlen(text) + 1; argv[2] = strlen(text) + 1;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_STATIC_TEXT); CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_STATIC_TEXT);
} }
void
void lv_label_set_long_mode(lv_obj_t * label, lv_label_long_mode_t long_mode) lv_label_set_long_mode(lv_obj_t *label, lv_label_long_mode_t long_mode)
{ {
uint32 argv[2] = {0}; uint32 argv[2] = { 0 };
argv[0] = (uint32)label; argv[0] = (uint32)label;
argv[1] = long_mode; argv[1] = long_mode;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_LONG_MODE); CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_LONG_MODE);
} }
void
void lv_label_set_align(lv_obj_t * label, lv_label_align_t align) lv_label_set_align(lv_obj_t *label, lv_label_align_t align)
{ {
uint32 argv[2] = {0}; uint32 argv[2] = { 0 };
argv[0] = (uint32)label; argv[0] = (uint32)label;
argv[1] = align; argv[1] = align;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_ALIGN); CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_ALIGN);
} }
void
void lv_label_set_recolor(lv_obj_t * label, bool en) lv_label_set_recolor(lv_obj_t *label, bool en)
{ {
uint32 argv[2] = {0}; uint32 argv[2] = { 0 };
argv[0] = (uint32)label; argv[0] = (uint32)label;
argv[1] = en; argv[1] = en;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_RECOLOR); CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_RECOLOR);
} }
void
void lv_label_set_body_draw(lv_obj_t * label, bool en) lv_label_set_body_draw(lv_obj_t *label, bool en)
{ {
uint32 argv[2] = {0}; uint32 argv[2] = { 0 };
argv[0] = (uint32)label; argv[0] = (uint32)label;
argv[1] = en; argv[1] = en;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_BODY_DRAW); CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_BODY_DRAW);
} }
void
void lv_label_set_anim_speed(lv_obj_t * label, uint16_t anim_speed) lv_label_set_anim_speed(lv_obj_t *label, uint16_t anim_speed)
{ {
uint32 argv[2] = {0}; uint32 argv[2] = { 0 };
argv[0] = (uint32)label; argv[0] = (uint32)label;
argv[1] = anim_speed; argv[1] = anim_speed;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_ANIM_SPEED); CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_ANIM_SPEED);
} }
void
void lv_label_set_text_sel_start(lv_obj_t * label, uint16_t index) lv_label_set_text_sel_start(lv_obj_t *label, uint16_t index)
{ {
uint32 argv[2] = {0}; uint32 argv[2] = { 0 };
argv[0] = (uint32)label; argv[0] = (uint32)label;
argv[1] = index; argv[1] = index;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_TEXT_SEL_START); CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_TEXT_SEL_START);
} }
void
void lv_label_set_text_sel_end(lv_obj_t * label, uint16_t index) lv_label_set_text_sel_end(lv_obj_t *label, uint16_t index)
{ {
uint32 argv[2] = {0}; uint32 argv[2] = { 0 };
argv[0] = (uint32)label; argv[0] = (uint32)label;
argv[1] = index; argv[1] = index;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_TEXT_SEL_END); CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_TEXT_SEL_END);
} }
unsigned int wgl_label_get_text_length(lv_obj_t * label) unsigned int
wgl_label_get_text_length(lv_obj_t *label)
{ {
uint32 argv[1] = {0}; uint32 argv[1] = { 0 };
argv[0] = (uint32)label; argv[0] = (uint32)label;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_TEXT_LENGTH); CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_TEXT_LENGTH);
return argv[0]; return argv[0];
} }
char * wgl_label_get_text(lv_obj_t * label, char *buffer, int buffer_len) char *
wgl_label_get_text(lv_obj_t *label, char *buffer, int buffer_len)
{ {
uint32 argv[3] = {0}; uint32 argv[3] = { 0 };
argv[0] = (uint32)label; argv[0] = (uint32)label;
argv[1] = (uint32)buffer; argv[1] = (uint32)buffer;
argv[2] = buffer_len; argv[2] = buffer_len;
@ -133,63 +134,63 @@ char * wgl_label_get_text(lv_obj_t * label, char *buffer, int buffer_len)
return (char *)argv[0]; return (char *)argv[0];
} }
// TODO: // TODO:
char * lv_label_get_text(const lv_obj_t * label) char *
lv_label_get_text(const lv_obj_t *label)
{ {
return NULL; return NULL;
} }
lv_label_long_mode_t
lv_label_long_mode_t lv_label_get_long_mode(const lv_obj_t * label) lv_label_get_long_mode(const lv_obj_t *label)
{ {
uint32 argv[1] = {0}; uint32 argv[1] = { 0 };
argv[0] = (uint32)label; argv[0] = (uint32)label;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_LONG_MODE); CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_LONG_MODE);
return (lv_label_long_mode_t)argv[0]; return (lv_label_long_mode_t)argv[0];
} }
lv_label_align_t
lv_label_align_t lv_label_get_align(const lv_obj_t * label) lv_label_get_align(const lv_obj_t *label)
{ {
uint32 argv[1] = {0}; uint32 argv[1] = { 0 };
argv[0] = (uint32)label; argv[0] = (uint32)label;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_ALIGN); CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_ALIGN);
return (lv_label_align_t)argv[0]; return (lv_label_align_t)argv[0];
} }
bool
bool lv_label_get_recolor(const lv_obj_t * label) lv_label_get_recolor(const lv_obj_t *label)
{ {
uint32 argv[1] = {0}; uint32 argv[1] = { 0 };
argv[0] = (uint32)label; argv[0] = (uint32)label;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_RECOLOR); CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_RECOLOR);
return (bool)argv[0]; return (bool)argv[0];
} }
bool
bool lv_label_get_body_draw(const lv_obj_t * label) lv_label_get_body_draw(const lv_obj_t *label)
{ {
uint32 argv[1] = {0}; uint32 argv[1] = { 0 };
argv[0] = (uint32)label; argv[0] = (uint32)label;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_BODY_DRAW); CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_BODY_DRAW);
return (bool)argv[0]; return (bool)argv[0];
} }
uint16_t
uint16_t lv_label_get_anim_speed(const lv_obj_t * label) lv_label_get_anim_speed(const lv_obj_t *label)
{ {
uint32 argv[1] = {0}; uint32 argv[1] = { 0 };
argv[0] = (uint32)label; argv[0] = (uint32)label;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_ANIM_SPEED); CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_ANIM_SPEED);
return (uint16_t)argv[0]; return (uint16_t)argv[0];
} }
void
void lv_label_get_letter_pos(const lv_obj_t * label, uint16_t index, lv_point_t * pos) lv_label_get_letter_pos(const lv_obj_t *label, uint16_t index, lv_point_t *pos)
{ {
uint32 argv[4] = {0}; uint32 argv[4] = { 0 };
argv[0] = (uint32)label; argv[0] = (uint32)label;
argv[1] = index; argv[1] = index;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_LETTER_POS); CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_LETTER_POS);
@ -197,10 +198,10 @@ void lv_label_get_letter_pos(const lv_obj_t * label, uint16_t index, lv_point_t
pos->y = argv[3]; pos->y = argv[3];
} }
uint16_t
uint16_t lv_label_get_letter_on(const lv_obj_t * label, lv_point_t * pos) lv_label_get_letter_on(const lv_obj_t *label, lv_point_t *pos)
{ {
uint32 argv[3] = {0}; uint32 argv[3] = { 0 };
argv[0] = (uint32)label; argv[0] = (uint32)label;
argv[1] = pos->x; argv[1] = pos->x;
argv[2] = pos->y; argv[2] = pos->y;
@ -208,10 +209,10 @@ uint16_t lv_label_get_letter_on(const lv_obj_t * label, lv_point_t * pos)
return (uint16_t)argv[0]; return (uint16_t)argv[0];
} }
bool
bool lv_label_is_char_under_pos(const lv_obj_t * label, lv_point_t * pos) lv_label_is_char_under_pos(const lv_obj_t *label, lv_point_t *pos)
{ {
uint32 argv[3] = {0}; uint32 argv[3] = { 0 };
argv[0] = (uint32)label; argv[0] = (uint32)label;
argv[1] = pos->x; argv[1] = pos->x;
argv[2] = pos->y; argv[2] = pos->y;
@ -219,28 +220,28 @@ bool lv_label_is_char_under_pos(const lv_obj_t * label, lv_point_t * pos)
return (bool)argv[0]; return (bool)argv[0];
} }
uint16_t
uint16_t lv_label_get_text_sel_start(const lv_obj_t * label) lv_label_get_text_sel_start(const lv_obj_t *label)
{ {
uint32 argv[1] = {0}; uint32 argv[1] = { 0 };
argv[0] = (uint32)label; argv[0] = (uint32)label;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_TEXT_SEL_START); CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_TEXT_SEL_START);
return (uint16_t)argv[0]; return (uint16_t)argv[0];
} }
uint16_t
uint16_t lv_label_get_text_sel_end(const lv_obj_t * label) lv_label_get_text_sel_end(const lv_obj_t *label)
{ {
uint32 argv[1] = {0}; uint32 argv[1] = { 0 };
argv[0] = (uint32)label; argv[0] = (uint32)label;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_TEXT_SEL_END); CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_TEXT_SEL_END);
return (uint16_t)argv[0]; return (uint16_t)argv[0];
} }
void
void lv_label_ins_text(lv_obj_t * label, uint32_t pos, const char * txt) lv_label_ins_text(lv_obj_t *label, uint32_t pos, const char *txt)
{ {
uint32 argv[4] = {0}; uint32 argv[4] = { 0 };
argv[0] = (uint32)label; argv[0] = (uint32)label;
argv[1] = pos; argv[1] = pos;
argv[2] = (uint32)txt; argv[2] = (uint32)txt;
@ -248,14 +249,12 @@ void lv_label_ins_text(lv_obj_t * label, uint32_t pos, const char * txt)
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_INS_TEXT); CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_INS_TEXT);
} }
void
void lv_label_cut_text(lv_obj_t * label, uint32_t pos, uint32_t cnt) lv_label_cut_text(lv_obj_t *label, uint32_t pos, uint32_t cnt)
{ {
uint32 argv[3] = {0}; uint32 argv[3] = { 0 };
argv[0] = (uint32)label; argv[0] = (uint32)label;
argv[1] = pos; argv[1] = pos;
argv[2] = cnt; argv[2] = cnt;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_CUT_TEXT); CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_CUT_TEXT);
} }

View File

@ -3,19 +3,18 @@
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/ */
#include "wa-inc/lvgl/lvgl.h" #include "wa-inc/lvgl/lvgl.h"
#include "gui_api.h" #include "gui_api.h"
#include <string.h> #include <string.h>
#define ARGC sizeof(argv)/sizeof(uint32) #define ARGC sizeof(argv) / sizeof(uint32)
#define CALL_LIST_NATIVE_FUNC(id) wasm_list_native_call(id, argv, ARGC) #define CALL_LIST_NATIVE_FUNC(id) wasm_list_native_call(id, argv, ARGC)
lv_obj_t *
lv_obj_t * lv_list_create(lv_obj_t * par, const lv_obj_t * copy) lv_list_create(lv_obj_t *par, const lv_obj_t *copy)
{ {
uint32 argv[2] = {0}; uint32 argv[2] = { 0 };
argv[0] = (uint32)par; argv[0] = (uint32)par;
argv[1] = (uint32)copy; argv[1] = (uint32)copy;
@ -25,15 +24,16 @@ lv_obj_t * lv_list_create(lv_obj_t * par, const lv_obj_t * copy)
} }
// //
// //
//void wgl_list_clean(wgl_obj_t obj) // void wgl_list_clean(wgl_obj_t obj)
//{ //{
// wasm_list_clean(obj); // wasm_list_clean(obj);
//} //}
// //
lv_obj_t * lv_list_add_btn(lv_obj_t * list, const void * img_src, const char * txt) lv_obj_t *
lv_list_add_btn(lv_obj_t *list, const void *img_src, const char *txt)
{ {
uint32 argv[3] = {0}; uint32 argv[3] = { 0 };
(void)img_src; /* doesn't support img src currently */ (void)img_src; /* doesn't support img src currently */
@ -45,13 +45,13 @@ lv_obj_t * lv_list_add_btn(lv_obj_t * list, const void * img_src, const char * t
} }
// //
// //
//bool wgl_list_remove(const wgl_obj_t list, uint16_t index) // bool wgl_list_remove(const wgl_obj_t list, uint16_t index)
//{ //{
// return wasm_list_remove(list, index); // return wasm_list_remove(list, index);
//} //}
// //
// //
//void wgl_list_set_single_mode(wgl_obj_t list, bool mode) // void wgl_list_set_single_mode(wgl_obj_t list, bool mode)
//{ //{
// wasm_list_set_single_mode(list, mode); // wasm_list_set_single_mode(list, mode);
//} //}
@ -59,68 +59,69 @@ lv_obj_t * lv_list_add_btn(lv_obj_t * list, const void * img_src, const char * t
//#if LV_USE_GROUP //#if LV_USE_GROUP
// //
// //
//void wgl_list_set_btn_selected(wgl_obj_t list, wgl_obj_t btn) // void wgl_list_set_btn_selected(wgl_obj_t list, wgl_obj_t btn)
//{ //{
// wasm_list_set_btn_selected(list, btn); // wasm_list_set_btn_selected(list, btn);
//} //}
//#endif //#endif
// //
// //
//void wgl_list_set_style(wgl_obj_t list, wgl_list_style_t type, const wgl_style_t * style) // void wgl_list_set_style(wgl_obj_t list, wgl_list_style_t type,
// const wgl_style_t * style)
//{ //{
// //TODO // //TODO
//} //}
// //
// //
//bool wgl_list_get_single_mode(wgl_obj_t list) // bool wgl_list_get_single_mode(wgl_obj_t list)
//{ //{
// return wasm_list_get_single_mode(list); // return wasm_list_get_single_mode(list);
//} //}
// //
// //
//const char * wgl_list_get_btn_text(const wgl_obj_t btn) // const char * wgl_list_get_btn_text(const wgl_obj_t btn)
//{ //{
// return wasm_list_get_btn_text(btn); // return wasm_list_get_btn_text(btn);
//} //}
// //
//wgl_obj_t wgl_list_get_btn_label(const wgl_obj_t btn) // wgl_obj_t wgl_list_get_btn_label(const wgl_obj_t btn)
//{ //{
// return wasm_list_get_btn_label(btn); // return wasm_list_get_btn_label(btn);
//} //}
// //
// //
//wgl_obj_t wgl_list_get_btn_img(const wgl_obj_t btn) // wgl_obj_t wgl_list_get_btn_img(const wgl_obj_t btn)
//{ //{
// return wasm_list_get_btn_img(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) // 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); // 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) // 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); // 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) // 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); // return wasm_list_get_btn_index(list, btn);
//} //}
// //
// //
//uint16_t wgl_list_get_size(const wgl_obj_t list) // uint16_t wgl_list_get_size(const wgl_obj_t list)
//{ //{
// return wasm_list_get_size(list); // return wasm_list_get_size(list);
//} //}
// //
//#if LV_USE_GROUP //#if LV_USE_GROUP
// //
//wgl_obj_t wgl_list_get_btn_selected(const wgl_obj_t list) // wgl_obj_t wgl_list_get_btn_selected(const wgl_obj_t list)
//{ //{
// return wasm_list_get_btn_selected(list); // return wasm_list_get_btn_selected(list);
//} //}
@ -128,28 +129,27 @@ lv_obj_t * lv_list_add_btn(lv_obj_t * list, const void * img_src, const char * t
// //
// //
// //
//const wgl_style_t * wgl_list_get_style(const wgl_obj_t list, wgl_list_style_t type) // const wgl_style_t * wgl_list_get_style(const wgl_obj_t list,
// wgl_list_style_t type)
//{ //{
// //TODO // //TODO
// return NULL; // return NULL;
//} //}
// //
// //
//void wgl_list_up(const wgl_obj_t list) // void wgl_list_up(const wgl_obj_t list)
//{ //{
// wasm_list_up(list); // wasm_list_up(list);
//} //}
// //
//void wgl_list_down(const wgl_obj_t list) // void wgl_list_down(const wgl_obj_t list)
//{ //{
// wasm_list_down(list); // wasm_list_down(list);
//} //}
// //
// //
//void wgl_list_focus(const wgl_obj_t btn, wgl_anim_enable_t anim) // void wgl_list_focus(const wgl_obj_t btn, wgl_anim_enable_t anim)
//{ //{
// wasm_list_focus(btn, anim); // wasm_list_focus(btn, anim);
//} //}
// //

View File

@ -3,19 +3,18 @@
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/ */
#include "wa-inc/lvgl/lvgl.h" #include "wa-inc/lvgl/lvgl.h"
#include "gui_api.h" #include "gui_api.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#define ARGC sizeof(argv)/sizeof(uint32) #define ARGC sizeof(argv) / sizeof(uint32)
#define CALL_OBJ_NATIVE_FUNC(id) wasm_obj_native_call(id, argv, ARGC) #define CALL_OBJ_NATIVE_FUNC(id) wasm_obj_native_call(id, argv, ARGC)
typedef struct _obj_evt_cb { typedef struct _obj_evt_cb {
struct _obj_evt_cb *next; struct _obj_evt_cb *next;
lv_obj_t * obj; lv_obj_t *obj;
lv_event_cb_t event_cb; lv_event_cb_t event_cb;
} obj_evt_cb_t; } obj_evt_cb_t;
@ -24,31 +23,36 @@ static obj_evt_cb_t *g_obj_evt_cb_list = NULL;
/* For lvgl compatible */ /* For lvgl compatible */
char g_widget_text[100]; char g_widget_text[100];
lv_res_t lv_obj_del(lv_obj_t * obj) lv_res_t
lv_obj_del(lv_obj_t *obj)
{ {
uint32 argv[1] = {0}; uint32 argv[1] = { 0 };
argv[0] = (uint32)obj; argv[0] = (uint32)obj;
CALL_OBJ_NATIVE_FUNC(OBJ_FUNC_ID_DEL); CALL_OBJ_NATIVE_FUNC(OBJ_FUNC_ID_DEL);
return (lv_res_t)argv[0]; return (lv_res_t)argv[0];
} }
void lv_obj_del_async(struct _lv_obj_t *obj) void
lv_obj_del_async(struct _lv_obj_t *obj)
{ {
uint32 argv[1] = {0}; uint32 argv[1] = { 0 };
argv[0] = (uint32)obj; argv[0] = (uint32)obj;
CALL_OBJ_NATIVE_FUNC(OBJ_FUNC_ID_DEL_ASYNC); CALL_OBJ_NATIVE_FUNC(OBJ_FUNC_ID_DEL_ASYNC);
} }
void lv_obj_clean(lv_obj_t * obj) void
lv_obj_clean(lv_obj_t *obj)
{ {
uint32 argv[1] = {0}; uint32 argv[1] = { 0 };
argv[0] = (uint32)obj; argv[0] = (uint32)obj;
CALL_OBJ_NATIVE_FUNC(OBJ_FUNC_ID_CLEAN); CALL_OBJ_NATIVE_FUNC(OBJ_FUNC_ID_CLEAN);
} }
void lv_obj_align(lv_obj_t * obj, const lv_obj_t * base, lv_align_t align, lv_coord_t x_mod, lv_coord_t y_mod) void
lv_obj_align(lv_obj_t *obj, const lv_obj_t *base, lv_align_t align,
lv_coord_t x_mod, lv_coord_t y_mod)
{ {
uint32 argv[5] = {0}; uint32 argv[5] = { 0 };
argv[0] = (uint32)obj; argv[0] = (uint32)obj;
argv[1] = (uint32)base; argv[1] = (uint32)base;
argv[2] = align; argv[2] = align;
@ -57,7 +61,8 @@ void lv_obj_align(lv_obj_t * obj, const lv_obj_t * base, lv_align_t align, lv_co
CALL_OBJ_NATIVE_FUNC(OBJ_FUNC_ID_ALIGN); CALL_OBJ_NATIVE_FUNC(OBJ_FUNC_ID_ALIGN);
} }
lv_event_cb_t lv_obj_get_event_cb(const lv_obj_t * obj) lv_event_cb_t
lv_obj_get_event_cb(const lv_obj_t *obj)
{ {
obj_evt_cb_t *obj_evt_cb = g_obj_evt_cb_list; obj_evt_cb_t *obj_evt_cb = g_obj_evt_cb_list;
while (obj_evt_cb != NULL) { while (obj_evt_cb != NULL) {
@ -70,10 +75,11 @@ lv_event_cb_t lv_obj_get_event_cb(const lv_obj_t * obj)
return NULL; return NULL;
} }
void lv_obj_set_event_cb(lv_obj_t * obj, lv_event_cb_t event_cb) void
lv_obj_set_event_cb(lv_obj_t *obj, lv_event_cb_t event_cb)
{ {
obj_evt_cb_t *obj_evt_cb; obj_evt_cb_t *obj_evt_cb;
uint32 argv[1] = {0}; uint32 argv[1] = { 0 };
obj_evt_cb = g_obj_evt_cb_list; obj_evt_cb = g_obj_evt_cb_list;
while (obj_evt_cb) { while (obj_evt_cb) {
@ -94,7 +100,8 @@ void lv_obj_set_event_cb(lv_obj_t * obj, lv_event_cb_t event_cb)
if (g_obj_evt_cb_list != NULL) { if (g_obj_evt_cb_list != NULL) {
obj_evt_cb->next = g_obj_evt_cb_list; obj_evt_cb->next = g_obj_evt_cb_list;
g_obj_evt_cb_list = obj_evt_cb; g_obj_evt_cb_list = obj_evt_cb;
} else { }
else {
g_obj_evt_cb_list = obj_evt_cb; g_obj_evt_cb_list = obj_evt_cb;
} }
@ -102,7 +109,8 @@ void lv_obj_set_event_cb(lv_obj_t * obj, lv_event_cb_t event_cb)
CALL_OBJ_NATIVE_FUNC(OBJ_FUNC_ID_SET_EVT_CB); CALL_OBJ_NATIVE_FUNC(OBJ_FUNC_ID_SET_EVT_CB);
} }
void on_widget_event(lv_obj_t * obj, lv_event_t event) void
on_widget_event(lv_obj_t *obj, lv_event_t event)
{ {
obj_evt_cb_t *obj_evt_cb = g_obj_evt_cb_list; obj_evt_cb_t *obj_evt_cb = g_obj_evt_cb_list;

File diff suppressed because it is too large Load Diff

View File

@ -10,7 +10,6 @@
extern "C" { extern "C" {
#endif #endif
//#include "bi-inc/wgl_shared_utils.h" /* shared types between app and native */ //#include "bi-inc/wgl_shared_utils.h" /* shared types between app and native */
/* /*
#include "lvgl-compatible/lv_types.h" #include "lvgl-compatible/lv_types.h"
@ -21,9 +20,6 @@ extern "C" {
#include "lvgl-compatible/lv_list.h" #include "lvgl-compatible/lv_list.h"
*/ */
#include "src/lv_version.h" #include "src/lv_version.h"
#include "src/lv_misc/lv_log.h" #include "src/lv_misc/lv_log.h"

View File

@ -1,11 +1,9 @@
#include "lvgl.h" #include "lvgl.h"
int
int main() main()
{ {
return 0;
return 0;
} }

View File

@ -13,34 +13,32 @@
extern "C" { extern "C" {
#endif #endif
/** /**
* gui interfaces * gui interfaces
*/ */
void void
wasm_obj_native_call(wasm_exec_env_t exec_env, wasm_obj_native_call(wasm_exec_env_t exec_env, int32 func_id, uint32 *argv,
int32 func_id, uint32 *argv, uint32 argc); uint32 argc);
void void
wasm_btn_native_call(wasm_exec_env_t exec_env, wasm_btn_native_call(wasm_exec_env_t exec_env, int32 func_id, uint32 *argv,
int32 func_id, uint32 *argv, uint32 argc); uint32 argc);
void void
wasm_label_native_call(wasm_exec_env_t exec_env, wasm_label_native_call(wasm_exec_env_t exec_env, int32 func_id, uint32 *argv,
int32 func_id, uint32 *argv, uint32 argc); uint32 argc);
void void
wasm_cb_native_call(wasm_exec_env_t exec_env, wasm_cb_native_call(wasm_exec_env_t exec_env, int32 func_id, uint32 *argv,
int32 func_id, uint32 *argv, uint32 argc); uint32 argc);
void void
wasm_list_native_call(wasm_exec_env_t exec_env, wasm_list_native_call(wasm_exec_env_t exec_env, int32 func_id, uint32 *argv,
int32 func_id, uint32 *argv, uint32 argc); uint32 argc);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif /* end of _GUI_API_H_ */ #endif /* end of _GUI_API_H_ */

View File

@ -10,8 +10,10 @@
extern "C" { extern "C" {
#endif #endif
void wgl_init(void); void
void wgl_exit(void); wgl_init(void);
void
wgl_exit(void);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -19,7 +19,8 @@ DEFINE_WGL_NATIVE_WRAPPER(lv_btn_create_wrapper)
wgl_native_get_arg(uint32, copy_obj_id); wgl_native_get_arg(uint32, copy_obj_id);
wasm_module_inst_t module_inst = get_module_inst(exec_env); wasm_module_inst_t module_inst = get_module_inst(exec_env);
res = wgl_native_wigdet_create(WIDGET_TYPE_BTN, par_obj_id, copy_obj_id, module_inst); res = wgl_native_wigdet_create(WIDGET_TYPE_BTN, par_obj_id, copy_obj_id,
module_inst);
wgl_native_set_return(res); wgl_native_set_return(res);
} }
@ -131,33 +132,30 @@ DEFINE_WGL_NATIVE_WRAPPER(lv_btn_toggle_wrapper)
lv_btn_toggle(btn); lv_btn_toggle(btn);
} }
/* clang-format off */
static WGLNativeFuncDef btn_native_func_defs[] = { static WGLNativeFuncDef btn_native_func_defs[] = {
{ BTN_FUNC_ID_CREATE, lv_btn_create_wrapper, 2, false }, { BTN_FUNC_ID_CREATE, lv_btn_create_wrapper, 2, false },
{ BTN_FUNC_ID_SET_TOGGLE, lv_btn_set_toggle_wrapper, 2, true }, { BTN_FUNC_ID_SET_TOGGLE, lv_btn_set_toggle_wrapper, 2, true },
{ BTN_FUNC_ID_SET_STATE, lv_btn_set_state_wrapper, 2, true }, { BTN_FUNC_ID_SET_STATE, lv_btn_set_state_wrapper, 2, true },
{ BTN_FUNC_ID_SET_INK_IN_TIME, lv_btn_set_ink_in_time_wrapper, 2, true }, { BTN_FUNC_ID_SET_INK_IN_TIME, lv_btn_set_ink_in_time_wrapper, 2, true },
{ BTN_FUNC_ID_SET_INK_OUT_TIME, lv_btn_set_ink_out_time_wrapper, 2, true }, { BTN_FUNC_ID_SET_INK_OUT_TIME, lv_btn_set_ink_out_time_wrapper, 2, true },
{ BTN_FUNC_ID_SET_INK_WAIT_TIME, lv_btn_set_ink_wait_time_wrapper, 2, true }, { BTN_FUNC_ID_SET_INK_WAIT_TIME, lv_btn_set_ink_wait_time_wrapper, 2, true },
{ BTN_FUNC_ID_GET_INK_IN_TIME, lv_btn_get_ink_in_time_wrapper, 1, true }, { BTN_FUNC_ID_GET_INK_IN_TIME, lv_btn_get_ink_in_time_wrapper, 1, true },
{ BTN_FUNC_ID_GET_INK_OUT_TIME, lv_btn_get_ink_out_time_wrapper, 1, true }, { BTN_FUNC_ID_GET_INK_OUT_TIME, lv_btn_get_ink_out_time_wrapper, 1, true },
{ BTN_FUNC_ID_GET_INK_WAIT_TIME, lv_btn_get_ink_wait_time_wrapper, 1, true }, { BTN_FUNC_ID_GET_INK_WAIT_TIME, lv_btn_get_ink_wait_time_wrapper, 1, true },
{ BTN_FUNC_ID_GET_STATE, lv_btn_get_state_wrapper, 1, true }, { BTN_FUNC_ID_GET_STATE, lv_btn_get_state_wrapper, 1, true },
{ BTN_FUNC_ID_GET_TOGGLE, lv_btn_get_toggle_wrapper, 1, true }, { BTN_FUNC_ID_GET_TOGGLE, lv_btn_get_toggle_wrapper, 1, true },
{ BTN_FUNC_ID_TOGGLE, lv_btn_toggle_wrapper, 1, true }, { BTN_FUNC_ID_TOGGLE, lv_btn_toggle_wrapper, 1, true },
}; };
/* clang-format on */
/*************** Native Interface to Wasm App ***********/ /*************** Native Interface to Wasm App ***********/
void void
wasm_btn_native_call(wasm_exec_env_t exec_env, wasm_btn_native_call(wasm_exec_env_t exec_env, int32 func_id, uint32 *argv,
int32 func_id, uint32 *argv, uint32 argc) uint32 argc)
{ {
uint32 size = sizeof(btn_native_func_defs) / sizeof(WGLNativeFuncDef); uint32 size = sizeof(btn_native_func_defs) / sizeof(WGLNativeFuncDef);
wgl_native_func_call(exec_env, wgl_native_func_call(exec_env, btn_native_func_defs, size, func_id, argv,
btn_native_func_defs,
size,
func_id,
argv,
argc); argc);
} }

View File

@ -20,7 +20,8 @@ DEFINE_WGL_NATIVE_WRAPPER(lv_cb_create_wrapper)
wgl_native_get_arg(uint32, copy_obj_id); wgl_native_get_arg(uint32, copy_obj_id);
wasm_module_inst_t module_inst = get_module_inst(exec_env); wasm_module_inst_t module_inst = get_module_inst(exec_env);
res = wgl_native_wigdet_create(WIDGET_TYPE_CB, par_obj_id, copy_obj_id, module_inst); res = wgl_native_wigdet_create(WIDGET_TYPE_CB, par_obj_id, copy_obj_id,
module_inst);
wgl_native_set_return(res); wgl_native_set_return(res);
} }
@ -63,7 +64,7 @@ DEFINE_WGL_NATIVE_WRAPPER(lv_cb_get_text_length_wrapper)
(void)exec_env; (void)exec_env;
text = lv_cb_get_text(cb); text = lv_cb_get_text(cb);
wgl_native_set_return(text ? strlen(text): 0); wgl_native_set_return(text ? strlen(text) : 0);
} }
DEFINE_WGL_NATIVE_WRAPPER(lv_cb_get_text_wrapper) DEFINE_WGL_NATIVE_WRAPPER(lv_cb_get_text_wrapper)
@ -89,24 +90,20 @@ DEFINE_WGL_NATIVE_WRAPPER(lv_cb_get_text_wrapper)
} }
static WGLNativeFuncDef cb_native_func_defs[] = { static WGLNativeFuncDef cb_native_func_defs[] = {
{ CB_FUNC_ID_CREATE, lv_cb_create_wrapper, 2, false }, { CB_FUNC_ID_CREATE, lv_cb_create_wrapper, 2, false },
{ CB_FUNC_ID_SET_TEXT, lv_cb_set_text_wrapper, 3, true }, { CB_FUNC_ID_SET_TEXT, lv_cb_set_text_wrapper, 3, true },
{ CB_FUNC_ID_SET_STATIC_TEXT, lv_cb_set_static_text_wrapper, 3, true }, { CB_FUNC_ID_SET_STATIC_TEXT, lv_cb_set_static_text_wrapper, 3, true },
{ CB_FUNC_ID_GET_TEXT_LENGTH, lv_cb_get_text_length_wrapper, 1, true }, { CB_FUNC_ID_GET_TEXT_LENGTH, lv_cb_get_text_length_wrapper, 1, true },
{ CB_FUNC_ID_GET_TEXT, lv_cb_get_text_wrapper, 3, true }, { CB_FUNC_ID_GET_TEXT, lv_cb_get_text_wrapper, 3, true },
}; };
/*************** Native Interface to Wasm App ***********/ /*************** Native Interface to Wasm App ***********/
void void
wasm_cb_native_call(wasm_exec_env_t exec_env, wasm_cb_native_call(wasm_exec_env_t exec_env, int32 func_id, uint32 *argv,
int32 func_id, uint32 *argv, uint32 argc) uint32 argc)
{ {
uint32 size = sizeof(cb_native_func_defs) / sizeof(WGLNativeFuncDef); uint32 size = sizeof(cb_native_func_defs) / sizeof(WGLNativeFuncDef);
wgl_native_func_call(exec_env, wgl_native_func_call(exec_env, cb_native_func_defs, size, func_id, argv,
cb_native_func_defs,
size,
func_id,
argv,
argc); argc);
} }

View File

@ -20,7 +20,8 @@ DEFINE_WGL_NATIVE_WRAPPER(lv_label_create_wrapper)
wgl_native_get_arg(uint32, copy_obj_id); wgl_native_get_arg(uint32, copy_obj_id);
wasm_module_inst_t module_inst = get_module_inst(exec_env); wasm_module_inst_t module_inst = get_module_inst(exec_env);
res = wgl_native_wigdet_create(WIDGET_TYPE_LABEL, par_obj_id, copy_obj_id, module_inst); res = wgl_native_wigdet_create(WIDGET_TYPE_LABEL, par_obj_id, copy_obj_id,
module_inst);
wgl_native_set_return(res); wgl_native_set_return(res);
} }
@ -73,24 +74,22 @@ DEFINE_WGL_NATIVE_WRAPPER(lv_label_get_text_wrapper)
wgl_native_set_return(buffer_offset); wgl_native_set_return(buffer_offset);
} }
/* clang-format off */
static WGLNativeFuncDef label_native_func_defs[] = { static WGLNativeFuncDef label_native_func_defs[] = {
{ LABEL_FUNC_ID_CREATE, lv_label_create_wrapper, 2, false }, { LABEL_FUNC_ID_CREATE, lv_label_create_wrapper, 2, false },
{ LABEL_FUNC_ID_SET_TEXT, lv_label_set_text_wrapper, 3, true }, { LABEL_FUNC_ID_SET_TEXT, lv_label_set_text_wrapper, 3, true },
{ LABEL_FUNC_ID_GET_TEXT_LENGTH, lv_label_get_text_length_wrapper, 1, true }, { LABEL_FUNC_ID_GET_TEXT_LENGTH, lv_label_get_text_length_wrapper, 1, true },
{ LABEL_FUNC_ID_GET_TEXT, lv_label_get_text_wrapper, 3, true }, { LABEL_FUNC_ID_GET_TEXT, lv_label_get_text_wrapper, 3, true },
}; };
/* clang-format on */
/*************** Native Interface to Wasm App ***********/ /*************** Native Interface to Wasm App ***********/
void void
wasm_label_native_call(wasm_exec_env_t exec_env, wasm_label_native_call(wasm_exec_env_t exec_env, int32 func_id, uint32 *argv,
int32 func_id, uint32 *argv, uint32 argc) uint32 argc)
{ {
uint32 size = sizeof(label_native_func_defs) / sizeof(WGLNativeFuncDef); uint32 size = sizeof(label_native_func_defs) / sizeof(WGLNativeFuncDef);
wgl_native_func_call(exec_env, wgl_native_func_call(exec_env, label_native_func_defs, size, func_id, argv,
label_native_func_defs,
size,
func_id,
argv,
argc); argc);
} }

View File

@ -20,7 +20,8 @@ DEFINE_WGL_NATIVE_WRAPPER(lv_list_create_wrapper)
wgl_native_get_arg(uint32, copy_obj_id); wgl_native_get_arg(uint32, copy_obj_id);
wasm_module_inst_t module_inst = get_module_inst(exec_env); wasm_module_inst_t module_inst = get_module_inst(exec_env);
res = wgl_native_wigdet_create(WIDGET_TYPE_LIST, par_obj_id, copy_obj_id, module_inst); res = wgl_native_wigdet_create(WIDGET_TYPE_LIST, par_obj_id, copy_obj_id,
module_inst);
wgl_native_set_return(res); wgl_native_set_return(res);
} }
@ -49,7 +50,8 @@ DEFINE_WGL_NATIVE_WRAPPER(lv_list_add_btn_wrapper)
bh_assert(mod_id != ID_NONE); bh_assert(mod_id != ID_NONE);
if (!wgl_native_add_object(btn, mod_id, &btn_obj_id)) { if (!wgl_native_add_object(btn, mod_id, &btn_obj_id)) {
wasm_runtime_set_exception(module_inst, "add button to object list fail."); wasm_runtime_set_exception(module_inst,
"add button to object list fail.");
return; return;
} }
@ -57,21 +59,17 @@ DEFINE_WGL_NATIVE_WRAPPER(lv_list_add_btn_wrapper)
} }
static WGLNativeFuncDef list_native_func_defs[] = { static WGLNativeFuncDef list_native_func_defs[] = {
{ LIST_FUNC_ID_CREATE, lv_list_create_wrapper, 2, false }, { LIST_FUNC_ID_CREATE, lv_list_create_wrapper, 2, false },
{ LIST_FUNC_ID_ADD_BTN, lv_list_add_btn_wrapper, 3, true }, { LIST_FUNC_ID_ADD_BTN, lv_list_add_btn_wrapper, 3, true },
}; };
/*************** Native Interface to Wasm App ***********/ /*************** Native Interface to Wasm App ***********/
void void
wasm_list_native_call(wasm_exec_env_t exec_env, wasm_list_native_call(wasm_exec_env_t exec_env, int32 func_id, uint32 *argv,
int32 func_id, uint32 *argv, uint32 argc) uint32 argc)
{ {
uint32 size = sizeof(list_native_func_defs) / sizeof(WGLNativeFuncDef); uint32 size = sizeof(list_native_func_defs) / sizeof(WGLNativeFuncDef);
wgl_native_func_call(exec_env, wgl_native_func_call(exec_env, list_native_func_defs, size, func_id, argv,
list_native_func_defs,
size,
func_id,
argv,
argc); argc);
} }

View File

@ -10,16 +10,15 @@
#define THROW_EXC(msg) wasm_runtime_set_exception(module_inst, msg); #define THROW_EXC(msg) wasm_runtime_set_exception(module_inst, msg);
uint32 wgl_native_wigdet_create(int8 widget_type, uint32
uint32 par_obj_id, wgl_native_wigdet_create(int8 widget_type, uint32 par_obj_id,
uint32 copy_obj_id, uint32 copy_obj_id, wasm_module_inst_t module_inst)
wasm_module_inst_t module_inst)
{ {
uint32 obj_id; uint32 obj_id;
lv_obj_t *wigdet = NULL, *par = NULL, *copy = NULL; lv_obj_t *wigdet = NULL, *par = NULL, *copy = NULL;
uint32 mod_id; uint32 mod_id;
//TODO: limit total widget number // TODO: limit total widget number
/* validate the parent object id if not equal to 0 */ /* validate the parent object id if not equal to 0 */
if (par_obj_id != 0 && !wgl_native_validate_object(par_obj_id, &par)) { if (par_obj_id != 0 && !wgl_native_validate_object(par_obj_id, &par)) {
@ -58,14 +57,11 @@ uint32 wgl_native_wigdet_create(int8 widget_type,
return 0; return 0;
} }
void wgl_native_func_call(wasm_exec_env_t exec_env, void
WGLNativeFuncDef *funcs, wgl_native_func_call(wasm_exec_env_t exec_env, WGLNativeFuncDef *funcs,
uint32 size, uint32 size, int32 func_id, uint32 *argv, uint32 argc)
int32 func_id,
uint32 *argv,
uint32 argc)
{ {
typedef void (*WGLNativeFuncPtr)(wasm_exec_env_t, uint64*, uint32*); typedef void (*WGLNativeFuncPtr)(wasm_exec_env_t, uint64 *, uint32 *);
WGLNativeFuncPtr wglNativeFuncPtr; WGLNativeFuncPtr wglNativeFuncPtr;
wasm_module_inst_t module_inst = get_module_inst(exec_env); wasm_module_inst_t module_inst = get_module_inst(exec_env);
WGLNativeFuncDef *func_def = funcs; WGLNativeFuncDef *func_def = funcs;
@ -74,12 +70,12 @@ void wgl_native_func_call(wasm_exec_env_t exec_env,
/* Note: argv is validated in wasm_runtime_invoke_native() /* Note: argv is validated in wasm_runtime_invoke_native()
* with pointer length equals to 1. Here validate the argv * with pointer length equals to 1. Here validate the argv
* buffer again but with its total length in bytes */ * buffer again but with its total length in bytes */
if (!wasm_runtime_validate_native_addr(module_inst, argv, argc * sizeof(uint32))) if (!wasm_runtime_validate_native_addr(module_inst, argv,
argc * sizeof(uint32)))
return; return;
while (func_def < func_def_end) { while (func_def < func_def_end) {
if (func_def->func_id == func_id if (func_def->func_id == func_id && (uint32)func_def->arg_num == argc) {
&& (uint32)func_def->arg_num == argc) {
uint64 argv_copy_buf[16], size; uint64 argv_copy_buf[16], size;
uint64 *argv_copy = argv_copy_buf; uint64 *argv_copy = argv_copy_buf;
int i; int i;
@ -96,7 +92,7 @@ void wgl_native_func_call(wasm_exec_env_t exec_env,
/* Init argv_copy */ /* Init argv_copy */
for (i = 0; i < func_def->arg_num; i++) for (i = 0; i < func_def->arg_num; i++)
*(uint32*)&argv_copy[i] = argv[i]; *(uint32 *)&argv_copy[i] = argv[i];
/* Validate the first argument which is a lvgl object if needed */ /* Validate the first argument which is a lvgl object if needed */
if (func_def->check_obj) { if (func_def->check_obj) {
@ -128,4 +124,3 @@ void wgl_native_func_call(wasm_exec_env_t exec_env,
THROW_EXC("the native widget function is not found!"); THROW_EXC("the native widget function is not found!");
} }

View File

@ -15,21 +15,22 @@ extern "C" {
#include "wasm_export.h" #include "wasm_export.h"
#include "bi-inc/wgl_shared_utils.h" #include "bi-inc/wgl_shared_utils.h"
#define wgl_native_return_type(type) type *wgl_ret = (type*)(args_ret) #define wgl_native_return_type(type) type *wgl_ret = (type *)(args_ret)
#define wgl_native_get_arg(type, name) type name = *((type*)(args++)) #define wgl_native_get_arg(type, name) type name = *((type *)(args++))
#define wgl_native_set_return(val) *wgl_ret = (val) #define wgl_native_set_return(val) *wgl_ret = (val)
#define DEFINE_WGL_NATIVE_WRAPPER(func_name) \ #define DEFINE_WGL_NATIVE_WRAPPER(func_name) \
static void func_name(wasm_exec_env_t exec_env, uint64 *args, uint32 *args_ret) static void func_name(wasm_exec_env_t exec_env, uint64 *args, \
uint32 *args_ret)
enum { enum {
WIDGET_TYPE_BTN, WIDGET_TYPE_BTN,
WIDGET_TYPE_LABEL, WIDGET_TYPE_LABEL,
WIDGET_TYPE_CB, WIDGET_TYPE_CB,
WIDGET_TYPE_LIST, WIDGET_TYPE_LIST,
WIDGET_TYPE_DDLIST, WIDGET_TYPE_DDLIST,
_WIDGET_TYPE_NUM, _WIDGET_TYPE_NUM,
}; };
typedef struct WGLNativeFuncDef { typedef struct WGLNativeFuncDef {
@ -46,21 +47,19 @@ typedef struct WGLNativeFuncDef {
bool check_obj; bool check_obj;
} WGLNativeFuncDef; } WGLNativeFuncDef;
bool wgl_native_validate_object(int32 obj_id, lv_obj_t **obj); 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); bool
wgl_native_add_object(lv_obj_t *obj, uint32 module_id, uint32 *obj_id);
uint32 wgl_native_wigdet_create(int8 widget_type, uint32
uint32 par_obj_id, wgl_native_wigdet_create(int8 widget_type, uint32 par_obj_id,
uint32 copy_obj_id, uint32 copy_obj_id, wasm_module_inst_t module_inst);
wasm_module_inst_t module_inst);
void wgl_native_func_call(wasm_exec_env_t exec_env, void
WGLNativeFuncDef *funcs, wgl_native_func_call(wasm_exec_env_t exec_env, WGLNativeFuncDef *funcs,
uint32 size, uint32 size, int32 func_id, uint32 *argv, uint32 argc);
int32 func_id,
uint32 *argv,
uint32 argc);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -10,7 +10,6 @@
#include "wgl_native_utils.h" #include "wgl_native_utils.h"
#include "wgl.h" #include "wgl.h"
typedef struct { typedef struct {
bh_list_link l; bh_list_link l;
@ -42,24 +41,24 @@ static korp_mutex task_handler_lock;
static korp_cond task_handler_cond; static korp_cond task_handler_cond;
static void app_mgr_object_event_callback(module_data *m_data, bh_message_t msg) static void
app_mgr_object_event_callback(module_data *m_data, bh_message_t msg)
{ {
uint32 argv[2]; uint32 argv[2];
wasm_function_inst_t func_on_object_event; wasm_function_inst_t func_on_object_event;
bh_assert(WIDGET_EVENT_WASM == bh_message_type(msg)); bh_assert(WIDGET_EVENT_WASM == bh_message_type(msg));
wasm_data *wasm_app_data = (wasm_data*)m_data->internal_data; wasm_data *wasm_app_data = (wasm_data *)m_data->internal_data;
wasm_module_inst_t inst = wasm_app_data->wasm_module_inst; wasm_module_inst_t inst = wasm_app_data->wasm_module_inst;
object_event_t *object_event object_event_t *object_event = (object_event_t *)bh_message_payload(msg);
= (object_event_t *)bh_message_payload(msg);
if (object_event == NULL) if (object_event == NULL)
return; return;
func_on_object_event = wasm_runtime_lookup_function(inst, "_on_widget_event", func_on_object_event =
"(i32i32)"); wasm_runtime_lookup_function(inst, "_on_widget_event", "(i32i32)");
if (!func_on_object_event) if (!func_on_object_event)
func_on_object_event = wasm_runtime_lookup_function(inst, "on_widget_event", func_on_object_event =
"(i32i32)"); wasm_runtime_lookup_function(inst, "on_widget_event", "(i32i32)");
if (!func_on_object_event) { if (!func_on_object_event) {
printf("Cannot find function on_widget_event\n"); printf("Cannot find function on_widget_event\n");
return; return;
@ -71,14 +70,14 @@ static void app_mgr_object_event_callback(module_data *m_data, bh_message_t msg)
2, argv)) { 2, argv)) {
const char *exception = wasm_runtime_get_exception(inst); const char *exception = wasm_runtime_get_exception(inst);
bh_assert(exception); bh_assert(exception);
printf(":Got exception running wasm code: %s\n", printf(":Got exception running wasm code: %s\n", exception);
exception);
wasm_runtime_clear_exception(inst); wasm_runtime_clear_exception(inst);
return; return;
} }
} }
static void cleanup_object_list(uint32 module_id) static void
cleanup_object_list(uint32 module_id)
{ {
object_node_t *elem; object_node_t *elem;
@ -89,8 +88,8 @@ static void cleanup_object_list(uint32 module_id)
elem = (object_node_t *)bh_list_first_elem(&g_object_list); elem = (object_node_t *)bh_list_first_elem(&g_object_list);
while (elem) { while (elem) {
/* delete the leaf node belongs to the module firstly */ /* delete the leaf node belongs to the module firstly */
if (module_id == elem->module_id && if (module_id == elem->module_id
lv_obj_count_children(elem->obj) == 0) { && lv_obj_count_children(elem->obj) == 0) {
object_node_t *next = (object_node_t *)bh_list_elem_next(elem); object_node_t *next = (object_node_t *)bh_list_elem_next(elem);
found = true; found = true;
@ -98,7 +97,8 @@ static void cleanup_object_list(uint32 module_id)
bh_list_remove(&g_object_list, elem); bh_list_remove(&g_object_list, elem);
wasm_runtime_free(elem); wasm_runtime_free(elem);
elem = next; elem = next;
} else { }
else {
elem = (object_node_t *)bh_list_elem_next(elem); elem = (object_node_t *)bh_list_elem_next(elem);
} }
} }
@ -110,7 +110,8 @@ static void cleanup_object_list(uint32 module_id)
os_mutex_unlock(&g_object_list_mutex); os_mutex_unlock(&g_object_list_mutex);
} }
static bool init_object_event_callback_framework() static bool
init_object_event_callback_framework()
{ {
if (!wasm_register_cleanup_callback(cleanup_object_list)) { if (!wasm_register_cleanup_callback(cleanup_object_list)) {
goto fail; goto fail;
@ -127,7 +128,8 @@ fail:
return false; return false;
} }
bool wgl_native_validate_object(int32 obj_id, lv_obj_t **obj) bool
wgl_native_validate_object(int32 obj_id, lv_obj_t **obj)
{ {
object_node_t *elem; object_node_t *elem;
@ -141,7 +143,7 @@ bool wgl_native_validate_object(int32 obj_id, lv_obj_t **obj)
os_mutex_unlock(&g_object_list_mutex); os_mutex_unlock(&g_object_list_mutex);
return true; return true;
} }
elem = (object_node_t *) bh_list_elem_next(elem); elem = (object_node_t *)bh_list_elem_next(elem);
} }
os_mutex_unlock(&g_object_list_mutex); os_mutex_unlock(&g_object_list_mutex);
@ -149,11 +151,12 @@ bool wgl_native_validate_object(int32 obj_id, lv_obj_t **obj)
return false; return false;
} }
bool wgl_native_add_object(lv_obj_t *obj, uint32 module_id, uint32 *obj_id) bool
wgl_native_add_object(lv_obj_t *obj, uint32 module_id, uint32 *obj_id)
{ {
object_node_t *node; object_node_t *node;
node = (object_node_t *) wasm_runtime_malloc(sizeof(object_node_t)); node = (object_node_t *)wasm_runtime_malloc(sizeof(object_node_t));
if (node == NULL) if (node == NULL)
return false; return false;
@ -178,11 +181,12 @@ bool wgl_native_add_object(lv_obj_t *obj, uint32 module_id, uint32 *obj_id)
return true; return true;
} }
static void _obj_del_recursive(lv_obj_t *obj) static void
_obj_del_recursive(lv_obj_t *obj)
{ {
object_node_t *elem; object_node_t *elem;
lv_obj_t * i; lv_obj_t *i;
lv_obj_t * i_next; lv_obj_t *i_next;
i = lv_ll_get_head(&(obj->child_ll)); i = lv_ll_get_head(&(obj->child_ll));
@ -207,16 +211,17 @@ static void _obj_del_recursive(lv_obj_t *obj)
os_mutex_unlock(&g_object_list_mutex); os_mutex_unlock(&g_object_list_mutex);
return; return;
} }
elem = (object_node_t *) bh_list_elem_next(elem); elem = (object_node_t *)bh_list_elem_next(elem);
} }
os_mutex_unlock(&g_object_list_mutex); os_mutex_unlock(&g_object_list_mutex);
} }
static void _obj_clean_recursive(lv_obj_t *obj) static void
_obj_clean_recursive(lv_obj_t *obj)
{ {
lv_obj_t * i; lv_obj_t *i;
lv_obj_t * i_next; lv_obj_t *i_next;
i = lv_ll_get_head(&(obj->child_ll)); i = lv_ll_get_head(&(obj->child_ll));
@ -232,7 +237,8 @@ static void _obj_clean_recursive(lv_obj_t *obj)
} }
} }
static void post_widget_msg_to_module(object_node_t *object_node, lv_event_t event) 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); module_data *module = module_data_list_lookup_id(object_node->module_id);
object_event_t *object_event; object_event_t *object_event;
@ -248,13 +254,12 @@ static void post_widget_msg_to_module(object_node_t *object_node, lv_event_t eve
object_event->obj_id = object_node->obj_id; object_event->obj_id = object_node->obj_id;
object_event->event = event; object_event->event = event;
bh_post_msg(module->queue, bh_post_msg(module->queue, WIDGET_EVENT_WASM, object_event,
WIDGET_EVENT_WASM,
object_event,
sizeof(*object_event)); sizeof(*object_event));
} }
static void internal_lv_obj_event_cb(lv_obj_t *obj, lv_event_t event) static void
internal_lv_obj_event_cb(lv_obj_t *obj, lv_event_t event)
{ {
object_node_t *elem; object_node_t *elem;
@ -267,13 +272,14 @@ static void internal_lv_obj_event_cb(lv_obj_t *obj, lv_event_t event)
os_mutex_unlock(&g_object_list_mutex); os_mutex_unlock(&g_object_list_mutex);
return; return;
} }
elem = (object_node_t *) bh_list_elem_next(elem); elem = (object_node_t *)bh_list_elem_next(elem);
} }
os_mutex_unlock(&g_object_list_mutex); os_mutex_unlock(&g_object_list_mutex);
} }
static void* lv_task_handler_thread_routine (void *arg) static void *
lv_task_handler_thread_routine(void *arg)
{ {
os_mutex_lock(&task_handler_lock); os_mutex_lock(&task_handler_lock);
@ -287,7 +293,8 @@ static void* lv_task_handler_thread_routine (void *arg)
return NULL; return NULL;
} }
void wgl_init(void) void
wgl_init(void)
{ {
korp_tid tid; korp_tid tid;
@ -306,13 +313,12 @@ void wgl_init(void)
init_object_event_callback_framework(); init_object_event_callback_framework();
/* new a thread, call lv_task_handler periodically */ /* new a thread, call lv_task_handler periodically */
os_thread_create(&tid, os_thread_create(&tid, lv_task_handler_thread_routine, NULL,
lv_task_handler_thread_routine,
NULL,
BH_APPLET_PRESERVED_STACK_SIZE); BH_APPLET_PRESERVED_STACK_SIZE);
} }
void wgl_exit(void) void
wgl_exit(void)
{ {
lv_task_handler_thread_run = false; lv_task_handler_thread_run = false;
os_cond_destroy(&task_handler_cond); os_cond_destroy(&task_handler_cond);
@ -371,7 +377,8 @@ DEFINE_WGL_NATIVE_WRAPPER(lv_obj_align_wrapper)
/* validate the base object id if not equal to 0 */ /* validate the base object id if not equal to 0 */
if (base_obj_id != 0 && !wgl_native_validate_object(base_obj_id, &base)) { if (base_obj_id != 0 && !wgl_native_validate_object(base_obj_id, &base)) {
wasm_runtime_set_exception(module_inst, "align with invalid base object."); wasm_runtime_set_exception(module_inst,
"align with invalid base object.");
return; return;
} }
@ -388,24 +395,20 @@ DEFINE_WGL_NATIVE_WRAPPER(lv_obj_set_event_cb_wrapper)
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
static WGLNativeFuncDef obj_native_func_defs[] = { static WGLNativeFuncDef obj_native_func_defs[] = {
{ OBJ_FUNC_ID_DEL, lv_obj_del_wrapper, 1, true }, { OBJ_FUNC_ID_DEL, lv_obj_del_wrapper, 1, true },
{ OBJ_FUNC_ID_DEL_ASYNC, lv_obj_del_async_wrapper, 1, true }, { OBJ_FUNC_ID_DEL_ASYNC, lv_obj_del_async_wrapper, 1, true },
{ OBJ_FUNC_ID_CLEAN, lv_obj_clean_wrapper, 1, true }, { OBJ_FUNC_ID_CLEAN, lv_obj_clean_wrapper, 1, true },
{ OBJ_FUNC_ID_ALIGN, lv_obj_align_wrapper, 5, true }, { OBJ_FUNC_ID_ALIGN, lv_obj_align_wrapper, 5, true },
{ OBJ_FUNC_ID_SET_EVT_CB, lv_obj_set_event_cb_wrapper, 1, true }, { OBJ_FUNC_ID_SET_EVT_CB, lv_obj_set_event_cb_wrapper, 1, true },
}; };
/*************** Native Interface to Wasm App ***********/ /*************** Native Interface to Wasm App ***********/
void void
wasm_obj_native_call(wasm_exec_env_t exec_env, wasm_obj_native_call(wasm_exec_env_t exec_env, int32 func_id, uint32 *argv,
int32 func_id, uint32 *argv, uint32 argc) uint32 argc)
{ {
uint32 size = sizeof(obj_native_func_defs) / sizeof(WGLNativeFuncDef); uint32 size = sizeof(obj_native_func_defs) / sizeof(WGLNativeFuncDef);
wgl_native_func_call(exec_env, wgl_native_func_call(exec_env, obj_native_func_defs, size, func_id, argv,
obj_native_func_defs,
size,
func_id,
argv,
argc); argc);
} }

View File

@ -14,13 +14,14 @@
/* Queue of app manager */ /* Queue of app manager */
static bh_queue *g_app_mgr_queue; static bh_queue *g_app_mgr_queue;
void* void *
get_app_manager_queue() get_app_manager_queue()
{ {
return g_app_mgr_queue; return g_app_mgr_queue;
} }
void app_manager_post_applets_update_event() void
app_manager_post_applets_update_event()
{ {
module_data *m_data; module_data *m_data;
attr_container_t *attr_cont; attr_container_t *attr_cont;
@ -56,7 +57,8 @@ void app_manager_post_applets_update_event()
char buf[32]; char buf[32];
i++; i++;
snprintf(buf, sizeof(buf), "%s%d", "applet", i); snprintf(buf, sizeof(buf), "%s%d", "applet", i);
if (!(attr_container_set_string(&attr_cont, buf, m_data->module_name))) { if (!(attr_container_set_string(&attr_cont, buf,
m_data->module_name))) {
app_manager_printf("Post applets update event failed: " app_manager_printf("Post applets update event failed: "
"set attr applet name key failed."); "set attr applet name key failed.");
goto fail; goto fail;
@ -73,7 +75,7 @@ void app_manager_post_applets_update_event()
memset(&msg, 0, sizeof(msg)); memset(&msg, 0, sizeof(msg));
msg.url = url; msg.url = url;
msg.action = COAP_EVENT; msg.action = COAP_EVENT;
msg.payload = (char*) attr_cont; msg.payload = (char *)attr_cont;
send_request_to_host(&msg); send_request_to_host(&msg);
app_manager_printf("Post applets update event success!\n"); app_manager_printf("Post applets update event success!\n");
@ -84,7 +86,8 @@ fail:
attr_container_destroy(attr_cont); attr_container_destroy(attr_cont);
} }
static int get_applets_count() static int
get_applets_count()
{ {
module_data *m_data; module_data *m_data;
int num = 0; int num = 0;
@ -103,7 +106,8 @@ static int get_applets_count()
} }
/* Query fw apps info if name = NULL, otherwise query specify app */ /* Query fw apps info if name = NULL, otherwise query specify app */
static bool app_manager_query_applets(request_t *msg, const char *name) static bool
app_manager_query_applets(request_t *msg, const char *name)
{ {
module_data *m_data; module_data *m_data;
attr_container_t *attr_cont; attr_container_t *attr_cont;
@ -127,8 +131,8 @@ static bool app_manager_query_applets(request_t *msg, const char *name)
} }
if (name == NULL && !(attr_container_set_int(&attr_cont, "num", num))) { if (name == NULL && !(attr_container_set_int(&attr_cont, "num", num))) {
SEND_ERR_RESPONSE(msg->mid, SEND_ERR_RESPONSE(
"Query Applets failed: set attr container key failed."); msg->mid, "Query Applets failed: set attr container key failed.");
goto fail; goto fail;
} }
@ -140,10 +144,9 @@ static bool app_manager_query_applets(request_t *msg, const char *name)
i++; i++;
snprintf(buf, sizeof(buf), "%s%d", "applet", i); snprintf(buf, sizeof(buf), "%s%d", "applet", i);
if (!(attr_container_set_string(&attr_cont, buf, if (!(attr_container_set_string(&attr_cont, buf,
m_data->module_name))) { m_data->module_name))) {
SEND_ERR_RESPONSE(msg->mid, SEND_ERR_RESPONSE(msg->mid, "Query Applets failed: "
"Query Applets failed: " "set attr container key failed.");
"set attr container key failed.");
goto fail; goto fail;
} }
snprintf(buf, sizeof(buf), "%s%d", "heap", i); snprintf(buf, sizeof(buf), "%s%d", "heap", i);
@ -157,13 +160,13 @@ static bool app_manager_query_applets(request_t *msg, const char *name)
else if (!strcmp(name, m_data->module_name)) { else if (!strcmp(name, m_data->module_name)) {
found = true; found = true;
if (!(attr_container_set_string(&attr_cont, "name", if (!(attr_container_set_string(&attr_cont, "name",
m_data->module_name))) { m_data->module_name))) {
SEND_ERR_RESPONSE(msg->mid, SEND_ERR_RESPONSE(msg->mid, "Query Applet failed: "
"Query Applet failed: " "set attr container key failed.");
"set attr container key failed.");
goto fail; goto fail;
} }
if (!(attr_container_set_int(&attr_cont, "heap", m_data->heap_size))) { if (!(attr_container_set_int(&attr_cont, "heap",
m_data->heap_size))) {
SEND_ERR_RESPONSE(msg->mid, SEND_ERR_RESPONSE(msg->mid,
"Query Applet failed: " "Query Applet failed: "
"set attr container heap key failed."); "set attr container heap key failed.");
@ -176,15 +179,15 @@ static bool app_manager_query_applets(request_t *msg, const char *name)
if (name != NULL && !found) { if (name != NULL && !found) {
SEND_ERR_RESPONSE(msg->mid, SEND_ERR_RESPONSE(msg->mid,
"Query Applet failed: the app is not found."); "Query Applet failed: the app is not found.");
goto fail; goto fail;
} }
len = attr_container_get_serialize_length(attr_cont); len = attr_container_get_serialize_length(attr_cont);
make_response_for_request(msg, response); make_response_for_request(msg, response);
set_response(response, CONTENT_2_05, set_response(response, CONTENT_2_05, FMT_ATTR_CONTAINER, (char *)attr_cont,
FMT_ATTR_CONTAINER, (char*) attr_cont, len); len);
send_response_to_host(response); send_response_to_host(response);
ret = true; ret = true;
@ -197,7 +200,8 @@ fail:
return ret; return ret;
} }
void applet_mgt_reqeust_handler(request_t *request, void *unused) void
applet_mgt_reqeust_handler(request_t *request, void *unused)
{ {
bh_message_t msg; bh_message_t msg;
/* deep copy, but not use app self heap, but use global heap */ /* deep copy, but not use app self heap, but use global heap */
@ -216,13 +220,14 @@ void applet_mgt_reqeust_handler(request_t *request, void *unused)
} }
/* return -1 for error */ /* return -1 for error */
static int get_module_type(char *kv_str) static int
get_module_type(char *kv_str)
{ {
int module_type = -1; int module_type = -1;
char type_str[16] = { 0 }; char type_str[16] = { 0 };
find_key_value(kv_str, strlen(kv_str), "type", type_str, find_key_value(kv_str, strlen(kv_str), "type", type_str,
sizeof(type_str) - 1, '&'); sizeof(type_str) - 1, '&');
if (strlen(type_str) == 0) if (strlen(type_str) == 0)
module_type = Module_WASM_App; module_type = Module_WASM_App;
@ -240,34 +245,37 @@ static int get_module_type(char *kv_str)
/* Queue callback of App Manager */ /* Queue callback of App Manager */
static void app_manager_queue_callback(void *message, void *arg) static void
app_manager_queue_callback(void *message, void *arg)
{ {
request_t *request = (request_t *) bh_message_payload((bh_message_t)message); request_t *request = (request_t *)bh_message_payload((bh_message_t)message);
int mid = request->mid, module_type, offset; int mid = request->mid, module_type, offset;
(void)arg; (void)arg;
if ((offset = check_url_start(request->url, strlen(request->url), "/applet")) if ((offset =
> 0) { check_url_start(request->url, strlen(request->url), "/applet"))
> 0) {
module_type = get_module_type(request->url + offset); module_type = get_module_type(request->url + offset);
if (module_type == -1) { if (module_type == -1) {
SEND_ERR_RESPONSE(mid, SEND_ERR_RESPONSE(mid,
"Applet Management failed: invalid module type."); "Applet Management failed: invalid module type.");
goto fail; goto fail;
} }
/* Install Applet */ /* Install Applet */
if (request->action == COAP_PUT) { if (request->action == COAP_PUT) {
if (get_applets_count() >= MAX_APP_INSTALLATIONS) { if (get_applets_count() >= MAX_APP_INSTALLATIONS) {
SEND_ERR_RESPONSE(mid, SEND_ERR_RESPONSE(
"Install Applet failed: exceed max app installations."); mid,
"Install Applet failed: exceed max app installations.");
goto fail; goto fail;
} }
if (!request->payload) { if (!request->payload) {
SEND_ERR_RESPONSE(mid, SEND_ERR_RESPONSE(mid,
"Install Applet failed: invalid payload."); "Install Applet failed: invalid payload.");
goto fail; goto fail;
} }
if (g_module_interfaces[module_type] if (g_module_interfaces[module_type]
@ -280,14 +288,15 @@ static void app_manager_queue_callback(void *message, void *arg)
else if (request->action == COAP_DELETE) { else if (request->action == COAP_DELETE) {
module_type = get_module_type(request->url + offset); module_type = get_module_type(request->url + offset);
if (module_type == -1) { if (module_type == -1) {
SEND_ERR_RESPONSE(mid, SEND_ERR_RESPONSE(
"Uninstall Applet failed: invalid module type."); mid, "Uninstall Applet failed: invalid module type.");
goto fail; goto fail;
} }
if (g_module_interfaces[module_type] if (g_module_interfaces[module_type]
&& g_module_interfaces[module_type]->module_uninstall) { && g_module_interfaces[module_type]->module_uninstall) {
if (!g_module_interfaces[module_type]->module_uninstall(request)) if (!g_module_interfaces[module_type]->module_uninstall(
request))
goto fail; goto fail;
} }
} }
@ -308,7 +317,8 @@ static void app_manager_queue_callback(void *message, void *arg)
} }
/* Event Register/Unregister */ /* Event Register/Unregister */
else if ((offset = check_url_start(request->url, strlen(request->url), else if ((offset = check_url_start(request->url, strlen(request->url),
"/event/")) > 0) { "/event/"))
> 0) {
char url_buf[256] = { 0 }; char url_buf[256] = { 0 };
strncpy(url_buf, request->url + offset, sizeof(url_buf) - 1); strncpy(url_buf, request->url + offset, sizeof(url_buf) - 1);
@ -328,14 +338,14 @@ static void app_manager_queue_callback(void *message, void *arg)
break; break;
} }
} }
} }
fail: fail:
return; return;
} }
static void module_interfaces_init() static void
module_interfaces_init()
{ {
int i; int i;
for (i = 0; i < Module_Max; i++) { for (i = 0; i < Module_Max; i++) {
@ -344,7 +354,8 @@ static void module_interfaces_init()
} }
} }
void app_manager_startup(host_interface *interface) void
app_manager_startup(host_interface *interface)
{ {
module_interfaces_init(); module_interfaces_init();
@ -390,21 +401,20 @@ fail1:
module_interface *g_module_interfaces[Module_Max] = { module_interface *g_module_interfaces[Module_Max] = {
#if ENABLE_MODULE_JEFF != 0 #if ENABLE_MODULE_JEFF != 0
&jeff_module_interface, &jeff_module_interface,
#else #else
NULL, NULL,
#endif #endif
#if ENABLE_MODULE_WASM_APP != 0 #if ENABLE_MODULE_WASM_APP != 0
&wasm_app_module_interface, &wasm_app_module_interface,
#else #else
NULL, NULL,
#endif #endif
#if ENABLE_MODULE_WASM_LIB != 0 #if ENABLE_MODULE_WASM_LIB != 0
&wasm_lib_module_interface &wasm_lib_module_interface
#else #else
NULL NULL
#endif #endif
}; };

View File

@ -21,10 +21,11 @@ extern "C" {
/* os_printf is defined in each platform */ /* os_printf is defined in each platform */
#define app_manager_printf os_printf #define app_manager_printf os_printf
#define SEND_ERR_RESPONSE(mid, err_msg) do { \ #define SEND_ERR_RESPONSE(mid, err_msg) \
app_manager_printf("%s\n", err_msg); \ do { \
send_error_response_to_host(mid, INTERNAL_SERVER_ERROR_5_00, err_msg); \ app_manager_printf("%s\n", err_msg); \
} while (0) send_error_response_to_host(mid, INTERNAL_SERVER_ERROR_5_00, err_msg); \
} while (0)
extern module_interface *g_module_interfaces[Module_Max]; extern module_interface *g_module_interfaces[Module_Max];
@ -49,14 +50,15 @@ module_data_list_destroy();
bool bool
app_manager_is_interrupting_module(uint32 module_type, void *module_inst); app_manager_is_interrupting_module(uint32 module_type, void *module_inst);
void release_module(module_data *m_data); void
release_module(module_data *m_data);
void void
module_data_list_remove(module_data *m_data); module_data_list_remove(module_data *m_data);
void* void *
app_manager_timer_create(void (*timer_callback)(void*), app_manager_timer_create(void (*timer_callback)(void *),
watchdog_timer *wd_timer); watchdog_timer *wd_timer);
void void
app_manager_timer_destroy(void *timer); app_manager_timer_destroy(void *timer);
@ -67,18 +69,18 @@ app_manager_timer_start(void *timer, int timeout);
void void
app_manager_timer_stop(void *timer); app_manager_timer_stop(void *timer);
watchdog_timer* watchdog_timer *
app_manager_get_wd_timer_from_timer_handle(void *timer); app_manager_get_wd_timer_from_timer_handle(void *timer);
int int
app_manager_signature_verify(const uint8_t *file, unsigned int file_len, app_manager_signature_verify(const uint8_t *file, unsigned int file_len,
const uint8_t *signature, unsigned int sig_size); const uint8_t *signature, unsigned int sig_size);
void targeted_app_request_handler(request_t *request, void *unused); void
targeted_app_request_handler(request_t *request, void *unused);
#ifdef __cplusplus #ifdef __cplusplus
} /* end of extern "C" */ } /* end of extern "C" */
#endif #endif
#endif #endif

View File

@ -13,10 +13,7 @@
static host_interface host_commu; static host_interface host_commu;
/* IMRTLink Two leading bytes */ /* IMRTLink Two leading bytes */
static unsigned char leadings[] = { static unsigned char leadings[] = { (unsigned char)0x12, (unsigned char)0x34 };
(unsigned char)0x12,
(unsigned char)0x34
};
/* IMRTLink Receiving Phase */ /* IMRTLink Receiving Phase */
typedef enum recv_phase_t { typedef enum recv_phase_t {
@ -43,14 +40,16 @@ static korp_mutex host_lock;
static bool enable_log = false; static bool enable_log = false;
static bool is_little_endian() static bool
is_little_endian()
{ {
long i = 0x01020304; long i = 0x01020304;
unsigned char* c = (unsigned char*) &i; unsigned char *c = (unsigned char *)&i;
return (*c == 0x04) ? true : false; return (*c == 0x04) ? true : false;
} }
static void exchange32(uint8* pData) static void
exchange32(uint8 *pData)
{ {
uint8 value = *pData; uint8 value = *pData;
*pData = *(pData + 3); *pData = *(pData + 3);
@ -65,7 +64,8 @@ static void exchange32(uint8* pData)
* 1: complete message received * 1: complete message received
* 0: incomplete message received * 0: incomplete message received
*/ */
static int on_imrt_link_byte_arrive(unsigned char ch, recv_context_t *ctx) static int
on_imrt_link_byte_arrive(unsigned char ch, recv_context_t *ctx)
{ {
if (ctx->phase == Phase_Non_Start) { if (ctx->phase == Phase_Non_Start) {
ctx->message.payload_size = 0; ctx->message.payload_size = 0;
@ -88,7 +88,8 @@ static int on_imrt_link_byte_arrive(unsigned char ch, recv_context_t *ctx)
if (enable_log) if (enable_log)
app_manager_printf("##On byte arrive: got leading 1\n"); app_manager_printf("##On byte arrive: got leading 1\n");
ctx->phase = Phase_Type; ctx->phase = Phase_Type;
} else }
else
ctx->phase = Phase_Non_Start; ctx->phase = Phase_Non_Start;
return 0; return 0;
@ -111,7 +112,7 @@ static int on_imrt_link_byte_arrive(unsigned char ch, recv_context_t *ctx)
return 0; return 0;
} }
else if (ctx->phase == Phase_Size) { else if (ctx->phase == Phase_Size) {
unsigned char *p = (unsigned char *) &ctx->message.payload_size; unsigned char *p = (unsigned char *)&ctx->message.payload_size;
if (enable_log) if (enable_log)
app_manager_printf("##On byte arrive: got payload_size, byte %d\n", app_manager_printf("##On byte arrive: got payload_size, byte %d\n",
@ -141,7 +142,7 @@ static int on_imrt_link_byte_arrive(unsigned char ch, recv_context_t *ctx)
if (ctx->message.message_type != INSTALL_WASM_APP) { if (ctx->message.message_type != INSTALL_WASM_APP) {
ctx->message.payload = ctx->message.payload =
(char *) APP_MGR_MALLOC(ctx->message.payload_size); (char *)APP_MGR_MALLOC(ctx->message.payload_size);
if (!ctx->message.payload) { if (!ctx->message.payload) {
ctx->phase = Phase_Non_Start; ctx->phase = Phase_Non_Start;
return 0; return 0;
@ -158,7 +159,7 @@ static int on_imrt_link_byte_arrive(unsigned char ch, recv_context_t *ctx)
if (ctx->message.message_type == INSTALL_WASM_APP) { if (ctx->message.message_type == INSTALL_WASM_APP) {
int received_size; int received_size;
module_on_install_request_byte_arrive_func module_on_install = module_on_install_request_byte_arrive_func module_on_install =
g_module_interfaces[Module_WASM_App]->module_on_install; g_module_interfaces[Module_WASM_App]->module_on_install;
ctx->size_in_phase++; ctx->size_in_phase++;
@ -216,7 +217,8 @@ static int on_imrt_link_byte_arrive(unsigned char ch, recv_context_t *ctx)
return 0; return 0;
} }
int aee_host_msg_callback(void *msg, uint32_t msg_len) int
aee_host_msg_callback(void *msg, uint32_t msg_len)
{ {
unsigned char *p = msg, *p_end = p + msg_len; unsigned char *p = msg, *p_end = p + msg_len;
@ -234,7 +236,8 @@ int aee_host_msg_callback(void *msg, uint32_t msg_len)
memset(&request, 0, sizeof(request)); memset(&request, 0, sizeof(request));
if (!unpack_request(recv_ctx.message.payload, if (!unpack_request(recv_ctx.message.payload,
recv_ctx.message.payload_size, &request)) recv_ctx.message.payload_size,
&request))
continue; continue;
request.sender = ID_HOST; request.sender = ID_HOST;
@ -242,7 +245,8 @@ int aee_host_msg_callback(void *msg, uint32_t msg_len)
am_dispatch_request(&request); am_dispatch_request(&request);
} }
else { else {
app_manager_printf("unexpected host msg type: %d\n", msg_type); app_manager_printf("unexpected host msg type: %d\n",
msg_type);
} }
APP_MGR_FREE(recv_ctx.message.payload); APP_MGR_FREE(recv_ctx.message.payload);
@ -257,7 +261,8 @@ int aee_host_msg_callback(void *msg, uint32_t msg_len)
return 0; return 0;
} }
bool app_manager_host_init(host_interface *interface) bool
app_manager_host_init(host_interface *interface)
{ {
os_mutex_init(&host_lock); os_mutex_init(&host_lock);
memset(&recv_ctx, 0, sizeof(recv_ctx)); memset(&recv_ctx, 0, sizeof(recv_ctx));
@ -267,12 +272,13 @@ bool app_manager_host_init(host_interface *interface)
host_commu.destroy = interface->destroy; host_commu.destroy = interface->destroy;
if (host_commu.init != NULL) if (host_commu.init != NULL)
return host_commu.init(); return host_commu.init();
return true; return true;
} }
int app_manager_host_send_msg(int msg_type, const char *buf, int size) int
app_manager_host_send_msg(int msg_type, const char *buf, int size)
{ {
/* send an IMRT LINK message contains the buf as payload */ /* send an IMRT LINK message contains the buf as payload */
if (host_commu.send != NULL) { if (host_commu.send != NULL) {
@ -285,11 +291,11 @@ int app_manager_host_send_msg(int msg_type, const char *buf, int size)
/* message type */ /* message type */
/* TODO: check if use network byte order!!! */ /* TODO: check if use network byte order!!! */
*((uint16*)(header + 2)) = htons(msg_type); *((uint16 *)(header + 2)) = htons(msg_type);
/* payload length */ /* payload length */
if (is_little_endian()) if (is_little_endian())
exchange32((uint8*) &size_s); exchange32((uint8 *)&size_s);
bh_memcpy_s(header + 4, 4, &size_s, 4); bh_memcpy_s(header + 4, 4, &size_s, 4);
n = host_commu.send(NULL, header, 8); n = host_commu.send(NULL, header, 8);

View File

@ -12,7 +12,7 @@
extern "C" { extern "C" {
#endif #endif
#define HOST_MODE_AON 1 #define HOST_MODE_AON 1
#define HOST_MODE_UART 2 #define HOST_MODE_UART 2
#define HOST_MODE_TEST 3 #define HOST_MODE_TEST 3
@ -21,4 +21,3 @@ extern "C" {
#endif #endif
#endif #endif

View File

@ -5,7 +5,7 @@
#if 0 #if 0
#define BLUETOOTH_INTERFACE_ADVERTISMENT_DATA_LENGTH 31 #define BLUETOOTH_INTERFACE_ADVERTISMENT_DATA_LENGTH 31
/* ble_device_info */ /* ble_device_info */
typedef struct ble_device_info { typedef struct ble_device_info {

View File

@ -11,25 +11,26 @@
#include "coap_ext.h" #include "coap_ext.h"
typedef struct _subscribe { typedef struct _subscribe {
struct _subscribe * next; struct _subscribe *next;
uint32 subscriber_id; uint32 subscriber_id;
} subscribe_t; } subscribe_t;
typedef struct _event { typedef struct _event {
struct _event *next; struct _event *next;
int subscriber_size; int subscriber_size;
subscribe_t * subscribers; subscribe_t *subscribers;
char url[1]; /* event url */ char url[1]; /* event url */
} event_reg_t; } event_reg_t;
event_reg_t *g_events = NULL; event_reg_t *g_events = NULL;
static bool find_subscriber(event_reg_t * reg, uint32 id, bool remove_found) static bool
find_subscriber(event_reg_t *reg, uint32 id, bool remove_found)
{ {
subscribe_t* c = reg->subscribers; subscribe_t *c = reg->subscribers;
subscribe_t * prev = NULL; subscribe_t *prev = NULL;
while (c) { while (c) {
subscribe_t * next = c->next; subscribe_t *next = c->next;
if (c->subscriber_id == id) { if (c->subscriber_id == id) {
if (remove_found) { if (remove_found) {
if (prev) if (prev)
@ -41,7 +42,8 @@ static bool find_subscriber(event_reg_t * reg, uint32 id, bool remove_found)
} }
return true; return true;
} else { }
else {
prev = c; prev = c;
c = next; c = next;
} }
@ -50,7 +52,8 @@ static bool find_subscriber(event_reg_t * reg, uint32 id, bool remove_found)
return false; return false;
} }
static bool check_url(const char *url) static bool
check_url(const char *url)
{ {
if (*url == 0) if (*url == 0)
return false; return false;
@ -58,7 +61,8 @@ static bool check_url(const char *url)
return true; return true;
} }
bool am_register_event(const char *url, uint32_t reg_client) bool
am_register_event(const char *url, uint32_t reg_client)
{ {
event_reg_t *current = g_events; event_reg_t *current = g_events;
@ -76,8 +80,8 @@ bool am_register_event(const char *url, uint32_t reg_client)
if (current == NULL) { if (current == NULL) {
if (NULL if (NULL
== (current = (event_reg_t *) APP_MGR_MALLOC( == (current = (event_reg_t *)APP_MGR_MALLOC(
offsetof(event_reg_t, url) + strlen(url) + 1))) { offsetof(event_reg_t, url) + strlen(url) + 1))) {
app_manager_printf("am_register_event: malloc fail\n"); app_manager_printf("am_register_event: malloc fail\n");
return false; return false;
} }
@ -90,8 +94,9 @@ bool am_register_event(const char *url, uint32_t reg_client)
if (find_subscriber(current, reg_client, false)) { if (find_subscriber(current, reg_client, false)) {
return true; return true;
} else { }
subscribe_t * s = (subscribe_t*) APP_MGR_MALLOC(sizeof(subscribe_t)); else {
subscribe_t *s = (subscribe_t *)APP_MGR_MALLOC(sizeof(subscribe_t));
if (s == NULL) if (s == NULL)
return false; return false;
@ -100,29 +105,30 @@ bool am_register_event(const char *url, uint32_t reg_client)
s->next = current->subscribers; s->next = current->subscribers;
current->subscribers = s; current->subscribers = s;
app_manager_printf("client: %d registered event (%s)\n", reg_client, app_manager_printf("client: %d registered event (%s)\n", reg_client,
url); url);
} }
return true; return true;
} }
// @url: NULL means the client wants to unregister all its subscribed items // @url: NULL means the client wants to unregister all its subscribed items
bool am_unregister_event(const char *url, uint32_t reg_client) bool
am_unregister_event(const char *url, uint32_t reg_client)
{ {
event_reg_t *current = g_events, *pre = NULL; event_reg_t *current = g_events, *pre = NULL;
while (current != NULL) { while (current != NULL) {
if (url == NULL || strcmp(current->url, url) == 0) { if (url == NULL || strcmp(current->url, url) == 0) {
event_reg_t * next = current->next; event_reg_t *next = current->next;
if (find_subscriber(current, reg_client, true)) { if (find_subscriber(current, reg_client, true)) {
app_manager_printf("client: %d deregistered event (%s)\n", app_manager_printf("client: %d deregistered event (%s)\n",
reg_client, current->url); reg_client, current->url);
} }
// remove the registration if no client subscribe it // remove the registration if no client subscribe it
if (current->subscribers == NULL) { if (current->subscribers == NULL) {
app_manager_printf("unregister for event deleted url:(%s)\n", app_manager_printf("unregister for event deleted url:(%s)\n",
current->url); current->url);
if (pre) if (pre)
pre->next = next; pre->next = next;
else else
@ -139,33 +145,38 @@ bool am_unregister_event(const char *url, uint32_t reg_client)
return true; return true;
} }
bool event_handle_event_request(uint8_t code, const char *event_url, bool
uint32_t reg_client) event_handle_event_request(uint8_t code, const char *event_url,
uint32_t reg_client)
{ {
if (code == COAP_PUT) { /* register */ if (code == COAP_PUT) { /* register */
return am_register_event(event_url, reg_client); return am_register_event(event_url, reg_client);
} else if (code == COAP_DELETE) { /* unregister */ }
else if (code == COAP_DELETE) { /* unregister */
return am_unregister_event(event_url, reg_client); return am_unregister_event(event_url, reg_client);
} else { }
else {
/* invalid request */ /* invalid request */
return false; return false;
} }
} }
void am_publish_event(request_t * event) void
am_publish_event(request_t *event)
{ {
bh_assert(event->action == COAP_EVENT); bh_assert(event->action == COAP_EVENT);
event_reg_t *current = g_events; event_reg_t *current = g_events;
while (current) { while (current) {
if (0 == strcmp(event->url, current->url)) { if (0 == strcmp(event->url, current->url)) {
subscribe_t* c = current->subscribers; subscribe_t *c = current->subscribers;
while (c) { while (c) {
if (c->subscriber_id == ID_HOST) { if (c->subscriber_id == ID_HOST) {
send_request_to_host(event); send_request_to_host(event);
} else { }
module_request_handler else {
(event, (void *)(uintptr_t)c->subscriber_id); module_request_handler(event,
(void *)(uintptr_t)c->subscriber_id);
} }
c = c->next; c = c->next;
} }
@ -177,7 +188,8 @@ void am_publish_event(request_t * event)
} }
} }
bool event_is_registered(const char *event_url) bool
event_is_registered(const char *event_url)
{ {
event_reg_t *current = g_events; event_reg_t *current = g_events;

View File

@ -22,7 +22,7 @@ extern "C" {
*/ */
bool bool
event_handle_event_request(uint8_t code, const char *event_url, event_handle_event_request(uint8_t code, const char *event_url,
uint32_t register); uint32_t register);
/** /**
* Test whether the event is registered * Test whether the event is registered

View File

@ -26,7 +26,8 @@ bool send_coap_packet_to_host(coap_packet_t * packet)
} }
#endif #endif
bool send_request_to_host(request_t *msg) bool
send_request_to_host(request_t *msg)
{ {
if (COAP_EVENT == msg->action && !event_is_registered(msg->url)) { if (COAP_EVENT == msg->action && !event_is_registered(msg->url)) {
app_manager_printf("Event is not registered\n"); app_manager_printf("Event is not registered\n");
@ -34,7 +35,7 @@ bool send_request_to_host(request_t *msg)
} }
int size; int size;
char * packet = pack_request(msg, &size); char *packet = pack_request(msg, &size);
if (packet == NULL) if (packet == NULL)
return false; return false;
@ -45,10 +46,11 @@ bool send_request_to_host(request_t *msg)
return true; return true;
} }
bool send_response_to_host(response_t *response) bool
send_response_to_host(response_t *response)
{ {
int size; int size;
char * packet = pack_response(response, &size); char *packet = pack_response(response, &size);
if (packet == NULL) if (packet == NULL)
return false; return false;
@ -59,7 +61,8 @@ bool send_response_to_host(response_t *response)
return true; return true;
} }
bool send_error_response_to_host(int mid, int status, const char *msg) bool
send_error_response_to_host(int mid, int status, const char *msg)
{ {
int payload_len = 0; int payload_len = 0;
attr_container_t *payload = NULL; attr_container_t *payload = NULL;
@ -73,8 +76,8 @@ bool send_error_response_to_host(int mid, int status, const char *msg)
} }
} }
set_response(response, status, FMT_ATTR_CONTAINER, set_response(response, status, FMT_ATTR_CONTAINER, (const char *)payload,
(const char *)payload, payload_len); payload_len);
response->mid = mid; response->mid = mid;
send_response_to_host(response); send_response_to_host(response);
@ -83,4 +86,3 @@ bool send_error_response_to_host(int mid, int status, const char *msg)
attr_container_destroy(payload); attr_container_destroy(payload);
return true; return true;
} }

File diff suppressed because it is too large Load Diff

View File

@ -17,13 +17,15 @@ korp_mutex module_data_list_lock;
/* Module data list */ /* Module data list */
module_data *module_data_list; module_data *module_data_list;
bool module_data_list_init() bool
module_data_list_init()
{ {
module_data_list = NULL; module_data_list = NULL;
return !os_mutex_init(&module_data_list_lock) ? true : false; return !os_mutex_init(&module_data_list_lock) ? true : false;
} }
void module_data_list_destroy() void
module_data_list_destroy()
{ {
os_mutex_lock(&module_data_list_lock); os_mutex_lock(&module_data_list_lock);
@ -38,7 +40,8 @@ void module_data_list_destroy()
os_mutex_destroy(&module_data_list_lock); os_mutex_destroy(&module_data_list_lock);
} }
static void module_data_list_add(module_data *m_data) static void
module_data_list_add(module_data *m_data)
{ {
static uint32 module_id_max = 1; static uint32 module_id_max = 1;
os_mutex_lock(&module_data_list_lock); os_mutex_lock(&module_data_list_lock);
@ -49,7 +52,8 @@ static void module_data_list_add(module_data *m_data)
m_data->id = module_id_max++; m_data->id = module_id_max++;
if (!module_data_list) { if (!module_data_list) {
module_data_list = m_data; module_data_list = m_data;
} else { }
else {
/* Set as head */ /* Set as head */
m_data->next = module_data_list; m_data->next = module_data_list;
module_data_list = m_data; module_data_list = m_data;
@ -57,7 +61,8 @@ static void module_data_list_add(module_data *m_data)
os_mutex_unlock(&module_data_list_lock); os_mutex_unlock(&module_data_list_lock);
} }
void module_data_list_remove(module_data *m_data) void
module_data_list_remove(module_data *m_data)
{ {
os_mutex_lock(&module_data_list_lock); os_mutex_lock(&module_data_list_lock);
if (module_data_list) { if (module_data_list) {
@ -76,7 +81,7 @@ void module_data_list_remove(module_data *m_data)
os_mutex_unlock(&module_data_list_lock); os_mutex_unlock(&module_data_list_lock);
} }
module_data* module_data *
module_data_list_lookup(const char *module_name) module_data_list_lookup(const char *module_name)
{ {
os_mutex_lock(&module_data_list_lock); os_mutex_lock(&module_data_list_lock);
@ -96,7 +101,7 @@ module_data_list_lookup(const char *module_name)
return NULL; return NULL;
} }
module_data* module_data *
module_data_list_lookup_id(unsigned int module_id) module_data_list_lookup_id(unsigned int module_id)
{ {
os_mutex_lock(&module_data_list_lock); os_mutex_lock(&module_data_list_lock);
@ -119,72 +124,78 @@ module_data_list_lookup_id(unsigned int module_id)
module_data * module_data *
app_manager_get_module_data(uint32 module_type, void *module_inst) app_manager_get_module_data(uint32 module_type, void *module_inst)
{ {
if (module_type < Module_Max if (module_type < Module_Max && g_module_interfaces[module_type]
&& g_module_interfaces[module_type]
&& g_module_interfaces[module_type]->module_get_module_data) && g_module_interfaces[module_type]->module_get_module_data)
return g_module_interfaces[module_type]->module_get_module_data(module_inst); return g_module_interfaces[module_type]->module_get_module_data(
module_inst);
return NULL; return NULL;
} }
void* void *
app_manager_get_module_queue(uint32 module_type, void *module_inst) app_manager_get_module_queue(uint32 module_type, void *module_inst)
{ {
module_data *m_data = app_manager_get_module_data(module_type, module_inst); module_data *m_data = app_manager_get_module_data(module_type, module_inst);
return m_data ? m_data->queue : NULL; return m_data ? m_data->queue : NULL;
} }
const char* const char *
app_manager_get_module_name(uint32 module_type, void *module_inst) app_manager_get_module_name(uint32 module_type, void *module_inst)
{ {
module_data *m_data = app_manager_get_module_data(module_type, module_inst); module_data *m_data = app_manager_get_module_data(module_type, module_inst);
return m_data ? m_data->module_name : NULL; return m_data ? m_data->module_name : NULL;
} }
unsigned int app_manager_get_module_id(uint32 module_type, void *module_inst) unsigned int
app_manager_get_module_id(uint32 module_type, void *module_inst)
{ {
module_data *m_data = app_manager_get_module_data(module_type, module_inst); module_data *m_data = app_manager_get_module_data(module_type, module_inst);
return m_data ? m_data->id : ID_NONE; return m_data ? m_data->id : ID_NONE;
} }
void* void *
app_manager_get_module_heap(uint32 module_type, void *module_inst) app_manager_get_module_heap(uint32 module_type, void *module_inst)
{ {
module_data *m_data = app_manager_get_module_data(module_type, module_inst); module_data *m_data = app_manager_get_module_data(module_type, module_inst);
return m_data ? m_data->heap : NULL; return m_data ? m_data->heap : NULL;
} }
module_data* module_data *
app_manager_lookup_module_data(const char *name) app_manager_lookup_module_data(const char *name)
{ {
return module_data_list_lookup(name); return module_data_list_lookup(name);
} }
void app_manager_add_module_data(module_data *m_data) void
app_manager_add_module_data(module_data *m_data)
{ {
module_data_list_add(m_data); module_data_list_add(m_data);
} }
void app_manager_del_module_data(module_data *m_data) void
app_manager_del_module_data(module_data *m_data)
{ {
module_data_list_remove(m_data); module_data_list_remove(m_data);
release_module(m_data); release_module(m_data);
} }
bool app_manager_is_interrupting_module(uint32 module_type, void *module_inst) bool
app_manager_is_interrupting_module(uint32 module_type, void *module_inst)
{ {
module_data *m_data = app_manager_get_module_data(module_type, module_inst); module_data *m_data = app_manager_get_module_data(module_type, module_inst);
return m_data ? m_data->wd_timer.is_interrupting : false; return m_data ? m_data->wd_timer.is_interrupting : false;
} }
extern void destroy_module_timer_ctx(unsigned int module_id); extern void
destroy_module_timer_ctx(unsigned int module_id);
void release_module(module_data *m_data) void
release_module(module_data *m_data)
{ {
watchdog_timer_destroy(&m_data->wd_timer); watchdog_timer_destroy(&m_data->wd_timer);
#ifdef HEAP_ENABLED /* TODO */ #ifdef HEAP_ENABLED /* TODO */
if(m_data->heap) if (m_data->heap)
gc_destroy_for_instance(m_data->heap); gc_destroy_for_instance(m_data->heap);
#endif #endif
@ -198,7 +209,8 @@ void release_module(module_data *m_data)
APP_MGR_FREE(m_data); APP_MGR_FREE(m_data);
} }
uint32 check_modules_timer_expiry() uint32
check_modules_timer_expiry()
{ {
os_mutex_lock(&module_data_list_lock); os_mutex_lock(&module_data_list_lock);
module_data *p = module_data_list; module_data *p = module_data_list;
@ -216,4 +228,3 @@ uint32 check_modules_timer_expiry()
os_mutex_unlock(&module_data_list_lock); os_mutex_unlock(&module_data_list_lock);
return ms_to_expiry; return ms_to_expiry;
} }

File diff suppressed because it is too large Load Diff

View File

@ -56,7 +56,7 @@ typedef struct wasm_data {
/* thread list mapped with this WASM module */ /* thread list mapped with this WASM module */
korp_tid thread_id; korp_tid thread_id;
/* for easily access the containing module data */ /* for easily access the containing module data */
module_data* m_data; module_data *m_data;
/* is bytecode or aot */ /* is bytecode or aot */
bool is_bytecode; bool is_bytecode;
/* sections of wasm bytecode or aot file */ /* sections of wasm bytecode or aot file */
@ -109,16 +109,18 @@ typedef struct wasm_app_file_t {
extern module_interface wasm_app_module_interface; extern module_interface wasm_app_module_interface;
typedef void (*message_type_handler_t)(module_data *m_data, bh_message_t msg); typedef void (*message_type_handler_t)(module_data *m_data, bh_message_t msg);
extern bool wasm_register_msg_callback(int msg_type, extern bool
message_type_handler_t message_handler); wasm_register_msg_callback(int msg_type,
message_type_handler_t message_handler);
typedef void (*resource_cleanup_handler_t)(uint32 module_id); typedef void (*resource_cleanup_handler_t)(uint32 module_id);
extern bool wasm_register_cleanup_callback(resource_cleanup_handler_t handler); extern bool
wasm_register_cleanup_callback(resource_cleanup_handler_t handler);
/** /**
* Set WASI root dir for modules. On each wasm app installation, a sub dir named * Set WASI root dir for modules. On each wasm app installation, a sub dir named
* with the app's name will be created autamically. That wasm app can only access * with the app's name will be created autamically. That wasm app can only
* this sub dir. * access this sub dir.
* *
* @param root_dir the root dir to set * @param root_dir the root dir to set
* @return true for success, false otherwise * @return true for success, false otherwise

View File

@ -3,47 +3,56 @@
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/ */
#include "module_wasm_lib.h" #include "module_wasm_lib.h"
static bool wasm_lib_module_init(void) static bool
wasm_lib_module_init(void)
{ {
return false; return false;
} }
static bool wasm_lib_module_install(request_t *msg) static bool
wasm_lib_module_install(request_t *msg)
{ {
(void) msg; (void)msg;
return false; return false;
} }
static bool wasm_lib_module_uninstall(request_t *msg) static bool
wasm_lib_module_uninstall(request_t *msg)
{ {
(void) msg; (void)msg;
return false; return false;
} }
static void wasm_lib_module_watchdog_kill(module_data *m_data) static void
wasm_lib_module_watchdog_kill(module_data *m_data)
{ {
(void) m_data; (void)m_data;
} }
static bool wasm_lib_module_handle_host_url(void *queue_msg) static bool
wasm_lib_module_handle_host_url(void *queue_msg)
{ {
(void) queue_msg; (void)queue_msg;
return false; return false;
} }
static module_data* static module_data *
wasm_lib_module_get_module_data(void *inst) wasm_lib_module_get_module_data(void *inst)
{ {
(void) inst; (void)inst;
return NULL; return NULL;
} }
module_interface wasm_lib_module_interface = { wasm_lib_module_init, /* clang-format off */
wasm_lib_module_install, wasm_lib_module_uninstall, module_interface wasm_lib_module_interface = {
wasm_lib_module_watchdog_kill, wasm_lib_module_handle_host_url, wasm_lib_module_init,
wasm_lib_module_get_module_data, wasm_lib_module_install,
NULL }; wasm_lib_module_uninstall,
wasm_lib_module_watchdog_kill,
wasm_lib_module_handle_host_url,
wasm_lib_module_get_module_data,
NULL
};
/* clang-format on */

View File

@ -5,25 +5,28 @@
#include "app_manager.h" #include "app_manager.h"
void* void *
app_manager_timer_create(void (*timer_callback)(void*), app_manager_timer_create(void (*timer_callback)(void *),
watchdog_timer *wd_timer) watchdog_timer *wd_timer)
{ {
/* TODO */ /* TODO */
return NULL; return NULL;
} }
void app_manager_timer_destroy(void *timer) void
app_manager_timer_destroy(void *timer)
{ {
/* TODO */ /* TODO */
} }
void app_manager_timer_start(void *timer, int timeout) void
app_manager_timer_start(void *timer, int timeout)
{ {
/* TODO */ /* TODO */
} }
void app_manager_timer_stop(void *timer) void
app_manager_timer_stop(void *timer)
{ {
/* TODO */ /* TODO */
} }
@ -35,9 +38,9 @@ app_manager_get_wd_timer_from_timer_handle(void *timer)
return NULL; return NULL;
} }
int app_manager_signature_verify(const uint8_t *file, unsigned int file_len, int
const uint8_t *signature, unsigned int sig_size) app_manager_signature_verify(const uint8_t *file, unsigned int file_len,
const uint8_t *signature, unsigned int sig_size)
{ {
return 1; return 1;
} }

View File

@ -16,32 +16,36 @@ typedef struct k_timer_watchdog {
watchdog_timer *wd_timer; watchdog_timer *wd_timer;
} k_timer_watchdog; } k_timer_watchdog;
void* void *
app_manager_timer_create(void (*timer_callback)(void*), app_manager_timer_create(void (*timer_callback)(void *),
watchdog_timer *wd_timer) watchdog_timer *wd_timer)
{ {
struct k_timer_watchdog *timer = APP_MGR_MALLOC(sizeof(struct k_timer_watchdog)); struct k_timer_watchdog *timer =
APP_MGR_MALLOC(sizeof(struct k_timer_watchdog));
if (timer) { if (timer) {
k_timer_init(&timer->timer, (void (*)(struct k_timer*)) timer_callback, k_timer_init(&timer->timer, (void (*)(struct k_timer *))timer_callback,
NULL); NULL);
timer->wd_timer = wd_timer; timer->wd_timer = wd_timer;
} }
return timer; return timer;
} }
void app_manager_timer_destroy(void *timer) void
app_manager_timer_destroy(void *timer)
{ {
APP_MGR_FREE(timer); APP_MGR_FREE(timer);
} }
void app_manager_timer_start(void *timer, int timeout) void
app_manager_timer_start(void *timer, int timeout)
{ {
k_timer_start(timer, Z_TIMEOUT_MS(timeout), Z_TIMEOUT_MS(0)); k_timer_start(timer, Z_TIMEOUT_MS(timeout), Z_TIMEOUT_MS(0));
} }
void app_manager_timer_stop(void *timer) void
app_manager_timer_stop(void *timer)
{ {
k_timer_stop(timer); k_timer_stop(timer);
} }
@ -49,7 +53,7 @@ void app_manager_timer_stop(void *timer)
watchdog_timer * watchdog_timer *
app_manager_get_wd_timer_from_timer_handle(void *timer) app_manager_get_wd_timer_from_timer_handle(void *timer)
{ {
return ((k_timer_watchdog*) timer)->wd_timer; return ((k_timer_watchdog *)timer)->wd_timer;
} }
#if 0 #if 0
int app_manager_signature_verify(const uint8_t *file, unsigned int file_len, int app_manager_signature_verify(const uint8_t *file, unsigned int file_len,

View File

@ -3,7 +3,6 @@
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/ */
#include "native_interface.h" #include "native_interface.h"
#include "app_manager.h" #include "app_manager.h"
#include "app_manager_export.h" #include "app_manager_export.h"
@ -13,14 +12,15 @@
typedef struct _app_res_register { typedef struct _app_res_register {
struct _app_res_register *next; struct _app_res_register *next;
char * url; char *url;
void (*request_handler)(request_t *, void *); void (*request_handler)(request_t *, void *);
uint32 register_id; uint32 register_id;
} app_res_register_t; } app_res_register_t;
static app_res_register_t * g_resources = NULL; static app_res_register_t *g_resources = NULL;
void module_request_handler(request_t *request, void *user_data) void
module_request_handler(request_t *request, void *user_data)
{ {
unsigned int mod_id = (unsigned int)(uintptr_t)user_data; unsigned int mod_id = (unsigned int)(uintptr_t)user_data;
bh_message_t msg; bh_message_t msg;
@ -54,10 +54,11 @@ void module_request_handler(request_t *request, void *user_data)
} }
app_manager_printf("Send request to app %s success.\n", app_manager_printf("Send request to app %s success.\n",
m_data->module_name); m_data->module_name);
} }
void targeted_app_request_handler(request_t *request, void *unused) void
targeted_app_request_handler(request_t *request, void *unused)
{ {
char applet_name[128] = { 0 }; char applet_name[128] = { 0 };
int offset; int offset;
@ -74,7 +75,8 @@ void targeted_app_request_handler(request_t *request, void *unused)
char *p = strchr(applet_name, '/'); char *p = strchr(applet_name, '/');
if (p) { if (p) {
*p = 0; *p = 0;
} else }
else
return; return;
app_manager_printf("Send request to applet: %s\n", applet_name); app_manager_printf("Send request to applet: %s\n", applet_name);
@ -84,16 +86,17 @@ void targeted_app_request_handler(request_t *request, void *unused)
m_data = module_data_list_lookup(applet_name); m_data = module_data_list_lookup(applet_name);
if (!m_data) { if (!m_data) {
SEND_ERR_RESPONSE(request->mid, SEND_ERR_RESPONSE(request->mid,
"Send request to applet failed: invalid applet name"); "Send request to applet failed: invalid applet name");
goto end; goto end;
} }
module_request_handler(request, (void *)(uintptr_t)m_data->id); module_request_handler(request, (void *)(uintptr_t)m_data->id);
end: request->url = url; end:
request->url = url;
} }
void am_send_response(response_t *response) void
am_send_response(response_t *response)
{ {
module_data *m_data; module_data *m_data;
@ -101,15 +104,15 @@ void am_send_response(response_t *response)
m_data = module_data_list_lookup_id(response->reciever); m_data = module_data_list_lookup_id(response->reciever);
if (!m_data) { if (!m_data) {
send_response_to_host(response); send_response_to_host(response);
}
} else { else {
response_t * resp_for_send = clone_response(response); response_t *resp_for_send = clone_response(response);
if (!resp_for_send) { if (!resp_for_send) {
return; return;
} }
bh_message_t msg = bh_new_msg(RESTFUL_RESPONSE, resp_for_send, bh_message_t msg = bh_new_msg(RESTFUL_RESPONSE, resp_for_send,
sizeof(*resp_for_send), response_cleaner); sizeof(*resp_for_send), response_cleaner);
if (!msg) { if (!msg) {
response_cleaner(resp_for_send); response_cleaner(resp_for_send);
return; return;
@ -121,7 +124,8 @@ void am_send_response(response_t *response)
} }
} }
void * am_dispatch_request(request_t *request) void *
am_dispatch_request(request_t *request)
{ {
app_res_register_t *r = g_resources; app_res_register_t *r = g_resources;
@ -135,11 +139,12 @@ void * am_dispatch_request(request_t *request)
return NULL; return NULL;
} }
bool am_register_resource(const char *url, bool
void (*request_handler)(request_t *, void *), am_register_resource(const char *url,
uint32 register_id) void (*request_handler)(request_t *, void *),
uint32 register_id)
{ {
app_res_register_t * r = g_resources; app_res_register_t *r = g_resources;
int register_num = 0; int register_num = 0;
while (r) { while (r) {
@ -178,10 +183,11 @@ bool am_register_resource(const char *url,
return true; return true;
} }
void am_cleanup_registeration(uint32 register_id) void
am_cleanup_registeration(uint32 register_id)
{ {
app_res_register_t * r = g_resources; app_res_register_t *r = g_resources;
app_res_register_t * prev = NULL; app_res_register_t *prev = NULL;
while (r) { while (r) {
app_res_register_t *next = r->next; app_res_register_t *next = r->next;
@ -194,7 +200,8 @@ void am_cleanup_registeration(uint32 register_id)
APP_MGR_FREE(r->url); APP_MGR_FREE(r->url);
APP_MGR_FREE(r); APP_MGR_FREE(r);
} else }
else
/* if r is freed, should not change prev. Only set prev to r /* if r is freed, should not change prev. Only set prev to r
when r isn't freed. */ when r isn't freed. */
prev = r; prev = r;

View File

@ -12,10 +12,11 @@
static bh_queue *watchdog_queue; static bh_queue *watchdog_queue;
#ifdef WATCHDOG_ENABLED /* TODO */ #ifdef WATCHDOG_ENABLED /* TODO */
static void watchdog_timer_callback(void *timer) static void
watchdog_timer_callback(void *timer)
{ {
watchdog_timer *wd_timer = app_manager_get_wd_timer_from_timer_handle( watchdog_timer *wd_timer =
timer); app_manager_get_wd_timer_from_timer_handle(timer);
watchdog_timer_stop(wd_timer); watchdog_timer_stop(wd_timer);
@ -26,14 +27,15 @@ static void watchdog_timer_callback(void *timer)
wd_timer->is_interrupting = true; wd_timer->is_interrupting = true;
bh_post_msg(watchdog_queue, WD_TIMEOUT, wd_timer->module_data, bh_post_msg(watchdog_queue, WD_TIMEOUT, wd_timer->module_data,
sizeof(module_data)); sizeof(module_data));
} }
os_mutex_unlock(&wd_timer->lock); os_mutex_unlock(&wd_timer->lock);
} }
#endif #endif
bool watchdog_timer_init(module_data *m_data) bool
watchdog_timer_init(module_data *m_data)
{ {
#ifdef WATCHDOG_ENABLED /* TODO */ #ifdef WATCHDOG_ENABLED /* TODO */
watchdog_timer *wd_timer = &m_data->wd_timer; watchdog_timer *wd_timer = &m_data->wd_timer;
@ -42,7 +44,7 @@ bool watchdog_timer_init(module_data *m_data)
return false; return false;
if (!(wd_timer->timer_handle = if (!(wd_timer->timer_handle =
app_manager_timer_create(watchdog_timer_callback, wd_timer))) { app_manager_timer_create(watchdog_timer_callback, wd_timer))) {
os_mutex_destroy(&wd_timer->lock); os_mutex_destroy(&wd_timer->lock);
return false; return false;
} }
@ -54,7 +56,8 @@ bool watchdog_timer_init(module_data *m_data)
return true; return true;
} }
void watchdog_timer_destroy(watchdog_timer *wd_timer) void
watchdog_timer_destroy(watchdog_timer *wd_timer)
{ {
#ifdef WATCHDOG_ENABLED /* TODO */ #ifdef WATCHDOG_ENABLED /* TODO */
app_manager_timer_destroy(wd_timer->timer_handle); app_manager_timer_destroy(wd_timer->timer_handle);
@ -62,32 +65,35 @@ void watchdog_timer_destroy(watchdog_timer *wd_timer)
#endif #endif
} }
void watchdog_timer_start(watchdog_timer *wd_timer) void
watchdog_timer_start(watchdog_timer *wd_timer)
{ {
os_mutex_lock(&wd_timer->lock); os_mutex_lock(&wd_timer->lock);
wd_timer->is_interrupting = false; wd_timer->is_interrupting = false;
wd_timer->is_stopped = false; wd_timer->is_stopped = false;
app_manager_timer_start(wd_timer->timer_handle, app_manager_timer_start(wd_timer->timer_handle,
wd_timer->module_data->timeout); wd_timer->module_data->timeout);
os_mutex_unlock(&wd_timer->lock); os_mutex_unlock(&wd_timer->lock);
} }
void watchdog_timer_stop(watchdog_timer *wd_timer) void
watchdog_timer_stop(watchdog_timer *wd_timer)
{ {
app_manager_timer_stop(wd_timer->timer_handle); app_manager_timer_stop(wd_timer->timer_handle);
} }
#ifdef WATCHDOG_ENABLED /* TODO */ #ifdef WATCHDOG_ENABLED /* TODO */
static void watchdog_queue_callback(void *queue_msg) static void
watchdog_queue_callback(void *queue_msg)
{ {
if (bh_message_type(queue_msg) == WD_TIMEOUT) { if (bh_message_type(queue_msg) == WD_TIMEOUT) {
module_data *m_data = (module_data *) bh_message_payload(queue_msg); module_data *m_data = (module_data *)bh_message_payload(queue_msg);
if (g_module_interfaces[m_data->module_type] if (g_module_interfaces[m_data->module_type]
&& g_module_interfaces[m_data->module_type]->module_watchdog_kill) { && g_module_interfaces[m_data->module_type]->module_watchdog_kill) {
g_module_interfaces[m_data->module_type]->module_watchdog_kill( g_module_interfaces[m_data->module_type]->module_watchdog_kill(
m_data); m_data);
app_manager_post_applets_update_event(); app_manager_post_applets_update_event();
} }
} }
@ -95,22 +101,23 @@ static void watchdog_queue_callback(void *queue_msg)
#endif #endif
#ifdef WATCHDOG_ENABLED /* TODO */ #ifdef WATCHDOG_ENABLED /* TODO */
static void* static void *
watchdog_thread_routine(void *arg) watchdog_thread_routine(void *arg)
{ {
/* Enter loop run */ /* Enter loop run */
bh_queue_enter_loop_run(watchdog_queue, watchdog_queue_callback); bh_queue_enter_loop_run(watchdog_queue, watchdog_queue_callback);
(void) arg; (void)arg;
return NULL; return NULL;
} }
#endif #endif
bool watchdog_startup() bool
watchdog_startup()
{ {
if (!(watchdog_queue = bh_queue_create())) { if (!(watchdog_queue = bh_queue_create())) {
app_manager_printf( app_manager_printf(
"App Manager start failed: create watchdog queue failed.\n"); "App Manager start failed: create watchdog queue failed.\n");
return false; return false;
} }
#if 0 #if 0
@ -125,7 +132,8 @@ bool watchdog_startup()
return true; return true;
} }
void watchdog_destroy() void
watchdog_destroy()
{ {
bh_queue_exit_loop_run(watchdog_queue); bh_queue_exit_loop_run(watchdog_queue);
bh_queue_destroy(watchdog_queue); bh_queue_destroy(watchdog_queue);

View File

@ -3,7 +3,6 @@
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/ */
#ifndef _WATCHDOG_H_ #ifndef _WATCHDOG_H_
#define _WATCHDOG_H_ #define _WATCHDOG_H_
@ -25,7 +24,7 @@ watchdog_timer_start(watchdog_timer *wd_timer);
void void
watchdog_timer_stop(watchdog_timer *wd_timer); watchdog_timer_stop(watchdog_timer *wd_timer);
watchdog_timer* watchdog_timer *
app_manager_get_watchdog_timer(void *timer); app_manager_get_watchdog_timer(void *timer);
bool bool

View File

@ -20,7 +20,7 @@ extern "C" {
#define ID_HOST -3 #define ID_HOST -3
#define ID_APP_MGR -2 #define ID_APP_MGR -2
/* Invalid module ID */ /* Invalid module ID */
#define ID_NONE (uint32)-1 #define ID_NONE ((uint32)-1)
struct attr_container_t; struct attr_container_t;
@ -40,7 +40,10 @@ typedef enum QUEUE_MSG_TYPE {
} QUEUE_MSG_TYPE; } QUEUE_MSG_TYPE;
typedef enum { typedef enum {
Module_Jeff, Module_WASM_App, Module_WASM_Lib, Module_Max Module_Jeff,
Module_WASM_App,
Module_WASM_Lib,
Module_Max
} Module_Type; } Module_Type;
struct module_data; struct module_data;
@ -111,12 +114,13 @@ typedef module_data *(*module_get_module_data_func)(void *inst);
* *
* @param ch the byte to be received and handled * @param ch the byte to be received and handled
* @param total_size total size of the request * @param total_size total size of the request
* @param received_total_size currently received total size when the function return * @param received_total_size currently received total size when
* the function return
* *
* @return true if success, false otherwise * @return true if success, false otherwise
*/ */
typedef bool (*module_on_install_request_byte_arrive_func) ( typedef bool (*module_on_install_request_byte_arrive_func)(
uint8 ch, int total_size, int *received_total_size); uint8 ch, int total_size, int *received_total_size);
/* Interfaces of each module */ /* Interfaces of each module */
typedef struct module_interface { typedef struct module_interface {
@ -148,7 +152,7 @@ typedef bool (*host_init_func)(void);
* *
* @return size of the data sent in bytes * @return size of the data sent in bytes
*/ */
typedef int (*host_send_fun)(void * ctx, const char *buf, int size); typedef int (*host_send_fun)(void *ctx, const char *buf, int size);
/** /**
* @typedef host_destroy_fun * @typedef host_destroy_fun
@ -191,22 +195,22 @@ app_manager_get_module_name(uint32 module_type, void *module_inst);
void * void *
app_manager_get_module_heap(uint32 module_type, void *module_inst); app_manager_get_module_heap(uint32 module_type, void *module_inst);
void* void *
get_app_manager_queue(); get_app_manager_queue();
module_data* module_data *
app_manager_get_module_data(uint32 module_type, void *module_inst); app_manager_get_module_data(uint32 module_type, void *module_inst);
unsigned int unsigned int
app_manager_get_module_id(uint32 module_type, void *module_inst); app_manager_get_module_id(uint32 module_type, void *module_inst);
module_data* module_data *
app_manager_lookup_module_data(const char *name); app_manager_lookup_module_data(const char *name);
module_data* module_data *
module_data_list_lookup(const char *module_name); module_data_list_lookup(const char *module_name);
module_data* module_data *
module_data_list_lookup_id(unsigned int module_id); module_data_list_lookup_id(unsigned int module_id);
void void
@ -214,9 +218,11 @@ app_manager_post_applets_update_event();
bool bool
am_register_resource(const char *url, am_register_resource(const char *url,
void (*request_handler)(request_t *, void *), uint32 register_id); void (*request_handler)(request_t *, void *),
uint32 register_id);
void am_cleanup_registeration(uint32 register_id); void
am_cleanup_registeration(uint32 register_id);
bool bool
am_register_event(const char *url, uint32_t reg_client); am_register_event(const char *url, uint32_t reg_client);
@ -224,13 +230,17 @@ am_register_event(const char *url, uint32_t reg_client);
bool bool
am_unregister_event(const char *url, uint32_t reg_client); am_unregister_event(const char *url, uint32_t reg_client);
void am_publish_event(request_t * event); void
am_publish_event(request_t *event);
void * am_dispatch_request(request_t *request); void *
am_dispatch_request(request_t *request);
void am_send_response(response_t *response); void
am_send_response(response_t *response);
void module_request_handler(request_t *request, void *user_data); void
module_request_handler(request_t *request, void *user_data);
/** /**
* Send request message to host * Send request message to host
@ -291,4 +301,3 @@ app_manager_host_send_msg(int msg_type, const char *buf, int size);
#endif #endif
#endif #endif

View File

@ -5,9 +5,9 @@
#include "aot_reloc.h" #include "aot_reloc.h"
#define R_386_32 1 /* Direct 32 bit */ #define R_386_32 1 /* Direct 32 bit */
#define R_386_PC32 2 /* PC relative 32 bit */ #define R_386_PC32 2 /* PC relative 32 bit */
#define R_386_PLT32 4 /* 32bit address ProcedureLinkageTable */ #define R_386_PLT32 4 /* 32-bit address ProcedureLinkageTable */
#if !defined(_WIN32) && !defined(_WIN32_) #if !defined(_WIN32) && !defined(_WIN32_)
/* clang-format off */ /* clang-format off */

View File

@ -302,7 +302,7 @@ Zephyr
You need to download the Zephyr source code first and embed WAMR into it. You need to download the Zephyr source code first and embed WAMR into it.
``` Bash ``` Bash
git clone https://github.com/zephyrproject-rtos/zephyr.git git clone https://github.com/zephyrproject-rtos/zephyr.git
source ../zephyr-env.sh source zephyr/zephyr-env.sh
cd <wamr_root_dir>/product-mini/platforms/zephyr/simple cd <wamr_root_dir>/product-mini/platforms/zephyr/simple
# Execute the ./build_and_run.sh script with board name as parameter. Here take x86 as example: # Execute the ./build_and_run.sh script with board name as parameter. Here take x86 as example:
./build_and_run.sh x86 ./build_and_run.sh x86

View File

@ -8,30 +8,30 @@
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include <string.h>
static unsigned char* static unsigned char *
read_file_to_buffer (const char *filename, int *ret_size) read_file_to_buffer(const char *filename, int *ret_size)
{ {
unsigned char *buffer; unsigned char *buffer;
FILE *file; FILE *file;
int file_size, read_size; int file_size, read_size;
if (!(file = fopen (filename, "r"))) if (!(file = fopen(filename, "r")))
return NULL; return NULL;
fseek (file, 0, SEEK_END); fseek(file, 0, SEEK_END);
file_size = ftell (file); file_size = ftell(file);
fseek (file, 0, SEEK_SET); fseek(file, 0, SEEK_SET);
if (!(buffer = malloc (file_size))) { if (!(buffer = malloc(file_size))) {
fclose (file); fclose(file);
return NULL; return NULL;
} }
read_size = fread (buffer, 1, file_size, file); read_size = fread(buffer, 1, file_size, file);
fclose (file); fclose(file);
if (read_size < file_size) { if (read_size < file_size) {
free (buffer); free(buffer);
return NULL; return NULL;
} }
@ -41,20 +41,19 @@ read_file_to_buffer (const char *filename, int *ret_size)
} }
static int static int
print_help () print_help()
{ {
printf ("Usage: binarydump -o <file> -n <name> input_file\n"); printf("Usage: binarydump -o <file> -n <name> input_file\n");
printf ("Options:\n"); printf("Options:\n");
printf (" -o <file> Place the output into <file>\n"); printf(" -o <file> Place the output into <file>\n");
printf (" -n <name> The name of array <file>\n"); printf(" -n <name> The name of array <file>\n");
return -1; return -1;
} }
static bool static bool
bin_file_dump (const unsigned char *file, int size, bin_file_dump(const unsigned char *file, int size, const char *bin_file_output,
const char *bin_file_output, const char *array_name)
const char *array_name)
{ {
unsigned i = 0; unsigned i = 0;
const unsigned char *p = file, *p_end = file + size; const unsigned char *p = file, *p_end = file + size;
@ -63,7 +62,8 @@ bin_file_dump (const unsigned char *file, int size,
if (!file_output) if (!file_output)
return false; return false;
fprintf(file_output, "\nunsigned char __aligned(4) %s[] = {\n ", array_name); fprintf(file_output, "\nunsigned char __aligned(4) %s[] = {\n ",
array_name);
while (p < p_end) { while (p < p_end) {
fprintf(file_output, "0x%02X", *p++); fprintf(file_output, "0x%02X", *p++);
@ -86,7 +86,7 @@ bin_file_dump (const unsigned char *file, int size,
} }
int int
main (int argc, char *argv[]) main(int argc, char *argv[])
{ {
unsigned char *file; unsigned char *file;
int size; int size;
@ -94,33 +94,33 @@ main (int argc, char *argv[])
const char *bin_file_input, *array_file_output = NULL, *array_name = NULL; const char *bin_file_input, *array_file_output = NULL, *array_name = NULL;
for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) { for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
if (!strcmp (argv[0], "-o")) { if (!strcmp(argv[0], "-o")) {
++argv; ++argv;
if (--argc == 0) if (--argc == 0)
return print_help (); return print_help();
array_file_output = *argv; array_file_output = *argv;
} }
else if (!strcmp (argv[0], "-n")) { else if (!strcmp(argv[0], "-n")) {
++argv; ++argv;
if (--argc == 0) if (--argc == 0)
return print_help (); return print_help();
array_name = *argv; array_name = *argv;
} }
else else
return print_help (); return print_help();
} }
if (!array_file_output || !array_name) if (!array_file_output || !array_name)
return print_help (); return print_help();
bin_file_input = *argv; bin_file_input = *argv;
if (!(file = read_file_to_buffer (bin_file_input, &size))) if (!(file = read_file_to_buffer(bin_file_input, &size)))
return -1; return -1;
ret = bin_file_dump (file, size, array_file_output, array_name); ret = bin_file_dump(file, size, array_file_output, array_name);
free (file); free(file);
return ret ? 0 : -1; return ret ? 0 : -1;
} }

View File

@ -11,32 +11,36 @@
#include "bi-inc/attr_container.h" #include "bi-inc/attr_container.h"
#include "er-coap-constants.h" #include "er-coap-constants.h"
static char *read_file_to_buffer(const char *filename, int *ret_size); static char *
read_file_to_buffer(const char *filename, int *ret_size);
int send_request_to_applet_success = 0; int send_request_to_applet_success = 0;
const char *label_for_request = "request1"; const char *label_for_request = "request1";
int event_listener_counter = 0; int event_listener_counter = 0;
char *applet_buf[1024 * 1024]; char *applet_buf[1024 * 1024];
const char *host_agent_ip = "127.0.0.1"; const char *host_agent_ip = "127.0.0.1";
void f_aee_response_handler(void *usr_ctx, aee_response_t *response) void
f_aee_response_handler(void *usr_ctx, aee_response_t *response)
{ {
if (response == NULL) { if (response == NULL) {
printf("########## request timeout!!! \n"); printf("########## request timeout!!! \n");
} else { }
char *str = (char *) usr_ctx; else {
char *str = (char *)usr_ctx;
printf("#### dump response ####\n"); printf("#### dump response ####\n");
printf("#### user data: %s \n", str); printf("#### user data: %s \n", str);
printf("#### status: %d \n", response->status); printf("#### status: %d \n", response->status);
if (response->payload != NULL) if (response->payload != NULL)
attr_container_dump((attr_container_t *) response->payload); attr_container_dump((attr_container_t *)response->payload);
} }
} }
void f_aee_event_listener(const char *url, void *event, int fmt) void
f_aee_event_listener(const char *url, void *event, int fmt)
{ {
printf("######## event is received. url: %s, fmt:%d ############\n", url, printf("######## event is received. url: %s, fmt:%d ############\n", url,
fmt); fmt);
attr_container_t *attr_obj = (attr_container_t *) event; attr_container_t *attr_obj = (attr_container_t *)event;
attr_container_dump(attr_obj); attr_container_dump(attr_obj);
/* /*
@ -48,7 +52,8 @@ void f_aee_event_listener(const char *url, void *event, int fmt)
*/ */
} }
static int print_menu_and_select(void) static int
print_menu_and_select(void)
{ {
char s[256]; char s[256];
int choice; int choice;
@ -83,7 +88,8 @@ static int print_menu_and_select(void)
return 0; return 0;
} }
static void install_applet(int index) static void
install_applet(int index)
{ {
char applet_name[64]; char applet_name[64];
char applet_file_name[64]; char applet_file_name[64];
@ -94,15 +100,15 @@ static void install_applet(int index)
printf("Installing TestApplet%d...\n", index); printf("Installing TestApplet%d...\n", index);
snprintf(applet_name, sizeof(applet_name), "TestApplet%d", index); snprintf(applet_name, sizeof(applet_name), "TestApplet%d", index);
snprintf(applet_file_name, sizeof(applet_file_name), "./TestApplet%d.wasm", snprintf(applet_file_name, sizeof(applet_file_name), "./TestApplet%d.wasm",
index); index);
buf = read_file_to_buffer(applet_file_name, &size); buf = read_file_to_buffer(applet_file_name, &size);
if (!buf) { if (!buf) {
printf("Install Applet failed: read file %s error.\n", printf("Install Applet failed: read file %s error.\n",
applet_file_name); applet_file_name);
return; return;
} }
//step2. install applet // step2. install applet
ret = aee_applet_install(buf, "wasm", size, applet_name, 5000); ret = aee_applet_install(buf, "wasm", size, applet_name, 5000);
if (ret) { if (ret) {
printf("%s install success\n", applet_name); printf("%s install success\n", applet_name);
@ -110,7 +116,8 @@ static void install_applet(int index)
free(buf); free(buf);
} }
static void uninstall_applet(int index) static void
uninstall_applet(int index)
{ {
int ret; int ret;
char applet_name[64]; char applet_name[64];
@ -118,51 +125,56 @@ static void uninstall_applet(int index)
ret = aee_applet_uninstall(applet_name, "wasm", 5000); ret = aee_applet_uninstall(applet_name, "wasm", 5000);
if (ret) { if (ret) {
printf("uninstall %s success\n", applet_name); printf("uninstall %s success\n", applet_name);
} else { }
else {
printf("uninstall %s failed\n", applet_name); printf("uninstall %s failed\n", applet_name);
} }
} }
static void send_request(int index) static void
send_request(int index)
{ {
char url[64]; char url[64];
int ret; int ret;
aee_request_t req; aee_request_t req;
const char *user_context = "label for request"; const char *user_context = "label for request";
attr_container_t *attr_obj = attr_container_create( attr_container_t *attr_obj =
"Send Request to Applet"); attr_container_create("Send Request to Applet");
attr_container_set_string(&attr_obj, "String key", "Hello"); attr_container_set_string(&attr_obj, "String key", "Hello");
attr_container_set_int(&attr_obj, "Int key", 1000); attr_container_set_int(&attr_obj, "Int key", 1000);
attr_container_set_int64(&attr_obj, "Int64 key", 0x77BBCCDD11223344LL); attr_container_set_int64(&attr_obj, "Int64 key", 0x77BBCCDD11223344LL);
//specify the target wasm app // specify the target wasm app
snprintf(url, sizeof(url), "/app/TestApplet%d/url1", index); snprintf(url, sizeof(url), "/app/TestApplet%d/url1", index);
//not specify the target wasm app // not specify the target wasm app
//snprintf(url, sizeof(url), "url1"); // snprintf(url, sizeof(url), "url1");
aee_request_init(&req, url, COAP_PUT); aee_request_init(&req, url, COAP_PUT);
aee_request_set_payload(&req, attr_obj, aee_request_set_payload(&req, attr_obj,
attr_container_get_serialize_length(attr_obj), attr_container_get_serialize_length(attr_obj),
PAYLOAD_FORMAT_ATTRIBUTE_OBJECT); PAYLOAD_FORMAT_ATTRIBUTE_OBJECT);
ret = aee_request_send(&req, f_aee_response_handler, (void *) user_context, ret = aee_request_send(&req, f_aee_response_handler, (void *)user_context,
10000); 10000);
if (ret) { if (ret) {
printf("send request to TestApplet1 success\n"); printf("send request to TestApplet1 success\n");
} }
} }
static void register_event(const char *event_path) static void
register_event(const char *event_path)
{ {
hostclient_register_event(event_path, f_aee_event_listener); hostclient_register_event(event_path, f_aee_event_listener);
} }
static void unregister_event(const char *event_path) static void
unregister_event(const char *event_path)
{ {
hostclient_unregister_event(event_path); hostclient_unregister_event(event_path);
} }
static void query_applets() static void
query_applets()
{ {
aee_applet_list_t applet_lst; aee_applet_list_t applet_lst;
aee_applet_list_init(&applet_lst); aee_applet_list_init(&applet_lst);
@ -189,7 +201,7 @@ read_file_to_buffer(const char *filename, int *ret_size)
return NULL; return NULL;
} }
if (!(buffer = (char *) malloc(file_size))) { if (!(buffer = (char *)malloc(file_size))) {
fclose(fl); fclose(fl);
return NULL; return NULL;
} }
@ -206,7 +218,8 @@ read_file_to_buffer(const char *filename, int *ret_size)
return buffer; return buffer;
} }
static void auto_test() static void
auto_test()
{ {
int i; int i;
int interval = 1000; /* ms */ int interval = 1000; /* ms */
@ -228,7 +241,8 @@ static void auto_test()
} }
} }
void exit_program() void
exit_program()
{ {
hostclient_shutdown(); hostclient_shutdown();
exit(0); exit(0);
@ -240,7 +254,7 @@ main()
{ {
bool ret; bool ret;
//step1. host client init // step1. host client init
ret = hostclient_initialize(host_agent_ip, 3456); ret = hostclient_initialize(host_agent_ip, 3456);
if (!ret) { if (!ret) {
@ -281,5 +295,7 @@ main()
// 2. Use the Team Explorer window to connect to source control // 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages // 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors // 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project // 5. Go to Project > Add New Item to create new code files, or
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file // Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project
// and select the .sln file

Some files were not shown because too many files have changed in this diff Show More