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
143 changed files with 5377 additions and 4627 deletions

View File

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

View File

@ -34,7 +34,7 @@ enum {
ATTR_TYPE_END = ATTR_TYPE_BYTEARRAY
};
#define ATTR_CONT_READONLY_SHIFT 2
#define ATTR_CONT_READONLY_SHIFT 2
typedef struct attr_container {
/* container flag:
@ -87,7 +87,7 @@ attr_container_destroy(const attr_container_t *attr_cont);
*/
bool
attr_container_set_short(attr_container_t **p_attr_cont, const char *key,
short value);
short value);
/**
* Set int attribute in attribute container
@ -101,7 +101,7 @@ attr_container_set_short(attr_container_t **p_attr_cont, const char *key,
*/
bool
attr_container_set_int(attr_container_t **p_attr_cont, const char *key,
int value);
int value);
/**
* Set int64 attribute in attribute container
@ -115,7 +115,7 @@ attr_container_set_int(attr_container_t **p_attr_cont, const char *key,
*/
bool
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
@ -129,7 +129,7 @@ attr_container_set_int64(attr_container_t **p_attr_cont, const char *key,
*/
bool
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
@ -143,7 +143,7 @@ attr_container_set_byte(attr_container_t **p_attr_cont, const char *key,
*/
bool
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
@ -157,7 +157,7 @@ attr_container_set_uint16(attr_container_t **p_attr_cont, const char *key,
*/
bool
attr_container_set_float(attr_container_t **p_attr_cont, const char *key,
float value);
float value);
/**
* Set double attribute in attribute container
@ -171,7 +171,7 @@ attr_container_set_float(attr_container_t **p_attr_cont, const char *key,
*/
bool
attr_container_set_double(attr_container_t **p_attr_cont, const char *key,
double value);
double value);
/**
* Set bool attribute in attribute container
@ -185,7 +185,7 @@ attr_container_set_double(attr_container_t **p_attr_cont, const char *key,
*/
bool
attr_container_set_bool(attr_container_t **p_attr_cont, const char *key,
bool value);
bool value);
/**
* Set string attribute in attribute container
@ -199,7 +199,7 @@ attr_container_set_bool(attr_container_t **p_attr_cont, const char *key,
*/
bool
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
@ -214,7 +214,7 @@ attr_container_set_string(attr_container_t **p_attr_cont, const char *key,
*/
bool
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
@ -223,7 +223,7 @@ attr_container_set_bytearray(attr_container_t **p_attr_cont, const char *key,
*
* @return tag of current attribute container
*/
const char*
const char *
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
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,
@ -331,7 +331,7 @@ attr_container_get_as_float(const attr_container_t *attr_cont, const char *key);
*/
double
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,
@ -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
*/
char*
char *
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,
@ -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
*/
const int8_t*
const int8_t *
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
@ -400,7 +400,7 @@ attr_container_serialize(char *buf, const attr_container_t *attr_cont);
* @return true if const, false otherwise
*/
bool
attr_container_is_constant(const attr_container_t* attr_cont);
attr_container_is_constant(const attr_container_t *attr_cont);
void
attr_container_dump(const attr_container_t *attr_cont);
@ -422,4 +422,3 @@ attr_container_dump(const attr_container_t *attr_cont);
#endif
#endif /* end of _ATTR_CONTAINER_H_ */

View File

@ -12,8 +12,8 @@
extern "C" {
#endif
#define FMT_ATTR_CONTAINER 99
#define FMT_APP_RAW_BINARY 98
#define FMT_ATTR_CONTAINER 99
#define FMT_APP_RAW_BINARY 98
/* the request structure */
typedef struct request {
@ -32,10 +32,10 @@ typedef struct request {
// payload of the request, currently only support attr_container_t type
void *payload;
//length in bytes of the payload
// length in bytes of the payload
int payload_len;
//sender of the request
// sender of the request
unsigned long sender;
} request_t;
@ -53,21 +53,21 @@ typedef struct response {
// payload of the response,
void *payload;
//length in bytes of the payload
// length in bytes of the payload
int payload_len;
//receiver of the response
// receiver of the response
unsigned long reciever;
} response_t;
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
match_url(char * pattern, char * matched);
match_url(char *pattern, char *matched);
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);
request_t *
@ -77,10 +77,10 @@ void
request_cleaner(request_t *request);
response_t *
clone_response(response_t * response);
clone_response(response_t *response);
void
response_cleaner(response_t * response);
response_cleaner(response_t *response);
/**
* @brief Set fields of response.
@ -96,8 +96,8 @@ response_cleaner(response_t * response);
* @warning the response pointer MUST NOT be NULL
*/
response_t *
set_response(response_t * response, int status, int fmt,
const char *payload, int payload_len);
set_response(response_t *response, int status, int fmt, const char *payload,
int payload_len);
/**
* @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
*/
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.
@ -127,28 +127,27 @@ make_response_for_request(request_t * request, response_t * response);
* @warning the request pointer MUST NOT be NULL
*/
request_t *
init_request(request_t * request, char *url, int action, int fmt,
void *payload, int payload_len);
init_request(request_t *request, char *url, int action, int fmt, void *payload,
int payload_len);
char *
pack_request(request_t *request, int * size);
pack_request(request_t *request, int *size);
request_t *
unpack_request(char * packet, int size, request_t * request);
unpack_request(char *packet, int size, request_t *request);
char *
pack_response(response_t *response, int * size);
pack_response(response_t *response, int *size);
response_t *
unpack_response(char * packet, int size, response_t * response);
unpack_response(char *packet, int size, response_t *response);
void
free_req_resp_packet(char * packet);
free_req_resp_packet(char *packet);
char *
wa_strdup(const char *str);
#ifdef __cplusplus
}
#endif

View File

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

View File

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

View File

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

View File

@ -1,33 +1,33 @@
#include "lib_export.h"
#ifdef APP_FRAMEWORK_SENSOR
#include "sensor_native_api.h"
#include "sensor_native_api.h"
#endif
#ifdef APP_FRAMEWORK_CONNECTION
#include "connection_native_api.h"
#include "connection_native_api.h"
#endif
#ifdef APP_FRAMEWORK_WGL
#include "gui_native_api.h"
#include "gui_native_api.h"
#endif
/* More header file here */
static NativeSymbol extended_native_symbol_defs[] = {
#ifdef APP_FRAMEWORK_SENSOR
#include "runtime_sensor.inl"
#include "runtime_sensor.inl"
#endif
#ifdef APP_FRAMEWORK_CONNECTION
#include "connection.inl"
#include "connection.inl"
#endif
#ifdef APP_FRAMEWORK_WGL
#include "wamr_gui.inl"
#include "wamr_gui.inl"
#endif
/* More inl file here */
/* More inl file here */
};
int
@ -36,4 +36,3 @@ get_ext_lib_export_apis(NativeSymbol **p_ext_lib_apis)
*p_ext_lib_apis = extended_native_symbol_defs;
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;
unsigned char* c = (unsigned char*) &i;
unsigned char *c = (unsigned char *)&i;
return (*c == 0x04) ? true : false;
}
static void swap32(uint8* pData)
static void
swap32(uint8 *pData)
{
uint8 value = *pData;
*pData = *(pData + 3);
@ -31,31 +33,35 @@ static void swap32(uint8* pData)
*(pData + 2) = value;
}
static void swap16(uint8* pData)
static void
swap16(uint8 *pData)
{
uint8 value = *pData;
*(pData) = *(pData + 1);
*(pData + 1) = value;
}
uint32 htonl(uint32 value)
uint32
htonl(uint32 value)
{
uint32 ret;
if (is_little_endian()) {
ret = value;
swap32((uint8*) &ret);
swap32((uint8 *)&ret);
return ret;
}
return value;
}
uint32 ntohl(uint32 value)
uint32
ntohl(uint32 value)
{
return htonl(value);
}
uint16 htons(uint16 value)
uint16
htons(uint16 value)
{
uint16 ret;
if (is_little_endian()) {
@ -67,12 +73,14 @@ uint16 htons(uint16 value)
return value;
}
uint16 ntohs(uint16 value)
uint16
ntohs(uint16 value)
{
return htons(value);
}
char *wa_strdup(const char *s)
char *
wa_strdup(const char *s)
{
char *s1 = NULL;
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;
#ifndef NULL
# define NULL ((void*) 0)
#define NULL ((void *)0)
#endif
#ifndef __cplusplus
@ -35,28 +35,31 @@ typedef int int32;
#define WA_FREE free
#endif
uint32 htonl(uint32 value);
uint32 ntohl(uint32 value);
uint16 htons(uint16 value);
uint16 ntohs(uint16 value);
uint32
htonl(uint32 value);
uint32
ntohl(uint32 value);
uint16
htons(uint16 value);
uint16
ntohs(uint16 value);
// 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
#define bh_assert(v) (void)0
#else
#define bh_assert(v) do { \
if (!(v)) { \
int _count; \
printf("ASSERTION FAILED: %s, at %s, line %d",\
#v, __FILE__, __LINE__); \
_count = printf("\n"); \
printf("%d\n", _count / (_count - 1)); \
} \
} while (0)
#define bh_assert(v) \
do { \
if (!(v)) { \
int _count; \
printf("ASSERTION FAILED: %s, at %s, line %d", #v, __FILE__, \
__LINE__); \
_count = printf("\n"); \
printf("%d\n", _count / (_count - 1)); \
} \
} while (0)
#endif
#endif /* DEPS_IWASM_APP_LIBS_BASE_BH_PLATFORM_H_ */

View File

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

View File

@ -13,13 +13,11 @@
#define TRANSACTION_TIMEOUT_MS 5000
typedef enum {
Reg_Event, Reg_Request
} reg_type_t;
typedef enum { Reg_Event, Reg_Request } reg_type_t;
typedef struct _res_register {
struct _res_register *next;
const char * url;
const char *url;
reg_type_t reg_type;
void (*request_handler)(request_t *);
} res_register_t;
@ -32,13 +30,14 @@ typedef struct transaction {
void *user_data;
} transaction_t;
static res_register_t * g_resources = NULL;
static res_register_t *g_resources = NULL;
static transaction_t *g_transactions = NULL;
static user_timer_t g_trans_timer = NULL;
static transaction_t *transaction_find(int mid)
static transaction_t *
transaction_find(int mid)
{
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
* is sorted by expiry time naturally.
*/
static void transaction_add(transaction_t *trans)
static void
transaction_add(transaction_t *trans)
{
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;
@ -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;
}
static bool register_url_handler(const char *url,
request_handler_f request_handler, reg_type_t reg_type)
static bool
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) {
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 = (res_register_t *) malloc(sizeof(res_register_t));
r = (res_register_t *)malloc(sizeof(res_register_t));
if (r == NULL)
return false;
@ -137,13 +140,15 @@ static bool register_url_handler(const char *url,
return true;
}
bool api_register_resource_handler(const char *url,
request_handler_f request_handler)
bool
api_register_resource_handler(const char *url,
request_handler_f request_handler)
{
return register_url_handler(url, request_handler, Reg_Request);
}
static void transaction_timeout_handler(user_timer_t timer)
static void
transaction_timeout_handler(user_timer_t timer)
{
transaction_t *cur, *expired = NULL;
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;
expired = cur;
cur = g_transactions;
} else {
}
else {
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();
if (now < g_transactions->time) {
elpased_ms = now + (0xFFFFFFFF - g_transactions->time) + 1;
} else {
}
else {
elpased_ms = now - g_transactions->time;
}
ms_to_expiry = TRANSACTION_TIMEOUT_MS - elpased_ms;
api_timer_restart(g_trans_timer, ms_to_expiry);
} else {
}
else {
api_timer_cancel(g_trans_timer);
g_trans_timer = NULL;
}
}
void api_send_request(request_t * request, response_handler_f response_handler,
void * user_data)
void
api_send_request(request_t *request, response_handler_f response_handler,
void *user_data)
{
int size;
char *buffer;
transaction_t *trans;
if ((trans = (transaction_t *) malloc(sizeof(transaction_t))) == NULL) {
if ((trans = (transaction_t *)malloc(sizeof(transaction_t))) == NULL) {
printf(
"send request: allocate memory for request transaction failed!\n");
"send request: allocate memory for request transaction failed!\n");
return;
}
@ -228,9 +237,8 @@ void api_send_request(request_t * request, response_handler_f response_handler,
if (trans == g_transactions) {
/* assert(g_trans_timer == NULL); */
if (g_trans_timer == NULL) {
g_trans_timer = api_timer_create(TRANSACTION_TIMEOUT_MS,
false,
true, transaction_timeout_handler);
g_trans_timer = api_timer_create(TRANSACTION_TIMEOUT_MS, false,
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];
transaction_t *trans;
@ -262,7 +272,8 @@ void on_response(char * buffer, int size)
/*
* 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;
*/
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();
if (now < trans->next->time) {
elpased_ms = now + (0xFFFFFFFF - trans->next->time) + 1;
} else {
}
else {
elpased_ms = now - trans->next->time;
}
ms_to_expiry = TRANSACTION_TIMEOUT_MS - elpased_ms;
api_timer_restart(g_trans_timer, ms_to_expiry);
} else {
}
else {
api_timer_cancel(g_trans_timer);
g_trans_timer = NULL;
}
@ -285,7 +298,8 @@ void on_response(char * buffer, int size)
transaction_remove(trans);
}
void on_request(char *buffer, int size)
void
on_request(char *buffer, int size)
{
request_t request[1];
bool is_event;
@ -300,9 +314,9 @@ void on_request(char *buffer, int size)
while (r) {
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)
> 0) {
> 0) {
r->request_handler(request);
return;
}
@ -314,10 +328,11 @@ void on_request(char *buffer, int size)
printf("on_request: exit. no service handler\n");
}
void api_response_send(response_t *response)
void
api_response_send(response_t *response)
{
int size;
char * buffer = pack_response(response, &size);
char *buffer = pack_response(response, &size);
if (buffer == NULL)
return;
@ -327,12 +342,13 @@ void api_response_send(response_t *response)
/// 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;
request_t request[1];
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)
return false;
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;
}
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);
}

View File

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

View File

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

View File

@ -12,7 +12,6 @@
extern "C" {
#endif
/* CoAP request method codes */
typedef enum {
COAP_GET = 1,
@ -26,39 +25,40 @@ typedef enum {
typedef enum {
NO_ERROR = 0,
CREATED_2_01 = 65, /* CREATED */
DELETED_2_02 = 66, /* DELETED */
VALID_2_03 = 67, /* NOT_MODIFIED */
CHANGED_2_04 = 68, /* CHANGED */
CONTENT_2_05 = 69, /* OK */
CREATED_2_01 = 65, /* CREATED */
DELETED_2_02 = 66, /* DELETED */
VALID_2_03 = 67, /* NOT_MODIFIED */
CHANGED_2_04 = 68, /* CHANGED */
CONTENT_2_05 = 69, /* OK */
CONTINUE_2_31 = 95, /* CONTINUE */
BAD_REQUEST_4_00 = 128, /* BAD_REQUEST */
UNAUTHORIZED_4_01 = 129, /* UNAUTHORIZED */
BAD_OPTION_4_02 = 130, /* BAD_OPTION */
FORBIDDEN_4_03 = 131, /* FORBIDDEN */
NOT_FOUND_4_04 = 132, /* NOT_FOUND */
METHOD_NOT_ALLOWED_4_05 = 133, /* METHOD_NOT_ALLOWED */
NOT_ACCEPTABLE_4_06 = 134, /* NOT_ACCEPTABLE */
PRECONDITION_FAILED_4_12 = 140, /* BAD_REQUEST */
BAD_REQUEST_4_00 = 128, /* BAD_REQUEST */
UNAUTHORIZED_4_01 = 129, /* UNAUTHORIZED */
BAD_OPTION_4_02 = 130, /* BAD_OPTION */
FORBIDDEN_4_03 = 131, /* FORBIDDEN */
NOT_FOUND_4_04 = 132, /* NOT_FOUND */
METHOD_NOT_ALLOWED_4_05 = 133, /* METHOD_NOT_ALLOWED */
NOT_ACCEPTABLE_4_06 = 134, /* NOT_ACCEPTABLE */
PRECONDITION_FAILED_4_12 = 140, /* BAD_REQUEST */
REQUEST_ENTITY_TOO_LARGE_4_13 = 141, /* REQUEST_ENTITY_TOO_LARGE */
UNSUPPORTED_MEDIA_TYPE_4_15 = 143, /* UNSUPPORTED_MEDIA_TYPE */
UNSUPPORTED_MEDIA_TYPE_4_15 = 143, /* UNSUPPORTED_MEDIA_TYPE */
INTERNAL_SERVER_ERROR_5_00 = 160, /* INTERNAL_SERVER_ERROR */
NOT_IMPLEMENTED_5_01 = 161, /* NOT_IMPLEMENTED */
BAD_GATEWAY_5_02 = 162, /* BAD_GATEWAY */
SERVICE_UNAVAILABLE_5_03 = 163, /* SERVICE_UNAVAILABLE */
GATEWAY_TIMEOUT_5_04 = 164, /* GATEWAY_TIMEOUT */
INTERNAL_SERVER_ERROR_5_00 = 160, /* INTERNAL_SERVER_ERROR */
NOT_IMPLEMENTED_5_01 = 161, /* NOT_IMPLEMENTED */
BAD_GATEWAY_5_02 = 162, /* BAD_GATEWAY */
SERVICE_UNAVAILABLE_5_03 = 163, /* SERVICE_UNAVAILABLE */
GATEWAY_TIMEOUT_5_04 = 164, /* GATEWAY_TIMEOUT */
PROXYING_NOT_SUPPORTED_5_05 = 165, /* PROXYING_NOT_SUPPORTED */
/* Erbium errors */
MEMORY_ALLOCATION_ERROR = 192, PACKET_SERIALIZATION_ERROR,
MEMORY_ALLOCATION_ERROR = 192,
PACKET_SERIALIZATION_ERROR,
/* Erbium hooks */
MANUAL_RESPONSE, PING_RESPONSE
MANUAL_RESPONSE,
PING_RESPONSE
} coap_status_t;
/**
* @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);
/*
*****************
* Request APIs
@ -102,7 +101,8 @@ typedef void (*response_handler_f)(response_t *response, void *user_data);
*
* @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.
@ -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 user_data user data
*/
void api_send_request(request_t * request, response_handler_f response_handler,
void * user_data);
void
api_send_request(request_t *request, response_handler_f response_handler,
void *user_data);
/**
* @brief Send response.
@ -130,8 +131,8 @@ void api_send_request(request_t * request, response_handler_f response_handler,
* }
* @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
*/
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);
/**
* @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
*/
bool api_subscribe_event(const char * url, request_handler_f handler);
bool
api_subscribe_event(const char *url, request_handler_f handler);
#ifdef __cplusplus
}

View File

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

View File

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

View File

@ -10,16 +10,15 @@
#include "req_resp_native_api.h"
#include "timer_native_api.h"
static NativeSymbol extended_native_symbol_defs[] = {
/* TODO: use macro EXPORT_WASM_API() or EXPORT_WASM_API2() to
add functions to register. */
#include "base_lib.inl"
/* TODO: use macro EXPORT_WASM_API() or EXPORT_WASM_API2() to
add functions to register. */
#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;
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 /* end of _REQ_RESP_API_H_ */

View File

@ -8,7 +8,8 @@
#include "wasm_export.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
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);
if (url != NULL) {
unsigned int mod_id = app_manager_get_module_id(Module_WASM_App,
module_inst);
unsigned int mod_id =
app_manager_get_module_id(Module_WASM_App, module_inst);
bh_assert(mod_id != ID_NONE);
am_register_resource(url, module_request_handler, mod_id);
}
@ -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
// set sender to help dispatch the response to the sender ap
unsigned int mod_id = app_manager_get_module_id(Module_WASM_App,
module_inst);
unsigned int mod_id =
app_manager_get_module_id(Module_WASM_App, module_inst);
bh_assert(mod_id != ID_NONE);
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);
if (url != NULL) {
unsigned int mod_id = app_manager_get_module_id(Module_WASM_App,
module_inst);
unsigned int mod_id =
app_manager_get_module_id(Module_WASM_App, module_inst);
bh_assert(mod_id != ID_NONE);
am_register_event(url, mod_id);
}
}

