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

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

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

View File

@ -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)
{
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++) {
@ -174,8 +185,8 @@ attr_container_find_attr(const attr_container_t *attr_cont, const char *key)
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)
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 */
@ -274,13 +288,15 @@ 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;
@ -290,7 +306,7 @@ static bool check_set_attr(attr_container_t **p_attr_cont, const char *key)
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");
@ -300,7 +316,8 @@ static bool check_set_attr(attr_container_t **p_attr_cont, const char *key)
return true;
}
bool attr_container_set_attr(attr_container_t **p_attr_cont, const char *key,
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;
@ -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");
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),
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((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,18 +426,18 @@ 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");
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),
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;
@ -434,51 +452,62 @@ 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,
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,
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,
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,
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,
bool
attr_container_set_uint16(attr_container_t **p_attr_cont, const char *key,
uint16_t value)
{
return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_UINT16, &value,
2);
}
bool attr_container_set_float(attr_container_t **p_attr_cont, const char *key,
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,
bool
attr_container_set_double(attr_container_t **p_attr_cont, const char *key,
double value)
{
return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_DOUBLE, &value,
8);
}
bool attr_container_set_bool(attr_container_t **p_attr_cont, const char *key,
bool
attr_container_set_bool(attr_container_t **p_attr_cont, const char *key,
bool value)
{
int8_t value1 = value ? 1 : 0;
@ -486,7 +515,8 @@ bool attr_container_set_bool(attr_container_t **p_attr_cont, const char *key,
1);
}
bool attr_container_set_string(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)
{
if (!value) {
@ -494,21 +524,22 @@ bool attr_container_set_string(attr_container_t **p_attr_cont, const char *key,
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;
@ -528,14 +559,15 @@ 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 {\
#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++; \
type = *(uint8_t *)addr++; \
switch (type) { \
case ATTR_TYPE_SHORT: \
case ATTR_TYPE_INT: \
@ -545,11 +577,12 @@ attr_container_get_attr(const attr_container_t *attr_cont, const char *key)
case ATTR_TYPE_FLOAT: \
case ATTR_TYPE_DOUBLE: \
case ATTR_TYPE_BOOLEAN: \
bh_memcpy_s(&val, sizeof(val.var_name), addr, 1 << (type & 3)); \
bh_memcpy_s(&val, sizeof(val.var_name), addr, \
1 << (type & 3)); \
break; \
case ATTR_TYPE_STRING: \
{ \
unsigned len= get_uint16(addr); \
unsigned len = get_uint16(addr); \
addr += 2; \
if (len > sizeof(val.var_name)) \
len = sizeof(val.var_name); \
@ -558,7 +591,7 @@ attr_container_get_attr(const attr_container_t *attr_cont, const char *key)
} \
case ATTR_TYPE_BYTEARRAY: \
{ \
unsigned len= get_uint32(addr); \
unsigned len = get_uint32(addr); \
addr += 4; \
if (len > sizeof(val.var_name)) \
len = sizeof(val.var_name); \
@ -572,55 +605,55 @@ attr_container_get_attr(const attr_container_t *attr_cont, const char *key)
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)
{
@ -636,7 +669,7 @@ 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:
@ -661,26 +694,26 @@ attr_container_get_as_bytearray(const attr_container_t *attr_cont,
}
*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,
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(
@ -690,14 +723,14 @@ bool attr_container_contain_key(const attr_container_t *attr_cont,
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;
@ -721,13 +755,14 @@ 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;
@ -737,11 +772,12 @@ bool attr_container_is_constant(const attr_container_t* attr_cont)
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;
@ -784,7 +820,8 @@ void attr_container_dump(const attr_container_t *attr_cont)
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));
attr_container_printf(", type: int64, value: 0x%llx\n",
(long long unsigned int)(value.j));
p += 8;
break;
case ATTR_TYPE_BYTE:
@ -831,4 +868,3 @@ void attr_container_dump(const attr_container_t *attr_cont)
attr_container_printf("\n");
}

View File

@ -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);
/**
@ -354,7 +354,7 @@ 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);
@ -367,7 +367,7 @@ 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);
@ -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

@ -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,12 +53,12 @@ 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);
@ -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);
@ -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,7 +471,7 @@ 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 */
while (*p != delimiter && remaining > 0 && value_len > 0) {

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

23
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,12 +35,14 @@ 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)
@ -48,11 +50,12 @@ uint16 ntohs(uint16 value);
#ifdef NDEBUG
#define bh_assert(v) (void)0
#else
#define bh_assert(v) do { \
#define bh_assert(v) \
do { \
if (!(v)) { \
int _count; \
printf("ASSERTION FAILED: %s, at %s, line %d",\
#v, __FILE__, __LINE__); \
printf("ASSERTION FAILED: %s, at %s, line %d", #v, __FILE__, \
__LINE__); \
_count = printf("\n"); \
printf("%d\n", _count / (_count - 1)); \
} \

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,
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,25 +192,28 @@ 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");
return;
@ -228,8 +237,7 @@ 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,
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;
@ -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,
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,
@ -52,13 +51,14 @@ typedef enum {
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,7 +43,8 @@ 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,
user_timer_t
api_timer_create(int interval, bool is_period, bool auto_start,
on_user_timer_update_f on_timer_update);
/**
@ -51,7 +52,8 @@ user_timer_t api_timer_create(int interval, bool is_period, bool auto_start,
*
* @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
/* TODO: use macro EXPORT_WASM_API() or EXPORT_WASM_API2() to
add functions to register. */
#include "base_lib.inl"
#include "base_lib.inl"
};
uint32 get_base_lib_export_apis(NativeSymbol **p_base_lib_apis)
uint32
get_base_lib_export_apis(NativeSymbol **p_base_lib_apis)
{
*p_base_lib_apis = extended_native_symbol_defs;
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,
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,12 +76,17 @@ 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
*/
@ -91,8 +96,10 @@ connection_interface_t connection_impl = {
._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 { \
#define FREE_CONNECTION(conn) \
do { \
if (conn->arg) \
wasm_runtime_free(conn->arg); \
wasm_runtime_free(conn); \
} while (0)
} 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;
@ -564,10 +588,9 @@ bool init_connection_framework()
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,6 +12,7 @@
#include "connection_lib.h"
/* clang-format off */
/*
* Platform implementation of connection library
*/
@ -21,3 +22,4 @@ connection_interface_t connection_impl = {
._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

@ -49,9 +49,8 @@ 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,
sensor_t
sensor_open(const char *name, int index, sensor_event_handler_f handler,
void *user_data);
/**
@ -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,12 +38,12 @@ 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)
@ -53,15 +53,17 @@ wasm_sensor_callback(void *client, uint32 sensor_id, void *user_data)
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,
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;
@ -134,62 +135,62 @@ char * wgl_label_get_text(lv_obj_t * label, char *buffer, int buffer_len)
}
// TODO:
char * lv_label_get_text(const lv_obj_t * label)
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;
}

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,6 +132,7 @@ 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 },
@ -144,20 +146,16 @@ static WGLNativeFuncDef btn_native_func_defs[] = {
{ 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)
@ -98,15 +99,11 @@ static WGLNativeFuncDef cb_native_func_defs[] = {
/*************** 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 },
};
/* 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;
}
@ -63,15 +65,11 @@ static WGLNativeFuncDef list_native_func_defs[] = {
/*************** 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,12 +15,13 @@ 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)
static void func_name(wasm_exec_env_t exec_env, uint64 *args, \
uint32 *args_ret)
enum {
WIDGET_TYPE_BTN,
@ -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;
}
@ -397,15 +404,11 @@ static WGLNativeFuncDef obj_native_func_defs[] = {
/*************** 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);
}

View File

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

View File

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

View File

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

View File

@ -21,4 +21,3 @@ extern "C" {
#endif
#endif

View File

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

View File

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

File diff suppressed because it is too large Load Diff

View File

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

View File

@ -20,25 +20,27 @@
#include "aot_export.h"
#endif
/* clang-format off */
#if WASM_ENABLE_INTERP != 0 || WASM_ENABLE_JIT != 0
/* Wasm bytecode file 4 version bytes */
static uint8 wasm_bytecode_version[4] = {
(uint8) 0x01,
(uint8) 0x00,
(uint8) 0x00,
(uint8) 0x00
(uint8)0x01,
(uint8)0x00,
(uint8)0x00,
(uint8)0x00
};
#endif
#if WASM_ENABLE_AOT != 0
/* Wasm aot file 4 version bytes */
static uint8 wasm_aot_version[4] = {
(uint8) 0x02,
(uint8) 0x00,
(uint8) 0x00,
(uint8) 0x00
(uint8)0x02,
(uint8)0x00,
(uint8)0x00,
(uint8)0x00
};
#endif
/* clang-format on */
static union {
int a;
@ -133,8 +135,7 @@ destroy_all_wasm_sections(wasm_section_list_t sections);
static void
destroy_part_wasm_sections(wasm_section_list_t *p_sections,
uint8 *section_types,
int section_cnt);
uint8 *section_types, int section_cnt);
#endif
#if WASM_ENABLE_AOT != 0
@ -142,8 +143,7 @@ static void
destroy_all_aot_sections(aot_section_list_t sections);
static void
destroy_part_aot_sections(aot_section_list_t *p_sections,
uint8 *section_types,
destroy_part_aot_sections(aot_section_list_t *p_sections, uint8 *section_types,
int section_cnt);
#endif
@ -152,8 +152,9 @@ int g_msg_type[Max_Msg_Callback] = { 0 };
message_type_handler_t g_msg_callbacks[Max_Msg_Callback] = { 0 };
#define Max_Cleanup_Callback 10
static resource_cleanup_handler_t
g_cleanup_callbacks[Max_Cleanup_Callback] = { 0 };
static resource_cleanup_handler_t g_cleanup_callbacks[Max_Cleanup_Callback] = {
0
};
module_interface wasm_app_module_interface = {
wasm_app_module_init,
@ -198,7 +199,6 @@ app_manager_lookup_function(const wasm_module_inst_t module_inst,
return func;
}
static void
app_instance_queue_callback(void *queue_msg, void *arg)
{
@ -207,27 +207,26 @@ app_instance_queue_callback(void *queue_msg, void *arg)
wasm_module_inst_t inst = (wasm_module_inst_t)arg;
module_data *m_data = app_manager_get_module_data(Module_WASM_App, inst);
wasm_data *wasm_app_data = (wasm_data*)m_data->internal_data;
wasm_data *wasm_app_data = (wasm_data *)m_data->internal_data;
int message_type = bh_message_type(queue_msg);
bh_assert(m_data);
if (message_type < BASE_EVENT_MAX) {
switch (message_type) {
case RESTFUL_REQUEST: {
case RESTFUL_REQUEST:
{
request_t *request = (request_t *)bh_message_payload(queue_msg);
int size;
char *buffer;
int32 buffer_offset;
app_manager_printf("App %s got request, url %s, action %d\n",
m_data->module_name,
request->url,
m_data->module_name, request->url,
request->action);
func_onRequest = app_manager_lookup_function(inst,
"_on_request",
"(i32i32)");
func_onRequest = app_manager_lookup_function(
inst, "_on_request", "(i32i32)");
if (!func_onRequest) {
app_manager_printf("Cannot find function onRequest\n");
break;
@ -237,12 +236,13 @@ app_instance_queue_callback(void *queue_msg, void *arg)
if (buffer == NULL)
break;
buffer_offset = wasm_runtime_module_dup_data(inst, buffer, size);
buffer_offset =
wasm_runtime_module_dup_data(inst, buffer, size);
if (buffer_offset == 0) {
const char *exception = wasm_runtime_get_exception(inst);
if (exception) {
app_manager_printf("Got exception running wasm code: %s\n",
exception);
app_manager_printf(
"Got exception running wasm code: %s\n", exception);
wasm_runtime_clear_exception(inst);
}
free_req_resp_packet(buffer);
@ -251,11 +251,11 @@ app_instance_queue_callback(void *queue_msg, void *arg)
free_req_resp_packet(buffer);
argv[0] = (uint32) buffer_offset;
argv[1] = (uint32) size;
argv[0] = (uint32)buffer_offset;
argv[1] = (uint32)size;
if (!wasm_runtime_call_wasm(wasm_app_data->exec_env, func_onRequest,
2, argv)) {
if (!wasm_runtime_call_wasm(wasm_app_data->exec_env,
func_onRequest, 2, argv)) {
const char *exception = wasm_runtime_get_exception(inst);
bh_assert(exception);
app_manager_printf("Got exception running wasm code: %s\n",
@ -269,9 +269,11 @@ app_instance_queue_callback(void *queue_msg, void *arg)
app_manager_printf("Wasm app process request success.\n");
break;
}
case RESTFUL_RESPONSE: {
case RESTFUL_RESPONSE:
{
wasm_function_inst_t func_onResponse;
response_t *response = (response_t *) bh_message_payload(queue_msg);
response_t *response =
(response_t *)bh_message_payload(queue_msg);
int size;
char *buffer;
int32 buffer_offset;
@ -279,8 +281,8 @@ app_instance_queue_callback(void *queue_msg, void *arg)
app_manager_printf("App %s got response_t,status %d\n",
m_data->module_name, response->status);
func_onResponse =
app_manager_lookup_function(inst, "_on_response", "(i32i32)");
func_onResponse = app_manager_lookup_function(
inst, "_on_response", "(i32i32)");
if (!func_onResponse) {
app_manager_printf("Cannot find function on_response\n");
break;
@ -290,12 +292,13 @@ app_instance_queue_callback(void *queue_msg, void *arg)
if (buffer == NULL)
break;
buffer_offset = wasm_runtime_module_dup_data(inst, buffer, size);
buffer_offset =
wasm_runtime_module_dup_data(inst, buffer, size);
if (buffer_offset == 0) {
const char *exception = wasm_runtime_get_exception(inst);
if (exception) {
app_manager_printf("Got exception running wasm code: %s\n",
exception);
app_manager_printf(
"Got exception running wasm code: %s\n", exception);
wasm_runtime_clear_exception(inst);
}
free_req_resp_packet(buffer);
@ -304,11 +307,11 @@ app_instance_queue_callback(void *queue_msg, void *arg)
free_req_resp_packet(buffer);
argv[0] = (uint32) buffer_offset;
argv[1] = (uint32) size;
argv[0] = (uint32)buffer_offset;
argv[1] = (uint32)size;
if (!wasm_runtime_call_wasm(wasm_app_data->exec_env, func_onResponse,
2, argv)) {
if (!wasm_runtime_call_wasm(wasm_app_data->exec_env,
func_onResponse, 2, argv)) {
const char *exception = wasm_runtime_get_exception(inst);
bh_assert(exception);
app_manager_printf("Got exception running wasm code: %s\n",
@ -322,59 +325,62 @@ app_instance_queue_callback(void *queue_msg, void *arg)
app_manager_printf("Wasm app process response success.\n");
break;
}
default: {
default:
{
for (int i = 0; i < Max_Msg_Callback; i++) {
if (g_msg_type[i] == message_type) {
g_msg_callbacks[i](m_data, queue_msg);
return;
}
}
app_manager_printf("Invalid message type of WASM app queue message.\n");
app_manager_printf(
"Invalid message type of WASM app queue message.\n");
break;
}
}
}
else {
switch (message_type) {
case TIMER_EVENT_WASM: {
case TIMER_EVENT_WASM:
{
unsigned int timer_id;
if (bh_message_payload(queue_msg)) {
/* Call Timer.callOnTimer() method */
func_onTimer =
app_manager_lookup_function(inst,
"_on_timer_callback",
"(i32)");
func_onTimer = app_manager_lookup_function(
inst, "_on_timer_callback", "(i32)");
if (!func_onTimer) {
app_manager_printf("Cannot find function _on_timer_callback\n");
app_manager_printf(
"Cannot find function _on_timer_callback\n");
break;
}
timer_id =
(unsigned int)(uintptr_t)bh_message_payload(queue_msg);
argv[0] = timer_id;
if (!wasm_runtime_call_wasm(wasm_app_data->exec_env, func_onTimer,
1, argv)) {
const char *exception = wasm_runtime_get_exception(inst);
if (!wasm_runtime_call_wasm(wasm_app_data->exec_env,
func_onTimer, 1, argv)) {
const char *exception =
wasm_runtime_get_exception(inst);
bh_assert(exception);
app_manager_printf("Got exception running wasm code: %s\n",
exception);
app_manager_printf(
"Got exception running wasm code: %s\n", exception);
wasm_runtime_clear_exception(inst);
}
}
break;
}
default: {
default:
{
for (int i = 0; i < Max_Msg_Callback; i++) {
if (g_msg_type[i] == message_type) {
g_msg_callbacks[i](m_data, queue_msg);
return;
}
}
app_manager_printf("Invalid message type of WASM app queue message.\n");
app_manager_printf(
"Invalid message type of WASM app queue message.\n");
break;
}
}
}
}
@ -421,14 +427,14 @@ wasm_app_prepare_wasi_dir(wasm_module_t module, const char *module_name,
#endif
/* WASM app thread main routine */
static void*
static void *
wasm_app_routine(void *arg)
{
wasm_function_inst_t func_onInit;
wasm_function_inst_t func_onDestroy;
module_data *m_data = (module_data *) arg;
wasm_data *wasm_app_data = (wasm_data*) m_data->internal_data;
module_data *m_data = (module_data *)arg;
wasm_data *wasm_app_data = (wasm_data *)m_data->internal_data;
wasm_module_inst_t inst = wasm_app_data->wasm_module_inst;
/* Set m_data to the VM managed instance's custom data */
@ -443,11 +449,12 @@ wasm_app_routine(void *arg)
which initializes the wasi envrionment. The "_start" function
will call "main" function */
if ((func_start = wasm_runtime_lookup_wasi_start_function(inst))) {
if (!wasm_runtime_call_wasm(wasm_app_data->exec_env, func_start,
0, NULL)) {
if (!wasm_runtime_call_wasm(wasm_app_data->exec_env, func_start, 0,
NULL)) {
const char *exception = wasm_runtime_get_exception(inst);
bh_assert(exception);
app_manager_printf("Got exception running wasi start function: %s\n",
app_manager_printf(
"Got exception running wasi start function: %s\n",
exception);
wasm_runtime_clear_exception(inst);
goto fail1;
@ -465,12 +472,11 @@ wasm_app_routine(void *arg)
goto fail1;
}
if (!wasm_runtime_call_wasm(wasm_app_data->exec_env, func_onInit,
0, NULL)) {
if (!wasm_runtime_call_wasm(wasm_app_data->exec_env, func_onInit, 0,
NULL)) {
const char *exception = wasm_runtime_get_exception(inst);
bh_assert(exception);
app_manager_printf("Got exception running WASM code: %s\n",
exception);
app_manager_printf("Got exception running WASM code: %s\n", exception);
wasm_runtime_clear_exception(inst);
/* call on_destroy() in case some resources are opened in on_init()
* and then exception thrown */
@ -486,7 +492,8 @@ fail2:
/* Call WASM app onDestroy() method if there is */
func_onDestroy = app_manager_lookup_function(inst, "_on_destroy", "()");
if (func_onDestroy)
wasm_runtime_call_wasm(wasm_app_data->exec_env, func_onDestroy, 0, NULL);
wasm_runtime_call_wasm(wasm_app_data->exec_env, func_onDestroy, 0,
NULL);
fail1:
@ -497,7 +504,7 @@ static void
cleanup_app_resource(module_data *m_data)
{
int i;
wasm_data *wasm_app_data = (wasm_data*) m_data->internal_data;
wasm_data *wasm_app_data = (wasm_data *)m_data->internal_data;
bool is_bytecode = wasm_app_data->is_bytecode;
am_cleanup_registeration(m_data->id);
@ -517,7 +524,8 @@ cleanup_app_resource(module_data *m_data)
* or text section of aot file) from app file's section list. */
if (is_bytecode) {
#if WASM_ENABLE_INTERP != 0 || WASM_ENABLE_JIT != 0
destroy_all_wasm_sections((wasm_section_list_t)(wasm_app_data->sections));
destroy_all_wasm_sections(
(wasm_section_list_t)(wasm_app_data->sections));
#else
bh_assert(0);
#endif
@ -572,7 +580,7 @@ wasm_app_module_init(void)
#define MAX_INT_STR_LEN 11
static bool
wasm_app_module_install(request_t * msg)
wasm_app_module_install(request_t *msg)
{
unsigned int m_data_size, heap_size, stack_size;
unsigned int timeout, timers, err_size;
@ -598,7 +606,8 @@ wasm_app_module_install(request_t * msg)
/* Check payload */
if (!msg->payload || msg->payload_len == 0) {
SEND_ERR_RESPONSE(msg->mid, "Install WASM app failed: invalid wasm file.");
SEND_ERR_RESPONSE(msg->mid,
"Install WASM app failed: invalid wasm file.");
return false;
}
@ -610,7 +619,8 @@ wasm_app_module_install(request_t * msg)
properties_offset = check_url_start(msg->url, strlen(msg->url), "/applet");
bh_assert(properties_offset > 0);
if (properties_offset <= 0) {
SEND_ERR_RESPONSE(msg->mid, "Install WASM app failed: invalid app name.");
SEND_ERR_RESPONSE(msg->mid,
"Install WASM app failed: invalid app name.");
goto fail;
}
@ -619,12 +629,14 @@ wasm_app_module_install(request_t * msg)
sizeof(m_name) - 1, '&');
if (strlen(m_name) == 0) {
SEND_ERR_RESPONSE(msg->mid, "Install WASM app failed: invalid app name.");
SEND_ERR_RESPONSE(msg->mid,
"Install WASM app failed: invalid app name.");
goto fail;
}
if (app_manager_lookup_module_data(m_name)) {
SEND_ERR_RESPONSE(msg->mid, "Install WASM app failed: app already installed.");
SEND_ERR_RESPONSE(msg->mid,
"Install WASM app failed: app already installed.");
goto fail;
}
@ -646,6 +658,7 @@ wasm_app_module_install(request_t * msg)
case Wasm_Module_AoT:
{
wasm_aot_file_t *aot_file;
/* clang-format off */
/* Sections to be released after loading */
uint8 sections1[] = {
AOT_SECTION_TYPE_TARGET_INFO,
@ -656,6 +669,7 @@ wasm_app_module_install(request_t * msg)
AOT_SECTION_TYPE_SIGANATURE,
AOT_SECTION_TYPE_CUSTOM,
};
/* clang-format on */
aot_file = &wasm_app_file->u.aot;
@ -669,26 +683,24 @@ wasm_app_module_install(request_t * msg)
goto fail;
}
/* Destroy useless sections from list after load */
destroy_part_aot_sections(&aot_file->sections,
sections1,
destroy_part_aot_sections(&aot_file->sections, sections1,
sizeof(sections1) / sizeof(uint8));
#if WASM_ENABLE_LIBC_WASI != 0
if (!wasm_app_prepare_wasi_dir(module, m_name,
wasi_dir_buf, sizeof(wasi_dir_buf))) {
SEND_ERR_RESPONSE(msg->mid,
if (!wasm_app_prepare_wasi_dir(module, m_name, wasi_dir_buf,
sizeof(wasi_dir_buf))) {
SEND_ERR_RESPONSE(
msg->mid,
"Install WASM app failed: prepare wasi env failed.");
goto fail;
}
wasm_runtime_set_wasi_args(module,
wasi_dir_list, 1,
NULL, 0,
NULL, 0,
NULL, 0);
wasm_runtime_set_wasi_args(module, wasi_dir_list, 1, NULL, 0, NULL,
0, NULL, 0);
#endif
/* Instantiate the AOT module */
inst = wasm_runtime_instantiate(module, 0, heap_size, err, err_size);
inst =
wasm_runtime_instantiate(module, 0, heap_size, err, err_size);
if (!inst) {
snprintf(err_resp, sizeof(err_resp),
"Install WASM app failed: %s", err);
@ -726,8 +738,8 @@ wasm_app_module_install(request_t * msg)
bytecode_file = &wasm_app_file->u.bytecode;
/* Load wasm module from sections */
module = wasm_runtime_load_from_sections(bytecode_file->sections, false,
err, err_size);
module = wasm_runtime_load_from_sections(bytecode_file->sections,
false, err, err_size);
if (!module) {
snprintf(err_resp, sizeof(err_resp),
"Install WASM app failed: %s", err);
@ -736,26 +748,24 @@ wasm_app_module_install(request_t * msg)
}
/* Destroy useless sections from list after load */
destroy_part_wasm_sections(&bytecode_file->sections,
sections1,
destroy_part_wasm_sections(&bytecode_file->sections, sections1,
sizeof(sections1) / sizeof(uint8));
#if WASM_ENABLE_LIBC_WASI != 0
if (!wasm_app_prepare_wasi_dir(module, m_name,
wasi_dir_buf, sizeof(wasi_dir_buf))) {
SEND_ERR_RESPONSE(msg->mid,
if (!wasm_app_prepare_wasi_dir(module, m_name, wasi_dir_buf,
sizeof(wasi_dir_buf))) {
SEND_ERR_RESPONSE(
msg->mid,
"Install WASM app failed: prepare wasi env failed.");
goto fail;
}
wasm_runtime_set_wasi_args(module,
wasi_dir_list, 1,
NULL, 0,
NULL, 0,
NULL, 0);
wasm_runtime_set_wasi_args(module, wasi_dir_list, 1, NULL, 0, NULL,
0, NULL, 0);
#endif
/* Instantiate the wasm module */
inst = wasm_runtime_instantiate(module, 0, heap_size, err, err_size);
inst =
wasm_runtime_instantiate(module, 0, heap_size, err, err_size);
if (!inst) {
snprintf(err_resp, sizeof(err_resp),
"Install WASM app failed: %s", err);
@ -764,14 +774,14 @@ wasm_app_module_install(request_t * msg)
}
/* Destroy useless sections from list after instantiate */
destroy_part_wasm_sections(&bytecode_file->sections,
sections2,
destroy_part_wasm_sections(&bytecode_file->sections, sections2,
sizeof(sections2) / sizeof(uint8));
break;
}
#endif /* endof WASM_ENALBE_INTERP != 0 || WASM_ENABLE_JIT != 0 */
default:
SEND_ERR_RESPONSE(msg->mid,
SEND_ERR_RESPONSE(
msg->mid,
"Install WASM app failed: invalid wasm package type.");
goto fail;
}
@ -781,14 +791,15 @@ wasm_app_module_install(request_t * msg)
m_data_size = align_uint(m_data_size, 4);
m_data = APP_MGR_MALLOC(m_data_size + sizeof(wasm_data));
if (!m_data) {
SEND_ERR_RESPONSE(msg->mid, "Install WASM app failed: allocate memory failed.");
SEND_ERR_RESPONSE(msg->mid,
"Install WASM app failed: allocate memory failed.");
goto fail;
}
memset(m_data, 0, m_data_size + sizeof(wasm_data));
m_data->module_type = Module_WASM_App;
m_data->internal_data = (uint8*) m_data + m_data_size;
wasm_app_data = (wasm_data*) m_data->internal_data;
m_data->internal_data = (uint8 *)m_data + m_data_size;
wasm_app_data = (wasm_data *)m_data->internal_data;
wasm_app_data->wasm_module_inst = inst;
wasm_app_data->wasm_module = module;
wasm_app_data->m_data = m_data;
@ -803,7 +814,8 @@ wasm_app_module_install(request_t * msg)
if (!(wasm_app_data->exec_env = exec_env =
wasm_runtime_create_exec_env(inst, DEFAULT_WASM_STACK_SIZE))) {
SEND_ERR_RESPONSE(msg->mid, "Install WASM app failed: create exec env failed.");
SEND_ERR_RESPONSE(msg->mid,
"Install WASM app failed: create exec env failed.");
goto fail;
}
@ -821,7 +833,8 @@ wasm_app_module_install(request_t * msg)
/* Set module data - create queue */
m_data->queue = bh_queue_create();
if (!m_data->queue) {
SEND_ERR_RESPONSE(msg->mid, "Install WASM app failed: create app queue failed.");
SEND_ERR_RESPONSE(msg->mid,
"Install WASM app failed: create app queue failed.");
goto fail;
}
@ -850,7 +863,8 @@ wasm_app_module_install(request_t * msg)
/* Initialize watchdog timer */
if (!watchdog_timer_init(m_data)) {
SEND_ERR_RESPONSE(msg->mid,
SEND_ERR_RESPONSE(
msg->mid,
"Install WASM app failed: create app watchdog timer failed.");
goto fail;
}
@ -861,7 +875,8 @@ wasm_app_module_install(request_t * msg)
#endif
/* Create WASM app thread. */
if (os_thread_create(&wasm_app_data->thread_id, wasm_app_routine,
(void*) m_data, stack_size) != 0) {
(void *)m_data, stack_size)
!= 0) {
module_data_list_remove(m_data);
SEND_ERR_RESPONSE(msg->mid,
"Install WASM app failed: create app thread failed.");
@ -933,7 +948,8 @@ wasm_app_module_uninstall(request_t *msg)
sizeof(m_name) - 1, '&');
if (strlen(m_name) == 0) {
SEND_ERR_RESPONSE(msg->mid, "Uninstall WASM app failed: invalid app name.");
SEND_ERR_RESPONSE(msg->mid,
"Uninstall WASM app failed: invalid app name.");
return false;
}
@ -944,12 +960,14 @@ wasm_app_module_uninstall(request_t *msg)
}
if (m_data->module_type != Module_WASM_App) {
SEND_ERR_RESPONSE(msg->mid, "Uninstall WASM app failed: invalid module type.");
SEND_ERR_RESPONSE(msg->mid,
"Uninstall WASM app failed: invalid module type.");
return false;
}
if (m_data->wd_timer.is_interrupting) {
SEND_ERR_RESPONSE(msg->mid,
SEND_ERR_RESPONSE(
msg->mid,
"Uninstall WASM app failed: app is being interrupted by watchdog.");
return false;
}
@ -958,7 +976,7 @@ wasm_app_module_uninstall(request_t *msg)
bh_queue_exit_loop_run(m_data->queue);
/* Wait for wasm app thread to exit */
wasm_app_data = (wasm_data*) m_data->internal_data;
wasm_app_data = (wasm_data *)m_data->internal_data;
os_thread_join(wasm_app_data->thread_id, NULL);
cleanup_app_resource(m_data);
@ -984,7 +1002,7 @@ wasm_app_module_handle_host_url(void *queue_msg)
return false;
}
static module_data*
static module_data *
wasm_app_module_get_module_data(void *inst)
{
wasm_module_inst_t module_inst = (wasm_module_inst_t)inst;
@ -1019,7 +1037,8 @@ wasm_register_msg_callback(int message_type,
else if (freeslot != -1) {
g_msg_callbacks[freeslot] = message_handler;
g_msg_type[freeslot] = message_type;
} else
}
else
return false;
return true;
@ -1040,7 +1059,8 @@ wasm_register_cleanup_callback(resource_cleanup_handler_t handler)
return false;
}
#define RECV_INTEGER(value, next_phase) do { \
#define RECV_INTEGER(value, next_phase) \
do { \
uint8 *p = (uint8 *)&value; \
p[recv_ctx.size_in_phase++] = ch; \
if (recv_ctx.size_in_phase == sizeof(value)) { \
@ -1051,7 +1071,7 @@ wasm_register_cleanup_callback(resource_cleanup_handler_t handler)
recv_ctx.phase = next_phase; \
recv_ctx.size_in_phase = 0; \
} \
} while(0)
} while (0)
/* return:
* 1: whole wasm app arrived
@ -1059,8 +1079,7 @@ wasm_register_cleanup_callback(resource_cleanup_handler_t handler)
* -1: fail to process the byte arrived, e.g. allocate memory fail
*/
static bool
wasm_app_module_on_install_request_byte_arrive(uint8 ch,
int request_total_size,
wasm_app_module_on_install_request_byte_arrive(uint8 ch, int request_total_size,
int *received_size)
{
uint8 *p;
@ -1101,11 +1120,11 @@ wasm_app_module_on_install_request_byte_arrive(uint8 ch,
return true;
}
else if (recv_ctx.phase == Phase_Req_Url_Len) {
p = (uint8*)&recv_ctx.message.request_url_len;
p = (uint8 *)&recv_ctx.message.request_url_len;
p[recv_ctx.size_in_phase++] = ch;
if (recv_ctx.size_in_phase ==
sizeof(recv_ctx.message.request_url_len)) {
if (recv_ctx.size_in_phase
== sizeof(recv_ctx.message.request_url_len)) {
recv_ctx.message.request_url_len =
ntohs(recv_ctx.message.request_url_len);
recv_ctx.message.request_url =
@ -1138,12 +1157,11 @@ wasm_app_module_on_install_request_byte_arrive(uint8 ch,
}
else if (recv_ctx.phase == Phase_App_Magic) {
/* start to receive wasm app magic: bytecode or aot */
p = (uint8*)&recv_ctx.message.app_file_magic;
p = (uint8 *)&recv_ctx.message.app_file_magic;
p[recv_ctx.size_in_phase++] = ch;
if (recv_ctx.size_in_phase ==
sizeof(recv_ctx.message.app_file_magic)) {
if (recv_ctx.size_in_phase == sizeof(recv_ctx.message.app_file_magic)) {
magic = recv_ctx.message.app_file_magic;
package_type = get_package_type((uint8 *)&magic, sizeof(magic) + 1);
switch (package_type) {
@ -1174,7 +1192,7 @@ wasm_app_module_on_install_request_byte_arrive(uint8 ch,
}
#if WASM_ENABLE_INTERP != 0 || WASM_ENABLE_JIT != 0
else if (recv_ctx.phase == Phase_Wasm_Version) {
p = (uint8*)&recv_ctx.message.app_file.u.bytecode.version;
p = (uint8 *)&recv_ctx.message.app_file.u.bytecode.version;
if (ch == wasm_bytecode_version[recv_ctx.size_in_phase])
p[recv_ctx.size_in_phase++] = ch;
@ -1185,8 +1203,8 @@ wasm_app_module_on_install_request_byte_arrive(uint8 ch,
goto fail;
}
if (recv_ctx.size_in_phase ==
sizeof(recv_ctx.message.app_file.u.bytecode.version)) {
if (recv_ctx.size_in_phase
== sizeof(recv_ctx.message.app_file.u.bytecode.version)) {
recv_ctx.phase = Phase_Wasm_Section_Type;
recv_ctx.size_in_phase = 0;
}
@ -1201,8 +1219,8 @@ wasm_app_module_on_install_request_byte_arrive(uint8 ch,
#endif
if (section_type <= section_type_max) {
wasm_section_t *new_section;
if (!(new_section = (wasm_section_t *)
APP_MGR_MALLOC(sizeof(wasm_section_t)))) {
if (!(new_section = (wasm_section_t *)APP_MGR_MALLOC(
sizeof(wasm_section_t)))) {
app_manager_printf("Allocate memory failed!\n");
SEND_ERR_RESPONSE(recv_ctx.message.request_mid,
"Install WASM app failed: "
@ -1219,7 +1237,8 @@ wasm_app_module_on_install_request_byte_arrive(uint8 ch,
recv_ctx.message.app_file.u.bytecode.section_end = new_section;
}
else {
recv_ctx.message.app_file.u.bytecode.section_end->next = new_section;
recv_ctx.message.app_file.u.bytecode.section_end->next =
new_section;
recv_ctx.message.app_file.u.bytecode.section_end = new_section;
}
@ -1241,7 +1260,8 @@ wasm_app_module_on_install_request_byte_arrive(uint8 ch,
}
else if (recv_ctx.phase == Phase_Wasm_Section_Size) {
/* the last section is the current receiving one */
wasm_section_t *section = recv_ctx.message.app_file.u.bytecode.section_end;
wasm_section_t *section =
recv_ctx.message.app_file.u.bytecode.section_end;
uint32 byte;
bh_assert(section);
@ -1252,8 +1272,8 @@ wasm_app_module_on_install_request_byte_arrive(uint8 ch,
((byte & 0x7f) << recv_ctx.size_in_phase * 7);
recv_ctx.size_in_phase++;
/* check leab128 overflow for uint32 value */
if (recv_ctx.size_in_phase >
(sizeof(section->section_body_size) * 8 + 7 - 1) / 7) {
if (recv_ctx.size_in_phase
> (sizeof(section->section_body_size) * 8 + 7 - 1) / 7) {
app_manager_printf("LEB overflow when parsing section size\n");
SEND_ERR_RESPONSE(recv_ctx.message.request_mid,
"Install WASM app failed: "
@ -1263,9 +1283,11 @@ wasm_app_module_on_install_request_byte_arrive(uint8 ch,
if ((byte & 0x80) == 0) {
/* leb128 encoded section size parsed done */
if (!(section->section_body = APP_MGR_MALLOC(section->section_body_size))) {
if (!(section->section_body =
APP_MGR_MALLOC(section->section_body_size))) {
app_manager_printf("Allocate memory failed!\n");
SEND_ERR_RESPONSE(recv_ctx.message.request_mid,
SEND_ERR_RESPONSE(
recv_ctx.message.request_mid,
"Install WASM app failed: allocate memory failed");
goto fail;
}
@ -1277,7 +1299,8 @@ wasm_app_module_on_install_request_byte_arrive(uint8 ch,
}
else if (recv_ctx.phase == Phase_Wasm_Section_Content) {
/* the last section is the current receiving one */
wasm_section_t *section = recv_ctx.message.app_file.u.bytecode.section_end;
wasm_section_t *section =
recv_ctx.message.app_file.u.bytecode.section_end;
bh_assert(section);
@ -1317,7 +1340,7 @@ wasm_app_module_on_install_request_byte_arrive(uint8 ch,
#endif /* end of WASM_ENABLE_INTERP != 0 || WASM_ENABLE_JIT != 0 */
#if WASM_ENABLE_AOT != 0
else if (recv_ctx.phase == Phase_AOT_Version) {
p = (uint8*)&recv_ctx.message.app_file.u.aot.version;
p = (uint8 *)&recv_ctx.message.app_file.u.aot.version;
if (ch == wasm_aot_version[recv_ctx.size_in_phase])
p[recv_ctx.size_in_phase++] = ch;
@ -1328,8 +1351,8 @@ wasm_app_module_on_install_request_byte_arrive(uint8 ch,
goto fail;
}
if (recv_ctx.size_in_phase ==
sizeof(recv_ctx.message.app_file.u.aot.version)) {
if (recv_ctx.size_in_phase
== sizeof(recv_ctx.message.app_file.u.aot.version)) {
recv_ctx.phase = Phase_AOT_Section_ID;
recv_ctx.size_in_phase = 0;
}
@ -1337,17 +1360,17 @@ wasm_app_module_on_install_request_byte_arrive(uint8 ch,
}
else if (recv_ctx.phase == Phase_AOT_Section_ID) {
aot_section_t *cur_section;
uint32 aot_file_cur_offset = recv_ctx.total_received_size - 1 -
18 /* Request fixed part */ -
recv_ctx.message.request_url_len;
uint32 aot_file_cur_offset =
recv_ctx.total_received_size - 1
- 18 /* Request fixed part */ - recv_ctx.message.request_url_len;
if (recv_ctx.size_in_phase == 0) {
/* Skip paddings */
if (aot_file_cur_offset % 4)
return true;
if (!(cur_section = (aot_section_t *)
APP_MGR_MALLOC(sizeof(aot_section_t)))) {
if (!(cur_section =
(aot_section_t *)APP_MGR_MALLOC(sizeof(aot_section_t)))) {
app_manager_printf("Allocate memory failed!\n");
SEND_ERR_RESPONSE(recv_ctx.message.request_mid,
"Install WASM app failed: "
@ -1365,7 +1388,8 @@ wasm_app_module_on_install_request_byte_arrive(uint8 ch,
recv_ctx.message.app_file.u.aot.section_end->next = cur_section;
recv_ctx.message.app_file.u.aot.section_end = cur_section;
}
} else {
}
else {
cur_section = recv_ctx.message.app_file.u.aot.section_end;
bh_assert(cur_section);
}
@ -1401,7 +1425,7 @@ wasm_app_module_on_install_request_byte_arrive(uint8 ch,
aot_section_t *section = recv_ctx.message.app_file.u.aot.section_end;
bh_assert(section);
p = (uint8*)&section->section_body_size;
p = (uint8 *)&section->section_body_size;
p[recv_ctx.size_in_phase++] = ch;
if (recv_ctx.size_in_phase == sizeof(section->section_body_size)) {
/* Notes: integers are always little endian encoded in AOT file */
@ -1413,9 +1437,10 @@ wasm_app_module_on_install_request_byte_arrive(uint8 ch,
int map_prot =
MMAP_PROT_READ | MMAP_PROT_WRITE | MMAP_PROT_EXEC;
#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
|| defined(BUILD_TARGET_RISCV64_LP64D) || defined(BUILD_TARGET_RISCV64_LP64)
/* aot code and data in x86_64 must be in range 0 to 2G due to
relocation for R_X86_64_32/32S/PC32 */
|| defined(BUILD_TARGET_RISCV64_LP64D) \
|| defined(BUILD_TARGET_RISCV64_LP64)
/* aot code and data in x86_64 must be in range 0 to 2G due
to relocation for R_X86_64_32/32S/PC32 */
int map_flags = MMAP_MAP_32BIT;
#else
int map_flags = MMAP_MAP_NONE;
@ -1425,9 +1450,10 @@ wasm_app_module_on_install_request_byte_arrive(uint8 ch,
total_size = (total_size + 3) & ~((uint64)3);
if (total_size >= UINT32_MAX
|| !(section->section_body =
os_mmap(NULL, (uint32)total_size,
map_prot, map_flags))) {
app_manager_printf("Allocate executable memory failed!\n");
os_mmap(NULL, (uint32)total_size, map_prot,
map_flags))) {
app_manager_printf(
"Allocate executable memory failed!\n");
SEND_ERR_RESPONSE(recv_ctx.message.request_mid,
"Install WASM app failed: "
"allocate memory failed");
@ -1466,8 +1492,8 @@ wasm_app_module_on_install_request_byte_arrive(uint8 ch,
if (recv_ctx.size_in_phase == section->section_body_size) {
if (section->section_type == AOT_SECTION_TYPE_TEXT) {
uint32 total_size = section->section_body_size
+ aot_get_plt_table_size();
uint32 total_size =
section->section_body_size + aot_get_plt_table_size();
total_size = (total_size + 3) & ~3;
if (total_size > section->section_body_size) {
memset(section->section_body + section->section_body_size,
@ -1514,7 +1540,8 @@ fail:
switch (package_type) {
#if WASM_ENABLE_INTERP != 0 || WASM_ENABLE_JIT != 0
case Wasm_Module_Bytecode:
destroy_all_wasm_sections(recv_ctx.message.app_file.u.bytecode.sections);
destroy_all_wasm_sections(
recv_ctx.message.app_file.u.bytecode.sections);
break;
#endif
#if WASM_ENABLE_AOT != 0
@ -1541,7 +1568,7 @@ module_wasm_app_handle_install_msg(install_wasm_app_msg_t *message)
request_t *request = NULL;
bh_message_t msg;
request = (request_t *) APP_MGR_MALLOC(sizeof(request_t));
request = (request_t *)APP_MGR_MALLOC(sizeof(request_t));
if (request == NULL)
return false;
@ -1561,8 +1588,8 @@ module_wasm_app_handle_install_msg(install_wasm_app_msg_t *message)
/* Request payload is set to wasm_app_file_t struct,
* but not whole app buffer */
bh_memcpy_s(request->payload, request->payload_len,
&message->app_file, request->payload_len);
bh_memcpy_s(request->payload, request->payload_len, &message->app_file,
request->payload_len);
/* Since it's a wasm app install request, so directly post to app-mgr's
* queue. The benefit is that section list can be freed when the msg
@ -1595,8 +1622,7 @@ destroy_all_wasm_sections(wasm_section_list_t sections)
static void
destroy_part_wasm_sections(wasm_section_list_t *p_sections,
uint8 *section_types,
int section_cnt)
uint8 *section_types, int section_cnt)
{
int i;
for (i = 0; i < section_cnt; i++) {
@ -1643,10 +1669,8 @@ destroy_all_aot_sections(aot_section_list_t sections)
}
}
static void
destroy_part_aot_sections(aot_section_list_t *p_sections,
uint8 *section_types,
destroy_part_aot_sections(aot_section_list_t *p_sections, uint8 *section_types,
int section_cnt)
{
int i;
@ -1701,4 +1725,3 @@ wasm_get_wasi_root_dir()
return wasi_root_dir;
}
#endif

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -12,10 +12,11 @@
static bh_queue *watchdog_queue;
#ifdef WATCHDOG_ENABLED /* TODO */
static void watchdog_timer_callback(void *timer)
static void
watchdog_timer_callback(void *timer)
{
watchdog_timer *wd_timer = app_manager_get_wd_timer_from_timer_handle(
timer);
watchdog_timer *wd_timer =
app_manager_get_wd_timer_from_timer_handle(timer);
watchdog_timer_stop(wd_timer);
@ -33,7 +34,8 @@ static void watchdog_timer_callback(void *timer)
}
#endif
bool watchdog_timer_init(module_data *m_data)
bool
watchdog_timer_init(module_data *m_data)
{
#ifdef WATCHDOG_ENABLED /* TODO */
watchdog_timer *wd_timer = &m_data->wd_timer;
@ -54,7 +56,8 @@ bool watchdog_timer_init(module_data *m_data)
return true;
}
void watchdog_timer_destroy(watchdog_timer *wd_timer)
void
watchdog_timer_destroy(watchdog_timer *wd_timer)
{
#ifdef WATCHDOG_ENABLED /* TODO */
app_manager_timer_destroy(wd_timer->timer_handle);
@ -62,7 +65,8 @@ void watchdog_timer_destroy(watchdog_timer *wd_timer)
#endif
}
void watchdog_timer_start(watchdog_timer *wd_timer)
void
watchdog_timer_start(watchdog_timer *wd_timer)
{
os_mutex_lock(&wd_timer->lock);
@ -74,16 +78,18 @@ void watchdog_timer_start(watchdog_timer *wd_timer)
os_mutex_unlock(&wd_timer->lock);
}
void watchdog_timer_stop(watchdog_timer *wd_timer)
void
watchdog_timer_stop(watchdog_timer *wd_timer)
{
app_manager_timer_stop(wd_timer->timer_handle);
}
#ifdef WATCHDOG_ENABLED /* TODO */
static void watchdog_queue_callback(void *queue_msg)
static void
watchdog_queue_callback(void *queue_msg)
{
if (bh_message_type(queue_msg) == WD_TIMEOUT) {
module_data *m_data = (module_data *) bh_message_payload(queue_msg);
module_data *m_data = (module_data *)bh_message_payload(queue_msg);
if (g_module_interfaces[m_data->module_type]
&& g_module_interfaces[m_data->module_type]->module_watchdog_kill) {
g_module_interfaces[m_data->module_type]->module_watchdog_kill(
@ -95,18 +101,19 @@ static void watchdog_queue_callback(void *queue_msg)
#endif
#ifdef WATCHDOG_ENABLED /* TODO */
static void*
static void *
watchdog_thread_routine(void *arg)
{
/* Enter loop run */
bh_queue_enter_loop_run(watchdog_queue, watchdog_queue_callback);
(void) arg;
(void)arg;
return NULL;
}
#endif
bool watchdog_startup()
bool
watchdog_startup()
{
if (!(watchdog_queue = bh_queue_create())) {
app_manager_printf(
@ -125,7 +132,8 @@ bool watchdog_startup()
return true;
}
void watchdog_destroy()
void
watchdog_destroy()
{
bh_queue_exit_loop_run(watchdog_queue);
bh_queue_destroy(watchdog_queue);

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

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