View File

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

View File

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

View File

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

View File

@ -24,10 +24,9 @@ typedef struct _connection {
/* Raw connections list */
static connection_t *g_conns = NULL;
connection_t *api_open_connection(const char *name,
attr_container_t *args,
on_connection_event_f on_event,
void *user_data)
connection_t *
api_open_connection(const char *name, attr_container_t *args,
on_connection_event_f on_event, void *user_data)
{
connection_t *conn;
char *args_buffer = (char *)args;
@ -51,14 +50,16 @@ connection_t *api_open_connection(const char *name,
if (g_conns != NULL) {
conn->next = g_conns;
g_conns = conn;
} else {
}
else {
g_conns = conn;
}
return conn;
}
void api_close_connection(connection_t *c)
void
api_close_connection(connection_t *c)
{
connection_t *conn = g_conns, *prev = NULL;
@ -71,19 +72,22 @@ void api_close_connection(connection_t *c)
g_conns = conn->next;
free(conn);
return;
} else {
}
else {
prev = conn;
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);
}
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;
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);
}
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;
while (conn != NULL) {
if (conn->handle == handle) {
if (len == 0) {
conn->on_event(conn,
CONN_EVENT_TYPE_DISCONNECT,
NULL,
0,
conn->on_event(conn, CONN_EVENT_TYPE_DISCONNECT, NULL, 0,
conn->user_data);
} else {
conn->on_event(conn,
CONN_EVENT_TYPE_DATA,
buffer,
len,
}
else {
conn->on_event(conn, CONN_EVENT_TYPE_DATA, buffer, len,
conn->user_data);
}
@ -116,4 +116,3 @@ void on_connection_data(uint32 handle, char *buffer, uint32 len)
conn = conn->next;
}
}

View File

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

View File

@ -33,10 +33,8 @@ typedef enum {
* @param user_data user data
*/
typedef void (*on_connection_event_f)(connection_t *conn,
conn_event_type_t type,
const char *data,
uint32 len,
void *user_data);
conn_event_type_t type, const char *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
*/
connection_t *api_open_connection(const char *name,
attr_container_t *args,
on_connection_event_f on_event,
void *user_data);
connection_t *
api_open_connection(const char *name, attr_container_t *args,
on_connection_event_f on_event, void *user_data);
/*
* @brief Close a connection.
*
* @param conn connection
*/
void api_close_connection(connection_t *conn);
void
api_close_connection(connection_t *conn);
/*
* 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)
*/
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.
@ -85,8 +84,8 @@ int api_send_on_connection(connection_t *conn, const char *data, uint32 len);
*
* @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
}

View File

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

View File

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

View File

@ -8,15 +8,15 @@
#include "native_interface.h"
#include "connection_native_api.h"
/* 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
wasm_open_connection(wasm_exec_env_t exec_env,
char *name, char *args_buf, uint32 len)
wasm_open_connection(wasm_exec_env_t exec_env, char *name, char *args_buf,
uint32 len)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
attr_container_t *args;
@ -37,8 +37,8 @@ wasm_close_connection(wasm_exec_env_t exec_env, uint32 handle)
}
int
wasm_send_on_connection(wasm_exec_env_t exec_env,
uint32 handle, char *data, uint32 len)
wasm_send_on_connection(wasm_exec_env_t exec_env, uint32 handle, char *data,
uint32 len)
{
if (connection_impl._send != NULL)
return connection_impl._send(handle, data, len);
@ -47,8 +47,8 @@ wasm_send_on_connection(wasm_exec_env_t exec_env,
}
bool
wasm_config_connection(wasm_exec_env_t exec_env,
uint32 handle, char *cfg_buf, uint32 len)
wasm_config_connection(wasm_exec_env_t exec_env, uint32 handle, char *cfg_buf,
uint32 len)
{
attr_container_t *cfg;

View File

@ -11,7 +11,8 @@
#include <fcntl.h>
#include <unistd.h>
int tcp_open(char *address, uint16 port)
int
tcp_open(char *address, uint16 port)
{
int sock, ret;
struct sockaddr_in servaddr;
@ -25,7 +26,7 @@ int tcp_open(char *address, uint16 port)
if (sock == -1)
return -1;
ret = connect(sock, (struct sockaddr*)&servaddr, sizeof(servaddr));
ret = connect(sock, (struct sockaddr *)&servaddr, sizeof(servaddr));
if (ret == -1) {
close(sock);
return -1;
@ -40,12 +41,14 @@ int tcp_open(char *address, uint16 port)
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);
}
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);
}

View File

@ -12,15 +12,17 @@
extern "C" {
#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
}
#endif
#endif

View File

@ -9,7 +9,8 @@
#include <termios.h>
#include <unistd.h>
static int parse_baudrate(int baud)
static int
parse_baudrate(int baud)
{
switch (baud) {
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;
struct termios uart_term;
@ -88,12 +90,14 @@ int uart_open(char* device, int baudrate)
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);
}
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);
}

View File

@ -12,15 +12,17 @@
extern "C" {
#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
}
#endif
#endif

View File

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

View File

@ -12,15 +12,17 @@
extern "C" {
#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
}
#endif
#endif

View File

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

View File

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

View File

@ -8,7 +8,7 @@
#include "sensor_api.h"
typedef struct _sensor {
struct _sensor * next;
struct _sensor *next;
char *name;
uint32 handle;
void (*sensor_callback)(sensor_t, attr_container_t *, void *);
@ -17,16 +17,16 @@ typedef struct _sensor {
static sensor_t g_sensors = NULL;
sensor_t sensor_open(const char* name, int index,
sensor_event_handler_f sensor_event_handler,
void *user_data)
sensor_t
sensor_open(const char *name, int index,
sensor_event_handler_f sensor_event_handler, void *user_data)
{
uint32 id = wasm_sensor_open(name, index);
if (id == -1)
return NULL;
//create local node for holding the user callback
sensor_t sensor = (sensor_t) malloc(sizeof(struct _sensor));
// create local node for holding the user callback
sensor_t sensor = (sensor_t)malloc(sizeof(struct _sensor));
if (sensor == NULL)
return NULL;
@ -43,7 +43,8 @@ sensor_t sensor_open(const char* name, int index,
if (g_sensors == NULL) {
g_sensors = sensor;
} else {
}
else {
sensor->next = g_sensors;
g_sensors = sensor;
}
@ -51,7 +52,8 @@ sensor_t sensor_open(const char* name, int index,
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;
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);
}
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);
return ret;
}
bool sensor_close(sensor_t sensor)
bool
sensor_close(sensor_t sensor)
{
wasm_sensor_close(sensor->handle);
@ -76,13 +80,15 @@ bool sensor_close(sensor_t sensor)
if (s == sensor) {
if (prev == NULL) {
g_sensors = s->next;
} else {
}
else {
prev->next = s->next;
}
free(s->name);
free(s);
return true;
} else {
}
else {
prev = s;
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
sensor_t s = g_sensors;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -14,16 +14,14 @@ extern "C" {
#endif
bool
wasm_sensor_config(wasm_exec_env_t exec_env,
uint32 sensor, int interval,
wasm_sensor_config(wasm_exec_env_t exec_env, uint32 sensor, int interval,
int bit_cfg, int delay);
uint32
wasm_sensor_open(wasm_exec_env_t exec_env,
char *name, int instance);
wasm_sensor_open(wasm_exec_env_t exec_env, char *name, int instance);
bool
wasm_sensor_config_with_attr_container(wasm_exec_env_t exec_env,
uint32 sensor, char *buffer, int len);
wasm_sensor_config_with_attr_container(wasm_exec_env_t exec_env, uint32 sensor,
char *buffer, int len);
bool
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 /* end of _SENSOR_NATIVE_API_H_ */

View File

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

View File

@ -7,12 +7,13 @@
#include "bh_platform.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)
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[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];
}
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[1] = tgl;
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[1] = 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;
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[1] = 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[1] = 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[1] = time;
CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_SET_INK_OUT_TIME);
}
//void wgl_btn_set_style(wgl_obj_t btn, wgl_btn_style_t type, const wgl_style_t * style)
// void wgl_btn_set_style(wgl_obj_t btn, wgl_btn_style_t type,
// const wgl_style_t *style)
//{
// //TODO: pack style
// //wasm_btn_set_style(btn, type, style);
//}
//
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;
CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_GET_STATE);
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;
CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_GET_TOGGLE);
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;
CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_GET_INK_IN_TIME);
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;
CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_GET_INK_WAIT_TIME);
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;
CALL_BTN_NATIVE_FUNC(BTN_FUNC_ID_GET_INK_OUT_TIME);
return (uint16_t)argv[0];
}
//
//const wgl_style_t * wgl_btn_get_style(const wgl_obj_t btn, wgl_btn_style_t type)
// const wgl_style_t * wgl_btn_get_style(const wgl_obj_t btn,
// wgl_btn_style_t type)
//{
// //TODO: pack style
// //wasm_btn_get_style(btn, type);

View File

@ -3,18 +3,18 @@
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "wa-inc/lvgl/lvgl.h"
#include "gui_api.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)
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[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];
}
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[1] = (uint32)txt;
argv[2] = strlen(txt) + 1;
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[1] = (uint32)txt;
argv[2] = strlen(txt) + 1;
CALL_CB_NATIVE_FUNC(CB_FUNC_ID_SET_STATIC_TEXT);
}
//void wgl_cb_set_style(wgl_obj_t cb, wgl_cb_style_t type, const wgl_style_t * style)
// void wgl_cb_set_style(wgl_obj_t cb, wgl_cb_style_t type,
// const wgl_style_t *style)
//{
// //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;
CALL_CB_NATIVE_FUNC(CB_FUNC_ID_GET_TEXT_LENGTH);
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[1] = (uint32)buffer;
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
const char * lv_cb_get_text(const lv_obj_t * cb)
const char *
lv_cb_get_text(const lv_obj_t *cb)
{
return NULL;
}
//const wgl_style_t * wgl_cb_get_style(const wgl_obj_t cb, wgl_cb_style_t type)
// const wgl_style_t * wgl_cb_get_style(const wgl_obj_t cb,
// wgl_cb_style_t type)
//{
// //TODO
// return NULL;
//}
//

View File

@ -3,19 +3,17 @@
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "wa-inc/lvgl/lvgl.h"
#include "gui_api.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)
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[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];
}
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[1] = (uint32)text;
argv[2] = strlen(text) + 1;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_TEXT);
}
void lv_label_set_array_text(lv_obj_t * label, const char * array, uint16_t size)
void
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[1] = (uint32)array;
argv[2] = size;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_ARRAY_TEXT);
}
void lv_label_set_static_text(lv_obj_t * label, const char * text)
void
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[1] = (uint32)text;
argv[2] = strlen(text) + 1;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_STATIC_TEXT);
}
void lv_label_set_long_mode(lv_obj_t * label, lv_label_long_mode_t long_mode)
void
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[1] = long_mode;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_LONG_MODE);
}
void lv_label_set_align(lv_obj_t * label, lv_label_align_t align)
void
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[1] = align;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_ALIGN);
}
void lv_label_set_recolor(lv_obj_t * label, bool en)
void
lv_label_set_recolor(lv_obj_t *label, bool en)
{
uint32 argv[2] = {0};
uint32 argv[2] = { 0 };
argv[0] = (uint32)label;
argv[1] = en;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_RECOLOR);
}
void lv_label_set_body_draw(lv_obj_t * label, bool en)
void
lv_label_set_body_draw(lv_obj_t *label, bool en)
{
uint32 argv[2] = {0};
uint32 argv[2] = { 0 };
argv[0] = (uint32)label;
argv[1] = en;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_BODY_DRAW);
}
void lv_label_set_anim_speed(lv_obj_t * label, uint16_t anim_speed)
void
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[1] = anim_speed;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_ANIM_SPEED);
}
void lv_label_set_text_sel_start(lv_obj_t * label, uint16_t index)
void
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[1] = index;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_TEXT_SEL_START);
}
void lv_label_set_text_sel_end(lv_obj_t * label, uint16_t index)
void
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[1] = index;
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;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_TEXT_LENGTH);
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[1] = (uint32)buffer;
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];
}
// TODO:
char * lv_label_get_text(const lv_obj_t * label)
// TODO:
char *
lv_label_get_text(const lv_obj_t *label)
{
return NULL;
}
lv_label_long_mode_t lv_label_get_long_mode(const lv_obj_t * label)
lv_label_long_mode_t
lv_label_get_long_mode(const lv_obj_t *label)
{
uint32 argv[1] = {0};
uint32 argv[1] = { 0 };
argv[0] = (uint32)label;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_LONG_MODE);
return (lv_label_long_mode_t)argv[0];
}
lv_label_align_t lv_label_get_align(const lv_obj_t * label)
lv_label_align_t
lv_label_get_align(const lv_obj_t *label)
{
uint32 argv[1] = {0};
uint32 argv[1] = { 0 };
argv[0] = (uint32)label;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_ALIGN);
return (lv_label_align_t)argv[0];
}
bool lv_label_get_recolor(const lv_obj_t * label)
bool
lv_label_get_recolor(const lv_obj_t *label)
{
uint32 argv[1] = {0};
uint32 argv[1] = { 0 };
argv[0] = (uint32)label;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_RECOLOR);
return (bool)argv[0];
}
bool lv_label_get_body_draw(const lv_obj_t * label)
bool
lv_label_get_body_draw(const lv_obj_t *label)
{
uint32 argv[1] = {0};
uint32 argv[1] = { 0 };
argv[0] = (uint32)label;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_BODY_DRAW);
return (bool)argv[0];
}
uint16_t lv_label_get_anim_speed(const lv_obj_t * label)
uint16_t
lv_label_get_anim_speed(const lv_obj_t *label)
{
uint32 argv[1] = {0};
uint32 argv[1] = { 0 };
argv[0] = (uint32)label;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_ANIM_SPEED);
return (uint16_t)argv[0];
}
void lv_label_get_letter_pos(const lv_obj_t * label, uint16_t index, lv_point_t * pos)
void
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[1] = index;
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];
}
uint16_t lv_label_get_letter_on(const lv_obj_t * label, lv_point_t * pos)
uint16_t
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[1] = pos->x;
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];
}
bool lv_label_is_char_under_pos(const lv_obj_t * label, lv_point_t * pos)
bool
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[1] = pos->x;
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];
}
uint16_t lv_label_get_text_sel_start(const lv_obj_t * label)
uint16_t
lv_label_get_text_sel_start(const lv_obj_t *label)
{
uint32 argv[1] = {0};
uint32 argv[1] = { 0 };
argv[0] = (uint32)label;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_TEXT_SEL_START);
return (uint16_t)argv[0];
}
uint16_t lv_label_get_text_sel_end(const lv_obj_t * label)
uint16_t
lv_label_get_text_sel_end(const lv_obj_t *label)
{
uint32 argv[1] = {0};
uint32 argv[1] = { 0 };
argv[0] = (uint32)label;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_TEXT_SEL_END);
return (uint16_t)argv[0];
}
void lv_label_ins_text(lv_obj_t * label, uint32_t pos, const char * txt)
void
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[1] = pos;
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);
}
void lv_label_cut_text(lv_obj_t * label, uint32_t pos, uint32_t cnt)
void
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[1] = pos;
argv[2] = cnt;
CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_CUT_TEXT);
}

View File

@ -3,19 +3,18 @@
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "wa-inc/lvgl/lvgl.h"
#include "gui_api.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)
lv_obj_t * lv_list_create(lv_obj_t * par, const lv_obj_t * copy)
lv_obj_t *
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[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);
//}
//
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 */
@ -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);
//}
//
//
//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);
//}
@ -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
//
//
//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);
//}
//#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
//}
//
//
//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);
//}
//
//
//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);
//}
//
//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);
//}
//
//
//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);
//}
//
//
//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);
//}
//
//
//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);
//}
//
//
//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);
//}
//
//
//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);
//}
//
//#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);
//}
@ -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
// return NULL;
//}
//
//
//void wgl_list_up(const wgl_obj_t list)
// void wgl_list_up(const wgl_obj_t 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);
//}
//
//
//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);
//}
//

View File

@ -3,19 +3,18 @@
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "wa-inc/lvgl/lvgl.h"
#include "gui_api.h"
#include <stdlib.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)
typedef struct _obj_evt_cb {
struct _obj_evt_cb *next;
lv_obj_t * obj;
lv_obj_t *obj;
lv_event_cb_t event_cb;
} obj_evt_cb_t;
@ -24,31 +23,36 @@ static obj_evt_cb_t *g_obj_evt_cb_list = NULL;
/* For lvgl compatible */
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;
CALL_OBJ_NATIVE_FUNC(OBJ_FUNC_ID_DEL);
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;
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;
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[1] = (uint32)base;
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);
}
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;
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;
}
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;
uint32 argv[1] = {0};
uint32 argv[1] = { 0 };
obj_evt_cb = g_obj_evt_cb_list;
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) {
obj_evt_cb->next = g_obj_evt_cb_list;
g_obj_evt_cb_list = obj_evt_cb;
} else {
}
else {
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);
}
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;

File diff suppressed because it is too large Load Diff

View File

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

View File

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

View File

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

View File

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

View File

@ -19,7 +19,8 @@ DEFINE_WGL_NATIVE_WRAPPER(lv_btn_create_wrapper)
wgl_native_get_arg(uint32, copy_obj_id);
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);
}
@ -131,33 +132,30 @@ DEFINE_WGL_NATIVE_WRAPPER(lv_btn_toggle_wrapper)
lv_btn_toggle(btn);
}
/* clang-format off */
static WGLNativeFuncDef btn_native_func_defs[] = {
{ 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_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_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_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_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_TOGGLE, lv_btn_get_toggle_wrapper, 1, true },
{ BTN_FUNC_ID_TOGGLE, lv_btn_toggle_wrapper, 1, true },
{ 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_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_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_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_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_TOGGLE, lv_btn_get_toggle_wrapper, 1, true },
{ BTN_FUNC_ID_TOGGLE, lv_btn_toggle_wrapper, 1, true },
};
/* clang-format on */
/*************** Native Interface to Wasm App ***********/
void
wasm_btn_native_call(wasm_exec_env_t exec_env,
int32 func_id, uint32 *argv, uint32 argc)
wasm_btn_native_call(wasm_exec_env_t exec_env, int32 func_id, uint32 *argv,
uint32 argc)
{
uint32 size = sizeof(btn_native_func_defs) / sizeof(WGLNativeFuncDef);
wgl_native_func_call(exec_env,
btn_native_func_defs,
size,
func_id,
argv,
wgl_native_func_call(exec_env, btn_native_func_defs, size, func_id, argv,
argc);
}

View File

@ -20,7 +20,8 @@ DEFINE_WGL_NATIVE_WRAPPER(lv_cb_create_wrapper)
wgl_native_get_arg(uint32, copy_obj_id);
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);
}
@ -63,7 +64,7 @@ DEFINE_WGL_NATIVE_WRAPPER(lv_cb_get_text_length_wrapper)
(void)exec_env;
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)
@ -89,24 +90,20 @@ DEFINE_WGL_NATIVE_WRAPPER(lv_cb_get_text_wrapper)
}
static WGLNativeFuncDef cb_native_func_defs[] = {
{ 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_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, lv_cb_get_text_wrapper, 3, true },
{ 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_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, lv_cb_get_text_wrapper, 3, true },
};
/*************** Native Interface to Wasm App ***********/
void
wasm_cb_native_call(wasm_exec_env_t exec_env,
int32 func_id, uint32 *argv, uint32 argc)
wasm_cb_native_call(wasm_exec_env_t exec_env, int32 func_id, uint32 *argv,
uint32 argc)
{
uint32 size = sizeof(cb_native_func_defs) / sizeof(WGLNativeFuncDef);
wgl_native_func_call(exec_env,
cb_native_func_defs,
size,
func_id,
argv,
wgl_native_func_call(exec_env, cb_native_func_defs, size, func_id, argv,
argc);
}

View File

@ -20,7 +20,8 @@ DEFINE_WGL_NATIVE_WRAPPER(lv_label_create_wrapper)
wgl_native_get_arg(uint32, copy_obj_id);
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);
}
@ -73,24 +74,22 @@ DEFINE_WGL_NATIVE_WRAPPER(lv_label_get_text_wrapper)
wgl_native_set_return(buffer_offset);
}
/* clang-format off */
static WGLNativeFuncDef label_native_func_defs[] = {
{ 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_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_CREATE, lv_label_create_wrapper, 2, false },
{ 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, lv_label_get_text_wrapper, 3, true },
};
/* clang-format on */
/*************** Native Interface to Wasm App ***********/
void
wasm_label_native_call(wasm_exec_env_t exec_env,
int32 func_id, uint32 *argv, uint32 argc)
wasm_label_native_call(wasm_exec_env_t exec_env, int32 func_id, uint32 *argv,
uint32 argc)
{
uint32 size = sizeof(label_native_func_defs) / sizeof(WGLNativeFuncDef);
wgl_native_func_call(exec_env,
label_native_func_defs,
size,
func_id,
argv,
wgl_native_func_call(exec_env, label_native_func_defs, size, func_id, argv,
argc);
}

View File

@ -20,7 +20,8 @@ DEFINE_WGL_NATIVE_WRAPPER(lv_list_create_wrapper)
wgl_native_get_arg(uint32, copy_obj_id);
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);
}
@ -49,7 +50,8 @@ DEFINE_WGL_NATIVE_WRAPPER(lv_list_add_btn_wrapper)
bh_assert(mod_id != ID_NONE);
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;
}
@ -57,21 +59,17 @@ DEFINE_WGL_NATIVE_WRAPPER(lv_list_add_btn_wrapper)
}
static WGLNativeFuncDef list_native_func_defs[] = {
{ 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_CREATE, lv_list_create_wrapper, 2, false },
{ LIST_FUNC_ID_ADD_BTN, lv_list_add_btn_wrapper, 3, true },
};
/*************** Native Interface to Wasm App ***********/
void
wasm_list_native_call(wasm_exec_env_t exec_env,
int32 func_id, uint32 *argv, uint32 argc)
wasm_list_native_call(wasm_exec_env_t exec_env, int32 func_id, uint32 *argv,
uint32 argc)
{
uint32 size = sizeof(list_native_func_defs) / sizeof(WGLNativeFuncDef);
wgl_native_func_call(exec_env,
list_native_func_defs,
size,
func_id,
argv,
wgl_native_func_call(exec_env, list_native_func_defs, size, func_id, argv,
argc);
}

View File

@ -10,16 +10,15 @@
#define THROW_EXC(msg) wasm_runtime_set_exception(module_inst, msg);
uint32 wgl_native_wigdet_create(int8 widget_type,
uint32 par_obj_id,
uint32 copy_obj_id,
wasm_module_inst_t module_inst)
uint32
wgl_native_wigdet_create(int8 widget_type, uint32 par_obj_id,
uint32 copy_obj_id, wasm_module_inst_t module_inst)
{
uint32 obj_id;
lv_obj_t *wigdet = NULL, *par = NULL, *copy = NULL;
uint32 mod_id;
//TODO: limit total widget number
// TODO: limit total widget number
/* validate the parent object id if not equal to 0 */
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;
}
void wgl_native_func_call(wasm_exec_env_t exec_env,
WGLNativeFuncDef *funcs,
uint32 size,
int32 func_id,
uint32 *argv,
uint32 argc)
void
wgl_native_func_call(wasm_exec_env_t exec_env, WGLNativeFuncDef *funcs,
uint32 size, 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;
wasm_module_inst_t module_inst = get_module_inst(exec_env);
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()
* with pointer length equals to 1. Here validate the argv
* 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;
while (func_def < func_def_end) {
if (func_def->func_id == func_id
&& (uint32)func_def->arg_num == argc) {
if (func_def->func_id == func_id && (uint32)func_def->arg_num == argc) {
uint64 argv_copy_buf[16], size;
uint64 *argv_copy = argv_copy_buf;
int i;
@ -96,7 +92,7 @@ void wgl_native_func_call(wasm_exec_env_t exec_env,
/* Init argv_copy */
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 */
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!");
}

View File

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

View File

@ -10,7 +10,6 @@
#include "wgl_native_utils.h"
#include "wgl.h"
typedef struct {
bh_list_link l;
@ -42,24 +41,24 @@ static korp_mutex task_handler_lock;
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];
wasm_function_inst_t func_on_object_event;
bh_assert(WIDGET_EVENT_WASM == bh_message_type(msg));
wasm_data *wasm_app_data = (wasm_data*)m_data->internal_data;
wasm_data *wasm_app_data = (wasm_data *)m_data->internal_data;
wasm_module_inst_t inst = wasm_app_data->wasm_module_inst;
object_event_t *object_event
= (object_event_t *)bh_message_payload(msg);
object_event_t *object_event = (object_event_t *)bh_message_payload(msg);
if (object_event == NULL)
return;
func_on_object_event = wasm_runtime_lookup_function(inst, "_on_widget_event",
"(i32i32)");
func_on_object_event =
wasm_runtime_lookup_function(inst, "_on_widget_event", "(i32i32)");
if (!func_on_object_event)
func_on_object_event = wasm_runtime_lookup_function(inst, "on_widget_event",
"(i32i32)");
func_on_object_event =
wasm_runtime_lookup_function(inst, "on_widget_event", "(i32i32)");
if (!func_on_object_event) {
printf("Cannot find function on_widget_event\n");
return;
@ -71,14 +70,14 @@ static void app_mgr_object_event_callback(module_data *m_data, bh_message_t msg)
2, argv)) {
const char *exception = wasm_runtime_get_exception(inst);
bh_assert(exception);
printf(":Got exception running wasm code: %s\n",
exception);
printf(":Got exception running wasm code: %s\n", exception);
wasm_runtime_clear_exception(inst);
return;
}
}
static void cleanup_object_list(uint32 module_id)
static void
cleanup_object_list(uint32 module_id)
{
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);
while (elem) {
/* delete the leaf node belongs to the module firstly */
if (module_id == elem->module_id &&
lv_obj_count_children(elem->obj) == 0) {
if (module_id == elem->module_id
&& lv_obj_count_children(elem->obj) == 0) {
object_node_t *next = (object_node_t *)bh_list_elem_next(elem);
found = true;
@ -98,7 +97,8 @@ static void cleanup_object_list(uint32 module_id)
bh_list_remove(&g_object_list, elem);
wasm_runtime_free(elem);
elem = next;
} else {
}
else {
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);
}
static bool init_object_event_callback_framework()
static bool
init_object_event_callback_framework()
{
if (!wasm_register_cleanup_callback(cleanup_object_list)) {
goto fail;
@ -127,7 +128,8 @@ fail:
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;
@ -141,7 +143,7 @@ bool wgl_native_validate_object(int32 obj_id, lv_obj_t **obj)
os_mutex_unlock(&g_object_list_mutex);
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);
@ -149,11 +151,12 @@ bool wgl_native_validate_object(int32 obj_id, lv_obj_t **obj)
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;
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)
return false;
@ -178,11 +181,12 @@ bool wgl_native_add_object(lv_obj_t *obj, uint32 module_id, uint32 *obj_id)
return true;
}
static void _obj_del_recursive(lv_obj_t *obj)
static void
_obj_del_recursive(lv_obj_t *obj)
{
object_node_t *elem;
lv_obj_t * i;
lv_obj_t * i_next;
lv_obj_t *i;
lv_obj_t *i_next;
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);
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);
}
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_next;
lv_obj_t *i;
lv_obj_t *i_next;
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);
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->event = event;
bh_post_msg(module->queue,
WIDGET_EVENT_WASM,
object_event,
bh_post_msg(module->queue, WIDGET_EVENT_WASM, object_event,
sizeof(*object_event));
}
static void internal_lv_obj_event_cb(lv_obj_t *obj, lv_event_t event)
static void
internal_lv_obj_event_cb(lv_obj_t *obj, lv_event_t event)
{
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);
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);
}
static void* lv_task_handler_thread_routine (void *arg)
static void *
lv_task_handler_thread_routine(void *arg)
{
os_mutex_lock(&task_handler_lock);
@ -287,7 +293,8 @@ static void* lv_task_handler_thread_routine (void *arg)
return NULL;
}
void wgl_init(void)
void
wgl_init(void)
{
korp_tid tid;
@ -306,13 +313,12 @@ void wgl_init(void)
init_object_event_callback_framework();
/* new a thread, call lv_task_handler periodically */
os_thread_create(&tid,
lv_task_handler_thread_routine,
NULL,
os_thread_create(&tid, lv_task_handler_thread_routine, NULL,
BH_APPLET_PRESERVED_STACK_SIZE);
}
void wgl_exit(void)
void
wgl_exit(void)
{
lv_task_handler_thread_run = false;
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 */
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;
}
@ -388,24 +395,20 @@ DEFINE_WGL_NATIVE_WRAPPER(lv_obj_set_event_cb_wrapper)
/* ------------------------------------------------------------------------- */
static WGLNativeFuncDef obj_native_func_defs[] = {
{ 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_CLEAN, lv_obj_clean_wrapper, 1, 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_DEL, lv_obj_del_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_ALIGN, lv_obj_align_wrapper, 5, true },
{ OBJ_FUNC_ID_SET_EVT_CB, lv_obj_set_event_cb_wrapper, 1, true },
};
/*************** Native Interface to Wasm App ***********/
void
wasm_obj_native_call(wasm_exec_env_t exec_env,
int32 func_id, uint32 *argv, uint32 argc)
wasm_obj_native_call(wasm_exec_env_t exec_env, int32 func_id, uint32 *argv,
uint32 argc)
{
uint32 size = sizeof(obj_native_func_defs) / sizeof(WGLNativeFuncDef);
wgl_native_func_call(exec_env,
obj_native_func_defs,
size,
func_id,
argv,
wgl_native_func_call(exec_env, obj_native_func_defs, size, func_id, argv,
argc);
}